blob: 2511664ff6717ffe34027738b4e693b86df46dee [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
Andre Przywara6bc31bd2010-04-11 23:07:28 +020047#define SVM_FEATURE_NPT (1 << 0)
48#define SVM_FEATURE_LBRV (1 << 1)
49#define SVM_FEATURE_SVML (1 << 2)
50#define SVM_FEATURE_NRIP (1 << 3)
51#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 Roedelce2ac082010-03-01 15:34:39 +010082 u64 vmcb_iopm;
Joerg Roedelaad42c62009-08-07 11:49:34 +020083
Joerg Roedelcd3ff652009-10-09 16:08:26 +020084 /* A VMEXIT is required but not yet emulated */
85 bool exit_required;
86
Joerg Roedelaad42c62009-08-07 11:49:34 +020087 /* cache for intercepts of the guest */
88 u16 intercept_cr_read;
89 u16 intercept_cr_write;
90 u16 intercept_dr_read;
91 u16 intercept_dr_write;
92 u32 intercept_exceptions;
93 u64 intercept;
94
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020095};
96
Joerg Roedel323c3d82010-03-01 15:34:37 +010097#define MSRPM_OFFSETS 16
98static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
99
Avi Kivity6c8166a2009-05-31 18:15:37 +0300100struct vcpu_svm {
101 struct kvm_vcpu vcpu;
102 struct vmcb *vmcb;
103 unsigned long vmcb_pa;
104 struct svm_cpu_data *svm_data;
105 uint64_t asid_generation;
106 uint64_t sysenter_esp;
107 uint64_t sysenter_eip;
108
109 u64 next_rip;
110
111 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
112 u64 host_gs_base;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300113
114 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300115
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200116 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200117
118 bool nmi_singlestep;
Jan Kiszka66b71382010-02-23 17:47:56 +0100119
120 unsigned int3_injected;
121 unsigned long int3_rip;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300122};
123
Joerg Roedel455716f2010-03-01 15:34:35 +0100124#define MSR_INVALID 0xffffffffU
125
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100126static struct svm_direct_access_msrs {
127 u32 index; /* Index of the MSR */
128 bool always; /* True if intercept is always on */
129} direct_access_msrs[] = {
130 { .index = MSR_K6_STAR, .always = true },
131 { .index = MSR_IA32_SYSENTER_CS, .always = true },
132#ifdef CONFIG_X86_64
133 { .index = MSR_GS_BASE, .always = true },
134 { .index = MSR_FS_BASE, .always = true },
135 { .index = MSR_KERNEL_GS_BASE, .always = true },
136 { .index = MSR_LSTAR, .always = true },
137 { .index = MSR_CSTAR, .always = true },
138 { .index = MSR_SYSCALL_MASK, .always = true },
139#endif
140 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
141 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
142 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
143 { .index = MSR_IA32_LASTINTTOIP, .always = false },
144 { .index = MSR_INVALID, .always = false },
145};
146
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100147/* enable NPT for AMD64 and X86 with PAE */
148#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
149static bool npt_enabled = true;
150#else
Joerg Roedele0231712010-02-24 18:59:10 +0100151static bool npt_enabled;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100152#endif
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100153static int npt = 1;
154
155module_param(npt, int, S_IRUGO);
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100156
Joerg Roedel4b6e4dc2009-08-07 11:49:48 +0200157static int nested = 1;
Alexander Graf236de052008-11-25 20:17:10 +0100158module_param(nested, int, S_IRUGO);
159
Joerg Roedel44874f82008-08-27 14:18:43 +0200160static void svm_flush_tlb(struct kvm_vcpu *vcpu);
Joerg Roedela5c38322009-08-07 11:49:32 +0200161static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity04d2cc72007-09-10 18:10:54 +0300162
Joerg Roedel410e4d52009-08-07 11:49:44 +0200163static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100164static int nested_svm_intercept(struct vcpu_svm *svm);
Alexander Grafcf74a782008-11-25 20:17:08 +0100165static int nested_svm_vmexit(struct vcpu_svm *svm);
Alexander Grafcf74a782008-11-25 20:17:08 +0100166static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
167 bool has_error_code, u32 error_code);
168
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400169static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
170{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000171 return container_of(vcpu, struct vcpu_svm, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400172}
173
Alexander Graf3d6368e2008-11-25 20:17:07 +0100174static inline bool is_nested(struct vcpu_svm *svm)
175{
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200176 return svm->nested.vmcb;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100177}
178
Joerg Roedel2af91942009-08-07 11:49:28 +0200179static inline void enable_gif(struct vcpu_svm *svm)
180{
181 svm->vcpu.arch.hflags |= HF_GIF_MASK;
182}
183
184static inline void disable_gif(struct vcpu_svm *svm)
185{
186 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
187}
188
189static inline bool gif_set(struct vcpu_svm *svm)
190{
191 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
192}
193
Harvey Harrison4866d5e2008-02-19 10:32:02 -0800194static unsigned long iopm_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800195
196struct kvm_ldttss_desc {
197 u16 limit0;
198 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100199 unsigned base1:8, type:5, dpl:2, p:1;
200 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800201 u32 base3;
202 u32 zero1;
203} __attribute__((packed));
204
205struct svm_cpu_data {
206 int cpu;
207
Avi Kivity5008fdf2007-04-02 13:05:50 +0300208 u64 asid_generation;
209 u32 max_asid;
210 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800211 struct kvm_ldttss_desc *tss_desc;
212
213 struct page *save_area;
214};
215
216static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
Joerg Roedel80b77062007-03-30 17:02:14 +0300217static uint32_t svm_features;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800218
219struct svm_init_data {
220 int cpu;
221 int r;
222};
223
224static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
225
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200226#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800227#define MSRS_RANGE_SIZE 2048
228#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
229
Joerg Roedel455716f2010-03-01 15:34:35 +0100230static u32 svm_msrpm_offset(u32 msr)
231{
232 u32 offset;
233 int i;
234
235 for (i = 0; i < NUM_MSR_MAPS; i++) {
236 if (msr < msrpm_ranges[i] ||
237 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
238 continue;
239
240 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
241 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
242
243 /* Now we have the u8 offset - but need the u32 offset */
244 return offset / 4;
245 }
246
247 /* MSR not in any range */
248 return MSR_INVALID;
249}
250
Avi Kivity6aa8b732006-12-10 02:21:36 -0800251#define MAX_INST_SIZE 15
252
Joerg Roedel80b77062007-03-30 17:02:14 +0300253static inline u32 svm_has(u32 feat)
254{
255 return svm_features & feat;
256}
257
Avi Kivity6aa8b732006-12-10 02:21:36 -0800258static inline void clgi(void)
259{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300260 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800261}
262
263static inline void stgi(void)
264{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300265 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800266}
267
268static inline void invlpga(unsigned long addr, u32 asid)
269{
Joerg Roedele0231712010-02-24 18:59:10 +0100270 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800271}
272
Avi Kivity6aa8b732006-12-10 02:21:36 -0800273static inline void force_new_asid(struct kvm_vcpu *vcpu)
274{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400275 to_svm(vcpu)->asid_generation--;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800276}
277
278static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
279{
280 force_new_asid(vcpu);
281}
282
283static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
284{
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100285 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600286 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800287
Alexander Graf9962d032008-11-25 20:17:02 +0100288 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Avi Kivityf6801df2010-01-21 15:31:50 +0200289 vcpu->arch.efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800290}
291
Avi Kivity6aa8b732006-12-10 02:21:36 -0800292static int is_external_interrupt(u32 info)
293{
294 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
295 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
296}
297
Glauber Costa2809f5d2009-05-12 16:21:05 -0400298static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
299{
300 struct vcpu_svm *svm = to_svm(vcpu);
301 u32 ret = 0;
302
303 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Jan Kiszka48005f62010-02-19 19:38:07 +0100304 ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400305 return ret & mask;
306}
307
308static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
309{
310 struct vcpu_svm *svm = to_svm(vcpu);
311
312 if (mask == 0)
313 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
314 else
315 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
316
317}
318
Avi Kivity6aa8b732006-12-10 02:21:36 -0800319static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
320{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400321 struct vcpu_svm *svm = to_svm(vcpu);
322
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200323 if (svm->vmcb->control.next_rip != 0)
324 svm->next_rip = svm->vmcb->control.next_rip;
325
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400326 if (!svm->next_rip) {
Avi Kivity851ba692009-08-24 11:10:17 +0300327 if (emulate_instruction(vcpu, 0, 0, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300328 EMULATE_DONE)
329 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800330 return;
331 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300332 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
333 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
334 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800335
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300336 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400337 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800338}
339
Jan Kiszka116a4752010-02-23 17:47:54 +0100340static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200341 bool has_error_code, u32 error_code,
342 bool reinject)
Jan Kiszka116a4752010-02-23 17:47:54 +0100343{
344 struct vcpu_svm *svm = to_svm(vcpu);
345
Joerg Roedele0231712010-02-24 18:59:10 +0100346 /*
347 * If we are within a nested VM we'd better #VMEXIT and let the guest
348 * handle the exception
349 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200350 if (!reinject &&
351 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100352 return;
353
Jan Kiszka66b71382010-02-23 17:47:56 +0100354 if (nr == BP_VECTOR && !svm_has(SVM_FEATURE_NRIP)) {
355 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
356
357 /*
358 * For guest debugging where we have to reinject #BP if some
359 * INT3 is guest-owned:
360 * Emulate nRIP by moving RIP forward. Will fail if injection
361 * raises a fault that is not intercepted. Still better than
362 * failing in all cases.
363 */
364 skip_emulated_instruction(&svm->vcpu);
365 rip = kvm_rip_read(&svm->vcpu);
366 svm->int3_rip = rip + svm->vmcb->save.cs.base;
367 svm->int3_injected = rip - old_rip;
368 }
369
Jan Kiszka116a4752010-02-23 17:47:54 +0100370 svm->vmcb->control.event_inj = nr
371 | SVM_EVTINJ_VALID
372 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
373 | SVM_EVTINJ_TYPE_EXEPT;
374 svm->vmcb->control.event_inj_err = error_code;
375}
376
Avi Kivity6aa8b732006-12-10 02:21:36 -0800377static int has_svm(void)
378{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200379 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800380
Eduardo Habkost63d11422008-11-17 19:03:20 -0200381 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800382 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800383 return 0;
384 }
385
Avi Kivity6aa8b732006-12-10 02:21:36 -0800386 return 1;
387}
388
389static void svm_hardware_disable(void *garbage)
390{
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200391 cpu_svm_disable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800392}
393
Alexander Graf10474ae2009-09-15 11:37:46 +0200394static int svm_hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800395{
396
Tejun Heo0fe1e002009-10-29 22:34:14 +0900397 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800398 uint64_t efer;
Gleb Natapov89a27f42010-02-16 10:51:48 +0200399 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800400 struct desc_struct *gdt;
401 int me = raw_smp_processor_id();
402
Alexander Graf10474ae2009-09-15 11:37:46 +0200403 rdmsrl(MSR_EFER, efer);
404 if (efer & EFER_SVME)
405 return -EBUSY;
406
Avi Kivity6aa8b732006-12-10 02:21:36 -0800407 if (!has_svm()) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000408 printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
409 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200410 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800411 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900412 sd = per_cpu(svm_data, me);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800413
Tejun Heo0fe1e002009-10-29 22:34:14 +0900414 if (!sd) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000415 printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -0800416 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200417 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800418 }
419
Tejun Heo0fe1e002009-10-29 22:34:14 +0900420 sd->asid_generation = 1;
421 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
422 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800423
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200424 native_store_gdt(&gdt_descr);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200425 gdt = (struct desc_struct *)gdt_descr.address;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900426 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800427
Alexander Graf9962d032008-11-25 20:17:02 +0100428 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800429
Linus Torvaldsd0316552009-12-14 09:58:24 -0800430 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200431
432 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800433}
434
Joerg Roedel0da1db752008-07-02 16:02:11 +0200435static void svm_cpu_uninit(int cpu)
436{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900437 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200438
Tejun Heo0fe1e002009-10-29 22:34:14 +0900439 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200440 return;
441
442 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900443 __free_page(sd->save_area);
444 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200445}
446
Avi Kivity6aa8b732006-12-10 02:21:36 -0800447static int svm_cpu_init(int cpu)
448{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900449 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800450 int r;
451
Tejun Heo0fe1e002009-10-29 22:34:14 +0900452 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
453 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800454 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900455 sd->cpu = cpu;
456 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800457 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900458 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800459 goto err_1;
460
Tejun Heo0fe1e002009-10-29 22:34:14 +0900461 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800462
463 return 0;
464
465err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900466 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800467 return r;
468
469}
470
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100471static bool valid_msr_intercept(u32 index)
472{
473 int i;
474
475 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
476 if (direct_access_msrs[i].index == index)
477 return true;
478
479 return false;
480}
481
Rusty Russellbfc733a2007-07-31 20:42:42 +1000482static void set_msr_interception(u32 *msrpm, unsigned msr,
483 int read, int write)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800484{
Joerg Roedel455716f2010-03-01 15:34:35 +0100485 u8 bit_read, bit_write;
486 unsigned long tmp;
487 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800488
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100489 /*
490 * If this warning triggers extend the direct_access_msrs list at the
491 * beginning of the file
492 */
493 WARN_ON(!valid_msr_intercept(msr));
494
Joerg Roedel455716f2010-03-01 15:34:35 +0100495 offset = svm_msrpm_offset(msr);
496 bit_read = 2 * (msr & 0x0f);
497 bit_write = 2 * (msr & 0x0f) + 1;
498 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800499
Joerg Roedel455716f2010-03-01 15:34:35 +0100500 BUG_ON(offset == MSR_INVALID);
501
502 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
503 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
504
505 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800506}
507
Joerg Roedelf65c2292008-02-13 18:58:46 +0100508static void svm_vcpu_init_msrpm(u32 *msrpm)
509{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100510 int i;
511
Joerg Roedelf65c2292008-02-13 18:58:46 +0100512 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
513
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100514 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
515 if (!direct_access_msrs[i].always)
516 continue;
517
518 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
519 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100520}
521
Joerg Roedel323c3d82010-03-01 15:34:37 +0100522static void add_msr_offset(u32 offset)
523{
524 int i;
525
526 for (i = 0; i < MSRPM_OFFSETS; ++i) {
527
528 /* Offset already in list? */
529 if (msrpm_offsets[i] == offset)
530 return;
531
532 /* Slot used by another offset? */
533 if (msrpm_offsets[i] != MSR_INVALID)
534 continue;
535
536 /* Add offset to list */
537 msrpm_offsets[i] = offset;
538
539 return;
540 }
541
542 /*
543 * If this BUG triggers the msrpm_offsets table has an overflow. Just
544 * increase MSRPM_OFFSETS in this case.
545 */
546 BUG();
547}
548
549static void init_msrpm_offsets(void)
550{
551 int i;
552
553 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
554
555 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
556 u32 offset;
557
558 offset = svm_msrpm_offset(direct_access_msrs[i].index);
559 BUG_ON(offset == MSR_INVALID);
560
561 add_msr_offset(offset);
562 }
563}
564
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100565static void svm_enable_lbrv(struct vcpu_svm *svm)
566{
567 u32 *msrpm = svm->msrpm;
568
569 svm->vmcb->control.lbr_ctl = 1;
570 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
571 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
572 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
573 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
574}
575
576static void svm_disable_lbrv(struct vcpu_svm *svm)
577{
578 u32 *msrpm = svm->msrpm;
579
580 svm->vmcb->control.lbr_ctl = 0;
581 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
582 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
583 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
584 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
585}
586
Avi Kivity6aa8b732006-12-10 02:21:36 -0800587static __init int svm_hardware_setup(void)
588{
589 int cpu;
590 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100591 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800592 int r;
593
Avi Kivity6aa8b732006-12-10 02:21:36 -0800594 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
595
596 if (!iopm_pages)
597 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300598
599 iopm_va = page_address(iopm_pages);
600 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800601 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
602
Joerg Roedel323c3d82010-03-01 15:34:37 +0100603 init_msrpm_offsets();
604
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100605 if (boot_cpu_has(X86_FEATURE_NX))
606 kvm_enable_efer_bits(EFER_NX);
607
Alexander Graf1b2fd702009-02-02 16:23:51 +0100608 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
609 kvm_enable_efer_bits(EFER_FFXSR);
610
Alexander Graf236de052008-11-25 20:17:10 +0100611 if (nested) {
612 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
613 kvm_enable_efer_bits(EFER_SVME);
614 }
615
Zachary Amsden3230bb42009-09-29 11:38:37 -1000616 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800617 r = svm_cpu_init(cpu);
618 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100619 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800620 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100621
622 svm_features = cpuid_edx(SVM_CPUID_FUNC);
623
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100624 if (!svm_has(SVM_FEATURE_NPT))
625 npt_enabled = false;
626
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100627 if (npt_enabled && !npt) {
628 printk(KERN_INFO "kvm: Nested Paging disabled\n");
629 npt_enabled = false;
630 }
631
Joerg Roedel18552672008-02-07 13:47:41 +0100632 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100633 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100634 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200635 } else
636 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100637
Avi Kivity6aa8b732006-12-10 02:21:36 -0800638 return 0;
639
Joerg Roedelf65c2292008-02-13 18:58:46 +0100640err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800641 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
642 iopm_base = 0;
643 return r;
644}
645
646static __exit void svm_hardware_unsetup(void)
647{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200648 int cpu;
649
Zachary Amsden3230bb42009-09-29 11:38:37 -1000650 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200651 svm_cpu_uninit(cpu);
652
Avi Kivity6aa8b732006-12-10 02:21:36 -0800653 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100654 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800655}
656
657static void init_seg(struct vmcb_seg *seg)
658{
659 seg->selector = 0;
660 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +0100661 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800662 seg->limit = 0xffff;
663 seg->base = 0;
664}
665
666static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
667{
668 seg->selector = 0;
669 seg->attrib = SVM_SELECTOR_P_MASK | type;
670 seg->limit = 0xffff;
671 seg->base = 0;
672}
673
Joerg Roedele6101a92008-02-13 18:58:45 +0100674static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800675{
Joerg Roedele6101a92008-02-13 18:58:45 +0100676 struct vmcb_control_area *control = &svm->vmcb->control;
677 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800678
Avi Kivitybff78272010-01-07 13:16:08 +0200679 svm->vcpu.fpu_active = 1;
680
Joerg Roedele0231712010-02-24 18:59:10 +0100681 control->intercept_cr_read = INTERCEPT_CR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800682 INTERCEPT_CR3_MASK |
Joerg Roedel649d6862008-04-16 16:51:15 +0200683 INTERCEPT_CR4_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800684
Joerg Roedele0231712010-02-24 18:59:10 +0100685 control->intercept_cr_write = INTERCEPT_CR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800686 INTERCEPT_CR3_MASK |
Avi Kivity80a81192007-12-06 19:50:00 +0200687 INTERCEPT_CR4_MASK |
688 INTERCEPT_CR8_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800689
Joerg Roedele0231712010-02-24 18:59:10 +0100690 control->intercept_dr_read = INTERCEPT_DR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800691 INTERCEPT_DR1_MASK |
692 INTERCEPT_DR2_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100693 INTERCEPT_DR3_MASK |
694 INTERCEPT_DR4_MASK |
695 INTERCEPT_DR5_MASK |
696 INTERCEPT_DR6_MASK |
697 INTERCEPT_DR7_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800698
Joerg Roedele0231712010-02-24 18:59:10 +0100699 control->intercept_dr_write = INTERCEPT_DR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800700 INTERCEPT_DR1_MASK |
701 INTERCEPT_DR2_MASK |
702 INTERCEPT_DR3_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100703 INTERCEPT_DR4_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800704 INTERCEPT_DR5_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100705 INTERCEPT_DR6_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800706 INTERCEPT_DR7_MASK;
707
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500708 control->intercept_exceptions = (1 << PF_VECTOR) |
Joerg Roedel53371b52008-04-09 14:15:30 +0200709 (1 << UD_VECTOR) |
710 (1 << MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800711
712
Joerg Roedele0231712010-02-24 18:59:10 +0100713 control->intercept = (1ULL << INTERCEPT_INTR) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800714 (1ULL << INTERCEPT_NMI) |
Joerg Roedel01525272007-02-19 14:37:47 +0200715 (1ULL << INTERCEPT_SMI) |
Avi Kivityd2251572010-01-06 10:55:27 +0200716 (1ULL << INTERCEPT_SELECTIVE_CR0) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800717 (1ULL << INTERCEPT_CPUID) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200718 (1ULL << INTERCEPT_INVD) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800719 (1ULL << INTERCEPT_HLT) |
Marcelo Tosattia7052892008-09-23 13:18:35 -0300720 (1ULL << INTERCEPT_INVLPG) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800721 (1ULL << INTERCEPT_INVLPGA) |
722 (1ULL << INTERCEPT_IOIO_PROT) |
723 (1ULL << INTERCEPT_MSR_PROT) |
724 (1ULL << INTERCEPT_TASK_SWITCH) |
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800725 (1ULL << INTERCEPT_SHUTDOWN) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800726 (1ULL << INTERCEPT_VMRUN) |
727 (1ULL << INTERCEPT_VMMCALL) |
728 (1ULL << INTERCEPT_VMLOAD) |
729 (1ULL << INTERCEPT_VMSAVE) |
730 (1ULL << INTERCEPT_STGI) |
731 (1ULL << INTERCEPT_CLGI) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100732 (1ULL << INTERCEPT_SKINIT) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200733 (1ULL << INTERCEPT_WBINVD) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100734 (1ULL << INTERCEPT_MONITOR) |
735 (1ULL << INTERCEPT_MWAIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800736
737 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100738 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity0cc50642007-03-25 12:07:27 +0200739 control->tsc_offset = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800740 control->int_ctl = V_INTR_MASKING_MASK;
741
742 init_seg(&save->es);
743 init_seg(&save->ss);
744 init_seg(&save->ds);
745 init_seg(&save->fs);
746 init_seg(&save->gs);
747
748 save->cs.selector = 0xf000;
749 /* Executable/Readable Code Segment */
750 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
751 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
752 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800753 /*
754 * cs.base should really be 0xffff0000, but vmx can't handle that, so
755 * be consistent with it.
756 *
757 * Replace when we have real mode working for vmx.
758 */
759 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800760
761 save->gdtr.limit = 0xffff;
762 save->idtr.limit = 0xffff;
763
764 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
765 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
766
Alexander Graf9962d032008-11-25 20:17:02 +0100767 save->efer = EFER_SVME;
Mike Dayd77c26f2007-10-08 09:02:08 -0400768 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800769 save->dr7 = 0x400;
770 save->rflags = 2;
771 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300772 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800773
Joerg Roedele0231712010-02-24 18:59:10 +0100774 /*
775 * This is the guest-visible cr0 value.
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200776 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777 */
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200778 svm->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
779 kvm_set_cr0(&svm->vcpu, svm->vcpu.arch.cr0);
780
Rusty Russell66aee912007-07-17 23:34:16 +1000781 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800782 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100783
784 if (npt_enabled) {
785 /* Setup VMCB for Nested Paging */
786 control->nested_ctl = 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -0300787 control->intercept &= ~((1ULL << INTERCEPT_TASK_SWITCH) |
788 (1ULL << INTERCEPT_INVLPG));
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100789 control->intercept_exceptions &= ~(1 << PF_VECTOR);
Avi Kivity888f9f32010-01-10 12:14:04 +0200790 control->intercept_cr_read &= ~INTERCEPT_CR3_MASK;
791 control->intercept_cr_write &= ~INTERCEPT_CR3_MASK;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100792 save->g_pat = 0x0007040600070406ULL;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100793 save->cr3 = 0;
794 save->cr4 = 0;
795 }
Avi Kivitya79d2f12008-04-14 13:10:21 +0300796 force_new_asid(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +0100797
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200798 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +0200799 svm->vcpu.arch.hflags = 0;
800
Mark Langsdorf565d0992009-10-06 14:25:02 -0500801 if (svm_has(SVM_FEATURE_PAUSE_FILTER)) {
802 control->pause_filter_count = 3000;
803 control->intercept |= (1ULL << INTERCEPT_PAUSE);
804 }
805
Joerg Roedel2af91942009-08-07 11:49:28 +0200806 enable_gif(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800807}
808
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200809static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +0300810{
811 struct vcpu_svm *svm = to_svm(vcpu);
812
Joerg Roedele6101a92008-02-13 18:58:45 +0100813 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +0200814
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300815 if (!kvm_vcpu_is_bsp(vcpu)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300816 kvm_rip_write(vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800817 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
818 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +0200819 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300820 vcpu->arch.regs_avail = ~0;
821 vcpu->arch.regs_dirty = ~0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200822
823 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300824}
825
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000826static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800827{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400828 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800829 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100830 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +0100831 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100832 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000833 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800834
Rusty Russellc16f8622007-07-30 21:12:19 +1000835 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000836 if (!svm) {
837 err = -ENOMEM;
838 goto out;
839 }
840
841 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
842 if (err)
843 goto free_svm;
844
Joerg Roedelf65c2292008-02-13 18:58:46 +0100845 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900846 page = alloc_page(GFP_KERNEL);
847 if (!page)
848 goto uninit;
849
Joerg Roedelf65c2292008-02-13 18:58:46 +0100850 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
851 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900852 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100853
854 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
855 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900856 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100857
Alexander Grafb286d5d2008-11-25 20:17:05 +0100858 hsave_page = alloc_page(GFP_KERNEL);
859 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900860 goto free_page3;
861
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200862 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +0100863
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900864 svm->msrpm = page_address(msrpm_pages);
865 svm_vcpu_init_msrpm(svm->msrpm);
866
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200867 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +0100868 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +0100869
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400870 svm->vmcb = page_address(page);
871 clear_page(svm->vmcb);
872 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
873 svm->asid_generation = 0;
Joerg Roedele6101a92008-02-13 18:58:45 +0100874 init_vmcb(svm);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400875
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000876 fx_init(&svm->vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800877 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300878 if (kvm_vcpu_is_bsp(&svm->vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800879 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800880
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000881 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -0800882
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900883free_page3:
884 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
885free_page2:
886 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
887free_page1:
888 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000889uninit:
890 kvm_vcpu_uninit(&svm->vcpu);
891free_svm:
Rusty Russella4770342007-08-01 14:46:11 +1000892 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000893out:
894 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800895}
896
897static void svm_free_vcpu(struct kvm_vcpu *vcpu)
898{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400899 struct vcpu_svm *svm = to_svm(vcpu);
900
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000901 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +0100902 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200903 __free_page(virt_to_page(svm->nested.hsave));
904 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000905 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +1000906 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800907}
908
Avi Kivity15ad7142007-07-11 18:17:21 +0300909static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400911 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300912 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +0200913
Avi Kivity0cc50642007-03-25 12:07:27 +0200914 if (unlikely(cpu != vcpu->cpu)) {
Joerg Roedele935d482009-09-16 15:24:19 +0200915 u64 delta;
Avi Kivity0cc50642007-03-25 12:07:27 +0200916
Joerg Roedel953899b2009-12-14 12:22:20 +0100917 if (check_tsc_unstable()) {
918 /*
919 * Make sure that the guest sees a monotonically
920 * increasing TSC.
921 */
922 delta = vcpu->arch.host_tsc - native_read_tsc();
923 svm->vmcb->control.tsc_offset += delta;
924 if (is_nested(svm))
925 svm->nested.hsave->control.tsc_offset += delta;
926 }
Avi Kivity0cc50642007-03-25 12:07:27 +0200927 vcpu->cpu = cpu;
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300928 kvm_migrate_timers(vcpu);
Marcelo Tosatti4b656b12009-07-21 12:47:45 -0300929 svm->asid_generation = 0;
Avi Kivity0cc50642007-03-25 12:07:27 +0200930 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300931
932 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400933 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934}
935
936static void svm_vcpu_put(struct kvm_vcpu *vcpu)
937{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400938 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300939 int i;
940
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200941 ++vcpu->stat.host_state_reload;
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300942 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400943 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300944
Joerg Roedele935d482009-09-16 15:24:19 +0200945 vcpu->arch.host_tsc = native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800946}
947
Avi Kivity6aa8b732006-12-10 02:21:36 -0800948static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
949{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400950 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951}
952
953static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
954{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400955 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800956}
957
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300958static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
959{
960 switch (reg) {
961 case VCPU_EXREG_PDPTR:
962 BUG_ON(!npt_enabled);
963 load_pdptrs(vcpu, vcpu->arch.cr3);
964 break;
965 default:
966 BUG();
967 }
968}
969
Alexander Graff0b85052008-11-25 20:17:01 +0100970static void svm_set_vintr(struct vcpu_svm *svm)
971{
972 svm->vmcb->control.intercept |= 1ULL << INTERCEPT_VINTR;
973}
974
975static void svm_clear_vintr(struct vcpu_svm *svm)
976{
977 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
978}
979
Avi Kivity6aa8b732006-12-10 02:21:36 -0800980static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
981{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400982 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800983
984 switch (seg) {
985 case VCPU_SREG_CS: return &save->cs;
986 case VCPU_SREG_DS: return &save->ds;
987 case VCPU_SREG_ES: return &save->es;
988 case VCPU_SREG_FS: return &save->fs;
989 case VCPU_SREG_GS: return &save->gs;
990 case VCPU_SREG_SS: return &save->ss;
991 case VCPU_SREG_TR: return &save->tr;
992 case VCPU_SREG_LDTR: return &save->ldtr;
993 }
994 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +0000995 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800996}
997
998static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
999{
1000 struct vmcb_seg *s = svm_seg(vcpu, seg);
1001
1002 return s->base;
1003}
1004
1005static void svm_get_segment(struct kvm_vcpu *vcpu,
1006 struct kvm_segment *var, int seg)
1007{
1008 struct vmcb_seg *s = svm_seg(vcpu, seg);
1009
1010 var->base = s->base;
1011 var->limit = s->limit;
1012 var->selector = s->selector;
1013 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1014 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1015 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1016 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1017 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1018 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1019 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
1020 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
Amit Shah25022ac2008-10-27 09:04:17 +00001021
Joerg Roedele0231712010-02-24 18:59:10 +01001022 /*
1023 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001024 * for cross vendor migration purposes by "not present"
1025 */
1026 var->unusable = !var->present || (var->type == 0);
1027
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001028 switch (seg) {
1029 case VCPU_SREG_CS:
1030 /*
1031 * SVM always stores 0 for the 'G' bit in the CS selector in
1032 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
1033 * Intel's VMENTRY has a check on the 'G' bit.
1034 */
Amit Shah25022ac2008-10-27 09:04:17 +00001035 var->g = s->limit > 0xfffff;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001036 break;
1037 case VCPU_SREG_TR:
1038 /*
1039 * Work around a bug where the busy flag in the tr selector
1040 * isn't exposed
1041 */
Amit Shahc0d09822008-10-27 09:04:18 +00001042 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001043 break;
1044 case VCPU_SREG_DS:
1045 case VCPU_SREG_ES:
1046 case VCPU_SREG_FS:
1047 case VCPU_SREG_GS:
1048 /*
1049 * The accessed bit must always be set in the segment
1050 * descriptor cache, although it can be cleared in the
1051 * descriptor, the cached bit always remains at 1. Since
1052 * Intel has a check on this, set it here to support
1053 * cross-vendor migration.
1054 */
1055 if (!var->unusable)
1056 var->type |= 0x1;
1057 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001058 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001059 /*
1060 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001061 * descriptor is left as 1, although the whole segment has
1062 * been made unusable. Clear it here to pass an Intel VMX
1063 * entry check when cross vendor migrating.
1064 */
1065 if (var->unusable)
1066 var->db = 0;
1067 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001068 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001069}
1070
Izik Eidus2e4d2652008-03-24 19:38:34 +02001071static int svm_get_cpl(struct kvm_vcpu *vcpu)
1072{
1073 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1074
1075 return save->cpl;
1076}
1077
Gleb Natapov89a27f42010-02-16 10:51:48 +02001078static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001079{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001080 struct vcpu_svm *svm = to_svm(vcpu);
1081
Gleb Natapov89a27f42010-02-16 10:51:48 +02001082 dt->size = svm->vmcb->save.idtr.limit;
1083 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001084}
1085
Gleb Natapov89a27f42010-02-16 10:51:48 +02001086static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001087{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001088 struct vcpu_svm *svm = to_svm(vcpu);
1089
Gleb Natapov89a27f42010-02-16 10:51:48 +02001090 svm->vmcb->save.idtr.limit = dt->size;
1091 svm->vmcb->save.idtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001092}
1093
Gleb Natapov89a27f42010-02-16 10:51:48 +02001094static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001095{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001096 struct vcpu_svm *svm = to_svm(vcpu);
1097
Gleb Natapov89a27f42010-02-16 10:51:48 +02001098 dt->size = svm->vmcb->save.gdtr.limit;
1099 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001100}
1101
Gleb Natapov89a27f42010-02-16 10:51:48 +02001102static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001103{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001104 struct vcpu_svm *svm = to_svm(vcpu);
1105
Gleb Natapov89a27f42010-02-16 10:51:48 +02001106 svm->vmcb->save.gdtr.limit = dt->size;
1107 svm->vmcb->save.gdtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108}
1109
Avi Kivitye8467fd2009-12-29 18:43:06 +02001110static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1111{
1112}
1113
Anthony Liguori25c4c272007-04-27 09:29:21 +03001114static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001115{
1116}
1117
Avi Kivityd2251572010-01-06 10:55:27 +02001118static void update_cr0_intercept(struct vcpu_svm *svm)
1119{
Joerg Roedel66a562f2010-02-19 16:23:08 +01001120 struct vmcb *vmcb = svm->vmcb;
Avi Kivityd2251572010-01-06 10:55:27 +02001121 ulong gcr0 = svm->vcpu.arch.cr0;
1122 u64 *hcr0 = &svm->vmcb->save.cr0;
1123
1124 if (!svm->vcpu.fpu_active)
1125 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1126 else
1127 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1128 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1129
1130
1131 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Joerg Roedel66a562f2010-02-19 16:23:08 +01001132 vmcb->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1133 vmcb->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1134 if (is_nested(svm)) {
1135 struct vmcb *hsave = svm->nested.hsave;
1136
1137 hsave->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1138 hsave->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1139 vmcb->control.intercept_cr_read |= svm->nested.intercept_cr_read;
1140 vmcb->control.intercept_cr_write |= svm->nested.intercept_cr_write;
1141 }
Avi Kivityd2251572010-01-06 10:55:27 +02001142 } else {
1143 svm->vmcb->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1144 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001145 if (is_nested(svm)) {
1146 struct vmcb *hsave = svm->nested.hsave;
1147
1148 hsave->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1149 hsave->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
1150 }
Avi Kivityd2251572010-01-06 10:55:27 +02001151 }
1152}
1153
Avi Kivity6aa8b732006-12-10 02:21:36 -08001154static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1155{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001156 struct vcpu_svm *svm = to_svm(vcpu);
1157
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001158 if (is_nested(svm)) {
1159 /*
1160 * We are here because we run in nested mode, the host kvm
1161 * intercepts cr0 writes but the l1 hypervisor does not.
1162 * But the L1 hypervisor may intercept selective cr0 writes.
1163 * This needs to be checked here.
1164 */
1165 unsigned long old, new;
1166
1167 /* Remove bits that would trigger a real cr0 write intercept */
1168 old = vcpu->arch.cr0 & SVM_CR0_SELECTIVE_MASK;
1169 new = cr0 & SVM_CR0_SELECTIVE_MASK;
1170
1171 if (old == new) {
1172 /* cr0 write with ts and mp unchanged */
1173 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
1174 if (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE)
1175 return;
1176 }
1177 }
1178
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001179#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001180 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001181 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001182 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001183 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001184 }
1185
Mike Dayd77c26f2007-10-08 09:02:08 -04001186 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001187 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001188 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001189 }
1190 }
1191#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001192 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001193
1194 if (!npt_enabled)
1195 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001196
1197 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001198 cr0 |= X86_CR0_TS;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001199 /*
1200 * re-enable caching here because the QEMU bios
1201 * does not do it - this results in some delay at
1202 * reboot
1203 */
1204 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001205 svm->vmcb->save.cr0 = cr0;
Avi Kivityd2251572010-01-06 10:55:27 +02001206 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001207}
1208
1209static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1210{
Joerg Roedel6394b642008-04-09 14:15:29 +02001211 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001212 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1213
1214 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
1215 force_new_asid(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001216
Joerg Roedelec077262008-04-09 14:15:28 +02001217 vcpu->arch.cr4 = cr4;
1218 if (!npt_enabled)
1219 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001220 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001221 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001222}
1223
1224static void svm_set_segment(struct kvm_vcpu *vcpu,
1225 struct kvm_segment *var, int seg)
1226{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001227 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001228 struct vmcb_seg *s = svm_seg(vcpu, seg);
1229
1230 s->base = var->base;
1231 s->limit = var->limit;
1232 s->selector = var->selector;
1233 if (var->unusable)
1234 s->attrib = 0;
1235 else {
1236 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1237 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1238 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1239 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1240 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1241 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1242 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1243 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1244 }
1245 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001246 svm->vmcb->save.cpl
1247 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -08001248 >> SVM_SELECTOR_DPL_SHIFT) & 3;
1249
1250}
1251
Gleb Natapov44c11432009-05-11 13:35:52 +03001252static void update_db_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001253{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001254 struct vcpu_svm *svm = to_svm(vcpu);
1255
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001256 svm->vmcb->control.intercept_exceptions &=
1257 ~((1 << DB_VECTOR) | (1 << BP_VECTOR));
Gleb Natapov44c11432009-05-11 13:35:52 +03001258
Jan Kiszka6be7d302009-10-18 13:24:54 +02001259 if (svm->nmi_singlestep)
Gleb Natapov44c11432009-05-11 13:35:52 +03001260 svm->vmcb->control.intercept_exceptions |= (1 << DB_VECTOR);
1261
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001262 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1263 if (vcpu->guest_debug &
1264 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
1265 svm->vmcb->control.intercept_exceptions |=
1266 1 << DB_VECTOR;
1267 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
1268 svm->vmcb->control.intercept_exceptions |=
1269 1 << BP_VECTOR;
1270 } else
1271 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001272}
1273
Jan Kiszka355be0b2009-10-03 00:31:21 +02001274static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Gleb Natapov44c11432009-05-11 13:35:52 +03001275{
Gleb Natapov44c11432009-05-11 13:35:52 +03001276 struct vcpu_svm *svm = to_svm(vcpu);
1277
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001278 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1279 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1280 else
1281 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1282
Jan Kiszka355be0b2009-10-03 00:31:21 +02001283 update_db_intercept(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001284}
1285
1286static void load_host_msrs(struct kvm_vcpu *vcpu)
1287{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001288#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001289 wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001290#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001291}
1292
1293static void save_host_msrs(struct kvm_vcpu *vcpu)
1294{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001295#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001296 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001297#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001298}
1299
Tejun Heo0fe1e002009-10-29 22:34:14 +09001300static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001301{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001302 if (sd->next_asid > sd->max_asid) {
1303 ++sd->asid_generation;
1304 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001305 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001306 }
1307
Tejun Heo0fe1e002009-10-29 22:34:14 +09001308 svm->asid_generation = sd->asid_generation;
1309 svm->vmcb->control.asid = sd->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001310}
1311
Gleb Natapov020df072010-04-13 10:05:23 +03001312static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001313{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001314 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001315
Gleb Natapov020df072010-04-13 10:05:23 +03001316 svm->vmcb->save.dr7 = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001317}
1318
Avi Kivity851ba692009-08-24 11:10:17 +03001319static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001320{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001321 u64 fault_address;
1322 u32 error_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001323
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001324 fault_address = svm->vmcb->control.exit_info_2;
1325 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001326
Marcelo Tosatti229456f2009-06-17 09:22:14 -03001327 trace_kvm_page_fault(fault_address, error_code);
Avi Kivity52c78472009-08-10 15:42:41 +03001328 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1329 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
Avi Kivity30677142007-10-28 18:48:59 +02001330 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001331}
1332
Avi Kivity851ba692009-08-24 11:10:17 +03001333static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001334{
Avi Kivity851ba692009-08-24 11:10:17 +03001335 struct kvm_run *kvm_run = svm->vcpu.run;
1336
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001337 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001338 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001339 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001340 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1341 return 1;
1342 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001343
Jan Kiszka6be7d302009-10-18 13:24:54 +02001344 if (svm->nmi_singlestep) {
1345 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001346 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1347 svm->vmcb->save.rflags &=
1348 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1349 update_db_intercept(&svm->vcpu);
1350 }
1351
1352 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001353 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001354 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1355 kvm_run->debug.arch.pc =
1356 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1357 kvm_run->debug.arch.exception = DB_VECTOR;
1358 return 0;
1359 }
1360
1361 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001362}
1363
Avi Kivity851ba692009-08-24 11:10:17 +03001364static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001365{
Avi Kivity851ba692009-08-24 11:10:17 +03001366 struct kvm_run *kvm_run = svm->vcpu.run;
1367
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001368 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1369 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1370 kvm_run->debug.arch.exception = BP_VECTOR;
1371 return 0;
1372}
1373
Avi Kivity851ba692009-08-24 11:10:17 +03001374static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001375{
1376 int er;
1377
Avi Kivity851ba692009-08-24 11:10:17 +03001378 er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001379 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001380 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001381 return 1;
1382}
1383
Avi Kivity6b52d182010-01-21 15:31:47 +02001384static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001385{
Avi Kivity6b52d182010-01-21 15:31:47 +02001386 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001387 u32 excp;
1388
1389 if (is_nested(svm)) {
1390 u32 h_excp, n_excp;
1391
1392 h_excp = svm->nested.hsave->control.intercept_exceptions;
1393 n_excp = svm->nested.intercept_exceptions;
1394 h_excp &= ~(1 << NM_VECTOR);
1395 excp = h_excp | n_excp;
1396 } else {
1397 excp = svm->vmcb->control.intercept_exceptions;
Joerg Roedele0231712010-02-24 18:59:10 +01001398 excp &= ~(1 << NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001399 }
1400
1401 svm->vmcb->control.intercept_exceptions = excp;
1402
Rusty Russelle756fc62007-07-30 20:07:08 +10001403 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001404 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001405}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001406
Avi Kivity6b52d182010-01-21 15:31:47 +02001407static int nm_interception(struct vcpu_svm *svm)
1408{
1409 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001410 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001411}
1412
Avi Kivity851ba692009-08-24 11:10:17 +03001413static int mc_interception(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001414{
1415 /*
1416 * On an #MC intercept the MCE handler is not called automatically in
1417 * the host. So do it by hand here.
1418 */
1419 asm volatile (
1420 "int $0x12\n");
1421 /* not sure if we ever come back to this point */
1422
1423 return 1;
1424}
1425
Avi Kivity851ba692009-08-24 11:10:17 +03001426static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001427{
Avi Kivity851ba692009-08-24 11:10:17 +03001428 struct kvm_run *kvm_run = svm->vcpu.run;
1429
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001430 /*
1431 * VMCB is undefined after a SHUTDOWN intercept
1432 * so reinitialize it.
1433 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001434 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001435 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001436
1437 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1438 return 0;
1439}
1440
Avi Kivity851ba692009-08-24 11:10:17 +03001441static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001442{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001443 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04001444 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001445 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001446 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001447
Rusty Russelle756fc62007-07-30 20:07:08 +10001448 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03001449 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001450 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001451 if (string || in)
1452 return !(emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DO_MMIO);
1453
Avi Kivity039576c2007-03-20 12:46:50 +02001454 port = io_info >> 16;
1455 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001456 svm->next_rip = svm->vmcb->control.exit_info_2;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001457 skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001458
1459 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001460}
1461
Avi Kivity851ba692009-08-24 11:10:17 +03001462static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001463{
1464 return 1;
1465}
1466
Avi Kivity851ba692009-08-24 11:10:17 +03001467static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001468{
1469 ++svm->vcpu.stat.irq_exits;
1470 return 1;
1471}
1472
Avi Kivity851ba692009-08-24 11:10:17 +03001473static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001474{
1475 return 1;
1476}
1477
Avi Kivity851ba692009-08-24 11:10:17 +03001478static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001479{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001480 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001481 skip_emulated_instruction(&svm->vcpu);
1482 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001483}
1484
Avi Kivity851ba692009-08-24 11:10:17 +03001485static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001486{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001487 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001488 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001489 kvm_emulate_hypercall(&svm->vcpu);
1490 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001491}
1492
Alexander Grafc0725422008-11-25 20:17:03 +01001493static int nested_svm_check_permissions(struct vcpu_svm *svm)
1494{
Avi Kivityf6801df2010-01-21 15:31:50 +02001495 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01001496 || !is_paging(&svm->vcpu)) {
1497 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1498 return 1;
1499 }
1500
1501 if (svm->vmcb->save.cpl) {
1502 kvm_inject_gp(&svm->vcpu, 0);
1503 return 1;
1504 }
1505
1506 return 0;
1507}
1508
Alexander Grafcf74a782008-11-25 20:17:08 +01001509static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
1510 bool has_error_code, u32 error_code)
1511{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001512 int vmexit;
1513
Joerg Roedel0295ad72009-08-07 11:49:37 +02001514 if (!is_nested(svm))
1515 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001516
Joerg Roedel0295ad72009-08-07 11:49:37 +02001517 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1518 svm->vmcb->control.exit_code_hi = 0;
1519 svm->vmcb->control.exit_info_1 = error_code;
1520 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1521
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001522 vmexit = nested_svm_intercept(svm);
1523 if (vmexit == NESTED_EXIT_DONE)
1524 svm->nested.exit_required = true;
1525
1526 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001527}
1528
Joerg Roedel8fe54652010-02-19 16:23:01 +01001529/* This function returns true if it is save to enable the irq window */
1530static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001531{
Joerg Roedel26666952009-08-07 11:49:46 +02001532 if (!is_nested(svm))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001533 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001534
Joerg Roedel26666952009-08-07 11:49:46 +02001535 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001536 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001537
Joerg Roedel26666952009-08-07 11:49:46 +02001538 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001539 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001540
Joerg Roedel197717d2010-02-24 18:59:19 +01001541 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
1542 svm->vmcb->control.exit_info_1 = 0;
1543 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02001544
Joerg Roedelcd3ff652009-10-09 16:08:26 +02001545 if (svm->nested.intercept & 1ULL) {
1546 /*
1547 * The #vmexit can't be emulated here directly because this
1548 * code path runs with irqs and preemtion disabled. A
1549 * #vmexit emulation might sleep. Only signal request for
1550 * the #vmexit here.
1551 */
1552 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02001553 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01001554 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001555 }
1556
Joerg Roedel8fe54652010-02-19 16:23:01 +01001557 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001558}
1559
Joerg Roedel887f5002010-02-24 18:59:12 +01001560/* This function returns true if it is save to enable the nmi window */
1561static inline bool nested_svm_nmi(struct vcpu_svm *svm)
1562{
1563 if (!is_nested(svm))
1564 return true;
1565
1566 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
1567 return true;
1568
1569 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
1570 svm->nested.exit_required = true;
1571
1572 return false;
1573}
1574
Joerg Roedel7597f122010-02-19 16:23:00 +01001575static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001576{
1577 struct page *page;
1578
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01001579 might_sleep();
1580
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001581 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001582 if (is_error_page(page))
1583 goto error;
1584
Joerg Roedel7597f122010-02-19 16:23:00 +01001585 *_page = page;
1586
1587 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001588
1589error:
1590 kvm_release_page_clean(page);
1591 kvm_inject_gp(&svm->vcpu, 0);
1592
1593 return NULL;
1594}
1595
Joerg Roedel7597f122010-02-19 16:23:00 +01001596static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001597{
Joerg Roedel7597f122010-02-19 16:23:00 +01001598 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001599 kvm_release_page_dirty(page);
1600}
1601
Joerg Roedelce2ac082010-03-01 15:34:39 +01001602static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
1603{
1604 unsigned port;
1605 u8 val, bit;
1606 u64 gpa;
1607
1608 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
1609 return NESTED_EXIT_HOST;
1610
1611 port = svm->vmcb->control.exit_info_1 >> 16;
1612 gpa = svm->nested.vmcb_iopm + (port / 8);
1613 bit = port % 8;
1614 val = 0;
1615
1616 if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1))
1617 val &= (1 << bit);
1618
1619 return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1620}
1621
Joerg Roedeld2477822010-03-01 15:34:34 +01001622static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001623{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001624 u32 offset, msr, value;
1625 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001626
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001627 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01001628 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001629
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001630 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1631 offset = svm_msrpm_offset(msr);
1632 write = svm->vmcb->control.exit_info_1 & 1;
1633 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001634
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001635 if (offset == MSR_INVALID)
1636 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001637
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001638 /* Offset is in 32 bit units but need in 8 bit units */
1639 offset *= 4;
1640
1641 if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4))
1642 return NESTED_EXIT_DONE;
1643
1644 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001645}
1646
Joerg Roedel410e4d52009-08-07 11:49:44 +02001647static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001648{
Alexander Grafcf74a782008-11-25 20:17:08 +01001649 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001650
Joerg Roedel410e4d52009-08-07 11:49:44 +02001651 switch (exit_code) {
1652 case SVM_EXIT_INTR:
1653 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02001654 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02001655 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001656 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01001657 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001658 if (npt_enabled)
1659 return NESTED_EXIT_HOST;
1660 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001661 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Joerg Roedele0231712010-02-24 18:59:10 +01001662 /* When we're shadowing, trap PFs */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001663 if (!npt_enabled)
1664 return NESTED_EXIT_HOST;
1665 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001666 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
1667 nm_interception(svm);
1668 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001669 default:
1670 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001671 }
1672
Joerg Roedel410e4d52009-08-07 11:49:44 +02001673 return NESTED_EXIT_CONTINUE;
1674}
1675
1676/*
1677 * If this function returns true, this #vmexit was already handled
1678 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001679static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001680{
1681 u32 exit_code = svm->vmcb->control.exit_code;
1682 int vmexit = NESTED_EXIT_HOST;
1683
Alexander Grafcf74a782008-11-25 20:17:08 +01001684 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001685 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001686 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001687 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01001688 case SVM_EXIT_IOIO:
1689 vmexit = nested_svm_intercept_ioio(svm);
1690 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001691 case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR8: {
1692 u32 cr_bits = 1 << (exit_code - SVM_EXIT_READ_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001693 if (svm->nested.intercept_cr_read & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001694 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001695 break;
1696 }
1697 case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR8: {
1698 u32 cr_bits = 1 << (exit_code - SVM_EXIT_WRITE_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001699 if (svm->nested.intercept_cr_write & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001700 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001701 break;
1702 }
1703 case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR7: {
1704 u32 dr_bits = 1 << (exit_code - SVM_EXIT_READ_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001705 if (svm->nested.intercept_dr_read & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001706 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001707 break;
1708 }
1709 case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR7: {
1710 u32 dr_bits = 1 << (exit_code - SVM_EXIT_WRITE_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001711 if (svm->nested.intercept_dr_write & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001712 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001713 break;
1714 }
1715 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1716 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001717 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001718 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001719 break;
1720 }
Joerg Roedel228070b2010-04-22 12:33:10 +02001721 case SVM_EXIT_ERR: {
1722 vmexit = NESTED_EXIT_DONE;
1723 break;
1724 }
Alexander Grafcf74a782008-11-25 20:17:08 +01001725 default: {
1726 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001727 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001728 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001729 }
1730 }
1731
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001732 return vmexit;
1733}
1734
1735static int nested_svm_exit_handled(struct vcpu_svm *svm)
1736{
1737 int vmexit;
1738
1739 vmexit = nested_svm_intercept(svm);
1740
1741 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001742 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001743
1744 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001745}
1746
Joerg Roedel0460a972009-08-07 11:49:31 +02001747static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
1748{
1749 struct vmcb_control_area *dst = &dst_vmcb->control;
1750 struct vmcb_control_area *from = &from_vmcb->control;
1751
1752 dst->intercept_cr_read = from->intercept_cr_read;
1753 dst->intercept_cr_write = from->intercept_cr_write;
1754 dst->intercept_dr_read = from->intercept_dr_read;
1755 dst->intercept_dr_write = from->intercept_dr_write;
1756 dst->intercept_exceptions = from->intercept_exceptions;
1757 dst->intercept = from->intercept;
1758 dst->iopm_base_pa = from->iopm_base_pa;
1759 dst->msrpm_base_pa = from->msrpm_base_pa;
1760 dst->tsc_offset = from->tsc_offset;
1761 dst->asid = from->asid;
1762 dst->tlb_ctl = from->tlb_ctl;
1763 dst->int_ctl = from->int_ctl;
1764 dst->int_vector = from->int_vector;
1765 dst->int_state = from->int_state;
1766 dst->exit_code = from->exit_code;
1767 dst->exit_code_hi = from->exit_code_hi;
1768 dst->exit_info_1 = from->exit_info_1;
1769 dst->exit_info_2 = from->exit_info_2;
1770 dst->exit_int_info = from->exit_int_info;
1771 dst->exit_int_info_err = from->exit_int_info_err;
1772 dst->nested_ctl = from->nested_ctl;
1773 dst->event_inj = from->event_inj;
1774 dst->event_inj_err = from->event_inj_err;
1775 dst->nested_cr3 = from->nested_cr3;
1776 dst->lbr_ctl = from->lbr_ctl;
1777}
1778
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001779static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001780{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001781 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001782 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02001783 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01001784 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01001785
Joerg Roedel17897f32009-10-09 16:08:29 +02001786 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
1787 vmcb->control.exit_info_1,
1788 vmcb->control.exit_info_2,
1789 vmcb->control.exit_int_info,
1790 vmcb->control.exit_int_info_err);
1791
Joerg Roedel7597f122010-02-19 16:23:00 +01001792 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001793 if (!nested_vmcb)
1794 return 1;
1795
Joerg Roedel06fc77722010-02-19 16:23:07 +01001796 /* Exit nested SVM mode */
1797 svm->nested.vmcb = 0;
1798
Alexander Grafcf74a782008-11-25 20:17:08 +01001799 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02001800 disable_gif(svm);
1801
1802 nested_vmcb->save.es = vmcb->save.es;
1803 nested_vmcb->save.cs = vmcb->save.cs;
1804 nested_vmcb->save.ss = vmcb->save.ss;
1805 nested_vmcb->save.ds = vmcb->save.ds;
1806 nested_vmcb->save.gdtr = vmcb->save.gdtr;
1807 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001808 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedel2be4fc72010-04-22 12:33:09 +02001809 nested_vmcb->save.cr3 = svm->vcpu.arch.cr3;
Joerg Roedel33740e42009-08-07 11:49:29 +02001810 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001811 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Joerg Roedel33740e42009-08-07 11:49:29 +02001812 nested_vmcb->save.rflags = vmcb->save.rflags;
1813 nested_vmcb->save.rip = vmcb->save.rip;
1814 nested_vmcb->save.rsp = vmcb->save.rsp;
1815 nested_vmcb->save.rax = vmcb->save.rax;
1816 nested_vmcb->save.dr7 = vmcb->save.dr7;
1817 nested_vmcb->save.dr6 = vmcb->save.dr6;
1818 nested_vmcb->save.cpl = vmcb->save.cpl;
1819
1820 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
1821 nested_vmcb->control.int_vector = vmcb->control.int_vector;
1822 nested_vmcb->control.int_state = vmcb->control.int_state;
1823 nested_vmcb->control.exit_code = vmcb->control.exit_code;
1824 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
1825 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
1826 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
1827 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
1828 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Alexander Graf8d23c462009-10-09 16:08:25 +02001829
1830 /*
1831 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
1832 * to make sure that we do not lose injected events. So check event_inj
1833 * here and copy it to exit_int_info if it is valid.
1834 * Exit_int_info and event_inj can't be both valid because the case
1835 * below only happens on a VMRUN instruction intercept which has
1836 * no valid exit_int_info set.
1837 */
1838 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
1839 struct vmcb_control_area *nc = &nested_vmcb->control;
1840
1841 nc->exit_int_info = vmcb->control.event_inj;
1842 nc->exit_int_info_err = vmcb->control.event_inj_err;
1843 }
1844
Joerg Roedel33740e42009-08-07 11:49:29 +02001845 nested_vmcb->control.tlb_ctl = 0;
1846 nested_vmcb->control.event_inj = 0;
1847 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001848
1849 /* We always set V_INTR_MASKING and remember the old value in hflags */
1850 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
1851 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
1852
Alexander Grafcf74a782008-11-25 20:17:08 +01001853 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02001854 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01001855
Alexander Graf219b65d2009-06-15 15:21:25 +02001856 kvm_clear_exception_queue(&svm->vcpu);
1857 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01001858
1859 /* Restore selected save entries */
1860 svm->vmcb->save.es = hsave->save.es;
1861 svm->vmcb->save.cs = hsave->save.cs;
1862 svm->vmcb->save.ss = hsave->save.ss;
1863 svm->vmcb->save.ds = hsave->save.ds;
1864 svm->vmcb->save.gdtr = hsave->save.gdtr;
1865 svm->vmcb->save.idtr = hsave->save.idtr;
1866 svm->vmcb->save.rflags = hsave->save.rflags;
1867 svm_set_efer(&svm->vcpu, hsave->save.efer);
1868 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
1869 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
1870 if (npt_enabled) {
1871 svm->vmcb->save.cr3 = hsave->save.cr3;
1872 svm->vcpu.arch.cr3 = hsave->save.cr3;
1873 } else {
1874 kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
1875 }
1876 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
1877 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
1878 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
1879 svm->vmcb->save.dr7 = 0;
1880 svm->vmcb->save.cpl = 0;
1881 svm->vmcb->control.exit_int_info = 0;
1882
Joerg Roedel7597f122010-02-19 16:23:00 +01001883 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01001884
1885 kvm_mmu_reset_context(&svm->vcpu);
1886 kvm_mmu_load(&svm->vcpu);
1887
1888 return 0;
1889}
Alexander Graf3d6368e2008-11-25 20:17:07 +01001890
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001891static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01001892{
Joerg Roedel323c3d82010-03-01 15:34:37 +01001893 /*
1894 * This function merges the msr permission bitmaps of kvm and the
1895 * nested vmcb. It is omptimized in that it only merges the parts where
1896 * the kvm msr permission bitmap may contain zero bits
1897 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01001898 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001899
Joerg Roedel323c3d82010-03-01 15:34:37 +01001900 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
1901 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001902
Joerg Roedel323c3d82010-03-01 15:34:37 +01001903 for (i = 0; i < MSRPM_OFFSETS; i++) {
1904 u32 value, p;
1905 u64 offset;
1906
1907 if (msrpm_offsets[i] == 0xffffffff)
1908 break;
1909
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001910 p = msrpm_offsets[i];
1911 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01001912
1913 if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4))
1914 return false;
1915
1916 svm->nested.msrpm[p] = svm->msrpm[p] | value;
1917 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001918
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001919 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001920
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001921 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001922}
1923
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001924static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01001925{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001926 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001927 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02001928 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01001929 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01001930 u64 vmcb_gpa;
1931
1932 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001933
Joerg Roedel7597f122010-02-19 16:23:00 +01001934 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001935 if (!nested_vmcb)
1936 return false;
1937
Joerg Roedelecf14052010-02-24 18:59:13 +01001938 trace_kvm_nested_vmrun(svm->vmcb->save.rip - 3, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02001939 nested_vmcb->save.rip,
1940 nested_vmcb->control.int_ctl,
1941 nested_vmcb->control.event_inj,
1942 nested_vmcb->control.nested_ctl);
1943
Joerg Roedel2e554e82010-02-24 18:59:14 +01001944 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr_read,
1945 nested_vmcb->control.intercept_cr_write,
1946 nested_vmcb->control.intercept_exceptions,
1947 nested_vmcb->control.intercept);
1948
Alexander Graf3d6368e2008-11-25 20:17:07 +01001949 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02001950 kvm_clear_exception_queue(&svm->vcpu);
1951 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001952
Joerg Roedele0231712010-02-24 18:59:10 +01001953 /*
1954 * Save the old vmcb, so we don't need to pick what we save, but can
1955 * restore everything when a VMEXIT occurs
1956 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02001957 hsave->save.es = vmcb->save.es;
1958 hsave->save.cs = vmcb->save.cs;
1959 hsave->save.ss = vmcb->save.ss;
1960 hsave->save.ds = vmcb->save.ds;
1961 hsave->save.gdtr = vmcb->save.gdtr;
1962 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02001963 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02001964 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02001965 hsave->save.cr4 = svm->vcpu.arch.cr4;
1966 hsave->save.rflags = vmcb->save.rflags;
1967 hsave->save.rip = svm->next_rip;
1968 hsave->save.rsp = vmcb->save.rsp;
1969 hsave->save.rax = vmcb->save.rax;
1970 if (npt_enabled)
1971 hsave->save.cr3 = vmcb->save.cr3;
1972 else
1973 hsave->save.cr3 = svm->vcpu.arch.cr3;
1974
Joerg Roedel0460a972009-08-07 11:49:31 +02001975 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001976
1977 if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
1978 svm->vcpu.arch.hflags |= HF_HIF_MASK;
1979 else
1980 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
1981
1982 /* Load the nested guest state */
1983 svm->vmcb->save.es = nested_vmcb->save.es;
1984 svm->vmcb->save.cs = nested_vmcb->save.cs;
1985 svm->vmcb->save.ss = nested_vmcb->save.ss;
1986 svm->vmcb->save.ds = nested_vmcb->save.ds;
1987 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
1988 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
1989 svm->vmcb->save.rflags = nested_vmcb->save.rflags;
1990 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
1991 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
1992 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
1993 if (npt_enabled) {
1994 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
1995 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01001996 } else
Alexander Graf3d6368e2008-11-25 20:17:07 +01001997 kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01001998
1999 /* Guest paging mode is active - reset mmu */
2000 kvm_mmu_reset_context(&svm->vcpu);
2001
Joerg Roedeldefbba52009-08-07 11:49:30 +02002002 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002003 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2004 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2005 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01002006
Alexander Graf3d6368e2008-11-25 20:17:07 +01002007 /* In case we don't even reach vcpu_run, the fields are not updated */
2008 svm->vmcb->save.rax = nested_vmcb->save.rax;
2009 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2010 svm->vmcb->save.rip = nested_vmcb->save.rip;
2011 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2012 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2013 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2014
Joerg Roedelf7138532010-03-01 15:34:40 +01002015 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002016 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002017
Joerg Roedelaad42c62009-08-07 11:49:34 +02002018 /* cache intercepts */
2019 svm->nested.intercept_cr_read = nested_vmcb->control.intercept_cr_read;
2020 svm->nested.intercept_cr_write = nested_vmcb->control.intercept_cr_write;
2021 svm->nested.intercept_dr_read = nested_vmcb->control.intercept_dr_read;
2022 svm->nested.intercept_dr_write = nested_vmcb->control.intercept_dr_write;
2023 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2024 svm->nested.intercept = nested_vmcb->control.intercept;
2025
Alexander Graf3d6368e2008-11-25 20:17:07 +01002026 force_new_asid(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002027 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002028 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2029 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2030 else
2031 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2032
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002033 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2034 /* We only want the cr8 intercept bits of the guest */
2035 svm->vmcb->control.intercept_cr_read &= ~INTERCEPT_CR8_MASK;
2036 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
2037 }
2038
Joerg Roedele0231712010-02-24 18:59:10 +01002039 /*
2040 * We don't want a nested guest to be more powerful than the guest, so
2041 * all intercepts are ORed
2042 */
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002043 svm->vmcb->control.intercept_cr_read |=
2044 nested_vmcb->control.intercept_cr_read;
2045 svm->vmcb->control.intercept_cr_write |=
2046 nested_vmcb->control.intercept_cr_write;
2047 svm->vmcb->control.intercept_dr_read |=
2048 nested_vmcb->control.intercept_dr_read;
2049 svm->vmcb->control.intercept_dr_write |=
2050 nested_vmcb->control.intercept_dr_write;
2051 svm->vmcb->control.intercept_exceptions |=
2052 nested_vmcb->control.intercept_exceptions;
2053
2054 svm->vmcb->control.intercept |= nested_vmcb->control.intercept;
2055
2056 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002057 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2058 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2059 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002060 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2061 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2062
Joerg Roedel7597f122010-02-19 16:23:00 +01002063 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002064
Joerg Roedel06fc77722010-02-19 16:23:07 +01002065 /* nested_vmcb is our indicator if nested SVM is activated */
2066 svm->nested.vmcb = vmcb_gpa;
2067
Joerg Roedel2af91942009-08-07 11:49:28 +02002068 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002069
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002070 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002071}
2072
Joerg Roedel9966bf62009-08-07 11:49:40 +02002073static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002074{
2075 to_vmcb->save.fs = from_vmcb->save.fs;
2076 to_vmcb->save.gs = from_vmcb->save.gs;
2077 to_vmcb->save.tr = from_vmcb->save.tr;
2078 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2079 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2080 to_vmcb->save.star = from_vmcb->save.star;
2081 to_vmcb->save.lstar = from_vmcb->save.lstar;
2082 to_vmcb->save.cstar = from_vmcb->save.cstar;
2083 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2084 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2085 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2086 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002087}
2088
Avi Kivity851ba692009-08-24 11:10:17 +03002089static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002090{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002091 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002092 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002093
Alexander Graf55426752008-11-25 20:17:06 +01002094 if (nested_svm_check_permissions(svm))
2095 return 1;
2096
2097 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2098 skip_emulated_instruction(&svm->vcpu);
2099
Joerg Roedel7597f122010-02-19 16:23:00 +01002100 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002101 if (!nested_vmcb)
2102 return 1;
2103
2104 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002105 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002106
2107 return 1;
2108}
2109
Avi Kivity851ba692009-08-24 11:10:17 +03002110static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002111{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002112 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002113 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002114
Alexander Graf55426752008-11-25 20:17:06 +01002115 if (nested_svm_check_permissions(svm))
2116 return 1;
2117
2118 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2119 skip_emulated_instruction(&svm->vcpu);
2120
Joerg Roedel7597f122010-02-19 16:23:00 +01002121 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002122 if (!nested_vmcb)
2123 return 1;
2124
2125 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002126 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002127
2128 return 1;
2129}
2130
Avi Kivity851ba692009-08-24 11:10:17 +03002131static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002132{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002133 if (nested_svm_check_permissions(svm))
2134 return 1;
2135
2136 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2137 skip_emulated_instruction(&svm->vcpu);
2138
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002139 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002140 return 1;
2141
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002142 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002143 goto failed;
2144
2145 return 1;
2146
2147failed:
2148
2149 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2150 svm->vmcb->control.exit_code_hi = 0;
2151 svm->vmcb->control.exit_info_1 = 0;
2152 svm->vmcb->control.exit_info_2 = 0;
2153
2154 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002155
2156 return 1;
2157}
2158
Avi Kivity851ba692009-08-24 11:10:17 +03002159static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002160{
2161 if (nested_svm_check_permissions(svm))
2162 return 1;
2163
2164 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2165 skip_emulated_instruction(&svm->vcpu);
2166
Joerg Roedel2af91942009-08-07 11:49:28 +02002167 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002168
2169 return 1;
2170}
2171
Avi Kivity851ba692009-08-24 11:10:17 +03002172static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002173{
2174 if (nested_svm_check_permissions(svm))
2175 return 1;
2176
2177 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2178 skip_emulated_instruction(&svm->vcpu);
2179
Joerg Roedel2af91942009-08-07 11:49:28 +02002180 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002181
2182 /* After a CLGI no interrupts should come */
2183 svm_clear_vintr(svm);
2184 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2185
2186 return 1;
2187}
2188
Avi Kivity851ba692009-08-24 11:10:17 +03002189static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002190{
2191 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002192
Joerg Roedelec1ff792009-10-09 16:08:31 +02002193 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
2194 vcpu->arch.regs[VCPU_REGS_RAX]);
2195
Alexander Grafff092382009-06-15 15:21:24 +02002196 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2197 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
2198
2199 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2200 skip_emulated_instruction(&svm->vcpu);
2201 return 1;
2202}
2203
Joerg Roedel532a46b2009-10-09 16:08:32 +02002204static int skinit_interception(struct vcpu_svm *svm)
2205{
2206 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
2207
2208 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2209 return 1;
2210}
2211
Avi Kivity851ba692009-08-24 11:10:17 +03002212static int invalid_op_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002213{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002214 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002215 return 1;
2216}
2217
Avi Kivity851ba692009-08-24 11:10:17 +03002218static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002219{
Izik Eidus37817f22008-03-24 23:14:53 +02002220 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002221 int reason;
2222 int int_type = svm->vmcb->control.exit_int_info &
2223 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002224 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002225 uint32_t type =
2226 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2227 uint32_t idt_v =
2228 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002229 bool has_error_code = false;
2230 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002231
2232 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002233
Izik Eidus37817f22008-03-24 23:14:53 +02002234 if (svm->vmcb->control.exit_info_2 &
2235 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002236 reason = TASK_SWITCH_IRET;
2237 else if (svm->vmcb->control.exit_info_2 &
2238 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2239 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002240 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002241 reason = TASK_SWITCH_GATE;
2242 else
2243 reason = TASK_SWITCH_CALL;
2244
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002245 if (reason == TASK_SWITCH_GATE) {
2246 switch (type) {
2247 case SVM_EXITINTINFO_TYPE_NMI:
2248 svm->vcpu.arch.nmi_injected = false;
2249 break;
2250 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002251 if (svm->vmcb->control.exit_info_2 &
2252 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2253 has_error_code = true;
2254 error_code =
2255 (u32)svm->vmcb->control.exit_info_2;
2256 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002257 kvm_clear_exception_queue(&svm->vcpu);
2258 break;
2259 case SVM_EXITINTINFO_TYPE_INTR:
2260 kvm_clear_interrupt_queue(&svm->vcpu);
2261 break;
2262 default:
2263 break;
2264 }
2265 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002266
Gleb Natapov8317c292009-04-12 13:37:02 +03002267 if (reason != TASK_SWITCH_GATE ||
2268 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2269 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002270 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2271 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002272
Gleb Natapovacb54512010-04-15 21:03:50 +03002273 if (kvm_task_switch(&svm->vcpu, tss_selector, reason,
2274 has_error_code, error_code) == EMULATE_FAIL) {
2275 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2276 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2277 svm->vcpu.run->internal.ndata = 0;
2278 return 0;
2279 }
2280 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002281}
2282
Avi Kivity851ba692009-08-24 11:10:17 +03002283static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002284{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002285 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002286 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002287 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002288}
2289
Avi Kivity851ba692009-08-24 11:10:17 +03002290static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002291{
2292 ++svm->vcpu.stat.nmi_window_exits;
2293 svm->vmcb->control.intercept &= ~(1UL << INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002294 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002295 return 1;
2296}
2297
Avi Kivity851ba692009-08-24 11:10:17 +03002298static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002299{
Avi Kivity851ba692009-08-24 11:10:17 +03002300 if (emulate_instruction(&svm->vcpu, 0, 0, 0) != EMULATE_DONE)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002301 pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
2302 return 1;
2303}
2304
Avi Kivity851ba692009-08-24 11:10:17 +03002305static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002306{
Avi Kivity851ba692009-08-24 11:10:17 +03002307 if (emulate_instruction(&svm->vcpu, 0, 0, 0) != EMULATE_DONE)
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002308 pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002309 return 1;
2310}
2311
Avi Kivity851ba692009-08-24 11:10:17 +03002312static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01002313{
Avi Kivity851ba692009-08-24 11:10:17 +03002314 struct kvm_run *kvm_run = svm->vcpu.run;
2315
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002316 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
2317 /* instruction emulation calls kvm_set_cr8() */
Avi Kivity851ba692009-08-24 11:10:17 +03002318 emulate_instruction(&svm->vcpu, 0, 0, 0);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002319 if (irqchip_in_kernel(svm->vcpu.kvm)) {
2320 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
Joerg Roedel1d075432007-12-06 21:02:25 +01002321 return 1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002322 }
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002323 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
2324 return 1;
Joerg Roedel1d075432007-12-06 21:02:25 +01002325 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2326 return 0;
2327}
2328
Avi Kivity6aa8b732006-12-10 02:21:36 -08002329static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
2330{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002331 struct vcpu_svm *svm = to_svm(vcpu);
2332
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302334 case MSR_IA32_TSC: {
Joerg Roedel20824f32009-09-16 15:24:18 +02002335 u64 tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002336
Joerg Roedel20824f32009-09-16 15:24:18 +02002337 if (is_nested(svm))
2338 tsc_offset = svm->nested.hsave->control.tsc_offset;
2339 else
2340 tsc_offset = svm->vmcb->control.tsc_offset;
2341
2342 *data = tsc_offset + native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002343 break;
2344 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08002345 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002346 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002347 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08002348#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002349 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002350 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002351 break;
2352 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002353 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002354 break;
2355 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002356 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002357 break;
2358 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002359 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002360 break;
2361#endif
2362 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002363 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002364 break;
2365 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002366 *data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002367 break;
2368 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002369 *data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002370 break;
Joerg Roedele0231712010-02-24 18:59:10 +01002371 /*
2372 * Nobody will change the following 5 values in the VMCB so we can
2373 * safely return them on rdmsr. They will always be 0 until LBRV is
2374 * implemented.
2375 */
Joerg Roedela2938c82008-02-13 16:30:28 +01002376 case MSR_IA32_DEBUGCTLMSR:
2377 *data = svm->vmcb->save.dbgctl;
2378 break;
2379 case MSR_IA32_LASTBRANCHFROMIP:
2380 *data = svm->vmcb->save.br_from;
2381 break;
2382 case MSR_IA32_LASTBRANCHTOIP:
2383 *data = svm->vmcb->save.br_to;
2384 break;
2385 case MSR_IA32_LASTINTFROMIP:
2386 *data = svm->vmcb->save.last_excp_from;
2387 break;
2388 case MSR_IA32_LASTINTTOIP:
2389 *data = svm->vmcb->save.last_excp_to;
2390 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002391 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002392 *data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002393 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002394 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002395 *data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002396 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01002397 case MSR_IA32_UCODE_REV:
2398 *data = 0x01000065;
2399 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002400 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002401 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002402 }
2403 return 0;
2404}
2405
Avi Kivity851ba692009-08-24 11:10:17 +03002406static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002407{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002408 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002409 u64 data;
2410
Avi Kivity59200272010-01-25 19:47:02 +02002411 if (svm_get_msr(&svm->vcpu, ecx, &data)) {
2412 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002413 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002414 } else {
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002415 trace_kvm_msr_read(ecx, data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002416
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002417 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002418 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002419 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002420 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002421 }
2422 return 1;
2423}
2424
Joerg Roedel4a810182010-02-24 18:59:15 +01002425static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2426{
2427 struct vcpu_svm *svm = to_svm(vcpu);
2428 int svm_dis, chg_mask;
2429
2430 if (data & ~SVM_VM_CR_VALID_MASK)
2431 return 1;
2432
2433 chg_mask = SVM_VM_CR_VALID_MASK;
2434
2435 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2436 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2437
2438 svm->nested.vm_cr_msr &= ~chg_mask;
2439 svm->nested.vm_cr_msr |= (data & chg_mask);
2440
2441 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2442
2443 /* check for svm_disable while efer.svme is set */
2444 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2445 return 1;
2446
2447 return 0;
2448}
2449
Avi Kivity6aa8b732006-12-10 02:21:36 -08002450static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
2451{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002452 struct vcpu_svm *svm = to_svm(vcpu);
2453
Avi Kivity6aa8b732006-12-10 02:21:36 -08002454 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302455 case MSR_IA32_TSC: {
Joerg Roedel20824f32009-09-16 15:24:18 +02002456 u64 tsc_offset = data - native_read_tsc();
2457 u64 g_tsc_offset = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002458
Joerg Roedel20824f32009-09-16 15:24:18 +02002459 if (is_nested(svm)) {
2460 g_tsc_offset = svm->vmcb->control.tsc_offset -
2461 svm->nested.hsave->control.tsc_offset;
2462 svm->nested.hsave->control.tsc_offset = tsc_offset;
2463 }
2464
2465 svm->vmcb->control.tsc_offset = tsc_offset + g_tsc_offset;
2466
Avi Kivity6aa8b732006-12-10 02:21:36 -08002467 break;
2468 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08002469 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002470 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002471 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08002472#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002473 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002474 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002475 break;
2476 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002477 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002478 break;
2479 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002480 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002481 break;
2482 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002483 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002484 break;
2485#endif
2486 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002487 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002488 break;
2489 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002490 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002491 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002492 break;
2493 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002494 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002495 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002496 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002497 case MSR_IA32_DEBUGCTLMSR:
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002498 if (!svm_has(SVM_FEATURE_LBRV)) {
2499 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002500 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002501 break;
2502 }
2503 if (data & DEBUGCTL_RESERVED_BITS)
2504 return 1;
2505
2506 svm->vmcb->save.dbgctl = data;
2507 if (data & (1ULL<<0))
2508 svm_enable_lbrv(svm);
2509 else
2510 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01002511 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002512 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002513 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002514 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002515 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002516 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002517 case MSR_VM_IGNNE:
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002518 pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
2519 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002520 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002521 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002522 }
2523 return 0;
2524}
2525
Avi Kivity851ba692009-08-24 11:10:17 +03002526static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002527{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002528 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002529 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002530 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002531
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002532
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002533 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Avi Kivity59200272010-01-25 19:47:02 +02002534 if (svm_set_msr(&svm->vcpu, ecx, data)) {
2535 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002536 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002537 } else {
2538 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10002539 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02002540 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002541 return 1;
2542}
2543
Avi Kivity851ba692009-08-24 11:10:17 +03002544static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002545{
Rusty Russelle756fc62007-07-30 20:07:08 +10002546 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03002547 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002548 else
Avi Kivity851ba692009-08-24 11:10:17 +03002549 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002550}
2551
Avi Kivity851ba692009-08-24 11:10:17 +03002552static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08002553{
Avi Kivity851ba692009-08-24 11:10:17 +03002554 struct kvm_run *kvm_run = svm->vcpu.run;
2555
Alexander Graff0b85052008-11-25 20:17:01 +01002556 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03002557 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -08002558 /*
2559 * If the user space waits to inject interrupts, exit as soon as
2560 * possible
2561 */
Gleb Natapov80618232009-04-21 17:44:56 +03002562 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
2563 kvm_run->request_interrupt_window &&
2564 !kvm_cpu_has_interrupt(&svm->vcpu)) {
Rusty Russelle756fc62007-07-30 20:07:08 +10002565 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002566 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
2567 return 0;
2568 }
2569
2570 return 1;
2571}
2572
Mark Langsdorf565d0992009-10-06 14:25:02 -05002573static int pause_interception(struct vcpu_svm *svm)
2574{
2575 kvm_vcpu_on_spin(&(svm->vcpu));
2576 return 1;
2577}
2578
Avi Kivity851ba692009-08-24 11:10:17 +03002579static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
Joerg Roedele0231712010-02-24 18:59:10 +01002580 [SVM_EXIT_READ_CR0] = emulate_on_interception,
2581 [SVM_EXIT_READ_CR3] = emulate_on_interception,
2582 [SVM_EXIT_READ_CR4] = emulate_on_interception,
2583 [SVM_EXIT_READ_CR8] = emulate_on_interception,
Avi Kivityd2251572010-01-06 10:55:27 +02002584 [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002585 [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
2586 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
2587 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
2588 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
2589 [SVM_EXIT_READ_DR0] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002590 [SVM_EXIT_READ_DR1] = emulate_on_interception,
2591 [SVM_EXIT_READ_DR2] = emulate_on_interception,
2592 [SVM_EXIT_READ_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002593 [SVM_EXIT_READ_DR4] = emulate_on_interception,
2594 [SVM_EXIT_READ_DR5] = emulate_on_interception,
2595 [SVM_EXIT_READ_DR6] = emulate_on_interception,
2596 [SVM_EXIT_READ_DR7] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002597 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
2598 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
2599 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
2600 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002601 [SVM_EXIT_WRITE_DR4] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002602 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002603 [SVM_EXIT_WRITE_DR6] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002604 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002605 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
2606 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002607 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002608 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
2609 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
2610 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
2611 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02002612 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002613 [SVM_EXIT_SMI] = nop_on_interception,
2614 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08002615 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002616 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002617 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002618 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05002619 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002620 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03002621 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02002622 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002623 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002624 [SVM_EXIT_MSR] = msr_interception,
2625 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002626 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01002627 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02002628 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01002629 [SVM_EXIT_VMLOAD] = vmload_interception,
2630 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01002631 [SVM_EXIT_STGI] = stgi_interception,
2632 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02002633 [SVM_EXIT_SKINIT] = skinit_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002634 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01002635 [SVM_EXIT_MONITOR] = invalid_op_interception,
2636 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002637 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002638};
2639
Avi Kivity851ba692009-08-24 11:10:17 +03002640static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002641{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002642 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002643 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002644 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002645
Avi Kivity5bfd8b52010-03-11 10:50:44 +02002646 trace_kvm_exit(exit_code, vcpu);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002647
Joerg Roedel2be4fc72010-04-22 12:33:09 +02002648 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR0_MASK))
2649 vcpu->arch.cr0 = svm->vmcb->save.cr0;
2650 if (npt_enabled)
2651 vcpu->arch.cr3 = svm->vmcb->save.cr3;
2652
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002653 if (unlikely(svm->nested.exit_required)) {
2654 nested_svm_vmexit(svm);
2655 svm->nested.exit_required = false;
2656
2657 return 1;
2658 }
2659
Alexander Grafcf74a782008-11-25 20:17:08 +01002660 if (is_nested(svm)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02002661 int vmexit;
2662
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02002663 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
2664 svm->vmcb->control.exit_info_1,
2665 svm->vmcb->control.exit_info_2,
2666 svm->vmcb->control.exit_int_info,
2667 svm->vmcb->control.exit_int_info_err);
2668
Joerg Roedel410e4d52009-08-07 11:49:44 +02002669 vmexit = nested_svm_exit_special(svm);
2670
2671 if (vmexit == NESTED_EXIT_CONTINUE)
2672 vmexit = nested_svm_exit_handled(svm);
2673
2674 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01002675 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01002676 }
2677
Joerg Roedela5c38322009-08-07 11:49:32 +02002678 svm_complete_interrupts(svm);
2679
Avi Kivity04d2cc72007-09-10 18:10:54 +03002680 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
2681 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2682 kvm_run->fail_entry.hardware_entry_failure_reason
2683 = svm->vmcb->control.exit_code;
2684 return 0;
2685 }
2686
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002687 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002688 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002689 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002690 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
2691 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002692 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002693 exit_code);
2694
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02002695 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08002696 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002697 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03002698 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002699 return 0;
2700 }
2701
Avi Kivity851ba692009-08-24 11:10:17 +03002702 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002703}
2704
2705static void reload_tss(struct kvm_vcpu *vcpu)
2706{
2707 int cpu = raw_smp_processor_id();
2708
Tejun Heo0fe1e002009-10-29 22:34:14 +09002709 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
2710 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002711 load_TR_desc();
2712}
2713
Rusty Russelle756fc62007-07-30 20:07:08 +10002714static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002715{
2716 int cpu = raw_smp_processor_id();
2717
Tejun Heo0fe1e002009-10-29 22:34:14 +09002718 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002719
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002720 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03002721 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09002722 if (svm->asid_generation != sd->asid_generation)
2723 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002724}
2725
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002726static void svm_inject_nmi(struct kvm_vcpu *vcpu)
2727{
2728 struct vcpu_svm *svm = to_svm(vcpu);
2729
2730 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
2731 vcpu->arch.hflags |= HF_NMI_MASK;
2732 svm->vmcb->control.intercept |= (1UL << INTERCEPT_IRET);
2733 ++vcpu->stat.nmi_injections;
2734}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002735
Eddie Dong85f455f2007-07-06 12:20:49 +03002736static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002737{
2738 struct vmcb_control_area *control;
2739
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002740 trace_kvm_inj_virq(irq);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002741
Avi Kivityfa89a812008-09-01 15:57:51 +03002742 ++svm->vcpu.stat.irq_injections;
Rusty Russelle756fc62007-07-30 20:07:08 +10002743 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03002744 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002745 control->int_ctl &= ~V_INTR_PRIO_MASK;
2746 control->int_ctl |= V_IRQ_MASK |
2747 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
2748}
2749
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002750static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03002751{
2752 struct vcpu_svm *svm = to_svm(vcpu);
2753
Joerg Roedel2af91942009-08-07 11:49:28 +02002754 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01002755
Alexander Graf219b65d2009-06-15 15:21:25 +02002756 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
2757 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002758}
2759
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002760static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
2761{
2762 struct vcpu_svm *svm = to_svm(vcpu);
2763
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002764 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
2765 return;
2766
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002767 if (irr == -1)
2768 return;
2769
2770 if (tpr >= irr)
2771 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK;
2772}
2773
2774static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02002775{
2776 struct vcpu_svm *svm = to_svm(vcpu);
2777 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02002778 int ret;
2779 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
2780 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
2781 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
2782
2783 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02002784}
2785
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002786static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
2787{
2788 struct vcpu_svm *svm = to_svm(vcpu);
2789
2790 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
2791}
2792
2793static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
2794{
2795 struct vcpu_svm *svm = to_svm(vcpu);
2796
2797 if (masked) {
2798 svm->vcpu.arch.hflags |= HF_NMI_MASK;
2799 svm->vmcb->control.intercept |= (1UL << INTERCEPT_IRET);
2800 } else {
2801 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
2802 svm->vmcb->control.intercept &= ~(1UL << INTERCEPT_IRET);
2803 }
2804}
2805
Gleb Natapov78646122009-03-23 12:12:11 +02002806static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
2807{
2808 struct vcpu_svm *svm = to_svm(vcpu);
2809 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02002810 int ret;
2811
2812 if (!gif_set(svm) ||
2813 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
2814 return 0;
2815
2816 ret = !!(vmcb->save.rflags & X86_EFLAGS_IF);
2817
2818 if (is_nested(svm))
2819 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
2820
2821 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02002822}
2823
Gleb Natapov9222be12009-04-23 17:14:37 +03002824static void enable_irq_window(struct kvm_vcpu *vcpu)
2825{
Alexander Graf219b65d2009-06-15 15:21:25 +02002826 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02002827
Joerg Roedele0231712010-02-24 18:59:10 +01002828 /*
2829 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
2830 * 1, because that's a separate STGI/VMRUN intercept. The next time we
2831 * get that intercept, this function will be called again though and
2832 * we'll get the vintr intercept.
2833 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01002834 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02002835 svm_set_vintr(svm);
2836 svm_inject_irq(svm, 0x0);
2837 }
Gleb Natapov9222be12009-04-23 17:14:37 +03002838}
2839
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002840static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002841{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002842 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03002843
Gleb Natapov44c11432009-05-11 13:35:52 +03002844 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
2845 == HF_NMI_MASK)
2846 return; /* IRET will cause a vm exit */
2847
Joerg Roedele0231712010-02-24 18:59:10 +01002848 /*
2849 * Something prevents NMI from been injected. Single step over possible
2850 * problem (IRET or exception injection or interrupt shadow)
2851 */
Joerg Roedel924584c2010-04-22 12:33:07 +02002852 svm->nmi_singlestep = true;
2853 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
2854 update_db_intercept(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03002855}
2856
Izik Eiduscbc94022007-10-25 00:29:55 +02002857static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
2858{
2859 return 0;
2860}
2861
Avi Kivityd9e368d2007-06-07 19:18:30 +03002862static void svm_flush_tlb(struct kvm_vcpu *vcpu)
2863{
2864 force_new_asid(vcpu);
2865}
2866
Avi Kivity04d2cc72007-09-10 18:10:54 +03002867static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
2868{
2869}
2870
Joerg Roedeld7bf8222008-04-16 16:51:17 +02002871static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
2872{
2873 struct vcpu_svm *svm = to_svm(vcpu);
2874
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002875 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
2876 return;
2877
Joerg Roedeld7bf8222008-04-16 16:51:17 +02002878 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) {
2879 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03002880 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02002881 }
2882}
2883
Joerg Roedel649d6862008-04-16 16:51:15 +02002884static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
2885{
2886 struct vcpu_svm *svm = to_svm(vcpu);
2887 u64 cr8;
2888
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002889 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
2890 return;
2891
Joerg Roedel649d6862008-04-16 16:51:15 +02002892 cr8 = kvm_get_cr8(vcpu);
2893 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
2894 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
2895}
2896
Gleb Natapov9222be12009-04-23 17:14:37 +03002897static void svm_complete_interrupts(struct vcpu_svm *svm)
2898{
2899 u8 vector;
2900 int type;
2901 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01002902 unsigned int3_injected = svm->int3_injected;
2903
2904 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03002905
Gleb Natapov44c11432009-05-11 13:35:52 +03002906 if (svm->vcpu.arch.hflags & HF_IRET_MASK)
2907 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
2908
Gleb Natapov9222be12009-04-23 17:14:37 +03002909 svm->vcpu.arch.nmi_injected = false;
2910 kvm_clear_exception_queue(&svm->vcpu);
2911 kvm_clear_interrupt_queue(&svm->vcpu);
2912
2913 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
2914 return;
2915
2916 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
2917 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
2918
2919 switch (type) {
2920 case SVM_EXITINTINFO_TYPE_NMI:
2921 svm->vcpu.arch.nmi_injected = true;
2922 break;
2923 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01002924 /*
2925 * In case of software exceptions, do not reinject the vector,
2926 * but re-execute the instruction instead. Rewind RIP first
2927 * if we emulated INT3 before.
2928 */
2929 if (kvm_exception_is_soft(vector)) {
2930 if (vector == BP_VECTOR && int3_injected &&
2931 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
2932 kvm_rip_write(&svm->vcpu,
2933 kvm_rip_read(&svm->vcpu) -
2934 int3_injected);
Gleb Natapov9222be12009-04-23 17:14:37 +03002935 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01002936 }
Gleb Natapov9222be12009-04-23 17:14:37 +03002937 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
2938 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02002939 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03002940
2941 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02002942 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03002943 break;
2944 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002945 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03002946 break;
2947 default:
2948 break;
2949 }
2950}
2951
Avi Kivity80e31d42008-07-14 14:44:59 +03002952#ifdef CONFIG_X86_64
2953#define R "r"
2954#else
2955#define R "e"
2956#endif
2957
Avi Kivity851ba692009-08-24 11:10:17 +03002958static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002959{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002960 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002961 u16 fs_selector;
2962 u16 gs_selector;
2963 u16 ldt_selector;
Avi Kivityd9e368d2007-06-07 19:18:30 +03002964
Joerg Roedel2041a062010-04-22 12:33:08 +02002965 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
2966 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
2967 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
2968
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002969 /*
2970 * A vmexit emulation is required before the vcpu can be executed
2971 * again.
2972 */
2973 if (unlikely(svm->nested.exit_required))
2974 return;
2975
Rusty Russelle756fc62007-07-30 20:07:08 +10002976 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002977
Joerg Roedel649d6862008-04-16 16:51:15 +02002978 sync_lapic_to_cr8(vcpu);
2979
Avi Kivity6aa8b732006-12-10 02:21:36 -08002980 save_host_msrs(vcpu);
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002981 fs_selector = kvm_read_fs();
2982 gs_selector = kvm_read_gs();
2983 ldt_selector = kvm_read_ldt();
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02002984 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002985 /* required for live migration with NPT */
2986 if (npt_enabled)
2987 svm->vmcb->save.cr3 = vcpu->arch.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002988
Avi Kivity04d2cc72007-09-10 18:10:54 +03002989 clgi();
2990
2991 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08002992
Avi Kivity6aa8b732006-12-10 02:21:36 -08002993 asm volatile (
Avi Kivity80e31d42008-07-14 14:44:59 +03002994 "push %%"R"bp; \n\t"
2995 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
2996 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
2997 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
2998 "mov %c[rsi](%[svm]), %%"R"si \n\t"
2999 "mov %c[rdi](%[svm]), %%"R"di \n\t"
3000 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003001#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003002 "mov %c[r8](%[svm]), %%r8 \n\t"
3003 "mov %c[r9](%[svm]), %%r9 \n\t"
3004 "mov %c[r10](%[svm]), %%r10 \n\t"
3005 "mov %c[r11](%[svm]), %%r11 \n\t"
3006 "mov %c[r12](%[svm]), %%r12 \n\t"
3007 "mov %c[r13](%[svm]), %%r13 \n\t"
3008 "mov %c[r14](%[svm]), %%r14 \n\t"
3009 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003010#endif
3011
Avi Kivity6aa8b732006-12-10 02:21:36 -08003012 /* Enter guest mode */
Avi Kivity80e31d42008-07-14 14:44:59 +03003013 "push %%"R"ax \n\t"
3014 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003015 __ex(SVM_VMLOAD) "\n\t"
3016 __ex(SVM_VMRUN) "\n\t"
3017 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity80e31d42008-07-14 14:44:59 +03003018 "pop %%"R"ax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003019
3020 /* Save guest registers, load host registers */
Avi Kivity80e31d42008-07-14 14:44:59 +03003021 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
3022 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
3023 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
3024 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
3025 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
3026 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003027#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003028 "mov %%r8, %c[r8](%[svm]) \n\t"
3029 "mov %%r9, %c[r9](%[svm]) \n\t"
3030 "mov %%r10, %c[r10](%[svm]) \n\t"
3031 "mov %%r11, %c[r11](%[svm]) \n\t"
3032 "mov %%r12, %c[r12](%[svm]) \n\t"
3033 "mov %%r13, %c[r13](%[svm]) \n\t"
3034 "mov %%r14, %c[r14](%[svm]) \n\t"
3035 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003036#endif
Avi Kivity80e31d42008-07-14 14:44:59 +03003037 "pop %%"R"bp"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003038 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003039 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003040 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003041 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
3042 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
3043 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
3044 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
3045 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
3046 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003047#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003048 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
3049 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
3050 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
3051 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
3052 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
3053 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
3054 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
3055 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003056#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02003057 : "cc", "memory"
Avi Kivity80e31d42008-07-14 14:44:59 +03003058 , R"bx", R"cx", R"dx", R"si", R"di"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003059#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02003060 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
3061#endif
3062 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003063
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003064 vcpu->arch.cr2 = svm->vmcb->save.cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003065 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3066 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3067 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003068
Avi Kivityd6e88ae2008-07-10 16:53:33 +03003069 kvm_load_fs(fs_selector);
3070 kvm_load_gs(gs_selector);
3071 kvm_load_ldt(ldt_selector);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003072 load_host_msrs(vcpu);
3073
3074 reload_tss(vcpu);
3075
Avi Kivity56ba47d2007-11-07 17:14:18 +02003076 local_irq_disable();
3077
3078 stgi();
3079
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003080 sync_cr8_to_lapic(vcpu);
3081
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003082 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003083
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003084 if (npt_enabled) {
3085 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3086 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3087 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003088}
3089
Avi Kivity80e31d42008-07-14 14:44:59 +03003090#undef R
3091
Avi Kivity6aa8b732006-12-10 02:21:36 -08003092static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3093{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003094 struct vcpu_svm *svm = to_svm(vcpu);
3095
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003096 if (npt_enabled) {
3097 svm->vmcb->control.nested_cr3 = root;
3098 force_new_asid(vcpu);
3099 return;
3100 }
3101
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003102 svm->vmcb->save.cr3 = root;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003103 force_new_asid(vcpu);
3104}
3105
Avi Kivity6aa8b732006-12-10 02:21:36 -08003106static int is_disabled(void)
3107{
Joerg Roedel6031a612007-06-22 12:29:50 +03003108 u64 vm_cr;
3109
3110 rdmsrl(MSR_VM_CR, vm_cr);
3111 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3112 return 1;
3113
Avi Kivity6aa8b732006-12-10 02:21:36 -08003114 return 0;
3115}
3116
Ingo Molnar102d8322007-02-19 14:37:47 +02003117static void
3118svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3119{
3120 /*
3121 * Patch in the VMMCALL instruction:
3122 */
3123 hypercall[0] = 0x0f;
3124 hypercall[1] = 0x01;
3125 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003126}
3127
Yang, Sheng002c7f72007-07-31 14:23:01 +03003128static void svm_check_processor_compat(void *rtn)
3129{
3130 *(int *)rtn = 0;
3131}
3132
Avi Kivity774ead32007-12-26 13:57:04 +02003133static bool svm_cpu_has_accelerated_tpr(void)
3134{
3135 return false;
3136}
3137
Sheng Yang67253af2008-04-25 10:20:22 +08003138static int get_npt_level(void)
3139{
3140#ifdef CONFIG_X86_64
3141 return PT64_ROOT_LEVEL;
3142#else
3143 return PT32E_ROOT_LEVEL;
3144#endif
3145}
3146
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003147static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003148{
3149 return 0;
3150}
3151
Sheng Yang0e851882009-12-18 16:48:46 +08003152static void svm_cpuid_update(struct kvm_vcpu *vcpu)
3153{
3154}
3155
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003156static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
3157{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003158 switch (func) {
3159 case 0x8000000A:
3160 entry->eax = 1; /* SVM revision 1 */
3161 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
3162 ASID emulation to nested SVM */
3163 entry->ecx = 0; /* Reserved */
3164 entry->edx = 0; /* Do not support any additional features */
3165
3166 break;
3167 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003168}
3169
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003170static const struct trace_print_flags svm_exit_reasons_str[] = {
Joerg Roedele0231712010-02-24 18:59:10 +01003171 { SVM_EXIT_READ_CR0, "read_cr0" },
3172 { SVM_EXIT_READ_CR3, "read_cr3" },
3173 { SVM_EXIT_READ_CR4, "read_cr4" },
3174 { SVM_EXIT_READ_CR8, "read_cr8" },
3175 { SVM_EXIT_WRITE_CR0, "write_cr0" },
3176 { SVM_EXIT_WRITE_CR3, "write_cr3" },
3177 { SVM_EXIT_WRITE_CR4, "write_cr4" },
3178 { SVM_EXIT_WRITE_CR8, "write_cr8" },
3179 { SVM_EXIT_READ_DR0, "read_dr0" },
3180 { SVM_EXIT_READ_DR1, "read_dr1" },
3181 { SVM_EXIT_READ_DR2, "read_dr2" },
3182 { SVM_EXIT_READ_DR3, "read_dr3" },
3183 { SVM_EXIT_WRITE_DR0, "write_dr0" },
3184 { SVM_EXIT_WRITE_DR1, "write_dr1" },
3185 { SVM_EXIT_WRITE_DR2, "write_dr2" },
3186 { SVM_EXIT_WRITE_DR3, "write_dr3" },
3187 { SVM_EXIT_WRITE_DR5, "write_dr5" },
3188 { SVM_EXIT_WRITE_DR7, "write_dr7" },
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003189 { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
3190 { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
3191 { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
3192 { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
3193 { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
3194 { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
3195 { SVM_EXIT_INTR, "interrupt" },
3196 { SVM_EXIT_NMI, "nmi" },
3197 { SVM_EXIT_SMI, "smi" },
3198 { SVM_EXIT_INIT, "init" },
3199 { SVM_EXIT_VINTR, "vintr" },
3200 { SVM_EXIT_CPUID, "cpuid" },
3201 { SVM_EXIT_INVD, "invd" },
3202 { SVM_EXIT_HLT, "hlt" },
3203 { SVM_EXIT_INVLPG, "invlpg" },
3204 { SVM_EXIT_INVLPGA, "invlpga" },
3205 { SVM_EXIT_IOIO, "io" },
3206 { SVM_EXIT_MSR, "msr" },
3207 { SVM_EXIT_TASK_SWITCH, "task_switch" },
3208 { SVM_EXIT_SHUTDOWN, "shutdown" },
3209 { SVM_EXIT_VMRUN, "vmrun" },
3210 { SVM_EXIT_VMMCALL, "hypercall" },
3211 { SVM_EXIT_VMLOAD, "vmload" },
3212 { SVM_EXIT_VMSAVE, "vmsave" },
3213 { SVM_EXIT_STGI, "stgi" },
3214 { SVM_EXIT_CLGI, "clgi" },
3215 { SVM_EXIT_SKINIT, "skinit" },
3216 { SVM_EXIT_WBINVD, "wbinvd" },
3217 { SVM_EXIT_MONITOR, "monitor" },
3218 { SVM_EXIT_MWAIT, "mwait" },
3219 { SVM_EXIT_NPF, "npf" },
3220 { -1, NULL }
3221};
3222
Sheng Yang17cc3932010-01-05 19:02:27 +08003223static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02003224{
Sheng Yang17cc3932010-01-05 19:02:27 +08003225 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02003226}
3227
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003228static bool svm_rdtscp_supported(void)
3229{
3230 return false;
3231}
3232
Avi Kivity02daab22009-12-30 12:40:26 +02003233static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
3234{
3235 struct vcpu_svm *svm = to_svm(vcpu);
3236
Avi Kivity02daab22009-12-30 12:40:26 +02003237 svm->vmcb->control.intercept_exceptions |= 1 << NM_VECTOR;
Joerg Roedel66a562f2010-02-19 16:23:08 +01003238 if (is_nested(svm))
3239 svm->nested.hsave->control.intercept_exceptions |= 1 << NM_VECTOR;
3240 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02003241}
3242
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003243static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003244 .cpu_has_kvm_support = has_svm,
3245 .disabled_by_bios = is_disabled,
3246 .hardware_setup = svm_hardware_setup,
3247 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003248 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003249 .hardware_enable = svm_hardware_enable,
3250 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003251 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003252
3253 .vcpu_create = svm_create_vcpu,
3254 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003255 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003256
Avi Kivity04d2cc72007-09-10 18:10:54 +03003257 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003258 .vcpu_load = svm_vcpu_load,
3259 .vcpu_put = svm_vcpu_put,
3260
3261 .set_guest_debug = svm_guest_debug,
3262 .get_msr = svm_get_msr,
3263 .set_msr = svm_set_msr,
3264 .get_segment_base = svm_get_segment_base,
3265 .get_segment = svm_get_segment,
3266 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003267 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10003268 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02003269 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003270 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003271 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003272 .set_cr3 = svm_set_cr3,
3273 .set_cr4 = svm_set_cr4,
3274 .set_efer = svm_set_efer,
3275 .get_idt = svm_get_idt,
3276 .set_idt = svm_set_idt,
3277 .get_gdt = svm_get_gdt,
3278 .set_gdt = svm_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03003279 .set_dr7 = svm_set_dr7,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003280 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003281 .get_rflags = svm_get_rflags,
3282 .set_rflags = svm_set_rflags,
Avi Kivity6b52d182010-01-21 15:31:47 +02003283 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02003284 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003285
Avi Kivity6aa8b732006-12-10 02:21:36 -08003286 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003287
Avi Kivity6aa8b732006-12-10 02:21:36 -08003288 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003289 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003290 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04003291 .set_interrupt_shadow = svm_set_interrupt_shadow,
3292 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02003293 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003294 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003295 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02003296 .queue_exception = svm_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02003297 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003298 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003299 .get_nmi_mask = svm_get_nmi_mask,
3300 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003301 .enable_nmi_window = enable_nmi_window,
3302 .enable_irq_window = enable_irq_window,
3303 .update_cr8_intercept = update_cr8_intercept,
Izik Eiduscbc94022007-10-25 00:29:55 +02003304
3305 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003306 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003307 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003308
3309 .exit_reasons_str = svm_exit_reasons_str,
Sheng Yang17cc3932010-01-05 19:02:27 +08003310 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08003311
3312 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003313
3314 .rdtscp_supported = svm_rdtscp_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003315
3316 .set_supported_cpuid = svm_set_supported_cpuid,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003317};
3318
3319static int __init svm_init(void)
3320{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003321 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03003322 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003323}
3324
3325static void __exit svm_exit(void)
3326{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003327 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003328}
3329
3330module_init(svm_init)
3331module_exit(svm_exit)