blob: 64b7f60dc5b81b90c218dce5e7082d122c933580 [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,
341 bool has_error_code, u32 error_code)
342{
343 struct vcpu_svm *svm = to_svm(vcpu);
344
Joerg Roedele0231712010-02-24 18:59:10 +0100345 /*
346 * If we are within a nested VM we'd better #VMEXIT and let the guest
347 * handle the exception
348 */
Jan Kiszka116a4752010-02-23 17:47:54 +0100349 if (nested_svm_check_exception(svm, nr, has_error_code, error_code))
350 return;
351
Jan Kiszka66b71382010-02-23 17:47:56 +0100352 if (nr == BP_VECTOR && !svm_has(SVM_FEATURE_NRIP)) {
353 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
354
355 /*
356 * For guest debugging where we have to reinject #BP if some
357 * INT3 is guest-owned:
358 * Emulate nRIP by moving RIP forward. Will fail if injection
359 * raises a fault that is not intercepted. Still better than
360 * failing in all cases.
361 */
362 skip_emulated_instruction(&svm->vcpu);
363 rip = kvm_rip_read(&svm->vcpu);
364 svm->int3_rip = rip + svm->vmcb->save.cs.base;
365 svm->int3_injected = rip - old_rip;
366 }
367
Jan Kiszka116a4752010-02-23 17:47:54 +0100368 svm->vmcb->control.event_inj = nr
369 | SVM_EVTINJ_VALID
370 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
371 | SVM_EVTINJ_TYPE_EXEPT;
372 svm->vmcb->control.event_inj_err = error_code;
373}
374
Avi Kivity6aa8b732006-12-10 02:21:36 -0800375static int has_svm(void)
376{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200377 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800378
Eduardo Habkost63d11422008-11-17 19:03:20 -0200379 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800380 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800381 return 0;
382 }
383
Avi Kivity6aa8b732006-12-10 02:21:36 -0800384 return 1;
385}
386
387static void svm_hardware_disable(void *garbage)
388{
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200389 cpu_svm_disable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800390}
391
Alexander Graf10474ae2009-09-15 11:37:46 +0200392static int svm_hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800393{
394
Tejun Heo0fe1e002009-10-29 22:34:14 +0900395 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800396 uint64_t efer;
Gleb Natapov89a27f42010-02-16 10:51:48 +0200397 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800398 struct desc_struct *gdt;
399 int me = raw_smp_processor_id();
400
Alexander Graf10474ae2009-09-15 11:37:46 +0200401 rdmsrl(MSR_EFER, efer);
402 if (efer & EFER_SVME)
403 return -EBUSY;
404
Avi Kivity6aa8b732006-12-10 02:21:36 -0800405 if (!has_svm()) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000406 printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
407 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200408 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800409 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900410 sd = per_cpu(svm_data, me);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800411
Tejun Heo0fe1e002009-10-29 22:34:14 +0900412 if (!sd) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000413 printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -0800414 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200415 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800416 }
417
Tejun Heo0fe1e002009-10-29 22:34:14 +0900418 sd->asid_generation = 1;
419 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
420 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800421
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200422 native_store_gdt(&gdt_descr);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200423 gdt = (struct desc_struct *)gdt_descr.address;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900424 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800425
Alexander Graf9962d032008-11-25 20:17:02 +0100426 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800427
Linus Torvaldsd0316552009-12-14 09:58:24 -0800428 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200429
430 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800431}
432
Joerg Roedel0da1db752008-07-02 16:02:11 +0200433static void svm_cpu_uninit(int cpu)
434{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900435 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200436
Tejun Heo0fe1e002009-10-29 22:34:14 +0900437 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200438 return;
439
440 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900441 __free_page(sd->save_area);
442 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200443}
444
Avi Kivity6aa8b732006-12-10 02:21:36 -0800445static int svm_cpu_init(int cpu)
446{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900447 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800448 int r;
449
Tejun Heo0fe1e002009-10-29 22:34:14 +0900450 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
451 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800452 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900453 sd->cpu = cpu;
454 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800455 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900456 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800457 goto err_1;
458
Tejun Heo0fe1e002009-10-29 22:34:14 +0900459 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800460
461 return 0;
462
463err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900464 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800465 return r;
466
467}
468
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100469static bool valid_msr_intercept(u32 index)
470{
471 int i;
472
473 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
474 if (direct_access_msrs[i].index == index)
475 return true;
476
477 return false;
478}
479
Rusty Russellbfc733a2007-07-31 20:42:42 +1000480static void set_msr_interception(u32 *msrpm, unsigned msr,
481 int read, int write)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800482{
Joerg Roedel455716f2010-03-01 15:34:35 +0100483 u8 bit_read, bit_write;
484 unsigned long tmp;
485 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800486
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100487 /*
488 * If this warning triggers extend the direct_access_msrs list at the
489 * beginning of the file
490 */
491 WARN_ON(!valid_msr_intercept(msr));
492
Joerg Roedel455716f2010-03-01 15:34:35 +0100493 offset = svm_msrpm_offset(msr);
494 bit_read = 2 * (msr & 0x0f);
495 bit_write = 2 * (msr & 0x0f) + 1;
496 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800497
Joerg Roedel455716f2010-03-01 15:34:35 +0100498 BUG_ON(offset == MSR_INVALID);
499
500 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
501 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
502
503 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800504}
505
Joerg Roedelf65c2292008-02-13 18:58:46 +0100506static void svm_vcpu_init_msrpm(u32 *msrpm)
507{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100508 int i;
509
Joerg Roedelf65c2292008-02-13 18:58:46 +0100510 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
511
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100512 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
513 if (!direct_access_msrs[i].always)
514 continue;
515
516 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
517 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100518}
519
Joerg Roedel323c3d82010-03-01 15:34:37 +0100520static void add_msr_offset(u32 offset)
521{
522 int i;
523
524 for (i = 0; i < MSRPM_OFFSETS; ++i) {
525
526 /* Offset already in list? */
527 if (msrpm_offsets[i] == offset)
528 return;
529
530 /* Slot used by another offset? */
531 if (msrpm_offsets[i] != MSR_INVALID)
532 continue;
533
534 /* Add offset to list */
535 msrpm_offsets[i] = offset;
536
537 return;
538 }
539
540 /*
541 * If this BUG triggers the msrpm_offsets table has an overflow. Just
542 * increase MSRPM_OFFSETS in this case.
543 */
544 BUG();
545}
546
547static void init_msrpm_offsets(void)
548{
549 int i;
550
551 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
552
553 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
554 u32 offset;
555
556 offset = svm_msrpm_offset(direct_access_msrs[i].index);
557 BUG_ON(offset == MSR_INVALID);
558
559 add_msr_offset(offset);
560 }
561}
562
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100563static void svm_enable_lbrv(struct vcpu_svm *svm)
564{
565 u32 *msrpm = svm->msrpm;
566
567 svm->vmcb->control.lbr_ctl = 1;
568 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
569 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
570 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
571 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
572}
573
574static void svm_disable_lbrv(struct vcpu_svm *svm)
575{
576 u32 *msrpm = svm->msrpm;
577
578 svm->vmcb->control.lbr_ctl = 0;
579 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
580 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
581 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
582 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
583}
584
Avi Kivity6aa8b732006-12-10 02:21:36 -0800585static __init int svm_hardware_setup(void)
586{
587 int cpu;
588 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100589 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800590 int r;
591
Avi Kivity6aa8b732006-12-10 02:21:36 -0800592 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
593
594 if (!iopm_pages)
595 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300596
597 iopm_va = page_address(iopm_pages);
598 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800599 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
600
Joerg Roedel323c3d82010-03-01 15:34:37 +0100601 init_msrpm_offsets();
602
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100603 if (boot_cpu_has(X86_FEATURE_NX))
604 kvm_enable_efer_bits(EFER_NX);
605
Alexander Graf1b2fd702009-02-02 16:23:51 +0100606 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
607 kvm_enable_efer_bits(EFER_FFXSR);
608
Alexander Graf236de052008-11-25 20:17:10 +0100609 if (nested) {
610 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
611 kvm_enable_efer_bits(EFER_SVME);
612 }
613
Zachary Amsden3230bb42009-09-29 11:38:37 -1000614 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800615 r = svm_cpu_init(cpu);
616 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100617 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800618 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100619
620 svm_features = cpuid_edx(SVM_CPUID_FUNC);
621
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100622 if (!svm_has(SVM_FEATURE_NPT))
623 npt_enabled = false;
624
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100625 if (npt_enabled && !npt) {
626 printk(KERN_INFO "kvm: Nested Paging disabled\n");
627 npt_enabled = false;
628 }
629
Joerg Roedel18552672008-02-07 13:47:41 +0100630 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100631 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100632 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200633 } else
634 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100635
Avi Kivity6aa8b732006-12-10 02:21:36 -0800636 return 0;
637
Joerg Roedelf65c2292008-02-13 18:58:46 +0100638err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800639 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
640 iopm_base = 0;
641 return r;
642}
643
644static __exit void svm_hardware_unsetup(void)
645{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200646 int cpu;
647
Zachary Amsden3230bb42009-09-29 11:38:37 -1000648 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200649 svm_cpu_uninit(cpu);
650
Avi Kivity6aa8b732006-12-10 02:21:36 -0800651 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100652 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800653}
654
655static void init_seg(struct vmcb_seg *seg)
656{
657 seg->selector = 0;
658 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +0100659 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800660 seg->limit = 0xffff;
661 seg->base = 0;
662}
663
664static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
665{
666 seg->selector = 0;
667 seg->attrib = SVM_SELECTOR_P_MASK | type;
668 seg->limit = 0xffff;
669 seg->base = 0;
670}
671
Joerg Roedele6101a92008-02-13 18:58:45 +0100672static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800673{
Joerg Roedele6101a92008-02-13 18:58:45 +0100674 struct vmcb_control_area *control = &svm->vmcb->control;
675 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800676
Avi Kivitybff78272010-01-07 13:16:08 +0200677 svm->vcpu.fpu_active = 1;
678
Joerg Roedele0231712010-02-24 18:59:10 +0100679 control->intercept_cr_read = INTERCEPT_CR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800680 INTERCEPT_CR3_MASK |
Joerg Roedel649d6862008-04-16 16:51:15 +0200681 INTERCEPT_CR4_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800682
Joerg Roedele0231712010-02-24 18:59:10 +0100683 control->intercept_cr_write = INTERCEPT_CR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800684 INTERCEPT_CR3_MASK |
Avi Kivity80a81192007-12-06 19:50:00 +0200685 INTERCEPT_CR4_MASK |
686 INTERCEPT_CR8_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800687
Joerg Roedele0231712010-02-24 18:59:10 +0100688 control->intercept_dr_read = INTERCEPT_DR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800689 INTERCEPT_DR1_MASK |
690 INTERCEPT_DR2_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100691 INTERCEPT_DR3_MASK |
692 INTERCEPT_DR4_MASK |
693 INTERCEPT_DR5_MASK |
694 INTERCEPT_DR6_MASK |
695 INTERCEPT_DR7_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800696
Joerg Roedele0231712010-02-24 18:59:10 +0100697 control->intercept_dr_write = INTERCEPT_DR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800698 INTERCEPT_DR1_MASK |
699 INTERCEPT_DR2_MASK |
700 INTERCEPT_DR3_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100701 INTERCEPT_DR4_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800702 INTERCEPT_DR5_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100703 INTERCEPT_DR6_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800704 INTERCEPT_DR7_MASK;
705
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500706 control->intercept_exceptions = (1 << PF_VECTOR) |
Joerg Roedel53371b52008-04-09 14:15:30 +0200707 (1 << UD_VECTOR) |
708 (1 << MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800709
710
Joerg Roedele0231712010-02-24 18:59:10 +0100711 control->intercept = (1ULL << INTERCEPT_INTR) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800712 (1ULL << INTERCEPT_NMI) |
Joerg Roedel01525272007-02-19 14:37:47 +0200713 (1ULL << INTERCEPT_SMI) |
Avi Kivityd2251572010-01-06 10:55:27 +0200714 (1ULL << INTERCEPT_SELECTIVE_CR0) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800715 (1ULL << INTERCEPT_CPUID) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200716 (1ULL << INTERCEPT_INVD) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800717 (1ULL << INTERCEPT_HLT) |
Marcelo Tosattia7052892008-09-23 13:18:35 -0300718 (1ULL << INTERCEPT_INVLPG) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800719 (1ULL << INTERCEPT_INVLPGA) |
720 (1ULL << INTERCEPT_IOIO_PROT) |
721 (1ULL << INTERCEPT_MSR_PROT) |
722 (1ULL << INTERCEPT_TASK_SWITCH) |
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800723 (1ULL << INTERCEPT_SHUTDOWN) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800724 (1ULL << INTERCEPT_VMRUN) |
725 (1ULL << INTERCEPT_VMMCALL) |
726 (1ULL << INTERCEPT_VMLOAD) |
727 (1ULL << INTERCEPT_VMSAVE) |
728 (1ULL << INTERCEPT_STGI) |
729 (1ULL << INTERCEPT_CLGI) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100730 (1ULL << INTERCEPT_SKINIT) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200731 (1ULL << INTERCEPT_WBINVD) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100732 (1ULL << INTERCEPT_MONITOR) |
733 (1ULL << INTERCEPT_MWAIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800734
735 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100736 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity0cc50642007-03-25 12:07:27 +0200737 control->tsc_offset = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800738 control->int_ctl = V_INTR_MASKING_MASK;
739
740 init_seg(&save->es);
741 init_seg(&save->ss);
742 init_seg(&save->ds);
743 init_seg(&save->fs);
744 init_seg(&save->gs);
745
746 save->cs.selector = 0xf000;
747 /* Executable/Readable Code Segment */
748 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
749 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
750 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800751 /*
752 * cs.base should really be 0xffff0000, but vmx can't handle that, so
753 * be consistent with it.
754 *
755 * Replace when we have real mode working for vmx.
756 */
757 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800758
759 save->gdtr.limit = 0xffff;
760 save->idtr.limit = 0xffff;
761
762 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
763 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
764
Alexander Graf9962d032008-11-25 20:17:02 +0100765 save->efer = EFER_SVME;
Mike Dayd77c26f2007-10-08 09:02:08 -0400766 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800767 save->dr7 = 0x400;
768 save->rflags = 2;
769 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300770 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800771
Joerg Roedele0231712010-02-24 18:59:10 +0100772 /*
773 * This is the guest-visible cr0 value.
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200774 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800775 */
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200776 svm->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
777 kvm_set_cr0(&svm->vcpu, svm->vcpu.arch.cr0);
778
Rusty Russell66aee912007-07-17 23:34:16 +1000779 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800780 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100781
782 if (npt_enabled) {
783 /* Setup VMCB for Nested Paging */
784 control->nested_ctl = 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -0300785 control->intercept &= ~((1ULL << INTERCEPT_TASK_SWITCH) |
786 (1ULL << INTERCEPT_INVLPG));
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100787 control->intercept_exceptions &= ~(1 << PF_VECTOR);
Avi Kivity888f9f32010-01-10 12:14:04 +0200788 control->intercept_cr_read &= ~INTERCEPT_CR3_MASK;
789 control->intercept_cr_write &= ~INTERCEPT_CR3_MASK;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100790 save->g_pat = 0x0007040600070406ULL;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100791 save->cr3 = 0;
792 save->cr4 = 0;
793 }
Avi Kivitya79d2f12008-04-14 13:10:21 +0300794 force_new_asid(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +0100795
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200796 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +0200797 svm->vcpu.arch.hflags = 0;
798
Mark Langsdorf565d0992009-10-06 14:25:02 -0500799 if (svm_has(SVM_FEATURE_PAUSE_FILTER)) {
800 control->pause_filter_count = 3000;
801 control->intercept |= (1ULL << INTERCEPT_PAUSE);
802 }
803
Joerg Roedel2af91942009-08-07 11:49:28 +0200804 enable_gif(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800805}
806
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200807static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +0300808{
809 struct vcpu_svm *svm = to_svm(vcpu);
810
Joerg Roedele6101a92008-02-13 18:58:45 +0100811 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +0200812
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300813 if (!kvm_vcpu_is_bsp(vcpu)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300814 kvm_rip_write(vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800815 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
816 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +0200817 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300818 vcpu->arch.regs_avail = ~0;
819 vcpu->arch.regs_dirty = ~0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200820
821 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300822}
823
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000824static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400826 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800827 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100828 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +0100829 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100830 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000831 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800832
Rusty Russellc16f8622007-07-30 21:12:19 +1000833 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000834 if (!svm) {
835 err = -ENOMEM;
836 goto out;
837 }
838
839 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
840 if (err)
841 goto free_svm;
842
Joerg Roedelf65c2292008-02-13 18:58:46 +0100843 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900844 page = alloc_page(GFP_KERNEL);
845 if (!page)
846 goto uninit;
847
Joerg Roedelf65c2292008-02-13 18:58:46 +0100848 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
849 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900850 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100851
852 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
853 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900854 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100855
Alexander Grafb286d5d2008-11-25 20:17:05 +0100856 hsave_page = alloc_page(GFP_KERNEL);
857 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900858 goto free_page3;
859
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200860 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +0100861
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900862 svm->msrpm = page_address(msrpm_pages);
863 svm_vcpu_init_msrpm(svm->msrpm);
864
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200865 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +0100866 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +0100867
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400868 svm->vmcb = page_address(page);
869 clear_page(svm->vmcb);
870 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
871 svm->asid_generation = 0;
Joerg Roedele6101a92008-02-13 18:58:45 +0100872 init_vmcb(svm);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400873
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000874 fx_init(&svm->vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800875 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300876 if (kvm_vcpu_is_bsp(&svm->vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800877 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800878
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000879 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -0800880
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900881free_page3:
882 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
883free_page2:
884 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
885free_page1:
886 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000887uninit:
888 kvm_vcpu_uninit(&svm->vcpu);
889free_svm:
Rusty Russella4770342007-08-01 14:46:11 +1000890 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000891out:
892 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800893}
894
895static void svm_free_vcpu(struct kvm_vcpu *vcpu)
896{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400897 struct vcpu_svm *svm = to_svm(vcpu);
898
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000899 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +0100900 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200901 __free_page(virt_to_page(svm->nested.hsave));
902 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000903 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +1000904 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905}
906
Avi Kivity15ad7142007-07-11 18:17:21 +0300907static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800908{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400909 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300910 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +0200911
Avi Kivity0cc50642007-03-25 12:07:27 +0200912 if (unlikely(cpu != vcpu->cpu)) {
Joerg Roedele935d482009-09-16 15:24:19 +0200913 u64 delta;
Avi Kivity0cc50642007-03-25 12:07:27 +0200914
Joerg Roedel953899b2009-12-14 12:22:20 +0100915 if (check_tsc_unstable()) {
916 /*
917 * Make sure that the guest sees a monotonically
918 * increasing TSC.
919 */
920 delta = vcpu->arch.host_tsc - native_read_tsc();
921 svm->vmcb->control.tsc_offset += delta;
922 if (is_nested(svm))
923 svm->nested.hsave->control.tsc_offset += delta;
924 }
Avi Kivity0cc50642007-03-25 12:07:27 +0200925 vcpu->cpu = cpu;
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300926 kvm_migrate_timers(vcpu);
Marcelo Tosatti4b656b12009-07-21 12:47:45 -0300927 svm->asid_generation = 0;
Avi Kivity0cc50642007-03-25 12:07:27 +0200928 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300929
930 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400931 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800932}
933
934static void svm_vcpu_put(struct kvm_vcpu *vcpu)
935{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400936 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300937 int i;
938
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200939 ++vcpu->stat.host_state_reload;
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300940 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400941 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300942
Joerg Roedele935d482009-09-16 15:24:19 +0200943 vcpu->arch.host_tsc = native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944}
945
Avi Kivity6aa8b732006-12-10 02:21:36 -0800946static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
947{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400948 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800949}
950
951static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
952{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400953 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800954}
955
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300956static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
957{
958 switch (reg) {
959 case VCPU_EXREG_PDPTR:
960 BUG_ON(!npt_enabled);
961 load_pdptrs(vcpu, vcpu->arch.cr3);
962 break;
963 default:
964 BUG();
965 }
966}
967
Alexander Graff0b85052008-11-25 20:17:01 +0100968static void svm_set_vintr(struct vcpu_svm *svm)
969{
970 svm->vmcb->control.intercept |= 1ULL << INTERCEPT_VINTR;
971}
972
973static void svm_clear_vintr(struct vcpu_svm *svm)
974{
975 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
976}
977
Avi Kivity6aa8b732006-12-10 02:21:36 -0800978static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
979{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400980 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800981
982 switch (seg) {
983 case VCPU_SREG_CS: return &save->cs;
984 case VCPU_SREG_DS: return &save->ds;
985 case VCPU_SREG_ES: return &save->es;
986 case VCPU_SREG_FS: return &save->fs;
987 case VCPU_SREG_GS: return &save->gs;
988 case VCPU_SREG_SS: return &save->ss;
989 case VCPU_SREG_TR: return &save->tr;
990 case VCPU_SREG_LDTR: return &save->ldtr;
991 }
992 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +0000993 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994}
995
996static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
997{
998 struct vmcb_seg *s = svm_seg(vcpu, seg);
999
1000 return s->base;
1001}
1002
1003static void svm_get_segment(struct kvm_vcpu *vcpu,
1004 struct kvm_segment *var, int seg)
1005{
1006 struct vmcb_seg *s = svm_seg(vcpu, seg);
1007
1008 var->base = s->base;
1009 var->limit = s->limit;
1010 var->selector = s->selector;
1011 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1012 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1013 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1014 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1015 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1016 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1017 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
1018 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
Amit Shah25022ac2008-10-27 09:04:17 +00001019
Joerg Roedele0231712010-02-24 18:59:10 +01001020 /*
1021 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001022 * for cross vendor migration purposes by "not present"
1023 */
1024 var->unusable = !var->present || (var->type == 0);
1025
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001026 switch (seg) {
1027 case VCPU_SREG_CS:
1028 /*
1029 * SVM always stores 0 for the 'G' bit in the CS selector in
1030 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
1031 * Intel's VMENTRY has a check on the 'G' bit.
1032 */
Amit Shah25022ac2008-10-27 09:04:17 +00001033 var->g = s->limit > 0xfffff;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001034 break;
1035 case VCPU_SREG_TR:
1036 /*
1037 * Work around a bug where the busy flag in the tr selector
1038 * isn't exposed
1039 */
Amit Shahc0d09822008-10-27 09:04:18 +00001040 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001041 break;
1042 case VCPU_SREG_DS:
1043 case VCPU_SREG_ES:
1044 case VCPU_SREG_FS:
1045 case VCPU_SREG_GS:
1046 /*
1047 * The accessed bit must always be set in the segment
1048 * descriptor cache, although it can be cleared in the
1049 * descriptor, the cached bit always remains at 1. Since
1050 * Intel has a check on this, set it here to support
1051 * cross-vendor migration.
1052 */
1053 if (!var->unusable)
1054 var->type |= 0x1;
1055 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001056 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001057 /*
1058 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001059 * descriptor is left as 1, although the whole segment has
1060 * been made unusable. Clear it here to pass an Intel VMX
1061 * entry check when cross vendor migrating.
1062 */
1063 if (var->unusable)
1064 var->db = 0;
1065 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001066 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001067}
1068
Izik Eidus2e4d2652008-03-24 19:38:34 +02001069static int svm_get_cpl(struct kvm_vcpu *vcpu)
1070{
1071 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1072
1073 return save->cpl;
1074}
1075
Gleb Natapov89a27f42010-02-16 10:51:48 +02001076static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001077{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001078 struct vcpu_svm *svm = to_svm(vcpu);
1079
Gleb Natapov89a27f42010-02-16 10:51:48 +02001080 dt->size = svm->vmcb->save.idtr.limit;
1081 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001082}
1083
Gleb Natapov89a27f42010-02-16 10:51:48 +02001084static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001085{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001086 struct vcpu_svm *svm = to_svm(vcpu);
1087
Gleb Natapov89a27f42010-02-16 10:51:48 +02001088 svm->vmcb->save.idtr.limit = dt->size;
1089 svm->vmcb->save.idtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001090}
1091
Gleb Natapov89a27f42010-02-16 10:51:48 +02001092static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001093{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001094 struct vcpu_svm *svm = to_svm(vcpu);
1095
Gleb Natapov89a27f42010-02-16 10:51:48 +02001096 dt->size = svm->vmcb->save.gdtr.limit;
1097 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001098}
1099
Gleb Natapov89a27f42010-02-16 10:51:48 +02001100static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001101{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001102 struct vcpu_svm *svm = to_svm(vcpu);
1103
Gleb Natapov89a27f42010-02-16 10:51:48 +02001104 svm->vmcb->save.gdtr.limit = dt->size;
1105 svm->vmcb->save.gdtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001106}
1107
Avi Kivitye8467fd2009-12-29 18:43:06 +02001108static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1109{
1110}
1111
Anthony Liguori25c4c272007-04-27 09:29:21 +03001112static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001113{
1114}
1115
Avi Kivityd2251572010-01-06 10:55:27 +02001116static void update_cr0_intercept(struct vcpu_svm *svm)
1117{
Joerg Roedel66a562f2010-02-19 16:23:08 +01001118 struct vmcb *vmcb = svm->vmcb;
Avi Kivityd2251572010-01-06 10:55:27 +02001119 ulong gcr0 = svm->vcpu.arch.cr0;
1120 u64 *hcr0 = &svm->vmcb->save.cr0;
1121
1122 if (!svm->vcpu.fpu_active)
1123 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1124 else
1125 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1126 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1127
1128
1129 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Joerg Roedel66a562f2010-02-19 16:23:08 +01001130 vmcb->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1131 vmcb->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1132 if (is_nested(svm)) {
1133 struct vmcb *hsave = svm->nested.hsave;
1134
1135 hsave->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1136 hsave->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1137 vmcb->control.intercept_cr_read |= svm->nested.intercept_cr_read;
1138 vmcb->control.intercept_cr_write |= svm->nested.intercept_cr_write;
1139 }
Avi Kivityd2251572010-01-06 10:55:27 +02001140 } else {
1141 svm->vmcb->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1142 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001143 if (is_nested(svm)) {
1144 struct vmcb *hsave = svm->nested.hsave;
1145
1146 hsave->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1147 hsave->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
1148 }
Avi Kivityd2251572010-01-06 10:55:27 +02001149 }
1150}
1151
Avi Kivity6aa8b732006-12-10 02:21:36 -08001152static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1153{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001154 struct vcpu_svm *svm = to_svm(vcpu);
1155
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001156 if (is_nested(svm)) {
1157 /*
1158 * We are here because we run in nested mode, the host kvm
1159 * intercepts cr0 writes but the l1 hypervisor does not.
1160 * But the L1 hypervisor may intercept selective cr0 writes.
1161 * This needs to be checked here.
1162 */
1163 unsigned long old, new;
1164
1165 /* Remove bits that would trigger a real cr0 write intercept */
1166 old = vcpu->arch.cr0 & SVM_CR0_SELECTIVE_MASK;
1167 new = cr0 & SVM_CR0_SELECTIVE_MASK;
1168
1169 if (old == new) {
1170 /* cr0 write with ts and mp unchanged */
1171 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
1172 if (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE)
1173 return;
1174 }
1175 }
1176
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001177#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001178 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92f2007-07-17 23:19:08 +10001179 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001180 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001181 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001182 }
1183
Mike Dayd77c26f2007-10-08 09:02:08 -04001184 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001185 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001186 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001187 }
1188 }
1189#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001190 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001191
1192 if (!npt_enabled)
1193 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001194
1195 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001196 cr0 |= X86_CR0_TS;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001197 /*
1198 * re-enable caching here because the QEMU bios
1199 * does not do it - this results in some delay at
1200 * reboot
1201 */
1202 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001203 svm->vmcb->save.cr0 = cr0;
Avi Kivityd2251572010-01-06 10:55:27 +02001204 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001205}
1206
1207static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1208{
Joerg Roedel6394b642008-04-09 14:15:29 +02001209 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001210 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1211
1212 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
1213 force_new_asid(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001214
Joerg Roedelec077262008-04-09 14:15:28 +02001215 vcpu->arch.cr4 = cr4;
1216 if (!npt_enabled)
1217 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001218 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001219 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001220}
1221
1222static void svm_set_segment(struct kvm_vcpu *vcpu,
1223 struct kvm_segment *var, int seg)
1224{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001225 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001226 struct vmcb_seg *s = svm_seg(vcpu, seg);
1227
1228 s->base = var->base;
1229 s->limit = var->limit;
1230 s->selector = var->selector;
1231 if (var->unusable)
1232 s->attrib = 0;
1233 else {
1234 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1235 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1236 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1237 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1238 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1239 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1240 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1241 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1242 }
1243 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001244 svm->vmcb->save.cpl
1245 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -08001246 >> SVM_SELECTOR_DPL_SHIFT) & 3;
1247
1248}
1249
Gleb Natapov44c11432009-05-11 13:35:52 +03001250static void update_db_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001251{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001252 struct vcpu_svm *svm = to_svm(vcpu);
1253
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001254 svm->vmcb->control.intercept_exceptions &=
1255 ~((1 << DB_VECTOR) | (1 << BP_VECTOR));
Gleb Natapov44c11432009-05-11 13:35:52 +03001256
Jan Kiszka6be7d302009-10-18 13:24:54 +02001257 if (svm->nmi_singlestep)
Gleb Natapov44c11432009-05-11 13:35:52 +03001258 svm->vmcb->control.intercept_exceptions |= (1 << DB_VECTOR);
1259
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001260 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1261 if (vcpu->guest_debug &
1262 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
1263 svm->vmcb->control.intercept_exceptions |=
1264 1 << DB_VECTOR;
1265 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
1266 svm->vmcb->control.intercept_exceptions |=
1267 1 << BP_VECTOR;
1268 } else
1269 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001270}
1271
Jan Kiszka355be0b2009-10-03 00:31:21 +02001272static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Gleb Natapov44c11432009-05-11 13:35:52 +03001273{
Gleb Natapov44c11432009-05-11 13:35:52 +03001274 struct vcpu_svm *svm = to_svm(vcpu);
1275
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001276 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1277 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1278 else
1279 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1280
Jan Kiszka355be0b2009-10-03 00:31:21 +02001281 update_db_intercept(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001282}
1283
1284static void load_host_msrs(struct kvm_vcpu *vcpu)
1285{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001286#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001287 wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001288#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001289}
1290
1291static void save_host_msrs(struct kvm_vcpu *vcpu)
1292{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001293#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001294 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001295#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001296}
1297
Tejun Heo0fe1e002009-10-29 22:34:14 +09001298static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001299{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001300 if (sd->next_asid > sd->max_asid) {
1301 ++sd->asid_generation;
1302 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001303 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001304 }
1305
Tejun Heo0fe1e002009-10-29 22:34:14 +09001306 svm->asid_generation = sd->asid_generation;
1307 svm->vmcb->control.asid = sd->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001308}
1309
Jan Kiszkac76de352010-01-20 18:20:20 +01001310static int svm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *dest)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001311{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001312 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001313
1314 switch (dr) {
1315 case 0 ... 3:
Jan Kiszkac76de352010-01-20 18:20:20 +01001316 *dest = vcpu->arch.db[dr];
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001317 break;
Jan Kiszkac76de352010-01-20 18:20:20 +01001318 case 4:
1319 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
1320 return EMULATE_FAIL; /* will re-inject UD */
1321 /* fall through */
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001322 case 6:
1323 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
Jan Kiszkac76de352010-01-20 18:20:20 +01001324 *dest = vcpu->arch.dr6;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001325 else
Jan Kiszkac76de352010-01-20 18:20:20 +01001326 *dest = svm->vmcb->save.dr6;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001327 break;
Jan Kiszkac76de352010-01-20 18:20:20 +01001328 case 5:
1329 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
1330 return EMULATE_FAIL; /* will re-inject UD */
1331 /* fall through */
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001332 case 7:
1333 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
Jan Kiszkac76de352010-01-20 18:20:20 +01001334 *dest = vcpu->arch.dr7;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001335 else
Jan Kiszkac76de352010-01-20 18:20:20 +01001336 *dest = svm->vmcb->save.dr7;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001337 break;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001338 }
1339
Jan Kiszkac76de352010-01-20 18:20:20 +01001340 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001341}
1342
Jan Kiszkac76de352010-01-20 18:20:20 +01001343static int svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001344{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001345 struct vcpu_svm *svm = to_svm(vcpu);
1346
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347 switch (dr) {
1348 case 0 ... 3:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001349 vcpu->arch.db[dr] = value;
1350 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1351 vcpu->arch.eff_db[dr] = value;
Jan Kiszkac76de352010-01-20 18:20:20 +01001352 break;
1353 case 4:
1354 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
1355 return EMULATE_FAIL; /* will re-inject UD */
1356 /* fall through */
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001357 case 6:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001358 vcpu->arch.dr6 = (value & DR6_VOLATILE) | DR6_FIXED_1;
Jan Kiszkac76de352010-01-20 18:20:20 +01001359 break;
1360 case 5:
1361 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
1362 return EMULATE_FAIL; /* will re-inject UD */
1363 /* fall through */
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001364 case 7:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001365 vcpu->arch.dr7 = (value & DR7_VOLATILE) | DR7_FIXED_1;
1366 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
1367 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1368 vcpu->arch.switch_db_regs = (value & DR7_BP_EN_MASK);
1369 }
Jan Kiszkac76de352010-01-20 18:20:20 +01001370 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001371 }
Jan Kiszkac76de352010-01-20 18:20:20 +01001372
1373 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001374}
1375
Avi Kivity851ba692009-08-24 11:10:17 +03001376static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001377{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001378 u64 fault_address;
1379 u32 error_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001380
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001381 fault_address = svm->vmcb->control.exit_info_2;
1382 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001383
Marcelo Tosatti229456f2009-06-17 09:22:14 -03001384 trace_kvm_page_fault(fault_address, error_code);
Avi Kivity52c78472009-08-10 15:42:41 +03001385 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1386 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
Avi Kivity30677142007-10-28 18:48:59 +02001387 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001388}
1389
Avi Kivity851ba692009-08-24 11:10:17 +03001390static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001391{
Avi Kivity851ba692009-08-24 11:10:17 +03001392 struct kvm_run *kvm_run = svm->vcpu.run;
1393
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001394 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001395 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001396 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001397 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1398 return 1;
1399 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001400
Jan Kiszka6be7d302009-10-18 13:24:54 +02001401 if (svm->nmi_singlestep) {
1402 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001403 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1404 svm->vmcb->save.rflags &=
1405 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1406 update_db_intercept(&svm->vcpu);
1407 }
1408
1409 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001410 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001411 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1412 kvm_run->debug.arch.pc =
1413 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1414 kvm_run->debug.arch.exception = DB_VECTOR;
1415 return 0;
1416 }
1417
1418 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001419}
1420
Avi Kivity851ba692009-08-24 11:10:17 +03001421static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001422{
Avi Kivity851ba692009-08-24 11:10:17 +03001423 struct kvm_run *kvm_run = svm->vcpu.run;
1424
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001425 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1426 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1427 kvm_run->debug.arch.exception = BP_VECTOR;
1428 return 0;
1429}
1430
Avi Kivity851ba692009-08-24 11:10:17 +03001431static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001432{
1433 int er;
1434
Avi Kivity851ba692009-08-24 11:10:17 +03001435 er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001436 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001437 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001438 return 1;
1439}
1440
Avi Kivity6b52d182010-01-21 15:31:47 +02001441static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001442{
Avi Kivity6b52d182010-01-21 15:31:47 +02001443 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001444 u32 excp;
1445
1446 if (is_nested(svm)) {
1447 u32 h_excp, n_excp;
1448
1449 h_excp = svm->nested.hsave->control.intercept_exceptions;
1450 n_excp = svm->nested.intercept_exceptions;
1451 h_excp &= ~(1 << NM_VECTOR);
1452 excp = h_excp | n_excp;
1453 } else {
1454 excp = svm->vmcb->control.intercept_exceptions;
Joerg Roedele0231712010-02-24 18:59:10 +01001455 excp &= ~(1 << NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001456 }
1457
1458 svm->vmcb->control.intercept_exceptions = excp;
1459
Rusty Russelle756fc62007-07-30 20:07:08 +10001460 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001461 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001462}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001463
Avi Kivity6b52d182010-01-21 15:31:47 +02001464static int nm_interception(struct vcpu_svm *svm)
1465{
1466 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001467 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001468}
1469
Avi Kivity851ba692009-08-24 11:10:17 +03001470static int mc_interception(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001471{
1472 /*
1473 * On an #MC intercept the MCE handler is not called automatically in
1474 * the host. So do it by hand here.
1475 */
1476 asm volatile (
1477 "int $0x12\n");
1478 /* not sure if we ever come back to this point */
1479
1480 return 1;
1481}
1482
Avi Kivity851ba692009-08-24 11:10:17 +03001483static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001484{
Avi Kivity851ba692009-08-24 11:10:17 +03001485 struct kvm_run *kvm_run = svm->vcpu.run;
1486
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001487 /*
1488 * VMCB is undefined after a SHUTDOWN intercept
1489 * so reinitialize it.
1490 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001491 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001492 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001493
1494 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1495 return 0;
1496}
1497
Avi Kivity851ba692009-08-24 11:10:17 +03001498static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001499{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001500 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04001501 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001502 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001503 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001504
Rusty Russelle756fc62007-07-30 20:07:08 +10001505 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03001506 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001507 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001508 if (string || in)
1509 return !(emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DO_MMIO);
1510
Avi Kivity039576c2007-03-20 12:46:50 +02001511 port = io_info >> 16;
1512 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001513 svm->next_rip = svm->vmcb->control.exit_info_2;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001514 skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001515
1516 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001517}
1518
Avi Kivity851ba692009-08-24 11:10:17 +03001519static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001520{
1521 return 1;
1522}
1523
Avi Kivity851ba692009-08-24 11:10:17 +03001524static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001525{
1526 ++svm->vcpu.stat.irq_exits;
1527 return 1;
1528}
1529
Avi Kivity851ba692009-08-24 11:10:17 +03001530static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001531{
1532 return 1;
1533}
1534
Avi Kivity851ba692009-08-24 11:10:17 +03001535static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001536{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001537 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001538 skip_emulated_instruction(&svm->vcpu);
1539 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001540}
1541
Avi Kivity851ba692009-08-24 11:10:17 +03001542static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001543{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001544 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001545 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001546 kvm_emulate_hypercall(&svm->vcpu);
1547 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001548}
1549
Alexander Grafc0725422008-11-25 20:17:03 +01001550static int nested_svm_check_permissions(struct vcpu_svm *svm)
1551{
Avi Kivityf6801df2010-01-21 15:31:50 +02001552 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01001553 || !is_paging(&svm->vcpu)) {
1554 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1555 return 1;
1556 }
1557
1558 if (svm->vmcb->save.cpl) {
1559 kvm_inject_gp(&svm->vcpu, 0);
1560 return 1;
1561 }
1562
1563 return 0;
1564}
1565
Alexander Grafcf74a782008-11-25 20:17:08 +01001566static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
1567 bool has_error_code, u32 error_code)
1568{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001569 int vmexit;
1570
Joerg Roedel0295ad72009-08-07 11:49:37 +02001571 if (!is_nested(svm))
1572 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001573
Joerg Roedel0295ad72009-08-07 11:49:37 +02001574 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1575 svm->vmcb->control.exit_code_hi = 0;
1576 svm->vmcb->control.exit_info_1 = error_code;
1577 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1578
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001579 vmexit = nested_svm_intercept(svm);
1580 if (vmexit == NESTED_EXIT_DONE)
1581 svm->nested.exit_required = true;
1582
1583 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001584}
1585
Joerg Roedel8fe54652010-02-19 16:23:01 +01001586/* This function returns true if it is save to enable the irq window */
1587static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001588{
Joerg Roedel26666952009-08-07 11:49:46 +02001589 if (!is_nested(svm))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001590 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001591
Joerg Roedel26666952009-08-07 11:49:46 +02001592 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001593 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001594
Joerg Roedel26666952009-08-07 11:49:46 +02001595 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001596 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001597
Joerg Roedel197717d2010-02-24 18:59:19 +01001598 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
1599 svm->vmcb->control.exit_info_1 = 0;
1600 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02001601
Joerg Roedelcd3ff652009-10-09 16:08:26 +02001602 if (svm->nested.intercept & 1ULL) {
1603 /*
1604 * The #vmexit can't be emulated here directly because this
1605 * code path runs with irqs and preemtion disabled. A
1606 * #vmexit emulation might sleep. Only signal request for
1607 * the #vmexit here.
1608 */
1609 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02001610 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01001611 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001612 }
1613
Joerg Roedel8fe54652010-02-19 16:23:01 +01001614 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001615}
1616
Joerg Roedel887f5002010-02-24 18:59:12 +01001617/* This function returns true if it is save to enable the nmi window */
1618static inline bool nested_svm_nmi(struct vcpu_svm *svm)
1619{
1620 if (!is_nested(svm))
1621 return true;
1622
1623 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
1624 return true;
1625
1626 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
1627 svm->nested.exit_required = true;
1628
1629 return false;
1630}
1631
Joerg Roedel7597f122010-02-19 16:23:00 +01001632static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001633{
1634 struct page *page;
1635
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01001636 might_sleep();
1637
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001638 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001639 if (is_error_page(page))
1640 goto error;
1641
Joerg Roedel7597f122010-02-19 16:23:00 +01001642 *_page = page;
1643
1644 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001645
1646error:
1647 kvm_release_page_clean(page);
1648 kvm_inject_gp(&svm->vcpu, 0);
1649
1650 return NULL;
1651}
1652
Joerg Roedel7597f122010-02-19 16:23:00 +01001653static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001654{
Joerg Roedel7597f122010-02-19 16:23:00 +01001655 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001656 kvm_release_page_dirty(page);
1657}
1658
Joerg Roedelce2ac082010-03-01 15:34:39 +01001659static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
1660{
1661 unsigned port;
1662 u8 val, bit;
1663 u64 gpa;
1664
1665 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
1666 return NESTED_EXIT_HOST;
1667
1668 port = svm->vmcb->control.exit_info_1 >> 16;
1669 gpa = svm->nested.vmcb_iopm + (port / 8);
1670 bit = port % 8;
1671 val = 0;
1672
1673 if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1))
1674 val &= (1 << bit);
1675
1676 return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1677}
1678
Joerg Roedeld2477822010-03-01 15:34:34 +01001679static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001680{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001681 u32 offset, msr, value;
1682 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001683
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001684 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01001685 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001686
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001687 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1688 offset = svm_msrpm_offset(msr);
1689 write = svm->vmcb->control.exit_info_1 & 1;
1690 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001691
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001692 if (offset == MSR_INVALID)
1693 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001694
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001695 /* Offset is in 32 bit units but need in 8 bit units */
1696 offset *= 4;
1697
1698 if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4))
1699 return NESTED_EXIT_DONE;
1700
1701 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001702}
1703
Joerg Roedel410e4d52009-08-07 11:49:44 +02001704static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001705{
Alexander Grafcf74a782008-11-25 20:17:08 +01001706 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001707
Joerg Roedel410e4d52009-08-07 11:49:44 +02001708 switch (exit_code) {
1709 case SVM_EXIT_INTR:
1710 case SVM_EXIT_NMI:
1711 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001712 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01001713 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001714 if (npt_enabled)
1715 return NESTED_EXIT_HOST;
1716 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001717 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Joerg Roedele0231712010-02-24 18:59:10 +01001718 /* When we're shadowing, trap PFs */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001719 if (!npt_enabled)
1720 return NESTED_EXIT_HOST;
1721 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001722 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
1723 nm_interception(svm);
1724 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001725 default:
1726 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001727 }
1728
Joerg Roedel410e4d52009-08-07 11:49:44 +02001729 return NESTED_EXIT_CONTINUE;
1730}
1731
1732/*
1733 * If this function returns true, this #vmexit was already handled
1734 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001735static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001736{
1737 u32 exit_code = svm->vmcb->control.exit_code;
1738 int vmexit = NESTED_EXIT_HOST;
1739
Alexander Grafcf74a782008-11-25 20:17:08 +01001740 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001741 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001742 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001743 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01001744 case SVM_EXIT_IOIO:
1745 vmexit = nested_svm_intercept_ioio(svm);
1746 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001747 case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR8: {
1748 u32 cr_bits = 1 << (exit_code - SVM_EXIT_READ_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001749 if (svm->nested.intercept_cr_read & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001750 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001751 break;
1752 }
1753 case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR8: {
1754 u32 cr_bits = 1 << (exit_code - SVM_EXIT_WRITE_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001755 if (svm->nested.intercept_cr_write & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001756 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001757 break;
1758 }
1759 case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR7: {
1760 u32 dr_bits = 1 << (exit_code - SVM_EXIT_READ_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001761 if (svm->nested.intercept_dr_read & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001762 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001763 break;
1764 }
1765 case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR7: {
1766 u32 dr_bits = 1 << (exit_code - SVM_EXIT_WRITE_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001767 if (svm->nested.intercept_dr_write & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001768 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001769 break;
1770 }
1771 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1772 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001773 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001774 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001775 break;
1776 }
1777 default: {
1778 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001779 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001780 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001781 }
1782 }
1783
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001784 return vmexit;
1785}
1786
1787static int nested_svm_exit_handled(struct vcpu_svm *svm)
1788{
1789 int vmexit;
1790
1791 vmexit = nested_svm_intercept(svm);
1792
1793 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001794 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001795
1796 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001797}
1798
Joerg Roedel0460a972009-08-07 11:49:31 +02001799static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
1800{
1801 struct vmcb_control_area *dst = &dst_vmcb->control;
1802 struct vmcb_control_area *from = &from_vmcb->control;
1803
1804 dst->intercept_cr_read = from->intercept_cr_read;
1805 dst->intercept_cr_write = from->intercept_cr_write;
1806 dst->intercept_dr_read = from->intercept_dr_read;
1807 dst->intercept_dr_write = from->intercept_dr_write;
1808 dst->intercept_exceptions = from->intercept_exceptions;
1809 dst->intercept = from->intercept;
1810 dst->iopm_base_pa = from->iopm_base_pa;
1811 dst->msrpm_base_pa = from->msrpm_base_pa;
1812 dst->tsc_offset = from->tsc_offset;
1813 dst->asid = from->asid;
1814 dst->tlb_ctl = from->tlb_ctl;
1815 dst->int_ctl = from->int_ctl;
1816 dst->int_vector = from->int_vector;
1817 dst->int_state = from->int_state;
1818 dst->exit_code = from->exit_code;
1819 dst->exit_code_hi = from->exit_code_hi;
1820 dst->exit_info_1 = from->exit_info_1;
1821 dst->exit_info_2 = from->exit_info_2;
1822 dst->exit_int_info = from->exit_int_info;
1823 dst->exit_int_info_err = from->exit_int_info_err;
1824 dst->nested_ctl = from->nested_ctl;
1825 dst->event_inj = from->event_inj;
1826 dst->event_inj_err = from->event_inj_err;
1827 dst->nested_cr3 = from->nested_cr3;
1828 dst->lbr_ctl = from->lbr_ctl;
1829}
1830
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001831static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001832{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001833 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001834 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02001835 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01001836 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01001837
Joerg Roedel17897f32009-10-09 16:08:29 +02001838 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
1839 vmcb->control.exit_info_1,
1840 vmcb->control.exit_info_2,
1841 vmcb->control.exit_int_info,
1842 vmcb->control.exit_int_info_err);
1843
Joerg Roedel7597f122010-02-19 16:23:00 +01001844 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001845 if (!nested_vmcb)
1846 return 1;
1847
Joerg Roedel06fc77722010-02-19 16:23:07 +01001848 /* Exit nested SVM mode */
1849 svm->nested.vmcb = 0;
1850
Alexander Grafcf74a782008-11-25 20:17:08 +01001851 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02001852 disable_gif(svm);
1853
1854 nested_vmcb->save.es = vmcb->save.es;
1855 nested_vmcb->save.cs = vmcb->save.cs;
1856 nested_vmcb->save.ss = vmcb->save.ss;
1857 nested_vmcb->save.ds = vmcb->save.ds;
1858 nested_vmcb->save.gdtr = vmcb->save.gdtr;
1859 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001860 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02001861 if (npt_enabled)
1862 nested_vmcb->save.cr3 = vmcb->save.cr3;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001863 else
1864 nested_vmcb->save.cr3 = svm->vcpu.arch.cr3;
Joerg Roedel33740e42009-08-07 11:49:29 +02001865 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001866 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Joerg Roedel33740e42009-08-07 11:49:29 +02001867 nested_vmcb->save.rflags = vmcb->save.rflags;
1868 nested_vmcb->save.rip = vmcb->save.rip;
1869 nested_vmcb->save.rsp = vmcb->save.rsp;
1870 nested_vmcb->save.rax = vmcb->save.rax;
1871 nested_vmcb->save.dr7 = vmcb->save.dr7;
1872 nested_vmcb->save.dr6 = vmcb->save.dr6;
1873 nested_vmcb->save.cpl = vmcb->save.cpl;
1874
1875 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
1876 nested_vmcb->control.int_vector = vmcb->control.int_vector;
1877 nested_vmcb->control.int_state = vmcb->control.int_state;
1878 nested_vmcb->control.exit_code = vmcb->control.exit_code;
1879 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
1880 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
1881 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
1882 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
1883 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Alexander Graf8d23c462009-10-09 16:08:25 +02001884
1885 /*
1886 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
1887 * to make sure that we do not lose injected events. So check event_inj
1888 * here and copy it to exit_int_info if it is valid.
1889 * Exit_int_info and event_inj can't be both valid because the case
1890 * below only happens on a VMRUN instruction intercept which has
1891 * no valid exit_int_info set.
1892 */
1893 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
1894 struct vmcb_control_area *nc = &nested_vmcb->control;
1895
1896 nc->exit_int_info = vmcb->control.event_inj;
1897 nc->exit_int_info_err = vmcb->control.event_inj_err;
1898 }
1899
Joerg Roedel33740e42009-08-07 11:49:29 +02001900 nested_vmcb->control.tlb_ctl = 0;
1901 nested_vmcb->control.event_inj = 0;
1902 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001903
1904 /* We always set V_INTR_MASKING and remember the old value in hflags */
1905 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
1906 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
1907
Alexander Grafcf74a782008-11-25 20:17:08 +01001908 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02001909 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01001910
Alexander Graf219b65d2009-06-15 15:21:25 +02001911 kvm_clear_exception_queue(&svm->vcpu);
1912 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01001913
1914 /* Restore selected save entries */
1915 svm->vmcb->save.es = hsave->save.es;
1916 svm->vmcb->save.cs = hsave->save.cs;
1917 svm->vmcb->save.ss = hsave->save.ss;
1918 svm->vmcb->save.ds = hsave->save.ds;
1919 svm->vmcb->save.gdtr = hsave->save.gdtr;
1920 svm->vmcb->save.idtr = hsave->save.idtr;
1921 svm->vmcb->save.rflags = hsave->save.rflags;
1922 svm_set_efer(&svm->vcpu, hsave->save.efer);
1923 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
1924 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
1925 if (npt_enabled) {
1926 svm->vmcb->save.cr3 = hsave->save.cr3;
1927 svm->vcpu.arch.cr3 = hsave->save.cr3;
1928 } else {
1929 kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
1930 }
1931 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
1932 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
1933 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
1934 svm->vmcb->save.dr7 = 0;
1935 svm->vmcb->save.cpl = 0;
1936 svm->vmcb->control.exit_int_info = 0;
1937
Joerg Roedel7597f122010-02-19 16:23:00 +01001938 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01001939
1940 kvm_mmu_reset_context(&svm->vcpu);
1941 kvm_mmu_load(&svm->vcpu);
1942
1943 return 0;
1944}
Alexander Graf3d6368e2008-11-25 20:17:07 +01001945
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001946static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01001947{
Joerg Roedel323c3d82010-03-01 15:34:37 +01001948 /*
1949 * This function merges the msr permission bitmaps of kvm and the
1950 * nested vmcb. It is omptimized in that it only merges the parts where
1951 * the kvm msr permission bitmap may contain zero bits
1952 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01001953 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001954
Joerg Roedel323c3d82010-03-01 15:34:37 +01001955 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
1956 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001957
Joerg Roedel323c3d82010-03-01 15:34:37 +01001958 for (i = 0; i < MSRPM_OFFSETS; i++) {
1959 u32 value, p;
1960 u64 offset;
1961
1962 if (msrpm_offsets[i] == 0xffffffff)
1963 break;
1964
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001965 p = msrpm_offsets[i];
1966 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01001967
1968 if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4))
1969 return false;
1970
1971 svm->nested.msrpm[p] = svm->msrpm[p] | value;
1972 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001973
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001974 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001975
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001976 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001977}
1978
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001979static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01001980{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001981 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001982 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02001983 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01001984 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01001985 u64 vmcb_gpa;
1986
1987 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001988
Joerg Roedel7597f122010-02-19 16:23:00 +01001989 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001990 if (!nested_vmcb)
1991 return false;
1992
Joerg Roedelecf14052010-02-24 18:59:13 +01001993 trace_kvm_nested_vmrun(svm->vmcb->save.rip - 3, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02001994 nested_vmcb->save.rip,
1995 nested_vmcb->control.int_ctl,
1996 nested_vmcb->control.event_inj,
1997 nested_vmcb->control.nested_ctl);
1998
Joerg Roedel2e554e82010-02-24 18:59:14 +01001999 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr_read,
2000 nested_vmcb->control.intercept_cr_write,
2001 nested_vmcb->control.intercept_exceptions,
2002 nested_vmcb->control.intercept);
2003
Alexander Graf3d6368e2008-11-25 20:17:07 +01002004 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02002005 kvm_clear_exception_queue(&svm->vcpu);
2006 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002007
Joerg Roedele0231712010-02-24 18:59:10 +01002008 /*
2009 * Save the old vmcb, so we don't need to pick what we save, but can
2010 * restore everything when a VMEXIT occurs
2011 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02002012 hsave->save.es = vmcb->save.es;
2013 hsave->save.cs = vmcb->save.cs;
2014 hsave->save.ss = vmcb->save.ss;
2015 hsave->save.ds = vmcb->save.ds;
2016 hsave->save.gdtr = vmcb->save.gdtr;
2017 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02002018 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002019 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002020 hsave->save.cr4 = svm->vcpu.arch.cr4;
2021 hsave->save.rflags = vmcb->save.rflags;
2022 hsave->save.rip = svm->next_rip;
2023 hsave->save.rsp = vmcb->save.rsp;
2024 hsave->save.rax = vmcb->save.rax;
2025 if (npt_enabled)
2026 hsave->save.cr3 = vmcb->save.cr3;
2027 else
2028 hsave->save.cr3 = svm->vcpu.arch.cr3;
2029
Joerg Roedel0460a972009-08-07 11:49:31 +02002030 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002031
2032 if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
2033 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2034 else
2035 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2036
2037 /* Load the nested guest state */
2038 svm->vmcb->save.es = nested_vmcb->save.es;
2039 svm->vmcb->save.cs = nested_vmcb->save.cs;
2040 svm->vmcb->save.ss = nested_vmcb->save.ss;
2041 svm->vmcb->save.ds = nested_vmcb->save.ds;
2042 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2043 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
2044 svm->vmcb->save.rflags = nested_vmcb->save.rflags;
2045 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2046 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2047 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2048 if (npt_enabled) {
2049 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2050 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002051 } else
Alexander Graf3d6368e2008-11-25 20:17:07 +01002052 kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002053
2054 /* Guest paging mode is active - reset mmu */
2055 kvm_mmu_reset_context(&svm->vcpu);
2056
Joerg Roedeldefbba52009-08-07 11:49:30 +02002057 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002058 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2059 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2060 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01002061
Alexander Graf3d6368e2008-11-25 20:17:07 +01002062 /* In case we don't even reach vcpu_run, the fields are not updated */
2063 svm->vmcb->save.rax = nested_vmcb->save.rax;
2064 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2065 svm->vmcb->save.rip = nested_vmcb->save.rip;
2066 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2067 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2068 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2069
Joerg Roedelf7138532010-03-01 15:34:40 +01002070 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002071 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002072
Joerg Roedelaad42c62009-08-07 11:49:34 +02002073 /* cache intercepts */
2074 svm->nested.intercept_cr_read = nested_vmcb->control.intercept_cr_read;
2075 svm->nested.intercept_cr_write = nested_vmcb->control.intercept_cr_write;
2076 svm->nested.intercept_dr_read = nested_vmcb->control.intercept_dr_read;
2077 svm->nested.intercept_dr_write = nested_vmcb->control.intercept_dr_write;
2078 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2079 svm->nested.intercept = nested_vmcb->control.intercept;
2080
Alexander Graf3d6368e2008-11-25 20:17:07 +01002081 force_new_asid(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002082 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002083 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2084 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2085 else
2086 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2087
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002088 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2089 /* We only want the cr8 intercept bits of the guest */
2090 svm->vmcb->control.intercept_cr_read &= ~INTERCEPT_CR8_MASK;
2091 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
2092 }
2093
Joerg Roedele0231712010-02-24 18:59:10 +01002094 /*
2095 * We don't want a nested guest to be more powerful than the guest, so
2096 * all intercepts are ORed
2097 */
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002098 svm->vmcb->control.intercept_cr_read |=
2099 nested_vmcb->control.intercept_cr_read;
2100 svm->vmcb->control.intercept_cr_write |=
2101 nested_vmcb->control.intercept_cr_write;
2102 svm->vmcb->control.intercept_dr_read |=
2103 nested_vmcb->control.intercept_dr_read;
2104 svm->vmcb->control.intercept_dr_write |=
2105 nested_vmcb->control.intercept_dr_write;
2106 svm->vmcb->control.intercept_exceptions |=
2107 nested_vmcb->control.intercept_exceptions;
2108
2109 svm->vmcb->control.intercept |= nested_vmcb->control.intercept;
2110
2111 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002112 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2113 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2114 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002115 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2116 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2117
Joerg Roedel7597f122010-02-19 16:23:00 +01002118 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002119
Joerg Roedel06fc77722010-02-19 16:23:07 +01002120 /* nested_vmcb is our indicator if nested SVM is activated */
2121 svm->nested.vmcb = vmcb_gpa;
2122
Joerg Roedel2af91942009-08-07 11:49:28 +02002123 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002124
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002125 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002126}
2127
Joerg Roedel9966bf62009-08-07 11:49:40 +02002128static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002129{
2130 to_vmcb->save.fs = from_vmcb->save.fs;
2131 to_vmcb->save.gs = from_vmcb->save.gs;
2132 to_vmcb->save.tr = from_vmcb->save.tr;
2133 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2134 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2135 to_vmcb->save.star = from_vmcb->save.star;
2136 to_vmcb->save.lstar = from_vmcb->save.lstar;
2137 to_vmcb->save.cstar = from_vmcb->save.cstar;
2138 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2139 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2140 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2141 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002142}
2143
Avi Kivity851ba692009-08-24 11:10:17 +03002144static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002145{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002146 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002147 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002148
Alexander Graf55426752008-11-25 20:17:06 +01002149 if (nested_svm_check_permissions(svm))
2150 return 1;
2151
2152 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2153 skip_emulated_instruction(&svm->vcpu);
2154
Joerg Roedel7597f122010-02-19 16:23:00 +01002155 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002156 if (!nested_vmcb)
2157 return 1;
2158
2159 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002160 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002161
2162 return 1;
2163}
2164
Avi Kivity851ba692009-08-24 11:10:17 +03002165static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002166{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002167 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002168 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002169
Alexander Graf55426752008-11-25 20:17:06 +01002170 if (nested_svm_check_permissions(svm))
2171 return 1;
2172
2173 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2174 skip_emulated_instruction(&svm->vcpu);
2175
Joerg Roedel7597f122010-02-19 16:23:00 +01002176 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002177 if (!nested_vmcb)
2178 return 1;
2179
2180 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002181 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002182
2183 return 1;
2184}
2185
Avi Kivity851ba692009-08-24 11:10:17 +03002186static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002187{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002188 if (nested_svm_check_permissions(svm))
2189 return 1;
2190
2191 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2192 skip_emulated_instruction(&svm->vcpu);
2193
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002194 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002195 return 1;
2196
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002197 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002198 goto failed;
2199
2200 return 1;
2201
2202failed:
2203
2204 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2205 svm->vmcb->control.exit_code_hi = 0;
2206 svm->vmcb->control.exit_info_1 = 0;
2207 svm->vmcb->control.exit_info_2 = 0;
2208
2209 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002210
2211 return 1;
2212}
2213
Avi Kivity851ba692009-08-24 11:10:17 +03002214static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002215{
2216 if (nested_svm_check_permissions(svm))
2217 return 1;
2218
2219 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2220 skip_emulated_instruction(&svm->vcpu);
2221
Joerg Roedel2af91942009-08-07 11:49:28 +02002222 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002223
2224 return 1;
2225}
2226
Avi Kivity851ba692009-08-24 11:10:17 +03002227static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002228{
2229 if (nested_svm_check_permissions(svm))
2230 return 1;
2231
2232 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2233 skip_emulated_instruction(&svm->vcpu);
2234
Joerg Roedel2af91942009-08-07 11:49:28 +02002235 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002236
2237 /* After a CLGI no interrupts should come */
2238 svm_clear_vintr(svm);
2239 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2240
2241 return 1;
2242}
2243
Avi Kivity851ba692009-08-24 11:10:17 +03002244static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002245{
2246 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002247
Joerg Roedelec1ff792009-10-09 16:08:31 +02002248 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
2249 vcpu->arch.regs[VCPU_REGS_RAX]);
2250
Alexander Grafff092382009-06-15 15:21:24 +02002251 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2252 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
2253
2254 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2255 skip_emulated_instruction(&svm->vcpu);
2256 return 1;
2257}
2258
Joerg Roedel532a46b2009-10-09 16:08:32 +02002259static int skinit_interception(struct vcpu_svm *svm)
2260{
2261 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
2262
2263 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2264 return 1;
2265}
2266
Avi Kivity851ba692009-08-24 11:10:17 +03002267static int invalid_op_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002268{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002269 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002270 return 1;
2271}
2272
Avi Kivity851ba692009-08-24 11:10:17 +03002273static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002274{
Izik Eidus37817f22008-03-24 23:14:53 +02002275 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002276 int reason;
2277 int int_type = svm->vmcb->control.exit_int_info &
2278 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002279 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002280 uint32_t type =
2281 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2282 uint32_t idt_v =
2283 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Izik Eidus37817f22008-03-24 23:14:53 +02002284
2285 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002286
Izik Eidus37817f22008-03-24 23:14:53 +02002287 if (svm->vmcb->control.exit_info_2 &
2288 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002289 reason = TASK_SWITCH_IRET;
2290 else if (svm->vmcb->control.exit_info_2 &
2291 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2292 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002293 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002294 reason = TASK_SWITCH_GATE;
2295 else
2296 reason = TASK_SWITCH_CALL;
2297
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002298 if (reason == TASK_SWITCH_GATE) {
2299 switch (type) {
2300 case SVM_EXITINTINFO_TYPE_NMI:
2301 svm->vcpu.arch.nmi_injected = false;
2302 break;
2303 case SVM_EXITINTINFO_TYPE_EXEPT:
2304 kvm_clear_exception_queue(&svm->vcpu);
2305 break;
2306 case SVM_EXITINTINFO_TYPE_INTR:
2307 kvm_clear_interrupt_queue(&svm->vcpu);
2308 break;
2309 default:
2310 break;
2311 }
2312 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002313
Gleb Natapov8317c292009-04-12 13:37:02 +03002314 if (reason != TASK_SWITCH_GATE ||
2315 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2316 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002317 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2318 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002319
2320 return kvm_task_switch(&svm->vcpu, tss_selector, reason);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321}
2322
Avi Kivity851ba692009-08-24 11:10:17 +03002323static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002324{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002325 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002326 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002327 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002328}
2329
Avi Kivity851ba692009-08-24 11:10:17 +03002330static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002331{
2332 ++svm->vcpu.stat.nmi_window_exits;
2333 svm->vmcb->control.intercept &= ~(1UL << INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002334 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002335 return 1;
2336}
2337
Avi Kivity851ba692009-08-24 11:10:17 +03002338static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002339{
Avi Kivity851ba692009-08-24 11:10:17 +03002340 if (emulate_instruction(&svm->vcpu, 0, 0, 0) != EMULATE_DONE)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002341 pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
2342 return 1;
2343}
2344
Avi Kivity851ba692009-08-24 11:10:17 +03002345static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002346{
Avi Kivity851ba692009-08-24 11:10:17 +03002347 if (emulate_instruction(&svm->vcpu, 0, 0, 0) != EMULATE_DONE)
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002348 pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002349 return 1;
2350}
2351
Avi Kivity851ba692009-08-24 11:10:17 +03002352static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01002353{
Avi Kivity851ba692009-08-24 11:10:17 +03002354 struct kvm_run *kvm_run = svm->vcpu.run;
2355
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002356 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
2357 /* instruction emulation calls kvm_set_cr8() */
Avi Kivity851ba692009-08-24 11:10:17 +03002358 emulate_instruction(&svm->vcpu, 0, 0, 0);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002359 if (irqchip_in_kernel(svm->vcpu.kvm)) {
2360 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
Joerg Roedel1d075432007-12-06 21:02:25 +01002361 return 1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002362 }
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002363 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
2364 return 1;
Joerg Roedel1d075432007-12-06 21:02:25 +01002365 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2366 return 0;
2367}
2368
Avi Kivity6aa8b732006-12-10 02:21:36 -08002369static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
2370{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002371 struct vcpu_svm *svm = to_svm(vcpu);
2372
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302374 case MSR_IA32_TSC: {
Joerg Roedel20824f32009-09-16 15:24:18 +02002375 u64 tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002376
Joerg Roedel20824f32009-09-16 15:24:18 +02002377 if (is_nested(svm))
2378 tsc_offset = svm->nested.hsave->control.tsc_offset;
2379 else
2380 tsc_offset = svm->vmcb->control.tsc_offset;
2381
2382 *data = tsc_offset + native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002383 break;
2384 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08002385 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002386 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002387 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08002388#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002389 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002390 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002391 break;
2392 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002393 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002394 break;
2395 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002396 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002397 break;
2398 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002399 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002400 break;
2401#endif
2402 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002403 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002404 break;
2405 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002406 *data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002407 break;
2408 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002409 *data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002410 break;
Joerg Roedele0231712010-02-24 18:59:10 +01002411 /*
2412 * Nobody will change the following 5 values in the VMCB so we can
2413 * safely return them on rdmsr. They will always be 0 until LBRV is
2414 * implemented.
2415 */
Joerg Roedela2938c82008-02-13 16:30:28 +01002416 case MSR_IA32_DEBUGCTLMSR:
2417 *data = svm->vmcb->save.dbgctl;
2418 break;
2419 case MSR_IA32_LASTBRANCHFROMIP:
2420 *data = svm->vmcb->save.br_from;
2421 break;
2422 case MSR_IA32_LASTBRANCHTOIP:
2423 *data = svm->vmcb->save.br_to;
2424 break;
2425 case MSR_IA32_LASTINTFROMIP:
2426 *data = svm->vmcb->save.last_excp_from;
2427 break;
2428 case MSR_IA32_LASTINTTOIP:
2429 *data = svm->vmcb->save.last_excp_to;
2430 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002431 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002432 *data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002433 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002434 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002435 *data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002436 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01002437 case MSR_IA32_UCODE_REV:
2438 *data = 0x01000065;
2439 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002440 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002441 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002442 }
2443 return 0;
2444}
2445
Avi Kivity851ba692009-08-24 11:10:17 +03002446static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002447{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002448 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002449 u64 data;
2450
Avi Kivity59200272010-01-25 19:47:02 +02002451 if (svm_get_msr(&svm->vcpu, ecx, &data)) {
2452 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002453 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002454 } else {
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002455 trace_kvm_msr_read(ecx, data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002456
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002457 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002458 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002459 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002460 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002461 }
2462 return 1;
2463}
2464
Joerg Roedel4a810182010-02-24 18:59:15 +01002465static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2466{
2467 struct vcpu_svm *svm = to_svm(vcpu);
2468 int svm_dis, chg_mask;
2469
2470 if (data & ~SVM_VM_CR_VALID_MASK)
2471 return 1;
2472
2473 chg_mask = SVM_VM_CR_VALID_MASK;
2474
2475 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2476 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2477
2478 svm->nested.vm_cr_msr &= ~chg_mask;
2479 svm->nested.vm_cr_msr |= (data & chg_mask);
2480
2481 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2482
2483 /* check for svm_disable while efer.svme is set */
2484 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2485 return 1;
2486
2487 return 0;
2488}
2489
Avi Kivity6aa8b732006-12-10 02:21:36 -08002490static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
2491{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002492 struct vcpu_svm *svm = to_svm(vcpu);
2493
Avi Kivity6aa8b732006-12-10 02:21:36 -08002494 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302495 case MSR_IA32_TSC: {
Joerg Roedel20824f32009-09-16 15:24:18 +02002496 u64 tsc_offset = data - native_read_tsc();
2497 u64 g_tsc_offset = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002498
Joerg Roedel20824f32009-09-16 15:24:18 +02002499 if (is_nested(svm)) {
2500 g_tsc_offset = svm->vmcb->control.tsc_offset -
2501 svm->nested.hsave->control.tsc_offset;
2502 svm->nested.hsave->control.tsc_offset = tsc_offset;
2503 }
2504
2505 svm->vmcb->control.tsc_offset = tsc_offset + g_tsc_offset;
2506
Avi Kivity6aa8b732006-12-10 02:21:36 -08002507 break;
2508 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08002509 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002510 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002511 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08002512#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002513 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002514 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002515 break;
2516 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002517 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002518 break;
2519 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002520 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002521 break;
2522 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002523 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002524 break;
2525#endif
2526 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002527 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002528 break;
2529 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002530 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002531 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002532 break;
2533 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002534 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002535 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002536 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002537 case MSR_IA32_DEBUGCTLMSR:
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002538 if (!svm_has(SVM_FEATURE_LBRV)) {
2539 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002540 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002541 break;
2542 }
2543 if (data & DEBUGCTL_RESERVED_BITS)
2544 return 1;
2545
2546 svm->vmcb->save.dbgctl = data;
2547 if (data & (1ULL<<0))
2548 svm_enable_lbrv(svm);
2549 else
2550 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01002551 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002552 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002553 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002554 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002555 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002556 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002557 case MSR_VM_IGNNE:
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002558 pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
2559 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002560 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002561 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002562 }
2563 return 0;
2564}
2565
Avi Kivity851ba692009-08-24 11:10:17 +03002566static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002567{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002568 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002569 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002570 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002571
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002572
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002573 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Avi Kivity59200272010-01-25 19:47:02 +02002574 if (svm_set_msr(&svm->vcpu, ecx, data)) {
2575 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002576 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002577 } else {
2578 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10002579 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02002580 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002581 return 1;
2582}
2583
Avi Kivity851ba692009-08-24 11:10:17 +03002584static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002585{
Rusty Russelle756fc62007-07-30 20:07:08 +10002586 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03002587 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002588 else
Avi Kivity851ba692009-08-24 11:10:17 +03002589 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002590}
2591
Avi Kivity851ba692009-08-24 11:10:17 +03002592static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08002593{
Avi Kivity851ba692009-08-24 11:10:17 +03002594 struct kvm_run *kvm_run = svm->vcpu.run;
2595
Alexander Graff0b85052008-11-25 20:17:01 +01002596 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03002597 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -08002598 /*
2599 * If the user space waits to inject interrupts, exit as soon as
2600 * possible
2601 */
Gleb Natapov80618232009-04-21 17:44:56 +03002602 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
2603 kvm_run->request_interrupt_window &&
2604 !kvm_cpu_has_interrupt(&svm->vcpu)) {
Rusty Russelle756fc62007-07-30 20:07:08 +10002605 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002606 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
2607 return 0;
2608 }
2609
2610 return 1;
2611}
2612
Mark Langsdorf565d0992009-10-06 14:25:02 -05002613static int pause_interception(struct vcpu_svm *svm)
2614{
2615 kvm_vcpu_on_spin(&(svm->vcpu));
2616 return 1;
2617}
2618
Avi Kivity851ba692009-08-24 11:10:17 +03002619static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
Joerg Roedele0231712010-02-24 18:59:10 +01002620 [SVM_EXIT_READ_CR0] = emulate_on_interception,
2621 [SVM_EXIT_READ_CR3] = emulate_on_interception,
2622 [SVM_EXIT_READ_CR4] = emulate_on_interception,
2623 [SVM_EXIT_READ_CR8] = emulate_on_interception,
Avi Kivityd2251572010-01-06 10:55:27 +02002624 [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002625 [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
2626 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
2627 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
2628 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
2629 [SVM_EXIT_READ_DR0] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002630 [SVM_EXIT_READ_DR1] = emulate_on_interception,
2631 [SVM_EXIT_READ_DR2] = emulate_on_interception,
2632 [SVM_EXIT_READ_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002633 [SVM_EXIT_READ_DR4] = emulate_on_interception,
2634 [SVM_EXIT_READ_DR5] = emulate_on_interception,
2635 [SVM_EXIT_READ_DR6] = emulate_on_interception,
2636 [SVM_EXIT_READ_DR7] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002637 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
2638 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
2639 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
2640 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002641 [SVM_EXIT_WRITE_DR4] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002642 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002643 [SVM_EXIT_WRITE_DR6] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002645 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
2646 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002647 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002648 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
2649 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
2650 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
2651 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02002652 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002653 [SVM_EXIT_SMI] = nop_on_interception,
2654 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08002655 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002656 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002657 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002658 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05002659 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002660 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03002661 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02002662 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002663 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002664 [SVM_EXIT_MSR] = msr_interception,
2665 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002666 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01002667 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02002668 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01002669 [SVM_EXIT_VMLOAD] = vmload_interception,
2670 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01002671 [SVM_EXIT_STGI] = stgi_interception,
2672 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02002673 [SVM_EXIT_SKINIT] = skinit_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002674 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01002675 [SVM_EXIT_MONITOR] = invalid_op_interception,
2676 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002677 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002678};
2679
Avi Kivity851ba692009-08-24 11:10:17 +03002680static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002681{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002682 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002683 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002684 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002685
Avi Kivity5bfd8b52010-03-11 10:50:44 +02002686 trace_kvm_exit(exit_code, vcpu);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002687
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002688 if (unlikely(svm->nested.exit_required)) {
2689 nested_svm_vmexit(svm);
2690 svm->nested.exit_required = false;
2691
2692 return 1;
2693 }
2694
Alexander Grafcf74a782008-11-25 20:17:08 +01002695 if (is_nested(svm)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02002696 int vmexit;
2697
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02002698 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
2699 svm->vmcb->control.exit_info_1,
2700 svm->vmcb->control.exit_info_2,
2701 svm->vmcb->control.exit_int_info,
2702 svm->vmcb->control.exit_int_info_err);
2703
Joerg Roedel410e4d52009-08-07 11:49:44 +02002704 vmexit = nested_svm_exit_special(svm);
2705
2706 if (vmexit == NESTED_EXIT_CONTINUE)
2707 vmexit = nested_svm_exit_handled(svm);
2708
2709 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01002710 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01002711 }
2712
Joerg Roedela5c38322009-08-07 11:49:32 +02002713 svm_complete_interrupts(svm);
2714
Avi Kivity888f9f32010-01-10 12:14:04 +02002715 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR0_MASK))
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002716 vcpu->arch.cr0 = svm->vmcb->save.cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02002717 if (npt_enabled)
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002718 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002719
2720 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
2721 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2722 kvm_run->fail_entry.hardware_entry_failure_reason
2723 = svm->vmcb->control.exit_code;
2724 return 0;
2725 }
2726
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002727 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002728 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002729 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002730 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
2731 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002732 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002733 exit_code);
2734
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02002735 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08002736 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002737 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03002738 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002739 return 0;
2740 }
2741
Avi Kivity851ba692009-08-24 11:10:17 +03002742 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002743}
2744
2745static void reload_tss(struct kvm_vcpu *vcpu)
2746{
2747 int cpu = raw_smp_processor_id();
2748
Tejun Heo0fe1e002009-10-29 22:34:14 +09002749 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
2750 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002751 load_TR_desc();
2752}
2753
Rusty Russelle756fc62007-07-30 20:07:08 +10002754static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002755{
2756 int cpu = raw_smp_processor_id();
2757
Tejun Heo0fe1e002009-10-29 22:34:14 +09002758 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002759
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002760 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03002761 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09002762 if (svm->asid_generation != sd->asid_generation)
2763 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002764}
2765
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002766static void svm_inject_nmi(struct kvm_vcpu *vcpu)
2767{
2768 struct vcpu_svm *svm = to_svm(vcpu);
2769
2770 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
2771 vcpu->arch.hflags |= HF_NMI_MASK;
2772 svm->vmcb->control.intercept |= (1UL << INTERCEPT_IRET);
2773 ++vcpu->stat.nmi_injections;
2774}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002775
Eddie Dong85f455f2007-07-06 12:20:49 +03002776static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002777{
2778 struct vmcb_control_area *control;
2779
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002780 trace_kvm_inj_virq(irq);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002781
Avi Kivityfa89a812008-09-01 15:57:51 +03002782 ++svm->vcpu.stat.irq_injections;
Rusty Russelle756fc62007-07-30 20:07:08 +10002783 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03002784 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002785 control->int_ctl &= ~V_INTR_PRIO_MASK;
2786 control->int_ctl |= V_IRQ_MASK |
2787 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
2788}
2789
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002790static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03002791{
2792 struct vcpu_svm *svm = to_svm(vcpu);
2793
Joerg Roedel2af91942009-08-07 11:49:28 +02002794 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01002795
Alexander Graf219b65d2009-06-15 15:21:25 +02002796 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
2797 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002798}
2799
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002800static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
2801{
2802 struct vcpu_svm *svm = to_svm(vcpu);
2803
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002804 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
2805 return;
2806
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002807 if (irr == -1)
2808 return;
2809
2810 if (tpr >= irr)
2811 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK;
2812}
2813
2814static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02002815{
2816 struct vcpu_svm *svm = to_svm(vcpu);
2817 struct vmcb *vmcb = svm->vmcb;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002818 return !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
2819 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
Joerg Roedelaaacfc92008-04-16 16:51:18 +02002820}
2821
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002822static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
2823{
2824 struct vcpu_svm *svm = to_svm(vcpu);
2825
2826 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
2827}
2828
2829static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
2830{
2831 struct vcpu_svm *svm = to_svm(vcpu);
2832
2833 if (masked) {
2834 svm->vcpu.arch.hflags |= HF_NMI_MASK;
2835 svm->vmcb->control.intercept |= (1UL << INTERCEPT_IRET);
2836 } else {
2837 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
2838 svm->vmcb->control.intercept &= ~(1UL << INTERCEPT_IRET);
2839 }
2840}
2841
Gleb Natapov78646122009-03-23 12:12:11 +02002842static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
2843{
2844 struct vcpu_svm *svm = to_svm(vcpu);
2845 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02002846 int ret;
2847
2848 if (!gif_set(svm) ||
2849 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
2850 return 0;
2851
2852 ret = !!(vmcb->save.rflags & X86_EFLAGS_IF);
2853
2854 if (is_nested(svm))
2855 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
2856
2857 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02002858}
2859
Gleb Natapov9222be12009-04-23 17:14:37 +03002860static void enable_irq_window(struct kvm_vcpu *vcpu)
2861{
Alexander Graf219b65d2009-06-15 15:21:25 +02002862 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02002863
Joerg Roedele0231712010-02-24 18:59:10 +01002864 /*
2865 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
2866 * 1, because that's a separate STGI/VMRUN intercept. The next time we
2867 * get that intercept, this function will be called again though and
2868 * we'll get the vintr intercept.
2869 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01002870 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02002871 svm_set_vintr(svm);
2872 svm_inject_irq(svm, 0x0);
2873 }
Gleb Natapov9222be12009-04-23 17:14:37 +03002874}
2875
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002876static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002877{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002878 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03002879
Gleb Natapov44c11432009-05-11 13:35:52 +03002880 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
2881 == HF_NMI_MASK)
2882 return; /* IRET will cause a vm exit */
2883
Joerg Roedele0231712010-02-24 18:59:10 +01002884 /*
2885 * Something prevents NMI from been injected. Single step over possible
2886 * problem (IRET or exception injection or interrupt shadow)
2887 */
Joerg Roedel887f5002010-02-24 18:59:12 +01002888 if (gif_set(svm) && nested_svm_nmi(svm)) {
2889 svm->nmi_singlestep = true;
2890 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
2891 update_db_intercept(vcpu);
2892 }
Eddie Dong85f455f2007-07-06 12:20:49 +03002893}
2894
Izik Eiduscbc94022007-10-25 00:29:55 +02002895static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
2896{
2897 return 0;
2898}
2899
Avi Kivityd9e368d2007-06-07 19:18:30 +03002900static void svm_flush_tlb(struct kvm_vcpu *vcpu)
2901{
2902 force_new_asid(vcpu);
2903}
2904
Avi Kivity04d2cc72007-09-10 18:10:54 +03002905static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
2906{
2907}
2908
Joerg Roedeld7bf8222008-04-16 16:51:17 +02002909static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
2910{
2911 struct vcpu_svm *svm = to_svm(vcpu);
2912
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002913 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
2914 return;
2915
Joerg Roedeld7bf8222008-04-16 16:51:17 +02002916 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) {
2917 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03002918 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02002919 }
2920}
2921
Joerg Roedel649d6862008-04-16 16:51:15 +02002922static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
2923{
2924 struct vcpu_svm *svm = to_svm(vcpu);
2925 u64 cr8;
2926
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002927 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
2928 return;
2929
Joerg Roedel649d6862008-04-16 16:51:15 +02002930 cr8 = kvm_get_cr8(vcpu);
2931 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
2932 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
2933}
2934
Gleb Natapov9222be12009-04-23 17:14:37 +03002935static void svm_complete_interrupts(struct vcpu_svm *svm)
2936{
2937 u8 vector;
2938 int type;
2939 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01002940 unsigned int3_injected = svm->int3_injected;
2941
2942 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03002943
Gleb Natapov44c11432009-05-11 13:35:52 +03002944 if (svm->vcpu.arch.hflags & HF_IRET_MASK)
2945 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
2946
Gleb Natapov9222be12009-04-23 17:14:37 +03002947 svm->vcpu.arch.nmi_injected = false;
2948 kvm_clear_exception_queue(&svm->vcpu);
2949 kvm_clear_interrupt_queue(&svm->vcpu);
2950
2951 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
2952 return;
2953
2954 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
2955 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
2956
2957 switch (type) {
2958 case SVM_EXITINTINFO_TYPE_NMI:
2959 svm->vcpu.arch.nmi_injected = true;
2960 break;
2961 case SVM_EXITINTINFO_TYPE_EXEPT:
Alexander Graf219b65d2009-06-15 15:21:25 +02002962 if (is_nested(svm))
2963 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01002964 /*
2965 * In case of software exceptions, do not reinject the vector,
2966 * but re-execute the instruction instead. Rewind RIP first
2967 * if we emulated INT3 before.
2968 */
2969 if (kvm_exception_is_soft(vector)) {
2970 if (vector == BP_VECTOR && int3_injected &&
2971 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
2972 kvm_rip_write(&svm->vcpu,
2973 kvm_rip_read(&svm->vcpu) -
2974 int3_injected);
Gleb Natapov9222be12009-04-23 17:14:37 +03002975 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01002976 }
Gleb Natapov9222be12009-04-23 17:14:37 +03002977 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
2978 u32 err = svm->vmcb->control.exit_int_info_err;
2979 kvm_queue_exception_e(&svm->vcpu, vector, err);
2980
2981 } else
2982 kvm_queue_exception(&svm->vcpu, vector);
2983 break;
2984 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002985 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03002986 break;
2987 default:
2988 break;
2989 }
2990}
2991
Avi Kivity80e31d42008-07-14 14:44:59 +03002992#ifdef CONFIG_X86_64
2993#define R "r"
2994#else
2995#define R "e"
2996#endif
2997
Avi Kivity851ba692009-08-24 11:10:17 +03002998static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002999{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003000 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003001 u16 fs_selector;
3002 u16 gs_selector;
3003 u16 ldt_selector;
Avi Kivityd9e368d2007-06-07 19:18:30 +03003004
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003005 /*
3006 * A vmexit emulation is required before the vcpu can be executed
3007 * again.
3008 */
3009 if (unlikely(svm->nested.exit_required))
3010 return;
3011
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003012 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3013 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3014 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3015
Rusty Russelle756fc62007-07-30 20:07:08 +10003016 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003017
Joerg Roedel649d6862008-04-16 16:51:15 +02003018 sync_lapic_to_cr8(vcpu);
3019
Avi Kivity6aa8b732006-12-10 02:21:36 -08003020 save_host_msrs(vcpu);
Avi Kivityd6e88ae2008-07-10 16:53:33 +03003021 fs_selector = kvm_read_fs();
3022 gs_selector = kvm_read_gs();
3023 ldt_selector = kvm_read_ldt();
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003024 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003025 /* required for live migration with NPT */
3026 if (npt_enabled)
3027 svm->vmcb->save.cr3 = vcpu->arch.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003028
Avi Kivity04d2cc72007-09-10 18:10:54 +03003029 clgi();
3030
3031 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08003032
Avi Kivity6aa8b732006-12-10 02:21:36 -08003033 asm volatile (
Avi Kivity80e31d42008-07-14 14:44:59 +03003034 "push %%"R"bp; \n\t"
3035 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
3036 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
3037 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
3038 "mov %c[rsi](%[svm]), %%"R"si \n\t"
3039 "mov %c[rdi](%[svm]), %%"R"di \n\t"
3040 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003041#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003042 "mov %c[r8](%[svm]), %%r8 \n\t"
3043 "mov %c[r9](%[svm]), %%r9 \n\t"
3044 "mov %c[r10](%[svm]), %%r10 \n\t"
3045 "mov %c[r11](%[svm]), %%r11 \n\t"
3046 "mov %c[r12](%[svm]), %%r12 \n\t"
3047 "mov %c[r13](%[svm]), %%r13 \n\t"
3048 "mov %c[r14](%[svm]), %%r14 \n\t"
3049 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003050#endif
3051
Avi Kivity6aa8b732006-12-10 02:21:36 -08003052 /* Enter guest mode */
Avi Kivity80e31d42008-07-14 14:44:59 +03003053 "push %%"R"ax \n\t"
3054 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003055 __ex(SVM_VMLOAD) "\n\t"
3056 __ex(SVM_VMRUN) "\n\t"
3057 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity80e31d42008-07-14 14:44:59 +03003058 "pop %%"R"ax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003059
3060 /* Save guest registers, load host registers */
Avi Kivity80e31d42008-07-14 14:44:59 +03003061 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
3062 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
3063 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
3064 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
3065 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
3066 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003067#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003068 "mov %%r8, %c[r8](%[svm]) \n\t"
3069 "mov %%r9, %c[r9](%[svm]) \n\t"
3070 "mov %%r10, %c[r10](%[svm]) \n\t"
3071 "mov %%r11, %c[r11](%[svm]) \n\t"
3072 "mov %%r12, %c[r12](%[svm]) \n\t"
3073 "mov %%r13, %c[r13](%[svm]) \n\t"
3074 "mov %%r14, %c[r14](%[svm]) \n\t"
3075 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003076#endif
Avi Kivity80e31d42008-07-14 14:44:59 +03003077 "pop %%"R"bp"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003078 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003079 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003080 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003081 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
3082 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
3083 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
3084 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
3085 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
3086 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003087#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003088 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
3089 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
3090 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
3091 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
3092 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
3093 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
3094 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
3095 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003096#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02003097 : "cc", "memory"
Avi Kivity80e31d42008-07-14 14:44:59 +03003098 , R"bx", R"cx", R"dx", R"si", R"di"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003099#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02003100 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
3101#endif
3102 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003103
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003104 vcpu->arch.cr2 = svm->vmcb->save.cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003105 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3106 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3107 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003108
Avi Kivityd6e88ae2008-07-10 16:53:33 +03003109 kvm_load_fs(fs_selector);
3110 kvm_load_gs(gs_selector);
3111 kvm_load_ldt(ldt_selector);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003112 load_host_msrs(vcpu);
3113
3114 reload_tss(vcpu);
3115
Avi Kivity56ba47d2007-11-07 17:14:18 +02003116 local_irq_disable();
3117
3118 stgi();
3119
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003120 sync_cr8_to_lapic(vcpu);
3121
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003122 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003123
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003124 if (npt_enabled) {
3125 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3126 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3127 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003128}
3129
Avi Kivity80e31d42008-07-14 14:44:59 +03003130#undef R
3131
Avi Kivity6aa8b732006-12-10 02:21:36 -08003132static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3133{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003134 struct vcpu_svm *svm = to_svm(vcpu);
3135
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003136 if (npt_enabled) {
3137 svm->vmcb->control.nested_cr3 = root;
3138 force_new_asid(vcpu);
3139 return;
3140 }
3141
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003142 svm->vmcb->save.cr3 = root;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003143 force_new_asid(vcpu);
3144}
3145
Avi Kivity6aa8b732006-12-10 02:21:36 -08003146static int is_disabled(void)
3147{
Joerg Roedel6031a612007-06-22 12:29:50 +03003148 u64 vm_cr;
3149
3150 rdmsrl(MSR_VM_CR, vm_cr);
3151 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3152 return 1;
3153
Avi Kivity6aa8b732006-12-10 02:21:36 -08003154 return 0;
3155}
3156
Ingo Molnar102d8322007-02-19 14:37:47 +02003157static void
3158svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3159{
3160 /*
3161 * Patch in the VMMCALL instruction:
3162 */
3163 hypercall[0] = 0x0f;
3164 hypercall[1] = 0x01;
3165 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003166}
3167
Yang, Sheng002c7f72007-07-31 14:23:01 +03003168static void svm_check_processor_compat(void *rtn)
3169{
3170 *(int *)rtn = 0;
3171}
3172
Avi Kivity774ead32007-12-26 13:57:04 +02003173static bool svm_cpu_has_accelerated_tpr(void)
3174{
3175 return false;
3176}
3177
Sheng Yang67253af2008-04-25 10:20:22 +08003178static int get_npt_level(void)
3179{
3180#ifdef CONFIG_X86_64
3181 return PT64_ROOT_LEVEL;
3182#else
3183 return PT32E_ROOT_LEVEL;
3184#endif
3185}
3186
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003187static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003188{
3189 return 0;
3190}
3191
Sheng Yang0e851882009-12-18 16:48:46 +08003192static void svm_cpuid_update(struct kvm_vcpu *vcpu)
3193{
3194}
3195
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003196static const struct trace_print_flags svm_exit_reasons_str[] = {
Joerg Roedele0231712010-02-24 18:59:10 +01003197 { SVM_EXIT_READ_CR0, "read_cr0" },
3198 { SVM_EXIT_READ_CR3, "read_cr3" },
3199 { SVM_EXIT_READ_CR4, "read_cr4" },
3200 { SVM_EXIT_READ_CR8, "read_cr8" },
3201 { SVM_EXIT_WRITE_CR0, "write_cr0" },
3202 { SVM_EXIT_WRITE_CR3, "write_cr3" },
3203 { SVM_EXIT_WRITE_CR4, "write_cr4" },
3204 { SVM_EXIT_WRITE_CR8, "write_cr8" },
3205 { SVM_EXIT_READ_DR0, "read_dr0" },
3206 { SVM_EXIT_READ_DR1, "read_dr1" },
3207 { SVM_EXIT_READ_DR2, "read_dr2" },
3208 { SVM_EXIT_READ_DR3, "read_dr3" },
3209 { SVM_EXIT_WRITE_DR0, "write_dr0" },
3210 { SVM_EXIT_WRITE_DR1, "write_dr1" },
3211 { SVM_EXIT_WRITE_DR2, "write_dr2" },
3212 { SVM_EXIT_WRITE_DR3, "write_dr3" },
3213 { SVM_EXIT_WRITE_DR5, "write_dr5" },
3214 { SVM_EXIT_WRITE_DR7, "write_dr7" },
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003215 { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
3216 { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
3217 { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
3218 { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
3219 { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
3220 { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
3221 { SVM_EXIT_INTR, "interrupt" },
3222 { SVM_EXIT_NMI, "nmi" },
3223 { SVM_EXIT_SMI, "smi" },
3224 { SVM_EXIT_INIT, "init" },
3225 { SVM_EXIT_VINTR, "vintr" },
3226 { SVM_EXIT_CPUID, "cpuid" },
3227 { SVM_EXIT_INVD, "invd" },
3228 { SVM_EXIT_HLT, "hlt" },
3229 { SVM_EXIT_INVLPG, "invlpg" },
3230 { SVM_EXIT_INVLPGA, "invlpga" },
3231 { SVM_EXIT_IOIO, "io" },
3232 { SVM_EXIT_MSR, "msr" },
3233 { SVM_EXIT_TASK_SWITCH, "task_switch" },
3234 { SVM_EXIT_SHUTDOWN, "shutdown" },
3235 { SVM_EXIT_VMRUN, "vmrun" },
3236 { SVM_EXIT_VMMCALL, "hypercall" },
3237 { SVM_EXIT_VMLOAD, "vmload" },
3238 { SVM_EXIT_VMSAVE, "vmsave" },
3239 { SVM_EXIT_STGI, "stgi" },
3240 { SVM_EXIT_CLGI, "clgi" },
3241 { SVM_EXIT_SKINIT, "skinit" },
3242 { SVM_EXIT_WBINVD, "wbinvd" },
3243 { SVM_EXIT_MONITOR, "monitor" },
3244 { SVM_EXIT_MWAIT, "mwait" },
3245 { SVM_EXIT_NPF, "npf" },
3246 { -1, NULL }
3247};
3248
Sheng Yang17cc3932010-01-05 19:02:27 +08003249static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02003250{
Sheng Yang17cc3932010-01-05 19:02:27 +08003251 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02003252}
3253
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003254static bool svm_rdtscp_supported(void)
3255{
3256 return false;
3257}
3258
Avi Kivity02daab22009-12-30 12:40:26 +02003259static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
3260{
3261 struct vcpu_svm *svm = to_svm(vcpu);
3262
Avi Kivity02daab22009-12-30 12:40:26 +02003263 svm->vmcb->control.intercept_exceptions |= 1 << NM_VECTOR;
Joerg Roedel66a562f2010-02-19 16:23:08 +01003264 if (is_nested(svm))
3265 svm->nested.hsave->control.intercept_exceptions |= 1 << NM_VECTOR;
3266 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02003267}
3268
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003269static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003270 .cpu_has_kvm_support = has_svm,
3271 .disabled_by_bios = is_disabled,
3272 .hardware_setup = svm_hardware_setup,
3273 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003274 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003275 .hardware_enable = svm_hardware_enable,
3276 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003277 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003278
3279 .vcpu_create = svm_create_vcpu,
3280 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003281 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003282
Avi Kivity04d2cc72007-09-10 18:10:54 +03003283 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003284 .vcpu_load = svm_vcpu_load,
3285 .vcpu_put = svm_vcpu_put,
3286
3287 .set_guest_debug = svm_guest_debug,
3288 .get_msr = svm_get_msr,
3289 .set_msr = svm_set_msr,
3290 .get_segment_base = svm_get_segment_base,
3291 .get_segment = svm_get_segment,
3292 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003293 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10003294 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02003295 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003296 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003297 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003298 .set_cr3 = svm_set_cr3,
3299 .set_cr4 = svm_set_cr4,
3300 .set_efer = svm_set_efer,
3301 .get_idt = svm_get_idt,
3302 .set_idt = svm_set_idt,
3303 .get_gdt = svm_get_gdt,
3304 .set_gdt = svm_set_gdt,
3305 .get_dr = svm_get_dr,
3306 .set_dr = svm_set_dr,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003307 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003308 .get_rflags = svm_get_rflags,
3309 .set_rflags = svm_set_rflags,
Avi Kivity6b52d182010-01-21 15:31:47 +02003310 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02003311 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003312
Avi Kivity6aa8b732006-12-10 02:21:36 -08003313 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003314
Avi Kivity6aa8b732006-12-10 02:21:36 -08003315 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003316 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003317 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04003318 .set_interrupt_shadow = svm_set_interrupt_shadow,
3319 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02003320 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003321 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003322 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02003323 .queue_exception = svm_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02003324 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003325 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003326 .get_nmi_mask = svm_get_nmi_mask,
3327 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003328 .enable_nmi_window = enable_nmi_window,
3329 .enable_irq_window = enable_irq_window,
3330 .update_cr8_intercept = update_cr8_intercept,
Izik Eiduscbc94022007-10-25 00:29:55 +02003331
3332 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003333 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003334 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003335
3336 .exit_reasons_str = svm_exit_reasons_str,
Sheng Yang17cc3932010-01-05 19:02:27 +08003337 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08003338
3339 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003340
3341 .rdtscp_supported = svm_rdtscp_supported,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003342};
3343
3344static int __init svm_init(void)
3345{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003346 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Rusty Russellc16f8622007-07-30 21:12:19 +10003347 THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003348}
3349
3350static void __exit svm_exit(void)
3351{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003352 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003353}
3354
3355module_init(svm_init)
3356module_exit(svm_exit)