Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1 | /* |
| 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 Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 18 | #include "irq.h" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 19 | #include "vmx.h" |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 20 | #include "mmu.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 21 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 22 | #include <linux/kvm_host.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 23 | #include <linux/module.h> |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 24 | #include <linux/kernel.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 25 | #include <linux/mm.h> |
| 26 | #include <linux/highmem.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 27 | #include <linux/sched.h> |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 28 | #include <linux/moduleparam.h> |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 29 | #include "kvm_cache_regs.h" |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 30 | #include "x86.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 31 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 32 | #include <asm/io.h> |
Anthony Liguori | 3b3be0d | 2006-12-13 00:33:43 -0800 | [diff] [blame] | 33 | #include <asm/desc.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 34 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 35 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
| 36 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 37 | MODULE_AUTHOR("Qumranet"); |
| 38 | MODULE_LICENSE("GPL"); |
| 39 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 40 | static int bypass_guest_pf = 1; |
| 41 | module_param(bypass_guest_pf, bool, 0); |
| 42 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 43 | static int enable_vpid = 1; |
| 44 | module_param(enable_vpid, bool, 0); |
| 45 | |
Avi Kivity | 4c9fc8e | 2008-03-24 18:15:14 +0200 | [diff] [blame] | 46 | static int flexpriority_enabled = 1; |
| 47 | module_param(flexpriority_enabled, bool, 0); |
| 48 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 49 | static int enable_ept = 1; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 50 | module_param(enable_ept, bool, 0); |
| 51 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 52 | struct vmcs { |
| 53 | u32 revision_id; |
| 54 | u32 abort; |
| 55 | char data[0]; |
| 56 | }; |
| 57 | |
| 58 | struct vcpu_vmx { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 59 | struct kvm_vcpu vcpu; |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 60 | struct list_head local_vcpus_link; |
Avi Kivity | 313dbd49 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 61 | unsigned long host_rsp; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 62 | int launched; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 63 | u8 fail; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 64 | u32 idt_vectoring_info; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 65 | struct kvm_msr_entry *guest_msrs; |
| 66 | struct kvm_msr_entry *host_msrs; |
| 67 | int nmsrs; |
| 68 | int save_nmsrs; |
| 69 | int msr_offset_efer; |
| 70 | #ifdef CONFIG_X86_64 |
| 71 | int msr_offset_kernel_gs_base; |
| 72 | #endif |
| 73 | struct vmcs *vmcs; |
| 74 | struct { |
| 75 | int loaded; |
| 76 | u16 fs_sel, gs_sel, ldt_sel; |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 77 | int gs_ldt_reload_needed; |
| 78 | int fs_reload_needed; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 79 | int guest_efer_loaded; |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 80 | } host_state; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 81 | struct { |
| 82 | struct { |
| 83 | bool pending; |
| 84 | u8 vector; |
| 85 | unsigned rip; |
| 86 | } irq; |
| 87 | } rmode; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 88 | int vpid; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) |
| 92 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 93 | return container_of(vcpu, struct vcpu_vmx, vcpu); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 94 | } |
| 95 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 96 | static int init_rmode(struct kvm *kvm); |
Sheng Yang | 4e1096d | 2008-07-06 19:16:51 +0800 | [diff] [blame] | 97 | static u64 construct_eptp(unsigned long root_hpa); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 98 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 99 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); |
| 100 | static DEFINE_PER_CPU(struct vmcs *, current_vmcs); |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 101 | static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 102 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 103 | static struct page *vmx_io_bitmap_a; |
| 104 | static struct page *vmx_io_bitmap_b; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 105 | static struct page *vmx_msr_bitmap; |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 106 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 107 | static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 108 | static DEFINE_SPINLOCK(vmx_vpid_lock); |
| 109 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 110 | static struct vmcs_config { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 111 | int size; |
| 112 | int order; |
| 113 | u32 revision_id; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 114 | u32 pin_based_exec_ctrl; |
| 115 | u32 cpu_based_exec_ctrl; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 116 | u32 cpu_based_2nd_exec_ctrl; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 117 | u32 vmexit_ctrl; |
| 118 | u32 vmentry_ctrl; |
| 119 | } vmcs_config; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 120 | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 121 | struct vmx_capability { |
| 122 | u32 ept; |
| 123 | u32 vpid; |
| 124 | } vmx_capability; |
| 125 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 126 | #define VMX_SEGMENT_FIELD(seg) \ |
| 127 | [VCPU_SREG_##seg] = { \ |
| 128 | .selector = GUEST_##seg##_SELECTOR, \ |
| 129 | .base = GUEST_##seg##_BASE, \ |
| 130 | .limit = GUEST_##seg##_LIMIT, \ |
| 131 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ |
| 132 | } |
| 133 | |
| 134 | static struct kvm_vmx_segment_field { |
| 135 | unsigned selector; |
| 136 | unsigned base; |
| 137 | unsigned limit; |
| 138 | unsigned ar_bytes; |
| 139 | } kvm_vmx_segment_fields[] = { |
| 140 | VMX_SEGMENT_FIELD(CS), |
| 141 | VMX_SEGMENT_FIELD(DS), |
| 142 | VMX_SEGMENT_FIELD(ES), |
| 143 | VMX_SEGMENT_FIELD(FS), |
| 144 | VMX_SEGMENT_FIELD(GS), |
| 145 | VMX_SEGMENT_FIELD(SS), |
| 146 | VMX_SEGMENT_FIELD(TR), |
| 147 | VMX_SEGMENT_FIELD(LDTR), |
| 148 | }; |
| 149 | |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 150 | /* |
| 151 | * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it |
| 152 | * away by decrementing the array size. |
| 153 | */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 154 | static const u32 vmx_msr_index[] = { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 155 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 156 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE, |
| 157 | #endif |
| 158 | MSR_EFER, MSR_K6_STAR, |
| 159 | }; |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 160 | #define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 161 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 162 | static void load_msrs(struct kvm_msr_entry *e, int n) |
| 163 | { |
| 164 | int i; |
| 165 | |
| 166 | for (i = 0; i < n; ++i) |
| 167 | wrmsrl(e[i].index, e[i].data); |
| 168 | } |
| 169 | |
| 170 | static void save_msrs(struct kvm_msr_entry *e, int n) |
| 171 | { |
| 172 | int i; |
| 173 | |
| 174 | for (i = 0; i < n; ++i) |
| 175 | rdmsrl(e[i].index, e[i].data); |
| 176 | } |
| 177 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 178 | static inline int is_page_fault(u32 intr_info) |
| 179 | { |
| 180 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 181 | INTR_INFO_VALID_MASK)) == |
| 182 | (INTR_TYPE_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK); |
| 183 | } |
| 184 | |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 185 | static inline int is_no_device(u32 intr_info) |
| 186 | { |
| 187 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 188 | INTR_INFO_VALID_MASK)) == |
| 189 | (INTR_TYPE_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK); |
| 190 | } |
| 191 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 192 | static inline int is_invalid_opcode(u32 intr_info) |
| 193 | { |
| 194 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 195 | INTR_INFO_VALID_MASK)) == |
| 196 | (INTR_TYPE_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK); |
| 197 | } |
| 198 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 199 | static inline int is_external_interrupt(u32 intr_info) |
| 200 | { |
| 201 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 202 | == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); |
| 203 | } |
| 204 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 205 | static inline int cpu_has_vmx_msr_bitmap(void) |
| 206 | { |
| 207 | return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS); |
| 208 | } |
| 209 | |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 210 | static inline int cpu_has_vmx_tpr_shadow(void) |
| 211 | { |
| 212 | return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW); |
| 213 | } |
| 214 | |
| 215 | static inline int vm_need_tpr_shadow(struct kvm *kvm) |
| 216 | { |
| 217 | return ((cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm))); |
| 218 | } |
| 219 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 220 | static inline int cpu_has_secondary_exec_ctrls(void) |
| 221 | { |
| 222 | return (vmcs_config.cpu_based_exec_ctrl & |
| 223 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS); |
| 224 | } |
| 225 | |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 226 | static inline bool cpu_has_vmx_virtualize_apic_accesses(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 227 | { |
Avi Kivity | 4c9fc8e | 2008-03-24 18:15:14 +0200 | [diff] [blame] | 228 | return flexpriority_enabled |
| 229 | && (vmcs_config.cpu_based_2nd_exec_ctrl & |
| 230 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 231 | } |
| 232 | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 233 | static inline int cpu_has_vmx_invept_individual_addr(void) |
| 234 | { |
| 235 | return (!!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT)); |
| 236 | } |
| 237 | |
| 238 | static inline int cpu_has_vmx_invept_context(void) |
| 239 | { |
| 240 | return (!!(vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT)); |
| 241 | } |
| 242 | |
| 243 | static inline int cpu_has_vmx_invept_global(void) |
| 244 | { |
| 245 | return (!!(vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT)); |
| 246 | } |
| 247 | |
| 248 | static inline int cpu_has_vmx_ept(void) |
| 249 | { |
| 250 | return (vmcs_config.cpu_based_2nd_exec_ctrl & |
| 251 | SECONDARY_EXEC_ENABLE_EPT); |
| 252 | } |
| 253 | |
| 254 | static inline int vm_need_ept(void) |
| 255 | { |
| 256 | return (cpu_has_vmx_ept() && enable_ept); |
| 257 | } |
| 258 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 259 | static inline int vm_need_virtualize_apic_accesses(struct kvm *kvm) |
| 260 | { |
| 261 | return ((cpu_has_vmx_virtualize_apic_accesses()) && |
| 262 | (irqchip_in_kernel(kvm))); |
| 263 | } |
| 264 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 265 | static inline int cpu_has_vmx_vpid(void) |
| 266 | { |
| 267 | return (vmcs_config.cpu_based_2nd_exec_ctrl & |
| 268 | SECONDARY_EXEC_ENABLE_VPID); |
| 269 | } |
| 270 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 271 | static inline int cpu_has_virtual_nmis(void) |
| 272 | { |
| 273 | return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS; |
| 274 | } |
| 275 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 276 | static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr) |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 277 | { |
| 278 | int i; |
| 279 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 280 | for (i = 0; i < vmx->nmsrs; ++i) |
| 281 | if (vmx->guest_msrs[i].index == msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 282 | return i; |
| 283 | return -1; |
| 284 | } |
| 285 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 286 | static inline void __invvpid(int ext, u16 vpid, gva_t gva) |
| 287 | { |
| 288 | struct { |
| 289 | u64 vpid : 16; |
| 290 | u64 rsvd : 48; |
| 291 | u64 gva; |
| 292 | } operand = { vpid, 0, gva }; |
| 293 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 294 | asm volatile (__ex(ASM_VMX_INVVPID) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 295 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 296 | "; ja 1f ; ud2 ; 1:" |
| 297 | : : "a"(&operand), "c"(ext) : "cc", "memory"); |
| 298 | } |
| 299 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 300 | static inline void __invept(int ext, u64 eptp, gpa_t gpa) |
| 301 | { |
| 302 | struct { |
| 303 | u64 eptp, gpa; |
| 304 | } operand = {eptp, gpa}; |
| 305 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 306 | asm volatile (__ex(ASM_VMX_INVEPT) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 307 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 308 | "; ja 1f ; ud2 ; 1:\n" |
| 309 | : : "a" (&operand), "c" (ext) : "cc", "memory"); |
| 310 | } |
| 311 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 312 | static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 313 | { |
| 314 | int i; |
| 315 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 316 | i = __find_msr_index(vmx, msr); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 317 | if (i >= 0) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 318 | return &vmx->guest_msrs[i]; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 319 | return NULL; |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 320 | } |
| 321 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 322 | static void vmcs_clear(struct vmcs *vmcs) |
| 323 | { |
| 324 | u64 phys_addr = __pa(vmcs); |
| 325 | u8 error; |
| 326 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 327 | asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 328 | : "=g"(error) : "a"(&phys_addr), "m"(phys_addr) |
| 329 | : "cc", "memory"); |
| 330 | if (error) |
| 331 | printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n", |
| 332 | vmcs, phys_addr); |
| 333 | } |
| 334 | |
| 335 | static void __vcpu_clear(void *arg) |
| 336 | { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 337 | struct vcpu_vmx *vmx = arg; |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 338 | int cpu = raw_smp_processor_id(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 339 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 340 | if (vmx->vcpu.cpu == cpu) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 341 | vmcs_clear(vmx->vmcs); |
| 342 | if (per_cpu(current_vmcs, cpu) == vmx->vmcs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 343 | per_cpu(current_vmcs, cpu) = NULL; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 344 | rdtscll(vmx->vcpu.arch.host_tsc); |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 345 | list_del(&vmx->local_vcpus_link); |
| 346 | vmx->vcpu.cpu = -1; |
| 347 | vmx->launched = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 348 | } |
| 349 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 350 | static void vcpu_clear(struct vcpu_vmx *vmx) |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 351 | { |
Avi Kivity | eae5ecb | 2007-09-30 10:50:12 +0200 | [diff] [blame] | 352 | if (vmx->vcpu.cpu == -1) |
| 353 | return; |
Jens Axboe | 8691e5a | 2008-06-06 11:18:06 +0200 | [diff] [blame] | 354 | smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1); |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 355 | } |
| 356 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 357 | static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx) |
| 358 | { |
| 359 | if (vmx->vpid == 0) |
| 360 | return; |
| 361 | |
| 362 | __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0); |
| 363 | } |
| 364 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 365 | static inline void ept_sync_global(void) |
| 366 | { |
| 367 | if (cpu_has_vmx_invept_global()) |
| 368 | __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0); |
| 369 | } |
| 370 | |
| 371 | static inline void ept_sync_context(u64 eptp) |
| 372 | { |
| 373 | if (vm_need_ept()) { |
| 374 | if (cpu_has_vmx_invept_context()) |
| 375 | __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0); |
| 376 | else |
| 377 | ept_sync_global(); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa) |
| 382 | { |
| 383 | if (vm_need_ept()) { |
| 384 | if (cpu_has_vmx_invept_individual_addr()) |
| 385 | __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR, |
| 386 | eptp, gpa); |
| 387 | else |
| 388 | ept_sync_context(eptp); |
| 389 | } |
| 390 | } |
| 391 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 392 | static unsigned long vmcs_readl(unsigned long field) |
| 393 | { |
| 394 | unsigned long value; |
| 395 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 396 | asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 397 | : "=a"(value) : "d"(field) : "cc"); |
| 398 | return value; |
| 399 | } |
| 400 | |
| 401 | static u16 vmcs_read16(unsigned long field) |
| 402 | { |
| 403 | return vmcs_readl(field); |
| 404 | } |
| 405 | |
| 406 | static u32 vmcs_read32(unsigned long field) |
| 407 | { |
| 408 | return vmcs_readl(field); |
| 409 | } |
| 410 | |
| 411 | static u64 vmcs_read64(unsigned long field) |
| 412 | { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 413 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 414 | return vmcs_readl(field); |
| 415 | #else |
| 416 | return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32); |
| 417 | #endif |
| 418 | } |
| 419 | |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 420 | static noinline void vmwrite_error(unsigned long field, unsigned long value) |
| 421 | { |
| 422 | printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n", |
| 423 | field, value, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 424 | dump_stack(); |
| 425 | } |
| 426 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 427 | static void vmcs_writel(unsigned long field, unsigned long value) |
| 428 | { |
| 429 | u8 error; |
| 430 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 431 | asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0" |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 432 | : "=q"(error) : "a"(value), "d"(field) : "cc"); |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 433 | if (unlikely(error)) |
| 434 | vmwrite_error(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | static void vmcs_write16(unsigned long field, u16 value) |
| 438 | { |
| 439 | vmcs_writel(field, value); |
| 440 | } |
| 441 | |
| 442 | static void vmcs_write32(unsigned long field, u32 value) |
| 443 | { |
| 444 | vmcs_writel(field, value); |
| 445 | } |
| 446 | |
| 447 | static void vmcs_write64(unsigned long field, u64 value) |
| 448 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 449 | vmcs_writel(field, value); |
Avi Kivity | 7682f2d | 2008-05-12 19:25:43 +0300 | [diff] [blame] | 450 | #ifndef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 451 | asm volatile (""); |
| 452 | vmcs_writel(field+1, value >> 32); |
| 453 | #endif |
| 454 | } |
| 455 | |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 456 | static void vmcs_clear_bits(unsigned long field, u32 mask) |
| 457 | { |
| 458 | vmcs_writel(field, vmcs_readl(field) & ~mask); |
| 459 | } |
| 460 | |
| 461 | static void vmcs_set_bits(unsigned long field, u32 mask) |
| 462 | { |
| 463 | vmcs_writel(field, vmcs_readl(field) | mask); |
| 464 | } |
| 465 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 466 | static void update_exception_bitmap(struct kvm_vcpu *vcpu) |
| 467 | { |
| 468 | u32 eb; |
| 469 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 470 | eb = (1u << PF_VECTOR) | (1u << UD_VECTOR); |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 471 | if (!vcpu->fpu_active) |
| 472 | eb |= 1u << NM_VECTOR; |
| 473 | if (vcpu->guest_debug.enabled) |
Jan Kiszka | 19bd8af | 2008-07-13 13:40:55 +0200 | [diff] [blame] | 474 | eb |= 1u << DB_VECTOR; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 475 | if (vcpu->arch.rmode.active) |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 476 | eb = ~0; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 477 | if (vm_need_ept()) |
| 478 | eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */ |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 479 | vmcs_write32(EXCEPTION_BITMAP, eb); |
| 480 | } |
| 481 | |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 482 | static void reload_tss(void) |
| 483 | { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 484 | /* |
| 485 | * VT restores TR but not its size. Useless. |
| 486 | */ |
| 487 | struct descriptor_table gdt; |
Avi Kivity | a5f6130 | 2008-02-20 17:57:21 +0200 | [diff] [blame] | 488 | struct desc_struct *descs; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 489 | |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 490 | kvm_get_gdt(&gdt); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 491 | descs = (void *)gdt.base; |
| 492 | descs[GDT_ENTRY_TSS].type = 9; /* available TSS */ |
| 493 | load_TR_desc(); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 494 | } |
| 495 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 496 | static void load_transition_efer(struct vcpu_vmx *vmx) |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 497 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 498 | int efer_offset = vmx->msr_offset_efer; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 499 | u64 host_efer = vmx->host_msrs[efer_offset].data; |
| 500 | u64 guest_efer = vmx->guest_msrs[efer_offset].data; |
| 501 | u64 ignore_bits; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 502 | |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 503 | if (efer_offset < 0) |
| 504 | return; |
| 505 | /* |
| 506 | * NX is emulated; LMA and LME handled by hardware; SCE meaninless |
| 507 | * outside long mode |
| 508 | */ |
| 509 | ignore_bits = EFER_NX | EFER_SCE; |
| 510 | #ifdef CONFIG_X86_64 |
| 511 | ignore_bits |= EFER_LMA | EFER_LME; |
| 512 | /* SCE is meaningful only in long mode on Intel */ |
| 513 | if (guest_efer & EFER_LMA) |
| 514 | ignore_bits &= ~(u64)EFER_SCE; |
| 515 | #endif |
| 516 | if ((guest_efer & ~ignore_bits) == (host_efer & ~ignore_bits)) |
| 517 | return; |
| 518 | |
| 519 | vmx->host_state.guest_efer_loaded = 1; |
| 520 | guest_efer &= ~ignore_bits; |
| 521 | guest_efer |= host_efer & ignore_bits; |
| 522 | wrmsrl(MSR_EFER, guest_efer); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 523 | vmx->vcpu.stat.efer_reload++; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 524 | } |
| 525 | |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 526 | static void reload_host_efer(struct vcpu_vmx *vmx) |
| 527 | { |
| 528 | if (vmx->host_state.guest_efer_loaded) { |
| 529 | vmx->host_state.guest_efer_loaded = 0; |
| 530 | load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1); |
| 531 | } |
| 532 | } |
| 533 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 534 | static void vmx_save_host_state(struct kvm_vcpu *vcpu) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 535 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 536 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 537 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 538 | if (vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 539 | return; |
| 540 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 541 | vmx->host_state.loaded = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 542 | /* |
| 543 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not |
| 544 | * allow segment selectors with cpl > 0 or ti == 1. |
| 545 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 546 | vmx->host_state.ldt_sel = kvm_read_ldt(); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 547 | vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel; |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 548 | vmx->host_state.fs_sel = kvm_read_fs(); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 549 | if (!(vmx->host_state.fs_sel & 7)) { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 550 | vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 551 | vmx->host_state.fs_reload_needed = 0; |
| 552 | } else { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 553 | vmcs_write16(HOST_FS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 554 | vmx->host_state.fs_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 555 | } |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 556 | vmx->host_state.gs_sel = kvm_read_gs(); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 557 | if (!(vmx->host_state.gs_sel & 7)) |
| 558 | vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 559 | else { |
| 560 | vmcs_write16(HOST_GS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 561 | vmx->host_state.gs_ldt_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | #ifdef CONFIG_X86_64 |
| 565 | vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE)); |
| 566 | vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE)); |
| 567 | #else |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 568 | vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel)); |
| 569 | vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel)); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 570 | #endif |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 571 | |
| 572 | #ifdef CONFIG_X86_64 |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 573 | if (is_long_mode(&vmx->vcpu)) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 574 | save_msrs(vmx->host_msrs + |
| 575 | vmx->msr_offset_kernel_gs_base, 1); |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 576 | |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 577 | #endif |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 578 | load_msrs(vmx->guest_msrs, vmx->save_nmsrs); |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 579 | load_transition_efer(vmx); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 580 | } |
| 581 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 582 | static void __vmx_load_host_state(struct vcpu_vmx *vmx) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 583 | { |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 584 | unsigned long flags; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 585 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 586 | if (!vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 587 | return; |
| 588 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 589 | ++vmx->vcpu.stat.host_state_reload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 590 | vmx->host_state.loaded = 0; |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 591 | if (vmx->host_state.fs_reload_needed) |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 592 | kvm_load_fs(vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 593 | if (vmx->host_state.gs_ldt_reload_needed) { |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 594 | kvm_load_ldt(vmx->host_state.ldt_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 595 | /* |
| 596 | * If we have to reload gs, we must take care to |
| 597 | * preserve our gs base. |
| 598 | */ |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 599 | local_irq_save(flags); |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 600 | kvm_load_gs(vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 601 | #ifdef CONFIG_X86_64 |
| 602 | wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE)); |
| 603 | #endif |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 604 | local_irq_restore(flags); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 605 | } |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 606 | reload_tss(); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 607 | save_msrs(vmx->guest_msrs, vmx->save_nmsrs); |
| 608 | load_msrs(vmx->host_msrs, vmx->save_nmsrs); |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 609 | reload_host_efer(vmx); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 610 | } |
| 611 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 612 | static void vmx_load_host_state(struct vcpu_vmx *vmx) |
| 613 | { |
| 614 | preempt_disable(); |
| 615 | __vmx_load_host_state(vmx); |
| 616 | preempt_enable(); |
| 617 | } |
| 618 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 619 | /* |
| 620 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes |
| 621 | * vcpu mutex is already taken. |
| 622 | */ |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 623 | static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 624 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 625 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 626 | u64 phys_addr = __pa(vmx->vmcs); |
Avi Kivity | 019960a | 2008-03-04 10:44:51 +0200 | [diff] [blame] | 627 | u64 tsc_this, delta, new_offset; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 628 | |
Eddie Dong | a3d7f85 | 2007-09-03 16:15:12 +0300 | [diff] [blame] | 629 | if (vcpu->cpu != cpu) { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 630 | vcpu_clear(vmx); |
Marcelo Tosatti | 2f59971 | 2008-05-27 12:10:20 -0300 | [diff] [blame] | 631 | kvm_migrate_timers(vcpu); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 632 | vpid_sync_vcpu_all(vmx); |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 633 | local_irq_disable(); |
| 634 | list_add(&vmx->local_vcpus_link, |
| 635 | &per_cpu(vcpus_on_cpu, cpu)); |
| 636 | local_irq_enable(); |
Eddie Dong | a3d7f85 | 2007-09-03 16:15:12 +0300 | [diff] [blame] | 637 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 638 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 639 | if (per_cpu(current_vmcs, cpu) != vmx->vmcs) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 640 | u8 error; |
| 641 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 642 | per_cpu(current_vmcs, cpu) = vmx->vmcs; |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 643 | asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 644 | : "=g"(error) : "a"(&phys_addr), "m"(phys_addr) |
| 645 | : "cc"); |
| 646 | if (error) |
| 647 | printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n", |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 648 | vmx->vmcs, phys_addr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | if (vcpu->cpu != cpu) { |
| 652 | struct descriptor_table dt; |
| 653 | unsigned long sysenter_esp; |
| 654 | |
| 655 | vcpu->cpu = cpu; |
| 656 | /* |
| 657 | * Linux uses per-cpu TSS and GDT, so set these when switching |
| 658 | * processors. |
| 659 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 660 | vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */ |
| 661 | kvm_get_gdt(&dt); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 662 | vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */ |
| 663 | |
| 664 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); |
| 665 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ |
Avi Kivity | 7700270 | 2007-06-13 19:55:28 +0300 | [diff] [blame] | 666 | |
| 667 | /* |
| 668 | * Make sure the time stamp counter is monotonous. |
| 669 | */ |
| 670 | rdtscll(tsc_this); |
Avi Kivity | 019960a | 2008-03-04 10:44:51 +0200 | [diff] [blame] | 671 | if (tsc_this < vcpu->arch.host_tsc) { |
| 672 | delta = vcpu->arch.host_tsc - tsc_this; |
| 673 | new_offset = vmcs_read64(TSC_OFFSET) + delta; |
| 674 | vmcs_write64(TSC_OFFSET, new_offset); |
| 675 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 676 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | static void vmx_vcpu_put(struct kvm_vcpu *vcpu) |
| 680 | { |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 681 | __vmx_load_host_state(to_vmx(vcpu)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 682 | } |
| 683 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 684 | static void vmx_fpu_activate(struct kvm_vcpu *vcpu) |
| 685 | { |
| 686 | if (vcpu->fpu_active) |
| 687 | return; |
| 688 | vcpu->fpu_active = 1; |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 689 | vmcs_clear_bits(GUEST_CR0, X86_CR0_TS); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 690 | if (vcpu->arch.cr0 & X86_CR0_TS) |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 691 | vmcs_set_bits(GUEST_CR0, X86_CR0_TS); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 692 | update_exception_bitmap(vcpu); |
| 693 | } |
| 694 | |
| 695 | static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu) |
| 696 | { |
| 697 | if (!vcpu->fpu_active) |
| 698 | return; |
| 699 | vcpu->fpu_active = 0; |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 700 | vmcs_set_bits(GUEST_CR0, X86_CR0_TS); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 701 | update_exception_bitmap(vcpu); |
| 702 | } |
| 703 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 704 | static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) |
| 705 | { |
| 706 | return vmcs_readl(GUEST_RFLAGS); |
| 707 | } |
| 708 | |
| 709 | static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 710 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 711 | if (vcpu->arch.rmode.active) |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 712 | rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 713 | vmcs_writel(GUEST_RFLAGS, rflags); |
| 714 | } |
| 715 | |
| 716 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 717 | { |
| 718 | unsigned long rip; |
| 719 | u32 interruptibility; |
| 720 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 721 | rip = kvm_rip_read(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 722 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 723 | kvm_rip_write(vcpu, rip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 724 | |
| 725 | /* |
| 726 | * We emulated an instruction, so temporary interrupt blocking |
| 727 | * should be removed, if set. |
| 728 | */ |
| 729 | interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 730 | if (interruptibility & 3) |
| 731 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, |
| 732 | interruptibility & ~3); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 733 | vcpu->arch.interrupt_window_open = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 734 | } |
| 735 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 736 | static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr, |
| 737 | bool has_error_code, u32 error_code) |
| 738 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 739 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 740 | |
| 741 | if (has_error_code) |
| 742 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); |
| 743 | |
| 744 | if (vcpu->arch.rmode.active) { |
| 745 | vmx->rmode.irq.pending = true; |
| 746 | vmx->rmode.irq.vector = nr; |
| 747 | vmx->rmode.irq.rip = kvm_rip_read(vcpu); |
| 748 | if (nr == BP_VECTOR) |
| 749 | vmx->rmode.irq.rip++; |
| 750 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 751 | nr | INTR_TYPE_SOFT_INTR |
| 752 | | (has_error_code ? INTR_INFO_DELIVER_CODE_MASK : 0) |
| 753 | | INTR_INFO_VALID_MASK); |
| 754 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1); |
| 755 | kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1); |
| 756 | return; |
| 757 | } |
| 758 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 759 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 760 | nr | INTR_TYPE_EXCEPTION |
Ryan Harper | 2e11384 | 2008-02-11 10:26:38 -0600 | [diff] [blame] | 761 | | (has_error_code ? INTR_INFO_DELIVER_CODE_MASK : 0) |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 762 | | INTR_INFO_VALID_MASK); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | static bool vmx_exception_injected(struct kvm_vcpu *vcpu) |
| 766 | { |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 767 | return false; |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 768 | } |
| 769 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 770 | /* |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 771 | * Swap MSR entry in host/guest MSR entry array. |
| 772 | */ |
Gabriel C | 54e11fa | 2007-08-01 16:23:10 +0200 | [diff] [blame] | 773 | #ifdef CONFIG_X86_64 |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 774 | static void move_msr_up(struct vcpu_vmx *vmx, int from, int to) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 775 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 776 | struct kvm_msr_entry tmp; |
| 777 | |
| 778 | tmp = vmx->guest_msrs[to]; |
| 779 | vmx->guest_msrs[to] = vmx->guest_msrs[from]; |
| 780 | vmx->guest_msrs[from] = tmp; |
| 781 | tmp = vmx->host_msrs[to]; |
| 782 | vmx->host_msrs[to] = vmx->host_msrs[from]; |
| 783 | vmx->host_msrs[from] = tmp; |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 784 | } |
Gabriel C | 54e11fa | 2007-08-01 16:23:10 +0200 | [diff] [blame] | 785 | #endif |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 786 | |
| 787 | /* |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 788 | * Set up the vmcs to automatically save and restore system |
| 789 | * msrs. Don't touch the 64-bit msrs if the guest is in legacy |
| 790 | * mode, as fiddling with msrs is very expensive. |
| 791 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 792 | static void setup_msrs(struct vcpu_vmx *vmx) |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 793 | { |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 794 | int save_nmsrs; |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 795 | |
Avi Kivity | 33f9c50 | 2008-02-27 16:06:57 +0200 | [diff] [blame] | 796 | vmx_load_host_state(vmx); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 797 | save_nmsrs = 0; |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 798 | #ifdef CONFIG_X86_64 |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 799 | if (is_long_mode(&vmx->vcpu)) { |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 800 | int index; |
| 801 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 802 | index = __find_msr_index(vmx, MSR_SYSCALL_MASK); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 803 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 804 | move_msr_up(vmx, index, save_nmsrs++); |
| 805 | index = __find_msr_index(vmx, MSR_LSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 806 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 807 | move_msr_up(vmx, index, save_nmsrs++); |
| 808 | index = __find_msr_index(vmx, MSR_CSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 809 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 810 | move_msr_up(vmx, index, save_nmsrs++); |
| 811 | index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 812 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 813 | move_msr_up(vmx, index, save_nmsrs++); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 814 | /* |
| 815 | * MSR_K6_STAR is only needed on long mode guests, and only |
| 816 | * if efer.sce is enabled. |
| 817 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 818 | index = __find_msr_index(vmx, MSR_K6_STAR); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 819 | if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE)) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 820 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 821 | } |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 822 | #endif |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 823 | vmx->save_nmsrs = save_nmsrs; |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 824 | |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 825 | #ifdef CONFIG_X86_64 |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 826 | vmx->msr_offset_kernel_gs_base = |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 827 | __find_msr_index(vmx, MSR_KERNEL_GS_BASE); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 828 | #endif |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 829 | vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER); |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 833 | * reads and returns guest's timestamp counter "register" |
| 834 | * guest_tsc = host_tsc + tsc_offset -- 21.3 |
| 835 | */ |
| 836 | static u64 guest_read_tsc(void) |
| 837 | { |
| 838 | u64 host_tsc, tsc_offset; |
| 839 | |
| 840 | rdtscll(host_tsc); |
| 841 | tsc_offset = vmcs_read64(TSC_OFFSET); |
| 842 | return host_tsc + tsc_offset; |
| 843 | } |
| 844 | |
| 845 | /* |
| 846 | * writes 'guest_tsc' into guest's timestamp counter "register" |
| 847 | * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc |
| 848 | */ |
| 849 | static void guest_write_tsc(u64 guest_tsc) |
| 850 | { |
| 851 | u64 host_tsc; |
| 852 | |
| 853 | rdtscll(host_tsc); |
| 854 | vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc); |
| 855 | } |
| 856 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 857 | /* |
| 858 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 859 | * Returns 0 on success, non-0 otherwise. |
| 860 | * Assumes vcpu_load() was already called. |
| 861 | */ |
| 862 | static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
| 863 | { |
| 864 | u64 data; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 865 | struct kvm_msr_entry *msr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 866 | |
| 867 | if (!pdata) { |
| 868 | printk(KERN_ERR "BUG: get_msr called with NULL pdata\n"); |
| 869 | return -EINVAL; |
| 870 | } |
| 871 | |
| 872 | switch (msr_index) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 873 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 874 | case MSR_FS_BASE: |
| 875 | data = vmcs_readl(GUEST_FS_BASE); |
| 876 | break; |
| 877 | case MSR_GS_BASE: |
| 878 | data = vmcs_readl(GUEST_GS_BASE); |
| 879 | break; |
| 880 | case MSR_EFER: |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 881 | return kvm_get_msr_common(vcpu, msr_index, pdata); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 882 | #endif |
| 883 | case MSR_IA32_TIME_STAMP_COUNTER: |
| 884 | data = guest_read_tsc(); |
| 885 | break; |
| 886 | case MSR_IA32_SYSENTER_CS: |
| 887 | data = vmcs_read32(GUEST_SYSENTER_CS); |
| 888 | break; |
| 889 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 890 | data = vmcs_readl(GUEST_SYSENTER_EIP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 891 | break; |
| 892 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 893 | data = vmcs_readl(GUEST_SYSENTER_ESP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 894 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 895 | default: |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 896 | msr = find_msr_entry(to_vmx(vcpu), msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 897 | if (msr) { |
| 898 | data = msr->data; |
| 899 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 900 | } |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 901 | return kvm_get_msr_common(vcpu, msr_index, pdata); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | *pdata = data; |
| 905 | return 0; |
| 906 | } |
| 907 | |
| 908 | /* |
| 909 | * Writes msr value into into the appropriate "register". |
| 910 | * Returns 0 on success, non-0 otherwise. |
| 911 | * Assumes vcpu_load() was already called. |
| 912 | */ |
| 913 | static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 914 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 915 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 916 | struct kvm_msr_entry *msr; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 917 | int ret = 0; |
| 918 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 919 | switch (msr_index) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 920 | #ifdef CONFIG_X86_64 |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 921 | case MSR_EFER: |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 922 | vmx_load_host_state(vmx); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 923 | ret = kvm_set_msr_common(vcpu, msr_index, data); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 924 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 925 | case MSR_FS_BASE: |
| 926 | vmcs_writel(GUEST_FS_BASE, data); |
| 927 | break; |
| 928 | case MSR_GS_BASE: |
| 929 | vmcs_writel(GUEST_GS_BASE, data); |
| 930 | break; |
| 931 | #endif |
| 932 | case MSR_IA32_SYSENTER_CS: |
| 933 | vmcs_write32(GUEST_SYSENTER_CS, data); |
| 934 | break; |
| 935 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 936 | vmcs_writel(GUEST_SYSENTER_EIP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 937 | break; |
| 938 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 939 | vmcs_writel(GUEST_SYSENTER_ESP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 940 | break; |
Avi Kivity | d27d4ac | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 941 | case MSR_IA32_TIME_STAMP_COUNTER: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 942 | guest_write_tsc(data); |
| 943 | break; |
Chris Lalancette | efa67e0 | 2008-06-20 09:51:30 +0200 | [diff] [blame] | 944 | case MSR_P6_PERFCTR0: |
| 945 | case MSR_P6_PERFCTR1: |
| 946 | case MSR_P6_EVNTSEL0: |
| 947 | case MSR_P6_EVNTSEL1: |
| 948 | /* |
| 949 | * Just discard all writes to the performance counters; this |
| 950 | * should keep both older linux and windows 64-bit guests |
| 951 | * happy |
| 952 | */ |
| 953 | pr_unimpl(vcpu, "unimplemented perfctr wrmsr: 0x%x data 0x%llx\n", msr_index, data); |
| 954 | |
| 955 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 956 | default: |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 957 | vmx_load_host_state(vmx); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 958 | msr = find_msr_entry(vmx, msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 959 | if (msr) { |
| 960 | msr->data = data; |
| 961 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 962 | } |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 963 | ret = kvm_set_msr_common(vcpu, msr_index, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 964 | } |
| 965 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 966 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 967 | } |
| 968 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 969 | static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 970 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 971 | __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); |
| 972 | switch (reg) { |
| 973 | case VCPU_REGS_RSP: |
| 974 | vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); |
| 975 | break; |
| 976 | case VCPU_REGS_RIP: |
| 977 | vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); |
| 978 | break; |
| 979 | default: |
| 980 | break; |
| 981 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg) |
| 985 | { |
| 986 | unsigned long dr7 = 0x400; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 987 | int old_singlestep; |
| 988 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 989 | old_singlestep = vcpu->guest_debug.singlestep; |
| 990 | |
| 991 | vcpu->guest_debug.enabled = dbg->enabled; |
| 992 | if (vcpu->guest_debug.enabled) { |
| 993 | int i; |
| 994 | |
| 995 | dr7 |= 0x200; /* exact */ |
| 996 | for (i = 0; i < 4; ++i) { |
| 997 | if (!dbg->breakpoints[i].enabled) |
| 998 | continue; |
| 999 | vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address; |
| 1000 | dr7 |= 2 << (i*2); /* global enable */ |
| 1001 | dr7 |= 0 << (i*4+16); /* execution breakpoint */ |
| 1002 | } |
| 1003 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1004 | vcpu->guest_debug.singlestep = dbg->singlestep; |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1005 | } else |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1006 | vcpu->guest_debug.singlestep = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1007 | |
| 1008 | if (old_singlestep && !vcpu->guest_debug.singlestep) { |
| 1009 | unsigned long flags; |
| 1010 | |
| 1011 | flags = vmcs_readl(GUEST_RFLAGS); |
| 1012 | flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF); |
| 1013 | vmcs_writel(GUEST_RFLAGS, flags); |
| 1014 | } |
| 1015 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1016 | update_exception_bitmap(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1017 | vmcs_writel(GUEST_DR7, dr7); |
| 1018 | |
| 1019 | return 0; |
| 1020 | } |
| 1021 | |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 1022 | static int vmx_get_irq(struct kvm_vcpu *vcpu) |
| 1023 | { |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 1024 | if (!vcpu->arch.interrupt.pending) |
| 1025 | return -1; |
| 1026 | return vcpu->arch.interrupt.nr; |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 1027 | } |
| 1028 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1029 | static __init int cpu_has_kvm_support(void) |
| 1030 | { |
| 1031 | unsigned long ecx = cpuid_ecx(1); |
| 1032 | return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */ |
| 1033 | } |
| 1034 | |
| 1035 | static __init int vmx_disabled_by_bios(void) |
| 1036 | { |
| 1037 | u64 msr; |
| 1038 | |
| 1039 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); |
Sheng Yang | ca60dfb | 2008-06-24 17:02:38 +0800 | [diff] [blame] | 1040 | return (msr & (IA32_FEATURE_CONTROL_LOCKED_BIT | |
| 1041 | IA32_FEATURE_CONTROL_VMXON_ENABLED_BIT)) |
| 1042 | == IA32_FEATURE_CONTROL_LOCKED_BIT; |
Yang, Sheng | 62b3ffb | 2007-07-25 12:17:06 +0300 | [diff] [blame] | 1043 | /* locked but not enabled */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1044 | } |
| 1045 | |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1046 | static void hardware_enable(void *garbage) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1047 | { |
| 1048 | int cpu = raw_smp_processor_id(); |
| 1049 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
| 1050 | u64 old; |
| 1051 | |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 1052 | INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1053 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); |
Sheng Yang | ca60dfb | 2008-06-24 17:02:38 +0800 | [diff] [blame] | 1054 | if ((old & (IA32_FEATURE_CONTROL_LOCKED_BIT | |
| 1055 | IA32_FEATURE_CONTROL_VMXON_ENABLED_BIT)) |
| 1056 | != (IA32_FEATURE_CONTROL_LOCKED_BIT | |
| 1057 | IA32_FEATURE_CONTROL_VMXON_ENABLED_BIT)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1058 | /* enable and lock */ |
Yang, Sheng | 62b3ffb | 2007-07-25 12:17:06 +0300 | [diff] [blame] | 1059 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | |
Sheng Yang | ca60dfb | 2008-06-24 17:02:38 +0800 | [diff] [blame] | 1060 | IA32_FEATURE_CONTROL_LOCKED_BIT | |
| 1061 | IA32_FEATURE_CONTROL_VMXON_ENABLED_BIT); |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 1062 | write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */ |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1063 | asm volatile (ASM_VMX_VMXON_RAX |
| 1064 | : : "a"(&phys_addr), "m"(phys_addr) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1065 | : "memory", "cc"); |
| 1066 | } |
| 1067 | |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 1068 | static void vmclear_local_vcpus(void) |
| 1069 | { |
| 1070 | int cpu = raw_smp_processor_id(); |
| 1071 | struct vcpu_vmx *vmx, *n; |
| 1072 | |
| 1073 | list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu), |
| 1074 | local_vcpus_link) |
| 1075 | __vcpu_clear(vmx); |
| 1076 | } |
| 1077 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1078 | static void hardware_disable(void *garbage) |
| 1079 | { |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 1080 | vmclear_local_vcpus(); |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1081 | asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc"); |
Eli Collins | e693d71 | 2008-06-01 20:24:40 -0700 | [diff] [blame] | 1082 | write_cr4(read_cr4() & ~X86_CR4_VMXE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1083 | } |
| 1084 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1085 | static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1086 | u32 msr, u32 *result) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1087 | { |
| 1088 | u32 vmx_msr_low, vmx_msr_high; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1089 | u32 ctl = ctl_min | ctl_opt; |
| 1090 | |
| 1091 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 1092 | |
| 1093 | ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */ |
| 1094 | ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */ |
| 1095 | |
| 1096 | /* Ensure minimum (required) set of control bits are supported. */ |
| 1097 | if (ctl_min & ~ctl) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1098 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1099 | |
| 1100 | *result = ctl; |
| 1101 | return 0; |
| 1102 | } |
| 1103 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1104 | static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf) |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1105 | { |
| 1106 | u32 vmx_msr_low, vmx_msr_high; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1107 | u32 min, opt, min2, opt2; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1108 | u32 _pin_based_exec_control = 0; |
| 1109 | u32 _cpu_based_exec_control = 0; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1110 | u32 _cpu_based_2nd_exec_control = 0; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1111 | u32 _vmexit_control = 0; |
| 1112 | u32 _vmentry_control = 0; |
| 1113 | |
| 1114 | min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING; |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 1115 | opt = PIN_BASED_VIRTUAL_NMIS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1116 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, |
| 1117 | &_pin_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1118 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1119 | |
| 1120 | min = CPU_BASED_HLT_EXITING | |
| 1121 | #ifdef CONFIG_X86_64 |
| 1122 | CPU_BASED_CR8_LOAD_EXITING | |
| 1123 | CPU_BASED_CR8_STORE_EXITING | |
| 1124 | #endif |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1125 | CPU_BASED_CR3_LOAD_EXITING | |
| 1126 | CPU_BASED_CR3_STORE_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1127 | CPU_BASED_USE_IO_BITMAPS | |
| 1128 | CPU_BASED_MOV_DR_EXITING | |
| 1129 | CPU_BASED_USE_TSC_OFFSETING; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1130 | opt = CPU_BASED_TPR_SHADOW | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 1131 | CPU_BASED_USE_MSR_BITMAPS | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1132 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1133 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
| 1134 | &_cpu_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1135 | return -EIO; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1136 | #ifdef CONFIG_X86_64 |
| 1137 | if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 1138 | _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING & |
| 1139 | ~CPU_BASED_CR8_STORE_EXITING; |
| 1140 | #endif |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1141 | if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1142 | min2 = 0; |
| 1143 | opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1144 | SECONDARY_EXEC_WBINVD_EXITING | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1145 | SECONDARY_EXEC_ENABLE_VPID | |
| 1146 | SECONDARY_EXEC_ENABLE_EPT; |
| 1147 | if (adjust_vmx_controls(min2, opt2, |
| 1148 | MSR_IA32_VMX_PROCBASED_CTLS2, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1149 | &_cpu_based_2nd_exec_control) < 0) |
| 1150 | return -EIO; |
| 1151 | } |
| 1152 | #ifndef CONFIG_X86_64 |
| 1153 | if (!(_cpu_based_2nd_exec_control & |
| 1154 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
| 1155 | _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 1156 | #endif |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1157 | if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { |
| 1158 | /* CR3 accesses don't need to cause VM Exits when EPT enabled */ |
| 1159 | min &= ~(CPU_BASED_CR3_LOAD_EXITING | |
| 1160 | CPU_BASED_CR3_STORE_EXITING); |
| 1161 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
| 1162 | &_cpu_based_exec_control) < 0) |
| 1163 | return -EIO; |
| 1164 | rdmsr(MSR_IA32_VMX_EPT_VPID_CAP, |
| 1165 | vmx_capability.ept, vmx_capability.vpid); |
| 1166 | } |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1167 | |
| 1168 | min = 0; |
| 1169 | #ifdef CONFIG_X86_64 |
| 1170 | min |= VM_EXIT_HOST_ADDR_SPACE_SIZE; |
| 1171 | #endif |
| 1172 | opt = 0; |
| 1173 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS, |
| 1174 | &_vmexit_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1175 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1176 | |
| 1177 | min = opt = 0; |
| 1178 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS, |
| 1179 | &_vmentry_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1180 | return -EIO; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1181 | |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 1182 | rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1183 | |
| 1184 | /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ |
| 1185 | if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1186 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1187 | |
| 1188 | #ifdef CONFIG_X86_64 |
| 1189 | /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ |
| 1190 | if (vmx_msr_high & (1u<<16)) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1191 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1192 | #endif |
| 1193 | |
| 1194 | /* Require Write-Back (WB) memory type for VMCS accesses. */ |
| 1195 | if (((vmx_msr_high >> 18) & 15) != 6) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1196 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1197 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1198 | vmcs_conf->size = vmx_msr_high & 0x1fff; |
| 1199 | vmcs_conf->order = get_order(vmcs_config.size); |
| 1200 | vmcs_conf->revision_id = vmx_msr_low; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1201 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1202 | vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; |
| 1203 | vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1204 | vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1205 | vmcs_conf->vmexit_ctrl = _vmexit_control; |
| 1206 | vmcs_conf->vmentry_ctrl = _vmentry_control; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1207 | |
| 1208 | return 0; |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 1209 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1210 | |
| 1211 | static struct vmcs *alloc_vmcs_cpu(int cpu) |
| 1212 | { |
| 1213 | int node = cpu_to_node(cpu); |
| 1214 | struct page *pages; |
| 1215 | struct vmcs *vmcs; |
| 1216 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1217 | pages = alloc_pages_node(node, GFP_KERNEL, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1218 | if (!pages) |
| 1219 | return NULL; |
| 1220 | vmcs = page_address(pages); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1221 | memset(vmcs, 0, vmcs_config.size); |
| 1222 | vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1223 | return vmcs; |
| 1224 | } |
| 1225 | |
| 1226 | static struct vmcs *alloc_vmcs(void) |
| 1227 | { |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 1228 | return alloc_vmcs_cpu(raw_smp_processor_id()); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | static void free_vmcs(struct vmcs *vmcs) |
| 1232 | { |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1233 | free_pages((unsigned long)vmcs, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1234 | } |
| 1235 | |
Sam Ravnborg | 3995958 | 2007-06-01 00:47:13 -0700 | [diff] [blame] | 1236 | static void free_kvm_area(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1237 | { |
| 1238 | int cpu; |
| 1239 | |
| 1240 | for_each_online_cpu(cpu) |
| 1241 | free_vmcs(per_cpu(vmxarea, cpu)); |
| 1242 | } |
| 1243 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1244 | static __init int alloc_kvm_area(void) |
| 1245 | { |
| 1246 | int cpu; |
| 1247 | |
| 1248 | for_each_online_cpu(cpu) { |
| 1249 | struct vmcs *vmcs; |
| 1250 | |
| 1251 | vmcs = alloc_vmcs_cpu(cpu); |
| 1252 | if (!vmcs) { |
| 1253 | free_kvm_area(); |
| 1254 | return -ENOMEM; |
| 1255 | } |
| 1256 | |
| 1257 | per_cpu(vmxarea, cpu) = vmcs; |
| 1258 | } |
| 1259 | return 0; |
| 1260 | } |
| 1261 | |
| 1262 | static __init int hardware_setup(void) |
| 1263 | { |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1264 | if (setup_vmcs_config(&vmcs_config) < 0) |
| 1265 | return -EIO; |
Joerg Roedel | 50a37eb | 2008-01-31 14:57:38 +0100 | [diff] [blame] | 1266 | |
| 1267 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 1268 | kvm_enable_efer_bits(EFER_NX); |
| 1269 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1270 | return alloc_kvm_area(); |
| 1271 | } |
| 1272 | |
| 1273 | static __exit void hardware_unsetup(void) |
| 1274 | { |
| 1275 | free_kvm_area(); |
| 1276 | } |
| 1277 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1278 | static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save) |
| 1279 | { |
| 1280 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 1281 | |
Avi Kivity | 6af11b9 | 2007-03-19 13:18:10 +0200 | [diff] [blame] | 1282 | if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1283 | vmcs_write16(sf->selector, save->selector); |
| 1284 | vmcs_writel(sf->base, save->base); |
| 1285 | vmcs_write32(sf->limit, save->limit); |
| 1286 | vmcs_write32(sf->ar_bytes, save->ar); |
| 1287 | } else { |
| 1288 | u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK) |
| 1289 | << AR_DPL_SHIFT; |
| 1290 | vmcs_write32(sf->ar_bytes, 0x93 | dpl); |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | static void enter_pmode(struct kvm_vcpu *vcpu) |
| 1295 | { |
| 1296 | unsigned long flags; |
| 1297 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1298 | vcpu->arch.rmode.active = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1299 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1300 | vmcs_writel(GUEST_TR_BASE, vcpu->arch.rmode.tr.base); |
| 1301 | vmcs_write32(GUEST_TR_LIMIT, vcpu->arch.rmode.tr.limit); |
| 1302 | vmcs_write32(GUEST_TR_AR_BYTES, vcpu->arch.rmode.tr.ar); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1303 | |
| 1304 | flags = vmcs_readl(GUEST_RFLAGS); |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 1305 | flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1306 | flags |= (vcpu->arch.rmode.save_iopl << IOPL_SHIFT); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1307 | vmcs_writel(GUEST_RFLAGS, flags); |
| 1308 | |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 1309 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | |
| 1310 | (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1311 | |
| 1312 | update_exception_bitmap(vcpu); |
| 1313 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1314 | fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->arch.rmode.es); |
| 1315 | fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->arch.rmode.ds); |
| 1316 | fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->arch.rmode.gs); |
| 1317 | fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->arch.rmode.fs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1318 | |
| 1319 | vmcs_write16(GUEST_SS_SELECTOR, 0); |
| 1320 | vmcs_write32(GUEST_SS_AR_BYTES, 0x93); |
| 1321 | |
| 1322 | vmcs_write16(GUEST_CS_SELECTOR, |
| 1323 | vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK); |
| 1324 | vmcs_write32(GUEST_CS_AR_BYTES, 0x9b); |
| 1325 | } |
| 1326 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1327 | static gva_t rmode_tss_base(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1328 | { |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 1329 | if (!kvm->arch.tss_addr) { |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 1330 | gfn_t base_gfn = kvm->memslots[0].base_gfn + |
| 1331 | kvm->memslots[0].npages - 3; |
| 1332 | return base_gfn << PAGE_SHIFT; |
| 1333 | } |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 1334 | return kvm->arch.tss_addr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | static void fix_rmode_seg(int seg, struct kvm_save_segment *save) |
| 1338 | { |
| 1339 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 1340 | |
| 1341 | save->selector = vmcs_read16(sf->selector); |
| 1342 | save->base = vmcs_readl(sf->base); |
| 1343 | save->limit = vmcs_read32(sf->limit); |
| 1344 | save->ar = vmcs_read32(sf->ar_bytes); |
Jan Kiszka | 15b00f3 | 2007-11-19 10:21:45 +0100 | [diff] [blame] | 1345 | vmcs_write16(sf->selector, save->base >> 4); |
| 1346 | vmcs_write32(sf->base, save->base & 0xfffff); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1347 | vmcs_write32(sf->limit, 0xffff); |
| 1348 | vmcs_write32(sf->ar_bytes, 0xf3); |
| 1349 | } |
| 1350 | |
| 1351 | static void enter_rmode(struct kvm_vcpu *vcpu) |
| 1352 | { |
| 1353 | unsigned long flags; |
| 1354 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1355 | vcpu->arch.rmode.active = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1356 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1357 | vcpu->arch.rmode.tr.base = vmcs_readl(GUEST_TR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1358 | vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm)); |
| 1359 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1360 | vcpu->arch.rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1361 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); |
| 1362 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1363 | vcpu->arch.rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1364 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 1365 | |
| 1366 | flags = vmcs_readl(GUEST_RFLAGS); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1367 | vcpu->arch.rmode.save_iopl |
| 1368 | = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1369 | |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 1370 | flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1371 | |
| 1372 | vmcs_writel(GUEST_RFLAGS, flags); |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 1373 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1374 | update_exception_bitmap(vcpu); |
| 1375 | |
| 1376 | vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4); |
| 1377 | vmcs_write32(GUEST_SS_LIMIT, 0xffff); |
| 1378 | vmcs_write32(GUEST_SS_AR_BYTES, 0xf3); |
| 1379 | |
| 1380 | vmcs_write32(GUEST_CS_AR_BYTES, 0xf3); |
Michael Riepe | abacf8d | 2006-12-22 01:05:45 -0800 | [diff] [blame] | 1381 | vmcs_write32(GUEST_CS_LIMIT, 0xffff); |
Avi Kivity | 8cb5b03 | 2007-03-20 18:40:40 +0200 | [diff] [blame] | 1382 | if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000) |
| 1383 | vmcs_writel(GUEST_CS_BASE, 0xf0000); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1384 | vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4); |
| 1385 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1386 | fix_rmode_seg(VCPU_SREG_ES, &vcpu->arch.rmode.es); |
| 1387 | fix_rmode_seg(VCPU_SREG_DS, &vcpu->arch.rmode.ds); |
| 1388 | fix_rmode_seg(VCPU_SREG_GS, &vcpu->arch.rmode.gs); |
| 1389 | fix_rmode_seg(VCPU_SREG_FS, &vcpu->arch.rmode.fs); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 1390 | |
Eddie Dong | 8668a3c | 2007-10-10 14:26:45 +0800 | [diff] [blame] | 1391 | kvm_mmu_reset_context(vcpu); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 1392 | init_rmode(vcpu->kvm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1393 | } |
| 1394 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1395 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1396 | |
| 1397 | static void enter_lmode(struct kvm_vcpu *vcpu) |
| 1398 | { |
| 1399 | u32 guest_tr_ar; |
| 1400 | |
| 1401 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); |
| 1402 | if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) { |
| 1403 | printk(KERN_DEBUG "%s: tss fixup for long mode. \n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 1404 | __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1405 | vmcs_write32(GUEST_TR_AR_BYTES, |
| 1406 | (guest_tr_ar & ~AR_TYPE_MASK) |
| 1407 | | AR_TYPE_BUSY_64_TSS); |
| 1408 | } |
| 1409 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1410 | vcpu->arch.shadow_efer |= EFER_LMA; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1411 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1412 | find_msr_entry(to_vmx(vcpu), MSR_EFER)->data |= EFER_LMA | EFER_LME; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1413 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 1414 | vmcs_read32(VM_ENTRY_CONTROLS) |
Li, Xin B | 1e4e6e0 | 2007-08-01 21:49:10 +0300 | [diff] [blame] | 1415 | | VM_ENTRY_IA32E_MODE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1416 | } |
| 1417 | |
| 1418 | static void exit_lmode(struct kvm_vcpu *vcpu) |
| 1419 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1420 | vcpu->arch.shadow_efer &= ~EFER_LMA; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1421 | |
| 1422 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 1423 | vmcs_read32(VM_ENTRY_CONTROLS) |
Li, Xin B | 1e4e6e0 | 2007-08-01 21:49:10 +0300 | [diff] [blame] | 1424 | & ~VM_ENTRY_IA32E_MODE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | #endif |
| 1428 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1429 | static void vmx_flush_tlb(struct kvm_vcpu *vcpu) |
| 1430 | { |
| 1431 | vpid_sync_vcpu_all(to_vmx(vcpu)); |
Sheng Yang | 4e1096d | 2008-07-06 19:16:51 +0800 | [diff] [blame] | 1432 | if (vm_need_ept()) |
| 1433 | ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa)); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1434 | } |
| 1435 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 1436 | static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 1437 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1438 | vcpu->arch.cr4 &= KVM_GUEST_CR4_MASK; |
| 1439 | vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK; |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 1440 | } |
| 1441 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1442 | static void ept_load_pdptrs(struct kvm_vcpu *vcpu) |
| 1443 | { |
| 1444 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
| 1445 | if (!load_pdptrs(vcpu, vcpu->arch.cr3)) { |
| 1446 | printk(KERN_ERR "EPT: Fail to load pdptrs!\n"); |
| 1447 | return; |
| 1448 | } |
| 1449 | vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]); |
| 1450 | vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]); |
| 1451 | vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]); |
| 1452 | vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]); |
| 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); |
| 1457 | |
| 1458 | static void ept_update_paging_mode_cr0(unsigned long *hw_cr0, |
| 1459 | unsigned long cr0, |
| 1460 | struct kvm_vcpu *vcpu) |
| 1461 | { |
| 1462 | if (!(cr0 & X86_CR0_PG)) { |
| 1463 | /* From paging/starting to nonpaging */ |
| 1464 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 1465 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1466 | (CPU_BASED_CR3_LOAD_EXITING | |
| 1467 | CPU_BASED_CR3_STORE_EXITING)); |
| 1468 | vcpu->arch.cr0 = cr0; |
| 1469 | vmx_set_cr4(vcpu, vcpu->arch.cr4); |
| 1470 | *hw_cr0 |= X86_CR0_PE | X86_CR0_PG; |
| 1471 | *hw_cr0 &= ~X86_CR0_WP; |
| 1472 | } else if (!is_paging(vcpu)) { |
| 1473 | /* From nonpaging to paging */ |
| 1474 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 1475 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) & |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1476 | ~(CPU_BASED_CR3_LOAD_EXITING | |
| 1477 | CPU_BASED_CR3_STORE_EXITING)); |
| 1478 | vcpu->arch.cr0 = cr0; |
| 1479 | vmx_set_cr4(vcpu, vcpu->arch.cr4); |
| 1480 | if (!(vcpu->arch.cr0 & X86_CR0_WP)) |
| 1481 | *hw_cr0 &= ~X86_CR0_WP; |
| 1482 | } |
| 1483 | } |
| 1484 | |
| 1485 | static void ept_update_paging_mode_cr4(unsigned long *hw_cr4, |
| 1486 | struct kvm_vcpu *vcpu) |
| 1487 | { |
| 1488 | if (!is_paging(vcpu)) { |
| 1489 | *hw_cr4 &= ~X86_CR4_PAE; |
| 1490 | *hw_cr4 |= X86_CR4_PSE; |
| 1491 | } else if (!(vcpu->arch.cr4 & X86_CR4_PAE)) |
| 1492 | *hw_cr4 &= ~X86_CR4_PAE; |
| 1493 | } |
| 1494 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1495 | static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 1496 | { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1497 | unsigned long hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | |
| 1498 | KVM_VM_CR0_ALWAYS_ON; |
| 1499 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1500 | vmx_fpu_deactivate(vcpu); |
| 1501 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1502 | if (vcpu->arch.rmode.active && (cr0 & X86_CR0_PE)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1503 | enter_pmode(vcpu); |
| 1504 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1505 | if (!vcpu->arch.rmode.active && !(cr0 & X86_CR0_PE)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1506 | enter_rmode(vcpu); |
| 1507 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1508 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1509 | if (vcpu->arch.shadow_efer & EFER_LME) { |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 1510 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1511 | enter_lmode(vcpu); |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 1512 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1513 | exit_lmode(vcpu); |
| 1514 | } |
| 1515 | #endif |
| 1516 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1517 | if (vm_need_ept()) |
| 1518 | ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu); |
| 1519 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1520 | vmcs_writel(CR0_READ_SHADOW, cr0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1521 | vmcs_writel(GUEST_CR0, hw_cr0); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1522 | vcpu->arch.cr0 = cr0; |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1523 | |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 1524 | if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE)) |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1525 | vmx_fpu_activate(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1526 | } |
| 1527 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1528 | static u64 construct_eptp(unsigned long root_hpa) |
| 1529 | { |
| 1530 | u64 eptp; |
| 1531 | |
| 1532 | /* TODO write the value reading from MSR */ |
| 1533 | eptp = VMX_EPT_DEFAULT_MT | |
| 1534 | VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT; |
| 1535 | eptp |= (root_hpa & PAGE_MASK); |
| 1536 | |
| 1537 | return eptp; |
| 1538 | } |
| 1539 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1540 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 1541 | { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1542 | unsigned long guest_cr3; |
| 1543 | u64 eptp; |
| 1544 | |
| 1545 | guest_cr3 = cr3; |
| 1546 | if (vm_need_ept()) { |
| 1547 | eptp = construct_eptp(cr3); |
| 1548 | vmcs_write64(EPT_POINTER, eptp); |
| 1549 | ept_sync_context(eptp); |
| 1550 | ept_load_pdptrs(vcpu); |
| 1551 | guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 : |
| 1552 | VMX_EPT_IDENTITY_PAGETABLE_ADDR; |
| 1553 | } |
| 1554 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1555 | vmx_flush_tlb(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1556 | vmcs_writel(GUEST_CR3, guest_cr3); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1557 | if (vcpu->arch.cr0 & X86_CR0_PE) |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1558 | vmx_fpu_deactivate(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
| 1562 | { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1563 | unsigned long hw_cr4 = cr4 | (vcpu->arch.rmode.active ? |
| 1564 | KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON); |
| 1565 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1566 | vcpu->arch.cr4 = cr4; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1567 | if (vm_need_ept()) |
| 1568 | ept_update_paging_mode_cr4(&hw_cr4, vcpu); |
| 1569 | |
| 1570 | vmcs_writel(CR4_READ_SHADOW, cr4); |
| 1571 | vmcs_writel(GUEST_CR4, hw_cr4); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1572 | } |
| 1573 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1574 | static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 1575 | { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1576 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1577 | struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1578 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1579 | vcpu->arch.shadow_efer = efer; |
Joerg Roedel | 9f62e19 | 2008-01-31 14:57:39 +0100 | [diff] [blame] | 1580 | if (!msr) |
| 1581 | return; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1582 | if (efer & EFER_LMA) { |
| 1583 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 1584 | vmcs_read32(VM_ENTRY_CONTROLS) | |
Li, Xin B | 1e4e6e0 | 2007-08-01 21:49:10 +0300 | [diff] [blame] | 1585 | VM_ENTRY_IA32E_MODE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1586 | msr->data = efer; |
| 1587 | |
| 1588 | } else { |
| 1589 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 1590 | vmcs_read32(VM_ENTRY_CONTROLS) & |
Li, Xin B | 1e4e6e0 | 2007-08-01 21:49:10 +0300 | [diff] [blame] | 1591 | ~VM_ENTRY_IA32E_MODE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1592 | |
| 1593 | msr->data = efer & ~EFER_LME; |
| 1594 | } |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1595 | setup_msrs(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1596 | } |
| 1597 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1598 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 1599 | { |
| 1600 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 1601 | |
| 1602 | return vmcs_readl(sf->base); |
| 1603 | } |
| 1604 | |
| 1605 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 1606 | struct kvm_segment *var, int seg) |
| 1607 | { |
| 1608 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 1609 | u32 ar; |
| 1610 | |
| 1611 | var->base = vmcs_readl(sf->base); |
| 1612 | var->limit = vmcs_read32(sf->limit); |
| 1613 | var->selector = vmcs_read16(sf->selector); |
| 1614 | ar = vmcs_read32(sf->ar_bytes); |
| 1615 | if (ar & AR_UNUSABLE_MASK) |
| 1616 | ar = 0; |
| 1617 | var->type = ar & 15; |
| 1618 | var->s = (ar >> 4) & 1; |
| 1619 | var->dpl = (ar >> 5) & 3; |
| 1620 | var->present = (ar >> 7) & 1; |
| 1621 | var->avl = (ar >> 12) & 1; |
| 1622 | var->l = (ar >> 13) & 1; |
| 1623 | var->db = (ar >> 14) & 1; |
| 1624 | var->g = (ar >> 15) & 1; |
| 1625 | var->unusable = (ar >> 16) & 1; |
| 1626 | } |
| 1627 | |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 1628 | static int vmx_get_cpl(struct kvm_vcpu *vcpu) |
| 1629 | { |
| 1630 | struct kvm_segment kvm_seg; |
| 1631 | |
| 1632 | if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */ |
| 1633 | return 0; |
| 1634 | |
| 1635 | if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */ |
| 1636 | return 3; |
| 1637 | |
| 1638 | vmx_get_segment(vcpu, &kvm_seg, VCPU_SREG_CS); |
| 1639 | return kvm_seg.selector & 3; |
| 1640 | } |
| 1641 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 1642 | static u32 vmx_segment_access_rights(struct kvm_segment *var) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1643 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1644 | u32 ar; |
| 1645 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 1646 | if (var->unusable) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1647 | ar = 1 << 16; |
| 1648 | else { |
| 1649 | ar = var->type & 15; |
| 1650 | ar |= (var->s & 1) << 4; |
| 1651 | ar |= (var->dpl & 3) << 5; |
| 1652 | ar |= (var->present & 1) << 7; |
| 1653 | ar |= (var->avl & 1) << 12; |
| 1654 | ar |= (var->l & 1) << 13; |
| 1655 | ar |= (var->db & 1) << 14; |
| 1656 | ar |= (var->g & 1) << 15; |
| 1657 | } |
Uri Lublin | f7fbf1f | 2006-12-13 00:34:00 -0800 | [diff] [blame] | 1658 | if (ar == 0) /* a 0 value means unusable */ |
| 1659 | ar = AR_UNUSABLE_MASK; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 1660 | |
| 1661 | return ar; |
| 1662 | } |
| 1663 | |
| 1664 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 1665 | struct kvm_segment *var, int seg) |
| 1666 | { |
| 1667 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 1668 | u32 ar; |
| 1669 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1670 | if (vcpu->arch.rmode.active && seg == VCPU_SREG_TR) { |
| 1671 | vcpu->arch.rmode.tr.selector = var->selector; |
| 1672 | vcpu->arch.rmode.tr.base = var->base; |
| 1673 | vcpu->arch.rmode.tr.limit = var->limit; |
| 1674 | vcpu->arch.rmode.tr.ar = vmx_segment_access_rights(var); |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 1675 | return; |
| 1676 | } |
| 1677 | vmcs_writel(sf->base, var->base); |
| 1678 | vmcs_write32(sf->limit, var->limit); |
| 1679 | vmcs_write16(sf->selector, var->selector); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1680 | if (vcpu->arch.rmode.active && var->s) { |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 1681 | /* |
| 1682 | * Hack real-mode segments into vm86 compatibility. |
| 1683 | */ |
| 1684 | if (var->base == 0xffff0000 && var->selector == 0xf000) |
| 1685 | vmcs_writel(sf->base, 0xf0000); |
| 1686 | ar = 0xf3; |
| 1687 | } else |
| 1688 | ar = vmx_segment_access_rights(var); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1689 | vmcs_write32(sf->ar_bytes, ar); |
| 1690 | } |
| 1691 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1692 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 1693 | { |
| 1694 | u32 ar = vmcs_read32(GUEST_CS_AR_BYTES); |
| 1695 | |
| 1696 | *db = (ar >> 14) & 1; |
| 1697 | *l = (ar >> 13) & 1; |
| 1698 | } |
| 1699 | |
| 1700 | static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) |
| 1701 | { |
| 1702 | dt->limit = vmcs_read32(GUEST_IDTR_LIMIT); |
| 1703 | dt->base = vmcs_readl(GUEST_IDTR_BASE); |
| 1704 | } |
| 1705 | |
| 1706 | static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) |
| 1707 | { |
| 1708 | vmcs_write32(GUEST_IDTR_LIMIT, dt->limit); |
| 1709 | vmcs_writel(GUEST_IDTR_BASE, dt->base); |
| 1710 | } |
| 1711 | |
| 1712 | static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) |
| 1713 | { |
| 1714 | dt->limit = vmcs_read32(GUEST_GDTR_LIMIT); |
| 1715 | dt->base = vmcs_readl(GUEST_GDTR_BASE); |
| 1716 | } |
| 1717 | |
| 1718 | static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) |
| 1719 | { |
| 1720 | vmcs_write32(GUEST_GDTR_LIMIT, dt->limit); |
| 1721 | vmcs_writel(GUEST_GDTR_BASE, dt->base); |
| 1722 | } |
| 1723 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame^] | 1724 | static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 1725 | { |
| 1726 | struct kvm_segment var; |
| 1727 | u32 ar; |
| 1728 | |
| 1729 | vmx_get_segment(vcpu, &var, seg); |
| 1730 | ar = vmx_segment_access_rights(&var); |
| 1731 | |
| 1732 | if (var.base != (var.selector << 4)) |
| 1733 | return false; |
| 1734 | if (var.limit != 0xffff) |
| 1735 | return false; |
| 1736 | if (ar != 0xf3) |
| 1737 | return false; |
| 1738 | |
| 1739 | return true; |
| 1740 | } |
| 1741 | |
| 1742 | static bool code_segment_valid(struct kvm_vcpu *vcpu) |
| 1743 | { |
| 1744 | struct kvm_segment cs; |
| 1745 | unsigned int cs_rpl; |
| 1746 | |
| 1747 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 1748 | cs_rpl = cs.selector & SELECTOR_RPL_MASK; |
| 1749 | |
| 1750 | if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK)) |
| 1751 | return false; |
| 1752 | if (!cs.s) |
| 1753 | return false; |
| 1754 | if (!(~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK))) { |
| 1755 | if (cs.dpl > cs_rpl) |
| 1756 | return false; |
| 1757 | } else if (cs.type & AR_TYPE_CODE_MASK) { |
| 1758 | if (cs.dpl != cs_rpl) |
| 1759 | return false; |
| 1760 | } |
| 1761 | if (!cs.present) |
| 1762 | return false; |
| 1763 | |
| 1764 | /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */ |
| 1765 | return true; |
| 1766 | } |
| 1767 | |
| 1768 | static bool stack_segment_valid(struct kvm_vcpu *vcpu) |
| 1769 | { |
| 1770 | struct kvm_segment ss; |
| 1771 | unsigned int ss_rpl; |
| 1772 | |
| 1773 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 1774 | ss_rpl = ss.selector & SELECTOR_RPL_MASK; |
| 1775 | |
| 1776 | if ((ss.type != 3) || (ss.type != 7)) |
| 1777 | return false; |
| 1778 | if (!ss.s) |
| 1779 | return false; |
| 1780 | if (ss.dpl != ss_rpl) /* DPL != RPL */ |
| 1781 | return false; |
| 1782 | if (!ss.present) |
| 1783 | return false; |
| 1784 | |
| 1785 | return true; |
| 1786 | } |
| 1787 | |
| 1788 | static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 1789 | { |
| 1790 | struct kvm_segment var; |
| 1791 | unsigned int rpl; |
| 1792 | |
| 1793 | vmx_get_segment(vcpu, &var, seg); |
| 1794 | rpl = var.selector & SELECTOR_RPL_MASK; |
| 1795 | |
| 1796 | if (!var.s) |
| 1797 | return false; |
| 1798 | if (!var.present) |
| 1799 | return false; |
| 1800 | if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) { |
| 1801 | if (var.dpl < rpl) /* DPL < RPL */ |
| 1802 | return false; |
| 1803 | } |
| 1804 | |
| 1805 | /* TODO: Add other members to kvm_segment_field to allow checking for other access |
| 1806 | * rights flags |
| 1807 | */ |
| 1808 | return true; |
| 1809 | } |
| 1810 | |
| 1811 | static bool tr_valid(struct kvm_vcpu *vcpu) |
| 1812 | { |
| 1813 | struct kvm_segment tr; |
| 1814 | |
| 1815 | vmx_get_segment(vcpu, &tr, VCPU_SREG_TR); |
| 1816 | |
| 1817 | if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */ |
| 1818 | return false; |
| 1819 | if ((tr.type != 3) || (tr.type != 11)) /* TODO: Check if guest is in IA32e mode */ |
| 1820 | return false; |
| 1821 | if (!tr.present) |
| 1822 | return false; |
| 1823 | |
| 1824 | return true; |
| 1825 | } |
| 1826 | |
| 1827 | static bool ldtr_valid(struct kvm_vcpu *vcpu) |
| 1828 | { |
| 1829 | struct kvm_segment ldtr; |
| 1830 | |
| 1831 | vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); |
| 1832 | |
| 1833 | if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */ |
| 1834 | return false; |
| 1835 | if (ldtr.type != 2) |
| 1836 | return false; |
| 1837 | if (!ldtr.present) |
| 1838 | return false; |
| 1839 | |
| 1840 | return true; |
| 1841 | } |
| 1842 | |
| 1843 | static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) |
| 1844 | { |
| 1845 | struct kvm_segment cs, ss; |
| 1846 | |
| 1847 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 1848 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 1849 | |
| 1850 | return ((cs.selector & SELECTOR_RPL_MASK) == |
| 1851 | (ss.selector & SELECTOR_RPL_MASK)); |
| 1852 | } |
| 1853 | |
| 1854 | /* |
| 1855 | * Check if guest state is valid. Returns true if valid, false if |
| 1856 | * not. |
| 1857 | * We assume that registers are always usable |
| 1858 | */ |
| 1859 | static bool guest_state_valid(struct kvm_vcpu *vcpu) |
| 1860 | { |
| 1861 | /* real mode guest state checks */ |
| 1862 | if (!(vcpu->arch.cr0 & X86_CR0_PE)) { |
| 1863 | if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) |
| 1864 | return false; |
| 1865 | if (!rmode_segment_valid(vcpu, VCPU_SREG_SS)) |
| 1866 | return false; |
| 1867 | if (!rmode_segment_valid(vcpu, VCPU_SREG_DS)) |
| 1868 | return false; |
| 1869 | if (!rmode_segment_valid(vcpu, VCPU_SREG_ES)) |
| 1870 | return false; |
| 1871 | if (!rmode_segment_valid(vcpu, VCPU_SREG_FS)) |
| 1872 | return false; |
| 1873 | if (!rmode_segment_valid(vcpu, VCPU_SREG_GS)) |
| 1874 | return false; |
| 1875 | } else { |
| 1876 | /* protected mode guest state checks */ |
| 1877 | if (!cs_ss_rpl_check(vcpu)) |
| 1878 | return false; |
| 1879 | if (!code_segment_valid(vcpu)) |
| 1880 | return false; |
| 1881 | if (!stack_segment_valid(vcpu)) |
| 1882 | return false; |
| 1883 | if (!data_segment_valid(vcpu, VCPU_SREG_DS)) |
| 1884 | return false; |
| 1885 | if (!data_segment_valid(vcpu, VCPU_SREG_ES)) |
| 1886 | return false; |
| 1887 | if (!data_segment_valid(vcpu, VCPU_SREG_FS)) |
| 1888 | return false; |
| 1889 | if (!data_segment_valid(vcpu, VCPU_SREG_GS)) |
| 1890 | return false; |
| 1891 | if (!tr_valid(vcpu)) |
| 1892 | return false; |
| 1893 | if (!ldtr_valid(vcpu)) |
| 1894 | return false; |
| 1895 | } |
| 1896 | /* TODO: |
| 1897 | * - Add checks on RIP |
| 1898 | * - Add checks on RFLAGS |
| 1899 | */ |
| 1900 | |
| 1901 | return true; |
| 1902 | } |
| 1903 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1904 | static int init_rmode_tss(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1905 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1906 | gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1907 | u16 data = 0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1908 | int ret = 0; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1909 | int r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1910 | |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1911 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 1912 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1913 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1914 | data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; |
Sheng Yang | 464d17c | 2008-08-13 14:10:33 +0800 | [diff] [blame] | 1915 | r = kvm_write_guest_page(kvm, fn++, &data, |
| 1916 | TSS_IOPB_BASE_OFFSET, sizeof(u16)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1917 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1918 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1919 | r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE); |
| 1920 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1921 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1922 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 1923 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1924 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1925 | data = ~0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1926 | r = kvm_write_guest_page(kvm, fn, &data, |
| 1927 | RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, |
| 1928 | sizeof(u8)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1929 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1930 | goto out; |
| 1931 | |
| 1932 | ret = 1; |
| 1933 | out: |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1934 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1935 | } |
| 1936 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 1937 | static int init_rmode_identity_map(struct kvm *kvm) |
| 1938 | { |
| 1939 | int i, r, ret; |
| 1940 | pfn_t identity_map_pfn; |
| 1941 | u32 tmp; |
| 1942 | |
| 1943 | if (!vm_need_ept()) |
| 1944 | return 1; |
| 1945 | if (unlikely(!kvm->arch.ept_identity_pagetable)) { |
| 1946 | printk(KERN_ERR "EPT: identity-mapping pagetable " |
| 1947 | "haven't been allocated!\n"); |
| 1948 | return 0; |
| 1949 | } |
| 1950 | if (likely(kvm->arch.ept_identity_pagetable_done)) |
| 1951 | return 1; |
| 1952 | ret = 0; |
| 1953 | identity_map_pfn = VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT; |
| 1954 | r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE); |
| 1955 | if (r < 0) |
| 1956 | goto out; |
| 1957 | /* Set up identity-mapping pagetable for EPT in real mode */ |
| 1958 | for (i = 0; i < PT32_ENT_PER_PAGE; i++) { |
| 1959 | tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | |
| 1960 | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); |
| 1961 | r = kvm_write_guest_page(kvm, identity_map_pfn, |
| 1962 | &tmp, i * sizeof(tmp), sizeof(tmp)); |
| 1963 | if (r < 0) |
| 1964 | goto out; |
| 1965 | } |
| 1966 | kvm->arch.ept_identity_pagetable_done = true; |
| 1967 | ret = 1; |
| 1968 | out: |
| 1969 | return ret; |
| 1970 | } |
| 1971 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1972 | static void seg_setup(int seg) |
| 1973 | { |
| 1974 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 1975 | |
| 1976 | vmcs_write16(sf->selector, 0); |
| 1977 | vmcs_writel(sf->base, 0); |
| 1978 | vmcs_write32(sf->limit, 0xffff); |
| 1979 | vmcs_write32(sf->ar_bytes, 0x93); |
| 1980 | } |
| 1981 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1982 | static int alloc_apic_access_page(struct kvm *kvm) |
| 1983 | { |
| 1984 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 1985 | int r = 0; |
| 1986 | |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 1987 | down_write(&kvm->slots_lock); |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 1988 | if (kvm->arch.apic_access_page) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1989 | goto out; |
| 1990 | kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT; |
| 1991 | kvm_userspace_mem.flags = 0; |
| 1992 | kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL; |
| 1993 | kvm_userspace_mem.memory_size = PAGE_SIZE; |
| 1994 | r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0); |
| 1995 | if (r) |
| 1996 | goto out; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 1997 | |
| 1998 | down_read(¤t->mm->mmap_sem); |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 1999 | kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 2000 | up_read(¤t->mm->mmap_sem); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2001 | out: |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 2002 | up_write(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2003 | return r; |
| 2004 | } |
| 2005 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 2006 | static int alloc_identity_pagetable(struct kvm *kvm) |
| 2007 | { |
| 2008 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 2009 | int r = 0; |
| 2010 | |
| 2011 | down_write(&kvm->slots_lock); |
| 2012 | if (kvm->arch.ept_identity_pagetable) |
| 2013 | goto out; |
| 2014 | kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT; |
| 2015 | kvm_userspace_mem.flags = 0; |
| 2016 | kvm_userspace_mem.guest_phys_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR; |
| 2017 | kvm_userspace_mem.memory_size = PAGE_SIZE; |
| 2018 | r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0); |
| 2019 | if (r) |
| 2020 | goto out; |
| 2021 | |
| 2022 | down_read(¤t->mm->mmap_sem); |
| 2023 | kvm->arch.ept_identity_pagetable = gfn_to_page(kvm, |
| 2024 | VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT); |
| 2025 | up_read(¤t->mm->mmap_sem); |
| 2026 | out: |
| 2027 | up_write(&kvm->slots_lock); |
| 2028 | return r; |
| 2029 | } |
| 2030 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2031 | static void allocate_vpid(struct vcpu_vmx *vmx) |
| 2032 | { |
| 2033 | int vpid; |
| 2034 | |
| 2035 | vmx->vpid = 0; |
| 2036 | if (!enable_vpid || !cpu_has_vmx_vpid()) |
| 2037 | return; |
| 2038 | spin_lock(&vmx_vpid_lock); |
| 2039 | vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 2040 | if (vpid < VMX_NR_VPIDS) { |
| 2041 | vmx->vpid = vpid; |
| 2042 | __set_bit(vpid, vmx_vpid_bitmap); |
| 2043 | } |
| 2044 | spin_unlock(&vmx_vpid_lock); |
| 2045 | } |
| 2046 | |
Harvey Harrison | 8b2cf73 | 2008-04-27 12:14:13 -0700 | [diff] [blame] | 2047 | static void vmx_disable_intercept_for_msr(struct page *msr_bitmap, u32 msr) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 2048 | { |
| 2049 | void *va; |
| 2050 | |
| 2051 | if (!cpu_has_vmx_msr_bitmap()) |
| 2052 | return; |
| 2053 | |
| 2054 | /* |
| 2055 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 2056 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 2057 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 2058 | */ |
| 2059 | va = kmap(msr_bitmap); |
| 2060 | if (msr <= 0x1fff) { |
| 2061 | __clear_bit(msr, va + 0x000); /* read-low */ |
| 2062 | __clear_bit(msr, va + 0x800); /* write-low */ |
| 2063 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 2064 | msr &= 0x1fff; |
| 2065 | __clear_bit(msr, va + 0x400); /* read-high */ |
| 2066 | __clear_bit(msr, va + 0xc00); /* write-high */ |
| 2067 | } |
| 2068 | kunmap(msr_bitmap); |
| 2069 | } |
| 2070 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2071 | /* |
| 2072 | * Sets up the vmcs for emulated real mode. |
| 2073 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2074 | static int vmx_vcpu_setup(struct vcpu_vmx *vmx) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2075 | { |
| 2076 | u32 host_sysenter_cs; |
| 2077 | u32 junk; |
| 2078 | unsigned long a; |
| 2079 | struct descriptor_table dt; |
| 2080 | int i; |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 2081 | unsigned long kvm_vmx_return; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 2082 | u32 exec_control; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2083 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2084 | /* I/O */ |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 2085 | vmcs_write64(IO_BITMAP_A, page_to_phys(vmx_io_bitmap_a)); |
| 2086 | vmcs_write64(IO_BITMAP_B, page_to_phys(vmx_io_bitmap_b)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2087 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 2088 | if (cpu_has_vmx_msr_bitmap()) |
| 2089 | vmcs_write64(MSR_BITMAP, page_to_phys(vmx_msr_bitmap)); |
| 2090 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2091 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ |
| 2092 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2093 | /* Control */ |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2094 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, |
| 2095 | vmcs_config.pin_based_exec_ctrl); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 2096 | |
| 2097 | exec_control = vmcs_config.cpu_based_exec_ctrl; |
| 2098 | if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) { |
| 2099 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 2100 | #ifdef CONFIG_X86_64 |
| 2101 | exec_control |= CPU_BASED_CR8_STORE_EXITING | |
| 2102 | CPU_BASED_CR8_LOAD_EXITING; |
| 2103 | #endif |
| 2104 | } |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2105 | if (!vm_need_ept()) |
| 2106 | exec_control |= CPU_BASED_CR3_STORE_EXITING | |
| 2107 | CPU_BASED_CR3_LOAD_EXITING; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 2108 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2109 | |
Sheng Yang | 83ff3b9 | 2007-11-21 14:33:25 +0800 | [diff] [blame] | 2110 | if (cpu_has_secondary_exec_ctrls()) { |
| 2111 | exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; |
| 2112 | if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm)) |
| 2113 | exec_control &= |
| 2114 | ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2115 | if (vmx->vpid == 0) |
| 2116 | exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2117 | if (!vm_need_ept()) |
| 2118 | exec_control &= ~SECONDARY_EXEC_ENABLE_EPT; |
Sheng Yang | 83ff3b9 | 2007-11-21 14:33:25 +0800 | [diff] [blame] | 2119 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
| 2120 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2121 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 2122 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf); |
| 2123 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2124 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ |
| 2125 | |
| 2126 | vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */ |
| 2127 | vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */ |
| 2128 | vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */ |
| 2129 | |
| 2130 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ |
| 2131 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 2132 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2133 | vmcs_write16(HOST_FS_SELECTOR, kvm_read_fs()); /* 22.2.4 */ |
| 2134 | vmcs_write16(HOST_GS_SELECTOR, kvm_read_gs()); /* 22.2.4 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2135 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2136 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2137 | rdmsrl(MSR_FS_BASE, a); |
| 2138 | vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */ |
| 2139 | rdmsrl(MSR_GS_BASE, a); |
| 2140 | vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */ |
| 2141 | #else |
| 2142 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ |
| 2143 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ |
| 2144 | #endif |
| 2145 | |
| 2146 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ |
| 2147 | |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2148 | kvm_get_idt(&dt); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2149 | vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */ |
| 2150 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2151 | asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return)); |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 2152 | vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */ |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2153 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 2154 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
| 2155 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2156 | |
| 2157 | rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk); |
| 2158 | vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs); |
| 2159 | rdmsrl(MSR_IA32_SYSENTER_ESP, a); |
| 2160 | vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */ |
| 2161 | rdmsrl(MSR_IA32_SYSENTER_EIP, a); |
| 2162 | vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */ |
| 2163 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2164 | for (i = 0; i < NR_VMX_MSR; ++i) { |
| 2165 | u32 index = vmx_msr_index[i]; |
| 2166 | u32 data_low, data_high; |
| 2167 | u64 data; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2168 | int j = vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2169 | |
| 2170 | if (rdmsr_safe(index, &data_low, &data_high) < 0) |
| 2171 | continue; |
Avi Kivity | 432bd6c | 2007-01-31 23:48:13 -0800 | [diff] [blame] | 2172 | if (wrmsr_safe(index, data_low, data_high) < 0) |
| 2173 | continue; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2174 | data = data_low | ((u64)data_high << 32); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2175 | vmx->host_msrs[j].index = index; |
| 2176 | vmx->host_msrs[j].reserved = 0; |
| 2177 | vmx->host_msrs[j].data = data; |
| 2178 | vmx->guest_msrs[j] = vmx->host_msrs[j]; |
| 2179 | ++vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2180 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2181 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2182 | vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2183 | |
| 2184 | /* 22.2.1, 20.8.1 */ |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2185 | vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl); |
| 2186 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2187 | vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL); |
| 2188 | vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK); |
| 2189 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2190 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2191 | return 0; |
| 2192 | } |
| 2193 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 2194 | static int init_rmode(struct kvm *kvm) |
| 2195 | { |
| 2196 | if (!init_rmode_tss(kvm)) |
| 2197 | return 0; |
| 2198 | if (!init_rmode_identity_map(kvm)) |
| 2199 | return 0; |
| 2200 | return 1; |
| 2201 | } |
| 2202 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2203 | static int vmx_vcpu_reset(struct kvm_vcpu *vcpu) |
| 2204 | { |
| 2205 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2206 | u64 msr; |
| 2207 | int ret; |
| 2208 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2209 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)); |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 2210 | down_read(&vcpu->kvm->slots_lock); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 2211 | if (!init_rmode(vmx->vcpu.kvm)) { |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2212 | ret = -ENOMEM; |
| 2213 | goto out; |
| 2214 | } |
| 2215 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2216 | vmx->vcpu.arch.rmode.active = 0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2217 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2218 | vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 2219 | kvm_set_cr8(&vmx->vcpu, 0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2220 | msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE; |
| 2221 | if (vmx->vcpu.vcpu_id == 0) |
| 2222 | msr |= MSR_IA32_APICBASE_BSP; |
| 2223 | kvm_set_apic_base(&vmx->vcpu, msr); |
| 2224 | |
| 2225 | fx_init(&vmx->vcpu); |
| 2226 | |
| 2227 | /* |
| 2228 | * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode |
| 2229 | * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh. |
| 2230 | */ |
| 2231 | if (vmx->vcpu.vcpu_id == 0) { |
| 2232 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); |
| 2233 | vmcs_writel(GUEST_CS_BASE, 0x000f0000); |
| 2234 | } else { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2235 | vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8); |
| 2236 | vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2237 | } |
| 2238 | vmcs_write32(GUEST_CS_LIMIT, 0xffff); |
| 2239 | vmcs_write32(GUEST_CS_AR_BYTES, 0x9b); |
| 2240 | |
| 2241 | seg_setup(VCPU_SREG_DS); |
| 2242 | seg_setup(VCPU_SREG_ES); |
| 2243 | seg_setup(VCPU_SREG_FS); |
| 2244 | seg_setup(VCPU_SREG_GS); |
| 2245 | seg_setup(VCPU_SREG_SS); |
| 2246 | |
| 2247 | vmcs_write16(GUEST_TR_SELECTOR, 0); |
| 2248 | vmcs_writel(GUEST_TR_BASE, 0); |
| 2249 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); |
| 2250 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 2251 | |
| 2252 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); |
| 2253 | vmcs_writel(GUEST_LDTR_BASE, 0); |
| 2254 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); |
| 2255 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); |
| 2256 | |
| 2257 | vmcs_write32(GUEST_SYSENTER_CS, 0); |
| 2258 | vmcs_writel(GUEST_SYSENTER_ESP, 0); |
| 2259 | vmcs_writel(GUEST_SYSENTER_EIP, 0); |
| 2260 | |
| 2261 | vmcs_writel(GUEST_RFLAGS, 0x02); |
| 2262 | if (vmx->vcpu.vcpu_id == 0) |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2263 | kvm_rip_write(vcpu, 0xfff0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2264 | else |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2265 | kvm_rip_write(vcpu, 0); |
| 2266 | kvm_register_write(vcpu, VCPU_REGS_RSP, 0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2267 | |
| 2268 | /* todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0 */ |
| 2269 | vmcs_writel(GUEST_DR7, 0x400); |
| 2270 | |
| 2271 | vmcs_writel(GUEST_GDTR_BASE, 0); |
| 2272 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); |
| 2273 | |
| 2274 | vmcs_writel(GUEST_IDTR_BASE, 0); |
| 2275 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); |
| 2276 | |
| 2277 | vmcs_write32(GUEST_ACTIVITY_STATE, 0); |
| 2278 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); |
| 2279 | vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0); |
| 2280 | |
| 2281 | guest_write_tsc(0); |
| 2282 | |
| 2283 | /* Special registers */ |
| 2284 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 2285 | |
| 2286 | setup_msrs(vmx); |
| 2287 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2288 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ |
| 2289 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2290 | if (cpu_has_vmx_tpr_shadow()) { |
| 2291 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); |
| 2292 | if (vm_need_tpr_shadow(vmx->vcpu.kvm)) |
| 2293 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2294 | page_to_phys(vmx->vcpu.arch.apic->regs_page)); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2295 | vmcs_write32(TPR_THRESHOLD, 0); |
| 2296 | } |
| 2297 | |
| 2298 | if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm)) |
| 2299 | vmcs_write64(APIC_ACCESS_ADDR, |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 2300 | page_to_phys(vmx->vcpu.kvm->arch.apic_access_page)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2301 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2302 | if (vmx->vpid != 0) |
| 2303 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 2304 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2305 | vmx->vcpu.arch.cr0 = 0x60000010; |
| 2306 | vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2307 | vmx_set_cr4(&vmx->vcpu, 0); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2308 | vmx_set_efer(&vmx->vcpu, 0); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2309 | vmx_fpu_activate(&vmx->vcpu); |
| 2310 | update_exception_bitmap(&vmx->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2311 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2312 | vpid_sync_vcpu_all(vmx); |
| 2313 | |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 2314 | ret = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2315 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2316 | out: |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 2317 | up_read(&vcpu->kvm->slots_lock); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2318 | return ret; |
| 2319 | } |
| 2320 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2321 | static void vmx_inject_irq(struct kvm_vcpu *vcpu, int irq) |
| 2322 | { |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 2323 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2324 | |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2325 | KVMTRACE_1D(INJ_VIRQ, vcpu, (u32)irq, handler); |
| 2326 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2327 | if (vcpu->arch.rmode.active) { |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 2328 | vmx->rmode.irq.pending = true; |
| 2329 | vmx->rmode.irq.vector = irq; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2330 | vmx->rmode.irq.rip = kvm_rip_read(vcpu); |
Avi Kivity | 9c5623e | 2007-11-08 18:19:20 +0200 | [diff] [blame] | 2331 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 2332 | irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK); |
| 2333 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2334 | kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2335 | return; |
| 2336 | } |
| 2337 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 2338 | irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); |
| 2339 | } |
| 2340 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 2341 | static void vmx_inject_nmi(struct kvm_vcpu *vcpu) |
| 2342 | { |
| 2343 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 2344 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 2345 | } |
| 2346 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2347 | static void kvm_do_inject_irq(struct kvm_vcpu *vcpu) |
| 2348 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2349 | int word_index = __ffs(vcpu->arch.irq_summary); |
| 2350 | int bit_index = __ffs(vcpu->arch.irq_pending[word_index]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2351 | int irq = word_index * BITS_PER_LONG + bit_index; |
| 2352 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2353 | clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]); |
| 2354 | if (!vcpu->arch.irq_pending[word_index]) |
| 2355 | clear_bit(word_index, &vcpu->arch.irq_summary); |
Avi Kivity | ecfc79c | 2008-08-14 11:13:16 +0300 | [diff] [blame] | 2356 | kvm_queue_interrupt(vcpu, irq); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2357 | } |
| 2358 | |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2359 | |
| 2360 | static void do_interrupt_requests(struct kvm_vcpu *vcpu, |
| 2361 | struct kvm_run *kvm_run) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2362 | { |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2363 | u32 cpu_based_vm_exec_control; |
| 2364 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2365 | vcpu->arch.interrupt_window_open = |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2366 | ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && |
| 2367 | (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0); |
| 2368 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2369 | if (vcpu->arch.interrupt_window_open && |
Avi Kivity | ecfc79c | 2008-08-14 11:13:16 +0300 | [diff] [blame] | 2370 | vcpu->arch.irq_summary && !vcpu->arch.interrupt.pending) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2371 | kvm_do_inject_irq(vcpu); |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2372 | |
Avi Kivity | ecfc79c | 2008-08-14 11:13:16 +0300 | [diff] [blame] | 2373 | if (vcpu->arch.interrupt_window_open && vcpu->arch.interrupt.pending) |
| 2374 | vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr); |
| 2375 | |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2376 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2377 | if (!vcpu->arch.interrupt_window_open && |
| 2378 | (vcpu->arch.irq_summary || kvm_run->request_interrupt_window)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2379 | /* |
| 2380 | * Interrupts blocked. Wait for unblock. |
| 2381 | */ |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2382 | cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING; |
| 2383 | else |
| 2384 | cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 2385 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2386 | } |
| 2387 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 2388 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 2389 | { |
| 2390 | int ret; |
| 2391 | struct kvm_userspace_memory_region tss_mem = { |
| 2392 | .slot = 8, |
| 2393 | .guest_phys_addr = addr, |
| 2394 | .memory_size = PAGE_SIZE * 3, |
| 2395 | .flags = 0, |
| 2396 | }; |
| 2397 | |
| 2398 | ret = kvm_set_memory_region(kvm, &tss_mem, 0); |
| 2399 | if (ret) |
| 2400 | return ret; |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 2401 | kvm->arch.tss_addr = addr; |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 2402 | return 0; |
| 2403 | } |
| 2404 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2405 | static void kvm_guest_debug_pre(struct kvm_vcpu *vcpu) |
| 2406 | { |
| 2407 | struct kvm_guest_debug *dbg = &vcpu->guest_debug; |
| 2408 | |
| 2409 | set_debugreg(dbg->bp[0], 0); |
| 2410 | set_debugreg(dbg->bp[1], 1); |
| 2411 | set_debugreg(dbg->bp[2], 2); |
| 2412 | set_debugreg(dbg->bp[3], 3); |
| 2413 | |
| 2414 | if (dbg->singlestep) { |
| 2415 | unsigned long flags; |
| 2416 | |
| 2417 | flags = vmcs_readl(GUEST_RFLAGS); |
| 2418 | flags |= X86_EFLAGS_TF | X86_EFLAGS_RF; |
| 2419 | vmcs_writel(GUEST_RFLAGS, flags); |
| 2420 | } |
| 2421 | } |
| 2422 | |
| 2423 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, |
| 2424 | int vec, u32 err_code) |
| 2425 | { |
Nitin A Kamble | b3f3770 | 2007-05-17 15:50:34 +0300 | [diff] [blame] | 2426 | /* |
| 2427 | * Instruction with address size override prefix opcode 0x67 |
| 2428 | * Cause the #SS fault with 0 error code in VM86 mode. |
| 2429 | */ |
| 2430 | if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 2431 | if (emulate_instruction(vcpu, NULL, 0, 0, 0) == EMULATE_DONE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2432 | return 1; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2433 | /* |
| 2434 | * Forward all other exceptions that are valid in real mode. |
| 2435 | * FIXME: Breaks guest debugging in real mode, needs to be fixed with |
| 2436 | * the required debugging infrastructure rework. |
| 2437 | */ |
| 2438 | switch (vec) { |
| 2439 | case DE_VECTOR: |
| 2440 | case DB_VECTOR: |
| 2441 | case BP_VECTOR: |
| 2442 | case OF_VECTOR: |
| 2443 | case BR_VECTOR: |
| 2444 | case UD_VECTOR: |
| 2445 | case DF_VECTOR: |
| 2446 | case SS_VECTOR: |
| 2447 | case GP_VECTOR: |
| 2448 | case MF_VECTOR: |
| 2449 | kvm_queue_exception(vcpu, vec); |
| 2450 | return 1; |
| 2451 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2452 | return 0; |
| 2453 | } |
| 2454 | |
| 2455 | static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2456 | { |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 2457 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2458 | u32 intr_info, error_code; |
| 2459 | unsigned long cr2, rip; |
| 2460 | u32 vect_info; |
| 2461 | enum emulation_result er; |
| 2462 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 2463 | vect_info = vmx->idt_vectoring_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2464 | intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 2465 | |
| 2466 | if ((vect_info & VECTORING_INFO_VALID_MASK) && |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2467 | !is_page_fault(intr_info)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2468 | printk(KERN_ERR "%s: unexpected, vectoring info 0x%x " |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 2469 | "intr info 0x%x\n", __func__, vect_info, intr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2470 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2471 | if (!irqchip_in_kernel(vcpu->kvm) && is_external_interrupt(vect_info)) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2472 | int irq = vect_info & VECTORING_INFO_VECTOR_MASK; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2473 | set_bit(irq, vcpu->arch.irq_pending); |
| 2474 | set_bit(irq / BITS_PER_LONG, &vcpu->arch.irq_summary); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2475 | } |
| 2476 | |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 2477 | if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) /* nmi */ |
| 2478 | return 1; /* already handled by vmx_vcpu_run() */ |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 2479 | |
| 2480 | if (is_no_device(intr_info)) { |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2481 | vmx_fpu_activate(vcpu); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 2482 | return 1; |
| 2483 | } |
| 2484 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 2485 | if (is_invalid_opcode(intr_info)) { |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 2486 | er = emulate_instruction(vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 2487 | if (er != EMULATE_DONE) |
Avi Kivity | 7ee5d940 | 2007-11-25 15:22:50 +0200 | [diff] [blame] | 2488 | kvm_queue_exception(vcpu, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 2489 | return 1; |
| 2490 | } |
| 2491 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2492 | error_code = 0; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2493 | rip = kvm_rip_read(vcpu); |
Ryan Harper | 2e11384 | 2008-02-11 10:26:38 -0600 | [diff] [blame] | 2494 | if (intr_info & INTR_INFO_DELIVER_CODE_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2495 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
| 2496 | if (is_page_fault(intr_info)) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2497 | /* EPT won't cause page fault directly */ |
| 2498 | if (vm_need_ept()) |
| 2499 | BUG(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2500 | cr2 = vmcs_readl(EXIT_QUALIFICATION); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2501 | KVMTRACE_3D(PAGE_FAULT, vcpu, error_code, (u32)cr2, |
| 2502 | (u32)((u64)cr2 >> 32), handler); |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 2503 | if (vcpu->arch.interrupt.pending || vcpu->arch.exception.pending) |
Avi Kivity | 577bdc4 | 2008-07-19 08:57:05 +0300 | [diff] [blame] | 2504 | kvm_mmu_unprotect_page_virt(vcpu, cr2); |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 2505 | return kvm_mmu_page_fault(vcpu, cr2, error_code); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2506 | } |
| 2507 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2508 | if (vcpu->arch.rmode.active && |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2509 | handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK, |
Avi Kivity | 72d6e5a | 2007-06-05 16:15:51 +0300 | [diff] [blame] | 2510 | error_code)) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2511 | if (vcpu->arch.halt_request) { |
| 2512 | vcpu->arch.halt_request = 0; |
Avi Kivity | 72d6e5a | 2007-06-05 16:15:51 +0300 | [diff] [blame] | 2513 | return kvm_emulate_halt(vcpu); |
| 2514 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2515 | return 1; |
Avi Kivity | 72d6e5a | 2007-06-05 16:15:51 +0300 | [diff] [blame] | 2516 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2517 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2518 | if ((intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK)) == |
| 2519 | (INTR_TYPE_EXCEPTION | 1)) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2520 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
| 2521 | return 0; |
| 2522 | } |
| 2523 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; |
| 2524 | kvm_run->ex.exception = intr_info & INTR_INFO_VECTOR_MASK; |
| 2525 | kvm_run->ex.error_code = error_code; |
| 2526 | return 0; |
| 2527 | } |
| 2528 | |
| 2529 | static int handle_external_interrupt(struct kvm_vcpu *vcpu, |
| 2530 | struct kvm_run *kvm_run) |
| 2531 | { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2532 | ++vcpu->stat.irq_exits; |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2533 | KVMTRACE_1D(INTR, vcpu, vmcs_read32(VM_EXIT_INTR_INFO), handler); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2534 | return 1; |
| 2535 | } |
| 2536 | |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 2537 | static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2538 | { |
| 2539 | kvm_run->exit_reason = KVM_EXIT_SHUTDOWN; |
| 2540 | return 0; |
| 2541 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2542 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2543 | static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2544 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 2545 | unsigned long exit_qualification; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 2546 | int size, down, in, string, rep; |
| 2547 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2548 | |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2549 | ++vcpu->stat.io_exits; |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 2550 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 2551 | string = (exit_qualification & 16) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 2552 | |
| 2553 | if (string) { |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 2554 | if (emulate_instruction(vcpu, |
| 2555 | kvm_run, 0, 0, 0) == EMULATE_DO_MMIO) |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 2556 | return 0; |
| 2557 | return 1; |
| 2558 | } |
| 2559 | |
| 2560 | size = (exit_qualification & 7) + 1; |
| 2561 | in = (exit_qualification & 8) != 0; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 2562 | down = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_DF) != 0; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 2563 | rep = (exit_qualification & 32) != 0; |
| 2564 | port = exit_qualification >> 16; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 2565 | |
Laurent Vivier | 3090dd7 | 2007-08-05 10:43:32 +0300 | [diff] [blame] | 2566 | return kvm_emulate_pio(vcpu, kvm_run, in, size, port); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2567 | } |
| 2568 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2569 | static void |
| 2570 | vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 2571 | { |
| 2572 | /* |
| 2573 | * Patch in the VMCALL instruction: |
| 2574 | */ |
| 2575 | hypercall[0] = 0x0f; |
| 2576 | hypercall[1] = 0x01; |
| 2577 | hypercall[2] = 0xc1; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2578 | } |
| 2579 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2580 | static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2581 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 2582 | unsigned long exit_qualification; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2583 | int cr; |
| 2584 | int reg; |
| 2585 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 2586 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2587 | cr = exit_qualification & 15; |
| 2588 | reg = (exit_qualification >> 8) & 15; |
| 2589 | switch ((exit_qualification >> 4) & 3) { |
| 2590 | case 0: /* mov to cr */ |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2591 | KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr, |
| 2592 | (u32)kvm_register_read(vcpu, reg), |
| 2593 | (u32)((u64)kvm_register_read(vcpu, reg) >> 32), |
| 2594 | handler); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2595 | switch (cr) { |
| 2596 | case 0: |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2597 | kvm_set_cr0(vcpu, kvm_register_read(vcpu, reg)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2598 | skip_emulated_instruction(vcpu); |
| 2599 | return 1; |
| 2600 | case 3: |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2601 | kvm_set_cr3(vcpu, kvm_register_read(vcpu, reg)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2602 | skip_emulated_instruction(vcpu); |
| 2603 | return 1; |
| 2604 | case 4: |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2605 | kvm_set_cr4(vcpu, kvm_register_read(vcpu, reg)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2606 | skip_emulated_instruction(vcpu); |
| 2607 | return 1; |
| 2608 | case 8: |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2609 | kvm_set_cr8(vcpu, kvm_register_read(vcpu, reg)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2610 | skip_emulated_instruction(vcpu); |
Avi Kivity | e531406 | 2007-12-06 16:32:45 +0200 | [diff] [blame] | 2611 | if (irqchip_in_kernel(vcpu->kvm)) |
| 2612 | return 1; |
Yang, Sheng | 253abde | 2007-08-16 13:01:00 +0300 | [diff] [blame] | 2613 | kvm_run->exit_reason = KVM_EXIT_SET_TPR; |
| 2614 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2615 | }; |
| 2616 | break; |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 2617 | case 2: /* clts */ |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2618 | vmx_fpu_deactivate(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2619 | vcpu->arch.cr0 &= ~X86_CR0_TS; |
| 2620 | vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2621 | vmx_fpu_activate(vcpu); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2622 | KVMTRACE_0D(CLTS, vcpu, handler); |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 2623 | skip_emulated_instruction(vcpu); |
| 2624 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2625 | case 1: /*mov from cr*/ |
| 2626 | switch (cr) { |
| 2627 | case 3: |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2628 | kvm_register_write(vcpu, reg, vcpu->arch.cr3); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2629 | KVMTRACE_3D(CR_READ, vcpu, (u32)cr, |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2630 | (u32)kvm_register_read(vcpu, reg), |
| 2631 | (u32)((u64)kvm_register_read(vcpu, reg) >> 32), |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2632 | handler); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2633 | skip_emulated_instruction(vcpu); |
| 2634 | return 1; |
| 2635 | case 8: |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2636 | kvm_register_write(vcpu, reg, kvm_get_cr8(vcpu)); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2637 | KVMTRACE_2D(CR_READ, vcpu, (u32)cr, |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2638 | (u32)kvm_register_read(vcpu, reg), handler); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2639 | skip_emulated_instruction(vcpu); |
| 2640 | return 1; |
| 2641 | } |
| 2642 | break; |
| 2643 | case 3: /* lmsw */ |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 2644 | kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2645 | |
| 2646 | skip_emulated_instruction(vcpu); |
| 2647 | return 1; |
| 2648 | default: |
| 2649 | break; |
| 2650 | } |
| 2651 | kvm_run->exit_reason = 0; |
Rusty Russell | f024247 | 2007-08-01 10:48:02 +1000 | [diff] [blame] | 2652 | pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n", |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2653 | (int)(exit_qualification >> 4) & 3, cr); |
| 2654 | return 0; |
| 2655 | } |
| 2656 | |
| 2657 | static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2658 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 2659 | unsigned long exit_qualification; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2660 | unsigned long val; |
| 2661 | int dr, reg; |
| 2662 | |
| 2663 | /* |
| 2664 | * FIXME: this code assumes the host is debugging the guest. |
| 2665 | * need to deal with guest debugging itself too. |
| 2666 | */ |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 2667 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2668 | dr = exit_qualification & 7; |
| 2669 | reg = (exit_qualification >> 8) & 15; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2670 | if (exit_qualification & 16) { |
| 2671 | /* mov from dr */ |
| 2672 | switch (dr) { |
| 2673 | case 6: |
| 2674 | val = 0xffff0ff0; |
| 2675 | break; |
| 2676 | case 7: |
| 2677 | val = 0x400; |
| 2678 | break; |
| 2679 | default: |
| 2680 | val = 0; |
| 2681 | } |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2682 | kvm_register_write(vcpu, reg, val); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2683 | KVMTRACE_2D(DR_READ, vcpu, (u32)dr, (u32)val, handler); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2684 | } else { |
| 2685 | /* mov to dr */ |
| 2686 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2687 | skip_emulated_instruction(vcpu); |
| 2688 | return 1; |
| 2689 | } |
| 2690 | |
| 2691 | static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2692 | { |
Avi Kivity | 06465c5 | 2007-02-28 20:46:53 +0200 | [diff] [blame] | 2693 | kvm_emulate_cpuid(vcpu); |
| 2694 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2695 | } |
| 2696 | |
| 2697 | static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2698 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2699 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2700 | u64 data; |
| 2701 | |
| 2702 | if (vmx_get_msr(vcpu, ecx, &data)) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 2703 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2704 | return 1; |
| 2705 | } |
| 2706 | |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2707 | KVMTRACE_3D(MSR_READ, vcpu, ecx, (u32)data, (u32)(data >> 32), |
| 2708 | handler); |
| 2709 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2710 | /* FIXME: handling of bits 32:63 of rax, rdx */ |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2711 | vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u; |
| 2712 | vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2713 | skip_emulated_instruction(vcpu); |
| 2714 | return 1; |
| 2715 | } |
| 2716 | |
| 2717 | static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2718 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2719 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 2720 | u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u) |
| 2721 | | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2722 | |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2723 | KVMTRACE_3D(MSR_WRITE, vcpu, ecx, (u32)data, (u32)(data >> 32), |
| 2724 | handler); |
| 2725 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2726 | if (vmx_set_msr(vcpu, ecx, data) != 0) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 2727 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2728 | return 1; |
| 2729 | } |
| 2730 | |
| 2731 | skip_emulated_instruction(vcpu); |
| 2732 | return 1; |
| 2733 | } |
| 2734 | |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 2735 | static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu, |
| 2736 | struct kvm_run *kvm_run) |
| 2737 | { |
| 2738 | return 1; |
| 2739 | } |
| 2740 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2741 | static int handle_interrupt_window(struct kvm_vcpu *vcpu, |
| 2742 | struct kvm_run *kvm_run) |
| 2743 | { |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2744 | u32 cpu_based_vm_exec_control; |
| 2745 | |
| 2746 | /* clear pending irq */ |
| 2747 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 2748 | cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 2749 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2750 | |
| 2751 | KVMTRACE_0D(PEND_INTR, vcpu, handler); |
| 2752 | |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2753 | /* |
| 2754 | * If the user space waits to inject interrupts, exit as soon as |
| 2755 | * possible |
| 2756 | */ |
| 2757 | if (kvm_run->request_interrupt_window && |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2758 | !vcpu->arch.irq_summary) { |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2759 | kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2760 | ++vcpu->stat.irq_window_exits; |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2761 | return 0; |
| 2762 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2763 | return 1; |
| 2764 | } |
| 2765 | |
| 2766 | static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2767 | { |
| 2768 | skip_emulated_instruction(vcpu); |
Avi Kivity | d3bef15 | 2007-06-05 15:53:05 +0300 | [diff] [blame] | 2769 | return kvm_emulate_halt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2770 | } |
| 2771 | |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2772 | static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2773 | { |
Dor Laor | 510043d | 2007-02-19 18:25:43 +0200 | [diff] [blame] | 2774 | skip_emulated_instruction(vcpu); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 2775 | kvm_emulate_hypercall(vcpu); |
| 2776 | return 1; |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2777 | } |
| 2778 | |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 2779 | static int handle_wbinvd(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2780 | { |
| 2781 | skip_emulated_instruction(vcpu); |
| 2782 | /* TODO: Add support for VT-d/pass-through device */ |
| 2783 | return 1; |
| 2784 | } |
| 2785 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2786 | static int handle_apic_access(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2787 | { |
| 2788 | u64 exit_qualification; |
| 2789 | enum emulation_result er; |
| 2790 | unsigned long offset; |
| 2791 | |
| 2792 | exit_qualification = vmcs_read64(EXIT_QUALIFICATION); |
| 2793 | offset = exit_qualification & 0xffful; |
| 2794 | |
| 2795 | er = emulate_instruction(vcpu, kvm_run, 0, 0, 0); |
| 2796 | |
| 2797 | if (er != EMULATE_DONE) { |
| 2798 | printk(KERN_ERR |
| 2799 | "Fail to handle apic access vmexit! Offset is 0x%lx\n", |
| 2800 | offset); |
| 2801 | return -ENOTSUPP; |
| 2802 | } |
| 2803 | return 1; |
| 2804 | } |
| 2805 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 2806 | static int handle_task_switch(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2807 | { |
| 2808 | unsigned long exit_qualification; |
| 2809 | u16 tss_selector; |
| 2810 | int reason; |
| 2811 | |
| 2812 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 2813 | |
| 2814 | reason = (u32)exit_qualification >> 30; |
| 2815 | tss_selector = exit_qualification; |
| 2816 | |
| 2817 | return kvm_task_switch(vcpu, tss_selector, reason); |
| 2818 | } |
| 2819 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2820 | static int handle_ept_violation(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2821 | { |
| 2822 | u64 exit_qualification; |
| 2823 | enum emulation_result er; |
| 2824 | gpa_t gpa; |
| 2825 | unsigned long hva; |
| 2826 | int gla_validity; |
| 2827 | int r; |
| 2828 | |
| 2829 | exit_qualification = vmcs_read64(EXIT_QUALIFICATION); |
| 2830 | |
| 2831 | if (exit_qualification & (1 << 6)) { |
| 2832 | printk(KERN_ERR "EPT: GPA exceeds GAW!\n"); |
| 2833 | return -ENOTSUPP; |
| 2834 | } |
| 2835 | |
| 2836 | gla_validity = (exit_qualification >> 7) & 0x3; |
| 2837 | if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) { |
| 2838 | printk(KERN_ERR "EPT: Handling EPT violation failed!\n"); |
| 2839 | printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n", |
| 2840 | (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS), |
| 2841 | (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS)); |
| 2842 | printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n", |
| 2843 | (long unsigned int)exit_qualification); |
| 2844 | kvm_run->exit_reason = KVM_EXIT_UNKNOWN; |
| 2845 | kvm_run->hw.hardware_exit_reason = 0; |
| 2846 | return -ENOTSUPP; |
| 2847 | } |
| 2848 | |
| 2849 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
| 2850 | hva = gfn_to_hva(vcpu->kvm, gpa >> PAGE_SHIFT); |
| 2851 | if (!kvm_is_error_hva(hva)) { |
| 2852 | r = kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0); |
| 2853 | if (r < 0) { |
| 2854 | printk(KERN_ERR "EPT: Not enough memory!\n"); |
| 2855 | return -ENOMEM; |
| 2856 | } |
| 2857 | return 1; |
| 2858 | } else { |
| 2859 | /* must be MMIO */ |
| 2860 | er = emulate_instruction(vcpu, kvm_run, 0, 0, 0); |
| 2861 | |
| 2862 | if (er == EMULATE_FAIL) { |
| 2863 | printk(KERN_ERR |
| 2864 | "EPT: Fail to handle EPT violation vmexit!er is %d\n", |
| 2865 | er); |
| 2866 | printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n", |
| 2867 | (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS), |
| 2868 | (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS)); |
| 2869 | printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n", |
| 2870 | (long unsigned int)exit_qualification); |
| 2871 | return -ENOTSUPP; |
| 2872 | } else if (er == EMULATE_DO_MMIO) |
| 2873 | return 0; |
| 2874 | } |
| 2875 | return 1; |
| 2876 | } |
| 2877 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 2878 | static int handle_nmi_window(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2879 | { |
| 2880 | u32 cpu_based_vm_exec_control; |
| 2881 | |
| 2882 | /* clear pending NMI */ |
| 2883 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 2884 | cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 2885 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 2886 | ++vcpu->stat.nmi_window_exits; |
| 2887 | |
| 2888 | return 1; |
| 2889 | } |
| 2890 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2891 | /* |
| 2892 | * The exit handlers return 1 if the exit was handled fully and guest execution |
| 2893 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs |
| 2894 | * to be done to userspace and return 0. |
| 2895 | */ |
| 2896 | static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu, |
| 2897 | struct kvm_run *kvm_run) = { |
| 2898 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, |
| 2899 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 2900 | [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 2901 | [EXIT_REASON_NMI_WINDOW] = handle_nmi_window, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2902 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2903 | [EXIT_REASON_CR_ACCESS] = handle_cr, |
| 2904 | [EXIT_REASON_DR_ACCESS] = handle_dr, |
| 2905 | [EXIT_REASON_CPUID] = handle_cpuid, |
| 2906 | [EXIT_REASON_MSR_READ] = handle_rdmsr, |
| 2907 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, |
| 2908 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, |
| 2909 | [EXIT_REASON_HLT] = handle_halt, |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2910 | [EXIT_REASON_VMCALL] = handle_vmcall, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2911 | [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, |
| 2912 | [EXIT_REASON_APIC_ACCESS] = handle_apic_access, |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 2913 | [EXIT_REASON_WBINVD] = handle_wbinvd, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 2914 | [EXIT_REASON_TASK_SWITCH] = handle_task_switch, |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2915 | [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2916 | }; |
| 2917 | |
| 2918 | static const int kvm_vmx_max_exit_handlers = |
Robert P. J. Day | 50a3485 | 2007-06-03 13:35:29 -0400 | [diff] [blame] | 2919 | ARRAY_SIZE(kvm_vmx_exit_handlers); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2920 | |
| 2921 | /* |
| 2922 | * The guest has exited. See if we can fix it or if we need userspace |
| 2923 | * assistance. |
| 2924 | */ |
| 2925 | static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu) |
| 2926 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2927 | u32 exit_reason = vmcs_read32(VM_EXIT_REASON); |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 2928 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 2929 | u32 vectoring_info = vmx->idt_vectoring_info; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 2930 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2931 | KVMTRACE_3D(VMEXIT, vcpu, exit_reason, (u32)kvm_rip_read(vcpu), |
| 2932 | (u32)((u64)kvm_rip_read(vcpu) >> 32), entryexit); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2933 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2934 | /* Access CR3 don't cause VMExit in paging mode, so we need |
| 2935 | * to sync with guest real CR3. */ |
| 2936 | if (vm_need_ept() && is_paging(vcpu)) { |
| 2937 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
| 2938 | ept_load_pdptrs(vcpu); |
| 2939 | } |
| 2940 | |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 2941 | if (unlikely(vmx->fail)) { |
| 2942 | kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 2943 | kvm_run->fail_entry.hardware_entry_failure_reason |
| 2944 | = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 2945 | return 0; |
| 2946 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2947 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2948 | if ((vectoring_info & VECTORING_INFO_VALID_MASK) && |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2949 | (exit_reason != EXIT_REASON_EXCEPTION_NMI && |
| 2950 | exit_reason != EXIT_REASON_EPT_VIOLATION)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2951 | printk(KERN_WARNING "%s: unexpected, valid vectoring info and " |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 2952 | "exit reason is 0x%x\n", __func__, exit_reason); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2953 | if (exit_reason < kvm_vmx_max_exit_handlers |
| 2954 | && kvm_vmx_exit_handlers[exit_reason]) |
| 2955 | return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run); |
| 2956 | else { |
| 2957 | kvm_run->exit_reason = KVM_EXIT_UNKNOWN; |
| 2958 | kvm_run->hw.hardware_exit_reason = exit_reason; |
| 2959 | } |
| 2960 | return 0; |
| 2961 | } |
| 2962 | |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 2963 | static void update_tpr_threshold(struct kvm_vcpu *vcpu) |
| 2964 | { |
| 2965 | int max_irr, tpr; |
| 2966 | |
| 2967 | if (!vm_need_tpr_shadow(vcpu->kvm)) |
| 2968 | return; |
| 2969 | |
| 2970 | if (!kvm_lapic_enabled(vcpu) || |
| 2971 | ((max_irr = kvm_lapic_find_highest_irr(vcpu)) == -1)) { |
| 2972 | vmcs_write32(TPR_THRESHOLD, 0); |
| 2973 | return; |
| 2974 | } |
| 2975 | |
| 2976 | tpr = (kvm_lapic_get_cr8(vcpu) & 0x0f) << 4; |
| 2977 | vmcs_write32(TPR_THRESHOLD, (max_irr > tpr) ? tpr >> 4 : max_irr >> 4); |
| 2978 | } |
| 2979 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2980 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
| 2981 | { |
| 2982 | u32 cpu_based_vm_exec_control; |
| 2983 | |
| 2984 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 2985 | cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING; |
| 2986 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 2987 | } |
| 2988 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 2989 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
| 2990 | { |
| 2991 | u32 cpu_based_vm_exec_control; |
| 2992 | |
| 2993 | if (!cpu_has_virtual_nmis()) |
| 2994 | return; |
| 2995 | |
| 2996 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 2997 | cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING; |
| 2998 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 2999 | } |
| 3000 | |
| 3001 | static int vmx_nmi_enabled(struct kvm_vcpu *vcpu) |
| 3002 | { |
| 3003 | u32 guest_intr = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 3004 | return !(guest_intr & (GUEST_INTR_STATE_NMI | |
| 3005 | GUEST_INTR_STATE_MOV_SS | |
| 3006 | GUEST_INTR_STATE_STI)); |
| 3007 | } |
| 3008 | |
| 3009 | static int vmx_irq_enabled(struct kvm_vcpu *vcpu) |
| 3010 | { |
| 3011 | u32 guest_intr = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 3012 | return (!(guest_intr & (GUEST_INTR_STATE_MOV_SS | |
| 3013 | GUEST_INTR_STATE_STI)) && |
| 3014 | (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF)); |
| 3015 | } |
| 3016 | |
| 3017 | static void enable_intr_window(struct kvm_vcpu *vcpu) |
| 3018 | { |
| 3019 | if (vcpu->arch.nmi_pending) |
| 3020 | enable_nmi_window(vcpu); |
| 3021 | else if (kvm_cpu_has_interrupt(vcpu)) |
| 3022 | enable_irq_window(vcpu); |
| 3023 | } |
| 3024 | |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 3025 | static void vmx_complete_interrupts(struct vcpu_vmx *vmx) |
| 3026 | { |
| 3027 | u32 exit_intr_info; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 3028 | u32 idt_vectoring_info; |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 3029 | bool unblock_nmi; |
| 3030 | u8 vector; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 3031 | int type; |
| 3032 | bool idtv_info_valid; |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 3033 | u32 error; |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 3034 | |
| 3035 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 3036 | if (cpu_has_virtual_nmis()) { |
| 3037 | unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; |
| 3038 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 3039 | /* |
| 3040 | * SDM 3: 25.7.1.2 |
| 3041 | * Re-set bit "block by NMI" before VM entry if vmexit caused by |
| 3042 | * a guest IRET fault. |
| 3043 | */ |
| 3044 | if (unblock_nmi && vector != DF_VECTOR) |
| 3045 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 3046 | GUEST_INTR_STATE_NMI); |
| 3047 | } |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 3048 | |
| 3049 | idt_vectoring_info = vmx->idt_vectoring_info; |
| 3050 | idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
| 3051 | vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK; |
| 3052 | type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK; |
| 3053 | if (vmx->vcpu.arch.nmi_injected) { |
| 3054 | /* |
| 3055 | * SDM 3: 25.7.1.2 |
| 3056 | * Clear bit "block by NMI" before VM entry if a NMI delivery |
| 3057 | * faulted. |
| 3058 | */ |
| 3059 | if (idtv_info_valid && type == INTR_TYPE_NMI_INTR) |
| 3060 | vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 3061 | GUEST_INTR_STATE_NMI); |
| 3062 | else |
| 3063 | vmx->vcpu.arch.nmi_injected = false; |
| 3064 | } |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 3065 | kvm_clear_exception_queue(&vmx->vcpu); |
| 3066 | if (idtv_info_valid && type == INTR_TYPE_EXCEPTION) { |
| 3067 | if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { |
| 3068 | error = vmcs_read32(IDT_VECTORING_ERROR_CODE); |
| 3069 | kvm_queue_exception_e(&vmx->vcpu, vector, error); |
| 3070 | } else |
| 3071 | kvm_queue_exception(&vmx->vcpu, vector); |
| 3072 | vmx->idt_vectoring_info = 0; |
| 3073 | } |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 3074 | kvm_clear_interrupt_queue(&vmx->vcpu); |
| 3075 | if (idtv_info_valid && type == INTR_TYPE_EXT_INTR) { |
| 3076 | kvm_queue_interrupt(&vmx->vcpu, vector); |
| 3077 | vmx->idt_vectoring_info = 0; |
| 3078 | } |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 3079 | } |
| 3080 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 3081 | static void vmx_intr_assist(struct kvm_vcpu *vcpu) |
| 3082 | { |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 3083 | u32 intr_info_field; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 3084 | |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 3085 | update_tpr_threshold(vcpu); |
| 3086 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 3087 | intr_info_field = vmcs_read32(VM_ENTRY_INTR_INFO_FIELD); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 3088 | if (cpu_has_virtual_nmis()) { |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 3089 | if (vcpu->arch.nmi_pending && !vcpu->arch.nmi_injected) { |
| 3090 | if (vmx_nmi_enabled(vcpu)) { |
| 3091 | vcpu->arch.nmi_pending = false; |
| 3092 | vcpu->arch.nmi_injected = true; |
| 3093 | } else { |
| 3094 | enable_intr_window(vcpu); |
| 3095 | return; |
| 3096 | } |
| 3097 | } |
| 3098 | if (vcpu->arch.nmi_injected) { |
| 3099 | vmx_inject_nmi(vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 3100 | enable_intr_window(vcpu); |
| 3101 | return; |
| 3102 | } |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 3103 | } |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 3104 | if (!vcpu->arch.interrupt.pending && kvm_cpu_has_interrupt(vcpu)) { |
| 3105 | if (vmx_irq_enabled(vcpu)) |
| 3106 | kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu)); |
| 3107 | else |
| 3108 | enable_irq_window(vcpu); |
| 3109 | } |
| 3110 | if (vcpu->arch.interrupt.pending) { |
| 3111 | vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr); |
| 3112 | kvm_timer_intr_post(vcpu, vcpu->arch.interrupt.nr); |
| 3113 | } |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 3114 | } |
| 3115 | |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 3116 | /* |
| 3117 | * Failure to inject an interrupt should give us the information |
| 3118 | * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs |
| 3119 | * when fetching the interrupt redirection bitmap in the real-mode |
| 3120 | * tss, this doesn't happen. So we do it ourselves. |
| 3121 | */ |
| 3122 | static void fixup_rmode_irq(struct vcpu_vmx *vmx) |
| 3123 | { |
| 3124 | vmx->rmode.irq.pending = 0; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3125 | if (kvm_rip_read(&vmx->vcpu) + 1 != vmx->rmode.irq.rip) |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 3126 | return; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3127 | kvm_rip_write(&vmx->vcpu, vmx->rmode.irq.rip); |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 3128 | if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) { |
| 3129 | vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK; |
| 3130 | vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR; |
| 3131 | return; |
| 3132 | } |
| 3133 | vmx->idt_vectoring_info = |
| 3134 | VECTORING_INFO_VALID_MASK |
| 3135 | | INTR_TYPE_EXT_INTR |
| 3136 | | vmx->rmode.irq.vector; |
| 3137 | } |
| 3138 | |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 3139 | #ifdef CONFIG_X86_64 |
| 3140 | #define R "r" |
| 3141 | #define Q "q" |
| 3142 | #else |
| 3143 | #define R "e" |
| 3144 | #define Q "l" |
| 3145 | #endif |
| 3146 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 3147 | static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3148 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3149 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 3150 | u32 intr_info; |
Avi Kivity | e6adf28 | 2007-04-30 16:07:54 +0300 | [diff] [blame] | 3151 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3152 | if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 3153 | vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); |
| 3154 | if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 3155 | vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); |
| 3156 | |
Avi Kivity | e6adf28 | 2007-04-30 16:07:54 +0300 | [diff] [blame] | 3157 | /* |
| 3158 | * Loading guest fpu may have cleared host cr0.ts |
| 3159 | */ |
| 3160 | vmcs_writel(HOST_CR0, read_cr0()); |
| 3161 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 3162 | asm( |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3163 | /* Store host registers */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 3164 | "push %%"R"dx; push %%"R"bp;" |
| 3165 | "push %%"R"cx \n\t" |
Avi Kivity | 313dbd49 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 3166 | "cmp %%"R"sp, %c[host_rsp](%0) \n\t" |
| 3167 | "je 1f \n\t" |
| 3168 | "mov %%"R"sp, %c[host_rsp](%0) \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 3169 | __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t" |
Avi Kivity | 313dbd49 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 3170 | "1: \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3171 | /* Check if vmlaunch of vmresume is needed */ |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 3172 | "cmpl $0, %c[launched](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3173 | /* Load guest registers. Don't clobber flags. */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 3174 | "mov %c[cr2](%0), %%"R"ax \n\t" |
| 3175 | "mov %%"R"ax, %%cr2 \n\t" |
| 3176 | "mov %c[rax](%0), %%"R"ax \n\t" |
| 3177 | "mov %c[rbx](%0), %%"R"bx \n\t" |
| 3178 | "mov %c[rdx](%0), %%"R"dx \n\t" |
| 3179 | "mov %c[rsi](%0), %%"R"si \n\t" |
| 3180 | "mov %c[rdi](%0), %%"R"di \n\t" |
| 3181 | "mov %c[rbp](%0), %%"R"bp \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3182 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 3183 | "mov %c[r8](%0), %%r8 \n\t" |
| 3184 | "mov %c[r9](%0), %%r9 \n\t" |
| 3185 | "mov %c[r10](%0), %%r10 \n\t" |
| 3186 | "mov %c[r11](%0), %%r11 \n\t" |
| 3187 | "mov %c[r12](%0), %%r12 \n\t" |
| 3188 | "mov %c[r13](%0), %%r13 \n\t" |
| 3189 | "mov %c[r14](%0), %%r14 \n\t" |
| 3190 | "mov %c[r15](%0), %%r15 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3191 | #endif |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 3192 | "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */ |
| 3193 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3194 | /* Enter guest mode */ |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 3195 | "jne .Llaunched \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 3196 | __ex(ASM_VMX_VMLAUNCH) "\n\t" |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 3197 | "jmp .Lkvm_vmx_return \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 3198 | ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t" |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 3199 | ".Lkvm_vmx_return: " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3200 | /* Save guest registers, load host registers, keep flags */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 3201 | "xchg %0, (%%"R"sp) \n\t" |
| 3202 | "mov %%"R"ax, %c[rax](%0) \n\t" |
| 3203 | "mov %%"R"bx, %c[rbx](%0) \n\t" |
| 3204 | "push"Q" (%%"R"sp); pop"Q" %c[rcx](%0) \n\t" |
| 3205 | "mov %%"R"dx, %c[rdx](%0) \n\t" |
| 3206 | "mov %%"R"si, %c[rsi](%0) \n\t" |
| 3207 | "mov %%"R"di, %c[rdi](%0) \n\t" |
| 3208 | "mov %%"R"bp, %c[rbp](%0) \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3209 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 3210 | "mov %%r8, %c[r8](%0) \n\t" |
| 3211 | "mov %%r9, %c[r9](%0) \n\t" |
| 3212 | "mov %%r10, %c[r10](%0) \n\t" |
| 3213 | "mov %%r11, %c[r11](%0) \n\t" |
| 3214 | "mov %%r12, %c[r12](%0) \n\t" |
| 3215 | "mov %%r13, %c[r13](%0) \n\t" |
| 3216 | "mov %%r14, %c[r14](%0) \n\t" |
| 3217 | "mov %%r15, %c[r15](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3218 | #endif |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 3219 | "mov %%cr2, %%"R"ax \n\t" |
| 3220 | "mov %%"R"ax, %c[cr2](%0) \n\t" |
| 3221 | |
| 3222 | "pop %%"R"bp; pop %%"R"bp; pop %%"R"dx \n\t" |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 3223 | "setbe %c[fail](%0) \n\t" |
| 3224 | : : "c"(vmx), "d"((unsigned long)HOST_RSP), |
| 3225 | [launched]"i"(offsetof(struct vcpu_vmx, launched)), |
| 3226 | [fail]"i"(offsetof(struct vcpu_vmx, fail)), |
Avi Kivity | 313dbd49 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 3227 | [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3228 | [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])), |
| 3229 | [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 3230 | [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 3231 | [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 3232 | [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 3233 | [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 3234 | [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])), |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3235 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3236 | [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])), |
| 3237 | [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])), |
| 3238 | [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])), |
| 3239 | [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])), |
| 3240 | [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])), |
| 3241 | [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])), |
| 3242 | [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), |
| 3243 | [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3244 | #endif |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3245 | [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)) |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 3246 | : "cc", "memory" |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 3247 | , R"bx", R"di", R"si" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 3248 | #ifdef CONFIG_X86_64 |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 3249 | , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
| 3250 | #endif |
| 3251 | ); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3252 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3253 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)); |
| 3254 | vcpu->arch.regs_dirty = 0; |
| 3255 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 3256 | vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 3257 | if (vmx->rmode.irq.pending) |
| 3258 | fixup_rmode_irq(vmx); |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 3259 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3260 | vcpu->arch.interrupt_window_open = |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 3261 | (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 3262 | (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)) == 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3263 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 3264 | asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS)); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 3265 | vmx->launched = 1; |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 3266 | |
| 3267 | intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 3268 | |
| 3269 | /* We need to handle NMIs before interrupts are enabled */ |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 3270 | if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200 && |
| 3271 | (intr_info & INTR_INFO_VALID_MASK)) { |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 3272 | KVMTRACE_0D(NMI, vcpu, handler); |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 3273 | asm("int $2"); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 3274 | } |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 3275 | |
| 3276 | vmx_complete_interrupts(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3277 | } |
| 3278 | |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 3279 | #undef R |
| 3280 | #undef Q |
| 3281 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3282 | static void vmx_free_vmcs(struct kvm_vcpu *vcpu) |
| 3283 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3284 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3285 | |
| 3286 | if (vmx->vmcs) { |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 3287 | vcpu_clear(vmx); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3288 | free_vmcs(vmx->vmcs); |
| 3289 | vmx->vmcs = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3290 | } |
| 3291 | } |
| 3292 | |
| 3293 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 3294 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3295 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3296 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3297 | spin_lock(&vmx_vpid_lock); |
| 3298 | if (vmx->vpid != 0) |
| 3299 | __clear_bit(vmx->vpid, vmx_vpid_bitmap); |
| 3300 | spin_unlock(&vmx_vpid_lock); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3301 | vmx_free_vmcs(vcpu); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3302 | kfree(vmx->host_msrs); |
| 3303 | kfree(vmx->guest_msrs); |
| 3304 | kvm_vcpu_uninit(vcpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 3305 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3306 | } |
| 3307 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3308 | static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3309 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3310 | int err; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 3311 | struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 3312 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3313 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3314 | if (!vmx) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3315 | return ERR_PTR(-ENOMEM); |
| 3316 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3317 | allocate_vpid(vmx); |
| 3318 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3319 | err = kvm_vcpu_init(&vmx->vcpu, kvm, id); |
| 3320 | if (err) |
| 3321 | goto free_vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 3322 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3323 | vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3324 | if (!vmx->guest_msrs) { |
| 3325 | err = -ENOMEM; |
| 3326 | goto uninit_vcpu; |
| 3327 | } |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 3328 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3329 | vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 3330 | if (!vmx->host_msrs) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3331 | goto free_guest_msrs; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 3332 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3333 | vmx->vmcs = alloc_vmcs(); |
| 3334 | if (!vmx->vmcs) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3335 | goto free_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3336 | |
| 3337 | vmcs_clear(vmx->vmcs); |
| 3338 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 3339 | cpu = get_cpu(); |
| 3340 | vmx_vcpu_load(&vmx->vcpu, cpu); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3341 | err = vmx_vcpu_setup(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3342 | vmx_vcpu_put(&vmx->vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 3343 | put_cpu(); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3344 | if (err) |
| 3345 | goto free_vmcs; |
Marcelo Tosatti | 5e4a0b3 | 2008-02-14 21:21:43 -0200 | [diff] [blame] | 3346 | if (vm_need_virtualize_apic_accesses(kvm)) |
| 3347 | if (alloc_apic_access_page(kvm) != 0) |
| 3348 | goto free_vmcs; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 3349 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3350 | if (vm_need_ept()) |
| 3351 | if (alloc_identity_pagetable(kvm) != 0) |
| 3352 | goto free_vmcs; |
| 3353 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3354 | return &vmx->vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 3355 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3356 | free_vmcs: |
| 3357 | free_vmcs(vmx->vmcs); |
| 3358 | free_msrs: |
| 3359 | kfree(vmx->host_msrs); |
| 3360 | free_guest_msrs: |
| 3361 | kfree(vmx->guest_msrs); |
| 3362 | uninit_vcpu: |
| 3363 | kvm_vcpu_uninit(&vmx->vcpu); |
| 3364 | free_vcpu: |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 3365 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3366 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3367 | } |
| 3368 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3369 | static void __init vmx_check_processor_compat(void *rtn) |
| 3370 | { |
| 3371 | struct vmcs_config vmcs_conf; |
| 3372 | |
| 3373 | *(int *)rtn = 0; |
| 3374 | if (setup_vmcs_config(&vmcs_conf) < 0) |
| 3375 | *(int *)rtn = -EIO; |
| 3376 | if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { |
| 3377 | printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", |
| 3378 | smp_processor_id()); |
| 3379 | *(int *)rtn = -EIO; |
| 3380 | } |
| 3381 | } |
| 3382 | |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 3383 | static int get_ept_level(void) |
| 3384 | { |
| 3385 | return VMX_EPT_DEFAULT_GAW + 1; |
| 3386 | } |
| 3387 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 3388 | static struct kvm_x86_ops vmx_x86_ops = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3389 | .cpu_has_kvm_support = cpu_has_kvm_support, |
| 3390 | .disabled_by_bios = vmx_disabled_by_bios, |
| 3391 | .hardware_setup = hardware_setup, |
| 3392 | .hardware_unsetup = hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3393 | .check_processor_compatibility = vmx_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3394 | .hardware_enable = hardware_enable, |
| 3395 | .hardware_disable = hardware_disable, |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 3396 | .cpu_has_accelerated_tpr = cpu_has_vmx_virtualize_apic_accesses, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3397 | |
| 3398 | .vcpu_create = vmx_create_vcpu, |
| 3399 | .vcpu_free = vmx_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 3400 | .vcpu_reset = vmx_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3401 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 3402 | .prepare_guest_switch = vmx_save_host_state, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3403 | .vcpu_load = vmx_vcpu_load, |
| 3404 | .vcpu_put = vmx_vcpu_put, |
| 3405 | |
| 3406 | .set_guest_debug = set_guest_debug, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 3407 | .guest_debug_pre = kvm_guest_debug_pre, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3408 | .get_msr = vmx_get_msr, |
| 3409 | .set_msr = vmx_set_msr, |
| 3410 | .get_segment_base = vmx_get_segment_base, |
| 3411 | .get_segment = vmx_get_segment, |
| 3412 | .set_segment = vmx_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 3413 | .get_cpl = vmx_get_cpl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3414 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 3415 | .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3416 | .set_cr0 = vmx_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3417 | .set_cr3 = vmx_set_cr3, |
| 3418 | .set_cr4 = vmx_set_cr4, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3419 | .set_efer = vmx_set_efer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3420 | .get_idt = vmx_get_idt, |
| 3421 | .set_idt = vmx_set_idt, |
| 3422 | .get_gdt = vmx_get_gdt, |
| 3423 | .set_gdt = vmx_set_gdt, |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3424 | .cache_reg = vmx_cache_reg, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3425 | .get_rflags = vmx_get_rflags, |
| 3426 | .set_rflags = vmx_set_rflags, |
| 3427 | |
| 3428 | .tlb_flush = vmx_flush_tlb, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3429 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3430 | .run = vmx_vcpu_run, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 3431 | .handle_exit = kvm_handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3432 | .skip_emulated_instruction = skip_emulated_instruction, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 3433 | .patch_hypercall = vmx_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 3434 | .get_irq = vmx_get_irq, |
| 3435 | .set_irq = vmx_inject_irq, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 3436 | .queue_exception = vmx_queue_exception, |
| 3437 | .exception_injected = vmx_exception_injected, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 3438 | .inject_pending_irq = vmx_intr_assist, |
| 3439 | .inject_pending_vectors = do_interrupt_requests, |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 3440 | |
| 3441 | .set_tss_addr = vmx_set_tss_addr, |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 3442 | .get_tdp_level = get_ept_level, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3443 | }; |
| 3444 | |
| 3445 | static int __init vmx_init(void) |
| 3446 | { |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3447 | void *va; |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 3448 | int r; |
| 3449 | |
| 3450 | vmx_io_bitmap_a = alloc_page(GFP_KERNEL | __GFP_HIGHMEM); |
| 3451 | if (!vmx_io_bitmap_a) |
| 3452 | return -ENOMEM; |
| 3453 | |
| 3454 | vmx_io_bitmap_b = alloc_page(GFP_KERNEL | __GFP_HIGHMEM); |
| 3455 | if (!vmx_io_bitmap_b) { |
| 3456 | r = -ENOMEM; |
| 3457 | goto out; |
| 3458 | } |
| 3459 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3460 | vmx_msr_bitmap = alloc_page(GFP_KERNEL | __GFP_HIGHMEM); |
| 3461 | if (!vmx_msr_bitmap) { |
| 3462 | r = -ENOMEM; |
| 3463 | goto out1; |
| 3464 | } |
| 3465 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 3466 | /* |
| 3467 | * Allow direct access to the PC debug port (it is often used for I/O |
| 3468 | * delays, but the vmexits simply slow things down). |
| 3469 | */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3470 | va = kmap(vmx_io_bitmap_a); |
| 3471 | memset(va, 0xff, PAGE_SIZE); |
| 3472 | clear_bit(0x80, va); |
Avi Kivity | cd0536d | 2007-05-08 11:34:07 +0300 | [diff] [blame] | 3473 | kunmap(vmx_io_bitmap_a); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 3474 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3475 | va = kmap(vmx_io_bitmap_b); |
| 3476 | memset(va, 0xff, PAGE_SIZE); |
Avi Kivity | cd0536d | 2007-05-08 11:34:07 +0300 | [diff] [blame] | 3477 | kunmap(vmx_io_bitmap_b); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 3478 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3479 | va = kmap(vmx_msr_bitmap); |
| 3480 | memset(va, 0xff, PAGE_SIZE); |
| 3481 | kunmap(vmx_msr_bitmap); |
| 3482 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3483 | set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */ |
| 3484 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 3485 | r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 3486 | if (r) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3487 | goto out2; |
| 3488 | |
| 3489 | vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_FS_BASE); |
| 3490 | vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_GS_BASE); |
| 3491 | vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_CS); |
| 3492 | vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_ESP); |
| 3493 | vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_EIP); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 3494 | |
Sheng Yang | 5fdbcb9 | 2008-07-16 09:25:40 +0800 | [diff] [blame] | 3495 | if (vm_need_ept()) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3496 | bypass_guest_pf = 0; |
Sheng Yang | 5fdbcb9 | 2008-07-16 09:25:40 +0800 | [diff] [blame] | 3497 | kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK | |
| 3498 | VMX_EPT_WRITABLE_MASK | |
| 3499 | VMX_EPT_DEFAULT_MT << VMX_EPT_MT_EPTE_SHIFT); |
Sheng Yang | 534e38b | 2008-09-08 15:12:30 +0800 | [diff] [blame] | 3500 | kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull, |
Sheng Yang | 5fdbcb9 | 2008-07-16 09:25:40 +0800 | [diff] [blame] | 3501 | VMX_EPT_EXECUTABLE_MASK); |
| 3502 | kvm_enable_tdp(); |
| 3503 | } else |
| 3504 | kvm_disable_tdp(); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3505 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 3506 | if (bypass_guest_pf) |
| 3507 | kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull); |
| 3508 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3509 | ept_sync_global(); |
| 3510 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 3511 | return 0; |
| 3512 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3513 | out2: |
| 3514 | __free_page(vmx_msr_bitmap); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 3515 | out1: |
| 3516 | __free_page(vmx_io_bitmap_b); |
| 3517 | out: |
| 3518 | __free_page(vmx_io_bitmap_a); |
| 3519 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3520 | } |
| 3521 | |
| 3522 | static void __exit vmx_exit(void) |
| 3523 | { |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3524 | __free_page(vmx_msr_bitmap); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 3525 | __free_page(vmx_io_bitmap_b); |
| 3526 | __free_page(vmx_io_bitmap_a); |
| 3527 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 3528 | kvm_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3529 | } |
| 3530 | |
| 3531 | module_init(vmx_init) |
| 3532 | module_exit(vmx_exit) |