blob: 5038201d6f3daf9c542aeb1612c8d4b9bcb4ccdc [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
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100188enum {
Joerg Roedel116a0a22010-12-03 11:45:49 +0100189 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
190 pause filter count */
Joerg Roedelf56838e2010-12-03 11:45:50 +0100191 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
Joerg Roedeld48086d2010-12-03 11:45:51 +0100192 VMCB_ASID, /* ASID */
Joerg Roedeldecdbf62010-12-03 11:45:52 +0100193 VMCB_INTR, /* int_ctl, int_vector */
Joerg Roedelb2747162010-12-03 11:45:53 +0100194 VMCB_NPT, /* npt_en, nCR3, gPAT */
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100195 VMCB_DIRTY_MAX,
196};
197
Joerg Roedeldecdbf62010-12-03 11:45:52 +0100198/* TPR is always written before VMRUN */
199#define VMCB_ALWAYS_DIRTY_MASK (1U << VMCB_INTR)
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100200
201static inline void mark_all_dirty(struct vmcb *vmcb)
202{
203 vmcb->control.clean = 0;
204}
205
206static inline void mark_all_clean(struct vmcb *vmcb)
207{
208 vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
209 & ~VMCB_ALWAYS_DIRTY_MASK;
210}
211
212static inline void mark_dirty(struct vmcb *vmcb, int bit)
213{
214 vmcb->control.clean &= ~(1 << bit);
215}
216
Avi Kivity6aa8b732006-12-10 02:21:36 -0800217static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
218{
219 return container_of(vcpu, struct vcpu_svm, vcpu);
220}
221
Joerg Roedel384c6362010-11-30 18:03:56 +0100222static void recalc_intercepts(struct vcpu_svm *svm)
223{
224 struct vmcb_control_area *c, *h;
225 struct nested_state *g;
226
Joerg Roedel116a0a22010-12-03 11:45:49 +0100227 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
228
Joerg Roedel384c6362010-11-30 18:03:56 +0100229 if (!is_guest_mode(&svm->vcpu))
230 return;
231
232 c = &svm->vmcb->control;
233 h = &svm->nested.hsave->control;
234 g = &svm->nested;
235
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100236 c->intercept_cr = h->intercept_cr | g->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100237 c->intercept_dr = h->intercept_dr | g->intercept_dr;
Joerg Roedel384c6362010-11-30 18:03:56 +0100238 c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
239 c->intercept = h->intercept | g->intercept;
240}
241
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100242static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
243{
244 if (is_guest_mode(&svm->vcpu))
245 return svm->nested.hsave;
246 else
247 return svm->vmcb;
248}
249
250static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
251{
252 struct vmcb *vmcb = get_host_vmcb(svm);
253
254 vmcb->control.intercept_cr |= (1U << bit);
255
256 recalc_intercepts(svm);
257}
258
259static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
260{
261 struct vmcb *vmcb = get_host_vmcb(svm);
262
263 vmcb->control.intercept_cr &= ~(1U << bit);
264
265 recalc_intercepts(svm);
266}
267
268static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
269{
270 struct vmcb *vmcb = get_host_vmcb(svm);
271
272 return vmcb->control.intercept_cr & (1U << bit);
273}
274
Joerg Roedel3aed0412010-11-30 18:03:58 +0100275static inline void set_dr_intercept(struct vcpu_svm *svm, int bit)
276{
277 struct vmcb *vmcb = get_host_vmcb(svm);
278
279 vmcb->control.intercept_dr |= (1U << bit);
280
281 recalc_intercepts(svm);
282}
283
284static inline void clr_dr_intercept(struct vcpu_svm *svm, int bit)
285{
286 struct vmcb *vmcb = get_host_vmcb(svm);
287
288 vmcb->control.intercept_dr &= ~(1U << bit);
289
290 recalc_intercepts(svm);
291}
292
Joerg Roedel18c918c2010-11-30 18:03:59 +0100293static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
294{
295 struct vmcb *vmcb = get_host_vmcb(svm);
296
297 vmcb->control.intercept_exceptions |= (1U << bit);
298
299 recalc_intercepts(svm);
300}
301
302static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
303{
304 struct vmcb *vmcb = get_host_vmcb(svm);
305
306 vmcb->control.intercept_exceptions &= ~(1U << bit);
307
308 recalc_intercepts(svm);
309}
310
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100311static inline void set_intercept(struct vcpu_svm *svm, int bit)
312{
313 struct vmcb *vmcb = get_host_vmcb(svm);
314
315 vmcb->control.intercept |= (1ULL << bit);
316
317 recalc_intercepts(svm);
318}
319
320static inline void clr_intercept(struct vcpu_svm *svm, int bit)
321{
322 struct vmcb *vmcb = get_host_vmcb(svm);
323
324 vmcb->control.intercept &= ~(1ULL << bit);
325
326 recalc_intercepts(svm);
327}
328
Joerg Roedel2af91942009-08-07 11:49:28 +0200329static inline void enable_gif(struct vcpu_svm *svm)
330{
331 svm->vcpu.arch.hflags |= HF_GIF_MASK;
332}
333
334static inline void disable_gif(struct vcpu_svm *svm)
335{
336 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
337}
338
339static inline bool gif_set(struct vcpu_svm *svm)
340{
341 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
342}
343
Avi Kivity6aa8b732006-12-10 02:21:36 -0800344static unsigned long iopm_base;
345
346struct kvm_ldttss_desc {
347 u16 limit0;
348 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100349 unsigned base1:8, type:5, dpl:2, p:1;
350 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800351 u32 base3;
352 u32 zero1;
353} __attribute__((packed));
354
355struct svm_cpu_data {
356 int cpu;
357
Avi Kivity5008fdf2007-04-02 13:05:50 +0300358 u64 asid_generation;
359 u32 max_asid;
360 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800361 struct kvm_ldttss_desc *tss_desc;
362
363 struct page *save_area;
364};
365
366static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
Joerg Roedel80b77062007-03-30 17:02:14 +0300367static uint32_t svm_features;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800368
369struct svm_init_data {
370 int cpu;
371 int r;
372};
373
374static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
375
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200376#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800377#define MSRS_RANGE_SIZE 2048
378#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
379
Joerg Roedel455716f2010-03-01 15:34:35 +0100380static u32 svm_msrpm_offset(u32 msr)
381{
382 u32 offset;
383 int i;
384
385 for (i = 0; i < NUM_MSR_MAPS; i++) {
386 if (msr < msrpm_ranges[i] ||
387 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
388 continue;
389
390 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
391 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
392
393 /* Now we have the u8 offset - but need the u32 offset */
394 return offset / 4;
395 }
396
397 /* MSR not in any range */
398 return MSR_INVALID;
399}
400
Avi Kivity6aa8b732006-12-10 02:21:36 -0800401#define MAX_INST_SIZE 15
402
Avi Kivity6aa8b732006-12-10 02:21:36 -0800403static inline void clgi(void)
404{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300405 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800406}
407
408static inline void stgi(void)
409{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300410 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800411}
412
413static inline void invlpga(unsigned long addr, u32 asid)
414{
Joerg Roedele0231712010-02-24 18:59:10 +0100415 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800416}
417
Avi Kivity6aa8b732006-12-10 02:21:36 -0800418static inline void force_new_asid(struct kvm_vcpu *vcpu)
419{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400420 to_svm(vcpu)->asid_generation--;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800421}
422
423static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
424{
425 force_new_asid(vcpu);
426}
427
Joerg Roedel4b161842010-09-10 17:31:03 +0200428static int get_npt_level(void)
429{
430#ifdef CONFIG_X86_64
431 return PT64_ROOT_LEVEL;
432#else
433 return PT32E_ROOT_LEVEL;
434#endif
435}
436
Avi Kivity6aa8b732006-12-10 02:21:36 -0800437static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
438{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000439 vcpu->arch.efer = efer;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100440 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600441 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800442
Alexander Graf9962d032008-11-25 20:17:02 +0100443 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800444}
445
Avi Kivity6aa8b732006-12-10 02:21:36 -0800446static int is_external_interrupt(u32 info)
447{
448 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
449 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
450}
451
Glauber Costa2809f5d2009-05-12 16:21:05 -0400452static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
453{
454 struct vcpu_svm *svm = to_svm(vcpu);
455 u32 ret = 0;
456
457 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Jan Kiszka48005f62010-02-19 19:38:07 +0100458 ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400459 return ret & mask;
460}
461
462static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
463{
464 struct vcpu_svm *svm = to_svm(vcpu);
465
466 if (mask == 0)
467 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
468 else
469 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
470
471}
472
Avi Kivity6aa8b732006-12-10 02:21:36 -0800473static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
474{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400475 struct vcpu_svm *svm = to_svm(vcpu);
476
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200477 if (svm->vmcb->control.next_rip != 0)
478 svm->next_rip = svm->vmcb->control.next_rip;
479
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400480 if (!svm->next_rip) {
Avi Kivity851ba692009-08-24 11:10:17 +0300481 if (emulate_instruction(vcpu, 0, 0, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300482 EMULATE_DONE)
483 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800484 return;
485 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300486 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
487 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
488 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800489
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300490 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400491 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800492}
493
Jan Kiszka116a4752010-02-23 17:47:54 +0100494static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200495 bool has_error_code, u32 error_code,
496 bool reinject)
Jan Kiszka116a4752010-02-23 17:47:54 +0100497{
498 struct vcpu_svm *svm = to_svm(vcpu);
499
Joerg Roedele0231712010-02-24 18:59:10 +0100500 /*
501 * If we are within a nested VM we'd better #VMEXIT and let the guest
502 * handle the exception
503 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200504 if (!reinject &&
505 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100506 return;
507
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200508 if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
Jan Kiszka66b71382010-02-23 17:47:56 +0100509 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
510
511 /*
512 * For guest debugging where we have to reinject #BP if some
513 * INT3 is guest-owned:
514 * Emulate nRIP by moving RIP forward. Will fail if injection
515 * raises a fault that is not intercepted. Still better than
516 * failing in all cases.
517 */
518 skip_emulated_instruction(&svm->vcpu);
519 rip = kvm_rip_read(&svm->vcpu);
520 svm->int3_rip = rip + svm->vmcb->save.cs.base;
521 svm->int3_injected = rip - old_rip;
522 }
523
Jan Kiszka116a4752010-02-23 17:47:54 +0100524 svm->vmcb->control.event_inj = nr
525 | SVM_EVTINJ_VALID
526 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
527 | SVM_EVTINJ_TYPE_EXEPT;
528 svm->vmcb->control.event_inj_err = error_code;
529}
530
Joerg Roedel67ec6602010-05-17 14:43:35 +0200531static void svm_init_erratum_383(void)
532{
533 u32 low, high;
534 int err;
535 u64 val;
536
Hans Rosenfeld1be85a62010-07-28 19:09:32 +0200537 if (!cpu_has_amd_erratum(amd_erratum_383))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200538 return;
539
540 /* Use _safe variants to not break nested virtualization */
541 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
542 if (err)
543 return;
544
545 val |= (1ULL << 47);
546
547 low = lower_32_bits(val);
548 high = upper_32_bits(val);
549
550 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
551
552 erratum_383_found = true;
553}
554
Avi Kivity6aa8b732006-12-10 02:21:36 -0800555static int has_svm(void)
556{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200557 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800558
Eduardo Habkost63d11422008-11-17 19:03:20 -0200559 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800560 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800561 return 0;
562 }
563
Avi Kivity6aa8b732006-12-10 02:21:36 -0800564 return 1;
565}
566
567static void svm_hardware_disable(void *garbage)
568{
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200569 cpu_svm_disable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800570}
571
Alexander Graf10474ae2009-09-15 11:37:46 +0200572static int svm_hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800573{
574
Tejun Heo0fe1e002009-10-29 22:34:14 +0900575 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800576 uint64_t efer;
Gleb Natapov89a27f42010-02-16 10:51:48 +0200577 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800578 struct desc_struct *gdt;
579 int me = raw_smp_processor_id();
580
Alexander Graf10474ae2009-09-15 11:37:46 +0200581 rdmsrl(MSR_EFER, efer);
582 if (efer & EFER_SVME)
583 return -EBUSY;
584
Avi Kivity6aa8b732006-12-10 02:21:36 -0800585 if (!has_svm()) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000586 printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
587 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200588 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800589 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900590 sd = per_cpu(svm_data, me);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800591
Tejun Heo0fe1e002009-10-29 22:34:14 +0900592 if (!sd) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000593 printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -0800594 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200595 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800596 }
597
Tejun Heo0fe1e002009-10-29 22:34:14 +0900598 sd->asid_generation = 1;
599 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
600 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800601
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200602 native_store_gdt(&gdt_descr);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200603 gdt = (struct desc_struct *)gdt_descr.address;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900604 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800605
Alexander Graf9962d032008-11-25 20:17:02 +0100606 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800607
Linus Torvaldsd0316552009-12-14 09:58:24 -0800608 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200609
Joerg Roedel67ec6602010-05-17 14:43:35 +0200610 svm_init_erratum_383();
611
Alexander Graf10474ae2009-09-15 11:37:46 +0200612 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800613}
614
Joerg Roedel0da1db752008-07-02 16:02:11 +0200615static void svm_cpu_uninit(int cpu)
616{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900617 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200618
Tejun Heo0fe1e002009-10-29 22:34:14 +0900619 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200620 return;
621
622 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900623 __free_page(sd->save_area);
624 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200625}
626
Avi Kivity6aa8b732006-12-10 02:21:36 -0800627static int svm_cpu_init(int cpu)
628{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900629 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800630 int r;
631
Tejun Heo0fe1e002009-10-29 22:34:14 +0900632 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
633 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800634 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900635 sd->cpu = cpu;
636 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800637 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900638 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800639 goto err_1;
640
Tejun Heo0fe1e002009-10-29 22:34:14 +0900641 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800642
643 return 0;
644
645err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900646 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800647 return r;
648
649}
650
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100651static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800652{
653 int i;
654
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100655 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
656 if (direct_access_msrs[i].index == index)
657 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800658
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100659 return false;
660}
661
Avi Kivity6aa8b732006-12-10 02:21:36 -0800662static void set_msr_interception(u32 *msrpm, unsigned msr,
663 int read, int write)
664{
Joerg Roedel455716f2010-03-01 15:34:35 +0100665 u8 bit_read, bit_write;
666 unsigned long tmp;
667 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800668
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100669 /*
670 * If this warning triggers extend the direct_access_msrs list at the
671 * beginning of the file
672 */
673 WARN_ON(!valid_msr_intercept(msr));
674
Joerg Roedel455716f2010-03-01 15:34:35 +0100675 offset = svm_msrpm_offset(msr);
676 bit_read = 2 * (msr & 0x0f);
677 bit_write = 2 * (msr & 0x0f) + 1;
678 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800679
Joerg Roedel455716f2010-03-01 15:34:35 +0100680 BUG_ON(offset == MSR_INVALID);
681
682 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
683 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
684
685 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800686}
687
Joerg Roedelf65c2292008-02-13 18:58:46 +0100688static void svm_vcpu_init_msrpm(u32 *msrpm)
689{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100690 int i;
691
Joerg Roedelf65c2292008-02-13 18:58:46 +0100692 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
693
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100694 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
695 if (!direct_access_msrs[i].always)
696 continue;
697
698 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
699 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100700}
701
Joerg Roedel323c3d82010-03-01 15:34:37 +0100702static void add_msr_offset(u32 offset)
703{
704 int i;
705
706 for (i = 0; i < MSRPM_OFFSETS; ++i) {
707
708 /* Offset already in list? */
709 if (msrpm_offsets[i] == offset)
710 return;
711
712 /* Slot used by another offset? */
713 if (msrpm_offsets[i] != MSR_INVALID)
714 continue;
715
716 /* Add offset to list */
717 msrpm_offsets[i] = offset;
718
719 return;
720 }
721
722 /*
723 * If this BUG triggers the msrpm_offsets table has an overflow. Just
724 * increase MSRPM_OFFSETS in this case.
725 */
726 BUG();
727}
728
729static void init_msrpm_offsets(void)
730{
731 int i;
732
733 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
734
735 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
736 u32 offset;
737
738 offset = svm_msrpm_offset(direct_access_msrs[i].index);
739 BUG_ON(offset == MSR_INVALID);
740
741 add_msr_offset(offset);
742 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800743}
744
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100745static void svm_enable_lbrv(struct vcpu_svm *svm)
746{
747 u32 *msrpm = svm->msrpm;
748
749 svm->vmcb->control.lbr_ctl = 1;
750 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
751 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
752 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
753 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
754}
755
756static void svm_disable_lbrv(struct vcpu_svm *svm)
757{
758 u32 *msrpm = svm->msrpm;
759
760 svm->vmcb->control.lbr_ctl = 0;
761 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
762 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
763 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
764 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
765}
766
Avi Kivity6aa8b732006-12-10 02:21:36 -0800767static __init int svm_hardware_setup(void)
768{
769 int cpu;
770 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100771 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800772 int r;
773
Avi Kivity6aa8b732006-12-10 02:21:36 -0800774 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
775
776 if (!iopm_pages)
777 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300778
779 iopm_va = page_address(iopm_pages);
780 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800781 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
782
Joerg Roedel323c3d82010-03-01 15:34:37 +0100783 init_msrpm_offsets();
784
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100785 if (boot_cpu_has(X86_FEATURE_NX))
786 kvm_enable_efer_bits(EFER_NX);
787
Alexander Graf1b2fd702009-02-02 16:23:51 +0100788 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
789 kvm_enable_efer_bits(EFER_FFXSR);
790
Alexander Graf236de052008-11-25 20:17:10 +0100791 if (nested) {
792 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +0200793 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +0100794 }
795
Zachary Amsden3230bb42009-09-29 11:38:37 -1000796 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800797 r = svm_cpu_init(cpu);
798 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100799 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800800 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100801
802 svm_features = cpuid_edx(SVM_CPUID_FUNC);
803
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200804 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100805 npt_enabled = false;
806
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100807 if (npt_enabled && !npt) {
808 printk(KERN_INFO "kvm: Nested Paging disabled\n");
809 npt_enabled = false;
810 }
811
Joerg Roedel18552672008-02-07 13:47:41 +0100812 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100813 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100814 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200815 } else
816 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100817
Avi Kivity6aa8b732006-12-10 02:21:36 -0800818 return 0;
819
Joerg Roedelf65c2292008-02-13 18:58:46 +0100820err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800821 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
822 iopm_base = 0;
823 return r;
824}
825
826static __exit void svm_hardware_unsetup(void)
827{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200828 int cpu;
829
Zachary Amsden3230bb42009-09-29 11:38:37 -1000830 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200831 svm_cpu_uninit(cpu);
832
Avi Kivity6aa8b732006-12-10 02:21:36 -0800833 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100834 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800835}
836
837static void init_seg(struct vmcb_seg *seg)
838{
839 seg->selector = 0;
840 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +0100841 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800842 seg->limit = 0xffff;
843 seg->base = 0;
844}
845
846static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
847{
848 seg->selector = 0;
849 seg->attrib = SVM_SELECTOR_P_MASK | type;
850 seg->limit = 0xffff;
851 seg->base = 0;
852}
853
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -1000854static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
855{
856 struct vcpu_svm *svm = to_svm(vcpu);
857 u64 g_tsc_offset = 0;
858
Joerg Roedel20307532010-11-29 17:51:48 +0100859 if (is_guest_mode(vcpu)) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -1000860 g_tsc_offset = svm->vmcb->control.tsc_offset -
861 svm->nested.hsave->control.tsc_offset;
862 svm->nested.hsave->control.tsc_offset = offset;
863 }
864
865 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
Joerg Roedel116a0a22010-12-03 11:45:49 +0100866
867 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -1000868}
869
Zachary Amsdene48672f2010-08-19 22:07:23 -1000870static void svm_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment)
871{
872 struct vcpu_svm *svm = to_svm(vcpu);
873
874 svm->vmcb->control.tsc_offset += adjustment;
Joerg Roedel20307532010-11-29 17:51:48 +0100875 if (is_guest_mode(vcpu))
Zachary Amsdene48672f2010-08-19 22:07:23 -1000876 svm->nested.hsave->control.tsc_offset += adjustment;
Joerg Roedel116a0a22010-12-03 11:45:49 +0100877 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdene48672f2010-08-19 22:07:23 -1000878}
879
Joerg Roedele6101a92008-02-13 18:58:45 +0100880static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881{
Joerg Roedele6101a92008-02-13 18:58:45 +0100882 struct vmcb_control_area *control = &svm->vmcb->control;
883 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800884
Avi Kivitybff78272010-01-07 13:16:08 +0200885 svm->vcpu.fpu_active = 1;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100886 svm->vcpu.arch.hflags = 0;
Avi Kivitybff78272010-01-07 13:16:08 +0200887
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100888 set_cr_intercept(svm, INTERCEPT_CR0_READ);
889 set_cr_intercept(svm, INTERCEPT_CR3_READ);
890 set_cr_intercept(svm, INTERCEPT_CR4_READ);
891 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
892 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
893 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
894 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800895
Joerg Roedel3aed0412010-11-30 18:03:58 +0100896 set_dr_intercept(svm, INTERCEPT_DR0_READ);
897 set_dr_intercept(svm, INTERCEPT_DR1_READ);
898 set_dr_intercept(svm, INTERCEPT_DR2_READ);
899 set_dr_intercept(svm, INTERCEPT_DR3_READ);
900 set_dr_intercept(svm, INTERCEPT_DR4_READ);
901 set_dr_intercept(svm, INTERCEPT_DR5_READ);
902 set_dr_intercept(svm, INTERCEPT_DR6_READ);
903 set_dr_intercept(svm, INTERCEPT_DR7_READ);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800904
Joerg Roedel3aed0412010-11-30 18:03:58 +0100905 set_dr_intercept(svm, INTERCEPT_DR0_WRITE);
906 set_dr_intercept(svm, INTERCEPT_DR1_WRITE);
907 set_dr_intercept(svm, INTERCEPT_DR2_WRITE);
908 set_dr_intercept(svm, INTERCEPT_DR3_WRITE);
909 set_dr_intercept(svm, INTERCEPT_DR4_WRITE);
910 set_dr_intercept(svm, INTERCEPT_DR5_WRITE);
911 set_dr_intercept(svm, INTERCEPT_DR6_WRITE);
912 set_dr_intercept(svm, INTERCEPT_DR7_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913
Joerg Roedel18c918c2010-11-30 18:03:59 +0100914 set_exception_intercept(svm, PF_VECTOR);
915 set_exception_intercept(svm, UD_VECTOR);
916 set_exception_intercept(svm, MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800917
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100918 set_intercept(svm, INTERCEPT_INTR);
919 set_intercept(svm, INTERCEPT_NMI);
920 set_intercept(svm, INTERCEPT_SMI);
921 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
922 set_intercept(svm, INTERCEPT_CPUID);
923 set_intercept(svm, INTERCEPT_INVD);
924 set_intercept(svm, INTERCEPT_HLT);
925 set_intercept(svm, INTERCEPT_INVLPG);
926 set_intercept(svm, INTERCEPT_INVLPGA);
927 set_intercept(svm, INTERCEPT_IOIO_PROT);
928 set_intercept(svm, INTERCEPT_MSR_PROT);
929 set_intercept(svm, INTERCEPT_TASK_SWITCH);
930 set_intercept(svm, INTERCEPT_SHUTDOWN);
931 set_intercept(svm, INTERCEPT_VMRUN);
932 set_intercept(svm, INTERCEPT_VMMCALL);
933 set_intercept(svm, INTERCEPT_VMLOAD);
934 set_intercept(svm, INTERCEPT_VMSAVE);
935 set_intercept(svm, INTERCEPT_STGI);
936 set_intercept(svm, INTERCEPT_CLGI);
937 set_intercept(svm, INTERCEPT_SKINIT);
938 set_intercept(svm, INTERCEPT_WBINVD);
939 set_intercept(svm, INTERCEPT_MONITOR);
940 set_intercept(svm, INTERCEPT_MWAIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800941
942 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100943 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944 control->int_ctl = V_INTR_MASKING_MASK;
945
946 init_seg(&save->es);
947 init_seg(&save->ss);
948 init_seg(&save->ds);
949 init_seg(&save->fs);
950 init_seg(&save->gs);
951
952 save->cs.selector = 0xf000;
953 /* Executable/Readable Code Segment */
954 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
955 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
956 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800957 /*
958 * cs.base should really be 0xffff0000, but vmx can't handle that, so
959 * be consistent with it.
960 *
961 * Replace when we have real mode working for vmx.
962 */
963 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800964
965 save->gdtr.limit = 0xffff;
966 save->idtr.limit = 0xffff;
967
968 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
969 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
970
Marcelo Tosattieaa48512010-08-31 19:13:14 -0300971 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -0400972 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800973 save->dr7 = 0x400;
974 save->rflags = 2;
975 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300976 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800977
Joerg Roedele0231712010-02-24 18:59:10 +0100978 /*
979 * This is the guest-visible cr0 value.
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200980 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800981 */
Marcelo Tosatti678041a2010-08-31 19:13:13 -0300982 svm->vcpu.arch.cr0 = 0;
983 (void)kvm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200984
Rusty Russell66aee912007-07-17 23:34:16 +1000985 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100987
988 if (npt_enabled) {
989 /* Setup VMCB for Nested Paging */
990 control->nested_ctl = 1;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100991 clr_intercept(svm, INTERCEPT_TASK_SWITCH);
992 clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +0100993 clr_exception_intercept(svm, PF_VECTOR);
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100994 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
995 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100996 save->g_pat = 0x0007040600070406ULL;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100997 save->cr3 = 0;
998 save->cr4 = 0;
999 }
Avi Kivitya79d2f12008-04-14 13:10:21 +03001000 force_new_asid(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01001001
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001002 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +02001003 svm->vcpu.arch.hflags = 0;
1004
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001005 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
Mark Langsdorf565d0992009-10-06 14:25:02 -05001006 control->pause_filter_count = 3000;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001007 set_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001008 }
1009
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001010 mark_all_dirty(svm->vmcb);
1011
Joerg Roedel2af91942009-08-07 11:49:28 +02001012 enable_gif(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001013}
1014
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001015static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +03001016{
1017 struct vcpu_svm *svm = to_svm(vcpu);
1018
Joerg Roedele6101a92008-02-13 18:58:45 +01001019 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +02001020
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001021 if (!kvm_vcpu_is_bsp(vcpu)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001022 kvm_rip_write(vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001023 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
1024 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +02001025 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001026 vcpu->arch.regs_avail = ~0;
1027 vcpu->arch.regs_dirty = ~0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001028
1029 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001030}
1031
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001032static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001034 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001036 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +01001037 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001038 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001039 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001040
Rusty Russellc16f8622007-07-30 21:12:19 +10001041 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001042 if (!svm) {
1043 err = -ENOMEM;
1044 goto out;
1045 }
1046
1047 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
1048 if (err)
1049 goto free_svm;
1050
Joerg Roedelf65c2292008-02-13 18:58:46 +01001051 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001052 page = alloc_page(GFP_KERNEL);
1053 if (!page)
1054 goto uninit;
1055
Joerg Roedelf65c2292008-02-13 18:58:46 +01001056 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1057 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001058 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001059
1060 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1061 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001062 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001063
Alexander Grafb286d5d2008-11-25 20:17:05 +01001064 hsave_page = alloc_page(GFP_KERNEL);
1065 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001066 goto free_page3;
1067
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001068 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +01001069
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001070 svm->msrpm = page_address(msrpm_pages);
1071 svm_vcpu_init_msrpm(svm->msrpm);
1072
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001073 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +01001074 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001075
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001076 svm->vmcb = page_address(page);
1077 clear_page(svm->vmcb);
1078 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
1079 svm->asid_generation = 0;
Joerg Roedele6101a92008-02-13 18:58:45 +01001080 init_vmcb(svm);
Zachary Amsden99e3e302010-08-19 22:07:17 -10001081 kvm_write_tsc(&svm->vcpu, 0);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001082
Jan Kiszka10ab25c2010-05-25 16:01:50 +02001083 err = fx_init(&svm->vcpu);
1084 if (err)
1085 goto free_page4;
1086
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001087 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001088 if (kvm_vcpu_is_bsp(&svm->vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001089 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001090
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001091 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -08001092
Jan Kiszka10ab25c2010-05-25 16:01:50 +02001093free_page4:
1094 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001095free_page3:
1096 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
1097free_page2:
1098 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
1099free_page1:
1100 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001101uninit:
1102 kvm_vcpu_uninit(&svm->vcpu);
1103free_svm:
Rusty Russella4770342007-08-01 14:46:11 +10001104 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001105out:
1106 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001107}
1108
1109static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1110{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001111 struct vcpu_svm *svm = to_svm(vcpu);
1112
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001113 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +01001114 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001115 __free_page(virt_to_page(svm->nested.hsave));
1116 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001117 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10001118 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001119}
1120
Avi Kivity15ad7142007-07-11 18:17:21 +03001121static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001122{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001123 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001124 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02001125
Avi Kivity0cc50642007-03-25 12:07:27 +02001126 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03001127 svm->asid_generation = 0;
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001128 mark_all_dirty(svm->vmcb);
Avi Kivity0cc50642007-03-25 12:07:27 +02001129 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001130
Avi Kivity82ca2d12010-10-21 12:20:34 +02001131#ifdef CONFIG_X86_64
1132 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
1133#endif
Avi Kivitydacccfd2010-10-21 12:20:33 +02001134 savesegment(fs, svm->host.fs);
1135 savesegment(gs, svm->host.gs);
1136 svm->host.ldt = kvm_read_ldt();
1137
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001138 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001139 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001140}
1141
1142static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1143{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001144 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001145 int i;
1146
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001147 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02001148 kvm_load_ldt(svm->host.ldt);
1149#ifdef CONFIG_X86_64
1150 loadsegment(fs, svm->host.fs);
1151 load_gs_index(svm->host.gs);
1152 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs);
1153#else
1154 loadsegment(gs, svm->host.gs);
1155#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001156 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001157 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001158}
1159
Avi Kivity6aa8b732006-12-10 02:21:36 -08001160static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1161{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001162 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001163}
1164
1165static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1166{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001167 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001168}
1169
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001170static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1171{
1172 switch (reg) {
1173 case VCPU_EXREG_PDPTR:
1174 BUG_ON(!npt_enabled);
Joerg Roedelff03a072010-09-10 17:30:57 +02001175 load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3);
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001176 break;
1177 default:
1178 BUG();
1179 }
1180}
1181
Alexander Graff0b85052008-11-25 20:17:01 +01001182static void svm_set_vintr(struct vcpu_svm *svm)
1183{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001184 set_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001185}
1186
1187static void svm_clear_vintr(struct vcpu_svm *svm)
1188{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001189 clr_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001190}
1191
Avi Kivity6aa8b732006-12-10 02:21:36 -08001192static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1193{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001194 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001195
1196 switch (seg) {
1197 case VCPU_SREG_CS: return &save->cs;
1198 case VCPU_SREG_DS: return &save->ds;
1199 case VCPU_SREG_ES: return &save->es;
1200 case VCPU_SREG_FS: return &save->fs;
1201 case VCPU_SREG_GS: return &save->gs;
1202 case VCPU_SREG_SS: return &save->ss;
1203 case VCPU_SREG_TR: return &save->tr;
1204 case VCPU_SREG_LDTR: return &save->ldtr;
1205 }
1206 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001207 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001208}
1209
1210static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1211{
1212 struct vmcb_seg *s = svm_seg(vcpu, seg);
1213
1214 return s->base;
1215}
1216
1217static void svm_get_segment(struct kvm_vcpu *vcpu,
1218 struct kvm_segment *var, int seg)
1219{
1220 struct vmcb_seg *s = svm_seg(vcpu, seg);
1221
1222 var->base = s->base;
1223 var->limit = s->limit;
1224 var->selector = s->selector;
1225 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1226 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1227 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1228 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1229 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1230 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1231 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
1232 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
Amit Shah25022ac2008-10-27 09:04:17 +00001233
Joerg Roedele0231712010-02-24 18:59:10 +01001234 /*
1235 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001236 * for cross vendor migration purposes by "not present"
1237 */
1238 var->unusable = !var->present || (var->type == 0);
1239
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001240 switch (seg) {
1241 case VCPU_SREG_CS:
1242 /*
1243 * SVM always stores 0 for the 'G' bit in the CS selector in
1244 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
1245 * Intel's VMENTRY has a check on the 'G' bit.
1246 */
Amit Shah25022ac2008-10-27 09:04:17 +00001247 var->g = s->limit > 0xfffff;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001248 break;
1249 case VCPU_SREG_TR:
1250 /*
1251 * Work around a bug where the busy flag in the tr selector
1252 * isn't exposed
1253 */
Amit Shahc0d09822008-10-27 09:04:18 +00001254 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001255 break;
1256 case VCPU_SREG_DS:
1257 case VCPU_SREG_ES:
1258 case VCPU_SREG_FS:
1259 case VCPU_SREG_GS:
1260 /*
1261 * The accessed bit must always be set in the segment
1262 * descriptor cache, although it can be cleared in the
1263 * descriptor, the cached bit always remains at 1. Since
1264 * Intel has a check on this, set it here to support
1265 * cross-vendor migration.
1266 */
1267 if (!var->unusable)
1268 var->type |= 0x1;
1269 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001270 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001271 /*
1272 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001273 * descriptor is left as 1, although the whole segment has
1274 * been made unusable. Clear it here to pass an Intel VMX
1275 * entry check when cross vendor migrating.
1276 */
1277 if (var->unusable)
1278 var->db = 0;
1279 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001280 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001281}
1282
Izik Eidus2e4d2652008-03-24 19:38:34 +02001283static int svm_get_cpl(struct kvm_vcpu *vcpu)
1284{
1285 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1286
1287 return save->cpl;
1288}
1289
Gleb Natapov89a27f42010-02-16 10:51:48 +02001290static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001291{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001292 struct vcpu_svm *svm = to_svm(vcpu);
1293
Gleb Natapov89a27f42010-02-16 10:51:48 +02001294 dt->size = svm->vmcb->save.idtr.limit;
1295 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001296}
1297
Gleb Natapov89a27f42010-02-16 10:51:48 +02001298static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001299{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001300 struct vcpu_svm *svm = to_svm(vcpu);
1301
Gleb Natapov89a27f42010-02-16 10:51:48 +02001302 svm->vmcb->save.idtr.limit = dt->size;
1303 svm->vmcb->save.idtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001304}
1305
Gleb Natapov89a27f42010-02-16 10:51:48 +02001306static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001307{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001308 struct vcpu_svm *svm = to_svm(vcpu);
1309
Gleb Natapov89a27f42010-02-16 10:51:48 +02001310 dt->size = svm->vmcb->save.gdtr.limit;
1311 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001312}
1313
Gleb Natapov89a27f42010-02-16 10:51:48 +02001314static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001315{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001316 struct vcpu_svm *svm = to_svm(vcpu);
1317
Gleb Natapov89a27f42010-02-16 10:51:48 +02001318 svm->vmcb->save.gdtr.limit = dt->size;
1319 svm->vmcb->save.gdtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001320}
1321
Avi Kivitye8467fd2009-12-29 18:43:06 +02001322static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1323{
1324}
1325
Anthony Liguori25c4c272007-04-27 09:29:21 +03001326static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001327{
1328}
1329
Avi Kivityd2251572010-01-06 10:55:27 +02001330static void update_cr0_intercept(struct vcpu_svm *svm)
1331{
1332 ulong gcr0 = svm->vcpu.arch.cr0;
1333 u64 *hcr0 = &svm->vmcb->save.cr0;
1334
1335 if (!svm->vcpu.fpu_active)
1336 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1337 else
1338 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1339 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1340
1341
1342 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001343 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
1344 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001345 } else {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001346 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1347 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001348 }
1349}
1350
Avi Kivity6aa8b732006-12-10 02:21:36 -08001351static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1352{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001353 struct vcpu_svm *svm = to_svm(vcpu);
1354
Joerg Roedel20307532010-11-29 17:51:48 +01001355 if (is_guest_mode(vcpu)) {
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001356 /*
1357 * We are here because we run in nested mode, the host kvm
1358 * intercepts cr0 writes but the l1 hypervisor does not.
1359 * But the L1 hypervisor may intercept selective cr0 writes.
1360 * This needs to be checked here.
1361 */
1362 unsigned long old, new;
1363
1364 /* Remove bits that would trigger a real cr0 write intercept */
1365 old = vcpu->arch.cr0 & SVM_CR0_SELECTIVE_MASK;
1366 new = cr0 & SVM_CR0_SELECTIVE_MASK;
1367
1368 if (old == new) {
1369 /* cr0 write with ts and mp unchanged */
1370 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
Joerg Roedelcda00082010-09-02 17:29:46 +02001371 if (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE) {
1372 svm->nested.vmexit_rip = kvm_rip_read(vcpu);
1373 svm->nested.vmexit_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
1374 svm->nested.vmexit_rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001375 return;
Joerg Roedelcda00082010-09-02 17:29:46 +02001376 }
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001377 }
1378 }
1379
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001380#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001381 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001382 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001383 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001384 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385 }
1386
Mike Dayd77c26f2007-10-08 09:02:08 -04001387 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001388 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001389 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001390 }
1391 }
1392#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001393 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001394
1395 if (!npt_enabled)
1396 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001397
1398 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001399 cr0 |= X86_CR0_TS;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001400 /*
1401 * re-enable caching here because the QEMU bios
1402 * does not do it - this results in some delay at
1403 * reboot
1404 */
1405 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001406 svm->vmcb->save.cr0 = cr0;
Avi Kivityd2251572010-01-06 10:55:27 +02001407 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001408}
1409
1410static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1411{
Joerg Roedel6394b642008-04-09 14:15:29 +02001412 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001413 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1414
1415 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
1416 force_new_asid(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001417
Joerg Roedelec077262008-04-09 14:15:28 +02001418 vcpu->arch.cr4 = cr4;
1419 if (!npt_enabled)
1420 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001421 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001422 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001423}
1424
1425static void svm_set_segment(struct kvm_vcpu *vcpu,
1426 struct kvm_segment *var, int seg)
1427{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001428 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429 struct vmcb_seg *s = svm_seg(vcpu, seg);
1430
1431 s->base = var->base;
1432 s->limit = var->limit;
1433 s->selector = var->selector;
1434 if (var->unusable)
1435 s->attrib = 0;
1436 else {
1437 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1438 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1439 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1440 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1441 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1442 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1443 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1444 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1445 }
1446 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001447 svm->vmcb->save.cpl
1448 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -08001449 >> SVM_SELECTOR_DPL_SHIFT) & 3;
1450
1451}
1452
Gleb Natapov44c11432009-05-11 13:35:52 +03001453static void update_db_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001454{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001455 struct vcpu_svm *svm = to_svm(vcpu);
1456
Joerg Roedel18c918c2010-11-30 18:03:59 +01001457 clr_exception_intercept(svm, DB_VECTOR);
1458 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001459
Jan Kiszka6be7d302009-10-18 13:24:54 +02001460 if (svm->nmi_singlestep)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001461 set_exception_intercept(svm, DB_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001462
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001463 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1464 if (vcpu->guest_debug &
1465 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
Joerg Roedel18c918c2010-11-30 18:03:59 +01001466 set_exception_intercept(svm, DB_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001467 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001468 set_exception_intercept(svm, BP_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001469 } else
1470 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001471}
1472
Jan Kiszka355be0b2009-10-03 00:31:21 +02001473static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Gleb Natapov44c11432009-05-11 13:35:52 +03001474{
Gleb Natapov44c11432009-05-11 13:35:52 +03001475 struct vcpu_svm *svm = to_svm(vcpu);
1476
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001477 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1478 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1479 else
1480 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1481
Jan Kiszka355be0b2009-10-03 00:31:21 +02001482 update_db_intercept(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001483}
1484
Tejun Heo0fe1e002009-10-29 22:34:14 +09001485static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001486{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001487 if (sd->next_asid > sd->max_asid) {
1488 ++sd->asid_generation;
1489 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001490 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001491 }
1492
Tejun Heo0fe1e002009-10-29 22:34:14 +09001493 svm->asid_generation = sd->asid_generation;
1494 svm->vmcb->control.asid = sd->next_asid++;
Joerg Roedeld48086d2010-12-03 11:45:51 +01001495
1496 mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001497}
1498
Gleb Natapov020df072010-04-13 10:05:23 +03001499static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001500{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001501 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001502
Gleb Natapov020df072010-04-13 10:05:23 +03001503 svm->vmcb->save.dr7 = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001504}
1505
Avi Kivity851ba692009-08-24 11:10:17 +03001506static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507{
Gleb Natapov631bc482010-10-14 11:22:52 +02001508 u64 fault_address = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001509 u32 error_code;
Gleb Natapov631bc482010-10-14 11:22:52 +02001510 int r = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001511
Gleb Natapov631bc482010-10-14 11:22:52 +02001512 switch (svm->apf_reason) {
1513 default:
1514 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001515
Gleb Natapov631bc482010-10-14 11:22:52 +02001516 trace_kvm_page_fault(fault_address, error_code);
1517 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1518 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
1519 r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
1520 break;
1521 case KVM_PV_REASON_PAGE_NOT_PRESENT:
1522 svm->apf_reason = 0;
1523 local_irq_disable();
1524 kvm_async_pf_task_wait(fault_address);
1525 local_irq_enable();
1526 break;
1527 case KVM_PV_REASON_PAGE_READY:
1528 svm->apf_reason = 0;
1529 local_irq_disable();
1530 kvm_async_pf_task_wake(fault_address);
1531 local_irq_enable();
1532 break;
1533 }
1534 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001535}
1536
Avi Kivity851ba692009-08-24 11:10:17 +03001537static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001538{
Avi Kivity851ba692009-08-24 11:10:17 +03001539 struct kvm_run *kvm_run = svm->vcpu.run;
1540
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001541 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001542 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001543 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001544 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1545 return 1;
1546 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001547
Jan Kiszka6be7d302009-10-18 13:24:54 +02001548 if (svm->nmi_singlestep) {
1549 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001550 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1551 svm->vmcb->save.rflags &=
1552 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1553 update_db_intercept(&svm->vcpu);
1554 }
1555
1556 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001557 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001558 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1559 kvm_run->debug.arch.pc =
1560 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1561 kvm_run->debug.arch.exception = DB_VECTOR;
1562 return 0;
1563 }
1564
1565 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001566}
1567
Avi Kivity851ba692009-08-24 11:10:17 +03001568static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001569{
Avi Kivity851ba692009-08-24 11:10:17 +03001570 struct kvm_run *kvm_run = svm->vcpu.run;
1571
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001572 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1573 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1574 kvm_run->debug.arch.exception = BP_VECTOR;
1575 return 0;
1576}
1577
Avi Kivity851ba692009-08-24 11:10:17 +03001578static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001579{
1580 int er;
1581
Avi Kivity851ba692009-08-24 11:10:17 +03001582 er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001583 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001584 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001585 return 1;
1586}
1587
Avi Kivity6b52d182010-01-21 15:31:47 +02001588static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001589{
Avi Kivity6b52d182010-01-21 15:31:47 +02001590 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001591
Joerg Roedel18c918c2010-11-30 18:03:59 +01001592 clr_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001593
Rusty Russelle756fc62007-07-30 20:07:08 +10001594 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001595 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001596}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001597
Avi Kivity6b52d182010-01-21 15:31:47 +02001598static int nm_interception(struct vcpu_svm *svm)
1599{
1600 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001601 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001602}
1603
Joerg Roedel67ec6602010-05-17 14:43:35 +02001604static bool is_erratum_383(void)
1605{
1606 int err, i;
1607 u64 value;
1608
1609 if (!erratum_383_found)
1610 return false;
1611
1612 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1613 if (err)
1614 return false;
1615
1616 /* Bit 62 may or may not be set for this mce */
1617 value &= ~(1ULL << 62);
1618
1619 if (value != 0xb600000000010015ULL)
1620 return false;
1621
1622 /* Clear MCi_STATUS registers */
1623 for (i = 0; i < 6; ++i)
1624 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1625
1626 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1627 if (!err) {
1628 u32 low, high;
1629
1630 value &= ~(1ULL << 2);
1631 low = lower_32_bits(value);
1632 high = upper_32_bits(value);
1633
1634 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1635 }
1636
1637 /* Flush tlb to evict multi-match entries */
1638 __flush_tlb_all();
1639
1640 return true;
1641}
1642
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001643static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001644{
Joerg Roedel67ec6602010-05-17 14:43:35 +02001645 if (is_erratum_383()) {
1646 /*
1647 * Erratum 383 triggered. Guest state is corrupt so kill the
1648 * guest.
1649 */
1650 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1651
Avi Kivitya8eeb042010-05-10 12:34:53 +03001652 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02001653
1654 return;
1655 }
1656
Joerg Roedel53371b52008-04-09 14:15:30 +02001657 /*
1658 * On an #MC intercept the MCE handler is not called automatically in
1659 * the host. So do it by hand here.
1660 */
1661 asm volatile (
1662 "int $0x12\n");
1663 /* not sure if we ever come back to this point */
1664
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001665 return;
1666}
1667
1668static int mc_interception(struct vcpu_svm *svm)
1669{
Joerg Roedel53371b52008-04-09 14:15:30 +02001670 return 1;
1671}
1672
Avi Kivity851ba692009-08-24 11:10:17 +03001673static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001674{
Avi Kivity851ba692009-08-24 11:10:17 +03001675 struct kvm_run *kvm_run = svm->vcpu.run;
1676
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001677 /*
1678 * VMCB is undefined after a SHUTDOWN intercept
1679 * so reinitialize it.
1680 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001681 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001682 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001683
1684 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1685 return 0;
1686}
1687
Avi Kivity851ba692009-08-24 11:10:17 +03001688static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001689{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001690 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04001691 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001692 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001693 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001694
Rusty Russelle756fc62007-07-30 20:07:08 +10001695 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03001696 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001697 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001698 if (string || in)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03001699 return emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001700
Avi Kivity039576c2007-03-20 12:46:50 +02001701 port = io_info >> 16;
1702 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001703 svm->next_rip = svm->vmcb->control.exit_info_2;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001704 skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001705
1706 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001707}
1708
Avi Kivity851ba692009-08-24 11:10:17 +03001709static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001710{
1711 return 1;
1712}
1713
Avi Kivity851ba692009-08-24 11:10:17 +03001714static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001715{
1716 ++svm->vcpu.stat.irq_exits;
1717 return 1;
1718}
1719
Avi Kivity851ba692009-08-24 11:10:17 +03001720static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001721{
1722 return 1;
1723}
1724
Avi Kivity851ba692009-08-24 11:10:17 +03001725static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001726{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001727 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001728 skip_emulated_instruction(&svm->vcpu);
1729 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001730}
1731
Avi Kivity851ba692009-08-24 11:10:17 +03001732static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001733{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001734 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001735 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001736 kvm_emulate_hypercall(&svm->vcpu);
1737 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001738}
1739
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001740static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
1741{
1742 struct vcpu_svm *svm = to_svm(vcpu);
1743
1744 return svm->nested.nested_cr3;
1745}
1746
1747static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
1748 unsigned long root)
1749{
1750 struct vcpu_svm *svm = to_svm(vcpu);
1751
1752 svm->vmcb->control.nested_cr3 = root;
Joerg Roedelb2747162010-12-03 11:45:53 +01001753 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001754 force_new_asid(vcpu);
1755}
1756
Avi Kivity6389ee92010-11-29 16:12:30 +02001757static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
1758 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001759{
1760 struct vcpu_svm *svm = to_svm(vcpu);
1761
1762 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
1763 svm->vmcb->control.exit_code_hi = 0;
Avi Kivity6389ee92010-11-29 16:12:30 +02001764 svm->vmcb->control.exit_info_1 = fault->error_code;
1765 svm->vmcb->control.exit_info_2 = fault->address;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001766
1767 nested_svm_vmexit(svm);
1768}
1769
Joerg Roedel4b161842010-09-10 17:31:03 +02001770static int nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
1771{
1772 int r;
1773
1774 r = kvm_init_shadow_mmu(vcpu, &vcpu->arch.mmu);
1775
1776 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
1777 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
1778 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
1779 vcpu->arch.mmu.shadow_root_level = get_npt_level();
1780 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
1781
1782 return r;
1783}
1784
1785static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
1786{
1787 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
1788}
1789
Alexander Grafc0725422008-11-25 20:17:03 +01001790static int nested_svm_check_permissions(struct vcpu_svm *svm)
1791{
Avi Kivityf6801df2010-01-21 15:31:50 +02001792 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01001793 || !is_paging(&svm->vcpu)) {
1794 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1795 return 1;
1796 }
1797
1798 if (svm->vmcb->save.cpl) {
1799 kvm_inject_gp(&svm->vcpu, 0);
1800 return 1;
1801 }
1802
1803 return 0;
1804}
1805
Alexander Grafcf74a782008-11-25 20:17:08 +01001806static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
1807 bool has_error_code, u32 error_code)
1808{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001809 int vmexit;
1810
Joerg Roedel20307532010-11-29 17:51:48 +01001811 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02001812 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001813
Joerg Roedel0295ad72009-08-07 11:49:37 +02001814 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1815 svm->vmcb->control.exit_code_hi = 0;
1816 svm->vmcb->control.exit_info_1 = error_code;
1817 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1818
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001819 vmexit = nested_svm_intercept(svm);
1820 if (vmexit == NESTED_EXIT_DONE)
1821 svm->nested.exit_required = true;
1822
1823 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001824}
1825
Joerg Roedel8fe54652010-02-19 16:23:01 +01001826/* This function returns true if it is save to enable the irq window */
1827static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001828{
Joerg Roedel20307532010-11-29 17:51:48 +01001829 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001830 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001831
Joerg Roedel26666952009-08-07 11:49:46 +02001832 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001833 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001834
Joerg Roedel26666952009-08-07 11:49:46 +02001835 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001836 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001837
Gleb Natapova0a07cd2010-09-20 10:15:32 +02001838 /*
1839 * if vmexit was already requested (by intercepted exception
1840 * for instance) do not overwrite it with "external interrupt"
1841 * vmexit.
1842 */
1843 if (svm->nested.exit_required)
1844 return false;
1845
Joerg Roedel197717d2010-02-24 18:59:19 +01001846 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
1847 svm->vmcb->control.exit_info_1 = 0;
1848 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02001849
Joerg Roedelcd3ff652009-10-09 16:08:26 +02001850 if (svm->nested.intercept & 1ULL) {
1851 /*
1852 * The #vmexit can't be emulated here directly because this
1853 * code path runs with irqs and preemtion disabled. A
1854 * #vmexit emulation might sleep. Only signal request for
1855 * the #vmexit here.
1856 */
1857 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02001858 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01001859 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001860 }
1861
Joerg Roedel8fe54652010-02-19 16:23:01 +01001862 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001863}
1864
Joerg Roedel887f5002010-02-24 18:59:12 +01001865/* This function returns true if it is save to enable the nmi window */
1866static inline bool nested_svm_nmi(struct vcpu_svm *svm)
1867{
Joerg Roedel20307532010-11-29 17:51:48 +01001868 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01001869 return true;
1870
1871 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
1872 return true;
1873
1874 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
1875 svm->nested.exit_required = true;
1876
1877 return false;
1878}
1879
Joerg Roedel7597f122010-02-19 16:23:00 +01001880static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001881{
1882 struct page *page;
1883
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01001884 might_sleep();
1885
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001886 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001887 if (is_error_page(page))
1888 goto error;
1889
Joerg Roedel7597f122010-02-19 16:23:00 +01001890 *_page = page;
1891
1892 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001893
1894error:
1895 kvm_release_page_clean(page);
1896 kvm_inject_gp(&svm->vcpu, 0);
1897
1898 return NULL;
1899}
1900
Joerg Roedel7597f122010-02-19 16:23:00 +01001901static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001902{
Joerg Roedel7597f122010-02-19 16:23:00 +01001903 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001904 kvm_release_page_dirty(page);
1905}
1906
Joerg Roedelce2ac082010-03-01 15:34:39 +01001907static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001908{
Joerg Roedelce2ac082010-03-01 15:34:39 +01001909 unsigned port;
1910 u8 val, bit;
1911 u64 gpa;
1912
1913 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
1914 return NESTED_EXIT_HOST;
1915
1916 port = svm->vmcb->control.exit_info_1 >> 16;
1917 gpa = svm->nested.vmcb_iopm + (port / 8);
1918 bit = port % 8;
1919 val = 0;
1920
1921 if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1))
1922 val &= (1 << bit);
1923
1924 return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1925}
1926
Joerg Roedeld2477822010-03-01 15:34:34 +01001927static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001928{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001929 u32 offset, msr, value;
1930 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001931
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001932 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01001933 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001934
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001935 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1936 offset = svm_msrpm_offset(msr);
1937 write = svm->vmcb->control.exit_info_1 & 1;
1938 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001939
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001940 if (offset == MSR_INVALID)
1941 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001942
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001943 /* Offset is in 32 bit units but need in 8 bit units */
1944 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001945
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001946 if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4))
1947 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001948
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001949 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001950}
1951
Joerg Roedel410e4d52009-08-07 11:49:44 +02001952static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001953{
Alexander Grafcf74a782008-11-25 20:17:08 +01001954 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001955
Joerg Roedel410e4d52009-08-07 11:49:44 +02001956 switch (exit_code) {
1957 case SVM_EXIT_INTR:
1958 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02001959 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02001960 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001961 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01001962 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001963 if (npt_enabled)
1964 return NESTED_EXIT_HOST;
1965 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001966 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02001967 /* When we're shadowing, trap PFs, but not async PF */
1968 if (!npt_enabled && svm->apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001969 return NESTED_EXIT_HOST;
1970 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001971 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
1972 nm_interception(svm);
1973 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001974 default:
1975 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001976 }
1977
Joerg Roedel410e4d52009-08-07 11:49:44 +02001978 return NESTED_EXIT_CONTINUE;
1979}
1980
1981/*
1982 * If this function returns true, this #vmexit was already handled
1983 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001984static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001985{
1986 u32 exit_code = svm->vmcb->control.exit_code;
1987 int vmexit = NESTED_EXIT_HOST;
1988
Alexander Grafcf74a782008-11-25 20:17:08 +01001989 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001990 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001991 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001992 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01001993 case SVM_EXIT_IOIO:
1994 vmexit = nested_svm_intercept_ioio(svm);
1995 break;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001996 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
1997 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
1998 if (svm->nested.intercept_cr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001999 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002000 break;
2001 }
Joerg Roedel3aed0412010-11-30 18:03:58 +01002002 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
2003 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
2004 if (svm->nested.intercept_dr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002005 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002006 break;
2007 }
2008 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
2009 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002010 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002011 vmexit = NESTED_EXIT_DONE;
Gleb Natapov631bc482010-10-14 11:22:52 +02002012 /* async page fault always cause vmexit */
2013 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
2014 svm->apf_reason != 0)
2015 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002016 break;
2017 }
Joerg Roedel228070b2010-04-22 12:33:10 +02002018 case SVM_EXIT_ERR: {
2019 vmexit = NESTED_EXIT_DONE;
2020 break;
2021 }
Alexander Grafcf74a782008-11-25 20:17:08 +01002022 default: {
2023 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002024 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002025 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002026 }
2027 }
2028
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002029 return vmexit;
2030}
2031
2032static int nested_svm_exit_handled(struct vcpu_svm *svm)
2033{
2034 int vmexit;
2035
2036 vmexit = nested_svm_intercept(svm);
2037
2038 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002039 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002040
2041 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002042}
2043
Joerg Roedel0460a972009-08-07 11:49:31 +02002044static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
2045{
2046 struct vmcb_control_area *dst = &dst_vmcb->control;
2047 struct vmcb_control_area *from = &from_vmcb->control;
2048
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002049 dst->intercept_cr = from->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002050 dst->intercept_dr = from->intercept_dr;
Joerg Roedel0460a972009-08-07 11:49:31 +02002051 dst->intercept_exceptions = from->intercept_exceptions;
2052 dst->intercept = from->intercept;
2053 dst->iopm_base_pa = from->iopm_base_pa;
2054 dst->msrpm_base_pa = from->msrpm_base_pa;
2055 dst->tsc_offset = from->tsc_offset;
2056 dst->asid = from->asid;
2057 dst->tlb_ctl = from->tlb_ctl;
2058 dst->int_ctl = from->int_ctl;
2059 dst->int_vector = from->int_vector;
2060 dst->int_state = from->int_state;
2061 dst->exit_code = from->exit_code;
2062 dst->exit_code_hi = from->exit_code_hi;
2063 dst->exit_info_1 = from->exit_info_1;
2064 dst->exit_info_2 = from->exit_info_2;
2065 dst->exit_int_info = from->exit_int_info;
2066 dst->exit_int_info_err = from->exit_int_info_err;
2067 dst->nested_ctl = from->nested_ctl;
2068 dst->event_inj = from->event_inj;
2069 dst->event_inj_err = from->event_inj_err;
2070 dst->nested_cr3 = from->nested_cr3;
2071 dst->lbr_ctl = from->lbr_ctl;
2072}
2073
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002074static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002075{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002076 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002077 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02002078 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002079 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01002080
Joerg Roedel17897f32009-10-09 16:08:29 +02002081 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
2082 vmcb->control.exit_info_1,
2083 vmcb->control.exit_info_2,
2084 vmcb->control.exit_int_info,
2085 vmcb->control.exit_int_info_err);
2086
Joerg Roedel7597f122010-02-19 16:23:00 +01002087 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002088 if (!nested_vmcb)
2089 return 1;
2090
Joerg Roedel20307532010-11-29 17:51:48 +01002091 /* Exit Guest-Mode */
2092 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01002093 svm->nested.vmcb = 0;
2094
Alexander Grafcf74a782008-11-25 20:17:08 +01002095 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02002096 disable_gif(svm);
2097
2098 nested_vmcb->save.es = vmcb->save.es;
2099 nested_vmcb->save.cs = vmcb->save.cs;
2100 nested_vmcb->save.ss = vmcb->save.ss;
2101 nested_vmcb->save.ds = vmcb->save.ds;
2102 nested_vmcb->save.gdtr = vmcb->save.gdtr;
2103 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02002104 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002105 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedel2be4fc72010-04-22 12:33:09 +02002106 nested_vmcb->save.cr3 = svm->vcpu.arch.cr3;
Joerg Roedel33740e42009-08-07 11:49:29 +02002107 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002108 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Joerg Roedel33740e42009-08-07 11:49:29 +02002109 nested_vmcb->save.rflags = vmcb->save.rflags;
2110 nested_vmcb->save.rip = vmcb->save.rip;
2111 nested_vmcb->save.rsp = vmcb->save.rsp;
2112 nested_vmcb->save.rax = vmcb->save.rax;
2113 nested_vmcb->save.dr7 = vmcb->save.dr7;
2114 nested_vmcb->save.dr6 = vmcb->save.dr6;
2115 nested_vmcb->save.cpl = vmcb->save.cpl;
2116
2117 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
2118 nested_vmcb->control.int_vector = vmcb->control.int_vector;
2119 nested_vmcb->control.int_state = vmcb->control.int_state;
2120 nested_vmcb->control.exit_code = vmcb->control.exit_code;
2121 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
2122 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
2123 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
2124 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
2125 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel7a190662010-07-27 18:14:21 +02002126 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02002127
2128 /*
2129 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
2130 * to make sure that we do not lose injected events. So check event_inj
2131 * here and copy it to exit_int_info if it is valid.
2132 * Exit_int_info and event_inj can't be both valid because the case
2133 * below only happens on a VMRUN instruction intercept which has
2134 * no valid exit_int_info set.
2135 */
2136 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
2137 struct vmcb_control_area *nc = &nested_vmcb->control;
2138
2139 nc->exit_int_info = vmcb->control.event_inj;
2140 nc->exit_int_info_err = vmcb->control.event_inj_err;
2141 }
2142
Joerg Roedel33740e42009-08-07 11:49:29 +02002143 nested_vmcb->control.tlb_ctl = 0;
2144 nested_vmcb->control.event_inj = 0;
2145 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002146
2147 /* We always set V_INTR_MASKING and remember the old value in hflags */
2148 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
2149 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
2150
Alexander Grafcf74a782008-11-25 20:17:08 +01002151 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02002152 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01002153
Alexander Graf219b65d2009-06-15 15:21:25 +02002154 kvm_clear_exception_queue(&svm->vcpu);
2155 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002156
Joerg Roedel4b161842010-09-10 17:31:03 +02002157 svm->nested.nested_cr3 = 0;
2158
Alexander Grafcf74a782008-11-25 20:17:08 +01002159 /* Restore selected save entries */
2160 svm->vmcb->save.es = hsave->save.es;
2161 svm->vmcb->save.cs = hsave->save.cs;
2162 svm->vmcb->save.ss = hsave->save.ss;
2163 svm->vmcb->save.ds = hsave->save.ds;
2164 svm->vmcb->save.gdtr = hsave->save.gdtr;
2165 svm->vmcb->save.idtr = hsave->save.idtr;
2166 svm->vmcb->save.rflags = hsave->save.rflags;
2167 svm_set_efer(&svm->vcpu, hsave->save.efer);
2168 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
2169 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
2170 if (npt_enabled) {
2171 svm->vmcb->save.cr3 = hsave->save.cr3;
2172 svm->vcpu.arch.cr3 = hsave->save.cr3;
2173 } else {
Avi Kivity23902182010-06-10 17:02:16 +03002174 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01002175 }
2176 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
2177 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
2178 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
2179 svm->vmcb->save.dr7 = 0;
2180 svm->vmcb->save.cpl = 0;
2181 svm->vmcb->control.exit_int_info = 0;
2182
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002183 mark_all_dirty(svm->vmcb);
2184
Joerg Roedel7597f122010-02-19 16:23:00 +01002185 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01002186
Joerg Roedel4b161842010-09-10 17:31:03 +02002187 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002188 kvm_mmu_reset_context(&svm->vcpu);
2189 kvm_mmu_load(&svm->vcpu);
2190
2191 return 0;
2192}
Alexander Graf3d6368e2008-11-25 20:17:07 +01002193
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002194static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002195{
Joerg Roedel323c3d82010-03-01 15:34:37 +01002196 /*
2197 * This function merges the msr permission bitmaps of kvm and the
2198 * nested vmcb. It is omptimized in that it only merges the parts where
2199 * the kvm msr permission bitmap may contain zero bits
2200 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01002201 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002202
Joerg Roedel323c3d82010-03-01 15:34:37 +01002203 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
2204 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002205
Joerg Roedel323c3d82010-03-01 15:34:37 +01002206 for (i = 0; i < MSRPM_OFFSETS; i++) {
2207 u32 value, p;
2208 u64 offset;
2209
2210 if (msrpm_offsets[i] == 0xffffffff)
2211 break;
2212
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002213 p = msrpm_offsets[i];
2214 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01002215
2216 if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4))
2217 return false;
2218
2219 svm->nested.msrpm[p] = svm->msrpm[p] | value;
2220 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002221
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002222 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002223
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002224 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002225}
2226
Joerg Roedel52c65a302010-08-02 16:46:44 +02002227static bool nested_vmcb_checks(struct vmcb *vmcb)
2228{
2229 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
2230 return false;
2231
Joerg Roedeldbe77582010-08-02 16:46:45 +02002232 if (vmcb->control.asid == 0)
2233 return false;
2234
Joerg Roedel4b161842010-09-10 17:31:03 +02002235 if (vmcb->control.nested_ctl && !npt_enabled)
2236 return false;
2237
Joerg Roedel52c65a302010-08-02 16:46:44 +02002238 return true;
2239}
2240
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002241static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002242{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002243 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002244 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02002245 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002246 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01002247 u64 vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002248
Joerg Roedel06fc77722010-02-19 16:23:07 +01002249 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002250
Joerg Roedel7597f122010-02-19 16:23:00 +01002251 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002252 if (!nested_vmcb)
2253 return false;
2254
Joerg Roedel52c65a302010-08-02 16:46:44 +02002255 if (!nested_vmcb_checks(nested_vmcb)) {
2256 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
2257 nested_vmcb->control.exit_code_hi = 0;
2258 nested_vmcb->control.exit_info_1 = 0;
2259 nested_vmcb->control.exit_info_2 = 0;
2260
2261 nested_svm_unmap(page);
2262
2263 return false;
2264 }
2265
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002266 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02002267 nested_vmcb->save.rip,
2268 nested_vmcb->control.int_ctl,
2269 nested_vmcb->control.event_inj,
2270 nested_vmcb->control.nested_ctl);
2271
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002272 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
2273 nested_vmcb->control.intercept_cr >> 16,
Joerg Roedel2e554e82010-02-24 18:59:14 +01002274 nested_vmcb->control.intercept_exceptions,
2275 nested_vmcb->control.intercept);
2276
Alexander Graf3d6368e2008-11-25 20:17:07 +01002277 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02002278 kvm_clear_exception_queue(&svm->vcpu);
2279 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002280
Joerg Roedele0231712010-02-24 18:59:10 +01002281 /*
2282 * Save the old vmcb, so we don't need to pick what we save, but can
2283 * restore everything when a VMEXIT occurs
2284 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02002285 hsave->save.es = vmcb->save.es;
2286 hsave->save.cs = vmcb->save.cs;
2287 hsave->save.ss = vmcb->save.ss;
2288 hsave->save.ds = vmcb->save.ds;
2289 hsave->save.gdtr = vmcb->save.gdtr;
2290 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02002291 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002292 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002293 hsave->save.cr4 = svm->vcpu.arch.cr4;
2294 hsave->save.rflags = vmcb->save.rflags;
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002295 hsave->save.rip = kvm_rip_read(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002296 hsave->save.rsp = vmcb->save.rsp;
2297 hsave->save.rax = vmcb->save.rax;
2298 if (npt_enabled)
2299 hsave->save.cr3 = vmcb->save.cr3;
2300 else
2301 hsave->save.cr3 = svm->vcpu.arch.cr3;
2302
Joerg Roedel0460a972009-08-07 11:49:31 +02002303 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002304
2305 if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
2306 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2307 else
2308 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2309
Joerg Roedel4b161842010-09-10 17:31:03 +02002310 if (nested_vmcb->control.nested_ctl) {
2311 kvm_mmu_unload(&svm->vcpu);
2312 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
2313 nested_svm_init_mmu_context(&svm->vcpu);
2314 }
2315
Alexander Graf3d6368e2008-11-25 20:17:07 +01002316 /* Load the nested guest state */
2317 svm->vmcb->save.es = nested_vmcb->save.es;
2318 svm->vmcb->save.cs = nested_vmcb->save.cs;
2319 svm->vmcb->save.ss = nested_vmcb->save.ss;
2320 svm->vmcb->save.ds = nested_vmcb->save.ds;
2321 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2322 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
2323 svm->vmcb->save.rflags = nested_vmcb->save.rflags;
2324 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2325 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2326 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2327 if (npt_enabled) {
2328 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2329 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002330 } else
Avi Kivity23902182010-06-10 17:02:16 +03002331 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002332
2333 /* Guest paging mode is active - reset mmu */
2334 kvm_mmu_reset_context(&svm->vcpu);
2335
Joerg Roedeldefbba52009-08-07 11:49:30 +02002336 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002337 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2338 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2339 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01002340
Alexander Graf3d6368e2008-11-25 20:17:07 +01002341 /* In case we don't even reach vcpu_run, the fields are not updated */
2342 svm->vmcb->save.rax = nested_vmcb->save.rax;
2343 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2344 svm->vmcb->save.rip = nested_vmcb->save.rip;
2345 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2346 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2347 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2348
Joerg Roedelf7138532010-03-01 15:34:40 +01002349 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002350 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002351
Joerg Roedelaad42c62009-08-07 11:49:34 +02002352 /* cache intercepts */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002353 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002354 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +02002355 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2356 svm->nested.intercept = nested_vmcb->control.intercept;
2357
Alexander Graf3d6368e2008-11-25 20:17:07 +01002358 force_new_asid(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002359 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002360 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2361 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2362 else
2363 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2364
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002365 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2366 /* We only want the cr8 intercept bits of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002367 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
2368 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002369 }
2370
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002371 /* We don't want to see VMMCALLs from a nested guest */
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002372 clr_intercept(svm, INTERCEPT_VMMCALL);
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002373
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002374 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002375 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2376 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2377 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002378 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2379 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2380
Joerg Roedel7597f122010-02-19 16:23:00 +01002381 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002382
Joerg Roedel20307532010-11-29 17:51:48 +01002383 /* Enter Guest-Mode */
2384 enter_guest_mode(&svm->vcpu);
2385
Joerg Roedel384c6362010-11-30 18:03:56 +01002386 /*
2387 * Merge guest and host intercepts - must be called with vcpu in
2388 * guest-mode to take affect here
2389 */
2390 recalc_intercepts(svm);
2391
Joerg Roedel06fc77722010-02-19 16:23:07 +01002392 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002393
Joerg Roedel2af91942009-08-07 11:49:28 +02002394 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002395
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002396 mark_all_dirty(svm->vmcb);
2397
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002398 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002399}
2400
Joerg Roedel9966bf62009-08-07 11:49:40 +02002401static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002402{
2403 to_vmcb->save.fs = from_vmcb->save.fs;
2404 to_vmcb->save.gs = from_vmcb->save.gs;
2405 to_vmcb->save.tr = from_vmcb->save.tr;
2406 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2407 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2408 to_vmcb->save.star = from_vmcb->save.star;
2409 to_vmcb->save.lstar = from_vmcb->save.lstar;
2410 to_vmcb->save.cstar = from_vmcb->save.cstar;
2411 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2412 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2413 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2414 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002415}
2416
Avi Kivity851ba692009-08-24 11:10:17 +03002417static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002418{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002419 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002420 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002421
Alexander Graf55426752008-11-25 20:17:06 +01002422 if (nested_svm_check_permissions(svm))
2423 return 1;
2424
2425 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2426 skip_emulated_instruction(&svm->vcpu);
2427
Joerg Roedel7597f122010-02-19 16:23:00 +01002428 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002429 if (!nested_vmcb)
2430 return 1;
2431
2432 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002433 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002434
2435 return 1;
2436}
2437
Avi Kivity851ba692009-08-24 11:10:17 +03002438static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002439{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002440 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002441 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002442
Alexander Graf55426752008-11-25 20:17:06 +01002443 if (nested_svm_check_permissions(svm))
2444 return 1;
2445
2446 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2447 skip_emulated_instruction(&svm->vcpu);
2448
Joerg Roedel7597f122010-02-19 16:23:00 +01002449 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002450 if (!nested_vmcb)
2451 return 1;
2452
2453 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002454 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002455
2456 return 1;
2457}
2458
Avi Kivity851ba692009-08-24 11:10:17 +03002459static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002460{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002461 if (nested_svm_check_permissions(svm))
2462 return 1;
2463
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002464 /* Save rip after vmrun instruction */
2465 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002466
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002467 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002468 return 1;
2469
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002470 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002471 goto failed;
2472
2473 return 1;
2474
2475failed:
2476
2477 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2478 svm->vmcb->control.exit_code_hi = 0;
2479 svm->vmcb->control.exit_info_1 = 0;
2480 svm->vmcb->control.exit_info_2 = 0;
2481
2482 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002483
2484 return 1;
2485}
2486
Avi Kivity851ba692009-08-24 11:10:17 +03002487static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002488{
2489 if (nested_svm_check_permissions(svm))
2490 return 1;
2491
2492 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2493 skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03002494 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01002495
Joerg Roedel2af91942009-08-07 11:49:28 +02002496 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002497
2498 return 1;
2499}
2500
Avi Kivity851ba692009-08-24 11:10:17 +03002501static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002502{
2503 if (nested_svm_check_permissions(svm))
2504 return 1;
2505
2506 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2507 skip_emulated_instruction(&svm->vcpu);
2508
Joerg Roedel2af91942009-08-07 11:49:28 +02002509 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002510
2511 /* After a CLGI no interrupts should come */
2512 svm_clear_vintr(svm);
2513 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2514
Joerg Roedeldecdbf62010-12-03 11:45:52 +01002515 mark_dirty(svm->vmcb, VMCB_INTR);
2516
Alexander Graf1371d902008-11-25 20:17:04 +01002517 return 1;
2518}
2519
Avi Kivity851ba692009-08-24 11:10:17 +03002520static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002521{
2522 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002523
Joerg Roedelec1ff792009-10-09 16:08:31 +02002524 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
2525 vcpu->arch.regs[VCPU_REGS_RAX]);
2526
Alexander Grafff092382009-06-15 15:21:24 +02002527 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2528 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
2529
2530 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2531 skip_emulated_instruction(&svm->vcpu);
2532 return 1;
2533}
2534
Joerg Roedel532a46b2009-10-09 16:08:32 +02002535static int skinit_interception(struct vcpu_svm *svm)
2536{
2537 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
2538
2539 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2540 return 1;
2541}
2542
Avi Kivity851ba692009-08-24 11:10:17 +03002543static int invalid_op_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002544{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002545 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002546 return 1;
2547}
2548
Avi Kivity851ba692009-08-24 11:10:17 +03002549static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002550{
Izik Eidus37817f22008-03-24 23:14:53 +02002551 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002552 int reason;
2553 int int_type = svm->vmcb->control.exit_int_info &
2554 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002555 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002556 uint32_t type =
2557 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2558 uint32_t idt_v =
2559 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002560 bool has_error_code = false;
2561 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002562
2563 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002564
Izik Eidus37817f22008-03-24 23:14:53 +02002565 if (svm->vmcb->control.exit_info_2 &
2566 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002567 reason = TASK_SWITCH_IRET;
2568 else if (svm->vmcb->control.exit_info_2 &
2569 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2570 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002571 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002572 reason = TASK_SWITCH_GATE;
2573 else
2574 reason = TASK_SWITCH_CALL;
2575
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002576 if (reason == TASK_SWITCH_GATE) {
2577 switch (type) {
2578 case SVM_EXITINTINFO_TYPE_NMI:
2579 svm->vcpu.arch.nmi_injected = false;
2580 break;
2581 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002582 if (svm->vmcb->control.exit_info_2 &
2583 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2584 has_error_code = true;
2585 error_code =
2586 (u32)svm->vmcb->control.exit_info_2;
2587 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002588 kvm_clear_exception_queue(&svm->vcpu);
2589 break;
2590 case SVM_EXITINTINFO_TYPE_INTR:
2591 kvm_clear_interrupt_queue(&svm->vcpu);
2592 break;
2593 default:
2594 break;
2595 }
2596 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002597
Gleb Natapov8317c292009-04-12 13:37:02 +03002598 if (reason != TASK_SWITCH_GATE ||
2599 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2600 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002601 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2602 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002603
Gleb Natapovacb54512010-04-15 21:03:50 +03002604 if (kvm_task_switch(&svm->vcpu, tss_selector, reason,
2605 has_error_code, error_code) == EMULATE_FAIL) {
2606 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2607 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2608 svm->vcpu.run->internal.ndata = 0;
2609 return 0;
2610 }
2611 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002612}
2613
Avi Kivity851ba692009-08-24 11:10:17 +03002614static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002615{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002616 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002617 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002618 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002619}
2620
Avi Kivity851ba692009-08-24 11:10:17 +03002621static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002622{
2623 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002624 clr_intercept(svm, INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002625 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002626 return 1;
2627}
2628
Avi Kivity851ba692009-08-24 11:10:17 +03002629static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002630{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002631 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002632}
2633
Avi Kivity851ba692009-08-24 11:10:17 +03002634static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002635{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002636 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002637}
2638
Joerg Roedelcda00082010-09-02 17:29:46 +02002639static int cr0_write_interception(struct vcpu_svm *svm)
2640{
2641 struct kvm_vcpu *vcpu = &svm->vcpu;
2642 int r;
2643
2644 r = emulate_instruction(&svm->vcpu, 0, 0, 0);
2645
2646 if (svm->nested.vmexit_rip) {
2647 kvm_register_write(vcpu, VCPU_REGS_RIP, svm->nested.vmexit_rip);
2648 kvm_register_write(vcpu, VCPU_REGS_RSP, svm->nested.vmexit_rsp);
2649 kvm_register_write(vcpu, VCPU_REGS_RAX, svm->nested.vmexit_rax);
2650 svm->nested.vmexit_rip = 0;
2651 }
2652
2653 return r == EMULATE_DONE;
2654}
2655
Avi Kivity851ba692009-08-24 11:10:17 +03002656static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01002657{
Avi Kivity851ba692009-08-24 11:10:17 +03002658 struct kvm_run *kvm_run = svm->vcpu.run;
2659
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002660 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
2661 /* instruction emulation calls kvm_set_cr8() */
Avi Kivity851ba692009-08-24 11:10:17 +03002662 emulate_instruction(&svm->vcpu, 0, 0, 0);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002663 if (irqchip_in_kernel(svm->vcpu.kvm)) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002664 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel1d075432007-12-06 21:02:25 +01002665 return 1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002666 }
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002667 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
2668 return 1;
Joerg Roedel1d075432007-12-06 21:02:25 +01002669 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2670 return 0;
2671}
2672
Avi Kivity6aa8b732006-12-10 02:21:36 -08002673static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
2674{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002675 struct vcpu_svm *svm = to_svm(vcpu);
2676
Avi Kivity6aa8b732006-12-10 02:21:36 -08002677 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302678 case MSR_IA32_TSC: {
Joerg Roedel4cc70312010-11-30 18:04:01 +01002679 struct vmcb *vmcb = get_host_vmcb(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002680
Joerg Roedel4cc70312010-11-30 18:04:01 +01002681 *data = vmcb->control.tsc_offset + native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002682 break;
2683 }
Brian Gerst8c065852010-07-17 09:03:26 -04002684 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002685 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002686 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08002687#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002688 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002689 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002690 break;
2691 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002692 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002693 break;
2694 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002695 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002696 break;
2697 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002698 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002699 break;
2700#endif
2701 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002702 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002703 break;
2704 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002705 *data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002706 break;
2707 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002708 *data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002709 break;
Joerg Roedele0231712010-02-24 18:59:10 +01002710 /*
2711 * Nobody will change the following 5 values in the VMCB so we can
2712 * safely return them on rdmsr. They will always be 0 until LBRV is
2713 * implemented.
2714 */
Joerg Roedela2938c82008-02-13 16:30:28 +01002715 case MSR_IA32_DEBUGCTLMSR:
2716 *data = svm->vmcb->save.dbgctl;
2717 break;
2718 case MSR_IA32_LASTBRANCHFROMIP:
2719 *data = svm->vmcb->save.br_from;
2720 break;
2721 case MSR_IA32_LASTBRANCHTOIP:
2722 *data = svm->vmcb->save.br_to;
2723 break;
2724 case MSR_IA32_LASTINTFROMIP:
2725 *data = svm->vmcb->save.last_excp_from;
2726 break;
2727 case MSR_IA32_LASTINTTOIP:
2728 *data = svm->vmcb->save.last_excp_to;
2729 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002730 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002731 *data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002732 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002733 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002734 *data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002735 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01002736 case MSR_IA32_UCODE_REV:
2737 *data = 0x01000065;
2738 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002739 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002740 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002741 }
2742 return 0;
2743}
2744
Avi Kivity851ba692009-08-24 11:10:17 +03002745static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002746{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002747 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002748 u64 data;
2749
Avi Kivity59200272010-01-25 19:47:02 +02002750 if (svm_get_msr(&svm->vcpu, ecx, &data)) {
2751 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002752 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002753 } else {
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002754 trace_kvm_msr_read(ecx, data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002755
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002756 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002757 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002758 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002759 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002760 }
2761 return 1;
2762}
2763
Joerg Roedel4a810182010-02-24 18:59:15 +01002764static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2765{
2766 struct vcpu_svm *svm = to_svm(vcpu);
2767 int svm_dis, chg_mask;
2768
2769 if (data & ~SVM_VM_CR_VALID_MASK)
2770 return 1;
2771
2772 chg_mask = SVM_VM_CR_VALID_MASK;
2773
2774 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2775 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2776
2777 svm->nested.vm_cr_msr &= ~chg_mask;
2778 svm->nested.vm_cr_msr |= (data & chg_mask);
2779
2780 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2781
2782 /* check for svm_disable while efer.svme is set */
2783 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2784 return 1;
2785
2786 return 0;
2787}
2788
Avi Kivity6aa8b732006-12-10 02:21:36 -08002789static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
2790{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002791 struct vcpu_svm *svm = to_svm(vcpu);
2792
Avi Kivity6aa8b732006-12-10 02:21:36 -08002793 switch (ecx) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10002794 case MSR_IA32_TSC:
Zachary Amsden99e3e302010-08-19 22:07:17 -10002795 kvm_write_tsc(vcpu, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002796 break;
Brian Gerst8c065852010-07-17 09:03:26 -04002797 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002798 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002799 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08002800#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002801 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002802 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002803 break;
2804 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002805 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002806 break;
2807 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002808 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002809 break;
2810 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002811 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002812 break;
2813#endif
2814 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002815 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002816 break;
2817 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002818 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002819 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002820 break;
2821 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002822 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002823 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002824 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002825 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02002826 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002827 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002828 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002829 break;
2830 }
2831 if (data & DEBUGCTL_RESERVED_BITS)
2832 return 1;
2833
2834 svm->vmcb->save.dbgctl = data;
2835 if (data & (1ULL<<0))
2836 svm_enable_lbrv(svm);
2837 else
2838 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01002839 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002840 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002841 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002842 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002843 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002844 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002845 case MSR_VM_IGNNE:
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002846 pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
2847 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002848 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002849 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002850 }
2851 return 0;
2852}
2853
Avi Kivity851ba692009-08-24 11:10:17 +03002854static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002855{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002856 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002857 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002858 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002859
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002860
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002861 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Avi Kivity59200272010-01-25 19:47:02 +02002862 if (svm_set_msr(&svm->vcpu, ecx, data)) {
2863 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002864 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002865 } else {
2866 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10002867 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02002868 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002869 return 1;
2870}
2871
Avi Kivity851ba692009-08-24 11:10:17 +03002872static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002873{
Rusty Russelle756fc62007-07-30 20:07:08 +10002874 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03002875 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002876 else
Avi Kivity851ba692009-08-24 11:10:17 +03002877 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002878}
2879
Avi Kivity851ba692009-08-24 11:10:17 +03002880static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08002881{
Avi Kivity851ba692009-08-24 11:10:17 +03002882 struct kvm_run *kvm_run = svm->vcpu.run;
2883
Avi Kivity3842d132010-07-27 12:30:24 +03002884 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01002885 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03002886 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Joerg Roedeldecdbf62010-12-03 11:45:52 +01002887 mark_dirty(svm->vmcb, VMCB_INTR);
Dor Laorc1150d82007-01-05 16:36:24 -08002888 /*
2889 * If the user space waits to inject interrupts, exit as soon as
2890 * possible
2891 */
Gleb Natapov80618232009-04-21 17:44:56 +03002892 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
2893 kvm_run->request_interrupt_window &&
2894 !kvm_cpu_has_interrupt(&svm->vcpu)) {
Rusty Russelle756fc62007-07-30 20:07:08 +10002895 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002896 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
2897 return 0;
2898 }
2899
2900 return 1;
2901}
2902
Mark Langsdorf565d0992009-10-06 14:25:02 -05002903static int pause_interception(struct vcpu_svm *svm)
2904{
2905 kvm_vcpu_on_spin(&(svm->vcpu));
2906 return 1;
2907}
2908
Avi Kivity851ba692009-08-24 11:10:17 +03002909static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
Joerg Roedele0231712010-02-24 18:59:10 +01002910 [SVM_EXIT_READ_CR0] = emulate_on_interception,
2911 [SVM_EXIT_READ_CR3] = emulate_on_interception,
2912 [SVM_EXIT_READ_CR4] = emulate_on_interception,
2913 [SVM_EXIT_READ_CR8] = emulate_on_interception,
Avi Kivityd2251572010-01-06 10:55:27 +02002914 [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
Joerg Roedelcda00082010-09-02 17:29:46 +02002915 [SVM_EXIT_WRITE_CR0] = cr0_write_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002916 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
2917 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
2918 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
2919 [SVM_EXIT_READ_DR0] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002920 [SVM_EXIT_READ_DR1] = emulate_on_interception,
2921 [SVM_EXIT_READ_DR2] = emulate_on_interception,
2922 [SVM_EXIT_READ_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002923 [SVM_EXIT_READ_DR4] = emulate_on_interception,
2924 [SVM_EXIT_READ_DR5] = emulate_on_interception,
2925 [SVM_EXIT_READ_DR6] = emulate_on_interception,
2926 [SVM_EXIT_READ_DR7] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002927 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
2928 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
2929 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
2930 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002931 [SVM_EXIT_WRITE_DR4] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002932 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002933 [SVM_EXIT_WRITE_DR6] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002934 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002935 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
2936 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002937 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002938 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
2939 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
2940 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
2941 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02002942 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002943 [SVM_EXIT_SMI] = nop_on_interception,
2944 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08002945 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002946 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002947 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002948 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05002949 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002950 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03002951 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02002952 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002953 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002954 [SVM_EXIT_MSR] = msr_interception,
2955 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002956 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01002957 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02002958 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01002959 [SVM_EXIT_VMLOAD] = vmload_interception,
2960 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01002961 [SVM_EXIT_STGI] = stgi_interception,
2962 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02002963 [SVM_EXIT_SKINIT] = skinit_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002964 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01002965 [SVM_EXIT_MONITOR] = invalid_op_interception,
2966 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002967 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002968};
2969
Joerg Roedel3f10c842010-05-05 16:04:42 +02002970void dump_vmcb(struct kvm_vcpu *vcpu)
2971{
2972 struct vcpu_svm *svm = to_svm(vcpu);
2973 struct vmcb_control_area *control = &svm->vmcb->control;
2974 struct vmcb_save_area *save = &svm->vmcb->save;
2975
2976 pr_err("VMCB Control Area:\n");
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002977 pr_err("cr_read: %04x\n", control->intercept_cr & 0xffff);
2978 pr_err("cr_write: %04x\n", control->intercept_cr >> 16);
Joerg Roedel3aed0412010-11-30 18:03:58 +01002979 pr_err("dr_read: %04x\n", control->intercept_dr & 0xffff);
2980 pr_err("dr_write: %04x\n", control->intercept_dr >> 16);
Joerg Roedel3f10c842010-05-05 16:04:42 +02002981 pr_err("exceptions: %08x\n", control->intercept_exceptions);
2982 pr_err("intercepts: %016llx\n", control->intercept);
2983 pr_err("pause filter count: %d\n", control->pause_filter_count);
2984 pr_err("iopm_base_pa: %016llx\n", control->iopm_base_pa);
2985 pr_err("msrpm_base_pa: %016llx\n", control->msrpm_base_pa);
2986 pr_err("tsc_offset: %016llx\n", control->tsc_offset);
2987 pr_err("asid: %d\n", control->asid);
2988 pr_err("tlb_ctl: %d\n", control->tlb_ctl);
2989 pr_err("int_ctl: %08x\n", control->int_ctl);
2990 pr_err("int_vector: %08x\n", control->int_vector);
2991 pr_err("int_state: %08x\n", control->int_state);
2992 pr_err("exit_code: %08x\n", control->exit_code);
2993 pr_err("exit_info1: %016llx\n", control->exit_info_1);
2994 pr_err("exit_info2: %016llx\n", control->exit_info_2);
2995 pr_err("exit_int_info: %08x\n", control->exit_int_info);
2996 pr_err("exit_int_info_err: %08x\n", control->exit_int_info_err);
2997 pr_err("nested_ctl: %lld\n", control->nested_ctl);
2998 pr_err("nested_cr3: %016llx\n", control->nested_cr3);
2999 pr_err("event_inj: %08x\n", control->event_inj);
3000 pr_err("event_inj_err: %08x\n", control->event_inj_err);
3001 pr_err("lbr_ctl: %lld\n", control->lbr_ctl);
3002 pr_err("next_rip: %016llx\n", control->next_rip);
3003 pr_err("VMCB State Save Area:\n");
3004 pr_err("es: s: %04x a: %04x l: %08x b: %016llx\n",
3005 save->es.selector, save->es.attrib,
3006 save->es.limit, save->es.base);
3007 pr_err("cs: s: %04x a: %04x l: %08x b: %016llx\n",
3008 save->cs.selector, save->cs.attrib,
3009 save->cs.limit, save->cs.base);
3010 pr_err("ss: s: %04x a: %04x l: %08x b: %016llx\n",
3011 save->ss.selector, save->ss.attrib,
3012 save->ss.limit, save->ss.base);
3013 pr_err("ds: s: %04x a: %04x l: %08x b: %016llx\n",
3014 save->ds.selector, save->ds.attrib,
3015 save->ds.limit, save->ds.base);
3016 pr_err("fs: s: %04x a: %04x l: %08x b: %016llx\n",
3017 save->fs.selector, save->fs.attrib,
3018 save->fs.limit, save->fs.base);
3019 pr_err("gs: s: %04x a: %04x l: %08x b: %016llx\n",
3020 save->gs.selector, save->gs.attrib,
3021 save->gs.limit, save->gs.base);
3022 pr_err("gdtr: s: %04x a: %04x l: %08x b: %016llx\n",
3023 save->gdtr.selector, save->gdtr.attrib,
3024 save->gdtr.limit, save->gdtr.base);
3025 pr_err("ldtr: s: %04x a: %04x l: %08x b: %016llx\n",
3026 save->ldtr.selector, save->ldtr.attrib,
3027 save->ldtr.limit, save->ldtr.base);
3028 pr_err("idtr: s: %04x a: %04x l: %08x b: %016llx\n",
3029 save->idtr.selector, save->idtr.attrib,
3030 save->idtr.limit, save->idtr.base);
3031 pr_err("tr: s: %04x a: %04x l: %08x b: %016llx\n",
3032 save->tr.selector, save->tr.attrib,
3033 save->tr.limit, save->tr.base);
3034 pr_err("cpl: %d efer: %016llx\n",
3035 save->cpl, save->efer);
3036 pr_err("cr0: %016llx cr2: %016llx\n",
3037 save->cr0, save->cr2);
3038 pr_err("cr3: %016llx cr4: %016llx\n",
3039 save->cr3, save->cr4);
3040 pr_err("dr6: %016llx dr7: %016llx\n",
3041 save->dr6, save->dr7);
3042 pr_err("rip: %016llx rflags: %016llx\n",
3043 save->rip, save->rflags);
3044 pr_err("rsp: %016llx rax: %016llx\n",
3045 save->rsp, save->rax);
3046 pr_err("star: %016llx lstar: %016llx\n",
3047 save->star, save->lstar);
3048 pr_err("cstar: %016llx sfmask: %016llx\n",
3049 save->cstar, save->sfmask);
3050 pr_err("kernel_gs_base: %016llx sysenter_cs: %016llx\n",
3051 save->kernel_gs_base, save->sysenter_cs);
3052 pr_err("sysenter_esp: %016llx sysenter_eip: %016llx\n",
3053 save->sysenter_esp, save->sysenter_eip);
3054 pr_err("gpat: %016llx dbgctl: %016llx\n",
3055 save->g_pat, save->dbgctl);
3056 pr_err("br_from: %016llx br_to: %016llx\n",
3057 save->br_from, save->br_to);
3058 pr_err("excp_from: %016llx excp_to: %016llx\n",
3059 save->last_excp_from, save->last_excp_to);
3060
3061}
3062
Avi Kivity586f9602010-11-18 13:09:54 +02003063static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
3064{
3065 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
3066
3067 *info1 = control->exit_info_1;
3068 *info2 = control->exit_info_2;
3069}
3070
Avi Kivity851ba692009-08-24 11:10:17 +03003071static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003072{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003073 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03003074 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003075 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003076
Avi Kivityaa179112010-11-17 18:44:19 +02003077 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003078
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003079 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02003080 vcpu->arch.cr0 = svm->vmcb->save.cr0;
3081 if (npt_enabled)
3082 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003083
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003084 if (unlikely(svm->nested.exit_required)) {
3085 nested_svm_vmexit(svm);
3086 svm->nested.exit_required = false;
3087
3088 return 1;
3089 }
3090
Joerg Roedel20307532010-11-29 17:51:48 +01003091 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02003092 int vmexit;
3093
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003094 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
3095 svm->vmcb->control.exit_info_1,
3096 svm->vmcb->control.exit_info_2,
3097 svm->vmcb->control.exit_int_info,
3098 svm->vmcb->control.exit_int_info_err);
3099
Joerg Roedel410e4d52009-08-07 11:49:44 +02003100 vmexit = nested_svm_exit_special(svm);
3101
3102 if (vmexit == NESTED_EXIT_CONTINUE)
3103 vmexit = nested_svm_exit_handled(svm);
3104
3105 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01003106 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01003107 }
3108
Joerg Roedela5c38322009-08-07 11:49:32 +02003109 svm_complete_interrupts(svm);
3110
Avi Kivity04d2cc72007-09-10 18:10:54 +03003111 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3112 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3113 kvm_run->fail_entry.hardware_entry_failure_reason
3114 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02003115 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
3116 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03003117 return 0;
3118 }
3119
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003120 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003121 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02003122 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3123 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003124 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
3125 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003126 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003127 exit_code);
3128
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02003129 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08003130 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003131 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03003132 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003133 return 0;
3134 }
3135
Avi Kivity851ba692009-08-24 11:10:17 +03003136 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003137}
3138
3139static void reload_tss(struct kvm_vcpu *vcpu)
3140{
3141 int cpu = raw_smp_processor_id();
3142
Tejun Heo0fe1e002009-10-29 22:34:14 +09003143 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
3144 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003145 load_TR_desc();
3146}
3147
Rusty Russelle756fc62007-07-30 20:07:08 +10003148static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003149{
3150 int cpu = raw_smp_processor_id();
3151
Tejun Heo0fe1e002009-10-29 22:34:14 +09003152 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003153
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003154 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03003155 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09003156 if (svm->asid_generation != sd->asid_generation)
3157 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003158}
3159
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003160static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3161{
3162 struct vcpu_svm *svm = to_svm(vcpu);
3163
3164 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3165 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003166 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003167 ++vcpu->stat.nmi_injections;
3168}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003169
Eddie Dong85f455f2007-07-06 12:20:49 +03003170static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003171{
3172 struct vmcb_control_area *control;
3173
Rusty Russelle756fc62007-07-30 20:07:08 +10003174 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03003175 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003176 control->int_ctl &= ~V_INTR_PRIO_MASK;
3177 control->int_ctl |= V_IRQ_MASK |
3178 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003179 mark_dirty(svm->vmcb, VMCB_INTR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003180}
3181
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003182static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03003183{
3184 struct vcpu_svm *svm = to_svm(vcpu);
3185
Joerg Roedel2af91942009-08-07 11:49:28 +02003186 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01003187
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03003188 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3189 ++vcpu->stat.irq_injections;
3190
Alexander Graf219b65d2009-06-15 15:21:25 +02003191 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3192 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03003193}
3194
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003195static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
3196{
3197 struct vcpu_svm *svm = to_svm(vcpu);
3198
Joerg Roedel20307532010-11-29 17:51:48 +01003199 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003200 return;
3201
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003202 if (irr == -1)
3203 return;
3204
3205 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003206 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003207}
3208
3209static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003210{
3211 struct vcpu_svm *svm = to_svm(vcpu);
3212 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02003213 int ret;
3214 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
3215 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
3216 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
3217
3218 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003219}
3220
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003221static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3222{
3223 struct vcpu_svm *svm = to_svm(vcpu);
3224
3225 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
3226}
3227
3228static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3229{
3230 struct vcpu_svm *svm = to_svm(vcpu);
3231
3232 if (masked) {
3233 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003234 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003235 } else {
3236 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003237 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003238 }
3239}
3240
Gleb Natapov78646122009-03-23 12:12:11 +02003241static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
3242{
3243 struct vcpu_svm *svm = to_svm(vcpu);
3244 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003245 int ret;
3246
3247 if (!gif_set(svm) ||
3248 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
3249 return 0;
3250
3251 ret = !!(vmcb->save.rflags & X86_EFLAGS_IF);
3252
Joerg Roedel20307532010-11-29 17:51:48 +01003253 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003254 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
3255
3256 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02003257}
3258
Gleb Natapov9222be12009-04-23 17:14:37 +03003259static void enable_irq_window(struct kvm_vcpu *vcpu)
3260{
Alexander Graf219b65d2009-06-15 15:21:25 +02003261 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02003262
Joerg Roedele0231712010-02-24 18:59:10 +01003263 /*
3264 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3265 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3266 * get that intercept, this function will be called again though and
3267 * we'll get the vintr intercept.
3268 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01003269 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02003270 svm_set_vintr(svm);
3271 svm_inject_irq(svm, 0x0);
3272 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003273}
3274
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003275static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003276{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003277 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003278
Gleb Natapov44c11432009-05-11 13:35:52 +03003279 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
3280 == HF_NMI_MASK)
3281 return; /* IRET will cause a vm exit */
3282
Joerg Roedele0231712010-02-24 18:59:10 +01003283 /*
3284 * Something prevents NMI from been injected. Single step over possible
3285 * problem (IRET or exception injection or interrupt shadow)
3286 */
Jan Kiszka6be7d302009-10-18 13:24:54 +02003287 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03003288 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
3289 update_db_intercept(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003290}
3291
Izik Eiduscbc94022007-10-25 00:29:55 +02003292static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3293{
3294 return 0;
3295}
3296
Avi Kivityd9e368d2007-06-07 19:18:30 +03003297static void svm_flush_tlb(struct kvm_vcpu *vcpu)
3298{
3299 force_new_asid(vcpu);
3300}
3301
Avi Kivity04d2cc72007-09-10 18:10:54 +03003302static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
3303{
3304}
3305
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003306static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3307{
3308 struct vcpu_svm *svm = to_svm(vcpu);
3309
Joerg Roedel20307532010-11-29 17:51:48 +01003310 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003311 return;
3312
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003313 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003314 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03003315 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003316 }
3317}
3318
Joerg Roedel649d6862008-04-16 16:51:15 +02003319static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3320{
3321 struct vcpu_svm *svm = to_svm(vcpu);
3322 u64 cr8;
3323
Joerg Roedel20307532010-11-29 17:51:48 +01003324 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003325 return;
3326
Joerg Roedel649d6862008-04-16 16:51:15 +02003327 cr8 = kvm_get_cr8(vcpu);
3328 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3329 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3330}
3331
Gleb Natapov9222be12009-04-23 17:14:37 +03003332static void svm_complete_interrupts(struct vcpu_svm *svm)
3333{
3334 u8 vector;
3335 int type;
3336 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01003337 unsigned int3_injected = svm->int3_injected;
3338
3339 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003340
Avi Kivity3842d132010-07-27 12:30:24 +03003341 if (svm->vcpu.arch.hflags & HF_IRET_MASK) {
Gleb Natapov44c11432009-05-11 13:35:52 +03003342 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03003343 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3344 }
Gleb Natapov44c11432009-05-11 13:35:52 +03003345
Gleb Natapov9222be12009-04-23 17:14:37 +03003346 svm->vcpu.arch.nmi_injected = false;
3347 kvm_clear_exception_queue(&svm->vcpu);
3348 kvm_clear_interrupt_queue(&svm->vcpu);
3349
3350 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3351 return;
3352
Avi Kivity3842d132010-07-27 12:30:24 +03003353 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3354
Gleb Natapov9222be12009-04-23 17:14:37 +03003355 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3356 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3357
3358 switch (type) {
3359 case SVM_EXITINTINFO_TYPE_NMI:
3360 svm->vcpu.arch.nmi_injected = true;
3361 break;
3362 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01003363 /*
3364 * In case of software exceptions, do not reinject the vector,
3365 * but re-execute the instruction instead. Rewind RIP first
3366 * if we emulated INT3 before.
3367 */
3368 if (kvm_exception_is_soft(vector)) {
3369 if (vector == BP_VECTOR && int3_injected &&
3370 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
3371 kvm_rip_write(&svm->vcpu,
3372 kvm_rip_read(&svm->vcpu) -
3373 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02003374 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01003375 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003376 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3377 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003378 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03003379
3380 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003381 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03003382 break;
3383 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003384 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03003385 break;
3386 default:
3387 break;
3388 }
3389}
3390
Avi Kivityb463a6f2010-07-20 15:06:17 +03003391static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3392{
3393 struct vcpu_svm *svm = to_svm(vcpu);
3394 struct vmcb_control_area *control = &svm->vmcb->control;
3395
3396 control->exit_int_info = control->event_inj;
3397 control->exit_int_info_err = control->event_inj_err;
3398 control->event_inj = 0;
3399 svm_complete_interrupts(svm);
3400}
3401
Avi Kivity80e31d42008-07-14 14:44:59 +03003402#ifdef CONFIG_X86_64
3403#define R "r"
3404#else
3405#define R "e"
3406#endif
3407
Avi Kivity851ba692009-08-24 11:10:17 +03003408static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003409{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003410 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03003411
Joerg Roedel2041a062010-04-22 12:33:08 +02003412 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3413 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3414 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3415
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003416 /*
3417 * A vmexit emulation is required before the vcpu can be executed
3418 * again.
3419 */
3420 if (unlikely(svm->nested.exit_required))
3421 return;
3422
Rusty Russelle756fc62007-07-30 20:07:08 +10003423 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003424
Joerg Roedel649d6862008-04-16 16:51:15 +02003425 sync_lapic_to_cr8(vcpu);
3426
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003427 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003428
Avi Kivity04d2cc72007-09-10 18:10:54 +03003429 clgi();
3430
3431 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08003432
Avi Kivity6aa8b732006-12-10 02:21:36 -08003433 asm volatile (
Avi Kivity80e31d42008-07-14 14:44:59 +03003434 "push %%"R"bp; \n\t"
3435 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
3436 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
3437 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
3438 "mov %c[rsi](%[svm]), %%"R"si \n\t"
3439 "mov %c[rdi](%[svm]), %%"R"di \n\t"
3440 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003441#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003442 "mov %c[r8](%[svm]), %%r8 \n\t"
3443 "mov %c[r9](%[svm]), %%r9 \n\t"
3444 "mov %c[r10](%[svm]), %%r10 \n\t"
3445 "mov %c[r11](%[svm]), %%r11 \n\t"
3446 "mov %c[r12](%[svm]), %%r12 \n\t"
3447 "mov %c[r13](%[svm]), %%r13 \n\t"
3448 "mov %c[r14](%[svm]), %%r14 \n\t"
3449 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003450#endif
3451
Avi Kivity6aa8b732006-12-10 02:21:36 -08003452 /* Enter guest mode */
Avi Kivity80e31d42008-07-14 14:44:59 +03003453 "push %%"R"ax \n\t"
3454 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003455 __ex(SVM_VMLOAD) "\n\t"
3456 __ex(SVM_VMRUN) "\n\t"
3457 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity80e31d42008-07-14 14:44:59 +03003458 "pop %%"R"ax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003459
3460 /* Save guest registers, load host registers */
Avi Kivity80e31d42008-07-14 14:44:59 +03003461 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
3462 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
3463 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
3464 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
3465 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
3466 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003467#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003468 "mov %%r8, %c[r8](%[svm]) \n\t"
3469 "mov %%r9, %c[r9](%[svm]) \n\t"
3470 "mov %%r10, %c[r10](%[svm]) \n\t"
3471 "mov %%r11, %c[r11](%[svm]) \n\t"
3472 "mov %%r12, %c[r12](%[svm]) \n\t"
3473 "mov %%r13, %c[r13](%[svm]) \n\t"
3474 "mov %%r14, %c[r14](%[svm]) \n\t"
3475 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003476#endif
Avi Kivity80e31d42008-07-14 14:44:59 +03003477 "pop %%"R"bp"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003478 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003479 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003480 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003481 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
3482 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
3483 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
3484 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
3485 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
3486 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003487#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003488 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
3489 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
3490 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
3491 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
3492 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
3493 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
3494 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
3495 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003496#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02003497 : "cc", "memory"
Avi Kivity80e31d42008-07-14 14:44:59 +03003498 , R"bx", R"cx", R"dx", R"si", R"di"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003499#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02003500 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
3501#endif
3502 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003503
Avi Kivity82ca2d12010-10-21 12:20:34 +02003504#ifdef CONFIG_X86_64
3505 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
3506#else
Avi Kivitydacccfd2010-10-21 12:20:33 +02003507 loadsegment(fs, svm->host.fs);
Avi Kivity9581d442010-10-19 16:46:55 +02003508#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003509
3510 reload_tss(vcpu);
3511
Avi Kivity56ba47d2007-11-07 17:14:18 +02003512 local_irq_disable();
3513
3514 stgi();
3515
Avi Kivity13c34e02010-10-21 12:20:31 +02003516 vcpu->arch.cr2 = svm->vmcb->save.cr2;
3517 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3518 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3519 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
3520
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003521 sync_cr8_to_lapic(vcpu);
3522
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003523 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003524
Gleb Natapov631bc482010-10-14 11:22:52 +02003525 /* if exit due to PF check for async PF */
3526 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
3527 svm->apf_reason = kvm_read_and_reset_pf_reason();
3528
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003529 if (npt_enabled) {
3530 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3531 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3532 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02003533
3534 /*
3535 * We need to handle MC intercepts here before the vcpu has a chance to
3536 * change the physical cpu
3537 */
3538 if (unlikely(svm->vmcb->control.exit_code ==
3539 SVM_EXIT_EXCP_BASE + MC_VECTOR))
3540 svm_handle_mce(svm);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003541
3542 mark_all_clean(svm->vmcb);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003543}
3544
Avi Kivity80e31d42008-07-14 14:44:59 +03003545#undef R
3546
Avi Kivity6aa8b732006-12-10 02:21:36 -08003547static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3548{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003549 struct vcpu_svm *svm = to_svm(vcpu);
3550
3551 svm->vmcb->save.cr3 = root;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003552 force_new_asid(vcpu);
3553}
3554
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003555static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3556{
3557 struct vcpu_svm *svm = to_svm(vcpu);
3558
3559 svm->vmcb->control.nested_cr3 = root;
Joerg Roedelb2747162010-12-03 11:45:53 +01003560 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003561
3562 /* Also sync guest cr3 here in case we live migrate */
3563 svm->vmcb->save.cr3 = vcpu->arch.cr3;
3564
3565 force_new_asid(vcpu);
3566}
3567
Avi Kivity6aa8b732006-12-10 02:21:36 -08003568static int is_disabled(void)
3569{
Joerg Roedel6031a612007-06-22 12:29:50 +03003570 u64 vm_cr;
3571
3572 rdmsrl(MSR_VM_CR, vm_cr);
3573 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3574 return 1;
3575
Avi Kivity6aa8b732006-12-10 02:21:36 -08003576 return 0;
3577}
3578
Ingo Molnar102d8322007-02-19 14:37:47 +02003579static void
3580svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3581{
3582 /*
3583 * Patch in the VMMCALL instruction:
3584 */
3585 hypercall[0] = 0x0f;
3586 hypercall[1] = 0x01;
3587 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003588}
3589
Yang, Sheng002c7f72007-07-31 14:23:01 +03003590static void svm_check_processor_compat(void *rtn)
3591{
3592 *(int *)rtn = 0;
3593}
3594
Avi Kivity774ead32007-12-26 13:57:04 +02003595static bool svm_cpu_has_accelerated_tpr(void)
3596{
3597 return false;
3598}
3599
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003600static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003601{
3602 return 0;
3603}
3604
Sheng Yang0e851882009-12-18 16:48:46 +08003605static void svm_cpuid_update(struct kvm_vcpu *vcpu)
3606{
3607}
3608
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003609static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
3610{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003611 switch (func) {
Joerg Roedel24d1b152010-12-07 17:15:05 +01003612 case 0x00000001:
3613 /* Mask out xsave bit as long as it is not supported by SVM */
3614 entry->ecx &= ~(bit(X86_FEATURE_XSAVE));
3615 break;
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02003616 case 0x80000001:
3617 if (nested)
3618 entry->ecx |= (1 << 2); /* Set SVM bit */
3619 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003620 case 0x8000000A:
3621 entry->eax = 1; /* SVM revision 1 */
3622 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
3623 ASID emulation to nested SVM */
3624 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02003625 entry->edx = 0; /* Per default do not support any
3626 additional features */
3627
3628 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02003629 if (boot_cpu_has(X86_FEATURE_NRIPS))
Joerg Roedel7a190662010-07-27 18:14:21 +02003630 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003631
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02003632 /* Support NPT for the guest if enabled */
3633 if (npt_enabled)
3634 entry->edx |= SVM_FEATURE_NPT;
3635
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003636 break;
3637 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003638}
3639
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003640static const struct trace_print_flags svm_exit_reasons_str[] = {
Joerg Roedele0231712010-02-24 18:59:10 +01003641 { SVM_EXIT_READ_CR0, "read_cr0" },
3642 { SVM_EXIT_READ_CR3, "read_cr3" },
3643 { SVM_EXIT_READ_CR4, "read_cr4" },
3644 { SVM_EXIT_READ_CR8, "read_cr8" },
3645 { SVM_EXIT_WRITE_CR0, "write_cr0" },
3646 { SVM_EXIT_WRITE_CR3, "write_cr3" },
3647 { SVM_EXIT_WRITE_CR4, "write_cr4" },
3648 { SVM_EXIT_WRITE_CR8, "write_cr8" },
3649 { SVM_EXIT_READ_DR0, "read_dr0" },
3650 { SVM_EXIT_READ_DR1, "read_dr1" },
3651 { SVM_EXIT_READ_DR2, "read_dr2" },
3652 { SVM_EXIT_READ_DR3, "read_dr3" },
3653 { SVM_EXIT_WRITE_DR0, "write_dr0" },
3654 { SVM_EXIT_WRITE_DR1, "write_dr1" },
3655 { SVM_EXIT_WRITE_DR2, "write_dr2" },
3656 { SVM_EXIT_WRITE_DR3, "write_dr3" },
3657 { SVM_EXIT_WRITE_DR5, "write_dr5" },
3658 { SVM_EXIT_WRITE_DR7, "write_dr7" },
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003659 { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
3660 { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
3661 { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
3662 { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
3663 { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
3664 { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
3665 { SVM_EXIT_INTR, "interrupt" },
3666 { SVM_EXIT_NMI, "nmi" },
3667 { SVM_EXIT_SMI, "smi" },
3668 { SVM_EXIT_INIT, "init" },
3669 { SVM_EXIT_VINTR, "vintr" },
3670 { SVM_EXIT_CPUID, "cpuid" },
3671 { SVM_EXIT_INVD, "invd" },
3672 { SVM_EXIT_HLT, "hlt" },
3673 { SVM_EXIT_INVLPG, "invlpg" },
3674 { SVM_EXIT_INVLPGA, "invlpga" },
3675 { SVM_EXIT_IOIO, "io" },
3676 { SVM_EXIT_MSR, "msr" },
3677 { SVM_EXIT_TASK_SWITCH, "task_switch" },
3678 { SVM_EXIT_SHUTDOWN, "shutdown" },
3679 { SVM_EXIT_VMRUN, "vmrun" },
3680 { SVM_EXIT_VMMCALL, "hypercall" },
3681 { SVM_EXIT_VMLOAD, "vmload" },
3682 { SVM_EXIT_VMSAVE, "vmsave" },
3683 { SVM_EXIT_STGI, "stgi" },
3684 { SVM_EXIT_CLGI, "clgi" },
3685 { SVM_EXIT_SKINIT, "skinit" },
3686 { SVM_EXIT_WBINVD, "wbinvd" },
3687 { SVM_EXIT_MONITOR, "monitor" },
3688 { SVM_EXIT_MWAIT, "mwait" },
3689 { SVM_EXIT_NPF, "npf" },
3690 { -1, NULL }
3691};
3692
Sheng Yang17cc3932010-01-05 19:02:27 +08003693static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02003694{
Sheng Yang17cc3932010-01-05 19:02:27 +08003695 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02003696}
3697
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003698static bool svm_rdtscp_supported(void)
3699{
3700 return false;
3701}
3702
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003703static bool svm_has_wbinvd_exit(void)
3704{
3705 return true;
3706}
3707
Avi Kivity02daab22009-12-30 12:40:26 +02003708static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
3709{
3710 struct vcpu_svm *svm = to_svm(vcpu);
3711
Joerg Roedel18c918c2010-11-30 18:03:59 +01003712 set_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01003713 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02003714}
3715
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003716static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003717 .cpu_has_kvm_support = has_svm,
3718 .disabled_by_bios = is_disabled,
3719 .hardware_setup = svm_hardware_setup,
3720 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003721 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003722 .hardware_enable = svm_hardware_enable,
3723 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003724 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003725
3726 .vcpu_create = svm_create_vcpu,
3727 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003728 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003729
Avi Kivity04d2cc72007-09-10 18:10:54 +03003730 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003731 .vcpu_load = svm_vcpu_load,
3732 .vcpu_put = svm_vcpu_put,
3733
3734 .set_guest_debug = svm_guest_debug,
3735 .get_msr = svm_get_msr,
3736 .set_msr = svm_set_msr,
3737 .get_segment_base = svm_get_segment_base,
3738 .get_segment = svm_get_segment,
3739 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003740 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10003741 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02003742 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003743 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003744 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003745 .set_cr3 = svm_set_cr3,
3746 .set_cr4 = svm_set_cr4,
3747 .set_efer = svm_set_efer,
3748 .get_idt = svm_get_idt,
3749 .set_idt = svm_set_idt,
3750 .get_gdt = svm_get_gdt,
3751 .set_gdt = svm_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03003752 .set_dr7 = svm_set_dr7,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003753 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003754 .get_rflags = svm_get_rflags,
3755 .set_rflags = svm_set_rflags,
Avi Kivity6b52d182010-01-21 15:31:47 +02003756 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02003757 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003758
Avi Kivity6aa8b732006-12-10 02:21:36 -08003759 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003760
Avi Kivity6aa8b732006-12-10 02:21:36 -08003761 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003762 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003763 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04003764 .set_interrupt_shadow = svm_set_interrupt_shadow,
3765 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02003766 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003767 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003768 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02003769 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03003770 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02003771 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003772 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003773 .get_nmi_mask = svm_get_nmi_mask,
3774 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003775 .enable_nmi_window = enable_nmi_window,
3776 .enable_irq_window = enable_irq_window,
3777 .update_cr8_intercept = update_cr8_intercept,
Izik Eiduscbc94022007-10-25 00:29:55 +02003778
3779 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003780 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003781 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003782
Avi Kivity586f9602010-11-18 13:09:54 +02003783 .get_exit_info = svm_get_exit_info,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003784 .exit_reasons_str = svm_exit_reasons_str,
Avi Kivity586f9602010-11-18 13:09:54 +02003785
Sheng Yang17cc3932010-01-05 19:02:27 +08003786 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08003787
3788 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003789
3790 .rdtscp_supported = svm_rdtscp_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003791
3792 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003793
3794 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10003795
3796 .write_tsc_offset = svm_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10003797 .adjust_tsc_offset = svm_adjust_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003798
3799 .set_tdp_cr3 = set_tdp_cr3,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003800};
3801
3802static int __init svm_init(void)
3803{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003804 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03003805 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003806}
3807
3808static void __exit svm_exit(void)
3809{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003810 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003811}
3812
3813module_init(svm_init)
3814module_exit(svm_exit)