blob: a079550d3886fbe54cc010858024752a815f50d0 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * AMD SVM support
5 *
6 * Copyright (C) 2006 Qumranet, Inc.
7 *
8 * Authors:
9 * Yaniv Kamay <yaniv@qumranet.com>
10 * Avi Kivity <avi@qumranet.com>
11 *
12 * This work is licensed under the terms of the GNU GPL, version 2. See
13 * the COPYING file in the top-level directory.
14 *
15 */
Avi Kivityedf88412007-12-16 11:02:48 +020016#include <linux/kvm_host.h>
17
Eddie Dong85f455f2007-07-06 12:20:49 +030018#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080019#include "mmu.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030020#include "kvm_cache_regs.h"
Gleb Natapovfe4c7b12009-03-23 11:23:18 +020021#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040022
Avi Kivity6aa8b732006-12-10 02:21:36 -080023#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020024#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/vmalloc.h>
26#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040027#include <linux/sched.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030028#include <linux/ftrace_event.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080030
Avi Kivitye4956062007-06-28 14:15:57 -040031#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080032
Eduardo Habkost63d11422008-11-17 19:03:20 -020033#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030034#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020035
Avi Kivity4ecac3f2008-05-13 13:23:38 +030036#define __ex(x) __kvm_handle_fault_on_reboot(x)
37
Avi Kivity6aa8b732006-12-10 02:21:36 -080038MODULE_AUTHOR("Qumranet");
39MODULE_LICENSE("GPL");
40
41#define IOPM_ALLOC_ORDER 2
42#define MSRPM_ALLOC_ORDER 1
43
Avi Kivity6aa8b732006-12-10 02:21:36 -080044#define SEG_TYPE_LDT 2
45#define SEG_TYPE_BUSY_TSS16 3
46
Joerg Roedel80b77062007-03-30 17:02:14 +030047#define SVM_FEATURE_NPT (1 << 0)
48#define SVM_FEATURE_LBRV (1 << 1)
Amit Shah94c935a12008-08-18 13:11:46 +030049#define SVM_FEATURE_SVML (1 << 2)
Jan Kiszka66b71382010-02-23 17:47:56 +010050#define SVM_FEATURE_NRIP (1 << 3)
Mark Langsdorf565d0992009-10-06 14:25:02 -050051#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030052
Joerg Roedel410e4d52009-08-07 11:49:44 +020053#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
54#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
55#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
56
Joerg Roedel24e09cb2008-02-13 18:58:47 +010057#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
58
Avi Kivity6c8166a2009-05-31 18:15:37 +030059static const u32 host_save_user_msrs[] = {
60#ifdef CONFIG_X86_64
61 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
62 MSR_FS_BASE,
63#endif
64 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
65};
66
67#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
68
69struct kvm_vcpu;
70
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020071struct nested_state {
72 struct vmcb *hsave;
73 u64 hsave_msr;
Joerg Roedel4a810182010-02-24 18:59:15 +010074 u64 vm_cr_msr;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020075 u64 vmcb;
76
77 /* These are the merged vectors */
78 u32 *msrpm;
79
80 /* gpa pointers to the real vectors */
81 u64 vmcb_msrpm;
Joerg Roedelaad42c62009-08-07 11:49:34 +020082
Joerg Roedelcd3ff652009-10-09 16:08:26 +020083 /* A VMEXIT is required but not yet emulated */
84 bool exit_required;
85
Joerg Roedelaad42c62009-08-07 11:49:34 +020086 /* cache for intercepts of the guest */
87 u16 intercept_cr_read;
88 u16 intercept_cr_write;
89 u16 intercept_dr_read;
90 u16 intercept_dr_write;
91 u32 intercept_exceptions;
92 u64 intercept;
93
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020094};
95
Avi Kivity6c8166a2009-05-31 18:15:37 +030096struct vcpu_svm {
97 struct kvm_vcpu vcpu;
98 struct vmcb *vmcb;
99 unsigned long vmcb_pa;
100 struct svm_cpu_data *svm_data;
101 uint64_t asid_generation;
102 uint64_t sysenter_esp;
103 uint64_t sysenter_eip;
104
105 u64 next_rip;
106
107 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
108 u64 host_gs_base;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300109
110 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300111
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200112 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200113
114 bool nmi_singlestep;
Jan Kiszka66b71382010-02-23 17:47:56 +0100115
116 unsigned int3_injected;
117 unsigned long int3_rip;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300118};
119
Joerg Roedel455716f2010-03-01 15:34:35 +0100120#define MSR_INVALID 0xffffffffU
121
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100122static struct svm_direct_access_msrs {
123 u32 index; /* Index of the MSR */
124 bool always; /* True if intercept is always on */
125} direct_access_msrs[] = {
126 { .index = MSR_K6_STAR, .always = true },
127 { .index = MSR_IA32_SYSENTER_CS, .always = true },
128#ifdef CONFIG_X86_64
129 { .index = MSR_GS_BASE, .always = true },
130 { .index = MSR_FS_BASE, .always = true },
131 { .index = MSR_KERNEL_GS_BASE, .always = true },
132 { .index = MSR_LSTAR, .always = true },
133 { .index = MSR_CSTAR, .always = true },
134 { .index = MSR_SYSCALL_MASK, .always = true },
135#endif
136 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
137 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
138 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
139 { .index = MSR_IA32_LASTINTTOIP, .always = false },
140 { .index = MSR_INVALID, .always = false },
141};
142
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100143/* enable NPT for AMD64 and X86 with PAE */
144#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
145static bool npt_enabled = true;
146#else
Joerg Roedele0231712010-02-24 18:59:10 +0100147static bool npt_enabled;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100148#endif
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100149static int npt = 1;
150
151module_param(npt, int, S_IRUGO);
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100152
Joerg Roedel4b6e4dc2009-08-07 11:49:48 +0200153static int nested = 1;
Alexander Graf236de052008-11-25 20:17:10 +0100154module_param(nested, int, S_IRUGO);
155
Joerg Roedel44874f82008-08-27 14:18:43 +0200156static void svm_flush_tlb(struct kvm_vcpu *vcpu);
Joerg Roedela5c38322009-08-07 11:49:32 +0200157static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity04d2cc72007-09-10 18:10:54 +0300158
Joerg Roedel410e4d52009-08-07 11:49:44 +0200159static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100160static int nested_svm_intercept(struct vcpu_svm *svm);
Alexander Grafcf74a782008-11-25 20:17:08 +0100161static int nested_svm_vmexit(struct vcpu_svm *svm);
Alexander Grafcf74a782008-11-25 20:17:08 +0100162static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
163 bool has_error_code, u32 error_code);
164
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400165static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
166{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000167 return container_of(vcpu, struct vcpu_svm, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400168}
169
Alexander Graf3d6368e2008-11-25 20:17:07 +0100170static inline bool is_nested(struct vcpu_svm *svm)
171{
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200172 return svm->nested.vmcb;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100173}
174
Joerg Roedel2af91942009-08-07 11:49:28 +0200175static inline void enable_gif(struct vcpu_svm *svm)
176{
177 svm->vcpu.arch.hflags |= HF_GIF_MASK;
178}
179
180static inline void disable_gif(struct vcpu_svm *svm)
181{
182 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
183}
184
185static inline bool gif_set(struct vcpu_svm *svm)
186{
187 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
188}
189
Harvey Harrison4866d5e2008-02-19 10:32:02 -0800190static unsigned long iopm_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800191
192struct kvm_ldttss_desc {
193 u16 limit0;
194 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100195 unsigned base1:8, type:5, dpl:2, p:1;
196 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800197 u32 base3;
198 u32 zero1;
199} __attribute__((packed));
200
201struct svm_cpu_data {
202 int cpu;
203
Avi Kivity5008fdf2007-04-02 13:05:50 +0300204 u64 asid_generation;
205 u32 max_asid;
206 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800207 struct kvm_ldttss_desc *tss_desc;
208
209 struct page *save_area;
210};
211
212static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
Joerg Roedel80b77062007-03-30 17:02:14 +0300213static uint32_t svm_features;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800214
215struct svm_init_data {
216 int cpu;
217 int r;
218};
219
220static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
221
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200222#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800223#define MSRS_RANGE_SIZE 2048
224#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
225
Joerg Roedel455716f2010-03-01 15:34:35 +0100226static u32 svm_msrpm_offset(u32 msr)
227{
228 u32 offset;
229 int i;
230
231 for (i = 0; i < NUM_MSR_MAPS; i++) {
232 if (msr < msrpm_ranges[i] ||
233 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
234 continue;
235
236 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
237 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
238
239 /* Now we have the u8 offset - but need the u32 offset */
240 return offset / 4;
241 }
242
243 /* MSR not in any range */
244 return MSR_INVALID;
245}
246
Avi Kivity6aa8b732006-12-10 02:21:36 -0800247#define MAX_INST_SIZE 15
248
Joerg Roedel80b77062007-03-30 17:02:14 +0300249static inline u32 svm_has(u32 feat)
250{
251 return svm_features & feat;
252}
253
Avi Kivity6aa8b732006-12-10 02:21:36 -0800254static inline void clgi(void)
255{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300256 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800257}
258
259static inline void stgi(void)
260{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300261 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800262}
263
264static inline void invlpga(unsigned long addr, u32 asid)
265{
Joerg Roedele0231712010-02-24 18:59:10 +0100266 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800267}
268
Avi Kivity6aa8b732006-12-10 02:21:36 -0800269static inline void force_new_asid(struct kvm_vcpu *vcpu)
270{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400271 to_svm(vcpu)->asid_generation--;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800272}
273
274static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
275{
276 force_new_asid(vcpu);
277}
278
279static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
280{
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100281 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600282 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800283
Alexander Graf9962d032008-11-25 20:17:02 +0100284 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Avi Kivityf6801df2010-01-21 15:31:50 +0200285 vcpu->arch.efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800286}
287
Avi Kivity6aa8b732006-12-10 02:21:36 -0800288static int is_external_interrupt(u32 info)
289{
290 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
291 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
292}
293
Glauber Costa2809f5d2009-05-12 16:21:05 -0400294static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
295{
296 struct vcpu_svm *svm = to_svm(vcpu);
297 u32 ret = 0;
298
299 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Jan Kiszka48005f62010-02-19 19:38:07 +0100300 ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400301 return ret & mask;
302}
303
304static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
305{
306 struct vcpu_svm *svm = to_svm(vcpu);
307
308 if (mask == 0)
309 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
310 else
311 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
312
313}
314
Avi Kivity6aa8b732006-12-10 02:21:36 -0800315static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
316{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400317 struct vcpu_svm *svm = to_svm(vcpu);
318
319 if (!svm->next_rip) {
Avi Kivity851ba692009-08-24 11:10:17 +0300320 if (emulate_instruction(vcpu, 0, 0, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300321 EMULATE_DONE)
322 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800323 return;
324 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300325 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
326 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
327 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800328
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300329 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400330 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800331}
332
Jan Kiszka116a4752010-02-23 17:47:54 +0100333static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
334 bool has_error_code, u32 error_code)
335{
336 struct vcpu_svm *svm = to_svm(vcpu);
337
Joerg Roedele0231712010-02-24 18:59:10 +0100338 /*
339 * If we are within a nested VM we'd better #VMEXIT and let the guest
340 * handle the exception
341 */
Jan Kiszka116a4752010-02-23 17:47:54 +0100342 if (nested_svm_check_exception(svm, nr, has_error_code, error_code))
343 return;
344
Jan Kiszka66b71382010-02-23 17:47:56 +0100345 if (nr == BP_VECTOR && !svm_has(SVM_FEATURE_NRIP)) {
346 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
347
348 /*
349 * For guest debugging where we have to reinject #BP if some
350 * INT3 is guest-owned:
351 * Emulate nRIP by moving RIP forward. Will fail if injection
352 * raises a fault that is not intercepted. Still better than
353 * failing in all cases.
354 */
355 skip_emulated_instruction(&svm->vcpu);
356 rip = kvm_rip_read(&svm->vcpu);
357 svm->int3_rip = rip + svm->vmcb->save.cs.base;
358 svm->int3_injected = rip - old_rip;
359 }
360
Jan Kiszka116a4752010-02-23 17:47:54 +0100361 svm->vmcb->control.event_inj = nr
362 | SVM_EVTINJ_VALID
363 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
364 | SVM_EVTINJ_TYPE_EXEPT;
365 svm->vmcb->control.event_inj_err = error_code;
366}
367
Avi Kivity6aa8b732006-12-10 02:21:36 -0800368static int has_svm(void)
369{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200370 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800371
Eduardo Habkost63d11422008-11-17 19:03:20 -0200372 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800373 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800374 return 0;
375 }
376
Avi Kivity6aa8b732006-12-10 02:21:36 -0800377 return 1;
378}
379
380static void svm_hardware_disable(void *garbage)
381{
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200382 cpu_svm_disable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800383}
384
Alexander Graf10474ae2009-09-15 11:37:46 +0200385static int svm_hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800386{
387
Tejun Heo0fe1e002009-10-29 22:34:14 +0900388 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800389 uint64_t efer;
Gleb Natapov89a27f42010-02-16 10:51:48 +0200390 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800391 struct desc_struct *gdt;
392 int me = raw_smp_processor_id();
393
Alexander Graf10474ae2009-09-15 11:37:46 +0200394 rdmsrl(MSR_EFER, efer);
395 if (efer & EFER_SVME)
396 return -EBUSY;
397
Avi Kivity6aa8b732006-12-10 02:21:36 -0800398 if (!has_svm()) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000399 printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
400 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200401 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800402 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900403 sd = per_cpu(svm_data, me);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800404
Tejun Heo0fe1e002009-10-29 22:34:14 +0900405 if (!sd) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000406 printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -0800407 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200408 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800409 }
410
Tejun Heo0fe1e002009-10-29 22:34:14 +0900411 sd->asid_generation = 1;
412 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
413 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800414
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200415 native_store_gdt(&gdt_descr);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200416 gdt = (struct desc_struct *)gdt_descr.address;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900417 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800418
Alexander Graf9962d032008-11-25 20:17:02 +0100419 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800420
Linus Torvaldsd0316552009-12-14 09:58:24 -0800421 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200422
423 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424}
425
Joerg Roedel0da1db752008-07-02 16:02:11 +0200426static void svm_cpu_uninit(int cpu)
427{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900428 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200429
Tejun Heo0fe1e002009-10-29 22:34:14 +0900430 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200431 return;
432
433 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900434 __free_page(sd->save_area);
435 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200436}
437
Avi Kivity6aa8b732006-12-10 02:21:36 -0800438static int svm_cpu_init(int cpu)
439{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900440 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800441 int r;
442
Tejun Heo0fe1e002009-10-29 22:34:14 +0900443 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
444 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800445 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900446 sd->cpu = cpu;
447 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800448 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900449 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800450 goto err_1;
451
Tejun Heo0fe1e002009-10-29 22:34:14 +0900452 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800453
454 return 0;
455
456err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900457 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800458 return r;
459
460}
461
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100462static bool valid_msr_intercept(u32 index)
463{
464 int i;
465
466 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
467 if (direct_access_msrs[i].index == index)
468 return true;
469
470 return false;
471}
472
Rusty Russellbfc733a2007-07-31 20:42:42 +1000473static void set_msr_interception(u32 *msrpm, unsigned msr,
474 int read, int write)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800475{
Joerg Roedel455716f2010-03-01 15:34:35 +0100476 u8 bit_read, bit_write;
477 unsigned long tmp;
478 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800479
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100480 /*
481 * If this warning triggers extend the direct_access_msrs list at the
482 * beginning of the file
483 */
484 WARN_ON(!valid_msr_intercept(msr));
485
Joerg Roedel455716f2010-03-01 15:34:35 +0100486 offset = svm_msrpm_offset(msr);
487 bit_read = 2 * (msr & 0x0f);
488 bit_write = 2 * (msr & 0x0f) + 1;
489 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800490
Joerg Roedel455716f2010-03-01 15:34:35 +0100491 BUG_ON(offset == MSR_INVALID);
492
493 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
494 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
495
496 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800497}
498
Joerg Roedelf65c2292008-02-13 18:58:46 +0100499static void svm_vcpu_init_msrpm(u32 *msrpm)
500{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100501 int i;
502
Joerg Roedelf65c2292008-02-13 18:58:46 +0100503 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
504
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100505 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
506 if (!direct_access_msrs[i].always)
507 continue;
508
509 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
510 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100511}
512
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100513static void svm_enable_lbrv(struct vcpu_svm *svm)
514{
515 u32 *msrpm = svm->msrpm;
516
517 svm->vmcb->control.lbr_ctl = 1;
518 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
519 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
520 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
521 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
522}
523
524static void svm_disable_lbrv(struct vcpu_svm *svm)
525{
526 u32 *msrpm = svm->msrpm;
527
528 svm->vmcb->control.lbr_ctl = 0;
529 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
530 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
531 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
532 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
533}
534
Avi Kivity6aa8b732006-12-10 02:21:36 -0800535static __init int svm_hardware_setup(void)
536{
537 int cpu;
538 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100539 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800540 int r;
541
Avi Kivity6aa8b732006-12-10 02:21:36 -0800542 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
543
544 if (!iopm_pages)
545 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300546
547 iopm_va = page_address(iopm_pages);
548 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800549 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
550
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100551 if (boot_cpu_has(X86_FEATURE_NX))
552 kvm_enable_efer_bits(EFER_NX);
553
Alexander Graf1b2fd702009-02-02 16:23:51 +0100554 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
555 kvm_enable_efer_bits(EFER_FFXSR);
556
Alexander Graf236de052008-11-25 20:17:10 +0100557 if (nested) {
558 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
559 kvm_enable_efer_bits(EFER_SVME);
560 }
561
Zachary Amsden3230bb42009-09-29 11:38:37 -1000562 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800563 r = svm_cpu_init(cpu);
564 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100565 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800566 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100567
568 svm_features = cpuid_edx(SVM_CPUID_FUNC);
569
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100570 if (!svm_has(SVM_FEATURE_NPT))
571 npt_enabled = false;
572
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100573 if (npt_enabled && !npt) {
574 printk(KERN_INFO "kvm: Nested Paging disabled\n");
575 npt_enabled = false;
576 }
577
Joerg Roedel18552672008-02-07 13:47:41 +0100578 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100579 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100580 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200581 } else
582 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100583
Avi Kivity6aa8b732006-12-10 02:21:36 -0800584 return 0;
585
Joerg Roedelf65c2292008-02-13 18:58:46 +0100586err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800587 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
588 iopm_base = 0;
589 return r;
590}
591
592static __exit void svm_hardware_unsetup(void)
593{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200594 int cpu;
595
Zachary Amsden3230bb42009-09-29 11:38:37 -1000596 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200597 svm_cpu_uninit(cpu);
598
Avi Kivity6aa8b732006-12-10 02:21:36 -0800599 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100600 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800601}
602
603static void init_seg(struct vmcb_seg *seg)
604{
605 seg->selector = 0;
606 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +0100607 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800608 seg->limit = 0xffff;
609 seg->base = 0;
610}
611
612static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
613{
614 seg->selector = 0;
615 seg->attrib = SVM_SELECTOR_P_MASK | type;
616 seg->limit = 0xffff;
617 seg->base = 0;
618}
619
Joerg Roedele6101a92008-02-13 18:58:45 +0100620static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800621{
Joerg Roedele6101a92008-02-13 18:58:45 +0100622 struct vmcb_control_area *control = &svm->vmcb->control;
623 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800624
Avi Kivitybff78272010-01-07 13:16:08 +0200625 svm->vcpu.fpu_active = 1;
626
Joerg Roedele0231712010-02-24 18:59:10 +0100627 control->intercept_cr_read = INTERCEPT_CR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800628 INTERCEPT_CR3_MASK |
Joerg Roedel649d6862008-04-16 16:51:15 +0200629 INTERCEPT_CR4_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800630
Joerg Roedele0231712010-02-24 18:59:10 +0100631 control->intercept_cr_write = INTERCEPT_CR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800632 INTERCEPT_CR3_MASK |
Avi Kivity80a81192007-12-06 19:50:00 +0200633 INTERCEPT_CR4_MASK |
634 INTERCEPT_CR8_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800635
Joerg Roedele0231712010-02-24 18:59:10 +0100636 control->intercept_dr_read = INTERCEPT_DR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800637 INTERCEPT_DR1_MASK |
638 INTERCEPT_DR2_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100639 INTERCEPT_DR3_MASK |
640 INTERCEPT_DR4_MASK |
641 INTERCEPT_DR5_MASK |
642 INTERCEPT_DR6_MASK |
643 INTERCEPT_DR7_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800644
Joerg Roedele0231712010-02-24 18:59:10 +0100645 control->intercept_dr_write = INTERCEPT_DR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800646 INTERCEPT_DR1_MASK |
647 INTERCEPT_DR2_MASK |
648 INTERCEPT_DR3_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100649 INTERCEPT_DR4_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800650 INTERCEPT_DR5_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100651 INTERCEPT_DR6_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800652 INTERCEPT_DR7_MASK;
653
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500654 control->intercept_exceptions = (1 << PF_VECTOR) |
Joerg Roedel53371b52008-04-09 14:15:30 +0200655 (1 << UD_VECTOR) |
656 (1 << MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800657
658
Joerg Roedele0231712010-02-24 18:59:10 +0100659 control->intercept = (1ULL << INTERCEPT_INTR) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800660 (1ULL << INTERCEPT_NMI) |
Joerg Roedel01525272007-02-19 14:37:47 +0200661 (1ULL << INTERCEPT_SMI) |
Avi Kivityd2251572010-01-06 10:55:27 +0200662 (1ULL << INTERCEPT_SELECTIVE_CR0) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800663 (1ULL << INTERCEPT_CPUID) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200664 (1ULL << INTERCEPT_INVD) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800665 (1ULL << INTERCEPT_HLT) |
Marcelo Tosattia7052892008-09-23 13:18:35 -0300666 (1ULL << INTERCEPT_INVLPG) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800667 (1ULL << INTERCEPT_INVLPGA) |
668 (1ULL << INTERCEPT_IOIO_PROT) |
669 (1ULL << INTERCEPT_MSR_PROT) |
670 (1ULL << INTERCEPT_TASK_SWITCH) |
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800671 (1ULL << INTERCEPT_SHUTDOWN) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800672 (1ULL << INTERCEPT_VMRUN) |
673 (1ULL << INTERCEPT_VMMCALL) |
674 (1ULL << INTERCEPT_VMLOAD) |
675 (1ULL << INTERCEPT_VMSAVE) |
676 (1ULL << INTERCEPT_STGI) |
677 (1ULL << INTERCEPT_CLGI) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100678 (1ULL << INTERCEPT_SKINIT) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200679 (1ULL << INTERCEPT_WBINVD) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100680 (1ULL << INTERCEPT_MONITOR) |
681 (1ULL << INTERCEPT_MWAIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800682
683 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100684 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity0cc50642007-03-25 12:07:27 +0200685 control->tsc_offset = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800686 control->int_ctl = V_INTR_MASKING_MASK;
687
688 init_seg(&save->es);
689 init_seg(&save->ss);
690 init_seg(&save->ds);
691 init_seg(&save->fs);
692 init_seg(&save->gs);
693
694 save->cs.selector = 0xf000;
695 /* Executable/Readable Code Segment */
696 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
697 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
698 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800699 /*
700 * cs.base should really be 0xffff0000, but vmx can't handle that, so
701 * be consistent with it.
702 *
703 * Replace when we have real mode working for vmx.
704 */
705 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800706
707 save->gdtr.limit = 0xffff;
708 save->idtr.limit = 0xffff;
709
710 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
711 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
712
Alexander Graf9962d032008-11-25 20:17:02 +0100713 save->efer = EFER_SVME;
Mike Dayd77c26f2007-10-08 09:02:08 -0400714 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800715 save->dr7 = 0x400;
716 save->rflags = 2;
717 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300718 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800719
Joerg Roedele0231712010-02-24 18:59:10 +0100720 /*
721 * This is the guest-visible cr0 value.
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200722 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800723 */
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200724 svm->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
725 kvm_set_cr0(&svm->vcpu, svm->vcpu.arch.cr0);
726
Rusty Russell66aee912007-07-17 23:34:16 +1000727 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800728 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100729
730 if (npt_enabled) {
731 /* Setup VMCB for Nested Paging */
732 control->nested_ctl = 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -0300733 control->intercept &= ~((1ULL << INTERCEPT_TASK_SWITCH) |
734 (1ULL << INTERCEPT_INVLPG));
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100735 control->intercept_exceptions &= ~(1 << PF_VECTOR);
Avi Kivity888f9f32010-01-10 12:14:04 +0200736 control->intercept_cr_read &= ~INTERCEPT_CR3_MASK;
737 control->intercept_cr_write &= ~INTERCEPT_CR3_MASK;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100738 save->g_pat = 0x0007040600070406ULL;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100739 save->cr3 = 0;
740 save->cr4 = 0;
741 }
Avi Kivitya79d2f12008-04-14 13:10:21 +0300742 force_new_asid(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +0100743
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200744 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +0200745 svm->vcpu.arch.hflags = 0;
746
Mark Langsdorf565d0992009-10-06 14:25:02 -0500747 if (svm_has(SVM_FEATURE_PAUSE_FILTER)) {
748 control->pause_filter_count = 3000;
749 control->intercept |= (1ULL << INTERCEPT_PAUSE);
750 }
751
Joerg Roedel2af91942009-08-07 11:49:28 +0200752 enable_gif(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800753}
754
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200755static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +0300756{
757 struct vcpu_svm *svm = to_svm(vcpu);
758
Joerg Roedele6101a92008-02-13 18:58:45 +0100759 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +0200760
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300761 if (!kvm_vcpu_is_bsp(vcpu)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300762 kvm_rip_write(vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800763 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
764 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +0200765 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300766 vcpu->arch.regs_avail = ~0;
767 vcpu->arch.regs_dirty = ~0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200768
769 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300770}
771
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000772static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800773{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400774 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800775 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100776 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +0100777 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100778 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000779 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800780
Rusty Russellc16f8622007-07-30 21:12:19 +1000781 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000782 if (!svm) {
783 err = -ENOMEM;
784 goto out;
785 }
786
787 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
788 if (err)
789 goto free_svm;
790
Joerg Roedelf65c2292008-02-13 18:58:46 +0100791 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900792 page = alloc_page(GFP_KERNEL);
793 if (!page)
794 goto uninit;
795
Joerg Roedelf65c2292008-02-13 18:58:46 +0100796 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
797 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900798 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100799
800 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
801 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900802 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100803
Alexander Grafb286d5d2008-11-25 20:17:05 +0100804 hsave_page = alloc_page(GFP_KERNEL);
805 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900806 goto free_page3;
807
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200808 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +0100809
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900810 svm->msrpm = page_address(msrpm_pages);
811 svm_vcpu_init_msrpm(svm->msrpm);
812
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200813 svm->nested.msrpm = page_address(nested_msrpm_pages);
Alexander Graf3d6368e2008-11-25 20:17:07 +0100814
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400815 svm->vmcb = page_address(page);
816 clear_page(svm->vmcb);
817 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
818 svm->asid_generation = 0;
Joerg Roedele6101a92008-02-13 18:58:45 +0100819 init_vmcb(svm);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400820
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000821 fx_init(&svm->vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800822 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300823 if (kvm_vcpu_is_bsp(&svm->vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800824 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000826 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -0800827
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900828free_page3:
829 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
830free_page2:
831 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
832free_page1:
833 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000834uninit:
835 kvm_vcpu_uninit(&svm->vcpu);
836free_svm:
Rusty Russella4770342007-08-01 14:46:11 +1000837 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000838out:
839 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800840}
841
842static void svm_free_vcpu(struct kvm_vcpu *vcpu)
843{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400844 struct vcpu_svm *svm = to_svm(vcpu);
845
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000846 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +0100847 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200848 __free_page(virt_to_page(svm->nested.hsave));
849 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000850 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +1000851 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800852}
853
Avi Kivity15ad7142007-07-11 18:17:21 +0300854static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800855{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400856 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300857 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +0200858
Avi Kivity0cc50642007-03-25 12:07:27 +0200859 if (unlikely(cpu != vcpu->cpu)) {
Joerg Roedele935d482009-09-16 15:24:19 +0200860 u64 delta;
Avi Kivity0cc50642007-03-25 12:07:27 +0200861
Joerg Roedel953899b2009-12-14 12:22:20 +0100862 if (check_tsc_unstable()) {
863 /*
864 * Make sure that the guest sees a monotonically
865 * increasing TSC.
866 */
867 delta = vcpu->arch.host_tsc - native_read_tsc();
868 svm->vmcb->control.tsc_offset += delta;
869 if (is_nested(svm))
870 svm->nested.hsave->control.tsc_offset += delta;
871 }
Avi Kivity0cc50642007-03-25 12:07:27 +0200872 vcpu->cpu = cpu;
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300873 kvm_migrate_timers(vcpu);
Marcelo Tosatti4b656b12009-07-21 12:47:45 -0300874 svm->asid_generation = 0;
Avi Kivity0cc50642007-03-25 12:07:27 +0200875 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300876
877 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400878 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800879}
880
881static void svm_vcpu_put(struct kvm_vcpu *vcpu)
882{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400883 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300884 int i;
885
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200886 ++vcpu->stat.host_state_reload;
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300887 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400888 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300889
Joerg Roedele935d482009-09-16 15:24:19 +0200890 vcpu->arch.host_tsc = native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891}
892
Avi Kivity6aa8b732006-12-10 02:21:36 -0800893static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
894{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400895 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800896}
897
898static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
899{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400900 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800901}
902
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300903static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
904{
905 switch (reg) {
906 case VCPU_EXREG_PDPTR:
907 BUG_ON(!npt_enabled);
908 load_pdptrs(vcpu, vcpu->arch.cr3);
909 break;
910 default:
911 BUG();
912 }
913}
914
Alexander Graff0b85052008-11-25 20:17:01 +0100915static void svm_set_vintr(struct vcpu_svm *svm)
916{
917 svm->vmcb->control.intercept |= 1ULL << INTERCEPT_VINTR;
918}
919
920static void svm_clear_vintr(struct vcpu_svm *svm)
921{
922 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
923}
924
Avi Kivity6aa8b732006-12-10 02:21:36 -0800925static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
926{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400927 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928
929 switch (seg) {
930 case VCPU_SREG_CS: return &save->cs;
931 case VCPU_SREG_DS: return &save->ds;
932 case VCPU_SREG_ES: return &save->es;
933 case VCPU_SREG_FS: return &save->fs;
934 case VCPU_SREG_GS: return &save->gs;
935 case VCPU_SREG_SS: return &save->ss;
936 case VCPU_SREG_TR: return &save->tr;
937 case VCPU_SREG_LDTR: return &save->ldtr;
938 }
939 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +0000940 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800941}
942
943static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
944{
945 struct vmcb_seg *s = svm_seg(vcpu, seg);
946
947 return s->base;
948}
949
950static void svm_get_segment(struct kvm_vcpu *vcpu,
951 struct kvm_segment *var, int seg)
952{
953 struct vmcb_seg *s = svm_seg(vcpu, seg);
954
955 var->base = s->base;
956 var->limit = s->limit;
957 var->selector = s->selector;
958 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
959 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
960 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
961 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
962 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
963 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
964 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
965 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
Amit Shah25022ac2008-10-27 09:04:17 +0000966
Joerg Roedele0231712010-02-24 18:59:10 +0100967 /*
968 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +0200969 * for cross vendor migration purposes by "not present"
970 */
971 var->unusable = !var->present || (var->type == 0);
972
Andre Przywara1fbdc7a2009-01-11 22:39:44 +0100973 switch (seg) {
974 case VCPU_SREG_CS:
975 /*
976 * SVM always stores 0 for the 'G' bit in the CS selector in
977 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
978 * Intel's VMENTRY has a check on the 'G' bit.
979 */
Amit Shah25022ac2008-10-27 09:04:17 +0000980 var->g = s->limit > 0xfffff;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +0100981 break;
982 case VCPU_SREG_TR:
983 /*
984 * Work around a bug where the busy flag in the tr selector
985 * isn't exposed
986 */
Amit Shahc0d09822008-10-27 09:04:18 +0000987 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +0100988 break;
989 case VCPU_SREG_DS:
990 case VCPU_SREG_ES:
991 case VCPU_SREG_FS:
992 case VCPU_SREG_GS:
993 /*
994 * The accessed bit must always be set in the segment
995 * descriptor cache, although it can be cleared in the
996 * descriptor, the cached bit always remains at 1. Since
997 * Intel has a check on this, set it here to support
998 * cross-vendor migration.
999 */
1000 if (!var->unusable)
1001 var->type |= 0x1;
1002 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001003 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001004 /*
1005 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001006 * descriptor is left as 1, although the whole segment has
1007 * been made unusable. Clear it here to pass an Intel VMX
1008 * entry check when cross vendor migrating.
1009 */
1010 if (var->unusable)
1011 var->db = 0;
1012 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001013 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014}
1015
Izik Eidus2e4d2652008-03-24 19:38:34 +02001016static int svm_get_cpl(struct kvm_vcpu *vcpu)
1017{
1018 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1019
1020 return save->cpl;
1021}
1022
Gleb Natapov89a27f42010-02-16 10:51:48 +02001023static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001024{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001025 struct vcpu_svm *svm = to_svm(vcpu);
1026
Gleb Natapov89a27f42010-02-16 10:51:48 +02001027 dt->size = svm->vmcb->save.idtr.limit;
1028 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001029}
1030
Gleb Natapov89a27f42010-02-16 10:51:48 +02001031static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001032{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001033 struct vcpu_svm *svm = to_svm(vcpu);
1034
Gleb Natapov89a27f42010-02-16 10:51:48 +02001035 svm->vmcb->save.idtr.limit = dt->size;
1036 svm->vmcb->save.idtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001037}
1038
Gleb Natapov89a27f42010-02-16 10:51:48 +02001039static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001040{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001041 struct vcpu_svm *svm = to_svm(vcpu);
1042
Gleb Natapov89a27f42010-02-16 10:51:48 +02001043 dt->size = svm->vmcb->save.gdtr.limit;
1044 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001045}
1046
Gleb Natapov89a27f42010-02-16 10:51:48 +02001047static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001048{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001049 struct vcpu_svm *svm = to_svm(vcpu);
1050
Gleb Natapov89a27f42010-02-16 10:51:48 +02001051 svm->vmcb->save.gdtr.limit = dt->size;
1052 svm->vmcb->save.gdtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001053}
1054
Avi Kivitye8467fd2009-12-29 18:43:06 +02001055static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1056{
1057}
1058
Anthony Liguori25c4c272007-04-27 09:29:21 +03001059static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001060{
1061}
1062
Avi Kivityd2251572010-01-06 10:55:27 +02001063static void update_cr0_intercept(struct vcpu_svm *svm)
1064{
Joerg Roedel66a562f2010-02-19 16:23:08 +01001065 struct vmcb *vmcb = svm->vmcb;
Avi Kivityd2251572010-01-06 10:55:27 +02001066 ulong gcr0 = svm->vcpu.arch.cr0;
1067 u64 *hcr0 = &svm->vmcb->save.cr0;
1068
1069 if (!svm->vcpu.fpu_active)
1070 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1071 else
1072 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1073 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1074
1075
1076 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Joerg Roedel66a562f2010-02-19 16:23:08 +01001077 vmcb->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1078 vmcb->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1079 if (is_nested(svm)) {
1080 struct vmcb *hsave = svm->nested.hsave;
1081
1082 hsave->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1083 hsave->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1084 vmcb->control.intercept_cr_read |= svm->nested.intercept_cr_read;
1085 vmcb->control.intercept_cr_write |= svm->nested.intercept_cr_write;
1086 }
Avi Kivityd2251572010-01-06 10:55:27 +02001087 } else {
1088 svm->vmcb->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1089 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001090 if (is_nested(svm)) {
1091 struct vmcb *hsave = svm->nested.hsave;
1092
1093 hsave->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1094 hsave->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
1095 }
Avi Kivityd2251572010-01-06 10:55:27 +02001096 }
1097}
1098
Avi Kivity6aa8b732006-12-10 02:21:36 -08001099static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1100{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001101 struct vcpu_svm *svm = to_svm(vcpu);
1102
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001103 if (is_nested(svm)) {
1104 /*
1105 * We are here because we run in nested mode, the host kvm
1106 * intercepts cr0 writes but the l1 hypervisor does not.
1107 * But the L1 hypervisor may intercept selective cr0 writes.
1108 * This needs to be checked here.
1109 */
1110 unsigned long old, new;
1111
1112 /* Remove bits that would trigger a real cr0 write intercept */
1113 old = vcpu->arch.cr0 & SVM_CR0_SELECTIVE_MASK;
1114 new = cr0 & SVM_CR0_SELECTIVE_MASK;
1115
1116 if (old == new) {
1117 /* cr0 write with ts and mp unchanged */
1118 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
1119 if (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE)
1120 return;
1121 }
1122 }
1123
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001124#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001125 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001126 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001127 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001128 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001129 }
1130
Mike Dayd77c26f2007-10-08 09:02:08 -04001131 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001132 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001133 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001134 }
1135 }
1136#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001137 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001138
1139 if (!npt_enabled)
1140 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001141
1142 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001143 cr0 |= X86_CR0_TS;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001144 /*
1145 * re-enable caching here because the QEMU bios
1146 * does not do it - this results in some delay at
1147 * reboot
1148 */
1149 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001150 svm->vmcb->save.cr0 = cr0;
Avi Kivityd2251572010-01-06 10:55:27 +02001151 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001152}
1153
1154static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1155{
Joerg Roedel6394b642008-04-09 14:15:29 +02001156 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001157 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1158
1159 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
1160 force_new_asid(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001161
Joerg Roedelec077262008-04-09 14:15:28 +02001162 vcpu->arch.cr4 = cr4;
1163 if (!npt_enabled)
1164 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001165 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001166 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001167}
1168
1169static void svm_set_segment(struct kvm_vcpu *vcpu,
1170 struct kvm_segment *var, int seg)
1171{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001172 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001173 struct vmcb_seg *s = svm_seg(vcpu, seg);
1174
1175 s->base = var->base;
1176 s->limit = var->limit;
1177 s->selector = var->selector;
1178 if (var->unusable)
1179 s->attrib = 0;
1180 else {
1181 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1182 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1183 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1184 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1185 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1186 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1187 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1188 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1189 }
1190 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001191 svm->vmcb->save.cpl
1192 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -08001193 >> SVM_SELECTOR_DPL_SHIFT) & 3;
1194
1195}
1196
Gleb Natapov44c11432009-05-11 13:35:52 +03001197static void update_db_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001198{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001199 struct vcpu_svm *svm = to_svm(vcpu);
1200
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001201 svm->vmcb->control.intercept_exceptions &=
1202 ~((1 << DB_VECTOR) | (1 << BP_VECTOR));
Gleb Natapov44c11432009-05-11 13:35:52 +03001203
Jan Kiszka6be7d302009-10-18 13:24:54 +02001204 if (svm->nmi_singlestep)
Gleb Natapov44c11432009-05-11 13:35:52 +03001205 svm->vmcb->control.intercept_exceptions |= (1 << DB_VECTOR);
1206
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001207 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1208 if (vcpu->guest_debug &
1209 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
1210 svm->vmcb->control.intercept_exceptions |=
1211 1 << DB_VECTOR;
1212 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
1213 svm->vmcb->control.intercept_exceptions |=
1214 1 << BP_VECTOR;
1215 } else
1216 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001217}
1218
Jan Kiszka355be0b2009-10-03 00:31:21 +02001219static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Gleb Natapov44c11432009-05-11 13:35:52 +03001220{
Gleb Natapov44c11432009-05-11 13:35:52 +03001221 struct vcpu_svm *svm = to_svm(vcpu);
1222
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001223 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1224 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1225 else
1226 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1227
Jan Kiszka355be0b2009-10-03 00:31:21 +02001228 update_db_intercept(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001229}
1230
1231static void load_host_msrs(struct kvm_vcpu *vcpu)
1232{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001233#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001234 wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001235#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001236}
1237
1238static void save_host_msrs(struct kvm_vcpu *vcpu)
1239{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001240#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001241 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001242#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001243}
1244
Tejun Heo0fe1e002009-10-29 22:34:14 +09001245static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001246{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001247 if (sd->next_asid > sd->max_asid) {
1248 ++sd->asid_generation;
1249 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001250 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001251 }
1252
Tejun Heo0fe1e002009-10-29 22:34:14 +09001253 svm->asid_generation = sd->asid_generation;
1254 svm->vmcb->control.asid = sd->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001255}
1256
Jan Kiszkac76de352010-01-20 18:20:20 +01001257static int svm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *dest)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001258{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001259 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001260
1261 switch (dr) {
1262 case 0 ... 3:
Jan Kiszkac76de352010-01-20 18:20:20 +01001263 *dest = vcpu->arch.db[dr];
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001264 break;
Jan Kiszkac76de352010-01-20 18:20:20 +01001265 case 4:
1266 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
1267 return EMULATE_FAIL; /* will re-inject UD */
1268 /* fall through */
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001269 case 6:
1270 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
Jan Kiszkac76de352010-01-20 18:20:20 +01001271 *dest = vcpu->arch.dr6;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001272 else
Jan Kiszkac76de352010-01-20 18:20:20 +01001273 *dest = svm->vmcb->save.dr6;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001274 break;
Jan Kiszkac76de352010-01-20 18:20:20 +01001275 case 5:
1276 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
1277 return EMULATE_FAIL; /* will re-inject UD */
1278 /* fall through */
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001279 case 7:
1280 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
Jan Kiszkac76de352010-01-20 18:20:20 +01001281 *dest = vcpu->arch.dr7;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001282 else
Jan Kiszkac76de352010-01-20 18:20:20 +01001283 *dest = svm->vmcb->save.dr7;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001284 break;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001285 }
1286
Jan Kiszkac76de352010-01-20 18:20:20 +01001287 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001288}
1289
Jan Kiszkac76de352010-01-20 18:20:20 +01001290static int svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001291{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001292 struct vcpu_svm *svm = to_svm(vcpu);
1293
Avi Kivity6aa8b732006-12-10 02:21:36 -08001294 switch (dr) {
1295 case 0 ... 3:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001296 vcpu->arch.db[dr] = value;
1297 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1298 vcpu->arch.eff_db[dr] = value;
Jan Kiszkac76de352010-01-20 18:20:20 +01001299 break;
1300 case 4:
1301 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
1302 return EMULATE_FAIL; /* will re-inject UD */
1303 /* fall through */
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001304 case 6:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001305 vcpu->arch.dr6 = (value & DR6_VOLATILE) | DR6_FIXED_1;
Jan Kiszkac76de352010-01-20 18:20:20 +01001306 break;
1307 case 5:
1308 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
1309 return EMULATE_FAIL; /* will re-inject UD */
1310 /* fall through */
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001311 case 7:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001312 vcpu->arch.dr7 = (value & DR7_VOLATILE) | DR7_FIXED_1;
1313 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
1314 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1315 vcpu->arch.switch_db_regs = (value & DR7_BP_EN_MASK);
1316 }
Jan Kiszkac76de352010-01-20 18:20:20 +01001317 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001318 }
Jan Kiszkac76de352010-01-20 18:20:20 +01001319
1320 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001321}
1322
Avi Kivity851ba692009-08-24 11:10:17 +03001323static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001324{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001325 u64 fault_address;
1326 u32 error_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001327
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001328 fault_address = svm->vmcb->control.exit_info_2;
1329 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001330
Marcelo Tosatti229456f2009-06-17 09:22:14 -03001331 trace_kvm_page_fault(fault_address, error_code);
Avi Kivity52c78472009-08-10 15:42:41 +03001332 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1333 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
Avi Kivity30677142007-10-28 18:48:59 +02001334 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001335}
1336
Avi Kivity851ba692009-08-24 11:10:17 +03001337static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001338{
Avi Kivity851ba692009-08-24 11:10:17 +03001339 struct kvm_run *kvm_run = svm->vcpu.run;
1340
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001341 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001342 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001343 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001344 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1345 return 1;
1346 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001347
Jan Kiszka6be7d302009-10-18 13:24:54 +02001348 if (svm->nmi_singlestep) {
1349 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001350 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1351 svm->vmcb->save.rflags &=
1352 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1353 update_db_intercept(&svm->vcpu);
1354 }
1355
1356 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001357 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001358 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1359 kvm_run->debug.arch.pc =
1360 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1361 kvm_run->debug.arch.exception = DB_VECTOR;
1362 return 0;
1363 }
1364
1365 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001366}
1367
Avi Kivity851ba692009-08-24 11:10:17 +03001368static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001369{
Avi Kivity851ba692009-08-24 11:10:17 +03001370 struct kvm_run *kvm_run = svm->vcpu.run;
1371
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001372 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1373 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1374 kvm_run->debug.arch.exception = BP_VECTOR;
1375 return 0;
1376}
1377
Avi Kivity851ba692009-08-24 11:10:17 +03001378static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001379{
1380 int er;
1381
Avi Kivity851ba692009-08-24 11:10:17 +03001382 er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001383 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001384 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001385 return 1;
1386}
1387
Avi Kivity6b52d182010-01-21 15:31:47 +02001388static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001389{
Avi Kivity6b52d182010-01-21 15:31:47 +02001390 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001391 u32 excp;
1392
1393 if (is_nested(svm)) {
1394 u32 h_excp, n_excp;
1395
1396 h_excp = svm->nested.hsave->control.intercept_exceptions;
1397 n_excp = svm->nested.intercept_exceptions;
1398 h_excp &= ~(1 << NM_VECTOR);
1399 excp = h_excp | n_excp;
1400 } else {
1401 excp = svm->vmcb->control.intercept_exceptions;
Joerg Roedele0231712010-02-24 18:59:10 +01001402 excp &= ~(1 << NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001403 }
1404
1405 svm->vmcb->control.intercept_exceptions = excp;
1406
Rusty Russelle756fc62007-07-30 20:07:08 +10001407 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001408 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001409}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001410
Avi Kivity6b52d182010-01-21 15:31:47 +02001411static int nm_interception(struct vcpu_svm *svm)
1412{
1413 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001414 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001415}
1416
Avi Kivity851ba692009-08-24 11:10:17 +03001417static int mc_interception(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001418{
1419 /*
1420 * On an #MC intercept the MCE handler is not called automatically in
1421 * the host. So do it by hand here.
1422 */
1423 asm volatile (
1424 "int $0x12\n");
1425 /* not sure if we ever come back to this point */
1426
1427 return 1;
1428}
1429
Avi Kivity851ba692009-08-24 11:10:17 +03001430static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001431{
Avi Kivity851ba692009-08-24 11:10:17 +03001432 struct kvm_run *kvm_run = svm->vcpu.run;
1433
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001434 /*
1435 * VMCB is undefined after a SHUTDOWN intercept
1436 * so reinitialize it.
1437 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001438 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001439 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001440
1441 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1442 return 0;
1443}
1444
Avi Kivity851ba692009-08-24 11:10:17 +03001445static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001446{
Mike Dayd77c26f2007-10-08 09:02:08 -04001447 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001448 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001449 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001450
Rusty Russelle756fc62007-07-30 20:07:08 +10001451 ++svm->vcpu.stat.io_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001452
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001453 svm->next_rip = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001454
Laurent Viviere70669a2007-08-05 10:36:40 +03001455 string = (io_info & SVM_IOIO_STR_MASK) != 0;
1456
1457 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02001458 if (emulate_instruction(&svm->vcpu,
Avi Kivity851ba692009-08-24 11:10:17 +03001459 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03001460 return 0;
1461 return 1;
1462 }
1463
Avi Kivity039576c2007-03-20 12:46:50 +02001464 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
1465 port = io_info >> 16;
1466 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001467
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001468 skip_emulated_instruction(&svm->vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03001469 return kvm_emulate_pio(&svm->vcpu, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001470}
1471
Avi Kivity851ba692009-08-24 11:10:17 +03001472static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001473{
1474 return 1;
1475}
1476
Avi Kivity851ba692009-08-24 11:10:17 +03001477static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001478{
1479 ++svm->vcpu.stat.irq_exits;
1480 return 1;
1481}
1482
Avi Kivity851ba692009-08-24 11:10:17 +03001483static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001484{
1485 return 1;
1486}
1487
Avi Kivity851ba692009-08-24 11:10:17 +03001488static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001489{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001490 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001491 skip_emulated_instruction(&svm->vcpu);
1492 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001493}
1494
Avi Kivity851ba692009-08-24 11:10:17 +03001495static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001496{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001497 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001498 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001499 kvm_emulate_hypercall(&svm->vcpu);
1500 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001501}
1502
Alexander Grafc0725422008-11-25 20:17:03 +01001503static int nested_svm_check_permissions(struct vcpu_svm *svm)
1504{
Avi Kivityf6801df2010-01-21 15:31:50 +02001505 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01001506 || !is_paging(&svm->vcpu)) {
1507 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1508 return 1;
1509 }
1510
1511 if (svm->vmcb->save.cpl) {
1512 kvm_inject_gp(&svm->vcpu, 0);
1513 return 1;
1514 }
1515
1516 return 0;
1517}
1518
Alexander Grafcf74a782008-11-25 20:17:08 +01001519static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
1520 bool has_error_code, u32 error_code)
1521{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001522 int vmexit;
1523
Joerg Roedel0295ad72009-08-07 11:49:37 +02001524 if (!is_nested(svm))
1525 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001526
Joerg Roedel0295ad72009-08-07 11:49:37 +02001527 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1528 svm->vmcb->control.exit_code_hi = 0;
1529 svm->vmcb->control.exit_info_1 = error_code;
1530 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1531
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001532 vmexit = nested_svm_intercept(svm);
1533 if (vmexit == NESTED_EXIT_DONE)
1534 svm->nested.exit_required = true;
1535
1536 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001537}
1538
Joerg Roedel8fe54652010-02-19 16:23:01 +01001539/* This function returns true if it is save to enable the irq window */
1540static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001541{
Joerg Roedel26666952009-08-07 11:49:46 +02001542 if (!is_nested(svm))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001543 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001544
Joerg Roedel26666952009-08-07 11:49:46 +02001545 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001546 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001547
Joerg Roedel26666952009-08-07 11:49:46 +02001548 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001549 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001550
Joerg Roedel197717d2010-02-24 18:59:19 +01001551 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
1552 svm->vmcb->control.exit_info_1 = 0;
1553 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02001554
Joerg Roedelcd3ff652009-10-09 16:08:26 +02001555 if (svm->nested.intercept & 1ULL) {
1556 /*
1557 * The #vmexit can't be emulated here directly because this
1558 * code path runs with irqs and preemtion disabled. A
1559 * #vmexit emulation might sleep. Only signal request for
1560 * the #vmexit here.
1561 */
1562 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02001563 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01001564 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001565 }
1566
Joerg Roedel8fe54652010-02-19 16:23:01 +01001567 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001568}
1569
Joerg Roedel887f5002010-02-24 18:59:12 +01001570/* This function returns true if it is save to enable the nmi window */
1571static inline bool nested_svm_nmi(struct vcpu_svm *svm)
1572{
1573 if (!is_nested(svm))
1574 return true;
1575
1576 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
1577 return true;
1578
1579 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
1580 svm->nested.exit_required = true;
1581
1582 return false;
1583}
1584
Joerg Roedel7597f122010-02-19 16:23:00 +01001585static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001586{
1587 struct page *page;
1588
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01001589 might_sleep();
1590
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001591 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001592 if (is_error_page(page))
1593 goto error;
1594
Joerg Roedel7597f122010-02-19 16:23:00 +01001595 *_page = page;
1596
1597 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001598
1599error:
1600 kvm_release_page_clean(page);
1601 kvm_inject_gp(&svm->vcpu, 0);
1602
1603 return NULL;
1604}
1605
Joerg Roedel7597f122010-02-19 16:23:00 +01001606static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001607{
Joerg Roedel7597f122010-02-19 16:23:00 +01001608 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001609 kvm_release_page_dirty(page);
1610}
1611
Joerg Roedeld2477822010-03-01 15:34:34 +01001612static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001613{
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001614 u32 param = svm->vmcb->control.exit_info_1 & 1;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001615 u32 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001616 u32 t0, t1;
Joerg Roedeld2477822010-03-01 15:34:34 +01001617 int ret;
Joerg Roedel4c7da8c2010-02-19 16:23:05 +01001618 u8 val;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001619
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001620 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01001621 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001622
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001623 switch (msr) {
1624 case 0 ... 0x1fff:
1625 t0 = (msr * 2) % 8;
1626 t1 = msr / 8;
1627 break;
1628 case 0xc0000000 ... 0xc0001fff:
1629 t0 = (8192 + msr - 0xc0000000) * 2;
1630 t1 = (t0 / 8);
1631 t0 %= 8;
1632 break;
1633 case 0xc0010000 ... 0xc0011fff:
1634 t0 = (16384 + msr - 0xc0010000) * 2;
1635 t1 = (t0 / 8);
1636 t0 %= 8;
1637 break;
1638 default:
Joerg Roedeld2477822010-03-01 15:34:34 +01001639 ret = NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001640 goto out;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001641 }
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001642
Joerg Roedel4c7da8c2010-02-19 16:23:05 +01001643 if (!kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + t1, &val, 1))
Joerg Roedeld2477822010-03-01 15:34:34 +01001644 ret = val & ((1 << param) << t0) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001645
1646out:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001647 return ret;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001648}
1649
Joerg Roedel410e4d52009-08-07 11:49:44 +02001650static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001651{
Alexander Grafcf74a782008-11-25 20:17:08 +01001652 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001653
Joerg Roedel410e4d52009-08-07 11:49:44 +02001654 switch (exit_code) {
1655 case SVM_EXIT_INTR:
1656 case SVM_EXIT_NMI:
1657 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001658 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01001659 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001660 if (npt_enabled)
1661 return NESTED_EXIT_HOST;
1662 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001663 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Joerg Roedele0231712010-02-24 18:59:10 +01001664 /* When we're shadowing, trap PFs */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001665 if (!npt_enabled)
1666 return NESTED_EXIT_HOST;
1667 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001668 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
1669 nm_interception(svm);
1670 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001671 default:
1672 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001673 }
1674
Joerg Roedel410e4d52009-08-07 11:49:44 +02001675 return NESTED_EXIT_CONTINUE;
1676}
1677
1678/*
1679 * If this function returns true, this #vmexit was already handled
1680 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001681static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001682{
1683 u32 exit_code = svm->vmcb->control.exit_code;
1684 int vmexit = NESTED_EXIT_HOST;
1685
Alexander Grafcf74a782008-11-25 20:17:08 +01001686 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001687 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001688 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001689 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001690 case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR8: {
1691 u32 cr_bits = 1 << (exit_code - SVM_EXIT_READ_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001692 if (svm->nested.intercept_cr_read & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001693 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001694 break;
1695 }
1696 case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR8: {
1697 u32 cr_bits = 1 << (exit_code - SVM_EXIT_WRITE_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001698 if (svm->nested.intercept_cr_write & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001699 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001700 break;
1701 }
1702 case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR7: {
1703 u32 dr_bits = 1 << (exit_code - SVM_EXIT_READ_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001704 if (svm->nested.intercept_dr_read & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001705 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001706 break;
1707 }
1708 case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR7: {
1709 u32 dr_bits = 1 << (exit_code - SVM_EXIT_WRITE_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001710 if (svm->nested.intercept_dr_write & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001711 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001712 break;
1713 }
1714 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1715 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001716 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001717 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001718 break;
1719 }
1720 default: {
1721 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001722 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001723 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001724 }
1725 }
1726
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001727 return vmexit;
1728}
1729
1730static int nested_svm_exit_handled(struct vcpu_svm *svm)
1731{
1732 int vmexit;
1733
1734 vmexit = nested_svm_intercept(svm);
1735
1736 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001737 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001738
1739 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001740}
1741
Joerg Roedel0460a972009-08-07 11:49:31 +02001742static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
1743{
1744 struct vmcb_control_area *dst = &dst_vmcb->control;
1745 struct vmcb_control_area *from = &from_vmcb->control;
1746
1747 dst->intercept_cr_read = from->intercept_cr_read;
1748 dst->intercept_cr_write = from->intercept_cr_write;
1749 dst->intercept_dr_read = from->intercept_dr_read;
1750 dst->intercept_dr_write = from->intercept_dr_write;
1751 dst->intercept_exceptions = from->intercept_exceptions;
1752 dst->intercept = from->intercept;
1753 dst->iopm_base_pa = from->iopm_base_pa;
1754 dst->msrpm_base_pa = from->msrpm_base_pa;
1755 dst->tsc_offset = from->tsc_offset;
1756 dst->asid = from->asid;
1757 dst->tlb_ctl = from->tlb_ctl;
1758 dst->int_ctl = from->int_ctl;
1759 dst->int_vector = from->int_vector;
1760 dst->int_state = from->int_state;
1761 dst->exit_code = from->exit_code;
1762 dst->exit_code_hi = from->exit_code_hi;
1763 dst->exit_info_1 = from->exit_info_1;
1764 dst->exit_info_2 = from->exit_info_2;
1765 dst->exit_int_info = from->exit_int_info;
1766 dst->exit_int_info_err = from->exit_int_info_err;
1767 dst->nested_ctl = from->nested_ctl;
1768 dst->event_inj = from->event_inj;
1769 dst->event_inj_err = from->event_inj_err;
1770 dst->nested_cr3 = from->nested_cr3;
1771 dst->lbr_ctl = from->lbr_ctl;
1772}
1773
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001774static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001775{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001776 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001777 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02001778 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01001779 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01001780
Joerg Roedel17897f32009-10-09 16:08:29 +02001781 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
1782 vmcb->control.exit_info_1,
1783 vmcb->control.exit_info_2,
1784 vmcb->control.exit_int_info,
1785 vmcb->control.exit_int_info_err);
1786
Joerg Roedel7597f122010-02-19 16:23:00 +01001787 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001788 if (!nested_vmcb)
1789 return 1;
1790
Joerg Roedel06fc77722010-02-19 16:23:07 +01001791 /* Exit nested SVM mode */
1792 svm->nested.vmcb = 0;
1793
Alexander Grafcf74a782008-11-25 20:17:08 +01001794 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02001795 disable_gif(svm);
1796
1797 nested_vmcb->save.es = vmcb->save.es;
1798 nested_vmcb->save.cs = vmcb->save.cs;
1799 nested_vmcb->save.ss = vmcb->save.ss;
1800 nested_vmcb->save.ds = vmcb->save.ds;
1801 nested_vmcb->save.gdtr = vmcb->save.gdtr;
1802 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001803 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02001804 if (npt_enabled)
1805 nested_vmcb->save.cr3 = vmcb->save.cr3;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001806 else
1807 nested_vmcb->save.cr3 = svm->vcpu.arch.cr3;
Joerg Roedel33740e42009-08-07 11:49:29 +02001808 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001809 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Joerg Roedel33740e42009-08-07 11:49:29 +02001810 nested_vmcb->save.rflags = vmcb->save.rflags;
1811 nested_vmcb->save.rip = vmcb->save.rip;
1812 nested_vmcb->save.rsp = vmcb->save.rsp;
1813 nested_vmcb->save.rax = vmcb->save.rax;
1814 nested_vmcb->save.dr7 = vmcb->save.dr7;
1815 nested_vmcb->save.dr6 = vmcb->save.dr6;
1816 nested_vmcb->save.cpl = vmcb->save.cpl;
1817
1818 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
1819 nested_vmcb->control.int_vector = vmcb->control.int_vector;
1820 nested_vmcb->control.int_state = vmcb->control.int_state;
1821 nested_vmcb->control.exit_code = vmcb->control.exit_code;
1822 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
1823 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
1824 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
1825 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
1826 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Alexander Graf8d23c462009-10-09 16:08:25 +02001827
1828 /*
1829 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
1830 * to make sure that we do not lose injected events. So check event_inj
1831 * here and copy it to exit_int_info if it is valid.
1832 * Exit_int_info and event_inj can't be both valid because the case
1833 * below only happens on a VMRUN instruction intercept which has
1834 * no valid exit_int_info set.
1835 */
1836 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
1837 struct vmcb_control_area *nc = &nested_vmcb->control;
1838
1839 nc->exit_int_info = vmcb->control.event_inj;
1840 nc->exit_int_info_err = vmcb->control.event_inj_err;
1841 }
1842
Joerg Roedel33740e42009-08-07 11:49:29 +02001843 nested_vmcb->control.tlb_ctl = 0;
1844 nested_vmcb->control.event_inj = 0;
1845 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001846
1847 /* We always set V_INTR_MASKING and remember the old value in hflags */
1848 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
1849 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
1850
Alexander Grafcf74a782008-11-25 20:17:08 +01001851 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02001852 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01001853
Alexander Graf219b65d2009-06-15 15:21:25 +02001854 kvm_clear_exception_queue(&svm->vcpu);
1855 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01001856
1857 /* Restore selected save entries */
1858 svm->vmcb->save.es = hsave->save.es;
1859 svm->vmcb->save.cs = hsave->save.cs;
1860 svm->vmcb->save.ss = hsave->save.ss;
1861 svm->vmcb->save.ds = hsave->save.ds;
1862 svm->vmcb->save.gdtr = hsave->save.gdtr;
1863 svm->vmcb->save.idtr = hsave->save.idtr;
1864 svm->vmcb->save.rflags = hsave->save.rflags;
1865 svm_set_efer(&svm->vcpu, hsave->save.efer);
1866 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
1867 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
1868 if (npt_enabled) {
1869 svm->vmcb->save.cr3 = hsave->save.cr3;
1870 svm->vcpu.arch.cr3 = hsave->save.cr3;
1871 } else {
1872 kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
1873 }
1874 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
1875 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
1876 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
1877 svm->vmcb->save.dr7 = 0;
1878 svm->vmcb->save.cpl = 0;
1879 svm->vmcb->control.exit_int_info = 0;
1880
Joerg Roedel7597f122010-02-19 16:23:00 +01001881 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01001882
1883 kvm_mmu_reset_context(&svm->vcpu);
1884 kvm_mmu_load(&svm->vcpu);
1885
1886 return 0;
1887}
Alexander Graf3d6368e2008-11-25 20:17:07 +01001888
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001889static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01001890{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001891 u32 *nested_msrpm;
Joerg Roedel7597f122010-02-19 16:23:00 +01001892 struct page *page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001893 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001894
Joerg Roedel7597f122010-02-19 16:23:00 +01001895 nested_msrpm = nested_svm_map(svm, svm->nested.vmcb_msrpm, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001896 if (!nested_msrpm)
1897 return false;
1898
Joerg Roedele0231712010-02-24 18:59:10 +01001899 for (i = 0; i < PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER) / 4; i++)
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001900 svm->nested.msrpm[i] = svm->msrpm[i] | nested_msrpm[i];
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001901
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001902 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001903
Joerg Roedel7597f122010-02-19 16:23:00 +01001904 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001905
1906 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001907}
1908
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001909static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01001910{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001911 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001912 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02001913 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01001914 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01001915 u64 vmcb_gpa;
1916
1917 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001918
Joerg Roedel7597f122010-02-19 16:23:00 +01001919 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001920 if (!nested_vmcb)
1921 return false;
1922
Joerg Roedelecf14052010-02-24 18:59:13 +01001923 trace_kvm_nested_vmrun(svm->vmcb->save.rip - 3, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02001924 nested_vmcb->save.rip,
1925 nested_vmcb->control.int_ctl,
1926 nested_vmcb->control.event_inj,
1927 nested_vmcb->control.nested_ctl);
1928
Joerg Roedel2e554e82010-02-24 18:59:14 +01001929 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr_read,
1930 nested_vmcb->control.intercept_cr_write,
1931 nested_vmcb->control.intercept_exceptions,
1932 nested_vmcb->control.intercept);
1933
Alexander Graf3d6368e2008-11-25 20:17:07 +01001934 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02001935 kvm_clear_exception_queue(&svm->vcpu);
1936 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001937
Joerg Roedele0231712010-02-24 18:59:10 +01001938 /*
1939 * Save the old vmcb, so we don't need to pick what we save, but can
1940 * restore everything when a VMEXIT occurs
1941 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02001942 hsave->save.es = vmcb->save.es;
1943 hsave->save.cs = vmcb->save.cs;
1944 hsave->save.ss = vmcb->save.ss;
1945 hsave->save.ds = vmcb->save.ds;
1946 hsave->save.gdtr = vmcb->save.gdtr;
1947 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02001948 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02001949 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02001950 hsave->save.cr4 = svm->vcpu.arch.cr4;
1951 hsave->save.rflags = vmcb->save.rflags;
1952 hsave->save.rip = svm->next_rip;
1953 hsave->save.rsp = vmcb->save.rsp;
1954 hsave->save.rax = vmcb->save.rax;
1955 if (npt_enabled)
1956 hsave->save.cr3 = vmcb->save.cr3;
1957 else
1958 hsave->save.cr3 = svm->vcpu.arch.cr3;
1959
Joerg Roedel0460a972009-08-07 11:49:31 +02001960 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001961
1962 if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
1963 svm->vcpu.arch.hflags |= HF_HIF_MASK;
1964 else
1965 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
1966
1967 /* Load the nested guest state */
1968 svm->vmcb->save.es = nested_vmcb->save.es;
1969 svm->vmcb->save.cs = nested_vmcb->save.cs;
1970 svm->vmcb->save.ss = nested_vmcb->save.ss;
1971 svm->vmcb->save.ds = nested_vmcb->save.ds;
1972 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
1973 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
1974 svm->vmcb->save.rflags = nested_vmcb->save.rflags;
1975 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
1976 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
1977 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
1978 if (npt_enabled) {
1979 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
1980 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01001981 } else
Alexander Graf3d6368e2008-11-25 20:17:07 +01001982 kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01001983
1984 /* Guest paging mode is active - reset mmu */
1985 kvm_mmu_reset_context(&svm->vcpu);
1986
Joerg Roedeldefbba52009-08-07 11:49:30 +02001987 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001988 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
1989 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
1990 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01001991
Alexander Graf3d6368e2008-11-25 20:17:07 +01001992 /* In case we don't even reach vcpu_run, the fields are not updated */
1993 svm->vmcb->save.rax = nested_vmcb->save.rax;
1994 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
1995 svm->vmcb->save.rip = nested_vmcb->save.rip;
1996 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
1997 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
1998 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
1999
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002000 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002001
Joerg Roedelaad42c62009-08-07 11:49:34 +02002002 /* cache intercepts */
2003 svm->nested.intercept_cr_read = nested_vmcb->control.intercept_cr_read;
2004 svm->nested.intercept_cr_write = nested_vmcb->control.intercept_cr_write;
2005 svm->nested.intercept_dr_read = nested_vmcb->control.intercept_dr_read;
2006 svm->nested.intercept_dr_write = nested_vmcb->control.intercept_dr_write;
2007 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2008 svm->nested.intercept = nested_vmcb->control.intercept;
2009
Alexander Graf3d6368e2008-11-25 20:17:07 +01002010 force_new_asid(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002011 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002012 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2013 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2014 else
2015 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2016
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002017 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2018 /* We only want the cr8 intercept bits of the guest */
2019 svm->vmcb->control.intercept_cr_read &= ~INTERCEPT_CR8_MASK;
2020 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
2021 }
2022
Joerg Roedele0231712010-02-24 18:59:10 +01002023 /*
2024 * We don't want a nested guest to be more powerful than the guest, so
2025 * all intercepts are ORed
2026 */
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002027 svm->vmcb->control.intercept_cr_read |=
2028 nested_vmcb->control.intercept_cr_read;
2029 svm->vmcb->control.intercept_cr_write |=
2030 nested_vmcb->control.intercept_cr_write;
2031 svm->vmcb->control.intercept_dr_read |=
2032 nested_vmcb->control.intercept_dr_read;
2033 svm->vmcb->control.intercept_dr_write |=
2034 nested_vmcb->control.intercept_dr_write;
2035 svm->vmcb->control.intercept_exceptions |=
2036 nested_vmcb->control.intercept_exceptions;
2037
2038 svm->vmcb->control.intercept |= nested_vmcb->control.intercept;
2039
2040 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002041 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2042 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2043 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002044 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2045 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2046
Joerg Roedel7597f122010-02-19 16:23:00 +01002047 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002048
Joerg Roedel06fc77722010-02-19 16:23:07 +01002049 /* nested_vmcb is our indicator if nested SVM is activated */
2050 svm->nested.vmcb = vmcb_gpa;
2051
Joerg Roedel2af91942009-08-07 11:49:28 +02002052 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002053
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002054 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002055}
2056
Joerg Roedel9966bf62009-08-07 11:49:40 +02002057static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002058{
2059 to_vmcb->save.fs = from_vmcb->save.fs;
2060 to_vmcb->save.gs = from_vmcb->save.gs;
2061 to_vmcb->save.tr = from_vmcb->save.tr;
2062 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2063 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2064 to_vmcb->save.star = from_vmcb->save.star;
2065 to_vmcb->save.lstar = from_vmcb->save.lstar;
2066 to_vmcb->save.cstar = from_vmcb->save.cstar;
2067 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2068 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2069 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2070 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002071}
2072
Avi Kivity851ba692009-08-24 11:10:17 +03002073static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002074{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002075 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002076 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002077
Alexander Graf55426752008-11-25 20:17:06 +01002078 if (nested_svm_check_permissions(svm))
2079 return 1;
2080
2081 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2082 skip_emulated_instruction(&svm->vcpu);
2083
Joerg Roedel7597f122010-02-19 16:23:00 +01002084 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002085 if (!nested_vmcb)
2086 return 1;
2087
2088 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002089 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002090
2091 return 1;
2092}
2093
Avi Kivity851ba692009-08-24 11:10:17 +03002094static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002095{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002096 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002097 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002098
Alexander Graf55426752008-11-25 20:17:06 +01002099 if (nested_svm_check_permissions(svm))
2100 return 1;
2101
2102 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2103 skip_emulated_instruction(&svm->vcpu);
2104
Joerg Roedel7597f122010-02-19 16:23:00 +01002105 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002106 if (!nested_vmcb)
2107 return 1;
2108
2109 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002110 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002111
2112 return 1;
2113}
2114
Avi Kivity851ba692009-08-24 11:10:17 +03002115static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002116{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002117 if (nested_svm_check_permissions(svm))
2118 return 1;
2119
2120 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2121 skip_emulated_instruction(&svm->vcpu);
2122
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002123 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002124 return 1;
2125
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002126 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002127 goto failed;
2128
2129 return 1;
2130
2131failed:
2132
2133 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2134 svm->vmcb->control.exit_code_hi = 0;
2135 svm->vmcb->control.exit_info_1 = 0;
2136 svm->vmcb->control.exit_info_2 = 0;
2137
2138 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002139
2140 return 1;
2141}
2142
Avi Kivity851ba692009-08-24 11:10:17 +03002143static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002144{
2145 if (nested_svm_check_permissions(svm))
2146 return 1;
2147
2148 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2149 skip_emulated_instruction(&svm->vcpu);
2150
Joerg Roedel2af91942009-08-07 11:49:28 +02002151 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002152
2153 return 1;
2154}
2155
Avi Kivity851ba692009-08-24 11:10:17 +03002156static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002157{
2158 if (nested_svm_check_permissions(svm))
2159 return 1;
2160
2161 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2162 skip_emulated_instruction(&svm->vcpu);
2163
Joerg Roedel2af91942009-08-07 11:49:28 +02002164 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002165
2166 /* After a CLGI no interrupts should come */
2167 svm_clear_vintr(svm);
2168 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2169
2170 return 1;
2171}
2172
Avi Kivity851ba692009-08-24 11:10:17 +03002173static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002174{
2175 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002176
Joerg Roedelec1ff792009-10-09 16:08:31 +02002177 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
2178 vcpu->arch.regs[VCPU_REGS_RAX]);
2179
Alexander Grafff092382009-06-15 15:21:24 +02002180 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2181 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
2182
2183 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2184 skip_emulated_instruction(&svm->vcpu);
2185 return 1;
2186}
2187
Joerg Roedel532a46b2009-10-09 16:08:32 +02002188static int skinit_interception(struct vcpu_svm *svm)
2189{
2190 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
2191
2192 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2193 return 1;
2194}
2195
Avi Kivity851ba692009-08-24 11:10:17 +03002196static int invalid_op_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002197{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002198 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002199 return 1;
2200}
2201
Avi Kivity851ba692009-08-24 11:10:17 +03002202static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002203{
Izik Eidus37817f22008-03-24 23:14:53 +02002204 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002205 int reason;
2206 int int_type = svm->vmcb->control.exit_int_info &
2207 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002208 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002209 uint32_t type =
2210 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2211 uint32_t idt_v =
2212 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Izik Eidus37817f22008-03-24 23:14:53 +02002213
2214 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002215
Izik Eidus37817f22008-03-24 23:14:53 +02002216 if (svm->vmcb->control.exit_info_2 &
2217 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002218 reason = TASK_SWITCH_IRET;
2219 else if (svm->vmcb->control.exit_info_2 &
2220 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2221 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002222 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002223 reason = TASK_SWITCH_GATE;
2224 else
2225 reason = TASK_SWITCH_CALL;
2226
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002227 if (reason == TASK_SWITCH_GATE) {
2228 switch (type) {
2229 case SVM_EXITINTINFO_TYPE_NMI:
2230 svm->vcpu.arch.nmi_injected = false;
2231 break;
2232 case SVM_EXITINTINFO_TYPE_EXEPT:
2233 kvm_clear_exception_queue(&svm->vcpu);
2234 break;
2235 case SVM_EXITINTINFO_TYPE_INTR:
2236 kvm_clear_interrupt_queue(&svm->vcpu);
2237 break;
2238 default:
2239 break;
2240 }
2241 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002242
Gleb Natapov8317c292009-04-12 13:37:02 +03002243 if (reason != TASK_SWITCH_GATE ||
2244 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2245 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002246 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2247 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002248
2249 return kvm_task_switch(&svm->vcpu, tss_selector, reason);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002250}
2251
Avi Kivity851ba692009-08-24 11:10:17 +03002252static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002253{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002254 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002255 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002256 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002257}
2258
Avi Kivity851ba692009-08-24 11:10:17 +03002259static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002260{
2261 ++svm->vcpu.stat.nmi_window_exits;
2262 svm->vmcb->control.intercept &= ~(1UL << INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002263 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002264 return 1;
2265}
2266
Avi Kivity851ba692009-08-24 11:10:17 +03002267static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002268{
Avi Kivity851ba692009-08-24 11:10:17 +03002269 if (emulate_instruction(&svm->vcpu, 0, 0, 0) != EMULATE_DONE)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002270 pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
2271 return 1;
2272}
2273
Avi Kivity851ba692009-08-24 11:10:17 +03002274static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002275{
Avi Kivity851ba692009-08-24 11:10:17 +03002276 if (emulate_instruction(&svm->vcpu, 0, 0, 0) != EMULATE_DONE)
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002277 pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002278 return 1;
2279}
2280
Avi Kivity851ba692009-08-24 11:10:17 +03002281static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01002282{
Avi Kivity851ba692009-08-24 11:10:17 +03002283 struct kvm_run *kvm_run = svm->vcpu.run;
2284
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002285 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
2286 /* instruction emulation calls kvm_set_cr8() */
Avi Kivity851ba692009-08-24 11:10:17 +03002287 emulate_instruction(&svm->vcpu, 0, 0, 0);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002288 if (irqchip_in_kernel(svm->vcpu.kvm)) {
2289 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
Joerg Roedel1d075432007-12-06 21:02:25 +01002290 return 1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002291 }
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002292 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
2293 return 1;
Joerg Roedel1d075432007-12-06 21:02:25 +01002294 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2295 return 0;
2296}
2297
Avi Kivity6aa8b732006-12-10 02:21:36 -08002298static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
2299{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002300 struct vcpu_svm *svm = to_svm(vcpu);
2301
Avi Kivity6aa8b732006-12-10 02:21:36 -08002302 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302303 case MSR_IA32_TSC: {
Joerg Roedel20824f32009-09-16 15:24:18 +02002304 u64 tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002305
Joerg Roedel20824f32009-09-16 15:24:18 +02002306 if (is_nested(svm))
2307 tsc_offset = svm->nested.hsave->control.tsc_offset;
2308 else
2309 tsc_offset = svm->vmcb->control.tsc_offset;
2310
2311 *data = tsc_offset + native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002312 break;
2313 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08002314 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002315 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002316 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08002317#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002318 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002319 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002320 break;
2321 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002322 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002323 break;
2324 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002325 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002326 break;
2327 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002328 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002329 break;
2330#endif
2331 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002332 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333 break;
2334 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002335 *data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002336 break;
2337 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002338 *data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002339 break;
Joerg Roedele0231712010-02-24 18:59:10 +01002340 /*
2341 * Nobody will change the following 5 values in the VMCB so we can
2342 * safely return them on rdmsr. They will always be 0 until LBRV is
2343 * implemented.
2344 */
Joerg Roedela2938c82008-02-13 16:30:28 +01002345 case MSR_IA32_DEBUGCTLMSR:
2346 *data = svm->vmcb->save.dbgctl;
2347 break;
2348 case MSR_IA32_LASTBRANCHFROMIP:
2349 *data = svm->vmcb->save.br_from;
2350 break;
2351 case MSR_IA32_LASTBRANCHTOIP:
2352 *data = svm->vmcb->save.br_to;
2353 break;
2354 case MSR_IA32_LASTINTFROMIP:
2355 *data = svm->vmcb->save.last_excp_from;
2356 break;
2357 case MSR_IA32_LASTINTTOIP:
2358 *data = svm->vmcb->save.last_excp_to;
2359 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002360 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002361 *data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002362 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002363 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002364 *data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002365 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01002366 case MSR_IA32_UCODE_REV:
2367 *data = 0x01000065;
2368 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002369 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002370 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002371 }
2372 return 0;
2373}
2374
Avi Kivity851ba692009-08-24 11:10:17 +03002375static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002376{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002377 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002378 u64 data;
2379
Avi Kivity59200272010-01-25 19:47:02 +02002380 if (svm_get_msr(&svm->vcpu, ecx, &data)) {
2381 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002382 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002383 } else {
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002384 trace_kvm_msr_read(ecx, data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002385
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002386 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002387 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002388 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002389 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002390 }
2391 return 1;
2392}
2393
Joerg Roedel4a810182010-02-24 18:59:15 +01002394static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2395{
2396 struct vcpu_svm *svm = to_svm(vcpu);
2397 int svm_dis, chg_mask;
2398
2399 if (data & ~SVM_VM_CR_VALID_MASK)
2400 return 1;
2401
2402 chg_mask = SVM_VM_CR_VALID_MASK;
2403
2404 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2405 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2406
2407 svm->nested.vm_cr_msr &= ~chg_mask;
2408 svm->nested.vm_cr_msr |= (data & chg_mask);
2409
2410 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2411
2412 /* check for svm_disable while efer.svme is set */
2413 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2414 return 1;
2415
2416 return 0;
2417}
2418
Avi Kivity6aa8b732006-12-10 02:21:36 -08002419static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
2420{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002421 struct vcpu_svm *svm = to_svm(vcpu);
2422
Avi Kivity6aa8b732006-12-10 02:21:36 -08002423 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302424 case MSR_IA32_TSC: {
Joerg Roedel20824f32009-09-16 15:24:18 +02002425 u64 tsc_offset = data - native_read_tsc();
2426 u64 g_tsc_offset = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002427
Joerg Roedel20824f32009-09-16 15:24:18 +02002428 if (is_nested(svm)) {
2429 g_tsc_offset = svm->vmcb->control.tsc_offset -
2430 svm->nested.hsave->control.tsc_offset;
2431 svm->nested.hsave->control.tsc_offset = tsc_offset;
2432 }
2433
2434 svm->vmcb->control.tsc_offset = tsc_offset + g_tsc_offset;
2435
Avi Kivity6aa8b732006-12-10 02:21:36 -08002436 break;
2437 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08002438 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002439 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002440 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08002441#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002442 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002443 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002444 break;
2445 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002446 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002447 break;
2448 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002449 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002450 break;
2451 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002452 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002453 break;
2454#endif
2455 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002456 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002457 break;
2458 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002459 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002460 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002461 break;
2462 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002463 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002464 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002465 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002466 case MSR_IA32_DEBUGCTLMSR:
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002467 if (!svm_has(SVM_FEATURE_LBRV)) {
2468 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002469 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002470 break;
2471 }
2472 if (data & DEBUGCTL_RESERVED_BITS)
2473 return 1;
2474
2475 svm->vmcb->save.dbgctl = data;
2476 if (data & (1ULL<<0))
2477 svm_enable_lbrv(svm);
2478 else
2479 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01002480 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002481 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002482 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002483 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002484 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002485 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002486 case MSR_VM_IGNNE:
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002487 pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
2488 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002489 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002490 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002491 }
2492 return 0;
2493}
2494
Avi Kivity851ba692009-08-24 11:10:17 +03002495static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002496{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002497 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002498 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002499 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002500
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002501
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002502 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Avi Kivity59200272010-01-25 19:47:02 +02002503 if (svm_set_msr(&svm->vcpu, ecx, data)) {
2504 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002505 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002506 } else {
2507 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10002508 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02002509 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002510 return 1;
2511}
2512
Avi Kivity851ba692009-08-24 11:10:17 +03002513static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002514{
Rusty Russelle756fc62007-07-30 20:07:08 +10002515 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03002516 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002517 else
Avi Kivity851ba692009-08-24 11:10:17 +03002518 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002519}
2520
Avi Kivity851ba692009-08-24 11:10:17 +03002521static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08002522{
Avi Kivity851ba692009-08-24 11:10:17 +03002523 struct kvm_run *kvm_run = svm->vcpu.run;
2524
Alexander Graff0b85052008-11-25 20:17:01 +01002525 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03002526 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -08002527 /*
2528 * If the user space waits to inject interrupts, exit as soon as
2529 * possible
2530 */
Gleb Natapov80618232009-04-21 17:44:56 +03002531 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
2532 kvm_run->request_interrupt_window &&
2533 !kvm_cpu_has_interrupt(&svm->vcpu)) {
Rusty Russelle756fc62007-07-30 20:07:08 +10002534 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002535 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
2536 return 0;
2537 }
2538
2539 return 1;
2540}
2541
Mark Langsdorf565d0992009-10-06 14:25:02 -05002542static int pause_interception(struct vcpu_svm *svm)
2543{
2544 kvm_vcpu_on_spin(&(svm->vcpu));
2545 return 1;
2546}
2547
Avi Kivity851ba692009-08-24 11:10:17 +03002548static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
Joerg Roedele0231712010-02-24 18:59:10 +01002549 [SVM_EXIT_READ_CR0] = emulate_on_interception,
2550 [SVM_EXIT_READ_CR3] = emulate_on_interception,
2551 [SVM_EXIT_READ_CR4] = emulate_on_interception,
2552 [SVM_EXIT_READ_CR8] = emulate_on_interception,
Avi Kivityd2251572010-01-06 10:55:27 +02002553 [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002554 [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
2555 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
2556 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
2557 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
2558 [SVM_EXIT_READ_DR0] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002559 [SVM_EXIT_READ_DR1] = emulate_on_interception,
2560 [SVM_EXIT_READ_DR2] = emulate_on_interception,
2561 [SVM_EXIT_READ_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002562 [SVM_EXIT_READ_DR4] = emulate_on_interception,
2563 [SVM_EXIT_READ_DR5] = emulate_on_interception,
2564 [SVM_EXIT_READ_DR6] = emulate_on_interception,
2565 [SVM_EXIT_READ_DR7] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002566 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
2567 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
2568 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
2569 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002570 [SVM_EXIT_WRITE_DR4] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002571 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002572 [SVM_EXIT_WRITE_DR6] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002573 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002574 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
2575 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002576 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002577 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
2578 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
2579 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
2580 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02002581 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002582 [SVM_EXIT_SMI] = nop_on_interception,
2583 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08002584 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002585 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002586 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002587 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05002588 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002589 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03002590 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02002591 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002592 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002593 [SVM_EXIT_MSR] = msr_interception,
2594 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002595 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01002596 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02002597 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01002598 [SVM_EXIT_VMLOAD] = vmload_interception,
2599 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01002600 [SVM_EXIT_STGI] = stgi_interception,
2601 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02002602 [SVM_EXIT_SKINIT] = skinit_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002603 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01002604 [SVM_EXIT_MONITOR] = invalid_op_interception,
2605 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002606 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002607};
2608
Avi Kivity851ba692009-08-24 11:10:17 +03002609static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002610{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002611 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002612 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002613 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002614
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002615 trace_kvm_exit(exit_code, svm->vmcb->save.rip);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002616
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002617 if (unlikely(svm->nested.exit_required)) {
2618 nested_svm_vmexit(svm);
2619 svm->nested.exit_required = false;
2620
2621 return 1;
2622 }
2623
Alexander Grafcf74a782008-11-25 20:17:08 +01002624 if (is_nested(svm)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02002625 int vmexit;
2626
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02002627 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
2628 svm->vmcb->control.exit_info_1,
2629 svm->vmcb->control.exit_info_2,
2630 svm->vmcb->control.exit_int_info,
2631 svm->vmcb->control.exit_int_info_err);
2632
Joerg Roedel410e4d52009-08-07 11:49:44 +02002633 vmexit = nested_svm_exit_special(svm);
2634
2635 if (vmexit == NESTED_EXIT_CONTINUE)
2636 vmexit = nested_svm_exit_handled(svm);
2637
2638 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01002639 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01002640 }
2641
Joerg Roedela5c38322009-08-07 11:49:32 +02002642 svm_complete_interrupts(svm);
2643
Avi Kivity888f9f32010-01-10 12:14:04 +02002644 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR0_MASK))
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002645 vcpu->arch.cr0 = svm->vmcb->save.cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02002646 if (npt_enabled)
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002647 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002648
2649 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
2650 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2651 kvm_run->fail_entry.hardware_entry_failure_reason
2652 = svm->vmcb->control.exit_code;
2653 return 0;
2654 }
2655
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002656 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002657 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002658 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002659 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
2660 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002661 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002662 exit_code);
2663
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02002664 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08002665 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002666 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03002667 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002668 return 0;
2669 }
2670
Avi Kivity851ba692009-08-24 11:10:17 +03002671 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002672}
2673
2674static void reload_tss(struct kvm_vcpu *vcpu)
2675{
2676 int cpu = raw_smp_processor_id();
2677
Tejun Heo0fe1e002009-10-29 22:34:14 +09002678 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
2679 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002680 load_TR_desc();
2681}
2682
Rusty Russelle756fc62007-07-30 20:07:08 +10002683static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002684{
2685 int cpu = raw_smp_processor_id();
2686
Tejun Heo0fe1e002009-10-29 22:34:14 +09002687 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002688
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002689 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03002690 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09002691 if (svm->asid_generation != sd->asid_generation)
2692 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002693}
2694
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002695static void svm_inject_nmi(struct kvm_vcpu *vcpu)
2696{
2697 struct vcpu_svm *svm = to_svm(vcpu);
2698
2699 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
2700 vcpu->arch.hflags |= HF_NMI_MASK;
2701 svm->vmcb->control.intercept |= (1UL << INTERCEPT_IRET);
2702 ++vcpu->stat.nmi_injections;
2703}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002704
Eddie Dong85f455f2007-07-06 12:20:49 +03002705static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002706{
2707 struct vmcb_control_area *control;
2708
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002709 trace_kvm_inj_virq(irq);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002710
Avi Kivityfa89a812008-09-01 15:57:51 +03002711 ++svm->vcpu.stat.irq_injections;
Rusty Russelle756fc62007-07-30 20:07:08 +10002712 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03002713 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002714 control->int_ctl &= ~V_INTR_PRIO_MASK;
2715 control->int_ctl |= V_IRQ_MASK |
2716 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
2717}
2718
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002719static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03002720{
2721 struct vcpu_svm *svm = to_svm(vcpu);
2722
Joerg Roedel2af91942009-08-07 11:49:28 +02002723 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01002724
Alexander Graf219b65d2009-06-15 15:21:25 +02002725 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
2726 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002727}
2728
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002729static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
2730{
2731 struct vcpu_svm *svm = to_svm(vcpu);
2732
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002733 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
2734 return;
2735
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002736 if (irr == -1)
2737 return;
2738
2739 if (tpr >= irr)
2740 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK;
2741}
2742
2743static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02002744{
2745 struct vcpu_svm *svm = to_svm(vcpu);
2746 struct vmcb *vmcb = svm->vmcb;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002747 return !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
2748 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
Joerg Roedelaaacfc92008-04-16 16:51:18 +02002749}
2750
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002751static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
2752{
2753 struct vcpu_svm *svm = to_svm(vcpu);
2754
2755 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
2756}
2757
2758static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
2759{
2760 struct vcpu_svm *svm = to_svm(vcpu);
2761
2762 if (masked) {
2763 svm->vcpu.arch.hflags |= HF_NMI_MASK;
2764 svm->vmcb->control.intercept |= (1UL << INTERCEPT_IRET);
2765 } else {
2766 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
2767 svm->vmcb->control.intercept &= ~(1UL << INTERCEPT_IRET);
2768 }
2769}
2770
Gleb Natapov78646122009-03-23 12:12:11 +02002771static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
2772{
2773 struct vcpu_svm *svm = to_svm(vcpu);
2774 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02002775 int ret;
2776
2777 if (!gif_set(svm) ||
2778 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
2779 return 0;
2780
2781 ret = !!(vmcb->save.rflags & X86_EFLAGS_IF);
2782
2783 if (is_nested(svm))
2784 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
2785
2786 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02002787}
2788
Gleb Natapov9222be12009-04-23 17:14:37 +03002789static void enable_irq_window(struct kvm_vcpu *vcpu)
2790{
Alexander Graf219b65d2009-06-15 15:21:25 +02002791 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02002792
Joerg Roedele0231712010-02-24 18:59:10 +01002793 /*
2794 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
2795 * 1, because that's a separate STGI/VMRUN intercept. The next time we
2796 * get that intercept, this function will be called again though and
2797 * we'll get the vintr intercept.
2798 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01002799 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02002800 svm_set_vintr(svm);
2801 svm_inject_irq(svm, 0x0);
2802 }
Gleb Natapov9222be12009-04-23 17:14:37 +03002803}
2804
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002805static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002806{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002807 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03002808
Gleb Natapov44c11432009-05-11 13:35:52 +03002809 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
2810 == HF_NMI_MASK)
2811 return; /* IRET will cause a vm exit */
2812
Joerg Roedele0231712010-02-24 18:59:10 +01002813 /*
2814 * Something prevents NMI from been injected. Single step over possible
2815 * problem (IRET or exception injection or interrupt shadow)
2816 */
Joerg Roedel887f5002010-02-24 18:59:12 +01002817 if (gif_set(svm) && nested_svm_nmi(svm)) {
2818 svm->nmi_singlestep = true;
2819 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
2820 update_db_intercept(vcpu);
2821 }
Eddie Dong85f455f2007-07-06 12:20:49 +03002822}
2823
Izik Eiduscbc94022007-10-25 00:29:55 +02002824static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
2825{
2826 return 0;
2827}
2828
Avi Kivityd9e368d2007-06-07 19:18:30 +03002829static void svm_flush_tlb(struct kvm_vcpu *vcpu)
2830{
2831 force_new_asid(vcpu);
2832}
2833
Avi Kivity04d2cc72007-09-10 18:10:54 +03002834static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
2835{
2836}
2837
Joerg Roedeld7bf8222008-04-16 16:51:17 +02002838static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
2839{
2840 struct vcpu_svm *svm = to_svm(vcpu);
2841
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002842 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
2843 return;
2844
Joerg Roedeld7bf8222008-04-16 16:51:17 +02002845 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) {
2846 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03002847 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02002848 }
2849}
2850
Joerg Roedel649d6862008-04-16 16:51:15 +02002851static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
2852{
2853 struct vcpu_svm *svm = to_svm(vcpu);
2854 u64 cr8;
2855
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002856 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
2857 return;
2858
Joerg Roedel649d6862008-04-16 16:51:15 +02002859 cr8 = kvm_get_cr8(vcpu);
2860 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
2861 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
2862}
2863
Gleb Natapov9222be12009-04-23 17:14:37 +03002864static void svm_complete_interrupts(struct vcpu_svm *svm)
2865{
2866 u8 vector;
2867 int type;
2868 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01002869 unsigned int3_injected = svm->int3_injected;
2870
2871 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03002872
Gleb Natapov44c11432009-05-11 13:35:52 +03002873 if (svm->vcpu.arch.hflags & HF_IRET_MASK)
2874 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
2875
Gleb Natapov9222be12009-04-23 17:14:37 +03002876 svm->vcpu.arch.nmi_injected = false;
2877 kvm_clear_exception_queue(&svm->vcpu);
2878 kvm_clear_interrupt_queue(&svm->vcpu);
2879
2880 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
2881 return;
2882
2883 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
2884 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
2885
2886 switch (type) {
2887 case SVM_EXITINTINFO_TYPE_NMI:
2888 svm->vcpu.arch.nmi_injected = true;
2889 break;
2890 case SVM_EXITINTINFO_TYPE_EXEPT:
Alexander Graf219b65d2009-06-15 15:21:25 +02002891 if (is_nested(svm))
2892 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01002893 /*
2894 * In case of software exceptions, do not reinject the vector,
2895 * but re-execute the instruction instead. Rewind RIP first
2896 * if we emulated INT3 before.
2897 */
2898 if (kvm_exception_is_soft(vector)) {
2899 if (vector == BP_VECTOR && int3_injected &&
2900 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
2901 kvm_rip_write(&svm->vcpu,
2902 kvm_rip_read(&svm->vcpu) -
2903 int3_injected);
Gleb Natapov9222be12009-04-23 17:14:37 +03002904 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01002905 }
Gleb Natapov9222be12009-04-23 17:14:37 +03002906 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
2907 u32 err = svm->vmcb->control.exit_int_info_err;
2908 kvm_queue_exception_e(&svm->vcpu, vector, err);
2909
2910 } else
2911 kvm_queue_exception(&svm->vcpu, vector);
2912 break;
2913 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002914 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03002915 break;
2916 default:
2917 break;
2918 }
2919}
2920
Avi Kivity80e31d42008-07-14 14:44:59 +03002921#ifdef CONFIG_X86_64
2922#define R "r"
2923#else
2924#define R "e"
2925#endif
2926
Avi Kivity851ba692009-08-24 11:10:17 +03002927static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002928{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002929 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002930 u16 fs_selector;
2931 u16 gs_selector;
2932 u16 ldt_selector;
Avi Kivityd9e368d2007-06-07 19:18:30 +03002933
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002934 /*
2935 * A vmexit emulation is required before the vcpu can be executed
2936 * again.
2937 */
2938 if (unlikely(svm->nested.exit_required))
2939 return;
2940
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002941 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
2942 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
2943 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
2944
Rusty Russelle756fc62007-07-30 20:07:08 +10002945 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002946
Joerg Roedel649d6862008-04-16 16:51:15 +02002947 sync_lapic_to_cr8(vcpu);
2948
Avi Kivity6aa8b732006-12-10 02:21:36 -08002949 save_host_msrs(vcpu);
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002950 fs_selector = kvm_read_fs();
2951 gs_selector = kvm_read_gs();
2952 ldt_selector = kvm_read_ldt();
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02002953 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002954 /* required for live migration with NPT */
2955 if (npt_enabled)
2956 svm->vmcb->save.cr3 = vcpu->arch.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002957
Avi Kivity04d2cc72007-09-10 18:10:54 +03002958 clgi();
2959
2960 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08002961
Avi Kivity6aa8b732006-12-10 02:21:36 -08002962 asm volatile (
Avi Kivity80e31d42008-07-14 14:44:59 +03002963 "push %%"R"bp; \n\t"
2964 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
2965 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
2966 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
2967 "mov %c[rsi](%[svm]), %%"R"si \n\t"
2968 "mov %c[rdi](%[svm]), %%"R"di \n\t"
2969 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002970#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002971 "mov %c[r8](%[svm]), %%r8 \n\t"
2972 "mov %c[r9](%[svm]), %%r9 \n\t"
2973 "mov %c[r10](%[svm]), %%r10 \n\t"
2974 "mov %c[r11](%[svm]), %%r11 \n\t"
2975 "mov %c[r12](%[svm]), %%r12 \n\t"
2976 "mov %c[r13](%[svm]), %%r13 \n\t"
2977 "mov %c[r14](%[svm]), %%r14 \n\t"
2978 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002979#endif
2980
Avi Kivity6aa8b732006-12-10 02:21:36 -08002981 /* Enter guest mode */
Avi Kivity80e31d42008-07-14 14:44:59 +03002982 "push %%"R"ax \n\t"
2983 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002984 __ex(SVM_VMLOAD) "\n\t"
2985 __ex(SVM_VMRUN) "\n\t"
2986 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity80e31d42008-07-14 14:44:59 +03002987 "pop %%"R"ax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002988
2989 /* Save guest registers, load host registers */
Avi Kivity80e31d42008-07-14 14:44:59 +03002990 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
2991 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
2992 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
2993 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
2994 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
2995 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002996#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002997 "mov %%r8, %c[r8](%[svm]) \n\t"
2998 "mov %%r9, %c[r9](%[svm]) \n\t"
2999 "mov %%r10, %c[r10](%[svm]) \n\t"
3000 "mov %%r11, %c[r11](%[svm]) \n\t"
3001 "mov %%r12, %c[r12](%[svm]) \n\t"
3002 "mov %%r13, %c[r13](%[svm]) \n\t"
3003 "mov %%r14, %c[r14](%[svm]) \n\t"
3004 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003005#endif
Avi Kivity80e31d42008-07-14 14:44:59 +03003006 "pop %%"R"bp"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003007 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003008 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003009 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003010 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
3011 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
3012 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
3013 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
3014 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
3015 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003016#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003017 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
3018 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
3019 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
3020 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
3021 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
3022 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
3023 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
3024 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003025#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02003026 : "cc", "memory"
Avi Kivity80e31d42008-07-14 14:44:59 +03003027 , R"bx", R"cx", R"dx", R"si", R"di"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003028#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02003029 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
3030#endif
3031 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003032
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003033 vcpu->arch.cr2 = svm->vmcb->save.cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003034 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3035 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3036 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003037
Avi Kivityd6e88ae2008-07-10 16:53:33 +03003038 kvm_load_fs(fs_selector);
3039 kvm_load_gs(gs_selector);
3040 kvm_load_ldt(ldt_selector);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003041 load_host_msrs(vcpu);
3042
3043 reload_tss(vcpu);
3044
Avi Kivity56ba47d2007-11-07 17:14:18 +02003045 local_irq_disable();
3046
3047 stgi();
3048
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003049 sync_cr8_to_lapic(vcpu);
3050
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003051 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003052
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003053 if (npt_enabled) {
3054 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3055 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3056 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003057}
3058
Avi Kivity80e31d42008-07-14 14:44:59 +03003059#undef R
3060
Avi Kivity6aa8b732006-12-10 02:21:36 -08003061static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3062{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003063 struct vcpu_svm *svm = to_svm(vcpu);
3064
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003065 if (npt_enabled) {
3066 svm->vmcb->control.nested_cr3 = root;
3067 force_new_asid(vcpu);
3068 return;
3069 }
3070
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003071 svm->vmcb->save.cr3 = root;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003072 force_new_asid(vcpu);
3073}
3074
Avi Kivity6aa8b732006-12-10 02:21:36 -08003075static int is_disabled(void)
3076{
Joerg Roedel6031a612007-06-22 12:29:50 +03003077 u64 vm_cr;
3078
3079 rdmsrl(MSR_VM_CR, vm_cr);
3080 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3081 return 1;
3082
Avi Kivity6aa8b732006-12-10 02:21:36 -08003083 return 0;
3084}
3085
Ingo Molnar102d8322007-02-19 14:37:47 +02003086static void
3087svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3088{
3089 /*
3090 * Patch in the VMMCALL instruction:
3091 */
3092 hypercall[0] = 0x0f;
3093 hypercall[1] = 0x01;
3094 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003095}
3096
Yang, Sheng002c7f72007-07-31 14:23:01 +03003097static void svm_check_processor_compat(void *rtn)
3098{
3099 *(int *)rtn = 0;
3100}
3101
Avi Kivity774ead32007-12-26 13:57:04 +02003102static bool svm_cpu_has_accelerated_tpr(void)
3103{
3104 return false;
3105}
3106
Sheng Yang67253af2008-04-25 10:20:22 +08003107static int get_npt_level(void)
3108{
3109#ifdef CONFIG_X86_64
3110 return PT64_ROOT_LEVEL;
3111#else
3112 return PT32E_ROOT_LEVEL;
3113#endif
3114}
3115
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003116static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003117{
3118 return 0;
3119}
3120
Sheng Yang0e851882009-12-18 16:48:46 +08003121static void svm_cpuid_update(struct kvm_vcpu *vcpu)
3122{
3123}
3124
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003125static const struct trace_print_flags svm_exit_reasons_str[] = {
Joerg Roedele0231712010-02-24 18:59:10 +01003126 { SVM_EXIT_READ_CR0, "read_cr0" },
3127 { SVM_EXIT_READ_CR3, "read_cr3" },
3128 { SVM_EXIT_READ_CR4, "read_cr4" },
3129 { SVM_EXIT_READ_CR8, "read_cr8" },
3130 { SVM_EXIT_WRITE_CR0, "write_cr0" },
3131 { SVM_EXIT_WRITE_CR3, "write_cr3" },
3132 { SVM_EXIT_WRITE_CR4, "write_cr4" },
3133 { SVM_EXIT_WRITE_CR8, "write_cr8" },
3134 { SVM_EXIT_READ_DR0, "read_dr0" },
3135 { SVM_EXIT_READ_DR1, "read_dr1" },
3136 { SVM_EXIT_READ_DR2, "read_dr2" },
3137 { SVM_EXIT_READ_DR3, "read_dr3" },
3138 { SVM_EXIT_WRITE_DR0, "write_dr0" },
3139 { SVM_EXIT_WRITE_DR1, "write_dr1" },
3140 { SVM_EXIT_WRITE_DR2, "write_dr2" },
3141 { SVM_EXIT_WRITE_DR3, "write_dr3" },
3142 { SVM_EXIT_WRITE_DR5, "write_dr5" },
3143 { SVM_EXIT_WRITE_DR7, "write_dr7" },
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003144 { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
3145 { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
3146 { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
3147 { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
3148 { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
3149 { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
3150 { SVM_EXIT_INTR, "interrupt" },
3151 { SVM_EXIT_NMI, "nmi" },
3152 { SVM_EXIT_SMI, "smi" },
3153 { SVM_EXIT_INIT, "init" },
3154 { SVM_EXIT_VINTR, "vintr" },
3155 { SVM_EXIT_CPUID, "cpuid" },
3156 { SVM_EXIT_INVD, "invd" },
3157 { SVM_EXIT_HLT, "hlt" },
3158 { SVM_EXIT_INVLPG, "invlpg" },
3159 { SVM_EXIT_INVLPGA, "invlpga" },
3160 { SVM_EXIT_IOIO, "io" },
3161 { SVM_EXIT_MSR, "msr" },
3162 { SVM_EXIT_TASK_SWITCH, "task_switch" },
3163 { SVM_EXIT_SHUTDOWN, "shutdown" },
3164 { SVM_EXIT_VMRUN, "vmrun" },
3165 { SVM_EXIT_VMMCALL, "hypercall" },
3166 { SVM_EXIT_VMLOAD, "vmload" },
3167 { SVM_EXIT_VMSAVE, "vmsave" },
3168 { SVM_EXIT_STGI, "stgi" },
3169 { SVM_EXIT_CLGI, "clgi" },
3170 { SVM_EXIT_SKINIT, "skinit" },
3171 { SVM_EXIT_WBINVD, "wbinvd" },
3172 { SVM_EXIT_MONITOR, "monitor" },
3173 { SVM_EXIT_MWAIT, "mwait" },
3174 { SVM_EXIT_NPF, "npf" },
3175 { -1, NULL }
3176};
3177
Sheng Yang17cc3932010-01-05 19:02:27 +08003178static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02003179{
Sheng Yang17cc3932010-01-05 19:02:27 +08003180 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02003181}
3182
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003183static bool svm_rdtscp_supported(void)
3184{
3185 return false;
3186}
3187
Avi Kivity02daab22009-12-30 12:40:26 +02003188static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
3189{
3190 struct vcpu_svm *svm = to_svm(vcpu);
3191
Avi Kivity02daab22009-12-30 12:40:26 +02003192 svm->vmcb->control.intercept_exceptions |= 1 << NM_VECTOR;
Joerg Roedel66a562f2010-02-19 16:23:08 +01003193 if (is_nested(svm))
3194 svm->nested.hsave->control.intercept_exceptions |= 1 << NM_VECTOR;
3195 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02003196}
3197
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003198static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003199 .cpu_has_kvm_support = has_svm,
3200 .disabled_by_bios = is_disabled,
3201 .hardware_setup = svm_hardware_setup,
3202 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003203 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003204 .hardware_enable = svm_hardware_enable,
3205 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003206 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003207
3208 .vcpu_create = svm_create_vcpu,
3209 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003210 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003211
Avi Kivity04d2cc72007-09-10 18:10:54 +03003212 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003213 .vcpu_load = svm_vcpu_load,
3214 .vcpu_put = svm_vcpu_put,
3215
3216 .set_guest_debug = svm_guest_debug,
3217 .get_msr = svm_get_msr,
3218 .set_msr = svm_set_msr,
3219 .get_segment_base = svm_get_segment_base,
3220 .get_segment = svm_get_segment,
3221 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003222 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10003223 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02003224 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003225 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003226 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003227 .set_cr3 = svm_set_cr3,
3228 .set_cr4 = svm_set_cr4,
3229 .set_efer = svm_set_efer,
3230 .get_idt = svm_get_idt,
3231 .set_idt = svm_set_idt,
3232 .get_gdt = svm_get_gdt,
3233 .set_gdt = svm_set_gdt,
3234 .get_dr = svm_get_dr,
3235 .set_dr = svm_set_dr,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003236 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003237 .get_rflags = svm_get_rflags,
3238 .set_rflags = svm_set_rflags,
Avi Kivity6b52d182010-01-21 15:31:47 +02003239 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02003240 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003241
Avi Kivity6aa8b732006-12-10 02:21:36 -08003242 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003243
Avi Kivity6aa8b732006-12-10 02:21:36 -08003244 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003245 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003246 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04003247 .set_interrupt_shadow = svm_set_interrupt_shadow,
3248 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02003249 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003250 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003251 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02003252 .queue_exception = svm_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02003253 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003254 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003255 .get_nmi_mask = svm_get_nmi_mask,
3256 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003257 .enable_nmi_window = enable_nmi_window,
3258 .enable_irq_window = enable_irq_window,
3259 .update_cr8_intercept = update_cr8_intercept,
Izik Eiduscbc94022007-10-25 00:29:55 +02003260
3261 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003262 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003263 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003264
3265 .exit_reasons_str = svm_exit_reasons_str,
Sheng Yang17cc3932010-01-05 19:02:27 +08003266 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08003267
3268 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003269
3270 .rdtscp_supported = svm_rdtscp_supported,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003271};
3272
3273static int __init svm_init(void)
3274{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003275 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Rusty Russellc16f8622007-07-30 21:12:19 +10003276 THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003277}
3278
3279static void __exit svm_exit(void)
3280{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003281 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003282}
3283
3284module_init(svm_init)
3285module_exit(svm_exit)