blob: da6461d5dc84a53ffa89c4e9f86cb9a1b19f18ae [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 *
9 * Authors:
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
17
Eddie Dong85f455f2007-07-06 12:20:49 +030018#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080019#include "mmu.h"
Avi Kivitye4956062007-06-28 14:15:57 -040020
Avi Kivityedf88412007-12-16 11:02:48 +020021#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080022#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020023#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/mm.h>
25#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040026#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020027#include <linux/moduleparam.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030028#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030029#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040030
Avi Kivity6aa8b732006-12-10 02:21:36 -080031#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080032#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020033#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020034#include <asm/virtext.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080035
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
Avi Kivity4462d212009-03-23 17:53:37 +020041static int __read_mostly bypass_guest_pf = 1;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020042module_param(bypass_guest_pf, bool, S_IRUGO);
Avi Kivityc7addb92007-09-16 18:58:32 +020043
Avi Kivity4462d212009-03-23 17:53:37 +020044static int __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020045module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080046
Avi Kivity4462d212009-03-23 17:53:37 +020047static int __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020048module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020049
Avi Kivity4462d212009-03-23 17:53:37 +020050static int __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020051module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080052
Avi Kivity4462d212009-03-23 17:53:37 +020053static int __read_mostly emulate_invalid_guest_state = 0;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020054module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030055
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040056struct vmcs {
57 u32 revision_id;
58 u32 abort;
59 char data[0];
60};
61
62struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +100063 struct kvm_vcpu vcpu;
Avi Kivity543e4242008-05-13 16:22:47 +030064 struct list_head local_vcpus_link;
Avi Kivity313dbd42008-07-17 18:04:30 +030065 unsigned long host_rsp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040066 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +030067 u8 fail;
Avi Kivity1155f762007-11-22 11:30:47 +020068 u32 idt_vectoring_info;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040069 struct kvm_msr_entry *guest_msrs;
70 struct kvm_msr_entry *host_msrs;
71 int nmsrs;
72 int save_nmsrs;
73 int msr_offset_efer;
74#ifdef CONFIG_X86_64
75 int msr_offset_kernel_gs_base;
76#endif
77 struct vmcs *vmcs;
78 struct {
79 int loaded;
80 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +020081 int gs_ldt_reload_needed;
82 int fs_reload_needed;
Avi Kivity51c6cf62007-08-29 03:48:05 +030083 int guest_efer_loaded;
Mike Dayd77c26f2007-10-08 09:02:08 -040084 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +020085 struct {
86 struct {
87 bool pending;
88 u8 vector;
89 unsigned rip;
90 } irq;
91 } rmode;
Sheng Yang2384d2b2008-01-17 15:14:33 +080092 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030093 bool emulation_required;
Avi Kivity8b3079a2009-01-05 12:10:54 +020094 enum emulation_result invalid_state_emulation_result;
Jan Kiszka3b86cd92008-09-26 09:30:57 +020095
96 /* Support for vnmi-less CPUs */
97 int soft_vnmi_blocked;
98 ktime_t entry_time;
99 s64 vnmi_blocked_time;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400100};
101
102static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
103{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000104 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400105}
106
Sheng Yangb7ebfb02008-04-25 21:44:52 +0800107static int init_rmode(struct kvm *kvm);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800108static u64 construct_eptp(unsigned long root_hpa);
Avi Kivity75880a02007-06-20 11:20:04 +0300109
Avi Kivity6aa8b732006-12-10 02:21:36 -0800110static DEFINE_PER_CPU(struct vmcs *, vmxarea);
111static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Avi Kivity543e4242008-05-13 16:22:47 +0300112static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800113
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200114static unsigned long *vmx_io_bitmap_a;
115static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200116static unsigned long *vmx_msr_bitmap_legacy;
117static unsigned long *vmx_msr_bitmap_longmode;
He, Qingfdef3ad2007-04-30 09:45:24 +0300118
Sheng Yang2384d2b2008-01-17 15:14:33 +0800119static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
120static DEFINE_SPINLOCK(vmx_vpid_lock);
121
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300122static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800123 int size;
124 int order;
125 u32 revision_id;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300126 u32 pin_based_exec_ctrl;
127 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800128 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300129 u32 vmexit_ctrl;
130 u32 vmentry_ctrl;
131} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800132
Hannes Ederefff9e52008-11-28 17:02:06 +0100133static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800134 u32 ept;
135 u32 vpid;
136} vmx_capability;
137
Avi Kivity6aa8b732006-12-10 02:21:36 -0800138#define VMX_SEGMENT_FIELD(seg) \
139 [VCPU_SREG_##seg] = { \
140 .selector = GUEST_##seg##_SELECTOR, \
141 .base = GUEST_##seg##_BASE, \
142 .limit = GUEST_##seg##_LIMIT, \
143 .ar_bytes = GUEST_##seg##_AR_BYTES, \
144 }
145
146static struct kvm_vmx_segment_field {
147 unsigned selector;
148 unsigned base;
149 unsigned limit;
150 unsigned ar_bytes;
151} kvm_vmx_segment_fields[] = {
152 VMX_SEGMENT_FIELD(CS),
153 VMX_SEGMENT_FIELD(DS),
154 VMX_SEGMENT_FIELD(ES),
155 VMX_SEGMENT_FIELD(FS),
156 VMX_SEGMENT_FIELD(GS),
157 VMX_SEGMENT_FIELD(SS),
158 VMX_SEGMENT_FIELD(TR),
159 VMX_SEGMENT_FIELD(LDTR),
160};
161
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300162/*
163 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
164 * away by decrementing the array size.
165 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800166static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800167#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800168 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE,
169#endif
170 MSR_EFER, MSR_K6_STAR,
171};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200172#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800173
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400174static void load_msrs(struct kvm_msr_entry *e, int n)
175{
176 int i;
177
178 for (i = 0; i < n; ++i)
179 wrmsrl(e[i].index, e[i].data);
180}
181
182static void save_msrs(struct kvm_msr_entry *e, int n)
183{
184 int i;
185
186 for (i = 0; i < n; ++i)
187 rdmsrl(e[i].index, e[i].data);
188}
189
Avi Kivity6aa8b732006-12-10 02:21:36 -0800190static inline int is_page_fault(u32 intr_info)
191{
192 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
193 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100194 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800195}
196
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300197static inline int is_no_device(u32 intr_info)
198{
199 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
200 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100201 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300202}
203
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500204static inline int is_invalid_opcode(u32 intr_info)
205{
206 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
207 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100208 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500209}
210
Avi Kivity6aa8b732006-12-10 02:21:36 -0800211static inline int is_external_interrupt(u32 intr_info)
212{
213 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
214 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
215}
216
Sheng Yang25c5f222008-03-28 13:18:56 +0800217static inline int cpu_has_vmx_msr_bitmap(void)
218{
219 return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS);
220}
221
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800222static inline int cpu_has_vmx_tpr_shadow(void)
223{
224 return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW);
225}
226
227static inline int vm_need_tpr_shadow(struct kvm *kvm)
228{
229 return ((cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm)));
230}
231
Sheng Yangf78e0e22007-10-29 09:40:42 +0800232static inline int cpu_has_secondary_exec_ctrls(void)
233{
234 return (vmcs_config.cpu_based_exec_ctrl &
235 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS);
236}
237
Avi Kivity774ead32007-12-26 13:57:04 +0200238static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800239{
Avi Kivity4c9fc8e2008-03-24 18:15:14 +0200240 return flexpriority_enabled
241 && (vmcs_config.cpu_based_2nd_exec_ctrl &
242 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800243}
244
Sheng Yangd56f5462008-04-25 10:13:16 +0800245static inline int cpu_has_vmx_invept_individual_addr(void)
246{
247 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT));
248}
249
250static inline int cpu_has_vmx_invept_context(void)
251{
252 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT));
253}
254
255static inline int cpu_has_vmx_invept_global(void)
256{
257 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT));
258}
259
260static inline int cpu_has_vmx_ept(void)
261{
262 return (vmcs_config.cpu_based_2nd_exec_ctrl &
263 SECONDARY_EXEC_ENABLE_EPT);
264}
265
Sheng Yangf78e0e22007-10-29 09:40:42 +0800266static inline int vm_need_virtualize_apic_accesses(struct kvm *kvm)
267{
268 return ((cpu_has_vmx_virtualize_apic_accesses()) &&
269 (irqchip_in_kernel(kvm)));
270}
271
Sheng Yang2384d2b2008-01-17 15:14:33 +0800272static inline int cpu_has_vmx_vpid(void)
273{
274 return (vmcs_config.cpu_based_2nd_exec_ctrl &
275 SECONDARY_EXEC_ENABLE_VPID);
276}
277
Sheng Yangf08864b2008-05-15 18:23:25 +0800278static inline int cpu_has_virtual_nmis(void)
279{
280 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
281}
282
Rusty Russell8b9cf982007-07-30 16:31:43 +1000283static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800284{
285 int i;
286
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400287 for (i = 0; i < vmx->nmsrs; ++i)
288 if (vmx->guest_msrs[i].index == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300289 return i;
290 return -1;
291}
292
Sheng Yang2384d2b2008-01-17 15:14:33 +0800293static inline void __invvpid(int ext, u16 vpid, gva_t gva)
294{
295 struct {
296 u64 vpid : 16;
297 u64 rsvd : 48;
298 u64 gva;
299 } operand = { vpid, 0, gva };
300
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300301 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800302 /* CF==1 or ZF==1 --> rc = -1 */
303 "; ja 1f ; ud2 ; 1:"
304 : : "a"(&operand), "c"(ext) : "cc", "memory");
305}
306
Sheng Yang14394422008-04-28 12:24:45 +0800307static inline void __invept(int ext, u64 eptp, gpa_t gpa)
308{
309 struct {
310 u64 eptp, gpa;
311 } operand = {eptp, gpa};
312
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300313 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800314 /* CF==1 or ZF==1 --> rc = -1 */
315 "; ja 1f ; ud2 ; 1:\n"
316 : : "a" (&operand), "c" (ext) : "cc", "memory");
317}
318
Rusty Russell8b9cf982007-07-30 16:31:43 +1000319static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300320{
321 int i;
322
Rusty Russell8b9cf982007-07-30 16:31:43 +1000323 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300324 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400325 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000326 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800327}
328
Avi Kivity6aa8b732006-12-10 02:21:36 -0800329static void vmcs_clear(struct vmcs *vmcs)
330{
331 u64 phys_addr = __pa(vmcs);
332 u8 error;
333
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300334 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800335 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
336 : "cc", "memory");
337 if (error)
338 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
339 vmcs, phys_addr);
340}
341
342static void __vcpu_clear(void *arg)
343{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000344 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800345 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800346
Rusty Russell8b9cf982007-07-30 16:31:43 +1000347 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400348 vmcs_clear(vmx->vmcs);
349 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800350 per_cpu(current_vmcs, cpu) = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800351 rdtscll(vmx->vcpu.arch.host_tsc);
Avi Kivity543e4242008-05-13 16:22:47 +0300352 list_del(&vmx->local_vcpus_link);
353 vmx->vcpu.cpu = -1;
354 vmx->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800355}
356
Rusty Russell8b9cf982007-07-30 16:31:43 +1000357static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800358{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200359 if (vmx->vcpu.cpu == -1)
360 return;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200361 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800362}
363
Sheng Yang2384d2b2008-01-17 15:14:33 +0800364static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx)
365{
366 if (vmx->vpid == 0)
367 return;
368
369 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
370}
371
Sheng Yang14394422008-04-28 12:24:45 +0800372static inline void ept_sync_global(void)
373{
374 if (cpu_has_vmx_invept_global())
375 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
376}
377
378static inline void ept_sync_context(u64 eptp)
379{
Avi Kivity089d0342009-03-23 18:26:32 +0200380 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800381 if (cpu_has_vmx_invept_context())
382 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
383 else
384 ept_sync_global();
385 }
386}
387
388static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
389{
Avi Kivity089d0342009-03-23 18:26:32 +0200390 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800391 if (cpu_has_vmx_invept_individual_addr())
392 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
393 eptp, gpa);
394 else
395 ept_sync_context(eptp);
396 }
397}
398
Avi Kivity6aa8b732006-12-10 02:21:36 -0800399static unsigned long vmcs_readl(unsigned long field)
400{
401 unsigned long value;
402
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300403 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800404 : "=a"(value) : "d"(field) : "cc");
405 return value;
406}
407
408static u16 vmcs_read16(unsigned long field)
409{
410 return vmcs_readl(field);
411}
412
413static u32 vmcs_read32(unsigned long field)
414{
415 return vmcs_readl(field);
416}
417
418static u64 vmcs_read64(unsigned long field)
419{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800420#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800421 return vmcs_readl(field);
422#else
423 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
424#endif
425}
426
Avi Kivitye52de1b2007-01-05 16:36:56 -0800427static noinline void vmwrite_error(unsigned long field, unsigned long value)
428{
429 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
430 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
431 dump_stack();
432}
433
Avi Kivity6aa8b732006-12-10 02:21:36 -0800434static void vmcs_writel(unsigned long field, unsigned long value)
435{
436 u8 error;
437
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300438 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400439 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800440 if (unlikely(error))
441 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800442}
443
444static void vmcs_write16(unsigned long field, u16 value)
445{
446 vmcs_writel(field, value);
447}
448
449static void vmcs_write32(unsigned long field, u32 value)
450{
451 vmcs_writel(field, value);
452}
453
454static void vmcs_write64(unsigned long field, u64 value)
455{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800456 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +0300457#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800458 asm volatile ("");
459 vmcs_writel(field+1, value >> 32);
460#endif
461}
462
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300463static void vmcs_clear_bits(unsigned long field, u32 mask)
464{
465 vmcs_writel(field, vmcs_readl(field) & ~mask);
466}
467
468static void vmcs_set_bits(unsigned long field, u32 mask)
469{
470 vmcs_writel(field, vmcs_readl(field) | mask);
471}
472
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300473static void update_exception_bitmap(struct kvm_vcpu *vcpu)
474{
475 u32 eb;
476
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500477 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300478 if (!vcpu->fpu_active)
479 eb |= 1u << NM_VECTOR;
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100480 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
481 if (vcpu->guest_debug &
482 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
483 eb |= 1u << DB_VECTOR;
484 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
485 eb |= 1u << BP_VECTOR;
486 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800487 if (vcpu->arch.rmode.active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300488 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +0200489 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +0800490 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300491 vmcs_write32(EXCEPTION_BITMAP, eb);
492}
493
Avi Kivity33ed6322007-05-02 16:54:03 +0300494static void reload_tss(void)
495{
Avi Kivity33ed6322007-05-02 16:54:03 +0300496 /*
497 * VT restores TR but not its size. Useless.
498 */
499 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200500 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300501
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300502 kvm_get_gdt(&gdt);
Avi Kivity33ed6322007-05-02 16:54:03 +0300503 descs = (void *)gdt.base;
504 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
505 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300506}
507
Rusty Russell8b9cf982007-07-30 16:31:43 +1000508static void load_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300509{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400510 int efer_offset = vmx->msr_offset_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300511 u64 host_efer = vmx->host_msrs[efer_offset].data;
512 u64 guest_efer = vmx->guest_msrs[efer_offset].data;
513 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300514
Avi Kivity51c6cf62007-08-29 03:48:05 +0300515 if (efer_offset < 0)
516 return;
517 /*
518 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
519 * outside long mode
520 */
521 ignore_bits = EFER_NX | EFER_SCE;
522#ifdef CONFIG_X86_64
523 ignore_bits |= EFER_LMA | EFER_LME;
524 /* SCE is meaningful only in long mode on Intel */
525 if (guest_efer & EFER_LMA)
526 ignore_bits &= ~(u64)EFER_SCE;
527#endif
528 if ((guest_efer & ~ignore_bits) == (host_efer & ~ignore_bits))
529 return;
530
531 vmx->host_state.guest_efer_loaded = 1;
532 guest_efer &= ~ignore_bits;
533 guest_efer |= host_efer & ignore_bits;
534 wrmsrl(MSR_EFER, guest_efer);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000535 vmx->vcpu.stat.efer_reload++;
Eddie Dong2cc51562007-05-21 07:28:09 +0300536}
537
Avi Kivity51c6cf62007-08-29 03:48:05 +0300538static void reload_host_efer(struct vcpu_vmx *vmx)
539{
540 if (vmx->host_state.guest_efer_loaded) {
541 vmx->host_state.guest_efer_loaded = 0;
542 load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1);
543 }
544}
545
Avi Kivity04d2cc72007-09-10 18:10:54 +0300546static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300547{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300548 struct vcpu_vmx *vmx = to_vmx(vcpu);
549
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400550 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300551 return;
552
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400553 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300554 /*
555 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
556 * allow segment selectors with cpl > 0 or ti == 1.
557 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300558 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200559 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300560 vmx->host_state.fs_sel = kvm_read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200561 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400562 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200563 vmx->host_state.fs_reload_needed = 0;
564 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300565 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200566 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300567 }
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300568 vmx->host_state.gs_sel = kvm_read_gs();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400569 if (!(vmx->host_state.gs_sel & 7))
570 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300571 else {
572 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200573 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300574 }
575
576#ifdef CONFIG_X86_64
577 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
578 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
579#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400580 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
581 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300582#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300583
584#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400585 if (is_long_mode(&vmx->vcpu))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400586 save_msrs(vmx->host_msrs +
587 vmx->msr_offset_kernel_gs_base, 1);
Mike Dayd77c26f2007-10-08 09:02:08 -0400588
Avi Kivity707c0872007-05-02 17:33:43 +0300589#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400590 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300591 load_transition_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300592}
593
Avi Kivitya9b21b62008-06-24 11:48:49 +0300594static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300595{
Avi Kivity15ad7142007-07-11 18:17:21 +0300596 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300597
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400598 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300599 return;
600
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200601 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400602 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200603 if (vmx->host_state.fs_reload_needed)
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300604 kvm_load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200605 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300606 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300607 /*
608 * If we have to reload gs, we must take care to
609 * preserve our gs base.
610 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300611 local_irq_save(flags);
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300612 kvm_load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300613#ifdef CONFIG_X86_64
614 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
615#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300616 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300617 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200618 reload_tss();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400619 save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
620 load_msrs(vmx->host_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300621 reload_host_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300622}
623
Avi Kivitya9b21b62008-06-24 11:48:49 +0300624static void vmx_load_host_state(struct vcpu_vmx *vmx)
625{
626 preempt_disable();
627 __vmx_load_host_state(vmx);
628 preempt_enable();
629}
630
Avi Kivity6aa8b732006-12-10 02:21:36 -0800631/*
632 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
633 * vcpu mutex is already taken.
634 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300635static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800636{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400637 struct vcpu_vmx *vmx = to_vmx(vcpu);
638 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity019960a2008-03-04 10:44:51 +0200639 u64 tsc_this, delta, new_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800640
Eddie Donga3d7f852007-09-03 16:15:12 +0300641 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000642 vcpu_clear(vmx);
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300643 kvm_migrate_timers(vcpu);
Sheng Yang2384d2b2008-01-17 15:14:33 +0800644 vpid_sync_vcpu_all(vmx);
Avi Kivity543e4242008-05-13 16:22:47 +0300645 local_irq_disable();
646 list_add(&vmx->local_vcpus_link,
647 &per_cpu(vcpus_on_cpu, cpu));
648 local_irq_enable();
Eddie Donga3d7f852007-09-03 16:15:12 +0300649 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800650
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400651 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800652 u8 error;
653
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400654 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300655 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800656 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
657 : "cc");
658 if (error)
659 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400660 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800661 }
662
663 if (vcpu->cpu != cpu) {
664 struct descriptor_table dt;
665 unsigned long sysenter_esp;
666
667 vcpu->cpu = cpu;
668 /*
669 * Linux uses per-cpu TSS and GDT, so set these when switching
670 * processors.
671 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300672 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
673 kvm_get_gdt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800674 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
675
676 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
677 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300678
679 /*
680 * Make sure the time stamp counter is monotonous.
681 */
682 rdtscll(tsc_this);
Avi Kivity019960a2008-03-04 10:44:51 +0200683 if (tsc_this < vcpu->arch.host_tsc) {
684 delta = vcpu->arch.host_tsc - tsc_this;
685 new_offset = vmcs_read64(TSC_OFFSET) + delta;
686 vmcs_write64(TSC_OFFSET, new_offset);
687 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800688 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800689}
690
691static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
692{
Avi Kivitya9b21b62008-06-24 11:48:49 +0300693 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800694}
695
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300696static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
697{
698 if (vcpu->fpu_active)
699 return;
700 vcpu->fpu_active = 1;
Rusty Russell707d92f2007-07-17 23:19:08 +1000701 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800702 if (vcpu->arch.cr0 & X86_CR0_TS)
Rusty Russell707d92f2007-07-17 23:19:08 +1000703 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300704 update_exception_bitmap(vcpu);
705}
706
707static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
708{
709 if (!vcpu->fpu_active)
710 return;
711 vcpu->fpu_active = 0;
Rusty Russell707d92f2007-07-17 23:19:08 +1000712 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300713 update_exception_bitmap(vcpu);
714}
715
Avi Kivity6aa8b732006-12-10 02:21:36 -0800716static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
717{
718 return vmcs_readl(GUEST_RFLAGS);
719}
720
721static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
722{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800723 if (vcpu->arch.rmode.active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100724 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800725 vmcs_writel(GUEST_RFLAGS, rflags);
726}
727
728static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
729{
730 unsigned long rip;
731 u32 interruptibility;
732
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300733 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800734 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300735 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800736
737 /*
738 * We emulated an instruction, so temporary interrupt blocking
739 * should be removed, if set.
740 */
741 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
742 if (interruptibility & 3)
743 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
744 interruptibility & ~3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800745 vcpu->arch.interrupt_window_open = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800746}
747
Avi Kivity298101d2007-11-25 13:41:11 +0200748static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
749 bool has_error_code, u32 error_code)
750{
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200751 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100752 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200753
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100754 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200755 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100756 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
757 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200758
759 if (vcpu->arch.rmode.active) {
760 vmx->rmode.irq.pending = true;
761 vmx->rmode.irq.vector = nr;
762 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100763 if (nr == BP_VECTOR || nr == OF_VECTOR)
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200764 vmx->rmode.irq.rip++;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100765 intr_info |= INTR_TYPE_SOFT_INTR;
766 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200767 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
768 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
769 return;
770 }
771
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100772 if (nr == BP_VECTOR || nr == OF_VECTOR) {
773 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
774 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
775 } else
776 intr_info |= INTR_TYPE_HARD_EXCEPTION;
777
778 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +0200779}
780
781static bool vmx_exception_injected(struct kvm_vcpu *vcpu)
782{
Avi Kivity35920a32008-07-03 14:50:12 +0300783 return false;
Avi Kivity298101d2007-11-25 13:41:11 +0200784}
785
Avi Kivity6aa8b732006-12-10 02:21:36 -0800786/*
Eddie Donga75beee2007-05-17 18:55:15 +0300787 * Swap MSR entry in host/guest MSR entry array.
788 */
Gabriel C54e11fa2007-08-01 16:23:10 +0200789#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000790static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300791{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400792 struct kvm_msr_entry tmp;
793
794 tmp = vmx->guest_msrs[to];
795 vmx->guest_msrs[to] = vmx->guest_msrs[from];
796 vmx->guest_msrs[from] = tmp;
797 tmp = vmx->host_msrs[to];
798 vmx->host_msrs[to] = vmx->host_msrs[from];
799 vmx->host_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300800}
Gabriel C54e11fa2007-08-01 16:23:10 +0200801#endif
Eddie Donga75beee2007-05-17 18:55:15 +0300802
803/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300804 * Set up the vmcs to automatically save and restore system
805 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
806 * mode, as fiddling with msrs is very expensive.
807 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000808static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300809{
Eddie Dong2cc51562007-05-21 07:28:09 +0300810 int save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +0200811 unsigned long *msr_bitmap;
Avi Kivitye38aea32007-04-19 13:22:48 +0300812
Avi Kivity33f9c502008-02-27 16:06:57 +0200813 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +0300814 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300815#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000816 if (is_long_mode(&vmx->vcpu)) {
Eddie Dong2cc51562007-05-21 07:28:09 +0300817 int index;
818
Rusty Russell8b9cf982007-07-30 16:31:43 +1000819 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300820 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000821 move_msr_up(vmx, index, save_nmsrs++);
822 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300823 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000824 move_msr_up(vmx, index, save_nmsrs++);
825 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300826 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000827 move_msr_up(vmx, index, save_nmsrs++);
828 index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300829 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000830 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300831 /*
832 * MSR_K6_STAR is only needed on long mode guests, and only
833 * if efer.sce is enabled.
834 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000835 index = __find_msr_index(vmx, MSR_K6_STAR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800836 if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +1000837 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300838 }
Eddie Donga75beee2007-05-17 18:55:15 +0300839#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400840 vmx->save_nmsrs = save_nmsrs;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300841
Eddie Donga75beee2007-05-17 18:55:15 +0300842#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400843 vmx->msr_offset_kernel_gs_base =
Rusty Russell8b9cf982007-07-30 16:31:43 +1000844 __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300845#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +1000846 vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
Avi Kivity58972972009-02-24 22:26:47 +0200847
848 if (cpu_has_vmx_msr_bitmap()) {
849 if (is_long_mode(&vmx->vcpu))
850 msr_bitmap = vmx_msr_bitmap_longmode;
851 else
852 msr_bitmap = vmx_msr_bitmap_legacy;
853
854 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
855 }
Avi Kivitye38aea32007-04-19 13:22:48 +0300856}
857
858/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800859 * reads and returns guest's timestamp counter "register"
860 * guest_tsc = host_tsc + tsc_offset -- 21.3
861 */
862static u64 guest_read_tsc(void)
863{
864 u64 host_tsc, tsc_offset;
865
866 rdtscll(host_tsc);
867 tsc_offset = vmcs_read64(TSC_OFFSET);
868 return host_tsc + tsc_offset;
869}
870
871/*
872 * writes 'guest_tsc' into guest's timestamp counter "register"
873 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
874 */
Marcelo Tosatti53f658b2008-12-11 20:45:05 +0100875static void guest_write_tsc(u64 guest_tsc, u64 host_tsc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800876{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800877 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
878}
879
Avi Kivity6aa8b732006-12-10 02:21:36 -0800880/*
881 * Reads an msr value (of 'msr_index') into 'pdata'.
882 * Returns 0 on success, non-0 otherwise.
883 * Assumes vcpu_load() was already called.
884 */
885static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
886{
887 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400888 struct kvm_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800889
890 if (!pdata) {
891 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
892 return -EINVAL;
893 }
894
895 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800896#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800897 case MSR_FS_BASE:
898 data = vmcs_readl(GUEST_FS_BASE);
899 break;
900 case MSR_GS_BASE:
901 data = vmcs_readl(GUEST_GS_BASE);
902 break;
903 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800904 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905#endif
906 case MSR_IA32_TIME_STAMP_COUNTER:
907 data = guest_read_tsc();
908 break;
909 case MSR_IA32_SYSENTER_CS:
910 data = vmcs_read32(GUEST_SYSENTER_CS);
911 break;
912 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200913 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800914 break;
915 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200916 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800917 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800918 default:
Avi Kivity516a1a72009-02-15 02:32:07 +0200919 vmx_load_host_state(to_vmx(vcpu));
Rusty Russell8b9cf982007-07-30 16:31:43 +1000920 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800921 if (msr) {
922 data = msr->data;
923 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924 }
Avi Kivity3bab1f52006-12-29 16:49:48 -0800925 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926 }
927
928 *pdata = data;
929 return 0;
930}
931
932/*
933 * Writes msr value into into the appropriate "register".
934 * Returns 0 on success, non-0 otherwise.
935 * Assumes vcpu_load() was already called.
936 */
937static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
938{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400939 struct vcpu_vmx *vmx = to_vmx(vcpu);
940 struct kvm_msr_entry *msr;
Marcelo Tosatti53f658b2008-12-11 20:45:05 +0100941 u64 host_tsc;
Eddie Dong2cc51562007-05-21 07:28:09 +0300942 int ret = 0;
943
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -0800945 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +0300946 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +0300947 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +0300948 break;
Avi Kivity16175a72009-03-23 22:13:44 +0200949#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800950 case MSR_FS_BASE:
951 vmcs_writel(GUEST_FS_BASE, data);
952 break;
953 case MSR_GS_BASE:
954 vmcs_writel(GUEST_GS_BASE, data);
955 break;
956#endif
957 case MSR_IA32_SYSENTER_CS:
958 vmcs_write32(GUEST_SYSENTER_CS, data);
959 break;
960 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200961 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962 break;
963 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200964 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965 break;
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200966 case MSR_IA32_TIME_STAMP_COUNTER:
Marcelo Tosatti53f658b2008-12-11 20:45:05 +0100967 rdtscll(host_tsc);
968 guest_write_tsc(data, host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969 break;
Chris Lalancetteefa67e02008-06-20 09:51:30 +0200970 case MSR_P6_PERFCTR0:
971 case MSR_P6_PERFCTR1:
972 case MSR_P6_EVNTSEL0:
973 case MSR_P6_EVNTSEL1:
974 /*
975 * Just discard all writes to the performance counters; this
976 * should keep both older linux and windows 64-bit guests
977 * happy
978 */
979 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: 0x%x data 0x%llx\n", msr_index, data);
980
981 break;
Sheng Yang468d4722008-10-09 16:01:55 +0800982 case MSR_IA32_CR_PAT:
983 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
984 vmcs_write64(GUEST_IA32_PAT, data);
985 vcpu->arch.pat = data;
986 break;
987 }
988 /* Otherwise falls through to kvm_set_msr_common */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800989 default:
Avi Kivitya9b21b62008-06-24 11:48:49 +0300990 vmx_load_host_state(vmx);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000991 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800992 if (msr) {
993 msr->data = data;
994 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995 }
Eddie Dong2cc51562007-05-21 07:28:09 +0300996 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997 }
998
Eddie Dong2cc51562007-05-21 07:28:09 +0300999 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001000}
1001
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001002static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001004 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
1005 switch (reg) {
1006 case VCPU_REGS_RSP:
1007 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
1008 break;
1009 case VCPU_REGS_RIP:
1010 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
1011 break;
1012 default:
1013 break;
1014 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015}
1016
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001017static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001018{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001019 int old_debug = vcpu->guest_debug;
1020 unsigned long flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001022 vcpu->guest_debug = dbg->control;
1023 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
1024 vcpu->guest_debug = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001025
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001026 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1027 vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
1028 else
1029 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
1030
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001031 flags = vmcs_readl(GUEST_RFLAGS);
1032 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
1033 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
1034 else if (old_debug & KVM_GUESTDBG_SINGLESTEP)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035 flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001036 vmcs_writel(GUEST_RFLAGS, flags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001037
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001038 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001039
1040 return 0;
1041}
1042
Eddie Dong2a8067f2007-08-06 16:29:07 +03001043static int vmx_get_irq(struct kvm_vcpu *vcpu)
1044{
Avi Kivityf7d92382008-07-03 16:14:28 +03001045 if (!vcpu->arch.interrupt.pending)
1046 return -1;
1047 return vcpu->arch.interrupt.nr;
Eddie Dong2a8067f2007-08-06 16:29:07 +03001048}
1049
Avi Kivity6aa8b732006-12-10 02:21:36 -08001050static __init int cpu_has_kvm_support(void)
1051{
Eduardo Habkost6210e372008-11-17 19:03:16 -02001052 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001053}
1054
1055static __init int vmx_disabled_by_bios(void)
1056{
1057 u64 msr;
1058
1059 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001060 return (msr & (FEATURE_CONTROL_LOCKED |
1061 FEATURE_CONTROL_VMXON_ENABLED))
1062 == FEATURE_CONTROL_LOCKED;
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001063 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064}
1065
Avi Kivity774c47f2007-02-12 00:54:47 -08001066static void hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001067{
1068 int cpu = raw_smp_processor_id();
1069 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
1070 u64 old;
1071
Avi Kivity543e4242008-05-13 16:22:47 +03001072 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001073 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001074 if ((old & (FEATURE_CONTROL_LOCKED |
1075 FEATURE_CONTROL_VMXON_ENABLED))
1076 != (FEATURE_CONTROL_LOCKED |
1077 FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001078 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001079 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
Sheng Yang9ea542f2008-09-11 15:27:49 +08001080 FEATURE_CONTROL_LOCKED |
1081 FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +10001082 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001083 asm volatile (ASM_VMX_VMXON_RAX
1084 : : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001085 : "memory", "cc");
1086}
1087
Avi Kivity543e4242008-05-13 16:22:47 +03001088static void vmclear_local_vcpus(void)
1089{
1090 int cpu = raw_smp_processor_id();
1091 struct vcpu_vmx *vmx, *n;
1092
1093 list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
1094 local_vcpus_link)
1095 __vcpu_clear(vmx);
1096}
1097
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001098
1099/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1100 * tricks.
1101 */
1102static void kvm_cpu_vmxoff(void)
1103{
1104 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
1105 write_cr4(read_cr4() & ~X86_CR4_VMXE);
1106}
1107
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108static void hardware_disable(void *garbage)
1109{
Avi Kivity543e4242008-05-13 16:22:47 +03001110 vmclear_local_vcpus();
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001111 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112}
1113
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001114static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001115 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001116{
1117 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001118 u32 ctl = ctl_min | ctl_opt;
1119
1120 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1121
1122 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1123 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1124
1125 /* Ensure minimum (required) set of control bits are supported. */
1126 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001127 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001128
1129 *result = ctl;
1130 return 0;
1131}
1132
Yang, Sheng002c7f72007-07-31 14:23:01 +03001133static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001134{
1135 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001136 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001137 u32 _pin_based_exec_control = 0;
1138 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001139 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001140 u32 _vmexit_control = 0;
1141 u32 _vmentry_control = 0;
1142
1143 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001144 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001145 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1146 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001147 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001148
1149 min = CPU_BASED_HLT_EXITING |
1150#ifdef CONFIG_X86_64
1151 CPU_BASED_CR8_LOAD_EXITING |
1152 CPU_BASED_CR8_STORE_EXITING |
1153#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001154 CPU_BASED_CR3_LOAD_EXITING |
1155 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001156 CPU_BASED_USE_IO_BITMAPS |
1157 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001158 CPU_BASED_USE_TSC_OFFSETING |
1159 CPU_BASED_INVLPG_EXITING;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001160 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001161 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001162 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001163 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1164 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001165 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001166#ifdef CONFIG_X86_64
1167 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1168 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1169 ~CPU_BASED_CR8_STORE_EXITING;
1170#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001171 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001172 min2 = 0;
1173 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001174 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001175 SECONDARY_EXEC_ENABLE_VPID |
1176 SECONDARY_EXEC_ENABLE_EPT;
1177 if (adjust_vmx_controls(min2, opt2,
1178 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001179 &_cpu_based_2nd_exec_control) < 0)
1180 return -EIO;
1181 }
1182#ifndef CONFIG_X86_64
1183 if (!(_cpu_based_2nd_exec_control &
1184 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1185 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1186#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001187 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03001188 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1189 enabled */
Sheng Yangd56f5462008-04-25 10:13:16 +08001190 min &= ~(CPU_BASED_CR3_LOAD_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001191 CPU_BASED_CR3_STORE_EXITING |
1192 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08001193 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1194 &_cpu_based_exec_control) < 0)
1195 return -EIO;
1196 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1197 vmx_capability.ept, vmx_capability.vpid);
1198 }
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001199
Avi Kivity919818a2009-03-23 18:01:29 +02001200 if (!cpu_has_vmx_vpid())
1201 enable_vpid = 0;
1202
Avi Kivity575ff2d2009-03-23 18:25:15 +02001203 if (!cpu_has_vmx_ept())
1204 enable_ept = 0;
1205
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001206 min = 0;
1207#ifdef CONFIG_X86_64
1208 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1209#endif
Sheng Yang468d4722008-10-09 16:01:55 +08001210 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001211 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1212 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001213 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001214
Sheng Yang468d4722008-10-09 16:01:55 +08001215 min = 0;
1216 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001217 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1218 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001219 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001220
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001221 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001222
1223 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1224 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001225 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001226
1227#ifdef CONFIG_X86_64
1228 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1229 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001230 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001231#endif
1232
1233 /* Require Write-Back (WB) memory type for VMCS accesses. */
1234 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001235 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001236
Yang, Sheng002c7f72007-07-31 14:23:01 +03001237 vmcs_conf->size = vmx_msr_high & 0x1fff;
1238 vmcs_conf->order = get_order(vmcs_config.size);
1239 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001240
Yang, Sheng002c7f72007-07-31 14:23:01 +03001241 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1242 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001243 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001244 vmcs_conf->vmexit_ctrl = _vmexit_control;
1245 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001246
1247 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001248}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001249
1250static struct vmcs *alloc_vmcs_cpu(int cpu)
1251{
1252 int node = cpu_to_node(cpu);
1253 struct page *pages;
1254 struct vmcs *vmcs;
1255
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001256 pages = alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001257 if (!pages)
1258 return NULL;
1259 vmcs = page_address(pages);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001260 memset(vmcs, 0, vmcs_config.size);
1261 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001262 return vmcs;
1263}
1264
1265static struct vmcs *alloc_vmcs(void)
1266{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001267 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001268}
1269
1270static void free_vmcs(struct vmcs *vmcs)
1271{
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001272 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001273}
1274
Sam Ravnborg39959582007-06-01 00:47:13 -07001275static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001276{
1277 int cpu;
1278
1279 for_each_online_cpu(cpu)
1280 free_vmcs(per_cpu(vmxarea, cpu));
1281}
1282
Avi Kivity6aa8b732006-12-10 02:21:36 -08001283static __init int alloc_kvm_area(void)
1284{
1285 int cpu;
1286
1287 for_each_online_cpu(cpu) {
1288 struct vmcs *vmcs;
1289
1290 vmcs = alloc_vmcs_cpu(cpu);
1291 if (!vmcs) {
1292 free_kvm_area();
1293 return -ENOMEM;
1294 }
1295
1296 per_cpu(vmxarea, cpu) = vmcs;
1297 }
1298 return 0;
1299}
1300
1301static __init int hardware_setup(void)
1302{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001303 if (setup_vmcs_config(&vmcs_config) < 0)
1304 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001305
1306 if (boot_cpu_has(X86_FEATURE_NX))
1307 kvm_enable_efer_bits(EFER_NX);
1308
Avi Kivity6aa8b732006-12-10 02:21:36 -08001309 return alloc_kvm_area();
1310}
1311
1312static __exit void hardware_unsetup(void)
1313{
1314 free_kvm_area();
1315}
1316
Avi Kivity6aa8b732006-12-10 02:21:36 -08001317static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1318{
1319 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1320
Avi Kivity6af11b92007-03-19 13:18:10 +02001321 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001322 vmcs_write16(sf->selector, save->selector);
1323 vmcs_writel(sf->base, save->base);
1324 vmcs_write32(sf->limit, save->limit);
1325 vmcs_write32(sf->ar_bytes, save->ar);
1326 } else {
1327 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1328 << AR_DPL_SHIFT;
1329 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1330 }
1331}
1332
1333static void enter_pmode(struct kvm_vcpu *vcpu)
1334{
1335 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001336 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001337
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001338 vmx->emulation_required = 1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001339 vcpu->arch.rmode.active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001340
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001341 vmcs_writel(GUEST_TR_BASE, vcpu->arch.rmode.tr.base);
1342 vmcs_write32(GUEST_TR_LIMIT, vcpu->arch.rmode.tr.limit);
1343 vmcs_write32(GUEST_TR_AR_BYTES, vcpu->arch.rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001344
1345 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001346 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001347 flags |= (vcpu->arch.rmode.save_iopl << IOPL_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001348 vmcs_writel(GUEST_RFLAGS, flags);
1349
Rusty Russell66aee912007-07-17 23:34:16 +10001350 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1351 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001352
1353 update_exception_bitmap(vcpu);
1354
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001355 if (emulate_invalid_guest_state)
1356 return;
1357
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001358 fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1359 fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1360 fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1361 fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001362
1363 vmcs_write16(GUEST_SS_SELECTOR, 0);
1364 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1365
1366 vmcs_write16(GUEST_CS_SELECTOR,
1367 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1368 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1369}
1370
Mike Dayd77c26f2007-10-08 09:02:08 -04001371static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001372{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001373 if (!kvm->arch.tss_addr) {
Izik Eiduscbc94022007-10-25 00:29:55 +02001374 gfn_t base_gfn = kvm->memslots[0].base_gfn +
1375 kvm->memslots[0].npages - 3;
1376 return base_gfn << PAGE_SHIFT;
1377 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001378 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001379}
1380
1381static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1382{
1383 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1384
1385 save->selector = vmcs_read16(sf->selector);
1386 save->base = vmcs_readl(sf->base);
1387 save->limit = vmcs_read32(sf->limit);
1388 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001389 vmcs_write16(sf->selector, save->base >> 4);
1390 vmcs_write32(sf->base, save->base & 0xfffff);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001391 vmcs_write32(sf->limit, 0xffff);
1392 vmcs_write32(sf->ar_bytes, 0xf3);
1393}
1394
1395static void enter_rmode(struct kvm_vcpu *vcpu)
1396{
1397 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001398 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001399
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001400 vmx->emulation_required = 1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001401 vcpu->arch.rmode.active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001402
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001403 vcpu->arch.rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001404 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1405
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001406 vcpu->arch.rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001407 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1408
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001409 vcpu->arch.rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001410 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1411
1412 flags = vmcs_readl(GUEST_RFLAGS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001413 vcpu->arch.rmode.save_iopl
1414 = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001415
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001416 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001417
1418 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001419 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001420 update_exception_bitmap(vcpu);
1421
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001422 if (emulate_invalid_guest_state)
1423 goto continue_rmode;
1424
Avi Kivity6aa8b732006-12-10 02:21:36 -08001425 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1426 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1427 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1428
1429 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001430 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001431 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1432 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001433 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1434
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001435 fix_rmode_seg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1436 fix_rmode_seg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1437 fix_rmode_seg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1438 fix_rmode_seg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001439
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001440continue_rmode:
Eddie Dong8668a3c2007-10-10 14:26:45 +08001441 kvm_mmu_reset_context(vcpu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001442 init_rmode(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001443}
1444
Amit Shah401d10d2009-02-20 22:53:37 +05301445static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1446{
1447 struct vcpu_vmx *vmx = to_vmx(vcpu);
1448 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
1449
1450 vcpu->arch.shadow_efer = efer;
1451 if (!msr)
1452 return;
1453 if (efer & EFER_LMA) {
1454 vmcs_write32(VM_ENTRY_CONTROLS,
1455 vmcs_read32(VM_ENTRY_CONTROLS) |
1456 VM_ENTRY_IA32E_MODE);
1457 msr->data = efer;
1458 } else {
1459 vmcs_write32(VM_ENTRY_CONTROLS,
1460 vmcs_read32(VM_ENTRY_CONTROLS) &
1461 ~VM_ENTRY_IA32E_MODE);
1462
1463 msr->data = efer & ~EFER_LME;
1464 }
1465 setup_msrs(vmx);
1466}
1467
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001468#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001469
1470static void enter_lmode(struct kvm_vcpu *vcpu)
1471{
1472 u32 guest_tr_ar;
1473
1474 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1475 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1476 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001477 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001478 vmcs_write32(GUEST_TR_AR_BYTES,
1479 (guest_tr_ar & ~AR_TYPE_MASK)
1480 | AR_TYPE_BUSY_64_TSS);
1481 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001482 vcpu->arch.shadow_efer |= EFER_LMA;
Amit Shah401d10d2009-02-20 22:53:37 +05301483 vmx_set_efer(vcpu, vcpu->arch.shadow_efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001484}
1485
1486static void exit_lmode(struct kvm_vcpu *vcpu)
1487{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001488 vcpu->arch.shadow_efer &= ~EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001489
1490 vmcs_write32(VM_ENTRY_CONTROLS,
1491 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001492 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001493}
1494
1495#endif
1496
Sheng Yang2384d2b2008-01-17 15:14:33 +08001497static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
1498{
1499 vpid_sync_vcpu_all(to_vmx(vcpu));
Avi Kivity089d0342009-03-23 18:26:32 +02001500 if (enable_ept)
Sheng Yang4e1096d2008-07-06 19:16:51 +08001501 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Sheng Yang2384d2b2008-01-17 15:14:33 +08001502}
1503
Anthony Liguori25c4c272007-04-27 09:29:21 +03001504static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001505{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001506 vcpu->arch.cr4 &= KVM_GUEST_CR4_MASK;
1507 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
Avi Kivity399badf2007-01-05 16:36:38 -08001508}
1509
Sheng Yang14394422008-04-28 12:24:45 +08001510static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
1511{
1512 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1513 if (!load_pdptrs(vcpu, vcpu->arch.cr3)) {
1514 printk(KERN_ERR "EPT: Fail to load pdptrs!\n");
1515 return;
1516 }
1517 vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]);
1518 vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]);
1519 vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]);
1520 vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]);
1521 }
1522}
1523
1524static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1525
1526static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
1527 unsigned long cr0,
1528 struct kvm_vcpu *vcpu)
1529{
1530 if (!(cr0 & X86_CR0_PG)) {
1531 /* From paging/starting to nonpaging */
1532 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001533 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08001534 (CPU_BASED_CR3_LOAD_EXITING |
1535 CPU_BASED_CR3_STORE_EXITING));
1536 vcpu->arch.cr0 = cr0;
1537 vmx_set_cr4(vcpu, vcpu->arch.cr4);
1538 *hw_cr0 |= X86_CR0_PE | X86_CR0_PG;
1539 *hw_cr0 &= ~X86_CR0_WP;
1540 } else if (!is_paging(vcpu)) {
1541 /* From nonpaging to paging */
1542 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001543 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08001544 ~(CPU_BASED_CR3_LOAD_EXITING |
1545 CPU_BASED_CR3_STORE_EXITING));
1546 vcpu->arch.cr0 = cr0;
1547 vmx_set_cr4(vcpu, vcpu->arch.cr4);
1548 if (!(vcpu->arch.cr0 & X86_CR0_WP))
1549 *hw_cr0 &= ~X86_CR0_WP;
1550 }
1551}
1552
1553static void ept_update_paging_mode_cr4(unsigned long *hw_cr4,
1554 struct kvm_vcpu *vcpu)
1555{
1556 if (!is_paging(vcpu)) {
1557 *hw_cr4 &= ~X86_CR4_PAE;
1558 *hw_cr4 |= X86_CR4_PSE;
1559 } else if (!(vcpu->arch.cr4 & X86_CR4_PAE))
1560 *hw_cr4 &= ~X86_CR4_PAE;
1561}
1562
Avi Kivity6aa8b732006-12-10 02:21:36 -08001563static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1564{
Sheng Yang14394422008-04-28 12:24:45 +08001565 unsigned long hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) |
1566 KVM_VM_CR0_ALWAYS_ON;
1567
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001568 vmx_fpu_deactivate(vcpu);
1569
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001570 if (vcpu->arch.rmode.active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001571 enter_pmode(vcpu);
1572
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001573 if (!vcpu->arch.rmode.active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001574 enter_rmode(vcpu);
1575
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001576#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001577 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92f2007-07-17 23:19:08 +10001578 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001579 enter_lmode(vcpu);
Rusty Russell707d92f2007-07-17 23:19:08 +10001580 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001581 exit_lmode(vcpu);
1582 }
1583#endif
1584
Avi Kivity089d0342009-03-23 18:26:32 +02001585 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001586 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
1587
Avi Kivity6aa8b732006-12-10 02:21:36 -08001588 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08001589 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001590 vcpu->arch.cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001591
Rusty Russell707d92f2007-07-17 23:19:08 +10001592 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001593 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001594}
1595
Sheng Yang14394422008-04-28 12:24:45 +08001596static u64 construct_eptp(unsigned long root_hpa)
1597{
1598 u64 eptp;
1599
1600 /* TODO write the value reading from MSR */
1601 eptp = VMX_EPT_DEFAULT_MT |
1602 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
1603 eptp |= (root_hpa & PAGE_MASK);
1604
1605 return eptp;
1606}
1607
Avi Kivity6aa8b732006-12-10 02:21:36 -08001608static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1609{
Sheng Yang14394422008-04-28 12:24:45 +08001610 unsigned long guest_cr3;
1611 u64 eptp;
1612
1613 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02001614 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001615 eptp = construct_eptp(cr3);
1616 vmcs_write64(EPT_POINTER, eptp);
1617 ept_sync_context(eptp);
1618 ept_load_pdptrs(vcpu);
1619 guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 :
1620 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
1621 }
1622
Sheng Yang2384d2b2008-01-17 15:14:33 +08001623 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001624 vmcs_writel(GUEST_CR3, guest_cr3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001625 if (vcpu->arch.cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001626 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001627}
1628
1629static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1630{
Sheng Yang14394422008-04-28 12:24:45 +08001631 unsigned long hw_cr4 = cr4 | (vcpu->arch.rmode.active ?
1632 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
1633
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001634 vcpu->arch.cr4 = cr4;
Avi Kivity089d0342009-03-23 18:26:32 +02001635 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001636 ept_update_paging_mode_cr4(&hw_cr4, vcpu);
1637
1638 vmcs_writel(CR4_READ_SHADOW, cr4);
1639 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001640}
1641
Avi Kivity6aa8b732006-12-10 02:21:36 -08001642static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1643{
1644 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1645
1646 return vmcs_readl(sf->base);
1647}
1648
1649static void vmx_get_segment(struct kvm_vcpu *vcpu,
1650 struct kvm_segment *var, int seg)
1651{
1652 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1653 u32 ar;
1654
1655 var->base = vmcs_readl(sf->base);
1656 var->limit = vmcs_read32(sf->limit);
1657 var->selector = vmcs_read16(sf->selector);
1658 ar = vmcs_read32(sf->ar_bytes);
Avi Kivity9fd4a3b2009-01-04 23:43:42 +02001659 if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001660 ar = 0;
1661 var->type = ar & 15;
1662 var->s = (ar >> 4) & 1;
1663 var->dpl = (ar >> 5) & 3;
1664 var->present = (ar >> 7) & 1;
1665 var->avl = (ar >> 12) & 1;
1666 var->l = (ar >> 13) & 1;
1667 var->db = (ar >> 14) & 1;
1668 var->g = (ar >> 15) & 1;
1669 var->unusable = (ar >> 16) & 1;
1670}
1671
Izik Eidus2e4d2652008-03-24 19:38:34 +02001672static int vmx_get_cpl(struct kvm_vcpu *vcpu)
1673{
1674 struct kvm_segment kvm_seg;
1675
1676 if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */
1677 return 0;
1678
1679 if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
1680 return 3;
1681
1682 vmx_get_segment(vcpu, &kvm_seg, VCPU_SREG_CS);
1683 return kvm_seg.selector & 3;
1684}
1685
Avi Kivity653e3102007-05-07 10:55:37 +03001686static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001687{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001688 u32 ar;
1689
Avi Kivity653e3102007-05-07 10:55:37 +03001690 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001691 ar = 1 << 16;
1692 else {
1693 ar = var->type & 15;
1694 ar |= (var->s & 1) << 4;
1695 ar |= (var->dpl & 3) << 5;
1696 ar |= (var->present & 1) << 7;
1697 ar |= (var->avl & 1) << 12;
1698 ar |= (var->l & 1) << 13;
1699 ar |= (var->db & 1) << 14;
1700 ar |= (var->g & 1) << 15;
1701 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001702 if (ar == 0) /* a 0 value means unusable */
1703 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001704
1705 return ar;
1706}
1707
1708static void vmx_set_segment(struct kvm_vcpu *vcpu,
1709 struct kvm_segment *var, int seg)
1710{
1711 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1712 u32 ar;
1713
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001714 if (vcpu->arch.rmode.active && seg == VCPU_SREG_TR) {
1715 vcpu->arch.rmode.tr.selector = var->selector;
1716 vcpu->arch.rmode.tr.base = var->base;
1717 vcpu->arch.rmode.tr.limit = var->limit;
1718 vcpu->arch.rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03001719 return;
1720 }
1721 vmcs_writel(sf->base, var->base);
1722 vmcs_write32(sf->limit, var->limit);
1723 vmcs_write16(sf->selector, var->selector);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001724 if (vcpu->arch.rmode.active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03001725 /*
1726 * Hack real-mode segments into vm86 compatibility.
1727 */
1728 if (var->base == 0xffff0000 && var->selector == 0xf000)
1729 vmcs_writel(sf->base, 0xf0000);
1730 ar = 0xf3;
1731 } else
1732 ar = vmx_segment_access_rights(var);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001733 vmcs_write32(sf->ar_bytes, ar);
1734}
1735
Avi Kivity6aa8b732006-12-10 02:21:36 -08001736static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1737{
1738 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1739
1740 *db = (ar >> 14) & 1;
1741 *l = (ar >> 13) & 1;
1742}
1743
1744static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1745{
1746 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1747 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1748}
1749
1750static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1751{
1752 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1753 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1754}
1755
1756static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1757{
1758 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1759 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1760}
1761
1762static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1763{
1764 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1765 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1766}
1767
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001768static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
1769{
1770 struct kvm_segment var;
1771 u32 ar;
1772
1773 vmx_get_segment(vcpu, &var, seg);
1774 ar = vmx_segment_access_rights(&var);
1775
1776 if (var.base != (var.selector << 4))
1777 return false;
1778 if (var.limit != 0xffff)
1779 return false;
1780 if (ar != 0xf3)
1781 return false;
1782
1783 return true;
1784}
1785
1786static bool code_segment_valid(struct kvm_vcpu *vcpu)
1787{
1788 struct kvm_segment cs;
1789 unsigned int cs_rpl;
1790
1791 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1792 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
1793
Avi Kivity1872a3f2009-01-04 23:26:52 +02001794 if (cs.unusable)
1795 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001796 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
1797 return false;
1798 if (!cs.s)
1799 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001800 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001801 if (cs.dpl > cs_rpl)
1802 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001803 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001804 if (cs.dpl != cs_rpl)
1805 return false;
1806 }
1807 if (!cs.present)
1808 return false;
1809
1810 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
1811 return true;
1812}
1813
1814static bool stack_segment_valid(struct kvm_vcpu *vcpu)
1815{
1816 struct kvm_segment ss;
1817 unsigned int ss_rpl;
1818
1819 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
1820 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
1821
Avi Kivity1872a3f2009-01-04 23:26:52 +02001822 if (ss.unusable)
1823 return true;
1824 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001825 return false;
1826 if (!ss.s)
1827 return false;
1828 if (ss.dpl != ss_rpl) /* DPL != RPL */
1829 return false;
1830 if (!ss.present)
1831 return false;
1832
1833 return true;
1834}
1835
1836static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
1837{
1838 struct kvm_segment var;
1839 unsigned int rpl;
1840
1841 vmx_get_segment(vcpu, &var, seg);
1842 rpl = var.selector & SELECTOR_RPL_MASK;
1843
Avi Kivity1872a3f2009-01-04 23:26:52 +02001844 if (var.unusable)
1845 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001846 if (!var.s)
1847 return false;
1848 if (!var.present)
1849 return false;
1850 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
1851 if (var.dpl < rpl) /* DPL < RPL */
1852 return false;
1853 }
1854
1855 /* TODO: Add other members to kvm_segment_field to allow checking for other access
1856 * rights flags
1857 */
1858 return true;
1859}
1860
1861static bool tr_valid(struct kvm_vcpu *vcpu)
1862{
1863 struct kvm_segment tr;
1864
1865 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
1866
Avi Kivity1872a3f2009-01-04 23:26:52 +02001867 if (tr.unusable)
1868 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001869 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
1870 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001871 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001872 return false;
1873 if (!tr.present)
1874 return false;
1875
1876 return true;
1877}
1878
1879static bool ldtr_valid(struct kvm_vcpu *vcpu)
1880{
1881 struct kvm_segment ldtr;
1882
1883 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
1884
Avi Kivity1872a3f2009-01-04 23:26:52 +02001885 if (ldtr.unusable)
1886 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001887 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
1888 return false;
1889 if (ldtr.type != 2)
1890 return false;
1891 if (!ldtr.present)
1892 return false;
1893
1894 return true;
1895}
1896
1897static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
1898{
1899 struct kvm_segment cs, ss;
1900
1901 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1902 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
1903
1904 return ((cs.selector & SELECTOR_RPL_MASK) ==
1905 (ss.selector & SELECTOR_RPL_MASK));
1906}
1907
1908/*
1909 * Check if guest state is valid. Returns true if valid, false if
1910 * not.
1911 * We assume that registers are always usable
1912 */
1913static bool guest_state_valid(struct kvm_vcpu *vcpu)
1914{
1915 /* real mode guest state checks */
1916 if (!(vcpu->arch.cr0 & X86_CR0_PE)) {
1917 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
1918 return false;
1919 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
1920 return false;
1921 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
1922 return false;
1923 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
1924 return false;
1925 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
1926 return false;
1927 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
1928 return false;
1929 } else {
1930 /* protected mode guest state checks */
1931 if (!cs_ss_rpl_check(vcpu))
1932 return false;
1933 if (!code_segment_valid(vcpu))
1934 return false;
1935 if (!stack_segment_valid(vcpu))
1936 return false;
1937 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
1938 return false;
1939 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
1940 return false;
1941 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
1942 return false;
1943 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
1944 return false;
1945 if (!tr_valid(vcpu))
1946 return false;
1947 if (!ldtr_valid(vcpu))
1948 return false;
1949 }
1950 /* TODO:
1951 * - Add checks on RIP
1952 * - Add checks on RFLAGS
1953 */
1954
1955 return true;
1956}
1957
Mike Dayd77c26f2007-10-08 09:02:08 -04001958static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001959{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001960 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001961 u16 data = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001962 int ret = 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02001963 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001964
Izik Eidus195aefd2007-10-01 22:14:18 +02001965 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
1966 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001967 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001968 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08001969 r = kvm_write_guest_page(kvm, fn++, &data,
1970 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02001971 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001972 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001973 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
1974 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001975 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001976 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
1977 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001978 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001979 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001980 r = kvm_write_guest_page(kvm, fn, &data,
1981 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
1982 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02001983 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001984 goto out;
1985
1986 ret = 1;
1987out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001988 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001989}
1990
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001991static int init_rmode_identity_map(struct kvm *kvm)
1992{
1993 int i, r, ret;
1994 pfn_t identity_map_pfn;
1995 u32 tmp;
1996
Avi Kivity089d0342009-03-23 18:26:32 +02001997 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001998 return 1;
1999 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
2000 printk(KERN_ERR "EPT: identity-mapping pagetable "
2001 "haven't been allocated!\n");
2002 return 0;
2003 }
2004 if (likely(kvm->arch.ept_identity_pagetable_done))
2005 return 1;
2006 ret = 0;
2007 identity_map_pfn = VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT;
2008 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
2009 if (r < 0)
2010 goto out;
2011 /* Set up identity-mapping pagetable for EPT in real mode */
2012 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
2013 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
2014 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
2015 r = kvm_write_guest_page(kvm, identity_map_pfn,
2016 &tmp, i * sizeof(tmp), sizeof(tmp));
2017 if (r < 0)
2018 goto out;
2019 }
2020 kvm->arch.ept_identity_pagetable_done = true;
2021 ret = 1;
2022out:
2023 return ret;
2024}
2025
Avi Kivity6aa8b732006-12-10 02:21:36 -08002026static void seg_setup(int seg)
2027{
2028 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
2029
2030 vmcs_write16(sf->selector, 0);
2031 vmcs_writel(sf->base, 0);
2032 vmcs_write32(sf->limit, 0xffff);
Avi Kivitya16b20d2008-08-20 15:48:27 +03002033 vmcs_write32(sf->ar_bytes, 0xf3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002034}
2035
Sheng Yangf78e0e22007-10-29 09:40:42 +08002036static int alloc_apic_access_page(struct kvm *kvm)
2037{
2038 struct kvm_userspace_memory_region kvm_userspace_mem;
2039 int r = 0;
2040
Izik Eidus72dc67a2008-02-10 18:04:15 +02002041 down_write(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002042 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08002043 goto out;
2044 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
2045 kvm_userspace_mem.flags = 0;
2046 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
2047 kvm_userspace_mem.memory_size = PAGE_SIZE;
2048 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2049 if (r)
2050 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002051
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002052 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002053out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02002054 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002055 return r;
2056}
2057
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002058static int alloc_identity_pagetable(struct kvm *kvm)
2059{
2060 struct kvm_userspace_memory_region kvm_userspace_mem;
2061 int r = 0;
2062
2063 down_write(&kvm->slots_lock);
2064 if (kvm->arch.ept_identity_pagetable)
2065 goto out;
2066 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
2067 kvm_userspace_mem.flags = 0;
2068 kvm_userspace_mem.guest_phys_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
2069 kvm_userspace_mem.memory_size = PAGE_SIZE;
2070 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2071 if (r)
2072 goto out;
2073
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002074 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
2075 VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002076out:
2077 up_write(&kvm->slots_lock);
2078 return r;
2079}
2080
Sheng Yang2384d2b2008-01-17 15:14:33 +08002081static void allocate_vpid(struct vcpu_vmx *vmx)
2082{
2083 int vpid;
2084
2085 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02002086 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002087 return;
2088 spin_lock(&vmx_vpid_lock);
2089 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
2090 if (vpid < VMX_NR_VPIDS) {
2091 vmx->vpid = vpid;
2092 __set_bit(vpid, vmx_vpid_bitmap);
2093 }
2094 spin_unlock(&vmx_vpid_lock);
2095}
2096
Avi Kivity58972972009-02-24 22:26:47 +02002097static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08002098{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002099 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08002100
2101 if (!cpu_has_vmx_msr_bitmap())
2102 return;
2103
2104 /*
2105 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2106 * have the write-low and read-high bitmap offsets the wrong way round.
2107 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2108 */
Sheng Yang25c5f222008-03-28 13:18:56 +08002109 if (msr <= 0x1fff) {
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002110 __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
2111 __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
Sheng Yang25c5f222008-03-28 13:18:56 +08002112 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2113 msr &= 0x1fff;
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002114 __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
2115 __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
Sheng Yang25c5f222008-03-28 13:18:56 +08002116 }
Sheng Yang25c5f222008-03-28 13:18:56 +08002117}
2118
Avi Kivity58972972009-02-24 22:26:47 +02002119static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
2120{
2121 if (!longmode_only)
2122 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
2123 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
2124}
2125
Avi Kivity6aa8b732006-12-10 02:21:36 -08002126/*
2127 * Sets up the vmcs for emulated real mode.
2128 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002129static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002130{
Sheng Yang468d4722008-10-09 16:01:55 +08002131 u32 host_sysenter_cs, msr_low, msr_high;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002132 u32 junk;
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01002133 u64 host_pat, tsc_this, tsc_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002134 unsigned long a;
2135 struct descriptor_table dt;
2136 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03002137 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002138 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002139
Avi Kivity6aa8b732006-12-10 02:21:36 -08002140 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002141 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
2142 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002143
Sheng Yang25c5f222008-03-28 13:18:56 +08002144 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02002145 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08002146
Avi Kivity6aa8b732006-12-10 02:21:36 -08002147 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
2148
Avi Kivity6aa8b732006-12-10 02:21:36 -08002149 /* Control */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002150 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
2151 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002152
2153 exec_control = vmcs_config.cpu_based_exec_ctrl;
2154 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
2155 exec_control &= ~CPU_BASED_TPR_SHADOW;
2156#ifdef CONFIG_X86_64
2157 exec_control |= CPU_BASED_CR8_STORE_EXITING |
2158 CPU_BASED_CR8_LOAD_EXITING;
2159#endif
2160 }
Avi Kivity089d0342009-03-23 18:26:32 +02002161 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002162 exec_control |= CPU_BASED_CR3_STORE_EXITING |
Marcelo Tosatti83dbc832008-10-07 17:01:27 -03002163 CPU_BASED_CR3_LOAD_EXITING |
2164 CPU_BASED_INVLPG_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002165 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002166
Sheng Yang83ff3b92007-11-21 14:33:25 +08002167 if (cpu_has_secondary_exec_ctrls()) {
2168 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
2169 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2170 exec_control &=
2171 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002172 if (vmx->vpid == 0)
2173 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Avi Kivity089d0342009-03-23 18:26:32 +02002174 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002175 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Sheng Yang83ff3b92007-11-21 14:33:25 +08002176 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
2177 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08002178
Avi Kivityc7addb92007-09-16 18:58:32 +02002179 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
2180 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002181 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
2182
2183 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
2184 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
2185 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2186
2187 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
2188 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
2189 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002190 vmcs_write16(HOST_FS_SELECTOR, kvm_read_fs()); /* 22.2.4 */
2191 vmcs_write16(HOST_GS_SELECTOR, kvm_read_gs()); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002192 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002193#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002194 rdmsrl(MSR_FS_BASE, a);
2195 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
2196 rdmsrl(MSR_GS_BASE, a);
2197 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
2198#else
2199 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
2200 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
2201#endif
2202
2203 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
2204
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002205 kvm_get_idt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002206 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
2207
Mike Dayd77c26f2007-10-08 09:02:08 -04002208 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03002209 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03002210 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
2211 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
2212 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002213
2214 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
2215 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
2216 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
2217 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
2218 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
2219 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
2220
Sheng Yang468d4722008-10-09 16:01:55 +08002221 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
2222 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2223 host_pat = msr_low | ((u64) msr_high << 32);
2224 vmcs_write64(HOST_IA32_PAT, host_pat);
2225 }
2226 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2227 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2228 host_pat = msr_low | ((u64) msr_high << 32);
2229 /* Write the default value follow host pat */
2230 vmcs_write64(GUEST_IA32_PAT, host_pat);
2231 /* Keep arch.pat sync with GUEST_IA32_PAT */
2232 vmx->vcpu.arch.pat = host_pat;
2233 }
2234
Avi Kivity6aa8b732006-12-10 02:21:36 -08002235 for (i = 0; i < NR_VMX_MSR; ++i) {
2236 u32 index = vmx_msr_index[i];
2237 u32 data_low, data_high;
2238 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002239 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002240
2241 if (rdmsr_safe(index, &data_low, &data_high) < 0)
2242 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08002243 if (wrmsr_safe(index, data_low, data_high) < 0)
2244 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002245 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002246 vmx->host_msrs[j].index = index;
2247 vmx->host_msrs[j].reserved = 0;
2248 vmx->host_msrs[j].data = data;
2249 vmx->guest_msrs[j] = vmx->host_msrs[j];
2250 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002251 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002252
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002253 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002254
2255 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002256 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
2257
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002258 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
2259 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
2260
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01002261 tsc_base = vmx->vcpu.kvm->arch.vm_init_tsc;
2262 rdtscll(tsc_this);
2263 if (tsc_this < vmx->vcpu.kvm->arch.vm_init_tsc)
2264 tsc_base = tsc_this;
2265
2266 guest_write_tsc(0, tsc_base);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002267
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002268 return 0;
2269}
2270
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002271static int init_rmode(struct kvm *kvm)
2272{
2273 if (!init_rmode_tss(kvm))
2274 return 0;
2275 if (!init_rmode_identity_map(kvm))
2276 return 0;
2277 return 1;
2278}
2279
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002280static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2281{
2282 struct vcpu_vmx *vmx = to_vmx(vcpu);
2283 u64 msr;
2284 int ret;
2285
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002286 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002287 down_read(&vcpu->kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002288 if (!init_rmode(vmx->vcpu.kvm)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002289 ret = -ENOMEM;
2290 goto out;
2291 }
2292
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002293 vmx->vcpu.arch.rmode.active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002294
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002295 vmx->soft_vnmi_blocked = 0;
2296
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002297 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002298 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002299 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
2300 if (vmx->vcpu.vcpu_id == 0)
2301 msr |= MSR_IA32_APICBASE_BSP;
2302 kvm_set_apic_base(&vmx->vcpu, msr);
2303
2304 fx_init(&vmx->vcpu);
2305
Avi Kivity5706be02008-08-20 15:07:31 +03002306 seg_setup(VCPU_SREG_CS);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002307 /*
2308 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2309 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2310 */
2311 if (vmx->vcpu.vcpu_id == 0) {
2312 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2313 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2314 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002315 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2316 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002317 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002318
2319 seg_setup(VCPU_SREG_DS);
2320 seg_setup(VCPU_SREG_ES);
2321 seg_setup(VCPU_SREG_FS);
2322 seg_setup(VCPU_SREG_GS);
2323 seg_setup(VCPU_SREG_SS);
2324
2325 vmcs_write16(GUEST_TR_SELECTOR, 0);
2326 vmcs_writel(GUEST_TR_BASE, 0);
2327 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2328 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2329
2330 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2331 vmcs_writel(GUEST_LDTR_BASE, 0);
2332 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2333 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2334
2335 vmcs_write32(GUEST_SYSENTER_CS, 0);
2336 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2337 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2338
2339 vmcs_writel(GUEST_RFLAGS, 0x02);
2340 if (vmx->vcpu.vcpu_id == 0)
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002341 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002342 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002343 kvm_rip_write(vcpu, 0);
2344 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002345
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002346 vmcs_writel(GUEST_DR7, 0x400);
2347
2348 vmcs_writel(GUEST_GDTR_BASE, 0);
2349 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2350
2351 vmcs_writel(GUEST_IDTR_BASE, 0);
2352 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2353
2354 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
2355 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2356 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2357
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002358 /* Special registers */
2359 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2360
2361 setup_msrs(vmx);
2362
Avi Kivity6aa8b732006-12-10 02:21:36 -08002363 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
2364
Sheng Yangf78e0e22007-10-29 09:40:42 +08002365 if (cpu_has_vmx_tpr_shadow()) {
2366 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
2367 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
2368 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002369 page_to_phys(vmx->vcpu.arch.apic->regs_page));
Sheng Yangf78e0e22007-10-29 09:40:42 +08002370 vmcs_write32(TPR_THRESHOLD, 0);
2371 }
2372
2373 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2374 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002375 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002376
Sheng Yang2384d2b2008-01-17 15:14:33 +08002377 if (vmx->vpid != 0)
2378 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2379
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002380 vmx->vcpu.arch.cr0 = 0x60000010;
2381 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002382 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002383 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002384 vmx_fpu_activate(&vmx->vcpu);
2385 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002386
Sheng Yang2384d2b2008-01-17 15:14:33 +08002387 vpid_sync_vcpu_all(vmx);
2388
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002389 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002390
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002391 /* HACK: Don't enable emulation on guest boot/reset */
2392 vmx->emulation_required = 0;
2393
Avi Kivity6aa8b732006-12-10 02:21:36 -08002394out:
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002395 up_read(&vcpu->kvm->slots_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002396 return ret;
2397}
2398
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002399static void enable_irq_window(struct kvm_vcpu *vcpu)
2400{
2401 u32 cpu_based_vm_exec_control;
2402
2403 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2404 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2405 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2406}
2407
2408static void enable_nmi_window(struct kvm_vcpu *vcpu)
2409{
2410 u32 cpu_based_vm_exec_control;
2411
2412 if (!cpu_has_virtual_nmis()) {
2413 enable_irq_window(vcpu);
2414 return;
2415 }
2416
2417 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2418 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
2419 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2420}
2421
Eddie Dong85f455f2007-07-06 12:20:49 +03002422static void vmx_inject_irq(struct kvm_vcpu *vcpu, int irq)
2423{
Avi Kivity9c8cba32007-11-22 11:42:59 +02002424 struct vcpu_vmx *vmx = to_vmx(vcpu);
2425
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002426 KVMTRACE_1D(INJ_VIRQ, vcpu, (u32)irq, handler);
2427
Avi Kivityfa89a812008-09-01 15:57:51 +03002428 ++vcpu->stat.irq_injections;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002429 if (vcpu->arch.rmode.active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002430 vmx->rmode.irq.pending = true;
2431 vmx->rmode.irq.vector = irq;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002432 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Avi Kivity9c5623e2007-11-08 18:19:20 +02002433 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2434 irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
2435 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002436 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
Eddie Dong85f455f2007-07-06 12:20:49 +03002437 return;
2438 }
2439 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2440 irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
2441}
2442
Sheng Yangf08864b2008-05-15 18:23:25 +08002443static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
2444{
Jan Kiszka66a5a342008-09-26 09:30:51 +02002445 struct vcpu_vmx *vmx = to_vmx(vcpu);
2446
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002447 if (!cpu_has_virtual_nmis()) {
2448 /*
2449 * Tracking the NMI-blocked state in software is built upon
2450 * finding the next open IRQ window. This, in turn, depends on
2451 * well-behaving guests: They have to keep IRQs disabled at
2452 * least as long as the NMI handler runs. Otherwise we may
2453 * cause NMI nesting, maybe breaking the guest. But as this is
2454 * highly unlikely, we can live with the residual risk.
2455 */
2456 vmx->soft_vnmi_blocked = 1;
2457 vmx->vnmi_blocked_time = 0;
2458 }
2459
Jan Kiszka487b3912008-09-26 09:30:56 +02002460 ++vcpu->stat.nmi_injections;
Jan Kiszka66a5a342008-09-26 09:30:51 +02002461 if (vcpu->arch.rmode.active) {
2462 vmx->rmode.irq.pending = true;
2463 vmx->rmode.irq.vector = NMI_VECTOR;
2464 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
2465 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2466 NMI_VECTOR | INTR_TYPE_SOFT_INTR |
2467 INTR_INFO_VALID_MASK);
2468 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
2469 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
2470 return;
2471 }
Sheng Yangf08864b2008-05-15 18:23:25 +08002472 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2473 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08002474}
2475
Jan Kiszka33f089c2008-09-26 09:30:49 +02002476static void vmx_update_window_states(struct kvm_vcpu *vcpu)
2477{
2478 u32 guest_intr = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2479
2480 vcpu->arch.nmi_window_open =
2481 !(guest_intr & (GUEST_INTR_STATE_STI |
2482 GUEST_INTR_STATE_MOV_SS |
2483 GUEST_INTR_STATE_NMI));
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002484 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
2485 vcpu->arch.nmi_window_open = 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02002486
2487 vcpu->arch.interrupt_window_open =
2488 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2489 !(guest_intr & (GUEST_INTR_STATE_STI |
2490 GUEST_INTR_STATE_MOV_SS)));
2491}
2492
Jan Kiszkaf460ee42008-09-26 09:30:50 +02002493static void do_interrupt_requests(struct kvm_vcpu *vcpu,
2494 struct kvm_run *kvm_run)
2495{
2496 vmx_update_window_states(vcpu);
2497
Jan Kiszka55934c02008-12-15 13:52:10 +01002498 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
2499 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
2500 GUEST_INTR_STATE_STI |
2501 GUEST_INTR_STATE_MOV_SS);
2502
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002503 if (vcpu->arch.nmi_pending && !vcpu->arch.nmi_injected) {
Jan Kiszka264ff012008-11-24 12:26:19 +01002504 if (vcpu->arch.interrupt.pending) {
2505 enable_nmi_window(vcpu);
2506 } else if (vcpu->arch.nmi_window_open) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002507 vcpu->arch.nmi_pending = false;
2508 vcpu->arch.nmi_injected = true;
2509 } else {
2510 enable_nmi_window(vcpu);
Jan Kiszka487b3912008-09-26 09:30:56 +02002511 return;
2512 }
Jan Kiszka487b3912008-09-26 09:30:56 +02002513 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002514 if (vcpu->arch.nmi_injected) {
2515 vmx_inject_nmi(vcpu);
Jan Kiszka45312202008-12-11 16:54:54 +01002516 if (vcpu->arch.nmi_pending)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002517 enable_nmi_window(vcpu);
2518 else if (vcpu->arch.irq_summary
2519 || kvm_run->request_interrupt_window)
2520 enable_irq_window(vcpu);
2521 return;
2522 }
Jan Kiszka487b3912008-09-26 09:30:56 +02002523
Jan Kiszkaf460ee42008-09-26 09:30:50 +02002524 if (vcpu->arch.interrupt_window_open) {
2525 if (vcpu->arch.irq_summary && !vcpu->arch.interrupt.pending)
Gleb Natapovfe4c7b12009-03-23 11:23:18 +02002526 kvm_queue_interrupt(vcpu, kvm_pop_irq(vcpu));
Jan Kiszkaf460ee42008-09-26 09:30:50 +02002527
2528 if (vcpu->arch.interrupt.pending)
2529 vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
2530 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002531 if (!vcpu->arch.interrupt_window_open &&
2532 (vcpu->arch.irq_summary || kvm_run->request_interrupt_window))
Jan Kiszkaf460ee42008-09-26 09:30:50 +02002533 enable_irq_window(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002534}
2535
Izik Eiduscbc94022007-10-25 00:29:55 +02002536static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
2537{
2538 int ret;
2539 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08002540 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02002541 .guest_phys_addr = addr,
2542 .memory_size = PAGE_SIZE * 3,
2543 .flags = 0,
2544 };
2545
2546 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
2547 if (ret)
2548 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002549 kvm->arch.tss_addr = addr;
Izik Eiduscbc94022007-10-25 00:29:55 +02002550 return 0;
2551}
2552
Avi Kivity6aa8b732006-12-10 02:21:36 -08002553static int handle_rmode_exception(struct kvm_vcpu *vcpu,
2554 int vec, u32 err_code)
2555{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03002556 /*
2557 * Instruction with address size override prefix opcode 0x67
2558 * Cause the #SS fault with 0 error code in VM86 mode.
2559 */
2560 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Laurent Vivier34273182007-09-18 11:27:37 +02002561 if (emulate_instruction(vcpu, NULL, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002562 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002563 /*
2564 * Forward all other exceptions that are valid in real mode.
2565 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
2566 * the required debugging infrastructure rework.
2567 */
2568 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002569 case DB_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002570 if (vcpu->guest_debug &
2571 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
2572 return 0;
2573 kvm_queue_exception(vcpu, vec);
2574 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002575 case BP_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002576 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
2577 return 0;
2578 /* fall through */
2579 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002580 case OF_VECTOR:
2581 case BR_VECTOR:
2582 case UD_VECTOR:
2583 case DF_VECTOR:
2584 case SS_VECTOR:
2585 case GP_VECTOR:
2586 case MF_VECTOR:
2587 kvm_queue_exception(vcpu, vec);
2588 return 1;
2589 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002590 return 0;
2591}
2592
2593static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2594{
Avi Kivity1155f762007-11-22 11:30:47 +02002595 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002596 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002597 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002598 u32 vect_info;
2599 enum emulation_result er;
2600
Avi Kivity1155f762007-11-22 11:30:47 +02002601 vect_info = vmx->idt_vectoring_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002602 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2603
2604 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Mike Dayd77c26f2007-10-08 09:02:08 -04002605 !is_page_fault(intr_info))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002606 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002607 "intr info 0x%x\n", __func__, vect_info, intr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002608
Eddie Dong85f455f2007-07-06 12:20:49 +03002609 if (!irqchip_in_kernel(vcpu->kvm) && is_external_interrupt(vect_info)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002610 int irq = vect_info & VECTORING_INFO_VECTOR_MASK;
Gleb Natapovfe4c7b12009-03-23 11:23:18 +02002611 kvm_push_irq(vcpu, irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002612 }
2613
Jan Kiszkae4a41882008-09-26 09:30:46 +02002614 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02002615 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002616
2617 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002618 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002619 return 1;
2620 }
2621
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002622 if (is_invalid_opcode(intr_info)) {
Sheng Yang571008d2008-01-02 14:49:22 +08002623 er = emulate_instruction(vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002624 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002625 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002626 return 1;
2627 }
2628
Avi Kivity6aa8b732006-12-10 02:21:36 -08002629 error_code = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002630 rip = kvm_rip_read(vcpu);
Ryan Harper2e113842008-02-11 10:26:38 -06002631 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002632 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
2633 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08002634 /* EPT won't cause page fault directly */
Avi Kivity089d0342009-03-23 18:26:32 +02002635 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002636 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002637 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002638 KVMTRACE_3D(PAGE_FAULT, vcpu, error_code, (u32)cr2,
2639 (u32)((u64)cr2 >> 32), handler);
Avi Kivityf7d92382008-07-03 16:14:28 +03002640 if (vcpu->arch.interrupt.pending || vcpu->arch.exception.pending)
Avi Kivity577bdc42008-07-19 08:57:05 +03002641 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Avi Kivity30677142007-10-28 18:48:59 +02002642 return kvm_mmu_page_fault(vcpu, cr2, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002643 }
2644
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002645 if (vcpu->arch.rmode.active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08002646 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002647 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002648 if (vcpu->arch.halt_request) {
2649 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002650 return kvm_emulate_halt(vcpu);
2651 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002652 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002653 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002654
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002655 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002656 switch (ex_no) {
2657 case DB_VECTOR:
2658 dr6 = vmcs_readl(EXIT_QUALIFICATION);
2659 if (!(vcpu->guest_debug &
2660 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
2661 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
2662 kvm_queue_exception(vcpu, DB_VECTOR);
2663 return 1;
2664 }
2665 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
2666 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
2667 /* fall through */
2668 case BP_VECTOR:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002669 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002670 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
2671 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002672 break;
2673 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002674 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
2675 kvm_run->ex.exception = ex_no;
2676 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002677 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002678 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002679 return 0;
2680}
2681
2682static int handle_external_interrupt(struct kvm_vcpu *vcpu,
2683 struct kvm_run *kvm_run)
2684{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002685 ++vcpu->stat.irq_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002686 KVMTRACE_1D(INTR, vcpu, vmcs_read32(VM_EXIT_INTR_INFO), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002687 return 1;
2688}
2689
Avi Kivity988ad742007-02-12 00:54:36 -08002690static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2691{
2692 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2693 return 0;
2694}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002695
Avi Kivity6aa8b732006-12-10 02:21:36 -08002696static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2697{
He, Qingbfdaab02007-09-12 14:18:28 +08002698 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01002699 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002700 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002701
Avi Kivity1165f5f2007-04-19 17:27:43 +03002702 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08002703 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02002704 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03002705
2706 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02002707 if (emulate_instruction(vcpu,
2708 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03002709 return 0;
2710 return 1;
2711 }
2712
2713 size = (exit_qualification & 7) + 1;
2714 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002715 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03002716
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002717 skip_emulated_instruction(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03002718 return kvm_emulate_pio(vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002719}
2720
Ingo Molnar102d8322007-02-19 14:37:47 +02002721static void
2722vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2723{
2724 /*
2725 * Patch in the VMCALL instruction:
2726 */
2727 hypercall[0] = 0x0f;
2728 hypercall[1] = 0x01;
2729 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02002730}
2731
Avi Kivity6aa8b732006-12-10 02:21:36 -08002732static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2733{
He, Qingbfdaab02007-09-12 14:18:28 +08002734 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002735 int cr;
2736 int reg;
2737
He, Qingbfdaab02007-09-12 14:18:28 +08002738 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002739 cr = exit_qualification & 15;
2740 reg = (exit_qualification >> 8) & 15;
2741 switch ((exit_qualification >> 4) & 3) {
2742 case 0: /* mov to cr */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002743 KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr,
2744 (u32)kvm_register_read(vcpu, reg),
2745 (u32)((u64)kvm_register_read(vcpu, reg) >> 32),
2746 handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002747 switch (cr) {
2748 case 0:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002749 kvm_set_cr0(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002750 skip_emulated_instruction(vcpu);
2751 return 1;
2752 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002753 kvm_set_cr3(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002754 skip_emulated_instruction(vcpu);
2755 return 1;
2756 case 4:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002757 kvm_set_cr4(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002758 skip_emulated_instruction(vcpu);
2759 return 1;
2760 case 8:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002761 kvm_set_cr8(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002762 skip_emulated_instruction(vcpu);
Avi Kivitye5314062007-12-06 16:32:45 +02002763 if (irqchip_in_kernel(vcpu->kvm))
2764 return 1;
Yang, Sheng253abde2007-08-16 13:01:00 +03002765 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2766 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002767 };
2768 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03002769 case 2: /* clts */
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002770 vmx_fpu_deactivate(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002771 vcpu->arch.cr0 &= ~X86_CR0_TS;
2772 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002773 vmx_fpu_activate(vcpu);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002774 KVMTRACE_0D(CLTS, vcpu, handler);
Anthony Liguori25c4c272007-04-27 09:29:21 +03002775 skip_emulated_instruction(vcpu);
2776 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002777 case 1: /*mov from cr*/
2778 switch (cr) {
2779 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002780 kvm_register_write(vcpu, reg, vcpu->arch.cr3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002781 KVMTRACE_3D(CR_READ, vcpu, (u32)cr,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002782 (u32)kvm_register_read(vcpu, reg),
2783 (u32)((u64)kvm_register_read(vcpu, reg) >> 32),
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002784 handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002785 skip_emulated_instruction(vcpu);
2786 return 1;
2787 case 8:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002788 kvm_register_write(vcpu, reg, kvm_get_cr8(vcpu));
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002789 KVMTRACE_2D(CR_READ, vcpu, (u32)cr,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002790 (u32)kvm_register_read(vcpu, reg), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002791 skip_emulated_instruction(vcpu);
2792 return 1;
2793 }
2794 break;
2795 case 3: /* lmsw */
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002796 kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002797
2798 skip_emulated_instruction(vcpu);
2799 return 1;
2800 default:
2801 break;
2802 }
2803 kvm_run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10002804 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08002805 (int)(exit_qualification >> 4) & 3, cr);
2806 return 0;
2807}
2808
2809static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2810{
He, Qingbfdaab02007-09-12 14:18:28 +08002811 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002812 unsigned long val;
2813 int dr, reg;
2814
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002815 dr = vmcs_readl(GUEST_DR7);
2816 if (dr & DR7_GD) {
2817 /*
2818 * As the vm-exit takes precedence over the debug trap, we
2819 * need to emulate the latter, either for the host or the
2820 * guest debugging itself.
2821 */
2822 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
2823 kvm_run->debug.arch.dr6 = vcpu->arch.dr6;
2824 kvm_run->debug.arch.dr7 = dr;
2825 kvm_run->debug.arch.pc =
2826 vmcs_readl(GUEST_CS_BASE) +
2827 vmcs_readl(GUEST_RIP);
2828 kvm_run->debug.arch.exception = DB_VECTOR;
2829 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2830 return 0;
2831 } else {
2832 vcpu->arch.dr7 &= ~DR7_GD;
2833 vcpu->arch.dr6 |= DR6_BD;
2834 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
2835 kvm_queue_exception(vcpu, DB_VECTOR);
2836 return 1;
2837 }
2838 }
2839
He, Qingbfdaab02007-09-12 14:18:28 +08002840 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002841 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
2842 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
2843 if (exit_qualification & TYPE_MOV_FROM_DR) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002844 switch (dr) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002845 case 0 ... 3:
2846 val = vcpu->arch.db[dr];
2847 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002848 case 6:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002849 val = vcpu->arch.dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002850 break;
2851 case 7:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002852 val = vcpu->arch.dr7;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002853 break;
2854 default:
2855 val = 0;
2856 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002857 kvm_register_write(vcpu, reg, val);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002858 KVMTRACE_2D(DR_READ, vcpu, (u32)dr, (u32)val, handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002859 } else {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002860 val = vcpu->arch.regs[reg];
2861 switch (dr) {
2862 case 0 ... 3:
2863 vcpu->arch.db[dr] = val;
2864 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
2865 vcpu->arch.eff_db[dr] = val;
2866 break;
2867 case 4 ... 5:
2868 if (vcpu->arch.cr4 & X86_CR4_DE)
2869 kvm_queue_exception(vcpu, UD_VECTOR);
2870 break;
2871 case 6:
2872 if (val & 0xffffffff00000000ULL) {
2873 kvm_queue_exception(vcpu, GP_VECTOR);
2874 break;
2875 }
2876 vcpu->arch.dr6 = (val & DR6_VOLATILE) | DR6_FIXED_1;
2877 break;
2878 case 7:
2879 if (val & 0xffffffff00000000ULL) {
2880 kvm_queue_exception(vcpu, GP_VECTOR);
2881 break;
2882 }
2883 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
2884 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
2885 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
2886 vcpu->arch.switch_db_regs =
2887 (val & DR7_BP_EN_MASK);
2888 }
2889 break;
2890 }
2891 KVMTRACE_2D(DR_WRITE, vcpu, (u32)dr, (u32)val, handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002892 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002893 skip_emulated_instruction(vcpu);
2894 return 1;
2895}
2896
2897static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2898{
Avi Kivity06465c52007-02-28 20:46:53 +02002899 kvm_emulate_cpuid(vcpu);
2900 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002901}
2902
2903static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2904{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002905 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002906 u64 data;
2907
2908 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002909 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002910 return 1;
2911 }
2912
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002913 KVMTRACE_3D(MSR_READ, vcpu, ecx, (u32)data, (u32)(data >> 32),
2914 handler);
2915
Avi Kivity6aa8b732006-12-10 02:21:36 -08002916 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002917 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
2918 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002919 skip_emulated_instruction(vcpu);
2920 return 1;
2921}
2922
2923static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2924{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002925 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
2926 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
2927 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002928
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002929 KVMTRACE_3D(MSR_WRITE, vcpu, ecx, (u32)data, (u32)(data >> 32),
2930 handler);
2931
Avi Kivity6aa8b732006-12-10 02:21:36 -08002932 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002933 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002934 return 1;
2935 }
2936
2937 skip_emulated_instruction(vcpu);
2938 return 1;
2939}
2940
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002941static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu,
2942 struct kvm_run *kvm_run)
2943{
2944 return 1;
2945}
2946
Avi Kivity6aa8b732006-12-10 02:21:36 -08002947static int handle_interrupt_window(struct kvm_vcpu *vcpu,
2948 struct kvm_run *kvm_run)
2949{
Eddie Dong85f455f2007-07-06 12:20:49 +03002950 u32 cpu_based_vm_exec_control;
2951
2952 /* clear pending irq */
2953 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2954 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
2955 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002956
2957 KVMTRACE_0D(PEND_INTR, vcpu, handler);
Jan Kiszkaa26bf122008-09-26 09:30:45 +02002958 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002959
Dor Laorc1150d82007-01-05 16:36:24 -08002960 /*
2961 * If the user space waits to inject interrupts, exit as soon as
2962 * possible
2963 */
2964 if (kvm_run->request_interrupt_window &&
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002965 !vcpu->arch.irq_summary) {
Dor Laorc1150d82007-01-05 16:36:24 -08002966 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08002967 return 0;
2968 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002969 return 1;
2970}
2971
2972static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2973{
2974 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03002975 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002976}
2977
Ingo Molnarc21415e2007-02-19 14:37:47 +02002978static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2979{
Dor Laor510043d2007-02-19 18:25:43 +02002980 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002981 kvm_emulate_hypercall(vcpu);
2982 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02002983}
2984
Marcelo Tosattia7052892008-09-23 13:18:35 -03002985static int handle_invlpg(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2986{
2987 u64 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
2988
2989 kvm_mmu_invlpg(vcpu, exit_qualification);
2990 skip_emulated_instruction(vcpu);
2991 return 1;
2992}
2993
Eddie Donge5edaa02007-11-11 12:28:35 +02002994static int handle_wbinvd(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2995{
2996 skip_emulated_instruction(vcpu);
2997 /* TODO: Add support for VT-d/pass-through device */
2998 return 1;
2999}
3000
Sheng Yangf78e0e22007-10-29 09:40:42 +08003001static int handle_apic_access(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3002{
3003 u64 exit_qualification;
3004 enum emulation_result er;
3005 unsigned long offset;
3006
3007 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
3008 offset = exit_qualification & 0xffful;
3009
3010 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
3011
3012 if (er != EMULATE_DONE) {
3013 printk(KERN_ERR
3014 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
3015 offset);
3016 return -ENOTSUPP;
3017 }
3018 return 1;
3019}
3020
Izik Eidus37817f22008-03-24 23:14:53 +02003021static int handle_task_switch(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3022{
Jan Kiszka60637aa2008-09-26 09:30:47 +02003023 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003024 unsigned long exit_qualification;
3025 u16 tss_selector;
3026 int reason;
3027
3028 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
3029
3030 reason = (u32)exit_qualification >> 30;
Jan Kiszka60637aa2008-09-26 09:30:47 +02003031 if (reason == TASK_SWITCH_GATE && vmx->vcpu.arch.nmi_injected &&
3032 (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
3033 (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK)
3034 == INTR_TYPE_NMI_INTR) {
3035 vcpu->arch.nmi_injected = false;
3036 if (cpu_has_virtual_nmis())
3037 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3038 GUEST_INTR_STATE_NMI);
3039 }
Izik Eidus37817f22008-03-24 23:14:53 +02003040 tss_selector = exit_qualification;
3041
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003042 if (!kvm_task_switch(vcpu, tss_selector, reason))
3043 return 0;
3044
3045 /* clear all local breakpoint enable flags */
3046 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
3047
3048 /*
3049 * TODO: What about debug traps on tss switch?
3050 * Are we supposed to inject them and update dr6?
3051 */
3052
3053 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02003054}
3055
Sheng Yang14394422008-04-28 12:24:45 +08003056static int handle_ept_violation(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3057{
3058 u64 exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08003059 gpa_t gpa;
Sheng Yang14394422008-04-28 12:24:45 +08003060 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08003061
3062 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
3063
3064 if (exit_qualification & (1 << 6)) {
3065 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
3066 return -ENOTSUPP;
3067 }
3068
3069 gla_validity = (exit_qualification >> 7) & 0x3;
3070 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
3071 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
3072 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3073 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
3074 (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS));
3075 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
3076 (long unsigned int)exit_qualification);
3077 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
3078 kvm_run->hw.hardware_exit_reason = 0;
3079 return -ENOTSUPP;
3080 }
3081
3082 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Sheng Yang49cd7d22009-02-11 13:50:40 +08003083 return kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
Sheng Yang14394422008-04-28 12:24:45 +08003084}
3085
Sheng Yangf08864b2008-05-15 18:23:25 +08003086static int handle_nmi_window(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3087{
3088 u32 cpu_based_vm_exec_control;
3089
3090 /* clear pending NMI */
3091 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3092 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
3093 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3094 ++vcpu->stat.nmi_window_exits;
3095
3096 return 1;
3097}
3098
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003099static void handle_invalid_guest_state(struct kvm_vcpu *vcpu,
3100 struct kvm_run *kvm_run)
3101{
Avi Kivity8b3079a2009-01-05 12:10:54 +02003102 struct vcpu_vmx *vmx = to_vmx(vcpu);
3103 enum emulation_result err = EMULATE_DONE;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003104
3105 preempt_enable();
3106 local_irq_enable();
3107
3108 while (!guest_state_valid(vcpu)) {
3109 err = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
3110
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003111 if (err == EMULATE_DO_MMIO)
3112 break;
3113
3114 if (err != EMULATE_DONE) {
3115 kvm_report_emulation_failure(vcpu, "emulation failure");
3116 return;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003117 }
3118
3119 if (signal_pending(current))
3120 break;
3121 if (need_resched())
3122 schedule();
3123 }
3124
3125 local_irq_disable();
3126 preempt_disable();
Avi Kivity8b3079a2009-01-05 12:10:54 +02003127
3128 vmx->invalid_state_emulation_result = err;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003129}
3130
Avi Kivity6aa8b732006-12-10 02:21:36 -08003131/*
3132 * The exit handlers return 1 if the exit was handled fully and guest execution
3133 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
3134 * to be done to userspace and return 0.
3135 */
3136static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
3137 struct kvm_run *kvm_run) = {
3138 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
3139 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08003140 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08003141 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003142 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003143 [EXIT_REASON_CR_ACCESS] = handle_cr,
3144 [EXIT_REASON_DR_ACCESS] = handle_dr,
3145 [EXIT_REASON_CPUID] = handle_cpuid,
3146 [EXIT_REASON_MSR_READ] = handle_rdmsr,
3147 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
3148 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
3149 [EXIT_REASON_HLT] = handle_halt,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003150 [EXIT_REASON_INVLPG] = handle_invlpg,
Ingo Molnarc21415e2007-02-19 14:37:47 +02003151 [EXIT_REASON_VMCALL] = handle_vmcall,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003152 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
3153 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02003154 [EXIT_REASON_WBINVD] = handle_wbinvd,
Izik Eidus37817f22008-03-24 23:14:53 +02003155 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Sheng Yang14394422008-04-28 12:24:45 +08003156 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003157};
3158
3159static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04003160 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003161
3162/*
3163 * The guest has exited. See if we can fix it or if we need userspace
3164 * assistance.
3165 */
Avi Kivity6062d012009-03-23 17:35:17 +02003166static int vmx_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003167{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003168 u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity29bd8a72007-09-10 17:27:03 +03003169 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1155f762007-11-22 11:30:47 +02003170 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03003171
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003172 KVMTRACE_3D(VMEXIT, vcpu, exit_reason, (u32)kvm_rip_read(vcpu),
3173 (u32)((u64)kvm_rip_read(vcpu) >> 32), entryexit);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003174
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003175 /* If we need to emulate an MMIO from handle_invalid_guest_state
3176 * we just return 0 */
Avi Kivity10f32d82009-01-05 00:53:19 +02003177 if (vmx->emulation_required && emulate_invalid_guest_state) {
3178 if (guest_state_valid(vcpu))
3179 vmx->emulation_required = 0;
Avi Kivity8b3079a2009-01-05 12:10:54 +02003180 return vmx->invalid_state_emulation_result != EMULATE_DO_MMIO;
Avi Kivity10f32d82009-01-05 00:53:19 +02003181 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003182
Sheng Yang14394422008-04-28 12:24:45 +08003183 /* Access CR3 don't cause VMExit in paging mode, so we need
3184 * to sync with guest real CR3. */
Avi Kivity089d0342009-03-23 18:26:32 +02003185 if (enable_ept && is_paging(vcpu)) {
Sheng Yang14394422008-04-28 12:24:45 +08003186 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3187 ept_load_pdptrs(vcpu);
3188 }
3189
Avi Kivity29bd8a72007-09-10 17:27:03 +03003190 if (unlikely(vmx->fail)) {
3191 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3192 kvm_run->fail_entry.hardware_entry_failure_reason
3193 = vmcs_read32(VM_INSTRUCTION_ERROR);
3194 return 0;
3195 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003196
Mike Dayd77c26f2007-10-08 09:02:08 -04003197 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08003198 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02003199 exit_reason != EXIT_REASON_EPT_VIOLATION &&
3200 exit_reason != EXIT_REASON_TASK_SWITCH))
3201 printk(KERN_WARNING "%s: unexpected, valid vectoring info "
3202 "(0x%x) and exit reason is 0x%x\n",
3203 __func__, vectoring_info, exit_reason);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003204
3205 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
3206 if (vcpu->arch.interrupt_window_open) {
3207 vmx->soft_vnmi_blocked = 0;
3208 vcpu->arch.nmi_window_open = 1;
3209 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01003210 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003211 /*
3212 * This CPU don't support us in finding the end of an
3213 * NMI-blocked window if the guest runs with IRQs
3214 * disabled. So we pull the trigger after 1 s of
3215 * futile waiting, but inform the user about this.
3216 */
3217 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
3218 "state on VCPU %d after 1 s timeout\n",
3219 __func__, vcpu->vcpu_id);
3220 vmx->soft_vnmi_blocked = 0;
3221 vmx->vcpu.arch.nmi_window_open = 1;
3222 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003223 }
3224
Avi Kivity6aa8b732006-12-10 02:21:36 -08003225 if (exit_reason < kvm_vmx_max_exit_handlers
3226 && kvm_vmx_exit_handlers[exit_reason])
3227 return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
3228 else {
3229 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
3230 kvm_run->hw.hardware_exit_reason = exit_reason;
3231 }
3232 return 0;
3233}
3234
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003235static void update_tpr_threshold(struct kvm_vcpu *vcpu)
3236{
3237 int max_irr, tpr;
3238
3239 if (!vm_need_tpr_shadow(vcpu->kvm))
3240 return;
3241
3242 if (!kvm_lapic_enabled(vcpu) ||
3243 ((max_irr = kvm_lapic_find_highest_irr(vcpu)) == -1)) {
3244 vmcs_write32(TPR_THRESHOLD, 0);
3245 return;
3246 }
3247
3248 tpr = (kvm_lapic_get_cr8(vcpu) & 0x0f) << 4;
3249 vmcs_write32(TPR_THRESHOLD, (max_irr > tpr) ? tpr >> 4 : max_irr >> 4);
3250}
3251
Avi Kivitycf393f72008-07-01 16:20:21 +03003252static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
3253{
3254 u32 exit_intr_info;
Avi Kivity668f6122008-07-02 09:28:55 +03003255 u32 idt_vectoring_info;
Avi Kivitycf393f72008-07-01 16:20:21 +03003256 bool unblock_nmi;
3257 u8 vector;
Avi Kivity668f6122008-07-02 09:28:55 +03003258 int type;
3259 bool idtv_info_valid;
Avi Kivity35920a32008-07-03 14:50:12 +03003260 u32 error;
Avi Kivitycf393f72008-07-01 16:20:21 +03003261
3262 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
3263 if (cpu_has_virtual_nmis()) {
3264 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
3265 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
3266 /*
3267 * SDM 3: 25.7.1.2
3268 * Re-set bit "block by NMI" before VM entry if vmexit caused by
3269 * a guest IRET fault.
3270 */
3271 if (unblock_nmi && vector != DF_VECTOR)
3272 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3273 GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003274 } else if (unlikely(vmx->soft_vnmi_blocked))
3275 vmx->vnmi_blocked_time +=
3276 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity668f6122008-07-02 09:28:55 +03003277
3278 idt_vectoring_info = vmx->idt_vectoring_info;
3279 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
3280 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
3281 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
3282 if (vmx->vcpu.arch.nmi_injected) {
3283 /*
3284 * SDM 3: 25.7.1.2
3285 * Clear bit "block by NMI" before VM entry if a NMI delivery
3286 * faulted.
3287 */
3288 if (idtv_info_valid && type == INTR_TYPE_NMI_INTR)
3289 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3290 GUEST_INTR_STATE_NMI);
3291 else
3292 vmx->vcpu.arch.nmi_injected = false;
3293 }
Avi Kivity35920a32008-07-03 14:50:12 +03003294 kvm_clear_exception_queue(&vmx->vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003295 if (idtv_info_valid && (type == INTR_TYPE_HARD_EXCEPTION ||
3296 type == INTR_TYPE_SOFT_EXCEPTION)) {
Avi Kivity35920a32008-07-03 14:50:12 +03003297 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
3298 error = vmcs_read32(IDT_VECTORING_ERROR_CODE);
3299 kvm_queue_exception_e(&vmx->vcpu, vector, error);
3300 } else
3301 kvm_queue_exception(&vmx->vcpu, vector);
3302 vmx->idt_vectoring_info = 0;
3303 }
Avi Kivityf7d92382008-07-03 16:14:28 +03003304 kvm_clear_interrupt_queue(&vmx->vcpu);
3305 if (idtv_info_valid && type == INTR_TYPE_EXT_INTR) {
3306 kvm_queue_interrupt(&vmx->vcpu, vector);
3307 vmx->idt_vectoring_info = 0;
3308 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003309}
3310
Eddie Dong85f455f2007-07-06 12:20:49 +03003311static void vmx_intr_assist(struct kvm_vcpu *vcpu)
3312{
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003313 update_tpr_threshold(vcpu);
3314
Jan Kiszka33f089c2008-09-26 09:30:49 +02003315 vmx_update_window_states(vcpu);
3316
Jan Kiszka55934c02008-12-15 13:52:10 +01003317 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
3318 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3319 GUEST_INTR_STATE_STI |
3320 GUEST_INTR_STATE_MOV_SS);
3321
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003322 if (vcpu->arch.nmi_pending && !vcpu->arch.nmi_injected) {
3323 if (vcpu->arch.interrupt.pending) {
3324 enable_nmi_window(vcpu);
3325 } else if (vcpu->arch.nmi_window_open) {
3326 vcpu->arch.nmi_pending = false;
3327 vcpu->arch.nmi_injected = true;
3328 } else {
3329 enable_nmi_window(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08003330 return;
3331 }
Sheng Yangf08864b2008-05-15 18:23:25 +08003332 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003333 if (vcpu->arch.nmi_injected) {
3334 vmx_inject_nmi(vcpu);
3335 if (vcpu->arch.nmi_pending)
3336 enable_nmi_window(vcpu);
3337 else if (kvm_cpu_has_interrupt(vcpu))
3338 enable_irq_window(vcpu);
3339 return;
3340 }
Avi Kivityf7d92382008-07-03 16:14:28 +03003341 if (!vcpu->arch.interrupt.pending && kvm_cpu_has_interrupt(vcpu)) {
Jan Kiszka33f089c2008-09-26 09:30:49 +02003342 if (vcpu->arch.interrupt_window_open)
Avi Kivityf7d92382008-07-03 16:14:28 +03003343 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu));
3344 else
3345 enable_irq_window(vcpu);
3346 }
3347 if (vcpu->arch.interrupt.pending) {
3348 vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
Avi Kivitydf203ec2008-11-23 18:08:57 +02003349 if (kvm_cpu_has_interrupt(vcpu))
3350 enable_irq_window(vcpu);
Avi Kivityf7d92382008-07-03 16:14:28 +03003351 }
Eddie Dong85f455f2007-07-06 12:20:49 +03003352}
3353
Avi Kivity9c8cba32007-11-22 11:42:59 +02003354/*
3355 * Failure to inject an interrupt should give us the information
3356 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
3357 * when fetching the interrupt redirection bitmap in the real-mode
3358 * tss, this doesn't happen. So we do it ourselves.
3359 */
3360static void fixup_rmode_irq(struct vcpu_vmx *vmx)
3361{
3362 vmx->rmode.irq.pending = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003363 if (kvm_rip_read(&vmx->vcpu) + 1 != vmx->rmode.irq.rip)
Avi Kivity9c8cba32007-11-22 11:42:59 +02003364 return;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003365 kvm_rip_write(&vmx->vcpu, vmx->rmode.irq.rip);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003366 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
3367 vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
3368 vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
3369 return;
3370 }
3371 vmx->idt_vectoring_info =
3372 VECTORING_INFO_VALID_MASK
3373 | INTR_TYPE_EXT_INTR
3374 | vmx->rmode.irq.vector;
3375}
3376
Avi Kivityc8019492008-07-14 14:44:59 +03003377#ifdef CONFIG_X86_64
3378#define R "r"
3379#define Q "q"
3380#else
3381#define R "e"
3382#define Q "l"
3383#endif
3384
Avi Kivity04d2cc72007-09-10 18:10:54 +03003385static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003386{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003387 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1b6269d2007-10-09 12:12:19 +02003388 u32 intr_info;
Avi Kivitye6adf282007-04-30 16:07:54 +03003389
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003390 /* Record the guest's net vcpu time for enforced NMI injections. */
3391 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
3392 vmx->entry_time = ktime_get();
3393
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003394 /* Handle invalid guest state instead of entering VMX */
3395 if (vmx->emulation_required && emulate_invalid_guest_state) {
3396 handle_invalid_guest_state(vcpu, kvm_run);
3397 return;
3398 }
3399
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003400 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
3401 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
3402 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
3403 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
3404
Avi Kivitye6adf282007-04-30 16:07:54 +03003405 /*
3406 * Loading guest fpu may have cleared host cr0.ts
3407 */
3408 vmcs_writel(HOST_CR0, read_cr0());
3409
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003410 set_debugreg(vcpu->arch.dr6, 6);
3411
Mike Dayd77c26f2007-10-08 09:02:08 -04003412 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08003413 /* Store host registers */
Avi Kivityc8019492008-07-14 14:44:59 +03003414 "push %%"R"dx; push %%"R"bp;"
3415 "push %%"R"cx \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003416 "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
3417 "je 1f \n\t"
3418 "mov %%"R"sp, %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003419 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003420 "1: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003421 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02003422 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003423 /* Load guest registers. Don't clobber flags. */
Avi Kivityc8019492008-07-14 14:44:59 +03003424 "mov %c[cr2](%0), %%"R"ax \n\t"
3425 "mov %%"R"ax, %%cr2 \n\t"
3426 "mov %c[rax](%0), %%"R"ax \n\t"
3427 "mov %c[rbx](%0), %%"R"bx \n\t"
3428 "mov %c[rdx](%0), %%"R"dx \n\t"
3429 "mov %c[rsi](%0), %%"R"si \n\t"
3430 "mov %c[rdi](%0), %%"R"di \n\t"
3431 "mov %c[rbp](%0), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003432#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003433 "mov %c[r8](%0), %%r8 \n\t"
3434 "mov %c[r9](%0), %%r9 \n\t"
3435 "mov %c[r10](%0), %%r10 \n\t"
3436 "mov %c[r11](%0), %%r11 \n\t"
3437 "mov %c[r12](%0), %%r12 \n\t"
3438 "mov %c[r13](%0), %%r13 \n\t"
3439 "mov %c[r14](%0), %%r14 \n\t"
3440 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003441#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003442 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
3443
Avi Kivity6aa8b732006-12-10 02:21:36 -08003444 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03003445 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003446 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003447 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003448 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003449 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003450 /* Save guest registers, load host registers, keep flags */
Avi Kivityc8019492008-07-14 14:44:59 +03003451 "xchg %0, (%%"R"sp) \n\t"
3452 "mov %%"R"ax, %c[rax](%0) \n\t"
3453 "mov %%"R"bx, %c[rbx](%0) \n\t"
3454 "push"Q" (%%"R"sp); pop"Q" %c[rcx](%0) \n\t"
3455 "mov %%"R"dx, %c[rdx](%0) \n\t"
3456 "mov %%"R"si, %c[rsi](%0) \n\t"
3457 "mov %%"R"di, %c[rdi](%0) \n\t"
3458 "mov %%"R"bp, %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003459#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003460 "mov %%r8, %c[r8](%0) \n\t"
3461 "mov %%r9, %c[r9](%0) \n\t"
3462 "mov %%r10, %c[r10](%0) \n\t"
3463 "mov %%r11, %c[r11](%0) \n\t"
3464 "mov %%r12, %c[r12](%0) \n\t"
3465 "mov %%r13, %c[r13](%0) \n\t"
3466 "mov %%r14, %c[r14](%0) \n\t"
3467 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003468#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003469 "mov %%cr2, %%"R"ax \n\t"
3470 "mov %%"R"ax, %c[cr2](%0) \n\t"
3471
3472 "pop %%"R"bp; pop %%"R"bp; pop %%"R"dx \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02003473 "setbe %c[fail](%0) \n\t"
3474 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
3475 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
3476 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03003477 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003478 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
3479 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
3480 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
3481 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
3482 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
3483 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
3484 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003485#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003486 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
3487 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
3488 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
3489 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
3490 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
3491 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
3492 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
3493 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003494#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003495 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
Laurent Vivierc2036302007-10-25 14:18:52 +02003496 : "cc", "memory"
Avi Kivityc8019492008-07-14 14:44:59 +03003497 , R"bx", R"di", R"si"
Laurent Vivierc2036302007-10-25 14:18:52 +02003498#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02003499 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3500#endif
3501 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003502
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003503 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
3504 vcpu->arch.regs_dirty = 0;
3505
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003506 get_debugreg(vcpu->arch.dr6, 6);
3507
Avi Kivity1155f762007-11-22 11:30:47 +02003508 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003509 if (vmx->rmode.irq.pending)
3510 fixup_rmode_irq(vmx);
Avi Kivity1155f762007-11-22 11:30:47 +02003511
Jan Kiszka33f089c2008-09-26 09:30:49 +02003512 vmx_update_window_states(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003513
Mike Dayd77c26f2007-10-08 09:02:08 -04003514 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03003515 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02003516
3517 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
3518
3519 /* We need to handle NMIs before interrupts are enabled */
Jan Kiszkae4a41882008-09-26 09:30:46 +02003520 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Sheng Yangf08864b2008-05-15 18:23:25 +08003521 (intr_info & INTR_INFO_VALID_MASK)) {
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003522 KVMTRACE_0D(NMI, vcpu, handler);
Avi Kivity1b6269d2007-10-09 12:12:19 +02003523 asm("int $2");
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003524 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003525
3526 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003527}
3528
Avi Kivityc8019492008-07-14 14:44:59 +03003529#undef R
3530#undef Q
3531
Avi Kivity6aa8b732006-12-10 02:21:36 -08003532static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
3533{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003534 struct vcpu_vmx *vmx = to_vmx(vcpu);
3535
3536 if (vmx->vmcs) {
Avi Kivity543e4242008-05-13 16:22:47 +03003537 vcpu_clear(vmx);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003538 free_vmcs(vmx->vmcs);
3539 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003540 }
3541}
3542
3543static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
3544{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003545 struct vcpu_vmx *vmx = to_vmx(vcpu);
3546
Sheng Yang2384d2b2008-01-17 15:14:33 +08003547 spin_lock(&vmx_vpid_lock);
3548 if (vmx->vpid != 0)
3549 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3550 spin_unlock(&vmx_vpid_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003551 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003552 kfree(vmx->host_msrs);
3553 kfree(vmx->guest_msrs);
3554 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10003555 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003556}
3557
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003558static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003559{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003560 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10003561 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03003562 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003563
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003564 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003565 return ERR_PTR(-ENOMEM);
3566
Sheng Yang2384d2b2008-01-17 15:14:33 +08003567 allocate_vpid(vmx);
3568
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003569 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
3570 if (err)
3571 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003572
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003573 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003574 if (!vmx->guest_msrs) {
3575 err = -ENOMEM;
3576 goto uninit_vcpu;
3577 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08003578
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003579 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
3580 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003581 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003582
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003583 vmx->vmcs = alloc_vmcs();
3584 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003585 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003586
3587 vmcs_clear(vmx->vmcs);
3588
Avi Kivity15ad7142007-07-11 18:17:21 +03003589 cpu = get_cpu();
3590 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003591 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003592 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003593 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003594 if (err)
3595 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02003596 if (vm_need_virtualize_apic_accesses(kvm))
3597 if (alloc_apic_access_page(kvm) != 0)
3598 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003599
Avi Kivity089d0342009-03-23 18:26:32 +02003600 if (enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003601 if (alloc_identity_pagetable(kvm) != 0)
3602 goto free_vmcs;
3603
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003604 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003605
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003606free_vmcs:
3607 free_vmcs(vmx->vmcs);
3608free_msrs:
3609 kfree(vmx->host_msrs);
3610free_guest_msrs:
3611 kfree(vmx->guest_msrs);
3612uninit_vcpu:
3613 kvm_vcpu_uninit(&vmx->vcpu);
3614free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10003615 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003616 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003617}
3618
Yang, Sheng002c7f72007-07-31 14:23:01 +03003619static void __init vmx_check_processor_compat(void *rtn)
3620{
3621 struct vmcs_config vmcs_conf;
3622
3623 *(int *)rtn = 0;
3624 if (setup_vmcs_config(&vmcs_conf) < 0)
3625 *(int *)rtn = -EIO;
3626 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
3627 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
3628 smp_processor_id());
3629 *(int *)rtn = -EIO;
3630 }
3631}
3632
Sheng Yang67253af2008-04-25 10:20:22 +08003633static int get_ept_level(void)
3634{
3635 return VMX_EPT_DEFAULT_GAW + 1;
3636}
3637
Sheng Yang64d4d522008-10-09 16:01:57 +08003638static int vmx_get_mt_mask_shift(void)
3639{
3640 return VMX_EPT_MT_EPTE_SHIFT;
3641}
3642
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003643static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003644 .cpu_has_kvm_support = cpu_has_kvm_support,
3645 .disabled_by_bios = vmx_disabled_by_bios,
3646 .hardware_setup = hardware_setup,
3647 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003648 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003649 .hardware_enable = hardware_enable,
3650 .hardware_disable = hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003651 .cpu_has_accelerated_tpr = cpu_has_vmx_virtualize_apic_accesses,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003652
3653 .vcpu_create = vmx_create_vcpu,
3654 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003655 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003656
Avi Kivity04d2cc72007-09-10 18:10:54 +03003657 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003658 .vcpu_load = vmx_vcpu_load,
3659 .vcpu_put = vmx_vcpu_put,
3660
3661 .set_guest_debug = set_guest_debug,
3662 .get_msr = vmx_get_msr,
3663 .set_msr = vmx_set_msr,
3664 .get_segment_base = vmx_get_segment_base,
3665 .get_segment = vmx_get_segment,
3666 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003667 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003668 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003669 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003670 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003671 .set_cr3 = vmx_set_cr3,
3672 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003673 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003674 .get_idt = vmx_get_idt,
3675 .set_idt = vmx_set_idt,
3676 .get_gdt = vmx_get_gdt,
3677 .set_gdt = vmx_set_gdt,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003678 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003679 .get_rflags = vmx_get_rflags,
3680 .set_rflags = vmx_set_rflags,
3681
3682 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003683
Avi Kivity6aa8b732006-12-10 02:21:36 -08003684 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02003685 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003686 .skip_emulated_instruction = skip_emulated_instruction,
Ingo Molnar102d8322007-02-19 14:37:47 +02003687 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003688 .get_irq = vmx_get_irq,
3689 .set_irq = vmx_inject_irq,
Avi Kivity298101d2007-11-25 13:41:11 +02003690 .queue_exception = vmx_queue_exception,
3691 .exception_injected = vmx_exception_injected,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003692 .inject_pending_irq = vmx_intr_assist,
3693 .inject_pending_vectors = do_interrupt_requests,
Izik Eiduscbc94022007-10-25 00:29:55 +02003694
3695 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003696 .get_tdp_level = get_ept_level,
Sheng Yang64d4d522008-10-09 16:01:57 +08003697 .get_mt_mask_shift = vmx_get_mt_mask_shift,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003698};
3699
3700static int __init vmx_init(void)
3701{
He, Qingfdef3ad2007-04-30 09:45:24 +03003702 int r;
3703
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003704 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03003705 if (!vmx_io_bitmap_a)
3706 return -ENOMEM;
3707
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003708 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03003709 if (!vmx_io_bitmap_b) {
3710 r = -ENOMEM;
3711 goto out;
3712 }
3713
Avi Kivity58972972009-02-24 22:26:47 +02003714 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
3715 if (!vmx_msr_bitmap_legacy) {
Sheng Yang25c5f222008-03-28 13:18:56 +08003716 r = -ENOMEM;
3717 goto out1;
3718 }
3719
Avi Kivity58972972009-02-24 22:26:47 +02003720 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
3721 if (!vmx_msr_bitmap_longmode) {
3722 r = -ENOMEM;
3723 goto out2;
3724 }
3725
He, Qingfdef3ad2007-04-30 09:45:24 +03003726 /*
3727 * Allow direct access to the PC debug port (it is often used for I/O
3728 * delays, but the vmexits simply slow things down).
3729 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003730 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
3731 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03003732
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003733 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03003734
Avi Kivity58972972009-02-24 22:26:47 +02003735 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
3736 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08003737
Sheng Yang2384d2b2008-01-17 15:14:33 +08003738 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
3739
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003740 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03003741 if (r)
Avi Kivity58972972009-02-24 22:26:47 +02003742 goto out3;
Sheng Yang25c5f222008-03-28 13:18:56 +08003743
Avi Kivity58972972009-02-24 22:26:47 +02003744 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
3745 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
3746 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
3747 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
3748 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
3749 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
He, Qingfdef3ad2007-04-30 09:45:24 +03003750
Avi Kivity089d0342009-03-23 18:26:32 +02003751 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08003752 bypass_guest_pf = 0;
Sheng Yang5fdbcb92008-07-16 09:25:40 +08003753 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
Sheng Yang2aaf69d2009-01-21 16:52:16 +08003754 VMX_EPT_WRITABLE_MASK);
Sheng Yang534e38b2008-09-08 15:12:30 +08003755 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang64d4d522008-10-09 16:01:57 +08003756 VMX_EPT_EXECUTABLE_MASK,
3757 VMX_EPT_DEFAULT_MT << VMX_EPT_MT_EPTE_SHIFT);
Sheng Yang5fdbcb92008-07-16 09:25:40 +08003758 kvm_enable_tdp();
3759 } else
3760 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08003761
Avi Kivityc7addb92007-09-16 18:58:32 +02003762 if (bypass_guest_pf)
3763 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
3764
Sheng Yang14394422008-04-28 12:24:45 +08003765 ept_sync_global();
3766
He, Qingfdef3ad2007-04-30 09:45:24 +03003767 return 0;
3768
Avi Kivity58972972009-02-24 22:26:47 +02003769out3:
3770 free_page((unsigned long)vmx_msr_bitmap_longmode);
Sheng Yang25c5f222008-03-28 13:18:56 +08003771out2:
Avi Kivity58972972009-02-24 22:26:47 +02003772 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03003773out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003774 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03003775out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003776 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03003777 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003778}
3779
3780static void __exit vmx_exit(void)
3781{
Avi Kivity58972972009-02-24 22:26:47 +02003782 free_page((unsigned long)vmx_msr_bitmap_legacy);
3783 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003784 free_page((unsigned long)vmx_io_bitmap_b);
3785 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03003786
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003787 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003788}
3789
3790module_init(vmx_init)
3791module_exit(vmx_exit)