blob: 60bc1e53d2370ea8f36f27785af969d5ab8453e7 [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.
Avi Kivity221d0592010-05-23 18:37:00 +03007 * Copyright 2010 Red Hat, Inc. and/or its affilates.
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>
Avi Kivity6aa8b732006-12-10 02:21:36 -080034
Eduardo Habkost63d11422008-11-17 19:03:20 -020035#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030036#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020037
Avi Kivity4ecac3f2008-05-13 13:23:38 +030038#define __ex(x) __kvm_handle_fault_on_reboot(x)
39
Avi Kivity6aa8b732006-12-10 02:21:36 -080040MODULE_AUTHOR("Qumranet");
41MODULE_LICENSE("GPL");
42
43#define IOPM_ALLOC_ORDER 2
44#define MSRPM_ALLOC_ORDER 1
45
Avi Kivity6aa8b732006-12-10 02:21:36 -080046#define SEG_TYPE_LDT 2
47#define SEG_TYPE_BUSY_TSS16 3
48
Andre Przywara6bc31bd2010-04-11 23:07:28 +020049#define SVM_FEATURE_NPT (1 << 0)
50#define SVM_FEATURE_LBRV (1 << 1)
51#define SVM_FEATURE_SVML (1 << 2)
52#define SVM_FEATURE_NRIP (1 << 3)
53#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030054
Joerg Roedel410e4d52009-08-07 11:49:44 +020055#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
56#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
57#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
58
Joerg Roedel24e09cb2008-02-13 18:58:47 +010059#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
60
Joerg Roedel67ec6602010-05-17 14:43:35 +020061static bool erratum_383_found __read_mostly;
62
Avi Kivity6c8166a2009-05-31 18:15:37 +030063static const u32 host_save_user_msrs[] = {
64#ifdef CONFIG_X86_64
65 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
66 MSR_FS_BASE,
67#endif
68 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
69};
70
71#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
72
73struct kvm_vcpu;
74
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020075struct nested_state {
76 struct vmcb *hsave;
77 u64 hsave_msr;
Joerg Roedel4a810182010-02-24 18:59:15 +010078 u64 vm_cr_msr;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020079 u64 vmcb;
80
81 /* These are the merged vectors */
82 u32 *msrpm;
83
84 /* gpa pointers to the real vectors */
85 u64 vmcb_msrpm;
Joerg Roedelce2ac082010-03-01 15:34:39 +010086 u64 vmcb_iopm;
Joerg Roedelaad42c62009-08-07 11:49:34 +020087
Joerg Roedelcd3ff652009-10-09 16:08:26 +020088 /* A VMEXIT is required but not yet emulated */
89 bool exit_required;
90
Joerg Roedelaad42c62009-08-07 11:49:34 +020091 /* cache for intercepts of the guest */
92 u16 intercept_cr_read;
93 u16 intercept_cr_write;
94 u16 intercept_dr_read;
95 u16 intercept_dr_write;
96 u32 intercept_exceptions;
97 u64 intercept;
98
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020099};
100
Joerg Roedel323c3d82010-03-01 15:34:37 +0100101#define MSRPM_OFFSETS 16
102static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
103
Avi Kivity6c8166a2009-05-31 18:15:37 +0300104struct vcpu_svm {
105 struct kvm_vcpu vcpu;
106 struct vmcb *vmcb;
107 unsigned long vmcb_pa;
108 struct svm_cpu_data *svm_data;
109 uint64_t asid_generation;
110 uint64_t sysenter_esp;
111 uint64_t sysenter_eip;
112
113 u64 next_rip;
114
115 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
116 u64 host_gs_base;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300117
118 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300119
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200120 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200121
122 bool nmi_singlestep;
Jan Kiszka66b71382010-02-23 17:47:56 +0100123
124 unsigned int3_injected;
125 unsigned long int3_rip;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300126};
127
Joerg Roedel455716f2010-03-01 15:34:35 +0100128#define MSR_INVALID 0xffffffffU
129
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100130static struct svm_direct_access_msrs {
131 u32 index; /* Index of the MSR */
132 bool always; /* True if intercept is always on */
133} direct_access_msrs[] = {
Brian Gerst8c065852010-07-17 09:03:26 -0400134 { .index = MSR_STAR, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100135 { .index = MSR_IA32_SYSENTER_CS, .always = true },
136#ifdef CONFIG_X86_64
137 { .index = MSR_GS_BASE, .always = true },
138 { .index = MSR_FS_BASE, .always = true },
139 { .index = MSR_KERNEL_GS_BASE, .always = true },
140 { .index = MSR_LSTAR, .always = true },
141 { .index = MSR_CSTAR, .always = true },
142 { .index = MSR_SYSCALL_MASK, .always = true },
143#endif
144 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
145 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
146 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
147 { .index = MSR_IA32_LASTINTTOIP, .always = false },
148 { .index = MSR_INVALID, .always = false },
Avi Kivity6aa8b732006-12-10 02:21:36 -0800149};
150
151/* enable NPT for AMD64 and X86 with PAE */
152#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
153static bool npt_enabled = true;
154#else
Joerg Roedele0231712010-02-24 18:59:10 +0100155static bool npt_enabled;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800156#endif
157static int npt = 1;
158
159module_param(npt, int, S_IRUGO);
160
Joerg Roedel4b6e4dc2009-08-07 11:49:48 +0200161static int nested = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800162module_param(nested, int, S_IRUGO);
163
164static void svm_flush_tlb(struct kvm_vcpu *vcpu);
Joerg Roedela5c38322009-08-07 11:49:32 +0200165static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800166
Joerg Roedel410e4d52009-08-07 11:49:44 +0200167static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100168static int nested_svm_intercept(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800169static int nested_svm_vmexit(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800170static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
171 bool has_error_code, u32 error_code);
172
173static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
174{
175 return container_of(vcpu, struct vcpu_svm, vcpu);
176}
177
178static inline bool is_nested(struct vcpu_svm *svm)
179{
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200180 return svm->nested.vmcb;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800181}
182
Joerg Roedel2af91942009-08-07 11:49:28 +0200183static inline void enable_gif(struct vcpu_svm *svm)
184{
185 svm->vcpu.arch.hflags |= HF_GIF_MASK;
186}
187
188static inline void disable_gif(struct vcpu_svm *svm)
189{
190 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
191}
192
193static inline bool gif_set(struct vcpu_svm *svm)
194{
195 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
196}
197
Avi Kivity6aa8b732006-12-10 02:21:36 -0800198static unsigned long iopm_base;
199
200struct kvm_ldttss_desc {
201 u16 limit0;
202 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100203 unsigned base1:8, type:5, dpl:2, p:1;
204 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800205 u32 base3;
206 u32 zero1;
207} __attribute__((packed));
208
209struct svm_cpu_data {
210 int cpu;
211
Avi Kivity5008fdf2007-04-02 13:05:50 +0300212 u64 asid_generation;
213 u32 max_asid;
214 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800215 struct kvm_ldttss_desc *tss_desc;
216
217 struct page *save_area;
218};
219
220static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
Joerg Roedel80b77062007-03-30 17:02:14 +0300221static uint32_t svm_features;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800222
223struct svm_init_data {
224 int cpu;
225 int r;
226};
227
228static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
229
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200230#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800231#define MSRS_RANGE_SIZE 2048
232#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
233
Joerg Roedel455716f2010-03-01 15:34:35 +0100234static u32 svm_msrpm_offset(u32 msr)
235{
236 u32 offset;
237 int i;
238
239 for (i = 0; i < NUM_MSR_MAPS; i++) {
240 if (msr < msrpm_ranges[i] ||
241 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
242 continue;
243
244 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
245 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
246
247 /* Now we have the u8 offset - but need the u32 offset */
248 return offset / 4;
249 }
250
251 /* MSR not in any range */
252 return MSR_INVALID;
253}
254
Avi Kivity6aa8b732006-12-10 02:21:36 -0800255#define MAX_INST_SIZE 15
256
Joerg Roedel80b77062007-03-30 17:02:14 +0300257static inline u32 svm_has(u32 feat)
258{
259 return svm_features & feat;
260}
261
Avi Kivity6aa8b732006-12-10 02:21:36 -0800262static inline void clgi(void)
263{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300264 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800265}
266
267static inline void stgi(void)
268{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300269 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800270}
271
272static inline void invlpga(unsigned long addr, u32 asid)
273{
Joerg Roedele0231712010-02-24 18:59:10 +0100274 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800275}
276
Avi Kivity6aa8b732006-12-10 02:21:36 -0800277static inline void force_new_asid(struct kvm_vcpu *vcpu)
278{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400279 to_svm(vcpu)->asid_generation--;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800280}
281
282static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
283{
284 force_new_asid(vcpu);
285}
286
287static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
288{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000289 vcpu->arch.efer = efer;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100290 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600291 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800292
Alexander Graf9962d032008-11-25 20:17:02 +0100293 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800294}
295
Avi Kivity6aa8b732006-12-10 02:21:36 -0800296static int is_external_interrupt(u32 info)
297{
298 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
299 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
300}
301
Glauber Costa2809f5d2009-05-12 16:21:05 -0400302static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
303{
304 struct vcpu_svm *svm = to_svm(vcpu);
305 u32 ret = 0;
306
307 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Jan Kiszka48005f62010-02-19 19:38:07 +0100308 ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400309 return ret & mask;
310}
311
312static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
313{
314 struct vcpu_svm *svm = to_svm(vcpu);
315
316 if (mask == 0)
317 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
318 else
319 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
320
321}
322
Avi Kivity6aa8b732006-12-10 02:21:36 -0800323static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
324{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400325 struct vcpu_svm *svm = to_svm(vcpu);
326
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200327 if (svm->vmcb->control.next_rip != 0)
328 svm->next_rip = svm->vmcb->control.next_rip;
329
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400330 if (!svm->next_rip) {
Avi Kivity851ba692009-08-24 11:10:17 +0300331 if (emulate_instruction(vcpu, 0, 0, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300332 EMULATE_DONE)
333 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800334 return;
335 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300336 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
337 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
338 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800339
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300340 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400341 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800342}
343
Jan Kiszka116a4752010-02-23 17:47:54 +0100344static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200345 bool has_error_code, u32 error_code,
346 bool reinject)
Jan Kiszka116a4752010-02-23 17:47:54 +0100347{
348 struct vcpu_svm *svm = to_svm(vcpu);
349
Joerg Roedele0231712010-02-24 18:59:10 +0100350 /*
351 * If we are within a nested VM we'd better #VMEXIT and let the guest
352 * handle the exception
353 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200354 if (!reinject &&
355 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100356 return;
357
Jan Kiszka66b71382010-02-23 17:47:56 +0100358 if (nr == BP_VECTOR && !svm_has(SVM_FEATURE_NRIP)) {
359 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
360
361 /*
362 * For guest debugging where we have to reinject #BP if some
363 * INT3 is guest-owned:
364 * Emulate nRIP by moving RIP forward. Will fail if injection
365 * raises a fault that is not intercepted. Still better than
366 * failing in all cases.
367 */
368 skip_emulated_instruction(&svm->vcpu);
369 rip = kvm_rip_read(&svm->vcpu);
370 svm->int3_rip = rip + svm->vmcb->save.cs.base;
371 svm->int3_injected = rip - old_rip;
372 }
373
Jan Kiszka116a4752010-02-23 17:47:54 +0100374 svm->vmcb->control.event_inj = nr
375 | SVM_EVTINJ_VALID
376 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
377 | SVM_EVTINJ_TYPE_EXEPT;
378 svm->vmcb->control.event_inj_err = error_code;
379}
380
Joerg Roedel67ec6602010-05-17 14:43:35 +0200381static void svm_init_erratum_383(void)
382{
383 u32 low, high;
384 int err;
385 u64 val;
386
Hans Rosenfeld1be85a62010-07-28 19:09:32 +0200387 if (!cpu_has_amd_erratum(amd_erratum_383))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200388 return;
389
390 /* Use _safe variants to not break nested virtualization */
391 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
392 if (err)
393 return;
394
395 val |= (1ULL << 47);
396
397 low = lower_32_bits(val);
398 high = upper_32_bits(val);
399
400 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
401
402 erratum_383_found = true;
403}
404
Avi Kivity6aa8b732006-12-10 02:21:36 -0800405static int has_svm(void)
406{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200407 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800408
Eduardo Habkost63d11422008-11-17 19:03:20 -0200409 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800410 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800411 return 0;
412 }
413
Avi Kivity6aa8b732006-12-10 02:21:36 -0800414 return 1;
415}
416
417static void svm_hardware_disable(void *garbage)
418{
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200419 cpu_svm_disable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800420}
421
Alexander Graf10474ae2009-09-15 11:37:46 +0200422static int svm_hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800423{
424
Tejun Heo0fe1e002009-10-29 22:34:14 +0900425 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800426 uint64_t efer;
Gleb Natapov89a27f42010-02-16 10:51:48 +0200427 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800428 struct desc_struct *gdt;
429 int me = raw_smp_processor_id();
430
Alexander Graf10474ae2009-09-15 11:37:46 +0200431 rdmsrl(MSR_EFER, efer);
432 if (efer & EFER_SVME)
433 return -EBUSY;
434
Avi Kivity6aa8b732006-12-10 02:21:36 -0800435 if (!has_svm()) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000436 printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
437 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200438 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800439 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900440 sd = per_cpu(svm_data, me);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800441
Tejun Heo0fe1e002009-10-29 22:34:14 +0900442 if (!sd) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000443 printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -0800444 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200445 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800446 }
447
Tejun Heo0fe1e002009-10-29 22:34:14 +0900448 sd->asid_generation = 1;
449 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
450 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800451
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200452 native_store_gdt(&gdt_descr);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200453 gdt = (struct desc_struct *)gdt_descr.address;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900454 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800455
Alexander Graf9962d032008-11-25 20:17:02 +0100456 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800457
Linus Torvaldsd0316552009-12-14 09:58:24 -0800458 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200459
Joerg Roedel67ec6602010-05-17 14:43:35 +0200460 svm_init_erratum_383();
461
Alexander Graf10474ae2009-09-15 11:37:46 +0200462 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800463}
464
Joerg Roedel0da1db752008-07-02 16:02:11 +0200465static void svm_cpu_uninit(int cpu)
466{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900467 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200468
Tejun Heo0fe1e002009-10-29 22:34:14 +0900469 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200470 return;
471
472 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900473 __free_page(sd->save_area);
474 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200475}
476
Avi Kivity6aa8b732006-12-10 02:21:36 -0800477static int svm_cpu_init(int cpu)
478{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900479 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800480 int r;
481
Tejun Heo0fe1e002009-10-29 22:34:14 +0900482 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
483 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800484 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900485 sd->cpu = cpu;
486 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800487 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900488 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800489 goto err_1;
490
Tejun Heo0fe1e002009-10-29 22:34:14 +0900491 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800492
493 return 0;
494
495err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900496 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800497 return r;
498
499}
500
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100501static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800502{
503 int i;
504
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100505 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
506 if (direct_access_msrs[i].index == index)
507 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800508
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100509 return false;
510}
511
Avi Kivity6aa8b732006-12-10 02:21:36 -0800512static void set_msr_interception(u32 *msrpm, unsigned msr,
513 int read, int write)
514{
Joerg Roedel455716f2010-03-01 15:34:35 +0100515 u8 bit_read, bit_write;
516 unsigned long tmp;
517 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800518
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100519 /*
520 * If this warning triggers extend the direct_access_msrs list at the
521 * beginning of the file
522 */
523 WARN_ON(!valid_msr_intercept(msr));
524
Joerg Roedel455716f2010-03-01 15:34:35 +0100525 offset = svm_msrpm_offset(msr);
526 bit_read = 2 * (msr & 0x0f);
527 bit_write = 2 * (msr & 0x0f) + 1;
528 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800529
Joerg Roedel455716f2010-03-01 15:34:35 +0100530 BUG_ON(offset == MSR_INVALID);
531
532 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
533 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
534
535 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800536}
537
Joerg Roedelf65c2292008-02-13 18:58:46 +0100538static void svm_vcpu_init_msrpm(u32 *msrpm)
539{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100540 int i;
541
Joerg Roedelf65c2292008-02-13 18:58:46 +0100542 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
543
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100544 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
545 if (!direct_access_msrs[i].always)
546 continue;
547
548 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
549 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100550}
551
Joerg Roedel323c3d82010-03-01 15:34:37 +0100552static void add_msr_offset(u32 offset)
553{
554 int i;
555
556 for (i = 0; i < MSRPM_OFFSETS; ++i) {
557
558 /* Offset already in list? */
559 if (msrpm_offsets[i] == offset)
560 return;
561
562 /* Slot used by another offset? */
563 if (msrpm_offsets[i] != MSR_INVALID)
564 continue;
565
566 /* Add offset to list */
567 msrpm_offsets[i] = offset;
568
569 return;
570 }
571
572 /*
573 * If this BUG triggers the msrpm_offsets table has an overflow. Just
574 * increase MSRPM_OFFSETS in this case.
575 */
576 BUG();
577}
578
579static void init_msrpm_offsets(void)
580{
581 int i;
582
583 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
584
585 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
586 u32 offset;
587
588 offset = svm_msrpm_offset(direct_access_msrs[i].index);
589 BUG_ON(offset == MSR_INVALID);
590
591 add_msr_offset(offset);
592 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800593}
594
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100595static void svm_enable_lbrv(struct vcpu_svm *svm)
596{
597 u32 *msrpm = svm->msrpm;
598
599 svm->vmcb->control.lbr_ctl = 1;
600 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
601 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
602 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
603 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
604}
605
606static void svm_disable_lbrv(struct vcpu_svm *svm)
607{
608 u32 *msrpm = svm->msrpm;
609
610 svm->vmcb->control.lbr_ctl = 0;
611 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
612 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
613 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
614 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
615}
616
Avi Kivity6aa8b732006-12-10 02:21:36 -0800617static __init int svm_hardware_setup(void)
618{
619 int cpu;
620 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100621 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800622 int r;
623
Avi Kivity6aa8b732006-12-10 02:21:36 -0800624 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
625
626 if (!iopm_pages)
627 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300628
629 iopm_va = page_address(iopm_pages);
630 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800631 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
632
Joerg Roedel323c3d82010-03-01 15:34:37 +0100633 init_msrpm_offsets();
634
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100635 if (boot_cpu_has(X86_FEATURE_NX))
636 kvm_enable_efer_bits(EFER_NX);
637
Alexander Graf1b2fd702009-02-02 16:23:51 +0100638 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
639 kvm_enable_efer_bits(EFER_FFXSR);
640
Alexander Graf236de052008-11-25 20:17:10 +0100641 if (nested) {
642 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +0200643 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +0100644 }
645
Zachary Amsden3230bb42009-09-29 11:38:37 -1000646 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800647 r = svm_cpu_init(cpu);
648 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100649 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800650 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100651
652 svm_features = cpuid_edx(SVM_CPUID_FUNC);
653
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100654 if (!svm_has(SVM_FEATURE_NPT))
655 npt_enabled = false;
656
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100657 if (npt_enabled && !npt) {
658 printk(KERN_INFO "kvm: Nested Paging disabled\n");
659 npt_enabled = false;
660 }
661
Joerg Roedel18552672008-02-07 13:47:41 +0100662 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100663 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100664 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200665 } else
666 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100667
Avi Kivity6aa8b732006-12-10 02:21:36 -0800668 return 0;
669
Joerg Roedelf65c2292008-02-13 18:58:46 +0100670err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800671 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
672 iopm_base = 0;
673 return r;
674}
675
676static __exit void svm_hardware_unsetup(void)
677{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200678 int cpu;
679
Zachary Amsden3230bb42009-09-29 11:38:37 -1000680 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200681 svm_cpu_uninit(cpu);
682
Avi Kivity6aa8b732006-12-10 02:21:36 -0800683 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100684 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800685}
686
687static void init_seg(struct vmcb_seg *seg)
688{
689 seg->selector = 0;
690 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +0100691 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800692 seg->limit = 0xffff;
693 seg->base = 0;
694}
695
696static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
697{
698 seg->selector = 0;
699 seg->attrib = SVM_SELECTOR_P_MASK | type;
700 seg->limit = 0xffff;
701 seg->base = 0;
702}
703
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -1000704static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
705{
706 struct vcpu_svm *svm = to_svm(vcpu);
707 u64 g_tsc_offset = 0;
708
709 if (is_nested(svm)) {
710 g_tsc_offset = svm->vmcb->control.tsc_offset -
711 svm->nested.hsave->control.tsc_offset;
712 svm->nested.hsave->control.tsc_offset = offset;
713 }
714
715 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
716}
717
Zachary Amsdene48672f2010-08-19 22:07:23 -1000718static void svm_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment)
719{
720 struct vcpu_svm *svm = to_svm(vcpu);
721
722 svm->vmcb->control.tsc_offset += adjustment;
723 if (is_nested(svm))
724 svm->nested.hsave->control.tsc_offset += adjustment;
725}
726
Joerg Roedele6101a92008-02-13 18:58:45 +0100727static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800728{
Joerg Roedele6101a92008-02-13 18:58:45 +0100729 struct vmcb_control_area *control = &svm->vmcb->control;
730 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800731
Avi Kivitybff78272010-01-07 13:16:08 +0200732 svm->vcpu.fpu_active = 1;
733
Joerg Roedele0231712010-02-24 18:59:10 +0100734 control->intercept_cr_read = INTERCEPT_CR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800735 INTERCEPT_CR3_MASK |
Joerg Roedel649d6862008-04-16 16:51:15 +0200736 INTERCEPT_CR4_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800737
Joerg Roedele0231712010-02-24 18:59:10 +0100738 control->intercept_cr_write = INTERCEPT_CR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800739 INTERCEPT_CR3_MASK |
Avi Kivity80a81192007-12-06 19:50:00 +0200740 INTERCEPT_CR4_MASK |
741 INTERCEPT_CR8_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800742
Joerg Roedele0231712010-02-24 18:59:10 +0100743 control->intercept_dr_read = INTERCEPT_DR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800744 INTERCEPT_DR1_MASK |
745 INTERCEPT_DR2_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100746 INTERCEPT_DR3_MASK |
747 INTERCEPT_DR4_MASK |
748 INTERCEPT_DR5_MASK |
749 INTERCEPT_DR6_MASK |
750 INTERCEPT_DR7_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800751
Joerg Roedele0231712010-02-24 18:59:10 +0100752 control->intercept_dr_write = INTERCEPT_DR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800753 INTERCEPT_DR1_MASK |
754 INTERCEPT_DR2_MASK |
755 INTERCEPT_DR3_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100756 INTERCEPT_DR4_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800757 INTERCEPT_DR5_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100758 INTERCEPT_DR6_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800759 INTERCEPT_DR7_MASK;
760
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500761 control->intercept_exceptions = (1 << PF_VECTOR) |
Joerg Roedel53371b52008-04-09 14:15:30 +0200762 (1 << UD_VECTOR) |
763 (1 << MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800764
765
Joerg Roedele0231712010-02-24 18:59:10 +0100766 control->intercept = (1ULL << INTERCEPT_INTR) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800767 (1ULL << INTERCEPT_NMI) |
Joerg Roedel01525272007-02-19 14:37:47 +0200768 (1ULL << INTERCEPT_SMI) |
Avi Kivityd2251572010-01-06 10:55:27 +0200769 (1ULL << INTERCEPT_SELECTIVE_CR0) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800770 (1ULL << INTERCEPT_CPUID) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200771 (1ULL << INTERCEPT_INVD) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800772 (1ULL << INTERCEPT_HLT) |
Marcelo Tosattia7052892008-09-23 13:18:35 -0300773 (1ULL << INTERCEPT_INVLPG) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800774 (1ULL << INTERCEPT_INVLPGA) |
775 (1ULL << INTERCEPT_IOIO_PROT) |
776 (1ULL << INTERCEPT_MSR_PROT) |
777 (1ULL << INTERCEPT_TASK_SWITCH) |
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800778 (1ULL << INTERCEPT_SHUTDOWN) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800779 (1ULL << INTERCEPT_VMRUN) |
780 (1ULL << INTERCEPT_VMMCALL) |
781 (1ULL << INTERCEPT_VMLOAD) |
782 (1ULL << INTERCEPT_VMSAVE) |
783 (1ULL << INTERCEPT_STGI) |
784 (1ULL << INTERCEPT_CLGI) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100785 (1ULL << INTERCEPT_SKINIT) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200786 (1ULL << INTERCEPT_WBINVD) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100787 (1ULL << INTERCEPT_MONITOR) |
788 (1ULL << INTERCEPT_MWAIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789
790 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100791 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800792 control->int_ctl = V_INTR_MASKING_MASK;
793
794 init_seg(&save->es);
795 init_seg(&save->ss);
796 init_seg(&save->ds);
797 init_seg(&save->fs);
798 init_seg(&save->gs);
799
800 save->cs.selector = 0xf000;
801 /* Executable/Readable Code Segment */
802 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
803 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
804 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800805 /*
806 * cs.base should really be 0xffff0000, but vmx can't handle that, so
807 * be consistent with it.
808 *
809 * Replace when we have real mode working for vmx.
810 */
811 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800812
813 save->gdtr.limit = 0xffff;
814 save->idtr.limit = 0xffff;
815
816 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
817 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
818
Alexander Graf9962d032008-11-25 20:17:02 +0100819 save->efer = EFER_SVME;
Mike Dayd77c26f2007-10-08 09:02:08 -0400820 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800821 save->dr7 = 0x400;
822 save->rflags = 2;
823 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300824 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825
Joerg Roedele0231712010-02-24 18:59:10 +0100826 /*
827 * This is the guest-visible cr0 value.
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200828 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800829 */
Marcelo Tosatti678041a2010-08-31 19:13:13 -0300830 svm->vcpu.arch.cr0 = 0;
831 (void)kvm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200832
Rusty Russell66aee912007-07-17 23:34:16 +1000833 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800834 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100835
836 if (npt_enabled) {
837 /* Setup VMCB for Nested Paging */
838 control->nested_ctl = 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -0300839 control->intercept &= ~((1ULL << INTERCEPT_TASK_SWITCH) |
840 (1ULL << INTERCEPT_INVLPG));
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100841 control->intercept_exceptions &= ~(1 << PF_VECTOR);
Avi Kivity888f9f32010-01-10 12:14:04 +0200842 control->intercept_cr_read &= ~INTERCEPT_CR3_MASK;
843 control->intercept_cr_write &= ~INTERCEPT_CR3_MASK;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100844 save->g_pat = 0x0007040600070406ULL;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100845 save->cr3 = 0;
846 save->cr4 = 0;
847 }
Avi Kivitya79d2f12008-04-14 13:10:21 +0300848 force_new_asid(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +0100849
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200850 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +0200851 svm->vcpu.arch.hflags = 0;
852
Mark Langsdorf565d0992009-10-06 14:25:02 -0500853 if (svm_has(SVM_FEATURE_PAUSE_FILTER)) {
854 control->pause_filter_count = 3000;
855 control->intercept |= (1ULL << INTERCEPT_PAUSE);
856 }
857
Joerg Roedel2af91942009-08-07 11:49:28 +0200858 enable_gif(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800859}
860
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200861static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +0300862{
863 struct vcpu_svm *svm = to_svm(vcpu);
864
Joerg Roedele6101a92008-02-13 18:58:45 +0100865 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +0200866
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300867 if (!kvm_vcpu_is_bsp(vcpu)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300868 kvm_rip_write(vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800869 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
870 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +0200871 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300872 vcpu->arch.regs_avail = ~0;
873 vcpu->arch.regs_dirty = ~0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200874
875 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300876}
877
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000878static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800879{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400880 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100882 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +0100883 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100884 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000885 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800886
Rusty Russellc16f8622007-07-30 21:12:19 +1000887 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000888 if (!svm) {
889 err = -ENOMEM;
890 goto out;
891 }
892
893 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
894 if (err)
895 goto free_svm;
896
Joerg Roedelf65c2292008-02-13 18:58:46 +0100897 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900898 page = alloc_page(GFP_KERNEL);
899 if (!page)
900 goto uninit;
901
Joerg Roedelf65c2292008-02-13 18:58:46 +0100902 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
903 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900904 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100905
906 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
907 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900908 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100909
Alexander Grafb286d5d2008-11-25 20:17:05 +0100910 hsave_page = alloc_page(GFP_KERNEL);
911 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900912 goto free_page3;
913
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200914 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +0100915
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900916 svm->msrpm = page_address(msrpm_pages);
917 svm_vcpu_init_msrpm(svm->msrpm);
918
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200919 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +0100920 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +0100921
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400922 svm->vmcb = page_address(page);
923 clear_page(svm->vmcb);
924 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
925 svm->asid_generation = 0;
Joerg Roedele6101a92008-02-13 18:58:45 +0100926 init_vmcb(svm);
Zachary Amsden99e3e302010-08-19 22:07:17 -1000927 kvm_write_tsc(&svm->vcpu, 0);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400928
Jan Kiszka10ab25c2010-05-25 16:01:50 +0200929 err = fx_init(&svm->vcpu);
930 if (err)
931 goto free_page4;
932
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800933 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300934 if (kvm_vcpu_is_bsp(&svm->vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800935 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000937 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -0800938
Jan Kiszka10ab25c2010-05-25 16:01:50 +0200939free_page4:
940 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900941free_page3:
942 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
943free_page2:
944 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
945free_page1:
946 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000947uninit:
948 kvm_vcpu_uninit(&svm->vcpu);
949free_svm:
Rusty Russella4770342007-08-01 14:46:11 +1000950 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000951out:
952 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800953}
954
955static void svm_free_vcpu(struct kvm_vcpu *vcpu)
956{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400957 struct vcpu_svm *svm = to_svm(vcpu);
958
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000959 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +0100960 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200961 __free_page(virt_to_page(svm->nested.hsave));
962 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000963 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +1000964 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965}
966
Avi Kivity15ad7142007-07-11 18:17:21 +0300967static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400969 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300970 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +0200971
Avi Kivity0cc50642007-03-25 12:07:27 +0200972 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -0300973 svm->asid_generation = 0;
Avi Kivity0cc50642007-03-25 12:07:27 +0200974 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300975
976 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400977 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800978}
979
980static void svm_vcpu_put(struct kvm_vcpu *vcpu)
981{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400982 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300983 int i;
984
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200985 ++vcpu->stat.host_state_reload;
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300986 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400987 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988}
989
Avi Kivity6aa8b732006-12-10 02:21:36 -0800990static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
991{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400992 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993}
994
995static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
996{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400997 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998}
999
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001000static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1001{
1002 switch (reg) {
1003 case VCPU_EXREG_PDPTR:
1004 BUG_ON(!npt_enabled);
1005 load_pdptrs(vcpu, vcpu->arch.cr3);
1006 break;
1007 default:
1008 BUG();
1009 }
1010}
1011
Alexander Graff0b85052008-11-25 20:17:01 +01001012static void svm_set_vintr(struct vcpu_svm *svm)
1013{
1014 svm->vmcb->control.intercept |= 1ULL << INTERCEPT_VINTR;
1015}
1016
1017static void svm_clear_vintr(struct vcpu_svm *svm)
1018{
1019 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
1020}
1021
Avi Kivity6aa8b732006-12-10 02:21:36 -08001022static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1023{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001024 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001025
1026 switch (seg) {
1027 case VCPU_SREG_CS: return &save->cs;
1028 case VCPU_SREG_DS: return &save->ds;
1029 case VCPU_SREG_ES: return &save->es;
1030 case VCPU_SREG_FS: return &save->fs;
1031 case VCPU_SREG_GS: return &save->gs;
1032 case VCPU_SREG_SS: return &save->ss;
1033 case VCPU_SREG_TR: return &save->tr;
1034 case VCPU_SREG_LDTR: return &save->ldtr;
1035 }
1036 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001037 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001038}
1039
1040static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1041{
1042 struct vmcb_seg *s = svm_seg(vcpu, seg);
1043
1044 return s->base;
1045}
1046
1047static void svm_get_segment(struct kvm_vcpu *vcpu,
1048 struct kvm_segment *var, int seg)
1049{
1050 struct vmcb_seg *s = svm_seg(vcpu, seg);
1051
1052 var->base = s->base;
1053 var->limit = s->limit;
1054 var->selector = s->selector;
1055 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1056 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1057 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1058 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1059 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1060 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1061 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
1062 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
Amit Shah25022ac2008-10-27 09:04:17 +00001063
Joerg Roedele0231712010-02-24 18:59:10 +01001064 /*
1065 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001066 * for cross vendor migration purposes by "not present"
1067 */
1068 var->unusable = !var->present || (var->type == 0);
1069
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001070 switch (seg) {
1071 case VCPU_SREG_CS:
1072 /*
1073 * SVM always stores 0 for the 'G' bit in the CS selector in
1074 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
1075 * Intel's VMENTRY has a check on the 'G' bit.
1076 */
Amit Shah25022ac2008-10-27 09:04:17 +00001077 var->g = s->limit > 0xfffff;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001078 break;
1079 case VCPU_SREG_TR:
1080 /*
1081 * Work around a bug where the busy flag in the tr selector
1082 * isn't exposed
1083 */
Amit Shahc0d09822008-10-27 09:04:18 +00001084 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001085 break;
1086 case VCPU_SREG_DS:
1087 case VCPU_SREG_ES:
1088 case VCPU_SREG_FS:
1089 case VCPU_SREG_GS:
1090 /*
1091 * The accessed bit must always be set in the segment
1092 * descriptor cache, although it can be cleared in the
1093 * descriptor, the cached bit always remains at 1. Since
1094 * Intel has a check on this, set it here to support
1095 * cross-vendor migration.
1096 */
1097 if (!var->unusable)
1098 var->type |= 0x1;
1099 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001100 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001101 /*
1102 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001103 * descriptor is left as 1, although the whole segment has
1104 * been made unusable. Clear it here to pass an Intel VMX
1105 * entry check when cross vendor migrating.
1106 */
1107 if (var->unusable)
1108 var->db = 0;
1109 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001110 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001111}
1112
Izik Eidus2e4d2652008-03-24 19:38:34 +02001113static int svm_get_cpl(struct kvm_vcpu *vcpu)
1114{
1115 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1116
1117 return save->cpl;
1118}
1119
Gleb Natapov89a27f42010-02-16 10:51:48 +02001120static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001121{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001122 struct vcpu_svm *svm = to_svm(vcpu);
1123
Gleb Natapov89a27f42010-02-16 10:51:48 +02001124 dt->size = svm->vmcb->save.idtr.limit;
1125 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001126}
1127
Gleb Natapov89a27f42010-02-16 10:51:48 +02001128static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001129{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001130 struct vcpu_svm *svm = to_svm(vcpu);
1131
Gleb Natapov89a27f42010-02-16 10:51:48 +02001132 svm->vmcb->save.idtr.limit = dt->size;
1133 svm->vmcb->save.idtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001134}
1135
Gleb Natapov89a27f42010-02-16 10:51:48 +02001136static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001137{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001138 struct vcpu_svm *svm = to_svm(vcpu);
1139
Gleb Natapov89a27f42010-02-16 10:51:48 +02001140 dt->size = svm->vmcb->save.gdtr.limit;
1141 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001142}
1143
Gleb Natapov89a27f42010-02-16 10:51:48 +02001144static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001145{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001146 struct vcpu_svm *svm = to_svm(vcpu);
1147
Gleb Natapov89a27f42010-02-16 10:51:48 +02001148 svm->vmcb->save.gdtr.limit = dt->size;
1149 svm->vmcb->save.gdtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001150}
1151
Avi Kivitye8467fd2009-12-29 18:43:06 +02001152static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1153{
1154}
1155
Anthony Liguori25c4c272007-04-27 09:29:21 +03001156static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001157{
1158}
1159
Avi Kivityd2251572010-01-06 10:55:27 +02001160static void update_cr0_intercept(struct vcpu_svm *svm)
1161{
Joerg Roedel66a562f2010-02-19 16:23:08 +01001162 struct vmcb *vmcb = svm->vmcb;
Avi Kivityd2251572010-01-06 10:55:27 +02001163 ulong gcr0 = svm->vcpu.arch.cr0;
1164 u64 *hcr0 = &svm->vmcb->save.cr0;
1165
1166 if (!svm->vcpu.fpu_active)
1167 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1168 else
1169 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1170 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1171
1172
1173 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Joerg Roedel66a562f2010-02-19 16:23:08 +01001174 vmcb->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1175 vmcb->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1176 if (is_nested(svm)) {
1177 struct vmcb *hsave = svm->nested.hsave;
1178
1179 hsave->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1180 hsave->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1181 vmcb->control.intercept_cr_read |= svm->nested.intercept_cr_read;
1182 vmcb->control.intercept_cr_write |= svm->nested.intercept_cr_write;
1183 }
Avi Kivityd2251572010-01-06 10:55:27 +02001184 } else {
1185 svm->vmcb->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1186 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001187 if (is_nested(svm)) {
1188 struct vmcb *hsave = svm->nested.hsave;
1189
1190 hsave->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1191 hsave->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
1192 }
Avi Kivityd2251572010-01-06 10:55:27 +02001193 }
1194}
1195
Avi Kivity6aa8b732006-12-10 02:21:36 -08001196static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1197{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001198 struct vcpu_svm *svm = to_svm(vcpu);
1199
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001200 if (is_nested(svm)) {
1201 /*
1202 * We are here because we run in nested mode, the host kvm
1203 * intercepts cr0 writes but the l1 hypervisor does not.
1204 * But the L1 hypervisor may intercept selective cr0 writes.
1205 * This needs to be checked here.
1206 */
1207 unsigned long old, new;
1208
1209 /* Remove bits that would trigger a real cr0 write intercept */
1210 old = vcpu->arch.cr0 & SVM_CR0_SELECTIVE_MASK;
1211 new = cr0 & SVM_CR0_SELECTIVE_MASK;
1212
1213 if (old == new) {
1214 /* cr0 write with ts and mp unchanged */
1215 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
1216 if (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE)
1217 return;
1218 }
1219 }
1220
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001221#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001222 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92f2007-07-17 23:19:08 +10001223 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001224 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001225 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001226 }
1227
Mike Dayd77c26f2007-10-08 09:02:08 -04001228 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001229 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001230 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001231 }
1232 }
1233#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001234 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001235
1236 if (!npt_enabled)
1237 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001238
1239 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001240 cr0 |= X86_CR0_TS;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001241 /*
1242 * re-enable caching here because the QEMU bios
1243 * does not do it - this results in some delay at
1244 * reboot
1245 */
1246 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001247 svm->vmcb->save.cr0 = cr0;
Avi Kivityd2251572010-01-06 10:55:27 +02001248 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001249}
1250
1251static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1252{
Joerg Roedel6394b642008-04-09 14:15:29 +02001253 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001254 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1255
1256 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
1257 force_new_asid(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001258
Joerg Roedelec077262008-04-09 14:15:28 +02001259 vcpu->arch.cr4 = cr4;
1260 if (!npt_enabled)
1261 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001262 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001263 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001264}
1265
1266static void svm_set_segment(struct kvm_vcpu *vcpu,
1267 struct kvm_segment *var, int seg)
1268{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001269 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001270 struct vmcb_seg *s = svm_seg(vcpu, seg);
1271
1272 s->base = var->base;
1273 s->limit = var->limit;
1274 s->selector = var->selector;
1275 if (var->unusable)
1276 s->attrib = 0;
1277 else {
1278 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1279 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1280 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1281 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1282 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1283 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1284 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1285 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1286 }
1287 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001288 svm->vmcb->save.cpl
1289 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -08001290 >> SVM_SELECTOR_DPL_SHIFT) & 3;
1291
1292}
1293
Gleb Natapov44c11432009-05-11 13:35:52 +03001294static void update_db_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001295{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001296 struct vcpu_svm *svm = to_svm(vcpu);
1297
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001298 svm->vmcb->control.intercept_exceptions &=
1299 ~((1 << DB_VECTOR) | (1 << BP_VECTOR));
Gleb Natapov44c11432009-05-11 13:35:52 +03001300
Jan Kiszka6be7d302009-10-18 13:24:54 +02001301 if (svm->nmi_singlestep)
Gleb Natapov44c11432009-05-11 13:35:52 +03001302 svm->vmcb->control.intercept_exceptions |= (1 << DB_VECTOR);
1303
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001304 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1305 if (vcpu->guest_debug &
1306 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
1307 svm->vmcb->control.intercept_exceptions |=
1308 1 << DB_VECTOR;
1309 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
1310 svm->vmcb->control.intercept_exceptions |=
1311 1 << BP_VECTOR;
1312 } else
1313 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001314}
1315
Jan Kiszka355be0b2009-10-03 00:31:21 +02001316static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Gleb Natapov44c11432009-05-11 13:35:52 +03001317{
Gleb Natapov44c11432009-05-11 13:35:52 +03001318 struct vcpu_svm *svm = to_svm(vcpu);
1319
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001320 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1321 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1322 else
1323 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1324
Jan Kiszka355be0b2009-10-03 00:31:21 +02001325 update_db_intercept(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001326}
1327
1328static void load_host_msrs(struct kvm_vcpu *vcpu)
1329{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001330#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001331 wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001332#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001333}
1334
1335static void save_host_msrs(struct kvm_vcpu *vcpu)
1336{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001337#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001338 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001339#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001340}
1341
Tejun Heo0fe1e002009-10-29 22:34:14 +09001342static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001343{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001344 if (sd->next_asid > sd->max_asid) {
1345 ++sd->asid_generation;
1346 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001347 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001348 }
1349
Tejun Heo0fe1e002009-10-29 22:34:14 +09001350 svm->asid_generation = sd->asid_generation;
1351 svm->vmcb->control.asid = sd->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001352}
1353
Gleb Natapov020df072010-04-13 10:05:23 +03001354static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001355{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001356 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001357
Gleb Natapov020df072010-04-13 10:05:23 +03001358 svm->vmcb->save.dr7 = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001359}
1360
Avi Kivity851ba692009-08-24 11:10:17 +03001361static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001362{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001363 u64 fault_address;
1364 u32 error_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001365
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001366 fault_address = svm->vmcb->control.exit_info_2;
1367 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001368
Marcelo Tosatti229456f2009-06-17 09:22:14 -03001369 trace_kvm_page_fault(fault_address, error_code);
Avi Kivity52c78472009-08-10 15:42:41 +03001370 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1371 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
Avi Kivity30677142007-10-28 18:48:59 +02001372 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001373}
1374
Avi Kivity851ba692009-08-24 11:10:17 +03001375static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001376{
Avi Kivity851ba692009-08-24 11:10:17 +03001377 struct kvm_run *kvm_run = svm->vcpu.run;
1378
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001379 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001380 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001381 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001382 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1383 return 1;
1384 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001385
Jan Kiszka6be7d302009-10-18 13:24:54 +02001386 if (svm->nmi_singlestep) {
1387 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001388 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1389 svm->vmcb->save.rflags &=
1390 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1391 update_db_intercept(&svm->vcpu);
1392 }
1393
1394 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001395 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001396 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1397 kvm_run->debug.arch.pc =
1398 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1399 kvm_run->debug.arch.exception = DB_VECTOR;
1400 return 0;
1401 }
1402
1403 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001404}
1405
Avi Kivity851ba692009-08-24 11:10:17 +03001406static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001407{
Avi Kivity851ba692009-08-24 11:10:17 +03001408 struct kvm_run *kvm_run = svm->vcpu.run;
1409
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001410 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1411 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1412 kvm_run->debug.arch.exception = BP_VECTOR;
1413 return 0;
1414}
1415
Avi Kivity851ba692009-08-24 11:10:17 +03001416static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001417{
1418 int er;
1419
Avi Kivity851ba692009-08-24 11:10:17 +03001420 er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001421 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001422 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001423 return 1;
1424}
1425
Avi Kivity6b52d182010-01-21 15:31:47 +02001426static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001427{
Avi Kivity6b52d182010-01-21 15:31:47 +02001428 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001429 u32 excp;
1430
1431 if (is_nested(svm)) {
1432 u32 h_excp, n_excp;
1433
1434 h_excp = svm->nested.hsave->control.intercept_exceptions;
1435 n_excp = svm->nested.intercept_exceptions;
1436 h_excp &= ~(1 << NM_VECTOR);
1437 excp = h_excp | n_excp;
1438 } else {
1439 excp = svm->vmcb->control.intercept_exceptions;
Joerg Roedele0231712010-02-24 18:59:10 +01001440 excp &= ~(1 << NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001441 }
1442
1443 svm->vmcb->control.intercept_exceptions = excp;
1444
Rusty Russelle756fc62007-07-30 20:07:08 +10001445 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001446 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001447}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001448
Avi Kivity6b52d182010-01-21 15:31:47 +02001449static int nm_interception(struct vcpu_svm *svm)
1450{
1451 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001452 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001453}
1454
Joerg Roedel67ec6602010-05-17 14:43:35 +02001455static bool is_erratum_383(void)
1456{
1457 int err, i;
1458 u64 value;
1459
1460 if (!erratum_383_found)
1461 return false;
1462
1463 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1464 if (err)
1465 return false;
1466
1467 /* Bit 62 may or may not be set for this mce */
1468 value &= ~(1ULL << 62);
1469
1470 if (value != 0xb600000000010015ULL)
1471 return false;
1472
1473 /* Clear MCi_STATUS registers */
1474 for (i = 0; i < 6; ++i)
1475 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1476
1477 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1478 if (!err) {
1479 u32 low, high;
1480
1481 value &= ~(1ULL << 2);
1482 low = lower_32_bits(value);
1483 high = upper_32_bits(value);
1484
1485 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1486 }
1487
1488 /* Flush tlb to evict multi-match entries */
1489 __flush_tlb_all();
1490
1491 return true;
1492}
1493
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001494static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001495{
Joerg Roedel67ec6602010-05-17 14:43:35 +02001496 if (is_erratum_383()) {
1497 /*
1498 * Erratum 383 triggered. Guest state is corrupt so kill the
1499 * guest.
1500 */
1501 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1502
Avi Kivitya8eeb042010-05-10 12:34:53 +03001503 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02001504
1505 return;
1506 }
1507
Joerg Roedel53371b52008-04-09 14:15:30 +02001508 /*
1509 * On an #MC intercept the MCE handler is not called automatically in
1510 * the host. So do it by hand here.
1511 */
1512 asm volatile (
1513 "int $0x12\n");
1514 /* not sure if we ever come back to this point */
1515
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001516 return;
1517}
1518
1519static int mc_interception(struct vcpu_svm *svm)
1520{
Joerg Roedel53371b52008-04-09 14:15:30 +02001521 return 1;
1522}
1523
Avi Kivity851ba692009-08-24 11:10:17 +03001524static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001525{
Avi Kivity851ba692009-08-24 11:10:17 +03001526 struct kvm_run *kvm_run = svm->vcpu.run;
1527
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001528 /*
1529 * VMCB is undefined after a SHUTDOWN intercept
1530 * so reinitialize it.
1531 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001532 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001533 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001534
1535 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1536 return 0;
1537}
1538
Avi Kivity851ba692009-08-24 11:10:17 +03001539static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001540{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001541 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04001542 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001543 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001544 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001545
Rusty Russelle756fc62007-07-30 20:07:08 +10001546 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03001547 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001548 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001549 if (string || in)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03001550 return emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001551
Avi Kivity039576c2007-03-20 12:46:50 +02001552 port = io_info >> 16;
1553 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001554 svm->next_rip = svm->vmcb->control.exit_info_2;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001555 skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001556
1557 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001558}
1559
Avi Kivity851ba692009-08-24 11:10:17 +03001560static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001561{
1562 return 1;
1563}
1564
Avi Kivity851ba692009-08-24 11:10:17 +03001565static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001566{
1567 ++svm->vcpu.stat.irq_exits;
1568 return 1;
1569}
1570
Avi Kivity851ba692009-08-24 11:10:17 +03001571static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001572{
1573 return 1;
1574}
1575
Avi Kivity851ba692009-08-24 11:10:17 +03001576static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001577{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001578 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001579 skip_emulated_instruction(&svm->vcpu);
1580 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001581}
1582
Avi Kivity851ba692009-08-24 11:10:17 +03001583static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001584{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001585 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001586 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001587 kvm_emulate_hypercall(&svm->vcpu);
1588 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001589}
1590
Alexander Grafc0725422008-11-25 20:17:03 +01001591static int nested_svm_check_permissions(struct vcpu_svm *svm)
1592{
Avi Kivityf6801df2010-01-21 15:31:50 +02001593 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01001594 || !is_paging(&svm->vcpu)) {
1595 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1596 return 1;
1597 }
1598
1599 if (svm->vmcb->save.cpl) {
1600 kvm_inject_gp(&svm->vcpu, 0);
1601 return 1;
1602 }
1603
1604 return 0;
1605}
1606
Alexander Grafcf74a782008-11-25 20:17:08 +01001607static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
1608 bool has_error_code, u32 error_code)
1609{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001610 int vmexit;
1611
Joerg Roedel0295ad72009-08-07 11:49:37 +02001612 if (!is_nested(svm))
1613 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001614
Joerg Roedel0295ad72009-08-07 11:49:37 +02001615 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1616 svm->vmcb->control.exit_code_hi = 0;
1617 svm->vmcb->control.exit_info_1 = error_code;
1618 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1619
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001620 vmexit = nested_svm_intercept(svm);
1621 if (vmexit == NESTED_EXIT_DONE)
1622 svm->nested.exit_required = true;
1623
1624 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001625}
1626
Joerg Roedel8fe54652010-02-19 16:23:01 +01001627/* This function returns true if it is save to enable the irq window */
1628static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001629{
Joerg Roedel26666952009-08-07 11:49:46 +02001630 if (!is_nested(svm))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001631 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001632
Joerg Roedel26666952009-08-07 11:49:46 +02001633 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001634 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001635
Joerg Roedel26666952009-08-07 11:49:46 +02001636 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001637 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001638
Joerg Roedel197717d2010-02-24 18:59:19 +01001639 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
1640 svm->vmcb->control.exit_info_1 = 0;
1641 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02001642
Joerg Roedelcd3ff652009-10-09 16:08:26 +02001643 if (svm->nested.intercept & 1ULL) {
1644 /*
1645 * The #vmexit can't be emulated here directly because this
1646 * code path runs with irqs and preemtion disabled. A
1647 * #vmexit emulation might sleep. Only signal request for
1648 * the #vmexit here.
1649 */
1650 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02001651 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01001652 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001653 }
1654
Joerg Roedel8fe54652010-02-19 16:23:01 +01001655 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001656}
1657
Joerg Roedel887f5002010-02-24 18:59:12 +01001658/* This function returns true if it is save to enable the nmi window */
1659static inline bool nested_svm_nmi(struct vcpu_svm *svm)
1660{
1661 if (!is_nested(svm))
1662 return true;
1663
1664 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
1665 return true;
1666
1667 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
1668 svm->nested.exit_required = true;
1669
1670 return false;
1671}
1672
Joerg Roedel7597f122010-02-19 16:23:00 +01001673static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001674{
1675 struct page *page;
1676
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01001677 might_sleep();
1678
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001679 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001680 if (is_error_page(page))
1681 goto error;
1682
Joerg Roedel7597f122010-02-19 16:23:00 +01001683 *_page = page;
1684
1685 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001686
1687error:
1688 kvm_release_page_clean(page);
1689 kvm_inject_gp(&svm->vcpu, 0);
1690
1691 return NULL;
1692}
1693
Joerg Roedel7597f122010-02-19 16:23:00 +01001694static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001695{
Joerg Roedel7597f122010-02-19 16:23:00 +01001696 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001697 kvm_release_page_dirty(page);
1698}
1699
Joerg Roedelce2ac082010-03-01 15:34:39 +01001700static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001701{
Joerg Roedelce2ac082010-03-01 15:34:39 +01001702 unsigned port;
1703 u8 val, bit;
1704 u64 gpa;
1705
1706 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
1707 return NESTED_EXIT_HOST;
1708
1709 port = svm->vmcb->control.exit_info_1 >> 16;
1710 gpa = svm->nested.vmcb_iopm + (port / 8);
1711 bit = port % 8;
1712 val = 0;
1713
1714 if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1))
1715 val &= (1 << bit);
1716
1717 return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1718}
1719
Joerg Roedeld2477822010-03-01 15:34:34 +01001720static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001721{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001722 u32 offset, msr, value;
1723 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001724
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001725 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01001726 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001727
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001728 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1729 offset = svm_msrpm_offset(msr);
1730 write = svm->vmcb->control.exit_info_1 & 1;
1731 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001732
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001733 if (offset == MSR_INVALID)
1734 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001735
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001736 /* Offset is in 32 bit units but need in 8 bit units */
1737 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001738
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001739 if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4))
1740 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001741
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001742 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001743}
1744
Joerg Roedel410e4d52009-08-07 11:49:44 +02001745static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001746{
Alexander Grafcf74a782008-11-25 20:17:08 +01001747 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001748
Joerg Roedel410e4d52009-08-07 11:49:44 +02001749 switch (exit_code) {
1750 case SVM_EXIT_INTR:
1751 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02001752 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02001753 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001754 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01001755 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001756 if (npt_enabled)
1757 return NESTED_EXIT_HOST;
1758 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001759 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Joerg Roedele0231712010-02-24 18:59:10 +01001760 /* When we're shadowing, trap PFs */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001761 if (!npt_enabled)
1762 return NESTED_EXIT_HOST;
1763 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001764 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
1765 nm_interception(svm);
1766 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001767 default:
1768 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001769 }
1770
Joerg Roedel410e4d52009-08-07 11:49:44 +02001771 return NESTED_EXIT_CONTINUE;
1772}
1773
1774/*
1775 * If this function returns true, this #vmexit was already handled
1776 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001777static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001778{
1779 u32 exit_code = svm->vmcb->control.exit_code;
1780 int vmexit = NESTED_EXIT_HOST;
1781
Alexander Grafcf74a782008-11-25 20:17:08 +01001782 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001783 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001784 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001785 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01001786 case SVM_EXIT_IOIO:
1787 vmexit = nested_svm_intercept_ioio(svm);
1788 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001789 case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR8: {
1790 u32 cr_bits = 1 << (exit_code - SVM_EXIT_READ_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001791 if (svm->nested.intercept_cr_read & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001792 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001793 break;
1794 }
1795 case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR8: {
1796 u32 cr_bits = 1 << (exit_code - SVM_EXIT_WRITE_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001797 if (svm->nested.intercept_cr_write & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001798 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001799 break;
1800 }
1801 case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR7: {
1802 u32 dr_bits = 1 << (exit_code - SVM_EXIT_READ_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001803 if (svm->nested.intercept_dr_read & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001804 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001805 break;
1806 }
1807 case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR7: {
1808 u32 dr_bits = 1 << (exit_code - SVM_EXIT_WRITE_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001809 if (svm->nested.intercept_dr_write & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001810 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001811 break;
1812 }
1813 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1814 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001815 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001816 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001817 break;
1818 }
Joerg Roedel228070b2010-04-22 12:33:10 +02001819 case SVM_EXIT_ERR: {
1820 vmexit = NESTED_EXIT_DONE;
1821 break;
1822 }
Alexander Grafcf74a782008-11-25 20:17:08 +01001823 default: {
1824 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001825 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001826 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001827 }
1828 }
1829
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001830 return vmexit;
1831}
1832
1833static int nested_svm_exit_handled(struct vcpu_svm *svm)
1834{
1835 int vmexit;
1836
1837 vmexit = nested_svm_intercept(svm);
1838
1839 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001840 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001841
1842 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001843}
1844
Joerg Roedel0460a972009-08-07 11:49:31 +02001845static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
1846{
1847 struct vmcb_control_area *dst = &dst_vmcb->control;
1848 struct vmcb_control_area *from = &from_vmcb->control;
1849
1850 dst->intercept_cr_read = from->intercept_cr_read;
1851 dst->intercept_cr_write = from->intercept_cr_write;
1852 dst->intercept_dr_read = from->intercept_dr_read;
1853 dst->intercept_dr_write = from->intercept_dr_write;
1854 dst->intercept_exceptions = from->intercept_exceptions;
1855 dst->intercept = from->intercept;
1856 dst->iopm_base_pa = from->iopm_base_pa;
1857 dst->msrpm_base_pa = from->msrpm_base_pa;
1858 dst->tsc_offset = from->tsc_offset;
1859 dst->asid = from->asid;
1860 dst->tlb_ctl = from->tlb_ctl;
1861 dst->int_ctl = from->int_ctl;
1862 dst->int_vector = from->int_vector;
1863 dst->int_state = from->int_state;
1864 dst->exit_code = from->exit_code;
1865 dst->exit_code_hi = from->exit_code_hi;
1866 dst->exit_info_1 = from->exit_info_1;
1867 dst->exit_info_2 = from->exit_info_2;
1868 dst->exit_int_info = from->exit_int_info;
1869 dst->exit_int_info_err = from->exit_int_info_err;
1870 dst->nested_ctl = from->nested_ctl;
1871 dst->event_inj = from->event_inj;
1872 dst->event_inj_err = from->event_inj_err;
1873 dst->nested_cr3 = from->nested_cr3;
1874 dst->lbr_ctl = from->lbr_ctl;
1875}
1876
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001877static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001878{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001879 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001880 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02001881 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01001882 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01001883
Joerg Roedel17897f32009-10-09 16:08:29 +02001884 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
1885 vmcb->control.exit_info_1,
1886 vmcb->control.exit_info_2,
1887 vmcb->control.exit_int_info,
1888 vmcb->control.exit_int_info_err);
1889
Joerg Roedel7597f122010-02-19 16:23:00 +01001890 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001891 if (!nested_vmcb)
1892 return 1;
1893
Joerg Roedel06fc77722010-02-19 16:23:07 +01001894 /* Exit nested SVM mode */
1895 svm->nested.vmcb = 0;
1896
Alexander Grafcf74a782008-11-25 20:17:08 +01001897 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02001898 disable_gif(svm);
1899
1900 nested_vmcb->save.es = vmcb->save.es;
1901 nested_vmcb->save.cs = vmcb->save.cs;
1902 nested_vmcb->save.ss = vmcb->save.ss;
1903 nested_vmcb->save.ds = vmcb->save.ds;
1904 nested_vmcb->save.gdtr = vmcb->save.gdtr;
1905 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02001906 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001907 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedel2be4fc72010-04-22 12:33:09 +02001908 nested_vmcb->save.cr3 = svm->vcpu.arch.cr3;
Joerg Roedel33740e42009-08-07 11:49:29 +02001909 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001910 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Joerg Roedel33740e42009-08-07 11:49:29 +02001911 nested_vmcb->save.rflags = vmcb->save.rflags;
1912 nested_vmcb->save.rip = vmcb->save.rip;
1913 nested_vmcb->save.rsp = vmcb->save.rsp;
1914 nested_vmcb->save.rax = vmcb->save.rax;
1915 nested_vmcb->save.dr7 = vmcb->save.dr7;
1916 nested_vmcb->save.dr6 = vmcb->save.dr6;
1917 nested_vmcb->save.cpl = vmcb->save.cpl;
1918
1919 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
1920 nested_vmcb->control.int_vector = vmcb->control.int_vector;
1921 nested_vmcb->control.int_state = vmcb->control.int_state;
1922 nested_vmcb->control.exit_code = vmcb->control.exit_code;
1923 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
1924 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
1925 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
1926 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
1927 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel7a190662010-07-27 18:14:21 +02001928 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02001929
1930 /*
1931 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
1932 * to make sure that we do not lose injected events. So check event_inj
1933 * here and copy it to exit_int_info if it is valid.
1934 * Exit_int_info and event_inj can't be both valid because the case
1935 * below only happens on a VMRUN instruction intercept which has
1936 * no valid exit_int_info set.
1937 */
1938 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
1939 struct vmcb_control_area *nc = &nested_vmcb->control;
1940
1941 nc->exit_int_info = vmcb->control.event_inj;
1942 nc->exit_int_info_err = vmcb->control.event_inj_err;
1943 }
1944
Joerg Roedel33740e42009-08-07 11:49:29 +02001945 nested_vmcb->control.tlb_ctl = 0;
1946 nested_vmcb->control.event_inj = 0;
1947 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001948
1949 /* We always set V_INTR_MASKING and remember the old value in hflags */
1950 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
1951 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
1952
Alexander Grafcf74a782008-11-25 20:17:08 +01001953 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02001954 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01001955
Alexander Graf219b65d2009-06-15 15:21:25 +02001956 kvm_clear_exception_queue(&svm->vcpu);
1957 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01001958
1959 /* Restore selected save entries */
1960 svm->vmcb->save.es = hsave->save.es;
1961 svm->vmcb->save.cs = hsave->save.cs;
1962 svm->vmcb->save.ss = hsave->save.ss;
1963 svm->vmcb->save.ds = hsave->save.ds;
1964 svm->vmcb->save.gdtr = hsave->save.gdtr;
1965 svm->vmcb->save.idtr = hsave->save.idtr;
1966 svm->vmcb->save.rflags = hsave->save.rflags;
1967 svm_set_efer(&svm->vcpu, hsave->save.efer);
1968 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
1969 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
1970 if (npt_enabled) {
1971 svm->vmcb->save.cr3 = hsave->save.cr3;
1972 svm->vcpu.arch.cr3 = hsave->save.cr3;
1973 } else {
Avi Kivity23902182010-06-10 17:02:16 +03001974 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01001975 }
1976 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
1977 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
1978 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
1979 svm->vmcb->save.dr7 = 0;
1980 svm->vmcb->save.cpl = 0;
1981 svm->vmcb->control.exit_int_info = 0;
1982
Joerg Roedel7597f122010-02-19 16:23:00 +01001983 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01001984
1985 kvm_mmu_reset_context(&svm->vcpu);
1986 kvm_mmu_load(&svm->vcpu);
1987
1988 return 0;
1989}
Alexander Graf3d6368e2008-11-25 20:17:07 +01001990
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001991static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01001992{
Joerg Roedel323c3d82010-03-01 15:34:37 +01001993 /*
1994 * This function merges the msr permission bitmaps of kvm and the
1995 * nested vmcb. It is omptimized in that it only merges the parts where
1996 * the kvm msr permission bitmap may contain zero bits
1997 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01001998 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001999
Joerg Roedel323c3d82010-03-01 15:34:37 +01002000 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
2001 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002002
Joerg Roedel323c3d82010-03-01 15:34:37 +01002003 for (i = 0; i < MSRPM_OFFSETS; i++) {
2004 u32 value, p;
2005 u64 offset;
2006
2007 if (msrpm_offsets[i] == 0xffffffff)
2008 break;
2009
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002010 p = msrpm_offsets[i];
2011 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01002012
2013 if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4))
2014 return false;
2015
2016 svm->nested.msrpm[p] = svm->msrpm[p] | value;
2017 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002018
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002019 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002020
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002021 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002022}
2023
Joerg Roedel52c65a302010-08-02 16:46:44 +02002024static bool nested_vmcb_checks(struct vmcb *vmcb)
2025{
2026 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
2027 return false;
2028
Joerg Roedeldbe77582010-08-02 16:46:45 +02002029 if (vmcb->control.asid == 0)
2030 return false;
2031
Joerg Roedel52c65a302010-08-02 16:46:44 +02002032 return true;
2033}
2034
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002035static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002036{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002037 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002038 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02002039 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002040 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01002041 u64 vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002042
Joerg Roedel06fc77722010-02-19 16:23:07 +01002043 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002044
Joerg Roedel7597f122010-02-19 16:23:00 +01002045 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002046 if (!nested_vmcb)
2047 return false;
2048
Joerg Roedel52c65a302010-08-02 16:46:44 +02002049 if (!nested_vmcb_checks(nested_vmcb)) {
2050 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
2051 nested_vmcb->control.exit_code_hi = 0;
2052 nested_vmcb->control.exit_info_1 = 0;
2053 nested_vmcb->control.exit_info_2 = 0;
2054
2055 nested_svm_unmap(page);
2056
2057 return false;
2058 }
2059
Joerg Roedelecf14052010-02-24 18:59:13 +01002060 trace_kvm_nested_vmrun(svm->vmcb->save.rip - 3, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02002061 nested_vmcb->save.rip,
2062 nested_vmcb->control.int_ctl,
2063 nested_vmcb->control.event_inj,
2064 nested_vmcb->control.nested_ctl);
2065
Joerg Roedel2e554e82010-02-24 18:59:14 +01002066 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr_read,
2067 nested_vmcb->control.intercept_cr_write,
2068 nested_vmcb->control.intercept_exceptions,
2069 nested_vmcb->control.intercept);
2070
Alexander Graf3d6368e2008-11-25 20:17:07 +01002071 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02002072 kvm_clear_exception_queue(&svm->vcpu);
2073 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002074
Joerg Roedele0231712010-02-24 18:59:10 +01002075 /*
2076 * Save the old vmcb, so we don't need to pick what we save, but can
2077 * restore everything when a VMEXIT occurs
2078 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02002079 hsave->save.es = vmcb->save.es;
2080 hsave->save.cs = vmcb->save.cs;
2081 hsave->save.ss = vmcb->save.ss;
2082 hsave->save.ds = vmcb->save.ds;
2083 hsave->save.gdtr = vmcb->save.gdtr;
2084 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02002085 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002086 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002087 hsave->save.cr4 = svm->vcpu.arch.cr4;
2088 hsave->save.rflags = vmcb->save.rflags;
2089 hsave->save.rip = svm->next_rip;
2090 hsave->save.rsp = vmcb->save.rsp;
2091 hsave->save.rax = vmcb->save.rax;
2092 if (npt_enabled)
2093 hsave->save.cr3 = vmcb->save.cr3;
2094 else
2095 hsave->save.cr3 = svm->vcpu.arch.cr3;
2096
Joerg Roedel0460a972009-08-07 11:49:31 +02002097 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002098
2099 if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
2100 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2101 else
2102 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2103
2104 /* Load the nested guest state */
2105 svm->vmcb->save.es = nested_vmcb->save.es;
2106 svm->vmcb->save.cs = nested_vmcb->save.cs;
2107 svm->vmcb->save.ss = nested_vmcb->save.ss;
2108 svm->vmcb->save.ds = nested_vmcb->save.ds;
2109 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2110 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
2111 svm->vmcb->save.rflags = nested_vmcb->save.rflags;
2112 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2113 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2114 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2115 if (npt_enabled) {
2116 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2117 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002118 } else
Avi Kivity23902182010-06-10 17:02:16 +03002119 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002120
2121 /* Guest paging mode is active - reset mmu */
2122 kvm_mmu_reset_context(&svm->vcpu);
2123
Joerg Roedeldefbba52009-08-07 11:49:30 +02002124 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002125 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2126 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2127 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01002128
Alexander Graf3d6368e2008-11-25 20:17:07 +01002129 /* In case we don't even reach vcpu_run, the fields are not updated */
2130 svm->vmcb->save.rax = nested_vmcb->save.rax;
2131 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2132 svm->vmcb->save.rip = nested_vmcb->save.rip;
2133 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2134 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2135 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2136
Joerg Roedelf7138532010-03-01 15:34:40 +01002137 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002138 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002139
Joerg Roedelaad42c62009-08-07 11:49:34 +02002140 /* cache intercepts */
2141 svm->nested.intercept_cr_read = nested_vmcb->control.intercept_cr_read;
2142 svm->nested.intercept_cr_write = nested_vmcb->control.intercept_cr_write;
2143 svm->nested.intercept_dr_read = nested_vmcb->control.intercept_dr_read;
2144 svm->nested.intercept_dr_write = nested_vmcb->control.intercept_dr_write;
2145 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2146 svm->nested.intercept = nested_vmcb->control.intercept;
2147
Alexander Graf3d6368e2008-11-25 20:17:07 +01002148 force_new_asid(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002149 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002150 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2151 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2152 else
2153 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2154
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002155 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2156 /* We only want the cr8 intercept bits of the guest */
2157 svm->vmcb->control.intercept_cr_read &= ~INTERCEPT_CR8_MASK;
2158 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
2159 }
2160
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002161 /* We don't want to see VMMCALLs from a nested guest */
2162 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VMMCALL);
2163
Joerg Roedele0231712010-02-24 18:59:10 +01002164 /*
2165 * We don't want a nested guest to be more powerful than the guest, so
2166 * all intercepts are ORed
2167 */
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002168 svm->vmcb->control.intercept_cr_read |=
2169 nested_vmcb->control.intercept_cr_read;
2170 svm->vmcb->control.intercept_cr_write |=
2171 nested_vmcb->control.intercept_cr_write;
2172 svm->vmcb->control.intercept_dr_read |=
2173 nested_vmcb->control.intercept_dr_read;
2174 svm->vmcb->control.intercept_dr_write |=
2175 nested_vmcb->control.intercept_dr_write;
2176 svm->vmcb->control.intercept_exceptions |=
2177 nested_vmcb->control.intercept_exceptions;
2178
2179 svm->vmcb->control.intercept |= nested_vmcb->control.intercept;
2180
2181 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002182 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2183 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2184 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002185 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2186 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2187
Joerg Roedel7597f122010-02-19 16:23:00 +01002188 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002189
Joerg Roedel06fc77722010-02-19 16:23:07 +01002190 /* nested_vmcb is our indicator if nested SVM is activated */
2191 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002192
Joerg Roedel2af91942009-08-07 11:49:28 +02002193 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002194
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002195 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002196}
2197
Joerg Roedel9966bf62009-08-07 11:49:40 +02002198static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002199{
2200 to_vmcb->save.fs = from_vmcb->save.fs;
2201 to_vmcb->save.gs = from_vmcb->save.gs;
2202 to_vmcb->save.tr = from_vmcb->save.tr;
2203 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2204 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2205 to_vmcb->save.star = from_vmcb->save.star;
2206 to_vmcb->save.lstar = from_vmcb->save.lstar;
2207 to_vmcb->save.cstar = from_vmcb->save.cstar;
2208 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2209 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2210 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2211 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002212}
2213
Avi Kivity851ba692009-08-24 11:10:17 +03002214static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002215{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002216 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002217 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002218
Alexander Graf55426752008-11-25 20:17:06 +01002219 if (nested_svm_check_permissions(svm))
2220 return 1;
2221
2222 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2223 skip_emulated_instruction(&svm->vcpu);
2224
Joerg Roedel7597f122010-02-19 16:23:00 +01002225 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002226 if (!nested_vmcb)
2227 return 1;
2228
2229 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002230 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002231
2232 return 1;
2233}
2234
Avi Kivity851ba692009-08-24 11:10:17 +03002235static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002236{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002237 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002238 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002239
Alexander Graf55426752008-11-25 20:17:06 +01002240 if (nested_svm_check_permissions(svm))
2241 return 1;
2242
2243 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2244 skip_emulated_instruction(&svm->vcpu);
2245
Joerg Roedel7597f122010-02-19 16:23:00 +01002246 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002247 if (!nested_vmcb)
2248 return 1;
2249
2250 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002251 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002252
2253 return 1;
2254}
2255
Avi Kivity851ba692009-08-24 11:10:17 +03002256static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002257{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002258 if (nested_svm_check_permissions(svm))
2259 return 1;
2260
2261 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2262 skip_emulated_instruction(&svm->vcpu);
2263
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002264 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002265 return 1;
2266
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002267 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002268 goto failed;
2269
2270 return 1;
2271
2272failed:
2273
2274 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2275 svm->vmcb->control.exit_code_hi = 0;
2276 svm->vmcb->control.exit_info_1 = 0;
2277 svm->vmcb->control.exit_info_2 = 0;
2278
2279 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002280
2281 return 1;
2282}
2283
Avi Kivity851ba692009-08-24 11:10:17 +03002284static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002285{
2286 if (nested_svm_check_permissions(svm))
2287 return 1;
2288
2289 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2290 skip_emulated_instruction(&svm->vcpu);
2291
Joerg Roedel2af91942009-08-07 11:49:28 +02002292 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002293
2294 return 1;
2295}
2296
Avi Kivity851ba692009-08-24 11:10:17 +03002297static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002298{
2299 if (nested_svm_check_permissions(svm))
2300 return 1;
2301
2302 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2303 skip_emulated_instruction(&svm->vcpu);
2304
Joerg Roedel2af91942009-08-07 11:49:28 +02002305 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002306
2307 /* After a CLGI no interrupts should come */
2308 svm_clear_vintr(svm);
2309 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2310
2311 return 1;
2312}
2313
Avi Kivity851ba692009-08-24 11:10:17 +03002314static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002315{
2316 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002317
Joerg Roedelec1ff792009-10-09 16:08:31 +02002318 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
2319 vcpu->arch.regs[VCPU_REGS_RAX]);
2320
Alexander Grafff092382009-06-15 15:21:24 +02002321 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2322 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
2323
2324 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2325 skip_emulated_instruction(&svm->vcpu);
2326 return 1;
2327}
2328
Joerg Roedel532a46b2009-10-09 16:08:32 +02002329static int skinit_interception(struct vcpu_svm *svm)
2330{
2331 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
2332
2333 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2334 return 1;
2335}
2336
Avi Kivity851ba692009-08-24 11:10:17 +03002337static int invalid_op_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002338{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002339 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002340 return 1;
2341}
2342
Avi Kivity851ba692009-08-24 11:10:17 +03002343static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002344{
Izik Eidus37817f22008-03-24 23:14:53 +02002345 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002346 int reason;
2347 int int_type = svm->vmcb->control.exit_int_info &
2348 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002349 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002350 uint32_t type =
2351 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2352 uint32_t idt_v =
2353 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002354 bool has_error_code = false;
2355 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002356
2357 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002358
Izik Eidus37817f22008-03-24 23:14:53 +02002359 if (svm->vmcb->control.exit_info_2 &
2360 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002361 reason = TASK_SWITCH_IRET;
2362 else if (svm->vmcb->control.exit_info_2 &
2363 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2364 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002365 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002366 reason = TASK_SWITCH_GATE;
2367 else
2368 reason = TASK_SWITCH_CALL;
2369
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002370 if (reason == TASK_SWITCH_GATE) {
2371 switch (type) {
2372 case SVM_EXITINTINFO_TYPE_NMI:
2373 svm->vcpu.arch.nmi_injected = false;
2374 break;
2375 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002376 if (svm->vmcb->control.exit_info_2 &
2377 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2378 has_error_code = true;
2379 error_code =
2380 (u32)svm->vmcb->control.exit_info_2;
2381 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002382 kvm_clear_exception_queue(&svm->vcpu);
2383 break;
2384 case SVM_EXITINTINFO_TYPE_INTR:
2385 kvm_clear_interrupt_queue(&svm->vcpu);
2386 break;
2387 default:
2388 break;
2389 }
2390 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002391
Gleb Natapov8317c292009-04-12 13:37:02 +03002392 if (reason != TASK_SWITCH_GATE ||
2393 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2394 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002395 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2396 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002397
Gleb Natapovacb54512010-04-15 21:03:50 +03002398 if (kvm_task_switch(&svm->vcpu, tss_selector, reason,
2399 has_error_code, error_code) == EMULATE_FAIL) {
2400 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2401 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2402 svm->vcpu.run->internal.ndata = 0;
2403 return 0;
2404 }
2405 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002406}
2407
Avi Kivity851ba692009-08-24 11:10:17 +03002408static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002409{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002410 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002411 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002412 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002413}
2414
Avi Kivity851ba692009-08-24 11:10:17 +03002415static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002416{
2417 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02002418 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002419 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002420 return 1;
2421}
2422
Avi Kivity851ba692009-08-24 11:10:17 +03002423static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002424{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002425 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002426}
2427
Avi Kivity851ba692009-08-24 11:10:17 +03002428static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002429{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002430 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002431}
2432
Avi Kivity851ba692009-08-24 11:10:17 +03002433static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01002434{
Avi Kivity851ba692009-08-24 11:10:17 +03002435 struct kvm_run *kvm_run = svm->vcpu.run;
2436
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002437 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
2438 /* instruction emulation calls kvm_set_cr8() */
Avi Kivity851ba692009-08-24 11:10:17 +03002439 emulate_instruction(&svm->vcpu, 0, 0, 0);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002440 if (irqchip_in_kernel(svm->vcpu.kvm)) {
2441 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
Joerg Roedel1d075432007-12-06 21:02:25 +01002442 return 1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002443 }
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002444 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
2445 return 1;
Joerg Roedel1d075432007-12-06 21:02:25 +01002446 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2447 return 0;
2448}
2449
Avi Kivity6aa8b732006-12-10 02:21:36 -08002450static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
2451{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002452 struct vcpu_svm *svm = to_svm(vcpu);
2453
Avi Kivity6aa8b732006-12-10 02:21:36 -08002454 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302455 case MSR_IA32_TSC: {
Joerg Roedel20824f32009-09-16 15:24:18 +02002456 u64 tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002457
Joerg Roedel20824f32009-09-16 15:24:18 +02002458 if (is_nested(svm))
2459 tsc_offset = svm->nested.hsave->control.tsc_offset;
2460 else
2461 tsc_offset = svm->vmcb->control.tsc_offset;
2462
2463 *data = tsc_offset + native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002464 break;
2465 }
Brian Gerst8c065852010-07-17 09:03:26 -04002466 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002467 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002468 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08002469#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002470 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002471 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002472 break;
2473 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002474 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002475 break;
2476 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002477 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002478 break;
2479 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002480 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002481 break;
2482#endif
2483 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002484 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002485 break;
2486 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002487 *data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002488 break;
2489 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002490 *data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002491 break;
Joerg Roedele0231712010-02-24 18:59:10 +01002492 /*
2493 * Nobody will change the following 5 values in the VMCB so we can
2494 * safely return them on rdmsr. They will always be 0 until LBRV is
2495 * implemented.
2496 */
Joerg Roedela2938c82008-02-13 16:30:28 +01002497 case MSR_IA32_DEBUGCTLMSR:
2498 *data = svm->vmcb->save.dbgctl;
2499 break;
2500 case MSR_IA32_LASTBRANCHFROMIP:
2501 *data = svm->vmcb->save.br_from;
2502 break;
2503 case MSR_IA32_LASTBRANCHTOIP:
2504 *data = svm->vmcb->save.br_to;
2505 break;
2506 case MSR_IA32_LASTINTFROMIP:
2507 *data = svm->vmcb->save.last_excp_from;
2508 break;
2509 case MSR_IA32_LASTINTTOIP:
2510 *data = svm->vmcb->save.last_excp_to;
2511 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002512 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002513 *data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002514 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002515 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002516 *data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002517 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01002518 case MSR_IA32_UCODE_REV:
2519 *data = 0x01000065;
2520 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002521 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002522 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002523 }
2524 return 0;
2525}
2526
Avi Kivity851ba692009-08-24 11:10:17 +03002527static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002528{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002529 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002530 u64 data;
2531
Avi Kivity59200272010-01-25 19:47:02 +02002532 if (svm_get_msr(&svm->vcpu, ecx, &data)) {
2533 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002534 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002535 } else {
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002536 trace_kvm_msr_read(ecx, data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002537
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002538 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002539 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002540 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002541 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002542 }
2543 return 1;
2544}
2545
Joerg Roedel4a810182010-02-24 18:59:15 +01002546static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2547{
2548 struct vcpu_svm *svm = to_svm(vcpu);
2549 int svm_dis, chg_mask;
2550
2551 if (data & ~SVM_VM_CR_VALID_MASK)
2552 return 1;
2553
2554 chg_mask = SVM_VM_CR_VALID_MASK;
2555
2556 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2557 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2558
2559 svm->nested.vm_cr_msr &= ~chg_mask;
2560 svm->nested.vm_cr_msr |= (data & chg_mask);
2561
2562 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2563
2564 /* check for svm_disable while efer.svme is set */
2565 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2566 return 1;
2567
2568 return 0;
2569}
2570
Avi Kivity6aa8b732006-12-10 02:21:36 -08002571static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
2572{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002573 struct vcpu_svm *svm = to_svm(vcpu);
2574
Avi Kivity6aa8b732006-12-10 02:21:36 -08002575 switch (ecx) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10002576 case MSR_IA32_TSC:
Zachary Amsden99e3e302010-08-19 22:07:17 -10002577 kvm_write_tsc(vcpu, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002578 break;
Brian Gerst8c065852010-07-17 09:03:26 -04002579 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002580 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002581 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08002582#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002583 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002584 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002585 break;
2586 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002587 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002588 break;
2589 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002590 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002591 break;
2592 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002593 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002594 break;
2595#endif
2596 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002597 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002598 break;
2599 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002600 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002601 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002602 break;
2603 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002604 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002605 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002606 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002607 case MSR_IA32_DEBUGCTLMSR:
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002608 if (!svm_has(SVM_FEATURE_LBRV)) {
2609 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002610 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002611 break;
2612 }
2613 if (data & DEBUGCTL_RESERVED_BITS)
2614 return 1;
2615
2616 svm->vmcb->save.dbgctl = data;
2617 if (data & (1ULL<<0))
2618 svm_enable_lbrv(svm);
2619 else
2620 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01002621 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002622 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002623 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002624 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002625 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002626 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002627 case MSR_VM_IGNNE:
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002628 pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
2629 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002630 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002631 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002632 }
2633 return 0;
2634}
2635
Avi Kivity851ba692009-08-24 11:10:17 +03002636static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002637{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002638 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002639 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002640 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002641
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002642
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002643 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Avi Kivity59200272010-01-25 19:47:02 +02002644 if (svm_set_msr(&svm->vcpu, ecx, data)) {
2645 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002646 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002647 } else {
2648 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10002649 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02002650 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002651 return 1;
2652}
2653
Avi Kivity851ba692009-08-24 11:10:17 +03002654static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002655{
Rusty Russelle756fc62007-07-30 20:07:08 +10002656 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03002657 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002658 else
Avi Kivity851ba692009-08-24 11:10:17 +03002659 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002660}
2661
Avi Kivity851ba692009-08-24 11:10:17 +03002662static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08002663{
Avi Kivity851ba692009-08-24 11:10:17 +03002664 struct kvm_run *kvm_run = svm->vcpu.run;
2665
Alexander Graff0b85052008-11-25 20:17:01 +01002666 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03002667 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -08002668 /*
2669 * If the user space waits to inject interrupts, exit as soon as
2670 * possible
2671 */
Gleb Natapov80618232009-04-21 17:44:56 +03002672 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
2673 kvm_run->request_interrupt_window &&
2674 !kvm_cpu_has_interrupt(&svm->vcpu)) {
Rusty Russelle756fc62007-07-30 20:07:08 +10002675 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002676 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
2677 return 0;
2678 }
2679
2680 return 1;
2681}
2682
Mark Langsdorf565d0992009-10-06 14:25:02 -05002683static int pause_interception(struct vcpu_svm *svm)
2684{
2685 kvm_vcpu_on_spin(&(svm->vcpu));
2686 return 1;
2687}
2688
Avi Kivity851ba692009-08-24 11:10:17 +03002689static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
Joerg Roedele0231712010-02-24 18:59:10 +01002690 [SVM_EXIT_READ_CR0] = emulate_on_interception,
2691 [SVM_EXIT_READ_CR3] = emulate_on_interception,
2692 [SVM_EXIT_READ_CR4] = emulate_on_interception,
2693 [SVM_EXIT_READ_CR8] = emulate_on_interception,
Avi Kivityd2251572010-01-06 10:55:27 +02002694 [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002695 [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
2696 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
2697 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
2698 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
2699 [SVM_EXIT_READ_DR0] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002700 [SVM_EXIT_READ_DR1] = emulate_on_interception,
2701 [SVM_EXIT_READ_DR2] = emulate_on_interception,
2702 [SVM_EXIT_READ_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002703 [SVM_EXIT_READ_DR4] = emulate_on_interception,
2704 [SVM_EXIT_READ_DR5] = emulate_on_interception,
2705 [SVM_EXIT_READ_DR6] = emulate_on_interception,
2706 [SVM_EXIT_READ_DR7] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002707 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
2708 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
2709 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
2710 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002711 [SVM_EXIT_WRITE_DR4] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002712 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002713 [SVM_EXIT_WRITE_DR6] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002714 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002715 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
2716 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002717 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002718 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
2719 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
2720 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
2721 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02002722 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002723 [SVM_EXIT_SMI] = nop_on_interception,
2724 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08002725 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002726 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002727 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002728 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05002729 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002730 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03002731 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02002732 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002733 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002734 [SVM_EXIT_MSR] = msr_interception,
2735 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002736 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01002737 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02002738 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01002739 [SVM_EXIT_VMLOAD] = vmload_interception,
2740 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01002741 [SVM_EXIT_STGI] = stgi_interception,
2742 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02002743 [SVM_EXIT_SKINIT] = skinit_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002744 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01002745 [SVM_EXIT_MONITOR] = invalid_op_interception,
2746 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002747 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002748};
2749
Joerg Roedel3f10c842010-05-05 16:04:42 +02002750void dump_vmcb(struct kvm_vcpu *vcpu)
2751{
2752 struct vcpu_svm *svm = to_svm(vcpu);
2753 struct vmcb_control_area *control = &svm->vmcb->control;
2754 struct vmcb_save_area *save = &svm->vmcb->save;
2755
2756 pr_err("VMCB Control Area:\n");
2757 pr_err("cr_read: %04x\n", control->intercept_cr_read);
2758 pr_err("cr_write: %04x\n", control->intercept_cr_write);
2759 pr_err("dr_read: %04x\n", control->intercept_dr_read);
2760 pr_err("dr_write: %04x\n", control->intercept_dr_write);
2761 pr_err("exceptions: %08x\n", control->intercept_exceptions);
2762 pr_err("intercepts: %016llx\n", control->intercept);
2763 pr_err("pause filter count: %d\n", control->pause_filter_count);
2764 pr_err("iopm_base_pa: %016llx\n", control->iopm_base_pa);
2765 pr_err("msrpm_base_pa: %016llx\n", control->msrpm_base_pa);
2766 pr_err("tsc_offset: %016llx\n", control->tsc_offset);
2767 pr_err("asid: %d\n", control->asid);
2768 pr_err("tlb_ctl: %d\n", control->tlb_ctl);
2769 pr_err("int_ctl: %08x\n", control->int_ctl);
2770 pr_err("int_vector: %08x\n", control->int_vector);
2771 pr_err("int_state: %08x\n", control->int_state);
2772 pr_err("exit_code: %08x\n", control->exit_code);
2773 pr_err("exit_info1: %016llx\n", control->exit_info_1);
2774 pr_err("exit_info2: %016llx\n", control->exit_info_2);
2775 pr_err("exit_int_info: %08x\n", control->exit_int_info);
2776 pr_err("exit_int_info_err: %08x\n", control->exit_int_info_err);
2777 pr_err("nested_ctl: %lld\n", control->nested_ctl);
2778 pr_err("nested_cr3: %016llx\n", control->nested_cr3);
2779 pr_err("event_inj: %08x\n", control->event_inj);
2780 pr_err("event_inj_err: %08x\n", control->event_inj_err);
2781 pr_err("lbr_ctl: %lld\n", control->lbr_ctl);
2782 pr_err("next_rip: %016llx\n", control->next_rip);
2783 pr_err("VMCB State Save Area:\n");
2784 pr_err("es: s: %04x a: %04x l: %08x b: %016llx\n",
2785 save->es.selector, save->es.attrib,
2786 save->es.limit, save->es.base);
2787 pr_err("cs: s: %04x a: %04x l: %08x b: %016llx\n",
2788 save->cs.selector, save->cs.attrib,
2789 save->cs.limit, save->cs.base);
2790 pr_err("ss: s: %04x a: %04x l: %08x b: %016llx\n",
2791 save->ss.selector, save->ss.attrib,
2792 save->ss.limit, save->ss.base);
2793 pr_err("ds: s: %04x a: %04x l: %08x b: %016llx\n",
2794 save->ds.selector, save->ds.attrib,
2795 save->ds.limit, save->ds.base);
2796 pr_err("fs: s: %04x a: %04x l: %08x b: %016llx\n",
2797 save->fs.selector, save->fs.attrib,
2798 save->fs.limit, save->fs.base);
2799 pr_err("gs: s: %04x a: %04x l: %08x b: %016llx\n",
2800 save->gs.selector, save->gs.attrib,
2801 save->gs.limit, save->gs.base);
2802 pr_err("gdtr: s: %04x a: %04x l: %08x b: %016llx\n",
2803 save->gdtr.selector, save->gdtr.attrib,
2804 save->gdtr.limit, save->gdtr.base);
2805 pr_err("ldtr: s: %04x a: %04x l: %08x b: %016llx\n",
2806 save->ldtr.selector, save->ldtr.attrib,
2807 save->ldtr.limit, save->ldtr.base);
2808 pr_err("idtr: s: %04x a: %04x l: %08x b: %016llx\n",
2809 save->idtr.selector, save->idtr.attrib,
2810 save->idtr.limit, save->idtr.base);
2811 pr_err("tr: s: %04x a: %04x l: %08x b: %016llx\n",
2812 save->tr.selector, save->tr.attrib,
2813 save->tr.limit, save->tr.base);
2814 pr_err("cpl: %d efer: %016llx\n",
2815 save->cpl, save->efer);
2816 pr_err("cr0: %016llx cr2: %016llx\n",
2817 save->cr0, save->cr2);
2818 pr_err("cr3: %016llx cr4: %016llx\n",
2819 save->cr3, save->cr4);
2820 pr_err("dr6: %016llx dr7: %016llx\n",
2821 save->dr6, save->dr7);
2822 pr_err("rip: %016llx rflags: %016llx\n",
2823 save->rip, save->rflags);
2824 pr_err("rsp: %016llx rax: %016llx\n",
2825 save->rsp, save->rax);
2826 pr_err("star: %016llx lstar: %016llx\n",
2827 save->star, save->lstar);
2828 pr_err("cstar: %016llx sfmask: %016llx\n",
2829 save->cstar, save->sfmask);
2830 pr_err("kernel_gs_base: %016llx sysenter_cs: %016llx\n",
2831 save->kernel_gs_base, save->sysenter_cs);
2832 pr_err("sysenter_esp: %016llx sysenter_eip: %016llx\n",
2833 save->sysenter_esp, save->sysenter_eip);
2834 pr_err("gpat: %016llx dbgctl: %016llx\n",
2835 save->g_pat, save->dbgctl);
2836 pr_err("br_from: %016llx br_to: %016llx\n",
2837 save->br_from, save->br_to);
2838 pr_err("excp_from: %016llx excp_to: %016llx\n",
2839 save->last_excp_from, save->last_excp_to);
2840
2841}
2842
Avi Kivity851ba692009-08-24 11:10:17 +03002843static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002844{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002845 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002846 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002847 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002848
Avi Kivity5bfd8b52010-03-11 10:50:44 +02002849 trace_kvm_exit(exit_code, vcpu);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002850
Joerg Roedel2be4fc72010-04-22 12:33:09 +02002851 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR0_MASK))
2852 vcpu->arch.cr0 = svm->vmcb->save.cr0;
2853 if (npt_enabled)
2854 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002855
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002856 if (unlikely(svm->nested.exit_required)) {
2857 nested_svm_vmexit(svm);
2858 svm->nested.exit_required = false;
2859
2860 return 1;
2861 }
2862
Alexander Grafcf74a782008-11-25 20:17:08 +01002863 if (is_nested(svm)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02002864 int vmexit;
2865
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02002866 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
2867 svm->vmcb->control.exit_info_1,
2868 svm->vmcb->control.exit_info_2,
2869 svm->vmcb->control.exit_int_info,
2870 svm->vmcb->control.exit_int_info_err);
2871
Joerg Roedel410e4d52009-08-07 11:49:44 +02002872 vmexit = nested_svm_exit_special(svm);
2873
2874 if (vmexit == NESTED_EXIT_CONTINUE)
2875 vmexit = nested_svm_exit_handled(svm);
2876
2877 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01002878 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01002879 }
2880
Joerg Roedela5c38322009-08-07 11:49:32 +02002881 svm_complete_interrupts(svm);
2882
Avi Kivity04d2cc72007-09-10 18:10:54 +03002883 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
2884 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2885 kvm_run->fail_entry.hardware_entry_failure_reason
2886 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02002887 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
2888 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03002889 return 0;
2890 }
2891
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002892 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002893 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002894 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002895 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
2896 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002897 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002898 exit_code);
2899
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02002900 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08002901 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002902 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03002903 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002904 return 0;
2905 }
2906
Avi Kivity851ba692009-08-24 11:10:17 +03002907 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002908}
2909
2910static void reload_tss(struct kvm_vcpu *vcpu)
2911{
2912 int cpu = raw_smp_processor_id();
2913
Tejun Heo0fe1e002009-10-29 22:34:14 +09002914 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
2915 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002916 load_TR_desc();
2917}
2918
Rusty Russelle756fc62007-07-30 20:07:08 +10002919static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002920{
2921 int cpu = raw_smp_processor_id();
2922
Tejun Heo0fe1e002009-10-29 22:34:14 +09002923 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002924
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002925 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03002926 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09002927 if (svm->asid_generation != sd->asid_generation)
2928 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002929}
2930
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002931static void svm_inject_nmi(struct kvm_vcpu *vcpu)
2932{
2933 struct vcpu_svm *svm = to_svm(vcpu);
2934
2935 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
2936 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02002937 svm->vmcb->control.intercept |= (1ULL << INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002938 ++vcpu->stat.nmi_injections;
2939}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002940
Eddie Dong85f455f2007-07-06 12:20:49 +03002941static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002942{
2943 struct vmcb_control_area *control;
2944
Rusty Russelle756fc62007-07-30 20:07:08 +10002945 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03002946 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002947 control->int_ctl &= ~V_INTR_PRIO_MASK;
2948 control->int_ctl |= V_IRQ_MASK |
2949 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
2950}
2951
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002952static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03002953{
2954 struct vcpu_svm *svm = to_svm(vcpu);
2955
Joerg Roedel2af91942009-08-07 11:49:28 +02002956 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01002957
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03002958 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
2959 ++vcpu->stat.irq_injections;
2960
Alexander Graf219b65d2009-06-15 15:21:25 +02002961 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
2962 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002963}
2964
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002965static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
2966{
2967 struct vcpu_svm *svm = to_svm(vcpu);
2968
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002969 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
2970 return;
2971
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002972 if (irr == -1)
2973 return;
2974
2975 if (tpr >= irr)
2976 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK;
2977}
2978
2979static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02002980{
2981 struct vcpu_svm *svm = to_svm(vcpu);
2982 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02002983 int ret;
2984 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
2985 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
2986 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
2987
2988 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02002989}
2990
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002991static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
2992{
2993 struct vcpu_svm *svm = to_svm(vcpu);
2994
2995 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
2996}
2997
2998static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
2999{
3000 struct vcpu_svm *svm = to_svm(vcpu);
3001
3002 if (masked) {
3003 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02003004 svm->vmcb->control.intercept |= (1ULL << INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003005 } else {
3006 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02003007 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003008 }
3009}
3010
Gleb Natapov78646122009-03-23 12:12:11 +02003011static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
3012{
3013 struct vcpu_svm *svm = to_svm(vcpu);
3014 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003015 int ret;
3016
3017 if (!gif_set(svm) ||
3018 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
3019 return 0;
3020
3021 ret = !!(vmcb->save.rflags & X86_EFLAGS_IF);
3022
3023 if (is_nested(svm))
3024 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
3025
3026 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02003027}
3028
Gleb Natapov9222be12009-04-23 17:14:37 +03003029static void enable_irq_window(struct kvm_vcpu *vcpu)
3030{
Alexander Graf219b65d2009-06-15 15:21:25 +02003031 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02003032
Joerg Roedele0231712010-02-24 18:59:10 +01003033 /*
3034 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3035 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3036 * get that intercept, this function will be called again though and
3037 * we'll get the vintr intercept.
3038 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01003039 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02003040 svm_set_vintr(svm);
3041 svm_inject_irq(svm, 0x0);
3042 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003043}
3044
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003045static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003046{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003047 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003048
Gleb Natapov44c11432009-05-11 13:35:52 +03003049 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
3050 == HF_NMI_MASK)
3051 return; /* IRET will cause a vm exit */
3052
Joerg Roedele0231712010-02-24 18:59:10 +01003053 /*
3054 * Something prevents NMI from been injected. Single step over possible
3055 * problem (IRET or exception injection or interrupt shadow)
3056 */
Jan Kiszka6be7d302009-10-18 13:24:54 +02003057 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03003058 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
3059 update_db_intercept(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003060}
3061
Izik Eiduscbc94022007-10-25 00:29:55 +02003062static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3063{
3064 return 0;
3065}
3066
Avi Kivityd9e368d2007-06-07 19:18:30 +03003067static void svm_flush_tlb(struct kvm_vcpu *vcpu)
3068{
3069 force_new_asid(vcpu);
3070}
3071
Avi Kivity04d2cc72007-09-10 18:10:54 +03003072static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
3073{
3074}
3075
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003076static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3077{
3078 struct vcpu_svm *svm = to_svm(vcpu);
3079
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003080 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
3081 return;
3082
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003083 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) {
3084 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03003085 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003086 }
3087}
3088
Joerg Roedel649d6862008-04-16 16:51:15 +02003089static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3090{
3091 struct vcpu_svm *svm = to_svm(vcpu);
3092 u64 cr8;
3093
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003094 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
3095 return;
3096
Joerg Roedel649d6862008-04-16 16:51:15 +02003097 cr8 = kvm_get_cr8(vcpu);
3098 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3099 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3100}
3101
Gleb Natapov9222be12009-04-23 17:14:37 +03003102static void svm_complete_interrupts(struct vcpu_svm *svm)
3103{
3104 u8 vector;
3105 int type;
3106 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01003107 unsigned int3_injected = svm->int3_injected;
3108
3109 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003110
Gleb Natapov44c11432009-05-11 13:35:52 +03003111 if (svm->vcpu.arch.hflags & HF_IRET_MASK)
3112 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
3113
Gleb Natapov9222be12009-04-23 17:14:37 +03003114 svm->vcpu.arch.nmi_injected = false;
3115 kvm_clear_exception_queue(&svm->vcpu);
3116 kvm_clear_interrupt_queue(&svm->vcpu);
3117
3118 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3119 return;
3120
3121 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3122 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3123
3124 switch (type) {
3125 case SVM_EXITINTINFO_TYPE_NMI:
3126 svm->vcpu.arch.nmi_injected = true;
3127 break;
3128 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01003129 /*
3130 * In case of software exceptions, do not reinject the vector,
3131 * but re-execute the instruction instead. Rewind RIP first
3132 * if we emulated INT3 before.
3133 */
3134 if (kvm_exception_is_soft(vector)) {
3135 if (vector == BP_VECTOR && int3_injected &&
3136 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
3137 kvm_rip_write(&svm->vcpu,
3138 kvm_rip_read(&svm->vcpu) -
3139 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02003140 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01003141 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003142 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3143 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003144 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03003145
3146 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003147 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03003148 break;
3149 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003150 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03003151 break;
3152 default:
3153 break;
3154 }
3155}
3156
Avi Kivity80e31d42008-07-14 14:44:59 +03003157#ifdef CONFIG_X86_64
3158#define R "r"
3159#else
3160#define R "e"
3161#endif
3162
Avi Kivity851ba692009-08-24 11:10:17 +03003163static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003164{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003165 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003166 u16 fs_selector;
3167 u16 gs_selector;
3168 u16 ldt_selector;
Avi Kivityd9e368d2007-06-07 19:18:30 +03003169
Joerg Roedel2041a062010-04-22 12:33:08 +02003170 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3171 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3172 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3173
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003174 /*
3175 * A vmexit emulation is required before the vcpu can be executed
3176 * again.
3177 */
3178 if (unlikely(svm->nested.exit_required))
3179 return;
3180
Rusty Russelle756fc62007-07-30 20:07:08 +10003181 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003182
Joerg Roedel649d6862008-04-16 16:51:15 +02003183 sync_lapic_to_cr8(vcpu);
3184
Avi Kivity6aa8b732006-12-10 02:21:36 -08003185 save_host_msrs(vcpu);
Avi Kivity9581d442010-10-19 16:46:55 +02003186 savesegment(fs, fs_selector);
3187 savesegment(gs, gs_selector);
Avi Kivityd6e88ae2008-07-10 16:53:33 +03003188 ldt_selector = kvm_read_ldt();
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003189 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003190 /* required for live migration with NPT */
3191 if (npt_enabled)
3192 svm->vmcb->save.cr3 = vcpu->arch.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003193
Avi Kivity04d2cc72007-09-10 18:10:54 +03003194 clgi();
3195
3196 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08003197
Avi Kivity6aa8b732006-12-10 02:21:36 -08003198 asm volatile (
Avi Kivity80e31d42008-07-14 14:44:59 +03003199 "push %%"R"bp; \n\t"
3200 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
3201 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
3202 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
3203 "mov %c[rsi](%[svm]), %%"R"si \n\t"
3204 "mov %c[rdi](%[svm]), %%"R"di \n\t"
3205 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003206#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003207 "mov %c[r8](%[svm]), %%r8 \n\t"
3208 "mov %c[r9](%[svm]), %%r9 \n\t"
3209 "mov %c[r10](%[svm]), %%r10 \n\t"
3210 "mov %c[r11](%[svm]), %%r11 \n\t"
3211 "mov %c[r12](%[svm]), %%r12 \n\t"
3212 "mov %c[r13](%[svm]), %%r13 \n\t"
3213 "mov %c[r14](%[svm]), %%r14 \n\t"
3214 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003215#endif
3216
Avi Kivity6aa8b732006-12-10 02:21:36 -08003217 /* Enter guest mode */
Avi Kivity80e31d42008-07-14 14:44:59 +03003218 "push %%"R"ax \n\t"
3219 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003220 __ex(SVM_VMLOAD) "\n\t"
3221 __ex(SVM_VMRUN) "\n\t"
3222 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity80e31d42008-07-14 14:44:59 +03003223 "pop %%"R"ax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003224
3225 /* Save guest registers, load host registers */
Avi Kivity80e31d42008-07-14 14:44:59 +03003226 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
3227 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
3228 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
3229 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
3230 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
3231 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003232#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003233 "mov %%r8, %c[r8](%[svm]) \n\t"
3234 "mov %%r9, %c[r9](%[svm]) \n\t"
3235 "mov %%r10, %c[r10](%[svm]) \n\t"
3236 "mov %%r11, %c[r11](%[svm]) \n\t"
3237 "mov %%r12, %c[r12](%[svm]) \n\t"
3238 "mov %%r13, %c[r13](%[svm]) \n\t"
3239 "mov %%r14, %c[r14](%[svm]) \n\t"
3240 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003241#endif
Avi Kivity80e31d42008-07-14 14:44:59 +03003242 "pop %%"R"bp"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003243 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003244 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003245 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003246 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
3247 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
3248 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
3249 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
3250 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
3251 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003252#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003253 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
3254 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
3255 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
3256 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
3257 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
3258 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
3259 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
3260 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003261#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02003262 : "cc", "memory"
Avi Kivity80e31d42008-07-14 14:44:59 +03003263 , R"bx", R"cx", R"dx", R"si", R"di"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003264#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02003265 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
3266#endif
3267 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003268
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003269 vcpu->arch.cr2 = svm->vmcb->save.cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003270 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3271 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3272 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003273
Avi Kivity6aa8b732006-12-10 02:21:36 -08003274 load_host_msrs(vcpu);
Avi Kivity9581d442010-10-19 16:46:55 +02003275 loadsegment(fs, fs_selector);
3276#ifdef CONFIG_X86_64
3277 load_gs_index(gs_selector);
3278 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs);
3279#else
3280 loadsegment(gs, gs_selector);
3281#endif
3282 kvm_load_ldt(ldt_selector);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003283
3284 reload_tss(vcpu);
3285
Avi Kivity56ba47d2007-11-07 17:14:18 +02003286 local_irq_disable();
3287
3288 stgi();
3289
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003290 sync_cr8_to_lapic(vcpu);
3291
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003292 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003293
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003294 if (npt_enabled) {
3295 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3296 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3297 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02003298
3299 /*
3300 * We need to handle MC intercepts here before the vcpu has a chance to
3301 * change the physical cpu
3302 */
3303 if (unlikely(svm->vmcb->control.exit_code ==
3304 SVM_EXIT_EXCP_BASE + MC_VECTOR))
3305 svm_handle_mce(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003306}
3307
Avi Kivity80e31d42008-07-14 14:44:59 +03003308#undef R
3309
Avi Kivity6aa8b732006-12-10 02:21:36 -08003310static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3311{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003312 struct vcpu_svm *svm = to_svm(vcpu);
3313
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003314 if (npt_enabled) {
3315 svm->vmcb->control.nested_cr3 = root;
3316 force_new_asid(vcpu);
3317 return;
3318 }
3319
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003320 svm->vmcb->save.cr3 = root;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003321 force_new_asid(vcpu);
3322}
3323
Avi Kivity6aa8b732006-12-10 02:21:36 -08003324static int is_disabled(void)
3325{
Joerg Roedel6031a612007-06-22 12:29:50 +03003326 u64 vm_cr;
3327
3328 rdmsrl(MSR_VM_CR, vm_cr);
3329 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3330 return 1;
3331
Avi Kivity6aa8b732006-12-10 02:21:36 -08003332 return 0;
3333}
3334
Ingo Molnar102d8322007-02-19 14:37:47 +02003335static void
3336svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3337{
3338 /*
3339 * Patch in the VMMCALL instruction:
3340 */
3341 hypercall[0] = 0x0f;
3342 hypercall[1] = 0x01;
3343 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003344}
3345
Yang, Sheng002c7f72007-07-31 14:23:01 +03003346static void svm_check_processor_compat(void *rtn)
3347{
3348 *(int *)rtn = 0;
3349}
3350
Avi Kivity774ead32007-12-26 13:57:04 +02003351static bool svm_cpu_has_accelerated_tpr(void)
3352{
3353 return false;
3354}
3355
Sheng Yang67253af2008-04-25 10:20:22 +08003356static int get_npt_level(void)
3357{
3358#ifdef CONFIG_X86_64
3359 return PT64_ROOT_LEVEL;
3360#else
3361 return PT32E_ROOT_LEVEL;
3362#endif
3363}
3364
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003365static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003366{
3367 return 0;
3368}
3369
Sheng Yang0e851882009-12-18 16:48:46 +08003370static void svm_cpuid_update(struct kvm_vcpu *vcpu)
3371{
3372}
3373
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003374static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
3375{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003376 switch (func) {
3377 case 0x8000000A:
3378 entry->eax = 1; /* SVM revision 1 */
3379 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
3380 ASID emulation to nested SVM */
3381 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02003382 entry->edx = 0; /* Per default do not support any
3383 additional features */
3384
3385 /* Support next_rip if host supports it */
3386 if (svm_has(SVM_FEATURE_NRIP))
3387 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003388
3389 break;
3390 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003391}
3392
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003393static const struct trace_print_flags svm_exit_reasons_str[] = {
Joerg Roedele0231712010-02-24 18:59:10 +01003394 { SVM_EXIT_READ_CR0, "read_cr0" },
3395 { SVM_EXIT_READ_CR3, "read_cr3" },
3396 { SVM_EXIT_READ_CR4, "read_cr4" },
3397 { SVM_EXIT_READ_CR8, "read_cr8" },
3398 { SVM_EXIT_WRITE_CR0, "write_cr0" },
3399 { SVM_EXIT_WRITE_CR3, "write_cr3" },
3400 { SVM_EXIT_WRITE_CR4, "write_cr4" },
3401 { SVM_EXIT_WRITE_CR8, "write_cr8" },
3402 { SVM_EXIT_READ_DR0, "read_dr0" },
3403 { SVM_EXIT_READ_DR1, "read_dr1" },
3404 { SVM_EXIT_READ_DR2, "read_dr2" },
3405 { SVM_EXIT_READ_DR3, "read_dr3" },
3406 { SVM_EXIT_WRITE_DR0, "write_dr0" },
3407 { SVM_EXIT_WRITE_DR1, "write_dr1" },
3408 { SVM_EXIT_WRITE_DR2, "write_dr2" },
3409 { SVM_EXIT_WRITE_DR3, "write_dr3" },
3410 { SVM_EXIT_WRITE_DR5, "write_dr5" },
3411 { SVM_EXIT_WRITE_DR7, "write_dr7" },
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003412 { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
3413 { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
3414 { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
3415 { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
3416 { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
3417 { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
3418 { SVM_EXIT_INTR, "interrupt" },
3419 { SVM_EXIT_NMI, "nmi" },
3420 { SVM_EXIT_SMI, "smi" },
3421 { SVM_EXIT_INIT, "init" },
3422 { SVM_EXIT_VINTR, "vintr" },
3423 { SVM_EXIT_CPUID, "cpuid" },
3424 { SVM_EXIT_INVD, "invd" },
3425 { SVM_EXIT_HLT, "hlt" },
3426 { SVM_EXIT_INVLPG, "invlpg" },
3427 { SVM_EXIT_INVLPGA, "invlpga" },
3428 { SVM_EXIT_IOIO, "io" },
3429 { SVM_EXIT_MSR, "msr" },
3430 { SVM_EXIT_TASK_SWITCH, "task_switch" },
3431 { SVM_EXIT_SHUTDOWN, "shutdown" },
3432 { SVM_EXIT_VMRUN, "vmrun" },
3433 { SVM_EXIT_VMMCALL, "hypercall" },
3434 { SVM_EXIT_VMLOAD, "vmload" },
3435 { SVM_EXIT_VMSAVE, "vmsave" },
3436 { SVM_EXIT_STGI, "stgi" },
3437 { SVM_EXIT_CLGI, "clgi" },
3438 { SVM_EXIT_SKINIT, "skinit" },
3439 { SVM_EXIT_WBINVD, "wbinvd" },
3440 { SVM_EXIT_MONITOR, "monitor" },
3441 { SVM_EXIT_MWAIT, "mwait" },
3442 { SVM_EXIT_NPF, "npf" },
3443 { -1, NULL }
3444};
3445
Sheng Yang17cc3932010-01-05 19:02:27 +08003446static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02003447{
Sheng Yang17cc3932010-01-05 19:02:27 +08003448 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02003449}
3450
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003451static bool svm_rdtscp_supported(void)
3452{
3453 return false;
3454}
3455
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003456static bool svm_has_wbinvd_exit(void)
3457{
3458 return true;
3459}
3460
Avi Kivity02daab22009-12-30 12:40:26 +02003461static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
3462{
3463 struct vcpu_svm *svm = to_svm(vcpu);
3464
Avi Kivity02daab22009-12-30 12:40:26 +02003465 svm->vmcb->control.intercept_exceptions |= 1 << NM_VECTOR;
Joerg Roedel66a562f2010-02-19 16:23:08 +01003466 if (is_nested(svm))
3467 svm->nested.hsave->control.intercept_exceptions |= 1 << NM_VECTOR;
3468 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02003469}
3470
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003471static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003472 .cpu_has_kvm_support = has_svm,
3473 .disabled_by_bios = is_disabled,
3474 .hardware_setup = svm_hardware_setup,
3475 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003476 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003477 .hardware_enable = svm_hardware_enable,
3478 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003479 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003480
3481 .vcpu_create = svm_create_vcpu,
3482 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003483 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003484
Avi Kivity04d2cc72007-09-10 18:10:54 +03003485 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003486 .vcpu_load = svm_vcpu_load,
3487 .vcpu_put = svm_vcpu_put,
3488
3489 .set_guest_debug = svm_guest_debug,
3490 .get_msr = svm_get_msr,
3491 .set_msr = svm_set_msr,
3492 .get_segment_base = svm_get_segment_base,
3493 .get_segment = svm_get_segment,
3494 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003495 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10003496 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02003497 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003498 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003499 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003500 .set_cr3 = svm_set_cr3,
3501 .set_cr4 = svm_set_cr4,
3502 .set_efer = svm_set_efer,
3503 .get_idt = svm_get_idt,
3504 .set_idt = svm_set_idt,
3505 .get_gdt = svm_get_gdt,
3506 .set_gdt = svm_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03003507 .set_dr7 = svm_set_dr7,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003508 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003509 .get_rflags = svm_get_rflags,
3510 .set_rflags = svm_set_rflags,
Avi Kivity6b52d182010-01-21 15:31:47 +02003511 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02003512 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003513
Avi Kivity6aa8b732006-12-10 02:21:36 -08003514 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003515
Avi Kivity6aa8b732006-12-10 02:21:36 -08003516 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003517 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003518 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04003519 .set_interrupt_shadow = svm_set_interrupt_shadow,
3520 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02003521 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003522 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003523 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02003524 .queue_exception = svm_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02003525 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003526 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003527 .get_nmi_mask = svm_get_nmi_mask,
3528 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003529 .enable_nmi_window = enable_nmi_window,
3530 .enable_irq_window = enable_irq_window,
3531 .update_cr8_intercept = update_cr8_intercept,
Izik Eiduscbc94022007-10-25 00:29:55 +02003532
3533 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003534 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003535 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003536
3537 .exit_reasons_str = svm_exit_reasons_str,
Sheng Yang17cc3932010-01-05 19:02:27 +08003538 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08003539
3540 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003541
3542 .rdtscp_supported = svm_rdtscp_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003543
3544 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003545
3546 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10003547
3548 .write_tsc_offset = svm_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10003549 .adjust_tsc_offset = svm_adjust_tsc_offset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003550};
3551
3552static int __init svm_init(void)
3553{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003554 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03003555 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003556}
3557
3558static void __exit svm_exit(void)
3559{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003560 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003561}
3562
3563module_init(svm_init)
3564module_exit(svm_exit)