blob: 6882be5b926798e5bf06548904ef43749dff47a9 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * AMD SVM support
5 *
6 * Copyright (C) 2006 Qumranet, Inc.
7 *
8 * Authors:
9 * Yaniv Kamay <yaniv@qumranet.com>
10 * Avi Kivity <avi@qumranet.com>
11 *
12 * This work is licensed under the terms of the GNU GPL, version 2. See
13 * the COPYING file in the top-level directory.
14 *
15 */
Avi Kivityedf88412007-12-16 11:02:48 +020016#include <linux/kvm_host.h>
17
Eddie Dong85f455f2007-07-06 12:20:49 +030018#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080019#include "mmu.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030020#include "kvm_cache_regs.h"
Gleb Natapovfe4c7b12009-03-23 11:23:18 +020021#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040022
Avi Kivity6aa8b732006-12-10 02:21:36 -080023#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020024#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/vmalloc.h>
26#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040027#include <linux/sched.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030028#include <linux/ftrace_event.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080030
Avi Kivitye4956062007-06-28 14:15:57 -040031#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080032
Eduardo Habkost63d11422008-11-17 19:03:20 -020033#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030034#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020035
Avi Kivity4ecac3f2008-05-13 13:23:38 +030036#define __ex(x) __kvm_handle_fault_on_reboot(x)
37
Avi Kivity6aa8b732006-12-10 02:21:36 -080038MODULE_AUTHOR("Qumranet");
39MODULE_LICENSE("GPL");
40
41#define IOPM_ALLOC_ORDER 2
42#define MSRPM_ALLOC_ORDER 1
43
Avi Kivity6aa8b732006-12-10 02:21:36 -080044#define SEG_TYPE_LDT 2
45#define SEG_TYPE_BUSY_TSS16 3
46
Joerg Roedel80b77062007-03-30 17:02:14 +030047#define SVM_FEATURE_NPT (1 << 0)
48#define SVM_FEATURE_LBRV (1 << 1)
Amit Shah94c935a12008-08-18 13:11:46 +030049#define SVM_FEATURE_SVML (1 << 2)
Jan Kiszka66b71382010-02-23 17:47:56 +010050#define SVM_FEATURE_NRIP (1 << 3)
Mark Langsdorf565d0992009-10-06 14:25:02 -050051#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030052
Joerg Roedel410e4d52009-08-07 11:49:44 +020053#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
54#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
55#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
56
Joerg Roedel24e09cb2008-02-13 18:58:47 +010057#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
58
Avi Kivity6c8166a2009-05-31 18:15:37 +030059static const u32 host_save_user_msrs[] = {
60#ifdef CONFIG_X86_64
61 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
62 MSR_FS_BASE,
63#endif
64 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
65};
66
67#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
68
69struct kvm_vcpu;
70
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020071struct nested_state {
72 struct vmcb *hsave;
73 u64 hsave_msr;
Joerg Roedel4a810182010-02-24 18:59:15 +010074 u64 vm_cr_msr;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020075 u64 vmcb;
76
77 /* These are the merged vectors */
78 u32 *msrpm;
79
80 /* gpa pointers to the real vectors */
81 u64 vmcb_msrpm;
Joerg Roedelaad42c62009-08-07 11:49:34 +020082
Joerg Roedelcd3ff652009-10-09 16:08:26 +020083 /* A VMEXIT is required but not yet emulated */
84 bool exit_required;
85
Joerg Roedelaad42c62009-08-07 11:49:34 +020086 /* cache for intercepts of the guest */
87 u16 intercept_cr_read;
88 u16 intercept_cr_write;
89 u16 intercept_dr_read;
90 u16 intercept_dr_write;
91 u32 intercept_exceptions;
92 u64 intercept;
93
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020094};
95
Avi Kivity6c8166a2009-05-31 18:15:37 +030096struct vcpu_svm {
97 struct kvm_vcpu vcpu;
98 struct vmcb *vmcb;
99 unsigned long vmcb_pa;
100 struct svm_cpu_data *svm_data;
101 uint64_t asid_generation;
102 uint64_t sysenter_esp;
103 uint64_t sysenter_eip;
104
105 u64 next_rip;
106
107 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
108 u64 host_gs_base;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300109
110 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300111
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200112 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200113
114 bool nmi_singlestep;
Jan Kiszka66b71382010-02-23 17:47:56 +0100115
116 unsigned int3_injected;
117 unsigned long int3_rip;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300118};
119
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100120/* enable NPT for AMD64 and X86 with PAE */
121#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
122static bool npt_enabled = true;
123#else
Joerg Roedele0231712010-02-24 18:59:10 +0100124static bool npt_enabled;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100125#endif
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100126static int npt = 1;
127
128module_param(npt, int, S_IRUGO);
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100129
Joerg Roedel4b6e4dc2009-08-07 11:49:48 +0200130static int nested = 1;
Alexander Graf236de052008-11-25 20:17:10 +0100131module_param(nested, int, S_IRUGO);
132
Joerg Roedel44874f82008-08-27 14:18:43 +0200133static void svm_flush_tlb(struct kvm_vcpu *vcpu);
Joerg Roedela5c38322009-08-07 11:49:32 +0200134static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity04d2cc72007-09-10 18:10:54 +0300135
Joerg Roedel410e4d52009-08-07 11:49:44 +0200136static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100137static int nested_svm_intercept(struct vcpu_svm *svm);
Alexander Grafcf74a782008-11-25 20:17:08 +0100138static int nested_svm_vmexit(struct vcpu_svm *svm);
Alexander Grafcf74a782008-11-25 20:17:08 +0100139static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
140 bool has_error_code, u32 error_code);
141
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400142static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
143{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000144 return container_of(vcpu, struct vcpu_svm, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400145}
146
Alexander Graf3d6368e2008-11-25 20:17:07 +0100147static inline bool is_nested(struct vcpu_svm *svm)
148{
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200149 return svm->nested.vmcb;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100150}
151
Joerg Roedel2af91942009-08-07 11:49:28 +0200152static inline void enable_gif(struct vcpu_svm *svm)
153{
154 svm->vcpu.arch.hflags |= HF_GIF_MASK;
155}
156
157static inline void disable_gif(struct vcpu_svm *svm)
158{
159 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
160}
161
162static inline bool gif_set(struct vcpu_svm *svm)
163{
164 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
165}
166
Harvey Harrison4866d5e2008-02-19 10:32:02 -0800167static unsigned long iopm_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800168
169struct kvm_ldttss_desc {
170 u16 limit0;
171 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100172 unsigned base1:8, type:5, dpl:2, p:1;
173 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800174 u32 base3;
175 u32 zero1;
176} __attribute__((packed));
177
178struct svm_cpu_data {
179 int cpu;
180
Avi Kivity5008fdf2007-04-02 13:05:50 +0300181 u64 asid_generation;
182 u32 max_asid;
183 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800184 struct kvm_ldttss_desc *tss_desc;
185
186 struct page *save_area;
187};
188
189static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
Joerg Roedel80b77062007-03-30 17:02:14 +0300190static uint32_t svm_features;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800191
192struct svm_init_data {
193 int cpu;
194 int r;
195};
196
197static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
198
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200199#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800200#define MSRS_RANGE_SIZE 2048
201#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
202
203#define MAX_INST_SIZE 15
204
Joerg Roedel80b77062007-03-30 17:02:14 +0300205static inline u32 svm_has(u32 feat)
206{
207 return svm_features & feat;
208}
209
Avi Kivity6aa8b732006-12-10 02:21:36 -0800210static inline void clgi(void)
211{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300212 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800213}
214
215static inline void stgi(void)
216{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300217 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800218}
219
220static inline void invlpga(unsigned long addr, u32 asid)
221{
Joerg Roedele0231712010-02-24 18:59:10 +0100222 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800223}
224
Avi Kivity6aa8b732006-12-10 02:21:36 -0800225static inline void force_new_asid(struct kvm_vcpu *vcpu)
226{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400227 to_svm(vcpu)->asid_generation--;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800228}
229
230static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
231{
232 force_new_asid(vcpu);
233}
234
235static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
236{
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100237 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600238 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800239
Alexander Graf9962d032008-11-25 20:17:02 +0100240 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Avi Kivityf6801df2010-01-21 15:31:50 +0200241 vcpu->arch.efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800242}
243
Avi Kivity6aa8b732006-12-10 02:21:36 -0800244static int is_external_interrupt(u32 info)
245{
246 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
247 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
248}
249
Glauber Costa2809f5d2009-05-12 16:21:05 -0400250static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
251{
252 struct vcpu_svm *svm = to_svm(vcpu);
253 u32 ret = 0;
254
255 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Jan Kiszka48005f62010-02-19 19:38:07 +0100256 ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400257 return ret & mask;
258}
259
260static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
261{
262 struct vcpu_svm *svm = to_svm(vcpu);
263
264 if (mask == 0)
265 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
266 else
267 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
268
269}
270
Avi Kivity6aa8b732006-12-10 02:21:36 -0800271static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
272{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400273 struct vcpu_svm *svm = to_svm(vcpu);
274
275 if (!svm->next_rip) {
Avi Kivity851ba692009-08-24 11:10:17 +0300276 if (emulate_instruction(vcpu, 0, 0, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300277 EMULATE_DONE)
278 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800279 return;
280 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300281 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
282 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
283 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800284
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300285 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400286 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800287}
288
Jan Kiszka116a4752010-02-23 17:47:54 +0100289static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
290 bool has_error_code, u32 error_code)
291{
292 struct vcpu_svm *svm = to_svm(vcpu);
293
Joerg Roedele0231712010-02-24 18:59:10 +0100294 /*
295 * If we are within a nested VM we'd better #VMEXIT and let the guest
296 * handle the exception
297 */
Jan Kiszka116a4752010-02-23 17:47:54 +0100298 if (nested_svm_check_exception(svm, nr, has_error_code, error_code))
299 return;
300
Jan Kiszka66b71382010-02-23 17:47:56 +0100301 if (nr == BP_VECTOR && !svm_has(SVM_FEATURE_NRIP)) {
302 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
303
304 /*
305 * For guest debugging where we have to reinject #BP if some
306 * INT3 is guest-owned:
307 * Emulate nRIP by moving RIP forward. Will fail if injection
308 * raises a fault that is not intercepted. Still better than
309 * failing in all cases.
310 */
311 skip_emulated_instruction(&svm->vcpu);
312 rip = kvm_rip_read(&svm->vcpu);
313 svm->int3_rip = rip + svm->vmcb->save.cs.base;
314 svm->int3_injected = rip - old_rip;
315 }
316
Jan Kiszka116a4752010-02-23 17:47:54 +0100317 svm->vmcb->control.event_inj = nr
318 | SVM_EVTINJ_VALID
319 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
320 | SVM_EVTINJ_TYPE_EXEPT;
321 svm->vmcb->control.event_inj_err = error_code;
322}
323
Avi Kivity6aa8b732006-12-10 02:21:36 -0800324static int has_svm(void)
325{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200326 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800327
Eduardo Habkost63d11422008-11-17 19:03:20 -0200328 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800329 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800330 return 0;
331 }
332
Avi Kivity6aa8b732006-12-10 02:21:36 -0800333 return 1;
334}
335
336static void svm_hardware_disable(void *garbage)
337{
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200338 cpu_svm_disable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800339}
340
Alexander Graf10474ae2009-09-15 11:37:46 +0200341static int svm_hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800342{
343
Tejun Heo0fe1e002009-10-29 22:34:14 +0900344 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800345 uint64_t efer;
Gleb Natapov89a27f42010-02-16 10:51:48 +0200346 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800347 struct desc_struct *gdt;
348 int me = raw_smp_processor_id();
349
Alexander Graf10474ae2009-09-15 11:37:46 +0200350 rdmsrl(MSR_EFER, efer);
351 if (efer & EFER_SVME)
352 return -EBUSY;
353
Avi Kivity6aa8b732006-12-10 02:21:36 -0800354 if (!has_svm()) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000355 printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
356 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200357 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800358 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900359 sd = per_cpu(svm_data, me);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800360
Tejun Heo0fe1e002009-10-29 22:34:14 +0900361 if (!sd) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000362 printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -0800363 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200364 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800365 }
366
Tejun Heo0fe1e002009-10-29 22:34:14 +0900367 sd->asid_generation = 1;
368 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
369 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800370
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200371 native_store_gdt(&gdt_descr);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200372 gdt = (struct desc_struct *)gdt_descr.address;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900373 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800374
Alexander Graf9962d032008-11-25 20:17:02 +0100375 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800376
Linus Torvaldsd0316552009-12-14 09:58:24 -0800377 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200378
379 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800380}
381
Joerg Roedel0da1db752008-07-02 16:02:11 +0200382static void svm_cpu_uninit(int cpu)
383{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900384 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200385
Tejun Heo0fe1e002009-10-29 22:34:14 +0900386 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200387 return;
388
389 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900390 __free_page(sd->save_area);
391 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200392}
393
Avi Kivity6aa8b732006-12-10 02:21:36 -0800394static int svm_cpu_init(int cpu)
395{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900396 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800397 int r;
398
Tejun Heo0fe1e002009-10-29 22:34:14 +0900399 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
400 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800401 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900402 sd->cpu = cpu;
403 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800404 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900405 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800406 goto err_1;
407
Tejun Heo0fe1e002009-10-29 22:34:14 +0900408 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800409
410 return 0;
411
412err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900413 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800414 return r;
415
416}
417
Rusty Russellbfc733a2007-07-31 20:42:42 +1000418static void set_msr_interception(u32 *msrpm, unsigned msr,
419 int read, int write)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800420{
421 int i;
422
423 for (i = 0; i < NUM_MSR_MAPS; i++) {
424 if (msr >= msrpm_ranges[i] &&
425 msr < msrpm_ranges[i] + MSRS_IN_RANGE) {
426 u32 msr_offset = (i * MSRS_IN_RANGE + msr -
427 msrpm_ranges[i]) * 2;
428
429 u32 *base = msrpm + (msr_offset / 32);
430 u32 msr_shift = msr_offset % 32;
431 u32 mask = ((write) ? 0 : 2) | ((read) ? 0 : 1);
432 *base = (*base & ~(0x3 << msr_shift)) |
433 (mask << msr_shift);
Rusty Russellbfc733a2007-07-31 20:42:42 +1000434 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800435 }
436 }
Rusty Russellbfc733a2007-07-31 20:42:42 +1000437 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800438}
439
Joerg Roedelf65c2292008-02-13 18:58:46 +0100440static void svm_vcpu_init_msrpm(u32 *msrpm)
441{
442 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
443
444#ifdef CONFIG_X86_64
445 set_msr_interception(msrpm, MSR_GS_BASE, 1, 1);
446 set_msr_interception(msrpm, MSR_FS_BASE, 1, 1);
447 set_msr_interception(msrpm, MSR_KERNEL_GS_BASE, 1, 1);
448 set_msr_interception(msrpm, MSR_LSTAR, 1, 1);
449 set_msr_interception(msrpm, MSR_CSTAR, 1, 1);
450 set_msr_interception(msrpm, MSR_SYSCALL_MASK, 1, 1);
451#endif
452 set_msr_interception(msrpm, MSR_K6_STAR, 1, 1);
453 set_msr_interception(msrpm, MSR_IA32_SYSENTER_CS, 1, 1);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100454}
455
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100456static void svm_enable_lbrv(struct vcpu_svm *svm)
457{
458 u32 *msrpm = svm->msrpm;
459
460 svm->vmcb->control.lbr_ctl = 1;
461 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
462 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
463 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
464 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
465}
466
467static void svm_disable_lbrv(struct vcpu_svm *svm)
468{
469 u32 *msrpm = svm->msrpm;
470
471 svm->vmcb->control.lbr_ctl = 0;
472 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
473 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
474 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
475 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
476}
477
Avi Kivity6aa8b732006-12-10 02:21:36 -0800478static __init int svm_hardware_setup(void)
479{
480 int cpu;
481 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100482 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800483 int r;
484
Avi Kivity6aa8b732006-12-10 02:21:36 -0800485 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
486
487 if (!iopm_pages)
488 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300489
490 iopm_va = page_address(iopm_pages);
491 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800492 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
493
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100494 if (boot_cpu_has(X86_FEATURE_NX))
495 kvm_enable_efer_bits(EFER_NX);
496
Alexander Graf1b2fd702009-02-02 16:23:51 +0100497 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
498 kvm_enable_efer_bits(EFER_FFXSR);
499
Alexander Graf236de052008-11-25 20:17:10 +0100500 if (nested) {
501 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
502 kvm_enable_efer_bits(EFER_SVME);
503 }
504
Zachary Amsden3230bb42009-09-29 11:38:37 -1000505 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800506 r = svm_cpu_init(cpu);
507 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100508 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800509 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100510
511 svm_features = cpuid_edx(SVM_CPUID_FUNC);
512
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100513 if (!svm_has(SVM_FEATURE_NPT))
514 npt_enabled = false;
515
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100516 if (npt_enabled && !npt) {
517 printk(KERN_INFO "kvm: Nested Paging disabled\n");
518 npt_enabled = false;
519 }
520
Joerg Roedel18552672008-02-07 13:47:41 +0100521 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100522 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100523 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200524 } else
525 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100526
Avi Kivity6aa8b732006-12-10 02:21:36 -0800527 return 0;
528
Joerg Roedelf65c2292008-02-13 18:58:46 +0100529err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800530 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
531 iopm_base = 0;
532 return r;
533}
534
535static __exit void svm_hardware_unsetup(void)
536{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200537 int cpu;
538
Zachary Amsden3230bb42009-09-29 11:38:37 -1000539 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200540 svm_cpu_uninit(cpu);
541
Avi Kivity6aa8b732006-12-10 02:21:36 -0800542 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100543 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800544}
545
546static void init_seg(struct vmcb_seg *seg)
547{
548 seg->selector = 0;
549 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +0100550 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800551 seg->limit = 0xffff;
552 seg->base = 0;
553}
554
555static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
556{
557 seg->selector = 0;
558 seg->attrib = SVM_SELECTOR_P_MASK | type;
559 seg->limit = 0xffff;
560 seg->base = 0;
561}
562
Joerg Roedele6101a92008-02-13 18:58:45 +0100563static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800564{
Joerg Roedele6101a92008-02-13 18:58:45 +0100565 struct vmcb_control_area *control = &svm->vmcb->control;
566 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800567
Avi Kivitybff78272010-01-07 13:16:08 +0200568 svm->vcpu.fpu_active = 1;
569
Joerg Roedele0231712010-02-24 18:59:10 +0100570 control->intercept_cr_read = INTERCEPT_CR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800571 INTERCEPT_CR3_MASK |
Joerg Roedel649d6862008-04-16 16:51:15 +0200572 INTERCEPT_CR4_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800573
Joerg Roedele0231712010-02-24 18:59:10 +0100574 control->intercept_cr_write = INTERCEPT_CR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800575 INTERCEPT_CR3_MASK |
Avi Kivity80a81192007-12-06 19:50:00 +0200576 INTERCEPT_CR4_MASK |
577 INTERCEPT_CR8_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800578
Joerg Roedele0231712010-02-24 18:59:10 +0100579 control->intercept_dr_read = INTERCEPT_DR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800580 INTERCEPT_DR1_MASK |
581 INTERCEPT_DR2_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100582 INTERCEPT_DR3_MASK |
583 INTERCEPT_DR4_MASK |
584 INTERCEPT_DR5_MASK |
585 INTERCEPT_DR6_MASK |
586 INTERCEPT_DR7_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800587
Joerg Roedele0231712010-02-24 18:59:10 +0100588 control->intercept_dr_write = INTERCEPT_DR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800589 INTERCEPT_DR1_MASK |
590 INTERCEPT_DR2_MASK |
591 INTERCEPT_DR3_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100592 INTERCEPT_DR4_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800593 INTERCEPT_DR5_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100594 INTERCEPT_DR6_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800595 INTERCEPT_DR7_MASK;
596
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500597 control->intercept_exceptions = (1 << PF_VECTOR) |
Joerg Roedel53371b52008-04-09 14:15:30 +0200598 (1 << UD_VECTOR) |
599 (1 << MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800600
601
Joerg Roedele0231712010-02-24 18:59:10 +0100602 control->intercept = (1ULL << INTERCEPT_INTR) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800603 (1ULL << INTERCEPT_NMI) |
Joerg Roedel01525272007-02-19 14:37:47 +0200604 (1ULL << INTERCEPT_SMI) |
Avi Kivityd2251572010-01-06 10:55:27 +0200605 (1ULL << INTERCEPT_SELECTIVE_CR0) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800606 (1ULL << INTERCEPT_CPUID) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200607 (1ULL << INTERCEPT_INVD) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800608 (1ULL << INTERCEPT_HLT) |
Marcelo Tosattia7052892008-09-23 13:18:35 -0300609 (1ULL << INTERCEPT_INVLPG) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800610 (1ULL << INTERCEPT_INVLPGA) |
611 (1ULL << INTERCEPT_IOIO_PROT) |
612 (1ULL << INTERCEPT_MSR_PROT) |
613 (1ULL << INTERCEPT_TASK_SWITCH) |
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800614 (1ULL << INTERCEPT_SHUTDOWN) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800615 (1ULL << INTERCEPT_VMRUN) |
616 (1ULL << INTERCEPT_VMMCALL) |
617 (1ULL << INTERCEPT_VMLOAD) |
618 (1ULL << INTERCEPT_VMSAVE) |
619 (1ULL << INTERCEPT_STGI) |
620 (1ULL << INTERCEPT_CLGI) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100621 (1ULL << INTERCEPT_SKINIT) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200622 (1ULL << INTERCEPT_WBINVD) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100623 (1ULL << INTERCEPT_MONITOR) |
624 (1ULL << INTERCEPT_MWAIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800625
626 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100627 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity0cc50642007-03-25 12:07:27 +0200628 control->tsc_offset = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800629 control->int_ctl = V_INTR_MASKING_MASK;
630
631 init_seg(&save->es);
632 init_seg(&save->ss);
633 init_seg(&save->ds);
634 init_seg(&save->fs);
635 init_seg(&save->gs);
636
637 save->cs.selector = 0xf000;
638 /* Executable/Readable Code Segment */
639 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
640 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
641 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800642 /*
643 * cs.base should really be 0xffff0000, but vmx can't handle that, so
644 * be consistent with it.
645 *
646 * Replace when we have real mode working for vmx.
647 */
648 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800649
650 save->gdtr.limit = 0xffff;
651 save->idtr.limit = 0xffff;
652
653 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
654 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
655
Alexander Graf9962d032008-11-25 20:17:02 +0100656 save->efer = EFER_SVME;
Mike Dayd77c26f2007-10-08 09:02:08 -0400657 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800658 save->dr7 = 0x400;
659 save->rflags = 2;
660 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300661 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800662
Joerg Roedele0231712010-02-24 18:59:10 +0100663 /*
664 * This is the guest-visible cr0 value.
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200665 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800666 */
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200667 svm->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
668 kvm_set_cr0(&svm->vcpu, svm->vcpu.arch.cr0);
669
Rusty Russell66aee912007-07-17 23:34:16 +1000670 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800671 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100672
673 if (npt_enabled) {
674 /* Setup VMCB for Nested Paging */
675 control->nested_ctl = 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -0300676 control->intercept &= ~((1ULL << INTERCEPT_TASK_SWITCH) |
677 (1ULL << INTERCEPT_INVLPG));
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100678 control->intercept_exceptions &= ~(1 << PF_VECTOR);
Avi Kivity888f9f32010-01-10 12:14:04 +0200679 control->intercept_cr_read &= ~INTERCEPT_CR3_MASK;
680 control->intercept_cr_write &= ~INTERCEPT_CR3_MASK;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100681 save->g_pat = 0x0007040600070406ULL;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100682 save->cr3 = 0;
683 save->cr4 = 0;
684 }
Avi Kivitya79d2f12008-04-14 13:10:21 +0300685 force_new_asid(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +0100686
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200687 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +0200688 svm->vcpu.arch.hflags = 0;
689
Mark Langsdorf565d0992009-10-06 14:25:02 -0500690 if (svm_has(SVM_FEATURE_PAUSE_FILTER)) {
691 control->pause_filter_count = 3000;
692 control->intercept |= (1ULL << INTERCEPT_PAUSE);
693 }
694
Joerg Roedel2af91942009-08-07 11:49:28 +0200695 enable_gif(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800696}
697
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200698static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +0300699{
700 struct vcpu_svm *svm = to_svm(vcpu);
701
Joerg Roedele6101a92008-02-13 18:58:45 +0100702 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +0200703
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300704 if (!kvm_vcpu_is_bsp(vcpu)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300705 kvm_rip_write(vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800706 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
707 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +0200708 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300709 vcpu->arch.regs_avail = ~0;
710 vcpu->arch.regs_dirty = ~0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200711
712 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300713}
714
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000715static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800716{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400717 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800718 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100719 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +0100720 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100721 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000722 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800723
Rusty Russellc16f8622007-07-30 21:12:19 +1000724 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000725 if (!svm) {
726 err = -ENOMEM;
727 goto out;
728 }
729
730 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
731 if (err)
732 goto free_svm;
733
Joerg Roedelf65c2292008-02-13 18:58:46 +0100734 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900735 page = alloc_page(GFP_KERNEL);
736 if (!page)
737 goto uninit;
738
Joerg Roedelf65c2292008-02-13 18:58:46 +0100739 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
740 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900741 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100742
743 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
744 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900745 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100746
Alexander Grafb286d5d2008-11-25 20:17:05 +0100747 hsave_page = alloc_page(GFP_KERNEL);
748 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900749 goto free_page3;
750
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200751 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +0100752
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900753 svm->msrpm = page_address(msrpm_pages);
754 svm_vcpu_init_msrpm(svm->msrpm);
755
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200756 svm->nested.msrpm = page_address(nested_msrpm_pages);
Alexander Graf3d6368e2008-11-25 20:17:07 +0100757
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400758 svm->vmcb = page_address(page);
759 clear_page(svm->vmcb);
760 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
761 svm->asid_generation = 0;
Joerg Roedele6101a92008-02-13 18:58:45 +0100762 init_vmcb(svm);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400763
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000764 fx_init(&svm->vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800765 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300766 if (kvm_vcpu_is_bsp(&svm->vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800767 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800768
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000769 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -0800770
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900771free_page3:
772 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
773free_page2:
774 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
775free_page1:
776 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000777uninit:
778 kvm_vcpu_uninit(&svm->vcpu);
779free_svm:
Rusty Russella4770342007-08-01 14:46:11 +1000780 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000781out:
782 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800783}
784
785static void svm_free_vcpu(struct kvm_vcpu *vcpu)
786{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400787 struct vcpu_svm *svm = to_svm(vcpu);
788
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000789 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +0100790 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200791 __free_page(virt_to_page(svm->nested.hsave));
792 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000793 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +1000794 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800795}
796
Avi Kivity15ad7142007-07-11 18:17:21 +0300797static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800798{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400799 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300800 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +0200801
Avi Kivity0cc50642007-03-25 12:07:27 +0200802 if (unlikely(cpu != vcpu->cpu)) {
Joerg Roedele935d482009-09-16 15:24:19 +0200803 u64 delta;
Avi Kivity0cc50642007-03-25 12:07:27 +0200804
Joerg Roedel953899b2009-12-14 12:22:20 +0100805 if (check_tsc_unstable()) {
806 /*
807 * Make sure that the guest sees a monotonically
808 * increasing TSC.
809 */
810 delta = vcpu->arch.host_tsc - native_read_tsc();
811 svm->vmcb->control.tsc_offset += delta;
812 if (is_nested(svm))
813 svm->nested.hsave->control.tsc_offset += delta;
814 }
Avi Kivity0cc50642007-03-25 12:07:27 +0200815 vcpu->cpu = cpu;
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300816 kvm_migrate_timers(vcpu);
Marcelo Tosatti4b656b12009-07-21 12:47:45 -0300817 svm->asid_generation = 0;
Avi Kivity0cc50642007-03-25 12:07:27 +0200818 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300819
820 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400821 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800822}
823
824static void svm_vcpu_put(struct kvm_vcpu *vcpu)
825{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400826 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300827 int i;
828
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200829 ++vcpu->stat.host_state_reload;
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300830 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400831 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300832
Joerg Roedele935d482009-09-16 15:24:19 +0200833 vcpu->arch.host_tsc = native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800834}
835
Avi Kivity6aa8b732006-12-10 02:21:36 -0800836static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
837{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400838 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839}
840
841static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
842{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400843 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800844}
845
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300846static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
847{
848 switch (reg) {
849 case VCPU_EXREG_PDPTR:
850 BUG_ON(!npt_enabled);
851 load_pdptrs(vcpu, vcpu->arch.cr3);
852 break;
853 default:
854 BUG();
855 }
856}
857
Alexander Graff0b85052008-11-25 20:17:01 +0100858static void svm_set_vintr(struct vcpu_svm *svm)
859{
860 svm->vmcb->control.intercept |= 1ULL << INTERCEPT_VINTR;
861}
862
863static void svm_clear_vintr(struct vcpu_svm *svm)
864{
865 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
866}
867
Avi Kivity6aa8b732006-12-10 02:21:36 -0800868static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
869{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400870 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800871
872 switch (seg) {
873 case VCPU_SREG_CS: return &save->cs;
874 case VCPU_SREG_DS: return &save->ds;
875 case VCPU_SREG_ES: return &save->es;
876 case VCPU_SREG_FS: return &save->fs;
877 case VCPU_SREG_GS: return &save->gs;
878 case VCPU_SREG_SS: return &save->ss;
879 case VCPU_SREG_TR: return &save->tr;
880 case VCPU_SREG_LDTR: return &save->ldtr;
881 }
882 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +0000883 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800884}
885
886static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
887{
888 struct vmcb_seg *s = svm_seg(vcpu, seg);
889
890 return s->base;
891}
892
893static void svm_get_segment(struct kvm_vcpu *vcpu,
894 struct kvm_segment *var, int seg)
895{
896 struct vmcb_seg *s = svm_seg(vcpu, seg);
897
898 var->base = s->base;
899 var->limit = s->limit;
900 var->selector = s->selector;
901 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
902 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
903 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
904 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
905 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
906 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
907 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
908 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
Amit Shah25022ac2008-10-27 09:04:17 +0000909
Joerg Roedele0231712010-02-24 18:59:10 +0100910 /*
911 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +0200912 * for cross vendor migration purposes by "not present"
913 */
914 var->unusable = !var->present || (var->type == 0);
915
Andre Przywara1fbdc7a2009-01-11 22:39:44 +0100916 switch (seg) {
917 case VCPU_SREG_CS:
918 /*
919 * SVM always stores 0 for the 'G' bit in the CS selector in
920 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
921 * Intel's VMENTRY has a check on the 'G' bit.
922 */
Amit Shah25022ac2008-10-27 09:04:17 +0000923 var->g = s->limit > 0xfffff;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +0100924 break;
925 case VCPU_SREG_TR:
926 /*
927 * Work around a bug where the busy flag in the tr selector
928 * isn't exposed
929 */
Amit Shahc0d09822008-10-27 09:04:18 +0000930 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +0100931 break;
932 case VCPU_SREG_DS:
933 case VCPU_SREG_ES:
934 case VCPU_SREG_FS:
935 case VCPU_SREG_GS:
936 /*
937 * The accessed bit must always be set in the segment
938 * descriptor cache, although it can be cleared in the
939 * descriptor, the cached bit always remains at 1. Since
940 * Intel has a check on this, set it here to support
941 * cross-vendor migration.
942 */
943 if (!var->unusable)
944 var->type |= 0x1;
945 break;
Andre Przywarab586eb02009-04-28 12:45:43 +0200946 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +0100947 /*
948 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +0200949 * descriptor is left as 1, although the whole segment has
950 * been made unusable. Clear it here to pass an Intel VMX
951 * entry check when cross vendor migrating.
952 */
953 if (var->unusable)
954 var->db = 0;
955 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +0100956 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800957}
958
Izik Eidus2e4d2652008-03-24 19:38:34 +0200959static int svm_get_cpl(struct kvm_vcpu *vcpu)
960{
961 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
962
963 return save->cpl;
964}
965
Gleb Natapov89a27f42010-02-16 10:51:48 +0200966static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800967{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400968 struct vcpu_svm *svm = to_svm(vcpu);
969
Gleb Natapov89a27f42010-02-16 10:51:48 +0200970 dt->size = svm->vmcb->save.idtr.limit;
971 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972}
973
Gleb Natapov89a27f42010-02-16 10:51:48 +0200974static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800975{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400976 struct vcpu_svm *svm = to_svm(vcpu);
977
Gleb Natapov89a27f42010-02-16 10:51:48 +0200978 svm->vmcb->save.idtr.limit = dt->size;
979 svm->vmcb->save.idtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800980}
981
Gleb Natapov89a27f42010-02-16 10:51:48 +0200982static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800983{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400984 struct vcpu_svm *svm = to_svm(vcpu);
985
Gleb Natapov89a27f42010-02-16 10:51:48 +0200986 dt->size = svm->vmcb->save.gdtr.limit;
987 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988}
989
Gleb Natapov89a27f42010-02-16 10:51:48 +0200990static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400992 struct vcpu_svm *svm = to_svm(vcpu);
993
Gleb Natapov89a27f42010-02-16 10:51:48 +0200994 svm->vmcb->save.gdtr.limit = dt->size;
995 svm->vmcb->save.gdtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800996}
997
Avi Kivitye8467fd2009-12-29 18:43:06 +0200998static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
999{
1000}
1001
Anthony Liguori25c4c272007-04-27 09:29:21 +03001002static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001003{
1004}
1005
Avi Kivityd2251572010-01-06 10:55:27 +02001006static void update_cr0_intercept(struct vcpu_svm *svm)
1007{
Joerg Roedel66a562f2010-02-19 16:23:08 +01001008 struct vmcb *vmcb = svm->vmcb;
Avi Kivityd2251572010-01-06 10:55:27 +02001009 ulong gcr0 = svm->vcpu.arch.cr0;
1010 u64 *hcr0 = &svm->vmcb->save.cr0;
1011
1012 if (!svm->vcpu.fpu_active)
1013 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1014 else
1015 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1016 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1017
1018
1019 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Joerg Roedel66a562f2010-02-19 16:23:08 +01001020 vmcb->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1021 vmcb->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1022 if (is_nested(svm)) {
1023 struct vmcb *hsave = svm->nested.hsave;
1024
1025 hsave->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1026 hsave->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1027 vmcb->control.intercept_cr_read |= svm->nested.intercept_cr_read;
1028 vmcb->control.intercept_cr_write |= svm->nested.intercept_cr_write;
1029 }
Avi Kivityd2251572010-01-06 10:55:27 +02001030 } else {
1031 svm->vmcb->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1032 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001033 if (is_nested(svm)) {
1034 struct vmcb *hsave = svm->nested.hsave;
1035
1036 hsave->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1037 hsave->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
1038 }
Avi Kivityd2251572010-01-06 10:55:27 +02001039 }
1040}
1041
Avi Kivity6aa8b732006-12-10 02:21:36 -08001042static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1043{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001044 struct vcpu_svm *svm = to_svm(vcpu);
1045
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001046 if (is_nested(svm)) {
1047 /*
1048 * We are here because we run in nested mode, the host kvm
1049 * intercepts cr0 writes but the l1 hypervisor does not.
1050 * But the L1 hypervisor may intercept selective cr0 writes.
1051 * This needs to be checked here.
1052 */
1053 unsigned long old, new;
1054
1055 /* Remove bits that would trigger a real cr0 write intercept */
1056 old = vcpu->arch.cr0 & SVM_CR0_SELECTIVE_MASK;
1057 new = cr0 & SVM_CR0_SELECTIVE_MASK;
1058
1059 if (old == new) {
1060 /* cr0 write with ts and mp unchanged */
1061 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
1062 if (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE)
1063 return;
1064 }
1065 }
1066
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001067#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001068 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001069 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001070 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001071 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001072 }
1073
Mike Dayd77c26f2007-10-08 09:02:08 -04001074 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001075 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001076 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001077 }
1078 }
1079#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001080 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001081
1082 if (!npt_enabled)
1083 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001084
1085 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001086 cr0 |= X86_CR0_TS;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001087 /*
1088 * re-enable caching here because the QEMU bios
1089 * does not do it - this results in some delay at
1090 * reboot
1091 */
1092 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001093 svm->vmcb->save.cr0 = cr0;
Avi Kivityd2251572010-01-06 10:55:27 +02001094 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001095}
1096
1097static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1098{
Joerg Roedel6394b642008-04-09 14:15:29 +02001099 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001100 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1101
1102 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
1103 force_new_asid(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001104
Joerg Roedelec077262008-04-09 14:15:28 +02001105 vcpu->arch.cr4 = cr4;
1106 if (!npt_enabled)
1107 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001108 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001109 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001110}
1111
1112static void svm_set_segment(struct kvm_vcpu *vcpu,
1113 struct kvm_segment *var, int seg)
1114{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001115 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001116 struct vmcb_seg *s = svm_seg(vcpu, seg);
1117
1118 s->base = var->base;
1119 s->limit = var->limit;
1120 s->selector = var->selector;
1121 if (var->unusable)
1122 s->attrib = 0;
1123 else {
1124 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1125 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1126 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1127 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1128 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1129 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1130 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1131 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1132 }
1133 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001134 svm->vmcb->save.cpl
1135 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -08001136 >> SVM_SELECTOR_DPL_SHIFT) & 3;
1137
1138}
1139
Gleb Natapov44c11432009-05-11 13:35:52 +03001140static void update_db_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001142 struct vcpu_svm *svm = to_svm(vcpu);
1143
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001144 svm->vmcb->control.intercept_exceptions &=
1145 ~((1 << DB_VECTOR) | (1 << BP_VECTOR));
Gleb Natapov44c11432009-05-11 13:35:52 +03001146
Jan Kiszka6be7d302009-10-18 13:24:54 +02001147 if (svm->nmi_singlestep)
Gleb Natapov44c11432009-05-11 13:35:52 +03001148 svm->vmcb->control.intercept_exceptions |= (1 << DB_VECTOR);
1149
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001150 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1151 if (vcpu->guest_debug &
1152 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
1153 svm->vmcb->control.intercept_exceptions |=
1154 1 << DB_VECTOR;
1155 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
1156 svm->vmcb->control.intercept_exceptions |=
1157 1 << BP_VECTOR;
1158 } else
1159 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001160}
1161
Jan Kiszka355be0b2009-10-03 00:31:21 +02001162static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Gleb Natapov44c11432009-05-11 13:35:52 +03001163{
Gleb Natapov44c11432009-05-11 13:35:52 +03001164 struct vcpu_svm *svm = to_svm(vcpu);
1165
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001166 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1167 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1168 else
1169 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1170
Jan Kiszka355be0b2009-10-03 00:31:21 +02001171 update_db_intercept(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001172}
1173
1174static void load_host_msrs(struct kvm_vcpu *vcpu)
1175{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001176#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001177 wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001178#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001179}
1180
1181static void save_host_msrs(struct kvm_vcpu *vcpu)
1182{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001183#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001184 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001185#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001186}
1187
Tejun Heo0fe1e002009-10-29 22:34:14 +09001188static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001189{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001190 if (sd->next_asid > sd->max_asid) {
1191 ++sd->asid_generation;
1192 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001193 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001194 }
1195
Tejun Heo0fe1e002009-10-29 22:34:14 +09001196 svm->asid_generation = sd->asid_generation;
1197 svm->vmcb->control.asid = sd->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001198}
1199
Jan Kiszkac76de352010-01-20 18:20:20 +01001200static int svm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *dest)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001201{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001202 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001203
1204 switch (dr) {
1205 case 0 ... 3:
Jan Kiszkac76de352010-01-20 18:20:20 +01001206 *dest = vcpu->arch.db[dr];
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001207 break;
Jan Kiszkac76de352010-01-20 18:20:20 +01001208 case 4:
1209 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
1210 return EMULATE_FAIL; /* will re-inject UD */
1211 /* fall through */
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001212 case 6:
1213 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
Jan Kiszkac76de352010-01-20 18:20:20 +01001214 *dest = vcpu->arch.dr6;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001215 else
Jan Kiszkac76de352010-01-20 18:20:20 +01001216 *dest = svm->vmcb->save.dr6;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001217 break;
Jan Kiszkac76de352010-01-20 18:20:20 +01001218 case 5:
1219 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
1220 return EMULATE_FAIL; /* will re-inject UD */
1221 /* fall through */
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001222 case 7:
1223 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
Jan Kiszkac76de352010-01-20 18:20:20 +01001224 *dest = vcpu->arch.dr7;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001225 else
Jan Kiszkac76de352010-01-20 18:20:20 +01001226 *dest = svm->vmcb->save.dr7;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001227 break;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001228 }
1229
Jan Kiszkac76de352010-01-20 18:20:20 +01001230 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001231}
1232
Jan Kiszkac76de352010-01-20 18:20:20 +01001233static int svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001234{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001235 struct vcpu_svm *svm = to_svm(vcpu);
1236
Avi Kivity6aa8b732006-12-10 02:21:36 -08001237 switch (dr) {
1238 case 0 ... 3:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001239 vcpu->arch.db[dr] = value;
1240 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1241 vcpu->arch.eff_db[dr] = value;
Jan Kiszkac76de352010-01-20 18:20:20 +01001242 break;
1243 case 4:
1244 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
1245 return EMULATE_FAIL; /* will re-inject UD */
1246 /* fall through */
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001247 case 6:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001248 vcpu->arch.dr6 = (value & DR6_VOLATILE) | DR6_FIXED_1;
Jan Kiszkac76de352010-01-20 18:20:20 +01001249 break;
1250 case 5:
1251 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
1252 return EMULATE_FAIL; /* will re-inject UD */
1253 /* fall through */
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001254 case 7:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001255 vcpu->arch.dr7 = (value & DR7_VOLATILE) | DR7_FIXED_1;
1256 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
1257 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1258 vcpu->arch.switch_db_regs = (value & DR7_BP_EN_MASK);
1259 }
Jan Kiszkac76de352010-01-20 18:20:20 +01001260 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001261 }
Jan Kiszkac76de352010-01-20 18:20:20 +01001262
1263 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001264}
1265
Avi Kivity851ba692009-08-24 11:10:17 +03001266static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001267{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001268 u64 fault_address;
1269 u32 error_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001270
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001271 fault_address = svm->vmcb->control.exit_info_2;
1272 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001273
Marcelo Tosatti229456f2009-06-17 09:22:14 -03001274 trace_kvm_page_fault(fault_address, error_code);
Avi Kivity52c78472009-08-10 15:42:41 +03001275 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1276 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
Avi Kivity30677142007-10-28 18:48:59 +02001277 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001278}
1279
Avi Kivity851ba692009-08-24 11:10:17 +03001280static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001281{
Avi Kivity851ba692009-08-24 11:10:17 +03001282 struct kvm_run *kvm_run = svm->vcpu.run;
1283
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001284 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001285 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001286 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001287 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1288 return 1;
1289 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001290
Jan Kiszka6be7d302009-10-18 13:24:54 +02001291 if (svm->nmi_singlestep) {
1292 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001293 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1294 svm->vmcb->save.rflags &=
1295 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1296 update_db_intercept(&svm->vcpu);
1297 }
1298
1299 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001300 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001301 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1302 kvm_run->debug.arch.pc =
1303 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1304 kvm_run->debug.arch.exception = DB_VECTOR;
1305 return 0;
1306 }
1307
1308 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001309}
1310
Avi Kivity851ba692009-08-24 11:10:17 +03001311static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001312{
Avi Kivity851ba692009-08-24 11:10:17 +03001313 struct kvm_run *kvm_run = svm->vcpu.run;
1314
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001315 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1316 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1317 kvm_run->debug.arch.exception = BP_VECTOR;
1318 return 0;
1319}
1320
Avi Kivity851ba692009-08-24 11:10:17 +03001321static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001322{
1323 int er;
1324
Avi Kivity851ba692009-08-24 11:10:17 +03001325 er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001326 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001327 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001328 return 1;
1329}
1330
Avi Kivity6b52d182010-01-21 15:31:47 +02001331static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001332{
Avi Kivity6b52d182010-01-21 15:31:47 +02001333 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001334 u32 excp;
1335
1336 if (is_nested(svm)) {
1337 u32 h_excp, n_excp;
1338
1339 h_excp = svm->nested.hsave->control.intercept_exceptions;
1340 n_excp = svm->nested.intercept_exceptions;
1341 h_excp &= ~(1 << NM_VECTOR);
1342 excp = h_excp | n_excp;
1343 } else {
1344 excp = svm->vmcb->control.intercept_exceptions;
Joerg Roedele0231712010-02-24 18:59:10 +01001345 excp &= ~(1 << NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001346 }
1347
1348 svm->vmcb->control.intercept_exceptions = excp;
1349
Rusty Russelle756fc62007-07-30 20:07:08 +10001350 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001351 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001352}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001353
Avi Kivity6b52d182010-01-21 15:31:47 +02001354static int nm_interception(struct vcpu_svm *svm)
1355{
1356 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001357 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001358}
1359
Avi Kivity851ba692009-08-24 11:10:17 +03001360static int mc_interception(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001361{
1362 /*
1363 * On an #MC intercept the MCE handler is not called automatically in
1364 * the host. So do it by hand here.
1365 */
1366 asm volatile (
1367 "int $0x12\n");
1368 /* not sure if we ever come back to this point */
1369
1370 return 1;
1371}
1372
Avi Kivity851ba692009-08-24 11:10:17 +03001373static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001374{
Avi Kivity851ba692009-08-24 11:10:17 +03001375 struct kvm_run *kvm_run = svm->vcpu.run;
1376
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001377 /*
1378 * VMCB is undefined after a SHUTDOWN intercept
1379 * so reinitialize it.
1380 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001381 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001382 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001383
1384 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1385 return 0;
1386}
1387
Avi Kivity851ba692009-08-24 11:10:17 +03001388static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001389{
Mike Dayd77c26f2007-10-08 09:02:08 -04001390 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001391 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001392 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001393
Rusty Russelle756fc62007-07-30 20:07:08 +10001394 ++svm->vcpu.stat.io_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001395
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001396 svm->next_rip = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001397
Laurent Viviere70669a2007-08-05 10:36:40 +03001398 string = (io_info & SVM_IOIO_STR_MASK) != 0;
1399
1400 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02001401 if (emulate_instruction(&svm->vcpu,
Avi Kivity851ba692009-08-24 11:10:17 +03001402 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03001403 return 0;
1404 return 1;
1405 }
1406
Avi Kivity039576c2007-03-20 12:46:50 +02001407 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
1408 port = io_info >> 16;
1409 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001410
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001411 skip_emulated_instruction(&svm->vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03001412 return kvm_emulate_pio(&svm->vcpu, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001413}
1414
Avi Kivity851ba692009-08-24 11:10:17 +03001415static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001416{
1417 return 1;
1418}
1419
Avi Kivity851ba692009-08-24 11:10:17 +03001420static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001421{
1422 ++svm->vcpu.stat.irq_exits;
1423 return 1;
1424}
1425
Avi Kivity851ba692009-08-24 11:10:17 +03001426static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001427{
1428 return 1;
1429}
1430
Avi Kivity851ba692009-08-24 11:10:17 +03001431static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001432{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001433 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001434 skip_emulated_instruction(&svm->vcpu);
1435 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001436}
1437
Avi Kivity851ba692009-08-24 11:10:17 +03001438static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001439{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001440 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001441 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001442 kvm_emulate_hypercall(&svm->vcpu);
1443 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001444}
1445
Alexander Grafc0725422008-11-25 20:17:03 +01001446static int nested_svm_check_permissions(struct vcpu_svm *svm)
1447{
Avi Kivityf6801df2010-01-21 15:31:50 +02001448 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01001449 || !is_paging(&svm->vcpu)) {
1450 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1451 return 1;
1452 }
1453
1454 if (svm->vmcb->save.cpl) {
1455 kvm_inject_gp(&svm->vcpu, 0);
1456 return 1;
1457 }
1458
1459 return 0;
1460}
1461
Alexander Grafcf74a782008-11-25 20:17:08 +01001462static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
1463 bool has_error_code, u32 error_code)
1464{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001465 int vmexit;
1466
Joerg Roedel0295ad72009-08-07 11:49:37 +02001467 if (!is_nested(svm))
1468 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001469
Joerg Roedel0295ad72009-08-07 11:49:37 +02001470 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1471 svm->vmcb->control.exit_code_hi = 0;
1472 svm->vmcb->control.exit_info_1 = error_code;
1473 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1474
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001475 vmexit = nested_svm_intercept(svm);
1476 if (vmexit == NESTED_EXIT_DONE)
1477 svm->nested.exit_required = true;
1478
1479 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001480}
1481
Joerg Roedel8fe54652010-02-19 16:23:01 +01001482/* This function returns true if it is save to enable the irq window */
1483static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001484{
Joerg Roedel26666952009-08-07 11:49:46 +02001485 if (!is_nested(svm))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001486 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001487
Joerg Roedel26666952009-08-07 11:49:46 +02001488 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001489 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001490
Joerg Roedel26666952009-08-07 11:49:46 +02001491 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001492 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001493
Joerg Roedel197717d2010-02-24 18:59:19 +01001494 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
1495 svm->vmcb->control.exit_info_1 = 0;
1496 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02001497
Joerg Roedelcd3ff652009-10-09 16:08:26 +02001498 if (svm->nested.intercept & 1ULL) {
1499 /*
1500 * The #vmexit can't be emulated here directly because this
1501 * code path runs with irqs and preemtion disabled. A
1502 * #vmexit emulation might sleep. Only signal request for
1503 * the #vmexit here.
1504 */
1505 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02001506 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01001507 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001508 }
1509
Joerg Roedel8fe54652010-02-19 16:23:01 +01001510 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001511}
1512
Joerg Roedel887f5002010-02-24 18:59:12 +01001513/* This function returns true if it is save to enable the nmi window */
1514static inline bool nested_svm_nmi(struct vcpu_svm *svm)
1515{
1516 if (!is_nested(svm))
1517 return true;
1518
1519 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
1520 return true;
1521
1522 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
1523 svm->nested.exit_required = true;
1524
1525 return false;
1526}
1527
Joerg Roedel7597f122010-02-19 16:23:00 +01001528static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001529{
1530 struct page *page;
1531
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01001532 might_sleep();
1533
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001534 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001535 if (is_error_page(page))
1536 goto error;
1537
Joerg Roedel7597f122010-02-19 16:23:00 +01001538 *_page = page;
1539
1540 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001541
1542error:
1543 kvm_release_page_clean(page);
1544 kvm_inject_gp(&svm->vcpu, 0);
1545
1546 return NULL;
1547}
1548
Joerg Roedel7597f122010-02-19 16:23:00 +01001549static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001550{
Joerg Roedel7597f122010-02-19 16:23:00 +01001551 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001552 kvm_release_page_dirty(page);
1553}
1554
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001555static bool nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001556{
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001557 u32 param = svm->vmcb->control.exit_info_1 & 1;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001558 u32 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1559 bool ret = false;
1560 u32 t0, t1;
Joerg Roedel4c7da8c2010-02-19 16:23:05 +01001561 u8 val;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001562
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001563 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
1564 return false;
1565
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001566 switch (msr) {
1567 case 0 ... 0x1fff:
1568 t0 = (msr * 2) % 8;
1569 t1 = msr / 8;
1570 break;
1571 case 0xc0000000 ... 0xc0001fff:
1572 t0 = (8192 + msr - 0xc0000000) * 2;
1573 t1 = (t0 / 8);
1574 t0 %= 8;
1575 break;
1576 case 0xc0010000 ... 0xc0011fff:
1577 t0 = (16384 + msr - 0xc0010000) * 2;
1578 t1 = (t0 / 8);
1579 t0 %= 8;
1580 break;
1581 default:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001582 ret = true;
1583 goto out;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001584 }
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001585
Joerg Roedel4c7da8c2010-02-19 16:23:05 +01001586 if (!kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + t1, &val, 1))
1587 ret = val & ((1 << param) << t0);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001588
1589out:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001590 return ret;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001591}
1592
Joerg Roedel410e4d52009-08-07 11:49:44 +02001593static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001594{
Alexander Grafcf74a782008-11-25 20:17:08 +01001595 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001596
Joerg Roedel410e4d52009-08-07 11:49:44 +02001597 switch (exit_code) {
1598 case SVM_EXIT_INTR:
1599 case SVM_EXIT_NMI:
1600 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001601 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01001602 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001603 if (npt_enabled)
1604 return NESTED_EXIT_HOST;
1605 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001606 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Joerg Roedele0231712010-02-24 18:59:10 +01001607 /* When we're shadowing, trap PFs */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001608 if (!npt_enabled)
1609 return NESTED_EXIT_HOST;
1610 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001611 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
1612 nm_interception(svm);
1613 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001614 default:
1615 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001616 }
1617
Joerg Roedel410e4d52009-08-07 11:49:44 +02001618 return NESTED_EXIT_CONTINUE;
1619}
1620
1621/*
1622 * If this function returns true, this #vmexit was already handled
1623 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001624static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001625{
1626 u32 exit_code = svm->vmcb->control.exit_code;
1627 int vmexit = NESTED_EXIT_HOST;
1628
Alexander Grafcf74a782008-11-25 20:17:08 +01001629 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001630 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001631 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001632 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001633 case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR8: {
1634 u32 cr_bits = 1 << (exit_code - SVM_EXIT_READ_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001635 if (svm->nested.intercept_cr_read & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001636 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001637 break;
1638 }
1639 case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR8: {
1640 u32 cr_bits = 1 << (exit_code - SVM_EXIT_WRITE_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001641 if (svm->nested.intercept_cr_write & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001642 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001643 break;
1644 }
1645 case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR7: {
1646 u32 dr_bits = 1 << (exit_code - SVM_EXIT_READ_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001647 if (svm->nested.intercept_dr_read & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001648 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001649 break;
1650 }
1651 case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR7: {
1652 u32 dr_bits = 1 << (exit_code - SVM_EXIT_WRITE_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001653 if (svm->nested.intercept_dr_write & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001654 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001655 break;
1656 }
1657 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1658 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001659 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001660 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001661 break;
1662 }
1663 default: {
1664 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001665 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001666 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001667 }
1668 }
1669
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001670 return vmexit;
1671}
1672
1673static int nested_svm_exit_handled(struct vcpu_svm *svm)
1674{
1675 int vmexit;
1676
1677 vmexit = nested_svm_intercept(svm);
1678
1679 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001680 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001681
1682 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001683}
1684
Joerg Roedel0460a972009-08-07 11:49:31 +02001685static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
1686{
1687 struct vmcb_control_area *dst = &dst_vmcb->control;
1688 struct vmcb_control_area *from = &from_vmcb->control;
1689
1690 dst->intercept_cr_read = from->intercept_cr_read;
1691 dst->intercept_cr_write = from->intercept_cr_write;
1692 dst->intercept_dr_read = from->intercept_dr_read;
1693 dst->intercept_dr_write = from->intercept_dr_write;
1694 dst->intercept_exceptions = from->intercept_exceptions;
1695 dst->intercept = from->intercept;
1696 dst->iopm_base_pa = from->iopm_base_pa;
1697 dst->msrpm_base_pa = from->msrpm_base_pa;
1698 dst->tsc_offset = from->tsc_offset;
1699 dst->asid = from->asid;
1700 dst->tlb_ctl = from->tlb_ctl;
1701 dst->int_ctl = from->int_ctl;
1702 dst->int_vector = from->int_vector;
1703 dst->int_state = from->int_state;
1704 dst->exit_code = from->exit_code;
1705 dst->exit_code_hi = from->exit_code_hi;
1706 dst->exit_info_1 = from->exit_info_1;
1707 dst->exit_info_2 = from->exit_info_2;
1708 dst->exit_int_info = from->exit_int_info;
1709 dst->exit_int_info_err = from->exit_int_info_err;
1710 dst->nested_ctl = from->nested_ctl;
1711 dst->event_inj = from->event_inj;
1712 dst->event_inj_err = from->event_inj_err;
1713 dst->nested_cr3 = from->nested_cr3;
1714 dst->lbr_ctl = from->lbr_ctl;
1715}
1716
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001717static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001718{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001719 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001720 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02001721 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01001722 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01001723
Joerg Roedel17897f32009-10-09 16:08:29 +02001724 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
1725 vmcb->control.exit_info_1,
1726 vmcb->control.exit_info_2,
1727 vmcb->control.exit_int_info,
1728 vmcb->control.exit_int_info_err);
1729
Joerg Roedel7597f122010-02-19 16:23:00 +01001730 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001731 if (!nested_vmcb)
1732 return 1;
1733
Joerg Roedel06fc77722010-02-19 16:23:07 +01001734 /* Exit nested SVM mode */
1735 svm->nested.vmcb = 0;
1736
Alexander Grafcf74a782008-11-25 20:17:08 +01001737 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02001738 disable_gif(svm);
1739
1740 nested_vmcb->save.es = vmcb->save.es;
1741 nested_vmcb->save.cs = vmcb->save.cs;
1742 nested_vmcb->save.ss = vmcb->save.ss;
1743 nested_vmcb->save.ds = vmcb->save.ds;
1744 nested_vmcb->save.gdtr = vmcb->save.gdtr;
1745 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001746 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02001747 if (npt_enabled)
1748 nested_vmcb->save.cr3 = vmcb->save.cr3;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001749 else
1750 nested_vmcb->save.cr3 = svm->vcpu.arch.cr3;
Joerg Roedel33740e42009-08-07 11:49:29 +02001751 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001752 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Joerg Roedel33740e42009-08-07 11:49:29 +02001753 nested_vmcb->save.rflags = vmcb->save.rflags;
1754 nested_vmcb->save.rip = vmcb->save.rip;
1755 nested_vmcb->save.rsp = vmcb->save.rsp;
1756 nested_vmcb->save.rax = vmcb->save.rax;
1757 nested_vmcb->save.dr7 = vmcb->save.dr7;
1758 nested_vmcb->save.dr6 = vmcb->save.dr6;
1759 nested_vmcb->save.cpl = vmcb->save.cpl;
1760
1761 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
1762 nested_vmcb->control.int_vector = vmcb->control.int_vector;
1763 nested_vmcb->control.int_state = vmcb->control.int_state;
1764 nested_vmcb->control.exit_code = vmcb->control.exit_code;
1765 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
1766 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
1767 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
1768 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
1769 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Alexander Graf8d23c462009-10-09 16:08:25 +02001770
1771 /*
1772 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
1773 * to make sure that we do not lose injected events. So check event_inj
1774 * here and copy it to exit_int_info if it is valid.
1775 * Exit_int_info and event_inj can't be both valid because the case
1776 * below only happens on a VMRUN instruction intercept which has
1777 * no valid exit_int_info set.
1778 */
1779 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
1780 struct vmcb_control_area *nc = &nested_vmcb->control;
1781
1782 nc->exit_int_info = vmcb->control.event_inj;
1783 nc->exit_int_info_err = vmcb->control.event_inj_err;
1784 }
1785
Joerg Roedel33740e42009-08-07 11:49:29 +02001786 nested_vmcb->control.tlb_ctl = 0;
1787 nested_vmcb->control.event_inj = 0;
1788 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001789
1790 /* We always set V_INTR_MASKING and remember the old value in hflags */
1791 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
1792 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
1793
Alexander Grafcf74a782008-11-25 20:17:08 +01001794 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02001795 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01001796
Alexander Graf219b65d2009-06-15 15:21:25 +02001797 kvm_clear_exception_queue(&svm->vcpu);
1798 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01001799
1800 /* Restore selected save entries */
1801 svm->vmcb->save.es = hsave->save.es;
1802 svm->vmcb->save.cs = hsave->save.cs;
1803 svm->vmcb->save.ss = hsave->save.ss;
1804 svm->vmcb->save.ds = hsave->save.ds;
1805 svm->vmcb->save.gdtr = hsave->save.gdtr;
1806 svm->vmcb->save.idtr = hsave->save.idtr;
1807 svm->vmcb->save.rflags = hsave->save.rflags;
1808 svm_set_efer(&svm->vcpu, hsave->save.efer);
1809 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
1810 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
1811 if (npt_enabled) {
1812 svm->vmcb->save.cr3 = hsave->save.cr3;
1813 svm->vcpu.arch.cr3 = hsave->save.cr3;
1814 } else {
1815 kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
1816 }
1817 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
1818 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
1819 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
1820 svm->vmcb->save.dr7 = 0;
1821 svm->vmcb->save.cpl = 0;
1822 svm->vmcb->control.exit_int_info = 0;
1823
Joerg Roedel7597f122010-02-19 16:23:00 +01001824 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01001825
1826 kvm_mmu_reset_context(&svm->vcpu);
1827 kvm_mmu_load(&svm->vcpu);
1828
1829 return 0;
1830}
Alexander Graf3d6368e2008-11-25 20:17:07 +01001831
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001832static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01001833{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001834 u32 *nested_msrpm;
Joerg Roedel7597f122010-02-19 16:23:00 +01001835 struct page *page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001836 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001837
Joerg Roedel7597f122010-02-19 16:23:00 +01001838 nested_msrpm = nested_svm_map(svm, svm->nested.vmcb_msrpm, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001839 if (!nested_msrpm)
1840 return false;
1841
Joerg Roedele0231712010-02-24 18:59:10 +01001842 for (i = 0; i < PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER) / 4; i++)
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001843 svm->nested.msrpm[i] = svm->msrpm[i] | nested_msrpm[i];
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001844
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001845 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001846
Joerg Roedel7597f122010-02-19 16:23:00 +01001847 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001848
1849 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001850}
1851
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001852static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01001853{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001854 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001855 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02001856 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01001857 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01001858 u64 vmcb_gpa;
1859
1860 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001861
Joerg Roedel7597f122010-02-19 16:23:00 +01001862 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001863 if (!nested_vmcb)
1864 return false;
1865
Joerg Roedelecf14052010-02-24 18:59:13 +01001866 trace_kvm_nested_vmrun(svm->vmcb->save.rip - 3, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02001867 nested_vmcb->save.rip,
1868 nested_vmcb->control.int_ctl,
1869 nested_vmcb->control.event_inj,
1870 nested_vmcb->control.nested_ctl);
1871
Joerg Roedel2e554e82010-02-24 18:59:14 +01001872 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr_read,
1873 nested_vmcb->control.intercept_cr_write,
1874 nested_vmcb->control.intercept_exceptions,
1875 nested_vmcb->control.intercept);
1876
Alexander Graf3d6368e2008-11-25 20:17:07 +01001877 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02001878 kvm_clear_exception_queue(&svm->vcpu);
1879 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001880
Joerg Roedele0231712010-02-24 18:59:10 +01001881 /*
1882 * Save the old vmcb, so we don't need to pick what we save, but can
1883 * restore everything when a VMEXIT occurs
1884 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02001885 hsave->save.es = vmcb->save.es;
1886 hsave->save.cs = vmcb->save.cs;
1887 hsave->save.ss = vmcb->save.ss;
1888 hsave->save.ds = vmcb->save.ds;
1889 hsave->save.gdtr = vmcb->save.gdtr;
1890 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02001891 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02001892 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02001893 hsave->save.cr4 = svm->vcpu.arch.cr4;
1894 hsave->save.rflags = vmcb->save.rflags;
1895 hsave->save.rip = svm->next_rip;
1896 hsave->save.rsp = vmcb->save.rsp;
1897 hsave->save.rax = vmcb->save.rax;
1898 if (npt_enabled)
1899 hsave->save.cr3 = vmcb->save.cr3;
1900 else
1901 hsave->save.cr3 = svm->vcpu.arch.cr3;
1902
Joerg Roedel0460a972009-08-07 11:49:31 +02001903 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001904
1905 if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
1906 svm->vcpu.arch.hflags |= HF_HIF_MASK;
1907 else
1908 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
1909
1910 /* Load the nested guest state */
1911 svm->vmcb->save.es = nested_vmcb->save.es;
1912 svm->vmcb->save.cs = nested_vmcb->save.cs;
1913 svm->vmcb->save.ss = nested_vmcb->save.ss;
1914 svm->vmcb->save.ds = nested_vmcb->save.ds;
1915 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
1916 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
1917 svm->vmcb->save.rflags = nested_vmcb->save.rflags;
1918 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
1919 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
1920 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
1921 if (npt_enabled) {
1922 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
1923 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01001924 } else
Alexander Graf3d6368e2008-11-25 20:17:07 +01001925 kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01001926
1927 /* Guest paging mode is active - reset mmu */
1928 kvm_mmu_reset_context(&svm->vcpu);
1929
Joerg Roedeldefbba52009-08-07 11:49:30 +02001930 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001931 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
1932 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
1933 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01001934
Alexander Graf3d6368e2008-11-25 20:17:07 +01001935 /* In case we don't even reach vcpu_run, the fields are not updated */
1936 svm->vmcb->save.rax = nested_vmcb->save.rax;
1937 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
1938 svm->vmcb->save.rip = nested_vmcb->save.rip;
1939 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
1940 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
1941 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
1942
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001943 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001944
Joerg Roedelaad42c62009-08-07 11:49:34 +02001945 /* cache intercepts */
1946 svm->nested.intercept_cr_read = nested_vmcb->control.intercept_cr_read;
1947 svm->nested.intercept_cr_write = nested_vmcb->control.intercept_cr_write;
1948 svm->nested.intercept_dr_read = nested_vmcb->control.intercept_dr_read;
1949 svm->nested.intercept_dr_write = nested_vmcb->control.intercept_dr_write;
1950 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
1951 svm->nested.intercept = nested_vmcb->control.intercept;
1952
Alexander Graf3d6368e2008-11-25 20:17:07 +01001953 force_new_asid(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001954 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001955 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
1956 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
1957 else
1958 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
1959
Joerg Roedel88ab24a2010-02-19 16:23:06 +01001960 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
1961 /* We only want the cr8 intercept bits of the guest */
1962 svm->vmcb->control.intercept_cr_read &= ~INTERCEPT_CR8_MASK;
1963 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
1964 }
1965
Joerg Roedele0231712010-02-24 18:59:10 +01001966 /*
1967 * We don't want a nested guest to be more powerful than the guest, so
1968 * all intercepts are ORed
1969 */
Joerg Roedel88ab24a2010-02-19 16:23:06 +01001970 svm->vmcb->control.intercept_cr_read |=
1971 nested_vmcb->control.intercept_cr_read;
1972 svm->vmcb->control.intercept_cr_write |=
1973 nested_vmcb->control.intercept_cr_write;
1974 svm->vmcb->control.intercept_dr_read |=
1975 nested_vmcb->control.intercept_dr_read;
1976 svm->vmcb->control.intercept_dr_write |=
1977 nested_vmcb->control.intercept_dr_write;
1978 svm->vmcb->control.intercept_exceptions |=
1979 nested_vmcb->control.intercept_exceptions;
1980
1981 svm->vmcb->control.intercept |= nested_vmcb->control.intercept;
1982
1983 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001984 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
1985 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
1986 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001987 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
1988 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
1989
Joerg Roedel7597f122010-02-19 16:23:00 +01001990 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001991
Joerg Roedel06fc77722010-02-19 16:23:07 +01001992 /* nested_vmcb is our indicator if nested SVM is activated */
1993 svm->nested.vmcb = vmcb_gpa;
1994
Joerg Roedel2af91942009-08-07 11:49:28 +02001995 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001996
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001997 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001998}
1999
Joerg Roedel9966bf62009-08-07 11:49:40 +02002000static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002001{
2002 to_vmcb->save.fs = from_vmcb->save.fs;
2003 to_vmcb->save.gs = from_vmcb->save.gs;
2004 to_vmcb->save.tr = from_vmcb->save.tr;
2005 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2006 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2007 to_vmcb->save.star = from_vmcb->save.star;
2008 to_vmcb->save.lstar = from_vmcb->save.lstar;
2009 to_vmcb->save.cstar = from_vmcb->save.cstar;
2010 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2011 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2012 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2013 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002014}
2015
Avi Kivity851ba692009-08-24 11:10:17 +03002016static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002017{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002018 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002019 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002020
Alexander Graf55426752008-11-25 20:17:06 +01002021 if (nested_svm_check_permissions(svm))
2022 return 1;
2023
2024 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2025 skip_emulated_instruction(&svm->vcpu);
2026
Joerg Roedel7597f122010-02-19 16:23:00 +01002027 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002028 if (!nested_vmcb)
2029 return 1;
2030
2031 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002032 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002033
2034 return 1;
2035}
2036
Avi Kivity851ba692009-08-24 11:10:17 +03002037static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002038{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002039 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002040 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002041
Alexander Graf55426752008-11-25 20:17:06 +01002042 if (nested_svm_check_permissions(svm))
2043 return 1;
2044
2045 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2046 skip_emulated_instruction(&svm->vcpu);
2047
Joerg Roedel7597f122010-02-19 16:23:00 +01002048 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002049 if (!nested_vmcb)
2050 return 1;
2051
2052 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002053 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002054
2055 return 1;
2056}
2057
Avi Kivity851ba692009-08-24 11:10:17 +03002058static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002059{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002060 if (nested_svm_check_permissions(svm))
2061 return 1;
2062
2063 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2064 skip_emulated_instruction(&svm->vcpu);
2065
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002066 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002067 return 1;
2068
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002069 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002070 goto failed;
2071
2072 return 1;
2073
2074failed:
2075
2076 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2077 svm->vmcb->control.exit_code_hi = 0;
2078 svm->vmcb->control.exit_info_1 = 0;
2079 svm->vmcb->control.exit_info_2 = 0;
2080
2081 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002082
2083 return 1;
2084}
2085
Avi Kivity851ba692009-08-24 11:10:17 +03002086static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002087{
2088 if (nested_svm_check_permissions(svm))
2089 return 1;
2090
2091 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2092 skip_emulated_instruction(&svm->vcpu);
2093
Joerg Roedel2af91942009-08-07 11:49:28 +02002094 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002095
2096 return 1;
2097}
2098
Avi Kivity851ba692009-08-24 11:10:17 +03002099static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002100{
2101 if (nested_svm_check_permissions(svm))
2102 return 1;
2103
2104 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2105 skip_emulated_instruction(&svm->vcpu);
2106
Joerg Roedel2af91942009-08-07 11:49:28 +02002107 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002108
2109 /* After a CLGI no interrupts should come */
2110 svm_clear_vintr(svm);
2111 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2112
2113 return 1;
2114}
2115
Avi Kivity851ba692009-08-24 11:10:17 +03002116static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002117{
2118 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002119
Joerg Roedelec1ff792009-10-09 16:08:31 +02002120 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
2121 vcpu->arch.regs[VCPU_REGS_RAX]);
2122
Alexander Grafff092382009-06-15 15:21:24 +02002123 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2124 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
2125
2126 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2127 skip_emulated_instruction(&svm->vcpu);
2128 return 1;
2129}
2130
Joerg Roedel532a46b2009-10-09 16:08:32 +02002131static int skinit_interception(struct vcpu_svm *svm)
2132{
2133 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
2134
2135 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2136 return 1;
2137}
2138
Avi Kivity851ba692009-08-24 11:10:17 +03002139static int invalid_op_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002140{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002141 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002142 return 1;
2143}
2144
Avi Kivity851ba692009-08-24 11:10:17 +03002145static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002146{
Izik Eidus37817f22008-03-24 23:14:53 +02002147 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002148 int reason;
2149 int int_type = svm->vmcb->control.exit_int_info &
2150 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002151 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002152 uint32_t type =
2153 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2154 uint32_t idt_v =
2155 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Izik Eidus37817f22008-03-24 23:14:53 +02002156
2157 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002158
Izik Eidus37817f22008-03-24 23:14:53 +02002159 if (svm->vmcb->control.exit_info_2 &
2160 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002161 reason = TASK_SWITCH_IRET;
2162 else if (svm->vmcb->control.exit_info_2 &
2163 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2164 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002165 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002166 reason = TASK_SWITCH_GATE;
2167 else
2168 reason = TASK_SWITCH_CALL;
2169
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002170 if (reason == TASK_SWITCH_GATE) {
2171 switch (type) {
2172 case SVM_EXITINTINFO_TYPE_NMI:
2173 svm->vcpu.arch.nmi_injected = false;
2174 break;
2175 case SVM_EXITINTINFO_TYPE_EXEPT:
2176 kvm_clear_exception_queue(&svm->vcpu);
2177 break;
2178 case SVM_EXITINTINFO_TYPE_INTR:
2179 kvm_clear_interrupt_queue(&svm->vcpu);
2180 break;
2181 default:
2182 break;
2183 }
2184 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002185
Gleb Natapov8317c292009-04-12 13:37:02 +03002186 if (reason != TASK_SWITCH_GATE ||
2187 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2188 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002189 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2190 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002191
2192 return kvm_task_switch(&svm->vcpu, tss_selector, reason);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002193}
2194
Avi Kivity851ba692009-08-24 11:10:17 +03002195static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002196{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002197 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002198 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002199 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002200}
2201
Avi Kivity851ba692009-08-24 11:10:17 +03002202static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002203{
2204 ++svm->vcpu.stat.nmi_window_exits;
2205 svm->vmcb->control.intercept &= ~(1UL << INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002206 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002207 return 1;
2208}
2209
Avi Kivity851ba692009-08-24 11:10:17 +03002210static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002211{
Avi Kivity851ba692009-08-24 11:10:17 +03002212 if (emulate_instruction(&svm->vcpu, 0, 0, 0) != EMULATE_DONE)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002213 pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
2214 return 1;
2215}
2216
Avi Kivity851ba692009-08-24 11:10:17 +03002217static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002218{
Avi Kivity851ba692009-08-24 11:10:17 +03002219 if (emulate_instruction(&svm->vcpu, 0, 0, 0) != EMULATE_DONE)
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002220 pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002221 return 1;
2222}
2223
Avi Kivity851ba692009-08-24 11:10:17 +03002224static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01002225{
Avi Kivity851ba692009-08-24 11:10:17 +03002226 struct kvm_run *kvm_run = svm->vcpu.run;
2227
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002228 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
2229 /* instruction emulation calls kvm_set_cr8() */
Avi Kivity851ba692009-08-24 11:10:17 +03002230 emulate_instruction(&svm->vcpu, 0, 0, 0);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002231 if (irqchip_in_kernel(svm->vcpu.kvm)) {
2232 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
Joerg Roedel1d075432007-12-06 21:02:25 +01002233 return 1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002234 }
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002235 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
2236 return 1;
Joerg Roedel1d075432007-12-06 21:02:25 +01002237 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2238 return 0;
2239}
2240
Avi Kivity6aa8b732006-12-10 02:21:36 -08002241static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
2242{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002243 struct vcpu_svm *svm = to_svm(vcpu);
2244
Avi Kivity6aa8b732006-12-10 02:21:36 -08002245 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302246 case MSR_IA32_TSC: {
Joerg Roedel20824f32009-09-16 15:24:18 +02002247 u64 tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002248
Joerg Roedel20824f32009-09-16 15:24:18 +02002249 if (is_nested(svm))
2250 tsc_offset = svm->nested.hsave->control.tsc_offset;
2251 else
2252 tsc_offset = svm->vmcb->control.tsc_offset;
2253
2254 *data = tsc_offset + native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002255 break;
2256 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08002257 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002258 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002259 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08002260#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002261 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002262 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002263 break;
2264 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002265 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002266 break;
2267 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002268 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002269 break;
2270 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002271 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002272 break;
2273#endif
2274 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002275 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002276 break;
2277 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002278 *data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002279 break;
2280 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002281 *data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002282 break;
Joerg Roedele0231712010-02-24 18:59:10 +01002283 /*
2284 * Nobody will change the following 5 values in the VMCB so we can
2285 * safely return them on rdmsr. They will always be 0 until LBRV is
2286 * implemented.
2287 */
Joerg Roedela2938c82008-02-13 16:30:28 +01002288 case MSR_IA32_DEBUGCTLMSR:
2289 *data = svm->vmcb->save.dbgctl;
2290 break;
2291 case MSR_IA32_LASTBRANCHFROMIP:
2292 *data = svm->vmcb->save.br_from;
2293 break;
2294 case MSR_IA32_LASTBRANCHTOIP:
2295 *data = svm->vmcb->save.br_to;
2296 break;
2297 case MSR_IA32_LASTINTFROMIP:
2298 *data = svm->vmcb->save.last_excp_from;
2299 break;
2300 case MSR_IA32_LASTINTTOIP:
2301 *data = svm->vmcb->save.last_excp_to;
2302 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002303 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002304 *data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002305 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002306 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002307 *data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002308 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01002309 case MSR_IA32_UCODE_REV:
2310 *data = 0x01000065;
2311 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002312 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002313 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002314 }
2315 return 0;
2316}
2317
Avi Kivity851ba692009-08-24 11:10:17 +03002318static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002319{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002320 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321 u64 data;
2322
Avi Kivity59200272010-01-25 19:47:02 +02002323 if (svm_get_msr(&svm->vcpu, ecx, &data)) {
2324 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002325 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002326 } else {
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002327 trace_kvm_msr_read(ecx, data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002328
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002329 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002330 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002331 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002332 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333 }
2334 return 1;
2335}
2336
Joerg Roedel4a810182010-02-24 18:59:15 +01002337static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2338{
2339 struct vcpu_svm *svm = to_svm(vcpu);
2340 int svm_dis, chg_mask;
2341
2342 if (data & ~SVM_VM_CR_VALID_MASK)
2343 return 1;
2344
2345 chg_mask = SVM_VM_CR_VALID_MASK;
2346
2347 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2348 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2349
2350 svm->nested.vm_cr_msr &= ~chg_mask;
2351 svm->nested.vm_cr_msr |= (data & chg_mask);
2352
2353 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2354
2355 /* check for svm_disable while efer.svme is set */
2356 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2357 return 1;
2358
2359 return 0;
2360}
2361
Avi Kivity6aa8b732006-12-10 02:21:36 -08002362static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
2363{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002364 struct vcpu_svm *svm = to_svm(vcpu);
2365
Avi Kivity6aa8b732006-12-10 02:21:36 -08002366 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302367 case MSR_IA32_TSC: {
Joerg Roedel20824f32009-09-16 15:24:18 +02002368 u64 tsc_offset = data - native_read_tsc();
2369 u64 g_tsc_offset = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002370
Joerg Roedel20824f32009-09-16 15:24:18 +02002371 if (is_nested(svm)) {
2372 g_tsc_offset = svm->vmcb->control.tsc_offset -
2373 svm->nested.hsave->control.tsc_offset;
2374 svm->nested.hsave->control.tsc_offset = tsc_offset;
2375 }
2376
2377 svm->vmcb->control.tsc_offset = tsc_offset + g_tsc_offset;
2378
Avi Kivity6aa8b732006-12-10 02:21:36 -08002379 break;
2380 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08002381 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002382 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002383 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08002384#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002385 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002386 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002387 break;
2388 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002389 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002390 break;
2391 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002392 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002393 break;
2394 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002395 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002396 break;
2397#endif
2398 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002399 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002400 break;
2401 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002402 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002403 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002404 break;
2405 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002406 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002407 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002408 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002409 case MSR_IA32_DEBUGCTLMSR:
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002410 if (!svm_has(SVM_FEATURE_LBRV)) {
2411 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002412 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002413 break;
2414 }
2415 if (data & DEBUGCTL_RESERVED_BITS)
2416 return 1;
2417
2418 svm->vmcb->save.dbgctl = data;
2419 if (data & (1ULL<<0))
2420 svm_enable_lbrv(svm);
2421 else
2422 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01002423 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002424 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002425 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002426 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002427 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002428 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002429 case MSR_VM_IGNNE:
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002430 pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
2431 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002432 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002433 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002434 }
2435 return 0;
2436}
2437
Avi Kivity851ba692009-08-24 11:10:17 +03002438static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002439{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002440 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002441 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002442 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002443
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002444
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002445 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Avi Kivity59200272010-01-25 19:47:02 +02002446 if (svm_set_msr(&svm->vcpu, ecx, data)) {
2447 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002448 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002449 } else {
2450 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10002451 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02002452 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002453 return 1;
2454}
2455
Avi Kivity851ba692009-08-24 11:10:17 +03002456static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002457{
Rusty Russelle756fc62007-07-30 20:07:08 +10002458 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03002459 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002460 else
Avi Kivity851ba692009-08-24 11:10:17 +03002461 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002462}
2463
Avi Kivity851ba692009-08-24 11:10:17 +03002464static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08002465{
Avi Kivity851ba692009-08-24 11:10:17 +03002466 struct kvm_run *kvm_run = svm->vcpu.run;
2467
Alexander Graff0b85052008-11-25 20:17:01 +01002468 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03002469 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -08002470 /*
2471 * If the user space waits to inject interrupts, exit as soon as
2472 * possible
2473 */
Gleb Natapov80618232009-04-21 17:44:56 +03002474 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
2475 kvm_run->request_interrupt_window &&
2476 !kvm_cpu_has_interrupt(&svm->vcpu)) {
Rusty Russelle756fc62007-07-30 20:07:08 +10002477 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002478 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
2479 return 0;
2480 }
2481
2482 return 1;
2483}
2484
Mark Langsdorf565d0992009-10-06 14:25:02 -05002485static int pause_interception(struct vcpu_svm *svm)
2486{
2487 kvm_vcpu_on_spin(&(svm->vcpu));
2488 return 1;
2489}
2490
Avi Kivity851ba692009-08-24 11:10:17 +03002491static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
Joerg Roedele0231712010-02-24 18:59:10 +01002492 [SVM_EXIT_READ_CR0] = emulate_on_interception,
2493 [SVM_EXIT_READ_CR3] = emulate_on_interception,
2494 [SVM_EXIT_READ_CR4] = emulate_on_interception,
2495 [SVM_EXIT_READ_CR8] = emulate_on_interception,
Avi Kivityd2251572010-01-06 10:55:27 +02002496 [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002497 [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
2498 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
2499 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
2500 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
2501 [SVM_EXIT_READ_DR0] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002502 [SVM_EXIT_READ_DR1] = emulate_on_interception,
2503 [SVM_EXIT_READ_DR2] = emulate_on_interception,
2504 [SVM_EXIT_READ_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002505 [SVM_EXIT_READ_DR4] = emulate_on_interception,
2506 [SVM_EXIT_READ_DR5] = emulate_on_interception,
2507 [SVM_EXIT_READ_DR6] = emulate_on_interception,
2508 [SVM_EXIT_READ_DR7] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002509 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
2510 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
2511 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
2512 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002513 [SVM_EXIT_WRITE_DR4] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002514 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002515 [SVM_EXIT_WRITE_DR6] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002516 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002517 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
2518 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002519 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002520 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
2521 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
2522 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
2523 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02002524 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002525 [SVM_EXIT_SMI] = nop_on_interception,
2526 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08002527 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002528 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002529 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002530 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05002531 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002532 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03002533 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02002534 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002535 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002536 [SVM_EXIT_MSR] = msr_interception,
2537 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002538 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01002539 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02002540 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01002541 [SVM_EXIT_VMLOAD] = vmload_interception,
2542 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01002543 [SVM_EXIT_STGI] = stgi_interception,
2544 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02002545 [SVM_EXIT_SKINIT] = skinit_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002546 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01002547 [SVM_EXIT_MONITOR] = invalid_op_interception,
2548 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002549 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002550};
2551
Avi Kivity851ba692009-08-24 11:10:17 +03002552static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002553{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002554 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002555 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002556 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002557
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002558 trace_kvm_exit(exit_code, svm->vmcb->save.rip);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002559
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002560 if (unlikely(svm->nested.exit_required)) {
2561 nested_svm_vmexit(svm);
2562 svm->nested.exit_required = false;
2563
2564 return 1;
2565 }
2566
Alexander Grafcf74a782008-11-25 20:17:08 +01002567 if (is_nested(svm)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02002568 int vmexit;
2569
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02002570 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
2571 svm->vmcb->control.exit_info_1,
2572 svm->vmcb->control.exit_info_2,
2573 svm->vmcb->control.exit_int_info,
2574 svm->vmcb->control.exit_int_info_err);
2575
Joerg Roedel410e4d52009-08-07 11:49:44 +02002576 vmexit = nested_svm_exit_special(svm);
2577
2578 if (vmexit == NESTED_EXIT_CONTINUE)
2579 vmexit = nested_svm_exit_handled(svm);
2580
2581 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01002582 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01002583 }
2584
Joerg Roedela5c38322009-08-07 11:49:32 +02002585 svm_complete_interrupts(svm);
2586
Avi Kivity888f9f32010-01-10 12:14:04 +02002587 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR0_MASK))
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002588 vcpu->arch.cr0 = svm->vmcb->save.cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02002589 if (npt_enabled)
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002590 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002591
2592 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
2593 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2594 kvm_run->fail_entry.hardware_entry_failure_reason
2595 = svm->vmcb->control.exit_code;
2596 return 0;
2597 }
2598
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002599 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002600 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002601 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002602 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
2603 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002604 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002605 exit_code);
2606
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02002607 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08002608 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002609 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03002610 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002611 return 0;
2612 }
2613
Avi Kivity851ba692009-08-24 11:10:17 +03002614 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002615}
2616
2617static void reload_tss(struct kvm_vcpu *vcpu)
2618{
2619 int cpu = raw_smp_processor_id();
2620
Tejun Heo0fe1e002009-10-29 22:34:14 +09002621 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
2622 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002623 load_TR_desc();
2624}
2625
Rusty Russelle756fc62007-07-30 20:07:08 +10002626static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002627{
2628 int cpu = raw_smp_processor_id();
2629
Tejun Heo0fe1e002009-10-29 22:34:14 +09002630 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002631
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002632 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03002633 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09002634 if (svm->asid_generation != sd->asid_generation)
2635 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002636}
2637
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002638static void svm_inject_nmi(struct kvm_vcpu *vcpu)
2639{
2640 struct vcpu_svm *svm = to_svm(vcpu);
2641
2642 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
2643 vcpu->arch.hflags |= HF_NMI_MASK;
2644 svm->vmcb->control.intercept |= (1UL << INTERCEPT_IRET);
2645 ++vcpu->stat.nmi_injections;
2646}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002647
Eddie Dong85f455f2007-07-06 12:20:49 +03002648static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002649{
2650 struct vmcb_control_area *control;
2651
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002652 trace_kvm_inj_virq(irq);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002653
Avi Kivityfa89a812008-09-01 15:57:51 +03002654 ++svm->vcpu.stat.irq_injections;
Rusty Russelle756fc62007-07-30 20:07:08 +10002655 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03002656 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002657 control->int_ctl &= ~V_INTR_PRIO_MASK;
2658 control->int_ctl |= V_IRQ_MASK |
2659 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
2660}
2661
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002662static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03002663{
2664 struct vcpu_svm *svm = to_svm(vcpu);
2665
Joerg Roedel2af91942009-08-07 11:49:28 +02002666 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01002667
Alexander Graf219b65d2009-06-15 15:21:25 +02002668 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
2669 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002670}
2671
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002672static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
2673{
2674 struct vcpu_svm *svm = to_svm(vcpu);
2675
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002676 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
2677 return;
2678
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002679 if (irr == -1)
2680 return;
2681
2682 if (tpr >= irr)
2683 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK;
2684}
2685
2686static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02002687{
2688 struct vcpu_svm *svm = to_svm(vcpu);
2689 struct vmcb *vmcb = svm->vmcb;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002690 return !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
2691 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
Joerg Roedelaaacfc92008-04-16 16:51:18 +02002692}
2693
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002694static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
2695{
2696 struct vcpu_svm *svm = to_svm(vcpu);
2697
2698 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
2699}
2700
2701static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
2702{
2703 struct vcpu_svm *svm = to_svm(vcpu);
2704
2705 if (masked) {
2706 svm->vcpu.arch.hflags |= HF_NMI_MASK;
2707 svm->vmcb->control.intercept |= (1UL << INTERCEPT_IRET);
2708 } else {
2709 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
2710 svm->vmcb->control.intercept &= ~(1UL << INTERCEPT_IRET);
2711 }
2712}
2713
Gleb Natapov78646122009-03-23 12:12:11 +02002714static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
2715{
2716 struct vcpu_svm *svm = to_svm(vcpu);
2717 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02002718 int ret;
2719
2720 if (!gif_set(svm) ||
2721 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
2722 return 0;
2723
2724 ret = !!(vmcb->save.rflags & X86_EFLAGS_IF);
2725
2726 if (is_nested(svm))
2727 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
2728
2729 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02002730}
2731
Gleb Natapov9222be12009-04-23 17:14:37 +03002732static void enable_irq_window(struct kvm_vcpu *vcpu)
2733{
Alexander Graf219b65d2009-06-15 15:21:25 +02002734 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02002735
Joerg Roedele0231712010-02-24 18:59:10 +01002736 /*
2737 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
2738 * 1, because that's a separate STGI/VMRUN intercept. The next time we
2739 * get that intercept, this function will be called again though and
2740 * we'll get the vintr intercept.
2741 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01002742 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02002743 svm_set_vintr(svm);
2744 svm_inject_irq(svm, 0x0);
2745 }
Gleb Natapov9222be12009-04-23 17:14:37 +03002746}
2747
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002748static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002749{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002750 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03002751
Gleb Natapov44c11432009-05-11 13:35:52 +03002752 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
2753 == HF_NMI_MASK)
2754 return; /* IRET will cause a vm exit */
2755
Joerg Roedele0231712010-02-24 18:59:10 +01002756 /*
2757 * Something prevents NMI from been injected. Single step over possible
2758 * problem (IRET or exception injection or interrupt shadow)
2759 */
Joerg Roedel887f5002010-02-24 18:59:12 +01002760 if (gif_set(svm) && nested_svm_nmi(svm)) {
2761 svm->nmi_singlestep = true;
2762 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
2763 update_db_intercept(vcpu);
2764 }
Eddie Dong85f455f2007-07-06 12:20:49 +03002765}
2766
Izik Eiduscbc94022007-10-25 00:29:55 +02002767static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
2768{
2769 return 0;
2770}
2771
Avi Kivityd9e368d2007-06-07 19:18:30 +03002772static void svm_flush_tlb(struct kvm_vcpu *vcpu)
2773{
2774 force_new_asid(vcpu);
2775}
2776
Avi Kivity04d2cc72007-09-10 18:10:54 +03002777static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
2778{
2779}
2780
Joerg Roedeld7bf8222008-04-16 16:51:17 +02002781static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
2782{
2783 struct vcpu_svm *svm = to_svm(vcpu);
2784
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002785 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
2786 return;
2787
Joerg Roedeld7bf8222008-04-16 16:51:17 +02002788 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) {
2789 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03002790 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02002791 }
2792}
2793
Joerg Roedel649d6862008-04-16 16:51:15 +02002794static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
2795{
2796 struct vcpu_svm *svm = to_svm(vcpu);
2797 u64 cr8;
2798
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002799 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
2800 return;
2801
Joerg Roedel649d6862008-04-16 16:51:15 +02002802 cr8 = kvm_get_cr8(vcpu);
2803 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
2804 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
2805}
2806
Gleb Natapov9222be12009-04-23 17:14:37 +03002807static void svm_complete_interrupts(struct vcpu_svm *svm)
2808{
2809 u8 vector;
2810 int type;
2811 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01002812 unsigned int3_injected = svm->int3_injected;
2813
2814 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03002815
Gleb Natapov44c11432009-05-11 13:35:52 +03002816 if (svm->vcpu.arch.hflags & HF_IRET_MASK)
2817 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
2818
Gleb Natapov9222be12009-04-23 17:14:37 +03002819 svm->vcpu.arch.nmi_injected = false;
2820 kvm_clear_exception_queue(&svm->vcpu);
2821 kvm_clear_interrupt_queue(&svm->vcpu);
2822
2823 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
2824 return;
2825
2826 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
2827 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
2828
2829 switch (type) {
2830 case SVM_EXITINTINFO_TYPE_NMI:
2831 svm->vcpu.arch.nmi_injected = true;
2832 break;
2833 case SVM_EXITINTINFO_TYPE_EXEPT:
Alexander Graf219b65d2009-06-15 15:21:25 +02002834 if (is_nested(svm))
2835 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01002836 /*
2837 * In case of software exceptions, do not reinject the vector,
2838 * but re-execute the instruction instead. Rewind RIP first
2839 * if we emulated INT3 before.
2840 */
2841 if (kvm_exception_is_soft(vector)) {
2842 if (vector == BP_VECTOR && int3_injected &&
2843 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
2844 kvm_rip_write(&svm->vcpu,
2845 kvm_rip_read(&svm->vcpu) -
2846 int3_injected);
Gleb Natapov9222be12009-04-23 17:14:37 +03002847 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01002848 }
Gleb Natapov9222be12009-04-23 17:14:37 +03002849 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
2850 u32 err = svm->vmcb->control.exit_int_info_err;
2851 kvm_queue_exception_e(&svm->vcpu, vector, err);
2852
2853 } else
2854 kvm_queue_exception(&svm->vcpu, vector);
2855 break;
2856 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002857 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03002858 break;
2859 default:
2860 break;
2861 }
2862}
2863
Avi Kivity80e31d42008-07-14 14:44:59 +03002864#ifdef CONFIG_X86_64
2865#define R "r"
2866#else
2867#define R "e"
2868#endif
2869
Avi Kivity851ba692009-08-24 11:10:17 +03002870static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002871{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002872 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002873 u16 fs_selector;
2874 u16 gs_selector;
2875 u16 ldt_selector;
Avi Kivityd9e368d2007-06-07 19:18:30 +03002876
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002877 /*
2878 * A vmexit emulation is required before the vcpu can be executed
2879 * again.
2880 */
2881 if (unlikely(svm->nested.exit_required))
2882 return;
2883
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002884 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
2885 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
2886 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
2887
Rusty Russelle756fc62007-07-30 20:07:08 +10002888 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002889
Joerg Roedel649d6862008-04-16 16:51:15 +02002890 sync_lapic_to_cr8(vcpu);
2891
Avi Kivity6aa8b732006-12-10 02:21:36 -08002892 save_host_msrs(vcpu);
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002893 fs_selector = kvm_read_fs();
2894 gs_selector = kvm_read_gs();
2895 ldt_selector = kvm_read_ldt();
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02002896 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002897 /* required for live migration with NPT */
2898 if (npt_enabled)
2899 svm->vmcb->save.cr3 = vcpu->arch.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002900
Avi Kivity04d2cc72007-09-10 18:10:54 +03002901 clgi();
2902
2903 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08002904
Avi Kivity6aa8b732006-12-10 02:21:36 -08002905 asm volatile (
Avi Kivity80e31d42008-07-14 14:44:59 +03002906 "push %%"R"bp; \n\t"
2907 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
2908 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
2909 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
2910 "mov %c[rsi](%[svm]), %%"R"si \n\t"
2911 "mov %c[rdi](%[svm]), %%"R"di \n\t"
2912 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002913#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002914 "mov %c[r8](%[svm]), %%r8 \n\t"
2915 "mov %c[r9](%[svm]), %%r9 \n\t"
2916 "mov %c[r10](%[svm]), %%r10 \n\t"
2917 "mov %c[r11](%[svm]), %%r11 \n\t"
2918 "mov %c[r12](%[svm]), %%r12 \n\t"
2919 "mov %c[r13](%[svm]), %%r13 \n\t"
2920 "mov %c[r14](%[svm]), %%r14 \n\t"
2921 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002922#endif
2923
Avi Kivity6aa8b732006-12-10 02:21:36 -08002924 /* Enter guest mode */
Avi Kivity80e31d42008-07-14 14:44:59 +03002925 "push %%"R"ax \n\t"
2926 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002927 __ex(SVM_VMLOAD) "\n\t"
2928 __ex(SVM_VMRUN) "\n\t"
2929 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity80e31d42008-07-14 14:44:59 +03002930 "pop %%"R"ax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002931
2932 /* Save guest registers, load host registers */
Avi Kivity80e31d42008-07-14 14:44:59 +03002933 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
2934 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
2935 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
2936 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
2937 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
2938 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002939#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002940 "mov %%r8, %c[r8](%[svm]) \n\t"
2941 "mov %%r9, %c[r9](%[svm]) \n\t"
2942 "mov %%r10, %c[r10](%[svm]) \n\t"
2943 "mov %%r11, %c[r11](%[svm]) \n\t"
2944 "mov %%r12, %c[r12](%[svm]) \n\t"
2945 "mov %%r13, %c[r13](%[svm]) \n\t"
2946 "mov %%r14, %c[r14](%[svm]) \n\t"
2947 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002948#endif
Avi Kivity80e31d42008-07-14 14:44:59 +03002949 "pop %%"R"bp"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002950 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002951 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08002952 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002953 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
2954 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
2955 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
2956 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
2957 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
2958 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002959#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002960 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
2961 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
2962 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
2963 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
2964 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
2965 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
2966 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
2967 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002968#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02002969 : "cc", "memory"
Avi Kivity80e31d42008-07-14 14:44:59 +03002970 , R"bx", R"cx", R"dx", R"si", R"di"
Laurent Vivier54a08c02007-10-25 14:18:53 +02002971#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02002972 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
2973#endif
2974 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08002975
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002976 vcpu->arch.cr2 = svm->vmcb->save.cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002977 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
2978 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
2979 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002980
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002981 kvm_load_fs(fs_selector);
2982 kvm_load_gs(gs_selector);
2983 kvm_load_ldt(ldt_selector);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002984 load_host_msrs(vcpu);
2985
2986 reload_tss(vcpu);
2987
Avi Kivity56ba47d2007-11-07 17:14:18 +02002988 local_irq_disable();
2989
2990 stgi();
2991
Joerg Roedeld7bf8222008-04-16 16:51:17 +02002992 sync_cr8_to_lapic(vcpu);
2993
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002994 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03002995
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002996 if (npt_enabled) {
2997 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
2998 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
2999 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003000}
3001
Avi Kivity80e31d42008-07-14 14:44:59 +03003002#undef R
3003
Avi Kivity6aa8b732006-12-10 02:21:36 -08003004static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3005{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003006 struct vcpu_svm *svm = to_svm(vcpu);
3007
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003008 if (npt_enabled) {
3009 svm->vmcb->control.nested_cr3 = root;
3010 force_new_asid(vcpu);
3011 return;
3012 }
3013
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003014 svm->vmcb->save.cr3 = root;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003015 force_new_asid(vcpu);
3016}
3017
Avi Kivity6aa8b732006-12-10 02:21:36 -08003018static int is_disabled(void)
3019{
Joerg Roedel6031a612007-06-22 12:29:50 +03003020 u64 vm_cr;
3021
3022 rdmsrl(MSR_VM_CR, vm_cr);
3023 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3024 return 1;
3025
Avi Kivity6aa8b732006-12-10 02:21:36 -08003026 return 0;
3027}
3028
Ingo Molnar102d8322007-02-19 14:37:47 +02003029static void
3030svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3031{
3032 /*
3033 * Patch in the VMMCALL instruction:
3034 */
3035 hypercall[0] = 0x0f;
3036 hypercall[1] = 0x01;
3037 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003038}
3039
Yang, Sheng002c7f72007-07-31 14:23:01 +03003040static void svm_check_processor_compat(void *rtn)
3041{
3042 *(int *)rtn = 0;
3043}
3044
Avi Kivity774ead32007-12-26 13:57:04 +02003045static bool svm_cpu_has_accelerated_tpr(void)
3046{
3047 return false;
3048}
3049
Sheng Yang67253af2008-04-25 10:20:22 +08003050static int get_npt_level(void)
3051{
3052#ifdef CONFIG_X86_64
3053 return PT64_ROOT_LEVEL;
3054#else
3055 return PT32E_ROOT_LEVEL;
3056#endif
3057}
3058
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003059static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003060{
3061 return 0;
3062}
3063
Sheng Yang0e851882009-12-18 16:48:46 +08003064static void svm_cpuid_update(struct kvm_vcpu *vcpu)
3065{
3066}
3067
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003068static const struct trace_print_flags svm_exit_reasons_str[] = {
Joerg Roedele0231712010-02-24 18:59:10 +01003069 { SVM_EXIT_READ_CR0, "read_cr0" },
3070 { SVM_EXIT_READ_CR3, "read_cr3" },
3071 { SVM_EXIT_READ_CR4, "read_cr4" },
3072 { SVM_EXIT_READ_CR8, "read_cr8" },
3073 { SVM_EXIT_WRITE_CR0, "write_cr0" },
3074 { SVM_EXIT_WRITE_CR3, "write_cr3" },
3075 { SVM_EXIT_WRITE_CR4, "write_cr4" },
3076 { SVM_EXIT_WRITE_CR8, "write_cr8" },
3077 { SVM_EXIT_READ_DR0, "read_dr0" },
3078 { SVM_EXIT_READ_DR1, "read_dr1" },
3079 { SVM_EXIT_READ_DR2, "read_dr2" },
3080 { SVM_EXIT_READ_DR3, "read_dr3" },
3081 { SVM_EXIT_WRITE_DR0, "write_dr0" },
3082 { SVM_EXIT_WRITE_DR1, "write_dr1" },
3083 { SVM_EXIT_WRITE_DR2, "write_dr2" },
3084 { SVM_EXIT_WRITE_DR3, "write_dr3" },
3085 { SVM_EXIT_WRITE_DR5, "write_dr5" },
3086 { SVM_EXIT_WRITE_DR7, "write_dr7" },
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003087 { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
3088 { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
3089 { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
3090 { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
3091 { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
3092 { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
3093 { SVM_EXIT_INTR, "interrupt" },
3094 { SVM_EXIT_NMI, "nmi" },
3095 { SVM_EXIT_SMI, "smi" },
3096 { SVM_EXIT_INIT, "init" },
3097 { SVM_EXIT_VINTR, "vintr" },
3098 { SVM_EXIT_CPUID, "cpuid" },
3099 { SVM_EXIT_INVD, "invd" },
3100 { SVM_EXIT_HLT, "hlt" },
3101 { SVM_EXIT_INVLPG, "invlpg" },
3102 { SVM_EXIT_INVLPGA, "invlpga" },
3103 { SVM_EXIT_IOIO, "io" },
3104 { SVM_EXIT_MSR, "msr" },
3105 { SVM_EXIT_TASK_SWITCH, "task_switch" },
3106 { SVM_EXIT_SHUTDOWN, "shutdown" },
3107 { SVM_EXIT_VMRUN, "vmrun" },
3108 { SVM_EXIT_VMMCALL, "hypercall" },
3109 { SVM_EXIT_VMLOAD, "vmload" },
3110 { SVM_EXIT_VMSAVE, "vmsave" },
3111 { SVM_EXIT_STGI, "stgi" },
3112 { SVM_EXIT_CLGI, "clgi" },
3113 { SVM_EXIT_SKINIT, "skinit" },
3114 { SVM_EXIT_WBINVD, "wbinvd" },
3115 { SVM_EXIT_MONITOR, "monitor" },
3116 { SVM_EXIT_MWAIT, "mwait" },
3117 { SVM_EXIT_NPF, "npf" },
3118 { -1, NULL }
3119};
3120
Sheng Yang17cc3932010-01-05 19:02:27 +08003121static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02003122{
Sheng Yang17cc3932010-01-05 19:02:27 +08003123 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02003124}
3125
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003126static bool svm_rdtscp_supported(void)
3127{
3128 return false;
3129}
3130
Avi Kivity02daab22009-12-30 12:40:26 +02003131static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
3132{
3133 struct vcpu_svm *svm = to_svm(vcpu);
3134
Avi Kivity02daab22009-12-30 12:40:26 +02003135 svm->vmcb->control.intercept_exceptions |= 1 << NM_VECTOR;
Joerg Roedel66a562f2010-02-19 16:23:08 +01003136 if (is_nested(svm))
3137 svm->nested.hsave->control.intercept_exceptions |= 1 << NM_VECTOR;
3138 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02003139}
3140
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003141static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003142 .cpu_has_kvm_support = has_svm,
3143 .disabled_by_bios = is_disabled,
3144 .hardware_setup = svm_hardware_setup,
3145 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003146 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003147 .hardware_enable = svm_hardware_enable,
3148 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003149 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003150
3151 .vcpu_create = svm_create_vcpu,
3152 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003153 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003154
Avi Kivity04d2cc72007-09-10 18:10:54 +03003155 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003156 .vcpu_load = svm_vcpu_load,
3157 .vcpu_put = svm_vcpu_put,
3158
3159 .set_guest_debug = svm_guest_debug,
3160 .get_msr = svm_get_msr,
3161 .set_msr = svm_set_msr,
3162 .get_segment_base = svm_get_segment_base,
3163 .get_segment = svm_get_segment,
3164 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003165 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10003166 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02003167 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003168 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003169 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003170 .set_cr3 = svm_set_cr3,
3171 .set_cr4 = svm_set_cr4,
3172 .set_efer = svm_set_efer,
3173 .get_idt = svm_get_idt,
3174 .set_idt = svm_set_idt,
3175 .get_gdt = svm_get_gdt,
3176 .set_gdt = svm_set_gdt,
3177 .get_dr = svm_get_dr,
3178 .set_dr = svm_set_dr,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003179 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003180 .get_rflags = svm_get_rflags,
3181 .set_rflags = svm_set_rflags,
Avi Kivity6b52d182010-01-21 15:31:47 +02003182 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02003183 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003184
Avi Kivity6aa8b732006-12-10 02:21:36 -08003185 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003186
Avi Kivity6aa8b732006-12-10 02:21:36 -08003187 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003188 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003189 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04003190 .set_interrupt_shadow = svm_set_interrupt_shadow,
3191 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02003192 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003193 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003194 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02003195 .queue_exception = svm_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02003196 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003197 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003198 .get_nmi_mask = svm_get_nmi_mask,
3199 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003200 .enable_nmi_window = enable_nmi_window,
3201 .enable_irq_window = enable_irq_window,
3202 .update_cr8_intercept = update_cr8_intercept,
Izik Eiduscbc94022007-10-25 00:29:55 +02003203
3204 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003205 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003206 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003207
3208 .exit_reasons_str = svm_exit_reasons_str,
Sheng Yang17cc3932010-01-05 19:02:27 +08003209 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08003210
3211 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003212
3213 .rdtscp_supported = svm_rdtscp_supported,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003214};
3215
3216static int __init svm_init(void)
3217{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003218 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Rusty Russellc16f8622007-07-30 21:12:19 +10003219 THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003220}
3221
3222static void __exit svm_exit(void)
3223{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003224 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003225}
3226
3227module_init(svm_init)
3228module_exit(svm_exit)