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