blob: 50387860a53ca261c8c73b35c10590a384beb0d5 [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.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02007 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08008 *
9 * Authors:
10 * Yaniv Kamay <yaniv@qumranet.com>
11 * Avi Kivity <avi@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
Avi Kivityedf88412007-12-16 11:02:48 +020017#include <linux/kvm_host.h>
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030021#include "kvm_cache_regs.h"
Gleb Natapovfe4c7b12009-03-23 11:23:18 +020022#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020025#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/vmalloc.h>
27#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040028#include <linux/sched.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030029#include <linux/ftrace_event.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080031
Joerg Roedel67ec6602010-05-17 14:43:35 +020032#include <asm/tlbflush.h>
Avi Kivitye4956062007-06-28 14:15:57 -040033#include <asm/desc.h>
Gleb Natapov631bc482010-10-14 11:22:52 +020034#include <asm/kvm_para.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080035
Eduardo Habkost63d11422008-11-17 19:03:20 -020036#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030037#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020038
Avi Kivity4ecac3f2008-05-13 13:23:38 +030039#define __ex(x) __kvm_handle_fault_on_reboot(x)
40
Avi Kivity6aa8b732006-12-10 02:21:36 -080041MODULE_AUTHOR("Qumranet");
42MODULE_LICENSE("GPL");
43
44#define IOPM_ALLOC_ORDER 2
45#define MSRPM_ALLOC_ORDER 1
46
Avi Kivity6aa8b732006-12-10 02:21:36 -080047#define SEG_TYPE_LDT 2
48#define SEG_TYPE_BUSY_TSS16 3
49
Andre Przywara6bc31bd2010-04-11 23:07:28 +020050#define SVM_FEATURE_NPT (1 << 0)
51#define SVM_FEATURE_LBRV (1 << 1)
52#define SVM_FEATURE_SVML (1 << 2)
53#define SVM_FEATURE_NRIP (1 << 3)
54#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030055
Joerg Roedel410e4d52009-08-07 11:49:44 +020056#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
57#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
58#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
59
Joerg Roedel24e09cb2008-02-13 18:58:47 +010060#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
61
Joerg Roedel67ec6602010-05-17 14:43:35 +020062static bool erratum_383_found __read_mostly;
63
Avi Kivity6c8166a2009-05-31 18:15:37 +030064static const u32 host_save_user_msrs[] = {
65#ifdef CONFIG_X86_64
66 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
67 MSR_FS_BASE,
68#endif
69 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
70};
71
72#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
73
74struct kvm_vcpu;
75
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020076struct nested_state {
77 struct vmcb *hsave;
78 u64 hsave_msr;
Joerg Roedel4a810182010-02-24 18:59:15 +010079 u64 vm_cr_msr;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020080 u64 vmcb;
81
82 /* These are the merged vectors */
83 u32 *msrpm;
84
85 /* gpa pointers to the real vectors */
86 u64 vmcb_msrpm;
Joerg Roedelce2ac082010-03-01 15:34:39 +010087 u64 vmcb_iopm;
Joerg Roedelaad42c62009-08-07 11:49:34 +020088
Joerg Roedelcd3ff652009-10-09 16:08:26 +020089 /* A VMEXIT is required but not yet emulated */
90 bool exit_required;
91
Joerg Roedelcda00082010-09-02 17:29:46 +020092 /*
93 * If we vmexit during an instruction emulation we need this to restore
94 * the l1 guest rip after the emulation
95 */
96 unsigned long vmexit_rip;
97 unsigned long vmexit_rsp;
98 unsigned long vmexit_rax;
99
Joerg Roedelaad42c62009-08-07 11:49:34 +0200100 /* cache for intercepts of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100101 u32 intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100102 u32 intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200103 u32 intercept_exceptions;
104 u64 intercept;
105
Joerg Roedel5bd2edc2010-09-10 17:31:02 +0200106 /* Nested Paging related state */
107 u64 nested_cr3;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200108};
109
Joerg Roedel323c3d82010-03-01 15:34:37 +0100110#define MSRPM_OFFSETS 16
111static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
112
Avi Kivity6c8166a2009-05-31 18:15:37 +0300113struct vcpu_svm {
114 struct kvm_vcpu vcpu;
115 struct vmcb *vmcb;
116 unsigned long vmcb_pa;
117 struct svm_cpu_data *svm_data;
118 uint64_t asid_generation;
119 uint64_t sysenter_esp;
120 uint64_t sysenter_eip;
121
122 u64 next_rip;
123
124 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
Avi Kivityafe9e662010-10-21 12:20:32 +0200125 struct {
Avi Kivitydacccfd2010-10-21 12:20:33 +0200126 u16 fs;
127 u16 gs;
128 u16 ldt;
Avi Kivityafe9e662010-10-21 12:20:32 +0200129 u64 gs_base;
130 } host;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300131
132 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300133
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200134 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200135
136 bool nmi_singlestep;
Jan Kiszka66b71382010-02-23 17:47:56 +0100137
138 unsigned int3_injected;
139 unsigned long int3_rip;
Gleb Natapov631bc482010-10-14 11:22:52 +0200140 u32 apf_reason;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300141};
142
Joerg Roedel455716f2010-03-01 15:34:35 +0100143#define MSR_INVALID 0xffffffffU
144
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100145static struct svm_direct_access_msrs {
146 u32 index; /* Index of the MSR */
147 bool always; /* True if intercept is always on */
148} direct_access_msrs[] = {
Brian Gerst8c065852010-07-17 09:03:26 -0400149 { .index = MSR_STAR, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100150 { .index = MSR_IA32_SYSENTER_CS, .always = true },
151#ifdef CONFIG_X86_64
152 { .index = MSR_GS_BASE, .always = true },
153 { .index = MSR_FS_BASE, .always = true },
154 { .index = MSR_KERNEL_GS_BASE, .always = true },
155 { .index = MSR_LSTAR, .always = true },
156 { .index = MSR_CSTAR, .always = true },
157 { .index = MSR_SYSCALL_MASK, .always = true },
158#endif
159 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
160 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
161 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
162 { .index = MSR_IA32_LASTINTTOIP, .always = false },
163 { .index = MSR_INVALID, .always = false },
Avi Kivity6aa8b732006-12-10 02:21:36 -0800164};
165
166/* enable NPT for AMD64 and X86 with PAE */
167#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
168static bool npt_enabled = true;
169#else
Joerg Roedele0231712010-02-24 18:59:10 +0100170static bool npt_enabled;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800171#endif
172static int npt = 1;
173
174module_param(npt, int, S_IRUGO);
175
Joerg Roedel4b6e4dc2009-08-07 11:49:48 +0200176static int nested = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800177module_param(nested, int, S_IRUGO);
178
179static void svm_flush_tlb(struct kvm_vcpu *vcpu);
Joerg Roedela5c38322009-08-07 11:49:32 +0200180static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800181
Joerg Roedel410e4d52009-08-07 11:49:44 +0200182static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100183static int nested_svm_intercept(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800184static int nested_svm_vmexit(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800185static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
186 bool has_error_code, u32 error_code);
187
188static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
189{
190 return container_of(vcpu, struct vcpu_svm, vcpu);
191}
192
Joerg Roedel384c6362010-11-30 18:03:56 +0100193static void recalc_intercepts(struct vcpu_svm *svm)
194{
195 struct vmcb_control_area *c, *h;
196 struct nested_state *g;
197
198 if (!is_guest_mode(&svm->vcpu))
199 return;
200
201 c = &svm->vmcb->control;
202 h = &svm->nested.hsave->control;
203 g = &svm->nested;
204
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100205 c->intercept_cr = h->intercept_cr | g->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100206 c->intercept_dr = h->intercept_dr | g->intercept_dr;
Joerg Roedel384c6362010-11-30 18:03:56 +0100207 c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
208 c->intercept = h->intercept | g->intercept;
209}
210
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100211static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
212{
213 if (is_guest_mode(&svm->vcpu))
214 return svm->nested.hsave;
215 else
216 return svm->vmcb;
217}
218
219static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
220{
221 struct vmcb *vmcb = get_host_vmcb(svm);
222
223 vmcb->control.intercept_cr |= (1U << bit);
224
225 recalc_intercepts(svm);
226}
227
228static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
229{
230 struct vmcb *vmcb = get_host_vmcb(svm);
231
232 vmcb->control.intercept_cr &= ~(1U << bit);
233
234 recalc_intercepts(svm);
235}
236
237static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
238{
239 struct vmcb *vmcb = get_host_vmcb(svm);
240
241 return vmcb->control.intercept_cr & (1U << bit);
242}
243
Joerg Roedel3aed0412010-11-30 18:03:58 +0100244static inline void set_dr_intercept(struct vcpu_svm *svm, int bit)
245{
246 struct vmcb *vmcb = get_host_vmcb(svm);
247
248 vmcb->control.intercept_dr |= (1U << bit);
249
250 recalc_intercepts(svm);
251}
252
253static inline void clr_dr_intercept(struct vcpu_svm *svm, int bit)
254{
255 struct vmcb *vmcb = get_host_vmcb(svm);
256
257 vmcb->control.intercept_dr &= ~(1U << bit);
258
259 recalc_intercepts(svm);
260}
261
Joerg Roedel18c918c2010-11-30 18:03:59 +0100262static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
263{
264 struct vmcb *vmcb = get_host_vmcb(svm);
265
266 vmcb->control.intercept_exceptions |= (1U << bit);
267
268 recalc_intercepts(svm);
269}
270
271static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
272{
273 struct vmcb *vmcb = get_host_vmcb(svm);
274
275 vmcb->control.intercept_exceptions &= ~(1U << bit);
276
277 recalc_intercepts(svm);
278}
279
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100280static inline void set_intercept(struct vcpu_svm *svm, int bit)
281{
282 struct vmcb *vmcb = get_host_vmcb(svm);
283
284 vmcb->control.intercept |= (1ULL << bit);
285
286 recalc_intercepts(svm);
287}
288
289static inline void clr_intercept(struct vcpu_svm *svm, int bit)
290{
291 struct vmcb *vmcb = get_host_vmcb(svm);
292
293 vmcb->control.intercept &= ~(1ULL << bit);
294
295 recalc_intercepts(svm);
296}
297
Joerg Roedel2af91942009-08-07 11:49:28 +0200298static inline void enable_gif(struct vcpu_svm *svm)
299{
300 svm->vcpu.arch.hflags |= HF_GIF_MASK;
301}
302
303static inline void disable_gif(struct vcpu_svm *svm)
304{
305 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
306}
307
308static inline bool gif_set(struct vcpu_svm *svm)
309{
310 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
311}
312
Avi Kivity6aa8b732006-12-10 02:21:36 -0800313static unsigned long iopm_base;
314
315struct kvm_ldttss_desc {
316 u16 limit0;
317 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100318 unsigned base1:8, type:5, dpl:2, p:1;
319 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800320 u32 base3;
321 u32 zero1;
322} __attribute__((packed));
323
324struct svm_cpu_data {
325 int cpu;
326
Avi Kivity5008fdf2007-04-02 13:05:50 +0300327 u64 asid_generation;
328 u32 max_asid;
329 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800330 struct kvm_ldttss_desc *tss_desc;
331
332 struct page *save_area;
333};
334
335static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
Joerg Roedel80b77062007-03-30 17:02:14 +0300336static uint32_t svm_features;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800337
338struct svm_init_data {
339 int cpu;
340 int r;
341};
342
343static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
344
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200345#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800346#define MSRS_RANGE_SIZE 2048
347#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
348
Joerg Roedel455716f2010-03-01 15:34:35 +0100349static u32 svm_msrpm_offset(u32 msr)
350{
351 u32 offset;
352 int i;
353
354 for (i = 0; i < NUM_MSR_MAPS; i++) {
355 if (msr < msrpm_ranges[i] ||
356 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
357 continue;
358
359 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
360 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
361
362 /* Now we have the u8 offset - but need the u32 offset */
363 return offset / 4;
364 }
365
366 /* MSR not in any range */
367 return MSR_INVALID;
368}
369
Avi Kivity6aa8b732006-12-10 02:21:36 -0800370#define MAX_INST_SIZE 15
371
Avi Kivity6aa8b732006-12-10 02:21:36 -0800372static inline void clgi(void)
373{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300374 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800375}
376
377static inline void stgi(void)
378{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300379 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800380}
381
382static inline void invlpga(unsigned long addr, u32 asid)
383{
Joerg Roedele0231712010-02-24 18:59:10 +0100384 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800385}
386
Avi Kivity6aa8b732006-12-10 02:21:36 -0800387static inline void force_new_asid(struct kvm_vcpu *vcpu)
388{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400389 to_svm(vcpu)->asid_generation--;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800390}
391
392static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
393{
394 force_new_asid(vcpu);
395}
396
Joerg Roedel4b161842010-09-10 17:31:03 +0200397static int get_npt_level(void)
398{
399#ifdef CONFIG_X86_64
400 return PT64_ROOT_LEVEL;
401#else
402 return PT32E_ROOT_LEVEL;
403#endif
404}
405
Avi Kivity6aa8b732006-12-10 02:21:36 -0800406static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
407{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000408 vcpu->arch.efer = efer;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100409 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600410 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800411
Alexander Graf9962d032008-11-25 20:17:02 +0100412 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800413}
414
Avi Kivity6aa8b732006-12-10 02:21:36 -0800415static int is_external_interrupt(u32 info)
416{
417 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
418 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
419}
420
Glauber Costa2809f5d2009-05-12 16:21:05 -0400421static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
422{
423 struct vcpu_svm *svm = to_svm(vcpu);
424 u32 ret = 0;
425
426 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Jan Kiszka48005f62010-02-19 19:38:07 +0100427 ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400428 return ret & mask;
429}
430
431static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
432{
433 struct vcpu_svm *svm = to_svm(vcpu);
434
435 if (mask == 0)
436 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
437 else
438 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
439
440}
441
Avi Kivity6aa8b732006-12-10 02:21:36 -0800442static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
443{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400444 struct vcpu_svm *svm = to_svm(vcpu);
445
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200446 if (svm->vmcb->control.next_rip != 0)
447 svm->next_rip = svm->vmcb->control.next_rip;
448
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400449 if (!svm->next_rip) {
Avi Kivity851ba692009-08-24 11:10:17 +0300450 if (emulate_instruction(vcpu, 0, 0, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300451 EMULATE_DONE)
452 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800453 return;
454 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300455 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
456 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
457 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800458
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300459 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400460 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800461}
462
Jan Kiszka116a4752010-02-23 17:47:54 +0100463static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200464 bool has_error_code, u32 error_code,
465 bool reinject)
Jan Kiszka116a4752010-02-23 17:47:54 +0100466{
467 struct vcpu_svm *svm = to_svm(vcpu);
468
Joerg Roedele0231712010-02-24 18:59:10 +0100469 /*
470 * If we are within a nested VM we'd better #VMEXIT and let the guest
471 * handle the exception
472 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200473 if (!reinject &&
474 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100475 return;
476
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200477 if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
Jan Kiszka66b71382010-02-23 17:47:56 +0100478 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
479
480 /*
481 * For guest debugging where we have to reinject #BP if some
482 * INT3 is guest-owned:
483 * Emulate nRIP by moving RIP forward. Will fail if injection
484 * raises a fault that is not intercepted. Still better than
485 * failing in all cases.
486 */
487 skip_emulated_instruction(&svm->vcpu);
488 rip = kvm_rip_read(&svm->vcpu);
489 svm->int3_rip = rip + svm->vmcb->save.cs.base;
490 svm->int3_injected = rip - old_rip;
491 }
492
Jan Kiszka116a4752010-02-23 17:47:54 +0100493 svm->vmcb->control.event_inj = nr
494 | SVM_EVTINJ_VALID
495 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
496 | SVM_EVTINJ_TYPE_EXEPT;
497 svm->vmcb->control.event_inj_err = error_code;
498}
499
Joerg Roedel67ec6602010-05-17 14:43:35 +0200500static void svm_init_erratum_383(void)
501{
502 u32 low, high;
503 int err;
504 u64 val;
505
Hans Rosenfeld1be85a62010-07-28 19:09:32 +0200506 if (!cpu_has_amd_erratum(amd_erratum_383))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200507 return;
508
509 /* Use _safe variants to not break nested virtualization */
510 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
511 if (err)
512 return;
513
514 val |= (1ULL << 47);
515
516 low = lower_32_bits(val);
517 high = upper_32_bits(val);
518
519 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
520
521 erratum_383_found = true;
522}
523
Avi Kivity6aa8b732006-12-10 02:21:36 -0800524static int has_svm(void)
525{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200526 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800527
Eduardo Habkost63d11422008-11-17 19:03:20 -0200528 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800529 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800530 return 0;
531 }
532
Avi Kivity6aa8b732006-12-10 02:21:36 -0800533 return 1;
534}
535
536static void svm_hardware_disable(void *garbage)
537{
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200538 cpu_svm_disable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800539}
540
Alexander Graf10474ae2009-09-15 11:37:46 +0200541static int svm_hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800542{
543
Tejun Heo0fe1e002009-10-29 22:34:14 +0900544 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800545 uint64_t efer;
Gleb Natapov89a27f42010-02-16 10:51:48 +0200546 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800547 struct desc_struct *gdt;
548 int me = raw_smp_processor_id();
549
Alexander Graf10474ae2009-09-15 11:37:46 +0200550 rdmsrl(MSR_EFER, efer);
551 if (efer & EFER_SVME)
552 return -EBUSY;
553
Avi Kivity6aa8b732006-12-10 02:21:36 -0800554 if (!has_svm()) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000555 printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
556 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200557 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800558 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900559 sd = per_cpu(svm_data, me);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800560
Tejun Heo0fe1e002009-10-29 22:34:14 +0900561 if (!sd) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000562 printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -0800563 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200564 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800565 }
566
Tejun Heo0fe1e002009-10-29 22:34:14 +0900567 sd->asid_generation = 1;
568 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
569 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800570
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200571 native_store_gdt(&gdt_descr);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200572 gdt = (struct desc_struct *)gdt_descr.address;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900573 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800574
Alexander Graf9962d032008-11-25 20:17:02 +0100575 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800576
Linus Torvaldsd0316552009-12-14 09:58:24 -0800577 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200578
Joerg Roedel67ec6602010-05-17 14:43:35 +0200579 svm_init_erratum_383();
580
Alexander Graf10474ae2009-09-15 11:37:46 +0200581 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800582}
583
Joerg Roedel0da1db752008-07-02 16:02:11 +0200584static void svm_cpu_uninit(int cpu)
585{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900586 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200587
Tejun Heo0fe1e002009-10-29 22:34:14 +0900588 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200589 return;
590
591 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900592 __free_page(sd->save_area);
593 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200594}
595
Avi Kivity6aa8b732006-12-10 02:21:36 -0800596static int svm_cpu_init(int cpu)
597{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900598 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800599 int r;
600
Tejun Heo0fe1e002009-10-29 22:34:14 +0900601 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
602 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800603 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900604 sd->cpu = cpu;
605 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800606 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900607 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800608 goto err_1;
609
Tejun Heo0fe1e002009-10-29 22:34:14 +0900610 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800611
612 return 0;
613
614err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900615 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800616 return r;
617
618}
619
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100620static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800621{
622 int i;
623
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100624 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
625 if (direct_access_msrs[i].index == index)
626 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800627
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100628 return false;
629}
630
Avi Kivity6aa8b732006-12-10 02:21:36 -0800631static void set_msr_interception(u32 *msrpm, unsigned msr,
632 int read, int write)
633{
Joerg Roedel455716f2010-03-01 15:34:35 +0100634 u8 bit_read, bit_write;
635 unsigned long tmp;
636 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800637
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100638 /*
639 * If this warning triggers extend the direct_access_msrs list at the
640 * beginning of the file
641 */
642 WARN_ON(!valid_msr_intercept(msr));
643
Joerg Roedel455716f2010-03-01 15:34:35 +0100644 offset = svm_msrpm_offset(msr);
645 bit_read = 2 * (msr & 0x0f);
646 bit_write = 2 * (msr & 0x0f) + 1;
647 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800648
Joerg Roedel455716f2010-03-01 15:34:35 +0100649 BUG_ON(offset == MSR_INVALID);
650
651 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
652 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
653
654 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800655}
656
Joerg Roedelf65c2292008-02-13 18:58:46 +0100657static void svm_vcpu_init_msrpm(u32 *msrpm)
658{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100659 int i;
660
Joerg Roedelf65c2292008-02-13 18:58:46 +0100661 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
662
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100663 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
664 if (!direct_access_msrs[i].always)
665 continue;
666
667 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
668 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100669}
670
Joerg Roedel323c3d82010-03-01 15:34:37 +0100671static void add_msr_offset(u32 offset)
672{
673 int i;
674
675 for (i = 0; i < MSRPM_OFFSETS; ++i) {
676
677 /* Offset already in list? */
678 if (msrpm_offsets[i] == offset)
679 return;
680
681 /* Slot used by another offset? */
682 if (msrpm_offsets[i] != MSR_INVALID)
683 continue;
684
685 /* Add offset to list */
686 msrpm_offsets[i] = offset;
687
688 return;
689 }
690
691 /*
692 * If this BUG triggers the msrpm_offsets table has an overflow. Just
693 * increase MSRPM_OFFSETS in this case.
694 */
695 BUG();
696}
697
698static void init_msrpm_offsets(void)
699{
700 int i;
701
702 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
703
704 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
705 u32 offset;
706
707 offset = svm_msrpm_offset(direct_access_msrs[i].index);
708 BUG_ON(offset == MSR_INVALID);
709
710 add_msr_offset(offset);
711 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800712}
713
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100714static void svm_enable_lbrv(struct vcpu_svm *svm)
715{
716 u32 *msrpm = svm->msrpm;
717
718 svm->vmcb->control.lbr_ctl = 1;
719 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
720 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
721 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
722 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
723}
724
725static void svm_disable_lbrv(struct vcpu_svm *svm)
726{
727 u32 *msrpm = svm->msrpm;
728
729 svm->vmcb->control.lbr_ctl = 0;
730 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
731 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
732 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
733 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
734}
735
Avi Kivity6aa8b732006-12-10 02:21:36 -0800736static __init int svm_hardware_setup(void)
737{
738 int cpu;
739 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100740 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800741 int r;
742
Avi Kivity6aa8b732006-12-10 02:21:36 -0800743 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
744
745 if (!iopm_pages)
746 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300747
748 iopm_va = page_address(iopm_pages);
749 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800750 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
751
Joerg Roedel323c3d82010-03-01 15:34:37 +0100752 init_msrpm_offsets();
753
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100754 if (boot_cpu_has(X86_FEATURE_NX))
755 kvm_enable_efer_bits(EFER_NX);
756
Alexander Graf1b2fd702009-02-02 16:23:51 +0100757 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
758 kvm_enable_efer_bits(EFER_FFXSR);
759
Alexander Graf236de052008-11-25 20:17:10 +0100760 if (nested) {
761 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +0200762 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +0100763 }
764
Zachary Amsden3230bb42009-09-29 11:38:37 -1000765 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800766 r = svm_cpu_init(cpu);
767 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100768 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800769 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100770
771 svm_features = cpuid_edx(SVM_CPUID_FUNC);
772
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200773 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100774 npt_enabled = false;
775
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100776 if (npt_enabled && !npt) {
777 printk(KERN_INFO "kvm: Nested Paging disabled\n");
778 npt_enabled = false;
779 }
780
Joerg Roedel18552672008-02-07 13:47:41 +0100781 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100782 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100783 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200784 } else
785 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100786
Avi Kivity6aa8b732006-12-10 02:21:36 -0800787 return 0;
788
Joerg Roedelf65c2292008-02-13 18:58:46 +0100789err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
791 iopm_base = 0;
792 return r;
793}
794
795static __exit void svm_hardware_unsetup(void)
796{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200797 int cpu;
798
Zachary Amsden3230bb42009-09-29 11:38:37 -1000799 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200800 svm_cpu_uninit(cpu);
801
Avi Kivity6aa8b732006-12-10 02:21:36 -0800802 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100803 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800804}
805
806static void init_seg(struct vmcb_seg *seg)
807{
808 seg->selector = 0;
809 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +0100810 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800811 seg->limit = 0xffff;
812 seg->base = 0;
813}
814
815static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
816{
817 seg->selector = 0;
818 seg->attrib = SVM_SELECTOR_P_MASK | type;
819 seg->limit = 0xffff;
820 seg->base = 0;
821}
822
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -1000823static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
824{
825 struct vcpu_svm *svm = to_svm(vcpu);
826 u64 g_tsc_offset = 0;
827
Joerg Roedel20307532010-11-29 17:51:48 +0100828 if (is_guest_mode(vcpu)) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -1000829 g_tsc_offset = svm->vmcb->control.tsc_offset -
830 svm->nested.hsave->control.tsc_offset;
831 svm->nested.hsave->control.tsc_offset = offset;
832 }
833
834 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
835}
836
Zachary Amsdene48672f2010-08-19 22:07:23 -1000837static void svm_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment)
838{
839 struct vcpu_svm *svm = to_svm(vcpu);
840
841 svm->vmcb->control.tsc_offset += adjustment;
Joerg Roedel20307532010-11-29 17:51:48 +0100842 if (is_guest_mode(vcpu))
Zachary Amsdene48672f2010-08-19 22:07:23 -1000843 svm->nested.hsave->control.tsc_offset += adjustment;
844}
845
Joerg Roedele6101a92008-02-13 18:58:45 +0100846static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800847{
Joerg Roedele6101a92008-02-13 18:58:45 +0100848 struct vmcb_control_area *control = &svm->vmcb->control;
849 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800850
Avi Kivitybff78272010-01-07 13:16:08 +0200851 svm->vcpu.fpu_active = 1;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100852 svm->vcpu.arch.hflags = 0;
Avi Kivitybff78272010-01-07 13:16:08 +0200853
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100854 set_cr_intercept(svm, INTERCEPT_CR0_READ);
855 set_cr_intercept(svm, INTERCEPT_CR3_READ);
856 set_cr_intercept(svm, INTERCEPT_CR4_READ);
857 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
858 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
859 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
860 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800861
Joerg Roedel3aed0412010-11-30 18:03:58 +0100862 set_dr_intercept(svm, INTERCEPT_DR0_READ);
863 set_dr_intercept(svm, INTERCEPT_DR1_READ);
864 set_dr_intercept(svm, INTERCEPT_DR2_READ);
865 set_dr_intercept(svm, INTERCEPT_DR3_READ);
866 set_dr_intercept(svm, INTERCEPT_DR4_READ);
867 set_dr_intercept(svm, INTERCEPT_DR5_READ);
868 set_dr_intercept(svm, INTERCEPT_DR6_READ);
869 set_dr_intercept(svm, INTERCEPT_DR7_READ);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800870
Joerg Roedel3aed0412010-11-30 18:03:58 +0100871 set_dr_intercept(svm, INTERCEPT_DR0_WRITE);
872 set_dr_intercept(svm, INTERCEPT_DR1_WRITE);
873 set_dr_intercept(svm, INTERCEPT_DR2_WRITE);
874 set_dr_intercept(svm, INTERCEPT_DR3_WRITE);
875 set_dr_intercept(svm, INTERCEPT_DR4_WRITE);
876 set_dr_intercept(svm, INTERCEPT_DR5_WRITE);
877 set_dr_intercept(svm, INTERCEPT_DR6_WRITE);
878 set_dr_intercept(svm, INTERCEPT_DR7_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800879
Joerg Roedel18c918c2010-11-30 18:03:59 +0100880 set_exception_intercept(svm, PF_VECTOR);
881 set_exception_intercept(svm, UD_VECTOR);
882 set_exception_intercept(svm, MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800883
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100884 set_intercept(svm, INTERCEPT_INTR);
885 set_intercept(svm, INTERCEPT_NMI);
886 set_intercept(svm, INTERCEPT_SMI);
887 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
888 set_intercept(svm, INTERCEPT_CPUID);
889 set_intercept(svm, INTERCEPT_INVD);
890 set_intercept(svm, INTERCEPT_HLT);
891 set_intercept(svm, INTERCEPT_INVLPG);
892 set_intercept(svm, INTERCEPT_INVLPGA);
893 set_intercept(svm, INTERCEPT_IOIO_PROT);
894 set_intercept(svm, INTERCEPT_MSR_PROT);
895 set_intercept(svm, INTERCEPT_TASK_SWITCH);
896 set_intercept(svm, INTERCEPT_SHUTDOWN);
897 set_intercept(svm, INTERCEPT_VMRUN);
898 set_intercept(svm, INTERCEPT_VMMCALL);
899 set_intercept(svm, INTERCEPT_VMLOAD);
900 set_intercept(svm, INTERCEPT_VMSAVE);
901 set_intercept(svm, INTERCEPT_STGI);
902 set_intercept(svm, INTERCEPT_CLGI);
903 set_intercept(svm, INTERCEPT_SKINIT);
904 set_intercept(svm, INTERCEPT_WBINVD);
905 set_intercept(svm, INTERCEPT_MONITOR);
906 set_intercept(svm, INTERCEPT_MWAIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800907
908 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100909 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910 control->int_ctl = V_INTR_MASKING_MASK;
911
912 init_seg(&save->es);
913 init_seg(&save->ss);
914 init_seg(&save->ds);
915 init_seg(&save->fs);
916 init_seg(&save->gs);
917
918 save->cs.selector = 0xf000;
919 /* Executable/Readable Code Segment */
920 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
921 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
922 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800923 /*
924 * cs.base should really be 0xffff0000, but vmx can't handle that, so
925 * be consistent with it.
926 *
927 * Replace when we have real mode working for vmx.
928 */
929 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800930
931 save->gdtr.limit = 0xffff;
932 save->idtr.limit = 0xffff;
933
934 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
935 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
936
Marcelo Tosattieaa48512010-08-31 19:13:14 -0300937 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -0400938 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800939 save->dr7 = 0x400;
940 save->rflags = 2;
941 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300942 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943
Joerg Roedele0231712010-02-24 18:59:10 +0100944 /*
945 * This is the guest-visible cr0 value.
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200946 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947 */
Marcelo Tosatti678041a2010-08-31 19:13:13 -0300948 svm->vcpu.arch.cr0 = 0;
949 (void)kvm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200950
Rusty Russell66aee912007-07-17 23:34:16 +1000951 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800952 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100953
954 if (npt_enabled) {
955 /* Setup VMCB for Nested Paging */
956 control->nested_ctl = 1;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100957 clr_intercept(svm, INTERCEPT_TASK_SWITCH);
958 clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +0100959 clr_exception_intercept(svm, PF_VECTOR);
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100960 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
961 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100962 save->g_pat = 0x0007040600070406ULL;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100963 save->cr3 = 0;
964 save->cr4 = 0;
965 }
Avi Kivitya79d2f12008-04-14 13:10:21 +0300966 force_new_asid(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +0100967
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200968 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +0200969 svm->vcpu.arch.hflags = 0;
970
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200971 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
Mark Langsdorf565d0992009-10-06 14:25:02 -0500972 control->pause_filter_count = 3000;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100973 set_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -0500974 }
975
Joerg Roedel2af91942009-08-07 11:49:28 +0200976 enable_gif(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800977}
978
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200979static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +0300980{
981 struct vcpu_svm *svm = to_svm(vcpu);
982
Joerg Roedele6101a92008-02-13 18:58:45 +0100983 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +0200984
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300985 if (!kvm_vcpu_is_bsp(vcpu)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300986 kvm_rip_write(vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800987 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
988 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +0200989 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300990 vcpu->arch.regs_avail = ~0;
991 vcpu->arch.regs_dirty = ~0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200992
993 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300994}
995
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000996static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400998 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800999 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001000 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +01001001 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001002 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001003 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001004
Rusty Russellc16f8622007-07-30 21:12:19 +10001005 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001006 if (!svm) {
1007 err = -ENOMEM;
1008 goto out;
1009 }
1010
1011 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
1012 if (err)
1013 goto free_svm;
1014
Joerg Roedelf65c2292008-02-13 18:58:46 +01001015 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001016 page = alloc_page(GFP_KERNEL);
1017 if (!page)
1018 goto uninit;
1019
Joerg Roedelf65c2292008-02-13 18:58:46 +01001020 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1021 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001022 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001023
1024 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1025 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001026 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001027
Alexander Grafb286d5d2008-11-25 20:17:05 +01001028 hsave_page = alloc_page(GFP_KERNEL);
1029 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001030 goto free_page3;
1031
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001032 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +01001033
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001034 svm->msrpm = page_address(msrpm_pages);
1035 svm_vcpu_init_msrpm(svm->msrpm);
1036
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001037 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +01001038 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001039
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001040 svm->vmcb = page_address(page);
1041 clear_page(svm->vmcb);
1042 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
1043 svm->asid_generation = 0;
Joerg Roedele6101a92008-02-13 18:58:45 +01001044 init_vmcb(svm);
Zachary Amsden99e3e302010-08-19 22:07:17 -10001045 kvm_write_tsc(&svm->vcpu, 0);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001046
Jan Kiszka10ab25c2010-05-25 16:01:50 +02001047 err = fx_init(&svm->vcpu);
1048 if (err)
1049 goto free_page4;
1050
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001051 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001052 if (kvm_vcpu_is_bsp(&svm->vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001053 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001054
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001055 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -08001056
Jan Kiszka10ab25c2010-05-25 16:01:50 +02001057free_page4:
1058 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001059free_page3:
1060 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
1061free_page2:
1062 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
1063free_page1:
1064 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001065uninit:
1066 kvm_vcpu_uninit(&svm->vcpu);
1067free_svm:
Rusty Russella4770342007-08-01 14:46:11 +10001068 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001069out:
1070 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001071}
1072
1073static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1074{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001075 struct vcpu_svm *svm = to_svm(vcpu);
1076
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001077 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +01001078 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001079 __free_page(virt_to_page(svm->nested.hsave));
1080 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001081 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10001082 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001083}
1084
Avi Kivity15ad7142007-07-11 18:17:21 +03001085static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001086{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001087 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001088 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02001089
Avi Kivity0cc50642007-03-25 12:07:27 +02001090 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03001091 svm->asid_generation = 0;
Avi Kivity0cc50642007-03-25 12:07:27 +02001092 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001093
Avi Kivity82ca2d12010-10-21 12:20:34 +02001094#ifdef CONFIG_X86_64
1095 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
1096#endif
Avi Kivitydacccfd2010-10-21 12:20:33 +02001097 savesegment(fs, svm->host.fs);
1098 savesegment(gs, svm->host.gs);
1099 svm->host.ldt = kvm_read_ldt();
1100
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001101 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001102 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001103}
1104
1105static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1106{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001107 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001108 int i;
1109
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001110 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02001111 kvm_load_ldt(svm->host.ldt);
1112#ifdef CONFIG_X86_64
1113 loadsegment(fs, svm->host.fs);
1114 load_gs_index(svm->host.gs);
1115 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs);
1116#else
1117 loadsegment(gs, svm->host.gs);
1118#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001119 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001120 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001121}
1122
Avi Kivity6aa8b732006-12-10 02:21:36 -08001123static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1124{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001125 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001126}
1127
1128static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1129{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001130 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001131}
1132
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001133static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1134{
1135 switch (reg) {
1136 case VCPU_EXREG_PDPTR:
1137 BUG_ON(!npt_enabled);
Joerg Roedelff03a072010-09-10 17:30:57 +02001138 load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3);
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001139 break;
1140 default:
1141 BUG();
1142 }
1143}
1144
Alexander Graff0b85052008-11-25 20:17:01 +01001145static void svm_set_vintr(struct vcpu_svm *svm)
1146{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001147 set_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001148}
1149
1150static void svm_clear_vintr(struct vcpu_svm *svm)
1151{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001152 clr_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001153}
1154
Avi Kivity6aa8b732006-12-10 02:21:36 -08001155static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1156{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001157 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001158
1159 switch (seg) {
1160 case VCPU_SREG_CS: return &save->cs;
1161 case VCPU_SREG_DS: return &save->ds;
1162 case VCPU_SREG_ES: return &save->es;
1163 case VCPU_SREG_FS: return &save->fs;
1164 case VCPU_SREG_GS: return &save->gs;
1165 case VCPU_SREG_SS: return &save->ss;
1166 case VCPU_SREG_TR: return &save->tr;
1167 case VCPU_SREG_LDTR: return &save->ldtr;
1168 }
1169 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001170 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001171}
1172
1173static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1174{
1175 struct vmcb_seg *s = svm_seg(vcpu, seg);
1176
1177 return s->base;
1178}
1179
1180static void svm_get_segment(struct kvm_vcpu *vcpu,
1181 struct kvm_segment *var, int seg)
1182{
1183 struct vmcb_seg *s = svm_seg(vcpu, seg);
1184
1185 var->base = s->base;
1186 var->limit = s->limit;
1187 var->selector = s->selector;
1188 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1189 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1190 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1191 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1192 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1193 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1194 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
1195 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
Amit Shah25022ac2008-10-27 09:04:17 +00001196
Joerg Roedele0231712010-02-24 18:59:10 +01001197 /*
1198 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001199 * for cross vendor migration purposes by "not present"
1200 */
1201 var->unusable = !var->present || (var->type == 0);
1202
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001203 switch (seg) {
1204 case VCPU_SREG_CS:
1205 /*
1206 * SVM always stores 0 for the 'G' bit in the CS selector in
1207 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
1208 * Intel's VMENTRY has a check on the 'G' bit.
1209 */
Amit Shah25022ac2008-10-27 09:04:17 +00001210 var->g = s->limit > 0xfffff;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001211 break;
1212 case VCPU_SREG_TR:
1213 /*
1214 * Work around a bug where the busy flag in the tr selector
1215 * isn't exposed
1216 */
Amit Shahc0d09822008-10-27 09:04:18 +00001217 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001218 break;
1219 case VCPU_SREG_DS:
1220 case VCPU_SREG_ES:
1221 case VCPU_SREG_FS:
1222 case VCPU_SREG_GS:
1223 /*
1224 * The accessed bit must always be set in the segment
1225 * descriptor cache, although it can be cleared in the
1226 * descriptor, the cached bit always remains at 1. Since
1227 * Intel has a check on this, set it here to support
1228 * cross-vendor migration.
1229 */
1230 if (!var->unusable)
1231 var->type |= 0x1;
1232 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001233 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001234 /*
1235 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001236 * descriptor is left as 1, although the whole segment has
1237 * been made unusable. Clear it here to pass an Intel VMX
1238 * entry check when cross vendor migrating.
1239 */
1240 if (var->unusable)
1241 var->db = 0;
1242 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001243 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001244}
1245
Izik Eidus2e4d2652008-03-24 19:38:34 +02001246static int svm_get_cpl(struct kvm_vcpu *vcpu)
1247{
1248 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1249
1250 return save->cpl;
1251}
1252
Gleb Natapov89a27f42010-02-16 10:51:48 +02001253static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001254{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001255 struct vcpu_svm *svm = to_svm(vcpu);
1256
Gleb Natapov89a27f42010-02-16 10:51:48 +02001257 dt->size = svm->vmcb->save.idtr.limit;
1258 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001259}
1260
Gleb Natapov89a27f42010-02-16 10:51:48 +02001261static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001262{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001263 struct vcpu_svm *svm = to_svm(vcpu);
1264
Gleb Natapov89a27f42010-02-16 10:51:48 +02001265 svm->vmcb->save.idtr.limit = dt->size;
1266 svm->vmcb->save.idtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001267}
1268
Gleb Natapov89a27f42010-02-16 10:51:48 +02001269static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001270{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001271 struct vcpu_svm *svm = to_svm(vcpu);
1272
Gleb Natapov89a27f42010-02-16 10:51:48 +02001273 dt->size = svm->vmcb->save.gdtr.limit;
1274 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001275}
1276
Gleb Natapov89a27f42010-02-16 10:51:48 +02001277static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001278{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001279 struct vcpu_svm *svm = to_svm(vcpu);
1280
Gleb Natapov89a27f42010-02-16 10:51:48 +02001281 svm->vmcb->save.gdtr.limit = dt->size;
1282 svm->vmcb->save.gdtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001283}
1284
Avi Kivitye8467fd2009-12-29 18:43:06 +02001285static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1286{
1287}
1288
Anthony Liguori25c4c272007-04-27 09:29:21 +03001289static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001290{
1291}
1292
Avi Kivityd2251572010-01-06 10:55:27 +02001293static void update_cr0_intercept(struct vcpu_svm *svm)
1294{
1295 ulong gcr0 = svm->vcpu.arch.cr0;
1296 u64 *hcr0 = &svm->vmcb->save.cr0;
1297
1298 if (!svm->vcpu.fpu_active)
1299 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1300 else
1301 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1302 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1303
1304
1305 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001306 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
1307 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001308 } else {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001309 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1310 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001311 }
1312}
1313
Avi Kivity6aa8b732006-12-10 02:21:36 -08001314static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1315{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001316 struct vcpu_svm *svm = to_svm(vcpu);
1317
Joerg Roedel20307532010-11-29 17:51:48 +01001318 if (is_guest_mode(vcpu)) {
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001319 /*
1320 * We are here because we run in nested mode, the host kvm
1321 * intercepts cr0 writes but the l1 hypervisor does not.
1322 * But the L1 hypervisor may intercept selective cr0 writes.
1323 * This needs to be checked here.
1324 */
1325 unsigned long old, new;
1326
1327 /* Remove bits that would trigger a real cr0 write intercept */
1328 old = vcpu->arch.cr0 & SVM_CR0_SELECTIVE_MASK;
1329 new = cr0 & SVM_CR0_SELECTIVE_MASK;
1330
1331 if (old == new) {
1332 /* cr0 write with ts and mp unchanged */
1333 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
Joerg Roedelcda00082010-09-02 17:29:46 +02001334 if (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE) {
1335 svm->nested.vmexit_rip = kvm_rip_read(vcpu);
1336 svm->nested.vmexit_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
1337 svm->nested.vmexit_rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001338 return;
Joerg Roedelcda00082010-09-02 17:29:46 +02001339 }
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001340 }
1341 }
1342
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001343#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001344 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001345 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001346 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001347 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001348 }
1349
Mike Dayd77c26f2007-10-08 09:02:08 -04001350 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001351 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001352 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001353 }
1354 }
1355#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001356 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001357
1358 if (!npt_enabled)
1359 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001360
1361 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001362 cr0 |= X86_CR0_TS;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001363 /*
1364 * re-enable caching here because the QEMU bios
1365 * does not do it - this results in some delay at
1366 * reboot
1367 */
1368 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001369 svm->vmcb->save.cr0 = cr0;
Avi Kivityd2251572010-01-06 10:55:27 +02001370 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001371}
1372
1373static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1374{
Joerg Roedel6394b642008-04-09 14:15:29 +02001375 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001376 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1377
1378 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
1379 force_new_asid(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001380
Joerg Roedelec077262008-04-09 14:15:28 +02001381 vcpu->arch.cr4 = cr4;
1382 if (!npt_enabled)
1383 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001384 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001385 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001386}
1387
1388static void svm_set_segment(struct kvm_vcpu *vcpu,
1389 struct kvm_segment *var, int seg)
1390{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001391 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001392 struct vmcb_seg *s = svm_seg(vcpu, seg);
1393
1394 s->base = var->base;
1395 s->limit = var->limit;
1396 s->selector = var->selector;
1397 if (var->unusable)
1398 s->attrib = 0;
1399 else {
1400 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1401 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1402 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1403 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1404 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1405 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1406 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1407 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1408 }
1409 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001410 svm->vmcb->save.cpl
1411 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -08001412 >> SVM_SELECTOR_DPL_SHIFT) & 3;
1413
1414}
1415
Gleb Natapov44c11432009-05-11 13:35:52 +03001416static void update_db_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001417{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001418 struct vcpu_svm *svm = to_svm(vcpu);
1419
Joerg Roedel18c918c2010-11-30 18:03:59 +01001420 clr_exception_intercept(svm, DB_VECTOR);
1421 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001422
Jan Kiszka6be7d302009-10-18 13:24:54 +02001423 if (svm->nmi_singlestep)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001424 set_exception_intercept(svm, DB_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001425
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001426 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1427 if (vcpu->guest_debug &
1428 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
Joerg Roedel18c918c2010-11-30 18:03:59 +01001429 set_exception_intercept(svm, DB_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001430 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001431 set_exception_intercept(svm, BP_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001432 } else
1433 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001434}
1435
Jan Kiszka355be0b2009-10-03 00:31:21 +02001436static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Gleb Natapov44c11432009-05-11 13:35:52 +03001437{
Gleb Natapov44c11432009-05-11 13:35:52 +03001438 struct vcpu_svm *svm = to_svm(vcpu);
1439
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001440 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1441 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1442 else
1443 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1444
Jan Kiszka355be0b2009-10-03 00:31:21 +02001445 update_db_intercept(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001446}
1447
Tejun Heo0fe1e002009-10-29 22:34:14 +09001448static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001449{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001450 if (sd->next_asid > sd->max_asid) {
1451 ++sd->asid_generation;
1452 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001453 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001454 }
1455
Tejun Heo0fe1e002009-10-29 22:34:14 +09001456 svm->asid_generation = sd->asid_generation;
1457 svm->vmcb->control.asid = sd->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001458}
1459
Gleb Natapov020df072010-04-13 10:05:23 +03001460static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001461{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001462 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001463
Gleb Natapov020df072010-04-13 10:05:23 +03001464 svm->vmcb->save.dr7 = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001465}
1466
Avi Kivity851ba692009-08-24 11:10:17 +03001467static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001468{
Gleb Natapov631bc482010-10-14 11:22:52 +02001469 u64 fault_address = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001470 u32 error_code;
Gleb Natapov631bc482010-10-14 11:22:52 +02001471 int r = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001472
Gleb Natapov631bc482010-10-14 11:22:52 +02001473 switch (svm->apf_reason) {
1474 default:
1475 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001476
Gleb Natapov631bc482010-10-14 11:22:52 +02001477 trace_kvm_page_fault(fault_address, error_code);
1478 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1479 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
1480 r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
1481 break;
1482 case KVM_PV_REASON_PAGE_NOT_PRESENT:
1483 svm->apf_reason = 0;
1484 local_irq_disable();
1485 kvm_async_pf_task_wait(fault_address);
1486 local_irq_enable();
1487 break;
1488 case KVM_PV_REASON_PAGE_READY:
1489 svm->apf_reason = 0;
1490 local_irq_disable();
1491 kvm_async_pf_task_wake(fault_address);
1492 local_irq_enable();
1493 break;
1494 }
1495 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001496}
1497
Avi Kivity851ba692009-08-24 11:10:17 +03001498static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001499{
Avi Kivity851ba692009-08-24 11:10:17 +03001500 struct kvm_run *kvm_run = svm->vcpu.run;
1501
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001502 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001503 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001504 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001505 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1506 return 1;
1507 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001508
Jan Kiszka6be7d302009-10-18 13:24:54 +02001509 if (svm->nmi_singlestep) {
1510 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001511 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1512 svm->vmcb->save.rflags &=
1513 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1514 update_db_intercept(&svm->vcpu);
1515 }
1516
1517 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001518 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001519 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1520 kvm_run->debug.arch.pc =
1521 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1522 kvm_run->debug.arch.exception = DB_VECTOR;
1523 return 0;
1524 }
1525
1526 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001527}
1528
Avi Kivity851ba692009-08-24 11:10:17 +03001529static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001530{
Avi Kivity851ba692009-08-24 11:10:17 +03001531 struct kvm_run *kvm_run = svm->vcpu.run;
1532
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001533 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1534 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1535 kvm_run->debug.arch.exception = BP_VECTOR;
1536 return 0;
1537}
1538
Avi Kivity851ba692009-08-24 11:10:17 +03001539static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001540{
1541 int er;
1542
Avi Kivity851ba692009-08-24 11:10:17 +03001543 er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001544 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001545 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001546 return 1;
1547}
1548
Avi Kivity6b52d182010-01-21 15:31:47 +02001549static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001550{
Avi Kivity6b52d182010-01-21 15:31:47 +02001551 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001552
Joerg Roedel18c918c2010-11-30 18:03:59 +01001553 clr_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001554
Rusty Russelle756fc62007-07-30 20:07:08 +10001555 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001556 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001557}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001558
Avi Kivity6b52d182010-01-21 15:31:47 +02001559static int nm_interception(struct vcpu_svm *svm)
1560{
1561 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001562 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001563}
1564
Joerg Roedel67ec6602010-05-17 14:43:35 +02001565static bool is_erratum_383(void)
1566{
1567 int err, i;
1568 u64 value;
1569
1570 if (!erratum_383_found)
1571 return false;
1572
1573 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1574 if (err)
1575 return false;
1576
1577 /* Bit 62 may or may not be set for this mce */
1578 value &= ~(1ULL << 62);
1579
1580 if (value != 0xb600000000010015ULL)
1581 return false;
1582
1583 /* Clear MCi_STATUS registers */
1584 for (i = 0; i < 6; ++i)
1585 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1586
1587 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1588 if (!err) {
1589 u32 low, high;
1590
1591 value &= ~(1ULL << 2);
1592 low = lower_32_bits(value);
1593 high = upper_32_bits(value);
1594
1595 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1596 }
1597
1598 /* Flush tlb to evict multi-match entries */
1599 __flush_tlb_all();
1600
1601 return true;
1602}
1603
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001604static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001605{
Joerg Roedel67ec6602010-05-17 14:43:35 +02001606 if (is_erratum_383()) {
1607 /*
1608 * Erratum 383 triggered. Guest state is corrupt so kill the
1609 * guest.
1610 */
1611 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1612
Avi Kivitya8eeb042010-05-10 12:34:53 +03001613 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02001614
1615 return;
1616 }
1617
Joerg Roedel53371b52008-04-09 14:15:30 +02001618 /*
1619 * On an #MC intercept the MCE handler is not called automatically in
1620 * the host. So do it by hand here.
1621 */
1622 asm volatile (
1623 "int $0x12\n");
1624 /* not sure if we ever come back to this point */
1625
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001626 return;
1627}
1628
1629static int mc_interception(struct vcpu_svm *svm)
1630{
Joerg Roedel53371b52008-04-09 14:15:30 +02001631 return 1;
1632}
1633
Avi Kivity851ba692009-08-24 11:10:17 +03001634static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001635{
Avi Kivity851ba692009-08-24 11:10:17 +03001636 struct kvm_run *kvm_run = svm->vcpu.run;
1637
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001638 /*
1639 * VMCB is undefined after a SHUTDOWN intercept
1640 * so reinitialize it.
1641 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001642 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001643 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001644
1645 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1646 return 0;
1647}
1648
Avi Kivity851ba692009-08-24 11:10:17 +03001649static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001650{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001651 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04001652 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001653 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001654 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001655
Rusty Russelle756fc62007-07-30 20:07:08 +10001656 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03001657 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001658 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001659 if (string || in)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03001660 return emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001661
Avi Kivity039576c2007-03-20 12:46:50 +02001662 port = io_info >> 16;
1663 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001664 svm->next_rip = svm->vmcb->control.exit_info_2;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001665 skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001666
1667 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001668}
1669
Avi Kivity851ba692009-08-24 11:10:17 +03001670static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001671{
1672 return 1;
1673}
1674
Avi Kivity851ba692009-08-24 11:10:17 +03001675static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001676{
1677 ++svm->vcpu.stat.irq_exits;
1678 return 1;
1679}
1680
Avi Kivity851ba692009-08-24 11:10:17 +03001681static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001682{
1683 return 1;
1684}
1685
Avi Kivity851ba692009-08-24 11:10:17 +03001686static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001687{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001688 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001689 skip_emulated_instruction(&svm->vcpu);
1690 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001691}
1692
Avi Kivity851ba692009-08-24 11:10:17 +03001693static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001694{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001695 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001696 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001697 kvm_emulate_hypercall(&svm->vcpu);
1698 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001699}
1700
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001701static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
1702{
1703 struct vcpu_svm *svm = to_svm(vcpu);
1704
1705 return svm->nested.nested_cr3;
1706}
1707
1708static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
1709 unsigned long root)
1710{
1711 struct vcpu_svm *svm = to_svm(vcpu);
1712
1713 svm->vmcb->control.nested_cr3 = root;
1714 force_new_asid(vcpu);
1715}
1716
Avi Kivity6389ee92010-11-29 16:12:30 +02001717static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
1718 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001719{
1720 struct vcpu_svm *svm = to_svm(vcpu);
1721
1722 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
1723 svm->vmcb->control.exit_code_hi = 0;
Avi Kivity6389ee92010-11-29 16:12:30 +02001724 svm->vmcb->control.exit_info_1 = fault->error_code;
1725 svm->vmcb->control.exit_info_2 = fault->address;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001726
1727 nested_svm_vmexit(svm);
1728}
1729
Joerg Roedel4b161842010-09-10 17:31:03 +02001730static int nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
1731{
1732 int r;
1733
1734 r = kvm_init_shadow_mmu(vcpu, &vcpu->arch.mmu);
1735
1736 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
1737 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
1738 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
1739 vcpu->arch.mmu.shadow_root_level = get_npt_level();
1740 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
1741
1742 return r;
1743}
1744
1745static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
1746{
1747 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
1748}
1749
Alexander Grafc0725422008-11-25 20:17:03 +01001750static int nested_svm_check_permissions(struct vcpu_svm *svm)
1751{
Avi Kivityf6801df2010-01-21 15:31:50 +02001752 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01001753 || !is_paging(&svm->vcpu)) {
1754 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1755 return 1;
1756 }
1757
1758 if (svm->vmcb->save.cpl) {
1759 kvm_inject_gp(&svm->vcpu, 0);
1760 return 1;
1761 }
1762
1763 return 0;
1764}
1765
Alexander Grafcf74a782008-11-25 20:17:08 +01001766static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
1767 bool has_error_code, u32 error_code)
1768{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001769 int vmexit;
1770
Joerg Roedel20307532010-11-29 17:51:48 +01001771 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02001772 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001773
Joerg Roedel0295ad72009-08-07 11:49:37 +02001774 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1775 svm->vmcb->control.exit_code_hi = 0;
1776 svm->vmcb->control.exit_info_1 = error_code;
1777 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1778
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001779 vmexit = nested_svm_intercept(svm);
1780 if (vmexit == NESTED_EXIT_DONE)
1781 svm->nested.exit_required = true;
1782
1783 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001784}
1785
Joerg Roedel8fe54652010-02-19 16:23:01 +01001786/* This function returns true if it is save to enable the irq window */
1787static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001788{
Joerg Roedel20307532010-11-29 17:51:48 +01001789 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001790 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001791
Joerg Roedel26666952009-08-07 11:49:46 +02001792 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001793 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001794
Joerg Roedel26666952009-08-07 11:49:46 +02001795 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001796 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001797
Gleb Natapova0a07cd2010-09-20 10:15:32 +02001798 /*
1799 * if vmexit was already requested (by intercepted exception
1800 * for instance) do not overwrite it with "external interrupt"
1801 * vmexit.
1802 */
1803 if (svm->nested.exit_required)
1804 return false;
1805
Joerg Roedel197717d2010-02-24 18:59:19 +01001806 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
1807 svm->vmcb->control.exit_info_1 = 0;
1808 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02001809
Joerg Roedelcd3ff652009-10-09 16:08:26 +02001810 if (svm->nested.intercept & 1ULL) {
1811 /*
1812 * The #vmexit can't be emulated here directly because this
1813 * code path runs with irqs and preemtion disabled. A
1814 * #vmexit emulation might sleep. Only signal request for
1815 * the #vmexit here.
1816 */
1817 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02001818 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01001819 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001820 }
1821
Joerg Roedel8fe54652010-02-19 16:23:01 +01001822 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001823}
1824
Joerg Roedel887f5002010-02-24 18:59:12 +01001825/* This function returns true if it is save to enable the nmi window */
1826static inline bool nested_svm_nmi(struct vcpu_svm *svm)
1827{
Joerg Roedel20307532010-11-29 17:51:48 +01001828 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01001829 return true;
1830
1831 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
1832 return true;
1833
1834 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
1835 svm->nested.exit_required = true;
1836
1837 return false;
1838}
1839
Joerg Roedel7597f122010-02-19 16:23:00 +01001840static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001841{
1842 struct page *page;
1843
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01001844 might_sleep();
1845
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001846 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001847 if (is_error_page(page))
1848 goto error;
1849
Joerg Roedel7597f122010-02-19 16:23:00 +01001850 *_page = page;
1851
1852 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001853
1854error:
1855 kvm_release_page_clean(page);
1856 kvm_inject_gp(&svm->vcpu, 0);
1857
1858 return NULL;
1859}
1860
Joerg Roedel7597f122010-02-19 16:23:00 +01001861static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001862{
Joerg Roedel7597f122010-02-19 16:23:00 +01001863 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001864 kvm_release_page_dirty(page);
1865}
1866
Joerg Roedelce2ac082010-03-01 15:34:39 +01001867static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001868{
Joerg Roedelce2ac082010-03-01 15:34:39 +01001869 unsigned port;
1870 u8 val, bit;
1871 u64 gpa;
1872
1873 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
1874 return NESTED_EXIT_HOST;
1875
1876 port = svm->vmcb->control.exit_info_1 >> 16;
1877 gpa = svm->nested.vmcb_iopm + (port / 8);
1878 bit = port % 8;
1879 val = 0;
1880
1881 if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1))
1882 val &= (1 << bit);
1883
1884 return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1885}
1886
Joerg Roedeld2477822010-03-01 15:34:34 +01001887static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001888{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001889 u32 offset, msr, value;
1890 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001891
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001892 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01001893 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001894
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001895 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1896 offset = svm_msrpm_offset(msr);
1897 write = svm->vmcb->control.exit_info_1 & 1;
1898 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001899
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001900 if (offset == MSR_INVALID)
1901 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001902
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001903 /* Offset is in 32 bit units but need in 8 bit units */
1904 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001905
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001906 if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4))
1907 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001908
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001909 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001910}
1911
Joerg Roedel410e4d52009-08-07 11:49:44 +02001912static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001913{
Alexander Grafcf74a782008-11-25 20:17:08 +01001914 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001915
Joerg Roedel410e4d52009-08-07 11:49:44 +02001916 switch (exit_code) {
1917 case SVM_EXIT_INTR:
1918 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02001919 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02001920 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001921 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01001922 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001923 if (npt_enabled)
1924 return NESTED_EXIT_HOST;
1925 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001926 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02001927 /* When we're shadowing, trap PFs, but not async PF */
1928 if (!npt_enabled && svm->apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001929 return NESTED_EXIT_HOST;
1930 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001931 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
1932 nm_interception(svm);
1933 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001934 default:
1935 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001936 }
1937
Joerg Roedel410e4d52009-08-07 11:49:44 +02001938 return NESTED_EXIT_CONTINUE;
1939}
1940
1941/*
1942 * If this function returns true, this #vmexit was already handled
1943 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001944static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001945{
1946 u32 exit_code = svm->vmcb->control.exit_code;
1947 int vmexit = NESTED_EXIT_HOST;
1948
Alexander Grafcf74a782008-11-25 20:17:08 +01001949 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001950 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001951 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001952 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01001953 case SVM_EXIT_IOIO:
1954 vmexit = nested_svm_intercept_ioio(svm);
1955 break;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001956 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
1957 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
1958 if (svm->nested.intercept_cr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001959 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001960 break;
1961 }
Joerg Roedel3aed0412010-11-30 18:03:58 +01001962 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
1963 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
1964 if (svm->nested.intercept_dr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001965 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001966 break;
1967 }
1968 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1969 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001970 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001971 vmexit = NESTED_EXIT_DONE;
Gleb Natapov631bc482010-10-14 11:22:52 +02001972 /* async page fault always cause vmexit */
1973 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
1974 svm->apf_reason != 0)
1975 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001976 break;
1977 }
Joerg Roedel228070b2010-04-22 12:33:10 +02001978 case SVM_EXIT_ERR: {
1979 vmexit = NESTED_EXIT_DONE;
1980 break;
1981 }
Alexander Grafcf74a782008-11-25 20:17:08 +01001982 default: {
1983 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001984 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001985 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001986 }
1987 }
1988
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001989 return vmexit;
1990}
1991
1992static int nested_svm_exit_handled(struct vcpu_svm *svm)
1993{
1994 int vmexit;
1995
1996 vmexit = nested_svm_intercept(svm);
1997
1998 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001999 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002000
2001 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002002}
2003
Joerg Roedel0460a972009-08-07 11:49:31 +02002004static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
2005{
2006 struct vmcb_control_area *dst = &dst_vmcb->control;
2007 struct vmcb_control_area *from = &from_vmcb->control;
2008
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002009 dst->intercept_cr = from->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002010 dst->intercept_dr = from->intercept_dr;
Joerg Roedel0460a972009-08-07 11:49:31 +02002011 dst->intercept_exceptions = from->intercept_exceptions;
2012 dst->intercept = from->intercept;
2013 dst->iopm_base_pa = from->iopm_base_pa;
2014 dst->msrpm_base_pa = from->msrpm_base_pa;
2015 dst->tsc_offset = from->tsc_offset;
2016 dst->asid = from->asid;
2017 dst->tlb_ctl = from->tlb_ctl;
2018 dst->int_ctl = from->int_ctl;
2019 dst->int_vector = from->int_vector;
2020 dst->int_state = from->int_state;
2021 dst->exit_code = from->exit_code;
2022 dst->exit_code_hi = from->exit_code_hi;
2023 dst->exit_info_1 = from->exit_info_1;
2024 dst->exit_info_2 = from->exit_info_2;
2025 dst->exit_int_info = from->exit_int_info;
2026 dst->exit_int_info_err = from->exit_int_info_err;
2027 dst->nested_ctl = from->nested_ctl;
2028 dst->event_inj = from->event_inj;
2029 dst->event_inj_err = from->event_inj_err;
2030 dst->nested_cr3 = from->nested_cr3;
2031 dst->lbr_ctl = from->lbr_ctl;
2032}
2033
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002034static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002035{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002036 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002037 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02002038 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002039 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01002040
Joerg Roedel17897f32009-10-09 16:08:29 +02002041 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
2042 vmcb->control.exit_info_1,
2043 vmcb->control.exit_info_2,
2044 vmcb->control.exit_int_info,
2045 vmcb->control.exit_int_info_err);
2046
Joerg Roedel7597f122010-02-19 16:23:00 +01002047 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002048 if (!nested_vmcb)
2049 return 1;
2050
Joerg Roedel20307532010-11-29 17:51:48 +01002051 /* Exit Guest-Mode */
2052 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01002053 svm->nested.vmcb = 0;
2054
Alexander Grafcf74a782008-11-25 20:17:08 +01002055 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02002056 disable_gif(svm);
2057
2058 nested_vmcb->save.es = vmcb->save.es;
2059 nested_vmcb->save.cs = vmcb->save.cs;
2060 nested_vmcb->save.ss = vmcb->save.ss;
2061 nested_vmcb->save.ds = vmcb->save.ds;
2062 nested_vmcb->save.gdtr = vmcb->save.gdtr;
2063 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02002064 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002065 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedel2be4fc72010-04-22 12:33:09 +02002066 nested_vmcb->save.cr3 = svm->vcpu.arch.cr3;
Joerg Roedel33740e42009-08-07 11:49:29 +02002067 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002068 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Joerg Roedel33740e42009-08-07 11:49:29 +02002069 nested_vmcb->save.rflags = vmcb->save.rflags;
2070 nested_vmcb->save.rip = vmcb->save.rip;
2071 nested_vmcb->save.rsp = vmcb->save.rsp;
2072 nested_vmcb->save.rax = vmcb->save.rax;
2073 nested_vmcb->save.dr7 = vmcb->save.dr7;
2074 nested_vmcb->save.dr6 = vmcb->save.dr6;
2075 nested_vmcb->save.cpl = vmcb->save.cpl;
2076
2077 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
2078 nested_vmcb->control.int_vector = vmcb->control.int_vector;
2079 nested_vmcb->control.int_state = vmcb->control.int_state;
2080 nested_vmcb->control.exit_code = vmcb->control.exit_code;
2081 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
2082 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
2083 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
2084 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
2085 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel7a190662010-07-27 18:14:21 +02002086 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02002087
2088 /*
2089 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
2090 * to make sure that we do not lose injected events. So check event_inj
2091 * here and copy it to exit_int_info if it is valid.
2092 * Exit_int_info and event_inj can't be both valid because the case
2093 * below only happens on a VMRUN instruction intercept which has
2094 * no valid exit_int_info set.
2095 */
2096 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
2097 struct vmcb_control_area *nc = &nested_vmcb->control;
2098
2099 nc->exit_int_info = vmcb->control.event_inj;
2100 nc->exit_int_info_err = vmcb->control.event_inj_err;
2101 }
2102
Joerg Roedel33740e42009-08-07 11:49:29 +02002103 nested_vmcb->control.tlb_ctl = 0;
2104 nested_vmcb->control.event_inj = 0;
2105 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002106
2107 /* We always set V_INTR_MASKING and remember the old value in hflags */
2108 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
2109 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
2110
Alexander Grafcf74a782008-11-25 20:17:08 +01002111 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02002112 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01002113
Alexander Graf219b65d2009-06-15 15:21:25 +02002114 kvm_clear_exception_queue(&svm->vcpu);
2115 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002116
Joerg Roedel4b161842010-09-10 17:31:03 +02002117 svm->nested.nested_cr3 = 0;
2118
Alexander Grafcf74a782008-11-25 20:17:08 +01002119 /* Restore selected save entries */
2120 svm->vmcb->save.es = hsave->save.es;
2121 svm->vmcb->save.cs = hsave->save.cs;
2122 svm->vmcb->save.ss = hsave->save.ss;
2123 svm->vmcb->save.ds = hsave->save.ds;
2124 svm->vmcb->save.gdtr = hsave->save.gdtr;
2125 svm->vmcb->save.idtr = hsave->save.idtr;
2126 svm->vmcb->save.rflags = hsave->save.rflags;
2127 svm_set_efer(&svm->vcpu, hsave->save.efer);
2128 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
2129 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
2130 if (npt_enabled) {
2131 svm->vmcb->save.cr3 = hsave->save.cr3;
2132 svm->vcpu.arch.cr3 = hsave->save.cr3;
2133 } else {
Avi Kivity23902182010-06-10 17:02:16 +03002134 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01002135 }
2136 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
2137 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
2138 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
2139 svm->vmcb->save.dr7 = 0;
2140 svm->vmcb->save.cpl = 0;
2141 svm->vmcb->control.exit_int_info = 0;
2142
Joerg Roedel7597f122010-02-19 16:23:00 +01002143 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01002144
Joerg Roedel4b161842010-09-10 17:31:03 +02002145 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002146 kvm_mmu_reset_context(&svm->vcpu);
2147 kvm_mmu_load(&svm->vcpu);
2148
2149 return 0;
2150}
Alexander Graf3d6368e2008-11-25 20:17:07 +01002151
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002152static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002153{
Joerg Roedel323c3d82010-03-01 15:34:37 +01002154 /*
2155 * This function merges the msr permission bitmaps of kvm and the
2156 * nested vmcb. It is omptimized in that it only merges the parts where
2157 * the kvm msr permission bitmap may contain zero bits
2158 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01002159 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002160
Joerg Roedel323c3d82010-03-01 15:34:37 +01002161 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
2162 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002163
Joerg Roedel323c3d82010-03-01 15:34:37 +01002164 for (i = 0; i < MSRPM_OFFSETS; i++) {
2165 u32 value, p;
2166 u64 offset;
2167
2168 if (msrpm_offsets[i] == 0xffffffff)
2169 break;
2170
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002171 p = msrpm_offsets[i];
2172 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01002173
2174 if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4))
2175 return false;
2176
2177 svm->nested.msrpm[p] = svm->msrpm[p] | value;
2178 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002179
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002180 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002181
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002182 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002183}
2184
Joerg Roedel52c65a302010-08-02 16:46:44 +02002185static bool nested_vmcb_checks(struct vmcb *vmcb)
2186{
2187 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
2188 return false;
2189
Joerg Roedeldbe77582010-08-02 16:46:45 +02002190 if (vmcb->control.asid == 0)
2191 return false;
2192
Joerg Roedel4b161842010-09-10 17:31:03 +02002193 if (vmcb->control.nested_ctl && !npt_enabled)
2194 return false;
2195
Joerg Roedel52c65a302010-08-02 16:46:44 +02002196 return true;
2197}
2198
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002199static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002200{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002201 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002202 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02002203 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002204 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01002205 u64 vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002206
Joerg Roedel06fc77722010-02-19 16:23:07 +01002207 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002208
Joerg Roedel7597f122010-02-19 16:23:00 +01002209 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002210 if (!nested_vmcb)
2211 return false;
2212
Joerg Roedel52c65a302010-08-02 16:46:44 +02002213 if (!nested_vmcb_checks(nested_vmcb)) {
2214 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
2215 nested_vmcb->control.exit_code_hi = 0;
2216 nested_vmcb->control.exit_info_1 = 0;
2217 nested_vmcb->control.exit_info_2 = 0;
2218
2219 nested_svm_unmap(page);
2220
2221 return false;
2222 }
2223
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002224 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02002225 nested_vmcb->save.rip,
2226 nested_vmcb->control.int_ctl,
2227 nested_vmcb->control.event_inj,
2228 nested_vmcb->control.nested_ctl);
2229
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002230 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
2231 nested_vmcb->control.intercept_cr >> 16,
Joerg Roedel2e554e82010-02-24 18:59:14 +01002232 nested_vmcb->control.intercept_exceptions,
2233 nested_vmcb->control.intercept);
2234
Alexander Graf3d6368e2008-11-25 20:17:07 +01002235 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02002236 kvm_clear_exception_queue(&svm->vcpu);
2237 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002238
Joerg Roedele0231712010-02-24 18:59:10 +01002239 /*
2240 * Save the old vmcb, so we don't need to pick what we save, but can
2241 * restore everything when a VMEXIT occurs
2242 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02002243 hsave->save.es = vmcb->save.es;
2244 hsave->save.cs = vmcb->save.cs;
2245 hsave->save.ss = vmcb->save.ss;
2246 hsave->save.ds = vmcb->save.ds;
2247 hsave->save.gdtr = vmcb->save.gdtr;
2248 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02002249 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002250 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002251 hsave->save.cr4 = svm->vcpu.arch.cr4;
2252 hsave->save.rflags = vmcb->save.rflags;
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002253 hsave->save.rip = kvm_rip_read(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002254 hsave->save.rsp = vmcb->save.rsp;
2255 hsave->save.rax = vmcb->save.rax;
2256 if (npt_enabled)
2257 hsave->save.cr3 = vmcb->save.cr3;
2258 else
2259 hsave->save.cr3 = svm->vcpu.arch.cr3;
2260
Joerg Roedel0460a972009-08-07 11:49:31 +02002261 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002262
2263 if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
2264 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2265 else
2266 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2267
Joerg Roedel4b161842010-09-10 17:31:03 +02002268 if (nested_vmcb->control.nested_ctl) {
2269 kvm_mmu_unload(&svm->vcpu);
2270 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
2271 nested_svm_init_mmu_context(&svm->vcpu);
2272 }
2273
Alexander Graf3d6368e2008-11-25 20:17:07 +01002274 /* Load the nested guest state */
2275 svm->vmcb->save.es = nested_vmcb->save.es;
2276 svm->vmcb->save.cs = nested_vmcb->save.cs;
2277 svm->vmcb->save.ss = nested_vmcb->save.ss;
2278 svm->vmcb->save.ds = nested_vmcb->save.ds;
2279 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2280 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
2281 svm->vmcb->save.rflags = nested_vmcb->save.rflags;
2282 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2283 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2284 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2285 if (npt_enabled) {
2286 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2287 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002288 } else
Avi Kivity23902182010-06-10 17:02:16 +03002289 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002290
2291 /* Guest paging mode is active - reset mmu */
2292 kvm_mmu_reset_context(&svm->vcpu);
2293
Joerg Roedeldefbba52009-08-07 11:49:30 +02002294 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002295 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2296 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2297 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01002298
Alexander Graf3d6368e2008-11-25 20:17:07 +01002299 /* In case we don't even reach vcpu_run, the fields are not updated */
2300 svm->vmcb->save.rax = nested_vmcb->save.rax;
2301 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2302 svm->vmcb->save.rip = nested_vmcb->save.rip;
2303 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2304 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2305 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2306
Joerg Roedelf7138532010-03-01 15:34:40 +01002307 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002308 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002309
Joerg Roedelaad42c62009-08-07 11:49:34 +02002310 /* cache intercepts */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002311 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002312 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +02002313 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2314 svm->nested.intercept = nested_vmcb->control.intercept;
2315
Alexander Graf3d6368e2008-11-25 20:17:07 +01002316 force_new_asid(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002317 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002318 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2319 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2320 else
2321 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2322
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002323 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2324 /* We only want the cr8 intercept bits of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002325 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
2326 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002327 }
2328
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002329 /* We don't want to see VMMCALLs from a nested guest */
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002330 clr_intercept(svm, INTERCEPT_VMMCALL);
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002331
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002332 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002333 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2334 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2335 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002336 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2337 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2338
Joerg Roedel7597f122010-02-19 16:23:00 +01002339 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002340
Joerg Roedel20307532010-11-29 17:51:48 +01002341 /* Enter Guest-Mode */
2342 enter_guest_mode(&svm->vcpu);
2343
Joerg Roedel384c6362010-11-30 18:03:56 +01002344 /*
2345 * Merge guest and host intercepts - must be called with vcpu in
2346 * guest-mode to take affect here
2347 */
2348 recalc_intercepts(svm);
2349
Joerg Roedel06fc77722010-02-19 16:23:07 +01002350 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002351
Joerg Roedel2af91942009-08-07 11:49:28 +02002352 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002353
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002354 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002355}
2356
Joerg Roedel9966bf62009-08-07 11:49:40 +02002357static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002358{
2359 to_vmcb->save.fs = from_vmcb->save.fs;
2360 to_vmcb->save.gs = from_vmcb->save.gs;
2361 to_vmcb->save.tr = from_vmcb->save.tr;
2362 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2363 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2364 to_vmcb->save.star = from_vmcb->save.star;
2365 to_vmcb->save.lstar = from_vmcb->save.lstar;
2366 to_vmcb->save.cstar = from_vmcb->save.cstar;
2367 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2368 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2369 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2370 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002371}
2372
Avi Kivity851ba692009-08-24 11:10:17 +03002373static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002374{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002375 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002376 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002377
Alexander Graf55426752008-11-25 20:17:06 +01002378 if (nested_svm_check_permissions(svm))
2379 return 1;
2380
2381 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2382 skip_emulated_instruction(&svm->vcpu);
2383
Joerg Roedel7597f122010-02-19 16:23:00 +01002384 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002385 if (!nested_vmcb)
2386 return 1;
2387
2388 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002389 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002390
2391 return 1;
2392}
2393
Avi Kivity851ba692009-08-24 11:10:17 +03002394static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002395{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002396 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002397 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002398
Alexander Graf55426752008-11-25 20:17:06 +01002399 if (nested_svm_check_permissions(svm))
2400 return 1;
2401
2402 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2403 skip_emulated_instruction(&svm->vcpu);
2404
Joerg Roedel7597f122010-02-19 16:23:00 +01002405 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002406 if (!nested_vmcb)
2407 return 1;
2408
2409 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002410 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002411
2412 return 1;
2413}
2414
Avi Kivity851ba692009-08-24 11:10:17 +03002415static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002416{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002417 if (nested_svm_check_permissions(svm))
2418 return 1;
2419
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002420 /* Save rip after vmrun instruction */
2421 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002422
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002423 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002424 return 1;
2425
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002426 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002427 goto failed;
2428
2429 return 1;
2430
2431failed:
2432
2433 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2434 svm->vmcb->control.exit_code_hi = 0;
2435 svm->vmcb->control.exit_info_1 = 0;
2436 svm->vmcb->control.exit_info_2 = 0;
2437
2438 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002439
2440 return 1;
2441}
2442
Avi Kivity851ba692009-08-24 11:10:17 +03002443static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002444{
2445 if (nested_svm_check_permissions(svm))
2446 return 1;
2447
2448 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2449 skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03002450 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01002451
Joerg Roedel2af91942009-08-07 11:49:28 +02002452 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002453
2454 return 1;
2455}
2456
Avi Kivity851ba692009-08-24 11:10:17 +03002457static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002458{
2459 if (nested_svm_check_permissions(svm))
2460 return 1;
2461
2462 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2463 skip_emulated_instruction(&svm->vcpu);
2464
Joerg Roedel2af91942009-08-07 11:49:28 +02002465 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002466
2467 /* After a CLGI no interrupts should come */
2468 svm_clear_vintr(svm);
2469 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2470
2471 return 1;
2472}
2473
Avi Kivity851ba692009-08-24 11:10:17 +03002474static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002475{
2476 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002477
Joerg Roedelec1ff792009-10-09 16:08:31 +02002478 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
2479 vcpu->arch.regs[VCPU_REGS_RAX]);
2480
Alexander Grafff092382009-06-15 15:21:24 +02002481 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2482 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
2483
2484 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2485 skip_emulated_instruction(&svm->vcpu);
2486 return 1;
2487}
2488
Joerg Roedel532a46b2009-10-09 16:08:32 +02002489static int skinit_interception(struct vcpu_svm *svm)
2490{
2491 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
2492
2493 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2494 return 1;
2495}
2496
Avi Kivity851ba692009-08-24 11:10:17 +03002497static int invalid_op_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002498{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002499 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002500 return 1;
2501}
2502
Avi Kivity851ba692009-08-24 11:10:17 +03002503static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002504{
Izik Eidus37817f22008-03-24 23:14:53 +02002505 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002506 int reason;
2507 int int_type = svm->vmcb->control.exit_int_info &
2508 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002509 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002510 uint32_t type =
2511 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2512 uint32_t idt_v =
2513 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002514 bool has_error_code = false;
2515 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002516
2517 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002518
Izik Eidus37817f22008-03-24 23:14:53 +02002519 if (svm->vmcb->control.exit_info_2 &
2520 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002521 reason = TASK_SWITCH_IRET;
2522 else if (svm->vmcb->control.exit_info_2 &
2523 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2524 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002525 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002526 reason = TASK_SWITCH_GATE;
2527 else
2528 reason = TASK_SWITCH_CALL;
2529
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002530 if (reason == TASK_SWITCH_GATE) {
2531 switch (type) {
2532 case SVM_EXITINTINFO_TYPE_NMI:
2533 svm->vcpu.arch.nmi_injected = false;
2534 break;
2535 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002536 if (svm->vmcb->control.exit_info_2 &
2537 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2538 has_error_code = true;
2539 error_code =
2540 (u32)svm->vmcb->control.exit_info_2;
2541 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002542 kvm_clear_exception_queue(&svm->vcpu);
2543 break;
2544 case SVM_EXITINTINFO_TYPE_INTR:
2545 kvm_clear_interrupt_queue(&svm->vcpu);
2546 break;
2547 default:
2548 break;
2549 }
2550 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002551
Gleb Natapov8317c292009-04-12 13:37:02 +03002552 if (reason != TASK_SWITCH_GATE ||
2553 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2554 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002555 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2556 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002557
Gleb Natapovacb54512010-04-15 21:03:50 +03002558 if (kvm_task_switch(&svm->vcpu, tss_selector, reason,
2559 has_error_code, error_code) == EMULATE_FAIL) {
2560 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2561 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2562 svm->vcpu.run->internal.ndata = 0;
2563 return 0;
2564 }
2565 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002566}
2567
Avi Kivity851ba692009-08-24 11:10:17 +03002568static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002569{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002570 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002571 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002572 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002573}
2574
Avi Kivity851ba692009-08-24 11:10:17 +03002575static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002576{
2577 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002578 clr_intercept(svm, INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002579 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002580 return 1;
2581}
2582
Avi Kivity851ba692009-08-24 11:10:17 +03002583static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002584{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002585 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002586}
2587
Avi Kivity851ba692009-08-24 11:10:17 +03002588static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002589{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002590 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002591}
2592
Joerg Roedelcda00082010-09-02 17:29:46 +02002593static int cr0_write_interception(struct vcpu_svm *svm)
2594{
2595 struct kvm_vcpu *vcpu = &svm->vcpu;
2596 int r;
2597
2598 r = emulate_instruction(&svm->vcpu, 0, 0, 0);
2599
2600 if (svm->nested.vmexit_rip) {
2601 kvm_register_write(vcpu, VCPU_REGS_RIP, svm->nested.vmexit_rip);
2602 kvm_register_write(vcpu, VCPU_REGS_RSP, svm->nested.vmexit_rsp);
2603 kvm_register_write(vcpu, VCPU_REGS_RAX, svm->nested.vmexit_rax);
2604 svm->nested.vmexit_rip = 0;
2605 }
2606
2607 return r == EMULATE_DONE;
2608}
2609
Avi Kivity851ba692009-08-24 11:10:17 +03002610static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01002611{
Avi Kivity851ba692009-08-24 11:10:17 +03002612 struct kvm_run *kvm_run = svm->vcpu.run;
2613
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002614 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
2615 /* instruction emulation calls kvm_set_cr8() */
Avi Kivity851ba692009-08-24 11:10:17 +03002616 emulate_instruction(&svm->vcpu, 0, 0, 0);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002617 if (irqchip_in_kernel(svm->vcpu.kvm)) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002618 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel1d075432007-12-06 21:02:25 +01002619 return 1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002620 }
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002621 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
2622 return 1;
Joerg Roedel1d075432007-12-06 21:02:25 +01002623 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2624 return 0;
2625}
2626
Avi Kivity6aa8b732006-12-10 02:21:36 -08002627static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
2628{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002629 struct vcpu_svm *svm = to_svm(vcpu);
2630
Avi Kivity6aa8b732006-12-10 02:21:36 -08002631 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302632 case MSR_IA32_TSC: {
Joerg Roedel4cc70312010-11-30 18:04:01 +01002633 struct vmcb *vmcb = get_host_vmcb(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002634
Joerg Roedel4cc70312010-11-30 18:04:01 +01002635 *data = vmcb->control.tsc_offset + native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002636 break;
2637 }
Brian Gerst8c065852010-07-17 09:03:26 -04002638 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002639 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002640 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08002641#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002642 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002643 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644 break;
2645 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002646 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002647 break;
2648 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002649 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002650 break;
2651 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002652 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002653 break;
2654#endif
2655 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002656 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002657 break;
2658 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002659 *data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002660 break;
2661 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002662 *data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002663 break;
Joerg Roedele0231712010-02-24 18:59:10 +01002664 /*
2665 * Nobody will change the following 5 values in the VMCB so we can
2666 * safely return them on rdmsr. They will always be 0 until LBRV is
2667 * implemented.
2668 */
Joerg Roedela2938c82008-02-13 16:30:28 +01002669 case MSR_IA32_DEBUGCTLMSR:
2670 *data = svm->vmcb->save.dbgctl;
2671 break;
2672 case MSR_IA32_LASTBRANCHFROMIP:
2673 *data = svm->vmcb->save.br_from;
2674 break;
2675 case MSR_IA32_LASTBRANCHTOIP:
2676 *data = svm->vmcb->save.br_to;
2677 break;
2678 case MSR_IA32_LASTINTFROMIP:
2679 *data = svm->vmcb->save.last_excp_from;
2680 break;
2681 case MSR_IA32_LASTINTTOIP:
2682 *data = svm->vmcb->save.last_excp_to;
2683 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002684 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002685 *data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002686 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002687 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002688 *data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002689 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01002690 case MSR_IA32_UCODE_REV:
2691 *data = 0x01000065;
2692 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002693 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002694 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002695 }
2696 return 0;
2697}
2698
Avi Kivity851ba692009-08-24 11:10:17 +03002699static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002700{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002701 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002702 u64 data;
2703
Avi Kivity59200272010-01-25 19:47:02 +02002704 if (svm_get_msr(&svm->vcpu, ecx, &data)) {
2705 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002706 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002707 } else {
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002708 trace_kvm_msr_read(ecx, data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002709
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002710 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002711 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002712 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002713 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002714 }
2715 return 1;
2716}
2717
Joerg Roedel4a810182010-02-24 18:59:15 +01002718static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2719{
2720 struct vcpu_svm *svm = to_svm(vcpu);
2721 int svm_dis, chg_mask;
2722
2723 if (data & ~SVM_VM_CR_VALID_MASK)
2724 return 1;
2725
2726 chg_mask = SVM_VM_CR_VALID_MASK;
2727
2728 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2729 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2730
2731 svm->nested.vm_cr_msr &= ~chg_mask;
2732 svm->nested.vm_cr_msr |= (data & chg_mask);
2733
2734 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2735
2736 /* check for svm_disable while efer.svme is set */
2737 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2738 return 1;
2739
2740 return 0;
2741}
2742
Avi Kivity6aa8b732006-12-10 02:21:36 -08002743static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
2744{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002745 struct vcpu_svm *svm = to_svm(vcpu);
2746
Avi Kivity6aa8b732006-12-10 02:21:36 -08002747 switch (ecx) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10002748 case MSR_IA32_TSC:
Zachary Amsden99e3e302010-08-19 22:07:17 -10002749 kvm_write_tsc(vcpu, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002750 break;
Brian Gerst8c065852010-07-17 09:03:26 -04002751 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002752 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002753 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08002754#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002755 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002756 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002757 break;
2758 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002759 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002760 break;
2761 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002762 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002763 break;
2764 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002765 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002766 break;
2767#endif
2768 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002769 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002770 break;
2771 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002772 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002773 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002774 break;
2775 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002776 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002777 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002778 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002779 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02002780 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002781 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002782 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002783 break;
2784 }
2785 if (data & DEBUGCTL_RESERVED_BITS)
2786 return 1;
2787
2788 svm->vmcb->save.dbgctl = data;
2789 if (data & (1ULL<<0))
2790 svm_enable_lbrv(svm);
2791 else
2792 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01002793 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002794 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002795 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002796 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002797 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002798 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002799 case MSR_VM_IGNNE:
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002800 pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
2801 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002802 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002803 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002804 }
2805 return 0;
2806}
2807
Avi Kivity851ba692009-08-24 11:10:17 +03002808static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002809{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002810 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002811 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002812 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002813
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002814
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002815 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Avi Kivity59200272010-01-25 19:47:02 +02002816 if (svm_set_msr(&svm->vcpu, ecx, data)) {
2817 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002818 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002819 } else {
2820 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10002821 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02002822 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002823 return 1;
2824}
2825
Avi Kivity851ba692009-08-24 11:10:17 +03002826static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002827{
Rusty Russelle756fc62007-07-30 20:07:08 +10002828 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03002829 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002830 else
Avi Kivity851ba692009-08-24 11:10:17 +03002831 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002832}
2833
Avi Kivity851ba692009-08-24 11:10:17 +03002834static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08002835{
Avi Kivity851ba692009-08-24 11:10:17 +03002836 struct kvm_run *kvm_run = svm->vcpu.run;
2837
Avi Kivity3842d132010-07-27 12:30:24 +03002838 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01002839 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03002840 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -08002841 /*
2842 * If the user space waits to inject interrupts, exit as soon as
2843 * possible
2844 */
Gleb Natapov80618232009-04-21 17:44:56 +03002845 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
2846 kvm_run->request_interrupt_window &&
2847 !kvm_cpu_has_interrupt(&svm->vcpu)) {
Rusty Russelle756fc62007-07-30 20:07:08 +10002848 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002849 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
2850 return 0;
2851 }
2852
2853 return 1;
2854}
2855
Mark Langsdorf565d0992009-10-06 14:25:02 -05002856static int pause_interception(struct vcpu_svm *svm)
2857{
2858 kvm_vcpu_on_spin(&(svm->vcpu));
2859 return 1;
2860}
2861
Avi Kivity851ba692009-08-24 11:10:17 +03002862static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
Joerg Roedele0231712010-02-24 18:59:10 +01002863 [SVM_EXIT_READ_CR0] = emulate_on_interception,
2864 [SVM_EXIT_READ_CR3] = emulate_on_interception,
2865 [SVM_EXIT_READ_CR4] = emulate_on_interception,
2866 [SVM_EXIT_READ_CR8] = emulate_on_interception,
Avi Kivityd2251572010-01-06 10:55:27 +02002867 [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
Joerg Roedelcda00082010-09-02 17:29:46 +02002868 [SVM_EXIT_WRITE_CR0] = cr0_write_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002869 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
2870 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
2871 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
2872 [SVM_EXIT_READ_DR0] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002873 [SVM_EXIT_READ_DR1] = emulate_on_interception,
2874 [SVM_EXIT_READ_DR2] = emulate_on_interception,
2875 [SVM_EXIT_READ_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002876 [SVM_EXIT_READ_DR4] = emulate_on_interception,
2877 [SVM_EXIT_READ_DR5] = emulate_on_interception,
2878 [SVM_EXIT_READ_DR6] = emulate_on_interception,
2879 [SVM_EXIT_READ_DR7] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002880 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
2881 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
2882 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
2883 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002884 [SVM_EXIT_WRITE_DR4] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002885 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002886 [SVM_EXIT_WRITE_DR6] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002887 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002888 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
2889 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002890 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002891 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
2892 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
2893 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
2894 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02002895 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002896 [SVM_EXIT_SMI] = nop_on_interception,
2897 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08002898 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002899 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002900 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002901 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05002902 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002903 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03002904 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02002905 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002906 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002907 [SVM_EXIT_MSR] = msr_interception,
2908 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002909 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01002910 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02002911 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01002912 [SVM_EXIT_VMLOAD] = vmload_interception,
2913 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01002914 [SVM_EXIT_STGI] = stgi_interception,
2915 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02002916 [SVM_EXIT_SKINIT] = skinit_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002917 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01002918 [SVM_EXIT_MONITOR] = invalid_op_interception,
2919 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002920 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002921};
2922
Joerg Roedel3f10c842010-05-05 16:04:42 +02002923void dump_vmcb(struct kvm_vcpu *vcpu)
2924{
2925 struct vcpu_svm *svm = to_svm(vcpu);
2926 struct vmcb_control_area *control = &svm->vmcb->control;
2927 struct vmcb_save_area *save = &svm->vmcb->save;
2928
2929 pr_err("VMCB Control Area:\n");
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002930 pr_err("cr_read: %04x\n", control->intercept_cr & 0xffff);
2931 pr_err("cr_write: %04x\n", control->intercept_cr >> 16);
Joerg Roedel3aed0412010-11-30 18:03:58 +01002932 pr_err("dr_read: %04x\n", control->intercept_dr & 0xffff);
2933 pr_err("dr_write: %04x\n", control->intercept_dr >> 16);
Joerg Roedel3f10c842010-05-05 16:04:42 +02002934 pr_err("exceptions: %08x\n", control->intercept_exceptions);
2935 pr_err("intercepts: %016llx\n", control->intercept);
2936 pr_err("pause filter count: %d\n", control->pause_filter_count);
2937 pr_err("iopm_base_pa: %016llx\n", control->iopm_base_pa);
2938 pr_err("msrpm_base_pa: %016llx\n", control->msrpm_base_pa);
2939 pr_err("tsc_offset: %016llx\n", control->tsc_offset);
2940 pr_err("asid: %d\n", control->asid);
2941 pr_err("tlb_ctl: %d\n", control->tlb_ctl);
2942 pr_err("int_ctl: %08x\n", control->int_ctl);
2943 pr_err("int_vector: %08x\n", control->int_vector);
2944 pr_err("int_state: %08x\n", control->int_state);
2945 pr_err("exit_code: %08x\n", control->exit_code);
2946 pr_err("exit_info1: %016llx\n", control->exit_info_1);
2947 pr_err("exit_info2: %016llx\n", control->exit_info_2);
2948 pr_err("exit_int_info: %08x\n", control->exit_int_info);
2949 pr_err("exit_int_info_err: %08x\n", control->exit_int_info_err);
2950 pr_err("nested_ctl: %lld\n", control->nested_ctl);
2951 pr_err("nested_cr3: %016llx\n", control->nested_cr3);
2952 pr_err("event_inj: %08x\n", control->event_inj);
2953 pr_err("event_inj_err: %08x\n", control->event_inj_err);
2954 pr_err("lbr_ctl: %lld\n", control->lbr_ctl);
2955 pr_err("next_rip: %016llx\n", control->next_rip);
2956 pr_err("VMCB State Save Area:\n");
2957 pr_err("es: s: %04x a: %04x l: %08x b: %016llx\n",
2958 save->es.selector, save->es.attrib,
2959 save->es.limit, save->es.base);
2960 pr_err("cs: s: %04x a: %04x l: %08x b: %016llx\n",
2961 save->cs.selector, save->cs.attrib,
2962 save->cs.limit, save->cs.base);
2963 pr_err("ss: s: %04x a: %04x l: %08x b: %016llx\n",
2964 save->ss.selector, save->ss.attrib,
2965 save->ss.limit, save->ss.base);
2966 pr_err("ds: s: %04x a: %04x l: %08x b: %016llx\n",
2967 save->ds.selector, save->ds.attrib,
2968 save->ds.limit, save->ds.base);
2969 pr_err("fs: s: %04x a: %04x l: %08x b: %016llx\n",
2970 save->fs.selector, save->fs.attrib,
2971 save->fs.limit, save->fs.base);
2972 pr_err("gs: s: %04x a: %04x l: %08x b: %016llx\n",
2973 save->gs.selector, save->gs.attrib,
2974 save->gs.limit, save->gs.base);
2975 pr_err("gdtr: s: %04x a: %04x l: %08x b: %016llx\n",
2976 save->gdtr.selector, save->gdtr.attrib,
2977 save->gdtr.limit, save->gdtr.base);
2978 pr_err("ldtr: s: %04x a: %04x l: %08x b: %016llx\n",
2979 save->ldtr.selector, save->ldtr.attrib,
2980 save->ldtr.limit, save->ldtr.base);
2981 pr_err("idtr: s: %04x a: %04x l: %08x b: %016llx\n",
2982 save->idtr.selector, save->idtr.attrib,
2983 save->idtr.limit, save->idtr.base);
2984 pr_err("tr: s: %04x a: %04x l: %08x b: %016llx\n",
2985 save->tr.selector, save->tr.attrib,
2986 save->tr.limit, save->tr.base);
2987 pr_err("cpl: %d efer: %016llx\n",
2988 save->cpl, save->efer);
2989 pr_err("cr0: %016llx cr2: %016llx\n",
2990 save->cr0, save->cr2);
2991 pr_err("cr3: %016llx cr4: %016llx\n",
2992 save->cr3, save->cr4);
2993 pr_err("dr6: %016llx dr7: %016llx\n",
2994 save->dr6, save->dr7);
2995 pr_err("rip: %016llx rflags: %016llx\n",
2996 save->rip, save->rflags);
2997 pr_err("rsp: %016llx rax: %016llx\n",
2998 save->rsp, save->rax);
2999 pr_err("star: %016llx lstar: %016llx\n",
3000 save->star, save->lstar);
3001 pr_err("cstar: %016llx sfmask: %016llx\n",
3002 save->cstar, save->sfmask);
3003 pr_err("kernel_gs_base: %016llx sysenter_cs: %016llx\n",
3004 save->kernel_gs_base, save->sysenter_cs);
3005 pr_err("sysenter_esp: %016llx sysenter_eip: %016llx\n",
3006 save->sysenter_esp, save->sysenter_eip);
3007 pr_err("gpat: %016llx dbgctl: %016llx\n",
3008 save->g_pat, save->dbgctl);
3009 pr_err("br_from: %016llx br_to: %016llx\n",
3010 save->br_from, save->br_to);
3011 pr_err("excp_from: %016llx excp_to: %016llx\n",
3012 save->last_excp_from, save->last_excp_to);
3013
3014}
3015
Avi Kivity586f9602010-11-18 13:09:54 +02003016static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
3017{
3018 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
3019
3020 *info1 = control->exit_info_1;
3021 *info2 = control->exit_info_2;
3022}
3023
Avi Kivity851ba692009-08-24 11:10:17 +03003024static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003025{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003026 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03003027 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003028 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003029
Avi Kivityaa179112010-11-17 18:44:19 +02003030 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003031
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003032 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02003033 vcpu->arch.cr0 = svm->vmcb->save.cr0;
3034 if (npt_enabled)
3035 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003036
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003037 if (unlikely(svm->nested.exit_required)) {
3038 nested_svm_vmexit(svm);
3039 svm->nested.exit_required = false;
3040
3041 return 1;
3042 }
3043
Joerg Roedel20307532010-11-29 17:51:48 +01003044 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02003045 int vmexit;
3046
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003047 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
3048 svm->vmcb->control.exit_info_1,
3049 svm->vmcb->control.exit_info_2,
3050 svm->vmcb->control.exit_int_info,
3051 svm->vmcb->control.exit_int_info_err);
3052
Joerg Roedel410e4d52009-08-07 11:49:44 +02003053 vmexit = nested_svm_exit_special(svm);
3054
3055 if (vmexit == NESTED_EXIT_CONTINUE)
3056 vmexit = nested_svm_exit_handled(svm);
3057
3058 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01003059 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01003060 }
3061
Joerg Roedela5c38322009-08-07 11:49:32 +02003062 svm_complete_interrupts(svm);
3063
Avi Kivity04d2cc72007-09-10 18:10:54 +03003064 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3065 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3066 kvm_run->fail_entry.hardware_entry_failure_reason
3067 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02003068 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
3069 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03003070 return 0;
3071 }
3072
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003073 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003074 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02003075 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3076 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003077 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
3078 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003079 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003080 exit_code);
3081
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02003082 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08003083 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003084 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03003085 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003086 return 0;
3087 }
3088
Avi Kivity851ba692009-08-24 11:10:17 +03003089 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003090}
3091
3092static void reload_tss(struct kvm_vcpu *vcpu)
3093{
3094 int cpu = raw_smp_processor_id();
3095
Tejun Heo0fe1e002009-10-29 22:34:14 +09003096 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
3097 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003098 load_TR_desc();
3099}
3100
Rusty Russelle756fc62007-07-30 20:07:08 +10003101static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003102{
3103 int cpu = raw_smp_processor_id();
3104
Tejun Heo0fe1e002009-10-29 22:34:14 +09003105 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003106
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003107 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03003108 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09003109 if (svm->asid_generation != sd->asid_generation)
3110 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003111}
3112
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003113static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3114{
3115 struct vcpu_svm *svm = to_svm(vcpu);
3116
3117 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3118 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003119 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003120 ++vcpu->stat.nmi_injections;
3121}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003122
Eddie Dong85f455f2007-07-06 12:20:49 +03003123static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003124{
3125 struct vmcb_control_area *control;
3126
Rusty Russelle756fc62007-07-30 20:07:08 +10003127 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03003128 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003129 control->int_ctl &= ~V_INTR_PRIO_MASK;
3130 control->int_ctl |= V_IRQ_MASK |
3131 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
3132}
3133
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003134static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03003135{
3136 struct vcpu_svm *svm = to_svm(vcpu);
3137
Joerg Roedel2af91942009-08-07 11:49:28 +02003138 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01003139
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03003140 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3141 ++vcpu->stat.irq_injections;
3142
Alexander Graf219b65d2009-06-15 15:21:25 +02003143 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3144 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03003145}
3146
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003147static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
3148{
3149 struct vcpu_svm *svm = to_svm(vcpu);
3150
Joerg Roedel20307532010-11-29 17:51:48 +01003151 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003152 return;
3153
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003154 if (irr == -1)
3155 return;
3156
3157 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003158 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003159}
3160
3161static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003162{
3163 struct vcpu_svm *svm = to_svm(vcpu);
3164 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02003165 int ret;
3166 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
3167 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
3168 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
3169
3170 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003171}
3172
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003173static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3174{
3175 struct vcpu_svm *svm = to_svm(vcpu);
3176
3177 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
3178}
3179
3180static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3181{
3182 struct vcpu_svm *svm = to_svm(vcpu);
3183
3184 if (masked) {
3185 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003186 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003187 } else {
3188 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003189 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003190 }
3191}
3192
Gleb Natapov78646122009-03-23 12:12:11 +02003193static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
3194{
3195 struct vcpu_svm *svm = to_svm(vcpu);
3196 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003197 int ret;
3198
3199 if (!gif_set(svm) ||
3200 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
3201 return 0;
3202
3203 ret = !!(vmcb->save.rflags & X86_EFLAGS_IF);
3204
Joerg Roedel20307532010-11-29 17:51:48 +01003205 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003206 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
3207
3208 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02003209}
3210
Gleb Natapov9222be12009-04-23 17:14:37 +03003211static void enable_irq_window(struct kvm_vcpu *vcpu)
3212{
Alexander Graf219b65d2009-06-15 15:21:25 +02003213 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02003214
Joerg Roedele0231712010-02-24 18:59:10 +01003215 /*
3216 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3217 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3218 * get that intercept, this function will be called again though and
3219 * we'll get the vintr intercept.
3220 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01003221 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02003222 svm_set_vintr(svm);
3223 svm_inject_irq(svm, 0x0);
3224 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003225}
3226
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003227static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003228{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003229 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003230
Gleb Natapov44c11432009-05-11 13:35:52 +03003231 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
3232 == HF_NMI_MASK)
3233 return; /* IRET will cause a vm exit */
3234
Joerg Roedele0231712010-02-24 18:59:10 +01003235 /*
3236 * Something prevents NMI from been injected. Single step over possible
3237 * problem (IRET or exception injection or interrupt shadow)
3238 */
Jan Kiszka6be7d302009-10-18 13:24:54 +02003239 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03003240 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
3241 update_db_intercept(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003242}
3243
Izik Eiduscbc94022007-10-25 00:29:55 +02003244static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3245{
3246 return 0;
3247}
3248
Avi Kivityd9e368d2007-06-07 19:18:30 +03003249static void svm_flush_tlb(struct kvm_vcpu *vcpu)
3250{
3251 force_new_asid(vcpu);
3252}
3253
Avi Kivity04d2cc72007-09-10 18:10:54 +03003254static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
3255{
3256}
3257
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003258static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3259{
3260 struct vcpu_svm *svm = to_svm(vcpu);
3261
Joerg Roedel20307532010-11-29 17:51:48 +01003262 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003263 return;
3264
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003265 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003266 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03003267 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003268 }
3269}
3270
Joerg Roedel649d6862008-04-16 16:51:15 +02003271static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3272{
3273 struct vcpu_svm *svm = to_svm(vcpu);
3274 u64 cr8;
3275
Joerg Roedel20307532010-11-29 17:51:48 +01003276 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003277 return;
3278
Joerg Roedel649d6862008-04-16 16:51:15 +02003279 cr8 = kvm_get_cr8(vcpu);
3280 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3281 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3282}
3283
Gleb Natapov9222be12009-04-23 17:14:37 +03003284static void svm_complete_interrupts(struct vcpu_svm *svm)
3285{
3286 u8 vector;
3287 int type;
3288 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01003289 unsigned int3_injected = svm->int3_injected;
3290
3291 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003292
Avi Kivity3842d132010-07-27 12:30:24 +03003293 if (svm->vcpu.arch.hflags & HF_IRET_MASK) {
Gleb Natapov44c11432009-05-11 13:35:52 +03003294 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03003295 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3296 }
Gleb Natapov44c11432009-05-11 13:35:52 +03003297
Gleb Natapov9222be12009-04-23 17:14:37 +03003298 svm->vcpu.arch.nmi_injected = false;
3299 kvm_clear_exception_queue(&svm->vcpu);
3300 kvm_clear_interrupt_queue(&svm->vcpu);
3301
3302 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3303 return;
3304
Avi Kivity3842d132010-07-27 12:30:24 +03003305 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3306
Gleb Natapov9222be12009-04-23 17:14:37 +03003307 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3308 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3309
3310 switch (type) {
3311 case SVM_EXITINTINFO_TYPE_NMI:
3312 svm->vcpu.arch.nmi_injected = true;
3313 break;
3314 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01003315 /*
3316 * In case of software exceptions, do not reinject the vector,
3317 * but re-execute the instruction instead. Rewind RIP first
3318 * if we emulated INT3 before.
3319 */
3320 if (kvm_exception_is_soft(vector)) {
3321 if (vector == BP_VECTOR && int3_injected &&
3322 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
3323 kvm_rip_write(&svm->vcpu,
3324 kvm_rip_read(&svm->vcpu) -
3325 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02003326 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01003327 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003328 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3329 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003330 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03003331
3332 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003333 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03003334 break;
3335 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003336 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03003337 break;
3338 default:
3339 break;
3340 }
3341}
3342
Avi Kivityb463a6f2010-07-20 15:06:17 +03003343static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3344{
3345 struct vcpu_svm *svm = to_svm(vcpu);
3346 struct vmcb_control_area *control = &svm->vmcb->control;
3347
3348 control->exit_int_info = control->event_inj;
3349 control->exit_int_info_err = control->event_inj_err;
3350 control->event_inj = 0;
3351 svm_complete_interrupts(svm);
3352}
3353
Avi Kivity80e31d42008-07-14 14:44:59 +03003354#ifdef CONFIG_X86_64
3355#define R "r"
3356#else
3357#define R "e"
3358#endif
3359
Avi Kivity851ba692009-08-24 11:10:17 +03003360static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003361{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003362 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03003363
Joerg Roedel2041a062010-04-22 12:33:08 +02003364 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3365 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3366 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3367
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003368 /*
3369 * A vmexit emulation is required before the vcpu can be executed
3370 * again.
3371 */
3372 if (unlikely(svm->nested.exit_required))
3373 return;
3374
Rusty Russelle756fc62007-07-30 20:07:08 +10003375 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003376
Joerg Roedel649d6862008-04-16 16:51:15 +02003377 sync_lapic_to_cr8(vcpu);
3378
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003379 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003380
Avi Kivity04d2cc72007-09-10 18:10:54 +03003381 clgi();
3382
3383 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08003384
Avi Kivity6aa8b732006-12-10 02:21:36 -08003385 asm volatile (
Avi Kivity80e31d42008-07-14 14:44:59 +03003386 "push %%"R"bp; \n\t"
3387 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
3388 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
3389 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
3390 "mov %c[rsi](%[svm]), %%"R"si \n\t"
3391 "mov %c[rdi](%[svm]), %%"R"di \n\t"
3392 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003393#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003394 "mov %c[r8](%[svm]), %%r8 \n\t"
3395 "mov %c[r9](%[svm]), %%r9 \n\t"
3396 "mov %c[r10](%[svm]), %%r10 \n\t"
3397 "mov %c[r11](%[svm]), %%r11 \n\t"
3398 "mov %c[r12](%[svm]), %%r12 \n\t"
3399 "mov %c[r13](%[svm]), %%r13 \n\t"
3400 "mov %c[r14](%[svm]), %%r14 \n\t"
3401 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003402#endif
3403
Avi Kivity6aa8b732006-12-10 02:21:36 -08003404 /* Enter guest mode */
Avi Kivity80e31d42008-07-14 14:44:59 +03003405 "push %%"R"ax \n\t"
3406 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003407 __ex(SVM_VMLOAD) "\n\t"
3408 __ex(SVM_VMRUN) "\n\t"
3409 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity80e31d42008-07-14 14:44:59 +03003410 "pop %%"R"ax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003411
3412 /* Save guest registers, load host registers */
Avi Kivity80e31d42008-07-14 14:44:59 +03003413 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
3414 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
3415 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
3416 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
3417 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
3418 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003419#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003420 "mov %%r8, %c[r8](%[svm]) \n\t"
3421 "mov %%r9, %c[r9](%[svm]) \n\t"
3422 "mov %%r10, %c[r10](%[svm]) \n\t"
3423 "mov %%r11, %c[r11](%[svm]) \n\t"
3424 "mov %%r12, %c[r12](%[svm]) \n\t"
3425 "mov %%r13, %c[r13](%[svm]) \n\t"
3426 "mov %%r14, %c[r14](%[svm]) \n\t"
3427 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003428#endif
Avi Kivity80e31d42008-07-14 14:44:59 +03003429 "pop %%"R"bp"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003430 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003431 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003432 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003433 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
3434 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
3435 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
3436 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
3437 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
3438 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003439#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003440 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
3441 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
3442 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
3443 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
3444 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
3445 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
3446 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
3447 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003448#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02003449 : "cc", "memory"
Avi Kivity80e31d42008-07-14 14:44:59 +03003450 , R"bx", R"cx", R"dx", R"si", R"di"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003451#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02003452 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
3453#endif
3454 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003455
Avi Kivity82ca2d12010-10-21 12:20:34 +02003456#ifdef CONFIG_X86_64
3457 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
3458#else
Avi Kivitydacccfd2010-10-21 12:20:33 +02003459 loadsegment(fs, svm->host.fs);
Avi Kivity9581d442010-10-19 16:46:55 +02003460#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003461
3462 reload_tss(vcpu);
3463
Avi Kivity56ba47d2007-11-07 17:14:18 +02003464 local_irq_disable();
3465
3466 stgi();
3467
Avi Kivity13c34e02010-10-21 12:20:31 +02003468 vcpu->arch.cr2 = svm->vmcb->save.cr2;
3469 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3470 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3471 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
3472
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003473 sync_cr8_to_lapic(vcpu);
3474
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003475 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003476
Gleb Natapov631bc482010-10-14 11:22:52 +02003477 /* if exit due to PF check for async PF */
3478 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
3479 svm->apf_reason = kvm_read_and_reset_pf_reason();
3480
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003481 if (npt_enabled) {
3482 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3483 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3484 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02003485
3486 /*
3487 * We need to handle MC intercepts here before the vcpu has a chance to
3488 * change the physical cpu
3489 */
3490 if (unlikely(svm->vmcb->control.exit_code ==
3491 SVM_EXIT_EXCP_BASE + MC_VECTOR))
3492 svm_handle_mce(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003493}
3494
Avi Kivity80e31d42008-07-14 14:44:59 +03003495#undef R
3496
Avi Kivity6aa8b732006-12-10 02:21:36 -08003497static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3498{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003499 struct vcpu_svm *svm = to_svm(vcpu);
3500
3501 svm->vmcb->save.cr3 = root;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003502 force_new_asid(vcpu);
3503}
3504
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003505static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3506{
3507 struct vcpu_svm *svm = to_svm(vcpu);
3508
3509 svm->vmcb->control.nested_cr3 = root;
3510
3511 /* Also sync guest cr3 here in case we live migrate */
3512 svm->vmcb->save.cr3 = vcpu->arch.cr3;
3513
3514 force_new_asid(vcpu);
3515}
3516
Avi Kivity6aa8b732006-12-10 02:21:36 -08003517static int is_disabled(void)
3518{
Joerg Roedel6031a612007-06-22 12:29:50 +03003519 u64 vm_cr;
3520
3521 rdmsrl(MSR_VM_CR, vm_cr);
3522 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3523 return 1;
3524
Avi Kivity6aa8b732006-12-10 02:21:36 -08003525 return 0;
3526}
3527
Ingo Molnar102d8322007-02-19 14:37:47 +02003528static void
3529svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3530{
3531 /*
3532 * Patch in the VMMCALL instruction:
3533 */
3534 hypercall[0] = 0x0f;
3535 hypercall[1] = 0x01;
3536 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003537}
3538
Yang, Sheng002c7f72007-07-31 14:23:01 +03003539static void svm_check_processor_compat(void *rtn)
3540{
3541 *(int *)rtn = 0;
3542}
3543
Avi Kivity774ead32007-12-26 13:57:04 +02003544static bool svm_cpu_has_accelerated_tpr(void)
3545{
3546 return false;
3547}
3548
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003549static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003550{
3551 return 0;
3552}
3553
Sheng Yang0e851882009-12-18 16:48:46 +08003554static void svm_cpuid_update(struct kvm_vcpu *vcpu)
3555{
3556}
3557
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003558static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
3559{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003560 switch (func) {
Joerg Roedel24d1b152010-12-07 17:15:05 +01003561 case 0x00000001:
3562 /* Mask out xsave bit as long as it is not supported by SVM */
3563 entry->ecx &= ~(bit(X86_FEATURE_XSAVE));
3564 break;
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02003565 case 0x80000001:
3566 if (nested)
3567 entry->ecx |= (1 << 2); /* Set SVM bit */
3568 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003569 case 0x8000000A:
3570 entry->eax = 1; /* SVM revision 1 */
3571 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
3572 ASID emulation to nested SVM */
3573 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02003574 entry->edx = 0; /* Per default do not support any
3575 additional features */
3576
3577 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02003578 if (boot_cpu_has(X86_FEATURE_NRIPS))
Joerg Roedel7a190662010-07-27 18:14:21 +02003579 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003580
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02003581 /* Support NPT for the guest if enabled */
3582 if (npt_enabled)
3583 entry->edx |= SVM_FEATURE_NPT;
3584
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003585 break;
3586 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003587}
3588
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003589static const struct trace_print_flags svm_exit_reasons_str[] = {
Joerg Roedele0231712010-02-24 18:59:10 +01003590 { SVM_EXIT_READ_CR0, "read_cr0" },
3591 { SVM_EXIT_READ_CR3, "read_cr3" },
3592 { SVM_EXIT_READ_CR4, "read_cr4" },
3593 { SVM_EXIT_READ_CR8, "read_cr8" },
3594 { SVM_EXIT_WRITE_CR0, "write_cr0" },
3595 { SVM_EXIT_WRITE_CR3, "write_cr3" },
3596 { SVM_EXIT_WRITE_CR4, "write_cr4" },
3597 { SVM_EXIT_WRITE_CR8, "write_cr8" },
3598 { SVM_EXIT_READ_DR0, "read_dr0" },
3599 { SVM_EXIT_READ_DR1, "read_dr1" },
3600 { SVM_EXIT_READ_DR2, "read_dr2" },
3601 { SVM_EXIT_READ_DR3, "read_dr3" },
3602 { SVM_EXIT_WRITE_DR0, "write_dr0" },
3603 { SVM_EXIT_WRITE_DR1, "write_dr1" },
3604 { SVM_EXIT_WRITE_DR2, "write_dr2" },
3605 { SVM_EXIT_WRITE_DR3, "write_dr3" },
3606 { SVM_EXIT_WRITE_DR5, "write_dr5" },
3607 { SVM_EXIT_WRITE_DR7, "write_dr7" },
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003608 { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
3609 { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
3610 { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
3611 { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
3612 { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
3613 { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
3614 { SVM_EXIT_INTR, "interrupt" },
3615 { SVM_EXIT_NMI, "nmi" },
3616 { SVM_EXIT_SMI, "smi" },
3617 { SVM_EXIT_INIT, "init" },
3618 { SVM_EXIT_VINTR, "vintr" },
3619 { SVM_EXIT_CPUID, "cpuid" },
3620 { SVM_EXIT_INVD, "invd" },
3621 { SVM_EXIT_HLT, "hlt" },
3622 { SVM_EXIT_INVLPG, "invlpg" },
3623 { SVM_EXIT_INVLPGA, "invlpga" },
3624 { SVM_EXIT_IOIO, "io" },
3625 { SVM_EXIT_MSR, "msr" },
3626 { SVM_EXIT_TASK_SWITCH, "task_switch" },
3627 { SVM_EXIT_SHUTDOWN, "shutdown" },
3628 { SVM_EXIT_VMRUN, "vmrun" },
3629 { SVM_EXIT_VMMCALL, "hypercall" },
3630 { SVM_EXIT_VMLOAD, "vmload" },
3631 { SVM_EXIT_VMSAVE, "vmsave" },
3632 { SVM_EXIT_STGI, "stgi" },
3633 { SVM_EXIT_CLGI, "clgi" },
3634 { SVM_EXIT_SKINIT, "skinit" },
3635 { SVM_EXIT_WBINVD, "wbinvd" },
3636 { SVM_EXIT_MONITOR, "monitor" },
3637 { SVM_EXIT_MWAIT, "mwait" },
3638 { SVM_EXIT_NPF, "npf" },
3639 { -1, NULL }
3640};
3641
Sheng Yang17cc3932010-01-05 19:02:27 +08003642static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02003643{
Sheng Yang17cc3932010-01-05 19:02:27 +08003644 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02003645}
3646
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003647static bool svm_rdtscp_supported(void)
3648{
3649 return false;
3650}
3651
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003652static bool svm_has_wbinvd_exit(void)
3653{
3654 return true;
3655}
3656
Avi Kivity02daab22009-12-30 12:40:26 +02003657static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
3658{
3659 struct vcpu_svm *svm = to_svm(vcpu);
3660
Joerg Roedel18c918c2010-11-30 18:03:59 +01003661 set_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01003662 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02003663}
3664
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003665static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003666 .cpu_has_kvm_support = has_svm,
3667 .disabled_by_bios = is_disabled,
3668 .hardware_setup = svm_hardware_setup,
3669 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003670 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003671 .hardware_enable = svm_hardware_enable,
3672 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003673 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003674
3675 .vcpu_create = svm_create_vcpu,
3676 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003677 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003678
Avi Kivity04d2cc72007-09-10 18:10:54 +03003679 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003680 .vcpu_load = svm_vcpu_load,
3681 .vcpu_put = svm_vcpu_put,
3682
3683 .set_guest_debug = svm_guest_debug,
3684 .get_msr = svm_get_msr,
3685 .set_msr = svm_set_msr,
3686 .get_segment_base = svm_get_segment_base,
3687 .get_segment = svm_get_segment,
3688 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003689 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10003690 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02003691 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003692 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003693 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003694 .set_cr3 = svm_set_cr3,
3695 .set_cr4 = svm_set_cr4,
3696 .set_efer = svm_set_efer,
3697 .get_idt = svm_get_idt,
3698 .set_idt = svm_set_idt,
3699 .get_gdt = svm_get_gdt,
3700 .set_gdt = svm_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03003701 .set_dr7 = svm_set_dr7,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003702 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003703 .get_rflags = svm_get_rflags,
3704 .set_rflags = svm_set_rflags,
Avi Kivity6b52d182010-01-21 15:31:47 +02003705 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02003706 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003707
Avi Kivity6aa8b732006-12-10 02:21:36 -08003708 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003709
Avi Kivity6aa8b732006-12-10 02:21:36 -08003710 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003711 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003712 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04003713 .set_interrupt_shadow = svm_set_interrupt_shadow,
3714 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02003715 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003716 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003717 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02003718 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03003719 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02003720 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003721 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003722 .get_nmi_mask = svm_get_nmi_mask,
3723 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003724 .enable_nmi_window = enable_nmi_window,
3725 .enable_irq_window = enable_irq_window,
3726 .update_cr8_intercept = update_cr8_intercept,
Izik Eiduscbc94022007-10-25 00:29:55 +02003727
3728 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003729 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003730 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003731
Avi Kivity586f9602010-11-18 13:09:54 +02003732 .get_exit_info = svm_get_exit_info,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003733 .exit_reasons_str = svm_exit_reasons_str,
Avi Kivity586f9602010-11-18 13:09:54 +02003734
Sheng Yang17cc3932010-01-05 19:02:27 +08003735 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08003736
3737 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003738
3739 .rdtscp_supported = svm_rdtscp_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003740
3741 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003742
3743 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10003744
3745 .write_tsc_offset = svm_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10003746 .adjust_tsc_offset = svm_adjust_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003747
3748 .set_tdp_cr3 = set_tdp_cr3,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003749};
3750
3751static int __init svm_init(void)
3752{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003753 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03003754 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003755}
3756
3757static void __exit svm_exit(void)
3758{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003759 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003760}
3761
3762module_init(svm_init)
3763module_exit(svm_exit)