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