blob: c929d007696dbb56cb8d1be7699c143cf87c7707 [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 Roedelcda00082010-09-02 17:29:46 +020091 /*
92 * If we vmexit during an instruction emulation we need this to restore
93 * the l1 guest rip after the emulation
94 */
95 unsigned long vmexit_rip;
96 unsigned long vmexit_rsp;
97 unsigned long vmexit_rax;
98
Joerg Roedelaad42c62009-08-07 11:49:34 +020099 /* cache for intercepts of the guest */
100 u16 intercept_cr_read;
101 u16 intercept_cr_write;
102 u16 intercept_dr_read;
103 u16 intercept_dr_write;
104 u32 intercept_exceptions;
105 u64 intercept;
106
Joerg Roedel5bd2edc2010-09-10 17:31:02 +0200107 /* Nested Paging related state */
108 u64 nested_cr3;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200109};
110
Joerg Roedel323c3d82010-03-01 15:34:37 +0100111#define MSRPM_OFFSETS 16
112static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
113
Avi Kivity6c8166a2009-05-31 18:15:37 +0300114struct vcpu_svm {
115 struct kvm_vcpu vcpu;
116 struct vmcb *vmcb;
117 unsigned long vmcb_pa;
118 struct svm_cpu_data *svm_data;
119 uint64_t asid_generation;
120 uint64_t sysenter_esp;
121 uint64_t sysenter_eip;
122
123 u64 next_rip;
124
125 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
126 u64 host_gs_base;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300127
128 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300129
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200130 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200131
132 bool nmi_singlestep;
Jan Kiszka66b71382010-02-23 17:47:56 +0100133
134 unsigned int3_injected;
135 unsigned long int3_rip;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300136};
137
Joerg Roedel455716f2010-03-01 15:34:35 +0100138#define MSR_INVALID 0xffffffffU
139
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100140static struct svm_direct_access_msrs {
141 u32 index; /* Index of the MSR */
142 bool always; /* True if intercept is always on */
143} direct_access_msrs[] = {
Brian Gerst8c065852010-07-17 09:03:26 -0400144 { .index = MSR_STAR, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100145 { .index = MSR_IA32_SYSENTER_CS, .always = true },
146#ifdef CONFIG_X86_64
147 { .index = MSR_GS_BASE, .always = true },
148 { .index = MSR_FS_BASE, .always = true },
149 { .index = MSR_KERNEL_GS_BASE, .always = true },
150 { .index = MSR_LSTAR, .always = true },
151 { .index = MSR_CSTAR, .always = true },
152 { .index = MSR_SYSCALL_MASK, .always = true },
153#endif
154 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
155 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
156 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
157 { .index = MSR_IA32_LASTINTTOIP, .always = false },
158 { .index = MSR_INVALID, .always = false },
Avi Kivity6aa8b732006-12-10 02:21:36 -0800159};
160
161/* enable NPT for AMD64 and X86 with PAE */
162#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
163static bool npt_enabled = true;
164#else
Joerg Roedele0231712010-02-24 18:59:10 +0100165static bool npt_enabled;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800166#endif
167static int npt = 1;
168
169module_param(npt, int, S_IRUGO);
170
Joerg Roedel4b6e4dc2009-08-07 11:49:48 +0200171static int nested = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800172module_param(nested, int, S_IRUGO);
173
174static void svm_flush_tlb(struct kvm_vcpu *vcpu);
Joerg Roedela5c38322009-08-07 11:49:32 +0200175static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800176
Joerg Roedel410e4d52009-08-07 11:49:44 +0200177static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100178static int nested_svm_intercept(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800179static int nested_svm_vmexit(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800180static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
181 bool has_error_code, u32 error_code);
182
183static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
184{
185 return container_of(vcpu, struct vcpu_svm, vcpu);
186}
187
188static inline bool is_nested(struct vcpu_svm *svm)
189{
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200190 return svm->nested.vmcb;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800191}
192
Joerg Roedel2af91942009-08-07 11:49:28 +0200193static inline void enable_gif(struct vcpu_svm *svm)
194{
195 svm->vcpu.arch.hflags |= HF_GIF_MASK;
196}
197
198static inline void disable_gif(struct vcpu_svm *svm)
199{
200 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
201}
202
203static inline bool gif_set(struct vcpu_svm *svm)
204{
205 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
206}
207
Avi Kivity6aa8b732006-12-10 02:21:36 -0800208static unsigned long iopm_base;
209
210struct kvm_ldttss_desc {
211 u16 limit0;
212 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100213 unsigned base1:8, type:5, dpl:2, p:1;
214 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800215 u32 base3;
216 u32 zero1;
217} __attribute__((packed));
218
219struct svm_cpu_data {
220 int cpu;
221
Avi Kivity5008fdf2007-04-02 13:05:50 +0300222 u64 asid_generation;
223 u32 max_asid;
224 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800225 struct kvm_ldttss_desc *tss_desc;
226
227 struct page *save_area;
228};
229
230static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
Joerg Roedel80b77062007-03-30 17:02:14 +0300231static uint32_t svm_features;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800232
233struct svm_init_data {
234 int cpu;
235 int r;
236};
237
238static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
239
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200240#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800241#define MSRS_RANGE_SIZE 2048
242#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
243
Joerg Roedel455716f2010-03-01 15:34:35 +0100244static u32 svm_msrpm_offset(u32 msr)
245{
246 u32 offset;
247 int i;
248
249 for (i = 0; i < NUM_MSR_MAPS; i++) {
250 if (msr < msrpm_ranges[i] ||
251 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
252 continue;
253
254 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
255 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
256
257 /* Now we have the u8 offset - but need the u32 offset */
258 return offset / 4;
259 }
260
261 /* MSR not in any range */
262 return MSR_INVALID;
263}
264
Avi Kivity6aa8b732006-12-10 02:21:36 -0800265#define MAX_INST_SIZE 15
266
Joerg Roedel80b77062007-03-30 17:02:14 +0300267static inline u32 svm_has(u32 feat)
268{
269 return svm_features & feat;
270}
271
Avi Kivity6aa8b732006-12-10 02:21:36 -0800272static inline void clgi(void)
273{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300274 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800275}
276
277static inline void stgi(void)
278{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300279 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800280}
281
282static inline void invlpga(unsigned long addr, u32 asid)
283{
Joerg Roedele0231712010-02-24 18:59:10 +0100284 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800285}
286
Avi Kivity6aa8b732006-12-10 02:21:36 -0800287static inline void force_new_asid(struct kvm_vcpu *vcpu)
288{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400289 to_svm(vcpu)->asid_generation--;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800290}
291
292static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
293{
294 force_new_asid(vcpu);
295}
296
Joerg Roedel4b161842010-09-10 17:31:03 +0200297static int get_npt_level(void)
298{
299#ifdef CONFIG_X86_64
300 return PT64_ROOT_LEVEL;
301#else
302 return PT32E_ROOT_LEVEL;
303#endif
304}
305
Avi Kivity6aa8b732006-12-10 02:21:36 -0800306static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
307{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000308 vcpu->arch.efer = efer;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100309 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600310 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800311
Alexander Graf9962d032008-11-25 20:17:02 +0100312 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800313}
314
Avi Kivity6aa8b732006-12-10 02:21:36 -0800315static int is_external_interrupt(u32 info)
316{
317 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
318 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
319}
320
Glauber Costa2809f5d2009-05-12 16:21:05 -0400321static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
322{
323 struct vcpu_svm *svm = to_svm(vcpu);
324 u32 ret = 0;
325
326 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Jan Kiszka48005f62010-02-19 19:38:07 +0100327 ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400328 return ret & mask;
329}
330
331static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
332{
333 struct vcpu_svm *svm = to_svm(vcpu);
334
335 if (mask == 0)
336 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
337 else
338 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
339
340}
341
Avi Kivity6aa8b732006-12-10 02:21:36 -0800342static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
343{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400344 struct vcpu_svm *svm = to_svm(vcpu);
345
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200346 if (svm->vmcb->control.next_rip != 0)
347 svm->next_rip = svm->vmcb->control.next_rip;
348
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400349 if (!svm->next_rip) {
Avi Kivity851ba692009-08-24 11:10:17 +0300350 if (emulate_instruction(vcpu, 0, 0, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300351 EMULATE_DONE)
352 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800353 return;
354 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300355 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
356 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
357 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800358
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300359 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400360 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800361}
362
Jan Kiszka116a4752010-02-23 17:47:54 +0100363static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200364 bool has_error_code, u32 error_code,
365 bool reinject)
Jan Kiszka116a4752010-02-23 17:47:54 +0100366{
367 struct vcpu_svm *svm = to_svm(vcpu);
368
Joerg Roedele0231712010-02-24 18:59:10 +0100369 /*
370 * If we are within a nested VM we'd better #VMEXIT and let the guest
371 * handle the exception
372 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200373 if (!reinject &&
374 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100375 return;
376
Jan Kiszka66b71382010-02-23 17:47:56 +0100377 if (nr == BP_VECTOR && !svm_has(SVM_FEATURE_NRIP)) {
378 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
379
380 /*
381 * For guest debugging where we have to reinject #BP if some
382 * INT3 is guest-owned:
383 * Emulate nRIP by moving RIP forward. Will fail if injection
384 * raises a fault that is not intercepted. Still better than
385 * failing in all cases.
386 */
387 skip_emulated_instruction(&svm->vcpu);
388 rip = kvm_rip_read(&svm->vcpu);
389 svm->int3_rip = rip + svm->vmcb->save.cs.base;
390 svm->int3_injected = rip - old_rip;
391 }
392
Jan Kiszka116a4752010-02-23 17:47:54 +0100393 svm->vmcb->control.event_inj = nr
394 | SVM_EVTINJ_VALID
395 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
396 | SVM_EVTINJ_TYPE_EXEPT;
397 svm->vmcb->control.event_inj_err = error_code;
398}
399
Joerg Roedel67ec6602010-05-17 14:43:35 +0200400static void svm_init_erratum_383(void)
401{
402 u32 low, high;
403 int err;
404 u64 val;
405
Hans Rosenfeld1be85a62010-07-28 19:09:32 +0200406 if (!cpu_has_amd_erratum(amd_erratum_383))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200407 return;
408
409 /* Use _safe variants to not break nested virtualization */
410 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
411 if (err)
412 return;
413
414 val |= (1ULL << 47);
415
416 low = lower_32_bits(val);
417 high = upper_32_bits(val);
418
419 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
420
421 erratum_383_found = true;
422}
423
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424static int has_svm(void)
425{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200426 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800427
Eduardo Habkost63d11422008-11-17 19:03:20 -0200428 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800429 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800430 return 0;
431 }
432
Avi Kivity6aa8b732006-12-10 02:21:36 -0800433 return 1;
434}
435
436static void svm_hardware_disable(void *garbage)
437{
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200438 cpu_svm_disable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800439}
440
Alexander Graf10474ae2009-09-15 11:37:46 +0200441static int svm_hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800442{
443
Tejun Heo0fe1e002009-10-29 22:34:14 +0900444 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800445 uint64_t efer;
Gleb Natapov89a27f42010-02-16 10:51:48 +0200446 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800447 struct desc_struct *gdt;
448 int me = raw_smp_processor_id();
449
Alexander Graf10474ae2009-09-15 11:37:46 +0200450 rdmsrl(MSR_EFER, efer);
451 if (efer & EFER_SVME)
452 return -EBUSY;
453
Avi Kivity6aa8b732006-12-10 02:21:36 -0800454 if (!has_svm()) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000455 printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
456 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200457 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800458 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900459 sd = per_cpu(svm_data, me);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800460
Tejun Heo0fe1e002009-10-29 22:34:14 +0900461 if (!sd) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000462 printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -0800463 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200464 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800465 }
466
Tejun Heo0fe1e002009-10-29 22:34:14 +0900467 sd->asid_generation = 1;
468 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
469 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800470
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200471 native_store_gdt(&gdt_descr);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200472 gdt = (struct desc_struct *)gdt_descr.address;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900473 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800474
Alexander Graf9962d032008-11-25 20:17:02 +0100475 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800476
Linus Torvaldsd0316552009-12-14 09:58:24 -0800477 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200478
Joerg Roedel67ec6602010-05-17 14:43:35 +0200479 svm_init_erratum_383();
480
Alexander Graf10474ae2009-09-15 11:37:46 +0200481 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800482}
483
Joerg Roedel0da1db752008-07-02 16:02:11 +0200484static void svm_cpu_uninit(int cpu)
485{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900486 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200487
Tejun Heo0fe1e002009-10-29 22:34:14 +0900488 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200489 return;
490
491 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900492 __free_page(sd->save_area);
493 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200494}
495
Avi Kivity6aa8b732006-12-10 02:21:36 -0800496static int svm_cpu_init(int cpu)
497{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900498 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800499 int r;
500
Tejun Heo0fe1e002009-10-29 22:34:14 +0900501 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
502 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800503 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900504 sd->cpu = cpu;
505 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800506 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900507 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800508 goto err_1;
509
Tejun Heo0fe1e002009-10-29 22:34:14 +0900510 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800511
512 return 0;
513
514err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900515 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800516 return r;
517
518}
519
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100520static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800521{
522 int i;
523
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100524 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
525 if (direct_access_msrs[i].index == index)
526 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800527
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100528 return false;
529}
530
Avi Kivity6aa8b732006-12-10 02:21:36 -0800531static void set_msr_interception(u32 *msrpm, unsigned msr,
532 int read, int write)
533{
Joerg Roedel455716f2010-03-01 15:34:35 +0100534 u8 bit_read, bit_write;
535 unsigned long tmp;
536 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800537
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100538 /*
539 * If this warning triggers extend the direct_access_msrs list at the
540 * beginning of the file
541 */
542 WARN_ON(!valid_msr_intercept(msr));
543
Joerg Roedel455716f2010-03-01 15:34:35 +0100544 offset = svm_msrpm_offset(msr);
545 bit_read = 2 * (msr & 0x0f);
546 bit_write = 2 * (msr & 0x0f) + 1;
547 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800548
Joerg Roedel455716f2010-03-01 15:34:35 +0100549 BUG_ON(offset == MSR_INVALID);
550
551 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
552 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
553
554 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800555}
556
Joerg Roedelf65c2292008-02-13 18:58:46 +0100557static void svm_vcpu_init_msrpm(u32 *msrpm)
558{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100559 int i;
560
Joerg Roedelf65c2292008-02-13 18:58:46 +0100561 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
562
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100563 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
564 if (!direct_access_msrs[i].always)
565 continue;
566
567 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
568 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100569}
570
Joerg Roedel323c3d82010-03-01 15:34:37 +0100571static void add_msr_offset(u32 offset)
572{
573 int i;
574
575 for (i = 0; i < MSRPM_OFFSETS; ++i) {
576
577 /* Offset already in list? */
578 if (msrpm_offsets[i] == offset)
579 return;
580
581 /* Slot used by another offset? */
582 if (msrpm_offsets[i] != MSR_INVALID)
583 continue;
584
585 /* Add offset to list */
586 msrpm_offsets[i] = offset;
587
588 return;
589 }
590
591 /*
592 * If this BUG triggers the msrpm_offsets table has an overflow. Just
593 * increase MSRPM_OFFSETS in this case.
594 */
595 BUG();
596}
597
598static void init_msrpm_offsets(void)
599{
600 int i;
601
602 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
603
604 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
605 u32 offset;
606
607 offset = svm_msrpm_offset(direct_access_msrs[i].index);
608 BUG_ON(offset == MSR_INVALID);
609
610 add_msr_offset(offset);
611 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800612}
613
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100614static void svm_enable_lbrv(struct vcpu_svm *svm)
615{
616 u32 *msrpm = svm->msrpm;
617
618 svm->vmcb->control.lbr_ctl = 1;
619 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
620 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
621 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
622 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
623}
624
625static void svm_disable_lbrv(struct vcpu_svm *svm)
626{
627 u32 *msrpm = svm->msrpm;
628
629 svm->vmcb->control.lbr_ctl = 0;
630 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
631 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
632 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
633 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
634}
635
Avi Kivity6aa8b732006-12-10 02:21:36 -0800636static __init int svm_hardware_setup(void)
637{
638 int cpu;
639 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100640 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800641 int r;
642
Avi Kivity6aa8b732006-12-10 02:21:36 -0800643 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
644
645 if (!iopm_pages)
646 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300647
648 iopm_va = page_address(iopm_pages);
649 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800650 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
651
Joerg Roedel323c3d82010-03-01 15:34:37 +0100652 init_msrpm_offsets();
653
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100654 if (boot_cpu_has(X86_FEATURE_NX))
655 kvm_enable_efer_bits(EFER_NX);
656
Alexander Graf1b2fd702009-02-02 16:23:51 +0100657 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
658 kvm_enable_efer_bits(EFER_FFXSR);
659
Alexander Graf236de052008-11-25 20:17:10 +0100660 if (nested) {
661 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +0200662 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +0100663 }
664
Zachary Amsden3230bb42009-09-29 11:38:37 -1000665 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800666 r = svm_cpu_init(cpu);
667 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100668 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800669 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100670
671 svm_features = cpuid_edx(SVM_CPUID_FUNC);
672
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100673 if (!svm_has(SVM_FEATURE_NPT))
674 npt_enabled = false;
675
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100676 if (npt_enabled && !npt) {
677 printk(KERN_INFO "kvm: Nested Paging disabled\n");
678 npt_enabled = false;
679 }
680
Joerg Roedel18552672008-02-07 13:47:41 +0100681 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100682 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100683 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200684 } else
685 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100686
Avi Kivity6aa8b732006-12-10 02:21:36 -0800687 return 0;
688
Joerg Roedelf65c2292008-02-13 18:58:46 +0100689err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800690 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
691 iopm_base = 0;
692 return r;
693}
694
695static __exit void svm_hardware_unsetup(void)
696{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200697 int cpu;
698
Zachary Amsden3230bb42009-09-29 11:38:37 -1000699 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200700 svm_cpu_uninit(cpu);
701
Avi Kivity6aa8b732006-12-10 02:21:36 -0800702 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100703 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800704}
705
706static void init_seg(struct vmcb_seg *seg)
707{
708 seg->selector = 0;
709 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +0100710 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800711 seg->limit = 0xffff;
712 seg->base = 0;
713}
714
715static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
716{
717 seg->selector = 0;
718 seg->attrib = SVM_SELECTOR_P_MASK | type;
719 seg->limit = 0xffff;
720 seg->base = 0;
721}
722
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -1000723static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
724{
725 struct vcpu_svm *svm = to_svm(vcpu);
726 u64 g_tsc_offset = 0;
727
728 if (is_nested(svm)) {
729 g_tsc_offset = svm->vmcb->control.tsc_offset -
730 svm->nested.hsave->control.tsc_offset;
731 svm->nested.hsave->control.tsc_offset = offset;
732 }
733
734 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
735}
736
Zachary Amsdene48672f2010-08-19 22:07:23 -1000737static void svm_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment)
738{
739 struct vcpu_svm *svm = to_svm(vcpu);
740
741 svm->vmcb->control.tsc_offset += adjustment;
742 if (is_nested(svm))
743 svm->nested.hsave->control.tsc_offset += adjustment;
744}
745
Joerg Roedele6101a92008-02-13 18:58:45 +0100746static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800747{
Joerg Roedele6101a92008-02-13 18:58:45 +0100748 struct vmcb_control_area *control = &svm->vmcb->control;
749 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800750
Avi Kivitybff78272010-01-07 13:16:08 +0200751 svm->vcpu.fpu_active = 1;
752
Joerg Roedele0231712010-02-24 18:59:10 +0100753 control->intercept_cr_read = INTERCEPT_CR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800754 INTERCEPT_CR3_MASK |
Joerg Roedel649d6862008-04-16 16:51:15 +0200755 INTERCEPT_CR4_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800756
Joerg Roedele0231712010-02-24 18:59:10 +0100757 control->intercept_cr_write = INTERCEPT_CR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800758 INTERCEPT_CR3_MASK |
Avi Kivity80a81192007-12-06 19:50:00 +0200759 INTERCEPT_CR4_MASK |
760 INTERCEPT_CR8_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800761
Joerg Roedele0231712010-02-24 18:59:10 +0100762 control->intercept_dr_read = INTERCEPT_DR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800763 INTERCEPT_DR1_MASK |
764 INTERCEPT_DR2_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100765 INTERCEPT_DR3_MASK |
766 INTERCEPT_DR4_MASK |
767 INTERCEPT_DR5_MASK |
768 INTERCEPT_DR6_MASK |
769 INTERCEPT_DR7_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800770
Joerg Roedele0231712010-02-24 18:59:10 +0100771 control->intercept_dr_write = INTERCEPT_DR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800772 INTERCEPT_DR1_MASK |
773 INTERCEPT_DR2_MASK |
774 INTERCEPT_DR3_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100775 INTERCEPT_DR4_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800776 INTERCEPT_DR5_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100777 INTERCEPT_DR6_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800778 INTERCEPT_DR7_MASK;
779
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500780 control->intercept_exceptions = (1 << PF_VECTOR) |
Joerg Roedel53371b52008-04-09 14:15:30 +0200781 (1 << UD_VECTOR) |
782 (1 << MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800783
784
Joerg Roedele0231712010-02-24 18:59:10 +0100785 control->intercept = (1ULL << INTERCEPT_INTR) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800786 (1ULL << INTERCEPT_NMI) |
Joerg Roedel01525272007-02-19 14:37:47 +0200787 (1ULL << INTERCEPT_SMI) |
Avi Kivityd2251572010-01-06 10:55:27 +0200788 (1ULL << INTERCEPT_SELECTIVE_CR0) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789 (1ULL << INTERCEPT_CPUID) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200790 (1ULL << INTERCEPT_INVD) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800791 (1ULL << INTERCEPT_HLT) |
Marcelo Tosattia7052892008-09-23 13:18:35 -0300792 (1ULL << INTERCEPT_INVLPG) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800793 (1ULL << INTERCEPT_INVLPGA) |
794 (1ULL << INTERCEPT_IOIO_PROT) |
795 (1ULL << INTERCEPT_MSR_PROT) |
796 (1ULL << INTERCEPT_TASK_SWITCH) |
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800797 (1ULL << INTERCEPT_SHUTDOWN) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800798 (1ULL << INTERCEPT_VMRUN) |
799 (1ULL << INTERCEPT_VMMCALL) |
800 (1ULL << INTERCEPT_VMLOAD) |
801 (1ULL << INTERCEPT_VMSAVE) |
802 (1ULL << INTERCEPT_STGI) |
803 (1ULL << INTERCEPT_CLGI) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100804 (1ULL << INTERCEPT_SKINIT) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200805 (1ULL << INTERCEPT_WBINVD) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100806 (1ULL << INTERCEPT_MONITOR) |
807 (1ULL << INTERCEPT_MWAIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800808
809 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100810 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800811 control->int_ctl = V_INTR_MASKING_MASK;
812
813 init_seg(&save->es);
814 init_seg(&save->ss);
815 init_seg(&save->ds);
816 init_seg(&save->fs);
817 init_seg(&save->gs);
818
819 save->cs.selector = 0xf000;
820 /* Executable/Readable Code Segment */
821 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
822 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
823 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800824 /*
825 * cs.base should really be 0xffff0000, but vmx can't handle that, so
826 * be consistent with it.
827 *
828 * Replace when we have real mode working for vmx.
829 */
830 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800831
832 save->gdtr.limit = 0xffff;
833 save->idtr.limit = 0xffff;
834
835 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
836 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
837
Marcelo Tosattieaa48512010-08-31 19:13:14 -0300838 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -0400839 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800840 save->dr7 = 0x400;
841 save->rflags = 2;
842 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300843 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800844
Joerg Roedele0231712010-02-24 18:59:10 +0100845 /*
846 * This is the guest-visible cr0 value.
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200847 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800848 */
Marcelo Tosatti678041a2010-08-31 19:13:13 -0300849 svm->vcpu.arch.cr0 = 0;
850 (void)kvm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200851
Rusty Russell66aee912007-07-17 23:34:16 +1000852 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800853 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100854
855 if (npt_enabled) {
856 /* Setup VMCB for Nested Paging */
857 control->nested_ctl = 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -0300858 control->intercept &= ~((1ULL << INTERCEPT_TASK_SWITCH) |
859 (1ULL << INTERCEPT_INVLPG));
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100860 control->intercept_exceptions &= ~(1 << PF_VECTOR);
Avi Kivity888f9f32010-01-10 12:14:04 +0200861 control->intercept_cr_read &= ~INTERCEPT_CR3_MASK;
862 control->intercept_cr_write &= ~INTERCEPT_CR3_MASK;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100863 save->g_pat = 0x0007040600070406ULL;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100864 save->cr3 = 0;
865 save->cr4 = 0;
866 }
Avi Kivitya79d2f12008-04-14 13:10:21 +0300867 force_new_asid(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +0100868
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200869 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +0200870 svm->vcpu.arch.hflags = 0;
871
Mark Langsdorf565d0992009-10-06 14:25:02 -0500872 if (svm_has(SVM_FEATURE_PAUSE_FILTER)) {
873 control->pause_filter_count = 3000;
874 control->intercept |= (1ULL << INTERCEPT_PAUSE);
875 }
876
Joerg Roedel2af91942009-08-07 11:49:28 +0200877 enable_gif(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800878}
879
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200880static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +0300881{
882 struct vcpu_svm *svm = to_svm(vcpu);
883
Joerg Roedele6101a92008-02-13 18:58:45 +0100884 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +0200885
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300886 if (!kvm_vcpu_is_bsp(vcpu)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300887 kvm_rip_write(vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800888 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
889 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +0200890 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300891 vcpu->arch.regs_avail = ~0;
892 vcpu->arch.regs_dirty = ~0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200893
894 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300895}
896
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000897static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800898{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400899 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800900 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100901 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +0100902 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100903 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000904 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905
Rusty Russellc16f8622007-07-30 21:12:19 +1000906 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000907 if (!svm) {
908 err = -ENOMEM;
909 goto out;
910 }
911
912 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
913 if (err)
914 goto free_svm;
915
Joerg Roedelf65c2292008-02-13 18:58:46 +0100916 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900917 page = alloc_page(GFP_KERNEL);
918 if (!page)
919 goto uninit;
920
Joerg Roedelf65c2292008-02-13 18:58:46 +0100921 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
922 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900923 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100924
925 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
926 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900927 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100928
Alexander Grafb286d5d2008-11-25 20:17:05 +0100929 hsave_page = alloc_page(GFP_KERNEL);
930 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900931 goto free_page3;
932
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200933 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +0100934
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900935 svm->msrpm = page_address(msrpm_pages);
936 svm_vcpu_init_msrpm(svm->msrpm);
937
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200938 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +0100939 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +0100940
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400941 svm->vmcb = page_address(page);
942 clear_page(svm->vmcb);
943 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
944 svm->asid_generation = 0;
Joerg Roedele6101a92008-02-13 18:58:45 +0100945 init_vmcb(svm);
Zachary Amsden99e3e302010-08-19 22:07:17 -1000946 kvm_write_tsc(&svm->vcpu, 0);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400947
Jan Kiszka10ab25c2010-05-25 16:01:50 +0200948 err = fx_init(&svm->vcpu);
949 if (err)
950 goto free_page4;
951
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800952 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300953 if (kvm_vcpu_is_bsp(&svm->vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800954 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000956 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -0800957
Jan Kiszka10ab25c2010-05-25 16:01:50 +0200958free_page4:
959 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900960free_page3:
961 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
962free_page2:
963 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
964free_page1:
965 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000966uninit:
967 kvm_vcpu_uninit(&svm->vcpu);
968free_svm:
Rusty Russella4770342007-08-01 14:46:11 +1000969 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000970out:
971 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972}
973
974static void svm_free_vcpu(struct kvm_vcpu *vcpu)
975{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400976 struct vcpu_svm *svm = to_svm(vcpu);
977
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000978 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +0100979 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200980 __free_page(virt_to_page(svm->nested.hsave));
981 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000982 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +1000983 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984}
985
Avi Kivity15ad7142007-07-11 18:17:21 +0300986static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400988 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300989 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +0200990
Avi Kivity0cc50642007-03-25 12:07:27 +0200991 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -0300992 svm->asid_generation = 0;
Avi Kivity0cc50642007-03-25 12:07:27 +0200993 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300994
995 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400996 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997}
998
999static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1000{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001001 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001002 int i;
1003
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001004 ++vcpu->stat.host_state_reload;
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001005 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001006 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001007}
1008
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1010{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001011 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012}
1013
1014static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1015{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001016 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001017}
1018
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001019static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1020{
1021 switch (reg) {
1022 case VCPU_EXREG_PDPTR:
1023 BUG_ON(!npt_enabled);
Joerg Roedelff03a072010-09-10 17:30:57 +02001024 load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3);
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001025 break;
1026 default:
1027 BUG();
1028 }
1029}
1030
Alexander Graff0b85052008-11-25 20:17:01 +01001031static void svm_set_vintr(struct vcpu_svm *svm)
1032{
1033 svm->vmcb->control.intercept |= 1ULL << INTERCEPT_VINTR;
1034}
1035
1036static void svm_clear_vintr(struct vcpu_svm *svm)
1037{
1038 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
1039}
1040
Avi Kivity6aa8b732006-12-10 02:21:36 -08001041static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1042{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001043 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044
1045 switch (seg) {
1046 case VCPU_SREG_CS: return &save->cs;
1047 case VCPU_SREG_DS: return &save->ds;
1048 case VCPU_SREG_ES: return &save->es;
1049 case VCPU_SREG_FS: return &save->fs;
1050 case VCPU_SREG_GS: return &save->gs;
1051 case VCPU_SREG_SS: return &save->ss;
1052 case VCPU_SREG_TR: return &save->tr;
1053 case VCPU_SREG_LDTR: return &save->ldtr;
1054 }
1055 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001056 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001057}
1058
1059static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1060{
1061 struct vmcb_seg *s = svm_seg(vcpu, seg);
1062
1063 return s->base;
1064}
1065
1066static void svm_get_segment(struct kvm_vcpu *vcpu,
1067 struct kvm_segment *var, int seg)
1068{
1069 struct vmcb_seg *s = svm_seg(vcpu, seg);
1070
1071 var->base = s->base;
1072 var->limit = s->limit;
1073 var->selector = s->selector;
1074 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1075 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1076 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1077 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1078 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1079 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1080 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
1081 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
Amit Shah25022ac2008-10-27 09:04:17 +00001082
Joerg Roedele0231712010-02-24 18:59:10 +01001083 /*
1084 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001085 * for cross vendor migration purposes by "not present"
1086 */
1087 var->unusable = !var->present || (var->type == 0);
1088
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001089 switch (seg) {
1090 case VCPU_SREG_CS:
1091 /*
1092 * SVM always stores 0 for the 'G' bit in the CS selector in
1093 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
1094 * Intel's VMENTRY has a check on the 'G' bit.
1095 */
Amit Shah25022ac2008-10-27 09:04:17 +00001096 var->g = s->limit > 0xfffff;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001097 break;
1098 case VCPU_SREG_TR:
1099 /*
1100 * Work around a bug where the busy flag in the tr selector
1101 * isn't exposed
1102 */
Amit Shahc0d09822008-10-27 09:04:18 +00001103 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001104 break;
1105 case VCPU_SREG_DS:
1106 case VCPU_SREG_ES:
1107 case VCPU_SREG_FS:
1108 case VCPU_SREG_GS:
1109 /*
1110 * The accessed bit must always be set in the segment
1111 * descriptor cache, although it can be cleared in the
1112 * descriptor, the cached bit always remains at 1. Since
1113 * Intel has a check on this, set it here to support
1114 * cross-vendor migration.
1115 */
1116 if (!var->unusable)
1117 var->type |= 0x1;
1118 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001119 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001120 /*
1121 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001122 * descriptor is left as 1, although the whole segment has
1123 * been made unusable. Clear it here to pass an Intel VMX
1124 * entry check when cross vendor migrating.
1125 */
1126 if (var->unusable)
1127 var->db = 0;
1128 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001129 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001130}
1131
Izik Eidus2e4d2652008-03-24 19:38:34 +02001132static int svm_get_cpl(struct kvm_vcpu *vcpu)
1133{
1134 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1135
1136 return save->cpl;
1137}
1138
Gleb Natapov89a27f42010-02-16 10:51:48 +02001139static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001140{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001141 struct vcpu_svm *svm = to_svm(vcpu);
1142
Gleb Natapov89a27f42010-02-16 10:51:48 +02001143 dt->size = svm->vmcb->save.idtr.limit;
1144 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001145}
1146
Gleb Natapov89a27f42010-02-16 10:51:48 +02001147static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001148{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001149 struct vcpu_svm *svm = to_svm(vcpu);
1150
Gleb Natapov89a27f42010-02-16 10:51:48 +02001151 svm->vmcb->save.idtr.limit = dt->size;
1152 svm->vmcb->save.idtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001153}
1154
Gleb Natapov89a27f42010-02-16 10:51:48 +02001155static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001156{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001157 struct vcpu_svm *svm = to_svm(vcpu);
1158
Gleb Natapov89a27f42010-02-16 10:51:48 +02001159 dt->size = svm->vmcb->save.gdtr.limit;
1160 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001161}
1162
Gleb Natapov89a27f42010-02-16 10:51:48 +02001163static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001164{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001165 struct vcpu_svm *svm = to_svm(vcpu);
1166
Gleb Natapov89a27f42010-02-16 10:51:48 +02001167 svm->vmcb->save.gdtr.limit = dt->size;
1168 svm->vmcb->save.gdtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001169}
1170
Avi Kivitye8467fd2009-12-29 18:43:06 +02001171static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1172{
1173}
1174
Anthony Liguori25c4c272007-04-27 09:29:21 +03001175static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001176{
1177}
1178
Avi Kivityd2251572010-01-06 10:55:27 +02001179static void update_cr0_intercept(struct vcpu_svm *svm)
1180{
Joerg Roedel66a562f2010-02-19 16:23:08 +01001181 struct vmcb *vmcb = svm->vmcb;
Avi Kivityd2251572010-01-06 10:55:27 +02001182 ulong gcr0 = svm->vcpu.arch.cr0;
1183 u64 *hcr0 = &svm->vmcb->save.cr0;
1184
1185 if (!svm->vcpu.fpu_active)
1186 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1187 else
1188 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1189 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1190
1191
1192 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Joerg Roedel66a562f2010-02-19 16:23:08 +01001193 vmcb->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1194 vmcb->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1195 if (is_nested(svm)) {
1196 struct vmcb *hsave = svm->nested.hsave;
1197
1198 hsave->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1199 hsave->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1200 vmcb->control.intercept_cr_read |= svm->nested.intercept_cr_read;
1201 vmcb->control.intercept_cr_write |= svm->nested.intercept_cr_write;
1202 }
Avi Kivityd2251572010-01-06 10:55:27 +02001203 } else {
1204 svm->vmcb->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1205 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001206 if (is_nested(svm)) {
1207 struct vmcb *hsave = svm->nested.hsave;
1208
1209 hsave->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1210 hsave->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
1211 }
Avi Kivityd2251572010-01-06 10:55:27 +02001212 }
1213}
1214
Avi Kivity6aa8b732006-12-10 02:21:36 -08001215static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1216{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001217 struct vcpu_svm *svm = to_svm(vcpu);
1218
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001219 if (is_nested(svm)) {
1220 /*
1221 * We are here because we run in nested mode, the host kvm
1222 * intercepts cr0 writes but the l1 hypervisor does not.
1223 * But the L1 hypervisor may intercept selective cr0 writes.
1224 * This needs to be checked here.
1225 */
1226 unsigned long old, new;
1227
1228 /* Remove bits that would trigger a real cr0 write intercept */
1229 old = vcpu->arch.cr0 & SVM_CR0_SELECTIVE_MASK;
1230 new = cr0 & SVM_CR0_SELECTIVE_MASK;
1231
1232 if (old == new) {
1233 /* cr0 write with ts and mp unchanged */
1234 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
Joerg Roedelcda00082010-09-02 17:29:46 +02001235 if (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE) {
1236 svm->nested.vmexit_rip = kvm_rip_read(vcpu);
1237 svm->nested.vmexit_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
1238 svm->nested.vmexit_rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001239 return;
Joerg Roedelcda00082010-09-02 17:29:46 +02001240 }
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001241 }
1242 }
1243
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001244#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001245 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001246 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001247 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001248 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001249 }
1250
Mike Dayd77c26f2007-10-08 09:02:08 -04001251 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001252 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001253 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001254 }
1255 }
1256#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001257 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001258
1259 if (!npt_enabled)
1260 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001261
1262 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001263 cr0 |= X86_CR0_TS;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001264 /*
1265 * re-enable caching here because the QEMU bios
1266 * does not do it - this results in some delay at
1267 * reboot
1268 */
1269 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001270 svm->vmcb->save.cr0 = cr0;
Avi Kivityd2251572010-01-06 10:55:27 +02001271 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001272}
1273
1274static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1275{
Joerg Roedel6394b642008-04-09 14:15:29 +02001276 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001277 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1278
1279 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
1280 force_new_asid(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001281
Joerg Roedelec077262008-04-09 14:15:28 +02001282 vcpu->arch.cr4 = cr4;
1283 if (!npt_enabled)
1284 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001285 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001286 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001287}
1288
1289static void svm_set_segment(struct kvm_vcpu *vcpu,
1290 struct kvm_segment *var, int seg)
1291{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001292 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001293 struct vmcb_seg *s = svm_seg(vcpu, seg);
1294
1295 s->base = var->base;
1296 s->limit = var->limit;
1297 s->selector = var->selector;
1298 if (var->unusable)
1299 s->attrib = 0;
1300 else {
1301 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1302 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1303 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1304 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1305 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1306 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1307 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1308 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1309 }
1310 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001311 svm->vmcb->save.cpl
1312 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -08001313 >> SVM_SELECTOR_DPL_SHIFT) & 3;
1314
1315}
1316
Gleb Natapov44c11432009-05-11 13:35:52 +03001317static void update_db_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001318{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001319 struct vcpu_svm *svm = to_svm(vcpu);
1320
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001321 svm->vmcb->control.intercept_exceptions &=
1322 ~((1 << DB_VECTOR) | (1 << BP_VECTOR));
Gleb Natapov44c11432009-05-11 13:35:52 +03001323
Jan Kiszka6be7d302009-10-18 13:24:54 +02001324 if (svm->nmi_singlestep)
Gleb Natapov44c11432009-05-11 13:35:52 +03001325 svm->vmcb->control.intercept_exceptions |= (1 << DB_VECTOR);
1326
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001327 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1328 if (vcpu->guest_debug &
1329 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
1330 svm->vmcb->control.intercept_exceptions |=
1331 1 << DB_VECTOR;
1332 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
1333 svm->vmcb->control.intercept_exceptions |=
1334 1 << BP_VECTOR;
1335 } else
1336 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001337}
1338
Jan Kiszka355be0b2009-10-03 00:31:21 +02001339static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Gleb Natapov44c11432009-05-11 13:35:52 +03001340{
Gleb Natapov44c11432009-05-11 13:35:52 +03001341 struct vcpu_svm *svm = to_svm(vcpu);
1342
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001343 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1344 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1345 else
1346 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1347
Jan Kiszka355be0b2009-10-03 00:31:21 +02001348 update_db_intercept(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001349}
1350
1351static void load_host_msrs(struct kvm_vcpu *vcpu)
1352{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001353#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001354 wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001355#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001356}
1357
1358static void save_host_msrs(struct kvm_vcpu *vcpu)
1359{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001360#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001361 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001362#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001363}
1364
Tejun Heo0fe1e002009-10-29 22:34:14 +09001365static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001367 if (sd->next_asid > sd->max_asid) {
1368 ++sd->asid_generation;
1369 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001370 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001371 }
1372
Tejun Heo0fe1e002009-10-29 22:34:14 +09001373 svm->asid_generation = sd->asid_generation;
1374 svm->vmcb->control.asid = sd->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001375}
1376
Gleb Natapov020df072010-04-13 10:05:23 +03001377static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001378{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001379 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001380
Gleb Natapov020df072010-04-13 10:05:23 +03001381 svm->vmcb->save.dr7 = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001382}
1383
Avi Kivity851ba692009-08-24 11:10:17 +03001384static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001386 u64 fault_address;
1387 u32 error_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001388
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001389 fault_address = svm->vmcb->control.exit_info_2;
1390 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001391
Marcelo Tosatti229456f2009-06-17 09:22:14 -03001392 trace_kvm_page_fault(fault_address, error_code);
Avi Kivity52c78472009-08-10 15:42:41 +03001393 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1394 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
Avi Kivity30677142007-10-28 18:48:59 +02001395 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001396}
1397
Avi Kivity851ba692009-08-24 11:10:17 +03001398static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001399{
Avi Kivity851ba692009-08-24 11:10:17 +03001400 struct kvm_run *kvm_run = svm->vcpu.run;
1401
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001402 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001403 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001404 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001405 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1406 return 1;
1407 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001408
Jan Kiszka6be7d302009-10-18 13:24:54 +02001409 if (svm->nmi_singlestep) {
1410 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001411 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1412 svm->vmcb->save.rflags &=
1413 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1414 update_db_intercept(&svm->vcpu);
1415 }
1416
1417 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001418 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001419 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1420 kvm_run->debug.arch.pc =
1421 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1422 kvm_run->debug.arch.exception = DB_VECTOR;
1423 return 0;
1424 }
1425
1426 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001427}
1428
Avi Kivity851ba692009-08-24 11:10:17 +03001429static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001430{
Avi Kivity851ba692009-08-24 11:10:17 +03001431 struct kvm_run *kvm_run = svm->vcpu.run;
1432
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001433 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1434 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1435 kvm_run->debug.arch.exception = BP_VECTOR;
1436 return 0;
1437}
1438
Avi Kivity851ba692009-08-24 11:10:17 +03001439static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001440{
1441 int er;
1442
Avi Kivity851ba692009-08-24 11:10:17 +03001443 er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001444 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001445 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001446 return 1;
1447}
1448
Avi Kivity6b52d182010-01-21 15:31:47 +02001449static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001450{
Avi Kivity6b52d182010-01-21 15:31:47 +02001451 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001452 u32 excp;
1453
1454 if (is_nested(svm)) {
1455 u32 h_excp, n_excp;
1456
1457 h_excp = svm->nested.hsave->control.intercept_exceptions;
1458 n_excp = svm->nested.intercept_exceptions;
1459 h_excp &= ~(1 << NM_VECTOR);
1460 excp = h_excp | n_excp;
1461 } else {
1462 excp = svm->vmcb->control.intercept_exceptions;
Joerg Roedele0231712010-02-24 18:59:10 +01001463 excp &= ~(1 << NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001464 }
1465
1466 svm->vmcb->control.intercept_exceptions = excp;
1467
Rusty Russelle756fc62007-07-30 20:07:08 +10001468 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001469 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001470}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001471
Avi Kivity6b52d182010-01-21 15:31:47 +02001472static int nm_interception(struct vcpu_svm *svm)
1473{
1474 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001475 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001476}
1477
Joerg Roedel67ec6602010-05-17 14:43:35 +02001478static bool is_erratum_383(void)
1479{
1480 int err, i;
1481 u64 value;
1482
1483 if (!erratum_383_found)
1484 return false;
1485
1486 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1487 if (err)
1488 return false;
1489
1490 /* Bit 62 may or may not be set for this mce */
1491 value &= ~(1ULL << 62);
1492
1493 if (value != 0xb600000000010015ULL)
1494 return false;
1495
1496 /* Clear MCi_STATUS registers */
1497 for (i = 0; i < 6; ++i)
1498 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1499
1500 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1501 if (!err) {
1502 u32 low, high;
1503
1504 value &= ~(1ULL << 2);
1505 low = lower_32_bits(value);
1506 high = upper_32_bits(value);
1507
1508 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1509 }
1510
1511 /* Flush tlb to evict multi-match entries */
1512 __flush_tlb_all();
1513
1514 return true;
1515}
1516
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001517static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001518{
Joerg Roedel67ec6602010-05-17 14:43:35 +02001519 if (is_erratum_383()) {
1520 /*
1521 * Erratum 383 triggered. Guest state is corrupt so kill the
1522 * guest.
1523 */
1524 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1525
Avi Kivitya8eeb042010-05-10 12:34:53 +03001526 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02001527
1528 return;
1529 }
1530
Joerg Roedel53371b52008-04-09 14:15:30 +02001531 /*
1532 * On an #MC intercept the MCE handler is not called automatically in
1533 * the host. So do it by hand here.
1534 */
1535 asm volatile (
1536 "int $0x12\n");
1537 /* not sure if we ever come back to this point */
1538
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001539 return;
1540}
1541
1542static int mc_interception(struct vcpu_svm *svm)
1543{
Joerg Roedel53371b52008-04-09 14:15:30 +02001544 return 1;
1545}
1546
Avi Kivity851ba692009-08-24 11:10:17 +03001547static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001548{
Avi Kivity851ba692009-08-24 11:10:17 +03001549 struct kvm_run *kvm_run = svm->vcpu.run;
1550
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001551 /*
1552 * VMCB is undefined after a SHUTDOWN intercept
1553 * so reinitialize it.
1554 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001555 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001556 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001557
1558 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1559 return 0;
1560}
1561
Avi Kivity851ba692009-08-24 11:10:17 +03001562static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001563{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001564 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04001565 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001566 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001567 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001568
Rusty Russelle756fc62007-07-30 20:07:08 +10001569 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03001570 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001571 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001572 if (string || in)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03001573 return emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001574
Avi Kivity039576c2007-03-20 12:46:50 +02001575 port = io_info >> 16;
1576 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001577 svm->next_rip = svm->vmcb->control.exit_info_2;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001578 skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001579
1580 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001581}
1582
Avi Kivity851ba692009-08-24 11:10:17 +03001583static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001584{
1585 return 1;
1586}
1587
Avi Kivity851ba692009-08-24 11:10:17 +03001588static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001589{
1590 ++svm->vcpu.stat.irq_exits;
1591 return 1;
1592}
1593
Avi Kivity851ba692009-08-24 11:10:17 +03001594static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001595{
1596 return 1;
1597}
1598
Avi Kivity851ba692009-08-24 11:10:17 +03001599static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001600{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001601 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001602 skip_emulated_instruction(&svm->vcpu);
1603 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001604}
1605
Avi Kivity851ba692009-08-24 11:10:17 +03001606static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001607{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001608 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001609 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001610 kvm_emulate_hypercall(&svm->vcpu);
1611 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001612}
1613
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001614static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
1615{
1616 struct vcpu_svm *svm = to_svm(vcpu);
1617
1618 return svm->nested.nested_cr3;
1619}
1620
1621static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
1622 unsigned long root)
1623{
1624 struct vcpu_svm *svm = to_svm(vcpu);
1625
1626 svm->vmcb->control.nested_cr3 = root;
1627 force_new_asid(vcpu);
1628}
1629
1630static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu)
1631{
1632 struct vcpu_svm *svm = to_svm(vcpu);
1633
1634 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
1635 svm->vmcb->control.exit_code_hi = 0;
1636 svm->vmcb->control.exit_info_1 = vcpu->arch.fault.error_code;
1637 svm->vmcb->control.exit_info_2 = vcpu->arch.fault.address;
1638
1639 nested_svm_vmexit(svm);
1640}
1641
Joerg Roedel4b161842010-09-10 17:31:03 +02001642static int nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
1643{
1644 int r;
1645
1646 r = kvm_init_shadow_mmu(vcpu, &vcpu->arch.mmu);
1647
1648 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
1649 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
1650 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
1651 vcpu->arch.mmu.shadow_root_level = get_npt_level();
1652 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
1653
1654 return r;
1655}
1656
1657static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
1658{
1659 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
1660}
1661
Alexander Grafc0725422008-11-25 20:17:03 +01001662static int nested_svm_check_permissions(struct vcpu_svm *svm)
1663{
Avi Kivityf6801df2010-01-21 15:31:50 +02001664 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01001665 || !is_paging(&svm->vcpu)) {
1666 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1667 return 1;
1668 }
1669
1670 if (svm->vmcb->save.cpl) {
1671 kvm_inject_gp(&svm->vcpu, 0);
1672 return 1;
1673 }
1674
1675 return 0;
1676}
1677
Alexander Grafcf74a782008-11-25 20:17:08 +01001678static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
1679 bool has_error_code, u32 error_code)
1680{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001681 int vmexit;
1682
Joerg Roedel0295ad72009-08-07 11:49:37 +02001683 if (!is_nested(svm))
1684 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001685
Joerg Roedel0295ad72009-08-07 11:49:37 +02001686 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1687 svm->vmcb->control.exit_code_hi = 0;
1688 svm->vmcb->control.exit_info_1 = error_code;
1689 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1690
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001691 vmexit = nested_svm_intercept(svm);
1692 if (vmexit == NESTED_EXIT_DONE)
1693 svm->nested.exit_required = true;
1694
1695 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001696}
1697
Joerg Roedel8fe54652010-02-19 16:23:01 +01001698/* This function returns true if it is save to enable the irq window */
1699static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001700{
Joerg Roedel26666952009-08-07 11:49:46 +02001701 if (!is_nested(svm))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001702 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001703
Joerg Roedel26666952009-08-07 11:49:46 +02001704 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001705 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001706
Joerg Roedel26666952009-08-07 11:49:46 +02001707 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001708 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001709
Gleb Natapova0a07cd2010-09-20 10:15:32 +02001710 /*
1711 * if vmexit was already requested (by intercepted exception
1712 * for instance) do not overwrite it with "external interrupt"
1713 * vmexit.
1714 */
1715 if (svm->nested.exit_required)
1716 return false;
1717
Joerg Roedel197717d2010-02-24 18:59:19 +01001718 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
1719 svm->vmcb->control.exit_info_1 = 0;
1720 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02001721
Joerg Roedelcd3ff652009-10-09 16:08:26 +02001722 if (svm->nested.intercept & 1ULL) {
1723 /*
1724 * The #vmexit can't be emulated here directly because this
1725 * code path runs with irqs and preemtion disabled. A
1726 * #vmexit emulation might sleep. Only signal request for
1727 * the #vmexit here.
1728 */
1729 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02001730 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01001731 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001732 }
1733
Joerg Roedel8fe54652010-02-19 16:23:01 +01001734 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001735}
1736
Joerg Roedel887f5002010-02-24 18:59:12 +01001737/* This function returns true if it is save to enable the nmi window */
1738static inline bool nested_svm_nmi(struct vcpu_svm *svm)
1739{
1740 if (!is_nested(svm))
1741 return true;
1742
1743 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
1744 return true;
1745
1746 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
1747 svm->nested.exit_required = true;
1748
1749 return false;
1750}
1751
Joerg Roedel7597f122010-02-19 16:23:00 +01001752static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001753{
1754 struct page *page;
1755
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01001756 might_sleep();
1757
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001758 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001759 if (is_error_page(page))
1760 goto error;
1761
Joerg Roedel7597f122010-02-19 16:23:00 +01001762 *_page = page;
1763
1764 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001765
1766error:
1767 kvm_release_page_clean(page);
1768 kvm_inject_gp(&svm->vcpu, 0);
1769
1770 return NULL;
1771}
1772
Joerg Roedel7597f122010-02-19 16:23:00 +01001773static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001774{
Joerg Roedel7597f122010-02-19 16:23:00 +01001775 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001776 kvm_release_page_dirty(page);
1777}
1778
Joerg Roedelce2ac082010-03-01 15:34:39 +01001779static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001780{
Joerg Roedelce2ac082010-03-01 15:34:39 +01001781 unsigned port;
1782 u8 val, bit;
1783 u64 gpa;
1784
1785 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
1786 return NESTED_EXIT_HOST;
1787
1788 port = svm->vmcb->control.exit_info_1 >> 16;
1789 gpa = svm->nested.vmcb_iopm + (port / 8);
1790 bit = port % 8;
1791 val = 0;
1792
1793 if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1))
1794 val &= (1 << bit);
1795
1796 return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1797}
1798
Joerg Roedeld2477822010-03-01 15:34:34 +01001799static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001800{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001801 u32 offset, msr, value;
1802 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001803
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001804 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01001805 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001806
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001807 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1808 offset = svm_msrpm_offset(msr);
1809 write = svm->vmcb->control.exit_info_1 & 1;
1810 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001811
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001812 if (offset == MSR_INVALID)
1813 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001814
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001815 /* Offset is in 32 bit units but need in 8 bit units */
1816 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001817
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001818 if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4))
1819 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001820
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001821 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001822}
1823
Joerg Roedel410e4d52009-08-07 11:49:44 +02001824static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001825{
Alexander Grafcf74a782008-11-25 20:17:08 +01001826 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001827
Joerg Roedel410e4d52009-08-07 11:49:44 +02001828 switch (exit_code) {
1829 case SVM_EXIT_INTR:
1830 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02001831 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02001832 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001833 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01001834 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001835 if (npt_enabled)
1836 return NESTED_EXIT_HOST;
1837 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001838 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Joerg Roedele0231712010-02-24 18:59:10 +01001839 /* When we're shadowing, trap PFs */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001840 if (!npt_enabled)
1841 return NESTED_EXIT_HOST;
1842 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001843 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
1844 nm_interception(svm);
1845 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001846 default:
1847 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001848 }
1849
Joerg Roedel410e4d52009-08-07 11:49:44 +02001850 return NESTED_EXIT_CONTINUE;
1851}
1852
1853/*
1854 * If this function returns true, this #vmexit was already handled
1855 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001856static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001857{
1858 u32 exit_code = svm->vmcb->control.exit_code;
1859 int vmexit = NESTED_EXIT_HOST;
1860
Alexander Grafcf74a782008-11-25 20:17:08 +01001861 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001862 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001863 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001864 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01001865 case SVM_EXIT_IOIO:
1866 vmexit = nested_svm_intercept_ioio(svm);
1867 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001868 case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR8: {
1869 u32 cr_bits = 1 << (exit_code - SVM_EXIT_READ_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001870 if (svm->nested.intercept_cr_read & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001871 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001872 break;
1873 }
1874 case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR8: {
1875 u32 cr_bits = 1 << (exit_code - SVM_EXIT_WRITE_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001876 if (svm->nested.intercept_cr_write & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001877 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001878 break;
1879 }
1880 case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR7: {
1881 u32 dr_bits = 1 << (exit_code - SVM_EXIT_READ_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001882 if (svm->nested.intercept_dr_read & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001883 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001884 break;
1885 }
1886 case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR7: {
1887 u32 dr_bits = 1 << (exit_code - SVM_EXIT_WRITE_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001888 if (svm->nested.intercept_dr_write & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001889 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001890 break;
1891 }
1892 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1893 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001894 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001895 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001896 break;
1897 }
Joerg Roedel228070b2010-04-22 12:33:10 +02001898 case SVM_EXIT_ERR: {
1899 vmexit = NESTED_EXIT_DONE;
1900 break;
1901 }
Alexander Grafcf74a782008-11-25 20:17:08 +01001902 default: {
1903 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001904 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001905 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001906 }
1907 }
1908
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001909 return vmexit;
1910}
1911
1912static int nested_svm_exit_handled(struct vcpu_svm *svm)
1913{
1914 int vmexit;
1915
1916 vmexit = nested_svm_intercept(svm);
1917
1918 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001919 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001920
1921 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001922}
1923
Joerg Roedel0460a972009-08-07 11:49:31 +02001924static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
1925{
1926 struct vmcb_control_area *dst = &dst_vmcb->control;
1927 struct vmcb_control_area *from = &from_vmcb->control;
1928
1929 dst->intercept_cr_read = from->intercept_cr_read;
1930 dst->intercept_cr_write = from->intercept_cr_write;
1931 dst->intercept_dr_read = from->intercept_dr_read;
1932 dst->intercept_dr_write = from->intercept_dr_write;
1933 dst->intercept_exceptions = from->intercept_exceptions;
1934 dst->intercept = from->intercept;
1935 dst->iopm_base_pa = from->iopm_base_pa;
1936 dst->msrpm_base_pa = from->msrpm_base_pa;
1937 dst->tsc_offset = from->tsc_offset;
1938 dst->asid = from->asid;
1939 dst->tlb_ctl = from->tlb_ctl;
1940 dst->int_ctl = from->int_ctl;
1941 dst->int_vector = from->int_vector;
1942 dst->int_state = from->int_state;
1943 dst->exit_code = from->exit_code;
1944 dst->exit_code_hi = from->exit_code_hi;
1945 dst->exit_info_1 = from->exit_info_1;
1946 dst->exit_info_2 = from->exit_info_2;
1947 dst->exit_int_info = from->exit_int_info;
1948 dst->exit_int_info_err = from->exit_int_info_err;
1949 dst->nested_ctl = from->nested_ctl;
1950 dst->event_inj = from->event_inj;
1951 dst->event_inj_err = from->event_inj_err;
1952 dst->nested_cr3 = from->nested_cr3;
1953 dst->lbr_ctl = from->lbr_ctl;
1954}
1955
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001956static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001957{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001958 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001959 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02001960 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01001961 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01001962
Joerg Roedel17897f32009-10-09 16:08:29 +02001963 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
1964 vmcb->control.exit_info_1,
1965 vmcb->control.exit_info_2,
1966 vmcb->control.exit_int_info,
1967 vmcb->control.exit_int_info_err);
1968
Joerg Roedel7597f122010-02-19 16:23:00 +01001969 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001970 if (!nested_vmcb)
1971 return 1;
1972
Joerg Roedel06fc77722010-02-19 16:23:07 +01001973 /* Exit nested SVM mode */
1974 svm->nested.vmcb = 0;
1975
Alexander Grafcf74a782008-11-25 20:17:08 +01001976 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02001977 disable_gif(svm);
1978
1979 nested_vmcb->save.es = vmcb->save.es;
1980 nested_vmcb->save.cs = vmcb->save.cs;
1981 nested_vmcb->save.ss = vmcb->save.ss;
1982 nested_vmcb->save.ds = vmcb->save.ds;
1983 nested_vmcb->save.gdtr = vmcb->save.gdtr;
1984 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02001985 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001986 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedel2be4fc72010-04-22 12:33:09 +02001987 nested_vmcb->save.cr3 = svm->vcpu.arch.cr3;
Joerg Roedel33740e42009-08-07 11:49:29 +02001988 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001989 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Joerg Roedel33740e42009-08-07 11:49:29 +02001990 nested_vmcb->save.rflags = vmcb->save.rflags;
1991 nested_vmcb->save.rip = vmcb->save.rip;
1992 nested_vmcb->save.rsp = vmcb->save.rsp;
1993 nested_vmcb->save.rax = vmcb->save.rax;
1994 nested_vmcb->save.dr7 = vmcb->save.dr7;
1995 nested_vmcb->save.dr6 = vmcb->save.dr6;
1996 nested_vmcb->save.cpl = vmcb->save.cpl;
1997
1998 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
1999 nested_vmcb->control.int_vector = vmcb->control.int_vector;
2000 nested_vmcb->control.int_state = vmcb->control.int_state;
2001 nested_vmcb->control.exit_code = vmcb->control.exit_code;
2002 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
2003 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
2004 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
2005 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
2006 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel7a190662010-07-27 18:14:21 +02002007 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02002008
2009 /*
2010 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
2011 * to make sure that we do not lose injected events. So check event_inj
2012 * here and copy it to exit_int_info if it is valid.
2013 * Exit_int_info and event_inj can't be both valid because the case
2014 * below only happens on a VMRUN instruction intercept which has
2015 * no valid exit_int_info set.
2016 */
2017 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
2018 struct vmcb_control_area *nc = &nested_vmcb->control;
2019
2020 nc->exit_int_info = vmcb->control.event_inj;
2021 nc->exit_int_info_err = vmcb->control.event_inj_err;
2022 }
2023
Joerg Roedel33740e42009-08-07 11:49:29 +02002024 nested_vmcb->control.tlb_ctl = 0;
2025 nested_vmcb->control.event_inj = 0;
2026 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002027
2028 /* We always set V_INTR_MASKING and remember the old value in hflags */
2029 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
2030 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
2031
Alexander Grafcf74a782008-11-25 20:17:08 +01002032 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02002033 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01002034
Alexander Graf219b65d2009-06-15 15:21:25 +02002035 kvm_clear_exception_queue(&svm->vcpu);
2036 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002037
Joerg Roedel4b161842010-09-10 17:31:03 +02002038 svm->nested.nested_cr3 = 0;
2039
Alexander Grafcf74a782008-11-25 20:17:08 +01002040 /* Restore selected save entries */
2041 svm->vmcb->save.es = hsave->save.es;
2042 svm->vmcb->save.cs = hsave->save.cs;
2043 svm->vmcb->save.ss = hsave->save.ss;
2044 svm->vmcb->save.ds = hsave->save.ds;
2045 svm->vmcb->save.gdtr = hsave->save.gdtr;
2046 svm->vmcb->save.idtr = hsave->save.idtr;
2047 svm->vmcb->save.rflags = hsave->save.rflags;
2048 svm_set_efer(&svm->vcpu, hsave->save.efer);
2049 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
2050 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
2051 if (npt_enabled) {
2052 svm->vmcb->save.cr3 = hsave->save.cr3;
2053 svm->vcpu.arch.cr3 = hsave->save.cr3;
2054 } else {
Avi Kivity23902182010-06-10 17:02:16 +03002055 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01002056 }
2057 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
2058 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
2059 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
2060 svm->vmcb->save.dr7 = 0;
2061 svm->vmcb->save.cpl = 0;
2062 svm->vmcb->control.exit_int_info = 0;
2063
Joerg Roedel7597f122010-02-19 16:23:00 +01002064 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01002065
Joerg Roedel4b161842010-09-10 17:31:03 +02002066 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002067 kvm_mmu_reset_context(&svm->vcpu);
2068 kvm_mmu_load(&svm->vcpu);
2069
2070 return 0;
2071}
Alexander Graf3d6368e2008-11-25 20:17:07 +01002072
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002073static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002074{
Joerg Roedel323c3d82010-03-01 15:34:37 +01002075 /*
2076 * This function merges the msr permission bitmaps of kvm and the
2077 * nested vmcb. It is omptimized in that it only merges the parts where
2078 * the kvm msr permission bitmap may contain zero bits
2079 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01002080 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002081
Joerg Roedel323c3d82010-03-01 15:34:37 +01002082 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
2083 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002084
Joerg Roedel323c3d82010-03-01 15:34:37 +01002085 for (i = 0; i < MSRPM_OFFSETS; i++) {
2086 u32 value, p;
2087 u64 offset;
2088
2089 if (msrpm_offsets[i] == 0xffffffff)
2090 break;
2091
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002092 p = msrpm_offsets[i];
2093 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01002094
2095 if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4))
2096 return false;
2097
2098 svm->nested.msrpm[p] = svm->msrpm[p] | value;
2099 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002100
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002101 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002102
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002103 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002104}
2105
Joerg Roedel52c65a302010-08-02 16:46:44 +02002106static bool nested_vmcb_checks(struct vmcb *vmcb)
2107{
2108 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
2109 return false;
2110
Joerg Roedeldbe77582010-08-02 16:46:45 +02002111 if (vmcb->control.asid == 0)
2112 return false;
2113
Joerg Roedel4b161842010-09-10 17:31:03 +02002114 if (vmcb->control.nested_ctl && !npt_enabled)
2115 return false;
2116
Joerg Roedel52c65a302010-08-02 16:46:44 +02002117 return true;
2118}
2119
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002120static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002121{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002122 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002123 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02002124 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002125 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01002126 u64 vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002127
Joerg Roedel06fc77722010-02-19 16:23:07 +01002128 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002129
Joerg Roedel7597f122010-02-19 16:23:00 +01002130 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002131 if (!nested_vmcb)
2132 return false;
2133
Joerg Roedel52c65a302010-08-02 16:46:44 +02002134 if (!nested_vmcb_checks(nested_vmcb)) {
2135 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
2136 nested_vmcb->control.exit_code_hi = 0;
2137 nested_vmcb->control.exit_info_1 = 0;
2138 nested_vmcb->control.exit_info_2 = 0;
2139
2140 nested_svm_unmap(page);
2141
2142 return false;
2143 }
2144
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002145 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02002146 nested_vmcb->save.rip,
2147 nested_vmcb->control.int_ctl,
2148 nested_vmcb->control.event_inj,
2149 nested_vmcb->control.nested_ctl);
2150
Joerg Roedel2e554e82010-02-24 18:59:14 +01002151 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr_read,
2152 nested_vmcb->control.intercept_cr_write,
2153 nested_vmcb->control.intercept_exceptions,
2154 nested_vmcb->control.intercept);
2155
Alexander Graf3d6368e2008-11-25 20:17:07 +01002156 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02002157 kvm_clear_exception_queue(&svm->vcpu);
2158 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002159
Joerg Roedele0231712010-02-24 18:59:10 +01002160 /*
2161 * Save the old vmcb, so we don't need to pick what we save, but can
2162 * restore everything when a VMEXIT occurs
2163 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02002164 hsave->save.es = vmcb->save.es;
2165 hsave->save.cs = vmcb->save.cs;
2166 hsave->save.ss = vmcb->save.ss;
2167 hsave->save.ds = vmcb->save.ds;
2168 hsave->save.gdtr = vmcb->save.gdtr;
2169 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02002170 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002171 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002172 hsave->save.cr4 = svm->vcpu.arch.cr4;
2173 hsave->save.rflags = vmcb->save.rflags;
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002174 hsave->save.rip = kvm_rip_read(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002175 hsave->save.rsp = vmcb->save.rsp;
2176 hsave->save.rax = vmcb->save.rax;
2177 if (npt_enabled)
2178 hsave->save.cr3 = vmcb->save.cr3;
2179 else
2180 hsave->save.cr3 = svm->vcpu.arch.cr3;
2181
Joerg Roedel0460a972009-08-07 11:49:31 +02002182 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002183
2184 if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
2185 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2186 else
2187 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2188
Joerg Roedel4b161842010-09-10 17:31:03 +02002189 if (nested_vmcb->control.nested_ctl) {
2190 kvm_mmu_unload(&svm->vcpu);
2191 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
2192 nested_svm_init_mmu_context(&svm->vcpu);
2193 }
2194
Alexander Graf3d6368e2008-11-25 20:17:07 +01002195 /* Load the nested guest state */
2196 svm->vmcb->save.es = nested_vmcb->save.es;
2197 svm->vmcb->save.cs = nested_vmcb->save.cs;
2198 svm->vmcb->save.ss = nested_vmcb->save.ss;
2199 svm->vmcb->save.ds = nested_vmcb->save.ds;
2200 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2201 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
2202 svm->vmcb->save.rflags = nested_vmcb->save.rflags;
2203 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2204 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2205 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2206 if (npt_enabled) {
2207 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2208 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002209 } else
Avi Kivity23902182010-06-10 17:02:16 +03002210 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002211
2212 /* Guest paging mode is active - reset mmu */
2213 kvm_mmu_reset_context(&svm->vcpu);
2214
Joerg Roedeldefbba52009-08-07 11:49:30 +02002215 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002216 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2217 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2218 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01002219
Alexander Graf3d6368e2008-11-25 20:17:07 +01002220 /* In case we don't even reach vcpu_run, the fields are not updated */
2221 svm->vmcb->save.rax = nested_vmcb->save.rax;
2222 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2223 svm->vmcb->save.rip = nested_vmcb->save.rip;
2224 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2225 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2226 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2227
Joerg Roedelf7138532010-03-01 15:34:40 +01002228 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002229 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002230
Joerg Roedelaad42c62009-08-07 11:49:34 +02002231 /* cache intercepts */
2232 svm->nested.intercept_cr_read = nested_vmcb->control.intercept_cr_read;
2233 svm->nested.intercept_cr_write = nested_vmcb->control.intercept_cr_write;
2234 svm->nested.intercept_dr_read = nested_vmcb->control.intercept_dr_read;
2235 svm->nested.intercept_dr_write = nested_vmcb->control.intercept_dr_write;
2236 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2237 svm->nested.intercept = nested_vmcb->control.intercept;
2238
Alexander Graf3d6368e2008-11-25 20:17:07 +01002239 force_new_asid(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002240 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002241 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2242 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2243 else
2244 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2245
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002246 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2247 /* We only want the cr8 intercept bits of the guest */
2248 svm->vmcb->control.intercept_cr_read &= ~INTERCEPT_CR8_MASK;
2249 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
2250 }
2251
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002252 /* We don't want to see VMMCALLs from a nested guest */
2253 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VMMCALL);
2254
Joerg Roedele0231712010-02-24 18:59:10 +01002255 /*
2256 * We don't want a nested guest to be more powerful than the guest, so
2257 * all intercepts are ORed
2258 */
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002259 svm->vmcb->control.intercept_cr_read |=
2260 nested_vmcb->control.intercept_cr_read;
2261 svm->vmcb->control.intercept_cr_write |=
2262 nested_vmcb->control.intercept_cr_write;
2263 svm->vmcb->control.intercept_dr_read |=
2264 nested_vmcb->control.intercept_dr_read;
2265 svm->vmcb->control.intercept_dr_write |=
2266 nested_vmcb->control.intercept_dr_write;
2267 svm->vmcb->control.intercept_exceptions |=
2268 nested_vmcb->control.intercept_exceptions;
2269
2270 svm->vmcb->control.intercept |= nested_vmcb->control.intercept;
2271
2272 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002273 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2274 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2275 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002276 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2277 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2278
Joerg Roedel7597f122010-02-19 16:23:00 +01002279 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002280
Joerg Roedel06fc77722010-02-19 16:23:07 +01002281 /* nested_vmcb is our indicator if nested SVM is activated */
2282 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002283
Joerg Roedel2af91942009-08-07 11:49:28 +02002284 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002285
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002286 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002287}
2288
Joerg Roedel9966bf62009-08-07 11:49:40 +02002289static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002290{
2291 to_vmcb->save.fs = from_vmcb->save.fs;
2292 to_vmcb->save.gs = from_vmcb->save.gs;
2293 to_vmcb->save.tr = from_vmcb->save.tr;
2294 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2295 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2296 to_vmcb->save.star = from_vmcb->save.star;
2297 to_vmcb->save.lstar = from_vmcb->save.lstar;
2298 to_vmcb->save.cstar = from_vmcb->save.cstar;
2299 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2300 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2301 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2302 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002303}
2304
Avi Kivity851ba692009-08-24 11:10:17 +03002305static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002306{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002307 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002308 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002309
Alexander Graf55426752008-11-25 20:17:06 +01002310 if (nested_svm_check_permissions(svm))
2311 return 1;
2312
2313 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2314 skip_emulated_instruction(&svm->vcpu);
2315
Joerg Roedel7597f122010-02-19 16:23:00 +01002316 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002317 if (!nested_vmcb)
2318 return 1;
2319
2320 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002321 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002322
2323 return 1;
2324}
2325
Avi Kivity851ba692009-08-24 11:10:17 +03002326static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002327{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002328 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002329 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002330
Alexander Graf55426752008-11-25 20:17:06 +01002331 if (nested_svm_check_permissions(svm))
2332 return 1;
2333
2334 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2335 skip_emulated_instruction(&svm->vcpu);
2336
Joerg Roedel7597f122010-02-19 16:23:00 +01002337 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002338 if (!nested_vmcb)
2339 return 1;
2340
2341 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002342 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002343
2344 return 1;
2345}
2346
Avi Kivity851ba692009-08-24 11:10:17 +03002347static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002348{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002349 if (nested_svm_check_permissions(svm))
2350 return 1;
2351
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002352 /* Save rip after vmrun instruction */
2353 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002354
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002355 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002356 return 1;
2357
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002358 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002359 goto failed;
2360
2361 return 1;
2362
2363failed:
2364
2365 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2366 svm->vmcb->control.exit_code_hi = 0;
2367 svm->vmcb->control.exit_info_1 = 0;
2368 svm->vmcb->control.exit_info_2 = 0;
2369
2370 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002371
2372 return 1;
2373}
2374
Avi Kivity851ba692009-08-24 11:10:17 +03002375static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002376{
2377 if (nested_svm_check_permissions(svm))
2378 return 1;
2379
2380 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2381 skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03002382 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01002383
Joerg Roedel2af91942009-08-07 11:49:28 +02002384 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002385
2386 return 1;
2387}
2388
Avi Kivity851ba692009-08-24 11:10:17 +03002389static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002390{
2391 if (nested_svm_check_permissions(svm))
2392 return 1;
2393
2394 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2395 skip_emulated_instruction(&svm->vcpu);
2396
Joerg Roedel2af91942009-08-07 11:49:28 +02002397 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002398
2399 /* After a CLGI no interrupts should come */
2400 svm_clear_vintr(svm);
2401 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2402
2403 return 1;
2404}
2405
Avi Kivity851ba692009-08-24 11:10:17 +03002406static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002407{
2408 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002409
Joerg Roedelec1ff792009-10-09 16:08:31 +02002410 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
2411 vcpu->arch.regs[VCPU_REGS_RAX]);
2412
Alexander Grafff092382009-06-15 15:21:24 +02002413 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2414 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
2415
2416 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2417 skip_emulated_instruction(&svm->vcpu);
2418 return 1;
2419}
2420
Joerg Roedel532a46b2009-10-09 16:08:32 +02002421static int skinit_interception(struct vcpu_svm *svm)
2422{
2423 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
2424
2425 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2426 return 1;
2427}
2428
Avi Kivity851ba692009-08-24 11:10:17 +03002429static int invalid_op_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002430{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002431 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002432 return 1;
2433}
2434
Avi Kivity851ba692009-08-24 11:10:17 +03002435static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002436{
Izik Eidus37817f22008-03-24 23:14:53 +02002437 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002438 int reason;
2439 int int_type = svm->vmcb->control.exit_int_info &
2440 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002441 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002442 uint32_t type =
2443 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2444 uint32_t idt_v =
2445 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002446 bool has_error_code = false;
2447 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002448
2449 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002450
Izik Eidus37817f22008-03-24 23:14:53 +02002451 if (svm->vmcb->control.exit_info_2 &
2452 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002453 reason = TASK_SWITCH_IRET;
2454 else if (svm->vmcb->control.exit_info_2 &
2455 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2456 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002457 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002458 reason = TASK_SWITCH_GATE;
2459 else
2460 reason = TASK_SWITCH_CALL;
2461
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002462 if (reason == TASK_SWITCH_GATE) {
2463 switch (type) {
2464 case SVM_EXITINTINFO_TYPE_NMI:
2465 svm->vcpu.arch.nmi_injected = false;
2466 break;
2467 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002468 if (svm->vmcb->control.exit_info_2 &
2469 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2470 has_error_code = true;
2471 error_code =
2472 (u32)svm->vmcb->control.exit_info_2;
2473 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002474 kvm_clear_exception_queue(&svm->vcpu);
2475 break;
2476 case SVM_EXITINTINFO_TYPE_INTR:
2477 kvm_clear_interrupt_queue(&svm->vcpu);
2478 break;
2479 default:
2480 break;
2481 }
2482 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002483
Gleb Natapov8317c292009-04-12 13:37:02 +03002484 if (reason != TASK_SWITCH_GATE ||
2485 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2486 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002487 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2488 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002489
Gleb Natapovacb54512010-04-15 21:03:50 +03002490 if (kvm_task_switch(&svm->vcpu, tss_selector, reason,
2491 has_error_code, error_code) == EMULATE_FAIL) {
2492 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2493 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2494 svm->vcpu.run->internal.ndata = 0;
2495 return 0;
2496 }
2497 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002498}
2499
Avi Kivity851ba692009-08-24 11:10:17 +03002500static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002501{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002502 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002503 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002504 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002505}
2506
Avi Kivity851ba692009-08-24 11:10:17 +03002507static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002508{
2509 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02002510 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002511 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002512 return 1;
2513}
2514
Avi Kivity851ba692009-08-24 11:10:17 +03002515static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002516{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002517 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002518}
2519
Avi Kivity851ba692009-08-24 11:10:17 +03002520static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002521{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002522 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002523}
2524
Joerg Roedelcda00082010-09-02 17:29:46 +02002525static int cr0_write_interception(struct vcpu_svm *svm)
2526{
2527 struct kvm_vcpu *vcpu = &svm->vcpu;
2528 int r;
2529
2530 r = emulate_instruction(&svm->vcpu, 0, 0, 0);
2531
2532 if (svm->nested.vmexit_rip) {
2533 kvm_register_write(vcpu, VCPU_REGS_RIP, svm->nested.vmexit_rip);
2534 kvm_register_write(vcpu, VCPU_REGS_RSP, svm->nested.vmexit_rsp);
2535 kvm_register_write(vcpu, VCPU_REGS_RAX, svm->nested.vmexit_rax);
2536 svm->nested.vmexit_rip = 0;
2537 }
2538
2539 return r == EMULATE_DONE;
2540}
2541
Avi Kivity851ba692009-08-24 11:10:17 +03002542static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01002543{
Avi Kivity851ba692009-08-24 11:10:17 +03002544 struct kvm_run *kvm_run = svm->vcpu.run;
2545
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002546 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
2547 /* instruction emulation calls kvm_set_cr8() */
Avi Kivity851ba692009-08-24 11:10:17 +03002548 emulate_instruction(&svm->vcpu, 0, 0, 0);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002549 if (irqchip_in_kernel(svm->vcpu.kvm)) {
2550 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
Joerg Roedel1d075432007-12-06 21:02:25 +01002551 return 1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002552 }
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002553 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
2554 return 1;
Joerg Roedel1d075432007-12-06 21:02:25 +01002555 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2556 return 0;
2557}
2558
Avi Kivity6aa8b732006-12-10 02:21:36 -08002559static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
2560{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002561 struct vcpu_svm *svm = to_svm(vcpu);
2562
Avi Kivity6aa8b732006-12-10 02:21:36 -08002563 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302564 case MSR_IA32_TSC: {
Joerg Roedel20824f32009-09-16 15:24:18 +02002565 u64 tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002566
Joerg Roedel20824f32009-09-16 15:24:18 +02002567 if (is_nested(svm))
2568 tsc_offset = svm->nested.hsave->control.tsc_offset;
2569 else
2570 tsc_offset = svm->vmcb->control.tsc_offset;
2571
2572 *data = tsc_offset + native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002573 break;
2574 }
Brian Gerst8c065852010-07-17 09:03:26 -04002575 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002576 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002577 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08002578#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002579 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002580 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002581 break;
2582 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002583 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002584 break;
2585 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002586 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002587 break;
2588 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002589 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002590 break;
2591#endif
2592 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002593 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002594 break;
2595 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002596 *data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002597 break;
2598 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002599 *data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002600 break;
Joerg Roedele0231712010-02-24 18:59:10 +01002601 /*
2602 * Nobody will change the following 5 values in the VMCB so we can
2603 * safely return them on rdmsr. They will always be 0 until LBRV is
2604 * implemented.
2605 */
Joerg Roedela2938c82008-02-13 16:30:28 +01002606 case MSR_IA32_DEBUGCTLMSR:
2607 *data = svm->vmcb->save.dbgctl;
2608 break;
2609 case MSR_IA32_LASTBRANCHFROMIP:
2610 *data = svm->vmcb->save.br_from;
2611 break;
2612 case MSR_IA32_LASTBRANCHTOIP:
2613 *data = svm->vmcb->save.br_to;
2614 break;
2615 case MSR_IA32_LASTINTFROMIP:
2616 *data = svm->vmcb->save.last_excp_from;
2617 break;
2618 case MSR_IA32_LASTINTTOIP:
2619 *data = svm->vmcb->save.last_excp_to;
2620 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002621 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002622 *data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002623 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002624 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002625 *data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002626 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01002627 case MSR_IA32_UCODE_REV:
2628 *data = 0x01000065;
2629 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002630 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002631 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002632 }
2633 return 0;
2634}
2635
Avi Kivity851ba692009-08-24 11:10:17 +03002636static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002637{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002638 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002639 u64 data;
2640
Avi Kivity59200272010-01-25 19:47:02 +02002641 if (svm_get_msr(&svm->vcpu, ecx, &data)) {
2642 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002643 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002644 } else {
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002645 trace_kvm_msr_read(ecx, data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002646
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002647 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002648 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002649 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002650 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002651 }
2652 return 1;
2653}
2654
Joerg Roedel4a810182010-02-24 18:59:15 +01002655static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2656{
2657 struct vcpu_svm *svm = to_svm(vcpu);
2658 int svm_dis, chg_mask;
2659
2660 if (data & ~SVM_VM_CR_VALID_MASK)
2661 return 1;
2662
2663 chg_mask = SVM_VM_CR_VALID_MASK;
2664
2665 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2666 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2667
2668 svm->nested.vm_cr_msr &= ~chg_mask;
2669 svm->nested.vm_cr_msr |= (data & chg_mask);
2670
2671 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2672
2673 /* check for svm_disable while efer.svme is set */
2674 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2675 return 1;
2676
2677 return 0;
2678}
2679
Avi Kivity6aa8b732006-12-10 02:21:36 -08002680static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
2681{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002682 struct vcpu_svm *svm = to_svm(vcpu);
2683
Avi Kivity6aa8b732006-12-10 02:21:36 -08002684 switch (ecx) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10002685 case MSR_IA32_TSC:
Zachary Amsden99e3e302010-08-19 22:07:17 -10002686 kvm_write_tsc(vcpu, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002687 break;
Brian Gerst8c065852010-07-17 09:03:26 -04002688 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002689 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002690 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08002691#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002692 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002693 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002694 break;
2695 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002696 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002697 break;
2698 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002699 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002700 break;
2701 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002702 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002703 break;
2704#endif
2705 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002706 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002707 break;
2708 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002709 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002710 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002711 break;
2712 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002713 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002714 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002715 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002716 case MSR_IA32_DEBUGCTLMSR:
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002717 if (!svm_has(SVM_FEATURE_LBRV)) {
2718 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002719 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002720 break;
2721 }
2722 if (data & DEBUGCTL_RESERVED_BITS)
2723 return 1;
2724
2725 svm->vmcb->save.dbgctl = data;
2726 if (data & (1ULL<<0))
2727 svm_enable_lbrv(svm);
2728 else
2729 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01002730 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002731 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002732 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002733 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002734 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002735 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002736 case MSR_VM_IGNNE:
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002737 pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
2738 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002739 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002740 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002741 }
2742 return 0;
2743}
2744
Avi Kivity851ba692009-08-24 11:10:17 +03002745static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002746{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002747 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002748 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002749 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002750
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002751
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002752 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Avi Kivity59200272010-01-25 19:47:02 +02002753 if (svm_set_msr(&svm->vcpu, ecx, data)) {
2754 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002755 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002756 } else {
2757 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10002758 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02002759 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002760 return 1;
2761}
2762
Avi Kivity851ba692009-08-24 11:10:17 +03002763static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002764{
Rusty Russelle756fc62007-07-30 20:07:08 +10002765 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03002766 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002767 else
Avi Kivity851ba692009-08-24 11:10:17 +03002768 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002769}
2770
Avi Kivity851ba692009-08-24 11:10:17 +03002771static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08002772{
Avi Kivity851ba692009-08-24 11:10:17 +03002773 struct kvm_run *kvm_run = svm->vcpu.run;
2774
Avi Kivity3842d132010-07-27 12:30:24 +03002775 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01002776 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03002777 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -08002778 /*
2779 * If the user space waits to inject interrupts, exit as soon as
2780 * possible
2781 */
Gleb Natapov80618232009-04-21 17:44:56 +03002782 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
2783 kvm_run->request_interrupt_window &&
2784 !kvm_cpu_has_interrupt(&svm->vcpu)) {
Rusty Russelle756fc62007-07-30 20:07:08 +10002785 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002786 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
2787 return 0;
2788 }
2789
2790 return 1;
2791}
2792
Mark Langsdorf565d0992009-10-06 14:25:02 -05002793static int pause_interception(struct vcpu_svm *svm)
2794{
2795 kvm_vcpu_on_spin(&(svm->vcpu));
2796 return 1;
2797}
2798
Avi Kivity851ba692009-08-24 11:10:17 +03002799static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
Joerg Roedele0231712010-02-24 18:59:10 +01002800 [SVM_EXIT_READ_CR0] = emulate_on_interception,
2801 [SVM_EXIT_READ_CR3] = emulate_on_interception,
2802 [SVM_EXIT_READ_CR4] = emulate_on_interception,
2803 [SVM_EXIT_READ_CR8] = emulate_on_interception,
Avi Kivityd2251572010-01-06 10:55:27 +02002804 [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
Joerg Roedelcda00082010-09-02 17:29:46 +02002805 [SVM_EXIT_WRITE_CR0] = cr0_write_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002806 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
2807 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
2808 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
2809 [SVM_EXIT_READ_DR0] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002810 [SVM_EXIT_READ_DR1] = emulate_on_interception,
2811 [SVM_EXIT_READ_DR2] = emulate_on_interception,
2812 [SVM_EXIT_READ_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002813 [SVM_EXIT_READ_DR4] = emulate_on_interception,
2814 [SVM_EXIT_READ_DR5] = emulate_on_interception,
2815 [SVM_EXIT_READ_DR6] = emulate_on_interception,
2816 [SVM_EXIT_READ_DR7] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002817 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
2818 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
2819 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
2820 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002821 [SVM_EXIT_WRITE_DR4] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002822 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002823 [SVM_EXIT_WRITE_DR6] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002824 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002825 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
2826 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002827 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002828 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
2829 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
2830 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
2831 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02002832 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002833 [SVM_EXIT_SMI] = nop_on_interception,
2834 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08002835 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002836 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002837 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002838 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05002839 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002840 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03002841 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02002842 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002843 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002844 [SVM_EXIT_MSR] = msr_interception,
2845 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002846 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01002847 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02002848 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01002849 [SVM_EXIT_VMLOAD] = vmload_interception,
2850 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01002851 [SVM_EXIT_STGI] = stgi_interception,
2852 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02002853 [SVM_EXIT_SKINIT] = skinit_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002854 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01002855 [SVM_EXIT_MONITOR] = invalid_op_interception,
2856 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002857 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002858};
2859
Joerg Roedel3f10c842010-05-05 16:04:42 +02002860void dump_vmcb(struct kvm_vcpu *vcpu)
2861{
2862 struct vcpu_svm *svm = to_svm(vcpu);
2863 struct vmcb_control_area *control = &svm->vmcb->control;
2864 struct vmcb_save_area *save = &svm->vmcb->save;
2865
2866 pr_err("VMCB Control Area:\n");
2867 pr_err("cr_read: %04x\n", control->intercept_cr_read);
2868 pr_err("cr_write: %04x\n", control->intercept_cr_write);
2869 pr_err("dr_read: %04x\n", control->intercept_dr_read);
2870 pr_err("dr_write: %04x\n", control->intercept_dr_write);
2871 pr_err("exceptions: %08x\n", control->intercept_exceptions);
2872 pr_err("intercepts: %016llx\n", control->intercept);
2873 pr_err("pause filter count: %d\n", control->pause_filter_count);
2874 pr_err("iopm_base_pa: %016llx\n", control->iopm_base_pa);
2875 pr_err("msrpm_base_pa: %016llx\n", control->msrpm_base_pa);
2876 pr_err("tsc_offset: %016llx\n", control->tsc_offset);
2877 pr_err("asid: %d\n", control->asid);
2878 pr_err("tlb_ctl: %d\n", control->tlb_ctl);
2879 pr_err("int_ctl: %08x\n", control->int_ctl);
2880 pr_err("int_vector: %08x\n", control->int_vector);
2881 pr_err("int_state: %08x\n", control->int_state);
2882 pr_err("exit_code: %08x\n", control->exit_code);
2883 pr_err("exit_info1: %016llx\n", control->exit_info_1);
2884 pr_err("exit_info2: %016llx\n", control->exit_info_2);
2885 pr_err("exit_int_info: %08x\n", control->exit_int_info);
2886 pr_err("exit_int_info_err: %08x\n", control->exit_int_info_err);
2887 pr_err("nested_ctl: %lld\n", control->nested_ctl);
2888 pr_err("nested_cr3: %016llx\n", control->nested_cr3);
2889 pr_err("event_inj: %08x\n", control->event_inj);
2890 pr_err("event_inj_err: %08x\n", control->event_inj_err);
2891 pr_err("lbr_ctl: %lld\n", control->lbr_ctl);
2892 pr_err("next_rip: %016llx\n", control->next_rip);
2893 pr_err("VMCB State Save Area:\n");
2894 pr_err("es: s: %04x a: %04x l: %08x b: %016llx\n",
2895 save->es.selector, save->es.attrib,
2896 save->es.limit, save->es.base);
2897 pr_err("cs: s: %04x a: %04x l: %08x b: %016llx\n",
2898 save->cs.selector, save->cs.attrib,
2899 save->cs.limit, save->cs.base);
2900 pr_err("ss: s: %04x a: %04x l: %08x b: %016llx\n",
2901 save->ss.selector, save->ss.attrib,
2902 save->ss.limit, save->ss.base);
2903 pr_err("ds: s: %04x a: %04x l: %08x b: %016llx\n",
2904 save->ds.selector, save->ds.attrib,
2905 save->ds.limit, save->ds.base);
2906 pr_err("fs: s: %04x a: %04x l: %08x b: %016llx\n",
2907 save->fs.selector, save->fs.attrib,
2908 save->fs.limit, save->fs.base);
2909 pr_err("gs: s: %04x a: %04x l: %08x b: %016llx\n",
2910 save->gs.selector, save->gs.attrib,
2911 save->gs.limit, save->gs.base);
2912 pr_err("gdtr: s: %04x a: %04x l: %08x b: %016llx\n",
2913 save->gdtr.selector, save->gdtr.attrib,
2914 save->gdtr.limit, save->gdtr.base);
2915 pr_err("ldtr: s: %04x a: %04x l: %08x b: %016llx\n",
2916 save->ldtr.selector, save->ldtr.attrib,
2917 save->ldtr.limit, save->ldtr.base);
2918 pr_err("idtr: s: %04x a: %04x l: %08x b: %016llx\n",
2919 save->idtr.selector, save->idtr.attrib,
2920 save->idtr.limit, save->idtr.base);
2921 pr_err("tr: s: %04x a: %04x l: %08x b: %016llx\n",
2922 save->tr.selector, save->tr.attrib,
2923 save->tr.limit, save->tr.base);
2924 pr_err("cpl: %d efer: %016llx\n",
2925 save->cpl, save->efer);
2926 pr_err("cr0: %016llx cr2: %016llx\n",
2927 save->cr0, save->cr2);
2928 pr_err("cr3: %016llx cr4: %016llx\n",
2929 save->cr3, save->cr4);
2930 pr_err("dr6: %016llx dr7: %016llx\n",
2931 save->dr6, save->dr7);
2932 pr_err("rip: %016llx rflags: %016llx\n",
2933 save->rip, save->rflags);
2934 pr_err("rsp: %016llx rax: %016llx\n",
2935 save->rsp, save->rax);
2936 pr_err("star: %016llx lstar: %016llx\n",
2937 save->star, save->lstar);
2938 pr_err("cstar: %016llx sfmask: %016llx\n",
2939 save->cstar, save->sfmask);
2940 pr_err("kernel_gs_base: %016llx sysenter_cs: %016llx\n",
2941 save->kernel_gs_base, save->sysenter_cs);
2942 pr_err("sysenter_esp: %016llx sysenter_eip: %016llx\n",
2943 save->sysenter_esp, save->sysenter_eip);
2944 pr_err("gpat: %016llx dbgctl: %016llx\n",
2945 save->g_pat, save->dbgctl);
2946 pr_err("br_from: %016llx br_to: %016llx\n",
2947 save->br_from, save->br_to);
2948 pr_err("excp_from: %016llx excp_to: %016llx\n",
2949 save->last_excp_from, save->last_excp_to);
2950
2951}
2952
Avi Kivity851ba692009-08-24 11:10:17 +03002953static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002954{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002955 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002956 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002957 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002958
Avi Kivity5bfd8b52010-03-11 10:50:44 +02002959 trace_kvm_exit(exit_code, vcpu);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002960
Joerg Roedel2be4fc72010-04-22 12:33:09 +02002961 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR0_MASK))
2962 vcpu->arch.cr0 = svm->vmcb->save.cr0;
2963 if (npt_enabled)
2964 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002965
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002966 if (unlikely(svm->nested.exit_required)) {
2967 nested_svm_vmexit(svm);
2968 svm->nested.exit_required = false;
2969
2970 return 1;
2971 }
2972
Alexander Grafcf74a782008-11-25 20:17:08 +01002973 if (is_nested(svm)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02002974 int vmexit;
2975
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02002976 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
2977 svm->vmcb->control.exit_info_1,
2978 svm->vmcb->control.exit_info_2,
2979 svm->vmcb->control.exit_int_info,
2980 svm->vmcb->control.exit_int_info_err);
2981
Joerg Roedel410e4d52009-08-07 11:49:44 +02002982 vmexit = nested_svm_exit_special(svm);
2983
2984 if (vmexit == NESTED_EXIT_CONTINUE)
2985 vmexit = nested_svm_exit_handled(svm);
2986
2987 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01002988 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01002989 }
2990
Joerg Roedela5c38322009-08-07 11:49:32 +02002991 svm_complete_interrupts(svm);
2992
Avi Kivity04d2cc72007-09-10 18:10:54 +03002993 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
2994 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2995 kvm_run->fail_entry.hardware_entry_failure_reason
2996 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02002997 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
2998 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03002999 return 0;
3000 }
3001
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003002 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003003 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02003004 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3005 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003006 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
3007 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003008 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003009 exit_code);
3010
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02003011 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08003012 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003013 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03003014 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003015 return 0;
3016 }
3017
Avi Kivity851ba692009-08-24 11:10:17 +03003018 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003019}
3020
3021static void reload_tss(struct kvm_vcpu *vcpu)
3022{
3023 int cpu = raw_smp_processor_id();
3024
Tejun Heo0fe1e002009-10-29 22:34:14 +09003025 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
3026 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003027 load_TR_desc();
3028}
3029
Rusty Russelle756fc62007-07-30 20:07:08 +10003030static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003031{
3032 int cpu = raw_smp_processor_id();
3033
Tejun Heo0fe1e002009-10-29 22:34:14 +09003034 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003035
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003036 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03003037 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09003038 if (svm->asid_generation != sd->asid_generation)
3039 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003040}
3041
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003042static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3043{
3044 struct vcpu_svm *svm = to_svm(vcpu);
3045
3046 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3047 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02003048 svm->vmcb->control.intercept |= (1ULL << INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003049 ++vcpu->stat.nmi_injections;
3050}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003051
Eddie Dong85f455f2007-07-06 12:20:49 +03003052static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003053{
3054 struct vmcb_control_area *control;
3055
Rusty Russelle756fc62007-07-30 20:07:08 +10003056 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03003057 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003058 control->int_ctl &= ~V_INTR_PRIO_MASK;
3059 control->int_ctl |= V_IRQ_MASK |
3060 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
3061}
3062
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003063static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03003064{
3065 struct vcpu_svm *svm = to_svm(vcpu);
3066
Joerg Roedel2af91942009-08-07 11:49:28 +02003067 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01003068
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03003069 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3070 ++vcpu->stat.irq_injections;
3071
Alexander Graf219b65d2009-06-15 15:21:25 +02003072 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3073 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03003074}
3075
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003076static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
3077{
3078 struct vcpu_svm *svm = to_svm(vcpu);
3079
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003080 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
3081 return;
3082
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003083 if (irr == -1)
3084 return;
3085
3086 if (tpr >= irr)
3087 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK;
3088}
3089
3090static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003091{
3092 struct vcpu_svm *svm = to_svm(vcpu);
3093 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02003094 int ret;
3095 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
3096 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
3097 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
3098
3099 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003100}
3101
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003102static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3103{
3104 struct vcpu_svm *svm = to_svm(vcpu);
3105
3106 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
3107}
3108
3109static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3110{
3111 struct vcpu_svm *svm = to_svm(vcpu);
3112
3113 if (masked) {
3114 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02003115 svm->vmcb->control.intercept |= (1ULL << INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003116 } else {
3117 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02003118 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003119 }
3120}
3121
Gleb Natapov78646122009-03-23 12:12:11 +02003122static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
3123{
3124 struct vcpu_svm *svm = to_svm(vcpu);
3125 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003126 int ret;
3127
3128 if (!gif_set(svm) ||
3129 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
3130 return 0;
3131
3132 ret = !!(vmcb->save.rflags & X86_EFLAGS_IF);
3133
3134 if (is_nested(svm))
3135 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
3136
3137 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02003138}
3139
Gleb Natapov9222be12009-04-23 17:14:37 +03003140static void enable_irq_window(struct kvm_vcpu *vcpu)
3141{
Alexander Graf219b65d2009-06-15 15:21:25 +02003142 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02003143
Joerg Roedele0231712010-02-24 18:59:10 +01003144 /*
3145 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3146 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3147 * get that intercept, this function will be called again though and
3148 * we'll get the vintr intercept.
3149 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01003150 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02003151 svm_set_vintr(svm);
3152 svm_inject_irq(svm, 0x0);
3153 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003154}
3155
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003156static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003157{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003158 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003159
Gleb Natapov44c11432009-05-11 13:35:52 +03003160 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
3161 == HF_NMI_MASK)
3162 return; /* IRET will cause a vm exit */
3163
Joerg Roedele0231712010-02-24 18:59:10 +01003164 /*
3165 * Something prevents NMI from been injected. Single step over possible
3166 * problem (IRET or exception injection or interrupt shadow)
3167 */
Jan Kiszka6be7d302009-10-18 13:24:54 +02003168 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03003169 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
3170 update_db_intercept(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003171}
3172
Izik Eiduscbc94022007-10-25 00:29:55 +02003173static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3174{
3175 return 0;
3176}
3177
Avi Kivityd9e368d2007-06-07 19:18:30 +03003178static void svm_flush_tlb(struct kvm_vcpu *vcpu)
3179{
3180 force_new_asid(vcpu);
3181}
3182
Avi Kivity04d2cc72007-09-10 18:10:54 +03003183static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
3184{
3185}
3186
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003187static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3188{
3189 struct vcpu_svm *svm = to_svm(vcpu);
3190
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003191 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
3192 return;
3193
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003194 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) {
3195 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03003196 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003197 }
3198}
3199
Joerg Roedel649d6862008-04-16 16:51:15 +02003200static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3201{
3202 struct vcpu_svm *svm = to_svm(vcpu);
3203 u64 cr8;
3204
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003205 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
3206 return;
3207
Joerg Roedel649d6862008-04-16 16:51:15 +02003208 cr8 = kvm_get_cr8(vcpu);
3209 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3210 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3211}
3212
Gleb Natapov9222be12009-04-23 17:14:37 +03003213static void svm_complete_interrupts(struct vcpu_svm *svm)
3214{
3215 u8 vector;
3216 int type;
3217 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01003218 unsigned int3_injected = svm->int3_injected;
3219
3220 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003221
Avi Kivity3842d132010-07-27 12:30:24 +03003222 if (svm->vcpu.arch.hflags & HF_IRET_MASK) {
Gleb Natapov44c11432009-05-11 13:35:52 +03003223 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03003224 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3225 }
Gleb Natapov44c11432009-05-11 13:35:52 +03003226
Gleb Natapov9222be12009-04-23 17:14:37 +03003227 svm->vcpu.arch.nmi_injected = false;
3228 kvm_clear_exception_queue(&svm->vcpu);
3229 kvm_clear_interrupt_queue(&svm->vcpu);
3230
3231 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3232 return;
3233
Avi Kivity3842d132010-07-27 12:30:24 +03003234 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3235
Gleb Natapov9222be12009-04-23 17:14:37 +03003236 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3237 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3238
3239 switch (type) {
3240 case SVM_EXITINTINFO_TYPE_NMI:
3241 svm->vcpu.arch.nmi_injected = true;
3242 break;
3243 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01003244 /*
3245 * In case of software exceptions, do not reinject the vector,
3246 * but re-execute the instruction instead. Rewind RIP first
3247 * if we emulated INT3 before.
3248 */
3249 if (kvm_exception_is_soft(vector)) {
3250 if (vector == BP_VECTOR && int3_injected &&
3251 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
3252 kvm_rip_write(&svm->vcpu,
3253 kvm_rip_read(&svm->vcpu) -
3254 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02003255 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01003256 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003257 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3258 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003259 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03003260
3261 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003262 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03003263 break;
3264 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003265 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03003266 break;
3267 default:
3268 break;
3269 }
3270}
3271
Avi Kivityb463a6f2010-07-20 15:06:17 +03003272static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3273{
3274 struct vcpu_svm *svm = to_svm(vcpu);
3275 struct vmcb_control_area *control = &svm->vmcb->control;
3276
3277 control->exit_int_info = control->event_inj;
3278 control->exit_int_info_err = control->event_inj_err;
3279 control->event_inj = 0;
3280 svm_complete_interrupts(svm);
3281}
3282
Avi Kivity80e31d42008-07-14 14:44:59 +03003283#ifdef CONFIG_X86_64
3284#define R "r"
3285#else
3286#define R "e"
3287#endif
3288
Avi Kivity851ba692009-08-24 11:10:17 +03003289static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003290{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003291 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003292 u16 fs_selector;
3293 u16 gs_selector;
3294 u16 ldt_selector;
Avi Kivityd9e368d2007-06-07 19:18:30 +03003295
Joerg Roedel2041a062010-04-22 12:33:08 +02003296 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3297 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3298 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3299
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003300 /*
3301 * A vmexit emulation is required before the vcpu can be executed
3302 * again.
3303 */
3304 if (unlikely(svm->nested.exit_required))
3305 return;
3306
Rusty Russelle756fc62007-07-30 20:07:08 +10003307 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003308
Joerg Roedel649d6862008-04-16 16:51:15 +02003309 sync_lapic_to_cr8(vcpu);
3310
Avi Kivity6aa8b732006-12-10 02:21:36 -08003311 save_host_msrs(vcpu);
Avi Kivity9581d442010-10-19 16:46:55 +02003312 savesegment(fs, fs_selector);
3313 savesegment(gs, gs_selector);
Avi Kivityd6e88ae2008-07-10 16:53:33 +03003314 ldt_selector = kvm_read_ldt();
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003315 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003316
Avi Kivity04d2cc72007-09-10 18:10:54 +03003317 clgi();
3318
3319 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08003320
Avi Kivity6aa8b732006-12-10 02:21:36 -08003321 asm volatile (
Avi Kivity80e31d42008-07-14 14:44:59 +03003322 "push %%"R"bp; \n\t"
3323 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
3324 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
3325 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
3326 "mov %c[rsi](%[svm]), %%"R"si \n\t"
3327 "mov %c[rdi](%[svm]), %%"R"di \n\t"
3328 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003329#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003330 "mov %c[r8](%[svm]), %%r8 \n\t"
3331 "mov %c[r9](%[svm]), %%r9 \n\t"
3332 "mov %c[r10](%[svm]), %%r10 \n\t"
3333 "mov %c[r11](%[svm]), %%r11 \n\t"
3334 "mov %c[r12](%[svm]), %%r12 \n\t"
3335 "mov %c[r13](%[svm]), %%r13 \n\t"
3336 "mov %c[r14](%[svm]), %%r14 \n\t"
3337 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003338#endif
3339
Avi Kivity6aa8b732006-12-10 02:21:36 -08003340 /* Enter guest mode */
Avi Kivity80e31d42008-07-14 14:44:59 +03003341 "push %%"R"ax \n\t"
3342 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003343 __ex(SVM_VMLOAD) "\n\t"
3344 __ex(SVM_VMRUN) "\n\t"
3345 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity80e31d42008-07-14 14:44:59 +03003346 "pop %%"R"ax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003347
3348 /* Save guest registers, load host registers */
Avi Kivity80e31d42008-07-14 14:44:59 +03003349 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
3350 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
3351 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
3352 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
3353 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
3354 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003355#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003356 "mov %%r8, %c[r8](%[svm]) \n\t"
3357 "mov %%r9, %c[r9](%[svm]) \n\t"
3358 "mov %%r10, %c[r10](%[svm]) \n\t"
3359 "mov %%r11, %c[r11](%[svm]) \n\t"
3360 "mov %%r12, %c[r12](%[svm]) \n\t"
3361 "mov %%r13, %c[r13](%[svm]) \n\t"
3362 "mov %%r14, %c[r14](%[svm]) \n\t"
3363 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003364#endif
Avi Kivity80e31d42008-07-14 14:44:59 +03003365 "pop %%"R"bp"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003366 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003367 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003368 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003369 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
3370 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
3371 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
3372 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
3373 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
3374 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003375#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003376 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
3377 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
3378 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
3379 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
3380 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
3381 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
3382 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
3383 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003384#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02003385 : "cc", "memory"
Avi Kivity80e31d42008-07-14 14:44:59 +03003386 , R"bx", R"cx", R"dx", R"si", R"di"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003387#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02003388 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
3389#endif
3390 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003391
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003392 vcpu->arch.cr2 = svm->vmcb->save.cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003393 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3394 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3395 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003396
Avi Kivity6aa8b732006-12-10 02:21:36 -08003397 load_host_msrs(vcpu);
Avi Kivity9581d442010-10-19 16:46:55 +02003398 loadsegment(fs, fs_selector);
3399#ifdef CONFIG_X86_64
3400 load_gs_index(gs_selector);
3401 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs);
3402#else
3403 loadsegment(gs, gs_selector);
3404#endif
3405 kvm_load_ldt(ldt_selector);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003406
3407 reload_tss(vcpu);
3408
Avi Kivity56ba47d2007-11-07 17:14:18 +02003409 local_irq_disable();
3410
3411 stgi();
3412
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003413 sync_cr8_to_lapic(vcpu);
3414
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003415 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003416
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003417 if (npt_enabled) {
3418 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3419 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3420 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02003421
3422 /*
3423 * We need to handle MC intercepts here before the vcpu has a chance to
3424 * change the physical cpu
3425 */
3426 if (unlikely(svm->vmcb->control.exit_code ==
3427 SVM_EXIT_EXCP_BASE + MC_VECTOR))
3428 svm_handle_mce(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003429}
3430
Avi Kivity80e31d42008-07-14 14:44:59 +03003431#undef R
3432
Avi Kivity6aa8b732006-12-10 02:21:36 -08003433static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3434{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003435 struct vcpu_svm *svm = to_svm(vcpu);
3436
3437 svm->vmcb->save.cr3 = root;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003438 force_new_asid(vcpu);
3439}
3440
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003441static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3442{
3443 struct vcpu_svm *svm = to_svm(vcpu);
3444
3445 svm->vmcb->control.nested_cr3 = root;
3446
3447 /* Also sync guest cr3 here in case we live migrate */
3448 svm->vmcb->save.cr3 = vcpu->arch.cr3;
3449
3450 force_new_asid(vcpu);
3451}
3452
Avi Kivity6aa8b732006-12-10 02:21:36 -08003453static int is_disabled(void)
3454{
Joerg Roedel6031a612007-06-22 12:29:50 +03003455 u64 vm_cr;
3456
3457 rdmsrl(MSR_VM_CR, vm_cr);
3458 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3459 return 1;
3460
Avi Kivity6aa8b732006-12-10 02:21:36 -08003461 return 0;
3462}
3463
Ingo Molnar102d8322007-02-19 14:37:47 +02003464static void
3465svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3466{
3467 /*
3468 * Patch in the VMMCALL instruction:
3469 */
3470 hypercall[0] = 0x0f;
3471 hypercall[1] = 0x01;
3472 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003473}
3474
Yang, Sheng002c7f72007-07-31 14:23:01 +03003475static void svm_check_processor_compat(void *rtn)
3476{
3477 *(int *)rtn = 0;
3478}
3479
Avi Kivity774ead32007-12-26 13:57:04 +02003480static bool svm_cpu_has_accelerated_tpr(void)
3481{
3482 return false;
3483}
3484
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003485static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003486{
3487 return 0;
3488}
3489
Sheng Yang0e851882009-12-18 16:48:46 +08003490static void svm_cpuid_update(struct kvm_vcpu *vcpu)
3491{
3492}
3493
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003494static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
3495{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003496 switch (func) {
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02003497 case 0x80000001:
3498 if (nested)
3499 entry->ecx |= (1 << 2); /* Set SVM bit */
3500 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003501 case 0x8000000A:
3502 entry->eax = 1; /* SVM revision 1 */
3503 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
3504 ASID emulation to nested SVM */
3505 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02003506 entry->edx = 0; /* Per default do not support any
3507 additional features */
3508
3509 /* Support next_rip if host supports it */
3510 if (svm_has(SVM_FEATURE_NRIP))
3511 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003512
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02003513 /* Support NPT for the guest if enabled */
3514 if (npt_enabled)
3515 entry->edx |= SVM_FEATURE_NPT;
3516
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003517 break;
3518 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003519}
3520
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003521static const struct trace_print_flags svm_exit_reasons_str[] = {
Joerg Roedele0231712010-02-24 18:59:10 +01003522 { SVM_EXIT_READ_CR0, "read_cr0" },
3523 { SVM_EXIT_READ_CR3, "read_cr3" },
3524 { SVM_EXIT_READ_CR4, "read_cr4" },
3525 { SVM_EXIT_READ_CR8, "read_cr8" },
3526 { SVM_EXIT_WRITE_CR0, "write_cr0" },
3527 { SVM_EXIT_WRITE_CR3, "write_cr3" },
3528 { SVM_EXIT_WRITE_CR4, "write_cr4" },
3529 { SVM_EXIT_WRITE_CR8, "write_cr8" },
3530 { SVM_EXIT_READ_DR0, "read_dr0" },
3531 { SVM_EXIT_READ_DR1, "read_dr1" },
3532 { SVM_EXIT_READ_DR2, "read_dr2" },
3533 { SVM_EXIT_READ_DR3, "read_dr3" },
3534 { SVM_EXIT_WRITE_DR0, "write_dr0" },
3535 { SVM_EXIT_WRITE_DR1, "write_dr1" },
3536 { SVM_EXIT_WRITE_DR2, "write_dr2" },
3537 { SVM_EXIT_WRITE_DR3, "write_dr3" },
3538 { SVM_EXIT_WRITE_DR5, "write_dr5" },
3539 { SVM_EXIT_WRITE_DR7, "write_dr7" },
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003540 { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
3541 { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
3542 { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
3543 { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
3544 { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
3545 { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
3546 { SVM_EXIT_INTR, "interrupt" },
3547 { SVM_EXIT_NMI, "nmi" },
3548 { SVM_EXIT_SMI, "smi" },
3549 { SVM_EXIT_INIT, "init" },
3550 { SVM_EXIT_VINTR, "vintr" },
3551 { SVM_EXIT_CPUID, "cpuid" },
3552 { SVM_EXIT_INVD, "invd" },
3553 { SVM_EXIT_HLT, "hlt" },
3554 { SVM_EXIT_INVLPG, "invlpg" },
3555 { SVM_EXIT_INVLPGA, "invlpga" },
3556 { SVM_EXIT_IOIO, "io" },
3557 { SVM_EXIT_MSR, "msr" },
3558 { SVM_EXIT_TASK_SWITCH, "task_switch" },
3559 { SVM_EXIT_SHUTDOWN, "shutdown" },
3560 { SVM_EXIT_VMRUN, "vmrun" },
3561 { SVM_EXIT_VMMCALL, "hypercall" },
3562 { SVM_EXIT_VMLOAD, "vmload" },
3563 { SVM_EXIT_VMSAVE, "vmsave" },
3564 { SVM_EXIT_STGI, "stgi" },
3565 { SVM_EXIT_CLGI, "clgi" },
3566 { SVM_EXIT_SKINIT, "skinit" },
3567 { SVM_EXIT_WBINVD, "wbinvd" },
3568 { SVM_EXIT_MONITOR, "monitor" },
3569 { SVM_EXIT_MWAIT, "mwait" },
3570 { SVM_EXIT_NPF, "npf" },
3571 { -1, NULL }
3572};
3573
Sheng Yang17cc3932010-01-05 19:02:27 +08003574static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02003575{
Sheng Yang17cc3932010-01-05 19:02:27 +08003576 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02003577}
3578
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003579static bool svm_rdtscp_supported(void)
3580{
3581 return false;
3582}
3583
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003584static bool svm_has_wbinvd_exit(void)
3585{
3586 return true;
3587}
3588
Avi Kivity02daab22009-12-30 12:40:26 +02003589static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
3590{
3591 struct vcpu_svm *svm = to_svm(vcpu);
3592
Avi Kivity02daab22009-12-30 12:40:26 +02003593 svm->vmcb->control.intercept_exceptions |= 1 << NM_VECTOR;
Joerg Roedel66a562f2010-02-19 16:23:08 +01003594 if (is_nested(svm))
3595 svm->nested.hsave->control.intercept_exceptions |= 1 << NM_VECTOR;
3596 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02003597}
3598
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003599static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003600 .cpu_has_kvm_support = has_svm,
3601 .disabled_by_bios = is_disabled,
3602 .hardware_setup = svm_hardware_setup,
3603 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003604 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003605 .hardware_enable = svm_hardware_enable,
3606 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003607 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003608
3609 .vcpu_create = svm_create_vcpu,
3610 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003611 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003612
Avi Kivity04d2cc72007-09-10 18:10:54 +03003613 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003614 .vcpu_load = svm_vcpu_load,
3615 .vcpu_put = svm_vcpu_put,
3616
3617 .set_guest_debug = svm_guest_debug,
3618 .get_msr = svm_get_msr,
3619 .set_msr = svm_set_msr,
3620 .get_segment_base = svm_get_segment_base,
3621 .get_segment = svm_get_segment,
3622 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003623 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10003624 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02003625 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003626 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003627 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003628 .set_cr3 = svm_set_cr3,
3629 .set_cr4 = svm_set_cr4,
3630 .set_efer = svm_set_efer,
3631 .get_idt = svm_get_idt,
3632 .set_idt = svm_set_idt,
3633 .get_gdt = svm_get_gdt,
3634 .set_gdt = svm_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03003635 .set_dr7 = svm_set_dr7,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003636 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003637 .get_rflags = svm_get_rflags,
3638 .set_rflags = svm_set_rflags,
Avi Kivity6b52d182010-01-21 15:31:47 +02003639 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02003640 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003641
Avi Kivity6aa8b732006-12-10 02:21:36 -08003642 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003643
Avi Kivity6aa8b732006-12-10 02:21:36 -08003644 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003645 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003646 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04003647 .set_interrupt_shadow = svm_set_interrupt_shadow,
3648 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02003649 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003650 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003651 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02003652 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03003653 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02003654 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003655 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003656 .get_nmi_mask = svm_get_nmi_mask,
3657 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003658 .enable_nmi_window = enable_nmi_window,
3659 .enable_irq_window = enable_irq_window,
3660 .update_cr8_intercept = update_cr8_intercept,
Izik Eiduscbc94022007-10-25 00:29:55 +02003661
3662 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003663 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003664 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003665
3666 .exit_reasons_str = svm_exit_reasons_str,
Sheng Yang17cc3932010-01-05 19:02:27 +08003667 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08003668
3669 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003670
3671 .rdtscp_supported = svm_rdtscp_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003672
3673 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003674
3675 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10003676
3677 .write_tsc_offset = svm_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10003678 .adjust_tsc_offset = svm_adjust_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003679
3680 .set_tdp_cr3 = set_tdp_cr3,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003681};
3682
3683static int __init svm_init(void)
3684{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003685 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03003686 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003687}
3688
3689static void __exit svm_exit(void)
3690{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003691 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003692}
3693
3694module_init(svm_init)
3695module_exit(svm_exit)