blob: 02ea5cf4b1e52e97885ce79e0b5ee71be38cd8a7 [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.
Avi Kivity221d0592010-05-23 18:37:00 +03007 * Copyright 2010 Red Hat, Inc. and/or its affilates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08008 *
9 * Authors:
10 * Yaniv Kamay <yaniv@qumranet.com>
11 * Avi Kivity <avi@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
Avi Kivityedf88412007-12-16 11:02:48 +020017#include <linux/kvm_host.h>
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030021#include "kvm_cache_regs.h"
Gleb Natapovfe4c7b12009-03-23 11:23:18 +020022#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020025#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/vmalloc.h>
27#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040028#include <linux/sched.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030029#include <linux/ftrace_event.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080031
Joerg Roedel67ec6602010-05-17 14:43:35 +020032#include <asm/tlbflush.h>
Avi Kivitye4956062007-06-28 14:15:57 -040033#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080034
Eduardo Habkost63d11422008-11-17 19:03:20 -020035#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030036#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020037
Avi Kivity4ecac3f2008-05-13 13:23:38 +030038#define __ex(x) __kvm_handle_fault_on_reboot(x)
39
Avi Kivity6aa8b732006-12-10 02:21:36 -080040MODULE_AUTHOR("Qumranet");
41MODULE_LICENSE("GPL");
42
43#define IOPM_ALLOC_ORDER 2
44#define MSRPM_ALLOC_ORDER 1
45
Avi Kivity6aa8b732006-12-10 02:21:36 -080046#define SEG_TYPE_LDT 2
47#define SEG_TYPE_BUSY_TSS16 3
48
Andre Przywara6bc31bd2010-04-11 23:07:28 +020049#define SVM_FEATURE_NPT (1 << 0)
50#define SVM_FEATURE_LBRV (1 << 1)
51#define SVM_FEATURE_SVML (1 << 2)
52#define SVM_FEATURE_NRIP (1 << 3)
53#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030054
Joerg Roedel410e4d52009-08-07 11:49:44 +020055#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
56#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
57#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
58
Joerg Roedel24e09cb2008-02-13 18:58:47 +010059#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
60
Joerg Roedel67ec6602010-05-17 14:43:35 +020061static bool erratum_383_found __read_mostly;
62
Avi Kivity6c8166a2009-05-31 18:15:37 +030063static const u32 host_save_user_msrs[] = {
64#ifdef CONFIG_X86_64
65 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
66 MSR_FS_BASE,
67#endif
68 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
69};
70
71#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
72
73struct kvm_vcpu;
74
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020075struct nested_state {
76 struct vmcb *hsave;
77 u64 hsave_msr;
Joerg Roedel4a810182010-02-24 18:59:15 +010078 u64 vm_cr_msr;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020079 u64 vmcb;
80
81 /* These are the merged vectors */
82 u32 *msrpm;
83
84 /* gpa pointers to the real vectors */
85 u64 vmcb_msrpm;
Joerg Roedelce2ac082010-03-01 15:34:39 +010086 u64 vmcb_iopm;
Joerg Roedelaad42c62009-08-07 11:49:34 +020087
Joerg Roedelcd3ff652009-10-09 16:08:26 +020088 /* A VMEXIT is required but not yet emulated */
89 bool exit_required;
90
Joerg Roedelaad42c62009-08-07 11:49:34 +020091 /* cache for intercepts of the guest */
92 u16 intercept_cr_read;
93 u16 intercept_cr_write;
94 u16 intercept_dr_read;
95 u16 intercept_dr_write;
96 u32 intercept_exceptions;
97 u64 intercept;
98
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020099};
100
Joerg Roedel323c3d82010-03-01 15:34:37 +0100101#define MSRPM_OFFSETS 16
102static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
103
Avi Kivity6c8166a2009-05-31 18:15:37 +0300104struct vcpu_svm {
105 struct kvm_vcpu vcpu;
106 struct vmcb *vmcb;
107 unsigned long vmcb_pa;
108 struct svm_cpu_data *svm_data;
109 uint64_t asid_generation;
110 uint64_t sysenter_esp;
111 uint64_t sysenter_eip;
112
113 u64 next_rip;
114
115 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
116 u64 host_gs_base;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300117
118 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300119
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200120 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200121
122 bool nmi_singlestep;
Jan Kiszka66b71382010-02-23 17:47:56 +0100123
124 unsigned int3_injected;
125 unsigned long int3_rip;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300126};
127
Joerg Roedel455716f2010-03-01 15:34:35 +0100128#define MSR_INVALID 0xffffffffU
129
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100130static struct svm_direct_access_msrs {
131 u32 index; /* Index of the MSR */
132 bool always; /* True if intercept is always on */
133} direct_access_msrs[] = {
134 { .index = MSR_K6_STAR, .always = true },
135 { .index = MSR_IA32_SYSENTER_CS, .always = true },
136#ifdef CONFIG_X86_64
137 { .index = MSR_GS_BASE, .always = true },
138 { .index = MSR_FS_BASE, .always = true },
139 { .index = MSR_KERNEL_GS_BASE, .always = true },
140 { .index = MSR_LSTAR, .always = true },
141 { .index = MSR_CSTAR, .always = true },
142 { .index = MSR_SYSCALL_MASK, .always = true },
143#endif
144 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
145 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
146 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
147 { .index = MSR_IA32_LASTINTTOIP, .always = false },
148 { .index = MSR_INVALID, .always = false },
Avi Kivity6aa8b732006-12-10 02:21:36 -0800149};
150
151/* enable NPT for AMD64 and X86 with PAE */
152#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
153static bool npt_enabled = true;
154#else
Joerg Roedele0231712010-02-24 18:59:10 +0100155static bool npt_enabled;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800156#endif
157static int npt = 1;
158
159module_param(npt, int, S_IRUGO);
160
Joerg Roedel4b6e4dc2009-08-07 11:49:48 +0200161static int nested = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800162module_param(nested, int, S_IRUGO);
163
164static void svm_flush_tlb(struct kvm_vcpu *vcpu);
Joerg Roedela5c38322009-08-07 11:49:32 +0200165static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800166
Joerg Roedel410e4d52009-08-07 11:49:44 +0200167static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100168static int nested_svm_intercept(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800169static int nested_svm_vmexit(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800170static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
171 bool has_error_code, u32 error_code);
172
173static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
174{
175 return container_of(vcpu, struct vcpu_svm, vcpu);
176}
177
178static inline bool is_nested(struct vcpu_svm *svm)
179{
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200180 return svm->nested.vmcb;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800181}
182
Joerg Roedel2af91942009-08-07 11:49:28 +0200183static inline void enable_gif(struct vcpu_svm *svm)
184{
185 svm->vcpu.arch.hflags |= HF_GIF_MASK;
186}
187
188static inline void disable_gif(struct vcpu_svm *svm)
189{
190 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
191}
192
193static inline bool gif_set(struct vcpu_svm *svm)
194{
195 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
196}
197
Avi Kivity6aa8b732006-12-10 02:21:36 -0800198static unsigned long iopm_base;
199
200struct kvm_ldttss_desc {
201 u16 limit0;
202 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100203 unsigned base1:8, type:5, dpl:2, p:1;
204 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800205 u32 base3;
206 u32 zero1;
207} __attribute__((packed));
208
209struct svm_cpu_data {
210 int cpu;
211
Avi Kivity5008fdf2007-04-02 13:05:50 +0300212 u64 asid_generation;
213 u32 max_asid;
214 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800215 struct kvm_ldttss_desc *tss_desc;
216
217 struct page *save_area;
218};
219
220static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
Joerg Roedel80b77062007-03-30 17:02:14 +0300221static uint32_t svm_features;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800222
223struct svm_init_data {
224 int cpu;
225 int r;
226};
227
228static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
229
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200230#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800231#define MSRS_RANGE_SIZE 2048
232#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
233
Joerg Roedel455716f2010-03-01 15:34:35 +0100234static u32 svm_msrpm_offset(u32 msr)
235{
236 u32 offset;
237 int i;
238
239 for (i = 0; i < NUM_MSR_MAPS; i++) {
240 if (msr < msrpm_ranges[i] ||
241 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
242 continue;
243
244 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
245 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
246
247 /* Now we have the u8 offset - but need the u32 offset */
248 return offset / 4;
249 }
250
251 /* MSR not in any range */
252 return MSR_INVALID;
253}
254
Avi Kivity6aa8b732006-12-10 02:21:36 -0800255#define MAX_INST_SIZE 15
256
Joerg Roedel80b77062007-03-30 17:02:14 +0300257static inline u32 svm_has(u32 feat)
258{
259 return svm_features & feat;
260}
261
Avi Kivity6aa8b732006-12-10 02:21:36 -0800262static inline void clgi(void)
263{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300264 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800265}
266
267static inline void stgi(void)
268{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300269 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800270}
271
272static inline void invlpga(unsigned long addr, u32 asid)
273{
Joerg Roedele0231712010-02-24 18:59:10 +0100274 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800275}
276
Avi Kivity6aa8b732006-12-10 02:21:36 -0800277static inline void force_new_asid(struct kvm_vcpu *vcpu)
278{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400279 to_svm(vcpu)->asid_generation--;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800280}
281
282static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
283{
284 force_new_asid(vcpu);
285}
286
287static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
288{
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100289 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600290 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800291
Alexander Graf9962d032008-11-25 20:17:02 +0100292 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Avi Kivityf6801df2010-01-21 15:31:50 +0200293 vcpu->arch.efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800294}
295
Avi Kivity6aa8b732006-12-10 02:21:36 -0800296static int is_external_interrupt(u32 info)
297{
298 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
299 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
300}
301
Glauber Costa2809f5d2009-05-12 16:21:05 -0400302static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
303{
304 struct vcpu_svm *svm = to_svm(vcpu);
305 u32 ret = 0;
306
307 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Jan Kiszka48005f62010-02-19 19:38:07 +0100308 ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400309 return ret & mask;
310}
311
312static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
313{
314 struct vcpu_svm *svm = to_svm(vcpu);
315
316 if (mask == 0)
317 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
318 else
319 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
320
321}
322
Avi Kivity6aa8b732006-12-10 02:21:36 -0800323static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
324{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400325 struct vcpu_svm *svm = to_svm(vcpu);
326
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200327 if (svm->vmcb->control.next_rip != 0)
328 svm->next_rip = svm->vmcb->control.next_rip;
329
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400330 if (!svm->next_rip) {
Avi Kivity851ba692009-08-24 11:10:17 +0300331 if (emulate_instruction(vcpu, 0, 0, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300332 EMULATE_DONE)
333 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800334 return;
335 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300336 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
337 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
338 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800339
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300340 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400341 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800342}
343
Jan Kiszka116a4752010-02-23 17:47:54 +0100344static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200345 bool has_error_code, u32 error_code,
346 bool reinject)
Jan Kiszka116a4752010-02-23 17:47:54 +0100347{
348 struct vcpu_svm *svm = to_svm(vcpu);
349
Joerg Roedele0231712010-02-24 18:59:10 +0100350 /*
351 * If we are within a nested VM we'd better #VMEXIT and let the guest
352 * handle the exception
353 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200354 if (!reinject &&
355 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100356 return;
357
Jan Kiszka66b71382010-02-23 17:47:56 +0100358 if (nr == BP_VECTOR && !svm_has(SVM_FEATURE_NRIP)) {
359 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
360
361 /*
362 * For guest debugging where we have to reinject #BP if some
363 * INT3 is guest-owned:
364 * Emulate nRIP by moving RIP forward. Will fail if injection
365 * raises a fault that is not intercepted. Still better than
366 * failing in all cases.
367 */
368 skip_emulated_instruction(&svm->vcpu);
369 rip = kvm_rip_read(&svm->vcpu);
370 svm->int3_rip = rip + svm->vmcb->save.cs.base;
371 svm->int3_injected = rip - old_rip;
372 }
373
Jan Kiszka116a4752010-02-23 17:47:54 +0100374 svm->vmcb->control.event_inj = nr
375 | SVM_EVTINJ_VALID
376 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
377 | SVM_EVTINJ_TYPE_EXEPT;
378 svm->vmcb->control.event_inj_err = error_code;
379}
380
Joerg Roedel67ec6602010-05-17 14:43:35 +0200381static void svm_init_erratum_383(void)
382{
383 u32 low, high;
384 int err;
385 u64 val;
386
387 /* Only Fam10h is affected */
388 if (boot_cpu_data.x86 != 0x10)
389 return;
390
391 /* Use _safe variants to not break nested virtualization */
392 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
393 if (err)
394 return;
395
396 val |= (1ULL << 47);
397
398 low = lower_32_bits(val);
399 high = upper_32_bits(val);
400
401 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
402
403 erratum_383_found = true;
404}
405
Avi Kivity6aa8b732006-12-10 02:21:36 -0800406static int has_svm(void)
407{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200408 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800409
Eduardo Habkost63d11422008-11-17 19:03:20 -0200410 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800411 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800412 return 0;
413 }
414
Avi Kivity6aa8b732006-12-10 02:21:36 -0800415 return 1;
416}
417
418static void svm_hardware_disable(void *garbage)
419{
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200420 cpu_svm_disable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800421}
422
Alexander Graf10474ae2009-09-15 11:37:46 +0200423static int svm_hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424{
425
Tejun Heo0fe1e002009-10-29 22:34:14 +0900426 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800427 uint64_t efer;
Gleb Natapov89a27f42010-02-16 10:51:48 +0200428 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800429 struct desc_struct *gdt;
430 int me = raw_smp_processor_id();
431
Alexander Graf10474ae2009-09-15 11:37:46 +0200432 rdmsrl(MSR_EFER, efer);
433 if (efer & EFER_SVME)
434 return -EBUSY;
435
Avi Kivity6aa8b732006-12-10 02:21:36 -0800436 if (!has_svm()) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000437 printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
438 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200439 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800440 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900441 sd = per_cpu(svm_data, me);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800442
Tejun Heo0fe1e002009-10-29 22:34:14 +0900443 if (!sd) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000444 printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -0800445 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200446 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800447 }
448
Tejun Heo0fe1e002009-10-29 22:34:14 +0900449 sd->asid_generation = 1;
450 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
451 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800452
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200453 native_store_gdt(&gdt_descr);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200454 gdt = (struct desc_struct *)gdt_descr.address;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900455 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800456
Alexander Graf9962d032008-11-25 20:17:02 +0100457 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800458
Linus Torvaldsd0316552009-12-14 09:58:24 -0800459 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200460
Joerg Roedel67ec6602010-05-17 14:43:35 +0200461 svm_init_erratum_383();
462
Alexander Graf10474ae2009-09-15 11:37:46 +0200463 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800464}
465
Joerg Roedel0da1db752008-07-02 16:02:11 +0200466static void svm_cpu_uninit(int cpu)
467{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900468 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200469
Tejun Heo0fe1e002009-10-29 22:34:14 +0900470 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200471 return;
472
473 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900474 __free_page(sd->save_area);
475 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200476}
477
Avi Kivity6aa8b732006-12-10 02:21:36 -0800478static int svm_cpu_init(int cpu)
479{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900480 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800481 int r;
482
Tejun Heo0fe1e002009-10-29 22:34:14 +0900483 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
484 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800485 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900486 sd->cpu = cpu;
487 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800488 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900489 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800490 goto err_1;
491
Tejun Heo0fe1e002009-10-29 22:34:14 +0900492 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800493
494 return 0;
495
496err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900497 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800498 return r;
499
500}
501
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100502static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800503{
504 int i;
505
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100506 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
507 if (direct_access_msrs[i].index == index)
508 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800509
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100510 return false;
511}
512
Avi Kivity6aa8b732006-12-10 02:21:36 -0800513static void set_msr_interception(u32 *msrpm, unsigned msr,
514 int read, int write)
515{
Joerg Roedel455716f2010-03-01 15:34:35 +0100516 u8 bit_read, bit_write;
517 unsigned long tmp;
518 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800519
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100520 /*
521 * If this warning triggers extend the direct_access_msrs list at the
522 * beginning of the file
523 */
524 WARN_ON(!valid_msr_intercept(msr));
525
Joerg Roedel455716f2010-03-01 15:34:35 +0100526 offset = svm_msrpm_offset(msr);
527 bit_read = 2 * (msr & 0x0f);
528 bit_write = 2 * (msr & 0x0f) + 1;
529 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800530
Joerg Roedel455716f2010-03-01 15:34:35 +0100531 BUG_ON(offset == MSR_INVALID);
532
533 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
534 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
535
536 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800537}
538
Joerg Roedelf65c2292008-02-13 18:58:46 +0100539static void svm_vcpu_init_msrpm(u32 *msrpm)
540{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100541 int i;
542
Joerg Roedelf65c2292008-02-13 18:58:46 +0100543 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
544
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100545 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
546 if (!direct_access_msrs[i].always)
547 continue;
548
549 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
550 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100551}
552
Joerg Roedel323c3d82010-03-01 15:34:37 +0100553static void add_msr_offset(u32 offset)
554{
555 int i;
556
557 for (i = 0; i < MSRPM_OFFSETS; ++i) {
558
559 /* Offset already in list? */
560 if (msrpm_offsets[i] == offset)
561 return;
562
563 /* Slot used by another offset? */
564 if (msrpm_offsets[i] != MSR_INVALID)
565 continue;
566
567 /* Add offset to list */
568 msrpm_offsets[i] = offset;
569
570 return;
571 }
572
573 /*
574 * If this BUG triggers the msrpm_offsets table has an overflow. Just
575 * increase MSRPM_OFFSETS in this case.
576 */
577 BUG();
578}
579
580static void init_msrpm_offsets(void)
581{
582 int i;
583
584 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
585
586 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
587 u32 offset;
588
589 offset = svm_msrpm_offset(direct_access_msrs[i].index);
590 BUG_ON(offset == MSR_INVALID);
591
592 add_msr_offset(offset);
593 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800594}
595
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100596static void svm_enable_lbrv(struct vcpu_svm *svm)
597{
598 u32 *msrpm = svm->msrpm;
599
600 svm->vmcb->control.lbr_ctl = 1;
601 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
602 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
603 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
604 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
605}
606
607static void svm_disable_lbrv(struct vcpu_svm *svm)
608{
609 u32 *msrpm = svm->msrpm;
610
611 svm->vmcb->control.lbr_ctl = 0;
612 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
613 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
614 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
615 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
616}
617
Avi Kivity6aa8b732006-12-10 02:21:36 -0800618static __init int svm_hardware_setup(void)
619{
620 int cpu;
621 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100622 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800623 int r;
624
Avi Kivity6aa8b732006-12-10 02:21:36 -0800625 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
626
627 if (!iopm_pages)
628 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300629
630 iopm_va = page_address(iopm_pages);
631 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800632 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
633
Joerg Roedel323c3d82010-03-01 15:34:37 +0100634 init_msrpm_offsets();
635
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100636 if (boot_cpu_has(X86_FEATURE_NX))
637 kvm_enable_efer_bits(EFER_NX);
638
Alexander Graf1b2fd702009-02-02 16:23:51 +0100639 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
640 kvm_enable_efer_bits(EFER_FFXSR);
641
Alexander Graf236de052008-11-25 20:17:10 +0100642 if (nested) {
643 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +0200644 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +0100645 }
646
Zachary Amsden3230bb42009-09-29 11:38:37 -1000647 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800648 r = svm_cpu_init(cpu);
649 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100650 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800651 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100652
653 svm_features = cpuid_edx(SVM_CPUID_FUNC);
654
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100655 if (!svm_has(SVM_FEATURE_NPT))
656 npt_enabled = false;
657
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100658 if (npt_enabled && !npt) {
659 printk(KERN_INFO "kvm: Nested Paging disabled\n");
660 npt_enabled = false;
661 }
662
Joerg Roedel18552672008-02-07 13:47:41 +0100663 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100664 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100665 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200666 } else
667 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100668
Avi Kivity6aa8b732006-12-10 02:21:36 -0800669 return 0;
670
Joerg Roedelf65c2292008-02-13 18:58:46 +0100671err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800672 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
673 iopm_base = 0;
674 return r;
675}
676
677static __exit void svm_hardware_unsetup(void)
678{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200679 int cpu;
680
Zachary Amsden3230bb42009-09-29 11:38:37 -1000681 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200682 svm_cpu_uninit(cpu);
683
Avi Kivity6aa8b732006-12-10 02:21:36 -0800684 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100685 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800686}
687
688static void init_seg(struct vmcb_seg *seg)
689{
690 seg->selector = 0;
691 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +0100692 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800693 seg->limit = 0xffff;
694 seg->base = 0;
695}
696
697static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
698{
699 seg->selector = 0;
700 seg->attrib = SVM_SELECTOR_P_MASK | type;
701 seg->limit = 0xffff;
702 seg->base = 0;
703}
704
Joerg Roedele6101a92008-02-13 18:58:45 +0100705static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800706{
Joerg Roedele6101a92008-02-13 18:58:45 +0100707 struct vmcb_control_area *control = &svm->vmcb->control;
708 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800709
Avi Kivitybff78272010-01-07 13:16:08 +0200710 svm->vcpu.fpu_active = 1;
711
Joerg Roedele0231712010-02-24 18:59:10 +0100712 control->intercept_cr_read = INTERCEPT_CR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800713 INTERCEPT_CR3_MASK |
Joerg Roedel649d6862008-04-16 16:51:15 +0200714 INTERCEPT_CR4_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800715
Joerg Roedele0231712010-02-24 18:59:10 +0100716 control->intercept_cr_write = INTERCEPT_CR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800717 INTERCEPT_CR3_MASK |
Avi Kivity80a81192007-12-06 19:50:00 +0200718 INTERCEPT_CR4_MASK |
719 INTERCEPT_CR8_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800720
Joerg Roedele0231712010-02-24 18:59:10 +0100721 control->intercept_dr_read = INTERCEPT_DR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800722 INTERCEPT_DR1_MASK |
723 INTERCEPT_DR2_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100724 INTERCEPT_DR3_MASK |
725 INTERCEPT_DR4_MASK |
726 INTERCEPT_DR5_MASK |
727 INTERCEPT_DR6_MASK |
728 INTERCEPT_DR7_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800729
Joerg Roedele0231712010-02-24 18:59:10 +0100730 control->intercept_dr_write = INTERCEPT_DR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800731 INTERCEPT_DR1_MASK |
732 INTERCEPT_DR2_MASK |
733 INTERCEPT_DR3_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100734 INTERCEPT_DR4_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800735 INTERCEPT_DR5_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100736 INTERCEPT_DR6_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800737 INTERCEPT_DR7_MASK;
738
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500739 control->intercept_exceptions = (1 << PF_VECTOR) |
Joerg Roedel53371b52008-04-09 14:15:30 +0200740 (1 << UD_VECTOR) |
741 (1 << MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800742
743
Joerg Roedele0231712010-02-24 18:59:10 +0100744 control->intercept = (1ULL << INTERCEPT_INTR) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800745 (1ULL << INTERCEPT_NMI) |
Joerg Roedel01525272007-02-19 14:37:47 +0200746 (1ULL << INTERCEPT_SMI) |
Avi Kivityd2251572010-01-06 10:55:27 +0200747 (1ULL << INTERCEPT_SELECTIVE_CR0) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800748 (1ULL << INTERCEPT_CPUID) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200749 (1ULL << INTERCEPT_INVD) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800750 (1ULL << INTERCEPT_HLT) |
Marcelo Tosattia7052892008-09-23 13:18:35 -0300751 (1ULL << INTERCEPT_INVLPG) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800752 (1ULL << INTERCEPT_INVLPGA) |
753 (1ULL << INTERCEPT_IOIO_PROT) |
754 (1ULL << INTERCEPT_MSR_PROT) |
755 (1ULL << INTERCEPT_TASK_SWITCH) |
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800756 (1ULL << INTERCEPT_SHUTDOWN) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800757 (1ULL << INTERCEPT_VMRUN) |
758 (1ULL << INTERCEPT_VMMCALL) |
759 (1ULL << INTERCEPT_VMLOAD) |
760 (1ULL << INTERCEPT_VMSAVE) |
761 (1ULL << INTERCEPT_STGI) |
762 (1ULL << INTERCEPT_CLGI) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100763 (1ULL << INTERCEPT_SKINIT) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200764 (1ULL << INTERCEPT_WBINVD) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100765 (1ULL << INTERCEPT_MONITOR) |
766 (1ULL << INTERCEPT_MWAIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800767
768 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100769 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity0cc50642007-03-25 12:07:27 +0200770 control->tsc_offset = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800771 control->int_ctl = V_INTR_MASKING_MASK;
772
773 init_seg(&save->es);
774 init_seg(&save->ss);
775 init_seg(&save->ds);
776 init_seg(&save->fs);
777 init_seg(&save->gs);
778
779 save->cs.selector = 0xf000;
780 /* Executable/Readable Code Segment */
781 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
782 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
783 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800784 /*
785 * cs.base should really be 0xffff0000, but vmx can't handle that, so
786 * be consistent with it.
787 *
788 * Replace when we have real mode working for vmx.
789 */
790 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800791
792 save->gdtr.limit = 0xffff;
793 save->idtr.limit = 0xffff;
794
795 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
796 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
797
Alexander Graf9962d032008-11-25 20:17:02 +0100798 save->efer = EFER_SVME;
Mike Dayd77c26f2007-10-08 09:02:08 -0400799 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800800 save->dr7 = 0x400;
801 save->rflags = 2;
802 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300803 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800804
Joerg Roedele0231712010-02-24 18:59:10 +0100805 /*
806 * This is the guest-visible cr0 value.
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200807 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800808 */
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200809 svm->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
810 kvm_set_cr0(&svm->vcpu, svm->vcpu.arch.cr0);
811
Rusty Russell66aee912007-07-17 23:34:16 +1000812 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800813 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100814
815 if (npt_enabled) {
816 /* Setup VMCB for Nested Paging */
817 control->nested_ctl = 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -0300818 control->intercept &= ~((1ULL << INTERCEPT_TASK_SWITCH) |
819 (1ULL << INTERCEPT_INVLPG));
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100820 control->intercept_exceptions &= ~(1 << PF_VECTOR);
Avi Kivity888f9f32010-01-10 12:14:04 +0200821 control->intercept_cr_read &= ~INTERCEPT_CR3_MASK;
822 control->intercept_cr_write &= ~INTERCEPT_CR3_MASK;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100823 save->g_pat = 0x0007040600070406ULL;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100824 save->cr3 = 0;
825 save->cr4 = 0;
826 }
Avi Kivitya79d2f12008-04-14 13:10:21 +0300827 force_new_asid(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +0100828
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200829 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +0200830 svm->vcpu.arch.hflags = 0;
831
Mark Langsdorf565d0992009-10-06 14:25:02 -0500832 if (svm_has(SVM_FEATURE_PAUSE_FILTER)) {
833 control->pause_filter_count = 3000;
834 control->intercept |= (1ULL << INTERCEPT_PAUSE);
835 }
836
Joerg Roedel2af91942009-08-07 11:49:28 +0200837 enable_gif(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800838}
839
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200840static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +0300841{
842 struct vcpu_svm *svm = to_svm(vcpu);
843
Joerg Roedele6101a92008-02-13 18:58:45 +0100844 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +0200845
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300846 if (!kvm_vcpu_is_bsp(vcpu)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300847 kvm_rip_write(vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800848 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
849 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +0200850 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300851 vcpu->arch.regs_avail = ~0;
852 vcpu->arch.regs_dirty = ~0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200853
854 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300855}
856
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000857static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400859 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800860 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100861 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +0100862 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100863 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000864 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800865
Rusty Russellc16f8622007-07-30 21:12:19 +1000866 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000867 if (!svm) {
868 err = -ENOMEM;
869 goto out;
870 }
871
872 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
873 if (err)
874 goto free_svm;
875
Joerg Roedelf65c2292008-02-13 18:58:46 +0100876 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900877 page = alloc_page(GFP_KERNEL);
878 if (!page)
879 goto uninit;
880
Joerg Roedelf65c2292008-02-13 18:58:46 +0100881 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
882 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900883 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100884
885 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
886 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900887 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100888
Alexander Grafb286d5d2008-11-25 20:17:05 +0100889 hsave_page = alloc_page(GFP_KERNEL);
890 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900891 goto free_page3;
892
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200893 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +0100894
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900895 svm->msrpm = page_address(msrpm_pages);
896 svm_vcpu_init_msrpm(svm->msrpm);
897
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200898 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +0100899 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +0100900
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400901 svm->vmcb = page_address(page);
902 clear_page(svm->vmcb);
903 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
904 svm->asid_generation = 0;
Joerg Roedele6101a92008-02-13 18:58:45 +0100905 init_vmcb(svm);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400906
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000907 fx_init(&svm->vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800908 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300909 if (kvm_vcpu_is_bsp(&svm->vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800910 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800911
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000912 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -0800913
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900914free_page3:
915 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
916free_page2:
917 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
918free_page1:
919 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000920uninit:
921 kvm_vcpu_uninit(&svm->vcpu);
922free_svm:
Rusty Russella4770342007-08-01 14:46:11 +1000923 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000924out:
925 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926}
927
928static void svm_free_vcpu(struct kvm_vcpu *vcpu)
929{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400930 struct vcpu_svm *svm = to_svm(vcpu);
931
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000932 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +0100933 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200934 __free_page(virt_to_page(svm->nested.hsave));
935 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000936 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +1000937 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938}
939
Avi Kivity15ad7142007-07-11 18:17:21 +0300940static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800941{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400942 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300943 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +0200944
Avi Kivity0cc50642007-03-25 12:07:27 +0200945 if (unlikely(cpu != vcpu->cpu)) {
Joerg Roedele935d482009-09-16 15:24:19 +0200946 u64 delta;
Avi Kivity0cc50642007-03-25 12:07:27 +0200947
Joerg Roedel953899b2009-12-14 12:22:20 +0100948 if (check_tsc_unstable()) {
949 /*
950 * Make sure that the guest sees a monotonically
951 * increasing TSC.
952 */
953 delta = vcpu->arch.host_tsc - native_read_tsc();
954 svm->vmcb->control.tsc_offset += delta;
955 if (is_nested(svm))
956 svm->nested.hsave->control.tsc_offset += delta;
957 }
Avi Kivity0cc50642007-03-25 12:07:27 +0200958 vcpu->cpu = cpu;
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300959 kvm_migrate_timers(vcpu);
Marcelo Tosatti4b656b12009-07-21 12:47:45 -0300960 svm->asid_generation = 0;
Avi Kivity0cc50642007-03-25 12:07:27 +0200961 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300962
963 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400964 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965}
966
967static void svm_vcpu_put(struct kvm_vcpu *vcpu)
968{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400969 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300970 int i;
971
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200972 ++vcpu->stat.host_state_reload;
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300973 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400974 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300975
Joerg Roedele935d482009-09-16 15:24:19 +0200976 vcpu->arch.host_tsc = native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800977}
978
Avi Kivity6aa8b732006-12-10 02:21:36 -0800979static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
980{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400981 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982}
983
984static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
985{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400986 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987}
988
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300989static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
990{
991 switch (reg) {
992 case VCPU_EXREG_PDPTR:
993 BUG_ON(!npt_enabled);
994 load_pdptrs(vcpu, vcpu->arch.cr3);
995 break;
996 default:
997 BUG();
998 }
999}
1000
Alexander Graff0b85052008-11-25 20:17:01 +01001001static void svm_set_vintr(struct vcpu_svm *svm)
1002{
1003 svm->vmcb->control.intercept |= 1ULL << INTERCEPT_VINTR;
1004}
1005
1006static void svm_clear_vintr(struct vcpu_svm *svm)
1007{
1008 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
1009}
1010
Avi Kivity6aa8b732006-12-10 02:21:36 -08001011static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1012{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001013 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014
1015 switch (seg) {
1016 case VCPU_SREG_CS: return &save->cs;
1017 case VCPU_SREG_DS: return &save->ds;
1018 case VCPU_SREG_ES: return &save->es;
1019 case VCPU_SREG_FS: return &save->fs;
1020 case VCPU_SREG_GS: return &save->gs;
1021 case VCPU_SREG_SS: return &save->ss;
1022 case VCPU_SREG_TR: return &save->tr;
1023 case VCPU_SREG_LDTR: return &save->ldtr;
1024 }
1025 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001026 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001027}
1028
1029static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1030{
1031 struct vmcb_seg *s = svm_seg(vcpu, seg);
1032
1033 return s->base;
1034}
1035
1036static void svm_get_segment(struct kvm_vcpu *vcpu,
1037 struct kvm_segment *var, int seg)
1038{
1039 struct vmcb_seg *s = svm_seg(vcpu, seg);
1040
1041 var->base = s->base;
1042 var->limit = s->limit;
1043 var->selector = s->selector;
1044 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1045 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1046 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1047 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1048 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1049 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1050 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
1051 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
Amit Shah25022ac2008-10-27 09:04:17 +00001052
Joerg Roedele0231712010-02-24 18:59:10 +01001053 /*
1054 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001055 * for cross vendor migration purposes by "not present"
1056 */
1057 var->unusable = !var->present || (var->type == 0);
1058
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001059 switch (seg) {
1060 case VCPU_SREG_CS:
1061 /*
1062 * SVM always stores 0 for the 'G' bit in the CS selector in
1063 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
1064 * Intel's VMENTRY has a check on the 'G' bit.
1065 */
Amit Shah25022ac2008-10-27 09:04:17 +00001066 var->g = s->limit > 0xfffff;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001067 break;
1068 case VCPU_SREG_TR:
1069 /*
1070 * Work around a bug where the busy flag in the tr selector
1071 * isn't exposed
1072 */
Amit Shahc0d09822008-10-27 09:04:18 +00001073 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001074 break;
1075 case VCPU_SREG_DS:
1076 case VCPU_SREG_ES:
1077 case VCPU_SREG_FS:
1078 case VCPU_SREG_GS:
1079 /*
1080 * The accessed bit must always be set in the segment
1081 * descriptor cache, although it can be cleared in the
1082 * descriptor, the cached bit always remains at 1. Since
1083 * Intel has a check on this, set it here to support
1084 * cross-vendor migration.
1085 */
1086 if (!var->unusable)
1087 var->type |= 0x1;
1088 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001089 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001090 /*
1091 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001092 * descriptor is left as 1, although the whole segment has
1093 * been made unusable. Clear it here to pass an Intel VMX
1094 * entry check when cross vendor migrating.
1095 */
1096 if (var->unusable)
1097 var->db = 0;
1098 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001099 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001100}
1101
Izik Eidus2e4d2652008-03-24 19:38:34 +02001102static int svm_get_cpl(struct kvm_vcpu *vcpu)
1103{
1104 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1105
1106 return save->cpl;
1107}
1108
Gleb Natapov89a27f42010-02-16 10:51:48 +02001109static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001110{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001111 struct vcpu_svm *svm = to_svm(vcpu);
1112
Gleb Natapov89a27f42010-02-16 10:51:48 +02001113 dt->size = svm->vmcb->save.idtr.limit;
1114 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001115}
1116
Gleb Natapov89a27f42010-02-16 10:51:48 +02001117static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001118{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001119 struct vcpu_svm *svm = to_svm(vcpu);
1120
Gleb Natapov89a27f42010-02-16 10:51:48 +02001121 svm->vmcb->save.idtr.limit = dt->size;
1122 svm->vmcb->save.idtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001123}
1124
Gleb Natapov89a27f42010-02-16 10:51:48 +02001125static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001126{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001127 struct vcpu_svm *svm = to_svm(vcpu);
1128
Gleb Natapov89a27f42010-02-16 10:51:48 +02001129 dt->size = svm->vmcb->save.gdtr.limit;
1130 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001131}
1132
Gleb Natapov89a27f42010-02-16 10:51:48 +02001133static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001134{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001135 struct vcpu_svm *svm = to_svm(vcpu);
1136
Gleb Natapov89a27f42010-02-16 10:51:48 +02001137 svm->vmcb->save.gdtr.limit = dt->size;
1138 svm->vmcb->save.gdtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001139}
1140
Avi Kivitye8467fd2009-12-29 18:43:06 +02001141static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1142{
1143}
1144
Anthony Liguori25c4c272007-04-27 09:29:21 +03001145static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001146{
1147}
1148
Avi Kivityd2251572010-01-06 10:55:27 +02001149static void update_cr0_intercept(struct vcpu_svm *svm)
1150{
Joerg Roedel66a562f2010-02-19 16:23:08 +01001151 struct vmcb *vmcb = svm->vmcb;
Avi Kivityd2251572010-01-06 10:55:27 +02001152 ulong gcr0 = svm->vcpu.arch.cr0;
1153 u64 *hcr0 = &svm->vmcb->save.cr0;
1154
1155 if (!svm->vcpu.fpu_active)
1156 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1157 else
1158 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1159 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1160
1161
1162 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Joerg Roedel66a562f2010-02-19 16:23:08 +01001163 vmcb->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1164 vmcb->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1165 if (is_nested(svm)) {
1166 struct vmcb *hsave = svm->nested.hsave;
1167
1168 hsave->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1169 hsave->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1170 vmcb->control.intercept_cr_read |= svm->nested.intercept_cr_read;
1171 vmcb->control.intercept_cr_write |= svm->nested.intercept_cr_write;
1172 }
Avi Kivityd2251572010-01-06 10:55:27 +02001173 } else {
1174 svm->vmcb->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1175 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001176 if (is_nested(svm)) {
1177 struct vmcb *hsave = svm->nested.hsave;
1178
1179 hsave->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1180 hsave->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
1181 }
Avi Kivityd2251572010-01-06 10:55:27 +02001182 }
1183}
1184
Avi Kivity6aa8b732006-12-10 02:21:36 -08001185static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1186{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001187 struct vcpu_svm *svm = to_svm(vcpu);
1188
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001189 if (is_nested(svm)) {
1190 /*
1191 * We are here because we run in nested mode, the host kvm
1192 * intercepts cr0 writes but the l1 hypervisor does not.
1193 * But the L1 hypervisor may intercept selective cr0 writes.
1194 * This needs to be checked here.
1195 */
1196 unsigned long old, new;
1197
1198 /* Remove bits that would trigger a real cr0 write intercept */
1199 old = vcpu->arch.cr0 & SVM_CR0_SELECTIVE_MASK;
1200 new = cr0 & SVM_CR0_SELECTIVE_MASK;
1201
1202 if (old == new) {
1203 /* cr0 write with ts and mp unchanged */
1204 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
1205 if (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE)
1206 return;
1207 }
1208 }
1209
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001210#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001211 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001212 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001213 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001214 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001215 }
1216
Mike Dayd77c26f2007-10-08 09:02:08 -04001217 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001218 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001219 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001220 }
1221 }
1222#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001223 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001224
1225 if (!npt_enabled)
1226 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001227
1228 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001229 cr0 |= X86_CR0_TS;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001230 /*
1231 * re-enable caching here because the QEMU bios
1232 * does not do it - this results in some delay at
1233 * reboot
1234 */
1235 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001236 svm->vmcb->save.cr0 = cr0;
Avi Kivityd2251572010-01-06 10:55:27 +02001237 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001238}
1239
1240static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1241{
Joerg Roedel6394b642008-04-09 14:15:29 +02001242 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001243 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1244
1245 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
1246 force_new_asid(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001247
Joerg Roedelec077262008-04-09 14:15:28 +02001248 vcpu->arch.cr4 = cr4;
1249 if (!npt_enabled)
1250 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001251 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001252 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001253}
1254
1255static void svm_set_segment(struct kvm_vcpu *vcpu,
1256 struct kvm_segment *var, int seg)
1257{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001258 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001259 struct vmcb_seg *s = svm_seg(vcpu, seg);
1260
1261 s->base = var->base;
1262 s->limit = var->limit;
1263 s->selector = var->selector;
1264 if (var->unusable)
1265 s->attrib = 0;
1266 else {
1267 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1268 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1269 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1270 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1271 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1272 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1273 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1274 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1275 }
1276 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001277 svm->vmcb->save.cpl
1278 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -08001279 >> SVM_SELECTOR_DPL_SHIFT) & 3;
1280
1281}
1282
Gleb Natapov44c11432009-05-11 13:35:52 +03001283static void update_db_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001284{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001285 struct vcpu_svm *svm = to_svm(vcpu);
1286
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001287 svm->vmcb->control.intercept_exceptions &=
1288 ~((1 << DB_VECTOR) | (1 << BP_VECTOR));
Gleb Natapov44c11432009-05-11 13:35:52 +03001289
Jan Kiszka6be7d302009-10-18 13:24:54 +02001290 if (svm->nmi_singlestep)
Gleb Natapov44c11432009-05-11 13:35:52 +03001291 svm->vmcb->control.intercept_exceptions |= (1 << DB_VECTOR);
1292
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001293 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1294 if (vcpu->guest_debug &
1295 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
1296 svm->vmcb->control.intercept_exceptions |=
1297 1 << DB_VECTOR;
1298 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
1299 svm->vmcb->control.intercept_exceptions |=
1300 1 << BP_VECTOR;
1301 } else
1302 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001303}
1304
Jan Kiszka355be0b2009-10-03 00:31:21 +02001305static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Gleb Natapov44c11432009-05-11 13:35:52 +03001306{
Gleb Natapov44c11432009-05-11 13:35:52 +03001307 struct vcpu_svm *svm = to_svm(vcpu);
1308
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001309 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1310 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1311 else
1312 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1313
Jan Kiszka355be0b2009-10-03 00:31:21 +02001314 update_db_intercept(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001315}
1316
1317static void load_host_msrs(struct kvm_vcpu *vcpu)
1318{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001319#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001320 wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001321#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001322}
1323
1324static void save_host_msrs(struct kvm_vcpu *vcpu)
1325{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001326#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001327 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001328#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001329}
1330
Tejun Heo0fe1e002009-10-29 22:34:14 +09001331static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001332{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001333 if (sd->next_asid > sd->max_asid) {
1334 ++sd->asid_generation;
1335 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001336 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001337 }
1338
Tejun Heo0fe1e002009-10-29 22:34:14 +09001339 svm->asid_generation = sd->asid_generation;
1340 svm->vmcb->control.asid = sd->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001341}
1342
Gleb Natapov020df072010-04-13 10:05:23 +03001343static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001344{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001345 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001346
Gleb Natapov020df072010-04-13 10:05:23 +03001347 svm->vmcb->save.dr7 = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001348}
1349
Avi Kivity851ba692009-08-24 11:10:17 +03001350static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001351{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001352 u64 fault_address;
1353 u32 error_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001354
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001355 fault_address = svm->vmcb->control.exit_info_2;
1356 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001357
Marcelo Tosatti229456f2009-06-17 09:22:14 -03001358 trace_kvm_page_fault(fault_address, error_code);
Avi Kivity52c78472009-08-10 15:42:41 +03001359 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1360 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
Avi Kivity30677142007-10-28 18:48:59 +02001361 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001362}
1363
Avi Kivity851ba692009-08-24 11:10:17 +03001364static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001365{
Avi Kivity851ba692009-08-24 11:10:17 +03001366 struct kvm_run *kvm_run = svm->vcpu.run;
1367
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001368 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001369 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001370 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001371 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1372 return 1;
1373 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001374
Jan Kiszka6be7d302009-10-18 13:24:54 +02001375 if (svm->nmi_singlestep) {
1376 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001377 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1378 svm->vmcb->save.rflags &=
1379 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1380 update_db_intercept(&svm->vcpu);
1381 }
1382
1383 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001384 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001385 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1386 kvm_run->debug.arch.pc =
1387 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1388 kvm_run->debug.arch.exception = DB_VECTOR;
1389 return 0;
1390 }
1391
1392 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001393}
1394
Avi Kivity851ba692009-08-24 11:10:17 +03001395static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001396{
Avi Kivity851ba692009-08-24 11:10:17 +03001397 struct kvm_run *kvm_run = svm->vcpu.run;
1398
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001399 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1400 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1401 kvm_run->debug.arch.exception = BP_VECTOR;
1402 return 0;
1403}
1404
Avi Kivity851ba692009-08-24 11:10:17 +03001405static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001406{
1407 int er;
1408
Avi Kivity851ba692009-08-24 11:10:17 +03001409 er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001410 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001411 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001412 return 1;
1413}
1414
Avi Kivity6b52d182010-01-21 15:31:47 +02001415static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001416{
Avi Kivity6b52d182010-01-21 15:31:47 +02001417 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001418 u32 excp;
1419
1420 if (is_nested(svm)) {
1421 u32 h_excp, n_excp;
1422
1423 h_excp = svm->nested.hsave->control.intercept_exceptions;
1424 n_excp = svm->nested.intercept_exceptions;
1425 h_excp &= ~(1 << NM_VECTOR);
1426 excp = h_excp | n_excp;
1427 } else {
1428 excp = svm->vmcb->control.intercept_exceptions;
Joerg Roedele0231712010-02-24 18:59:10 +01001429 excp &= ~(1 << NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001430 }
1431
1432 svm->vmcb->control.intercept_exceptions = excp;
1433
Rusty Russelle756fc62007-07-30 20:07:08 +10001434 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001435 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001436}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001437
Avi Kivity6b52d182010-01-21 15:31:47 +02001438static int nm_interception(struct vcpu_svm *svm)
1439{
1440 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001441 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001442}
1443
Joerg Roedel67ec6602010-05-17 14:43:35 +02001444static bool is_erratum_383(void)
1445{
1446 int err, i;
1447 u64 value;
1448
1449 if (!erratum_383_found)
1450 return false;
1451
1452 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1453 if (err)
1454 return false;
1455
1456 /* Bit 62 may or may not be set for this mce */
1457 value &= ~(1ULL << 62);
1458
1459 if (value != 0xb600000000010015ULL)
1460 return false;
1461
1462 /* Clear MCi_STATUS registers */
1463 for (i = 0; i < 6; ++i)
1464 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1465
1466 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1467 if (!err) {
1468 u32 low, high;
1469
1470 value &= ~(1ULL << 2);
1471 low = lower_32_bits(value);
1472 high = upper_32_bits(value);
1473
1474 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1475 }
1476
1477 /* Flush tlb to evict multi-match entries */
1478 __flush_tlb_all();
1479
1480 return true;
1481}
1482
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001483static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001484{
Joerg Roedel67ec6602010-05-17 14:43:35 +02001485 if (is_erratum_383()) {
1486 /*
1487 * Erratum 383 triggered. Guest state is corrupt so kill the
1488 * guest.
1489 */
1490 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1491
1492 set_bit(KVM_REQ_TRIPLE_FAULT, &svm->vcpu.requests);
1493
1494 return;
1495 }
1496
Joerg Roedel53371b52008-04-09 14:15:30 +02001497 /*
1498 * On an #MC intercept the MCE handler is not called automatically in
1499 * the host. So do it by hand here.
1500 */
1501 asm volatile (
1502 "int $0x12\n");
1503 /* not sure if we ever come back to this point */
1504
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001505 return;
1506}
1507
1508static int mc_interception(struct vcpu_svm *svm)
1509{
Joerg Roedel53371b52008-04-09 14:15:30 +02001510 return 1;
1511}
1512
Avi Kivity851ba692009-08-24 11:10:17 +03001513static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001514{
Avi Kivity851ba692009-08-24 11:10:17 +03001515 struct kvm_run *kvm_run = svm->vcpu.run;
1516
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001517 /*
1518 * VMCB is undefined after a SHUTDOWN intercept
1519 * so reinitialize it.
1520 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001521 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001522 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001523
1524 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1525 return 0;
1526}
1527
Avi Kivity851ba692009-08-24 11:10:17 +03001528static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001529{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001530 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04001531 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001532 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001533 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001534
Rusty Russelle756fc62007-07-30 20:07:08 +10001535 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03001536 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001537 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001538 if (string || in)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03001539 return emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001540
Avi Kivity039576c2007-03-20 12:46:50 +02001541 port = io_info >> 16;
1542 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001543 svm->next_rip = svm->vmcb->control.exit_info_2;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001544 skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001545
1546 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001547}
1548
Avi Kivity851ba692009-08-24 11:10:17 +03001549static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001550{
1551 return 1;
1552}
1553
Avi Kivity851ba692009-08-24 11:10:17 +03001554static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001555{
1556 ++svm->vcpu.stat.irq_exits;
1557 return 1;
1558}
1559
Avi Kivity851ba692009-08-24 11:10:17 +03001560static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001561{
1562 return 1;
1563}
1564
Avi Kivity851ba692009-08-24 11:10:17 +03001565static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001566{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001567 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001568 skip_emulated_instruction(&svm->vcpu);
1569 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001570}
1571
Avi Kivity851ba692009-08-24 11:10:17 +03001572static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001573{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001574 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001575 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001576 kvm_emulate_hypercall(&svm->vcpu);
1577 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001578}
1579
Alexander Grafc0725422008-11-25 20:17:03 +01001580static int nested_svm_check_permissions(struct vcpu_svm *svm)
1581{
Avi Kivityf6801df2010-01-21 15:31:50 +02001582 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01001583 || !is_paging(&svm->vcpu)) {
1584 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1585 return 1;
1586 }
1587
1588 if (svm->vmcb->save.cpl) {
1589 kvm_inject_gp(&svm->vcpu, 0);
1590 return 1;
1591 }
1592
1593 return 0;
1594}
1595
Alexander Grafcf74a782008-11-25 20:17:08 +01001596static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
1597 bool has_error_code, u32 error_code)
1598{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001599 int vmexit;
1600
Joerg Roedel0295ad72009-08-07 11:49:37 +02001601 if (!is_nested(svm))
1602 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001603
Joerg Roedel0295ad72009-08-07 11:49:37 +02001604 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1605 svm->vmcb->control.exit_code_hi = 0;
1606 svm->vmcb->control.exit_info_1 = error_code;
1607 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1608
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001609 vmexit = nested_svm_intercept(svm);
1610 if (vmexit == NESTED_EXIT_DONE)
1611 svm->nested.exit_required = true;
1612
1613 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001614}
1615
Joerg Roedel8fe54652010-02-19 16:23:01 +01001616/* This function returns true if it is save to enable the irq window */
1617static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001618{
Joerg Roedel26666952009-08-07 11:49:46 +02001619 if (!is_nested(svm))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001620 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001621
Joerg Roedel26666952009-08-07 11:49:46 +02001622 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001623 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001624
Joerg Roedel26666952009-08-07 11:49:46 +02001625 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001626 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001627
Joerg Roedel197717d2010-02-24 18:59:19 +01001628 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
1629 svm->vmcb->control.exit_info_1 = 0;
1630 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02001631
Joerg Roedelcd3ff652009-10-09 16:08:26 +02001632 if (svm->nested.intercept & 1ULL) {
1633 /*
1634 * The #vmexit can't be emulated here directly because this
1635 * code path runs with irqs and preemtion disabled. A
1636 * #vmexit emulation might sleep. Only signal request for
1637 * the #vmexit here.
1638 */
1639 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02001640 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01001641 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001642 }
1643
Joerg Roedel8fe54652010-02-19 16:23:01 +01001644 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001645}
1646
Joerg Roedel887f5002010-02-24 18:59:12 +01001647/* This function returns true if it is save to enable the nmi window */
1648static inline bool nested_svm_nmi(struct vcpu_svm *svm)
1649{
1650 if (!is_nested(svm))
1651 return true;
1652
1653 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
1654 return true;
1655
1656 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
1657 svm->nested.exit_required = true;
1658
1659 return false;
1660}
1661
Joerg Roedel7597f122010-02-19 16:23:00 +01001662static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001663{
1664 struct page *page;
1665
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01001666 might_sleep();
1667
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001668 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001669 if (is_error_page(page))
1670 goto error;
1671
Joerg Roedel7597f122010-02-19 16:23:00 +01001672 *_page = page;
1673
1674 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001675
1676error:
1677 kvm_release_page_clean(page);
1678 kvm_inject_gp(&svm->vcpu, 0);
1679
1680 return NULL;
1681}
1682
Joerg Roedel7597f122010-02-19 16:23:00 +01001683static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001684{
Joerg Roedel7597f122010-02-19 16:23:00 +01001685 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001686 kvm_release_page_dirty(page);
1687}
1688
Joerg Roedelce2ac082010-03-01 15:34:39 +01001689static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001690{
Joerg Roedelce2ac082010-03-01 15:34:39 +01001691 unsigned port;
1692 u8 val, bit;
1693 u64 gpa;
1694
1695 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
1696 return NESTED_EXIT_HOST;
1697
1698 port = svm->vmcb->control.exit_info_1 >> 16;
1699 gpa = svm->nested.vmcb_iopm + (port / 8);
1700 bit = port % 8;
1701 val = 0;
1702
1703 if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1))
1704 val &= (1 << bit);
1705
1706 return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1707}
1708
Joerg Roedeld2477822010-03-01 15:34:34 +01001709static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001710{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001711 u32 offset, msr, value;
1712 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001713
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001714 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01001715 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001716
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001717 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1718 offset = svm_msrpm_offset(msr);
1719 write = svm->vmcb->control.exit_info_1 & 1;
1720 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001721
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001722 if (offset == MSR_INVALID)
1723 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001724
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001725 /* Offset is in 32 bit units but need in 8 bit units */
1726 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001727
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001728 if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4))
1729 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001730
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001731 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001732}
1733
Joerg Roedel410e4d52009-08-07 11:49:44 +02001734static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001735{
Alexander Grafcf74a782008-11-25 20:17:08 +01001736 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001737
Joerg Roedel410e4d52009-08-07 11:49:44 +02001738 switch (exit_code) {
1739 case SVM_EXIT_INTR:
1740 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02001741 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02001742 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001743 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01001744 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001745 if (npt_enabled)
1746 return NESTED_EXIT_HOST;
1747 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001748 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Joerg Roedele0231712010-02-24 18:59:10 +01001749 /* When we're shadowing, trap PFs */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001750 if (!npt_enabled)
1751 return NESTED_EXIT_HOST;
1752 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001753 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
1754 nm_interception(svm);
1755 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001756 default:
1757 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001758 }
1759
Joerg Roedel410e4d52009-08-07 11:49:44 +02001760 return NESTED_EXIT_CONTINUE;
1761}
1762
1763/*
1764 * If this function returns true, this #vmexit was already handled
1765 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001766static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001767{
1768 u32 exit_code = svm->vmcb->control.exit_code;
1769 int vmexit = NESTED_EXIT_HOST;
1770
Alexander Grafcf74a782008-11-25 20:17:08 +01001771 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001772 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001773 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001774 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01001775 case SVM_EXIT_IOIO:
1776 vmexit = nested_svm_intercept_ioio(svm);
1777 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001778 case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR8: {
1779 u32 cr_bits = 1 << (exit_code - SVM_EXIT_READ_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001780 if (svm->nested.intercept_cr_read & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001781 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001782 break;
1783 }
1784 case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR8: {
1785 u32 cr_bits = 1 << (exit_code - SVM_EXIT_WRITE_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001786 if (svm->nested.intercept_cr_write & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001787 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001788 break;
1789 }
1790 case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR7: {
1791 u32 dr_bits = 1 << (exit_code - SVM_EXIT_READ_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001792 if (svm->nested.intercept_dr_read & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001793 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001794 break;
1795 }
1796 case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR7: {
1797 u32 dr_bits = 1 << (exit_code - SVM_EXIT_WRITE_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001798 if (svm->nested.intercept_dr_write & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001799 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001800 break;
1801 }
1802 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1803 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001804 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001805 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001806 break;
1807 }
Joerg Roedel228070b2010-04-22 12:33:10 +02001808 case SVM_EXIT_ERR: {
1809 vmexit = NESTED_EXIT_DONE;
1810 break;
1811 }
Alexander Grafcf74a782008-11-25 20:17:08 +01001812 default: {
1813 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001814 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001815 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001816 }
1817 }
1818
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001819 return vmexit;
1820}
1821
1822static int nested_svm_exit_handled(struct vcpu_svm *svm)
1823{
1824 int vmexit;
1825
1826 vmexit = nested_svm_intercept(svm);
1827
1828 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001829 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001830
1831 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001832}
1833
Joerg Roedel0460a972009-08-07 11:49:31 +02001834static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
1835{
1836 struct vmcb_control_area *dst = &dst_vmcb->control;
1837 struct vmcb_control_area *from = &from_vmcb->control;
1838
1839 dst->intercept_cr_read = from->intercept_cr_read;
1840 dst->intercept_cr_write = from->intercept_cr_write;
1841 dst->intercept_dr_read = from->intercept_dr_read;
1842 dst->intercept_dr_write = from->intercept_dr_write;
1843 dst->intercept_exceptions = from->intercept_exceptions;
1844 dst->intercept = from->intercept;
1845 dst->iopm_base_pa = from->iopm_base_pa;
1846 dst->msrpm_base_pa = from->msrpm_base_pa;
1847 dst->tsc_offset = from->tsc_offset;
1848 dst->asid = from->asid;
1849 dst->tlb_ctl = from->tlb_ctl;
1850 dst->int_ctl = from->int_ctl;
1851 dst->int_vector = from->int_vector;
1852 dst->int_state = from->int_state;
1853 dst->exit_code = from->exit_code;
1854 dst->exit_code_hi = from->exit_code_hi;
1855 dst->exit_info_1 = from->exit_info_1;
1856 dst->exit_info_2 = from->exit_info_2;
1857 dst->exit_int_info = from->exit_int_info;
1858 dst->exit_int_info_err = from->exit_int_info_err;
1859 dst->nested_ctl = from->nested_ctl;
1860 dst->event_inj = from->event_inj;
1861 dst->event_inj_err = from->event_inj_err;
1862 dst->nested_cr3 = from->nested_cr3;
1863 dst->lbr_ctl = from->lbr_ctl;
1864}
1865
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001866static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001867{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001868 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001869 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02001870 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01001871 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01001872
Joerg Roedel17897f32009-10-09 16:08:29 +02001873 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
1874 vmcb->control.exit_info_1,
1875 vmcb->control.exit_info_2,
1876 vmcb->control.exit_int_info,
1877 vmcb->control.exit_int_info_err);
1878
Joerg Roedel7597f122010-02-19 16:23:00 +01001879 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001880 if (!nested_vmcb)
1881 return 1;
1882
Joerg Roedel06fc77722010-02-19 16:23:07 +01001883 /* Exit nested SVM mode */
1884 svm->nested.vmcb = 0;
1885
Alexander Grafcf74a782008-11-25 20:17:08 +01001886 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02001887 disable_gif(svm);
1888
1889 nested_vmcb->save.es = vmcb->save.es;
1890 nested_vmcb->save.cs = vmcb->save.cs;
1891 nested_vmcb->save.ss = vmcb->save.ss;
1892 nested_vmcb->save.ds = vmcb->save.ds;
1893 nested_vmcb->save.gdtr = vmcb->save.gdtr;
1894 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001895 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedel2be4fc72010-04-22 12:33:09 +02001896 nested_vmcb->save.cr3 = svm->vcpu.arch.cr3;
Joerg Roedel33740e42009-08-07 11:49:29 +02001897 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001898 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Joerg Roedel33740e42009-08-07 11:49:29 +02001899 nested_vmcb->save.rflags = vmcb->save.rflags;
1900 nested_vmcb->save.rip = vmcb->save.rip;
1901 nested_vmcb->save.rsp = vmcb->save.rsp;
1902 nested_vmcb->save.rax = vmcb->save.rax;
1903 nested_vmcb->save.dr7 = vmcb->save.dr7;
1904 nested_vmcb->save.dr6 = vmcb->save.dr6;
1905 nested_vmcb->save.cpl = vmcb->save.cpl;
1906
1907 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
1908 nested_vmcb->control.int_vector = vmcb->control.int_vector;
1909 nested_vmcb->control.int_state = vmcb->control.int_state;
1910 nested_vmcb->control.exit_code = vmcb->control.exit_code;
1911 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
1912 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
1913 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
1914 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
1915 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Alexander Graf8d23c462009-10-09 16:08:25 +02001916
1917 /*
1918 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
1919 * to make sure that we do not lose injected events. So check event_inj
1920 * here and copy it to exit_int_info if it is valid.
1921 * Exit_int_info and event_inj can't be both valid because the case
1922 * below only happens on a VMRUN instruction intercept which has
1923 * no valid exit_int_info set.
1924 */
1925 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
1926 struct vmcb_control_area *nc = &nested_vmcb->control;
1927
1928 nc->exit_int_info = vmcb->control.event_inj;
1929 nc->exit_int_info_err = vmcb->control.event_inj_err;
1930 }
1931
Joerg Roedel33740e42009-08-07 11:49:29 +02001932 nested_vmcb->control.tlb_ctl = 0;
1933 nested_vmcb->control.event_inj = 0;
1934 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001935
1936 /* We always set V_INTR_MASKING and remember the old value in hflags */
1937 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
1938 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
1939
Alexander Grafcf74a782008-11-25 20:17:08 +01001940 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02001941 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01001942
Alexander Graf219b65d2009-06-15 15:21:25 +02001943 kvm_clear_exception_queue(&svm->vcpu);
1944 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01001945
1946 /* Restore selected save entries */
1947 svm->vmcb->save.es = hsave->save.es;
1948 svm->vmcb->save.cs = hsave->save.cs;
1949 svm->vmcb->save.ss = hsave->save.ss;
1950 svm->vmcb->save.ds = hsave->save.ds;
1951 svm->vmcb->save.gdtr = hsave->save.gdtr;
1952 svm->vmcb->save.idtr = hsave->save.idtr;
1953 svm->vmcb->save.rflags = hsave->save.rflags;
1954 svm_set_efer(&svm->vcpu, hsave->save.efer);
1955 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
1956 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
1957 if (npt_enabled) {
1958 svm->vmcb->save.cr3 = hsave->save.cr3;
1959 svm->vcpu.arch.cr3 = hsave->save.cr3;
1960 } else {
1961 kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
1962 }
1963 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
1964 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
1965 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
1966 svm->vmcb->save.dr7 = 0;
1967 svm->vmcb->save.cpl = 0;
1968 svm->vmcb->control.exit_int_info = 0;
1969
Joerg Roedel7597f122010-02-19 16:23:00 +01001970 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01001971
1972 kvm_mmu_reset_context(&svm->vcpu);
1973 kvm_mmu_load(&svm->vcpu);
1974
1975 return 0;
1976}
Alexander Graf3d6368e2008-11-25 20:17:07 +01001977
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001978static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01001979{
Joerg Roedel323c3d82010-03-01 15:34:37 +01001980 /*
1981 * This function merges the msr permission bitmaps of kvm and the
1982 * nested vmcb. It is omptimized in that it only merges the parts where
1983 * the kvm msr permission bitmap may contain zero bits
1984 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01001985 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001986
Joerg Roedel323c3d82010-03-01 15:34:37 +01001987 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
1988 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001989
Joerg Roedel323c3d82010-03-01 15:34:37 +01001990 for (i = 0; i < MSRPM_OFFSETS; i++) {
1991 u32 value, p;
1992 u64 offset;
1993
1994 if (msrpm_offsets[i] == 0xffffffff)
1995 break;
1996
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001997 p = msrpm_offsets[i];
1998 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01001999
2000 if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4))
2001 return false;
2002
2003 svm->nested.msrpm[p] = svm->msrpm[p] | value;
2004 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002005
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002006 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002007
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002008 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002009}
2010
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002011static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002012{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002013 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002014 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02002015 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002016 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01002017 u64 vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002018
Joerg Roedel06fc77722010-02-19 16:23:07 +01002019 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002020
Joerg Roedel7597f122010-02-19 16:23:00 +01002021 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002022 if (!nested_vmcb)
2023 return false;
2024
Joerg Roedelecf14052010-02-24 18:59:13 +01002025 trace_kvm_nested_vmrun(svm->vmcb->save.rip - 3, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02002026 nested_vmcb->save.rip,
2027 nested_vmcb->control.int_ctl,
2028 nested_vmcb->control.event_inj,
2029 nested_vmcb->control.nested_ctl);
2030
Joerg Roedel2e554e82010-02-24 18:59:14 +01002031 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr_read,
2032 nested_vmcb->control.intercept_cr_write,
2033 nested_vmcb->control.intercept_exceptions,
2034 nested_vmcb->control.intercept);
2035
Alexander Graf3d6368e2008-11-25 20:17:07 +01002036 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02002037 kvm_clear_exception_queue(&svm->vcpu);
2038 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002039
Joerg Roedele0231712010-02-24 18:59:10 +01002040 /*
2041 * Save the old vmcb, so we don't need to pick what we save, but can
2042 * restore everything when a VMEXIT occurs
2043 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02002044 hsave->save.es = vmcb->save.es;
2045 hsave->save.cs = vmcb->save.cs;
2046 hsave->save.ss = vmcb->save.ss;
2047 hsave->save.ds = vmcb->save.ds;
2048 hsave->save.gdtr = vmcb->save.gdtr;
2049 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02002050 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002051 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002052 hsave->save.cr4 = svm->vcpu.arch.cr4;
2053 hsave->save.rflags = vmcb->save.rflags;
2054 hsave->save.rip = svm->next_rip;
2055 hsave->save.rsp = vmcb->save.rsp;
2056 hsave->save.rax = vmcb->save.rax;
2057 if (npt_enabled)
2058 hsave->save.cr3 = vmcb->save.cr3;
2059 else
2060 hsave->save.cr3 = svm->vcpu.arch.cr3;
2061
Joerg Roedel0460a972009-08-07 11:49:31 +02002062 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002063
2064 if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
2065 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2066 else
2067 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2068
2069 /* Load the nested guest state */
2070 svm->vmcb->save.es = nested_vmcb->save.es;
2071 svm->vmcb->save.cs = nested_vmcb->save.cs;
2072 svm->vmcb->save.ss = nested_vmcb->save.ss;
2073 svm->vmcb->save.ds = nested_vmcb->save.ds;
2074 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2075 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
2076 svm->vmcb->save.rflags = nested_vmcb->save.rflags;
2077 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2078 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2079 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2080 if (npt_enabled) {
2081 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2082 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002083 } else
Alexander Graf3d6368e2008-11-25 20:17:07 +01002084 kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002085
2086 /* Guest paging mode is active - reset mmu */
2087 kvm_mmu_reset_context(&svm->vcpu);
2088
Joerg Roedeldefbba52009-08-07 11:49:30 +02002089 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002090 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2091 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2092 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01002093
Alexander Graf3d6368e2008-11-25 20:17:07 +01002094 /* In case we don't even reach vcpu_run, the fields are not updated */
2095 svm->vmcb->save.rax = nested_vmcb->save.rax;
2096 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2097 svm->vmcb->save.rip = nested_vmcb->save.rip;
2098 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2099 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2100 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2101
Joerg Roedelf7138532010-03-01 15:34:40 +01002102 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002103 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002104
Joerg Roedelaad42c62009-08-07 11:49:34 +02002105 /* cache intercepts */
2106 svm->nested.intercept_cr_read = nested_vmcb->control.intercept_cr_read;
2107 svm->nested.intercept_cr_write = nested_vmcb->control.intercept_cr_write;
2108 svm->nested.intercept_dr_read = nested_vmcb->control.intercept_dr_read;
2109 svm->nested.intercept_dr_write = nested_vmcb->control.intercept_dr_write;
2110 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2111 svm->nested.intercept = nested_vmcb->control.intercept;
2112
Alexander Graf3d6368e2008-11-25 20:17:07 +01002113 force_new_asid(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002114 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002115 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2116 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2117 else
2118 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2119
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002120 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2121 /* We only want the cr8 intercept bits of the guest */
2122 svm->vmcb->control.intercept_cr_read &= ~INTERCEPT_CR8_MASK;
2123 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
2124 }
2125
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002126 /* We don't want to see VMMCALLs from a nested guest */
2127 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VMMCALL);
2128
Joerg Roedele0231712010-02-24 18:59:10 +01002129 /*
2130 * We don't want a nested guest to be more powerful than the guest, so
2131 * all intercepts are ORed
2132 */
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002133 svm->vmcb->control.intercept_cr_read |=
2134 nested_vmcb->control.intercept_cr_read;
2135 svm->vmcb->control.intercept_cr_write |=
2136 nested_vmcb->control.intercept_cr_write;
2137 svm->vmcb->control.intercept_dr_read |=
2138 nested_vmcb->control.intercept_dr_read;
2139 svm->vmcb->control.intercept_dr_write |=
2140 nested_vmcb->control.intercept_dr_write;
2141 svm->vmcb->control.intercept_exceptions |=
2142 nested_vmcb->control.intercept_exceptions;
2143
2144 svm->vmcb->control.intercept |= nested_vmcb->control.intercept;
2145
2146 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002147 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2148 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2149 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002150 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2151 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2152
Joerg Roedel7597f122010-02-19 16:23:00 +01002153 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002154
Joerg Roedel06fc77722010-02-19 16:23:07 +01002155 /* nested_vmcb is our indicator if nested SVM is activated */
2156 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002157
Joerg Roedel2af91942009-08-07 11:49:28 +02002158 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002159
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002160 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002161}
2162
Joerg Roedel9966bf62009-08-07 11:49:40 +02002163static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002164{
2165 to_vmcb->save.fs = from_vmcb->save.fs;
2166 to_vmcb->save.gs = from_vmcb->save.gs;
2167 to_vmcb->save.tr = from_vmcb->save.tr;
2168 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2169 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2170 to_vmcb->save.star = from_vmcb->save.star;
2171 to_vmcb->save.lstar = from_vmcb->save.lstar;
2172 to_vmcb->save.cstar = from_vmcb->save.cstar;
2173 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2174 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2175 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2176 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002177}
2178
Avi Kivity851ba692009-08-24 11:10:17 +03002179static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002180{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002181 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002182 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002183
Alexander Graf55426752008-11-25 20:17:06 +01002184 if (nested_svm_check_permissions(svm))
2185 return 1;
2186
2187 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2188 skip_emulated_instruction(&svm->vcpu);
2189
Joerg Roedel7597f122010-02-19 16:23:00 +01002190 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002191 if (!nested_vmcb)
2192 return 1;
2193
2194 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002195 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002196
2197 return 1;
2198}
2199
Avi Kivity851ba692009-08-24 11:10:17 +03002200static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002201{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002202 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002203 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002204
Alexander Graf55426752008-11-25 20:17:06 +01002205 if (nested_svm_check_permissions(svm))
2206 return 1;
2207
2208 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2209 skip_emulated_instruction(&svm->vcpu);
2210
Joerg Roedel7597f122010-02-19 16:23:00 +01002211 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002212 if (!nested_vmcb)
2213 return 1;
2214
2215 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002216 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002217
2218 return 1;
2219}
2220
Avi Kivity851ba692009-08-24 11:10:17 +03002221static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002222{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002223 if (nested_svm_check_permissions(svm))
2224 return 1;
2225
2226 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2227 skip_emulated_instruction(&svm->vcpu);
2228
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002229 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002230 return 1;
2231
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002232 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002233 goto failed;
2234
2235 return 1;
2236
2237failed:
2238
2239 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2240 svm->vmcb->control.exit_code_hi = 0;
2241 svm->vmcb->control.exit_info_1 = 0;
2242 svm->vmcb->control.exit_info_2 = 0;
2243
2244 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002245
2246 return 1;
2247}
2248
Avi Kivity851ba692009-08-24 11:10:17 +03002249static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002250{
2251 if (nested_svm_check_permissions(svm))
2252 return 1;
2253
2254 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2255 skip_emulated_instruction(&svm->vcpu);
2256
Joerg Roedel2af91942009-08-07 11:49:28 +02002257 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002258
2259 return 1;
2260}
2261
Avi Kivity851ba692009-08-24 11:10:17 +03002262static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002263{
2264 if (nested_svm_check_permissions(svm))
2265 return 1;
2266
2267 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2268 skip_emulated_instruction(&svm->vcpu);
2269
Joerg Roedel2af91942009-08-07 11:49:28 +02002270 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002271
2272 /* After a CLGI no interrupts should come */
2273 svm_clear_vintr(svm);
2274 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2275
2276 return 1;
2277}
2278
Avi Kivity851ba692009-08-24 11:10:17 +03002279static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002280{
2281 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002282
Joerg Roedelec1ff792009-10-09 16:08:31 +02002283 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
2284 vcpu->arch.regs[VCPU_REGS_RAX]);
2285
Alexander Grafff092382009-06-15 15:21:24 +02002286 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2287 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
2288
2289 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2290 skip_emulated_instruction(&svm->vcpu);
2291 return 1;
2292}
2293
Joerg Roedel532a46b2009-10-09 16:08:32 +02002294static int skinit_interception(struct vcpu_svm *svm)
2295{
2296 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
2297
2298 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2299 return 1;
2300}
2301
Avi Kivity851ba692009-08-24 11:10:17 +03002302static int invalid_op_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002303{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002304 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002305 return 1;
2306}
2307
Avi Kivity851ba692009-08-24 11:10:17 +03002308static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002309{
Izik Eidus37817f22008-03-24 23:14:53 +02002310 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002311 int reason;
2312 int int_type = svm->vmcb->control.exit_int_info &
2313 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002314 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002315 uint32_t type =
2316 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2317 uint32_t idt_v =
2318 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002319 bool has_error_code = false;
2320 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002321
2322 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002323
Izik Eidus37817f22008-03-24 23:14:53 +02002324 if (svm->vmcb->control.exit_info_2 &
2325 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002326 reason = TASK_SWITCH_IRET;
2327 else if (svm->vmcb->control.exit_info_2 &
2328 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2329 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002330 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002331 reason = TASK_SWITCH_GATE;
2332 else
2333 reason = TASK_SWITCH_CALL;
2334
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002335 if (reason == TASK_SWITCH_GATE) {
2336 switch (type) {
2337 case SVM_EXITINTINFO_TYPE_NMI:
2338 svm->vcpu.arch.nmi_injected = false;
2339 break;
2340 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002341 if (svm->vmcb->control.exit_info_2 &
2342 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2343 has_error_code = true;
2344 error_code =
2345 (u32)svm->vmcb->control.exit_info_2;
2346 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002347 kvm_clear_exception_queue(&svm->vcpu);
2348 break;
2349 case SVM_EXITINTINFO_TYPE_INTR:
2350 kvm_clear_interrupt_queue(&svm->vcpu);
2351 break;
2352 default:
2353 break;
2354 }
2355 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002356
Gleb Natapov8317c292009-04-12 13:37:02 +03002357 if (reason != TASK_SWITCH_GATE ||
2358 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2359 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002360 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2361 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002362
Gleb Natapovacb54512010-04-15 21:03:50 +03002363 if (kvm_task_switch(&svm->vcpu, tss_selector, reason,
2364 has_error_code, error_code) == EMULATE_FAIL) {
2365 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2366 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2367 svm->vcpu.run->internal.ndata = 0;
2368 return 0;
2369 }
2370 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002371}
2372
Avi Kivity851ba692009-08-24 11:10:17 +03002373static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002374{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002375 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002376 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002377 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002378}
2379
Avi Kivity851ba692009-08-24 11:10:17 +03002380static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002381{
2382 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02002383 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002384 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002385 return 1;
2386}
2387
Avi Kivity851ba692009-08-24 11:10:17 +03002388static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002389{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002390 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002391}
2392
Avi Kivity851ba692009-08-24 11:10:17 +03002393static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002394{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002395 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002396}
2397
Avi Kivity851ba692009-08-24 11:10:17 +03002398static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01002399{
Avi Kivity851ba692009-08-24 11:10:17 +03002400 struct kvm_run *kvm_run = svm->vcpu.run;
2401
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002402 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
2403 /* instruction emulation calls kvm_set_cr8() */
Avi Kivity851ba692009-08-24 11:10:17 +03002404 emulate_instruction(&svm->vcpu, 0, 0, 0);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002405 if (irqchip_in_kernel(svm->vcpu.kvm)) {
2406 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
Joerg Roedel1d075432007-12-06 21:02:25 +01002407 return 1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002408 }
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002409 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
2410 return 1;
Joerg Roedel1d075432007-12-06 21:02:25 +01002411 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2412 return 0;
2413}
2414
Avi Kivity6aa8b732006-12-10 02:21:36 -08002415static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
2416{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002417 struct vcpu_svm *svm = to_svm(vcpu);
2418
Avi Kivity6aa8b732006-12-10 02:21:36 -08002419 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302420 case MSR_IA32_TSC: {
Joerg Roedel20824f32009-09-16 15:24:18 +02002421 u64 tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002422
Joerg Roedel20824f32009-09-16 15:24:18 +02002423 if (is_nested(svm))
2424 tsc_offset = svm->nested.hsave->control.tsc_offset;
2425 else
2426 tsc_offset = svm->vmcb->control.tsc_offset;
2427
2428 *data = tsc_offset + native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002429 break;
2430 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08002431 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002432 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002433 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08002434#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002435 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002436 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002437 break;
2438 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002439 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002440 break;
2441 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002442 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002443 break;
2444 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002445 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002446 break;
2447#endif
2448 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002449 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002450 break;
2451 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002452 *data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002453 break;
2454 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002455 *data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002456 break;
Joerg Roedele0231712010-02-24 18:59:10 +01002457 /*
2458 * Nobody will change the following 5 values in the VMCB so we can
2459 * safely return them on rdmsr. They will always be 0 until LBRV is
2460 * implemented.
2461 */
Joerg Roedela2938c82008-02-13 16:30:28 +01002462 case MSR_IA32_DEBUGCTLMSR:
2463 *data = svm->vmcb->save.dbgctl;
2464 break;
2465 case MSR_IA32_LASTBRANCHFROMIP:
2466 *data = svm->vmcb->save.br_from;
2467 break;
2468 case MSR_IA32_LASTBRANCHTOIP:
2469 *data = svm->vmcb->save.br_to;
2470 break;
2471 case MSR_IA32_LASTINTFROMIP:
2472 *data = svm->vmcb->save.last_excp_from;
2473 break;
2474 case MSR_IA32_LASTINTTOIP:
2475 *data = svm->vmcb->save.last_excp_to;
2476 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002477 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002478 *data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002479 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002480 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002481 *data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002482 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01002483 case MSR_IA32_UCODE_REV:
2484 *data = 0x01000065;
2485 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002486 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002487 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002488 }
2489 return 0;
2490}
2491
Avi Kivity851ba692009-08-24 11:10:17 +03002492static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002493{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002494 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002495 u64 data;
2496
Avi Kivity59200272010-01-25 19:47:02 +02002497 if (svm_get_msr(&svm->vcpu, ecx, &data)) {
2498 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002499 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002500 } else {
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002501 trace_kvm_msr_read(ecx, data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002502
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002503 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002504 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002505 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002506 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002507 }
2508 return 1;
2509}
2510
Joerg Roedel4a810182010-02-24 18:59:15 +01002511static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2512{
2513 struct vcpu_svm *svm = to_svm(vcpu);
2514 int svm_dis, chg_mask;
2515
2516 if (data & ~SVM_VM_CR_VALID_MASK)
2517 return 1;
2518
2519 chg_mask = SVM_VM_CR_VALID_MASK;
2520
2521 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2522 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2523
2524 svm->nested.vm_cr_msr &= ~chg_mask;
2525 svm->nested.vm_cr_msr |= (data & chg_mask);
2526
2527 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2528
2529 /* check for svm_disable while efer.svme is set */
2530 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2531 return 1;
2532
2533 return 0;
2534}
2535
Avi Kivity6aa8b732006-12-10 02:21:36 -08002536static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
2537{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002538 struct vcpu_svm *svm = to_svm(vcpu);
2539
Avi Kivity6aa8b732006-12-10 02:21:36 -08002540 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302541 case MSR_IA32_TSC: {
Joerg Roedel20824f32009-09-16 15:24:18 +02002542 u64 tsc_offset = data - native_read_tsc();
2543 u64 g_tsc_offset = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002544
Joerg Roedel20824f32009-09-16 15:24:18 +02002545 if (is_nested(svm)) {
2546 g_tsc_offset = svm->vmcb->control.tsc_offset -
2547 svm->nested.hsave->control.tsc_offset;
2548 svm->nested.hsave->control.tsc_offset = tsc_offset;
2549 }
2550
2551 svm->vmcb->control.tsc_offset = tsc_offset + g_tsc_offset;
2552
Avi Kivity6aa8b732006-12-10 02:21:36 -08002553 break;
2554 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08002555 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002556 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002557 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08002558#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002559 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002560 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002561 break;
2562 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002563 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002564 break;
2565 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002566 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002567 break;
2568 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002569 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002570 break;
2571#endif
2572 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002573 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002574 break;
2575 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002576 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002577 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002578 break;
2579 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002580 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002581 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002582 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002583 case MSR_IA32_DEBUGCTLMSR:
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002584 if (!svm_has(SVM_FEATURE_LBRV)) {
2585 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002586 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002587 break;
2588 }
2589 if (data & DEBUGCTL_RESERVED_BITS)
2590 return 1;
2591
2592 svm->vmcb->save.dbgctl = data;
2593 if (data & (1ULL<<0))
2594 svm_enable_lbrv(svm);
2595 else
2596 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01002597 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002598 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002599 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002600 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002601 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002602 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002603 case MSR_VM_IGNNE:
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002604 pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
2605 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002606 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002607 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002608 }
2609 return 0;
2610}
2611
Avi Kivity851ba692009-08-24 11:10:17 +03002612static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002613{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002614 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002615 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002616 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002617
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002618
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002619 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Avi Kivity59200272010-01-25 19:47:02 +02002620 if (svm_set_msr(&svm->vcpu, ecx, data)) {
2621 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002622 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002623 } else {
2624 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10002625 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02002626 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002627 return 1;
2628}
2629
Avi Kivity851ba692009-08-24 11:10:17 +03002630static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002631{
Rusty Russelle756fc62007-07-30 20:07:08 +10002632 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03002633 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002634 else
Avi Kivity851ba692009-08-24 11:10:17 +03002635 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002636}
2637
Avi Kivity851ba692009-08-24 11:10:17 +03002638static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08002639{
Avi Kivity851ba692009-08-24 11:10:17 +03002640 struct kvm_run *kvm_run = svm->vcpu.run;
2641
Alexander Graff0b85052008-11-25 20:17:01 +01002642 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03002643 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -08002644 /*
2645 * If the user space waits to inject interrupts, exit as soon as
2646 * possible
2647 */
Gleb Natapov80618232009-04-21 17:44:56 +03002648 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
2649 kvm_run->request_interrupt_window &&
2650 !kvm_cpu_has_interrupt(&svm->vcpu)) {
Rusty Russelle756fc62007-07-30 20:07:08 +10002651 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002652 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
2653 return 0;
2654 }
2655
2656 return 1;
2657}
2658
Mark Langsdorf565d0992009-10-06 14:25:02 -05002659static int pause_interception(struct vcpu_svm *svm)
2660{
2661 kvm_vcpu_on_spin(&(svm->vcpu));
2662 return 1;
2663}
2664
Avi Kivity851ba692009-08-24 11:10:17 +03002665static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
Joerg Roedele0231712010-02-24 18:59:10 +01002666 [SVM_EXIT_READ_CR0] = emulate_on_interception,
2667 [SVM_EXIT_READ_CR3] = emulate_on_interception,
2668 [SVM_EXIT_READ_CR4] = emulate_on_interception,
2669 [SVM_EXIT_READ_CR8] = emulate_on_interception,
Avi Kivityd2251572010-01-06 10:55:27 +02002670 [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002671 [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
2672 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
2673 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
2674 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
2675 [SVM_EXIT_READ_DR0] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002676 [SVM_EXIT_READ_DR1] = emulate_on_interception,
2677 [SVM_EXIT_READ_DR2] = emulate_on_interception,
2678 [SVM_EXIT_READ_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002679 [SVM_EXIT_READ_DR4] = emulate_on_interception,
2680 [SVM_EXIT_READ_DR5] = emulate_on_interception,
2681 [SVM_EXIT_READ_DR6] = emulate_on_interception,
2682 [SVM_EXIT_READ_DR7] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002683 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
2684 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
2685 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
2686 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002687 [SVM_EXIT_WRITE_DR4] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002688 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002689 [SVM_EXIT_WRITE_DR6] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002690 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002691 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
2692 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002693 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002694 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
2695 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
2696 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
2697 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02002698 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002699 [SVM_EXIT_SMI] = nop_on_interception,
2700 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08002701 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002702 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002703 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002704 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05002705 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002706 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03002707 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02002708 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002709 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002710 [SVM_EXIT_MSR] = msr_interception,
2711 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002712 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01002713 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02002714 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01002715 [SVM_EXIT_VMLOAD] = vmload_interception,
2716 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01002717 [SVM_EXIT_STGI] = stgi_interception,
2718 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02002719 [SVM_EXIT_SKINIT] = skinit_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002720 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01002721 [SVM_EXIT_MONITOR] = invalid_op_interception,
2722 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002723 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002724};
2725
Joerg Roedel3f10c842010-05-05 16:04:42 +02002726void dump_vmcb(struct kvm_vcpu *vcpu)
2727{
2728 struct vcpu_svm *svm = to_svm(vcpu);
2729 struct vmcb_control_area *control = &svm->vmcb->control;
2730 struct vmcb_save_area *save = &svm->vmcb->save;
2731
2732 pr_err("VMCB Control Area:\n");
2733 pr_err("cr_read: %04x\n", control->intercept_cr_read);
2734 pr_err("cr_write: %04x\n", control->intercept_cr_write);
2735 pr_err("dr_read: %04x\n", control->intercept_dr_read);
2736 pr_err("dr_write: %04x\n", control->intercept_dr_write);
2737 pr_err("exceptions: %08x\n", control->intercept_exceptions);
2738 pr_err("intercepts: %016llx\n", control->intercept);
2739 pr_err("pause filter count: %d\n", control->pause_filter_count);
2740 pr_err("iopm_base_pa: %016llx\n", control->iopm_base_pa);
2741 pr_err("msrpm_base_pa: %016llx\n", control->msrpm_base_pa);
2742 pr_err("tsc_offset: %016llx\n", control->tsc_offset);
2743 pr_err("asid: %d\n", control->asid);
2744 pr_err("tlb_ctl: %d\n", control->tlb_ctl);
2745 pr_err("int_ctl: %08x\n", control->int_ctl);
2746 pr_err("int_vector: %08x\n", control->int_vector);
2747 pr_err("int_state: %08x\n", control->int_state);
2748 pr_err("exit_code: %08x\n", control->exit_code);
2749 pr_err("exit_info1: %016llx\n", control->exit_info_1);
2750 pr_err("exit_info2: %016llx\n", control->exit_info_2);
2751 pr_err("exit_int_info: %08x\n", control->exit_int_info);
2752 pr_err("exit_int_info_err: %08x\n", control->exit_int_info_err);
2753 pr_err("nested_ctl: %lld\n", control->nested_ctl);
2754 pr_err("nested_cr3: %016llx\n", control->nested_cr3);
2755 pr_err("event_inj: %08x\n", control->event_inj);
2756 pr_err("event_inj_err: %08x\n", control->event_inj_err);
2757 pr_err("lbr_ctl: %lld\n", control->lbr_ctl);
2758 pr_err("next_rip: %016llx\n", control->next_rip);
2759 pr_err("VMCB State Save Area:\n");
2760 pr_err("es: s: %04x a: %04x l: %08x b: %016llx\n",
2761 save->es.selector, save->es.attrib,
2762 save->es.limit, save->es.base);
2763 pr_err("cs: s: %04x a: %04x l: %08x b: %016llx\n",
2764 save->cs.selector, save->cs.attrib,
2765 save->cs.limit, save->cs.base);
2766 pr_err("ss: s: %04x a: %04x l: %08x b: %016llx\n",
2767 save->ss.selector, save->ss.attrib,
2768 save->ss.limit, save->ss.base);
2769 pr_err("ds: s: %04x a: %04x l: %08x b: %016llx\n",
2770 save->ds.selector, save->ds.attrib,
2771 save->ds.limit, save->ds.base);
2772 pr_err("fs: s: %04x a: %04x l: %08x b: %016llx\n",
2773 save->fs.selector, save->fs.attrib,
2774 save->fs.limit, save->fs.base);
2775 pr_err("gs: s: %04x a: %04x l: %08x b: %016llx\n",
2776 save->gs.selector, save->gs.attrib,
2777 save->gs.limit, save->gs.base);
2778 pr_err("gdtr: s: %04x a: %04x l: %08x b: %016llx\n",
2779 save->gdtr.selector, save->gdtr.attrib,
2780 save->gdtr.limit, save->gdtr.base);
2781 pr_err("ldtr: s: %04x a: %04x l: %08x b: %016llx\n",
2782 save->ldtr.selector, save->ldtr.attrib,
2783 save->ldtr.limit, save->ldtr.base);
2784 pr_err("idtr: s: %04x a: %04x l: %08x b: %016llx\n",
2785 save->idtr.selector, save->idtr.attrib,
2786 save->idtr.limit, save->idtr.base);
2787 pr_err("tr: s: %04x a: %04x l: %08x b: %016llx\n",
2788 save->tr.selector, save->tr.attrib,
2789 save->tr.limit, save->tr.base);
2790 pr_err("cpl: %d efer: %016llx\n",
2791 save->cpl, save->efer);
2792 pr_err("cr0: %016llx cr2: %016llx\n",
2793 save->cr0, save->cr2);
2794 pr_err("cr3: %016llx cr4: %016llx\n",
2795 save->cr3, save->cr4);
2796 pr_err("dr6: %016llx dr7: %016llx\n",
2797 save->dr6, save->dr7);
2798 pr_err("rip: %016llx rflags: %016llx\n",
2799 save->rip, save->rflags);
2800 pr_err("rsp: %016llx rax: %016llx\n",
2801 save->rsp, save->rax);
2802 pr_err("star: %016llx lstar: %016llx\n",
2803 save->star, save->lstar);
2804 pr_err("cstar: %016llx sfmask: %016llx\n",
2805 save->cstar, save->sfmask);
2806 pr_err("kernel_gs_base: %016llx sysenter_cs: %016llx\n",
2807 save->kernel_gs_base, save->sysenter_cs);
2808 pr_err("sysenter_esp: %016llx sysenter_eip: %016llx\n",
2809 save->sysenter_esp, save->sysenter_eip);
2810 pr_err("gpat: %016llx dbgctl: %016llx\n",
2811 save->g_pat, save->dbgctl);
2812 pr_err("br_from: %016llx br_to: %016llx\n",
2813 save->br_from, save->br_to);
2814 pr_err("excp_from: %016llx excp_to: %016llx\n",
2815 save->last_excp_from, save->last_excp_to);
2816
2817}
2818
Avi Kivity851ba692009-08-24 11:10:17 +03002819static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002820{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002821 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002822 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002823 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002824
Avi Kivity5bfd8b52010-03-11 10:50:44 +02002825 trace_kvm_exit(exit_code, vcpu);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002826
Joerg Roedel2be4fc72010-04-22 12:33:09 +02002827 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR0_MASK))
2828 vcpu->arch.cr0 = svm->vmcb->save.cr0;
2829 if (npt_enabled)
2830 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002831
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002832 if (unlikely(svm->nested.exit_required)) {
2833 nested_svm_vmexit(svm);
2834 svm->nested.exit_required = false;
2835
2836 return 1;
2837 }
2838
Alexander Grafcf74a782008-11-25 20:17:08 +01002839 if (is_nested(svm)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02002840 int vmexit;
2841
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02002842 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
2843 svm->vmcb->control.exit_info_1,
2844 svm->vmcb->control.exit_info_2,
2845 svm->vmcb->control.exit_int_info,
2846 svm->vmcb->control.exit_int_info_err);
2847
Joerg Roedel410e4d52009-08-07 11:49:44 +02002848 vmexit = nested_svm_exit_special(svm);
2849
2850 if (vmexit == NESTED_EXIT_CONTINUE)
2851 vmexit = nested_svm_exit_handled(svm);
2852
2853 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01002854 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01002855 }
2856
Joerg Roedela5c38322009-08-07 11:49:32 +02002857 svm_complete_interrupts(svm);
2858
Avi Kivity04d2cc72007-09-10 18:10:54 +03002859 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
2860 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2861 kvm_run->fail_entry.hardware_entry_failure_reason
2862 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02002863 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
2864 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03002865 return 0;
2866 }
2867
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002868 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002869 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002870 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002871 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
2872 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002873 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002874 exit_code);
2875
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02002876 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08002877 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002878 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03002879 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002880 return 0;
2881 }
2882
Avi Kivity851ba692009-08-24 11:10:17 +03002883 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002884}
2885
2886static void reload_tss(struct kvm_vcpu *vcpu)
2887{
2888 int cpu = raw_smp_processor_id();
2889
Tejun Heo0fe1e002009-10-29 22:34:14 +09002890 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
2891 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002892 load_TR_desc();
2893}
2894
Rusty Russelle756fc62007-07-30 20:07:08 +10002895static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002896{
2897 int cpu = raw_smp_processor_id();
2898
Tejun Heo0fe1e002009-10-29 22:34:14 +09002899 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002900
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002901 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03002902 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09002903 if (svm->asid_generation != sd->asid_generation)
2904 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002905}
2906
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002907static void svm_inject_nmi(struct kvm_vcpu *vcpu)
2908{
2909 struct vcpu_svm *svm = to_svm(vcpu);
2910
2911 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
2912 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02002913 svm->vmcb->control.intercept |= (1ULL << INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002914 ++vcpu->stat.nmi_injections;
2915}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002916
Eddie Dong85f455f2007-07-06 12:20:49 +03002917static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002918{
2919 struct vmcb_control_area *control;
2920
Rusty Russelle756fc62007-07-30 20:07:08 +10002921 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03002922 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002923 control->int_ctl &= ~V_INTR_PRIO_MASK;
2924 control->int_ctl |= V_IRQ_MASK |
2925 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
2926}
2927
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002928static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03002929{
2930 struct vcpu_svm *svm = to_svm(vcpu);
2931
Joerg Roedel2af91942009-08-07 11:49:28 +02002932 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01002933
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03002934 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
2935 ++vcpu->stat.irq_injections;
2936
Alexander Graf219b65d2009-06-15 15:21:25 +02002937 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
2938 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002939}
2940
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002941static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
2942{
2943 struct vcpu_svm *svm = to_svm(vcpu);
2944
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002945 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
2946 return;
2947
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002948 if (irr == -1)
2949 return;
2950
2951 if (tpr >= irr)
2952 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK;
2953}
2954
2955static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02002956{
2957 struct vcpu_svm *svm = to_svm(vcpu);
2958 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02002959 int ret;
2960 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
2961 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
2962 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
2963
2964 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02002965}
2966
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002967static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
2968{
2969 struct vcpu_svm *svm = to_svm(vcpu);
2970
2971 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
2972}
2973
2974static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
2975{
2976 struct vcpu_svm *svm = to_svm(vcpu);
2977
2978 if (masked) {
2979 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02002980 svm->vmcb->control.intercept |= (1ULL << INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002981 } else {
2982 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02002983 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002984 }
2985}
2986
Gleb Natapov78646122009-03-23 12:12:11 +02002987static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
2988{
2989 struct vcpu_svm *svm = to_svm(vcpu);
2990 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02002991 int ret;
2992
2993 if (!gif_set(svm) ||
2994 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
2995 return 0;
2996
2997 ret = !!(vmcb->save.rflags & X86_EFLAGS_IF);
2998
2999 if (is_nested(svm))
3000 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
3001
3002 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02003003}
3004
Gleb Natapov9222be12009-04-23 17:14:37 +03003005static void enable_irq_window(struct kvm_vcpu *vcpu)
3006{
Alexander Graf219b65d2009-06-15 15:21:25 +02003007 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02003008
Joerg Roedele0231712010-02-24 18:59:10 +01003009 /*
3010 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3011 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3012 * get that intercept, this function will be called again though and
3013 * we'll get the vintr intercept.
3014 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01003015 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02003016 svm_set_vintr(svm);
3017 svm_inject_irq(svm, 0x0);
3018 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003019}
3020
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003021static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003022{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003023 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003024
Gleb Natapov44c11432009-05-11 13:35:52 +03003025 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
3026 == HF_NMI_MASK)
3027 return; /* IRET will cause a vm exit */
3028
Joerg Roedele0231712010-02-24 18:59:10 +01003029 /*
3030 * Something prevents NMI from been injected. Single step over possible
3031 * problem (IRET or exception injection or interrupt shadow)
3032 */
Jan Kiszka6be7d302009-10-18 13:24:54 +02003033 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03003034 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
3035 update_db_intercept(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003036}
3037
Izik Eiduscbc94022007-10-25 00:29:55 +02003038static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3039{
3040 return 0;
3041}
3042
Avi Kivityd9e368d2007-06-07 19:18:30 +03003043static void svm_flush_tlb(struct kvm_vcpu *vcpu)
3044{
3045 force_new_asid(vcpu);
3046}
3047
Avi Kivity04d2cc72007-09-10 18:10:54 +03003048static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
3049{
3050}
3051
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003052static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3053{
3054 struct vcpu_svm *svm = to_svm(vcpu);
3055
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003056 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
3057 return;
3058
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003059 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) {
3060 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03003061 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003062 }
3063}
3064
Joerg Roedel649d6862008-04-16 16:51:15 +02003065static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3066{
3067 struct vcpu_svm *svm = to_svm(vcpu);
3068 u64 cr8;
3069
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003070 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
3071 return;
3072
Joerg Roedel649d6862008-04-16 16:51:15 +02003073 cr8 = kvm_get_cr8(vcpu);
3074 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3075 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3076}
3077
Gleb Natapov9222be12009-04-23 17:14:37 +03003078static void svm_complete_interrupts(struct vcpu_svm *svm)
3079{
3080 u8 vector;
3081 int type;
3082 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01003083 unsigned int3_injected = svm->int3_injected;
3084
3085 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003086
Gleb Natapov44c11432009-05-11 13:35:52 +03003087 if (svm->vcpu.arch.hflags & HF_IRET_MASK)
3088 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
3089
Gleb Natapov9222be12009-04-23 17:14:37 +03003090 svm->vcpu.arch.nmi_injected = false;
3091 kvm_clear_exception_queue(&svm->vcpu);
3092 kvm_clear_interrupt_queue(&svm->vcpu);
3093
3094 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3095 return;
3096
3097 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3098 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3099
3100 switch (type) {
3101 case SVM_EXITINTINFO_TYPE_NMI:
3102 svm->vcpu.arch.nmi_injected = true;
3103 break;
3104 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01003105 /*
3106 * In case of software exceptions, do not reinject the vector,
3107 * but re-execute the instruction instead. Rewind RIP first
3108 * if we emulated INT3 before.
3109 */
3110 if (kvm_exception_is_soft(vector)) {
3111 if (vector == BP_VECTOR && int3_injected &&
3112 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
3113 kvm_rip_write(&svm->vcpu,
3114 kvm_rip_read(&svm->vcpu) -
3115 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02003116 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01003117 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003118 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3119 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003120 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03003121
3122 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003123 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03003124 break;
3125 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003126 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03003127 break;
3128 default:
3129 break;
3130 }
3131}
3132
Avi Kivity80e31d42008-07-14 14:44:59 +03003133#ifdef CONFIG_X86_64
3134#define R "r"
3135#else
3136#define R "e"
3137#endif
3138
Avi Kivity851ba692009-08-24 11:10:17 +03003139static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003140{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003141 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003142 u16 fs_selector;
3143 u16 gs_selector;
3144 u16 ldt_selector;
Avi Kivityd9e368d2007-06-07 19:18:30 +03003145
Joerg Roedel2041a062010-04-22 12:33:08 +02003146 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3147 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3148 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3149
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003150 /*
3151 * A vmexit emulation is required before the vcpu can be executed
3152 * again.
3153 */
3154 if (unlikely(svm->nested.exit_required))
3155 return;
3156
Rusty Russelle756fc62007-07-30 20:07:08 +10003157 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003158
Joerg Roedel649d6862008-04-16 16:51:15 +02003159 sync_lapic_to_cr8(vcpu);
3160
Avi Kivity6aa8b732006-12-10 02:21:36 -08003161 save_host_msrs(vcpu);
Avi Kivityd6e88ae2008-07-10 16:53:33 +03003162 fs_selector = kvm_read_fs();
3163 gs_selector = kvm_read_gs();
3164 ldt_selector = kvm_read_ldt();
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003165 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003166 /* required for live migration with NPT */
3167 if (npt_enabled)
3168 svm->vmcb->save.cr3 = vcpu->arch.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003169
Avi Kivity04d2cc72007-09-10 18:10:54 +03003170 clgi();
3171
3172 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08003173
Avi Kivity6aa8b732006-12-10 02:21:36 -08003174 asm volatile (
Avi Kivity80e31d42008-07-14 14:44:59 +03003175 "push %%"R"bp; \n\t"
3176 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
3177 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
3178 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
3179 "mov %c[rsi](%[svm]), %%"R"si \n\t"
3180 "mov %c[rdi](%[svm]), %%"R"di \n\t"
3181 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003182#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003183 "mov %c[r8](%[svm]), %%r8 \n\t"
3184 "mov %c[r9](%[svm]), %%r9 \n\t"
3185 "mov %c[r10](%[svm]), %%r10 \n\t"
3186 "mov %c[r11](%[svm]), %%r11 \n\t"
3187 "mov %c[r12](%[svm]), %%r12 \n\t"
3188 "mov %c[r13](%[svm]), %%r13 \n\t"
3189 "mov %c[r14](%[svm]), %%r14 \n\t"
3190 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003191#endif
3192
Avi Kivity6aa8b732006-12-10 02:21:36 -08003193 /* Enter guest mode */
Avi Kivity80e31d42008-07-14 14:44:59 +03003194 "push %%"R"ax \n\t"
3195 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003196 __ex(SVM_VMLOAD) "\n\t"
3197 __ex(SVM_VMRUN) "\n\t"
3198 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity80e31d42008-07-14 14:44:59 +03003199 "pop %%"R"ax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003200
3201 /* Save guest registers, load host registers */
Avi Kivity80e31d42008-07-14 14:44:59 +03003202 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
3203 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
3204 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
3205 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
3206 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
3207 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003208#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003209 "mov %%r8, %c[r8](%[svm]) \n\t"
3210 "mov %%r9, %c[r9](%[svm]) \n\t"
3211 "mov %%r10, %c[r10](%[svm]) \n\t"
3212 "mov %%r11, %c[r11](%[svm]) \n\t"
3213 "mov %%r12, %c[r12](%[svm]) \n\t"
3214 "mov %%r13, %c[r13](%[svm]) \n\t"
3215 "mov %%r14, %c[r14](%[svm]) \n\t"
3216 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003217#endif
Avi Kivity80e31d42008-07-14 14:44:59 +03003218 "pop %%"R"bp"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003219 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003220 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003221 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003222 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
3223 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
3224 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
3225 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
3226 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
3227 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003228#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003229 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
3230 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
3231 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
3232 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
3233 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
3234 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
3235 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
3236 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003237#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02003238 : "cc", "memory"
Avi Kivity80e31d42008-07-14 14:44:59 +03003239 , R"bx", R"cx", R"dx", R"si", R"di"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003240#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02003241 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
3242#endif
3243 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003244
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003245 vcpu->arch.cr2 = svm->vmcb->save.cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003246 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3247 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3248 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003249
Avi Kivityd6e88ae2008-07-10 16:53:33 +03003250 kvm_load_fs(fs_selector);
3251 kvm_load_gs(gs_selector);
3252 kvm_load_ldt(ldt_selector);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003253 load_host_msrs(vcpu);
3254
3255 reload_tss(vcpu);
3256
Avi Kivity56ba47d2007-11-07 17:14:18 +02003257 local_irq_disable();
3258
3259 stgi();
3260
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003261 sync_cr8_to_lapic(vcpu);
3262
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003263 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003264
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003265 if (npt_enabled) {
3266 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3267 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3268 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02003269
3270 /*
3271 * We need to handle MC intercepts here before the vcpu has a chance to
3272 * change the physical cpu
3273 */
3274 if (unlikely(svm->vmcb->control.exit_code ==
3275 SVM_EXIT_EXCP_BASE + MC_VECTOR))
3276 svm_handle_mce(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003277}
3278
Avi Kivity80e31d42008-07-14 14:44:59 +03003279#undef R
3280
Avi Kivity6aa8b732006-12-10 02:21:36 -08003281static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3282{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003283 struct vcpu_svm *svm = to_svm(vcpu);
3284
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003285 if (npt_enabled) {
3286 svm->vmcb->control.nested_cr3 = root;
3287 force_new_asid(vcpu);
3288 return;
3289 }
3290
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003291 svm->vmcb->save.cr3 = root;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003292 force_new_asid(vcpu);
3293}
3294
Avi Kivity6aa8b732006-12-10 02:21:36 -08003295static int is_disabled(void)
3296{
Joerg Roedel6031a612007-06-22 12:29:50 +03003297 u64 vm_cr;
3298
3299 rdmsrl(MSR_VM_CR, vm_cr);
3300 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3301 return 1;
3302
Avi Kivity6aa8b732006-12-10 02:21:36 -08003303 return 0;
3304}
3305
Ingo Molnar102d8322007-02-19 14:37:47 +02003306static void
3307svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3308{
3309 /*
3310 * Patch in the VMMCALL instruction:
3311 */
3312 hypercall[0] = 0x0f;
3313 hypercall[1] = 0x01;
3314 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003315}
3316
Yang, Sheng002c7f72007-07-31 14:23:01 +03003317static void svm_check_processor_compat(void *rtn)
3318{
3319 *(int *)rtn = 0;
3320}
3321
Avi Kivity774ead32007-12-26 13:57:04 +02003322static bool svm_cpu_has_accelerated_tpr(void)
3323{
3324 return false;
3325}
3326
Sheng Yang67253af2008-04-25 10:20:22 +08003327static int get_npt_level(void)
3328{
3329#ifdef CONFIG_X86_64
3330 return PT64_ROOT_LEVEL;
3331#else
3332 return PT32E_ROOT_LEVEL;
3333#endif
3334}
3335
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003336static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003337{
3338 return 0;
3339}
3340
Sheng Yang0e851882009-12-18 16:48:46 +08003341static void svm_cpuid_update(struct kvm_vcpu *vcpu)
3342{
3343}
3344
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003345static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
3346{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003347 switch (func) {
3348 case 0x8000000A:
3349 entry->eax = 1; /* SVM revision 1 */
3350 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
3351 ASID emulation to nested SVM */
3352 entry->ecx = 0; /* Reserved */
3353 entry->edx = 0; /* Do not support any additional features */
3354
3355 break;
3356 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003357}
3358
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003359static const struct trace_print_flags svm_exit_reasons_str[] = {
Joerg Roedele0231712010-02-24 18:59:10 +01003360 { SVM_EXIT_READ_CR0, "read_cr0" },
3361 { SVM_EXIT_READ_CR3, "read_cr3" },
3362 { SVM_EXIT_READ_CR4, "read_cr4" },
3363 { SVM_EXIT_READ_CR8, "read_cr8" },
3364 { SVM_EXIT_WRITE_CR0, "write_cr0" },
3365 { SVM_EXIT_WRITE_CR3, "write_cr3" },
3366 { SVM_EXIT_WRITE_CR4, "write_cr4" },
3367 { SVM_EXIT_WRITE_CR8, "write_cr8" },
3368 { SVM_EXIT_READ_DR0, "read_dr0" },
3369 { SVM_EXIT_READ_DR1, "read_dr1" },
3370 { SVM_EXIT_READ_DR2, "read_dr2" },
3371 { SVM_EXIT_READ_DR3, "read_dr3" },
3372 { SVM_EXIT_WRITE_DR0, "write_dr0" },
3373 { SVM_EXIT_WRITE_DR1, "write_dr1" },
3374 { SVM_EXIT_WRITE_DR2, "write_dr2" },
3375 { SVM_EXIT_WRITE_DR3, "write_dr3" },
3376 { SVM_EXIT_WRITE_DR5, "write_dr5" },
3377 { SVM_EXIT_WRITE_DR7, "write_dr7" },
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003378 { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
3379 { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
3380 { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
3381 { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
3382 { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
3383 { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
3384 { SVM_EXIT_INTR, "interrupt" },
3385 { SVM_EXIT_NMI, "nmi" },
3386 { SVM_EXIT_SMI, "smi" },
3387 { SVM_EXIT_INIT, "init" },
3388 { SVM_EXIT_VINTR, "vintr" },
3389 { SVM_EXIT_CPUID, "cpuid" },
3390 { SVM_EXIT_INVD, "invd" },
3391 { SVM_EXIT_HLT, "hlt" },
3392 { SVM_EXIT_INVLPG, "invlpg" },
3393 { SVM_EXIT_INVLPGA, "invlpga" },
3394 { SVM_EXIT_IOIO, "io" },
3395 { SVM_EXIT_MSR, "msr" },
3396 { SVM_EXIT_TASK_SWITCH, "task_switch" },
3397 { SVM_EXIT_SHUTDOWN, "shutdown" },
3398 { SVM_EXIT_VMRUN, "vmrun" },
3399 { SVM_EXIT_VMMCALL, "hypercall" },
3400 { SVM_EXIT_VMLOAD, "vmload" },
3401 { SVM_EXIT_VMSAVE, "vmsave" },
3402 { SVM_EXIT_STGI, "stgi" },
3403 { SVM_EXIT_CLGI, "clgi" },
3404 { SVM_EXIT_SKINIT, "skinit" },
3405 { SVM_EXIT_WBINVD, "wbinvd" },
3406 { SVM_EXIT_MONITOR, "monitor" },
3407 { SVM_EXIT_MWAIT, "mwait" },
3408 { SVM_EXIT_NPF, "npf" },
3409 { -1, NULL }
3410};
3411
Sheng Yang17cc3932010-01-05 19:02:27 +08003412static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02003413{
Sheng Yang17cc3932010-01-05 19:02:27 +08003414 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02003415}
3416
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003417static bool svm_rdtscp_supported(void)
3418{
3419 return false;
3420}
3421
Avi Kivity02daab22009-12-30 12:40:26 +02003422static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
3423{
3424 struct vcpu_svm *svm = to_svm(vcpu);
3425
Avi Kivity02daab22009-12-30 12:40:26 +02003426 svm->vmcb->control.intercept_exceptions |= 1 << NM_VECTOR;
Joerg Roedel66a562f2010-02-19 16:23:08 +01003427 if (is_nested(svm))
3428 svm->nested.hsave->control.intercept_exceptions |= 1 << NM_VECTOR;
3429 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02003430}
3431
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003432static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003433 .cpu_has_kvm_support = has_svm,
3434 .disabled_by_bios = is_disabled,
3435 .hardware_setup = svm_hardware_setup,
3436 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003437 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003438 .hardware_enable = svm_hardware_enable,
3439 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003440 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003441
3442 .vcpu_create = svm_create_vcpu,
3443 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003444 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003445
Avi Kivity04d2cc72007-09-10 18:10:54 +03003446 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003447 .vcpu_load = svm_vcpu_load,
3448 .vcpu_put = svm_vcpu_put,
3449
3450 .set_guest_debug = svm_guest_debug,
3451 .get_msr = svm_get_msr,
3452 .set_msr = svm_set_msr,
3453 .get_segment_base = svm_get_segment_base,
3454 .get_segment = svm_get_segment,
3455 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003456 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10003457 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02003458 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003459 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003460 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003461 .set_cr3 = svm_set_cr3,
3462 .set_cr4 = svm_set_cr4,
3463 .set_efer = svm_set_efer,
3464 .get_idt = svm_get_idt,
3465 .set_idt = svm_set_idt,
3466 .get_gdt = svm_get_gdt,
3467 .set_gdt = svm_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03003468 .set_dr7 = svm_set_dr7,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003469 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003470 .get_rflags = svm_get_rflags,
3471 .set_rflags = svm_set_rflags,
Avi Kivity6b52d182010-01-21 15:31:47 +02003472 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02003473 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003474
Avi Kivity6aa8b732006-12-10 02:21:36 -08003475 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003476
Avi Kivity6aa8b732006-12-10 02:21:36 -08003477 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003478 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003479 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04003480 .set_interrupt_shadow = svm_set_interrupt_shadow,
3481 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02003482 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003483 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003484 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02003485 .queue_exception = svm_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02003486 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003487 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003488 .get_nmi_mask = svm_get_nmi_mask,
3489 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003490 .enable_nmi_window = enable_nmi_window,
3491 .enable_irq_window = enable_irq_window,
3492 .update_cr8_intercept = update_cr8_intercept,
Izik Eiduscbc94022007-10-25 00:29:55 +02003493
3494 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003495 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003496 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003497
3498 .exit_reasons_str = svm_exit_reasons_str,
Sheng Yang17cc3932010-01-05 19:02:27 +08003499 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08003500
3501 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003502
3503 .rdtscp_supported = svm_rdtscp_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003504
3505 .set_supported_cpuid = svm_set_supported_cpuid,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003506};
3507
3508static int __init svm_init(void)
3509{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003510 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03003511 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003512}
3513
3514static void __exit svm_exit(void)
3515{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003516 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003517}
3518
3519module_init(svm_init)
3520module_exit(svm_exit)