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