blob: e9f79619e18566dde236e304c1e58aabe22cbcbf [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * AMD SVM support
5 *
6 * Copyright (C) 2006 Qumranet, Inc.
7 *
8 * Authors:
9 * Yaniv Kamay <yaniv@qumranet.com>
10 * Avi Kivity <avi@qumranet.com>
11 *
12 * This work is licensed under the terms of the GNU GPL, version 2. See
13 * the COPYING file in the top-level directory.
14 *
15 */
Avi Kivityedf88412007-12-16 11:02:48 +020016#include <linux/kvm_host.h>
17
Eddie Dong85f455f2007-07-06 12:20:49 +030018#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080019#include "mmu.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030020#include "kvm_cache_regs.h"
Gleb Natapovfe4c7b12009-03-23 11:23:18 +020021#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040022
Avi Kivity6aa8b732006-12-10 02:21:36 -080023#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020024#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/vmalloc.h>
26#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040027#include <linux/sched.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030028#include <linux/ftrace_event.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080030
Avi Kivitye4956062007-06-28 14:15:57 -040031#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080032
Eduardo Habkost63d11422008-11-17 19:03:20 -020033#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030034#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020035
Avi Kivity4ecac3f2008-05-13 13:23:38 +030036#define __ex(x) __kvm_handle_fault_on_reboot(x)
37
Avi Kivity6aa8b732006-12-10 02:21:36 -080038MODULE_AUTHOR("Qumranet");
39MODULE_LICENSE("GPL");
40
41#define IOPM_ALLOC_ORDER 2
42#define MSRPM_ALLOC_ORDER 1
43
Avi Kivity6aa8b732006-12-10 02:21:36 -080044#define SEG_TYPE_LDT 2
45#define SEG_TYPE_BUSY_TSS16 3
46
Joerg Roedel80b77062007-03-30 17:02:14 +030047#define SVM_FEATURE_NPT (1 << 0)
48#define SVM_FEATURE_LBRV (1 << 1)
Amit Shah94c935a12008-08-18 13:11:46 +030049#define SVM_FEATURE_SVML (1 << 2)
Jan Kiszka66b71382010-02-23 17:47:56 +010050#define SVM_FEATURE_NRIP (1 << 3)
Mark Langsdorf565d0992009-10-06 14:25:02 -050051#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030052
Joerg Roedel410e4d52009-08-07 11:49:44 +020053#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
54#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
55#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
56
Joerg Roedel24e09cb2008-02-13 18:58:47 +010057#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
58
Avi Kivity6c8166a2009-05-31 18:15:37 +030059static const u32 host_save_user_msrs[] = {
60#ifdef CONFIG_X86_64
61 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
62 MSR_FS_BASE,
63#endif
64 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
65};
66
67#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
68
69struct kvm_vcpu;
70
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020071struct nested_state {
72 struct vmcb *hsave;
73 u64 hsave_msr;
Joerg Roedel4a810182010-02-24 18:59:15 +010074 u64 vm_cr_msr;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020075 u64 vmcb;
76
77 /* These are the merged vectors */
78 u32 *msrpm;
79
80 /* gpa pointers to the real vectors */
81 u64 vmcb_msrpm;
Joerg 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
323 if (!svm->next_rip) {
Avi Kivity851ba692009-08-24 11:10:17 +0300324 if (emulate_instruction(vcpu, 0, 0, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300325 EMULATE_DONE)
326 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800327 return;
328 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300329 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
330 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
331 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800332
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300333 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400334 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800335}
336
Jan Kiszka116a4752010-02-23 17:47:54 +0100337static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
338 bool has_error_code, u32 error_code)
339{
340 struct vcpu_svm *svm = to_svm(vcpu);
341
Joerg Roedele0231712010-02-24 18:59:10 +0100342 /*
343 * If we are within a nested VM we'd better #VMEXIT and let the guest
344 * handle the exception
345 */
Jan Kiszka116a4752010-02-23 17:47:54 +0100346 if (nested_svm_check_exception(svm, nr, has_error_code, error_code))
347 return;
348
Jan Kiszka66b71382010-02-23 17:47:56 +0100349 if (nr == BP_VECTOR && !svm_has(SVM_FEATURE_NRIP)) {
350 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
351
352 /*
353 * For guest debugging where we have to reinject #BP if some
354 * INT3 is guest-owned:
355 * Emulate nRIP by moving RIP forward. Will fail if injection
356 * raises a fault that is not intercepted. Still better than
357 * failing in all cases.
358 */
359 skip_emulated_instruction(&svm->vcpu);
360 rip = kvm_rip_read(&svm->vcpu);
361 svm->int3_rip = rip + svm->vmcb->save.cs.base;
362 svm->int3_injected = rip - old_rip;
363 }
364
Jan Kiszka116a4752010-02-23 17:47:54 +0100365 svm->vmcb->control.event_inj = nr
366 | SVM_EVTINJ_VALID
367 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
368 | SVM_EVTINJ_TYPE_EXEPT;
369 svm->vmcb->control.event_inj_err = error_code;
370}
371
Avi Kivity6aa8b732006-12-10 02:21:36 -0800372static int has_svm(void)
373{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200374 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800375
Eduardo Habkost63d11422008-11-17 19:03:20 -0200376 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800377 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800378 return 0;
379 }
380
Avi Kivity6aa8b732006-12-10 02:21:36 -0800381 return 1;
382}
383
384static void svm_hardware_disable(void *garbage)
385{
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200386 cpu_svm_disable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800387}
388
Alexander Graf10474ae2009-09-15 11:37:46 +0200389static int svm_hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800390{
391
Tejun Heo0fe1e002009-10-29 22:34:14 +0900392 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800393 uint64_t efer;
Gleb Natapov89a27f42010-02-16 10:51:48 +0200394 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800395 struct desc_struct *gdt;
396 int me = raw_smp_processor_id();
397
Alexander Graf10474ae2009-09-15 11:37:46 +0200398 rdmsrl(MSR_EFER, efer);
399 if (efer & EFER_SVME)
400 return -EBUSY;
401
Avi Kivity6aa8b732006-12-10 02:21:36 -0800402 if (!has_svm()) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000403 printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
404 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200405 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800406 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900407 sd = per_cpu(svm_data, me);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800408
Tejun Heo0fe1e002009-10-29 22:34:14 +0900409 if (!sd) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000410 printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -0800411 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200412 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800413 }
414
Tejun Heo0fe1e002009-10-29 22:34:14 +0900415 sd->asid_generation = 1;
416 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
417 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800418
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200419 native_store_gdt(&gdt_descr);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200420 gdt = (struct desc_struct *)gdt_descr.address;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900421 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800422
Alexander Graf9962d032008-11-25 20:17:02 +0100423 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424
Linus Torvaldsd0316552009-12-14 09:58:24 -0800425 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200426
427 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800428}
429
Joerg Roedel0da1db752008-07-02 16:02:11 +0200430static void svm_cpu_uninit(int cpu)
431{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900432 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200433
Tejun Heo0fe1e002009-10-29 22:34:14 +0900434 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200435 return;
436
437 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900438 __free_page(sd->save_area);
439 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200440}
441
Avi Kivity6aa8b732006-12-10 02:21:36 -0800442static int svm_cpu_init(int cpu)
443{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900444 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800445 int r;
446
Tejun Heo0fe1e002009-10-29 22:34:14 +0900447 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
448 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800449 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900450 sd->cpu = cpu;
451 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800452 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900453 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800454 goto err_1;
455
Tejun Heo0fe1e002009-10-29 22:34:14 +0900456 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800457
458 return 0;
459
460err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900461 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800462 return r;
463
464}
465
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100466static bool valid_msr_intercept(u32 index)
467{
468 int i;
469
470 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
471 if (direct_access_msrs[i].index == index)
472 return true;
473
474 return false;
475}
476
Rusty Russellbfc733a2007-07-31 20:42:42 +1000477static void set_msr_interception(u32 *msrpm, unsigned msr,
478 int read, int write)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800479{
Joerg Roedel455716f2010-03-01 15:34:35 +0100480 u8 bit_read, bit_write;
481 unsigned long tmp;
482 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800483
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100484 /*
485 * If this warning triggers extend the direct_access_msrs list at the
486 * beginning of the file
487 */
488 WARN_ON(!valid_msr_intercept(msr));
489
Joerg Roedel455716f2010-03-01 15:34:35 +0100490 offset = svm_msrpm_offset(msr);
491 bit_read = 2 * (msr & 0x0f);
492 bit_write = 2 * (msr & 0x0f) + 1;
493 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800494
Joerg Roedel455716f2010-03-01 15:34:35 +0100495 BUG_ON(offset == MSR_INVALID);
496
497 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
498 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
499
500 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800501}
502
Joerg Roedelf65c2292008-02-13 18:58:46 +0100503static void svm_vcpu_init_msrpm(u32 *msrpm)
504{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100505 int i;
506
Joerg Roedelf65c2292008-02-13 18:58:46 +0100507 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
508
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100509 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
510 if (!direct_access_msrs[i].always)
511 continue;
512
513 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
514 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100515}
516
Joerg Roedel323c3d82010-03-01 15:34:37 +0100517static void add_msr_offset(u32 offset)
518{
519 int i;
520
521 for (i = 0; i < MSRPM_OFFSETS; ++i) {
522
523 /* Offset already in list? */
524 if (msrpm_offsets[i] == offset)
525 return;
526
527 /* Slot used by another offset? */
528 if (msrpm_offsets[i] != MSR_INVALID)
529 continue;
530
531 /* Add offset to list */
532 msrpm_offsets[i] = offset;
533
534 return;
535 }
536
537 /*
538 * If this BUG triggers the msrpm_offsets table has an overflow. Just
539 * increase MSRPM_OFFSETS in this case.
540 */
541 BUG();
542}
543
544static void init_msrpm_offsets(void)
545{
546 int i;
547
548 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
549
550 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
551 u32 offset;
552
553 offset = svm_msrpm_offset(direct_access_msrs[i].index);
554 BUG_ON(offset == MSR_INVALID);
555
556 add_msr_offset(offset);
557 }
558}
559
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100560static void svm_enable_lbrv(struct vcpu_svm *svm)
561{
562 u32 *msrpm = svm->msrpm;
563
564 svm->vmcb->control.lbr_ctl = 1;
565 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
566 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
567 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
568 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
569}
570
571static void svm_disable_lbrv(struct vcpu_svm *svm)
572{
573 u32 *msrpm = svm->msrpm;
574
575 svm->vmcb->control.lbr_ctl = 0;
576 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
577 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
578 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
579 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
580}
581
Avi Kivity6aa8b732006-12-10 02:21:36 -0800582static __init int svm_hardware_setup(void)
583{
584 int cpu;
585 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100586 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800587 int r;
588
Avi Kivity6aa8b732006-12-10 02:21:36 -0800589 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
590
591 if (!iopm_pages)
592 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300593
594 iopm_va = page_address(iopm_pages);
595 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800596 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
597
Joerg Roedel323c3d82010-03-01 15:34:37 +0100598 init_msrpm_offsets();
599
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100600 if (boot_cpu_has(X86_FEATURE_NX))
601 kvm_enable_efer_bits(EFER_NX);
602
Alexander Graf1b2fd702009-02-02 16:23:51 +0100603 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
604 kvm_enable_efer_bits(EFER_FFXSR);
605
Alexander Graf236de052008-11-25 20:17:10 +0100606 if (nested) {
607 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
608 kvm_enable_efer_bits(EFER_SVME);
609 }
610
Zachary Amsden3230bb42009-09-29 11:38:37 -1000611 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800612 r = svm_cpu_init(cpu);
613 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100614 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800615 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100616
617 svm_features = cpuid_edx(SVM_CPUID_FUNC);
618
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100619 if (!svm_has(SVM_FEATURE_NPT))
620 npt_enabled = false;
621
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100622 if (npt_enabled && !npt) {
623 printk(KERN_INFO "kvm: Nested Paging disabled\n");
624 npt_enabled = false;
625 }
626
Joerg Roedel18552672008-02-07 13:47:41 +0100627 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100628 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100629 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200630 } else
631 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100632
Avi Kivity6aa8b732006-12-10 02:21:36 -0800633 return 0;
634
Joerg Roedelf65c2292008-02-13 18:58:46 +0100635err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800636 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
637 iopm_base = 0;
638 return r;
639}
640
641static __exit void svm_hardware_unsetup(void)
642{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200643 int cpu;
644
Zachary Amsden3230bb42009-09-29 11:38:37 -1000645 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200646 svm_cpu_uninit(cpu);
647
Avi Kivity6aa8b732006-12-10 02:21:36 -0800648 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100649 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800650}
651
652static void init_seg(struct vmcb_seg *seg)
653{
654 seg->selector = 0;
655 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +0100656 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800657 seg->limit = 0xffff;
658 seg->base = 0;
659}
660
661static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
662{
663 seg->selector = 0;
664 seg->attrib = SVM_SELECTOR_P_MASK | type;
665 seg->limit = 0xffff;
666 seg->base = 0;
667}
668
Joerg Roedele6101a92008-02-13 18:58:45 +0100669static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800670{
Joerg Roedele6101a92008-02-13 18:58:45 +0100671 struct vmcb_control_area *control = &svm->vmcb->control;
672 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800673
Avi Kivitybff78272010-01-07 13:16:08 +0200674 svm->vcpu.fpu_active = 1;
675
Joerg Roedele0231712010-02-24 18:59:10 +0100676 control->intercept_cr_read = INTERCEPT_CR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800677 INTERCEPT_CR3_MASK |
Joerg Roedel649d6862008-04-16 16:51:15 +0200678 INTERCEPT_CR4_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800679
Joerg Roedele0231712010-02-24 18:59:10 +0100680 control->intercept_cr_write = INTERCEPT_CR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800681 INTERCEPT_CR3_MASK |
Avi Kivity80a81192007-12-06 19:50:00 +0200682 INTERCEPT_CR4_MASK |
683 INTERCEPT_CR8_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800684
Joerg Roedele0231712010-02-24 18:59:10 +0100685 control->intercept_dr_read = INTERCEPT_DR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800686 INTERCEPT_DR1_MASK |
687 INTERCEPT_DR2_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100688 INTERCEPT_DR3_MASK |
689 INTERCEPT_DR4_MASK |
690 INTERCEPT_DR5_MASK |
691 INTERCEPT_DR6_MASK |
692 INTERCEPT_DR7_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800693
Joerg Roedele0231712010-02-24 18:59:10 +0100694 control->intercept_dr_write = INTERCEPT_DR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800695 INTERCEPT_DR1_MASK |
696 INTERCEPT_DR2_MASK |
697 INTERCEPT_DR3_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100698 INTERCEPT_DR4_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800699 INTERCEPT_DR5_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100700 INTERCEPT_DR6_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800701 INTERCEPT_DR7_MASK;
702
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500703 control->intercept_exceptions = (1 << PF_VECTOR) |
Joerg Roedel53371b52008-04-09 14:15:30 +0200704 (1 << UD_VECTOR) |
705 (1 << MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800706
707
Joerg Roedele0231712010-02-24 18:59:10 +0100708 control->intercept = (1ULL << INTERCEPT_INTR) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800709 (1ULL << INTERCEPT_NMI) |
Joerg Roedel01525272007-02-19 14:37:47 +0200710 (1ULL << INTERCEPT_SMI) |
Avi Kivityd2251572010-01-06 10:55:27 +0200711 (1ULL << INTERCEPT_SELECTIVE_CR0) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800712 (1ULL << INTERCEPT_CPUID) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200713 (1ULL << INTERCEPT_INVD) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800714 (1ULL << INTERCEPT_HLT) |
Marcelo Tosattia7052892008-09-23 13:18:35 -0300715 (1ULL << INTERCEPT_INVLPG) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800716 (1ULL << INTERCEPT_INVLPGA) |
717 (1ULL << INTERCEPT_IOIO_PROT) |
718 (1ULL << INTERCEPT_MSR_PROT) |
719 (1ULL << INTERCEPT_TASK_SWITCH) |
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800720 (1ULL << INTERCEPT_SHUTDOWN) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800721 (1ULL << INTERCEPT_VMRUN) |
722 (1ULL << INTERCEPT_VMMCALL) |
723 (1ULL << INTERCEPT_VMLOAD) |
724 (1ULL << INTERCEPT_VMSAVE) |
725 (1ULL << INTERCEPT_STGI) |
726 (1ULL << INTERCEPT_CLGI) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100727 (1ULL << INTERCEPT_SKINIT) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200728 (1ULL << INTERCEPT_WBINVD) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100729 (1ULL << INTERCEPT_MONITOR) |
730 (1ULL << INTERCEPT_MWAIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800731
732 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100733 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity0cc50642007-03-25 12:07:27 +0200734 control->tsc_offset = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800735 control->int_ctl = V_INTR_MASKING_MASK;
736
737 init_seg(&save->es);
738 init_seg(&save->ss);
739 init_seg(&save->ds);
740 init_seg(&save->fs);
741 init_seg(&save->gs);
742
743 save->cs.selector = 0xf000;
744 /* Executable/Readable Code Segment */
745 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
746 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
747 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800748 /*
749 * cs.base should really be 0xffff0000, but vmx can't handle that, so
750 * be consistent with it.
751 *
752 * Replace when we have real mode working for vmx.
753 */
754 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800755
756 save->gdtr.limit = 0xffff;
757 save->idtr.limit = 0xffff;
758
759 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
760 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
761
Alexander Graf9962d032008-11-25 20:17:02 +0100762 save->efer = EFER_SVME;
Mike Dayd77c26f2007-10-08 09:02:08 -0400763 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800764 save->dr7 = 0x400;
765 save->rflags = 2;
766 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300767 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800768
Joerg Roedele0231712010-02-24 18:59:10 +0100769 /*
770 * This is the guest-visible cr0 value.
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200771 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800772 */
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200773 svm->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
774 kvm_set_cr0(&svm->vcpu, svm->vcpu.arch.cr0);
775
Rusty Russell66aee912007-07-17 23:34:16 +1000776 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100778
779 if (npt_enabled) {
780 /* Setup VMCB for Nested Paging */
781 control->nested_ctl = 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -0300782 control->intercept &= ~((1ULL << INTERCEPT_TASK_SWITCH) |
783 (1ULL << INTERCEPT_INVLPG));
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100784 control->intercept_exceptions &= ~(1 << PF_VECTOR);
Avi Kivity888f9f32010-01-10 12:14:04 +0200785 control->intercept_cr_read &= ~INTERCEPT_CR3_MASK;
786 control->intercept_cr_write &= ~INTERCEPT_CR3_MASK;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100787 save->g_pat = 0x0007040600070406ULL;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100788 save->cr3 = 0;
789 save->cr4 = 0;
790 }
Avi Kivitya79d2f12008-04-14 13:10:21 +0300791 force_new_asid(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +0100792
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200793 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +0200794 svm->vcpu.arch.hflags = 0;
795
Mark Langsdorf565d0992009-10-06 14:25:02 -0500796 if (svm_has(SVM_FEATURE_PAUSE_FILTER)) {
797 control->pause_filter_count = 3000;
798 control->intercept |= (1ULL << INTERCEPT_PAUSE);
799 }
800
Joerg Roedel2af91942009-08-07 11:49:28 +0200801 enable_gif(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800802}
803
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200804static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +0300805{
806 struct vcpu_svm *svm = to_svm(vcpu);
807
Joerg Roedele6101a92008-02-13 18:58:45 +0100808 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +0200809
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300810 if (!kvm_vcpu_is_bsp(vcpu)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300811 kvm_rip_write(vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800812 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
813 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +0200814 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300815 vcpu->arch.regs_avail = ~0;
816 vcpu->arch.regs_dirty = ~0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200817
818 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300819}
820
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000821static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800822{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400823 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800824 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100825 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +0100826 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100827 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000828 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800829
Rusty Russellc16f8622007-07-30 21:12:19 +1000830 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000831 if (!svm) {
832 err = -ENOMEM;
833 goto out;
834 }
835
836 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
837 if (err)
838 goto free_svm;
839
Joerg Roedelf65c2292008-02-13 18:58:46 +0100840 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900841 page = alloc_page(GFP_KERNEL);
842 if (!page)
843 goto uninit;
844
Joerg Roedelf65c2292008-02-13 18:58:46 +0100845 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
846 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900847 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100848
849 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
850 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900851 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100852
Alexander Grafb286d5d2008-11-25 20:17:05 +0100853 hsave_page = alloc_page(GFP_KERNEL);
854 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900855 goto free_page3;
856
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200857 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +0100858
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900859 svm->msrpm = page_address(msrpm_pages);
860 svm_vcpu_init_msrpm(svm->msrpm);
861
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200862 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +0100863 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +0100864
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400865 svm->vmcb = page_address(page);
866 clear_page(svm->vmcb);
867 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
868 svm->asid_generation = 0;
Joerg Roedele6101a92008-02-13 18:58:45 +0100869 init_vmcb(svm);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400870
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000871 fx_init(&svm->vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800872 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300873 if (kvm_vcpu_is_bsp(&svm->vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800874 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000876 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -0800877
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900878free_page3:
879 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
880free_page2:
881 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
882free_page1:
883 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000884uninit:
885 kvm_vcpu_uninit(&svm->vcpu);
886free_svm:
Rusty Russella4770342007-08-01 14:46:11 +1000887 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000888out:
889 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890}
891
892static void svm_free_vcpu(struct kvm_vcpu *vcpu)
893{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400894 struct vcpu_svm *svm = to_svm(vcpu);
895
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000896 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +0100897 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200898 __free_page(virt_to_page(svm->nested.hsave));
899 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000900 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +1000901 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800902}
903
Avi Kivity15ad7142007-07-11 18:17:21 +0300904static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400906 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300907 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +0200908
Avi Kivity0cc50642007-03-25 12:07:27 +0200909 if (unlikely(cpu != vcpu->cpu)) {
Joerg Roedele935d482009-09-16 15:24:19 +0200910 u64 delta;
Avi Kivity0cc50642007-03-25 12:07:27 +0200911
Joerg Roedel953899b2009-12-14 12:22:20 +0100912 if (check_tsc_unstable()) {
913 /*
914 * Make sure that the guest sees a monotonically
915 * increasing TSC.
916 */
917 delta = vcpu->arch.host_tsc - native_read_tsc();
918 svm->vmcb->control.tsc_offset += delta;
919 if (is_nested(svm))
920 svm->nested.hsave->control.tsc_offset += delta;
921 }
Avi Kivity0cc50642007-03-25 12:07:27 +0200922 vcpu->cpu = cpu;
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300923 kvm_migrate_timers(vcpu);
Marcelo Tosatti4b656b12009-07-21 12:47:45 -0300924 svm->asid_generation = 0;
Avi Kivity0cc50642007-03-25 12:07:27 +0200925 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300926
927 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400928 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929}
930
931static void svm_vcpu_put(struct kvm_vcpu *vcpu)
932{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400933 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300934 int i;
935
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200936 ++vcpu->stat.host_state_reload;
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300937 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400938 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300939
Joerg Roedele935d482009-09-16 15:24:19 +0200940 vcpu->arch.host_tsc = native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800941}
942
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
944{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400945 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800946}
947
948static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
949{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400950 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951}
952
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300953static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
954{
955 switch (reg) {
956 case VCPU_EXREG_PDPTR:
957 BUG_ON(!npt_enabled);
958 load_pdptrs(vcpu, vcpu->arch.cr3);
959 break;
960 default:
961 BUG();
962 }
963}
964
Alexander Graff0b85052008-11-25 20:17:01 +0100965static void svm_set_vintr(struct vcpu_svm *svm)
966{
967 svm->vmcb->control.intercept |= 1ULL << INTERCEPT_VINTR;
968}
969
970static void svm_clear_vintr(struct vcpu_svm *svm)
971{
972 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
973}
974
Avi Kivity6aa8b732006-12-10 02:21:36 -0800975static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
976{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400977 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800978
979 switch (seg) {
980 case VCPU_SREG_CS: return &save->cs;
981 case VCPU_SREG_DS: return &save->ds;
982 case VCPU_SREG_ES: return &save->es;
983 case VCPU_SREG_FS: return &save->fs;
984 case VCPU_SREG_GS: return &save->gs;
985 case VCPU_SREG_SS: return &save->ss;
986 case VCPU_SREG_TR: return &save->tr;
987 case VCPU_SREG_LDTR: return &save->ldtr;
988 }
989 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +0000990 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991}
992
993static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
994{
995 struct vmcb_seg *s = svm_seg(vcpu, seg);
996
997 return s->base;
998}
999
1000static void svm_get_segment(struct kvm_vcpu *vcpu,
1001 struct kvm_segment *var, int seg)
1002{
1003 struct vmcb_seg *s = svm_seg(vcpu, seg);
1004
1005 var->base = s->base;
1006 var->limit = s->limit;
1007 var->selector = s->selector;
1008 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1009 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1010 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1011 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1012 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1013 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1014 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
1015 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
Amit Shah25022ac2008-10-27 09:04:17 +00001016
Joerg Roedele0231712010-02-24 18:59:10 +01001017 /*
1018 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001019 * for cross vendor migration purposes by "not present"
1020 */
1021 var->unusable = !var->present || (var->type == 0);
1022
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001023 switch (seg) {
1024 case VCPU_SREG_CS:
1025 /*
1026 * SVM always stores 0 for the 'G' bit in the CS selector in
1027 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
1028 * Intel's VMENTRY has a check on the 'G' bit.
1029 */
Amit Shah25022ac2008-10-27 09:04:17 +00001030 var->g = s->limit > 0xfffff;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001031 break;
1032 case VCPU_SREG_TR:
1033 /*
1034 * Work around a bug where the busy flag in the tr selector
1035 * isn't exposed
1036 */
Amit Shahc0d09822008-10-27 09:04:18 +00001037 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001038 break;
1039 case VCPU_SREG_DS:
1040 case VCPU_SREG_ES:
1041 case VCPU_SREG_FS:
1042 case VCPU_SREG_GS:
1043 /*
1044 * The accessed bit must always be set in the segment
1045 * descriptor cache, although it can be cleared in the
1046 * descriptor, the cached bit always remains at 1. Since
1047 * Intel has a check on this, set it here to support
1048 * cross-vendor migration.
1049 */
1050 if (!var->unusable)
1051 var->type |= 0x1;
1052 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001053 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001054 /*
1055 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001056 * descriptor is left as 1, although the whole segment has
1057 * been made unusable. Clear it here to pass an Intel VMX
1058 * entry check when cross vendor migrating.
1059 */
1060 if (var->unusable)
1061 var->db = 0;
1062 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001063 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064}
1065
Izik Eidus2e4d2652008-03-24 19:38:34 +02001066static int svm_get_cpl(struct kvm_vcpu *vcpu)
1067{
1068 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1069
1070 return save->cpl;
1071}
1072
Gleb Natapov89a27f42010-02-16 10:51:48 +02001073static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001074{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001075 struct vcpu_svm *svm = to_svm(vcpu);
1076
Gleb Natapov89a27f42010-02-16 10:51:48 +02001077 dt->size = svm->vmcb->save.idtr.limit;
1078 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001079}
1080
Gleb Natapov89a27f42010-02-16 10:51:48 +02001081static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001082{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001083 struct vcpu_svm *svm = to_svm(vcpu);
1084
Gleb Natapov89a27f42010-02-16 10:51:48 +02001085 svm->vmcb->save.idtr.limit = dt->size;
1086 svm->vmcb->save.idtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001087}
1088
Gleb Natapov89a27f42010-02-16 10:51:48 +02001089static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001090{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001091 struct vcpu_svm *svm = to_svm(vcpu);
1092
Gleb Natapov89a27f42010-02-16 10:51:48 +02001093 dt->size = svm->vmcb->save.gdtr.limit;
1094 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001095}
1096
Gleb Natapov89a27f42010-02-16 10:51:48 +02001097static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001098{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001099 struct vcpu_svm *svm = to_svm(vcpu);
1100
Gleb Natapov89a27f42010-02-16 10:51:48 +02001101 svm->vmcb->save.gdtr.limit = dt->size;
1102 svm->vmcb->save.gdtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001103}
1104
Avi Kivitye8467fd2009-12-29 18:43:06 +02001105static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1106{
1107}
1108
Anthony Liguori25c4c272007-04-27 09:29:21 +03001109static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001110{
1111}
1112
Avi Kivityd2251572010-01-06 10:55:27 +02001113static void update_cr0_intercept(struct vcpu_svm *svm)
1114{
Joerg Roedel66a562f2010-02-19 16:23:08 +01001115 struct vmcb *vmcb = svm->vmcb;
Avi Kivityd2251572010-01-06 10:55:27 +02001116 ulong gcr0 = svm->vcpu.arch.cr0;
1117 u64 *hcr0 = &svm->vmcb->save.cr0;
1118
1119 if (!svm->vcpu.fpu_active)
1120 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1121 else
1122 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1123 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1124
1125
1126 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Joerg Roedel66a562f2010-02-19 16:23:08 +01001127 vmcb->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1128 vmcb->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1129 if (is_nested(svm)) {
1130 struct vmcb *hsave = svm->nested.hsave;
1131
1132 hsave->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1133 hsave->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1134 vmcb->control.intercept_cr_read |= svm->nested.intercept_cr_read;
1135 vmcb->control.intercept_cr_write |= svm->nested.intercept_cr_write;
1136 }
Avi Kivityd2251572010-01-06 10:55:27 +02001137 } else {
1138 svm->vmcb->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1139 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001140 if (is_nested(svm)) {
1141 struct vmcb *hsave = svm->nested.hsave;
1142
1143 hsave->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1144 hsave->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
1145 }
Avi Kivityd2251572010-01-06 10:55:27 +02001146 }
1147}
1148
Avi Kivity6aa8b732006-12-10 02:21:36 -08001149static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1150{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001151 struct vcpu_svm *svm = to_svm(vcpu);
1152
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001153 if (is_nested(svm)) {
1154 /*
1155 * We are here because we run in nested mode, the host kvm
1156 * intercepts cr0 writes but the l1 hypervisor does not.
1157 * But the L1 hypervisor may intercept selective cr0 writes.
1158 * This needs to be checked here.
1159 */
1160 unsigned long old, new;
1161
1162 /* Remove bits that would trigger a real cr0 write intercept */
1163 old = vcpu->arch.cr0 & SVM_CR0_SELECTIVE_MASK;
1164 new = cr0 & SVM_CR0_SELECTIVE_MASK;
1165
1166 if (old == new) {
1167 /* cr0 write with ts and mp unchanged */
1168 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
1169 if (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE)
1170 return;
1171 }
1172 }
1173
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001174#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001175 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001176 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001177 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001178 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001179 }
1180
Mike Dayd77c26f2007-10-08 09:02:08 -04001181 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001182 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001183 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001184 }
1185 }
1186#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001187 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001188
1189 if (!npt_enabled)
1190 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001191
1192 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001193 cr0 |= X86_CR0_TS;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001194 /*
1195 * re-enable caching here because the QEMU bios
1196 * does not do it - this results in some delay at
1197 * reboot
1198 */
1199 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001200 svm->vmcb->save.cr0 = cr0;
Avi Kivityd2251572010-01-06 10:55:27 +02001201 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001202}
1203
1204static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1205{
Joerg Roedel6394b642008-04-09 14:15:29 +02001206 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001207 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1208
1209 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
1210 force_new_asid(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001211
Joerg Roedelec077262008-04-09 14:15:28 +02001212 vcpu->arch.cr4 = cr4;
1213 if (!npt_enabled)
1214 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001215 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001216 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001217}
1218
1219static void svm_set_segment(struct kvm_vcpu *vcpu,
1220 struct kvm_segment *var, int seg)
1221{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001222 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001223 struct vmcb_seg *s = svm_seg(vcpu, seg);
1224
1225 s->base = var->base;
1226 s->limit = var->limit;
1227 s->selector = var->selector;
1228 if (var->unusable)
1229 s->attrib = 0;
1230 else {
1231 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1232 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1233 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1234 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1235 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1236 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1237 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1238 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1239 }
1240 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001241 svm->vmcb->save.cpl
1242 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -08001243 >> SVM_SELECTOR_DPL_SHIFT) & 3;
1244
1245}
1246
Gleb Natapov44c11432009-05-11 13:35:52 +03001247static void update_db_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001248{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001249 struct vcpu_svm *svm = to_svm(vcpu);
1250
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001251 svm->vmcb->control.intercept_exceptions &=
1252 ~((1 << DB_VECTOR) | (1 << BP_VECTOR));
Gleb Natapov44c11432009-05-11 13:35:52 +03001253
Jan Kiszka6be7d302009-10-18 13:24:54 +02001254 if (svm->nmi_singlestep)
Gleb Natapov44c11432009-05-11 13:35:52 +03001255 svm->vmcb->control.intercept_exceptions |= (1 << DB_VECTOR);
1256
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001257 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1258 if (vcpu->guest_debug &
1259 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
1260 svm->vmcb->control.intercept_exceptions |=
1261 1 << DB_VECTOR;
1262 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
1263 svm->vmcb->control.intercept_exceptions |=
1264 1 << BP_VECTOR;
1265 } else
1266 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001267}
1268
Jan Kiszka355be0b2009-10-03 00:31:21 +02001269static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Gleb Natapov44c11432009-05-11 13:35:52 +03001270{
Gleb Natapov44c11432009-05-11 13:35:52 +03001271 struct vcpu_svm *svm = to_svm(vcpu);
1272
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001273 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1274 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1275 else
1276 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1277
Jan Kiszka355be0b2009-10-03 00:31:21 +02001278 update_db_intercept(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001279}
1280
1281static void load_host_msrs(struct kvm_vcpu *vcpu)
1282{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001283#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001284 wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001285#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001286}
1287
1288static void save_host_msrs(struct kvm_vcpu *vcpu)
1289{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001290#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001291 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001292#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001293}
1294
Tejun Heo0fe1e002009-10-29 22:34:14 +09001295static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001296{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001297 if (sd->next_asid > sd->max_asid) {
1298 ++sd->asid_generation;
1299 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001300 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001301 }
1302
Tejun Heo0fe1e002009-10-29 22:34:14 +09001303 svm->asid_generation = sd->asid_generation;
1304 svm->vmcb->control.asid = sd->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001305}
1306
Jan Kiszkac76de352010-01-20 18:20:20 +01001307static int svm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *dest)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001308{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001309 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001310
1311 switch (dr) {
1312 case 0 ... 3:
Jan Kiszkac76de352010-01-20 18:20:20 +01001313 *dest = vcpu->arch.db[dr];
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001314 break;
Jan Kiszkac76de352010-01-20 18:20:20 +01001315 case 4:
1316 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
1317 return EMULATE_FAIL; /* will re-inject UD */
1318 /* fall through */
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001319 case 6:
1320 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
Jan Kiszkac76de352010-01-20 18:20:20 +01001321 *dest = vcpu->arch.dr6;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001322 else
Jan Kiszkac76de352010-01-20 18:20:20 +01001323 *dest = svm->vmcb->save.dr6;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001324 break;
Jan Kiszkac76de352010-01-20 18:20:20 +01001325 case 5:
1326 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
1327 return EMULATE_FAIL; /* will re-inject UD */
1328 /* fall through */
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001329 case 7:
1330 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
Jan Kiszkac76de352010-01-20 18:20:20 +01001331 *dest = vcpu->arch.dr7;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001332 else
Jan Kiszkac76de352010-01-20 18:20:20 +01001333 *dest = svm->vmcb->save.dr7;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001334 break;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001335 }
1336
Jan Kiszkac76de352010-01-20 18:20:20 +01001337 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001338}
1339
Jan Kiszkac76de352010-01-20 18:20:20 +01001340static int svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001341{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001342 struct vcpu_svm *svm = to_svm(vcpu);
1343
Avi Kivity6aa8b732006-12-10 02:21:36 -08001344 switch (dr) {
1345 case 0 ... 3:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001346 vcpu->arch.db[dr] = value;
1347 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1348 vcpu->arch.eff_db[dr] = value;
Jan Kiszkac76de352010-01-20 18:20:20 +01001349 break;
1350 case 4:
1351 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
1352 return EMULATE_FAIL; /* will re-inject UD */
1353 /* fall through */
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001354 case 6:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001355 vcpu->arch.dr6 = (value & DR6_VOLATILE) | DR6_FIXED_1;
Jan Kiszkac76de352010-01-20 18:20:20 +01001356 break;
1357 case 5:
1358 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
1359 return EMULATE_FAIL; /* will re-inject UD */
1360 /* fall through */
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001361 case 7:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001362 vcpu->arch.dr7 = (value & DR7_VOLATILE) | DR7_FIXED_1;
1363 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
1364 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1365 vcpu->arch.switch_db_regs = (value & DR7_BP_EN_MASK);
1366 }
Jan Kiszkac76de352010-01-20 18:20:20 +01001367 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001368 }
Jan Kiszkac76de352010-01-20 18:20:20 +01001369
1370 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001371}
1372
Avi Kivity851ba692009-08-24 11:10:17 +03001373static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001374{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001375 u64 fault_address;
1376 u32 error_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001377
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001378 fault_address = svm->vmcb->control.exit_info_2;
1379 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001380
Marcelo Tosatti229456f2009-06-17 09:22:14 -03001381 trace_kvm_page_fault(fault_address, error_code);
Avi Kivity52c78472009-08-10 15:42:41 +03001382 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1383 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
Avi Kivity30677142007-10-28 18:48:59 +02001384 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385}
1386
Avi Kivity851ba692009-08-24 11:10:17 +03001387static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001388{
Avi Kivity851ba692009-08-24 11:10:17 +03001389 struct kvm_run *kvm_run = svm->vcpu.run;
1390
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001391 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001392 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001393 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001394 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1395 return 1;
1396 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001397
Jan Kiszka6be7d302009-10-18 13:24:54 +02001398 if (svm->nmi_singlestep) {
1399 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001400 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1401 svm->vmcb->save.rflags &=
1402 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1403 update_db_intercept(&svm->vcpu);
1404 }
1405
1406 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001407 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001408 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1409 kvm_run->debug.arch.pc =
1410 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1411 kvm_run->debug.arch.exception = DB_VECTOR;
1412 return 0;
1413 }
1414
1415 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001416}
1417
Avi Kivity851ba692009-08-24 11:10:17 +03001418static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001419{
Avi Kivity851ba692009-08-24 11:10:17 +03001420 struct kvm_run *kvm_run = svm->vcpu.run;
1421
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001422 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1423 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1424 kvm_run->debug.arch.exception = BP_VECTOR;
1425 return 0;
1426}
1427
Avi Kivity851ba692009-08-24 11:10:17 +03001428static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001429{
1430 int er;
1431
Avi Kivity851ba692009-08-24 11:10:17 +03001432 er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001433 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001434 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001435 return 1;
1436}
1437
Avi Kivity6b52d182010-01-21 15:31:47 +02001438static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001439{
Avi Kivity6b52d182010-01-21 15:31:47 +02001440 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001441 u32 excp;
1442
1443 if (is_nested(svm)) {
1444 u32 h_excp, n_excp;
1445
1446 h_excp = svm->nested.hsave->control.intercept_exceptions;
1447 n_excp = svm->nested.intercept_exceptions;
1448 h_excp &= ~(1 << NM_VECTOR);
1449 excp = h_excp | n_excp;
1450 } else {
1451 excp = svm->vmcb->control.intercept_exceptions;
Joerg Roedele0231712010-02-24 18:59:10 +01001452 excp &= ~(1 << NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001453 }
1454
1455 svm->vmcb->control.intercept_exceptions = excp;
1456
Rusty Russelle756fc62007-07-30 20:07:08 +10001457 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001458 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001459}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001460
Avi Kivity6b52d182010-01-21 15:31:47 +02001461static int nm_interception(struct vcpu_svm *svm)
1462{
1463 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001464 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001465}
1466
Avi Kivity851ba692009-08-24 11:10:17 +03001467static int mc_interception(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001468{
1469 /*
1470 * On an #MC intercept the MCE handler is not called automatically in
1471 * the host. So do it by hand here.
1472 */
1473 asm volatile (
1474 "int $0x12\n");
1475 /* not sure if we ever come back to this point */
1476
1477 return 1;
1478}
1479
Avi Kivity851ba692009-08-24 11:10:17 +03001480static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001481{
Avi Kivity851ba692009-08-24 11:10:17 +03001482 struct kvm_run *kvm_run = svm->vcpu.run;
1483
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001484 /*
1485 * VMCB is undefined after a SHUTDOWN intercept
1486 * so reinitialize it.
1487 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001488 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001489 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001490
1491 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1492 return 0;
1493}
1494
Avi Kivity851ba692009-08-24 11:10:17 +03001495static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001496{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001497 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04001498 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001499 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001500 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001501
Rusty Russelle756fc62007-07-30 20:07:08 +10001502 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03001503 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001504 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001505 if (string || in)
1506 return !(emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DO_MMIO);
1507
Avi Kivity039576c2007-03-20 12:46:50 +02001508 port = io_info >> 16;
1509 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001510 svm->next_rip = svm->vmcb->control.exit_info_2;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001511 skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001512
1513 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001514}
1515
Avi Kivity851ba692009-08-24 11:10:17 +03001516static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001517{
1518 return 1;
1519}
1520
Avi Kivity851ba692009-08-24 11:10:17 +03001521static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001522{
1523 ++svm->vcpu.stat.irq_exits;
1524 return 1;
1525}
1526
Avi Kivity851ba692009-08-24 11:10:17 +03001527static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001528{
1529 return 1;
1530}
1531
Avi Kivity851ba692009-08-24 11:10:17 +03001532static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001533{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001534 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001535 skip_emulated_instruction(&svm->vcpu);
1536 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001537}
1538
Avi Kivity851ba692009-08-24 11:10:17 +03001539static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001540{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001541 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001542 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001543 kvm_emulate_hypercall(&svm->vcpu);
1544 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001545}
1546
Alexander Grafc0725422008-11-25 20:17:03 +01001547static int nested_svm_check_permissions(struct vcpu_svm *svm)
1548{
Avi Kivityf6801df2010-01-21 15:31:50 +02001549 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01001550 || !is_paging(&svm->vcpu)) {
1551 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1552 return 1;
1553 }
1554
1555 if (svm->vmcb->save.cpl) {
1556 kvm_inject_gp(&svm->vcpu, 0);
1557 return 1;
1558 }
1559
1560 return 0;
1561}
1562
Alexander Grafcf74a782008-11-25 20:17:08 +01001563static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
1564 bool has_error_code, u32 error_code)
1565{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001566 int vmexit;
1567
Joerg Roedel0295ad72009-08-07 11:49:37 +02001568 if (!is_nested(svm))
1569 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001570
Joerg Roedel0295ad72009-08-07 11:49:37 +02001571 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1572 svm->vmcb->control.exit_code_hi = 0;
1573 svm->vmcb->control.exit_info_1 = error_code;
1574 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1575
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001576 vmexit = nested_svm_intercept(svm);
1577 if (vmexit == NESTED_EXIT_DONE)
1578 svm->nested.exit_required = true;
1579
1580 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001581}
1582
Joerg Roedel8fe54652010-02-19 16:23:01 +01001583/* This function returns true if it is save to enable the irq window */
1584static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001585{
Joerg Roedel26666952009-08-07 11:49:46 +02001586 if (!is_nested(svm))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001587 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001588
Joerg Roedel26666952009-08-07 11:49:46 +02001589 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
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_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001593 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001594
Joerg Roedel197717d2010-02-24 18:59:19 +01001595 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
1596 svm->vmcb->control.exit_info_1 = 0;
1597 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02001598
Joerg Roedelcd3ff652009-10-09 16:08:26 +02001599 if (svm->nested.intercept & 1ULL) {
1600 /*
1601 * The #vmexit can't be emulated here directly because this
1602 * code path runs with irqs and preemtion disabled. A
1603 * #vmexit emulation might sleep. Only signal request for
1604 * the #vmexit here.
1605 */
1606 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02001607 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01001608 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001609 }
1610
Joerg Roedel8fe54652010-02-19 16:23:01 +01001611 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001612}
1613
Joerg Roedel887f5002010-02-24 18:59:12 +01001614/* This function returns true if it is save to enable the nmi window */
1615static inline bool nested_svm_nmi(struct vcpu_svm *svm)
1616{
1617 if (!is_nested(svm))
1618 return true;
1619
1620 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
1621 return true;
1622
1623 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
1624 svm->nested.exit_required = true;
1625
1626 return false;
1627}
1628
Joerg Roedel7597f122010-02-19 16:23:00 +01001629static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001630{
1631 struct page *page;
1632
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01001633 might_sleep();
1634
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001635 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001636 if (is_error_page(page))
1637 goto error;
1638
Joerg Roedel7597f122010-02-19 16:23:00 +01001639 *_page = page;
1640
1641 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001642
1643error:
1644 kvm_release_page_clean(page);
1645 kvm_inject_gp(&svm->vcpu, 0);
1646
1647 return NULL;
1648}
1649
Joerg Roedel7597f122010-02-19 16:23:00 +01001650static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001651{
Joerg Roedel7597f122010-02-19 16:23:00 +01001652 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001653 kvm_release_page_dirty(page);
1654}
1655
Joerg Roedelce2ac082010-03-01 15:34:39 +01001656static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
1657{
1658 unsigned port;
1659 u8 val, bit;
1660 u64 gpa;
1661
1662 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
1663 return NESTED_EXIT_HOST;
1664
1665 port = svm->vmcb->control.exit_info_1 >> 16;
1666 gpa = svm->nested.vmcb_iopm + (port / 8);
1667 bit = port % 8;
1668 val = 0;
1669
1670 if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1))
1671 val &= (1 << bit);
1672
1673 return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1674}
1675
Joerg Roedeld2477822010-03-01 15:34:34 +01001676static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001677{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001678 u32 offset, msr, value;
1679 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001680
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001681 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01001682 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001683
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001684 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1685 offset = svm_msrpm_offset(msr);
1686 write = svm->vmcb->control.exit_info_1 & 1;
1687 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001688
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001689 if (offset == MSR_INVALID)
1690 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001691
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001692 /* Offset is in 32 bit units but need in 8 bit units */
1693 offset *= 4;
1694
1695 if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4))
1696 return NESTED_EXIT_DONE;
1697
1698 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001699}
1700
Joerg Roedel410e4d52009-08-07 11:49:44 +02001701static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001702{
Alexander Grafcf74a782008-11-25 20:17:08 +01001703 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001704
Joerg Roedel410e4d52009-08-07 11:49:44 +02001705 switch (exit_code) {
1706 case SVM_EXIT_INTR:
1707 case SVM_EXIT_NMI:
1708 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001709 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01001710 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001711 if (npt_enabled)
1712 return NESTED_EXIT_HOST;
1713 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001714 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Joerg Roedele0231712010-02-24 18:59:10 +01001715 /* When we're shadowing, trap PFs */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001716 if (!npt_enabled)
1717 return NESTED_EXIT_HOST;
1718 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001719 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
1720 nm_interception(svm);
1721 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001722 default:
1723 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001724 }
1725
Joerg Roedel410e4d52009-08-07 11:49:44 +02001726 return NESTED_EXIT_CONTINUE;
1727}
1728
1729/*
1730 * If this function returns true, this #vmexit was already handled
1731 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001732static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001733{
1734 u32 exit_code = svm->vmcb->control.exit_code;
1735 int vmexit = NESTED_EXIT_HOST;
1736
Alexander Grafcf74a782008-11-25 20:17:08 +01001737 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001738 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001739 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001740 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01001741 case SVM_EXIT_IOIO:
1742 vmexit = nested_svm_intercept_ioio(svm);
1743 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001744 case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR8: {
1745 u32 cr_bits = 1 << (exit_code - SVM_EXIT_READ_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001746 if (svm->nested.intercept_cr_read & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001747 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001748 break;
1749 }
1750 case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR8: {
1751 u32 cr_bits = 1 << (exit_code - SVM_EXIT_WRITE_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001752 if (svm->nested.intercept_cr_write & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001753 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001754 break;
1755 }
1756 case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR7: {
1757 u32 dr_bits = 1 << (exit_code - SVM_EXIT_READ_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001758 if (svm->nested.intercept_dr_read & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001759 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001760 break;
1761 }
1762 case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR7: {
1763 u32 dr_bits = 1 << (exit_code - SVM_EXIT_WRITE_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001764 if (svm->nested.intercept_dr_write & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001765 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001766 break;
1767 }
1768 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1769 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001770 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001771 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001772 break;
1773 }
1774 default: {
1775 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001776 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001777 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001778 }
1779 }
1780
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001781 return vmexit;
1782}
1783
1784static int nested_svm_exit_handled(struct vcpu_svm *svm)
1785{
1786 int vmexit;
1787
1788 vmexit = nested_svm_intercept(svm);
1789
1790 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001791 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001792
1793 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001794}
1795
Joerg Roedel0460a972009-08-07 11:49:31 +02001796static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
1797{
1798 struct vmcb_control_area *dst = &dst_vmcb->control;
1799 struct vmcb_control_area *from = &from_vmcb->control;
1800
1801 dst->intercept_cr_read = from->intercept_cr_read;
1802 dst->intercept_cr_write = from->intercept_cr_write;
1803 dst->intercept_dr_read = from->intercept_dr_read;
1804 dst->intercept_dr_write = from->intercept_dr_write;
1805 dst->intercept_exceptions = from->intercept_exceptions;
1806 dst->intercept = from->intercept;
1807 dst->iopm_base_pa = from->iopm_base_pa;
1808 dst->msrpm_base_pa = from->msrpm_base_pa;
1809 dst->tsc_offset = from->tsc_offset;
1810 dst->asid = from->asid;
1811 dst->tlb_ctl = from->tlb_ctl;
1812 dst->int_ctl = from->int_ctl;
1813 dst->int_vector = from->int_vector;
1814 dst->int_state = from->int_state;
1815 dst->exit_code = from->exit_code;
1816 dst->exit_code_hi = from->exit_code_hi;
1817 dst->exit_info_1 = from->exit_info_1;
1818 dst->exit_info_2 = from->exit_info_2;
1819 dst->exit_int_info = from->exit_int_info;
1820 dst->exit_int_info_err = from->exit_int_info_err;
1821 dst->nested_ctl = from->nested_ctl;
1822 dst->event_inj = from->event_inj;
1823 dst->event_inj_err = from->event_inj_err;
1824 dst->nested_cr3 = from->nested_cr3;
1825 dst->lbr_ctl = from->lbr_ctl;
1826}
1827
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001828static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001829{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001830 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001831 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02001832 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01001833 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01001834
Joerg Roedel17897f32009-10-09 16:08:29 +02001835 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
1836 vmcb->control.exit_info_1,
1837 vmcb->control.exit_info_2,
1838 vmcb->control.exit_int_info,
1839 vmcb->control.exit_int_info_err);
1840
Joerg Roedel7597f122010-02-19 16:23:00 +01001841 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001842 if (!nested_vmcb)
1843 return 1;
1844
Joerg Roedel06fc77722010-02-19 16:23:07 +01001845 /* Exit nested SVM mode */
1846 svm->nested.vmcb = 0;
1847
Alexander Grafcf74a782008-11-25 20:17:08 +01001848 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02001849 disable_gif(svm);
1850
1851 nested_vmcb->save.es = vmcb->save.es;
1852 nested_vmcb->save.cs = vmcb->save.cs;
1853 nested_vmcb->save.ss = vmcb->save.ss;
1854 nested_vmcb->save.ds = vmcb->save.ds;
1855 nested_vmcb->save.gdtr = vmcb->save.gdtr;
1856 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001857 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02001858 if (npt_enabled)
1859 nested_vmcb->save.cr3 = vmcb->save.cr3;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001860 else
1861 nested_vmcb->save.cr3 = svm->vcpu.arch.cr3;
Joerg Roedel33740e42009-08-07 11:49:29 +02001862 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001863 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Joerg Roedel33740e42009-08-07 11:49:29 +02001864 nested_vmcb->save.rflags = vmcb->save.rflags;
1865 nested_vmcb->save.rip = vmcb->save.rip;
1866 nested_vmcb->save.rsp = vmcb->save.rsp;
1867 nested_vmcb->save.rax = vmcb->save.rax;
1868 nested_vmcb->save.dr7 = vmcb->save.dr7;
1869 nested_vmcb->save.dr6 = vmcb->save.dr6;
1870 nested_vmcb->save.cpl = vmcb->save.cpl;
1871
1872 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
1873 nested_vmcb->control.int_vector = vmcb->control.int_vector;
1874 nested_vmcb->control.int_state = vmcb->control.int_state;
1875 nested_vmcb->control.exit_code = vmcb->control.exit_code;
1876 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
1877 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
1878 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
1879 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
1880 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Alexander Graf8d23c462009-10-09 16:08:25 +02001881
1882 /*
1883 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
1884 * to make sure that we do not lose injected events. So check event_inj
1885 * here and copy it to exit_int_info if it is valid.
1886 * Exit_int_info and event_inj can't be both valid because the case
1887 * below only happens on a VMRUN instruction intercept which has
1888 * no valid exit_int_info set.
1889 */
1890 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
1891 struct vmcb_control_area *nc = &nested_vmcb->control;
1892
1893 nc->exit_int_info = vmcb->control.event_inj;
1894 nc->exit_int_info_err = vmcb->control.event_inj_err;
1895 }
1896
Joerg Roedel33740e42009-08-07 11:49:29 +02001897 nested_vmcb->control.tlb_ctl = 0;
1898 nested_vmcb->control.event_inj = 0;
1899 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001900
1901 /* We always set V_INTR_MASKING and remember the old value in hflags */
1902 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
1903 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
1904
Alexander Grafcf74a782008-11-25 20:17:08 +01001905 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02001906 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01001907
Alexander Graf219b65d2009-06-15 15:21:25 +02001908 kvm_clear_exception_queue(&svm->vcpu);
1909 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01001910
1911 /* Restore selected save entries */
1912 svm->vmcb->save.es = hsave->save.es;
1913 svm->vmcb->save.cs = hsave->save.cs;
1914 svm->vmcb->save.ss = hsave->save.ss;
1915 svm->vmcb->save.ds = hsave->save.ds;
1916 svm->vmcb->save.gdtr = hsave->save.gdtr;
1917 svm->vmcb->save.idtr = hsave->save.idtr;
1918 svm->vmcb->save.rflags = hsave->save.rflags;
1919 svm_set_efer(&svm->vcpu, hsave->save.efer);
1920 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
1921 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
1922 if (npt_enabled) {
1923 svm->vmcb->save.cr3 = hsave->save.cr3;
1924 svm->vcpu.arch.cr3 = hsave->save.cr3;
1925 } else {
1926 kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
1927 }
1928 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
1929 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
1930 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
1931 svm->vmcb->save.dr7 = 0;
1932 svm->vmcb->save.cpl = 0;
1933 svm->vmcb->control.exit_int_info = 0;
1934
Joerg Roedel7597f122010-02-19 16:23:00 +01001935 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01001936
1937 kvm_mmu_reset_context(&svm->vcpu);
1938 kvm_mmu_load(&svm->vcpu);
1939
1940 return 0;
1941}
Alexander Graf3d6368e2008-11-25 20:17:07 +01001942
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001943static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01001944{
Joerg Roedel323c3d82010-03-01 15:34:37 +01001945 /*
1946 * This function merges the msr permission bitmaps of kvm and the
1947 * nested vmcb. It is omptimized in that it only merges the parts where
1948 * the kvm msr permission bitmap may contain zero bits
1949 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01001950 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001951
Joerg Roedel323c3d82010-03-01 15:34:37 +01001952 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
1953 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001954
Joerg Roedel323c3d82010-03-01 15:34:37 +01001955 for (i = 0; i < MSRPM_OFFSETS; i++) {
1956 u32 value, p;
1957 u64 offset;
1958
1959 if (msrpm_offsets[i] == 0xffffffff)
1960 break;
1961
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001962 p = msrpm_offsets[i];
1963 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01001964
1965 if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4))
1966 return false;
1967
1968 svm->nested.msrpm[p] = svm->msrpm[p] | value;
1969 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001970
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001971 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001972
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001973 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001974}
1975
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001976static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01001977{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001978 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001979 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02001980 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01001981 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01001982 u64 vmcb_gpa;
1983
1984 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001985
Joerg Roedel7597f122010-02-19 16:23:00 +01001986 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001987 if (!nested_vmcb)
1988 return false;
1989
Joerg Roedelecf14052010-02-24 18:59:13 +01001990 trace_kvm_nested_vmrun(svm->vmcb->save.rip - 3, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02001991 nested_vmcb->save.rip,
1992 nested_vmcb->control.int_ctl,
1993 nested_vmcb->control.event_inj,
1994 nested_vmcb->control.nested_ctl);
1995
Joerg Roedel2e554e82010-02-24 18:59:14 +01001996 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr_read,
1997 nested_vmcb->control.intercept_cr_write,
1998 nested_vmcb->control.intercept_exceptions,
1999 nested_vmcb->control.intercept);
2000
Alexander Graf3d6368e2008-11-25 20:17:07 +01002001 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02002002 kvm_clear_exception_queue(&svm->vcpu);
2003 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002004
Joerg Roedele0231712010-02-24 18:59:10 +01002005 /*
2006 * Save the old vmcb, so we don't need to pick what we save, but can
2007 * restore everything when a VMEXIT occurs
2008 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02002009 hsave->save.es = vmcb->save.es;
2010 hsave->save.cs = vmcb->save.cs;
2011 hsave->save.ss = vmcb->save.ss;
2012 hsave->save.ds = vmcb->save.ds;
2013 hsave->save.gdtr = vmcb->save.gdtr;
2014 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02002015 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002016 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002017 hsave->save.cr4 = svm->vcpu.arch.cr4;
2018 hsave->save.rflags = vmcb->save.rflags;
2019 hsave->save.rip = svm->next_rip;
2020 hsave->save.rsp = vmcb->save.rsp;
2021 hsave->save.rax = vmcb->save.rax;
2022 if (npt_enabled)
2023 hsave->save.cr3 = vmcb->save.cr3;
2024 else
2025 hsave->save.cr3 = svm->vcpu.arch.cr3;
2026
Joerg Roedel0460a972009-08-07 11:49:31 +02002027 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002028
2029 if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
2030 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2031 else
2032 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2033
2034 /* Load the nested guest state */
2035 svm->vmcb->save.es = nested_vmcb->save.es;
2036 svm->vmcb->save.cs = nested_vmcb->save.cs;
2037 svm->vmcb->save.ss = nested_vmcb->save.ss;
2038 svm->vmcb->save.ds = nested_vmcb->save.ds;
2039 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2040 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
2041 svm->vmcb->save.rflags = nested_vmcb->save.rflags;
2042 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2043 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2044 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2045 if (npt_enabled) {
2046 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2047 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002048 } else
Alexander Graf3d6368e2008-11-25 20:17:07 +01002049 kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002050
2051 /* Guest paging mode is active - reset mmu */
2052 kvm_mmu_reset_context(&svm->vcpu);
2053
Joerg Roedeldefbba52009-08-07 11:49:30 +02002054 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002055 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2056 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2057 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01002058
Alexander Graf3d6368e2008-11-25 20:17:07 +01002059 /* In case we don't even reach vcpu_run, the fields are not updated */
2060 svm->vmcb->save.rax = nested_vmcb->save.rax;
2061 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2062 svm->vmcb->save.rip = nested_vmcb->save.rip;
2063 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2064 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2065 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2066
Joerg Roedelf7138532010-03-01 15:34:40 +01002067 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002068 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002069
Joerg Roedelaad42c62009-08-07 11:49:34 +02002070 /* cache intercepts */
2071 svm->nested.intercept_cr_read = nested_vmcb->control.intercept_cr_read;
2072 svm->nested.intercept_cr_write = nested_vmcb->control.intercept_cr_write;
2073 svm->nested.intercept_dr_read = nested_vmcb->control.intercept_dr_read;
2074 svm->nested.intercept_dr_write = nested_vmcb->control.intercept_dr_write;
2075 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2076 svm->nested.intercept = nested_vmcb->control.intercept;
2077
Alexander Graf3d6368e2008-11-25 20:17:07 +01002078 force_new_asid(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002079 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002080 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2081 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2082 else
2083 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2084
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002085 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2086 /* We only want the cr8 intercept bits of the guest */
2087 svm->vmcb->control.intercept_cr_read &= ~INTERCEPT_CR8_MASK;
2088 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
2089 }
2090
Joerg Roedele0231712010-02-24 18:59:10 +01002091 /*
2092 * We don't want a nested guest to be more powerful than the guest, so
2093 * all intercepts are ORed
2094 */
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002095 svm->vmcb->control.intercept_cr_read |=
2096 nested_vmcb->control.intercept_cr_read;
2097 svm->vmcb->control.intercept_cr_write |=
2098 nested_vmcb->control.intercept_cr_write;
2099 svm->vmcb->control.intercept_dr_read |=
2100 nested_vmcb->control.intercept_dr_read;
2101 svm->vmcb->control.intercept_dr_write |=
2102 nested_vmcb->control.intercept_dr_write;
2103 svm->vmcb->control.intercept_exceptions |=
2104 nested_vmcb->control.intercept_exceptions;
2105
2106 svm->vmcb->control.intercept |= nested_vmcb->control.intercept;
2107
2108 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002109 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2110 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2111 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002112 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2113 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2114
Joerg Roedel7597f122010-02-19 16:23:00 +01002115 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002116
Joerg Roedel06fc77722010-02-19 16:23:07 +01002117 /* nested_vmcb is our indicator if nested SVM is activated */
2118 svm->nested.vmcb = vmcb_gpa;
2119
Joerg Roedel2af91942009-08-07 11:49:28 +02002120 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002121
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002122 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002123}
2124
Joerg Roedel9966bf62009-08-07 11:49:40 +02002125static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002126{
2127 to_vmcb->save.fs = from_vmcb->save.fs;
2128 to_vmcb->save.gs = from_vmcb->save.gs;
2129 to_vmcb->save.tr = from_vmcb->save.tr;
2130 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2131 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2132 to_vmcb->save.star = from_vmcb->save.star;
2133 to_vmcb->save.lstar = from_vmcb->save.lstar;
2134 to_vmcb->save.cstar = from_vmcb->save.cstar;
2135 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2136 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2137 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2138 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002139}
2140
Avi Kivity851ba692009-08-24 11:10:17 +03002141static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002142{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002143 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002144 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002145
Alexander Graf55426752008-11-25 20:17:06 +01002146 if (nested_svm_check_permissions(svm))
2147 return 1;
2148
2149 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2150 skip_emulated_instruction(&svm->vcpu);
2151
Joerg Roedel7597f122010-02-19 16:23:00 +01002152 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002153 if (!nested_vmcb)
2154 return 1;
2155
2156 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002157 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002158
2159 return 1;
2160}
2161
Avi Kivity851ba692009-08-24 11:10:17 +03002162static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002163{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002164 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002165 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002166
Alexander Graf55426752008-11-25 20:17:06 +01002167 if (nested_svm_check_permissions(svm))
2168 return 1;
2169
2170 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2171 skip_emulated_instruction(&svm->vcpu);
2172
Joerg Roedel7597f122010-02-19 16:23:00 +01002173 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002174 if (!nested_vmcb)
2175 return 1;
2176
2177 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002178 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002179
2180 return 1;
2181}
2182
Avi Kivity851ba692009-08-24 11:10:17 +03002183static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002184{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002185 if (nested_svm_check_permissions(svm))
2186 return 1;
2187
2188 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2189 skip_emulated_instruction(&svm->vcpu);
2190
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002191 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002192 return 1;
2193
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002194 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002195 goto failed;
2196
2197 return 1;
2198
2199failed:
2200
2201 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2202 svm->vmcb->control.exit_code_hi = 0;
2203 svm->vmcb->control.exit_info_1 = 0;
2204 svm->vmcb->control.exit_info_2 = 0;
2205
2206 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002207
2208 return 1;
2209}
2210
Avi Kivity851ba692009-08-24 11:10:17 +03002211static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002212{
2213 if (nested_svm_check_permissions(svm))
2214 return 1;
2215
2216 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2217 skip_emulated_instruction(&svm->vcpu);
2218
Joerg Roedel2af91942009-08-07 11:49:28 +02002219 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002220
2221 return 1;
2222}
2223
Avi Kivity851ba692009-08-24 11:10:17 +03002224static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002225{
2226 if (nested_svm_check_permissions(svm))
2227 return 1;
2228
2229 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2230 skip_emulated_instruction(&svm->vcpu);
2231
Joerg Roedel2af91942009-08-07 11:49:28 +02002232 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002233
2234 /* After a CLGI no interrupts should come */
2235 svm_clear_vintr(svm);
2236 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2237
2238 return 1;
2239}
2240
Avi Kivity851ba692009-08-24 11:10:17 +03002241static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002242{
2243 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002244
Joerg Roedelec1ff792009-10-09 16:08:31 +02002245 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
2246 vcpu->arch.regs[VCPU_REGS_RAX]);
2247
Alexander Grafff092382009-06-15 15:21:24 +02002248 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2249 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
2250
2251 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2252 skip_emulated_instruction(&svm->vcpu);
2253 return 1;
2254}
2255
Joerg Roedel532a46b2009-10-09 16:08:32 +02002256static int skinit_interception(struct vcpu_svm *svm)
2257{
2258 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
2259
2260 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2261 return 1;
2262}
2263
Avi Kivity851ba692009-08-24 11:10:17 +03002264static int invalid_op_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002265{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002266 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002267 return 1;
2268}
2269
Avi Kivity851ba692009-08-24 11:10:17 +03002270static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002271{
Izik Eidus37817f22008-03-24 23:14:53 +02002272 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002273 int reason;
2274 int int_type = svm->vmcb->control.exit_int_info &
2275 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002276 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002277 uint32_t type =
2278 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2279 uint32_t idt_v =
2280 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Izik Eidus37817f22008-03-24 23:14:53 +02002281
2282 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002283
Izik Eidus37817f22008-03-24 23:14:53 +02002284 if (svm->vmcb->control.exit_info_2 &
2285 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002286 reason = TASK_SWITCH_IRET;
2287 else if (svm->vmcb->control.exit_info_2 &
2288 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2289 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002290 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002291 reason = TASK_SWITCH_GATE;
2292 else
2293 reason = TASK_SWITCH_CALL;
2294
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002295 if (reason == TASK_SWITCH_GATE) {
2296 switch (type) {
2297 case SVM_EXITINTINFO_TYPE_NMI:
2298 svm->vcpu.arch.nmi_injected = false;
2299 break;
2300 case SVM_EXITINTINFO_TYPE_EXEPT:
2301 kvm_clear_exception_queue(&svm->vcpu);
2302 break;
2303 case SVM_EXITINTINFO_TYPE_INTR:
2304 kvm_clear_interrupt_queue(&svm->vcpu);
2305 break;
2306 default:
2307 break;
2308 }
2309 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002310
Gleb Natapov8317c292009-04-12 13:37:02 +03002311 if (reason != TASK_SWITCH_GATE ||
2312 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2313 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002314 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2315 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002316
2317 return kvm_task_switch(&svm->vcpu, tss_selector, reason);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002318}
2319
Avi Kivity851ba692009-08-24 11:10:17 +03002320static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002322 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002323 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002324 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002325}
2326
Avi Kivity851ba692009-08-24 11:10:17 +03002327static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002328{
2329 ++svm->vcpu.stat.nmi_window_exits;
2330 svm->vmcb->control.intercept &= ~(1UL << INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002331 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002332 return 1;
2333}
2334
Avi Kivity851ba692009-08-24 11:10:17 +03002335static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002336{
Avi Kivity851ba692009-08-24 11:10:17 +03002337 if (emulate_instruction(&svm->vcpu, 0, 0, 0) != EMULATE_DONE)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002338 pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
2339 return 1;
2340}
2341
Avi Kivity851ba692009-08-24 11:10:17 +03002342static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002343{
Avi Kivity851ba692009-08-24 11:10:17 +03002344 if (emulate_instruction(&svm->vcpu, 0, 0, 0) != EMULATE_DONE)
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002345 pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002346 return 1;
2347}
2348
Avi Kivity851ba692009-08-24 11:10:17 +03002349static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01002350{
Avi Kivity851ba692009-08-24 11:10:17 +03002351 struct kvm_run *kvm_run = svm->vcpu.run;
2352
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002353 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
2354 /* instruction emulation calls kvm_set_cr8() */
Avi Kivity851ba692009-08-24 11:10:17 +03002355 emulate_instruction(&svm->vcpu, 0, 0, 0);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002356 if (irqchip_in_kernel(svm->vcpu.kvm)) {
2357 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
Joerg Roedel1d075432007-12-06 21:02:25 +01002358 return 1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002359 }
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002360 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
2361 return 1;
Joerg Roedel1d075432007-12-06 21:02:25 +01002362 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2363 return 0;
2364}
2365
Avi Kivity6aa8b732006-12-10 02:21:36 -08002366static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
2367{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002368 struct vcpu_svm *svm = to_svm(vcpu);
2369
Avi Kivity6aa8b732006-12-10 02:21:36 -08002370 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302371 case MSR_IA32_TSC: {
Joerg Roedel20824f32009-09-16 15:24:18 +02002372 u64 tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373
Joerg Roedel20824f32009-09-16 15:24:18 +02002374 if (is_nested(svm))
2375 tsc_offset = svm->nested.hsave->control.tsc_offset;
2376 else
2377 tsc_offset = svm->vmcb->control.tsc_offset;
2378
2379 *data = tsc_offset + native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002380 break;
2381 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08002382 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002383 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002384 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08002385#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002386 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002387 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002388 break;
2389 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002390 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002391 break;
2392 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002393 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002394 break;
2395 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002396 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002397 break;
2398#endif
2399 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002400 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002401 break;
2402 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002403 *data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002404 break;
2405 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002406 *data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002407 break;
Joerg Roedele0231712010-02-24 18:59:10 +01002408 /*
2409 * Nobody will change the following 5 values in the VMCB so we can
2410 * safely return them on rdmsr. They will always be 0 until LBRV is
2411 * implemented.
2412 */
Joerg Roedela2938c82008-02-13 16:30:28 +01002413 case MSR_IA32_DEBUGCTLMSR:
2414 *data = svm->vmcb->save.dbgctl;
2415 break;
2416 case MSR_IA32_LASTBRANCHFROMIP:
2417 *data = svm->vmcb->save.br_from;
2418 break;
2419 case MSR_IA32_LASTBRANCHTOIP:
2420 *data = svm->vmcb->save.br_to;
2421 break;
2422 case MSR_IA32_LASTINTFROMIP:
2423 *data = svm->vmcb->save.last_excp_from;
2424 break;
2425 case MSR_IA32_LASTINTTOIP:
2426 *data = svm->vmcb->save.last_excp_to;
2427 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002428 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002429 *data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002430 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002431 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002432 *data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002433 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01002434 case MSR_IA32_UCODE_REV:
2435 *data = 0x01000065;
2436 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002437 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002438 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002439 }
2440 return 0;
2441}
2442
Avi Kivity851ba692009-08-24 11:10:17 +03002443static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002444{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002445 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002446 u64 data;
2447
Avi Kivity59200272010-01-25 19:47:02 +02002448 if (svm_get_msr(&svm->vcpu, ecx, &data)) {
2449 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002450 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002451 } else {
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002452 trace_kvm_msr_read(ecx, data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002453
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002454 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002455 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002456 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002457 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002458 }
2459 return 1;
2460}
2461
Joerg Roedel4a810182010-02-24 18:59:15 +01002462static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2463{
2464 struct vcpu_svm *svm = to_svm(vcpu);
2465 int svm_dis, chg_mask;
2466
2467 if (data & ~SVM_VM_CR_VALID_MASK)
2468 return 1;
2469
2470 chg_mask = SVM_VM_CR_VALID_MASK;
2471
2472 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2473 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2474
2475 svm->nested.vm_cr_msr &= ~chg_mask;
2476 svm->nested.vm_cr_msr |= (data & chg_mask);
2477
2478 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2479
2480 /* check for svm_disable while efer.svme is set */
2481 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2482 return 1;
2483
2484 return 0;
2485}
2486
Avi Kivity6aa8b732006-12-10 02:21:36 -08002487static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
2488{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002489 struct vcpu_svm *svm = to_svm(vcpu);
2490
Avi Kivity6aa8b732006-12-10 02:21:36 -08002491 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302492 case MSR_IA32_TSC: {
Joerg Roedel20824f32009-09-16 15:24:18 +02002493 u64 tsc_offset = data - native_read_tsc();
2494 u64 g_tsc_offset = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002495
Joerg Roedel20824f32009-09-16 15:24:18 +02002496 if (is_nested(svm)) {
2497 g_tsc_offset = svm->vmcb->control.tsc_offset -
2498 svm->nested.hsave->control.tsc_offset;
2499 svm->nested.hsave->control.tsc_offset = tsc_offset;
2500 }
2501
2502 svm->vmcb->control.tsc_offset = tsc_offset + g_tsc_offset;
2503
Avi Kivity6aa8b732006-12-10 02:21:36 -08002504 break;
2505 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08002506 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002507 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002508 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08002509#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002510 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002511 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002512 break;
2513 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002514 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002515 break;
2516 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002517 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002518 break;
2519 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002520 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002521 break;
2522#endif
2523 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002524 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002525 break;
2526 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002527 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002528 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002529 break;
2530 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002531 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002532 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002533 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002534 case MSR_IA32_DEBUGCTLMSR:
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002535 if (!svm_has(SVM_FEATURE_LBRV)) {
2536 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002537 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002538 break;
2539 }
2540 if (data & DEBUGCTL_RESERVED_BITS)
2541 return 1;
2542
2543 svm->vmcb->save.dbgctl = data;
2544 if (data & (1ULL<<0))
2545 svm_enable_lbrv(svm);
2546 else
2547 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01002548 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002549 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002550 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002551 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002552 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002553 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002554 case MSR_VM_IGNNE:
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002555 pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
2556 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002557 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002558 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002559 }
2560 return 0;
2561}
2562
Avi Kivity851ba692009-08-24 11:10:17 +03002563static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002564{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002565 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002566 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002567 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002568
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002569
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002570 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Avi Kivity59200272010-01-25 19:47:02 +02002571 if (svm_set_msr(&svm->vcpu, ecx, data)) {
2572 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002573 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002574 } else {
2575 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10002576 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02002577 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002578 return 1;
2579}
2580
Avi Kivity851ba692009-08-24 11:10:17 +03002581static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002582{
Rusty Russelle756fc62007-07-30 20:07:08 +10002583 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03002584 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002585 else
Avi Kivity851ba692009-08-24 11:10:17 +03002586 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002587}
2588
Avi Kivity851ba692009-08-24 11:10:17 +03002589static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08002590{
Avi Kivity851ba692009-08-24 11:10:17 +03002591 struct kvm_run *kvm_run = svm->vcpu.run;
2592
Alexander Graff0b85052008-11-25 20:17:01 +01002593 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03002594 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -08002595 /*
2596 * If the user space waits to inject interrupts, exit as soon as
2597 * possible
2598 */
Gleb Natapov80618232009-04-21 17:44:56 +03002599 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
2600 kvm_run->request_interrupt_window &&
2601 !kvm_cpu_has_interrupt(&svm->vcpu)) {
Rusty Russelle756fc62007-07-30 20:07:08 +10002602 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002603 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
2604 return 0;
2605 }
2606
2607 return 1;
2608}
2609
Mark Langsdorf565d0992009-10-06 14:25:02 -05002610static int pause_interception(struct vcpu_svm *svm)
2611{
2612 kvm_vcpu_on_spin(&(svm->vcpu));
2613 return 1;
2614}
2615
Avi Kivity851ba692009-08-24 11:10:17 +03002616static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
Joerg Roedele0231712010-02-24 18:59:10 +01002617 [SVM_EXIT_READ_CR0] = emulate_on_interception,
2618 [SVM_EXIT_READ_CR3] = emulate_on_interception,
2619 [SVM_EXIT_READ_CR4] = emulate_on_interception,
2620 [SVM_EXIT_READ_CR8] = emulate_on_interception,
Avi Kivityd2251572010-01-06 10:55:27 +02002621 [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002622 [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
2623 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
2624 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
2625 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
2626 [SVM_EXIT_READ_DR0] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002627 [SVM_EXIT_READ_DR1] = emulate_on_interception,
2628 [SVM_EXIT_READ_DR2] = emulate_on_interception,
2629 [SVM_EXIT_READ_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002630 [SVM_EXIT_READ_DR4] = emulate_on_interception,
2631 [SVM_EXIT_READ_DR5] = emulate_on_interception,
2632 [SVM_EXIT_READ_DR6] = emulate_on_interception,
2633 [SVM_EXIT_READ_DR7] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002634 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
2635 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
2636 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
2637 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002638 [SVM_EXIT_WRITE_DR4] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002639 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002640 [SVM_EXIT_WRITE_DR6] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002641 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002642 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
2643 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002644 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002645 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
2646 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
2647 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
2648 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02002649 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002650 [SVM_EXIT_SMI] = nop_on_interception,
2651 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08002652 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002653 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002654 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002655 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05002656 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002657 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03002658 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02002659 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002660 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002661 [SVM_EXIT_MSR] = msr_interception,
2662 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002663 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01002664 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02002665 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01002666 [SVM_EXIT_VMLOAD] = vmload_interception,
2667 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01002668 [SVM_EXIT_STGI] = stgi_interception,
2669 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02002670 [SVM_EXIT_SKINIT] = skinit_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002671 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01002672 [SVM_EXIT_MONITOR] = invalid_op_interception,
2673 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002674 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002675};
2676
Avi Kivity851ba692009-08-24 11:10:17 +03002677static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002678{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002679 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002680 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002681 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002682
Avi Kivity5bfd8b52010-03-11 10:50:44 +02002683 trace_kvm_exit(exit_code, vcpu);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002684
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002685 if (unlikely(svm->nested.exit_required)) {
2686 nested_svm_vmexit(svm);
2687 svm->nested.exit_required = false;
2688
2689 return 1;
2690 }
2691
Alexander Grafcf74a782008-11-25 20:17:08 +01002692 if (is_nested(svm)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02002693 int vmexit;
2694
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02002695 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
2696 svm->vmcb->control.exit_info_1,
2697 svm->vmcb->control.exit_info_2,
2698 svm->vmcb->control.exit_int_info,
2699 svm->vmcb->control.exit_int_info_err);
2700
Joerg Roedel410e4d52009-08-07 11:49:44 +02002701 vmexit = nested_svm_exit_special(svm);
2702
2703 if (vmexit == NESTED_EXIT_CONTINUE)
2704 vmexit = nested_svm_exit_handled(svm);
2705
2706 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01002707 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01002708 }
2709
Joerg Roedela5c38322009-08-07 11:49:32 +02002710 svm_complete_interrupts(svm);
2711
Avi Kivity888f9f32010-01-10 12:14:04 +02002712 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR0_MASK))
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002713 vcpu->arch.cr0 = svm->vmcb->save.cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02002714 if (npt_enabled)
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002715 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002716
2717 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
2718 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2719 kvm_run->fail_entry.hardware_entry_failure_reason
2720 = svm->vmcb->control.exit_code;
2721 return 0;
2722 }
2723
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002724 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002725 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002726 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002727 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
2728 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002729 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002730 exit_code);
2731
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02002732 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08002733 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002734 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03002735 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002736 return 0;
2737 }
2738
Avi Kivity851ba692009-08-24 11:10:17 +03002739 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002740}
2741
2742static void reload_tss(struct kvm_vcpu *vcpu)
2743{
2744 int cpu = raw_smp_processor_id();
2745
Tejun Heo0fe1e002009-10-29 22:34:14 +09002746 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
2747 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002748 load_TR_desc();
2749}
2750
Rusty Russelle756fc62007-07-30 20:07:08 +10002751static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002752{
2753 int cpu = raw_smp_processor_id();
2754
Tejun Heo0fe1e002009-10-29 22:34:14 +09002755 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002756
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002757 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03002758 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09002759 if (svm->asid_generation != sd->asid_generation)
2760 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002761}
2762
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002763static void svm_inject_nmi(struct kvm_vcpu *vcpu)
2764{
2765 struct vcpu_svm *svm = to_svm(vcpu);
2766
2767 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
2768 vcpu->arch.hflags |= HF_NMI_MASK;
2769 svm->vmcb->control.intercept |= (1UL << INTERCEPT_IRET);
2770 ++vcpu->stat.nmi_injections;
2771}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002772
Eddie Dong85f455f2007-07-06 12:20:49 +03002773static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002774{
2775 struct vmcb_control_area *control;
2776
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002777 trace_kvm_inj_virq(irq);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002778
Avi Kivityfa89a812008-09-01 15:57:51 +03002779 ++svm->vcpu.stat.irq_injections;
Rusty Russelle756fc62007-07-30 20:07:08 +10002780 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03002781 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002782 control->int_ctl &= ~V_INTR_PRIO_MASK;
2783 control->int_ctl |= V_IRQ_MASK |
2784 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
2785}
2786
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002787static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03002788{
2789 struct vcpu_svm *svm = to_svm(vcpu);
2790
Joerg Roedel2af91942009-08-07 11:49:28 +02002791 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01002792
Alexander Graf219b65d2009-06-15 15:21:25 +02002793 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
2794 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002795}
2796
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002797static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
2798{
2799 struct vcpu_svm *svm = to_svm(vcpu);
2800
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002801 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
2802 return;
2803
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002804 if (irr == -1)
2805 return;
2806
2807 if (tpr >= irr)
2808 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK;
2809}
2810
2811static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02002812{
2813 struct vcpu_svm *svm = to_svm(vcpu);
2814 struct vmcb *vmcb = svm->vmcb;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002815 return !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
2816 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
Joerg Roedelaaacfc92008-04-16 16:51:18 +02002817}
2818
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002819static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
2820{
2821 struct vcpu_svm *svm = to_svm(vcpu);
2822
2823 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
2824}
2825
2826static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
2827{
2828 struct vcpu_svm *svm = to_svm(vcpu);
2829
2830 if (masked) {
2831 svm->vcpu.arch.hflags |= HF_NMI_MASK;
2832 svm->vmcb->control.intercept |= (1UL << INTERCEPT_IRET);
2833 } else {
2834 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
2835 svm->vmcb->control.intercept &= ~(1UL << INTERCEPT_IRET);
2836 }
2837}
2838
Gleb Natapov78646122009-03-23 12:12:11 +02002839static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
2840{
2841 struct vcpu_svm *svm = to_svm(vcpu);
2842 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02002843 int ret;
2844
2845 if (!gif_set(svm) ||
2846 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
2847 return 0;
2848
2849 ret = !!(vmcb->save.rflags & X86_EFLAGS_IF);
2850
2851 if (is_nested(svm))
2852 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
2853
2854 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02002855}
2856
Gleb Natapov9222be12009-04-23 17:14:37 +03002857static void enable_irq_window(struct kvm_vcpu *vcpu)
2858{
Alexander Graf219b65d2009-06-15 15:21:25 +02002859 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02002860
Joerg Roedele0231712010-02-24 18:59:10 +01002861 /*
2862 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
2863 * 1, because that's a separate STGI/VMRUN intercept. The next time we
2864 * get that intercept, this function will be called again though and
2865 * we'll get the vintr intercept.
2866 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01002867 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02002868 svm_set_vintr(svm);
2869 svm_inject_irq(svm, 0x0);
2870 }
Gleb Natapov9222be12009-04-23 17:14:37 +03002871}
2872
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002873static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002874{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002875 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03002876
Gleb Natapov44c11432009-05-11 13:35:52 +03002877 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
2878 == HF_NMI_MASK)
2879 return; /* IRET will cause a vm exit */
2880
Joerg Roedele0231712010-02-24 18:59:10 +01002881 /*
2882 * Something prevents NMI from been injected. Single step over possible
2883 * problem (IRET or exception injection or interrupt shadow)
2884 */
Joerg Roedel887f5002010-02-24 18:59:12 +01002885 if (gif_set(svm) && nested_svm_nmi(svm)) {
2886 svm->nmi_singlestep = true;
2887 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
2888 update_db_intercept(vcpu);
2889 }
Eddie Dong85f455f2007-07-06 12:20:49 +03002890}
2891
Izik Eiduscbc94022007-10-25 00:29:55 +02002892static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
2893{
2894 return 0;
2895}
2896
Avi Kivityd9e368d2007-06-07 19:18:30 +03002897static void svm_flush_tlb(struct kvm_vcpu *vcpu)
2898{
2899 force_new_asid(vcpu);
2900}
2901
Avi Kivity04d2cc72007-09-10 18:10:54 +03002902static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
2903{
2904}
2905
Joerg Roedeld7bf8222008-04-16 16:51:17 +02002906static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
2907{
2908 struct vcpu_svm *svm = to_svm(vcpu);
2909
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002910 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
2911 return;
2912
Joerg Roedeld7bf8222008-04-16 16:51:17 +02002913 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) {
2914 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03002915 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02002916 }
2917}
2918
Joerg Roedel649d6862008-04-16 16:51:15 +02002919static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
2920{
2921 struct vcpu_svm *svm = to_svm(vcpu);
2922 u64 cr8;
2923
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002924 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
2925 return;
2926
Joerg Roedel649d6862008-04-16 16:51:15 +02002927 cr8 = kvm_get_cr8(vcpu);
2928 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
2929 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
2930}
2931
Gleb Natapov9222be12009-04-23 17:14:37 +03002932static void svm_complete_interrupts(struct vcpu_svm *svm)
2933{
2934 u8 vector;
2935 int type;
2936 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01002937 unsigned int3_injected = svm->int3_injected;
2938
2939 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03002940
Gleb Natapov44c11432009-05-11 13:35:52 +03002941 if (svm->vcpu.arch.hflags & HF_IRET_MASK)
2942 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
2943
Gleb Natapov9222be12009-04-23 17:14:37 +03002944 svm->vcpu.arch.nmi_injected = false;
2945 kvm_clear_exception_queue(&svm->vcpu);
2946 kvm_clear_interrupt_queue(&svm->vcpu);
2947
2948 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
2949 return;
2950
2951 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
2952 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
2953
2954 switch (type) {
2955 case SVM_EXITINTINFO_TYPE_NMI:
2956 svm->vcpu.arch.nmi_injected = true;
2957 break;
2958 case SVM_EXITINTINFO_TYPE_EXEPT:
Alexander Graf219b65d2009-06-15 15:21:25 +02002959 if (is_nested(svm))
2960 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01002961 /*
2962 * In case of software exceptions, do not reinject the vector,
2963 * but re-execute the instruction instead. Rewind RIP first
2964 * if we emulated INT3 before.
2965 */
2966 if (kvm_exception_is_soft(vector)) {
2967 if (vector == BP_VECTOR && int3_injected &&
2968 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
2969 kvm_rip_write(&svm->vcpu,
2970 kvm_rip_read(&svm->vcpu) -
2971 int3_injected);
Gleb Natapov9222be12009-04-23 17:14:37 +03002972 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01002973 }
Gleb Natapov9222be12009-04-23 17:14:37 +03002974 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
2975 u32 err = svm->vmcb->control.exit_int_info_err;
2976 kvm_queue_exception_e(&svm->vcpu, vector, err);
2977
2978 } else
2979 kvm_queue_exception(&svm->vcpu, vector);
2980 break;
2981 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002982 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03002983 break;
2984 default:
2985 break;
2986 }
2987}
2988
Avi Kivity80e31d42008-07-14 14:44:59 +03002989#ifdef CONFIG_X86_64
2990#define R "r"
2991#else
2992#define R "e"
2993#endif
2994
Avi Kivity851ba692009-08-24 11:10:17 +03002995static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002996{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002997 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002998 u16 fs_selector;
2999 u16 gs_selector;
3000 u16 ldt_selector;
Avi Kivityd9e368d2007-06-07 19:18:30 +03003001
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003002 /*
3003 * A vmexit emulation is required before the vcpu can be executed
3004 * again.
3005 */
3006 if (unlikely(svm->nested.exit_required))
3007 return;
3008
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003009 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3010 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3011 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3012
Rusty Russelle756fc62007-07-30 20:07:08 +10003013 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003014
Joerg Roedel649d6862008-04-16 16:51:15 +02003015 sync_lapic_to_cr8(vcpu);
3016
Avi Kivity6aa8b732006-12-10 02:21:36 -08003017 save_host_msrs(vcpu);
Avi Kivityd6e88ae2008-07-10 16:53:33 +03003018 fs_selector = kvm_read_fs();
3019 gs_selector = kvm_read_gs();
3020 ldt_selector = kvm_read_ldt();
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003021 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003022 /* required for live migration with NPT */
3023 if (npt_enabled)
3024 svm->vmcb->save.cr3 = vcpu->arch.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003025
Avi Kivity04d2cc72007-09-10 18:10:54 +03003026 clgi();
3027
3028 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08003029
Avi Kivity6aa8b732006-12-10 02:21:36 -08003030 asm volatile (
Avi Kivity80e31d42008-07-14 14:44:59 +03003031 "push %%"R"bp; \n\t"
3032 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
3033 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
3034 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
3035 "mov %c[rsi](%[svm]), %%"R"si \n\t"
3036 "mov %c[rdi](%[svm]), %%"R"di \n\t"
3037 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003038#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003039 "mov %c[r8](%[svm]), %%r8 \n\t"
3040 "mov %c[r9](%[svm]), %%r9 \n\t"
3041 "mov %c[r10](%[svm]), %%r10 \n\t"
3042 "mov %c[r11](%[svm]), %%r11 \n\t"
3043 "mov %c[r12](%[svm]), %%r12 \n\t"
3044 "mov %c[r13](%[svm]), %%r13 \n\t"
3045 "mov %c[r14](%[svm]), %%r14 \n\t"
3046 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003047#endif
3048
Avi Kivity6aa8b732006-12-10 02:21:36 -08003049 /* Enter guest mode */
Avi Kivity80e31d42008-07-14 14:44:59 +03003050 "push %%"R"ax \n\t"
3051 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003052 __ex(SVM_VMLOAD) "\n\t"
3053 __ex(SVM_VMRUN) "\n\t"
3054 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity80e31d42008-07-14 14:44:59 +03003055 "pop %%"R"ax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003056
3057 /* Save guest registers, load host registers */
Avi Kivity80e31d42008-07-14 14:44:59 +03003058 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
3059 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
3060 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
3061 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
3062 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
3063 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003064#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003065 "mov %%r8, %c[r8](%[svm]) \n\t"
3066 "mov %%r9, %c[r9](%[svm]) \n\t"
3067 "mov %%r10, %c[r10](%[svm]) \n\t"
3068 "mov %%r11, %c[r11](%[svm]) \n\t"
3069 "mov %%r12, %c[r12](%[svm]) \n\t"
3070 "mov %%r13, %c[r13](%[svm]) \n\t"
3071 "mov %%r14, %c[r14](%[svm]) \n\t"
3072 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003073#endif
Avi Kivity80e31d42008-07-14 14:44:59 +03003074 "pop %%"R"bp"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003075 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003076 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003077 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003078 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
3079 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
3080 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
3081 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
3082 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
3083 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003084#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003085 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
3086 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
3087 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
3088 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
3089 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
3090 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
3091 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
3092 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003093#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02003094 : "cc", "memory"
Avi Kivity80e31d42008-07-14 14:44:59 +03003095 , R"bx", R"cx", R"dx", R"si", R"di"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003096#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02003097 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
3098#endif
3099 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003100
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003101 vcpu->arch.cr2 = svm->vmcb->save.cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003102 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3103 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3104 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003105
Avi Kivityd6e88ae2008-07-10 16:53:33 +03003106 kvm_load_fs(fs_selector);
3107 kvm_load_gs(gs_selector);
3108 kvm_load_ldt(ldt_selector);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003109 load_host_msrs(vcpu);
3110
3111 reload_tss(vcpu);
3112
Avi Kivity56ba47d2007-11-07 17:14:18 +02003113 local_irq_disable();
3114
3115 stgi();
3116
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003117 sync_cr8_to_lapic(vcpu);
3118
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003119 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003120
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003121 if (npt_enabled) {
3122 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3123 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3124 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003125}
3126
Avi Kivity80e31d42008-07-14 14:44:59 +03003127#undef R
3128
Avi Kivity6aa8b732006-12-10 02:21:36 -08003129static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3130{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003131 struct vcpu_svm *svm = to_svm(vcpu);
3132
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003133 if (npt_enabled) {
3134 svm->vmcb->control.nested_cr3 = root;
3135 force_new_asid(vcpu);
3136 return;
3137 }
3138
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003139 svm->vmcb->save.cr3 = root;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003140 force_new_asid(vcpu);
3141}
3142
Avi Kivity6aa8b732006-12-10 02:21:36 -08003143static int is_disabled(void)
3144{
Joerg Roedel6031a612007-06-22 12:29:50 +03003145 u64 vm_cr;
3146
3147 rdmsrl(MSR_VM_CR, vm_cr);
3148 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3149 return 1;
3150
Avi Kivity6aa8b732006-12-10 02:21:36 -08003151 return 0;
3152}
3153
Ingo Molnar102d8322007-02-19 14:37:47 +02003154static void
3155svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3156{
3157 /*
3158 * Patch in the VMMCALL instruction:
3159 */
3160 hypercall[0] = 0x0f;
3161 hypercall[1] = 0x01;
3162 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003163}
3164
Yang, Sheng002c7f72007-07-31 14:23:01 +03003165static void svm_check_processor_compat(void *rtn)
3166{
3167 *(int *)rtn = 0;
3168}
3169
Avi Kivity774ead32007-12-26 13:57:04 +02003170static bool svm_cpu_has_accelerated_tpr(void)
3171{
3172 return false;
3173}
3174
Sheng Yang67253af2008-04-25 10:20:22 +08003175static int get_npt_level(void)
3176{
3177#ifdef CONFIG_X86_64
3178 return PT64_ROOT_LEVEL;
3179#else
3180 return PT32E_ROOT_LEVEL;
3181#endif
3182}
3183
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003184static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003185{
3186 return 0;
3187}
3188
Sheng Yang0e851882009-12-18 16:48:46 +08003189static void svm_cpuid_update(struct kvm_vcpu *vcpu)
3190{
3191}
3192
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003193static const struct trace_print_flags svm_exit_reasons_str[] = {
Joerg Roedele0231712010-02-24 18:59:10 +01003194 { SVM_EXIT_READ_CR0, "read_cr0" },
3195 { SVM_EXIT_READ_CR3, "read_cr3" },
3196 { SVM_EXIT_READ_CR4, "read_cr4" },
3197 { SVM_EXIT_READ_CR8, "read_cr8" },
3198 { SVM_EXIT_WRITE_CR0, "write_cr0" },
3199 { SVM_EXIT_WRITE_CR3, "write_cr3" },
3200 { SVM_EXIT_WRITE_CR4, "write_cr4" },
3201 { SVM_EXIT_WRITE_CR8, "write_cr8" },
3202 { SVM_EXIT_READ_DR0, "read_dr0" },
3203 { SVM_EXIT_READ_DR1, "read_dr1" },
3204 { SVM_EXIT_READ_DR2, "read_dr2" },
3205 { SVM_EXIT_READ_DR3, "read_dr3" },
3206 { SVM_EXIT_WRITE_DR0, "write_dr0" },
3207 { SVM_EXIT_WRITE_DR1, "write_dr1" },
3208 { SVM_EXIT_WRITE_DR2, "write_dr2" },
3209 { SVM_EXIT_WRITE_DR3, "write_dr3" },
3210 { SVM_EXIT_WRITE_DR5, "write_dr5" },
3211 { SVM_EXIT_WRITE_DR7, "write_dr7" },
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003212 { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
3213 { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
3214 { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
3215 { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
3216 { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
3217 { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
3218 { SVM_EXIT_INTR, "interrupt" },
3219 { SVM_EXIT_NMI, "nmi" },
3220 { SVM_EXIT_SMI, "smi" },
3221 { SVM_EXIT_INIT, "init" },
3222 { SVM_EXIT_VINTR, "vintr" },
3223 { SVM_EXIT_CPUID, "cpuid" },
3224 { SVM_EXIT_INVD, "invd" },
3225 { SVM_EXIT_HLT, "hlt" },
3226 { SVM_EXIT_INVLPG, "invlpg" },
3227 { SVM_EXIT_INVLPGA, "invlpga" },
3228 { SVM_EXIT_IOIO, "io" },
3229 { SVM_EXIT_MSR, "msr" },
3230 { SVM_EXIT_TASK_SWITCH, "task_switch" },
3231 { SVM_EXIT_SHUTDOWN, "shutdown" },
3232 { SVM_EXIT_VMRUN, "vmrun" },
3233 { SVM_EXIT_VMMCALL, "hypercall" },
3234 { SVM_EXIT_VMLOAD, "vmload" },
3235 { SVM_EXIT_VMSAVE, "vmsave" },
3236 { SVM_EXIT_STGI, "stgi" },
3237 { SVM_EXIT_CLGI, "clgi" },
3238 { SVM_EXIT_SKINIT, "skinit" },
3239 { SVM_EXIT_WBINVD, "wbinvd" },
3240 { SVM_EXIT_MONITOR, "monitor" },
3241 { SVM_EXIT_MWAIT, "mwait" },
3242 { SVM_EXIT_NPF, "npf" },
3243 { -1, NULL }
3244};
3245
Sheng Yang17cc3932010-01-05 19:02:27 +08003246static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02003247{
Sheng Yang17cc3932010-01-05 19:02:27 +08003248 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02003249}
3250
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003251static bool svm_rdtscp_supported(void)
3252{
3253 return false;
3254}
3255
Avi Kivity02daab22009-12-30 12:40:26 +02003256static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
3257{
3258 struct vcpu_svm *svm = to_svm(vcpu);
3259
Avi Kivity02daab22009-12-30 12:40:26 +02003260 svm->vmcb->control.intercept_exceptions |= 1 << NM_VECTOR;
Joerg Roedel66a562f2010-02-19 16:23:08 +01003261 if (is_nested(svm))
3262 svm->nested.hsave->control.intercept_exceptions |= 1 << NM_VECTOR;
3263 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02003264}
3265
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003266static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003267 .cpu_has_kvm_support = has_svm,
3268 .disabled_by_bios = is_disabled,
3269 .hardware_setup = svm_hardware_setup,
3270 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003271 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003272 .hardware_enable = svm_hardware_enable,
3273 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003274 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003275
3276 .vcpu_create = svm_create_vcpu,
3277 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003278 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003279
Avi Kivity04d2cc72007-09-10 18:10:54 +03003280 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003281 .vcpu_load = svm_vcpu_load,
3282 .vcpu_put = svm_vcpu_put,
3283
3284 .set_guest_debug = svm_guest_debug,
3285 .get_msr = svm_get_msr,
3286 .set_msr = svm_set_msr,
3287 .get_segment_base = svm_get_segment_base,
3288 .get_segment = svm_get_segment,
3289 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003290 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10003291 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02003292 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003293 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003294 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003295 .set_cr3 = svm_set_cr3,
3296 .set_cr4 = svm_set_cr4,
3297 .set_efer = svm_set_efer,
3298 .get_idt = svm_get_idt,
3299 .set_idt = svm_set_idt,
3300 .get_gdt = svm_get_gdt,
3301 .set_gdt = svm_set_gdt,
3302 .get_dr = svm_get_dr,
3303 .set_dr = svm_set_dr,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003304 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003305 .get_rflags = svm_get_rflags,
3306 .set_rflags = svm_set_rflags,
Avi Kivity6b52d182010-01-21 15:31:47 +02003307 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02003308 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003309
Avi Kivity6aa8b732006-12-10 02:21:36 -08003310 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003311
Avi Kivity6aa8b732006-12-10 02:21:36 -08003312 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003313 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003314 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04003315 .set_interrupt_shadow = svm_set_interrupt_shadow,
3316 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02003317 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003318 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003319 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02003320 .queue_exception = svm_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02003321 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003322 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003323 .get_nmi_mask = svm_get_nmi_mask,
3324 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003325 .enable_nmi_window = enable_nmi_window,
3326 .enable_irq_window = enable_irq_window,
3327 .update_cr8_intercept = update_cr8_intercept,
Izik Eiduscbc94022007-10-25 00:29:55 +02003328
3329 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003330 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003331 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003332
3333 .exit_reasons_str = svm_exit_reasons_str,
Sheng Yang17cc3932010-01-05 19:02:27 +08003334 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08003335
3336 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003337
3338 .rdtscp_supported = svm_rdtscp_supported,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003339};
3340
3341static int __init svm_init(void)
3342{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003343 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Rusty Russellc16f8622007-07-30 21:12:19 +10003344 THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003345}
3346
3347static void __exit svm_exit(void)
3348{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003349 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003350}
3351
3352module_init(svm_init)
3353module_exit(svm_exit)