blob: 429a24435c6d29231e4628d284032e30195db7ff [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 Roedel455716f2010-03-01 15:34:35 +0100120#define MSR_INVALID 0xffffffffU
121
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100122/* enable NPT for AMD64 and X86 with PAE */
123#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
124static bool npt_enabled = true;
125#else
Joerg Roedele0231712010-02-24 18:59:10 +0100126static bool npt_enabled;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100127#endif
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100128static int npt = 1;
129
130module_param(npt, int, S_IRUGO);
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100131
Joerg Roedel4b6e4dc2009-08-07 11:49:48 +0200132static int nested = 1;
Alexander Graf236de052008-11-25 20:17:10 +0100133module_param(nested, int, S_IRUGO);
134
Joerg Roedel44874f82008-08-27 14:18:43 +0200135static void svm_flush_tlb(struct kvm_vcpu *vcpu);
Joerg Roedela5c38322009-08-07 11:49:32 +0200136static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity04d2cc72007-09-10 18:10:54 +0300137
Joerg Roedel410e4d52009-08-07 11:49:44 +0200138static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100139static int nested_svm_intercept(struct vcpu_svm *svm);
Alexander Grafcf74a782008-11-25 20:17:08 +0100140static int nested_svm_vmexit(struct vcpu_svm *svm);
Alexander Grafcf74a782008-11-25 20:17:08 +0100141static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
142 bool has_error_code, u32 error_code);
143
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400144static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
145{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000146 return container_of(vcpu, struct vcpu_svm, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400147}
148
Alexander Graf3d6368e2008-11-25 20:17:07 +0100149static inline bool is_nested(struct vcpu_svm *svm)
150{
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200151 return svm->nested.vmcb;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100152}
153
Joerg Roedel2af91942009-08-07 11:49:28 +0200154static inline void enable_gif(struct vcpu_svm *svm)
155{
156 svm->vcpu.arch.hflags |= HF_GIF_MASK;
157}
158
159static inline void disable_gif(struct vcpu_svm *svm)
160{
161 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
162}
163
164static inline bool gif_set(struct vcpu_svm *svm)
165{
166 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
167}
168
Harvey Harrison4866d5e2008-02-19 10:32:02 -0800169static unsigned long iopm_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800170
171struct kvm_ldttss_desc {
172 u16 limit0;
173 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100174 unsigned base1:8, type:5, dpl:2, p:1;
175 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800176 u32 base3;
177 u32 zero1;
178} __attribute__((packed));
179
180struct svm_cpu_data {
181 int cpu;
182
Avi Kivity5008fdf2007-04-02 13:05:50 +0300183 u64 asid_generation;
184 u32 max_asid;
185 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800186 struct kvm_ldttss_desc *tss_desc;
187
188 struct page *save_area;
189};
190
191static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
Joerg Roedel80b77062007-03-30 17:02:14 +0300192static uint32_t svm_features;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800193
194struct svm_init_data {
195 int cpu;
196 int r;
197};
198
199static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
200
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200201#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800202#define MSRS_RANGE_SIZE 2048
203#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
204
Joerg Roedel455716f2010-03-01 15:34:35 +0100205static u32 svm_msrpm_offset(u32 msr)
206{
207 u32 offset;
208 int i;
209
210 for (i = 0; i < NUM_MSR_MAPS; i++) {
211 if (msr < msrpm_ranges[i] ||
212 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
213 continue;
214
215 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
216 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
217
218 /* Now we have the u8 offset - but need the u32 offset */
219 return offset / 4;
220 }
221
222 /* MSR not in any range */
223 return MSR_INVALID;
224}
225
Avi Kivity6aa8b732006-12-10 02:21:36 -0800226#define MAX_INST_SIZE 15
227
Joerg Roedel80b77062007-03-30 17:02:14 +0300228static inline u32 svm_has(u32 feat)
229{
230 return svm_features & feat;
231}
232
Avi Kivity6aa8b732006-12-10 02:21:36 -0800233static inline void clgi(void)
234{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300235 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800236}
237
238static inline void stgi(void)
239{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300240 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800241}
242
243static inline void invlpga(unsigned long addr, u32 asid)
244{
Joerg Roedele0231712010-02-24 18:59:10 +0100245 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800246}
247
Avi Kivity6aa8b732006-12-10 02:21:36 -0800248static inline void force_new_asid(struct kvm_vcpu *vcpu)
249{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400250 to_svm(vcpu)->asid_generation--;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800251}
252
253static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
254{
255 force_new_asid(vcpu);
256}
257
258static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
259{
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100260 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600261 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800262
Alexander Graf9962d032008-11-25 20:17:02 +0100263 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Avi Kivityf6801df2010-01-21 15:31:50 +0200264 vcpu->arch.efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800265}
266
Avi Kivity6aa8b732006-12-10 02:21:36 -0800267static int is_external_interrupt(u32 info)
268{
269 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
270 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
271}
272
Glauber Costa2809f5d2009-05-12 16:21:05 -0400273static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
274{
275 struct vcpu_svm *svm = to_svm(vcpu);
276 u32 ret = 0;
277
278 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Jan Kiszka48005f62010-02-19 19:38:07 +0100279 ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400280 return ret & mask;
281}
282
283static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
284{
285 struct vcpu_svm *svm = to_svm(vcpu);
286
287 if (mask == 0)
288 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
289 else
290 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
291
292}
293
Avi Kivity6aa8b732006-12-10 02:21:36 -0800294static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
295{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400296 struct vcpu_svm *svm = to_svm(vcpu);
297
298 if (!svm->next_rip) {
Avi Kivity851ba692009-08-24 11:10:17 +0300299 if (emulate_instruction(vcpu, 0, 0, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300300 EMULATE_DONE)
301 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800302 return;
303 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300304 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
305 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
306 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800307
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300308 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400309 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800310}
311
Jan Kiszka116a4752010-02-23 17:47:54 +0100312static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
313 bool has_error_code, u32 error_code)
314{
315 struct vcpu_svm *svm = to_svm(vcpu);
316
Joerg Roedele0231712010-02-24 18:59:10 +0100317 /*
318 * If we are within a nested VM we'd better #VMEXIT and let the guest
319 * handle the exception
320 */
Jan Kiszka116a4752010-02-23 17:47:54 +0100321 if (nested_svm_check_exception(svm, nr, has_error_code, error_code))
322 return;
323
Jan Kiszka66b71382010-02-23 17:47:56 +0100324 if (nr == BP_VECTOR && !svm_has(SVM_FEATURE_NRIP)) {
325 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
326
327 /*
328 * For guest debugging where we have to reinject #BP if some
329 * INT3 is guest-owned:
330 * Emulate nRIP by moving RIP forward. Will fail if injection
331 * raises a fault that is not intercepted. Still better than
332 * failing in all cases.
333 */
334 skip_emulated_instruction(&svm->vcpu);
335 rip = kvm_rip_read(&svm->vcpu);
336 svm->int3_rip = rip + svm->vmcb->save.cs.base;
337 svm->int3_injected = rip - old_rip;
338 }
339
Jan Kiszka116a4752010-02-23 17:47:54 +0100340 svm->vmcb->control.event_inj = nr
341 | SVM_EVTINJ_VALID
342 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
343 | SVM_EVTINJ_TYPE_EXEPT;
344 svm->vmcb->control.event_inj_err = error_code;
345}
346
Avi Kivity6aa8b732006-12-10 02:21:36 -0800347static int has_svm(void)
348{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200349 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800350
Eduardo Habkost63d11422008-11-17 19:03:20 -0200351 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800352 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800353 return 0;
354 }
355
Avi Kivity6aa8b732006-12-10 02:21:36 -0800356 return 1;
357}
358
359static void svm_hardware_disable(void *garbage)
360{
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200361 cpu_svm_disable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800362}
363
Alexander Graf10474ae2009-09-15 11:37:46 +0200364static int svm_hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800365{
366
Tejun Heo0fe1e002009-10-29 22:34:14 +0900367 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800368 uint64_t efer;
Gleb Natapov89a27f42010-02-16 10:51:48 +0200369 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800370 struct desc_struct *gdt;
371 int me = raw_smp_processor_id();
372
Alexander Graf10474ae2009-09-15 11:37:46 +0200373 rdmsrl(MSR_EFER, efer);
374 if (efer & EFER_SVME)
375 return -EBUSY;
376
Avi Kivity6aa8b732006-12-10 02:21:36 -0800377 if (!has_svm()) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000378 printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
379 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200380 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800381 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900382 sd = per_cpu(svm_data, me);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800383
Tejun Heo0fe1e002009-10-29 22:34:14 +0900384 if (!sd) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000385 printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -0800386 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200387 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800388 }
389
Tejun Heo0fe1e002009-10-29 22:34:14 +0900390 sd->asid_generation = 1;
391 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
392 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800393
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200394 native_store_gdt(&gdt_descr);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200395 gdt = (struct desc_struct *)gdt_descr.address;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900396 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800397
Alexander Graf9962d032008-11-25 20:17:02 +0100398 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800399
Linus Torvaldsd0316552009-12-14 09:58:24 -0800400 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200401
402 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800403}
404
Joerg Roedel0da1db752008-07-02 16:02:11 +0200405static void svm_cpu_uninit(int cpu)
406{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900407 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200408
Tejun Heo0fe1e002009-10-29 22:34:14 +0900409 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200410 return;
411
412 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900413 __free_page(sd->save_area);
414 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200415}
416
Avi Kivity6aa8b732006-12-10 02:21:36 -0800417static int svm_cpu_init(int cpu)
418{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900419 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800420 int r;
421
Tejun Heo0fe1e002009-10-29 22:34:14 +0900422 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
423 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900425 sd->cpu = cpu;
426 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800427 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900428 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800429 goto err_1;
430
Tejun Heo0fe1e002009-10-29 22:34:14 +0900431 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800432
433 return 0;
434
435err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900436 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800437 return r;
438
439}
440
Rusty Russellbfc733a2007-07-31 20:42:42 +1000441static void set_msr_interception(u32 *msrpm, unsigned msr,
442 int read, int write)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800443{
Joerg Roedel455716f2010-03-01 15:34:35 +0100444 u8 bit_read, bit_write;
445 unsigned long tmp;
446 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800447
Joerg Roedel455716f2010-03-01 15:34:35 +0100448 offset = svm_msrpm_offset(msr);
449 bit_read = 2 * (msr & 0x0f);
450 bit_write = 2 * (msr & 0x0f) + 1;
451 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800452
Joerg Roedel455716f2010-03-01 15:34:35 +0100453 BUG_ON(offset == MSR_INVALID);
454
455 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
456 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
457
458 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800459}
460
Joerg Roedelf65c2292008-02-13 18:58:46 +0100461static void svm_vcpu_init_msrpm(u32 *msrpm)
462{
463 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
464
465#ifdef CONFIG_X86_64
466 set_msr_interception(msrpm, MSR_GS_BASE, 1, 1);
467 set_msr_interception(msrpm, MSR_FS_BASE, 1, 1);
468 set_msr_interception(msrpm, MSR_KERNEL_GS_BASE, 1, 1);
469 set_msr_interception(msrpm, MSR_LSTAR, 1, 1);
470 set_msr_interception(msrpm, MSR_CSTAR, 1, 1);
471 set_msr_interception(msrpm, MSR_SYSCALL_MASK, 1, 1);
472#endif
473 set_msr_interception(msrpm, MSR_K6_STAR, 1, 1);
474 set_msr_interception(msrpm, MSR_IA32_SYSENTER_CS, 1, 1);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100475}
476
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100477static void svm_enable_lbrv(struct vcpu_svm *svm)
478{
479 u32 *msrpm = svm->msrpm;
480
481 svm->vmcb->control.lbr_ctl = 1;
482 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
483 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
484 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
485 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
486}
487
488static void svm_disable_lbrv(struct vcpu_svm *svm)
489{
490 u32 *msrpm = svm->msrpm;
491
492 svm->vmcb->control.lbr_ctl = 0;
493 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
494 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
495 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
496 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
497}
498
Avi Kivity6aa8b732006-12-10 02:21:36 -0800499static __init int svm_hardware_setup(void)
500{
501 int cpu;
502 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100503 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800504 int r;
505
Avi Kivity6aa8b732006-12-10 02:21:36 -0800506 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
507
508 if (!iopm_pages)
509 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300510
511 iopm_va = page_address(iopm_pages);
512 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800513 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
514
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100515 if (boot_cpu_has(X86_FEATURE_NX))
516 kvm_enable_efer_bits(EFER_NX);
517
Alexander Graf1b2fd702009-02-02 16:23:51 +0100518 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
519 kvm_enable_efer_bits(EFER_FFXSR);
520
Alexander Graf236de052008-11-25 20:17:10 +0100521 if (nested) {
522 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
523 kvm_enable_efer_bits(EFER_SVME);
524 }
525
Zachary Amsden3230bb42009-09-29 11:38:37 -1000526 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800527 r = svm_cpu_init(cpu);
528 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100529 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800530 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100531
532 svm_features = cpuid_edx(SVM_CPUID_FUNC);
533
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100534 if (!svm_has(SVM_FEATURE_NPT))
535 npt_enabled = false;
536
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100537 if (npt_enabled && !npt) {
538 printk(KERN_INFO "kvm: Nested Paging disabled\n");
539 npt_enabled = false;
540 }
541
Joerg Roedel18552672008-02-07 13:47:41 +0100542 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100543 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100544 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200545 } else
546 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100547
Avi Kivity6aa8b732006-12-10 02:21:36 -0800548 return 0;
549
Joerg Roedelf65c2292008-02-13 18:58:46 +0100550err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800551 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
552 iopm_base = 0;
553 return r;
554}
555
556static __exit void svm_hardware_unsetup(void)
557{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200558 int cpu;
559
Zachary Amsden3230bb42009-09-29 11:38:37 -1000560 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200561 svm_cpu_uninit(cpu);
562
Avi Kivity6aa8b732006-12-10 02:21:36 -0800563 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100564 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800565}
566
567static void init_seg(struct vmcb_seg *seg)
568{
569 seg->selector = 0;
570 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +0100571 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800572 seg->limit = 0xffff;
573 seg->base = 0;
574}
575
576static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
577{
578 seg->selector = 0;
579 seg->attrib = SVM_SELECTOR_P_MASK | type;
580 seg->limit = 0xffff;
581 seg->base = 0;
582}
583
Joerg Roedele6101a92008-02-13 18:58:45 +0100584static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800585{
Joerg Roedele6101a92008-02-13 18:58:45 +0100586 struct vmcb_control_area *control = &svm->vmcb->control;
587 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800588
Avi Kivitybff78272010-01-07 13:16:08 +0200589 svm->vcpu.fpu_active = 1;
590
Joerg Roedele0231712010-02-24 18:59:10 +0100591 control->intercept_cr_read = INTERCEPT_CR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800592 INTERCEPT_CR3_MASK |
Joerg Roedel649d6862008-04-16 16:51:15 +0200593 INTERCEPT_CR4_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800594
Joerg Roedele0231712010-02-24 18:59:10 +0100595 control->intercept_cr_write = INTERCEPT_CR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800596 INTERCEPT_CR3_MASK |
Avi Kivity80a81192007-12-06 19:50:00 +0200597 INTERCEPT_CR4_MASK |
598 INTERCEPT_CR8_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800599
Joerg Roedele0231712010-02-24 18:59:10 +0100600 control->intercept_dr_read = INTERCEPT_DR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800601 INTERCEPT_DR1_MASK |
602 INTERCEPT_DR2_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100603 INTERCEPT_DR3_MASK |
604 INTERCEPT_DR4_MASK |
605 INTERCEPT_DR5_MASK |
606 INTERCEPT_DR6_MASK |
607 INTERCEPT_DR7_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800608
Joerg Roedele0231712010-02-24 18:59:10 +0100609 control->intercept_dr_write = INTERCEPT_DR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800610 INTERCEPT_DR1_MASK |
611 INTERCEPT_DR2_MASK |
612 INTERCEPT_DR3_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100613 INTERCEPT_DR4_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800614 INTERCEPT_DR5_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100615 INTERCEPT_DR6_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800616 INTERCEPT_DR7_MASK;
617
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500618 control->intercept_exceptions = (1 << PF_VECTOR) |
Joerg Roedel53371b52008-04-09 14:15:30 +0200619 (1 << UD_VECTOR) |
620 (1 << MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800621
622
Joerg Roedele0231712010-02-24 18:59:10 +0100623 control->intercept = (1ULL << INTERCEPT_INTR) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800624 (1ULL << INTERCEPT_NMI) |
Joerg Roedel01525272007-02-19 14:37:47 +0200625 (1ULL << INTERCEPT_SMI) |
Avi Kivityd2251572010-01-06 10:55:27 +0200626 (1ULL << INTERCEPT_SELECTIVE_CR0) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800627 (1ULL << INTERCEPT_CPUID) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200628 (1ULL << INTERCEPT_INVD) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800629 (1ULL << INTERCEPT_HLT) |
Marcelo Tosattia7052892008-09-23 13:18:35 -0300630 (1ULL << INTERCEPT_INVLPG) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800631 (1ULL << INTERCEPT_INVLPGA) |
632 (1ULL << INTERCEPT_IOIO_PROT) |
633 (1ULL << INTERCEPT_MSR_PROT) |
634 (1ULL << INTERCEPT_TASK_SWITCH) |
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800635 (1ULL << INTERCEPT_SHUTDOWN) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800636 (1ULL << INTERCEPT_VMRUN) |
637 (1ULL << INTERCEPT_VMMCALL) |
638 (1ULL << INTERCEPT_VMLOAD) |
639 (1ULL << INTERCEPT_VMSAVE) |
640 (1ULL << INTERCEPT_STGI) |
641 (1ULL << INTERCEPT_CLGI) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100642 (1ULL << INTERCEPT_SKINIT) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200643 (1ULL << INTERCEPT_WBINVD) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100644 (1ULL << INTERCEPT_MONITOR) |
645 (1ULL << INTERCEPT_MWAIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800646
647 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100648 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity0cc50642007-03-25 12:07:27 +0200649 control->tsc_offset = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800650 control->int_ctl = V_INTR_MASKING_MASK;
651
652 init_seg(&save->es);
653 init_seg(&save->ss);
654 init_seg(&save->ds);
655 init_seg(&save->fs);
656 init_seg(&save->gs);
657
658 save->cs.selector = 0xf000;
659 /* Executable/Readable Code Segment */
660 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
661 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
662 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800663 /*
664 * cs.base should really be 0xffff0000, but vmx can't handle that, so
665 * be consistent with it.
666 *
667 * Replace when we have real mode working for vmx.
668 */
669 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800670
671 save->gdtr.limit = 0xffff;
672 save->idtr.limit = 0xffff;
673
674 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
675 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
676
Alexander Graf9962d032008-11-25 20:17:02 +0100677 save->efer = EFER_SVME;
Mike Dayd77c26f2007-10-08 09:02:08 -0400678 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800679 save->dr7 = 0x400;
680 save->rflags = 2;
681 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300682 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800683
Joerg Roedele0231712010-02-24 18:59:10 +0100684 /*
685 * This is the guest-visible cr0 value.
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200686 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800687 */
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200688 svm->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
689 kvm_set_cr0(&svm->vcpu, svm->vcpu.arch.cr0);
690
Rusty Russell66aee912007-07-17 23:34:16 +1000691 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800692 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100693
694 if (npt_enabled) {
695 /* Setup VMCB for Nested Paging */
696 control->nested_ctl = 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -0300697 control->intercept &= ~((1ULL << INTERCEPT_TASK_SWITCH) |
698 (1ULL << INTERCEPT_INVLPG));
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100699 control->intercept_exceptions &= ~(1 << PF_VECTOR);
Avi Kivity888f9f32010-01-10 12:14:04 +0200700 control->intercept_cr_read &= ~INTERCEPT_CR3_MASK;
701 control->intercept_cr_write &= ~INTERCEPT_CR3_MASK;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100702 save->g_pat = 0x0007040600070406ULL;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100703 save->cr3 = 0;
704 save->cr4 = 0;
705 }
Avi Kivitya79d2f12008-04-14 13:10:21 +0300706 force_new_asid(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +0100707
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200708 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +0200709 svm->vcpu.arch.hflags = 0;
710
Mark Langsdorf565d0992009-10-06 14:25:02 -0500711 if (svm_has(SVM_FEATURE_PAUSE_FILTER)) {
712 control->pause_filter_count = 3000;
713 control->intercept |= (1ULL << INTERCEPT_PAUSE);
714 }
715
Joerg Roedel2af91942009-08-07 11:49:28 +0200716 enable_gif(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800717}
718
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200719static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +0300720{
721 struct vcpu_svm *svm = to_svm(vcpu);
722
Joerg Roedele6101a92008-02-13 18:58:45 +0100723 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +0200724
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300725 if (!kvm_vcpu_is_bsp(vcpu)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300726 kvm_rip_write(vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800727 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
728 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +0200729 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300730 vcpu->arch.regs_avail = ~0;
731 vcpu->arch.regs_dirty = ~0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200732
733 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300734}
735
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000736static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800737{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400738 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800739 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100740 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +0100741 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100742 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000743 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800744
Rusty Russellc16f8622007-07-30 21:12:19 +1000745 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000746 if (!svm) {
747 err = -ENOMEM;
748 goto out;
749 }
750
751 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
752 if (err)
753 goto free_svm;
754
Joerg Roedelf65c2292008-02-13 18:58:46 +0100755 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900756 page = alloc_page(GFP_KERNEL);
757 if (!page)
758 goto uninit;
759
Joerg Roedelf65c2292008-02-13 18:58:46 +0100760 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
761 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900762 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100763
764 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
765 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900766 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100767
Alexander Grafb286d5d2008-11-25 20:17:05 +0100768 hsave_page = alloc_page(GFP_KERNEL);
769 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900770 goto free_page3;
771
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200772 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +0100773
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900774 svm->msrpm = page_address(msrpm_pages);
775 svm_vcpu_init_msrpm(svm->msrpm);
776
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200777 svm->nested.msrpm = page_address(nested_msrpm_pages);
Alexander Graf3d6368e2008-11-25 20:17:07 +0100778
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400779 svm->vmcb = page_address(page);
780 clear_page(svm->vmcb);
781 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
782 svm->asid_generation = 0;
Joerg Roedele6101a92008-02-13 18:58:45 +0100783 init_vmcb(svm);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400784
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000785 fx_init(&svm->vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800786 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300787 if (kvm_vcpu_is_bsp(&svm->vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800788 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000790 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -0800791
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900792free_page3:
793 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
794free_page2:
795 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
796free_page1:
797 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000798uninit:
799 kvm_vcpu_uninit(&svm->vcpu);
800free_svm:
Rusty Russella4770342007-08-01 14:46:11 +1000801 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000802out:
803 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800804}
805
806static void svm_free_vcpu(struct kvm_vcpu *vcpu)
807{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400808 struct vcpu_svm *svm = to_svm(vcpu);
809
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000810 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +0100811 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200812 __free_page(virt_to_page(svm->nested.hsave));
813 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000814 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +1000815 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800816}
817
Avi Kivity15ad7142007-07-11 18:17:21 +0300818static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800819{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400820 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300821 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +0200822
Avi Kivity0cc50642007-03-25 12:07:27 +0200823 if (unlikely(cpu != vcpu->cpu)) {
Joerg Roedele935d482009-09-16 15:24:19 +0200824 u64 delta;
Avi Kivity0cc50642007-03-25 12:07:27 +0200825
Joerg Roedel953899b2009-12-14 12:22:20 +0100826 if (check_tsc_unstable()) {
827 /*
828 * Make sure that the guest sees a monotonically
829 * increasing TSC.
830 */
831 delta = vcpu->arch.host_tsc - native_read_tsc();
832 svm->vmcb->control.tsc_offset += delta;
833 if (is_nested(svm))
834 svm->nested.hsave->control.tsc_offset += delta;
835 }
Avi Kivity0cc50642007-03-25 12:07:27 +0200836 vcpu->cpu = cpu;
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300837 kvm_migrate_timers(vcpu);
Marcelo Tosatti4b656b12009-07-21 12:47:45 -0300838 svm->asid_generation = 0;
Avi Kivity0cc50642007-03-25 12:07:27 +0200839 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300840
841 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400842 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800843}
844
845static void svm_vcpu_put(struct kvm_vcpu *vcpu)
846{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400847 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300848 int i;
849
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200850 ++vcpu->stat.host_state_reload;
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300851 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400852 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300853
Joerg Roedele935d482009-09-16 15:24:19 +0200854 vcpu->arch.host_tsc = native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800855}
856
Avi Kivity6aa8b732006-12-10 02:21:36 -0800857static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
858{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400859 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800860}
861
862static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
863{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400864 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800865}
866
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300867static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
868{
869 switch (reg) {
870 case VCPU_EXREG_PDPTR:
871 BUG_ON(!npt_enabled);
872 load_pdptrs(vcpu, vcpu->arch.cr3);
873 break;
874 default:
875 BUG();
876 }
877}
878
Alexander Graff0b85052008-11-25 20:17:01 +0100879static void svm_set_vintr(struct vcpu_svm *svm)
880{
881 svm->vmcb->control.intercept |= 1ULL << INTERCEPT_VINTR;
882}
883
884static void svm_clear_vintr(struct vcpu_svm *svm)
885{
886 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
887}
888
Avi Kivity6aa8b732006-12-10 02:21:36 -0800889static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
890{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400891 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800892
893 switch (seg) {
894 case VCPU_SREG_CS: return &save->cs;
895 case VCPU_SREG_DS: return &save->ds;
896 case VCPU_SREG_ES: return &save->es;
897 case VCPU_SREG_FS: return &save->fs;
898 case VCPU_SREG_GS: return &save->gs;
899 case VCPU_SREG_SS: return &save->ss;
900 case VCPU_SREG_TR: return &save->tr;
901 case VCPU_SREG_LDTR: return &save->ldtr;
902 }
903 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +0000904 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905}
906
907static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
908{
909 struct vmcb_seg *s = svm_seg(vcpu, seg);
910
911 return s->base;
912}
913
914static void svm_get_segment(struct kvm_vcpu *vcpu,
915 struct kvm_segment *var, int seg)
916{
917 struct vmcb_seg *s = svm_seg(vcpu, seg);
918
919 var->base = s->base;
920 var->limit = s->limit;
921 var->selector = s->selector;
922 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
923 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
924 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
925 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
926 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
927 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
928 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
929 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
Amit Shah25022ac2008-10-27 09:04:17 +0000930
Joerg Roedele0231712010-02-24 18:59:10 +0100931 /*
932 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +0200933 * for cross vendor migration purposes by "not present"
934 */
935 var->unusable = !var->present || (var->type == 0);
936
Andre Przywara1fbdc7a2009-01-11 22:39:44 +0100937 switch (seg) {
938 case VCPU_SREG_CS:
939 /*
940 * SVM always stores 0 for the 'G' bit in the CS selector in
941 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
942 * Intel's VMENTRY has a check on the 'G' bit.
943 */
Amit Shah25022ac2008-10-27 09:04:17 +0000944 var->g = s->limit > 0xfffff;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +0100945 break;
946 case VCPU_SREG_TR:
947 /*
948 * Work around a bug where the busy flag in the tr selector
949 * isn't exposed
950 */
Amit Shahc0d09822008-10-27 09:04:18 +0000951 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +0100952 break;
953 case VCPU_SREG_DS:
954 case VCPU_SREG_ES:
955 case VCPU_SREG_FS:
956 case VCPU_SREG_GS:
957 /*
958 * The accessed bit must always be set in the segment
959 * descriptor cache, although it can be cleared in the
960 * descriptor, the cached bit always remains at 1. Since
961 * Intel has a check on this, set it here to support
962 * cross-vendor migration.
963 */
964 if (!var->unusable)
965 var->type |= 0x1;
966 break;
Andre Przywarab586eb02009-04-28 12:45:43 +0200967 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +0100968 /*
969 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +0200970 * descriptor is left as 1, although the whole segment has
971 * been made unusable. Clear it here to pass an Intel VMX
972 * entry check when cross vendor migrating.
973 */
974 if (var->unusable)
975 var->db = 0;
976 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +0100977 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800978}
979
Izik Eidus2e4d2652008-03-24 19:38:34 +0200980static int svm_get_cpl(struct kvm_vcpu *vcpu)
981{
982 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
983
984 return save->cpl;
985}
986
Gleb Natapov89a27f42010-02-16 10:51:48 +0200987static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400989 struct vcpu_svm *svm = to_svm(vcpu);
990
Gleb Natapov89a27f42010-02-16 10:51:48 +0200991 dt->size = svm->vmcb->save.idtr.limit;
992 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993}
994
Gleb Natapov89a27f42010-02-16 10:51:48 +0200995static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800996{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400997 struct vcpu_svm *svm = to_svm(vcpu);
998
Gleb Natapov89a27f42010-02-16 10:51:48 +0200999 svm->vmcb->save.idtr.limit = dt->size;
1000 svm->vmcb->save.idtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001001}
1002
Gleb Natapov89a27f42010-02-16 10:51:48 +02001003static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001004{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001005 struct vcpu_svm *svm = to_svm(vcpu);
1006
Gleb Natapov89a27f42010-02-16 10:51:48 +02001007 dt->size = svm->vmcb->save.gdtr.limit;
1008 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009}
1010
Gleb Natapov89a27f42010-02-16 10:51:48 +02001011static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001013 struct vcpu_svm *svm = to_svm(vcpu);
1014
Gleb Natapov89a27f42010-02-16 10:51:48 +02001015 svm->vmcb->save.gdtr.limit = dt->size;
1016 svm->vmcb->save.gdtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001017}
1018
Avi Kivitye8467fd2009-12-29 18:43:06 +02001019static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1020{
1021}
1022
Anthony Liguori25c4c272007-04-27 09:29:21 +03001023static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001024{
1025}
1026
Avi Kivityd2251572010-01-06 10:55:27 +02001027static void update_cr0_intercept(struct vcpu_svm *svm)
1028{
Joerg Roedel66a562f2010-02-19 16:23:08 +01001029 struct vmcb *vmcb = svm->vmcb;
Avi Kivityd2251572010-01-06 10:55:27 +02001030 ulong gcr0 = svm->vcpu.arch.cr0;
1031 u64 *hcr0 = &svm->vmcb->save.cr0;
1032
1033 if (!svm->vcpu.fpu_active)
1034 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1035 else
1036 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1037 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1038
1039
1040 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Joerg Roedel66a562f2010-02-19 16:23:08 +01001041 vmcb->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1042 vmcb->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1043 if (is_nested(svm)) {
1044 struct vmcb *hsave = svm->nested.hsave;
1045
1046 hsave->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1047 hsave->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1048 vmcb->control.intercept_cr_read |= svm->nested.intercept_cr_read;
1049 vmcb->control.intercept_cr_write |= svm->nested.intercept_cr_write;
1050 }
Avi Kivityd2251572010-01-06 10:55:27 +02001051 } else {
1052 svm->vmcb->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1053 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001054 if (is_nested(svm)) {
1055 struct vmcb *hsave = svm->nested.hsave;
1056
1057 hsave->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1058 hsave->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
1059 }
Avi Kivityd2251572010-01-06 10:55:27 +02001060 }
1061}
1062
Avi Kivity6aa8b732006-12-10 02:21:36 -08001063static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1064{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001065 struct vcpu_svm *svm = to_svm(vcpu);
1066
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001067 if (is_nested(svm)) {
1068 /*
1069 * We are here because we run in nested mode, the host kvm
1070 * intercepts cr0 writes but the l1 hypervisor does not.
1071 * But the L1 hypervisor may intercept selective cr0 writes.
1072 * This needs to be checked here.
1073 */
1074 unsigned long old, new;
1075
1076 /* Remove bits that would trigger a real cr0 write intercept */
1077 old = vcpu->arch.cr0 & SVM_CR0_SELECTIVE_MASK;
1078 new = cr0 & SVM_CR0_SELECTIVE_MASK;
1079
1080 if (old == new) {
1081 /* cr0 write with ts and mp unchanged */
1082 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
1083 if (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE)
1084 return;
1085 }
1086 }
1087
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001088#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001089 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001090 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001091 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001092 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001093 }
1094
Mike Dayd77c26f2007-10-08 09:02:08 -04001095 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001096 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001097 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001098 }
1099 }
1100#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001101 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001102
1103 if (!npt_enabled)
1104 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001105
1106 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001107 cr0 |= X86_CR0_TS;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001108 /*
1109 * re-enable caching here because the QEMU bios
1110 * does not do it - this results in some delay at
1111 * reboot
1112 */
1113 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001114 svm->vmcb->save.cr0 = cr0;
Avi Kivityd2251572010-01-06 10:55:27 +02001115 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001116}
1117
1118static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1119{
Joerg Roedel6394b642008-04-09 14:15:29 +02001120 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001121 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1122
1123 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
1124 force_new_asid(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001125
Joerg Roedelec077262008-04-09 14:15:28 +02001126 vcpu->arch.cr4 = cr4;
1127 if (!npt_enabled)
1128 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001129 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001130 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001131}
1132
1133static void svm_set_segment(struct kvm_vcpu *vcpu,
1134 struct kvm_segment *var, int seg)
1135{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001136 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001137 struct vmcb_seg *s = svm_seg(vcpu, seg);
1138
1139 s->base = var->base;
1140 s->limit = var->limit;
1141 s->selector = var->selector;
1142 if (var->unusable)
1143 s->attrib = 0;
1144 else {
1145 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1146 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1147 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1148 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1149 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1150 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1151 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1152 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1153 }
1154 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001155 svm->vmcb->save.cpl
1156 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -08001157 >> SVM_SELECTOR_DPL_SHIFT) & 3;
1158
1159}
1160
Gleb Natapov44c11432009-05-11 13:35:52 +03001161static void update_db_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001162{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001163 struct vcpu_svm *svm = to_svm(vcpu);
1164
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001165 svm->vmcb->control.intercept_exceptions &=
1166 ~((1 << DB_VECTOR) | (1 << BP_VECTOR));
Gleb Natapov44c11432009-05-11 13:35:52 +03001167
Jan Kiszka6be7d302009-10-18 13:24:54 +02001168 if (svm->nmi_singlestep)
Gleb Natapov44c11432009-05-11 13:35:52 +03001169 svm->vmcb->control.intercept_exceptions |= (1 << DB_VECTOR);
1170
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001171 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1172 if (vcpu->guest_debug &
1173 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
1174 svm->vmcb->control.intercept_exceptions |=
1175 1 << DB_VECTOR;
1176 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
1177 svm->vmcb->control.intercept_exceptions |=
1178 1 << BP_VECTOR;
1179 } else
1180 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001181}
1182
Jan Kiszka355be0b2009-10-03 00:31:21 +02001183static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Gleb Natapov44c11432009-05-11 13:35:52 +03001184{
Gleb Natapov44c11432009-05-11 13:35:52 +03001185 struct vcpu_svm *svm = to_svm(vcpu);
1186
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001187 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1188 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1189 else
1190 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1191
Jan Kiszka355be0b2009-10-03 00:31:21 +02001192 update_db_intercept(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001193}
1194
1195static void load_host_msrs(struct kvm_vcpu *vcpu)
1196{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001197#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001198 wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001199#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001200}
1201
1202static void save_host_msrs(struct kvm_vcpu *vcpu)
1203{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001204#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001205 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001206#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001207}
1208
Tejun Heo0fe1e002009-10-29 22:34:14 +09001209static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001210{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001211 if (sd->next_asid > sd->max_asid) {
1212 ++sd->asid_generation;
1213 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001214 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001215 }
1216
Tejun Heo0fe1e002009-10-29 22:34:14 +09001217 svm->asid_generation = sd->asid_generation;
1218 svm->vmcb->control.asid = sd->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001219}
1220
Jan Kiszkac76de352010-01-20 18:20:20 +01001221static int svm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *dest)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001222{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001223 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001224
1225 switch (dr) {
1226 case 0 ... 3:
Jan Kiszkac76de352010-01-20 18:20:20 +01001227 *dest = vcpu->arch.db[dr];
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001228 break;
Jan Kiszkac76de352010-01-20 18:20:20 +01001229 case 4:
1230 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
1231 return EMULATE_FAIL; /* will re-inject UD */
1232 /* fall through */
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001233 case 6:
1234 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
Jan Kiszkac76de352010-01-20 18:20:20 +01001235 *dest = vcpu->arch.dr6;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001236 else
Jan Kiszkac76de352010-01-20 18:20:20 +01001237 *dest = svm->vmcb->save.dr6;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001238 break;
Jan Kiszkac76de352010-01-20 18:20:20 +01001239 case 5:
1240 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
1241 return EMULATE_FAIL; /* will re-inject UD */
1242 /* fall through */
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001243 case 7:
1244 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
Jan Kiszkac76de352010-01-20 18:20:20 +01001245 *dest = vcpu->arch.dr7;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001246 else
Jan Kiszkac76de352010-01-20 18:20:20 +01001247 *dest = svm->vmcb->save.dr7;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001248 break;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001249 }
1250
Jan Kiszkac76de352010-01-20 18:20:20 +01001251 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001252}
1253
Jan Kiszkac76de352010-01-20 18:20:20 +01001254static int svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001255{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001256 struct vcpu_svm *svm = to_svm(vcpu);
1257
Avi Kivity6aa8b732006-12-10 02:21:36 -08001258 switch (dr) {
1259 case 0 ... 3:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001260 vcpu->arch.db[dr] = value;
1261 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1262 vcpu->arch.eff_db[dr] = value;
Jan Kiszkac76de352010-01-20 18:20:20 +01001263 break;
1264 case 4:
1265 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
1266 return EMULATE_FAIL; /* will re-inject UD */
1267 /* fall through */
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001268 case 6:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001269 vcpu->arch.dr6 = (value & DR6_VOLATILE) | DR6_FIXED_1;
Jan Kiszkac76de352010-01-20 18:20:20 +01001270 break;
1271 case 5:
1272 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
1273 return EMULATE_FAIL; /* will re-inject UD */
1274 /* fall through */
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001275 case 7:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001276 vcpu->arch.dr7 = (value & DR7_VOLATILE) | DR7_FIXED_1;
1277 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
1278 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1279 vcpu->arch.switch_db_regs = (value & DR7_BP_EN_MASK);
1280 }
Jan Kiszkac76de352010-01-20 18:20:20 +01001281 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001282 }
Jan Kiszkac76de352010-01-20 18:20:20 +01001283
1284 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001285}
1286
Avi Kivity851ba692009-08-24 11:10:17 +03001287static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001288{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001289 u64 fault_address;
1290 u32 error_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001291
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001292 fault_address = svm->vmcb->control.exit_info_2;
1293 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001294
Marcelo Tosatti229456f2009-06-17 09:22:14 -03001295 trace_kvm_page_fault(fault_address, error_code);
Avi Kivity52c78472009-08-10 15:42:41 +03001296 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1297 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
Avi Kivity30677142007-10-28 18:48:59 +02001298 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001299}
1300
Avi Kivity851ba692009-08-24 11:10:17 +03001301static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001302{
Avi Kivity851ba692009-08-24 11:10:17 +03001303 struct kvm_run *kvm_run = svm->vcpu.run;
1304
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001305 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001306 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001307 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001308 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1309 return 1;
1310 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001311
Jan Kiszka6be7d302009-10-18 13:24:54 +02001312 if (svm->nmi_singlestep) {
1313 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001314 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1315 svm->vmcb->save.rflags &=
1316 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1317 update_db_intercept(&svm->vcpu);
1318 }
1319
1320 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001321 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001322 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1323 kvm_run->debug.arch.pc =
1324 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1325 kvm_run->debug.arch.exception = DB_VECTOR;
1326 return 0;
1327 }
1328
1329 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001330}
1331
Avi Kivity851ba692009-08-24 11:10:17 +03001332static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001333{
Avi Kivity851ba692009-08-24 11:10:17 +03001334 struct kvm_run *kvm_run = svm->vcpu.run;
1335
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001336 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1337 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1338 kvm_run->debug.arch.exception = BP_VECTOR;
1339 return 0;
1340}
1341
Avi Kivity851ba692009-08-24 11:10:17 +03001342static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001343{
1344 int er;
1345
Avi Kivity851ba692009-08-24 11:10:17 +03001346 er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001347 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001348 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001349 return 1;
1350}
1351
Avi Kivity6b52d182010-01-21 15:31:47 +02001352static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001353{
Avi Kivity6b52d182010-01-21 15:31:47 +02001354 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001355 u32 excp;
1356
1357 if (is_nested(svm)) {
1358 u32 h_excp, n_excp;
1359
1360 h_excp = svm->nested.hsave->control.intercept_exceptions;
1361 n_excp = svm->nested.intercept_exceptions;
1362 h_excp &= ~(1 << NM_VECTOR);
1363 excp = h_excp | n_excp;
1364 } else {
1365 excp = svm->vmcb->control.intercept_exceptions;
Joerg Roedele0231712010-02-24 18:59:10 +01001366 excp &= ~(1 << NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001367 }
1368
1369 svm->vmcb->control.intercept_exceptions = excp;
1370
Rusty Russelle756fc62007-07-30 20:07:08 +10001371 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001372 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001373}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001374
Avi Kivity6b52d182010-01-21 15:31:47 +02001375static int nm_interception(struct vcpu_svm *svm)
1376{
1377 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001378 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001379}
1380
Avi Kivity851ba692009-08-24 11:10:17 +03001381static int mc_interception(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001382{
1383 /*
1384 * On an #MC intercept the MCE handler is not called automatically in
1385 * the host. So do it by hand here.
1386 */
1387 asm volatile (
1388 "int $0x12\n");
1389 /* not sure if we ever come back to this point */
1390
1391 return 1;
1392}
1393
Avi Kivity851ba692009-08-24 11:10:17 +03001394static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001395{
Avi Kivity851ba692009-08-24 11:10:17 +03001396 struct kvm_run *kvm_run = svm->vcpu.run;
1397
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001398 /*
1399 * VMCB is undefined after a SHUTDOWN intercept
1400 * so reinitialize it.
1401 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001402 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001403 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001404
1405 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1406 return 0;
1407}
1408
Avi Kivity851ba692009-08-24 11:10:17 +03001409static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001410{
Mike Dayd77c26f2007-10-08 09:02:08 -04001411 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001412 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001413 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001414
Rusty Russelle756fc62007-07-30 20:07:08 +10001415 ++svm->vcpu.stat.io_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001416
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001417 svm->next_rip = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001418
Laurent Viviere70669a2007-08-05 10:36:40 +03001419 string = (io_info & SVM_IOIO_STR_MASK) != 0;
1420
1421 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02001422 if (emulate_instruction(&svm->vcpu,
Avi Kivity851ba692009-08-24 11:10:17 +03001423 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03001424 return 0;
1425 return 1;
1426 }
1427
Avi Kivity039576c2007-03-20 12:46:50 +02001428 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
1429 port = io_info >> 16;
1430 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001431
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001432 skip_emulated_instruction(&svm->vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03001433 return kvm_emulate_pio(&svm->vcpu, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001434}
1435
Avi Kivity851ba692009-08-24 11:10:17 +03001436static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001437{
1438 return 1;
1439}
1440
Avi Kivity851ba692009-08-24 11:10:17 +03001441static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001442{
1443 ++svm->vcpu.stat.irq_exits;
1444 return 1;
1445}
1446
Avi Kivity851ba692009-08-24 11:10:17 +03001447static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001448{
1449 return 1;
1450}
1451
Avi Kivity851ba692009-08-24 11:10:17 +03001452static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001453{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001454 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001455 skip_emulated_instruction(&svm->vcpu);
1456 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001457}
1458
Avi Kivity851ba692009-08-24 11:10:17 +03001459static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001460{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001461 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001462 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001463 kvm_emulate_hypercall(&svm->vcpu);
1464 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001465}
1466
Alexander Grafc0725422008-11-25 20:17:03 +01001467static int nested_svm_check_permissions(struct vcpu_svm *svm)
1468{
Avi Kivityf6801df2010-01-21 15:31:50 +02001469 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01001470 || !is_paging(&svm->vcpu)) {
1471 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1472 return 1;
1473 }
1474
1475 if (svm->vmcb->save.cpl) {
1476 kvm_inject_gp(&svm->vcpu, 0);
1477 return 1;
1478 }
1479
1480 return 0;
1481}
1482
Alexander Grafcf74a782008-11-25 20:17:08 +01001483static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
1484 bool has_error_code, u32 error_code)
1485{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001486 int vmexit;
1487
Joerg Roedel0295ad72009-08-07 11:49:37 +02001488 if (!is_nested(svm))
1489 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001490
Joerg Roedel0295ad72009-08-07 11:49:37 +02001491 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1492 svm->vmcb->control.exit_code_hi = 0;
1493 svm->vmcb->control.exit_info_1 = error_code;
1494 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1495
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001496 vmexit = nested_svm_intercept(svm);
1497 if (vmexit == NESTED_EXIT_DONE)
1498 svm->nested.exit_required = true;
1499
1500 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001501}
1502
Joerg Roedel8fe54652010-02-19 16:23:01 +01001503/* This function returns true if it is save to enable the irq window */
1504static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001505{
Joerg Roedel26666952009-08-07 11:49:46 +02001506 if (!is_nested(svm))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001507 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001508
Joerg Roedel26666952009-08-07 11:49:46 +02001509 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001510 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001511
Joerg Roedel26666952009-08-07 11:49:46 +02001512 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001513 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001514
Joerg Roedel197717d2010-02-24 18:59:19 +01001515 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
1516 svm->vmcb->control.exit_info_1 = 0;
1517 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02001518
Joerg Roedelcd3ff652009-10-09 16:08:26 +02001519 if (svm->nested.intercept & 1ULL) {
1520 /*
1521 * The #vmexit can't be emulated here directly because this
1522 * code path runs with irqs and preemtion disabled. A
1523 * #vmexit emulation might sleep. Only signal request for
1524 * the #vmexit here.
1525 */
1526 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02001527 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01001528 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001529 }
1530
Joerg Roedel8fe54652010-02-19 16:23:01 +01001531 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001532}
1533
Joerg Roedel887f5002010-02-24 18:59:12 +01001534/* This function returns true if it is save to enable the nmi window */
1535static inline bool nested_svm_nmi(struct vcpu_svm *svm)
1536{
1537 if (!is_nested(svm))
1538 return true;
1539
1540 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
1541 return true;
1542
1543 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
1544 svm->nested.exit_required = true;
1545
1546 return false;
1547}
1548
Joerg Roedel7597f122010-02-19 16:23:00 +01001549static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001550{
1551 struct page *page;
1552
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01001553 might_sleep();
1554
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001555 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001556 if (is_error_page(page))
1557 goto error;
1558
Joerg Roedel7597f122010-02-19 16:23:00 +01001559 *_page = page;
1560
1561 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001562
1563error:
1564 kvm_release_page_clean(page);
1565 kvm_inject_gp(&svm->vcpu, 0);
1566
1567 return NULL;
1568}
1569
Joerg Roedel7597f122010-02-19 16:23:00 +01001570static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001571{
Joerg Roedel7597f122010-02-19 16:23:00 +01001572 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001573 kvm_release_page_dirty(page);
1574}
1575
Joerg Roedeld2477822010-03-01 15:34:34 +01001576static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001577{
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001578 u32 param = svm->vmcb->control.exit_info_1 & 1;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001579 u32 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001580 u32 t0, t1;
Joerg Roedeld2477822010-03-01 15:34:34 +01001581 int ret;
Joerg Roedel4c7da8c2010-02-19 16:23:05 +01001582 u8 val;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001583
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001584 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01001585 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001586
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001587 switch (msr) {
1588 case 0 ... 0x1fff:
1589 t0 = (msr * 2) % 8;
1590 t1 = msr / 8;
1591 break;
1592 case 0xc0000000 ... 0xc0001fff:
1593 t0 = (8192 + msr - 0xc0000000) * 2;
1594 t1 = (t0 / 8);
1595 t0 %= 8;
1596 break;
1597 case 0xc0010000 ... 0xc0011fff:
1598 t0 = (16384 + msr - 0xc0010000) * 2;
1599 t1 = (t0 / 8);
1600 t0 %= 8;
1601 break;
1602 default:
Joerg Roedeld2477822010-03-01 15:34:34 +01001603 ret = NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001604 goto out;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001605 }
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001606
Joerg Roedel4c7da8c2010-02-19 16:23:05 +01001607 if (!kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + t1, &val, 1))
Joerg Roedeld2477822010-03-01 15:34:34 +01001608 ret = val & ((1 << param) << t0) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001609
1610out:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001611 return ret;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001612}
1613
Joerg Roedel410e4d52009-08-07 11:49:44 +02001614static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001615{
Alexander Grafcf74a782008-11-25 20:17:08 +01001616 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001617
Joerg Roedel410e4d52009-08-07 11:49:44 +02001618 switch (exit_code) {
1619 case SVM_EXIT_INTR:
1620 case SVM_EXIT_NMI:
1621 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001622 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01001623 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001624 if (npt_enabled)
1625 return NESTED_EXIT_HOST;
1626 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001627 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Joerg Roedele0231712010-02-24 18:59:10 +01001628 /* When we're shadowing, trap PFs */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001629 if (!npt_enabled)
1630 return NESTED_EXIT_HOST;
1631 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001632 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
1633 nm_interception(svm);
1634 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001635 default:
1636 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001637 }
1638
Joerg Roedel410e4d52009-08-07 11:49:44 +02001639 return NESTED_EXIT_CONTINUE;
1640}
1641
1642/*
1643 * If this function returns true, this #vmexit was already handled
1644 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001645static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001646{
1647 u32 exit_code = svm->vmcb->control.exit_code;
1648 int vmexit = NESTED_EXIT_HOST;
1649
Alexander Grafcf74a782008-11-25 20:17:08 +01001650 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001651 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001652 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001653 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001654 case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR8: {
1655 u32 cr_bits = 1 << (exit_code - SVM_EXIT_READ_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001656 if (svm->nested.intercept_cr_read & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001657 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001658 break;
1659 }
1660 case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR8: {
1661 u32 cr_bits = 1 << (exit_code - SVM_EXIT_WRITE_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001662 if (svm->nested.intercept_cr_write & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001663 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001664 break;
1665 }
1666 case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR7: {
1667 u32 dr_bits = 1 << (exit_code - SVM_EXIT_READ_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001668 if (svm->nested.intercept_dr_read & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001669 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001670 break;
1671 }
1672 case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR7: {
1673 u32 dr_bits = 1 << (exit_code - SVM_EXIT_WRITE_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001674 if (svm->nested.intercept_dr_write & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001675 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001676 break;
1677 }
1678 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1679 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001680 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001681 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001682 break;
1683 }
1684 default: {
1685 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001686 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001687 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001688 }
1689 }
1690
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001691 return vmexit;
1692}
1693
1694static int nested_svm_exit_handled(struct vcpu_svm *svm)
1695{
1696 int vmexit;
1697
1698 vmexit = nested_svm_intercept(svm);
1699
1700 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001701 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001702
1703 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001704}
1705
Joerg Roedel0460a972009-08-07 11:49:31 +02001706static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
1707{
1708 struct vmcb_control_area *dst = &dst_vmcb->control;
1709 struct vmcb_control_area *from = &from_vmcb->control;
1710
1711 dst->intercept_cr_read = from->intercept_cr_read;
1712 dst->intercept_cr_write = from->intercept_cr_write;
1713 dst->intercept_dr_read = from->intercept_dr_read;
1714 dst->intercept_dr_write = from->intercept_dr_write;
1715 dst->intercept_exceptions = from->intercept_exceptions;
1716 dst->intercept = from->intercept;
1717 dst->iopm_base_pa = from->iopm_base_pa;
1718 dst->msrpm_base_pa = from->msrpm_base_pa;
1719 dst->tsc_offset = from->tsc_offset;
1720 dst->asid = from->asid;
1721 dst->tlb_ctl = from->tlb_ctl;
1722 dst->int_ctl = from->int_ctl;
1723 dst->int_vector = from->int_vector;
1724 dst->int_state = from->int_state;
1725 dst->exit_code = from->exit_code;
1726 dst->exit_code_hi = from->exit_code_hi;
1727 dst->exit_info_1 = from->exit_info_1;
1728 dst->exit_info_2 = from->exit_info_2;
1729 dst->exit_int_info = from->exit_int_info;
1730 dst->exit_int_info_err = from->exit_int_info_err;
1731 dst->nested_ctl = from->nested_ctl;
1732 dst->event_inj = from->event_inj;
1733 dst->event_inj_err = from->event_inj_err;
1734 dst->nested_cr3 = from->nested_cr3;
1735 dst->lbr_ctl = from->lbr_ctl;
1736}
1737
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001738static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001739{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001740 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001741 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02001742 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01001743 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01001744
Joerg Roedel17897f32009-10-09 16:08:29 +02001745 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
1746 vmcb->control.exit_info_1,
1747 vmcb->control.exit_info_2,
1748 vmcb->control.exit_int_info,
1749 vmcb->control.exit_int_info_err);
1750
Joerg Roedel7597f122010-02-19 16:23:00 +01001751 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001752 if (!nested_vmcb)
1753 return 1;
1754
Joerg Roedel06fc77722010-02-19 16:23:07 +01001755 /* Exit nested SVM mode */
1756 svm->nested.vmcb = 0;
1757
Alexander Grafcf74a782008-11-25 20:17:08 +01001758 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02001759 disable_gif(svm);
1760
1761 nested_vmcb->save.es = vmcb->save.es;
1762 nested_vmcb->save.cs = vmcb->save.cs;
1763 nested_vmcb->save.ss = vmcb->save.ss;
1764 nested_vmcb->save.ds = vmcb->save.ds;
1765 nested_vmcb->save.gdtr = vmcb->save.gdtr;
1766 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001767 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02001768 if (npt_enabled)
1769 nested_vmcb->save.cr3 = vmcb->save.cr3;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001770 else
1771 nested_vmcb->save.cr3 = svm->vcpu.arch.cr3;
Joerg Roedel33740e42009-08-07 11:49:29 +02001772 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001773 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Joerg Roedel33740e42009-08-07 11:49:29 +02001774 nested_vmcb->save.rflags = vmcb->save.rflags;
1775 nested_vmcb->save.rip = vmcb->save.rip;
1776 nested_vmcb->save.rsp = vmcb->save.rsp;
1777 nested_vmcb->save.rax = vmcb->save.rax;
1778 nested_vmcb->save.dr7 = vmcb->save.dr7;
1779 nested_vmcb->save.dr6 = vmcb->save.dr6;
1780 nested_vmcb->save.cpl = vmcb->save.cpl;
1781
1782 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
1783 nested_vmcb->control.int_vector = vmcb->control.int_vector;
1784 nested_vmcb->control.int_state = vmcb->control.int_state;
1785 nested_vmcb->control.exit_code = vmcb->control.exit_code;
1786 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
1787 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
1788 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
1789 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
1790 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Alexander Graf8d23c462009-10-09 16:08:25 +02001791
1792 /*
1793 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
1794 * to make sure that we do not lose injected events. So check event_inj
1795 * here and copy it to exit_int_info if it is valid.
1796 * Exit_int_info and event_inj can't be both valid because the case
1797 * below only happens on a VMRUN instruction intercept which has
1798 * no valid exit_int_info set.
1799 */
1800 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
1801 struct vmcb_control_area *nc = &nested_vmcb->control;
1802
1803 nc->exit_int_info = vmcb->control.event_inj;
1804 nc->exit_int_info_err = vmcb->control.event_inj_err;
1805 }
1806
Joerg Roedel33740e42009-08-07 11:49:29 +02001807 nested_vmcb->control.tlb_ctl = 0;
1808 nested_vmcb->control.event_inj = 0;
1809 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001810
1811 /* We always set V_INTR_MASKING and remember the old value in hflags */
1812 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
1813 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
1814
Alexander Grafcf74a782008-11-25 20:17:08 +01001815 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02001816 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01001817
Alexander Graf219b65d2009-06-15 15:21:25 +02001818 kvm_clear_exception_queue(&svm->vcpu);
1819 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01001820
1821 /* Restore selected save entries */
1822 svm->vmcb->save.es = hsave->save.es;
1823 svm->vmcb->save.cs = hsave->save.cs;
1824 svm->vmcb->save.ss = hsave->save.ss;
1825 svm->vmcb->save.ds = hsave->save.ds;
1826 svm->vmcb->save.gdtr = hsave->save.gdtr;
1827 svm->vmcb->save.idtr = hsave->save.idtr;
1828 svm->vmcb->save.rflags = hsave->save.rflags;
1829 svm_set_efer(&svm->vcpu, hsave->save.efer);
1830 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
1831 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
1832 if (npt_enabled) {
1833 svm->vmcb->save.cr3 = hsave->save.cr3;
1834 svm->vcpu.arch.cr3 = hsave->save.cr3;
1835 } else {
1836 kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
1837 }
1838 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
1839 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
1840 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
1841 svm->vmcb->save.dr7 = 0;
1842 svm->vmcb->save.cpl = 0;
1843 svm->vmcb->control.exit_int_info = 0;
1844
Joerg Roedel7597f122010-02-19 16:23:00 +01001845 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01001846
1847 kvm_mmu_reset_context(&svm->vcpu);
1848 kvm_mmu_load(&svm->vcpu);
1849
1850 return 0;
1851}
Alexander Graf3d6368e2008-11-25 20:17:07 +01001852
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001853static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01001854{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001855 u32 *nested_msrpm;
Joerg Roedel7597f122010-02-19 16:23:00 +01001856 struct page *page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001857 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001858
Joerg Roedel7597f122010-02-19 16:23:00 +01001859 nested_msrpm = nested_svm_map(svm, svm->nested.vmcb_msrpm, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001860 if (!nested_msrpm)
1861 return false;
1862
Joerg Roedele0231712010-02-24 18:59:10 +01001863 for (i = 0; i < PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER) / 4; i++)
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001864 svm->nested.msrpm[i] = svm->msrpm[i] | nested_msrpm[i];
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001865
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001866 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001867
Joerg Roedel7597f122010-02-19 16:23:00 +01001868 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001869
1870 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001871}
1872
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001873static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01001874{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001875 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001876 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02001877 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01001878 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01001879 u64 vmcb_gpa;
1880
1881 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001882
Joerg Roedel7597f122010-02-19 16:23:00 +01001883 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001884 if (!nested_vmcb)
1885 return false;
1886
Joerg Roedelecf14052010-02-24 18:59:13 +01001887 trace_kvm_nested_vmrun(svm->vmcb->save.rip - 3, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02001888 nested_vmcb->save.rip,
1889 nested_vmcb->control.int_ctl,
1890 nested_vmcb->control.event_inj,
1891 nested_vmcb->control.nested_ctl);
1892
Joerg Roedel2e554e82010-02-24 18:59:14 +01001893 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr_read,
1894 nested_vmcb->control.intercept_cr_write,
1895 nested_vmcb->control.intercept_exceptions,
1896 nested_vmcb->control.intercept);
1897
Alexander Graf3d6368e2008-11-25 20:17:07 +01001898 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02001899 kvm_clear_exception_queue(&svm->vcpu);
1900 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001901
Joerg Roedele0231712010-02-24 18:59:10 +01001902 /*
1903 * Save the old vmcb, so we don't need to pick what we save, but can
1904 * restore everything when a VMEXIT occurs
1905 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02001906 hsave->save.es = vmcb->save.es;
1907 hsave->save.cs = vmcb->save.cs;
1908 hsave->save.ss = vmcb->save.ss;
1909 hsave->save.ds = vmcb->save.ds;
1910 hsave->save.gdtr = vmcb->save.gdtr;
1911 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02001912 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02001913 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02001914 hsave->save.cr4 = svm->vcpu.arch.cr4;
1915 hsave->save.rflags = vmcb->save.rflags;
1916 hsave->save.rip = svm->next_rip;
1917 hsave->save.rsp = vmcb->save.rsp;
1918 hsave->save.rax = vmcb->save.rax;
1919 if (npt_enabled)
1920 hsave->save.cr3 = vmcb->save.cr3;
1921 else
1922 hsave->save.cr3 = svm->vcpu.arch.cr3;
1923
Joerg Roedel0460a972009-08-07 11:49:31 +02001924 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001925
1926 if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
1927 svm->vcpu.arch.hflags |= HF_HIF_MASK;
1928 else
1929 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
1930
1931 /* Load the nested guest state */
1932 svm->vmcb->save.es = nested_vmcb->save.es;
1933 svm->vmcb->save.cs = nested_vmcb->save.cs;
1934 svm->vmcb->save.ss = nested_vmcb->save.ss;
1935 svm->vmcb->save.ds = nested_vmcb->save.ds;
1936 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
1937 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
1938 svm->vmcb->save.rflags = nested_vmcb->save.rflags;
1939 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
1940 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
1941 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
1942 if (npt_enabled) {
1943 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
1944 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01001945 } else
Alexander Graf3d6368e2008-11-25 20:17:07 +01001946 kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01001947
1948 /* Guest paging mode is active - reset mmu */
1949 kvm_mmu_reset_context(&svm->vcpu);
1950
Joerg Roedeldefbba52009-08-07 11:49:30 +02001951 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001952 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
1953 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
1954 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01001955
Alexander Graf3d6368e2008-11-25 20:17:07 +01001956 /* In case we don't even reach vcpu_run, the fields are not updated */
1957 svm->vmcb->save.rax = nested_vmcb->save.rax;
1958 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
1959 svm->vmcb->save.rip = nested_vmcb->save.rip;
1960 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
1961 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
1962 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
1963
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001964 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001965
Joerg Roedelaad42c62009-08-07 11:49:34 +02001966 /* cache intercepts */
1967 svm->nested.intercept_cr_read = nested_vmcb->control.intercept_cr_read;
1968 svm->nested.intercept_cr_write = nested_vmcb->control.intercept_cr_write;
1969 svm->nested.intercept_dr_read = nested_vmcb->control.intercept_dr_read;
1970 svm->nested.intercept_dr_write = nested_vmcb->control.intercept_dr_write;
1971 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
1972 svm->nested.intercept = nested_vmcb->control.intercept;
1973
Alexander Graf3d6368e2008-11-25 20:17:07 +01001974 force_new_asid(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001975 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001976 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
1977 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
1978 else
1979 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
1980
Joerg Roedel88ab24a2010-02-19 16:23:06 +01001981 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
1982 /* We only want the cr8 intercept bits of the guest */
1983 svm->vmcb->control.intercept_cr_read &= ~INTERCEPT_CR8_MASK;
1984 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
1985 }
1986
Joerg Roedele0231712010-02-24 18:59:10 +01001987 /*
1988 * We don't want a nested guest to be more powerful than the guest, so
1989 * all intercepts are ORed
1990 */
Joerg Roedel88ab24a2010-02-19 16:23:06 +01001991 svm->vmcb->control.intercept_cr_read |=
1992 nested_vmcb->control.intercept_cr_read;
1993 svm->vmcb->control.intercept_cr_write |=
1994 nested_vmcb->control.intercept_cr_write;
1995 svm->vmcb->control.intercept_dr_read |=
1996 nested_vmcb->control.intercept_dr_read;
1997 svm->vmcb->control.intercept_dr_write |=
1998 nested_vmcb->control.intercept_dr_write;
1999 svm->vmcb->control.intercept_exceptions |=
2000 nested_vmcb->control.intercept_exceptions;
2001
2002 svm->vmcb->control.intercept |= nested_vmcb->control.intercept;
2003
2004 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002005 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2006 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2007 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002008 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2009 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2010
Joerg Roedel7597f122010-02-19 16:23:00 +01002011 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002012
Joerg Roedel06fc77722010-02-19 16:23:07 +01002013 /* nested_vmcb is our indicator if nested SVM is activated */
2014 svm->nested.vmcb = vmcb_gpa;
2015
Joerg Roedel2af91942009-08-07 11:49:28 +02002016 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002017
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002018 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002019}
2020
Joerg Roedel9966bf62009-08-07 11:49:40 +02002021static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002022{
2023 to_vmcb->save.fs = from_vmcb->save.fs;
2024 to_vmcb->save.gs = from_vmcb->save.gs;
2025 to_vmcb->save.tr = from_vmcb->save.tr;
2026 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2027 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2028 to_vmcb->save.star = from_vmcb->save.star;
2029 to_vmcb->save.lstar = from_vmcb->save.lstar;
2030 to_vmcb->save.cstar = from_vmcb->save.cstar;
2031 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2032 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2033 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2034 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002035}
2036
Avi Kivity851ba692009-08-24 11:10:17 +03002037static int vmload_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(nested_vmcb, svm->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 vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002059{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002060 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002061 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002062
Alexander Graf55426752008-11-25 20:17:06 +01002063 if (nested_svm_check_permissions(svm))
2064 return 1;
2065
2066 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2067 skip_emulated_instruction(&svm->vcpu);
2068
Joerg Roedel7597f122010-02-19 16:23:00 +01002069 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002070 if (!nested_vmcb)
2071 return 1;
2072
2073 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002074 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002075
2076 return 1;
2077}
2078
Avi Kivity851ba692009-08-24 11:10:17 +03002079static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002080{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002081 if (nested_svm_check_permissions(svm))
2082 return 1;
2083
2084 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2085 skip_emulated_instruction(&svm->vcpu);
2086
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002087 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002088 return 1;
2089
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002090 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002091 goto failed;
2092
2093 return 1;
2094
2095failed:
2096
2097 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2098 svm->vmcb->control.exit_code_hi = 0;
2099 svm->vmcb->control.exit_info_1 = 0;
2100 svm->vmcb->control.exit_info_2 = 0;
2101
2102 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002103
2104 return 1;
2105}
2106
Avi Kivity851ba692009-08-24 11:10:17 +03002107static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002108{
2109 if (nested_svm_check_permissions(svm))
2110 return 1;
2111
2112 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2113 skip_emulated_instruction(&svm->vcpu);
2114
Joerg Roedel2af91942009-08-07 11:49:28 +02002115 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002116
2117 return 1;
2118}
2119
Avi Kivity851ba692009-08-24 11:10:17 +03002120static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002121{
2122 if (nested_svm_check_permissions(svm))
2123 return 1;
2124
2125 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2126 skip_emulated_instruction(&svm->vcpu);
2127
Joerg Roedel2af91942009-08-07 11:49:28 +02002128 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002129
2130 /* After a CLGI no interrupts should come */
2131 svm_clear_vintr(svm);
2132 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2133
2134 return 1;
2135}
2136
Avi Kivity851ba692009-08-24 11:10:17 +03002137static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002138{
2139 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002140
Joerg Roedelec1ff792009-10-09 16:08:31 +02002141 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
2142 vcpu->arch.regs[VCPU_REGS_RAX]);
2143
Alexander Grafff092382009-06-15 15:21:24 +02002144 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2145 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
2146
2147 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2148 skip_emulated_instruction(&svm->vcpu);
2149 return 1;
2150}
2151
Joerg Roedel532a46b2009-10-09 16:08:32 +02002152static int skinit_interception(struct vcpu_svm *svm)
2153{
2154 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
2155
2156 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2157 return 1;
2158}
2159
Avi Kivity851ba692009-08-24 11:10:17 +03002160static int invalid_op_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002161{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002162 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002163 return 1;
2164}
2165
Avi Kivity851ba692009-08-24 11:10:17 +03002166static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002167{
Izik Eidus37817f22008-03-24 23:14:53 +02002168 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002169 int reason;
2170 int int_type = svm->vmcb->control.exit_int_info &
2171 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002172 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002173 uint32_t type =
2174 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2175 uint32_t idt_v =
2176 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Izik Eidus37817f22008-03-24 23:14:53 +02002177
2178 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002179
Izik Eidus37817f22008-03-24 23:14:53 +02002180 if (svm->vmcb->control.exit_info_2 &
2181 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002182 reason = TASK_SWITCH_IRET;
2183 else if (svm->vmcb->control.exit_info_2 &
2184 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2185 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002186 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002187 reason = TASK_SWITCH_GATE;
2188 else
2189 reason = TASK_SWITCH_CALL;
2190
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002191 if (reason == TASK_SWITCH_GATE) {
2192 switch (type) {
2193 case SVM_EXITINTINFO_TYPE_NMI:
2194 svm->vcpu.arch.nmi_injected = false;
2195 break;
2196 case SVM_EXITINTINFO_TYPE_EXEPT:
2197 kvm_clear_exception_queue(&svm->vcpu);
2198 break;
2199 case SVM_EXITINTINFO_TYPE_INTR:
2200 kvm_clear_interrupt_queue(&svm->vcpu);
2201 break;
2202 default:
2203 break;
2204 }
2205 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002206
Gleb Natapov8317c292009-04-12 13:37:02 +03002207 if (reason != TASK_SWITCH_GATE ||
2208 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2209 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002210 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2211 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002212
2213 return kvm_task_switch(&svm->vcpu, tss_selector, reason);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002214}
2215
Avi Kivity851ba692009-08-24 11:10:17 +03002216static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002217{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002218 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002219 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002220 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002221}
2222
Avi Kivity851ba692009-08-24 11:10:17 +03002223static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002224{
2225 ++svm->vcpu.stat.nmi_window_exits;
2226 svm->vmcb->control.intercept &= ~(1UL << INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002227 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002228 return 1;
2229}
2230
Avi Kivity851ba692009-08-24 11:10:17 +03002231static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002232{
Avi Kivity851ba692009-08-24 11:10:17 +03002233 if (emulate_instruction(&svm->vcpu, 0, 0, 0) != EMULATE_DONE)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002234 pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
2235 return 1;
2236}
2237
Avi Kivity851ba692009-08-24 11:10:17 +03002238static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002239{
Avi Kivity851ba692009-08-24 11:10:17 +03002240 if (emulate_instruction(&svm->vcpu, 0, 0, 0) != EMULATE_DONE)
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002241 pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002242 return 1;
2243}
2244
Avi Kivity851ba692009-08-24 11:10:17 +03002245static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01002246{
Avi Kivity851ba692009-08-24 11:10:17 +03002247 struct kvm_run *kvm_run = svm->vcpu.run;
2248
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002249 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
2250 /* instruction emulation calls kvm_set_cr8() */
Avi Kivity851ba692009-08-24 11:10:17 +03002251 emulate_instruction(&svm->vcpu, 0, 0, 0);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002252 if (irqchip_in_kernel(svm->vcpu.kvm)) {
2253 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
Joerg Roedel1d075432007-12-06 21:02:25 +01002254 return 1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002255 }
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002256 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
2257 return 1;
Joerg Roedel1d075432007-12-06 21:02:25 +01002258 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2259 return 0;
2260}
2261
Avi Kivity6aa8b732006-12-10 02:21:36 -08002262static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
2263{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002264 struct vcpu_svm *svm = to_svm(vcpu);
2265
Avi Kivity6aa8b732006-12-10 02:21:36 -08002266 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302267 case MSR_IA32_TSC: {
Joerg Roedel20824f32009-09-16 15:24:18 +02002268 u64 tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002269
Joerg Roedel20824f32009-09-16 15:24:18 +02002270 if (is_nested(svm))
2271 tsc_offset = svm->nested.hsave->control.tsc_offset;
2272 else
2273 tsc_offset = svm->vmcb->control.tsc_offset;
2274
2275 *data = tsc_offset + native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002276 break;
2277 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08002278 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002279 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002280 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08002281#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002282 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002283 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002284 break;
2285 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002286 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002287 break;
2288 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002289 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002290 break;
2291 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002292 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002293 break;
2294#endif
2295 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002296 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002297 break;
2298 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002299 *data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002300 break;
2301 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002302 *data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002303 break;
Joerg Roedele0231712010-02-24 18:59:10 +01002304 /*
2305 * Nobody will change the following 5 values in the VMCB so we can
2306 * safely return them on rdmsr. They will always be 0 until LBRV is
2307 * implemented.
2308 */
Joerg Roedela2938c82008-02-13 16:30:28 +01002309 case MSR_IA32_DEBUGCTLMSR:
2310 *data = svm->vmcb->save.dbgctl;
2311 break;
2312 case MSR_IA32_LASTBRANCHFROMIP:
2313 *data = svm->vmcb->save.br_from;
2314 break;
2315 case MSR_IA32_LASTBRANCHTOIP:
2316 *data = svm->vmcb->save.br_to;
2317 break;
2318 case MSR_IA32_LASTINTFROMIP:
2319 *data = svm->vmcb->save.last_excp_from;
2320 break;
2321 case MSR_IA32_LASTINTTOIP:
2322 *data = svm->vmcb->save.last_excp_to;
2323 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002324 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002325 *data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002326 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002327 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002328 *data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002329 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01002330 case MSR_IA32_UCODE_REV:
2331 *data = 0x01000065;
2332 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002334 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002335 }
2336 return 0;
2337}
2338
Avi Kivity851ba692009-08-24 11:10:17 +03002339static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002340{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002341 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002342 u64 data;
2343
Avi Kivity59200272010-01-25 19:47:02 +02002344 if (svm_get_msr(&svm->vcpu, ecx, &data)) {
2345 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002346 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002347 } else {
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002348 trace_kvm_msr_read(ecx, data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002349
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002350 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002351 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002352 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002353 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002354 }
2355 return 1;
2356}
2357
Joerg Roedel4a810182010-02-24 18:59:15 +01002358static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2359{
2360 struct vcpu_svm *svm = to_svm(vcpu);
2361 int svm_dis, chg_mask;
2362
2363 if (data & ~SVM_VM_CR_VALID_MASK)
2364 return 1;
2365
2366 chg_mask = SVM_VM_CR_VALID_MASK;
2367
2368 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2369 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2370
2371 svm->nested.vm_cr_msr &= ~chg_mask;
2372 svm->nested.vm_cr_msr |= (data & chg_mask);
2373
2374 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2375
2376 /* check for svm_disable while efer.svme is set */
2377 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2378 return 1;
2379
2380 return 0;
2381}
2382
Avi Kivity6aa8b732006-12-10 02:21:36 -08002383static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
2384{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002385 struct vcpu_svm *svm = to_svm(vcpu);
2386
Avi Kivity6aa8b732006-12-10 02:21:36 -08002387 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302388 case MSR_IA32_TSC: {
Joerg Roedel20824f32009-09-16 15:24:18 +02002389 u64 tsc_offset = data - native_read_tsc();
2390 u64 g_tsc_offset = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002391
Joerg Roedel20824f32009-09-16 15:24:18 +02002392 if (is_nested(svm)) {
2393 g_tsc_offset = svm->vmcb->control.tsc_offset -
2394 svm->nested.hsave->control.tsc_offset;
2395 svm->nested.hsave->control.tsc_offset = tsc_offset;
2396 }
2397
2398 svm->vmcb->control.tsc_offset = tsc_offset + g_tsc_offset;
2399
Avi Kivity6aa8b732006-12-10 02:21:36 -08002400 break;
2401 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08002402 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002403 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002404 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08002405#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002406 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002407 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002408 break;
2409 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002410 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002411 break;
2412 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002413 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002414 break;
2415 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002416 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002417 break;
2418#endif
2419 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002420 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002421 break;
2422 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002423 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002424 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002425 break;
2426 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002427 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002428 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002429 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002430 case MSR_IA32_DEBUGCTLMSR:
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002431 if (!svm_has(SVM_FEATURE_LBRV)) {
2432 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002433 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002434 break;
2435 }
2436 if (data & DEBUGCTL_RESERVED_BITS)
2437 return 1;
2438
2439 svm->vmcb->save.dbgctl = data;
2440 if (data & (1ULL<<0))
2441 svm_enable_lbrv(svm);
2442 else
2443 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01002444 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002445 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002446 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002447 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002448 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002449 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002450 case MSR_VM_IGNNE:
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002451 pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
2452 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002453 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002454 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002455 }
2456 return 0;
2457}
2458
Avi Kivity851ba692009-08-24 11:10:17 +03002459static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002460{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002461 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002462 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002463 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002464
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002465
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002466 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Avi Kivity59200272010-01-25 19:47:02 +02002467 if (svm_set_msr(&svm->vcpu, ecx, data)) {
2468 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002469 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002470 } else {
2471 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10002472 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02002473 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002474 return 1;
2475}
2476
Avi Kivity851ba692009-08-24 11:10:17 +03002477static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002478{
Rusty Russelle756fc62007-07-30 20:07:08 +10002479 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03002480 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002481 else
Avi Kivity851ba692009-08-24 11:10:17 +03002482 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002483}
2484
Avi Kivity851ba692009-08-24 11:10:17 +03002485static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08002486{
Avi Kivity851ba692009-08-24 11:10:17 +03002487 struct kvm_run *kvm_run = svm->vcpu.run;
2488
Alexander Graff0b85052008-11-25 20:17:01 +01002489 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03002490 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -08002491 /*
2492 * If the user space waits to inject interrupts, exit as soon as
2493 * possible
2494 */
Gleb Natapov80618232009-04-21 17:44:56 +03002495 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
2496 kvm_run->request_interrupt_window &&
2497 !kvm_cpu_has_interrupt(&svm->vcpu)) {
Rusty Russelle756fc62007-07-30 20:07:08 +10002498 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002499 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
2500 return 0;
2501 }
2502
2503 return 1;
2504}
2505
Mark Langsdorf565d0992009-10-06 14:25:02 -05002506static int pause_interception(struct vcpu_svm *svm)
2507{
2508 kvm_vcpu_on_spin(&(svm->vcpu));
2509 return 1;
2510}
2511
Avi Kivity851ba692009-08-24 11:10:17 +03002512static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
Joerg Roedele0231712010-02-24 18:59:10 +01002513 [SVM_EXIT_READ_CR0] = emulate_on_interception,
2514 [SVM_EXIT_READ_CR3] = emulate_on_interception,
2515 [SVM_EXIT_READ_CR4] = emulate_on_interception,
2516 [SVM_EXIT_READ_CR8] = emulate_on_interception,
Avi Kivityd2251572010-01-06 10:55:27 +02002517 [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002518 [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
2519 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
2520 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
2521 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
2522 [SVM_EXIT_READ_DR0] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002523 [SVM_EXIT_READ_DR1] = emulate_on_interception,
2524 [SVM_EXIT_READ_DR2] = emulate_on_interception,
2525 [SVM_EXIT_READ_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002526 [SVM_EXIT_READ_DR4] = emulate_on_interception,
2527 [SVM_EXIT_READ_DR5] = emulate_on_interception,
2528 [SVM_EXIT_READ_DR6] = emulate_on_interception,
2529 [SVM_EXIT_READ_DR7] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002530 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
2531 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
2532 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
2533 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002534 [SVM_EXIT_WRITE_DR4] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002535 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002536 [SVM_EXIT_WRITE_DR6] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002537 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002538 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
2539 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002540 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002541 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
2542 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
2543 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
2544 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02002545 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002546 [SVM_EXIT_SMI] = nop_on_interception,
2547 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08002548 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002549 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002550 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002551 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05002552 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002553 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03002554 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02002555 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002556 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002557 [SVM_EXIT_MSR] = msr_interception,
2558 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002559 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01002560 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02002561 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01002562 [SVM_EXIT_VMLOAD] = vmload_interception,
2563 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01002564 [SVM_EXIT_STGI] = stgi_interception,
2565 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02002566 [SVM_EXIT_SKINIT] = skinit_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002567 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01002568 [SVM_EXIT_MONITOR] = invalid_op_interception,
2569 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002570 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002571};
2572
Avi Kivity851ba692009-08-24 11:10:17 +03002573static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002574{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002575 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002576 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002577 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002578
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002579 trace_kvm_exit(exit_code, svm->vmcb->save.rip);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002580
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002581 if (unlikely(svm->nested.exit_required)) {
2582 nested_svm_vmexit(svm);
2583 svm->nested.exit_required = false;
2584
2585 return 1;
2586 }
2587
Alexander Grafcf74a782008-11-25 20:17:08 +01002588 if (is_nested(svm)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02002589 int vmexit;
2590
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02002591 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
2592 svm->vmcb->control.exit_info_1,
2593 svm->vmcb->control.exit_info_2,
2594 svm->vmcb->control.exit_int_info,
2595 svm->vmcb->control.exit_int_info_err);
2596
Joerg Roedel410e4d52009-08-07 11:49:44 +02002597 vmexit = nested_svm_exit_special(svm);
2598
2599 if (vmexit == NESTED_EXIT_CONTINUE)
2600 vmexit = nested_svm_exit_handled(svm);
2601
2602 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01002603 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01002604 }
2605
Joerg Roedela5c38322009-08-07 11:49:32 +02002606 svm_complete_interrupts(svm);
2607
Avi Kivity888f9f32010-01-10 12:14:04 +02002608 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR0_MASK))
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002609 vcpu->arch.cr0 = svm->vmcb->save.cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02002610 if (npt_enabled)
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002611 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002612
2613 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
2614 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2615 kvm_run->fail_entry.hardware_entry_failure_reason
2616 = svm->vmcb->control.exit_code;
2617 return 0;
2618 }
2619
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002620 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002621 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002622 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002623 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
2624 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002625 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002626 exit_code);
2627
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02002628 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08002629 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002630 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03002631 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002632 return 0;
2633 }
2634
Avi Kivity851ba692009-08-24 11:10:17 +03002635 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002636}
2637
2638static void reload_tss(struct kvm_vcpu *vcpu)
2639{
2640 int cpu = raw_smp_processor_id();
2641
Tejun Heo0fe1e002009-10-29 22:34:14 +09002642 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
2643 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644 load_TR_desc();
2645}
2646
Rusty Russelle756fc62007-07-30 20:07:08 +10002647static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002648{
2649 int cpu = raw_smp_processor_id();
2650
Tejun Heo0fe1e002009-10-29 22:34:14 +09002651 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002652
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002653 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03002654 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09002655 if (svm->asid_generation != sd->asid_generation)
2656 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002657}
2658
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002659static void svm_inject_nmi(struct kvm_vcpu *vcpu)
2660{
2661 struct vcpu_svm *svm = to_svm(vcpu);
2662
2663 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
2664 vcpu->arch.hflags |= HF_NMI_MASK;
2665 svm->vmcb->control.intercept |= (1UL << INTERCEPT_IRET);
2666 ++vcpu->stat.nmi_injections;
2667}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002668
Eddie Dong85f455f2007-07-06 12:20:49 +03002669static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002670{
2671 struct vmcb_control_area *control;
2672
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002673 trace_kvm_inj_virq(irq);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002674
Avi Kivityfa89a812008-09-01 15:57:51 +03002675 ++svm->vcpu.stat.irq_injections;
Rusty Russelle756fc62007-07-30 20:07:08 +10002676 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03002677 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002678 control->int_ctl &= ~V_INTR_PRIO_MASK;
2679 control->int_ctl |= V_IRQ_MASK |
2680 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
2681}
2682
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002683static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03002684{
2685 struct vcpu_svm *svm = to_svm(vcpu);
2686
Joerg Roedel2af91942009-08-07 11:49:28 +02002687 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01002688
Alexander Graf219b65d2009-06-15 15:21:25 +02002689 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
2690 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002691}
2692
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002693static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
2694{
2695 struct vcpu_svm *svm = to_svm(vcpu);
2696
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002697 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
2698 return;
2699
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002700 if (irr == -1)
2701 return;
2702
2703 if (tpr >= irr)
2704 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK;
2705}
2706
2707static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02002708{
2709 struct vcpu_svm *svm = to_svm(vcpu);
2710 struct vmcb *vmcb = svm->vmcb;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002711 return !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
2712 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
Joerg Roedelaaacfc92008-04-16 16:51:18 +02002713}
2714
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002715static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
2716{
2717 struct vcpu_svm *svm = to_svm(vcpu);
2718
2719 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
2720}
2721
2722static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
2723{
2724 struct vcpu_svm *svm = to_svm(vcpu);
2725
2726 if (masked) {
2727 svm->vcpu.arch.hflags |= HF_NMI_MASK;
2728 svm->vmcb->control.intercept |= (1UL << INTERCEPT_IRET);
2729 } else {
2730 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
2731 svm->vmcb->control.intercept &= ~(1UL << INTERCEPT_IRET);
2732 }
2733}
2734
Gleb Natapov78646122009-03-23 12:12:11 +02002735static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
2736{
2737 struct vcpu_svm *svm = to_svm(vcpu);
2738 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02002739 int ret;
2740
2741 if (!gif_set(svm) ||
2742 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
2743 return 0;
2744
2745 ret = !!(vmcb->save.rflags & X86_EFLAGS_IF);
2746
2747 if (is_nested(svm))
2748 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
2749
2750 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02002751}
2752
Gleb Natapov9222be12009-04-23 17:14:37 +03002753static void enable_irq_window(struct kvm_vcpu *vcpu)
2754{
Alexander Graf219b65d2009-06-15 15:21:25 +02002755 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02002756
Joerg Roedele0231712010-02-24 18:59:10 +01002757 /*
2758 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
2759 * 1, because that's a separate STGI/VMRUN intercept. The next time we
2760 * get that intercept, this function will be called again though and
2761 * we'll get the vintr intercept.
2762 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01002763 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02002764 svm_set_vintr(svm);
2765 svm_inject_irq(svm, 0x0);
2766 }
Gleb Natapov9222be12009-04-23 17:14:37 +03002767}
2768
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002769static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002770{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002771 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03002772
Gleb Natapov44c11432009-05-11 13:35:52 +03002773 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
2774 == HF_NMI_MASK)
2775 return; /* IRET will cause a vm exit */
2776
Joerg Roedele0231712010-02-24 18:59:10 +01002777 /*
2778 * Something prevents NMI from been injected. Single step over possible
2779 * problem (IRET or exception injection or interrupt shadow)
2780 */
Joerg Roedel887f5002010-02-24 18:59:12 +01002781 if (gif_set(svm) && nested_svm_nmi(svm)) {
2782 svm->nmi_singlestep = true;
2783 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
2784 update_db_intercept(vcpu);
2785 }
Eddie Dong85f455f2007-07-06 12:20:49 +03002786}
2787
Izik Eiduscbc94022007-10-25 00:29:55 +02002788static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
2789{
2790 return 0;
2791}
2792
Avi Kivityd9e368d2007-06-07 19:18:30 +03002793static void svm_flush_tlb(struct kvm_vcpu *vcpu)
2794{
2795 force_new_asid(vcpu);
2796}
2797
Avi Kivity04d2cc72007-09-10 18:10:54 +03002798static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
2799{
2800}
2801
Joerg Roedeld7bf8222008-04-16 16:51:17 +02002802static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
2803{
2804 struct vcpu_svm *svm = to_svm(vcpu);
2805
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002806 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
2807 return;
2808
Joerg Roedeld7bf8222008-04-16 16:51:17 +02002809 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) {
2810 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03002811 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02002812 }
2813}
2814
Joerg Roedel649d6862008-04-16 16:51:15 +02002815static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
2816{
2817 struct vcpu_svm *svm = to_svm(vcpu);
2818 u64 cr8;
2819
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002820 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
2821 return;
2822
Joerg Roedel649d6862008-04-16 16:51:15 +02002823 cr8 = kvm_get_cr8(vcpu);
2824 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
2825 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
2826}
2827
Gleb Natapov9222be12009-04-23 17:14:37 +03002828static void svm_complete_interrupts(struct vcpu_svm *svm)
2829{
2830 u8 vector;
2831 int type;
2832 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01002833 unsigned int3_injected = svm->int3_injected;
2834
2835 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03002836
Gleb Natapov44c11432009-05-11 13:35:52 +03002837 if (svm->vcpu.arch.hflags & HF_IRET_MASK)
2838 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
2839
Gleb Natapov9222be12009-04-23 17:14:37 +03002840 svm->vcpu.arch.nmi_injected = false;
2841 kvm_clear_exception_queue(&svm->vcpu);
2842 kvm_clear_interrupt_queue(&svm->vcpu);
2843
2844 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
2845 return;
2846
2847 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
2848 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
2849
2850 switch (type) {
2851 case SVM_EXITINTINFO_TYPE_NMI:
2852 svm->vcpu.arch.nmi_injected = true;
2853 break;
2854 case SVM_EXITINTINFO_TYPE_EXEPT:
Alexander Graf219b65d2009-06-15 15:21:25 +02002855 if (is_nested(svm))
2856 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01002857 /*
2858 * In case of software exceptions, do not reinject the vector,
2859 * but re-execute the instruction instead. Rewind RIP first
2860 * if we emulated INT3 before.
2861 */
2862 if (kvm_exception_is_soft(vector)) {
2863 if (vector == BP_VECTOR && int3_injected &&
2864 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
2865 kvm_rip_write(&svm->vcpu,
2866 kvm_rip_read(&svm->vcpu) -
2867 int3_injected);
Gleb Natapov9222be12009-04-23 17:14:37 +03002868 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01002869 }
Gleb Natapov9222be12009-04-23 17:14:37 +03002870 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
2871 u32 err = svm->vmcb->control.exit_int_info_err;
2872 kvm_queue_exception_e(&svm->vcpu, vector, err);
2873
2874 } else
2875 kvm_queue_exception(&svm->vcpu, vector);
2876 break;
2877 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002878 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03002879 break;
2880 default:
2881 break;
2882 }
2883}
2884
Avi Kivity80e31d42008-07-14 14:44:59 +03002885#ifdef CONFIG_X86_64
2886#define R "r"
2887#else
2888#define R "e"
2889#endif
2890
Avi Kivity851ba692009-08-24 11:10:17 +03002891static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002892{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002893 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002894 u16 fs_selector;
2895 u16 gs_selector;
2896 u16 ldt_selector;
Avi Kivityd9e368d2007-06-07 19:18:30 +03002897
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002898 /*
2899 * A vmexit emulation is required before the vcpu can be executed
2900 * again.
2901 */
2902 if (unlikely(svm->nested.exit_required))
2903 return;
2904
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002905 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
2906 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
2907 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
2908
Rusty Russelle756fc62007-07-30 20:07:08 +10002909 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002910
Joerg Roedel649d6862008-04-16 16:51:15 +02002911 sync_lapic_to_cr8(vcpu);
2912
Avi Kivity6aa8b732006-12-10 02:21:36 -08002913 save_host_msrs(vcpu);
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002914 fs_selector = kvm_read_fs();
2915 gs_selector = kvm_read_gs();
2916 ldt_selector = kvm_read_ldt();
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02002917 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002918 /* required for live migration with NPT */
2919 if (npt_enabled)
2920 svm->vmcb->save.cr3 = vcpu->arch.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002921
Avi Kivity04d2cc72007-09-10 18:10:54 +03002922 clgi();
2923
2924 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08002925
Avi Kivity6aa8b732006-12-10 02:21:36 -08002926 asm volatile (
Avi Kivity80e31d42008-07-14 14:44:59 +03002927 "push %%"R"bp; \n\t"
2928 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
2929 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
2930 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
2931 "mov %c[rsi](%[svm]), %%"R"si \n\t"
2932 "mov %c[rdi](%[svm]), %%"R"di \n\t"
2933 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002934#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002935 "mov %c[r8](%[svm]), %%r8 \n\t"
2936 "mov %c[r9](%[svm]), %%r9 \n\t"
2937 "mov %c[r10](%[svm]), %%r10 \n\t"
2938 "mov %c[r11](%[svm]), %%r11 \n\t"
2939 "mov %c[r12](%[svm]), %%r12 \n\t"
2940 "mov %c[r13](%[svm]), %%r13 \n\t"
2941 "mov %c[r14](%[svm]), %%r14 \n\t"
2942 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002943#endif
2944
Avi Kivity6aa8b732006-12-10 02:21:36 -08002945 /* Enter guest mode */
Avi Kivity80e31d42008-07-14 14:44:59 +03002946 "push %%"R"ax \n\t"
2947 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002948 __ex(SVM_VMLOAD) "\n\t"
2949 __ex(SVM_VMRUN) "\n\t"
2950 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity80e31d42008-07-14 14:44:59 +03002951 "pop %%"R"ax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002952
2953 /* Save guest registers, load host registers */
Avi Kivity80e31d42008-07-14 14:44:59 +03002954 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
2955 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
2956 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
2957 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
2958 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
2959 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002960#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002961 "mov %%r8, %c[r8](%[svm]) \n\t"
2962 "mov %%r9, %c[r9](%[svm]) \n\t"
2963 "mov %%r10, %c[r10](%[svm]) \n\t"
2964 "mov %%r11, %c[r11](%[svm]) \n\t"
2965 "mov %%r12, %c[r12](%[svm]) \n\t"
2966 "mov %%r13, %c[r13](%[svm]) \n\t"
2967 "mov %%r14, %c[r14](%[svm]) \n\t"
2968 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002969#endif
Avi Kivity80e31d42008-07-14 14:44:59 +03002970 "pop %%"R"bp"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002971 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002972 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08002973 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002974 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
2975 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
2976 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
2977 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
2978 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
2979 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002980#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002981 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
2982 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
2983 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
2984 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
2985 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
2986 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
2987 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
2988 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002989#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02002990 : "cc", "memory"
Avi Kivity80e31d42008-07-14 14:44:59 +03002991 , R"bx", R"cx", R"dx", R"si", R"di"
Laurent Vivier54a08c02007-10-25 14:18:53 +02002992#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02002993 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
2994#endif
2995 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08002996
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002997 vcpu->arch.cr2 = svm->vmcb->save.cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002998 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
2999 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3000 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003001
Avi Kivityd6e88ae2008-07-10 16:53:33 +03003002 kvm_load_fs(fs_selector);
3003 kvm_load_gs(gs_selector);
3004 kvm_load_ldt(ldt_selector);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003005 load_host_msrs(vcpu);
3006
3007 reload_tss(vcpu);
3008
Avi Kivity56ba47d2007-11-07 17:14:18 +02003009 local_irq_disable();
3010
3011 stgi();
3012
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003013 sync_cr8_to_lapic(vcpu);
3014
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003015 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003016
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003017 if (npt_enabled) {
3018 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3019 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3020 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003021}
3022
Avi Kivity80e31d42008-07-14 14:44:59 +03003023#undef R
3024
Avi Kivity6aa8b732006-12-10 02:21:36 -08003025static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3026{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003027 struct vcpu_svm *svm = to_svm(vcpu);
3028
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003029 if (npt_enabled) {
3030 svm->vmcb->control.nested_cr3 = root;
3031 force_new_asid(vcpu);
3032 return;
3033 }
3034
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003035 svm->vmcb->save.cr3 = root;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003036 force_new_asid(vcpu);
3037}
3038
Avi Kivity6aa8b732006-12-10 02:21:36 -08003039static int is_disabled(void)
3040{
Joerg Roedel6031a612007-06-22 12:29:50 +03003041 u64 vm_cr;
3042
3043 rdmsrl(MSR_VM_CR, vm_cr);
3044 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3045 return 1;
3046
Avi Kivity6aa8b732006-12-10 02:21:36 -08003047 return 0;
3048}
3049
Ingo Molnar102d8322007-02-19 14:37:47 +02003050static void
3051svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3052{
3053 /*
3054 * Patch in the VMMCALL instruction:
3055 */
3056 hypercall[0] = 0x0f;
3057 hypercall[1] = 0x01;
3058 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003059}
3060
Yang, Sheng002c7f72007-07-31 14:23:01 +03003061static void svm_check_processor_compat(void *rtn)
3062{
3063 *(int *)rtn = 0;
3064}
3065
Avi Kivity774ead32007-12-26 13:57:04 +02003066static bool svm_cpu_has_accelerated_tpr(void)
3067{
3068 return false;
3069}
3070
Sheng Yang67253af2008-04-25 10:20:22 +08003071static int get_npt_level(void)
3072{
3073#ifdef CONFIG_X86_64
3074 return PT64_ROOT_LEVEL;
3075#else
3076 return PT32E_ROOT_LEVEL;
3077#endif
3078}
3079
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003080static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003081{
3082 return 0;
3083}
3084
Sheng Yang0e851882009-12-18 16:48:46 +08003085static void svm_cpuid_update(struct kvm_vcpu *vcpu)
3086{
3087}
3088
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003089static const struct trace_print_flags svm_exit_reasons_str[] = {
Joerg Roedele0231712010-02-24 18:59:10 +01003090 { SVM_EXIT_READ_CR0, "read_cr0" },
3091 { SVM_EXIT_READ_CR3, "read_cr3" },
3092 { SVM_EXIT_READ_CR4, "read_cr4" },
3093 { SVM_EXIT_READ_CR8, "read_cr8" },
3094 { SVM_EXIT_WRITE_CR0, "write_cr0" },
3095 { SVM_EXIT_WRITE_CR3, "write_cr3" },
3096 { SVM_EXIT_WRITE_CR4, "write_cr4" },
3097 { SVM_EXIT_WRITE_CR8, "write_cr8" },
3098 { SVM_EXIT_READ_DR0, "read_dr0" },
3099 { SVM_EXIT_READ_DR1, "read_dr1" },
3100 { SVM_EXIT_READ_DR2, "read_dr2" },
3101 { SVM_EXIT_READ_DR3, "read_dr3" },
3102 { SVM_EXIT_WRITE_DR0, "write_dr0" },
3103 { SVM_EXIT_WRITE_DR1, "write_dr1" },
3104 { SVM_EXIT_WRITE_DR2, "write_dr2" },
3105 { SVM_EXIT_WRITE_DR3, "write_dr3" },
3106 { SVM_EXIT_WRITE_DR5, "write_dr5" },
3107 { SVM_EXIT_WRITE_DR7, "write_dr7" },
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003108 { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
3109 { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
3110 { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
3111 { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
3112 { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
3113 { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
3114 { SVM_EXIT_INTR, "interrupt" },
3115 { SVM_EXIT_NMI, "nmi" },
3116 { SVM_EXIT_SMI, "smi" },
3117 { SVM_EXIT_INIT, "init" },
3118 { SVM_EXIT_VINTR, "vintr" },
3119 { SVM_EXIT_CPUID, "cpuid" },
3120 { SVM_EXIT_INVD, "invd" },
3121 { SVM_EXIT_HLT, "hlt" },
3122 { SVM_EXIT_INVLPG, "invlpg" },
3123 { SVM_EXIT_INVLPGA, "invlpga" },
3124 { SVM_EXIT_IOIO, "io" },
3125 { SVM_EXIT_MSR, "msr" },
3126 { SVM_EXIT_TASK_SWITCH, "task_switch" },
3127 { SVM_EXIT_SHUTDOWN, "shutdown" },
3128 { SVM_EXIT_VMRUN, "vmrun" },
3129 { SVM_EXIT_VMMCALL, "hypercall" },
3130 { SVM_EXIT_VMLOAD, "vmload" },
3131 { SVM_EXIT_VMSAVE, "vmsave" },
3132 { SVM_EXIT_STGI, "stgi" },
3133 { SVM_EXIT_CLGI, "clgi" },
3134 { SVM_EXIT_SKINIT, "skinit" },
3135 { SVM_EXIT_WBINVD, "wbinvd" },
3136 { SVM_EXIT_MONITOR, "monitor" },
3137 { SVM_EXIT_MWAIT, "mwait" },
3138 { SVM_EXIT_NPF, "npf" },
3139 { -1, NULL }
3140};
3141
Sheng Yang17cc3932010-01-05 19:02:27 +08003142static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02003143{
Sheng Yang17cc3932010-01-05 19:02:27 +08003144 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02003145}
3146
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003147static bool svm_rdtscp_supported(void)
3148{
3149 return false;
3150}
3151
Avi Kivity02daab22009-12-30 12:40:26 +02003152static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
3153{
3154 struct vcpu_svm *svm = to_svm(vcpu);
3155
Avi Kivity02daab22009-12-30 12:40:26 +02003156 svm->vmcb->control.intercept_exceptions |= 1 << NM_VECTOR;
Joerg Roedel66a562f2010-02-19 16:23:08 +01003157 if (is_nested(svm))
3158 svm->nested.hsave->control.intercept_exceptions |= 1 << NM_VECTOR;
3159 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02003160}
3161
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003162static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003163 .cpu_has_kvm_support = has_svm,
3164 .disabled_by_bios = is_disabled,
3165 .hardware_setup = svm_hardware_setup,
3166 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003167 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003168 .hardware_enable = svm_hardware_enable,
3169 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003170 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003171
3172 .vcpu_create = svm_create_vcpu,
3173 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003174 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003175
Avi Kivity04d2cc72007-09-10 18:10:54 +03003176 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003177 .vcpu_load = svm_vcpu_load,
3178 .vcpu_put = svm_vcpu_put,
3179
3180 .set_guest_debug = svm_guest_debug,
3181 .get_msr = svm_get_msr,
3182 .set_msr = svm_set_msr,
3183 .get_segment_base = svm_get_segment_base,
3184 .get_segment = svm_get_segment,
3185 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003186 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10003187 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02003188 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003189 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003190 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003191 .set_cr3 = svm_set_cr3,
3192 .set_cr4 = svm_set_cr4,
3193 .set_efer = svm_set_efer,
3194 .get_idt = svm_get_idt,
3195 .set_idt = svm_set_idt,
3196 .get_gdt = svm_get_gdt,
3197 .set_gdt = svm_set_gdt,
3198 .get_dr = svm_get_dr,
3199 .set_dr = svm_set_dr,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003200 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003201 .get_rflags = svm_get_rflags,
3202 .set_rflags = svm_set_rflags,
Avi Kivity6b52d182010-01-21 15:31:47 +02003203 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02003204 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003205
Avi Kivity6aa8b732006-12-10 02:21:36 -08003206 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003207
Avi Kivity6aa8b732006-12-10 02:21:36 -08003208 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003209 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003210 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04003211 .set_interrupt_shadow = svm_set_interrupt_shadow,
3212 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02003213 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003214 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003215 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02003216 .queue_exception = svm_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02003217 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003218 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003219 .get_nmi_mask = svm_get_nmi_mask,
3220 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003221 .enable_nmi_window = enable_nmi_window,
3222 .enable_irq_window = enable_irq_window,
3223 .update_cr8_intercept = update_cr8_intercept,
Izik Eiduscbc94022007-10-25 00:29:55 +02003224
3225 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003226 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003227 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003228
3229 .exit_reasons_str = svm_exit_reasons_str,
Sheng Yang17cc3932010-01-05 19:02:27 +08003230 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08003231
3232 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003233
3234 .rdtscp_supported = svm_rdtscp_supported,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003235};
3236
3237static int __init svm_init(void)
3238{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003239 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Rusty Russellc16f8622007-07-30 21:12:19 +10003240 THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003241}
3242
3243static void __exit svm_exit(void)
3244{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003245 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003246}
3247
3248module_init(svm_init)
3249module_exit(svm_exit)