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