blob: 468ff6e721cedc72c82fecdac39328193a320372 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * AMD SVM support
5 *
6 * Copyright (C) 2006 Qumranet, Inc.
7 *
8 * Authors:
9 * Yaniv Kamay <yaniv@qumranet.com>
10 * Avi Kivity <avi@qumranet.com>
11 *
12 * This work is licensed under the terms of the GNU GPL, version 2. See
13 * the COPYING file in the top-level directory.
14 *
15 */
Avi Kivityedf88412007-12-16 11:02:48 +020016#include <linux/kvm_host.h>
17
Eddie Dong85f455f2007-07-06 12:20:49 +030018#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080019#include "mmu.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030020#include "kvm_cache_regs.h"
Gleb Natapovfe4c7b12009-03-23 11:23:18 +020021#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040022
Avi Kivity6aa8b732006-12-10 02:21:36 -080023#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020024#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/vmalloc.h>
26#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040027#include <linux/sched.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030028#include <linux/ftrace_event.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080030
Avi Kivitye4956062007-06-28 14:15:57 -040031#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080032
Eduardo Habkost63d11422008-11-17 19:03:20 -020033#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030034#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020035
Avi Kivity4ecac3f2008-05-13 13:23:38 +030036#define __ex(x) __kvm_handle_fault_on_reboot(x)
37
Avi Kivity6aa8b732006-12-10 02:21:36 -080038MODULE_AUTHOR("Qumranet");
39MODULE_LICENSE("GPL");
40
41#define IOPM_ALLOC_ORDER 2
42#define MSRPM_ALLOC_ORDER 1
43
Avi Kivity6aa8b732006-12-10 02:21:36 -080044#define SEG_TYPE_LDT 2
45#define SEG_TYPE_BUSY_TSS16 3
46
Joerg Roedel80b77062007-03-30 17:02:14 +030047#define SVM_FEATURE_NPT (1 << 0)
48#define SVM_FEATURE_LBRV (1 << 1)
Amit Shah94c935a12008-08-18 13:11:46 +030049#define SVM_FEATURE_SVML (1 << 2)
Jan Kiszka66b71382010-02-23 17:47:56 +010050#define SVM_FEATURE_NRIP (1 << 3)
Mark Langsdorf565d0992009-10-06 14:25:02 -050051#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030052
Joerg Roedel410e4d52009-08-07 11:49:44 +020053#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
54#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
55#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
56
Joerg Roedel24e09cb2008-02-13 18:58:47 +010057#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
58
Avi Kivity6c8166a2009-05-31 18:15:37 +030059static const u32 host_save_user_msrs[] = {
60#ifdef CONFIG_X86_64
61 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
62 MSR_FS_BASE,
63#endif
64 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
65};
66
67#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
68
69struct kvm_vcpu;
70
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020071struct nested_state {
72 struct vmcb *hsave;
73 u64 hsave_msr;
74 u64 vmcb;
75
76 /* These are the merged vectors */
77 u32 *msrpm;
78
79 /* gpa pointers to the real vectors */
80 u64 vmcb_msrpm;
Joerg Roedelaad42c62009-08-07 11:49:34 +020081
Joerg Roedelcd3ff652009-10-09 16:08:26 +020082 /* A VMEXIT is required but not yet emulated */
83 bool exit_required;
84
Joerg Roedelaad42c62009-08-07 11:49:34 +020085 /* cache for intercepts of the guest */
86 u16 intercept_cr_read;
87 u16 intercept_cr_write;
88 u16 intercept_dr_read;
89 u16 intercept_dr_write;
90 u32 intercept_exceptions;
91 u64 intercept;
92
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020093};
94
Avi Kivity6c8166a2009-05-31 18:15:37 +030095struct vcpu_svm {
96 struct kvm_vcpu vcpu;
97 struct vmcb *vmcb;
98 unsigned long vmcb_pa;
99 struct svm_cpu_data *svm_data;
100 uint64_t asid_generation;
101 uint64_t sysenter_esp;
102 uint64_t sysenter_eip;
103
104 u64 next_rip;
105
106 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
107 u64 host_gs_base;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300108
109 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300110
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200111 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200112
113 bool nmi_singlestep;
Jan Kiszka66b71382010-02-23 17:47:56 +0100114
115 unsigned int3_injected;
116 unsigned long int3_rip;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300117};
118
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100119/* enable NPT for AMD64 and X86 with PAE */
120#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
121static bool npt_enabled = true;
122#else
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100123static bool npt_enabled = false;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100124#endif
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100125static int npt = 1;
126
127module_param(npt, int, S_IRUGO);
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100128
Joerg Roedel4b6e4dc2009-08-07 11:49:48 +0200129static int nested = 1;
Alexander Graf236de052008-11-25 20:17:10 +0100130module_param(nested, int, S_IRUGO);
131
Joerg Roedel44874f82008-08-27 14:18:43 +0200132static void svm_flush_tlb(struct kvm_vcpu *vcpu);
Joerg Roedela5c38322009-08-07 11:49:32 +0200133static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity04d2cc72007-09-10 18:10:54 +0300134
Joerg Roedel410e4d52009-08-07 11:49:44 +0200135static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100136static int nested_svm_intercept(struct vcpu_svm *svm);
Alexander Grafcf74a782008-11-25 20:17:08 +0100137static int nested_svm_vmexit(struct vcpu_svm *svm);
Alexander Grafcf74a782008-11-25 20:17:08 +0100138static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
139 bool has_error_code, u32 error_code);
140
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400141static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
142{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000143 return container_of(vcpu, struct vcpu_svm, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400144}
145
Alexander Graf3d6368e2008-11-25 20:17:07 +0100146static inline bool is_nested(struct vcpu_svm *svm)
147{
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200148 return svm->nested.vmcb;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100149}
150
Joerg Roedel2af91942009-08-07 11:49:28 +0200151static inline void enable_gif(struct vcpu_svm *svm)
152{
153 svm->vcpu.arch.hflags |= HF_GIF_MASK;
154}
155
156static inline void disable_gif(struct vcpu_svm *svm)
157{
158 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
159}
160
161static inline bool gif_set(struct vcpu_svm *svm)
162{
163 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
164}
165
Harvey Harrison4866d5e2008-02-19 10:32:02 -0800166static unsigned long iopm_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800167
168struct kvm_ldttss_desc {
169 u16 limit0;
170 u16 base0;
171 unsigned base1 : 8, type : 5, dpl : 2, p : 1;
172 unsigned limit1 : 4, zero0 : 3, g : 1, base2 : 8;
173 u32 base3;
174 u32 zero1;
175} __attribute__((packed));
176
177struct svm_cpu_data {
178 int cpu;
179
Avi Kivity5008fdf2007-04-02 13:05:50 +0300180 u64 asid_generation;
181 u32 max_asid;
182 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800183 struct kvm_ldttss_desc *tss_desc;
184
185 struct page *save_area;
186};
187
188static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
Joerg Roedel80b77062007-03-30 17:02:14 +0300189static uint32_t svm_features;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800190
191struct svm_init_data {
192 int cpu;
193 int r;
194};
195
196static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
197
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200198#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800199#define MSRS_RANGE_SIZE 2048
200#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
201
202#define MAX_INST_SIZE 15
203
Joerg Roedel80b77062007-03-30 17:02:14 +0300204static inline u32 svm_has(u32 feat)
205{
206 return svm_features & feat;
207}
208
Avi Kivity6aa8b732006-12-10 02:21:36 -0800209static inline void clgi(void)
210{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300211 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800212}
213
214static inline void stgi(void)
215{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300216 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800217}
218
219static inline void invlpga(unsigned long addr, u32 asid)
220{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300221 asm volatile (__ex(SVM_INVLPGA) :: "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800222}
223
Avi Kivity6aa8b732006-12-10 02:21:36 -0800224static inline void force_new_asid(struct kvm_vcpu *vcpu)
225{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400226 to_svm(vcpu)->asid_generation--;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800227}
228
229static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
230{
231 force_new_asid(vcpu);
232}
233
234static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
235{
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100236 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600237 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800238
Alexander Graf9962d032008-11-25 20:17:02 +0100239 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Avi Kivityf6801df2010-01-21 15:31:50 +0200240 vcpu->arch.efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800241}
242
Avi Kivity6aa8b732006-12-10 02:21:36 -0800243static int is_external_interrupt(u32 info)
244{
245 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
246 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
247}
248
Glauber Costa2809f5d2009-05-12 16:21:05 -0400249static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
250{
251 struct vcpu_svm *svm = to_svm(vcpu);
252 u32 ret = 0;
253
254 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Jan Kiszka48005f62010-02-19 19:38:07 +0100255 ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400256 return ret & mask;
257}
258
259static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
260{
261 struct vcpu_svm *svm = to_svm(vcpu);
262
263 if (mask == 0)
264 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
265 else
266 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
267
268}
269
Avi Kivity6aa8b732006-12-10 02:21:36 -0800270static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
271{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400272 struct vcpu_svm *svm = to_svm(vcpu);
273
274 if (!svm->next_rip) {
Avi Kivity851ba692009-08-24 11:10:17 +0300275 if (emulate_instruction(vcpu, 0, 0, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300276 EMULATE_DONE)
277 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800278 return;
279 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300280 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
281 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
282 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800283
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300284 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400285 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800286}
287
Jan Kiszka116a4752010-02-23 17:47:54 +0100288static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
289 bool has_error_code, u32 error_code)
290{
291 struct vcpu_svm *svm = to_svm(vcpu);
292
293 /* If we are within a nested VM we'd better #VMEXIT and let the
294 guest handle the exception */
295 if (nested_svm_check_exception(svm, nr, has_error_code, error_code))
296 return;
297
Jan Kiszka66b71382010-02-23 17:47:56 +0100298 if (nr == BP_VECTOR && !svm_has(SVM_FEATURE_NRIP)) {
299 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
300
301 /*
302 * For guest debugging where we have to reinject #BP if some
303 * INT3 is guest-owned:
304 * Emulate nRIP by moving RIP forward. Will fail if injection
305 * raises a fault that is not intercepted. Still better than
306 * failing in all cases.
307 */
308 skip_emulated_instruction(&svm->vcpu);
309 rip = kvm_rip_read(&svm->vcpu);
310 svm->int3_rip = rip + svm->vmcb->save.cs.base;
311 svm->int3_injected = rip - old_rip;
312 }
313
Jan Kiszka116a4752010-02-23 17:47:54 +0100314 svm->vmcb->control.event_inj = nr
315 | SVM_EVTINJ_VALID
316 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
317 | SVM_EVTINJ_TYPE_EXEPT;
318 svm->vmcb->control.event_inj_err = error_code;
319}
320
Avi Kivity6aa8b732006-12-10 02:21:36 -0800321static int has_svm(void)
322{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200323 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800324
Eduardo Habkost63d11422008-11-17 19:03:20 -0200325 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800326 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800327 return 0;
328 }
329
Avi Kivity6aa8b732006-12-10 02:21:36 -0800330 return 1;
331}
332
333static void svm_hardware_disable(void *garbage)
334{
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200335 cpu_svm_disable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800336}
337
Alexander Graf10474ae2009-09-15 11:37:46 +0200338static int svm_hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800339{
340
Tejun Heo0fe1e002009-10-29 22:34:14 +0900341 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800342 uint64_t efer;
Gleb Natapov89a27f42010-02-16 10:51:48 +0200343 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800344 struct desc_struct *gdt;
345 int me = raw_smp_processor_id();
346
Alexander Graf10474ae2009-09-15 11:37:46 +0200347 rdmsrl(MSR_EFER, efer);
348 if (efer & EFER_SVME)
349 return -EBUSY;
350
Avi Kivity6aa8b732006-12-10 02:21:36 -0800351 if (!has_svm()) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000352 printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
353 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200354 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800355 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900356 sd = per_cpu(svm_data, me);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800357
Tejun Heo0fe1e002009-10-29 22:34:14 +0900358 if (!sd) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000359 printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -0800360 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200361 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800362 }
363
Tejun Heo0fe1e002009-10-29 22:34:14 +0900364 sd->asid_generation = 1;
365 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
366 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800367
Akinobu Mitab792c342009-07-19 00:00:01 +0900368 kvm_get_gdt(&gdt_descr);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200369 gdt = (struct desc_struct *)gdt_descr.address;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900370 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800371
Alexander Graf9962d032008-11-25 20:17:02 +0100372 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800373
Linus Torvaldsd0316552009-12-14 09:58:24 -0800374 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200375
376 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800377}
378
Joerg Roedel0da1db752008-07-02 16:02:11 +0200379static void svm_cpu_uninit(int cpu)
380{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900381 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200382
Tejun Heo0fe1e002009-10-29 22:34:14 +0900383 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200384 return;
385
386 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900387 __free_page(sd->save_area);
388 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200389}
390
Avi Kivity6aa8b732006-12-10 02:21:36 -0800391static int svm_cpu_init(int cpu)
392{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900393 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800394 int r;
395
Tejun Heo0fe1e002009-10-29 22:34:14 +0900396 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
397 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800398 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900399 sd->cpu = cpu;
400 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800401 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900402 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800403 goto err_1;
404
Tejun Heo0fe1e002009-10-29 22:34:14 +0900405 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800406
407 return 0;
408
409err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900410 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800411 return r;
412
413}
414
Rusty Russellbfc733a2007-07-31 20:42:42 +1000415static void set_msr_interception(u32 *msrpm, unsigned msr,
416 int read, int write)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800417{
418 int i;
419
420 for (i = 0; i < NUM_MSR_MAPS; i++) {
421 if (msr >= msrpm_ranges[i] &&
422 msr < msrpm_ranges[i] + MSRS_IN_RANGE) {
423 u32 msr_offset = (i * MSRS_IN_RANGE + msr -
424 msrpm_ranges[i]) * 2;
425
426 u32 *base = msrpm + (msr_offset / 32);
427 u32 msr_shift = msr_offset % 32;
428 u32 mask = ((write) ? 0 : 2) | ((read) ? 0 : 1);
429 *base = (*base & ~(0x3 << msr_shift)) |
430 (mask << msr_shift);
Rusty Russellbfc733a2007-07-31 20:42:42 +1000431 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800432 }
433 }
Rusty Russellbfc733a2007-07-31 20:42:42 +1000434 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800435}
436
Joerg Roedelf65c2292008-02-13 18:58:46 +0100437static void svm_vcpu_init_msrpm(u32 *msrpm)
438{
439 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
440
441#ifdef CONFIG_X86_64
442 set_msr_interception(msrpm, MSR_GS_BASE, 1, 1);
443 set_msr_interception(msrpm, MSR_FS_BASE, 1, 1);
444 set_msr_interception(msrpm, MSR_KERNEL_GS_BASE, 1, 1);
445 set_msr_interception(msrpm, MSR_LSTAR, 1, 1);
446 set_msr_interception(msrpm, MSR_CSTAR, 1, 1);
447 set_msr_interception(msrpm, MSR_SYSCALL_MASK, 1, 1);
448#endif
449 set_msr_interception(msrpm, MSR_K6_STAR, 1, 1);
450 set_msr_interception(msrpm, MSR_IA32_SYSENTER_CS, 1, 1);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100451}
452
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100453static void svm_enable_lbrv(struct vcpu_svm *svm)
454{
455 u32 *msrpm = svm->msrpm;
456
457 svm->vmcb->control.lbr_ctl = 1;
458 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
459 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
460 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
461 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
462}
463
464static void svm_disable_lbrv(struct vcpu_svm *svm)
465{
466 u32 *msrpm = svm->msrpm;
467
468 svm->vmcb->control.lbr_ctl = 0;
469 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
470 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
471 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
472 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
473}
474
Avi Kivity6aa8b732006-12-10 02:21:36 -0800475static __init int svm_hardware_setup(void)
476{
477 int cpu;
478 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100479 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800480 int r;
481
Avi Kivity6aa8b732006-12-10 02:21:36 -0800482 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
483
484 if (!iopm_pages)
485 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300486
487 iopm_va = page_address(iopm_pages);
488 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800489 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
490
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100491 if (boot_cpu_has(X86_FEATURE_NX))
492 kvm_enable_efer_bits(EFER_NX);
493
Alexander Graf1b2fd702009-02-02 16:23:51 +0100494 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
495 kvm_enable_efer_bits(EFER_FFXSR);
496
Alexander Graf236de052008-11-25 20:17:10 +0100497 if (nested) {
498 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
499 kvm_enable_efer_bits(EFER_SVME);
500 }
501
Zachary Amsden3230bb42009-09-29 11:38:37 -1000502 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800503 r = svm_cpu_init(cpu);
504 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100505 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800506 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100507
508 svm_features = cpuid_edx(SVM_CPUID_FUNC);
509
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100510 if (!svm_has(SVM_FEATURE_NPT))
511 npt_enabled = false;
512
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100513 if (npt_enabled && !npt) {
514 printk(KERN_INFO "kvm: Nested Paging disabled\n");
515 npt_enabled = false;
516 }
517
Joerg Roedel18552672008-02-07 13:47:41 +0100518 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100519 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100520 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200521 } else
522 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100523
Avi Kivity6aa8b732006-12-10 02:21:36 -0800524 return 0;
525
Joerg Roedelf65c2292008-02-13 18:58:46 +0100526err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800527 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
528 iopm_base = 0;
529 return r;
530}
531
532static __exit void svm_hardware_unsetup(void)
533{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200534 int cpu;
535
Zachary Amsden3230bb42009-09-29 11:38:37 -1000536 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200537 svm_cpu_uninit(cpu);
538
Avi Kivity6aa8b732006-12-10 02:21:36 -0800539 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100540 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800541}
542
543static void init_seg(struct vmcb_seg *seg)
544{
545 seg->selector = 0;
546 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
547 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
548 seg->limit = 0xffff;
549 seg->base = 0;
550}
551
552static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
553{
554 seg->selector = 0;
555 seg->attrib = SVM_SELECTOR_P_MASK | type;
556 seg->limit = 0xffff;
557 seg->base = 0;
558}
559
Joerg Roedele6101a92008-02-13 18:58:45 +0100560static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800561{
Joerg Roedele6101a92008-02-13 18:58:45 +0100562 struct vmcb_control_area *control = &svm->vmcb->control;
563 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800564
Avi Kivitybff78272010-01-07 13:16:08 +0200565 svm->vcpu.fpu_active = 1;
566
Avi Kivity6aa8b732006-12-10 02:21:36 -0800567 control->intercept_cr_read = INTERCEPT_CR0_MASK |
568 INTERCEPT_CR3_MASK |
Joerg Roedel649d6862008-04-16 16:51:15 +0200569 INTERCEPT_CR4_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800570
571 control->intercept_cr_write = INTERCEPT_CR0_MASK |
572 INTERCEPT_CR3_MASK |
Avi Kivity80a81192007-12-06 19:50:00 +0200573 INTERCEPT_CR4_MASK |
574 INTERCEPT_CR8_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800575
576 control->intercept_dr_read = INTERCEPT_DR0_MASK |
577 INTERCEPT_DR1_MASK |
578 INTERCEPT_DR2_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100579 INTERCEPT_DR3_MASK |
580 INTERCEPT_DR4_MASK |
581 INTERCEPT_DR5_MASK |
582 INTERCEPT_DR6_MASK |
583 INTERCEPT_DR7_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800584
585 control->intercept_dr_write = INTERCEPT_DR0_MASK |
586 INTERCEPT_DR1_MASK |
587 INTERCEPT_DR2_MASK |
588 INTERCEPT_DR3_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100589 INTERCEPT_DR4_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800590 INTERCEPT_DR5_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100591 INTERCEPT_DR6_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800592 INTERCEPT_DR7_MASK;
593
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500594 control->intercept_exceptions = (1 << PF_VECTOR) |
Joerg Roedel53371b52008-04-09 14:15:30 +0200595 (1 << UD_VECTOR) |
596 (1 << MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800597
598
599 control->intercept = (1ULL << INTERCEPT_INTR) |
600 (1ULL << INTERCEPT_NMI) |
Joerg Roedel01525272007-02-19 14:37:47 +0200601 (1ULL << INTERCEPT_SMI) |
Avi Kivityd2251572010-01-06 10:55:27 +0200602 (1ULL << INTERCEPT_SELECTIVE_CR0) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800603 (1ULL << INTERCEPT_CPUID) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200604 (1ULL << INTERCEPT_INVD) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800605 (1ULL << INTERCEPT_HLT) |
Marcelo Tosattia7052892008-09-23 13:18:35 -0300606 (1ULL << INTERCEPT_INVLPG) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800607 (1ULL << INTERCEPT_INVLPGA) |
608 (1ULL << INTERCEPT_IOIO_PROT) |
609 (1ULL << INTERCEPT_MSR_PROT) |
610 (1ULL << INTERCEPT_TASK_SWITCH) |
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800611 (1ULL << INTERCEPT_SHUTDOWN) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800612 (1ULL << INTERCEPT_VMRUN) |
613 (1ULL << INTERCEPT_VMMCALL) |
614 (1ULL << INTERCEPT_VMLOAD) |
615 (1ULL << INTERCEPT_VMSAVE) |
616 (1ULL << INTERCEPT_STGI) |
617 (1ULL << INTERCEPT_CLGI) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100618 (1ULL << INTERCEPT_SKINIT) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200619 (1ULL << INTERCEPT_WBINVD) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100620 (1ULL << INTERCEPT_MONITOR) |
621 (1ULL << INTERCEPT_MWAIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800622
623 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100624 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity0cc50642007-03-25 12:07:27 +0200625 control->tsc_offset = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800626 control->int_ctl = V_INTR_MASKING_MASK;
627
628 init_seg(&save->es);
629 init_seg(&save->ss);
630 init_seg(&save->ds);
631 init_seg(&save->fs);
632 init_seg(&save->gs);
633
634 save->cs.selector = 0xf000;
635 /* Executable/Readable Code Segment */
636 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
637 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
638 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800639 /*
640 * cs.base should really be 0xffff0000, but vmx can't handle that, so
641 * be consistent with it.
642 *
643 * Replace when we have real mode working for vmx.
644 */
645 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800646
647 save->gdtr.limit = 0xffff;
648 save->idtr.limit = 0xffff;
649
650 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
651 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
652
Alexander Graf9962d032008-11-25 20:17:02 +0100653 save->efer = EFER_SVME;
Mike Dayd77c26f2007-10-08 09:02:08 -0400654 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800655 save->dr7 = 0x400;
656 save->rflags = 2;
657 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300658 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800659
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200660 /* This is the guest-visible cr0 value.
661 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800662 */
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200663 svm->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
664 kvm_set_cr0(&svm->vcpu, svm->vcpu.arch.cr0);
665
Rusty Russell66aee912007-07-17 23:34:16 +1000666 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800667 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100668
669 if (npt_enabled) {
670 /* Setup VMCB for Nested Paging */
671 control->nested_ctl = 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -0300672 control->intercept &= ~((1ULL << INTERCEPT_TASK_SWITCH) |
673 (1ULL << INTERCEPT_INVLPG));
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100674 control->intercept_exceptions &= ~(1 << PF_VECTOR);
Avi Kivity888f9f32010-01-10 12:14:04 +0200675 control->intercept_cr_read &= ~INTERCEPT_CR3_MASK;
676 control->intercept_cr_write &= ~INTERCEPT_CR3_MASK;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100677 save->g_pat = 0x0007040600070406ULL;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100678 save->cr3 = 0;
679 save->cr4 = 0;
680 }
Avi Kivitya79d2f12008-04-14 13:10:21 +0300681 force_new_asid(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +0100682
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200683 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +0200684 svm->vcpu.arch.hflags = 0;
685
Mark Langsdorf565d0992009-10-06 14:25:02 -0500686 if (svm_has(SVM_FEATURE_PAUSE_FILTER)) {
687 control->pause_filter_count = 3000;
688 control->intercept |= (1ULL << INTERCEPT_PAUSE);
689 }
690
Joerg Roedel2af91942009-08-07 11:49:28 +0200691 enable_gif(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800692}
693
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200694static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +0300695{
696 struct vcpu_svm *svm = to_svm(vcpu);
697
Joerg Roedele6101a92008-02-13 18:58:45 +0100698 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +0200699
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300700 if (!kvm_vcpu_is_bsp(vcpu)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300701 kvm_rip_write(vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800702 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
703 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +0200704 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300705 vcpu->arch.regs_avail = ~0;
706 vcpu->arch.regs_dirty = ~0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200707
708 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300709}
710
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000711static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800712{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400713 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800714 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100715 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +0100716 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100717 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000718 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800719
Rusty Russellc16f8622007-07-30 21:12:19 +1000720 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000721 if (!svm) {
722 err = -ENOMEM;
723 goto out;
724 }
725
726 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
727 if (err)
728 goto free_svm;
729
Joerg Roedelf65c2292008-02-13 18:58:46 +0100730 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900731 page = alloc_page(GFP_KERNEL);
732 if (!page)
733 goto uninit;
734
Joerg Roedelf65c2292008-02-13 18:58:46 +0100735 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
736 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900737 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100738
739 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
740 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900741 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100742
Alexander Grafb286d5d2008-11-25 20:17:05 +0100743 hsave_page = alloc_page(GFP_KERNEL);
744 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900745 goto free_page3;
746
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200747 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +0100748
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900749 svm->msrpm = page_address(msrpm_pages);
750 svm_vcpu_init_msrpm(svm->msrpm);
751
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200752 svm->nested.msrpm = page_address(nested_msrpm_pages);
Alexander Graf3d6368e2008-11-25 20:17:07 +0100753
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400754 svm->vmcb = page_address(page);
755 clear_page(svm->vmcb);
756 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
757 svm->asid_generation = 0;
Joerg Roedele6101a92008-02-13 18:58:45 +0100758 init_vmcb(svm);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400759
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000760 fx_init(&svm->vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800761 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300762 if (kvm_vcpu_is_bsp(&svm->vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800763 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800764
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000765 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -0800766
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900767free_page3:
768 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
769free_page2:
770 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
771free_page1:
772 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000773uninit:
774 kvm_vcpu_uninit(&svm->vcpu);
775free_svm:
Rusty Russella4770342007-08-01 14:46:11 +1000776 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000777out:
778 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800779}
780
781static void svm_free_vcpu(struct kvm_vcpu *vcpu)
782{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400783 struct vcpu_svm *svm = to_svm(vcpu);
784
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000785 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +0100786 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200787 __free_page(virt_to_page(svm->nested.hsave));
788 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000789 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +1000790 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800791}
792
Avi Kivity15ad7142007-07-11 18:17:21 +0300793static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800794{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400795 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300796 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +0200797
Avi Kivity0cc50642007-03-25 12:07:27 +0200798 if (unlikely(cpu != vcpu->cpu)) {
Joerg Roedele935d482009-09-16 15:24:19 +0200799 u64 delta;
Avi Kivity0cc50642007-03-25 12:07:27 +0200800
Joerg Roedel953899b2009-12-14 12:22:20 +0100801 if (check_tsc_unstable()) {
802 /*
803 * Make sure that the guest sees a monotonically
804 * increasing TSC.
805 */
806 delta = vcpu->arch.host_tsc - native_read_tsc();
807 svm->vmcb->control.tsc_offset += delta;
808 if (is_nested(svm))
809 svm->nested.hsave->control.tsc_offset += delta;
810 }
Avi Kivity0cc50642007-03-25 12:07:27 +0200811 vcpu->cpu = cpu;
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300812 kvm_migrate_timers(vcpu);
Marcelo Tosatti4b656b12009-07-21 12:47:45 -0300813 svm->asid_generation = 0;
Avi Kivity0cc50642007-03-25 12:07:27 +0200814 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300815
816 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400817 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800818}
819
820static void svm_vcpu_put(struct kvm_vcpu *vcpu)
821{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400822 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300823 int i;
824
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200825 ++vcpu->stat.host_state_reload;
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300826 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400827 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300828
Joerg Roedele935d482009-09-16 15:24:19 +0200829 vcpu->arch.host_tsc = native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800830}
831
Avi Kivity6aa8b732006-12-10 02:21:36 -0800832static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
833{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400834 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800835}
836
837static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
838{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400839 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800840}
841
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300842static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
843{
844 switch (reg) {
845 case VCPU_EXREG_PDPTR:
846 BUG_ON(!npt_enabled);
847 load_pdptrs(vcpu, vcpu->arch.cr3);
848 break;
849 default:
850 BUG();
851 }
852}
853
Alexander Graff0b85052008-11-25 20:17:01 +0100854static void svm_set_vintr(struct vcpu_svm *svm)
855{
856 svm->vmcb->control.intercept |= 1ULL << INTERCEPT_VINTR;
857}
858
859static void svm_clear_vintr(struct vcpu_svm *svm)
860{
861 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
862}
863
Avi Kivity6aa8b732006-12-10 02:21:36 -0800864static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
865{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400866 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800867
868 switch (seg) {
869 case VCPU_SREG_CS: return &save->cs;
870 case VCPU_SREG_DS: return &save->ds;
871 case VCPU_SREG_ES: return &save->es;
872 case VCPU_SREG_FS: return &save->fs;
873 case VCPU_SREG_GS: return &save->gs;
874 case VCPU_SREG_SS: return &save->ss;
875 case VCPU_SREG_TR: return &save->tr;
876 case VCPU_SREG_LDTR: return &save->ldtr;
877 }
878 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +0000879 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800880}
881
882static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
883{
884 struct vmcb_seg *s = svm_seg(vcpu, seg);
885
886 return s->base;
887}
888
889static void svm_get_segment(struct kvm_vcpu *vcpu,
890 struct kvm_segment *var, int seg)
891{
892 struct vmcb_seg *s = svm_seg(vcpu, seg);
893
894 var->base = s->base;
895 var->limit = s->limit;
896 var->selector = s->selector;
897 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
898 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
899 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
900 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
901 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
902 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
903 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
904 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
Amit Shah25022ac2008-10-27 09:04:17 +0000905
Andre Przywara19bca6a2009-04-28 12:45:30 +0200906 /* AMD's VMCB does not have an explicit unusable field, so emulate it
907 * for cross vendor migration purposes by "not present"
908 */
909 var->unusable = !var->present || (var->type == 0);
910
Andre Przywara1fbdc7a2009-01-11 22:39:44 +0100911 switch (seg) {
912 case VCPU_SREG_CS:
913 /*
914 * SVM always stores 0 for the 'G' bit in the CS selector in
915 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
916 * Intel's VMENTRY has a check on the 'G' bit.
917 */
Amit Shah25022ac2008-10-27 09:04:17 +0000918 var->g = s->limit > 0xfffff;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +0100919 break;
920 case VCPU_SREG_TR:
921 /*
922 * Work around a bug where the busy flag in the tr selector
923 * isn't exposed
924 */
Amit Shahc0d09822008-10-27 09:04:18 +0000925 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +0100926 break;
927 case VCPU_SREG_DS:
928 case VCPU_SREG_ES:
929 case VCPU_SREG_FS:
930 case VCPU_SREG_GS:
931 /*
932 * The accessed bit must always be set in the segment
933 * descriptor cache, although it can be cleared in the
934 * descriptor, the cached bit always remains at 1. Since
935 * Intel has a check on this, set it here to support
936 * cross-vendor migration.
937 */
938 if (!var->unusable)
939 var->type |= 0x1;
940 break;
Andre Przywarab586eb02009-04-28 12:45:43 +0200941 case VCPU_SREG_SS:
942 /* On AMD CPUs sometimes the DB bit in the segment
943 * descriptor is left as 1, although the whole segment has
944 * been made unusable. Clear it here to pass an Intel VMX
945 * entry check when cross vendor migrating.
946 */
947 if (var->unusable)
948 var->db = 0;
949 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +0100950 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951}
952
Izik Eidus2e4d2652008-03-24 19:38:34 +0200953static int svm_get_cpl(struct kvm_vcpu *vcpu)
954{
955 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
956
957 return save->cpl;
958}
959
Gleb Natapov89a27f42010-02-16 10:51:48 +0200960static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800961{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400962 struct vcpu_svm *svm = to_svm(vcpu);
963
Gleb Natapov89a27f42010-02-16 10:51:48 +0200964 dt->size = svm->vmcb->save.idtr.limit;
965 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966}
967
Gleb Natapov89a27f42010-02-16 10:51:48 +0200968static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400970 struct vcpu_svm *svm = to_svm(vcpu);
971
Gleb Natapov89a27f42010-02-16 10:51:48 +0200972 svm->vmcb->save.idtr.limit = dt->size;
973 svm->vmcb->save.idtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974}
975
Gleb Natapov89a27f42010-02-16 10:51:48 +0200976static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800977{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400978 struct vcpu_svm *svm = to_svm(vcpu);
979
Gleb Natapov89a27f42010-02-16 10:51:48 +0200980 dt->size = svm->vmcb->save.gdtr.limit;
981 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982}
983
Gleb Natapov89a27f42010-02-16 10:51:48 +0200984static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400986 struct vcpu_svm *svm = to_svm(vcpu);
987
Gleb Natapov89a27f42010-02-16 10:51:48 +0200988 svm->vmcb->save.gdtr.limit = dt->size;
989 svm->vmcb->save.gdtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800990}
991
Avi Kivitye8467fd2009-12-29 18:43:06 +0200992static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
993{
994}
995
Anthony Liguori25c4c272007-04-27 09:29:21 +0300996static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -0800997{
998}
999
Avi Kivityd2251572010-01-06 10:55:27 +02001000static void update_cr0_intercept(struct vcpu_svm *svm)
1001{
Joerg Roedel66a562f2010-02-19 16:23:08 +01001002 struct vmcb *vmcb = svm->vmcb;
Avi Kivityd2251572010-01-06 10:55:27 +02001003 ulong gcr0 = svm->vcpu.arch.cr0;
1004 u64 *hcr0 = &svm->vmcb->save.cr0;
1005
1006 if (!svm->vcpu.fpu_active)
1007 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1008 else
1009 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1010 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1011
1012
1013 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Joerg Roedel66a562f2010-02-19 16:23:08 +01001014 vmcb->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1015 vmcb->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1016 if (is_nested(svm)) {
1017 struct vmcb *hsave = svm->nested.hsave;
1018
1019 hsave->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1020 hsave->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1021 vmcb->control.intercept_cr_read |= svm->nested.intercept_cr_read;
1022 vmcb->control.intercept_cr_write |= svm->nested.intercept_cr_write;
1023 }
Avi Kivityd2251572010-01-06 10:55:27 +02001024 } else {
1025 svm->vmcb->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1026 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001027 if (is_nested(svm)) {
1028 struct vmcb *hsave = svm->nested.hsave;
1029
1030 hsave->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1031 hsave->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
1032 }
Avi Kivityd2251572010-01-06 10:55:27 +02001033 }
1034}
1035
Avi Kivity6aa8b732006-12-10 02:21:36 -08001036static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1037{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001038 struct vcpu_svm *svm = to_svm(vcpu);
1039
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001040#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001041 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001042 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001043 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001044 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001045 }
1046
Mike Dayd77c26f2007-10-08 09:02:08 -04001047 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001048 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001049 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001050 }
1051 }
1052#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001053 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001054
1055 if (!npt_enabled)
1056 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001057
1058 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001059 cr0 |= X86_CR0_TS;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001060 /*
1061 * re-enable caching here because the QEMU bios
1062 * does not do it - this results in some delay at
1063 * reboot
1064 */
1065 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001066 svm->vmcb->save.cr0 = cr0;
Avi Kivityd2251572010-01-06 10:55:27 +02001067 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001068}
1069
1070static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1071{
Joerg Roedel6394b642008-04-09 14:15:29 +02001072 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001073 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1074
1075 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
1076 force_new_asid(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001077
Joerg Roedelec077262008-04-09 14:15:28 +02001078 vcpu->arch.cr4 = cr4;
1079 if (!npt_enabled)
1080 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001081 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001082 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001083}
1084
1085static void svm_set_segment(struct kvm_vcpu *vcpu,
1086 struct kvm_segment *var, int seg)
1087{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001088 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089 struct vmcb_seg *s = svm_seg(vcpu, seg);
1090
1091 s->base = var->base;
1092 s->limit = var->limit;
1093 s->selector = var->selector;
1094 if (var->unusable)
1095 s->attrib = 0;
1096 else {
1097 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1098 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1099 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1100 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1101 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1102 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1103 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1104 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1105 }
1106 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001107 svm->vmcb->save.cpl
1108 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -08001109 >> SVM_SELECTOR_DPL_SHIFT) & 3;
1110
1111}
1112
Gleb Natapov44c11432009-05-11 13:35:52 +03001113static void update_db_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001114{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001115 struct vcpu_svm *svm = to_svm(vcpu);
1116
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001117 svm->vmcb->control.intercept_exceptions &=
1118 ~((1 << DB_VECTOR) | (1 << BP_VECTOR));
Gleb Natapov44c11432009-05-11 13:35:52 +03001119
Jan Kiszka6be7d302009-10-18 13:24:54 +02001120 if (svm->nmi_singlestep)
Gleb Natapov44c11432009-05-11 13:35:52 +03001121 svm->vmcb->control.intercept_exceptions |= (1 << DB_VECTOR);
1122
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001123 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1124 if (vcpu->guest_debug &
1125 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
1126 svm->vmcb->control.intercept_exceptions |=
1127 1 << DB_VECTOR;
1128 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
1129 svm->vmcb->control.intercept_exceptions |=
1130 1 << BP_VECTOR;
1131 } else
1132 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001133}
1134
Jan Kiszka355be0b2009-10-03 00:31:21 +02001135static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Gleb Natapov44c11432009-05-11 13:35:52 +03001136{
Gleb Natapov44c11432009-05-11 13:35:52 +03001137 struct vcpu_svm *svm = to_svm(vcpu);
1138
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001139 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1140 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1141 else
1142 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1143
Jan Kiszka355be0b2009-10-03 00:31:21 +02001144 update_db_intercept(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001145}
1146
1147static void load_host_msrs(struct kvm_vcpu *vcpu)
1148{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001149#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001150 wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001151#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001152}
1153
1154static void save_host_msrs(struct kvm_vcpu *vcpu)
1155{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001156#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001157 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001158#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159}
1160
Tejun Heo0fe1e002009-10-29 22:34:14 +09001161static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001162{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001163 if (sd->next_asid > sd->max_asid) {
1164 ++sd->asid_generation;
1165 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001166 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001167 }
1168
Tejun Heo0fe1e002009-10-29 22:34:14 +09001169 svm->asid_generation = sd->asid_generation;
1170 svm->vmcb->control.asid = sd->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001171}
1172
Jan Kiszkac76de352010-01-20 18:20:20 +01001173static int svm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *dest)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001174{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001175 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001176
1177 switch (dr) {
1178 case 0 ... 3:
Jan Kiszkac76de352010-01-20 18:20:20 +01001179 *dest = vcpu->arch.db[dr];
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001180 break;
Jan Kiszkac76de352010-01-20 18:20:20 +01001181 case 4:
1182 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
1183 return EMULATE_FAIL; /* will re-inject UD */
1184 /* fall through */
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001185 case 6:
1186 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
Jan Kiszkac76de352010-01-20 18:20:20 +01001187 *dest = vcpu->arch.dr6;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001188 else
Jan Kiszkac76de352010-01-20 18:20:20 +01001189 *dest = svm->vmcb->save.dr6;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001190 break;
Jan Kiszkac76de352010-01-20 18:20:20 +01001191 case 5:
1192 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
1193 return EMULATE_FAIL; /* will re-inject UD */
1194 /* fall through */
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001195 case 7:
1196 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
Jan Kiszkac76de352010-01-20 18:20:20 +01001197 *dest = vcpu->arch.dr7;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001198 else
Jan Kiszkac76de352010-01-20 18:20:20 +01001199 *dest = svm->vmcb->save.dr7;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001200 break;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001201 }
1202
Jan Kiszkac76de352010-01-20 18:20:20 +01001203 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001204}
1205
Jan Kiszkac76de352010-01-20 18:20:20 +01001206static int svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001207{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001208 struct vcpu_svm *svm = to_svm(vcpu);
1209
Avi Kivity6aa8b732006-12-10 02:21:36 -08001210 switch (dr) {
1211 case 0 ... 3:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001212 vcpu->arch.db[dr] = value;
1213 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1214 vcpu->arch.eff_db[dr] = value;
Jan Kiszkac76de352010-01-20 18:20:20 +01001215 break;
1216 case 4:
1217 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
1218 return EMULATE_FAIL; /* will re-inject UD */
1219 /* fall through */
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001220 case 6:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001221 vcpu->arch.dr6 = (value & DR6_VOLATILE) | DR6_FIXED_1;
Jan Kiszkac76de352010-01-20 18:20:20 +01001222 break;
1223 case 5:
1224 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
1225 return EMULATE_FAIL; /* will re-inject UD */
1226 /* fall through */
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001227 case 7:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001228 vcpu->arch.dr7 = (value & DR7_VOLATILE) | DR7_FIXED_1;
1229 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
1230 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1231 vcpu->arch.switch_db_regs = (value & DR7_BP_EN_MASK);
1232 }
Jan Kiszkac76de352010-01-20 18:20:20 +01001233 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001234 }
Jan Kiszkac76de352010-01-20 18:20:20 +01001235
1236 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001237}
1238
Avi Kivity851ba692009-08-24 11:10:17 +03001239static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001240{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001241 u64 fault_address;
1242 u32 error_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001243
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001244 fault_address = svm->vmcb->control.exit_info_2;
1245 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001246
Marcelo Tosatti229456f2009-06-17 09:22:14 -03001247 trace_kvm_page_fault(fault_address, error_code);
Avi Kivity52c78472009-08-10 15:42:41 +03001248 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1249 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
Avi Kivity30677142007-10-28 18:48:59 +02001250 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001251}
1252
Avi Kivity851ba692009-08-24 11:10:17 +03001253static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001254{
Avi Kivity851ba692009-08-24 11:10:17 +03001255 struct kvm_run *kvm_run = svm->vcpu.run;
1256
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001257 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001258 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001259 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001260 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1261 return 1;
1262 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001263
Jan Kiszka6be7d302009-10-18 13:24:54 +02001264 if (svm->nmi_singlestep) {
1265 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001266 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1267 svm->vmcb->save.rflags &=
1268 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1269 update_db_intercept(&svm->vcpu);
1270 }
1271
1272 if (svm->vcpu.guest_debug &
1273 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)){
1274 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1275 kvm_run->debug.arch.pc =
1276 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1277 kvm_run->debug.arch.exception = DB_VECTOR;
1278 return 0;
1279 }
1280
1281 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001282}
1283
Avi Kivity851ba692009-08-24 11:10:17 +03001284static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001285{
Avi Kivity851ba692009-08-24 11:10:17 +03001286 struct kvm_run *kvm_run = svm->vcpu.run;
1287
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001288 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1289 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1290 kvm_run->debug.arch.exception = BP_VECTOR;
1291 return 0;
1292}
1293
Avi Kivity851ba692009-08-24 11:10:17 +03001294static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001295{
1296 int er;
1297
Avi Kivity851ba692009-08-24 11:10:17 +03001298 er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001299 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001300 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001301 return 1;
1302}
1303
Avi Kivity6b52d182010-01-21 15:31:47 +02001304static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001305{
Avi Kivity6b52d182010-01-21 15:31:47 +02001306 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001307 u32 excp;
1308
1309 if (is_nested(svm)) {
1310 u32 h_excp, n_excp;
1311
1312 h_excp = svm->nested.hsave->control.intercept_exceptions;
1313 n_excp = svm->nested.intercept_exceptions;
1314 h_excp &= ~(1 << NM_VECTOR);
1315 excp = h_excp | n_excp;
1316 } else {
1317 excp = svm->vmcb->control.intercept_exceptions;
1318 excp &= ~(1 << NM_VECTOR);
1319 }
1320
1321 svm->vmcb->control.intercept_exceptions = excp;
1322
Rusty Russelle756fc62007-07-30 20:07:08 +10001323 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001324 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001325}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001326
Avi Kivity6b52d182010-01-21 15:31:47 +02001327static int nm_interception(struct vcpu_svm *svm)
1328{
1329 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001330 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001331}
1332
Avi Kivity851ba692009-08-24 11:10:17 +03001333static int mc_interception(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001334{
1335 /*
1336 * On an #MC intercept the MCE handler is not called automatically in
1337 * the host. So do it by hand here.
1338 */
1339 asm volatile (
1340 "int $0x12\n");
1341 /* not sure if we ever come back to this point */
1342
1343 return 1;
1344}
1345
Avi Kivity851ba692009-08-24 11:10:17 +03001346static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001347{
Avi Kivity851ba692009-08-24 11:10:17 +03001348 struct kvm_run *kvm_run = svm->vcpu.run;
1349
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001350 /*
1351 * VMCB is undefined after a SHUTDOWN intercept
1352 * so reinitialize it.
1353 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001354 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001355 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001356
1357 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1358 return 0;
1359}
1360
Avi Kivity851ba692009-08-24 11:10:17 +03001361static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001362{
Mike Dayd77c26f2007-10-08 09:02:08 -04001363 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001364 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001365 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366
Rusty Russelle756fc62007-07-30 20:07:08 +10001367 ++svm->vcpu.stat.io_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001368
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001369 svm->next_rip = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001370
Laurent Viviere70669a2007-08-05 10:36:40 +03001371 string = (io_info & SVM_IOIO_STR_MASK) != 0;
1372
1373 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02001374 if (emulate_instruction(&svm->vcpu,
Avi Kivity851ba692009-08-24 11:10:17 +03001375 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03001376 return 0;
1377 return 1;
1378 }
1379
Avi Kivity039576c2007-03-20 12:46:50 +02001380 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
1381 port = io_info >> 16;
1382 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001383
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001384 skip_emulated_instruction(&svm->vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03001385 return kvm_emulate_pio(&svm->vcpu, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001386}
1387
Avi Kivity851ba692009-08-24 11:10:17 +03001388static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001389{
1390 return 1;
1391}
1392
Avi Kivity851ba692009-08-24 11:10:17 +03001393static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001394{
1395 ++svm->vcpu.stat.irq_exits;
1396 return 1;
1397}
1398
Avi Kivity851ba692009-08-24 11:10:17 +03001399static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001400{
1401 return 1;
1402}
1403
Avi Kivity851ba692009-08-24 11:10:17 +03001404static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001405{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001406 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001407 skip_emulated_instruction(&svm->vcpu);
1408 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001409}
1410
Avi Kivity851ba692009-08-24 11:10:17 +03001411static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001412{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001413 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001414 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001415 kvm_emulate_hypercall(&svm->vcpu);
1416 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001417}
1418
Alexander Grafc0725422008-11-25 20:17:03 +01001419static int nested_svm_check_permissions(struct vcpu_svm *svm)
1420{
Avi Kivityf6801df2010-01-21 15:31:50 +02001421 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01001422 || !is_paging(&svm->vcpu)) {
1423 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1424 return 1;
1425 }
1426
1427 if (svm->vmcb->save.cpl) {
1428 kvm_inject_gp(&svm->vcpu, 0);
1429 return 1;
1430 }
1431
1432 return 0;
1433}
1434
Alexander Grafcf74a782008-11-25 20:17:08 +01001435static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
1436 bool has_error_code, u32 error_code)
1437{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001438 int vmexit;
1439
Joerg Roedel0295ad72009-08-07 11:49:37 +02001440 if (!is_nested(svm))
1441 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001442
Joerg Roedel0295ad72009-08-07 11:49:37 +02001443 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1444 svm->vmcb->control.exit_code_hi = 0;
1445 svm->vmcb->control.exit_info_1 = error_code;
1446 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1447
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001448 vmexit = nested_svm_intercept(svm);
1449 if (vmexit == NESTED_EXIT_DONE)
1450 svm->nested.exit_required = true;
1451
1452 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001453}
1454
Joerg Roedel8fe54652010-02-19 16:23:01 +01001455/* This function returns true if it is save to enable the irq window */
1456static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001457{
Joerg Roedel26666952009-08-07 11:49:46 +02001458 if (!is_nested(svm))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001459 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001460
Joerg Roedel26666952009-08-07 11:49:46 +02001461 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001462 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001463
Joerg Roedel26666952009-08-07 11:49:46 +02001464 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001465 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001466
Joerg Roedel26666952009-08-07 11:49:46 +02001467 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
1468
Joerg Roedelcd3ff652009-10-09 16:08:26 +02001469 if (svm->nested.intercept & 1ULL) {
1470 /*
1471 * The #vmexit can't be emulated here directly because this
1472 * code path runs with irqs and preemtion disabled. A
1473 * #vmexit emulation might sleep. Only signal request for
1474 * the #vmexit here.
1475 */
1476 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02001477 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01001478 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001479 }
1480
Joerg Roedel8fe54652010-02-19 16:23:01 +01001481 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001482}
1483
Joerg Roedel7597f122010-02-19 16:23:00 +01001484static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001485{
1486 struct page *page;
1487
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01001488 might_sleep();
1489
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001490 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001491 if (is_error_page(page))
1492 goto error;
1493
Joerg Roedel7597f122010-02-19 16:23:00 +01001494 *_page = page;
1495
1496 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001497
1498error:
1499 kvm_release_page_clean(page);
1500 kvm_inject_gp(&svm->vcpu, 0);
1501
1502 return NULL;
1503}
1504
Joerg Roedel7597f122010-02-19 16:23:00 +01001505static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001506{
Joerg Roedel7597f122010-02-19 16:23:00 +01001507 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001508 kvm_release_page_dirty(page);
1509}
1510
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001511static bool nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001512{
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001513 u32 param = svm->vmcb->control.exit_info_1 & 1;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001514 u32 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1515 bool ret = false;
1516 u32 t0, t1;
Joerg Roedel4c7da8c2010-02-19 16:23:05 +01001517 u8 val;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001518
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001519 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
1520 return false;
1521
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001522 switch (msr) {
1523 case 0 ... 0x1fff:
1524 t0 = (msr * 2) % 8;
1525 t1 = msr / 8;
1526 break;
1527 case 0xc0000000 ... 0xc0001fff:
1528 t0 = (8192 + msr - 0xc0000000) * 2;
1529 t1 = (t0 / 8);
1530 t0 %= 8;
1531 break;
1532 case 0xc0010000 ... 0xc0011fff:
1533 t0 = (16384 + msr - 0xc0010000) * 2;
1534 t1 = (t0 / 8);
1535 t0 %= 8;
1536 break;
1537 default:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001538 ret = true;
1539 goto out;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001540 }
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001541
Joerg Roedel4c7da8c2010-02-19 16:23:05 +01001542 if (!kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + t1, &val, 1))
1543 ret = val & ((1 << param) << t0);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001544
1545out:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001546 return ret;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001547}
1548
Joerg Roedel410e4d52009-08-07 11:49:44 +02001549static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001550{
Alexander Grafcf74a782008-11-25 20:17:08 +01001551 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001552
Joerg Roedel410e4d52009-08-07 11:49:44 +02001553 switch (exit_code) {
1554 case SVM_EXIT_INTR:
1555 case SVM_EXIT_NMI:
1556 return NESTED_EXIT_HOST;
Alexander Grafcf74a782008-11-25 20:17:08 +01001557 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001558 case SVM_EXIT_NPF:
1559 if (npt_enabled)
1560 return NESTED_EXIT_HOST;
1561 break;
1562 /* When we're shadowing, trap PFs */
1563 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
1564 if (!npt_enabled)
1565 return NESTED_EXIT_HOST;
1566 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001567 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
1568 nm_interception(svm);
1569 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001570 default:
1571 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001572 }
1573
Joerg Roedel410e4d52009-08-07 11:49:44 +02001574 return NESTED_EXIT_CONTINUE;
1575}
1576
1577/*
1578 * If this function returns true, this #vmexit was already handled
1579 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001580static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001581{
1582 u32 exit_code = svm->vmcb->control.exit_code;
1583 int vmexit = NESTED_EXIT_HOST;
1584
Alexander Grafcf74a782008-11-25 20:17:08 +01001585 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001586 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001587 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001588 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001589 case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR8: {
1590 u32 cr_bits = 1 << (exit_code - SVM_EXIT_READ_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001591 if (svm->nested.intercept_cr_read & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001592 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001593 break;
1594 }
1595 case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR8: {
1596 u32 cr_bits = 1 << (exit_code - SVM_EXIT_WRITE_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001597 if (svm->nested.intercept_cr_write & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001598 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001599 break;
1600 }
1601 case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR7: {
1602 u32 dr_bits = 1 << (exit_code - SVM_EXIT_READ_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001603 if (svm->nested.intercept_dr_read & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001604 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001605 break;
1606 }
1607 case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR7: {
1608 u32 dr_bits = 1 << (exit_code - SVM_EXIT_WRITE_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001609 if (svm->nested.intercept_dr_write & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001610 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001611 break;
1612 }
1613 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1614 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001615 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001616 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001617 break;
1618 }
1619 default: {
1620 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001621 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001622 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001623 }
1624 }
1625
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001626 return vmexit;
1627}
1628
1629static int nested_svm_exit_handled(struct vcpu_svm *svm)
1630{
1631 int vmexit;
1632
1633 vmexit = nested_svm_intercept(svm);
1634
1635 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001636 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001637
1638 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001639}
1640
Joerg Roedel0460a972009-08-07 11:49:31 +02001641static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
1642{
1643 struct vmcb_control_area *dst = &dst_vmcb->control;
1644 struct vmcb_control_area *from = &from_vmcb->control;
1645
1646 dst->intercept_cr_read = from->intercept_cr_read;
1647 dst->intercept_cr_write = from->intercept_cr_write;
1648 dst->intercept_dr_read = from->intercept_dr_read;
1649 dst->intercept_dr_write = from->intercept_dr_write;
1650 dst->intercept_exceptions = from->intercept_exceptions;
1651 dst->intercept = from->intercept;
1652 dst->iopm_base_pa = from->iopm_base_pa;
1653 dst->msrpm_base_pa = from->msrpm_base_pa;
1654 dst->tsc_offset = from->tsc_offset;
1655 dst->asid = from->asid;
1656 dst->tlb_ctl = from->tlb_ctl;
1657 dst->int_ctl = from->int_ctl;
1658 dst->int_vector = from->int_vector;
1659 dst->int_state = from->int_state;
1660 dst->exit_code = from->exit_code;
1661 dst->exit_code_hi = from->exit_code_hi;
1662 dst->exit_info_1 = from->exit_info_1;
1663 dst->exit_info_2 = from->exit_info_2;
1664 dst->exit_int_info = from->exit_int_info;
1665 dst->exit_int_info_err = from->exit_int_info_err;
1666 dst->nested_ctl = from->nested_ctl;
1667 dst->event_inj = from->event_inj;
1668 dst->event_inj_err = from->event_inj_err;
1669 dst->nested_cr3 = from->nested_cr3;
1670 dst->lbr_ctl = from->lbr_ctl;
1671}
1672
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001673static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001674{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001675 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001676 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02001677 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01001678 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01001679
Joerg Roedel17897f32009-10-09 16:08:29 +02001680 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
1681 vmcb->control.exit_info_1,
1682 vmcb->control.exit_info_2,
1683 vmcb->control.exit_int_info,
1684 vmcb->control.exit_int_info_err);
1685
Joerg Roedel7597f122010-02-19 16:23:00 +01001686 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001687 if (!nested_vmcb)
1688 return 1;
1689
Joerg Roedel06fc77722010-02-19 16:23:07 +01001690 /* Exit nested SVM mode */
1691 svm->nested.vmcb = 0;
1692
Alexander Grafcf74a782008-11-25 20:17:08 +01001693 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02001694 disable_gif(svm);
1695
1696 nested_vmcb->save.es = vmcb->save.es;
1697 nested_vmcb->save.cs = vmcb->save.cs;
1698 nested_vmcb->save.ss = vmcb->save.ss;
1699 nested_vmcb->save.ds = vmcb->save.ds;
1700 nested_vmcb->save.gdtr = vmcb->save.gdtr;
1701 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001702 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02001703 if (npt_enabled)
1704 nested_vmcb->save.cr3 = vmcb->save.cr3;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001705 else
1706 nested_vmcb->save.cr3 = svm->vcpu.arch.cr3;
Joerg Roedel33740e42009-08-07 11:49:29 +02001707 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001708 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Joerg Roedel33740e42009-08-07 11:49:29 +02001709 nested_vmcb->save.rflags = vmcb->save.rflags;
1710 nested_vmcb->save.rip = vmcb->save.rip;
1711 nested_vmcb->save.rsp = vmcb->save.rsp;
1712 nested_vmcb->save.rax = vmcb->save.rax;
1713 nested_vmcb->save.dr7 = vmcb->save.dr7;
1714 nested_vmcb->save.dr6 = vmcb->save.dr6;
1715 nested_vmcb->save.cpl = vmcb->save.cpl;
1716
1717 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
1718 nested_vmcb->control.int_vector = vmcb->control.int_vector;
1719 nested_vmcb->control.int_state = vmcb->control.int_state;
1720 nested_vmcb->control.exit_code = vmcb->control.exit_code;
1721 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
1722 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
1723 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
1724 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
1725 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Alexander Graf8d23c462009-10-09 16:08:25 +02001726
1727 /*
1728 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
1729 * to make sure that we do not lose injected events. So check event_inj
1730 * here and copy it to exit_int_info if it is valid.
1731 * Exit_int_info and event_inj can't be both valid because the case
1732 * below only happens on a VMRUN instruction intercept which has
1733 * no valid exit_int_info set.
1734 */
1735 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
1736 struct vmcb_control_area *nc = &nested_vmcb->control;
1737
1738 nc->exit_int_info = vmcb->control.event_inj;
1739 nc->exit_int_info_err = vmcb->control.event_inj_err;
1740 }
1741
Joerg Roedel33740e42009-08-07 11:49:29 +02001742 nested_vmcb->control.tlb_ctl = 0;
1743 nested_vmcb->control.event_inj = 0;
1744 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001745
1746 /* We always set V_INTR_MASKING and remember the old value in hflags */
1747 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
1748 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
1749
Alexander Grafcf74a782008-11-25 20:17:08 +01001750 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02001751 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01001752
Alexander Graf219b65d2009-06-15 15:21:25 +02001753 kvm_clear_exception_queue(&svm->vcpu);
1754 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01001755
1756 /* Restore selected save entries */
1757 svm->vmcb->save.es = hsave->save.es;
1758 svm->vmcb->save.cs = hsave->save.cs;
1759 svm->vmcb->save.ss = hsave->save.ss;
1760 svm->vmcb->save.ds = hsave->save.ds;
1761 svm->vmcb->save.gdtr = hsave->save.gdtr;
1762 svm->vmcb->save.idtr = hsave->save.idtr;
1763 svm->vmcb->save.rflags = hsave->save.rflags;
1764 svm_set_efer(&svm->vcpu, hsave->save.efer);
1765 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
1766 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
1767 if (npt_enabled) {
1768 svm->vmcb->save.cr3 = hsave->save.cr3;
1769 svm->vcpu.arch.cr3 = hsave->save.cr3;
1770 } else {
1771 kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
1772 }
1773 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
1774 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
1775 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
1776 svm->vmcb->save.dr7 = 0;
1777 svm->vmcb->save.cpl = 0;
1778 svm->vmcb->control.exit_int_info = 0;
1779
Joerg Roedel7597f122010-02-19 16:23:00 +01001780 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01001781
1782 kvm_mmu_reset_context(&svm->vcpu);
1783 kvm_mmu_load(&svm->vcpu);
1784
1785 return 0;
1786}
Alexander Graf3d6368e2008-11-25 20:17:07 +01001787
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001788static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01001789{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001790 u32 *nested_msrpm;
Joerg Roedel7597f122010-02-19 16:23:00 +01001791 struct page *page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001792 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001793
Joerg Roedel7597f122010-02-19 16:23:00 +01001794 nested_msrpm = nested_svm_map(svm, svm->nested.vmcb_msrpm, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001795 if (!nested_msrpm)
1796 return false;
1797
Alexander Graf3d6368e2008-11-25 20:17:07 +01001798 for (i=0; i< PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER) / 4; i++)
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001799 svm->nested.msrpm[i] = svm->msrpm[i] | nested_msrpm[i];
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001800
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001801 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001802
Joerg Roedel7597f122010-02-19 16:23:00 +01001803 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001804
1805 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001806}
1807
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001808static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01001809{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001810 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001811 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02001812 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01001813 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01001814 u64 vmcb_gpa;
1815
1816 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001817
Joerg Roedel7597f122010-02-19 16:23:00 +01001818 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001819 if (!nested_vmcb)
1820 return false;
1821
Joerg Roedel0ac406d2009-10-09 16:08:27 +02001822 trace_kvm_nested_vmrun(svm->vmcb->save.rip - 3, svm->nested.vmcb,
1823 nested_vmcb->save.rip,
1824 nested_vmcb->control.int_ctl,
1825 nested_vmcb->control.event_inj,
1826 nested_vmcb->control.nested_ctl);
1827
Alexander Graf3d6368e2008-11-25 20:17:07 +01001828 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02001829 kvm_clear_exception_queue(&svm->vcpu);
1830 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001831
1832 /* Save the old vmcb, so we don't need to pick what we save, but
1833 can restore everything when a VMEXIT occurs */
Joerg Roedeldefbba52009-08-07 11:49:30 +02001834 hsave->save.es = vmcb->save.es;
1835 hsave->save.cs = vmcb->save.cs;
1836 hsave->save.ss = vmcb->save.ss;
1837 hsave->save.ds = vmcb->save.ds;
1838 hsave->save.gdtr = vmcb->save.gdtr;
1839 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02001840 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02001841 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02001842 hsave->save.cr4 = svm->vcpu.arch.cr4;
1843 hsave->save.rflags = vmcb->save.rflags;
1844 hsave->save.rip = svm->next_rip;
1845 hsave->save.rsp = vmcb->save.rsp;
1846 hsave->save.rax = vmcb->save.rax;
1847 if (npt_enabled)
1848 hsave->save.cr3 = vmcb->save.cr3;
1849 else
1850 hsave->save.cr3 = svm->vcpu.arch.cr3;
1851
Joerg Roedel0460a972009-08-07 11:49:31 +02001852 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001853
1854 if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
1855 svm->vcpu.arch.hflags |= HF_HIF_MASK;
1856 else
1857 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
1858
1859 /* Load the nested guest state */
1860 svm->vmcb->save.es = nested_vmcb->save.es;
1861 svm->vmcb->save.cs = nested_vmcb->save.cs;
1862 svm->vmcb->save.ss = nested_vmcb->save.ss;
1863 svm->vmcb->save.ds = nested_vmcb->save.ds;
1864 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
1865 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
1866 svm->vmcb->save.rflags = nested_vmcb->save.rflags;
1867 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
1868 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
1869 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
1870 if (npt_enabled) {
1871 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
1872 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
1873 } else {
1874 kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
1875 kvm_mmu_reset_context(&svm->vcpu);
1876 }
Joerg Roedeldefbba52009-08-07 11:49:30 +02001877 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001878 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
1879 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
1880 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
1881 /* In case we don't even reach vcpu_run, the fields are not updated */
1882 svm->vmcb->save.rax = nested_vmcb->save.rax;
1883 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
1884 svm->vmcb->save.rip = nested_vmcb->save.rip;
1885 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
1886 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
1887 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
1888
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001889 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001890
Joerg Roedelaad42c62009-08-07 11:49:34 +02001891 /* cache intercepts */
1892 svm->nested.intercept_cr_read = nested_vmcb->control.intercept_cr_read;
1893 svm->nested.intercept_cr_write = nested_vmcb->control.intercept_cr_write;
1894 svm->nested.intercept_dr_read = nested_vmcb->control.intercept_dr_read;
1895 svm->nested.intercept_dr_write = nested_vmcb->control.intercept_dr_write;
1896 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
1897 svm->nested.intercept = nested_vmcb->control.intercept;
1898
Alexander Graf3d6368e2008-11-25 20:17:07 +01001899 force_new_asid(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001900 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001901 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
1902 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
1903 else
1904 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
1905
Joerg Roedel88ab24a2010-02-19 16:23:06 +01001906 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
1907 /* We only want the cr8 intercept bits of the guest */
1908 svm->vmcb->control.intercept_cr_read &= ~INTERCEPT_CR8_MASK;
1909 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
1910 }
1911
1912 /* We don't want a nested guest to be more powerful than the guest,
1913 so all intercepts are ORed */
1914 svm->vmcb->control.intercept_cr_read |=
1915 nested_vmcb->control.intercept_cr_read;
1916 svm->vmcb->control.intercept_cr_write |=
1917 nested_vmcb->control.intercept_cr_write;
1918 svm->vmcb->control.intercept_dr_read |=
1919 nested_vmcb->control.intercept_dr_read;
1920 svm->vmcb->control.intercept_dr_write |=
1921 nested_vmcb->control.intercept_dr_write;
1922 svm->vmcb->control.intercept_exceptions |=
1923 nested_vmcb->control.intercept_exceptions;
1924
1925 svm->vmcb->control.intercept |= nested_vmcb->control.intercept;
1926
1927 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001928 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
1929 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
1930 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001931 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
1932 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
1933
Joerg Roedel7597f122010-02-19 16:23:00 +01001934 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001935
Joerg Roedel06fc77722010-02-19 16:23:07 +01001936 /* nested_vmcb is our indicator if nested SVM is activated */
1937 svm->nested.vmcb = vmcb_gpa;
1938
Joerg Roedel2af91942009-08-07 11:49:28 +02001939 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001940
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001941 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001942}
1943
Joerg Roedel9966bf62009-08-07 11:49:40 +02001944static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01001945{
1946 to_vmcb->save.fs = from_vmcb->save.fs;
1947 to_vmcb->save.gs = from_vmcb->save.gs;
1948 to_vmcb->save.tr = from_vmcb->save.tr;
1949 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
1950 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
1951 to_vmcb->save.star = from_vmcb->save.star;
1952 to_vmcb->save.lstar = from_vmcb->save.lstar;
1953 to_vmcb->save.cstar = from_vmcb->save.cstar;
1954 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
1955 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
1956 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
1957 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01001958}
1959
Avi Kivity851ba692009-08-24 11:10:17 +03001960static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01001961{
Joerg Roedel9966bf62009-08-07 11:49:40 +02001962 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01001963 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02001964
Alexander Graf55426752008-11-25 20:17:06 +01001965 if (nested_svm_check_permissions(svm))
1966 return 1;
1967
1968 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
1969 skip_emulated_instruction(&svm->vcpu);
1970
Joerg Roedel7597f122010-02-19 16:23:00 +01001971 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02001972 if (!nested_vmcb)
1973 return 1;
1974
1975 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01001976 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01001977
1978 return 1;
1979}
1980
Avi Kivity851ba692009-08-24 11:10:17 +03001981static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01001982{
Joerg Roedel9966bf62009-08-07 11:49:40 +02001983 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01001984 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02001985
Alexander Graf55426752008-11-25 20:17:06 +01001986 if (nested_svm_check_permissions(svm))
1987 return 1;
1988
1989 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
1990 skip_emulated_instruction(&svm->vcpu);
1991
Joerg Roedel7597f122010-02-19 16:23:00 +01001992 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02001993 if (!nested_vmcb)
1994 return 1;
1995
1996 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01001997 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01001998
1999 return 1;
2000}
2001
Avi Kivity851ba692009-08-24 11:10:17 +03002002static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002003{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002004 if (nested_svm_check_permissions(svm))
2005 return 1;
2006
2007 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2008 skip_emulated_instruction(&svm->vcpu);
2009
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002010 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002011 return 1;
2012
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002013 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002014 goto failed;
2015
2016 return 1;
2017
2018failed:
2019
2020 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2021 svm->vmcb->control.exit_code_hi = 0;
2022 svm->vmcb->control.exit_info_1 = 0;
2023 svm->vmcb->control.exit_info_2 = 0;
2024
2025 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002026
2027 return 1;
2028}
2029
Avi Kivity851ba692009-08-24 11:10:17 +03002030static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002031{
2032 if (nested_svm_check_permissions(svm))
2033 return 1;
2034
2035 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2036 skip_emulated_instruction(&svm->vcpu);
2037
Joerg Roedel2af91942009-08-07 11:49:28 +02002038 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002039
2040 return 1;
2041}
2042
Avi Kivity851ba692009-08-24 11:10:17 +03002043static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002044{
2045 if (nested_svm_check_permissions(svm))
2046 return 1;
2047
2048 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2049 skip_emulated_instruction(&svm->vcpu);
2050
Joerg Roedel2af91942009-08-07 11:49:28 +02002051 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002052
2053 /* After a CLGI no interrupts should come */
2054 svm_clear_vintr(svm);
2055 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2056
2057 return 1;
2058}
2059
Avi Kivity851ba692009-08-24 11:10:17 +03002060static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002061{
2062 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002063
Joerg Roedelec1ff792009-10-09 16:08:31 +02002064 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
2065 vcpu->arch.regs[VCPU_REGS_RAX]);
2066
Alexander Grafff092382009-06-15 15:21:24 +02002067 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2068 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
2069
2070 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2071 skip_emulated_instruction(&svm->vcpu);
2072 return 1;
2073}
2074
Joerg Roedel532a46b2009-10-09 16:08:32 +02002075static int skinit_interception(struct vcpu_svm *svm)
2076{
2077 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
2078
2079 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2080 return 1;
2081}
2082
Avi Kivity851ba692009-08-24 11:10:17 +03002083static int invalid_op_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002084{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002085 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002086 return 1;
2087}
2088
Avi Kivity851ba692009-08-24 11:10:17 +03002089static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002090{
Izik Eidus37817f22008-03-24 23:14:53 +02002091 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002092 int reason;
2093 int int_type = svm->vmcb->control.exit_int_info &
2094 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002095 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002096 uint32_t type =
2097 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2098 uint32_t idt_v =
2099 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Izik Eidus37817f22008-03-24 23:14:53 +02002100
2101 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002102
Izik Eidus37817f22008-03-24 23:14:53 +02002103 if (svm->vmcb->control.exit_info_2 &
2104 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002105 reason = TASK_SWITCH_IRET;
2106 else if (svm->vmcb->control.exit_info_2 &
2107 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2108 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002109 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002110 reason = TASK_SWITCH_GATE;
2111 else
2112 reason = TASK_SWITCH_CALL;
2113
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002114 if (reason == TASK_SWITCH_GATE) {
2115 switch (type) {
2116 case SVM_EXITINTINFO_TYPE_NMI:
2117 svm->vcpu.arch.nmi_injected = false;
2118 break;
2119 case SVM_EXITINTINFO_TYPE_EXEPT:
2120 kvm_clear_exception_queue(&svm->vcpu);
2121 break;
2122 case SVM_EXITINTINFO_TYPE_INTR:
2123 kvm_clear_interrupt_queue(&svm->vcpu);
2124 break;
2125 default:
2126 break;
2127 }
2128 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002129
Gleb Natapov8317c292009-04-12 13:37:02 +03002130 if (reason != TASK_SWITCH_GATE ||
2131 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2132 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002133 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2134 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002135
2136 return kvm_task_switch(&svm->vcpu, tss_selector, reason);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002137}
2138
Avi Kivity851ba692009-08-24 11:10:17 +03002139static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002140{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002141 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002142 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002143 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002144}
2145
Avi Kivity851ba692009-08-24 11:10:17 +03002146static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002147{
2148 ++svm->vcpu.stat.nmi_window_exits;
2149 svm->vmcb->control.intercept &= ~(1UL << INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002150 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002151 return 1;
2152}
2153
Avi Kivity851ba692009-08-24 11:10:17 +03002154static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002155{
Avi Kivity851ba692009-08-24 11:10:17 +03002156 if (emulate_instruction(&svm->vcpu, 0, 0, 0) != EMULATE_DONE)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002157 pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
2158 return 1;
2159}
2160
Avi Kivity851ba692009-08-24 11:10:17 +03002161static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002162{
Avi Kivity851ba692009-08-24 11:10:17 +03002163 if (emulate_instruction(&svm->vcpu, 0, 0, 0) != EMULATE_DONE)
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002164 pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002165 return 1;
2166}
2167
Avi Kivity851ba692009-08-24 11:10:17 +03002168static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01002169{
Avi Kivity851ba692009-08-24 11:10:17 +03002170 struct kvm_run *kvm_run = svm->vcpu.run;
2171
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002172 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
2173 /* instruction emulation calls kvm_set_cr8() */
Avi Kivity851ba692009-08-24 11:10:17 +03002174 emulate_instruction(&svm->vcpu, 0, 0, 0);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002175 if (irqchip_in_kernel(svm->vcpu.kvm)) {
2176 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
Joerg Roedel1d075432007-12-06 21:02:25 +01002177 return 1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002178 }
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002179 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
2180 return 1;
Joerg Roedel1d075432007-12-06 21:02:25 +01002181 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2182 return 0;
2183}
2184
Avi Kivity6aa8b732006-12-10 02:21:36 -08002185static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
2186{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002187 struct vcpu_svm *svm = to_svm(vcpu);
2188
Avi Kivity6aa8b732006-12-10 02:21:36 -08002189 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302190 case MSR_IA32_TSC: {
Joerg Roedel20824f32009-09-16 15:24:18 +02002191 u64 tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002192
Joerg Roedel20824f32009-09-16 15:24:18 +02002193 if (is_nested(svm))
2194 tsc_offset = svm->nested.hsave->control.tsc_offset;
2195 else
2196 tsc_offset = svm->vmcb->control.tsc_offset;
2197
2198 *data = tsc_offset + native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002199 break;
2200 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08002201 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002202 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002203 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08002204#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002205 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002206 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002207 break;
2208 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002209 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002210 break;
2211 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002212 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002213 break;
2214 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002215 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002216 break;
2217#endif
2218 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002219 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002220 break;
2221 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002222 *data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002223 break;
2224 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002225 *data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002226 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002227 /* Nobody will change the following 5 values in the VMCB so
2228 we can safely return them on rdmsr. They will always be 0
2229 until LBRV is implemented. */
2230 case MSR_IA32_DEBUGCTLMSR:
2231 *data = svm->vmcb->save.dbgctl;
2232 break;
2233 case MSR_IA32_LASTBRANCHFROMIP:
2234 *data = svm->vmcb->save.br_from;
2235 break;
2236 case MSR_IA32_LASTBRANCHTOIP:
2237 *data = svm->vmcb->save.br_to;
2238 break;
2239 case MSR_IA32_LASTINTFROMIP:
2240 *data = svm->vmcb->save.last_excp_from;
2241 break;
2242 case MSR_IA32_LASTINTTOIP:
2243 *data = svm->vmcb->save.last_excp_to;
2244 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002245 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002246 *data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002247 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002248 case MSR_VM_CR:
2249 *data = 0;
2250 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01002251 case MSR_IA32_UCODE_REV:
2252 *data = 0x01000065;
2253 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002254 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002255 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002256 }
2257 return 0;
2258}
2259
Avi Kivity851ba692009-08-24 11:10:17 +03002260static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002261{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002262 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002263 u64 data;
2264
Avi Kivity59200272010-01-25 19:47:02 +02002265 if (svm_get_msr(&svm->vcpu, ecx, &data)) {
2266 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002267 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002268 } else {
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002269 trace_kvm_msr_read(ecx, data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002270
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002271 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002272 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002273 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002274 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002275 }
2276 return 1;
2277}
2278
2279static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
2280{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002281 struct vcpu_svm *svm = to_svm(vcpu);
2282
Avi Kivity6aa8b732006-12-10 02:21:36 -08002283 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302284 case MSR_IA32_TSC: {
Joerg Roedel20824f32009-09-16 15:24:18 +02002285 u64 tsc_offset = data - native_read_tsc();
2286 u64 g_tsc_offset = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002287
Joerg Roedel20824f32009-09-16 15:24:18 +02002288 if (is_nested(svm)) {
2289 g_tsc_offset = svm->vmcb->control.tsc_offset -
2290 svm->nested.hsave->control.tsc_offset;
2291 svm->nested.hsave->control.tsc_offset = tsc_offset;
2292 }
2293
2294 svm->vmcb->control.tsc_offset = tsc_offset + g_tsc_offset;
2295
Avi Kivity6aa8b732006-12-10 02:21:36 -08002296 break;
2297 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08002298 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002299 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002300 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08002301#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002302 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002303 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002304 break;
2305 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002306 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002307 break;
2308 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002309 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002310 break;
2311 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002312 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002313 break;
2314#endif
2315 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002316 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002317 break;
2318 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002319 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002320 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321 break;
2322 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002323 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002324 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002325 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002326 case MSR_IA32_DEBUGCTLMSR:
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002327 if (!svm_has(SVM_FEATURE_LBRV)) {
2328 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002329 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002330 break;
2331 }
2332 if (data & DEBUGCTL_RESERVED_BITS)
2333 return 1;
2334
2335 svm->vmcb->save.dbgctl = data;
2336 if (data & (1ULL<<0))
2337 svm_enable_lbrv(svm);
2338 else
2339 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01002340 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002341 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002342 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002343 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002344 case MSR_VM_CR:
2345 case MSR_VM_IGNNE:
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002346 pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
2347 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002348 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002349 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002350 }
2351 return 0;
2352}
2353
Avi Kivity851ba692009-08-24 11:10:17 +03002354static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002355{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002356 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002357 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002358 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002359
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002360
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002361 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Avi Kivity59200272010-01-25 19:47:02 +02002362 if (svm_set_msr(&svm->vcpu, ecx, data)) {
2363 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002364 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002365 } else {
2366 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10002367 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02002368 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002369 return 1;
2370}
2371
Avi Kivity851ba692009-08-24 11:10:17 +03002372static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373{
Rusty Russelle756fc62007-07-30 20:07:08 +10002374 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03002375 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002376 else
Avi Kivity851ba692009-08-24 11:10:17 +03002377 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002378}
2379
Avi Kivity851ba692009-08-24 11:10:17 +03002380static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08002381{
Avi Kivity851ba692009-08-24 11:10:17 +03002382 struct kvm_run *kvm_run = svm->vcpu.run;
2383
Alexander Graff0b85052008-11-25 20:17:01 +01002384 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03002385 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -08002386 /*
2387 * If the user space waits to inject interrupts, exit as soon as
2388 * possible
2389 */
Gleb Natapov80618232009-04-21 17:44:56 +03002390 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
2391 kvm_run->request_interrupt_window &&
2392 !kvm_cpu_has_interrupt(&svm->vcpu)) {
Rusty Russelle756fc62007-07-30 20:07:08 +10002393 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002394 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
2395 return 0;
2396 }
2397
2398 return 1;
2399}
2400
Mark Langsdorf565d0992009-10-06 14:25:02 -05002401static int pause_interception(struct vcpu_svm *svm)
2402{
2403 kvm_vcpu_on_spin(&(svm->vcpu));
2404 return 1;
2405}
2406
Avi Kivity851ba692009-08-24 11:10:17 +03002407static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002408 [SVM_EXIT_READ_CR0] = emulate_on_interception,
2409 [SVM_EXIT_READ_CR3] = emulate_on_interception,
2410 [SVM_EXIT_READ_CR4] = emulate_on_interception,
Avi Kivity80a81192007-12-06 19:50:00 +02002411 [SVM_EXIT_READ_CR8] = emulate_on_interception,
Avi Kivityd2251572010-01-06 10:55:27 +02002412 [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002413 [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
2414 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
2415 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
Joerg Roedel1d075432007-12-06 21:02:25 +01002416 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002417 [SVM_EXIT_READ_DR0] = emulate_on_interception,
2418 [SVM_EXIT_READ_DR1] = emulate_on_interception,
2419 [SVM_EXIT_READ_DR2] = emulate_on_interception,
2420 [SVM_EXIT_READ_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002421 [SVM_EXIT_READ_DR4] = emulate_on_interception,
2422 [SVM_EXIT_READ_DR5] = emulate_on_interception,
2423 [SVM_EXIT_READ_DR6] = emulate_on_interception,
2424 [SVM_EXIT_READ_DR7] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002425 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
2426 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
2427 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
2428 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002429 [SVM_EXIT_WRITE_DR4] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002430 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002431 [SVM_EXIT_WRITE_DR6] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002432 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002433 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
2434 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002435 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002436 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
Anthony Liguori7807fa62007-04-23 09:17:21 -05002437 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
Joerg Roedel53371b52008-04-09 14:15:30 +02002438 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
Joerg Roedela0698052008-04-30 17:56:01 +02002439 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02002440 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002441 [SVM_EXIT_SMI] = nop_on_interception,
2442 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08002443 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002444 /* [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, */
2445 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002446 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002447 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05002448 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002449 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03002450 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02002451 [SVM_EXIT_INVLPGA] = invlpga_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002452 [SVM_EXIT_IOIO] = io_interception,
2453 [SVM_EXIT_MSR] = msr_interception,
2454 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002455 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01002456 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02002457 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01002458 [SVM_EXIT_VMLOAD] = vmload_interception,
2459 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01002460 [SVM_EXIT_STGI] = stgi_interception,
2461 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02002462 [SVM_EXIT_SKINIT] = skinit_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002463 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01002464 [SVM_EXIT_MONITOR] = invalid_op_interception,
2465 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002466 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002467};
2468
Avi Kivity851ba692009-08-24 11:10:17 +03002469static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002470{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002471 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002472 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002473 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002474
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002475 trace_kvm_exit(exit_code, svm->vmcb->save.rip);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002476
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002477 if (unlikely(svm->nested.exit_required)) {
2478 nested_svm_vmexit(svm);
2479 svm->nested.exit_required = false;
2480
2481 return 1;
2482 }
2483
Alexander Grafcf74a782008-11-25 20:17:08 +01002484 if (is_nested(svm)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02002485 int vmexit;
2486
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02002487 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
2488 svm->vmcb->control.exit_info_1,
2489 svm->vmcb->control.exit_info_2,
2490 svm->vmcb->control.exit_int_info,
2491 svm->vmcb->control.exit_int_info_err);
2492
Joerg Roedel410e4d52009-08-07 11:49:44 +02002493 vmexit = nested_svm_exit_special(svm);
2494
2495 if (vmexit == NESTED_EXIT_CONTINUE)
2496 vmexit = nested_svm_exit_handled(svm);
2497
2498 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01002499 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01002500 }
2501
Joerg Roedela5c38322009-08-07 11:49:32 +02002502 svm_complete_interrupts(svm);
2503
Avi Kivity888f9f32010-01-10 12:14:04 +02002504 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR0_MASK))
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002505 vcpu->arch.cr0 = svm->vmcb->save.cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02002506 if (npt_enabled)
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002507 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002508
2509 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
2510 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2511 kvm_run->fail_entry.hardware_entry_failure_reason
2512 = svm->vmcb->control.exit_code;
2513 return 0;
2514 }
2515
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002516 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002517 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002518 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002519 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
2520 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002521 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002522 exit_code);
2523
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02002524 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08002525 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002526 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03002527 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002528 return 0;
2529 }
2530
Avi Kivity851ba692009-08-24 11:10:17 +03002531 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002532}
2533
2534static void reload_tss(struct kvm_vcpu *vcpu)
2535{
2536 int cpu = raw_smp_processor_id();
2537
Tejun Heo0fe1e002009-10-29 22:34:14 +09002538 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
2539 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002540 load_TR_desc();
2541}
2542
Rusty Russelle756fc62007-07-30 20:07:08 +10002543static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002544{
2545 int cpu = raw_smp_processor_id();
2546
Tejun Heo0fe1e002009-10-29 22:34:14 +09002547 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002548
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002549 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03002550 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09002551 if (svm->asid_generation != sd->asid_generation)
2552 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002553}
2554
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002555static void svm_inject_nmi(struct kvm_vcpu *vcpu)
2556{
2557 struct vcpu_svm *svm = to_svm(vcpu);
2558
2559 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
2560 vcpu->arch.hflags |= HF_NMI_MASK;
2561 svm->vmcb->control.intercept |= (1UL << INTERCEPT_IRET);
2562 ++vcpu->stat.nmi_injections;
2563}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002564
Eddie Dong85f455f2007-07-06 12:20:49 +03002565static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002566{
2567 struct vmcb_control_area *control;
2568
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002569 trace_kvm_inj_virq(irq);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002570
Avi Kivityfa89a812008-09-01 15:57:51 +03002571 ++svm->vcpu.stat.irq_injections;
Rusty Russelle756fc62007-07-30 20:07:08 +10002572 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03002573 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002574 control->int_ctl &= ~V_INTR_PRIO_MASK;
2575 control->int_ctl |= V_IRQ_MASK |
2576 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
2577}
2578
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002579static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03002580{
2581 struct vcpu_svm *svm = to_svm(vcpu);
2582
Joerg Roedel2af91942009-08-07 11:49:28 +02002583 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01002584
Alexander Graf219b65d2009-06-15 15:21:25 +02002585 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
2586 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002587}
2588
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002589static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
2590{
2591 struct vcpu_svm *svm = to_svm(vcpu);
2592
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002593 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
2594 return;
2595
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002596 if (irr == -1)
2597 return;
2598
2599 if (tpr >= irr)
2600 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK;
2601}
2602
2603static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02002604{
2605 struct vcpu_svm *svm = to_svm(vcpu);
2606 struct vmcb *vmcb = svm->vmcb;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002607 return !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
2608 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
Joerg Roedelaaacfc92008-04-16 16:51:18 +02002609}
2610
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002611static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
2612{
2613 struct vcpu_svm *svm = to_svm(vcpu);
2614
2615 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
2616}
2617
2618static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
2619{
2620 struct vcpu_svm *svm = to_svm(vcpu);
2621
2622 if (masked) {
2623 svm->vcpu.arch.hflags |= HF_NMI_MASK;
2624 svm->vmcb->control.intercept |= (1UL << INTERCEPT_IRET);
2625 } else {
2626 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
2627 svm->vmcb->control.intercept &= ~(1UL << INTERCEPT_IRET);
2628 }
2629}
2630
Gleb Natapov78646122009-03-23 12:12:11 +02002631static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
2632{
2633 struct vcpu_svm *svm = to_svm(vcpu);
2634 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02002635 int ret;
2636
2637 if (!gif_set(svm) ||
2638 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
2639 return 0;
2640
2641 ret = !!(vmcb->save.rflags & X86_EFLAGS_IF);
2642
2643 if (is_nested(svm))
2644 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
2645
2646 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02002647}
2648
Gleb Natapov9222be12009-04-23 17:14:37 +03002649static void enable_irq_window(struct kvm_vcpu *vcpu)
2650{
Alexander Graf219b65d2009-06-15 15:21:25 +02002651 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02002652
Alexander Graf219b65d2009-06-15 15:21:25 +02002653 /* In case GIF=0 we can't rely on the CPU to tell us when
2654 * GIF becomes 1, because that's a separate STGI/VMRUN intercept.
2655 * The next time we get that intercept, this function will be
2656 * called again though and we'll get the vintr intercept. */
Joerg Roedel8fe54652010-02-19 16:23:01 +01002657 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02002658 svm_set_vintr(svm);
2659 svm_inject_irq(svm, 0x0);
2660 }
Gleb Natapov9222be12009-04-23 17:14:37 +03002661}
2662
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002663static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002664{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002665 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03002666
Gleb Natapov44c11432009-05-11 13:35:52 +03002667 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
2668 == HF_NMI_MASK)
2669 return; /* IRET will cause a vm exit */
2670
2671 /* Something prevents NMI from been injected. Single step over
2672 possible problem (IRET or exception injection or interrupt
2673 shadow) */
Jan Kiszka6be7d302009-10-18 13:24:54 +02002674 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03002675 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
2676 update_db_intercept(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03002677}
2678
Izik Eiduscbc94022007-10-25 00:29:55 +02002679static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
2680{
2681 return 0;
2682}
2683
Avi Kivityd9e368d2007-06-07 19:18:30 +03002684static void svm_flush_tlb(struct kvm_vcpu *vcpu)
2685{
2686 force_new_asid(vcpu);
2687}
2688
Avi Kivity04d2cc72007-09-10 18:10:54 +03002689static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
2690{
2691}
2692
Joerg Roedeld7bf8222008-04-16 16:51:17 +02002693static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
2694{
2695 struct vcpu_svm *svm = to_svm(vcpu);
2696
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002697 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
2698 return;
2699
Joerg Roedeld7bf8222008-04-16 16:51:17 +02002700 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) {
2701 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03002702 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02002703 }
2704}
2705
Joerg Roedel649d6862008-04-16 16:51:15 +02002706static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
2707{
2708 struct vcpu_svm *svm = to_svm(vcpu);
2709 u64 cr8;
2710
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002711 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
2712 return;
2713
Joerg Roedel649d6862008-04-16 16:51:15 +02002714 cr8 = kvm_get_cr8(vcpu);
2715 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
2716 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
2717}
2718
Gleb Natapov9222be12009-04-23 17:14:37 +03002719static void svm_complete_interrupts(struct vcpu_svm *svm)
2720{
2721 u8 vector;
2722 int type;
2723 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01002724 unsigned int3_injected = svm->int3_injected;
2725
2726 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03002727
Gleb Natapov44c11432009-05-11 13:35:52 +03002728 if (svm->vcpu.arch.hflags & HF_IRET_MASK)
2729 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
2730
Gleb Natapov9222be12009-04-23 17:14:37 +03002731 svm->vcpu.arch.nmi_injected = false;
2732 kvm_clear_exception_queue(&svm->vcpu);
2733 kvm_clear_interrupt_queue(&svm->vcpu);
2734
2735 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
2736 return;
2737
2738 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
2739 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
2740
2741 switch (type) {
2742 case SVM_EXITINTINFO_TYPE_NMI:
2743 svm->vcpu.arch.nmi_injected = true;
2744 break;
2745 case SVM_EXITINTINFO_TYPE_EXEPT:
Alexander Graf219b65d2009-06-15 15:21:25 +02002746 if (is_nested(svm))
2747 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01002748 /*
2749 * In case of software exceptions, do not reinject the vector,
2750 * but re-execute the instruction instead. Rewind RIP first
2751 * if we emulated INT3 before.
2752 */
2753 if (kvm_exception_is_soft(vector)) {
2754 if (vector == BP_VECTOR && int3_injected &&
2755 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
2756 kvm_rip_write(&svm->vcpu,
2757 kvm_rip_read(&svm->vcpu) -
2758 int3_injected);
Gleb Natapov9222be12009-04-23 17:14:37 +03002759 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01002760 }
Gleb Natapov9222be12009-04-23 17:14:37 +03002761 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
2762 u32 err = svm->vmcb->control.exit_int_info_err;
2763 kvm_queue_exception_e(&svm->vcpu, vector, err);
2764
2765 } else
2766 kvm_queue_exception(&svm->vcpu, vector);
2767 break;
2768 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002769 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03002770 break;
2771 default:
2772 break;
2773 }
2774}
2775
Avi Kivity80e31d42008-07-14 14:44:59 +03002776#ifdef CONFIG_X86_64
2777#define R "r"
2778#else
2779#define R "e"
2780#endif
2781
Avi Kivity851ba692009-08-24 11:10:17 +03002782static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002783{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002784 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002785 u16 fs_selector;
2786 u16 gs_selector;
2787 u16 ldt_selector;
Avi Kivityd9e368d2007-06-07 19:18:30 +03002788
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002789 /*
2790 * A vmexit emulation is required before the vcpu can be executed
2791 * again.
2792 */
2793 if (unlikely(svm->nested.exit_required))
2794 return;
2795
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002796 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
2797 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
2798 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
2799
Rusty Russelle756fc62007-07-30 20:07:08 +10002800 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002801
Joerg Roedel649d6862008-04-16 16:51:15 +02002802 sync_lapic_to_cr8(vcpu);
2803
Avi Kivity6aa8b732006-12-10 02:21:36 -08002804 save_host_msrs(vcpu);
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002805 fs_selector = kvm_read_fs();
2806 gs_selector = kvm_read_gs();
2807 ldt_selector = kvm_read_ldt();
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02002808 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002809 /* required for live migration with NPT */
2810 if (npt_enabled)
2811 svm->vmcb->save.cr3 = vcpu->arch.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002812
Avi Kivity04d2cc72007-09-10 18:10:54 +03002813 clgi();
2814
2815 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08002816
Avi Kivity6aa8b732006-12-10 02:21:36 -08002817 asm volatile (
Avi Kivity80e31d42008-07-14 14:44:59 +03002818 "push %%"R"bp; \n\t"
2819 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
2820 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
2821 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
2822 "mov %c[rsi](%[svm]), %%"R"si \n\t"
2823 "mov %c[rdi](%[svm]), %%"R"di \n\t"
2824 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002825#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002826 "mov %c[r8](%[svm]), %%r8 \n\t"
2827 "mov %c[r9](%[svm]), %%r9 \n\t"
2828 "mov %c[r10](%[svm]), %%r10 \n\t"
2829 "mov %c[r11](%[svm]), %%r11 \n\t"
2830 "mov %c[r12](%[svm]), %%r12 \n\t"
2831 "mov %c[r13](%[svm]), %%r13 \n\t"
2832 "mov %c[r14](%[svm]), %%r14 \n\t"
2833 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002834#endif
2835
Avi Kivity6aa8b732006-12-10 02:21:36 -08002836 /* Enter guest mode */
Avi Kivity80e31d42008-07-14 14:44:59 +03002837 "push %%"R"ax \n\t"
2838 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002839 __ex(SVM_VMLOAD) "\n\t"
2840 __ex(SVM_VMRUN) "\n\t"
2841 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity80e31d42008-07-14 14:44:59 +03002842 "pop %%"R"ax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002843
2844 /* Save guest registers, load host registers */
Avi Kivity80e31d42008-07-14 14:44:59 +03002845 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
2846 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
2847 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
2848 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
2849 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
2850 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002851#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002852 "mov %%r8, %c[r8](%[svm]) \n\t"
2853 "mov %%r9, %c[r9](%[svm]) \n\t"
2854 "mov %%r10, %c[r10](%[svm]) \n\t"
2855 "mov %%r11, %c[r11](%[svm]) \n\t"
2856 "mov %%r12, %c[r12](%[svm]) \n\t"
2857 "mov %%r13, %c[r13](%[svm]) \n\t"
2858 "mov %%r14, %c[r14](%[svm]) \n\t"
2859 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002860#endif
Avi Kivity80e31d42008-07-14 14:44:59 +03002861 "pop %%"R"bp"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002862 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002863 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08002864 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002865 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
2866 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
2867 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
2868 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
2869 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
2870 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002871#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002872 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
2873 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
2874 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
2875 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
2876 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
2877 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
2878 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
2879 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002880#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02002881 : "cc", "memory"
Avi Kivity80e31d42008-07-14 14:44:59 +03002882 , R"bx", R"cx", R"dx", R"si", R"di"
Laurent Vivier54a08c02007-10-25 14:18:53 +02002883#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02002884 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
2885#endif
2886 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08002887
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002888 vcpu->arch.cr2 = svm->vmcb->save.cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002889 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
2890 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
2891 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002892
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002893 kvm_load_fs(fs_selector);
2894 kvm_load_gs(gs_selector);
2895 kvm_load_ldt(ldt_selector);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002896 load_host_msrs(vcpu);
2897
2898 reload_tss(vcpu);
2899
Avi Kivity56ba47d2007-11-07 17:14:18 +02002900 local_irq_disable();
2901
2902 stgi();
2903
Joerg Roedeld7bf8222008-04-16 16:51:17 +02002904 sync_cr8_to_lapic(vcpu);
2905
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002906 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03002907
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002908 if (npt_enabled) {
2909 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
2910 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
2911 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002912}
2913
Avi Kivity80e31d42008-07-14 14:44:59 +03002914#undef R
2915
Avi Kivity6aa8b732006-12-10 02:21:36 -08002916static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
2917{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002918 struct vcpu_svm *svm = to_svm(vcpu);
2919
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002920 if (npt_enabled) {
2921 svm->vmcb->control.nested_cr3 = root;
2922 force_new_asid(vcpu);
2923 return;
2924 }
2925
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002926 svm->vmcb->save.cr3 = root;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002927 force_new_asid(vcpu);
2928}
2929
Avi Kivity6aa8b732006-12-10 02:21:36 -08002930static int is_disabled(void)
2931{
Joerg Roedel6031a612007-06-22 12:29:50 +03002932 u64 vm_cr;
2933
2934 rdmsrl(MSR_VM_CR, vm_cr);
2935 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
2936 return 1;
2937
Avi Kivity6aa8b732006-12-10 02:21:36 -08002938 return 0;
2939}
2940
Ingo Molnar102d8322007-02-19 14:37:47 +02002941static void
2942svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2943{
2944 /*
2945 * Patch in the VMMCALL instruction:
2946 */
2947 hypercall[0] = 0x0f;
2948 hypercall[1] = 0x01;
2949 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02002950}
2951
Yang, Sheng002c7f72007-07-31 14:23:01 +03002952static void svm_check_processor_compat(void *rtn)
2953{
2954 *(int *)rtn = 0;
2955}
2956
Avi Kivity774ead32007-12-26 13:57:04 +02002957static bool svm_cpu_has_accelerated_tpr(void)
2958{
2959 return false;
2960}
2961
Sheng Yang67253af2008-04-25 10:20:22 +08002962static int get_npt_level(void)
2963{
2964#ifdef CONFIG_X86_64
2965 return PT64_ROOT_LEVEL;
2966#else
2967 return PT32E_ROOT_LEVEL;
2968#endif
2969}
2970
Sheng Yang4b12f0d2009-04-27 20:35:42 +08002971static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08002972{
2973 return 0;
2974}
2975
Sheng Yang0e851882009-12-18 16:48:46 +08002976static void svm_cpuid_update(struct kvm_vcpu *vcpu)
2977{
2978}
2979
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002980static const struct trace_print_flags svm_exit_reasons_str[] = {
2981 { SVM_EXIT_READ_CR0, "read_cr0" },
2982 { SVM_EXIT_READ_CR3, "read_cr3" },
2983 { SVM_EXIT_READ_CR4, "read_cr4" },
2984 { SVM_EXIT_READ_CR8, "read_cr8" },
2985 { SVM_EXIT_WRITE_CR0, "write_cr0" },
2986 { SVM_EXIT_WRITE_CR3, "write_cr3" },
2987 { SVM_EXIT_WRITE_CR4, "write_cr4" },
2988 { SVM_EXIT_WRITE_CR8, "write_cr8" },
2989 { SVM_EXIT_READ_DR0, "read_dr0" },
2990 { SVM_EXIT_READ_DR1, "read_dr1" },
2991 { SVM_EXIT_READ_DR2, "read_dr2" },
2992 { SVM_EXIT_READ_DR3, "read_dr3" },
2993 { SVM_EXIT_WRITE_DR0, "write_dr0" },
2994 { SVM_EXIT_WRITE_DR1, "write_dr1" },
2995 { SVM_EXIT_WRITE_DR2, "write_dr2" },
2996 { SVM_EXIT_WRITE_DR3, "write_dr3" },
2997 { SVM_EXIT_WRITE_DR5, "write_dr5" },
2998 { SVM_EXIT_WRITE_DR7, "write_dr7" },
2999 { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
3000 { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
3001 { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
3002 { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
3003 { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
3004 { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
3005 { SVM_EXIT_INTR, "interrupt" },
3006 { SVM_EXIT_NMI, "nmi" },
3007 { SVM_EXIT_SMI, "smi" },
3008 { SVM_EXIT_INIT, "init" },
3009 { SVM_EXIT_VINTR, "vintr" },
3010 { SVM_EXIT_CPUID, "cpuid" },
3011 { SVM_EXIT_INVD, "invd" },
3012 { SVM_EXIT_HLT, "hlt" },
3013 { SVM_EXIT_INVLPG, "invlpg" },
3014 { SVM_EXIT_INVLPGA, "invlpga" },
3015 { SVM_EXIT_IOIO, "io" },
3016 { SVM_EXIT_MSR, "msr" },
3017 { SVM_EXIT_TASK_SWITCH, "task_switch" },
3018 { SVM_EXIT_SHUTDOWN, "shutdown" },
3019 { SVM_EXIT_VMRUN, "vmrun" },
3020 { SVM_EXIT_VMMCALL, "hypercall" },
3021 { SVM_EXIT_VMLOAD, "vmload" },
3022 { SVM_EXIT_VMSAVE, "vmsave" },
3023 { SVM_EXIT_STGI, "stgi" },
3024 { SVM_EXIT_CLGI, "clgi" },
3025 { SVM_EXIT_SKINIT, "skinit" },
3026 { SVM_EXIT_WBINVD, "wbinvd" },
3027 { SVM_EXIT_MONITOR, "monitor" },
3028 { SVM_EXIT_MWAIT, "mwait" },
3029 { SVM_EXIT_NPF, "npf" },
3030 { -1, NULL }
3031};
3032
Sheng Yang17cc3932010-01-05 19:02:27 +08003033static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02003034{
Sheng Yang17cc3932010-01-05 19:02:27 +08003035 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02003036}
3037
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003038static bool svm_rdtscp_supported(void)
3039{
3040 return false;
3041}
3042
Avi Kivity02daab22009-12-30 12:40:26 +02003043static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
3044{
3045 struct vcpu_svm *svm = to_svm(vcpu);
3046
Avi Kivity02daab22009-12-30 12:40:26 +02003047 svm->vmcb->control.intercept_exceptions |= 1 << NM_VECTOR;
Joerg Roedel66a562f2010-02-19 16:23:08 +01003048 if (is_nested(svm))
3049 svm->nested.hsave->control.intercept_exceptions |= 1 << NM_VECTOR;
3050 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02003051}
3052
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003053static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003054 .cpu_has_kvm_support = has_svm,
3055 .disabled_by_bios = is_disabled,
3056 .hardware_setup = svm_hardware_setup,
3057 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003058 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003059 .hardware_enable = svm_hardware_enable,
3060 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003061 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003062
3063 .vcpu_create = svm_create_vcpu,
3064 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003065 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003066
Avi Kivity04d2cc72007-09-10 18:10:54 +03003067 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003068 .vcpu_load = svm_vcpu_load,
3069 .vcpu_put = svm_vcpu_put,
3070
3071 .set_guest_debug = svm_guest_debug,
3072 .get_msr = svm_get_msr,
3073 .set_msr = svm_set_msr,
3074 .get_segment_base = svm_get_segment_base,
3075 .get_segment = svm_get_segment,
3076 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003077 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10003078 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02003079 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003080 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003081 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003082 .set_cr3 = svm_set_cr3,
3083 .set_cr4 = svm_set_cr4,
3084 .set_efer = svm_set_efer,
3085 .get_idt = svm_get_idt,
3086 .set_idt = svm_set_idt,
3087 .get_gdt = svm_get_gdt,
3088 .set_gdt = svm_set_gdt,
3089 .get_dr = svm_get_dr,
3090 .set_dr = svm_set_dr,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003091 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003092 .get_rflags = svm_get_rflags,
3093 .set_rflags = svm_set_rflags,
Avi Kivity6b52d182010-01-21 15:31:47 +02003094 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02003095 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003096
Avi Kivity6aa8b732006-12-10 02:21:36 -08003097 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003098
Avi Kivity6aa8b732006-12-10 02:21:36 -08003099 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003100 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003101 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04003102 .set_interrupt_shadow = svm_set_interrupt_shadow,
3103 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02003104 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003105 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003106 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02003107 .queue_exception = svm_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02003108 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003109 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003110 .get_nmi_mask = svm_get_nmi_mask,
3111 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003112 .enable_nmi_window = enable_nmi_window,
3113 .enable_irq_window = enable_irq_window,
3114 .update_cr8_intercept = update_cr8_intercept,
Izik Eiduscbc94022007-10-25 00:29:55 +02003115
3116 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003117 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003118 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003119
3120 .exit_reasons_str = svm_exit_reasons_str,
Sheng Yang17cc3932010-01-05 19:02:27 +08003121 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08003122
3123 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003124
3125 .rdtscp_supported = svm_rdtscp_supported,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003126};
3127
3128static int __init svm_init(void)
3129{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003130 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Rusty Russellc16f8622007-07-30 21:12:19 +10003131 THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003132}
3133
3134static void __exit svm_exit(void)
3135{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003136 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003137}
3138
3139module_init(svm_init)
3140module_exit(svm_exit)