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