blob: 9a9a4405b571cd78105c6fb3c50e84fed0943f73 [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
297static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
298{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000299 vcpu->arch.efer = efer;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100300 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600301 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800302
Alexander Graf9962d032008-11-25 20:17:02 +0100303 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800304}
305
Avi Kivity6aa8b732006-12-10 02:21:36 -0800306static int is_external_interrupt(u32 info)
307{
308 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
309 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
310}
311
Glauber Costa2809f5d2009-05-12 16:21:05 -0400312static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
313{
314 struct vcpu_svm *svm = to_svm(vcpu);
315 u32 ret = 0;
316
317 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Jan Kiszka48005f62010-02-19 19:38:07 +0100318 ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400319 return ret & mask;
320}
321
322static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
323{
324 struct vcpu_svm *svm = to_svm(vcpu);
325
326 if (mask == 0)
327 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
328 else
329 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
330
331}
332
Avi Kivity6aa8b732006-12-10 02:21:36 -0800333static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
334{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400335 struct vcpu_svm *svm = to_svm(vcpu);
336
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200337 if (svm->vmcb->control.next_rip != 0)
338 svm->next_rip = svm->vmcb->control.next_rip;
339
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400340 if (!svm->next_rip) {
Avi Kivity851ba692009-08-24 11:10:17 +0300341 if (emulate_instruction(vcpu, 0, 0, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300342 EMULATE_DONE)
343 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800344 return;
345 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300346 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
347 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
348 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800349
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300350 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400351 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800352}
353
Jan Kiszka116a4752010-02-23 17:47:54 +0100354static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200355 bool has_error_code, u32 error_code,
356 bool reinject)
Jan Kiszka116a4752010-02-23 17:47:54 +0100357{
358 struct vcpu_svm *svm = to_svm(vcpu);
359
Joerg Roedele0231712010-02-24 18:59:10 +0100360 /*
361 * If we are within a nested VM we'd better #VMEXIT and let the guest
362 * handle the exception
363 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200364 if (!reinject &&
365 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100366 return;
367
Jan Kiszka66b71382010-02-23 17:47:56 +0100368 if (nr == BP_VECTOR && !svm_has(SVM_FEATURE_NRIP)) {
369 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
370
371 /*
372 * For guest debugging where we have to reinject #BP if some
373 * INT3 is guest-owned:
374 * Emulate nRIP by moving RIP forward. Will fail if injection
375 * raises a fault that is not intercepted. Still better than
376 * failing in all cases.
377 */
378 skip_emulated_instruction(&svm->vcpu);
379 rip = kvm_rip_read(&svm->vcpu);
380 svm->int3_rip = rip + svm->vmcb->save.cs.base;
381 svm->int3_injected = rip - old_rip;
382 }
383
Jan Kiszka116a4752010-02-23 17:47:54 +0100384 svm->vmcb->control.event_inj = nr
385 | SVM_EVTINJ_VALID
386 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
387 | SVM_EVTINJ_TYPE_EXEPT;
388 svm->vmcb->control.event_inj_err = error_code;
389}
390
Joerg Roedel67ec6602010-05-17 14:43:35 +0200391static void svm_init_erratum_383(void)
392{
393 u32 low, high;
394 int err;
395 u64 val;
396
Hans Rosenfeld1be85a62010-07-28 19:09:32 +0200397 if (!cpu_has_amd_erratum(amd_erratum_383))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200398 return;
399
400 /* Use _safe variants to not break nested virtualization */
401 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
402 if (err)
403 return;
404
405 val |= (1ULL << 47);
406
407 low = lower_32_bits(val);
408 high = upper_32_bits(val);
409
410 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
411
412 erratum_383_found = true;
413}
414
Avi Kivity6aa8b732006-12-10 02:21:36 -0800415static int has_svm(void)
416{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200417 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800418
Eduardo Habkost63d11422008-11-17 19:03:20 -0200419 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800420 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800421 return 0;
422 }
423
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424 return 1;
425}
426
427static void svm_hardware_disable(void *garbage)
428{
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200429 cpu_svm_disable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800430}
431
Alexander Graf10474ae2009-09-15 11:37:46 +0200432static int svm_hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800433{
434
Tejun Heo0fe1e002009-10-29 22:34:14 +0900435 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800436 uint64_t efer;
Gleb Natapov89a27f42010-02-16 10:51:48 +0200437 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800438 struct desc_struct *gdt;
439 int me = raw_smp_processor_id();
440
Alexander Graf10474ae2009-09-15 11:37:46 +0200441 rdmsrl(MSR_EFER, efer);
442 if (efer & EFER_SVME)
443 return -EBUSY;
444
Avi Kivity6aa8b732006-12-10 02:21:36 -0800445 if (!has_svm()) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000446 printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
447 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200448 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800449 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900450 sd = per_cpu(svm_data, me);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800451
Tejun Heo0fe1e002009-10-29 22:34:14 +0900452 if (!sd) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000453 printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -0800454 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200455 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800456 }
457
Tejun Heo0fe1e002009-10-29 22:34:14 +0900458 sd->asid_generation = 1;
459 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
460 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800461
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200462 native_store_gdt(&gdt_descr);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200463 gdt = (struct desc_struct *)gdt_descr.address;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900464 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800465
Alexander Graf9962d032008-11-25 20:17:02 +0100466 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800467
Linus Torvaldsd0316552009-12-14 09:58:24 -0800468 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200469
Joerg Roedel67ec6602010-05-17 14:43:35 +0200470 svm_init_erratum_383();
471
Alexander Graf10474ae2009-09-15 11:37:46 +0200472 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800473}
474
Joerg Roedel0da1db752008-07-02 16:02:11 +0200475static void svm_cpu_uninit(int cpu)
476{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900477 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200478
Tejun Heo0fe1e002009-10-29 22:34:14 +0900479 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200480 return;
481
482 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900483 __free_page(sd->save_area);
484 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200485}
486
Avi Kivity6aa8b732006-12-10 02:21:36 -0800487static int svm_cpu_init(int cpu)
488{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900489 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800490 int r;
491
Tejun Heo0fe1e002009-10-29 22:34:14 +0900492 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
493 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800494 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900495 sd->cpu = cpu;
496 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800497 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900498 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800499 goto err_1;
500
Tejun Heo0fe1e002009-10-29 22:34:14 +0900501 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800502
503 return 0;
504
505err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900506 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800507 return r;
508
509}
510
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100511static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800512{
513 int i;
514
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100515 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
516 if (direct_access_msrs[i].index == index)
517 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800518
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100519 return false;
520}
521
Avi Kivity6aa8b732006-12-10 02:21:36 -0800522static void set_msr_interception(u32 *msrpm, unsigned msr,
523 int read, int write)
524{
Joerg Roedel455716f2010-03-01 15:34:35 +0100525 u8 bit_read, bit_write;
526 unsigned long tmp;
527 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800528
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100529 /*
530 * If this warning triggers extend the direct_access_msrs list at the
531 * beginning of the file
532 */
533 WARN_ON(!valid_msr_intercept(msr));
534
Joerg Roedel455716f2010-03-01 15:34:35 +0100535 offset = svm_msrpm_offset(msr);
536 bit_read = 2 * (msr & 0x0f);
537 bit_write = 2 * (msr & 0x0f) + 1;
538 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800539
Joerg Roedel455716f2010-03-01 15:34:35 +0100540 BUG_ON(offset == MSR_INVALID);
541
542 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
543 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
544
545 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800546}
547
Joerg Roedelf65c2292008-02-13 18:58:46 +0100548static void svm_vcpu_init_msrpm(u32 *msrpm)
549{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100550 int i;
551
Joerg Roedelf65c2292008-02-13 18:58:46 +0100552 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
553
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100554 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
555 if (!direct_access_msrs[i].always)
556 continue;
557
558 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
559 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100560}
561
Joerg Roedel323c3d82010-03-01 15:34:37 +0100562static void add_msr_offset(u32 offset)
563{
564 int i;
565
566 for (i = 0; i < MSRPM_OFFSETS; ++i) {
567
568 /* Offset already in list? */
569 if (msrpm_offsets[i] == offset)
570 return;
571
572 /* Slot used by another offset? */
573 if (msrpm_offsets[i] != MSR_INVALID)
574 continue;
575
576 /* Add offset to list */
577 msrpm_offsets[i] = offset;
578
579 return;
580 }
581
582 /*
583 * If this BUG triggers the msrpm_offsets table has an overflow. Just
584 * increase MSRPM_OFFSETS in this case.
585 */
586 BUG();
587}
588
589static void init_msrpm_offsets(void)
590{
591 int i;
592
593 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
594
595 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
596 u32 offset;
597
598 offset = svm_msrpm_offset(direct_access_msrs[i].index);
599 BUG_ON(offset == MSR_INVALID);
600
601 add_msr_offset(offset);
602 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800603}
604
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100605static void svm_enable_lbrv(struct vcpu_svm *svm)
606{
607 u32 *msrpm = svm->msrpm;
608
609 svm->vmcb->control.lbr_ctl = 1;
610 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
611 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
612 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
613 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
614}
615
616static void svm_disable_lbrv(struct vcpu_svm *svm)
617{
618 u32 *msrpm = svm->msrpm;
619
620 svm->vmcb->control.lbr_ctl = 0;
621 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
622 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
623 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
624 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
625}
626
Avi Kivity6aa8b732006-12-10 02:21:36 -0800627static __init int svm_hardware_setup(void)
628{
629 int cpu;
630 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100631 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800632 int r;
633
Avi Kivity6aa8b732006-12-10 02:21:36 -0800634 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
635
636 if (!iopm_pages)
637 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300638
639 iopm_va = page_address(iopm_pages);
640 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800641 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
642
Joerg Roedel323c3d82010-03-01 15:34:37 +0100643 init_msrpm_offsets();
644
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100645 if (boot_cpu_has(X86_FEATURE_NX))
646 kvm_enable_efer_bits(EFER_NX);
647
Alexander Graf1b2fd702009-02-02 16:23:51 +0100648 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
649 kvm_enable_efer_bits(EFER_FFXSR);
650
Alexander Graf236de052008-11-25 20:17:10 +0100651 if (nested) {
652 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +0200653 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +0100654 }
655
Zachary Amsden3230bb42009-09-29 11:38:37 -1000656 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800657 r = svm_cpu_init(cpu);
658 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100659 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800660 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100661
662 svm_features = cpuid_edx(SVM_CPUID_FUNC);
663
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100664 if (!svm_has(SVM_FEATURE_NPT))
665 npt_enabled = false;
666
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100667 if (npt_enabled && !npt) {
668 printk(KERN_INFO "kvm: Nested Paging disabled\n");
669 npt_enabled = false;
670 }
671
Joerg Roedel18552672008-02-07 13:47:41 +0100672 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100673 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100674 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200675 } else
676 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100677
Avi Kivity6aa8b732006-12-10 02:21:36 -0800678 return 0;
679
Joerg Roedelf65c2292008-02-13 18:58:46 +0100680err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800681 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
682 iopm_base = 0;
683 return r;
684}
685
686static __exit void svm_hardware_unsetup(void)
687{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200688 int cpu;
689
Zachary Amsden3230bb42009-09-29 11:38:37 -1000690 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200691 svm_cpu_uninit(cpu);
692
Avi Kivity6aa8b732006-12-10 02:21:36 -0800693 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100694 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800695}
696
697static void init_seg(struct vmcb_seg *seg)
698{
699 seg->selector = 0;
700 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +0100701 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800702 seg->limit = 0xffff;
703 seg->base = 0;
704}
705
706static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
707{
708 seg->selector = 0;
709 seg->attrib = SVM_SELECTOR_P_MASK | type;
710 seg->limit = 0xffff;
711 seg->base = 0;
712}
713
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -1000714static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
715{
716 struct vcpu_svm *svm = to_svm(vcpu);
717 u64 g_tsc_offset = 0;
718
719 if (is_nested(svm)) {
720 g_tsc_offset = svm->vmcb->control.tsc_offset -
721 svm->nested.hsave->control.tsc_offset;
722 svm->nested.hsave->control.tsc_offset = offset;
723 }
724
725 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
726}
727
Zachary Amsdene48672f2010-08-19 22:07:23 -1000728static void svm_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment)
729{
730 struct vcpu_svm *svm = to_svm(vcpu);
731
732 svm->vmcb->control.tsc_offset += adjustment;
733 if (is_nested(svm))
734 svm->nested.hsave->control.tsc_offset += adjustment;
735}
736
Joerg Roedele6101a92008-02-13 18:58:45 +0100737static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800738{
Joerg Roedele6101a92008-02-13 18:58:45 +0100739 struct vmcb_control_area *control = &svm->vmcb->control;
740 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800741
Avi Kivitybff78272010-01-07 13:16:08 +0200742 svm->vcpu.fpu_active = 1;
743
Joerg Roedele0231712010-02-24 18:59:10 +0100744 control->intercept_cr_read = INTERCEPT_CR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800745 INTERCEPT_CR3_MASK |
Joerg Roedel649d6862008-04-16 16:51:15 +0200746 INTERCEPT_CR4_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800747
Joerg Roedele0231712010-02-24 18:59:10 +0100748 control->intercept_cr_write = INTERCEPT_CR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800749 INTERCEPT_CR3_MASK |
Avi Kivity80a81192007-12-06 19:50:00 +0200750 INTERCEPT_CR4_MASK |
751 INTERCEPT_CR8_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800752
Joerg Roedele0231712010-02-24 18:59:10 +0100753 control->intercept_dr_read = INTERCEPT_DR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800754 INTERCEPT_DR1_MASK |
755 INTERCEPT_DR2_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100756 INTERCEPT_DR3_MASK |
757 INTERCEPT_DR4_MASK |
758 INTERCEPT_DR5_MASK |
759 INTERCEPT_DR6_MASK |
760 INTERCEPT_DR7_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800761
Joerg Roedele0231712010-02-24 18:59:10 +0100762 control->intercept_dr_write = INTERCEPT_DR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800763 INTERCEPT_DR1_MASK |
764 INTERCEPT_DR2_MASK |
765 INTERCEPT_DR3_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100766 INTERCEPT_DR4_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800767 INTERCEPT_DR5_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100768 INTERCEPT_DR6_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800769 INTERCEPT_DR7_MASK;
770
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500771 control->intercept_exceptions = (1 << PF_VECTOR) |
Joerg Roedel53371b52008-04-09 14:15:30 +0200772 (1 << UD_VECTOR) |
773 (1 << MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800774
775
Joerg Roedele0231712010-02-24 18:59:10 +0100776 control->intercept = (1ULL << INTERCEPT_INTR) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777 (1ULL << INTERCEPT_NMI) |
Joerg Roedel01525272007-02-19 14:37:47 +0200778 (1ULL << INTERCEPT_SMI) |
Avi Kivityd2251572010-01-06 10:55:27 +0200779 (1ULL << INTERCEPT_SELECTIVE_CR0) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800780 (1ULL << INTERCEPT_CPUID) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200781 (1ULL << INTERCEPT_INVD) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800782 (1ULL << INTERCEPT_HLT) |
Marcelo Tosattia7052892008-09-23 13:18:35 -0300783 (1ULL << INTERCEPT_INVLPG) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800784 (1ULL << INTERCEPT_INVLPGA) |
785 (1ULL << INTERCEPT_IOIO_PROT) |
786 (1ULL << INTERCEPT_MSR_PROT) |
787 (1ULL << INTERCEPT_TASK_SWITCH) |
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800788 (1ULL << INTERCEPT_SHUTDOWN) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789 (1ULL << INTERCEPT_VMRUN) |
790 (1ULL << INTERCEPT_VMMCALL) |
791 (1ULL << INTERCEPT_VMLOAD) |
792 (1ULL << INTERCEPT_VMSAVE) |
793 (1ULL << INTERCEPT_STGI) |
794 (1ULL << INTERCEPT_CLGI) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100795 (1ULL << INTERCEPT_SKINIT) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200796 (1ULL << INTERCEPT_WBINVD) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100797 (1ULL << INTERCEPT_MONITOR) |
798 (1ULL << INTERCEPT_MWAIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800799
800 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100801 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800802 control->int_ctl = V_INTR_MASKING_MASK;
803
804 init_seg(&save->es);
805 init_seg(&save->ss);
806 init_seg(&save->ds);
807 init_seg(&save->fs);
808 init_seg(&save->gs);
809
810 save->cs.selector = 0xf000;
811 /* Executable/Readable Code Segment */
812 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
813 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
814 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800815 /*
816 * cs.base should really be 0xffff0000, but vmx can't handle that, so
817 * be consistent with it.
818 *
819 * Replace when we have real mode working for vmx.
820 */
821 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800822
823 save->gdtr.limit = 0xffff;
824 save->idtr.limit = 0xffff;
825
826 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
827 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
828
Marcelo Tosattieaa48512010-08-31 19:13:14 -0300829 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -0400830 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800831 save->dr7 = 0x400;
832 save->rflags = 2;
833 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300834 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800835
Joerg Roedele0231712010-02-24 18:59:10 +0100836 /*
837 * This is the guest-visible cr0 value.
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200838 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839 */
Marcelo Tosatti678041a2010-08-31 19:13:13 -0300840 svm->vcpu.arch.cr0 = 0;
841 (void)kvm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200842
Rusty Russell66aee912007-07-17 23:34:16 +1000843 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800844 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100845
846 if (npt_enabled) {
847 /* Setup VMCB for Nested Paging */
848 control->nested_ctl = 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -0300849 control->intercept &= ~((1ULL << INTERCEPT_TASK_SWITCH) |
850 (1ULL << INTERCEPT_INVLPG));
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100851 control->intercept_exceptions &= ~(1 << PF_VECTOR);
Avi Kivity888f9f32010-01-10 12:14:04 +0200852 control->intercept_cr_read &= ~INTERCEPT_CR3_MASK;
853 control->intercept_cr_write &= ~INTERCEPT_CR3_MASK;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100854 save->g_pat = 0x0007040600070406ULL;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100855 save->cr3 = 0;
856 save->cr4 = 0;
857 }
Avi Kivitya79d2f12008-04-14 13:10:21 +0300858 force_new_asid(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +0100859
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200860 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +0200861 svm->vcpu.arch.hflags = 0;
862
Mark Langsdorf565d0992009-10-06 14:25:02 -0500863 if (svm_has(SVM_FEATURE_PAUSE_FILTER)) {
864 control->pause_filter_count = 3000;
865 control->intercept |= (1ULL << INTERCEPT_PAUSE);
866 }
867
Joerg Roedel2af91942009-08-07 11:49:28 +0200868 enable_gif(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800869}
870
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200871static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +0300872{
873 struct vcpu_svm *svm = to_svm(vcpu);
874
Joerg Roedele6101a92008-02-13 18:58:45 +0100875 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +0200876
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300877 if (!kvm_vcpu_is_bsp(vcpu)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300878 kvm_rip_write(vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800879 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
880 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +0200881 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300882 vcpu->arch.regs_avail = ~0;
883 vcpu->arch.regs_dirty = ~0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200884
885 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300886}
887
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000888static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800889{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400890 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100892 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +0100893 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100894 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000895 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800896
Rusty Russellc16f8622007-07-30 21:12:19 +1000897 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000898 if (!svm) {
899 err = -ENOMEM;
900 goto out;
901 }
902
903 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
904 if (err)
905 goto free_svm;
906
Joerg Roedelf65c2292008-02-13 18:58:46 +0100907 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900908 page = alloc_page(GFP_KERNEL);
909 if (!page)
910 goto uninit;
911
Joerg Roedelf65c2292008-02-13 18:58:46 +0100912 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
913 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900914 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100915
916 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
917 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900918 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100919
Alexander Grafb286d5d2008-11-25 20:17:05 +0100920 hsave_page = alloc_page(GFP_KERNEL);
921 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900922 goto free_page3;
923
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200924 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +0100925
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900926 svm->msrpm = page_address(msrpm_pages);
927 svm_vcpu_init_msrpm(svm->msrpm);
928
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200929 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +0100930 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +0100931
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400932 svm->vmcb = page_address(page);
933 clear_page(svm->vmcb);
934 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
935 svm->asid_generation = 0;
Joerg Roedele6101a92008-02-13 18:58:45 +0100936 init_vmcb(svm);
Zachary Amsden99e3e302010-08-19 22:07:17 -1000937 kvm_write_tsc(&svm->vcpu, 0);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400938
Jan Kiszka10ab25c2010-05-25 16:01:50 +0200939 err = fx_init(&svm->vcpu);
940 if (err)
941 goto free_page4;
942
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800943 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300944 if (kvm_vcpu_is_bsp(&svm->vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800945 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800946
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000947 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -0800948
Jan Kiszka10ab25c2010-05-25 16:01:50 +0200949free_page4:
950 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900951free_page3:
952 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
953free_page2:
954 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
955free_page1:
956 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000957uninit:
958 kvm_vcpu_uninit(&svm->vcpu);
959free_svm:
Rusty Russella4770342007-08-01 14:46:11 +1000960 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000961out:
962 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963}
964
965static void svm_free_vcpu(struct kvm_vcpu *vcpu)
966{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400967 struct vcpu_svm *svm = to_svm(vcpu);
968
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000969 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +0100970 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200971 __free_page(virt_to_page(svm->nested.hsave));
972 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000973 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +1000974 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800975}
976
Avi Kivity15ad7142007-07-11 18:17:21 +0300977static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800978{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400979 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300980 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +0200981
Avi Kivity0cc50642007-03-25 12:07:27 +0200982 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -0300983 svm->asid_generation = 0;
Avi Kivity0cc50642007-03-25 12:07:27 +0200984 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300985
986 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400987 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988}
989
990static void svm_vcpu_put(struct kvm_vcpu *vcpu)
991{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400992 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300993 int i;
994
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200995 ++vcpu->stat.host_state_reload;
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300996 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400997 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998}
999
Avi Kivity6aa8b732006-12-10 02:21:36 -08001000static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1001{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001002 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003}
1004
1005static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1006{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001007 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008}
1009
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001010static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1011{
1012 switch (reg) {
1013 case VCPU_EXREG_PDPTR:
1014 BUG_ON(!npt_enabled);
Joerg Roedelff03a072010-09-10 17:30:57 +02001015 load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3);
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001016 break;
1017 default:
1018 BUG();
1019 }
1020}
1021
Alexander Graff0b85052008-11-25 20:17:01 +01001022static void svm_set_vintr(struct vcpu_svm *svm)
1023{
1024 svm->vmcb->control.intercept |= 1ULL << INTERCEPT_VINTR;
1025}
1026
1027static void svm_clear_vintr(struct vcpu_svm *svm)
1028{
1029 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
1030}
1031
Avi Kivity6aa8b732006-12-10 02:21:36 -08001032static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1033{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001034 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035
1036 switch (seg) {
1037 case VCPU_SREG_CS: return &save->cs;
1038 case VCPU_SREG_DS: return &save->ds;
1039 case VCPU_SREG_ES: return &save->es;
1040 case VCPU_SREG_FS: return &save->fs;
1041 case VCPU_SREG_GS: return &save->gs;
1042 case VCPU_SREG_SS: return &save->ss;
1043 case VCPU_SREG_TR: return &save->tr;
1044 case VCPU_SREG_LDTR: return &save->ldtr;
1045 }
1046 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001047 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001048}
1049
1050static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1051{
1052 struct vmcb_seg *s = svm_seg(vcpu, seg);
1053
1054 return s->base;
1055}
1056
1057static void svm_get_segment(struct kvm_vcpu *vcpu,
1058 struct kvm_segment *var, int seg)
1059{
1060 struct vmcb_seg *s = svm_seg(vcpu, seg);
1061
1062 var->base = s->base;
1063 var->limit = s->limit;
1064 var->selector = s->selector;
1065 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1066 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1067 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1068 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1069 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1070 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1071 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
1072 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
Amit Shah25022ac2008-10-27 09:04:17 +00001073
Joerg Roedele0231712010-02-24 18:59:10 +01001074 /*
1075 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001076 * for cross vendor migration purposes by "not present"
1077 */
1078 var->unusable = !var->present || (var->type == 0);
1079
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001080 switch (seg) {
1081 case VCPU_SREG_CS:
1082 /*
1083 * SVM always stores 0 for the 'G' bit in the CS selector in
1084 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
1085 * Intel's VMENTRY has a check on the 'G' bit.
1086 */
Amit Shah25022ac2008-10-27 09:04:17 +00001087 var->g = s->limit > 0xfffff;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001088 break;
1089 case VCPU_SREG_TR:
1090 /*
1091 * Work around a bug where the busy flag in the tr selector
1092 * isn't exposed
1093 */
Amit Shahc0d09822008-10-27 09:04:18 +00001094 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001095 break;
1096 case VCPU_SREG_DS:
1097 case VCPU_SREG_ES:
1098 case VCPU_SREG_FS:
1099 case VCPU_SREG_GS:
1100 /*
1101 * The accessed bit must always be set in the segment
1102 * descriptor cache, although it can be cleared in the
1103 * descriptor, the cached bit always remains at 1. Since
1104 * Intel has a check on this, set it here to support
1105 * cross-vendor migration.
1106 */
1107 if (!var->unusable)
1108 var->type |= 0x1;
1109 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001110 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001111 /*
1112 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001113 * descriptor is left as 1, although the whole segment has
1114 * been made unusable. Clear it here to pass an Intel VMX
1115 * entry check when cross vendor migrating.
1116 */
1117 if (var->unusable)
1118 var->db = 0;
1119 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001120 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001121}
1122
Izik Eidus2e4d2652008-03-24 19:38:34 +02001123static int svm_get_cpl(struct kvm_vcpu *vcpu)
1124{
1125 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1126
1127 return save->cpl;
1128}
1129
Gleb Natapov89a27f42010-02-16 10:51:48 +02001130static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001131{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001132 struct vcpu_svm *svm = to_svm(vcpu);
1133
Gleb Natapov89a27f42010-02-16 10:51:48 +02001134 dt->size = svm->vmcb->save.idtr.limit;
1135 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001136}
1137
Gleb Natapov89a27f42010-02-16 10:51:48 +02001138static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001139{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001140 struct vcpu_svm *svm = to_svm(vcpu);
1141
Gleb Natapov89a27f42010-02-16 10:51:48 +02001142 svm->vmcb->save.idtr.limit = dt->size;
1143 svm->vmcb->save.idtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001144}
1145
Gleb Natapov89a27f42010-02-16 10:51:48 +02001146static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001147{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001148 struct vcpu_svm *svm = to_svm(vcpu);
1149
Gleb Natapov89a27f42010-02-16 10:51:48 +02001150 dt->size = svm->vmcb->save.gdtr.limit;
1151 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001152}
1153
Gleb Natapov89a27f42010-02-16 10:51:48 +02001154static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001155{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001156 struct vcpu_svm *svm = to_svm(vcpu);
1157
Gleb Natapov89a27f42010-02-16 10:51:48 +02001158 svm->vmcb->save.gdtr.limit = dt->size;
1159 svm->vmcb->save.gdtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001160}
1161
Avi Kivitye8467fd2009-12-29 18:43:06 +02001162static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1163{
1164}
1165
Anthony Liguori25c4c272007-04-27 09:29:21 +03001166static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001167{
1168}
1169
Avi Kivityd2251572010-01-06 10:55:27 +02001170static void update_cr0_intercept(struct vcpu_svm *svm)
1171{
Joerg Roedel66a562f2010-02-19 16:23:08 +01001172 struct vmcb *vmcb = svm->vmcb;
Avi Kivityd2251572010-01-06 10:55:27 +02001173 ulong gcr0 = svm->vcpu.arch.cr0;
1174 u64 *hcr0 = &svm->vmcb->save.cr0;
1175
1176 if (!svm->vcpu.fpu_active)
1177 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1178 else
1179 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1180 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1181
1182
1183 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Joerg Roedel66a562f2010-02-19 16:23:08 +01001184 vmcb->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1185 vmcb->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1186 if (is_nested(svm)) {
1187 struct vmcb *hsave = svm->nested.hsave;
1188
1189 hsave->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1190 hsave->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1191 vmcb->control.intercept_cr_read |= svm->nested.intercept_cr_read;
1192 vmcb->control.intercept_cr_write |= svm->nested.intercept_cr_write;
1193 }
Avi Kivityd2251572010-01-06 10:55:27 +02001194 } else {
1195 svm->vmcb->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1196 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001197 if (is_nested(svm)) {
1198 struct vmcb *hsave = svm->nested.hsave;
1199
1200 hsave->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1201 hsave->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
1202 }
Avi Kivityd2251572010-01-06 10:55:27 +02001203 }
1204}
1205
Avi Kivity6aa8b732006-12-10 02:21:36 -08001206static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1207{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001208 struct vcpu_svm *svm = to_svm(vcpu);
1209
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001210 if (is_nested(svm)) {
1211 /*
1212 * We are here because we run in nested mode, the host kvm
1213 * intercepts cr0 writes but the l1 hypervisor does not.
1214 * But the L1 hypervisor may intercept selective cr0 writes.
1215 * This needs to be checked here.
1216 */
1217 unsigned long old, new;
1218
1219 /* Remove bits that would trigger a real cr0 write intercept */
1220 old = vcpu->arch.cr0 & SVM_CR0_SELECTIVE_MASK;
1221 new = cr0 & SVM_CR0_SELECTIVE_MASK;
1222
1223 if (old == new) {
1224 /* cr0 write with ts and mp unchanged */
1225 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
Joerg Roedelcda00082010-09-02 17:29:46 +02001226 if (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE) {
1227 svm->nested.vmexit_rip = kvm_rip_read(vcpu);
1228 svm->nested.vmexit_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
1229 svm->nested.vmexit_rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001230 return;
Joerg Roedelcda00082010-09-02 17:29:46 +02001231 }
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001232 }
1233 }
1234
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001235#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001236 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001237 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001238 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001239 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001240 }
1241
Mike Dayd77c26f2007-10-08 09:02:08 -04001242 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001243 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001244 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001245 }
1246 }
1247#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001248 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001249
1250 if (!npt_enabled)
1251 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001252
1253 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001254 cr0 |= X86_CR0_TS;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001255 /*
1256 * re-enable caching here because the QEMU bios
1257 * does not do it - this results in some delay at
1258 * reboot
1259 */
1260 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001261 svm->vmcb->save.cr0 = cr0;
Avi Kivityd2251572010-01-06 10:55:27 +02001262 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001263}
1264
1265static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1266{
Joerg Roedel6394b642008-04-09 14:15:29 +02001267 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001268 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1269
1270 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
1271 force_new_asid(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001272
Joerg Roedelec077262008-04-09 14:15:28 +02001273 vcpu->arch.cr4 = cr4;
1274 if (!npt_enabled)
1275 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001276 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001277 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001278}
1279
1280static void svm_set_segment(struct kvm_vcpu *vcpu,
1281 struct kvm_segment *var, int seg)
1282{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001283 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001284 struct vmcb_seg *s = svm_seg(vcpu, seg);
1285
1286 s->base = var->base;
1287 s->limit = var->limit;
1288 s->selector = var->selector;
1289 if (var->unusable)
1290 s->attrib = 0;
1291 else {
1292 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1293 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1294 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1295 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1296 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1297 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1298 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1299 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1300 }
1301 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001302 svm->vmcb->save.cpl
1303 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -08001304 >> SVM_SELECTOR_DPL_SHIFT) & 3;
1305
1306}
1307
Gleb Natapov44c11432009-05-11 13:35:52 +03001308static void update_db_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001309{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001310 struct vcpu_svm *svm = to_svm(vcpu);
1311
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001312 svm->vmcb->control.intercept_exceptions &=
1313 ~((1 << DB_VECTOR) | (1 << BP_VECTOR));
Gleb Natapov44c11432009-05-11 13:35:52 +03001314
Jan Kiszka6be7d302009-10-18 13:24:54 +02001315 if (svm->nmi_singlestep)
Gleb Natapov44c11432009-05-11 13:35:52 +03001316 svm->vmcb->control.intercept_exceptions |= (1 << DB_VECTOR);
1317
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001318 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1319 if (vcpu->guest_debug &
1320 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
1321 svm->vmcb->control.intercept_exceptions |=
1322 1 << DB_VECTOR;
1323 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
1324 svm->vmcb->control.intercept_exceptions |=
1325 1 << BP_VECTOR;
1326 } else
1327 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001328}
1329
Jan Kiszka355be0b2009-10-03 00:31:21 +02001330static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Gleb Natapov44c11432009-05-11 13:35:52 +03001331{
Gleb Natapov44c11432009-05-11 13:35:52 +03001332 struct vcpu_svm *svm = to_svm(vcpu);
1333
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001334 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1335 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1336 else
1337 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1338
Jan Kiszka355be0b2009-10-03 00:31:21 +02001339 update_db_intercept(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001340}
1341
1342static void load_host_msrs(struct kvm_vcpu *vcpu)
1343{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001344#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001345 wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001346#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347}
1348
1349static void save_host_msrs(struct kvm_vcpu *vcpu)
1350{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001351#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001352 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001353#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001354}
1355
Tejun Heo0fe1e002009-10-29 22:34:14 +09001356static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001357{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001358 if (sd->next_asid > sd->max_asid) {
1359 ++sd->asid_generation;
1360 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001361 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001362 }
1363
Tejun Heo0fe1e002009-10-29 22:34:14 +09001364 svm->asid_generation = sd->asid_generation;
1365 svm->vmcb->control.asid = sd->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366}
1367
Gleb Natapov020df072010-04-13 10:05:23 +03001368static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001369{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001370 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001371
Gleb Natapov020df072010-04-13 10:05:23 +03001372 svm->vmcb->save.dr7 = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001373}
1374
Avi Kivity851ba692009-08-24 11:10:17 +03001375static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001376{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001377 u64 fault_address;
1378 u32 error_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001379
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001380 fault_address = svm->vmcb->control.exit_info_2;
1381 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001382
Marcelo Tosatti229456f2009-06-17 09:22:14 -03001383 trace_kvm_page_fault(fault_address, error_code);
Avi Kivity52c78472009-08-10 15:42:41 +03001384 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1385 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
Avi Kivity30677142007-10-28 18:48:59 +02001386 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001387}
1388
Avi Kivity851ba692009-08-24 11:10:17 +03001389static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001390{
Avi Kivity851ba692009-08-24 11:10:17 +03001391 struct kvm_run *kvm_run = svm->vcpu.run;
1392
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001393 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001394 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001395 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001396 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1397 return 1;
1398 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001399
Jan Kiszka6be7d302009-10-18 13:24:54 +02001400 if (svm->nmi_singlestep) {
1401 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001402 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1403 svm->vmcb->save.rflags &=
1404 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1405 update_db_intercept(&svm->vcpu);
1406 }
1407
1408 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001409 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001410 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1411 kvm_run->debug.arch.pc =
1412 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1413 kvm_run->debug.arch.exception = DB_VECTOR;
1414 return 0;
1415 }
1416
1417 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001418}
1419
Avi Kivity851ba692009-08-24 11:10:17 +03001420static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001421{
Avi Kivity851ba692009-08-24 11:10:17 +03001422 struct kvm_run *kvm_run = svm->vcpu.run;
1423
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001424 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1425 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1426 kvm_run->debug.arch.exception = BP_VECTOR;
1427 return 0;
1428}
1429
Avi Kivity851ba692009-08-24 11:10:17 +03001430static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001431{
1432 int er;
1433
Avi Kivity851ba692009-08-24 11:10:17 +03001434 er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001435 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001436 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001437 return 1;
1438}
1439
Avi Kivity6b52d182010-01-21 15:31:47 +02001440static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001441{
Avi Kivity6b52d182010-01-21 15:31:47 +02001442 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001443 u32 excp;
1444
1445 if (is_nested(svm)) {
1446 u32 h_excp, n_excp;
1447
1448 h_excp = svm->nested.hsave->control.intercept_exceptions;
1449 n_excp = svm->nested.intercept_exceptions;
1450 h_excp &= ~(1 << NM_VECTOR);
1451 excp = h_excp | n_excp;
1452 } else {
1453 excp = svm->vmcb->control.intercept_exceptions;
Joerg Roedele0231712010-02-24 18:59:10 +01001454 excp &= ~(1 << NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001455 }
1456
1457 svm->vmcb->control.intercept_exceptions = excp;
1458
Rusty Russelle756fc62007-07-30 20:07:08 +10001459 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001460 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001461}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001462
Avi Kivity6b52d182010-01-21 15:31:47 +02001463static int nm_interception(struct vcpu_svm *svm)
1464{
1465 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001466 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001467}
1468
Joerg Roedel67ec6602010-05-17 14:43:35 +02001469static bool is_erratum_383(void)
1470{
1471 int err, i;
1472 u64 value;
1473
1474 if (!erratum_383_found)
1475 return false;
1476
1477 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1478 if (err)
1479 return false;
1480
1481 /* Bit 62 may or may not be set for this mce */
1482 value &= ~(1ULL << 62);
1483
1484 if (value != 0xb600000000010015ULL)
1485 return false;
1486
1487 /* Clear MCi_STATUS registers */
1488 for (i = 0; i < 6; ++i)
1489 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1490
1491 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1492 if (!err) {
1493 u32 low, high;
1494
1495 value &= ~(1ULL << 2);
1496 low = lower_32_bits(value);
1497 high = upper_32_bits(value);
1498
1499 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1500 }
1501
1502 /* Flush tlb to evict multi-match entries */
1503 __flush_tlb_all();
1504
1505 return true;
1506}
1507
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001508static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001509{
Joerg Roedel67ec6602010-05-17 14:43:35 +02001510 if (is_erratum_383()) {
1511 /*
1512 * Erratum 383 triggered. Guest state is corrupt so kill the
1513 * guest.
1514 */
1515 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1516
Avi Kivitya8eeb042010-05-10 12:34:53 +03001517 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02001518
1519 return;
1520 }
1521
Joerg Roedel53371b52008-04-09 14:15:30 +02001522 /*
1523 * On an #MC intercept the MCE handler is not called automatically in
1524 * the host. So do it by hand here.
1525 */
1526 asm volatile (
1527 "int $0x12\n");
1528 /* not sure if we ever come back to this point */
1529
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001530 return;
1531}
1532
1533static int mc_interception(struct vcpu_svm *svm)
1534{
Joerg Roedel53371b52008-04-09 14:15:30 +02001535 return 1;
1536}
1537
Avi Kivity851ba692009-08-24 11:10:17 +03001538static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001539{
Avi Kivity851ba692009-08-24 11:10:17 +03001540 struct kvm_run *kvm_run = svm->vcpu.run;
1541
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001542 /*
1543 * VMCB is undefined after a SHUTDOWN intercept
1544 * so reinitialize it.
1545 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001546 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001547 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001548
1549 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1550 return 0;
1551}
1552
Avi Kivity851ba692009-08-24 11:10:17 +03001553static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001554{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001555 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04001556 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001557 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001558 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001559
Rusty Russelle756fc62007-07-30 20:07:08 +10001560 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03001561 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001562 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001563 if (string || in)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03001564 return emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001565
Avi Kivity039576c2007-03-20 12:46:50 +02001566 port = io_info >> 16;
1567 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001568 svm->next_rip = svm->vmcb->control.exit_info_2;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001569 skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001570
1571 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001572}
1573
Avi Kivity851ba692009-08-24 11:10:17 +03001574static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001575{
1576 return 1;
1577}
1578
Avi Kivity851ba692009-08-24 11:10:17 +03001579static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001580{
1581 ++svm->vcpu.stat.irq_exits;
1582 return 1;
1583}
1584
Avi Kivity851ba692009-08-24 11:10:17 +03001585static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001586{
1587 return 1;
1588}
1589
Avi Kivity851ba692009-08-24 11:10:17 +03001590static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001591{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001592 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001593 skip_emulated_instruction(&svm->vcpu);
1594 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001595}
1596
Avi Kivity851ba692009-08-24 11:10:17 +03001597static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001598{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001599 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001600 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001601 kvm_emulate_hypercall(&svm->vcpu);
1602 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001603}
1604
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001605static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
1606{
1607 struct vcpu_svm *svm = to_svm(vcpu);
1608
1609 return svm->nested.nested_cr3;
1610}
1611
1612static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
1613 unsigned long root)
1614{
1615 struct vcpu_svm *svm = to_svm(vcpu);
1616
1617 svm->vmcb->control.nested_cr3 = root;
1618 force_new_asid(vcpu);
1619}
1620
1621static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu)
1622{
1623 struct vcpu_svm *svm = to_svm(vcpu);
1624
1625 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
1626 svm->vmcb->control.exit_code_hi = 0;
1627 svm->vmcb->control.exit_info_1 = vcpu->arch.fault.error_code;
1628 svm->vmcb->control.exit_info_2 = vcpu->arch.fault.address;
1629
1630 nested_svm_vmexit(svm);
1631}
1632
Alexander Grafc0725422008-11-25 20:17:03 +01001633static int nested_svm_check_permissions(struct vcpu_svm *svm)
1634{
Avi Kivityf6801df2010-01-21 15:31:50 +02001635 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01001636 || !is_paging(&svm->vcpu)) {
1637 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1638 return 1;
1639 }
1640
1641 if (svm->vmcb->save.cpl) {
1642 kvm_inject_gp(&svm->vcpu, 0);
1643 return 1;
1644 }
1645
1646 return 0;
1647}
1648
Alexander Grafcf74a782008-11-25 20:17:08 +01001649static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
1650 bool has_error_code, u32 error_code)
1651{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001652 int vmexit;
1653
Joerg Roedel0295ad72009-08-07 11:49:37 +02001654 if (!is_nested(svm))
1655 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001656
Joerg Roedel0295ad72009-08-07 11:49:37 +02001657 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1658 svm->vmcb->control.exit_code_hi = 0;
1659 svm->vmcb->control.exit_info_1 = error_code;
1660 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1661
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001662 vmexit = nested_svm_intercept(svm);
1663 if (vmexit == NESTED_EXIT_DONE)
1664 svm->nested.exit_required = true;
1665
1666 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001667}
1668
Joerg Roedel8fe54652010-02-19 16:23:01 +01001669/* This function returns true if it is save to enable the irq window */
1670static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001671{
Joerg Roedel26666952009-08-07 11:49:46 +02001672 if (!is_nested(svm))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001673 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001674
Joerg Roedel26666952009-08-07 11:49:46 +02001675 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001676 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001677
Joerg Roedel26666952009-08-07 11:49:46 +02001678 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001679 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001680
Joerg Roedel197717d2010-02-24 18:59:19 +01001681 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
1682 svm->vmcb->control.exit_info_1 = 0;
1683 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02001684
Joerg Roedelcd3ff652009-10-09 16:08:26 +02001685 if (svm->nested.intercept & 1ULL) {
1686 /*
1687 * The #vmexit can't be emulated here directly because this
1688 * code path runs with irqs and preemtion disabled. A
1689 * #vmexit emulation might sleep. Only signal request for
1690 * the #vmexit here.
1691 */
1692 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02001693 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01001694 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001695 }
1696
Joerg Roedel8fe54652010-02-19 16:23:01 +01001697 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001698}
1699
Joerg Roedel887f5002010-02-24 18:59:12 +01001700/* This function returns true if it is save to enable the nmi window */
1701static inline bool nested_svm_nmi(struct vcpu_svm *svm)
1702{
1703 if (!is_nested(svm))
1704 return true;
1705
1706 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
1707 return true;
1708
1709 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
1710 svm->nested.exit_required = true;
1711
1712 return false;
1713}
1714
Joerg Roedel7597f122010-02-19 16:23:00 +01001715static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001716{
1717 struct page *page;
1718
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01001719 might_sleep();
1720
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001721 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001722 if (is_error_page(page))
1723 goto error;
1724
Joerg Roedel7597f122010-02-19 16:23:00 +01001725 *_page = page;
1726
1727 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001728
1729error:
1730 kvm_release_page_clean(page);
1731 kvm_inject_gp(&svm->vcpu, 0);
1732
1733 return NULL;
1734}
1735
Joerg Roedel7597f122010-02-19 16:23:00 +01001736static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001737{
Joerg Roedel7597f122010-02-19 16:23:00 +01001738 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001739 kvm_release_page_dirty(page);
1740}
1741
Joerg Roedelce2ac082010-03-01 15:34:39 +01001742static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001743{
Joerg Roedelce2ac082010-03-01 15:34:39 +01001744 unsigned port;
1745 u8 val, bit;
1746 u64 gpa;
1747
1748 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
1749 return NESTED_EXIT_HOST;
1750
1751 port = svm->vmcb->control.exit_info_1 >> 16;
1752 gpa = svm->nested.vmcb_iopm + (port / 8);
1753 bit = port % 8;
1754 val = 0;
1755
1756 if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1))
1757 val &= (1 << bit);
1758
1759 return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1760}
1761
Joerg Roedeld2477822010-03-01 15:34:34 +01001762static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001763{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001764 u32 offset, msr, value;
1765 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001766
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001767 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01001768 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001769
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001770 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1771 offset = svm_msrpm_offset(msr);
1772 write = svm->vmcb->control.exit_info_1 & 1;
1773 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001774
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001775 if (offset == MSR_INVALID)
1776 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001777
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001778 /* Offset is in 32 bit units but need in 8 bit units */
1779 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001780
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001781 if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4))
1782 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001783
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001784 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001785}
1786
Joerg Roedel410e4d52009-08-07 11:49:44 +02001787static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001788{
Alexander Grafcf74a782008-11-25 20:17:08 +01001789 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001790
Joerg Roedel410e4d52009-08-07 11:49:44 +02001791 switch (exit_code) {
1792 case SVM_EXIT_INTR:
1793 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02001794 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02001795 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001796 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01001797 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001798 if (npt_enabled)
1799 return NESTED_EXIT_HOST;
1800 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001801 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Joerg Roedele0231712010-02-24 18:59:10 +01001802 /* When we're shadowing, trap PFs */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001803 if (!npt_enabled)
1804 return NESTED_EXIT_HOST;
1805 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001806 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
1807 nm_interception(svm);
1808 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001809 default:
1810 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001811 }
1812
Joerg Roedel410e4d52009-08-07 11:49:44 +02001813 return NESTED_EXIT_CONTINUE;
1814}
1815
1816/*
1817 * If this function returns true, this #vmexit was already handled
1818 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001819static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001820{
1821 u32 exit_code = svm->vmcb->control.exit_code;
1822 int vmexit = NESTED_EXIT_HOST;
1823
Alexander Grafcf74a782008-11-25 20:17:08 +01001824 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001825 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001826 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001827 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01001828 case SVM_EXIT_IOIO:
1829 vmexit = nested_svm_intercept_ioio(svm);
1830 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001831 case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR8: {
1832 u32 cr_bits = 1 << (exit_code - SVM_EXIT_READ_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001833 if (svm->nested.intercept_cr_read & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001834 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001835 break;
1836 }
1837 case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR8: {
1838 u32 cr_bits = 1 << (exit_code - SVM_EXIT_WRITE_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001839 if (svm->nested.intercept_cr_write & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001840 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001841 break;
1842 }
1843 case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR7: {
1844 u32 dr_bits = 1 << (exit_code - SVM_EXIT_READ_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001845 if (svm->nested.intercept_dr_read & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001846 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001847 break;
1848 }
1849 case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR7: {
1850 u32 dr_bits = 1 << (exit_code - SVM_EXIT_WRITE_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001851 if (svm->nested.intercept_dr_write & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001852 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001853 break;
1854 }
1855 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1856 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001857 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001858 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001859 break;
1860 }
Joerg Roedel228070b2010-04-22 12:33:10 +02001861 case SVM_EXIT_ERR: {
1862 vmexit = NESTED_EXIT_DONE;
1863 break;
1864 }
Alexander Grafcf74a782008-11-25 20:17:08 +01001865 default: {
1866 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001867 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001868 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001869 }
1870 }
1871
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001872 return vmexit;
1873}
1874
1875static int nested_svm_exit_handled(struct vcpu_svm *svm)
1876{
1877 int vmexit;
1878
1879 vmexit = nested_svm_intercept(svm);
1880
1881 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001882 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001883
1884 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001885}
1886
Joerg Roedel0460a972009-08-07 11:49:31 +02001887static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
1888{
1889 struct vmcb_control_area *dst = &dst_vmcb->control;
1890 struct vmcb_control_area *from = &from_vmcb->control;
1891
1892 dst->intercept_cr_read = from->intercept_cr_read;
1893 dst->intercept_cr_write = from->intercept_cr_write;
1894 dst->intercept_dr_read = from->intercept_dr_read;
1895 dst->intercept_dr_write = from->intercept_dr_write;
1896 dst->intercept_exceptions = from->intercept_exceptions;
1897 dst->intercept = from->intercept;
1898 dst->iopm_base_pa = from->iopm_base_pa;
1899 dst->msrpm_base_pa = from->msrpm_base_pa;
1900 dst->tsc_offset = from->tsc_offset;
1901 dst->asid = from->asid;
1902 dst->tlb_ctl = from->tlb_ctl;
1903 dst->int_ctl = from->int_ctl;
1904 dst->int_vector = from->int_vector;
1905 dst->int_state = from->int_state;
1906 dst->exit_code = from->exit_code;
1907 dst->exit_code_hi = from->exit_code_hi;
1908 dst->exit_info_1 = from->exit_info_1;
1909 dst->exit_info_2 = from->exit_info_2;
1910 dst->exit_int_info = from->exit_int_info;
1911 dst->exit_int_info_err = from->exit_int_info_err;
1912 dst->nested_ctl = from->nested_ctl;
1913 dst->event_inj = from->event_inj;
1914 dst->event_inj_err = from->event_inj_err;
1915 dst->nested_cr3 = from->nested_cr3;
1916 dst->lbr_ctl = from->lbr_ctl;
1917}
1918
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001919static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001920{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001921 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001922 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02001923 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01001924 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01001925
Joerg Roedel17897f32009-10-09 16:08:29 +02001926 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
1927 vmcb->control.exit_info_1,
1928 vmcb->control.exit_info_2,
1929 vmcb->control.exit_int_info,
1930 vmcb->control.exit_int_info_err);
1931
Joerg Roedel7597f122010-02-19 16:23:00 +01001932 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001933 if (!nested_vmcb)
1934 return 1;
1935
Joerg Roedel06fc77722010-02-19 16:23:07 +01001936 /* Exit nested SVM mode */
1937 svm->nested.vmcb = 0;
1938
Alexander Grafcf74a782008-11-25 20:17:08 +01001939 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02001940 disable_gif(svm);
1941
1942 nested_vmcb->save.es = vmcb->save.es;
1943 nested_vmcb->save.cs = vmcb->save.cs;
1944 nested_vmcb->save.ss = vmcb->save.ss;
1945 nested_vmcb->save.ds = vmcb->save.ds;
1946 nested_vmcb->save.gdtr = vmcb->save.gdtr;
1947 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02001948 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001949 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedel2be4fc72010-04-22 12:33:09 +02001950 nested_vmcb->save.cr3 = svm->vcpu.arch.cr3;
Joerg Roedel33740e42009-08-07 11:49:29 +02001951 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001952 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Joerg Roedel33740e42009-08-07 11:49:29 +02001953 nested_vmcb->save.rflags = vmcb->save.rflags;
1954 nested_vmcb->save.rip = vmcb->save.rip;
1955 nested_vmcb->save.rsp = vmcb->save.rsp;
1956 nested_vmcb->save.rax = vmcb->save.rax;
1957 nested_vmcb->save.dr7 = vmcb->save.dr7;
1958 nested_vmcb->save.dr6 = vmcb->save.dr6;
1959 nested_vmcb->save.cpl = vmcb->save.cpl;
1960
1961 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
1962 nested_vmcb->control.int_vector = vmcb->control.int_vector;
1963 nested_vmcb->control.int_state = vmcb->control.int_state;
1964 nested_vmcb->control.exit_code = vmcb->control.exit_code;
1965 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
1966 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
1967 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
1968 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
1969 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel7a190662010-07-27 18:14:21 +02001970 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02001971
1972 /*
1973 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
1974 * to make sure that we do not lose injected events. So check event_inj
1975 * here and copy it to exit_int_info if it is valid.
1976 * Exit_int_info and event_inj can't be both valid because the case
1977 * below only happens on a VMRUN instruction intercept which has
1978 * no valid exit_int_info set.
1979 */
1980 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
1981 struct vmcb_control_area *nc = &nested_vmcb->control;
1982
1983 nc->exit_int_info = vmcb->control.event_inj;
1984 nc->exit_int_info_err = vmcb->control.event_inj_err;
1985 }
1986
Joerg Roedel33740e42009-08-07 11:49:29 +02001987 nested_vmcb->control.tlb_ctl = 0;
1988 nested_vmcb->control.event_inj = 0;
1989 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001990
1991 /* We always set V_INTR_MASKING and remember the old value in hflags */
1992 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
1993 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
1994
Alexander Grafcf74a782008-11-25 20:17:08 +01001995 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02001996 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01001997
Alexander Graf219b65d2009-06-15 15:21:25 +02001998 kvm_clear_exception_queue(&svm->vcpu);
1999 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002000
2001 /* Restore selected save entries */
2002 svm->vmcb->save.es = hsave->save.es;
2003 svm->vmcb->save.cs = hsave->save.cs;
2004 svm->vmcb->save.ss = hsave->save.ss;
2005 svm->vmcb->save.ds = hsave->save.ds;
2006 svm->vmcb->save.gdtr = hsave->save.gdtr;
2007 svm->vmcb->save.idtr = hsave->save.idtr;
2008 svm->vmcb->save.rflags = hsave->save.rflags;
2009 svm_set_efer(&svm->vcpu, hsave->save.efer);
2010 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
2011 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
2012 if (npt_enabled) {
2013 svm->vmcb->save.cr3 = hsave->save.cr3;
2014 svm->vcpu.arch.cr3 = hsave->save.cr3;
2015 } else {
Avi Kivity23902182010-06-10 17:02:16 +03002016 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01002017 }
2018 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
2019 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
2020 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
2021 svm->vmcb->save.dr7 = 0;
2022 svm->vmcb->save.cpl = 0;
2023 svm->vmcb->control.exit_int_info = 0;
2024
Joerg Roedel7597f122010-02-19 16:23:00 +01002025 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01002026
2027 kvm_mmu_reset_context(&svm->vcpu);
2028 kvm_mmu_load(&svm->vcpu);
2029
2030 return 0;
2031}
Alexander Graf3d6368e2008-11-25 20:17:07 +01002032
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002033static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002034{
Joerg Roedel323c3d82010-03-01 15:34:37 +01002035 /*
2036 * This function merges the msr permission bitmaps of kvm and the
2037 * nested vmcb. It is omptimized in that it only merges the parts where
2038 * the kvm msr permission bitmap may contain zero bits
2039 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01002040 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002041
Joerg Roedel323c3d82010-03-01 15:34:37 +01002042 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
2043 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002044
Joerg Roedel323c3d82010-03-01 15:34:37 +01002045 for (i = 0; i < MSRPM_OFFSETS; i++) {
2046 u32 value, p;
2047 u64 offset;
2048
2049 if (msrpm_offsets[i] == 0xffffffff)
2050 break;
2051
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002052 p = msrpm_offsets[i];
2053 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01002054
2055 if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4))
2056 return false;
2057
2058 svm->nested.msrpm[p] = svm->msrpm[p] | value;
2059 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002060
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002061 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002062
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002063 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002064}
2065
Joerg Roedel52c65a302010-08-02 16:46:44 +02002066static bool nested_vmcb_checks(struct vmcb *vmcb)
2067{
2068 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
2069 return false;
2070
Joerg Roedeldbe77582010-08-02 16:46:45 +02002071 if (vmcb->control.asid == 0)
2072 return false;
2073
Joerg Roedel52c65a302010-08-02 16:46:44 +02002074 return true;
2075}
2076
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002077static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002078{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002079 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002080 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02002081 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002082 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01002083 u64 vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002084
Joerg Roedel06fc77722010-02-19 16:23:07 +01002085 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002086
Joerg Roedel7597f122010-02-19 16:23:00 +01002087 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002088 if (!nested_vmcb)
2089 return false;
2090
Joerg Roedel52c65a302010-08-02 16:46:44 +02002091 if (!nested_vmcb_checks(nested_vmcb)) {
2092 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
2093 nested_vmcb->control.exit_code_hi = 0;
2094 nested_vmcb->control.exit_info_1 = 0;
2095 nested_vmcb->control.exit_info_2 = 0;
2096
2097 nested_svm_unmap(page);
2098
2099 return false;
2100 }
2101
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002102 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02002103 nested_vmcb->save.rip,
2104 nested_vmcb->control.int_ctl,
2105 nested_vmcb->control.event_inj,
2106 nested_vmcb->control.nested_ctl);
2107
Joerg Roedel2e554e82010-02-24 18:59:14 +01002108 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr_read,
2109 nested_vmcb->control.intercept_cr_write,
2110 nested_vmcb->control.intercept_exceptions,
2111 nested_vmcb->control.intercept);
2112
Alexander Graf3d6368e2008-11-25 20:17:07 +01002113 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02002114 kvm_clear_exception_queue(&svm->vcpu);
2115 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002116
Joerg Roedele0231712010-02-24 18:59:10 +01002117 /*
2118 * Save the old vmcb, so we don't need to pick what we save, but can
2119 * restore everything when a VMEXIT occurs
2120 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02002121 hsave->save.es = vmcb->save.es;
2122 hsave->save.cs = vmcb->save.cs;
2123 hsave->save.ss = vmcb->save.ss;
2124 hsave->save.ds = vmcb->save.ds;
2125 hsave->save.gdtr = vmcb->save.gdtr;
2126 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02002127 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002128 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002129 hsave->save.cr4 = svm->vcpu.arch.cr4;
2130 hsave->save.rflags = vmcb->save.rflags;
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002131 hsave->save.rip = kvm_rip_read(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002132 hsave->save.rsp = vmcb->save.rsp;
2133 hsave->save.rax = vmcb->save.rax;
2134 if (npt_enabled)
2135 hsave->save.cr3 = vmcb->save.cr3;
2136 else
2137 hsave->save.cr3 = svm->vcpu.arch.cr3;
2138
Joerg Roedel0460a972009-08-07 11:49:31 +02002139 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002140
2141 if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
2142 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2143 else
2144 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2145
2146 /* Load the nested guest state */
2147 svm->vmcb->save.es = nested_vmcb->save.es;
2148 svm->vmcb->save.cs = nested_vmcb->save.cs;
2149 svm->vmcb->save.ss = nested_vmcb->save.ss;
2150 svm->vmcb->save.ds = nested_vmcb->save.ds;
2151 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2152 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
2153 svm->vmcb->save.rflags = nested_vmcb->save.rflags;
2154 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2155 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2156 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2157 if (npt_enabled) {
2158 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2159 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002160 } else
Avi Kivity23902182010-06-10 17:02:16 +03002161 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002162
2163 /* Guest paging mode is active - reset mmu */
2164 kvm_mmu_reset_context(&svm->vcpu);
2165
Joerg Roedeldefbba52009-08-07 11:49:30 +02002166 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002167 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2168 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2169 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01002170
Alexander Graf3d6368e2008-11-25 20:17:07 +01002171 /* In case we don't even reach vcpu_run, the fields are not updated */
2172 svm->vmcb->save.rax = nested_vmcb->save.rax;
2173 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2174 svm->vmcb->save.rip = nested_vmcb->save.rip;
2175 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2176 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2177 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2178
Joerg Roedelf7138532010-03-01 15:34:40 +01002179 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002180 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002181
Joerg Roedelaad42c62009-08-07 11:49:34 +02002182 /* cache intercepts */
2183 svm->nested.intercept_cr_read = nested_vmcb->control.intercept_cr_read;
2184 svm->nested.intercept_cr_write = nested_vmcb->control.intercept_cr_write;
2185 svm->nested.intercept_dr_read = nested_vmcb->control.intercept_dr_read;
2186 svm->nested.intercept_dr_write = nested_vmcb->control.intercept_dr_write;
2187 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2188 svm->nested.intercept = nested_vmcb->control.intercept;
2189
Alexander Graf3d6368e2008-11-25 20:17:07 +01002190 force_new_asid(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002191 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002192 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2193 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2194 else
2195 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2196
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002197 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2198 /* We only want the cr8 intercept bits of the guest */
2199 svm->vmcb->control.intercept_cr_read &= ~INTERCEPT_CR8_MASK;
2200 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
2201 }
2202
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002203 /* We don't want to see VMMCALLs from a nested guest */
2204 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VMMCALL);
2205
Joerg Roedele0231712010-02-24 18:59:10 +01002206 /*
2207 * We don't want a nested guest to be more powerful than the guest, so
2208 * all intercepts are ORed
2209 */
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002210 svm->vmcb->control.intercept_cr_read |=
2211 nested_vmcb->control.intercept_cr_read;
2212 svm->vmcb->control.intercept_cr_write |=
2213 nested_vmcb->control.intercept_cr_write;
2214 svm->vmcb->control.intercept_dr_read |=
2215 nested_vmcb->control.intercept_dr_read;
2216 svm->vmcb->control.intercept_dr_write |=
2217 nested_vmcb->control.intercept_dr_write;
2218 svm->vmcb->control.intercept_exceptions |=
2219 nested_vmcb->control.intercept_exceptions;
2220
2221 svm->vmcb->control.intercept |= nested_vmcb->control.intercept;
2222
2223 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002224 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2225 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2226 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002227 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2228 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2229
Joerg Roedel7597f122010-02-19 16:23:00 +01002230 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002231
Joerg Roedel06fc77722010-02-19 16:23:07 +01002232 /* nested_vmcb is our indicator if nested SVM is activated */
2233 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002234
Joerg Roedel2af91942009-08-07 11:49:28 +02002235 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002236
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002237 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002238}
2239
Joerg Roedel9966bf62009-08-07 11:49:40 +02002240static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002241{
2242 to_vmcb->save.fs = from_vmcb->save.fs;
2243 to_vmcb->save.gs = from_vmcb->save.gs;
2244 to_vmcb->save.tr = from_vmcb->save.tr;
2245 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2246 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2247 to_vmcb->save.star = from_vmcb->save.star;
2248 to_vmcb->save.lstar = from_vmcb->save.lstar;
2249 to_vmcb->save.cstar = from_vmcb->save.cstar;
2250 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2251 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2252 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2253 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002254}
2255
Avi Kivity851ba692009-08-24 11:10:17 +03002256static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002257{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002258 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002259 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002260
Alexander Graf55426752008-11-25 20:17:06 +01002261 if (nested_svm_check_permissions(svm))
2262 return 1;
2263
2264 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2265 skip_emulated_instruction(&svm->vcpu);
2266
Joerg Roedel7597f122010-02-19 16:23:00 +01002267 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002268 if (!nested_vmcb)
2269 return 1;
2270
2271 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002272 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002273
2274 return 1;
2275}
2276
Avi Kivity851ba692009-08-24 11:10:17 +03002277static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002278{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002279 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002280 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002281
Alexander Graf55426752008-11-25 20:17:06 +01002282 if (nested_svm_check_permissions(svm))
2283 return 1;
2284
2285 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2286 skip_emulated_instruction(&svm->vcpu);
2287
Joerg Roedel7597f122010-02-19 16:23:00 +01002288 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002289 if (!nested_vmcb)
2290 return 1;
2291
2292 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002293 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002294
2295 return 1;
2296}
2297
Avi Kivity851ba692009-08-24 11:10:17 +03002298static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002299{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002300 if (nested_svm_check_permissions(svm))
2301 return 1;
2302
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002303 /* Save rip after vmrun instruction */
2304 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002305
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002306 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002307 return 1;
2308
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002309 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002310 goto failed;
2311
2312 return 1;
2313
2314failed:
2315
2316 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2317 svm->vmcb->control.exit_code_hi = 0;
2318 svm->vmcb->control.exit_info_1 = 0;
2319 svm->vmcb->control.exit_info_2 = 0;
2320
2321 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002322
2323 return 1;
2324}
2325
Avi Kivity851ba692009-08-24 11:10:17 +03002326static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002327{
2328 if (nested_svm_check_permissions(svm))
2329 return 1;
2330
2331 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2332 skip_emulated_instruction(&svm->vcpu);
2333
Joerg Roedel2af91942009-08-07 11:49:28 +02002334 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002335
2336 return 1;
2337}
2338
Avi Kivity851ba692009-08-24 11:10:17 +03002339static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002340{
2341 if (nested_svm_check_permissions(svm))
2342 return 1;
2343
2344 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2345 skip_emulated_instruction(&svm->vcpu);
2346
Joerg Roedel2af91942009-08-07 11:49:28 +02002347 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002348
2349 /* After a CLGI no interrupts should come */
2350 svm_clear_vintr(svm);
2351 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2352
2353 return 1;
2354}
2355
Avi Kivity851ba692009-08-24 11:10:17 +03002356static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002357{
2358 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002359
Joerg Roedelec1ff792009-10-09 16:08:31 +02002360 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
2361 vcpu->arch.regs[VCPU_REGS_RAX]);
2362
Alexander Grafff092382009-06-15 15:21:24 +02002363 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2364 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
2365
2366 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2367 skip_emulated_instruction(&svm->vcpu);
2368 return 1;
2369}
2370
Joerg Roedel532a46b2009-10-09 16:08:32 +02002371static int skinit_interception(struct vcpu_svm *svm)
2372{
2373 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
2374
2375 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2376 return 1;
2377}
2378
Avi Kivity851ba692009-08-24 11:10:17 +03002379static int invalid_op_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002380{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002381 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002382 return 1;
2383}
2384
Avi Kivity851ba692009-08-24 11:10:17 +03002385static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002386{
Izik Eidus37817f22008-03-24 23:14:53 +02002387 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002388 int reason;
2389 int int_type = svm->vmcb->control.exit_int_info &
2390 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002391 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002392 uint32_t type =
2393 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2394 uint32_t idt_v =
2395 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002396 bool has_error_code = false;
2397 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002398
2399 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002400
Izik Eidus37817f22008-03-24 23:14:53 +02002401 if (svm->vmcb->control.exit_info_2 &
2402 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002403 reason = TASK_SWITCH_IRET;
2404 else if (svm->vmcb->control.exit_info_2 &
2405 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2406 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002407 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002408 reason = TASK_SWITCH_GATE;
2409 else
2410 reason = TASK_SWITCH_CALL;
2411
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002412 if (reason == TASK_SWITCH_GATE) {
2413 switch (type) {
2414 case SVM_EXITINTINFO_TYPE_NMI:
2415 svm->vcpu.arch.nmi_injected = false;
2416 break;
2417 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002418 if (svm->vmcb->control.exit_info_2 &
2419 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2420 has_error_code = true;
2421 error_code =
2422 (u32)svm->vmcb->control.exit_info_2;
2423 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002424 kvm_clear_exception_queue(&svm->vcpu);
2425 break;
2426 case SVM_EXITINTINFO_TYPE_INTR:
2427 kvm_clear_interrupt_queue(&svm->vcpu);
2428 break;
2429 default:
2430 break;
2431 }
2432 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002433
Gleb Natapov8317c292009-04-12 13:37:02 +03002434 if (reason != TASK_SWITCH_GATE ||
2435 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2436 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002437 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2438 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002439
Gleb Natapovacb54512010-04-15 21:03:50 +03002440 if (kvm_task_switch(&svm->vcpu, tss_selector, reason,
2441 has_error_code, error_code) == EMULATE_FAIL) {
2442 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2443 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2444 svm->vcpu.run->internal.ndata = 0;
2445 return 0;
2446 }
2447 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002448}
2449
Avi Kivity851ba692009-08-24 11:10:17 +03002450static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002451{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002452 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002453 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002454 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002455}
2456
Avi Kivity851ba692009-08-24 11:10:17 +03002457static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002458{
2459 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02002460 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002461 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002462 return 1;
2463}
2464
Avi Kivity851ba692009-08-24 11:10:17 +03002465static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002466{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002467 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002468}
2469
Avi Kivity851ba692009-08-24 11:10:17 +03002470static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002471{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002472 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002473}
2474
Joerg Roedelcda00082010-09-02 17:29:46 +02002475static int cr0_write_interception(struct vcpu_svm *svm)
2476{
2477 struct kvm_vcpu *vcpu = &svm->vcpu;
2478 int r;
2479
2480 r = emulate_instruction(&svm->vcpu, 0, 0, 0);
2481
2482 if (svm->nested.vmexit_rip) {
2483 kvm_register_write(vcpu, VCPU_REGS_RIP, svm->nested.vmexit_rip);
2484 kvm_register_write(vcpu, VCPU_REGS_RSP, svm->nested.vmexit_rsp);
2485 kvm_register_write(vcpu, VCPU_REGS_RAX, svm->nested.vmexit_rax);
2486 svm->nested.vmexit_rip = 0;
2487 }
2488
2489 return r == EMULATE_DONE;
2490}
2491
Avi Kivity851ba692009-08-24 11:10:17 +03002492static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01002493{
Avi Kivity851ba692009-08-24 11:10:17 +03002494 struct kvm_run *kvm_run = svm->vcpu.run;
2495
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002496 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
2497 /* instruction emulation calls kvm_set_cr8() */
Avi Kivity851ba692009-08-24 11:10:17 +03002498 emulate_instruction(&svm->vcpu, 0, 0, 0);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002499 if (irqchip_in_kernel(svm->vcpu.kvm)) {
2500 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
Joerg Roedel1d075432007-12-06 21:02:25 +01002501 return 1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002502 }
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002503 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
2504 return 1;
Joerg Roedel1d075432007-12-06 21:02:25 +01002505 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2506 return 0;
2507}
2508
Avi Kivity6aa8b732006-12-10 02:21:36 -08002509static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
2510{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002511 struct vcpu_svm *svm = to_svm(vcpu);
2512
Avi Kivity6aa8b732006-12-10 02:21:36 -08002513 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302514 case MSR_IA32_TSC: {
Joerg Roedel20824f32009-09-16 15:24:18 +02002515 u64 tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002516
Joerg Roedel20824f32009-09-16 15:24:18 +02002517 if (is_nested(svm))
2518 tsc_offset = svm->nested.hsave->control.tsc_offset;
2519 else
2520 tsc_offset = svm->vmcb->control.tsc_offset;
2521
2522 *data = tsc_offset + native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002523 break;
2524 }
Brian Gerst8c065852010-07-17 09:03:26 -04002525 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002526 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002527 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08002528#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002529 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002530 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002531 break;
2532 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002533 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002534 break;
2535 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002536 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002537 break;
2538 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002539 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002540 break;
2541#endif
2542 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002543 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002544 break;
2545 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002546 *data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002547 break;
2548 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002549 *data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002550 break;
Joerg Roedele0231712010-02-24 18:59:10 +01002551 /*
2552 * Nobody will change the following 5 values in the VMCB so we can
2553 * safely return them on rdmsr. They will always be 0 until LBRV is
2554 * implemented.
2555 */
Joerg Roedela2938c82008-02-13 16:30:28 +01002556 case MSR_IA32_DEBUGCTLMSR:
2557 *data = svm->vmcb->save.dbgctl;
2558 break;
2559 case MSR_IA32_LASTBRANCHFROMIP:
2560 *data = svm->vmcb->save.br_from;
2561 break;
2562 case MSR_IA32_LASTBRANCHTOIP:
2563 *data = svm->vmcb->save.br_to;
2564 break;
2565 case MSR_IA32_LASTINTFROMIP:
2566 *data = svm->vmcb->save.last_excp_from;
2567 break;
2568 case MSR_IA32_LASTINTTOIP:
2569 *data = svm->vmcb->save.last_excp_to;
2570 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002571 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002572 *data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002573 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002574 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002575 *data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002576 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01002577 case MSR_IA32_UCODE_REV:
2578 *data = 0x01000065;
2579 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002580 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002581 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002582 }
2583 return 0;
2584}
2585
Avi Kivity851ba692009-08-24 11:10:17 +03002586static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002587{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002588 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002589 u64 data;
2590
Avi Kivity59200272010-01-25 19:47:02 +02002591 if (svm_get_msr(&svm->vcpu, ecx, &data)) {
2592 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002593 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002594 } else {
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002595 trace_kvm_msr_read(ecx, data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002596
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002597 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002598 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002599 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002600 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002601 }
2602 return 1;
2603}
2604
Joerg Roedel4a810182010-02-24 18:59:15 +01002605static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2606{
2607 struct vcpu_svm *svm = to_svm(vcpu);
2608 int svm_dis, chg_mask;
2609
2610 if (data & ~SVM_VM_CR_VALID_MASK)
2611 return 1;
2612
2613 chg_mask = SVM_VM_CR_VALID_MASK;
2614
2615 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2616 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2617
2618 svm->nested.vm_cr_msr &= ~chg_mask;
2619 svm->nested.vm_cr_msr |= (data & chg_mask);
2620
2621 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2622
2623 /* check for svm_disable while efer.svme is set */
2624 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2625 return 1;
2626
2627 return 0;
2628}
2629
Avi Kivity6aa8b732006-12-10 02:21:36 -08002630static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
2631{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002632 struct vcpu_svm *svm = to_svm(vcpu);
2633
Avi Kivity6aa8b732006-12-10 02:21:36 -08002634 switch (ecx) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10002635 case MSR_IA32_TSC:
Zachary Amsden99e3e302010-08-19 22:07:17 -10002636 kvm_write_tsc(vcpu, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002637 break;
Brian Gerst8c065852010-07-17 09:03:26 -04002638 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002639 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002640 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08002641#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002642 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002643 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644 break;
2645 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002646 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002647 break;
2648 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002649 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002650 break;
2651 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002652 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002653 break;
2654#endif
2655 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002656 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002657 break;
2658 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002659 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002660 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002661 break;
2662 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002663 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002664 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002665 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002666 case MSR_IA32_DEBUGCTLMSR:
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002667 if (!svm_has(SVM_FEATURE_LBRV)) {
2668 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002669 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002670 break;
2671 }
2672 if (data & DEBUGCTL_RESERVED_BITS)
2673 return 1;
2674
2675 svm->vmcb->save.dbgctl = data;
2676 if (data & (1ULL<<0))
2677 svm_enable_lbrv(svm);
2678 else
2679 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01002680 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002681 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002682 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002683 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002684 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002685 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002686 case MSR_VM_IGNNE:
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002687 pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
2688 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002689 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002690 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002691 }
2692 return 0;
2693}
2694
Avi Kivity851ba692009-08-24 11:10:17 +03002695static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002696{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002697 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002698 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002699 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002700
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002701
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002702 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Avi Kivity59200272010-01-25 19:47:02 +02002703 if (svm_set_msr(&svm->vcpu, ecx, data)) {
2704 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002705 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002706 } else {
2707 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10002708 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02002709 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002710 return 1;
2711}
2712
Avi Kivity851ba692009-08-24 11:10:17 +03002713static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002714{
Rusty Russelle756fc62007-07-30 20:07:08 +10002715 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03002716 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002717 else
Avi Kivity851ba692009-08-24 11:10:17 +03002718 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002719}
2720
Avi Kivity851ba692009-08-24 11:10:17 +03002721static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08002722{
Avi Kivity851ba692009-08-24 11:10:17 +03002723 struct kvm_run *kvm_run = svm->vcpu.run;
2724
Alexander Graff0b85052008-11-25 20:17:01 +01002725 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03002726 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -08002727 /*
2728 * If the user space waits to inject interrupts, exit as soon as
2729 * possible
2730 */
Gleb Natapov80618232009-04-21 17:44:56 +03002731 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
2732 kvm_run->request_interrupt_window &&
2733 !kvm_cpu_has_interrupt(&svm->vcpu)) {
Rusty Russelle756fc62007-07-30 20:07:08 +10002734 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002735 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
2736 return 0;
2737 }
2738
2739 return 1;
2740}
2741
Mark Langsdorf565d0992009-10-06 14:25:02 -05002742static int pause_interception(struct vcpu_svm *svm)
2743{
2744 kvm_vcpu_on_spin(&(svm->vcpu));
2745 return 1;
2746}
2747
Avi Kivity851ba692009-08-24 11:10:17 +03002748static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
Joerg Roedele0231712010-02-24 18:59:10 +01002749 [SVM_EXIT_READ_CR0] = emulate_on_interception,
2750 [SVM_EXIT_READ_CR3] = emulate_on_interception,
2751 [SVM_EXIT_READ_CR4] = emulate_on_interception,
2752 [SVM_EXIT_READ_CR8] = emulate_on_interception,
Avi Kivityd2251572010-01-06 10:55:27 +02002753 [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
Joerg Roedelcda00082010-09-02 17:29:46 +02002754 [SVM_EXIT_WRITE_CR0] = cr0_write_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002755 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
2756 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
2757 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
2758 [SVM_EXIT_READ_DR0] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002759 [SVM_EXIT_READ_DR1] = emulate_on_interception,
2760 [SVM_EXIT_READ_DR2] = emulate_on_interception,
2761 [SVM_EXIT_READ_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002762 [SVM_EXIT_READ_DR4] = emulate_on_interception,
2763 [SVM_EXIT_READ_DR5] = emulate_on_interception,
2764 [SVM_EXIT_READ_DR6] = emulate_on_interception,
2765 [SVM_EXIT_READ_DR7] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002766 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
2767 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
2768 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
2769 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002770 [SVM_EXIT_WRITE_DR4] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002771 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002772 [SVM_EXIT_WRITE_DR6] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002773 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002774 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
2775 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002776 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002777 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
2778 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
2779 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
2780 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02002781 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002782 [SVM_EXIT_SMI] = nop_on_interception,
2783 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08002784 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002785 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002786 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002787 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05002788 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002789 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03002790 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02002791 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002792 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002793 [SVM_EXIT_MSR] = msr_interception,
2794 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002795 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01002796 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02002797 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01002798 [SVM_EXIT_VMLOAD] = vmload_interception,
2799 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01002800 [SVM_EXIT_STGI] = stgi_interception,
2801 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02002802 [SVM_EXIT_SKINIT] = skinit_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002803 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01002804 [SVM_EXIT_MONITOR] = invalid_op_interception,
2805 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002806 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002807};
2808
Joerg Roedel3f10c842010-05-05 16:04:42 +02002809void dump_vmcb(struct kvm_vcpu *vcpu)
2810{
2811 struct vcpu_svm *svm = to_svm(vcpu);
2812 struct vmcb_control_area *control = &svm->vmcb->control;
2813 struct vmcb_save_area *save = &svm->vmcb->save;
2814
2815 pr_err("VMCB Control Area:\n");
2816 pr_err("cr_read: %04x\n", control->intercept_cr_read);
2817 pr_err("cr_write: %04x\n", control->intercept_cr_write);
2818 pr_err("dr_read: %04x\n", control->intercept_dr_read);
2819 pr_err("dr_write: %04x\n", control->intercept_dr_write);
2820 pr_err("exceptions: %08x\n", control->intercept_exceptions);
2821 pr_err("intercepts: %016llx\n", control->intercept);
2822 pr_err("pause filter count: %d\n", control->pause_filter_count);
2823 pr_err("iopm_base_pa: %016llx\n", control->iopm_base_pa);
2824 pr_err("msrpm_base_pa: %016llx\n", control->msrpm_base_pa);
2825 pr_err("tsc_offset: %016llx\n", control->tsc_offset);
2826 pr_err("asid: %d\n", control->asid);
2827 pr_err("tlb_ctl: %d\n", control->tlb_ctl);
2828 pr_err("int_ctl: %08x\n", control->int_ctl);
2829 pr_err("int_vector: %08x\n", control->int_vector);
2830 pr_err("int_state: %08x\n", control->int_state);
2831 pr_err("exit_code: %08x\n", control->exit_code);
2832 pr_err("exit_info1: %016llx\n", control->exit_info_1);
2833 pr_err("exit_info2: %016llx\n", control->exit_info_2);
2834 pr_err("exit_int_info: %08x\n", control->exit_int_info);
2835 pr_err("exit_int_info_err: %08x\n", control->exit_int_info_err);
2836 pr_err("nested_ctl: %lld\n", control->nested_ctl);
2837 pr_err("nested_cr3: %016llx\n", control->nested_cr3);
2838 pr_err("event_inj: %08x\n", control->event_inj);
2839 pr_err("event_inj_err: %08x\n", control->event_inj_err);
2840 pr_err("lbr_ctl: %lld\n", control->lbr_ctl);
2841 pr_err("next_rip: %016llx\n", control->next_rip);
2842 pr_err("VMCB State Save Area:\n");
2843 pr_err("es: s: %04x a: %04x l: %08x b: %016llx\n",
2844 save->es.selector, save->es.attrib,
2845 save->es.limit, save->es.base);
2846 pr_err("cs: s: %04x a: %04x l: %08x b: %016llx\n",
2847 save->cs.selector, save->cs.attrib,
2848 save->cs.limit, save->cs.base);
2849 pr_err("ss: s: %04x a: %04x l: %08x b: %016llx\n",
2850 save->ss.selector, save->ss.attrib,
2851 save->ss.limit, save->ss.base);
2852 pr_err("ds: s: %04x a: %04x l: %08x b: %016llx\n",
2853 save->ds.selector, save->ds.attrib,
2854 save->ds.limit, save->ds.base);
2855 pr_err("fs: s: %04x a: %04x l: %08x b: %016llx\n",
2856 save->fs.selector, save->fs.attrib,
2857 save->fs.limit, save->fs.base);
2858 pr_err("gs: s: %04x a: %04x l: %08x b: %016llx\n",
2859 save->gs.selector, save->gs.attrib,
2860 save->gs.limit, save->gs.base);
2861 pr_err("gdtr: s: %04x a: %04x l: %08x b: %016llx\n",
2862 save->gdtr.selector, save->gdtr.attrib,
2863 save->gdtr.limit, save->gdtr.base);
2864 pr_err("ldtr: s: %04x a: %04x l: %08x b: %016llx\n",
2865 save->ldtr.selector, save->ldtr.attrib,
2866 save->ldtr.limit, save->ldtr.base);
2867 pr_err("idtr: s: %04x a: %04x l: %08x b: %016llx\n",
2868 save->idtr.selector, save->idtr.attrib,
2869 save->idtr.limit, save->idtr.base);
2870 pr_err("tr: s: %04x a: %04x l: %08x b: %016llx\n",
2871 save->tr.selector, save->tr.attrib,
2872 save->tr.limit, save->tr.base);
2873 pr_err("cpl: %d efer: %016llx\n",
2874 save->cpl, save->efer);
2875 pr_err("cr0: %016llx cr2: %016llx\n",
2876 save->cr0, save->cr2);
2877 pr_err("cr3: %016llx cr4: %016llx\n",
2878 save->cr3, save->cr4);
2879 pr_err("dr6: %016llx dr7: %016llx\n",
2880 save->dr6, save->dr7);
2881 pr_err("rip: %016llx rflags: %016llx\n",
2882 save->rip, save->rflags);
2883 pr_err("rsp: %016llx rax: %016llx\n",
2884 save->rsp, save->rax);
2885 pr_err("star: %016llx lstar: %016llx\n",
2886 save->star, save->lstar);
2887 pr_err("cstar: %016llx sfmask: %016llx\n",
2888 save->cstar, save->sfmask);
2889 pr_err("kernel_gs_base: %016llx sysenter_cs: %016llx\n",
2890 save->kernel_gs_base, save->sysenter_cs);
2891 pr_err("sysenter_esp: %016llx sysenter_eip: %016llx\n",
2892 save->sysenter_esp, save->sysenter_eip);
2893 pr_err("gpat: %016llx dbgctl: %016llx\n",
2894 save->g_pat, save->dbgctl);
2895 pr_err("br_from: %016llx br_to: %016llx\n",
2896 save->br_from, save->br_to);
2897 pr_err("excp_from: %016llx excp_to: %016llx\n",
2898 save->last_excp_from, save->last_excp_to);
2899
2900}
2901
Avi Kivity851ba692009-08-24 11:10:17 +03002902static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002903{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002904 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002905 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002906 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002907
Avi Kivity5bfd8b52010-03-11 10:50:44 +02002908 trace_kvm_exit(exit_code, vcpu);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002909
Joerg Roedel2be4fc72010-04-22 12:33:09 +02002910 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR0_MASK))
2911 vcpu->arch.cr0 = svm->vmcb->save.cr0;
2912 if (npt_enabled)
2913 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002914
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002915 if (unlikely(svm->nested.exit_required)) {
2916 nested_svm_vmexit(svm);
2917 svm->nested.exit_required = false;
2918
2919 return 1;
2920 }
2921
Alexander Grafcf74a782008-11-25 20:17:08 +01002922 if (is_nested(svm)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02002923 int vmexit;
2924
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02002925 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
2926 svm->vmcb->control.exit_info_1,
2927 svm->vmcb->control.exit_info_2,
2928 svm->vmcb->control.exit_int_info,
2929 svm->vmcb->control.exit_int_info_err);
2930
Joerg Roedel410e4d52009-08-07 11:49:44 +02002931 vmexit = nested_svm_exit_special(svm);
2932
2933 if (vmexit == NESTED_EXIT_CONTINUE)
2934 vmexit = nested_svm_exit_handled(svm);
2935
2936 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01002937 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01002938 }
2939
Joerg Roedela5c38322009-08-07 11:49:32 +02002940 svm_complete_interrupts(svm);
2941
Avi Kivity04d2cc72007-09-10 18:10:54 +03002942 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
2943 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2944 kvm_run->fail_entry.hardware_entry_failure_reason
2945 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02002946 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
2947 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03002948 return 0;
2949 }
2950
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002951 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002952 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002953 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002954 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
2955 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002956 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002957 exit_code);
2958
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02002959 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08002960 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002961 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03002962 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002963 return 0;
2964 }
2965
Avi Kivity851ba692009-08-24 11:10:17 +03002966 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002967}
2968
2969static void reload_tss(struct kvm_vcpu *vcpu)
2970{
2971 int cpu = raw_smp_processor_id();
2972
Tejun Heo0fe1e002009-10-29 22:34:14 +09002973 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
2974 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002975 load_TR_desc();
2976}
2977
Rusty Russelle756fc62007-07-30 20:07:08 +10002978static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002979{
2980 int cpu = raw_smp_processor_id();
2981
Tejun Heo0fe1e002009-10-29 22:34:14 +09002982 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002983
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002984 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03002985 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09002986 if (svm->asid_generation != sd->asid_generation)
2987 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002988}
2989
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002990static void svm_inject_nmi(struct kvm_vcpu *vcpu)
2991{
2992 struct vcpu_svm *svm = to_svm(vcpu);
2993
2994 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
2995 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02002996 svm->vmcb->control.intercept |= (1ULL << INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002997 ++vcpu->stat.nmi_injections;
2998}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002999
Eddie Dong85f455f2007-07-06 12:20:49 +03003000static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003001{
3002 struct vmcb_control_area *control;
3003
Rusty Russelle756fc62007-07-30 20:07:08 +10003004 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03003005 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003006 control->int_ctl &= ~V_INTR_PRIO_MASK;
3007 control->int_ctl |= V_IRQ_MASK |
3008 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
3009}
3010
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003011static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03003012{
3013 struct vcpu_svm *svm = to_svm(vcpu);
3014
Joerg Roedel2af91942009-08-07 11:49:28 +02003015 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01003016
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03003017 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3018 ++vcpu->stat.irq_injections;
3019
Alexander Graf219b65d2009-06-15 15:21:25 +02003020 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3021 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03003022}
3023
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003024static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
3025{
3026 struct vcpu_svm *svm = to_svm(vcpu);
3027
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003028 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
3029 return;
3030
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003031 if (irr == -1)
3032 return;
3033
3034 if (tpr >= irr)
3035 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK;
3036}
3037
3038static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003039{
3040 struct vcpu_svm *svm = to_svm(vcpu);
3041 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02003042 int ret;
3043 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
3044 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
3045 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
3046
3047 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003048}
3049
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003050static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3051{
3052 struct vcpu_svm *svm = to_svm(vcpu);
3053
3054 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
3055}
3056
3057static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3058{
3059 struct vcpu_svm *svm = to_svm(vcpu);
3060
3061 if (masked) {
3062 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02003063 svm->vmcb->control.intercept |= (1ULL << INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003064 } else {
3065 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02003066 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003067 }
3068}
3069
Gleb Natapov78646122009-03-23 12:12:11 +02003070static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
3071{
3072 struct vcpu_svm *svm = to_svm(vcpu);
3073 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003074 int ret;
3075
3076 if (!gif_set(svm) ||
3077 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
3078 return 0;
3079
3080 ret = !!(vmcb->save.rflags & X86_EFLAGS_IF);
3081
3082 if (is_nested(svm))
3083 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
3084
3085 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02003086}
3087
Gleb Natapov9222be12009-04-23 17:14:37 +03003088static void enable_irq_window(struct kvm_vcpu *vcpu)
3089{
Alexander Graf219b65d2009-06-15 15:21:25 +02003090 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02003091
Joerg Roedele0231712010-02-24 18:59:10 +01003092 /*
3093 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3094 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3095 * get that intercept, this function will be called again though and
3096 * we'll get the vintr intercept.
3097 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01003098 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02003099 svm_set_vintr(svm);
3100 svm_inject_irq(svm, 0x0);
3101 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003102}
3103
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003104static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003105{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003106 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003107
Gleb Natapov44c11432009-05-11 13:35:52 +03003108 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
3109 == HF_NMI_MASK)
3110 return; /* IRET will cause a vm exit */
3111
Joerg Roedele0231712010-02-24 18:59:10 +01003112 /*
3113 * Something prevents NMI from been injected. Single step over possible
3114 * problem (IRET or exception injection or interrupt shadow)
3115 */
Jan Kiszka6be7d302009-10-18 13:24:54 +02003116 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03003117 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
3118 update_db_intercept(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003119}
3120
Izik Eiduscbc94022007-10-25 00:29:55 +02003121static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3122{
3123 return 0;
3124}
3125
Avi Kivityd9e368d2007-06-07 19:18:30 +03003126static void svm_flush_tlb(struct kvm_vcpu *vcpu)
3127{
3128 force_new_asid(vcpu);
3129}
3130
Avi Kivity04d2cc72007-09-10 18:10:54 +03003131static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
3132{
3133}
3134
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003135static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3136{
3137 struct vcpu_svm *svm = to_svm(vcpu);
3138
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003139 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
3140 return;
3141
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003142 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) {
3143 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03003144 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003145 }
3146}
3147
Joerg Roedel649d6862008-04-16 16:51:15 +02003148static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3149{
3150 struct vcpu_svm *svm = to_svm(vcpu);
3151 u64 cr8;
3152
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003153 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
3154 return;
3155
Joerg Roedel649d6862008-04-16 16:51:15 +02003156 cr8 = kvm_get_cr8(vcpu);
3157 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3158 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3159}
3160
Gleb Natapov9222be12009-04-23 17:14:37 +03003161static void svm_complete_interrupts(struct vcpu_svm *svm)
3162{
3163 u8 vector;
3164 int type;
3165 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01003166 unsigned int3_injected = svm->int3_injected;
3167
3168 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003169
Gleb Natapov44c11432009-05-11 13:35:52 +03003170 if (svm->vcpu.arch.hflags & HF_IRET_MASK)
3171 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
3172
Gleb Natapov9222be12009-04-23 17:14:37 +03003173 svm->vcpu.arch.nmi_injected = false;
3174 kvm_clear_exception_queue(&svm->vcpu);
3175 kvm_clear_interrupt_queue(&svm->vcpu);
3176
3177 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3178 return;
3179
3180 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3181 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3182
3183 switch (type) {
3184 case SVM_EXITINTINFO_TYPE_NMI:
3185 svm->vcpu.arch.nmi_injected = true;
3186 break;
3187 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01003188 /*
3189 * In case of software exceptions, do not reinject the vector,
3190 * but re-execute the instruction instead. Rewind RIP first
3191 * if we emulated INT3 before.
3192 */
3193 if (kvm_exception_is_soft(vector)) {
3194 if (vector == BP_VECTOR && int3_injected &&
3195 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
3196 kvm_rip_write(&svm->vcpu,
3197 kvm_rip_read(&svm->vcpu) -
3198 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02003199 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01003200 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003201 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3202 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003203 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03003204
3205 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003206 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03003207 break;
3208 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003209 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03003210 break;
3211 default:
3212 break;
3213 }
3214}
3215
Avi Kivity80e31d42008-07-14 14:44:59 +03003216#ifdef CONFIG_X86_64
3217#define R "r"
3218#else
3219#define R "e"
3220#endif
3221
Avi Kivity851ba692009-08-24 11:10:17 +03003222static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003223{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003224 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003225 u16 fs_selector;
3226 u16 gs_selector;
3227 u16 ldt_selector;
Avi Kivityd9e368d2007-06-07 19:18:30 +03003228
Joerg Roedel2041a062010-04-22 12:33:08 +02003229 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3230 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3231 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3232
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003233 /*
3234 * A vmexit emulation is required before the vcpu can be executed
3235 * again.
3236 */
3237 if (unlikely(svm->nested.exit_required))
3238 return;
3239
Rusty Russelle756fc62007-07-30 20:07:08 +10003240 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003241
Joerg Roedel649d6862008-04-16 16:51:15 +02003242 sync_lapic_to_cr8(vcpu);
3243
Avi Kivity6aa8b732006-12-10 02:21:36 -08003244 save_host_msrs(vcpu);
Avi Kivity9581d442010-10-19 16:46:55 +02003245 savesegment(fs, fs_selector);
3246 savesegment(gs, gs_selector);
Avi Kivityd6e88ae2008-07-10 16:53:33 +03003247 ldt_selector = kvm_read_ldt();
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003248 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003249
Avi Kivity04d2cc72007-09-10 18:10:54 +03003250 clgi();
3251
3252 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08003253
Avi Kivity6aa8b732006-12-10 02:21:36 -08003254 asm volatile (
Avi Kivity80e31d42008-07-14 14:44:59 +03003255 "push %%"R"bp; \n\t"
3256 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
3257 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
3258 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
3259 "mov %c[rsi](%[svm]), %%"R"si \n\t"
3260 "mov %c[rdi](%[svm]), %%"R"di \n\t"
3261 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003262#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003263 "mov %c[r8](%[svm]), %%r8 \n\t"
3264 "mov %c[r9](%[svm]), %%r9 \n\t"
3265 "mov %c[r10](%[svm]), %%r10 \n\t"
3266 "mov %c[r11](%[svm]), %%r11 \n\t"
3267 "mov %c[r12](%[svm]), %%r12 \n\t"
3268 "mov %c[r13](%[svm]), %%r13 \n\t"
3269 "mov %c[r14](%[svm]), %%r14 \n\t"
3270 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003271#endif
3272
Avi Kivity6aa8b732006-12-10 02:21:36 -08003273 /* Enter guest mode */
Avi Kivity80e31d42008-07-14 14:44:59 +03003274 "push %%"R"ax \n\t"
3275 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003276 __ex(SVM_VMLOAD) "\n\t"
3277 __ex(SVM_VMRUN) "\n\t"
3278 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity80e31d42008-07-14 14:44:59 +03003279 "pop %%"R"ax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003280
3281 /* Save guest registers, load host registers */
Avi Kivity80e31d42008-07-14 14:44:59 +03003282 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
3283 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
3284 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
3285 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
3286 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
3287 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003288#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003289 "mov %%r8, %c[r8](%[svm]) \n\t"
3290 "mov %%r9, %c[r9](%[svm]) \n\t"
3291 "mov %%r10, %c[r10](%[svm]) \n\t"
3292 "mov %%r11, %c[r11](%[svm]) \n\t"
3293 "mov %%r12, %c[r12](%[svm]) \n\t"
3294 "mov %%r13, %c[r13](%[svm]) \n\t"
3295 "mov %%r14, %c[r14](%[svm]) \n\t"
3296 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003297#endif
Avi Kivity80e31d42008-07-14 14:44:59 +03003298 "pop %%"R"bp"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003299 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003300 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003301 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003302 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
3303 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
3304 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
3305 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
3306 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
3307 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003308#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003309 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
3310 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
3311 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
3312 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
3313 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
3314 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
3315 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
3316 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003317#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02003318 : "cc", "memory"
Avi Kivity80e31d42008-07-14 14:44:59 +03003319 , R"bx", R"cx", R"dx", R"si", R"di"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003320#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02003321 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
3322#endif
3323 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003324
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003325 vcpu->arch.cr2 = svm->vmcb->save.cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003326 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3327 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3328 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003329
Avi Kivity6aa8b732006-12-10 02:21:36 -08003330 load_host_msrs(vcpu);
Avi Kivity9581d442010-10-19 16:46:55 +02003331 loadsegment(fs, fs_selector);
3332#ifdef CONFIG_X86_64
3333 load_gs_index(gs_selector);
3334 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs);
3335#else
3336 loadsegment(gs, gs_selector);
3337#endif
3338 kvm_load_ldt(ldt_selector);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003339
3340 reload_tss(vcpu);
3341
Avi Kivity56ba47d2007-11-07 17:14:18 +02003342 local_irq_disable();
3343
3344 stgi();
3345
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003346 sync_cr8_to_lapic(vcpu);
3347
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003348 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003349
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003350 if (npt_enabled) {
3351 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3352 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3353 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02003354
3355 /*
3356 * We need to handle MC intercepts here before the vcpu has a chance to
3357 * change the physical cpu
3358 */
3359 if (unlikely(svm->vmcb->control.exit_code ==
3360 SVM_EXIT_EXCP_BASE + MC_VECTOR))
3361 svm_handle_mce(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003362}
3363
Avi Kivity80e31d42008-07-14 14:44:59 +03003364#undef R
3365
Avi Kivity6aa8b732006-12-10 02:21:36 -08003366static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3367{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003368 struct vcpu_svm *svm = to_svm(vcpu);
3369
3370 svm->vmcb->save.cr3 = root;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003371 force_new_asid(vcpu);
3372}
3373
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003374static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3375{
3376 struct vcpu_svm *svm = to_svm(vcpu);
3377
3378 svm->vmcb->control.nested_cr3 = root;
3379
3380 /* Also sync guest cr3 here in case we live migrate */
3381 svm->vmcb->save.cr3 = vcpu->arch.cr3;
3382
3383 force_new_asid(vcpu);
3384}
3385
Avi Kivity6aa8b732006-12-10 02:21:36 -08003386static int is_disabled(void)
3387{
Joerg Roedel6031a612007-06-22 12:29:50 +03003388 u64 vm_cr;
3389
3390 rdmsrl(MSR_VM_CR, vm_cr);
3391 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3392 return 1;
3393
Avi Kivity6aa8b732006-12-10 02:21:36 -08003394 return 0;
3395}
3396
Ingo Molnar102d8322007-02-19 14:37:47 +02003397static void
3398svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3399{
3400 /*
3401 * Patch in the VMMCALL instruction:
3402 */
3403 hypercall[0] = 0x0f;
3404 hypercall[1] = 0x01;
3405 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003406}
3407
Yang, Sheng002c7f72007-07-31 14:23:01 +03003408static void svm_check_processor_compat(void *rtn)
3409{
3410 *(int *)rtn = 0;
3411}
3412
Avi Kivity774ead32007-12-26 13:57:04 +02003413static bool svm_cpu_has_accelerated_tpr(void)
3414{
3415 return false;
3416}
3417
Sheng Yang67253af2008-04-25 10:20:22 +08003418static int get_npt_level(void)
3419{
3420#ifdef CONFIG_X86_64
3421 return PT64_ROOT_LEVEL;
3422#else
3423 return PT32E_ROOT_LEVEL;
3424#endif
3425}
3426
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003427static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003428{
3429 return 0;
3430}
3431
Sheng Yang0e851882009-12-18 16:48:46 +08003432static void svm_cpuid_update(struct kvm_vcpu *vcpu)
3433{
3434}
3435
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003436static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
3437{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003438 switch (func) {
3439 case 0x8000000A:
3440 entry->eax = 1; /* SVM revision 1 */
3441 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
3442 ASID emulation to nested SVM */
3443 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02003444 entry->edx = 0; /* Per default do not support any
3445 additional features */
3446
3447 /* Support next_rip if host supports it */
3448 if (svm_has(SVM_FEATURE_NRIP))
3449 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003450
3451 break;
3452 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003453}
3454
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003455static const struct trace_print_flags svm_exit_reasons_str[] = {
Joerg Roedele0231712010-02-24 18:59:10 +01003456 { SVM_EXIT_READ_CR0, "read_cr0" },
3457 { SVM_EXIT_READ_CR3, "read_cr3" },
3458 { SVM_EXIT_READ_CR4, "read_cr4" },
3459 { SVM_EXIT_READ_CR8, "read_cr8" },
3460 { SVM_EXIT_WRITE_CR0, "write_cr0" },
3461 { SVM_EXIT_WRITE_CR3, "write_cr3" },
3462 { SVM_EXIT_WRITE_CR4, "write_cr4" },
3463 { SVM_EXIT_WRITE_CR8, "write_cr8" },
3464 { SVM_EXIT_READ_DR0, "read_dr0" },
3465 { SVM_EXIT_READ_DR1, "read_dr1" },
3466 { SVM_EXIT_READ_DR2, "read_dr2" },
3467 { SVM_EXIT_READ_DR3, "read_dr3" },
3468 { SVM_EXIT_WRITE_DR0, "write_dr0" },
3469 { SVM_EXIT_WRITE_DR1, "write_dr1" },
3470 { SVM_EXIT_WRITE_DR2, "write_dr2" },
3471 { SVM_EXIT_WRITE_DR3, "write_dr3" },
3472 { SVM_EXIT_WRITE_DR5, "write_dr5" },
3473 { SVM_EXIT_WRITE_DR7, "write_dr7" },
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003474 { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
3475 { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
3476 { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
3477 { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
3478 { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
3479 { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
3480 { SVM_EXIT_INTR, "interrupt" },
3481 { SVM_EXIT_NMI, "nmi" },
3482 { SVM_EXIT_SMI, "smi" },
3483 { SVM_EXIT_INIT, "init" },
3484 { SVM_EXIT_VINTR, "vintr" },
3485 { SVM_EXIT_CPUID, "cpuid" },
3486 { SVM_EXIT_INVD, "invd" },
3487 { SVM_EXIT_HLT, "hlt" },
3488 { SVM_EXIT_INVLPG, "invlpg" },
3489 { SVM_EXIT_INVLPGA, "invlpga" },
3490 { SVM_EXIT_IOIO, "io" },
3491 { SVM_EXIT_MSR, "msr" },
3492 { SVM_EXIT_TASK_SWITCH, "task_switch" },
3493 { SVM_EXIT_SHUTDOWN, "shutdown" },
3494 { SVM_EXIT_VMRUN, "vmrun" },
3495 { SVM_EXIT_VMMCALL, "hypercall" },
3496 { SVM_EXIT_VMLOAD, "vmload" },
3497 { SVM_EXIT_VMSAVE, "vmsave" },
3498 { SVM_EXIT_STGI, "stgi" },
3499 { SVM_EXIT_CLGI, "clgi" },
3500 { SVM_EXIT_SKINIT, "skinit" },
3501 { SVM_EXIT_WBINVD, "wbinvd" },
3502 { SVM_EXIT_MONITOR, "monitor" },
3503 { SVM_EXIT_MWAIT, "mwait" },
3504 { SVM_EXIT_NPF, "npf" },
3505 { -1, NULL }
3506};
3507
Sheng Yang17cc3932010-01-05 19:02:27 +08003508static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02003509{
Sheng Yang17cc3932010-01-05 19:02:27 +08003510 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02003511}
3512
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003513static bool svm_rdtscp_supported(void)
3514{
3515 return false;
3516}
3517
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003518static bool svm_has_wbinvd_exit(void)
3519{
3520 return true;
3521}
3522
Avi Kivity02daab22009-12-30 12:40:26 +02003523static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
3524{
3525 struct vcpu_svm *svm = to_svm(vcpu);
3526
Avi Kivity02daab22009-12-30 12:40:26 +02003527 svm->vmcb->control.intercept_exceptions |= 1 << NM_VECTOR;
Joerg Roedel66a562f2010-02-19 16:23:08 +01003528 if (is_nested(svm))
3529 svm->nested.hsave->control.intercept_exceptions |= 1 << NM_VECTOR;
3530 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02003531}
3532
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003533static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003534 .cpu_has_kvm_support = has_svm,
3535 .disabled_by_bios = is_disabled,
3536 .hardware_setup = svm_hardware_setup,
3537 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003538 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003539 .hardware_enable = svm_hardware_enable,
3540 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003541 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003542
3543 .vcpu_create = svm_create_vcpu,
3544 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003545 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003546
Avi Kivity04d2cc72007-09-10 18:10:54 +03003547 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003548 .vcpu_load = svm_vcpu_load,
3549 .vcpu_put = svm_vcpu_put,
3550
3551 .set_guest_debug = svm_guest_debug,
3552 .get_msr = svm_get_msr,
3553 .set_msr = svm_set_msr,
3554 .get_segment_base = svm_get_segment_base,
3555 .get_segment = svm_get_segment,
3556 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003557 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10003558 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02003559 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003560 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003561 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003562 .set_cr3 = svm_set_cr3,
3563 .set_cr4 = svm_set_cr4,
3564 .set_efer = svm_set_efer,
3565 .get_idt = svm_get_idt,
3566 .set_idt = svm_set_idt,
3567 .get_gdt = svm_get_gdt,
3568 .set_gdt = svm_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03003569 .set_dr7 = svm_set_dr7,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003570 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003571 .get_rflags = svm_get_rflags,
3572 .set_rflags = svm_set_rflags,
Avi Kivity6b52d182010-01-21 15:31:47 +02003573 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02003574 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003575
Avi Kivity6aa8b732006-12-10 02:21:36 -08003576 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003577
Avi Kivity6aa8b732006-12-10 02:21:36 -08003578 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003579 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003580 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04003581 .set_interrupt_shadow = svm_set_interrupt_shadow,
3582 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02003583 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003584 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003585 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02003586 .queue_exception = svm_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02003587 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003588 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003589 .get_nmi_mask = svm_get_nmi_mask,
3590 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003591 .enable_nmi_window = enable_nmi_window,
3592 .enable_irq_window = enable_irq_window,
3593 .update_cr8_intercept = update_cr8_intercept,
Izik Eiduscbc94022007-10-25 00:29:55 +02003594
3595 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003596 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003597 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003598
3599 .exit_reasons_str = svm_exit_reasons_str,
Sheng Yang17cc3932010-01-05 19:02:27 +08003600 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08003601
3602 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003603
3604 .rdtscp_supported = svm_rdtscp_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003605
3606 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003607
3608 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10003609
3610 .write_tsc_offset = svm_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10003611 .adjust_tsc_offset = svm_adjust_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003612
3613 .set_tdp_cr3 = set_tdp_cr3,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003614};
3615
3616static int __init svm_init(void)
3617{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003618 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03003619 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003620}
3621
3622static void __exit svm_exit(void)
3623{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003624 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003625}
3626
3627module_init(svm_init)
3628module_exit(svm_exit)