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