Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Kernel-based Virtual Machine driver for Linux |
| 3 | * |
| 4 | * AMD SVM support |
| 5 | * |
| 6 | * Copyright (C) 2006 Qumranet, Inc. |
| 7 | * |
| 8 | * Authors: |
| 9 | * Yaniv Kamay <yaniv@qumranet.com> |
| 10 | * Avi Kivity <avi@qumranet.com> |
| 11 | * |
| 12 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 13 | * the COPYING file in the top-level directory. |
| 14 | * |
| 15 | */ |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 16 | #include <linux/kvm_host.h> |
| 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" |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 20 | #include "kvm_cache_regs.h" |
Gleb Natapov | fe4c7b1 | 2009-03-23 11:23:18 +0200 | [diff] [blame] | 21 | #include "x86.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 22 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 23 | #include <linux/module.h> |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 24 | #include <linux/kernel.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 25 | #include <linux/vmalloc.h> |
| 26 | #include <linux/highmem.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 27 | #include <linux/sched.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 28 | |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 29 | #include <asm/desc.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 30 | |
Eduardo Habkost | 63d1142 | 2008-11-17 19:03:20 -0200 | [diff] [blame] | 31 | #include <asm/virtext.h> |
| 32 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 33 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
| 34 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 35 | MODULE_AUTHOR("Qumranet"); |
| 36 | MODULE_LICENSE("GPL"); |
| 37 | |
| 38 | #define IOPM_ALLOC_ORDER 2 |
| 39 | #define MSRPM_ALLOC_ORDER 1 |
| 40 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 41 | #define SEG_TYPE_LDT 2 |
| 42 | #define SEG_TYPE_BUSY_TSS16 3 |
| 43 | |
Joerg Roedel | 80b7706 | 2007-03-30 17:02:14 +0300 | [diff] [blame] | 44 | #define SVM_FEATURE_NPT (1 << 0) |
| 45 | #define SVM_FEATURE_LBRV (1 << 1) |
Amit Shah | 94c935a1 | 2008-08-18 13:11:46 +0300 | [diff] [blame] | 46 | #define SVM_FEATURE_SVML (1 << 2) |
Joerg Roedel | 80b7706 | 2007-03-30 17:02:14 +0300 | [diff] [blame] | 47 | |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 48 | #define DEBUGCTL_RESERVED_BITS (~(0x3fULL)) |
| 49 | |
Alexander Graf | c072542 | 2008-11-25 20:17:03 +0100 | [diff] [blame] | 50 | /* Turn on to get debugging output*/ |
| 51 | /* #define NESTED_DEBUG */ |
| 52 | |
| 53 | #ifdef NESTED_DEBUG |
| 54 | #define nsvm_printk(fmt, args...) printk(KERN_INFO fmt, ## args) |
| 55 | #else |
| 56 | #define nsvm_printk(fmt, args...) do {} while(0) |
| 57 | #endif |
| 58 | |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 59 | static const u32 host_save_user_msrs[] = { |
| 60 | #ifdef CONFIG_X86_64 |
| 61 | MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE, |
| 62 | MSR_FS_BASE, |
| 63 | #endif |
| 64 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, |
| 65 | }; |
| 66 | |
| 67 | #define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs) |
| 68 | |
| 69 | struct kvm_vcpu; |
| 70 | |
| 71 | struct vcpu_svm { |
| 72 | struct kvm_vcpu vcpu; |
| 73 | struct vmcb *vmcb; |
| 74 | unsigned long vmcb_pa; |
| 75 | struct svm_cpu_data *svm_data; |
| 76 | uint64_t asid_generation; |
| 77 | uint64_t sysenter_esp; |
| 78 | uint64_t sysenter_eip; |
| 79 | |
| 80 | u64 next_rip; |
| 81 | |
| 82 | u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS]; |
| 83 | u64 host_gs_base; |
| 84 | unsigned long host_cr2; |
| 85 | |
| 86 | u32 *msrpm; |
| 87 | struct vmcb *hsave; |
| 88 | u64 hsave_msr; |
| 89 | |
| 90 | u64 nested_vmcb; |
| 91 | |
| 92 | /* These are the merged vectors */ |
| 93 | u32 *nested_msrpm; |
| 94 | |
| 95 | /* gpa pointers to the real vectors */ |
| 96 | u64 nested_vmcb_msrpm; |
| 97 | }; |
| 98 | |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 99 | /* enable NPT for AMD64 and X86 with PAE */ |
| 100 | #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE) |
| 101 | static bool npt_enabled = true; |
| 102 | #else |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 103 | static bool npt_enabled = false; |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 104 | #endif |
Joerg Roedel | 6c7dac7 | 2008-02-07 13:47:40 +0100 | [diff] [blame] | 105 | static int npt = 1; |
| 106 | |
| 107 | module_param(npt, int, S_IRUGO); |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 108 | |
Alexander Graf | 236de05 | 2008-11-25 20:17:10 +0100 | [diff] [blame] | 109 | static int nested = 0; |
| 110 | module_param(nested, int, S_IRUGO); |
| 111 | |
Joerg Roedel | 44874f8 | 2008-08-27 14:18:43 +0200 | [diff] [blame] | 112 | static void svm_flush_tlb(struct kvm_vcpu *vcpu); |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 113 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 114 | static int nested_svm_exit_handled(struct vcpu_svm *svm, bool kvm_override); |
| 115 | static int nested_svm_vmexit(struct vcpu_svm *svm); |
| 116 | static int nested_svm_vmsave(struct vcpu_svm *svm, void *nested_vmcb, |
| 117 | void *arg2, void *opaque); |
| 118 | static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr, |
| 119 | bool has_error_code, u32 error_code); |
| 120 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 121 | static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu) |
| 122 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 123 | return container_of(vcpu, struct vcpu_svm, vcpu); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 124 | } |
| 125 | |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 126 | static inline bool is_nested(struct vcpu_svm *svm) |
| 127 | { |
| 128 | return svm->nested_vmcb; |
| 129 | } |
| 130 | |
Harvey Harrison | 4866d5e | 2008-02-19 10:32:02 -0800 | [diff] [blame] | 131 | static unsigned long iopm_base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 132 | |
| 133 | struct kvm_ldttss_desc { |
| 134 | u16 limit0; |
| 135 | u16 base0; |
| 136 | unsigned base1 : 8, type : 5, dpl : 2, p : 1; |
| 137 | unsigned limit1 : 4, zero0 : 3, g : 1, base2 : 8; |
| 138 | u32 base3; |
| 139 | u32 zero1; |
| 140 | } __attribute__((packed)); |
| 141 | |
| 142 | struct svm_cpu_data { |
| 143 | int cpu; |
| 144 | |
Avi Kivity | 5008fdf | 2007-04-02 13:05:50 +0300 | [diff] [blame] | 145 | u64 asid_generation; |
| 146 | u32 max_asid; |
| 147 | u32 next_asid; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 148 | struct kvm_ldttss_desc *tss_desc; |
| 149 | |
| 150 | struct page *save_area; |
| 151 | }; |
| 152 | |
| 153 | static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data); |
Joerg Roedel | 80b7706 | 2007-03-30 17:02:14 +0300 | [diff] [blame] | 154 | static uint32_t svm_features; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 155 | |
| 156 | struct svm_init_data { |
| 157 | int cpu; |
| 158 | int r; |
| 159 | }; |
| 160 | |
| 161 | static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000}; |
| 162 | |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 163 | #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 164 | #define MSRS_RANGE_SIZE 2048 |
| 165 | #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2) |
| 166 | |
| 167 | #define MAX_INST_SIZE 15 |
| 168 | |
Joerg Roedel | 80b7706 | 2007-03-30 17:02:14 +0300 | [diff] [blame] | 169 | static inline u32 svm_has(u32 feat) |
| 170 | { |
| 171 | return svm_features & feat; |
| 172 | } |
| 173 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 174 | static inline void clgi(void) |
| 175 | { |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 176 | asm volatile (__ex(SVM_CLGI)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | static inline void stgi(void) |
| 180 | { |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 181 | asm volatile (__ex(SVM_STGI)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | static inline void invlpga(unsigned long addr, u32 asid) |
| 185 | { |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 186 | asm volatile (__ex(SVM_INVLPGA) :: "a"(addr), "c"(asid)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | static inline unsigned long kvm_read_cr2(void) |
| 190 | { |
| 191 | unsigned long cr2; |
| 192 | |
| 193 | asm volatile ("mov %%cr2, %0" : "=r" (cr2)); |
| 194 | return cr2; |
| 195 | } |
| 196 | |
| 197 | static inline void kvm_write_cr2(unsigned long val) |
| 198 | { |
| 199 | asm volatile ("mov %0, %%cr2" :: "r" (val)); |
| 200 | } |
| 201 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 202 | static inline void force_new_asid(struct kvm_vcpu *vcpu) |
| 203 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 204 | to_svm(vcpu)->asid_generation--; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static inline void flush_guest_tlb(struct kvm_vcpu *vcpu) |
| 208 | { |
| 209 | force_new_asid(vcpu); |
| 210 | } |
| 211 | |
| 212 | static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 213 | { |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 214 | if (!npt_enabled && !(efer & EFER_LMA)) |
Carlo Marcelo Arenas Belon | 2b5203e | 2007-12-01 06:17:11 -0600 | [diff] [blame] | 215 | efer &= ~EFER_LME; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 216 | |
Alexander Graf | 9962d03 | 2008-11-25 20:17:02 +0100 | [diff] [blame] | 217 | to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 218 | vcpu->arch.shadow_efer = efer; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 221 | static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr, |
| 222 | bool has_error_code, u32 error_code) |
| 223 | { |
| 224 | struct vcpu_svm *svm = to_svm(vcpu); |
| 225 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 226 | /* If we are within a nested VM we'd better #VMEXIT and let the |
| 227 | guest handle the exception */ |
| 228 | if (nested_svm_check_exception(svm, nr, has_error_code, error_code)) |
| 229 | return; |
| 230 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 231 | svm->vmcb->control.event_inj = nr |
| 232 | | SVM_EVTINJ_VALID |
| 233 | | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0) |
| 234 | | SVM_EVTINJ_TYPE_EXEPT; |
| 235 | svm->vmcb->control.event_inj_err = error_code; |
| 236 | } |
| 237 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 238 | static int is_external_interrupt(u32 info) |
| 239 | { |
| 240 | info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID; |
| 241 | return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR); |
| 242 | } |
| 243 | |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 244 | static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 245 | { |
| 246 | struct vcpu_svm *svm = to_svm(vcpu); |
| 247 | u32 ret = 0; |
| 248 | |
| 249 | if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) |
| 250 | ret |= X86_SHADOW_INT_STI | X86_SHADOW_INT_MOV_SS; |
| 251 | return ret & mask; |
| 252 | } |
| 253 | |
| 254 | static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 255 | { |
| 256 | struct vcpu_svm *svm = to_svm(vcpu); |
| 257 | |
| 258 | if (mask == 0) |
| 259 | svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK; |
| 260 | else |
| 261 | svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK; |
| 262 | |
| 263 | } |
| 264 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 265 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 266 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 267 | struct vcpu_svm *svm = to_svm(vcpu); |
| 268 | |
| 269 | if (!svm->next_rip) { |
Gleb Natapov | f629cf8 | 2009-05-11 13:35:49 +0300 | [diff] [blame] | 270 | if (emulate_instruction(vcpu, vcpu->run, 0, 0, EMULTYPE_SKIP) != |
| 271 | EMULATE_DONE) |
| 272 | printk(KERN_DEBUG "%s: NOP\n", __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 273 | return; |
| 274 | } |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 275 | if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE) |
| 276 | printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n", |
| 277 | __func__, kvm_rip_read(vcpu), svm->next_rip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 278 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 279 | kvm_rip_write(vcpu, svm->next_rip); |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 280 | svm_set_interrupt_shadow(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | static int has_svm(void) |
| 284 | { |
Eduardo Habkost | 63d1142 | 2008-11-17 19:03:20 -0200 | [diff] [blame] | 285 | const char *msg; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 286 | |
Eduardo Habkost | 63d1142 | 2008-11-17 19:03:20 -0200 | [diff] [blame] | 287 | if (!cpu_has_svm(&msg)) { |
Joe Perches | ff81ff1 | 2009-01-08 11:05:17 -0800 | [diff] [blame] | 288 | printk(KERN_INFO "has_svm: %s\n", msg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 289 | return 0; |
| 290 | } |
| 291 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 292 | return 1; |
| 293 | } |
| 294 | |
| 295 | static void svm_hardware_disable(void *garbage) |
| 296 | { |
Eduardo Habkost | 2c8dcee | 2008-11-17 19:03:21 -0200 | [diff] [blame] | 297 | cpu_svm_disable(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | static void svm_hardware_enable(void *garbage) |
| 301 | { |
| 302 | |
| 303 | struct svm_cpu_data *svm_data; |
| 304 | uint64_t efer; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 305 | struct desc_ptr gdt_descr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 306 | struct desc_struct *gdt; |
| 307 | int me = raw_smp_processor_id(); |
| 308 | |
| 309 | if (!has_svm()) { |
| 310 | printk(KERN_ERR "svm_cpu_init: err EOPNOTSUPP on %d\n", me); |
| 311 | return; |
| 312 | } |
| 313 | svm_data = per_cpu(svm_data, me); |
| 314 | |
| 315 | if (!svm_data) { |
| 316 | printk(KERN_ERR "svm_cpu_init: svm_data is NULL on %d\n", |
| 317 | me); |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | svm_data->asid_generation = 1; |
| 322 | svm_data->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1; |
| 323 | svm_data->next_asid = svm_data->max_asid + 1; |
| 324 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 325 | asm volatile ("sgdt %0" : "=m"(gdt_descr)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 326 | gdt = (struct desc_struct *)gdt_descr.address; |
| 327 | svm_data->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS); |
| 328 | |
| 329 | rdmsrl(MSR_EFER, efer); |
Alexander Graf | 9962d03 | 2008-11-25 20:17:02 +0100 | [diff] [blame] | 330 | wrmsrl(MSR_EFER, efer | EFER_SVME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 331 | |
| 332 | wrmsrl(MSR_VM_HSAVE_PA, |
| 333 | page_to_pfn(svm_data->save_area) << PAGE_SHIFT); |
| 334 | } |
| 335 | |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 336 | static void svm_cpu_uninit(int cpu) |
| 337 | { |
| 338 | struct svm_cpu_data *svm_data |
| 339 | = per_cpu(svm_data, raw_smp_processor_id()); |
| 340 | |
| 341 | if (!svm_data) |
| 342 | return; |
| 343 | |
| 344 | per_cpu(svm_data, raw_smp_processor_id()) = NULL; |
| 345 | __free_page(svm_data->save_area); |
| 346 | kfree(svm_data); |
| 347 | } |
| 348 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 349 | static int svm_cpu_init(int cpu) |
| 350 | { |
| 351 | struct svm_cpu_data *svm_data; |
| 352 | int r; |
| 353 | |
| 354 | svm_data = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL); |
| 355 | if (!svm_data) |
| 356 | return -ENOMEM; |
| 357 | svm_data->cpu = cpu; |
| 358 | svm_data->save_area = alloc_page(GFP_KERNEL); |
| 359 | r = -ENOMEM; |
| 360 | if (!svm_data->save_area) |
| 361 | goto err_1; |
| 362 | |
| 363 | per_cpu(svm_data, cpu) = svm_data; |
| 364 | |
| 365 | return 0; |
| 366 | |
| 367 | err_1: |
| 368 | kfree(svm_data); |
| 369 | return r; |
| 370 | |
| 371 | } |
| 372 | |
Rusty Russell | bfc733a | 2007-07-31 20:42:42 +1000 | [diff] [blame] | 373 | static void set_msr_interception(u32 *msrpm, unsigned msr, |
| 374 | int read, int write) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 375 | { |
| 376 | int i; |
| 377 | |
| 378 | for (i = 0; i < NUM_MSR_MAPS; i++) { |
| 379 | if (msr >= msrpm_ranges[i] && |
| 380 | msr < msrpm_ranges[i] + MSRS_IN_RANGE) { |
| 381 | u32 msr_offset = (i * MSRS_IN_RANGE + msr - |
| 382 | msrpm_ranges[i]) * 2; |
| 383 | |
| 384 | u32 *base = msrpm + (msr_offset / 32); |
| 385 | u32 msr_shift = msr_offset % 32; |
| 386 | u32 mask = ((write) ? 0 : 2) | ((read) ? 0 : 1); |
| 387 | *base = (*base & ~(0x3 << msr_shift)) | |
| 388 | (mask << msr_shift); |
Rusty Russell | bfc733a | 2007-07-31 20:42:42 +1000 | [diff] [blame] | 389 | return; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 390 | } |
| 391 | } |
Rusty Russell | bfc733a | 2007-07-31 20:42:42 +1000 | [diff] [blame] | 392 | BUG(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 393 | } |
| 394 | |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 395 | static void svm_vcpu_init_msrpm(u32 *msrpm) |
| 396 | { |
| 397 | memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER)); |
| 398 | |
| 399 | #ifdef CONFIG_X86_64 |
| 400 | set_msr_interception(msrpm, MSR_GS_BASE, 1, 1); |
| 401 | set_msr_interception(msrpm, MSR_FS_BASE, 1, 1); |
| 402 | set_msr_interception(msrpm, MSR_KERNEL_GS_BASE, 1, 1); |
| 403 | set_msr_interception(msrpm, MSR_LSTAR, 1, 1); |
| 404 | set_msr_interception(msrpm, MSR_CSTAR, 1, 1); |
| 405 | set_msr_interception(msrpm, MSR_SYSCALL_MASK, 1, 1); |
| 406 | #endif |
| 407 | set_msr_interception(msrpm, MSR_K6_STAR, 1, 1); |
| 408 | set_msr_interception(msrpm, MSR_IA32_SYSENTER_CS, 1, 1); |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 409 | } |
| 410 | |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 411 | static void svm_enable_lbrv(struct vcpu_svm *svm) |
| 412 | { |
| 413 | u32 *msrpm = svm->msrpm; |
| 414 | |
| 415 | svm->vmcb->control.lbr_ctl = 1; |
| 416 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1); |
| 417 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1); |
| 418 | set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1); |
| 419 | set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1); |
| 420 | } |
| 421 | |
| 422 | static void svm_disable_lbrv(struct vcpu_svm *svm) |
| 423 | { |
| 424 | u32 *msrpm = svm->msrpm; |
| 425 | |
| 426 | svm->vmcb->control.lbr_ctl = 0; |
| 427 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0); |
| 428 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0); |
| 429 | set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0); |
| 430 | set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0); |
| 431 | } |
| 432 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 433 | static __init int svm_hardware_setup(void) |
| 434 | { |
| 435 | int cpu; |
| 436 | struct page *iopm_pages; |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 437 | void *iopm_va; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 438 | int r; |
| 439 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 440 | iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER); |
| 441 | |
| 442 | if (!iopm_pages) |
| 443 | return -ENOMEM; |
Anthony Liguori | c868133 | 2007-04-30 09:48:11 +0300 | [diff] [blame] | 444 | |
| 445 | iopm_va = page_address(iopm_pages); |
| 446 | memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 447 | iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT; |
| 448 | |
Joerg Roedel | 50a37eb | 2008-01-31 14:57:38 +0100 | [diff] [blame] | 449 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 450 | kvm_enable_efer_bits(EFER_NX); |
| 451 | |
Alexander Graf | 1b2fd70 | 2009-02-02 16:23:51 +0100 | [diff] [blame] | 452 | if (boot_cpu_has(X86_FEATURE_FXSR_OPT)) |
| 453 | kvm_enable_efer_bits(EFER_FFXSR); |
| 454 | |
Alexander Graf | 236de05 | 2008-11-25 20:17:10 +0100 | [diff] [blame] | 455 | if (nested) { |
| 456 | printk(KERN_INFO "kvm: Nested Virtualization enabled\n"); |
| 457 | kvm_enable_efer_bits(EFER_SVME); |
| 458 | } |
| 459 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 460 | for_each_online_cpu(cpu) { |
| 461 | r = svm_cpu_init(cpu); |
| 462 | if (r) |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 463 | goto err; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 464 | } |
Joerg Roedel | 33bd6a0 | 2008-02-07 13:47:38 +0100 | [diff] [blame] | 465 | |
| 466 | svm_features = cpuid_edx(SVM_CPUID_FUNC); |
| 467 | |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 468 | if (!svm_has(SVM_FEATURE_NPT)) |
| 469 | npt_enabled = false; |
| 470 | |
Joerg Roedel | 6c7dac7 | 2008-02-07 13:47:40 +0100 | [diff] [blame] | 471 | if (npt_enabled && !npt) { |
| 472 | printk(KERN_INFO "kvm: Nested Paging disabled\n"); |
| 473 | npt_enabled = false; |
| 474 | } |
| 475 | |
Joerg Roedel | 1855267 | 2008-02-07 13:47:41 +0100 | [diff] [blame] | 476 | if (npt_enabled) { |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 477 | printk(KERN_INFO "kvm: Nested Paging enabled\n"); |
Joerg Roedel | 1855267 | 2008-02-07 13:47:41 +0100 | [diff] [blame] | 478 | kvm_enable_tdp(); |
Joerg Roedel | 5f4cb66 | 2008-07-14 20:36:36 +0200 | [diff] [blame] | 479 | } else |
| 480 | kvm_disable_tdp(); |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 481 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 482 | return 0; |
| 483 | |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 484 | err: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 485 | __free_pages(iopm_pages, IOPM_ALLOC_ORDER); |
| 486 | iopm_base = 0; |
| 487 | return r; |
| 488 | } |
| 489 | |
| 490 | static __exit void svm_hardware_unsetup(void) |
| 491 | { |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 492 | int cpu; |
| 493 | |
| 494 | for_each_online_cpu(cpu) |
| 495 | svm_cpu_uninit(cpu); |
| 496 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 497 | __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER); |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 498 | iopm_base = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | static void init_seg(struct vmcb_seg *seg) |
| 502 | { |
| 503 | seg->selector = 0; |
| 504 | seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK | |
| 505 | SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */ |
| 506 | seg->limit = 0xffff; |
| 507 | seg->base = 0; |
| 508 | } |
| 509 | |
| 510 | static void init_sys_seg(struct vmcb_seg *seg, uint32_t type) |
| 511 | { |
| 512 | seg->selector = 0; |
| 513 | seg->attrib = SVM_SELECTOR_P_MASK | type; |
| 514 | seg->limit = 0xffff; |
| 515 | seg->base = 0; |
| 516 | } |
| 517 | |
Joerg Roedel | e6101a9 | 2008-02-13 18:58:45 +0100 | [diff] [blame] | 518 | static void init_vmcb(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 519 | { |
Joerg Roedel | e6101a9 | 2008-02-13 18:58:45 +0100 | [diff] [blame] | 520 | struct vmcb_control_area *control = &svm->vmcb->control; |
| 521 | struct vmcb_save_area *save = &svm->vmcb->save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 522 | |
| 523 | control->intercept_cr_read = INTERCEPT_CR0_MASK | |
| 524 | INTERCEPT_CR3_MASK | |
Joerg Roedel | 649d686 | 2008-04-16 16:51:15 +0200 | [diff] [blame] | 525 | INTERCEPT_CR4_MASK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 526 | |
| 527 | control->intercept_cr_write = INTERCEPT_CR0_MASK | |
| 528 | INTERCEPT_CR3_MASK | |
Avi Kivity | 80a8119 | 2007-12-06 19:50:00 +0200 | [diff] [blame] | 529 | INTERCEPT_CR4_MASK | |
| 530 | INTERCEPT_CR8_MASK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 531 | |
| 532 | control->intercept_dr_read = INTERCEPT_DR0_MASK | |
| 533 | INTERCEPT_DR1_MASK | |
| 534 | INTERCEPT_DR2_MASK | |
| 535 | INTERCEPT_DR3_MASK; |
| 536 | |
| 537 | control->intercept_dr_write = INTERCEPT_DR0_MASK | |
| 538 | INTERCEPT_DR1_MASK | |
| 539 | INTERCEPT_DR2_MASK | |
| 540 | INTERCEPT_DR3_MASK | |
| 541 | INTERCEPT_DR5_MASK | |
| 542 | INTERCEPT_DR7_MASK; |
| 543 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 544 | control->intercept_exceptions = (1 << PF_VECTOR) | |
Joerg Roedel | 53371b5 | 2008-04-09 14:15:30 +0200 | [diff] [blame] | 545 | (1 << UD_VECTOR) | |
| 546 | (1 << MC_VECTOR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 547 | |
| 548 | |
| 549 | control->intercept = (1ULL << INTERCEPT_INTR) | |
| 550 | (1ULL << INTERCEPT_NMI) | |
Joerg Roedel | 0152527 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 551 | (1ULL << INTERCEPT_SMI) | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 552 | (1ULL << INTERCEPT_CPUID) | |
Avi Kivity | cf5a94d | 2007-10-28 16:11:58 +0200 | [diff] [blame] | 553 | (1ULL << INTERCEPT_INVD) | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 554 | (1ULL << INTERCEPT_HLT) | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 555 | (1ULL << INTERCEPT_INVLPG) | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 556 | (1ULL << INTERCEPT_INVLPGA) | |
| 557 | (1ULL << INTERCEPT_IOIO_PROT) | |
| 558 | (1ULL << INTERCEPT_MSR_PROT) | |
| 559 | (1ULL << INTERCEPT_TASK_SWITCH) | |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 560 | (1ULL << INTERCEPT_SHUTDOWN) | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 561 | (1ULL << INTERCEPT_VMRUN) | |
| 562 | (1ULL << INTERCEPT_VMMCALL) | |
| 563 | (1ULL << INTERCEPT_VMLOAD) | |
| 564 | (1ULL << INTERCEPT_VMSAVE) | |
| 565 | (1ULL << INTERCEPT_STGI) | |
| 566 | (1ULL << INTERCEPT_CLGI) | |
Joerg Roedel | 916ce23 | 2007-03-21 19:47:00 +0100 | [diff] [blame] | 567 | (1ULL << INTERCEPT_SKINIT) | |
Avi Kivity | cf5a94d | 2007-10-28 16:11:58 +0200 | [diff] [blame] | 568 | (1ULL << INTERCEPT_WBINVD) | |
Joerg Roedel | 916ce23 | 2007-03-21 19:47:00 +0100 | [diff] [blame] | 569 | (1ULL << INTERCEPT_MONITOR) | |
| 570 | (1ULL << INTERCEPT_MWAIT); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 571 | |
| 572 | control->iopm_base_pa = iopm_base; |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 573 | control->msrpm_base_pa = __pa(svm->msrpm); |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 574 | control->tsc_offset = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 575 | control->int_ctl = V_INTR_MASKING_MASK; |
| 576 | |
| 577 | init_seg(&save->es); |
| 578 | init_seg(&save->ss); |
| 579 | init_seg(&save->ds); |
| 580 | init_seg(&save->fs); |
| 581 | init_seg(&save->gs); |
| 582 | |
| 583 | save->cs.selector = 0xf000; |
| 584 | /* Executable/Readable Code Segment */ |
| 585 | save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK | |
| 586 | SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK; |
| 587 | save->cs.limit = 0xffff; |
Avi Kivity | d92899a | 2007-02-12 00:54:38 -0800 | [diff] [blame] | 588 | /* |
| 589 | * cs.base should really be 0xffff0000, but vmx can't handle that, so |
| 590 | * be consistent with it. |
| 591 | * |
| 592 | * Replace when we have real mode working for vmx. |
| 593 | */ |
| 594 | save->cs.base = 0xf0000; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 595 | |
| 596 | save->gdtr.limit = 0xffff; |
| 597 | save->idtr.limit = 0xffff; |
| 598 | |
| 599 | init_sys_seg(&save->ldtr, SEG_TYPE_LDT); |
| 600 | init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16); |
| 601 | |
Alexander Graf | 9962d03 | 2008-11-25 20:17:02 +0100 | [diff] [blame] | 602 | save->efer = EFER_SVME; |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 603 | save->dr6 = 0xffff0ff0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 604 | save->dr7 = 0x400; |
| 605 | save->rflags = 2; |
| 606 | save->rip = 0x0000fff0; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 607 | svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 608 | |
| 609 | /* |
| 610 | * cr0 val on cpu init should be 0x60000010, we enable cpu |
| 611 | * cache by default. the orderly way is to enable cache in bios. |
| 612 | */ |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 613 | save->cr0 = 0x00000010 | X86_CR0_PG | X86_CR0_WP; |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 614 | save->cr4 = X86_CR4_PAE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 615 | /* rdx = ?? */ |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 616 | |
| 617 | if (npt_enabled) { |
| 618 | /* Setup VMCB for Nested Paging */ |
| 619 | control->nested_ctl = 1; |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 620 | control->intercept &= ~((1ULL << INTERCEPT_TASK_SWITCH) | |
| 621 | (1ULL << INTERCEPT_INVLPG)); |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 622 | control->intercept_exceptions &= ~(1 << PF_VECTOR); |
| 623 | control->intercept_cr_read &= ~(INTERCEPT_CR0_MASK| |
| 624 | INTERCEPT_CR3_MASK); |
| 625 | control->intercept_cr_write &= ~(INTERCEPT_CR0_MASK| |
| 626 | INTERCEPT_CR3_MASK); |
| 627 | save->g_pat = 0x0007040600070406ULL; |
| 628 | /* enable caching because the QEMU Bios doesn't enable it */ |
| 629 | save->cr0 = X86_CR0_ET; |
| 630 | save->cr3 = 0; |
| 631 | save->cr4 = 0; |
| 632 | } |
Avi Kivity | a79d2f1 | 2008-04-14 13:10:21 +0300 | [diff] [blame] | 633 | force_new_asid(&svm->vcpu); |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 634 | |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 635 | svm->nested_vmcb = 0; |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 636 | svm->vcpu.arch.hflags = HF_GIF_MASK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 637 | } |
| 638 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 639 | static int svm_vcpu_reset(struct kvm_vcpu *vcpu) |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 640 | { |
| 641 | struct vcpu_svm *svm = to_svm(vcpu); |
| 642 | |
Joerg Roedel | e6101a9 | 2008-02-13 18:58:45 +0100 | [diff] [blame] | 643 | init_vmcb(svm); |
Avi Kivity | 7043338 | 2007-11-07 12:57:23 +0200 | [diff] [blame] | 644 | |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame^] | 645 | if (!kvm_vcpu_is_bsp(vcpu)) { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 646 | kvm_rip_write(vcpu, 0); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 647 | svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12; |
| 648 | svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8; |
Avi Kivity | 7043338 | 2007-11-07 12:57:23 +0200 | [diff] [blame] | 649 | } |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 650 | vcpu->arch.regs_avail = ~0; |
| 651 | vcpu->arch.regs_dirty = ~0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 652 | |
| 653 | return 0; |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 654 | } |
| 655 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 656 | static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 657 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 658 | struct vcpu_svm *svm; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 659 | struct page *page; |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 660 | struct page *msrpm_pages; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 661 | struct page *hsave_page; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 662 | struct page *nested_msrpm_pages; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 663 | int err; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 664 | |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 665 | svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 666 | if (!svm) { |
| 667 | err = -ENOMEM; |
| 668 | goto out; |
| 669 | } |
| 670 | |
| 671 | err = kvm_vcpu_init(&svm->vcpu, kvm, id); |
| 672 | if (err) |
| 673 | goto free_svm; |
| 674 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 675 | page = alloc_page(GFP_KERNEL); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 676 | if (!page) { |
| 677 | err = -ENOMEM; |
| 678 | goto uninit; |
| 679 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 680 | |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 681 | err = -ENOMEM; |
| 682 | msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER); |
| 683 | if (!msrpm_pages) |
| 684 | goto uninit; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 685 | |
| 686 | nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER); |
| 687 | if (!nested_msrpm_pages) |
| 688 | goto uninit; |
| 689 | |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 690 | svm->msrpm = page_address(msrpm_pages); |
| 691 | svm_vcpu_init_msrpm(svm->msrpm); |
| 692 | |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 693 | hsave_page = alloc_page(GFP_KERNEL); |
| 694 | if (!hsave_page) |
| 695 | goto uninit; |
| 696 | svm->hsave = page_address(hsave_page); |
| 697 | |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 698 | svm->nested_msrpm = page_address(nested_msrpm_pages); |
| 699 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 700 | svm->vmcb = page_address(page); |
| 701 | clear_page(svm->vmcb); |
| 702 | svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT; |
| 703 | svm->asid_generation = 0; |
Joerg Roedel | e6101a9 | 2008-02-13 18:58:45 +0100 | [diff] [blame] | 704 | init_vmcb(svm); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 705 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 706 | fx_init(&svm->vcpu); |
| 707 | svm->vcpu.fpu_active = 1; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 708 | svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE; |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame^] | 709 | if (kvm_vcpu_is_bsp(&svm->vcpu)) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 710 | svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 711 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 712 | return &svm->vcpu; |
Avi Kivity | 36241b8 | 2006-12-22 01:05:20 -0800 | [diff] [blame] | 713 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 714 | uninit: |
| 715 | kvm_vcpu_uninit(&svm->vcpu); |
| 716 | free_svm: |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 717 | kmem_cache_free(kvm_vcpu_cache, svm); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 718 | out: |
| 719 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | static void svm_free_vcpu(struct kvm_vcpu *vcpu) |
| 723 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 724 | struct vcpu_svm *svm = to_svm(vcpu); |
| 725 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 726 | __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT)); |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 727 | __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER); |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 728 | __free_page(virt_to_page(svm->hsave)); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 729 | __free_pages(virt_to_page(svm->nested_msrpm), MSRPM_ALLOC_ORDER); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 730 | kvm_vcpu_uninit(vcpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 731 | kmem_cache_free(kvm_vcpu_cache, svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 732 | } |
| 733 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 734 | static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 735 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 736 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 737 | int i; |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 738 | |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 739 | if (unlikely(cpu != vcpu->cpu)) { |
| 740 | u64 tsc_this, delta; |
| 741 | |
| 742 | /* |
| 743 | * Make sure that the guest sees a monotonically |
| 744 | * increasing TSC. |
| 745 | */ |
| 746 | rdtscll(tsc_this); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 747 | delta = vcpu->arch.host_tsc - tsc_this; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 748 | svm->vmcb->control.tsc_offset += delta; |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 749 | vcpu->cpu = cpu; |
Marcelo Tosatti | 2f59971 | 2008-05-27 12:10:20 -0300 | [diff] [blame] | 750 | kvm_migrate_timers(vcpu); |
Marcelo Tosatti | 4b656b1 | 2009-07-21 12:47:45 -0300 | [diff] [blame] | 751 | svm->asid_generation = 0; |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 752 | } |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 753 | |
| 754 | for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 755 | rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | static void svm_vcpu_put(struct kvm_vcpu *vcpu) |
| 759 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 760 | struct vcpu_svm *svm = to_svm(vcpu); |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 761 | int i; |
| 762 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 763 | ++vcpu->stat.host_state_reload; |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 764 | for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 765 | wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]); |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 766 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 767 | rdtscll(vcpu->arch.host_tsc); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 768 | } |
| 769 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 770 | static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu) |
| 771 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 772 | return to_svm(vcpu)->vmcb->save.rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 776 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 777 | to_svm(vcpu)->vmcb->save.rflags = rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 778 | } |
| 779 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 780 | static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg) |
| 781 | { |
| 782 | switch (reg) { |
| 783 | case VCPU_EXREG_PDPTR: |
| 784 | BUG_ON(!npt_enabled); |
| 785 | load_pdptrs(vcpu, vcpu->arch.cr3); |
| 786 | break; |
| 787 | default: |
| 788 | BUG(); |
| 789 | } |
| 790 | } |
| 791 | |
Alexander Graf | f0b8505 | 2008-11-25 20:17:01 +0100 | [diff] [blame] | 792 | static void svm_set_vintr(struct vcpu_svm *svm) |
| 793 | { |
| 794 | svm->vmcb->control.intercept |= 1ULL << INTERCEPT_VINTR; |
| 795 | } |
| 796 | |
| 797 | static void svm_clear_vintr(struct vcpu_svm *svm) |
| 798 | { |
| 799 | svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR); |
| 800 | } |
| 801 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 802 | static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg) |
| 803 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 804 | struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 805 | |
| 806 | switch (seg) { |
| 807 | case VCPU_SREG_CS: return &save->cs; |
| 808 | case VCPU_SREG_DS: return &save->ds; |
| 809 | case VCPU_SREG_ES: return &save->es; |
| 810 | case VCPU_SREG_FS: return &save->fs; |
| 811 | case VCPU_SREG_GS: return &save->gs; |
| 812 | case VCPU_SREG_SS: return &save->ss; |
| 813 | case VCPU_SREG_TR: return &save->tr; |
| 814 | case VCPU_SREG_LDTR: return &save->ldtr; |
| 815 | } |
| 816 | BUG(); |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 817 | return NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 821 | { |
| 822 | struct vmcb_seg *s = svm_seg(vcpu, seg); |
| 823 | |
| 824 | return s->base; |
| 825 | } |
| 826 | |
| 827 | static void svm_get_segment(struct kvm_vcpu *vcpu, |
| 828 | struct kvm_segment *var, int seg) |
| 829 | { |
| 830 | struct vmcb_seg *s = svm_seg(vcpu, seg); |
| 831 | |
| 832 | var->base = s->base; |
| 833 | var->limit = s->limit; |
| 834 | var->selector = s->selector; |
| 835 | var->type = s->attrib & SVM_SELECTOR_TYPE_MASK; |
| 836 | var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1; |
| 837 | var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3; |
| 838 | var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1; |
| 839 | var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1; |
| 840 | var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1; |
| 841 | var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1; |
| 842 | var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1; |
Amit Shah | 25022ac | 2008-10-27 09:04:17 +0000 | [diff] [blame] | 843 | |
Andre Przywara | 19bca6a | 2009-04-28 12:45:30 +0200 | [diff] [blame] | 844 | /* AMD's VMCB does not have an explicit unusable field, so emulate it |
| 845 | * for cross vendor migration purposes by "not present" |
| 846 | */ |
| 847 | var->unusable = !var->present || (var->type == 0); |
| 848 | |
Andre Przywara | 1fbdc7a | 2009-01-11 22:39:44 +0100 | [diff] [blame] | 849 | switch (seg) { |
| 850 | case VCPU_SREG_CS: |
| 851 | /* |
| 852 | * SVM always stores 0 for the 'G' bit in the CS selector in |
| 853 | * the VMCB on a VMEXIT. This hurts cross-vendor migration: |
| 854 | * Intel's VMENTRY has a check on the 'G' bit. |
| 855 | */ |
Amit Shah | 25022ac | 2008-10-27 09:04:17 +0000 | [diff] [blame] | 856 | var->g = s->limit > 0xfffff; |
Andre Przywara | 1fbdc7a | 2009-01-11 22:39:44 +0100 | [diff] [blame] | 857 | break; |
| 858 | case VCPU_SREG_TR: |
| 859 | /* |
| 860 | * Work around a bug where the busy flag in the tr selector |
| 861 | * isn't exposed |
| 862 | */ |
Amit Shah | c0d0982 | 2008-10-27 09:04:18 +0000 | [diff] [blame] | 863 | var->type |= 0x2; |
Andre Przywara | 1fbdc7a | 2009-01-11 22:39:44 +0100 | [diff] [blame] | 864 | break; |
| 865 | case VCPU_SREG_DS: |
| 866 | case VCPU_SREG_ES: |
| 867 | case VCPU_SREG_FS: |
| 868 | case VCPU_SREG_GS: |
| 869 | /* |
| 870 | * The accessed bit must always be set in the segment |
| 871 | * descriptor cache, although it can be cleared in the |
| 872 | * descriptor, the cached bit always remains at 1. Since |
| 873 | * Intel has a check on this, set it here to support |
| 874 | * cross-vendor migration. |
| 875 | */ |
| 876 | if (!var->unusable) |
| 877 | var->type |= 0x1; |
| 878 | break; |
Andre Przywara | b586eb0 | 2009-04-28 12:45:43 +0200 | [diff] [blame] | 879 | case VCPU_SREG_SS: |
| 880 | /* On AMD CPUs sometimes the DB bit in the segment |
| 881 | * descriptor is left as 1, although the whole segment has |
| 882 | * been made unusable. Clear it here to pass an Intel VMX |
| 883 | * entry check when cross vendor migrating. |
| 884 | */ |
| 885 | if (var->unusable) |
| 886 | var->db = 0; |
| 887 | break; |
Andre Przywara | 1fbdc7a | 2009-01-11 22:39:44 +0100 | [diff] [blame] | 888 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 889 | } |
| 890 | |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 891 | static int svm_get_cpl(struct kvm_vcpu *vcpu) |
| 892 | { |
| 893 | struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save; |
| 894 | |
| 895 | return save->cpl; |
| 896 | } |
| 897 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 898 | static void svm_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) |
| 899 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 900 | struct vcpu_svm *svm = to_svm(vcpu); |
| 901 | |
| 902 | dt->limit = svm->vmcb->save.idtr.limit; |
| 903 | dt->base = svm->vmcb->save.idtr.base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | static void svm_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) |
| 907 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 908 | struct vcpu_svm *svm = to_svm(vcpu); |
| 909 | |
| 910 | svm->vmcb->save.idtr.limit = dt->limit; |
| 911 | svm->vmcb->save.idtr.base = dt->base ; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | static void svm_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) |
| 915 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 916 | struct vcpu_svm *svm = to_svm(vcpu); |
| 917 | |
| 918 | dt->limit = svm->vmcb->save.gdtr.limit; |
| 919 | dt->base = svm->vmcb->save.gdtr.base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | static void svm_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) |
| 923 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 924 | struct vcpu_svm *svm = to_svm(vcpu); |
| 925 | |
| 926 | svm->vmcb->save.gdtr.limit = dt->limit; |
| 927 | svm->vmcb->save.gdtr.base = dt->base ; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 928 | } |
| 929 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 930 | static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 931 | { |
| 932 | } |
| 933 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 934 | static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 935 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 936 | struct vcpu_svm *svm = to_svm(vcpu); |
| 937 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 938 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 939 | if (vcpu->arch.shadow_efer & EFER_LME) { |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 940 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 941 | vcpu->arch.shadow_efer |= EFER_LMA; |
Carlo Marcelo Arenas Belon | 2b5203e | 2007-12-01 06:17:11 -0600 | [diff] [blame] | 942 | svm->vmcb->save.efer |= EFER_LMA | EFER_LME; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 943 | } |
| 944 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 945 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 946 | vcpu->arch.shadow_efer &= ~EFER_LMA; |
Carlo Marcelo Arenas Belon | 2b5203e | 2007-12-01 06:17:11 -0600 | [diff] [blame] | 947 | svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 948 | } |
| 949 | } |
| 950 | #endif |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 951 | if (npt_enabled) |
| 952 | goto set; |
| 953 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 954 | if ((vcpu->arch.cr0 & X86_CR0_TS) && !(cr0 & X86_CR0_TS)) { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 955 | svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR); |
Anthony Liguori | 7807fa6 | 2007-04-23 09:17:21 -0500 | [diff] [blame] | 956 | vcpu->fpu_active = 1; |
| 957 | } |
| 958 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 959 | vcpu->arch.cr0 = cr0; |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 960 | cr0 |= X86_CR0_PG | X86_CR0_WP; |
Joerg Roedel | 6b390b6 | 2008-01-29 13:01:27 +0100 | [diff] [blame] | 961 | if (!vcpu->fpu_active) { |
| 962 | svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR); |
Joerg Roedel | 334df50 | 2008-01-21 13:09:33 +0100 | [diff] [blame] | 963 | cr0 |= X86_CR0_TS; |
Joerg Roedel | 6b390b6 | 2008-01-29 13:01:27 +0100 | [diff] [blame] | 964 | } |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 965 | set: |
| 966 | /* |
| 967 | * re-enable caching here because the QEMU bios |
| 968 | * does not do it - this results in some delay at |
| 969 | * reboot |
| 970 | */ |
| 971 | cr0 &= ~(X86_CR0_CD | X86_CR0_NW); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 972 | svm->vmcb->save.cr0 = cr0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
| 976 | { |
Joerg Roedel | 6394b64 | 2008-04-09 14:15:29 +0200 | [diff] [blame] | 977 | unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE; |
Joerg Roedel | e5eab0c | 2008-09-09 19:11:51 +0200 | [diff] [blame] | 978 | unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4; |
| 979 | |
| 980 | if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE)) |
| 981 | force_new_asid(vcpu); |
Joerg Roedel | 6394b64 | 2008-04-09 14:15:29 +0200 | [diff] [blame] | 982 | |
Joerg Roedel | ec07726 | 2008-04-09 14:15:28 +0200 | [diff] [blame] | 983 | vcpu->arch.cr4 = cr4; |
| 984 | if (!npt_enabled) |
| 985 | cr4 |= X86_CR4_PAE; |
Joerg Roedel | 6394b64 | 2008-04-09 14:15:29 +0200 | [diff] [blame] | 986 | cr4 |= host_cr4_mce; |
Joerg Roedel | ec07726 | 2008-04-09 14:15:28 +0200 | [diff] [blame] | 987 | to_svm(vcpu)->vmcb->save.cr4 = cr4; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | static void svm_set_segment(struct kvm_vcpu *vcpu, |
| 991 | struct kvm_segment *var, int seg) |
| 992 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 993 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 994 | struct vmcb_seg *s = svm_seg(vcpu, seg); |
| 995 | |
| 996 | s->base = var->base; |
| 997 | s->limit = var->limit; |
| 998 | s->selector = var->selector; |
| 999 | if (var->unusable) |
| 1000 | s->attrib = 0; |
| 1001 | else { |
| 1002 | s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK); |
| 1003 | s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT; |
| 1004 | s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT; |
| 1005 | s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT; |
| 1006 | s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT; |
| 1007 | s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT; |
| 1008 | s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT; |
| 1009 | s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT; |
| 1010 | } |
| 1011 | if (seg == VCPU_SREG_CS) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1012 | svm->vmcb->save.cpl |
| 1013 | = (svm->vmcb->save.cs.attrib |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1014 | >> SVM_SELECTOR_DPL_SHIFT) & 3; |
| 1015 | |
| 1016 | } |
| 1017 | |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1018 | static void update_db_intercept(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1019 | { |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1020 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1021 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1022 | svm->vmcb->control.intercept_exceptions &= |
| 1023 | ~((1 << DB_VECTOR) | (1 << BP_VECTOR)); |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1024 | |
| 1025 | if (vcpu->arch.singlestep) |
| 1026 | svm->vmcb->control.intercept_exceptions |= (1 << DB_VECTOR); |
| 1027 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1028 | if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) { |
| 1029 | if (vcpu->guest_debug & |
| 1030 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 1031 | svm->vmcb->control.intercept_exceptions |= |
| 1032 | 1 << DB_VECTOR; |
| 1033 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
| 1034 | svm->vmcb->control.intercept_exceptions |= |
| 1035 | 1 << BP_VECTOR; |
| 1036 | } else |
| 1037 | vcpu->guest_debug = 0; |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | static int svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg) |
| 1041 | { |
| 1042 | int old_debug = vcpu->guest_debug; |
| 1043 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1044 | |
| 1045 | vcpu->guest_debug = dbg->control; |
| 1046 | |
| 1047 | update_db_intercept(vcpu); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1048 | |
Jan Kiszka | ae675ef | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1049 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) |
| 1050 | svm->vmcb->save.dr7 = dbg->arch.debugreg[7]; |
| 1051 | else |
| 1052 | svm->vmcb->save.dr7 = vcpu->arch.dr7; |
| 1053 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1054 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
| 1055 | svm->vmcb->save.rflags |= X86_EFLAGS_TF | X86_EFLAGS_RF; |
| 1056 | else if (old_debug & KVM_GUESTDBG_SINGLESTEP) |
| 1057 | svm->vmcb->save.rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF); |
| 1058 | |
| 1059 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | static void load_host_msrs(struct kvm_vcpu *vcpu) |
| 1063 | { |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 1064 | #ifdef CONFIG_X86_64 |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1065 | wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base); |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 1066 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | static void save_host_msrs(struct kvm_vcpu *vcpu) |
| 1070 | { |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 1071 | #ifdef CONFIG_X86_64 |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1072 | rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base); |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 1073 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1074 | } |
| 1075 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1076 | static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *svm_data) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1077 | { |
| 1078 | if (svm_data->next_asid > svm_data->max_asid) { |
| 1079 | ++svm_data->asid_generation; |
| 1080 | svm_data->next_asid = 1; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1081 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1082 | } |
| 1083 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1084 | svm->asid_generation = svm_data->asid_generation; |
| 1085 | svm->vmcb->control.asid = svm_data->next_asid++; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1086 | } |
| 1087 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1088 | static unsigned long svm_get_dr(struct kvm_vcpu *vcpu, int dr) |
| 1089 | { |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1090 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1091 | unsigned long val; |
| 1092 | |
| 1093 | switch (dr) { |
| 1094 | case 0 ... 3: |
| 1095 | val = vcpu->arch.db[dr]; |
| 1096 | break; |
| 1097 | case 6: |
| 1098 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) |
| 1099 | val = vcpu->arch.dr6; |
| 1100 | else |
| 1101 | val = svm->vmcb->save.dr6; |
| 1102 | break; |
| 1103 | case 7: |
| 1104 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) |
| 1105 | val = vcpu->arch.dr7; |
| 1106 | else |
| 1107 | val = svm->vmcb->save.dr7; |
| 1108 | break; |
| 1109 | default: |
| 1110 | val = 0; |
| 1111 | } |
| 1112 | |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 1113 | KVMTRACE_2D(DR_READ, vcpu, (u32)dr, (u32)val, handler); |
| 1114 | return val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | static void svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value, |
| 1118 | int *exception) |
| 1119 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1120 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1121 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1122 | KVMTRACE_2D(DR_WRITE, vcpu, (u32)dr, (u32)value, handler); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1123 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1124 | *exception = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1125 | |
| 1126 | switch (dr) { |
| 1127 | case 0 ... 3: |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1128 | vcpu->arch.db[dr] = value; |
| 1129 | if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) |
| 1130 | vcpu->arch.eff_db[dr] = value; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1131 | return; |
| 1132 | case 4 ... 5: |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1133 | if (vcpu->arch.cr4 & X86_CR4_DE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1134 | *exception = UD_VECTOR; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1135 | return; |
| 1136 | case 6: |
| 1137 | if (value & 0xffffffff00000000ULL) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1138 | *exception = GP_VECTOR; |
| 1139 | return; |
| 1140 | } |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1141 | vcpu->arch.dr6 = (value & DR6_VOLATILE) | DR6_FIXED_1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1142 | return; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1143 | case 7: |
| 1144 | if (value & 0xffffffff00000000ULL) { |
| 1145 | *exception = GP_VECTOR; |
| 1146 | return; |
| 1147 | } |
| 1148 | vcpu->arch.dr7 = (value & DR7_VOLATILE) | DR7_FIXED_1; |
| 1149 | if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) { |
| 1150 | svm->vmcb->save.dr7 = vcpu->arch.dr7; |
| 1151 | vcpu->arch.switch_db_regs = (value & DR7_BP_EN_MASK); |
| 1152 | } |
| 1153 | return; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1154 | default: |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1155 | /* FIXME: Possible case? */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1156 | printk(KERN_DEBUG "%s: unexpected dr %u\n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 1157 | __func__, dr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1158 | *exception = UD_VECTOR; |
| 1159 | return; |
| 1160 | } |
| 1161 | } |
| 1162 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1163 | static int pf_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1164 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1165 | u64 fault_address; |
| 1166 | u32 error_code; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1167 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1168 | fault_address = svm->vmcb->control.exit_info_2; |
| 1169 | error_code = svm->vmcb->control.exit_info_1; |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 1170 | |
| 1171 | if (!npt_enabled) |
| 1172 | KVMTRACE_3D(PAGE_FAULT, &svm->vcpu, error_code, |
| 1173 | (u32)fault_address, (u32)(fault_address >> 32), |
| 1174 | handler); |
Joerg Roedel | d2ebb41 | 2008-04-30 17:56:04 +0200 | [diff] [blame] | 1175 | else |
| 1176 | KVMTRACE_3D(TDP_FAULT, &svm->vcpu, error_code, |
| 1177 | (u32)fault_address, (u32)(fault_address >> 32), |
| 1178 | handler); |
Joerg Roedel | 44874f8 | 2008-08-27 14:18:43 +0200 | [diff] [blame] | 1179 | /* |
| 1180 | * FIXME: Tis shouldn't be necessary here, but there is a flush |
| 1181 | * missing in the MMU code. Until we find this bug, flush the |
| 1182 | * complete TLB here on an NPF |
| 1183 | */ |
| 1184 | if (npt_enabled) |
| 1185 | svm_flush_tlb(&svm->vcpu); |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 1186 | else { |
Gleb Natapov | 3298b75 | 2009-05-11 13:35:46 +0300 | [diff] [blame] | 1187 | if (kvm_event_needs_reinjection(&svm->vcpu)) |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 1188 | kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address); |
| 1189 | } |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 1190 | return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1191 | } |
| 1192 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1193 | static int db_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) |
| 1194 | { |
| 1195 | if (!(svm->vcpu.guest_debug & |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1196 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) && |
| 1197 | !svm->vcpu.arch.singlestep) { |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1198 | kvm_queue_exception(&svm->vcpu, DB_VECTOR); |
| 1199 | return 1; |
| 1200 | } |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1201 | |
| 1202 | if (svm->vcpu.arch.singlestep) { |
| 1203 | svm->vcpu.arch.singlestep = false; |
| 1204 | if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) |
| 1205 | svm->vmcb->save.rflags &= |
| 1206 | ~(X86_EFLAGS_TF | X86_EFLAGS_RF); |
| 1207 | update_db_intercept(&svm->vcpu); |
| 1208 | } |
| 1209 | |
| 1210 | if (svm->vcpu.guest_debug & |
| 1211 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)){ |
| 1212 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
| 1213 | kvm_run->debug.arch.pc = |
| 1214 | svm->vmcb->save.cs.base + svm->vmcb->save.rip; |
| 1215 | kvm_run->debug.arch.exception = DB_VECTOR; |
| 1216 | return 0; |
| 1217 | } |
| 1218 | |
| 1219 | return 1; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | static int bp_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) |
| 1223 | { |
| 1224 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
| 1225 | kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip; |
| 1226 | kvm_run->debug.arch.exception = BP_VECTOR; |
| 1227 | return 0; |
| 1228 | } |
| 1229 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1230 | static int ud_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) |
| 1231 | { |
| 1232 | int er; |
| 1233 | |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 1234 | er = emulate_instruction(&svm->vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1235 | if (er != EMULATE_DONE) |
Avi Kivity | 7ee5d940 | 2007-11-25 15:22:50 +0200 | [diff] [blame] | 1236 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1237 | return 1; |
| 1238 | } |
| 1239 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1240 | static int nm_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) |
Anthony Liguori | 7807fa6 | 2007-04-23 09:17:21 -0500 | [diff] [blame] | 1241 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1242 | svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1243 | if (!(svm->vcpu.arch.cr0 & X86_CR0_TS)) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1244 | svm->vmcb->save.cr0 &= ~X86_CR0_TS; |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1245 | svm->vcpu.fpu_active = 1; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1246 | |
| 1247 | return 1; |
Anthony Liguori | 7807fa6 | 2007-04-23 09:17:21 -0500 | [diff] [blame] | 1248 | } |
| 1249 | |
Joerg Roedel | 53371b5 | 2008-04-09 14:15:30 +0200 | [diff] [blame] | 1250 | static int mc_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) |
| 1251 | { |
| 1252 | /* |
| 1253 | * On an #MC intercept the MCE handler is not called automatically in |
| 1254 | * the host. So do it by hand here. |
| 1255 | */ |
| 1256 | asm volatile ( |
| 1257 | "int $0x12\n"); |
| 1258 | /* not sure if we ever come back to this point */ |
| 1259 | |
| 1260 | return 1; |
| 1261 | } |
| 1262 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1263 | static int shutdown_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 1264 | { |
| 1265 | /* |
| 1266 | * VMCB is undefined after a SHUTDOWN intercept |
| 1267 | * so reinitialize it. |
| 1268 | */ |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1269 | clear_page(svm->vmcb); |
Joerg Roedel | e6101a9 | 2008-02-13 18:58:45 +0100 | [diff] [blame] | 1270 | init_vmcb(svm); |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 1271 | |
| 1272 | kvm_run->exit_reason = KVM_EXIT_SHUTDOWN; |
| 1273 | return 0; |
| 1274 | } |
| 1275 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1276 | static int io_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1277 | { |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1278 | u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */ |
Jan Kiszka | 34c33d1 | 2009-02-08 13:28:15 +0100 | [diff] [blame] | 1279 | int size, in, string; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1280 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1281 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1282 | ++svm->vcpu.stat.io_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1283 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1284 | svm->next_rip = svm->vmcb->control.exit_info_2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1285 | |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 1286 | string = (io_info & SVM_IOIO_STR_MASK) != 0; |
| 1287 | |
| 1288 | if (string) { |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 1289 | if (emulate_instruction(&svm->vcpu, |
| 1290 | kvm_run, 0, 0, 0) == EMULATE_DO_MMIO) |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 1291 | return 0; |
| 1292 | return 1; |
| 1293 | } |
| 1294 | |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1295 | in = (io_info & SVM_IOIO_TYPE_MASK) != 0; |
| 1296 | port = io_info >> 16; |
| 1297 | size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1298 | |
Guillaume Thouvenin | e93f36b | 2008-10-28 10:51:30 +0100 | [diff] [blame] | 1299 | skip_emulated_instruction(&svm->vcpu); |
Laurent Vivier | 3090dd7 | 2007-08-05 10:43:32 +0300 | [diff] [blame] | 1300 | return kvm_emulate_pio(&svm->vcpu, kvm_run, in, size, port); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1301 | } |
| 1302 | |
Joerg Roedel | c47f098 | 2008-04-30 17:56:00 +0200 | [diff] [blame] | 1303 | static int nmi_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) |
| 1304 | { |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 1305 | KVMTRACE_0D(NMI, &svm->vcpu, handler); |
Joerg Roedel | c47f098 | 2008-04-30 17:56:00 +0200 | [diff] [blame] | 1306 | return 1; |
| 1307 | } |
| 1308 | |
Joerg Roedel | a069805 | 2008-04-30 17:56:01 +0200 | [diff] [blame] | 1309 | static int intr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) |
| 1310 | { |
| 1311 | ++svm->vcpu.stat.irq_exits; |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 1312 | KVMTRACE_0D(INTR, &svm->vcpu, handler); |
Joerg Roedel | a069805 | 2008-04-30 17:56:01 +0200 | [diff] [blame] | 1313 | return 1; |
| 1314 | } |
| 1315 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1316 | static int nop_on_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1317 | { |
| 1318 | return 1; |
| 1319 | } |
| 1320 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1321 | static int halt_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1322 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1323 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 1; |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1324 | skip_emulated_instruction(&svm->vcpu); |
| 1325 | return kvm_emulate_halt(&svm->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1326 | } |
| 1327 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1328 | static int vmmcall_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) |
Avi Kivity | 02e235b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1329 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1330 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1331 | skip_emulated_instruction(&svm->vcpu); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1332 | kvm_emulate_hypercall(&svm->vcpu); |
| 1333 | return 1; |
Avi Kivity | 02e235b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1334 | } |
| 1335 | |
Alexander Graf | c072542 | 2008-11-25 20:17:03 +0100 | [diff] [blame] | 1336 | static int nested_svm_check_permissions(struct vcpu_svm *svm) |
| 1337 | { |
| 1338 | if (!(svm->vcpu.arch.shadow_efer & EFER_SVME) |
| 1339 | || !is_paging(&svm->vcpu)) { |
| 1340 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
| 1341 | return 1; |
| 1342 | } |
| 1343 | |
| 1344 | if (svm->vmcb->save.cpl) { |
| 1345 | kvm_inject_gp(&svm->vcpu, 0); |
| 1346 | return 1; |
| 1347 | } |
| 1348 | |
| 1349 | return 0; |
| 1350 | } |
| 1351 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1352 | static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr, |
| 1353 | bool has_error_code, u32 error_code) |
| 1354 | { |
| 1355 | if (is_nested(svm)) { |
| 1356 | svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr; |
| 1357 | svm->vmcb->control.exit_code_hi = 0; |
| 1358 | svm->vmcb->control.exit_info_1 = error_code; |
| 1359 | svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2; |
| 1360 | if (nested_svm_exit_handled(svm, false)) { |
| 1361 | nsvm_printk("VMexit -> EXCP 0x%x\n", nr); |
| 1362 | |
| 1363 | nested_svm_vmexit(svm); |
| 1364 | return 1; |
| 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | return 0; |
| 1369 | } |
| 1370 | |
| 1371 | static inline int nested_svm_intr(struct vcpu_svm *svm) |
| 1372 | { |
| 1373 | if (is_nested(svm)) { |
| 1374 | if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK)) |
| 1375 | return 0; |
| 1376 | |
| 1377 | if (!(svm->vcpu.arch.hflags & HF_HIF_MASK)) |
| 1378 | return 0; |
| 1379 | |
| 1380 | svm->vmcb->control.exit_code = SVM_EXIT_INTR; |
| 1381 | |
| 1382 | if (nested_svm_exit_handled(svm, false)) { |
| 1383 | nsvm_printk("VMexit -> INTR\n"); |
| 1384 | nested_svm_vmexit(svm); |
| 1385 | return 1; |
| 1386 | } |
| 1387 | } |
| 1388 | |
| 1389 | return 0; |
| 1390 | } |
| 1391 | |
Alexander Graf | c072542 | 2008-11-25 20:17:03 +0100 | [diff] [blame] | 1392 | static struct page *nested_svm_get_page(struct vcpu_svm *svm, u64 gpa) |
| 1393 | { |
| 1394 | struct page *page; |
| 1395 | |
| 1396 | down_read(¤t->mm->mmap_sem); |
| 1397 | page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT); |
| 1398 | up_read(¤t->mm->mmap_sem); |
| 1399 | |
| 1400 | if (is_error_page(page)) { |
| 1401 | printk(KERN_INFO "%s: could not find page at 0x%llx\n", |
| 1402 | __func__, gpa); |
| 1403 | kvm_release_page_clean(page); |
| 1404 | kvm_inject_gp(&svm->vcpu, 0); |
| 1405 | return NULL; |
| 1406 | } |
| 1407 | return page; |
| 1408 | } |
| 1409 | |
| 1410 | static int nested_svm_do(struct vcpu_svm *svm, |
| 1411 | u64 arg1_gpa, u64 arg2_gpa, void *opaque, |
| 1412 | int (*handler)(struct vcpu_svm *svm, |
| 1413 | void *arg1, |
| 1414 | void *arg2, |
| 1415 | void *opaque)) |
| 1416 | { |
| 1417 | struct page *arg1_page; |
| 1418 | struct page *arg2_page = NULL; |
| 1419 | void *arg1; |
| 1420 | void *arg2 = NULL; |
| 1421 | int retval; |
| 1422 | |
| 1423 | arg1_page = nested_svm_get_page(svm, arg1_gpa); |
| 1424 | if(arg1_page == NULL) |
| 1425 | return 1; |
| 1426 | |
| 1427 | if (arg2_gpa) { |
| 1428 | arg2_page = nested_svm_get_page(svm, arg2_gpa); |
| 1429 | if(arg2_page == NULL) { |
| 1430 | kvm_release_page_clean(arg1_page); |
| 1431 | return 1; |
| 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | arg1 = kmap_atomic(arg1_page, KM_USER0); |
| 1436 | if (arg2_gpa) |
| 1437 | arg2 = kmap_atomic(arg2_page, KM_USER1); |
| 1438 | |
| 1439 | retval = handler(svm, arg1, arg2, opaque); |
| 1440 | |
| 1441 | kunmap_atomic(arg1, KM_USER0); |
| 1442 | if (arg2_gpa) |
| 1443 | kunmap_atomic(arg2, KM_USER1); |
| 1444 | |
| 1445 | kvm_release_page_dirty(arg1_page); |
| 1446 | if (arg2_gpa) |
| 1447 | kvm_release_page_dirty(arg2_page); |
| 1448 | |
| 1449 | return retval; |
| 1450 | } |
| 1451 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1452 | static int nested_svm_exit_handled_real(struct vcpu_svm *svm, |
| 1453 | void *arg1, |
| 1454 | void *arg2, |
| 1455 | void *opaque) |
| 1456 | { |
| 1457 | struct vmcb *nested_vmcb = (struct vmcb *)arg1; |
| 1458 | bool kvm_overrides = *(bool *)opaque; |
| 1459 | u32 exit_code = svm->vmcb->control.exit_code; |
| 1460 | |
| 1461 | if (kvm_overrides) { |
| 1462 | switch (exit_code) { |
| 1463 | case SVM_EXIT_INTR: |
| 1464 | case SVM_EXIT_NMI: |
| 1465 | return 0; |
| 1466 | /* For now we are always handling NPFs when using them */ |
| 1467 | case SVM_EXIT_NPF: |
| 1468 | if (npt_enabled) |
| 1469 | return 0; |
| 1470 | break; |
| 1471 | /* When we're shadowing, trap PFs */ |
| 1472 | case SVM_EXIT_EXCP_BASE + PF_VECTOR: |
| 1473 | if (!npt_enabled) |
| 1474 | return 0; |
| 1475 | break; |
| 1476 | default: |
| 1477 | break; |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | switch (exit_code) { |
| 1482 | case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR8: { |
| 1483 | u32 cr_bits = 1 << (exit_code - SVM_EXIT_READ_CR0); |
| 1484 | if (nested_vmcb->control.intercept_cr_read & cr_bits) |
| 1485 | return 1; |
| 1486 | break; |
| 1487 | } |
| 1488 | case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR8: { |
| 1489 | u32 cr_bits = 1 << (exit_code - SVM_EXIT_WRITE_CR0); |
| 1490 | if (nested_vmcb->control.intercept_cr_write & cr_bits) |
| 1491 | return 1; |
| 1492 | break; |
| 1493 | } |
| 1494 | case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR7: { |
| 1495 | u32 dr_bits = 1 << (exit_code - SVM_EXIT_READ_DR0); |
| 1496 | if (nested_vmcb->control.intercept_dr_read & dr_bits) |
| 1497 | return 1; |
| 1498 | break; |
| 1499 | } |
| 1500 | case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR7: { |
| 1501 | u32 dr_bits = 1 << (exit_code - SVM_EXIT_WRITE_DR0); |
| 1502 | if (nested_vmcb->control.intercept_dr_write & dr_bits) |
| 1503 | return 1; |
| 1504 | break; |
| 1505 | } |
| 1506 | case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: { |
| 1507 | u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE); |
| 1508 | if (nested_vmcb->control.intercept_exceptions & excp_bits) |
| 1509 | return 1; |
| 1510 | break; |
| 1511 | } |
| 1512 | default: { |
| 1513 | u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR); |
| 1514 | nsvm_printk("exit code: 0x%x\n", exit_code); |
| 1515 | if (nested_vmcb->control.intercept & exit_bits) |
| 1516 | return 1; |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | return 0; |
| 1521 | } |
| 1522 | |
| 1523 | static int nested_svm_exit_handled_msr(struct vcpu_svm *svm, |
| 1524 | void *arg1, void *arg2, |
| 1525 | void *opaque) |
| 1526 | { |
| 1527 | struct vmcb *nested_vmcb = (struct vmcb *)arg1; |
| 1528 | u8 *msrpm = (u8 *)arg2; |
| 1529 | u32 t0, t1; |
| 1530 | u32 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX]; |
| 1531 | u32 param = svm->vmcb->control.exit_info_1 & 1; |
| 1532 | |
| 1533 | if (!(nested_vmcb->control.intercept & (1ULL << INTERCEPT_MSR_PROT))) |
| 1534 | return 0; |
| 1535 | |
| 1536 | switch(msr) { |
| 1537 | case 0 ... 0x1fff: |
| 1538 | t0 = (msr * 2) % 8; |
| 1539 | t1 = msr / 8; |
| 1540 | break; |
| 1541 | case 0xc0000000 ... 0xc0001fff: |
| 1542 | t0 = (8192 + msr - 0xc0000000) * 2; |
| 1543 | t1 = (t0 / 8); |
| 1544 | t0 %= 8; |
| 1545 | break; |
| 1546 | case 0xc0010000 ... 0xc0011fff: |
| 1547 | t0 = (16384 + msr - 0xc0010000) * 2; |
| 1548 | t1 = (t0 / 8); |
| 1549 | t0 %= 8; |
| 1550 | break; |
| 1551 | default: |
| 1552 | return 1; |
| 1553 | break; |
| 1554 | } |
| 1555 | if (msrpm[t1] & ((1 << param) << t0)) |
| 1556 | return 1; |
| 1557 | |
| 1558 | return 0; |
| 1559 | } |
| 1560 | |
| 1561 | static int nested_svm_exit_handled(struct vcpu_svm *svm, bool kvm_override) |
| 1562 | { |
| 1563 | bool k = kvm_override; |
| 1564 | |
| 1565 | switch (svm->vmcb->control.exit_code) { |
| 1566 | case SVM_EXIT_MSR: |
| 1567 | return nested_svm_do(svm, svm->nested_vmcb, |
| 1568 | svm->nested_vmcb_msrpm, NULL, |
| 1569 | nested_svm_exit_handled_msr); |
| 1570 | default: break; |
| 1571 | } |
| 1572 | |
| 1573 | return nested_svm_do(svm, svm->nested_vmcb, 0, &k, |
| 1574 | nested_svm_exit_handled_real); |
| 1575 | } |
| 1576 | |
| 1577 | static int nested_svm_vmexit_real(struct vcpu_svm *svm, void *arg1, |
| 1578 | void *arg2, void *opaque) |
| 1579 | { |
| 1580 | struct vmcb *nested_vmcb = (struct vmcb *)arg1; |
| 1581 | struct vmcb *hsave = svm->hsave; |
| 1582 | u64 nested_save[] = { nested_vmcb->save.cr0, |
| 1583 | nested_vmcb->save.cr3, |
| 1584 | nested_vmcb->save.cr4, |
| 1585 | nested_vmcb->save.efer, |
| 1586 | nested_vmcb->control.intercept_cr_read, |
| 1587 | nested_vmcb->control.intercept_cr_write, |
| 1588 | nested_vmcb->control.intercept_dr_read, |
| 1589 | nested_vmcb->control.intercept_dr_write, |
| 1590 | nested_vmcb->control.intercept_exceptions, |
| 1591 | nested_vmcb->control.intercept, |
| 1592 | nested_vmcb->control.msrpm_base_pa, |
| 1593 | nested_vmcb->control.iopm_base_pa, |
| 1594 | nested_vmcb->control.tsc_offset }; |
| 1595 | |
| 1596 | /* Give the current vmcb to the guest */ |
| 1597 | memcpy(nested_vmcb, svm->vmcb, sizeof(struct vmcb)); |
| 1598 | nested_vmcb->save.cr0 = nested_save[0]; |
| 1599 | if (!npt_enabled) |
| 1600 | nested_vmcb->save.cr3 = nested_save[1]; |
| 1601 | nested_vmcb->save.cr4 = nested_save[2]; |
| 1602 | nested_vmcb->save.efer = nested_save[3]; |
| 1603 | nested_vmcb->control.intercept_cr_read = nested_save[4]; |
| 1604 | nested_vmcb->control.intercept_cr_write = nested_save[5]; |
| 1605 | nested_vmcb->control.intercept_dr_read = nested_save[6]; |
| 1606 | nested_vmcb->control.intercept_dr_write = nested_save[7]; |
| 1607 | nested_vmcb->control.intercept_exceptions = nested_save[8]; |
| 1608 | nested_vmcb->control.intercept = nested_save[9]; |
| 1609 | nested_vmcb->control.msrpm_base_pa = nested_save[10]; |
| 1610 | nested_vmcb->control.iopm_base_pa = nested_save[11]; |
| 1611 | nested_vmcb->control.tsc_offset = nested_save[12]; |
| 1612 | |
| 1613 | /* We always set V_INTR_MASKING and remember the old value in hflags */ |
| 1614 | if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK)) |
| 1615 | nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK; |
| 1616 | |
| 1617 | if ((nested_vmcb->control.int_ctl & V_IRQ_MASK) && |
| 1618 | (nested_vmcb->control.int_vector)) { |
| 1619 | nsvm_printk("WARNING: IRQ 0x%x still enabled on #VMEXIT\n", |
| 1620 | nested_vmcb->control.int_vector); |
| 1621 | } |
| 1622 | |
| 1623 | /* Restore the original control entries */ |
| 1624 | svm->vmcb->control = hsave->control; |
| 1625 | |
| 1626 | /* Kill any pending exceptions */ |
| 1627 | if (svm->vcpu.arch.exception.pending == true) |
| 1628 | nsvm_printk("WARNING: Pending Exception\n"); |
| 1629 | svm->vcpu.arch.exception.pending = false; |
| 1630 | |
| 1631 | /* Restore selected save entries */ |
| 1632 | svm->vmcb->save.es = hsave->save.es; |
| 1633 | svm->vmcb->save.cs = hsave->save.cs; |
| 1634 | svm->vmcb->save.ss = hsave->save.ss; |
| 1635 | svm->vmcb->save.ds = hsave->save.ds; |
| 1636 | svm->vmcb->save.gdtr = hsave->save.gdtr; |
| 1637 | svm->vmcb->save.idtr = hsave->save.idtr; |
| 1638 | svm->vmcb->save.rflags = hsave->save.rflags; |
| 1639 | svm_set_efer(&svm->vcpu, hsave->save.efer); |
| 1640 | svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE); |
| 1641 | svm_set_cr4(&svm->vcpu, hsave->save.cr4); |
| 1642 | if (npt_enabled) { |
| 1643 | svm->vmcb->save.cr3 = hsave->save.cr3; |
| 1644 | svm->vcpu.arch.cr3 = hsave->save.cr3; |
| 1645 | } else { |
| 1646 | kvm_set_cr3(&svm->vcpu, hsave->save.cr3); |
| 1647 | } |
| 1648 | kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax); |
| 1649 | kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp); |
| 1650 | kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip); |
| 1651 | svm->vmcb->save.dr7 = 0; |
| 1652 | svm->vmcb->save.cpl = 0; |
| 1653 | svm->vmcb->control.exit_int_info = 0; |
| 1654 | |
| 1655 | svm->vcpu.arch.hflags &= ~HF_GIF_MASK; |
| 1656 | /* Exit nested SVM mode */ |
| 1657 | svm->nested_vmcb = 0; |
| 1658 | |
| 1659 | return 0; |
| 1660 | } |
| 1661 | |
| 1662 | static int nested_svm_vmexit(struct vcpu_svm *svm) |
| 1663 | { |
| 1664 | nsvm_printk("VMexit\n"); |
| 1665 | if (nested_svm_do(svm, svm->nested_vmcb, 0, |
| 1666 | NULL, nested_svm_vmexit_real)) |
| 1667 | return 1; |
| 1668 | |
| 1669 | kvm_mmu_reset_context(&svm->vcpu); |
| 1670 | kvm_mmu_load(&svm->vcpu); |
| 1671 | |
| 1672 | return 0; |
| 1673 | } |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1674 | |
| 1675 | static int nested_svm_vmrun_msrpm(struct vcpu_svm *svm, void *arg1, |
| 1676 | void *arg2, void *opaque) |
| 1677 | { |
| 1678 | int i; |
| 1679 | u32 *nested_msrpm = (u32*)arg1; |
| 1680 | for (i=0; i< PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER) / 4; i++) |
| 1681 | svm->nested_msrpm[i] = svm->msrpm[i] | nested_msrpm[i]; |
| 1682 | svm->vmcb->control.msrpm_base_pa = __pa(svm->nested_msrpm); |
| 1683 | |
| 1684 | return 0; |
| 1685 | } |
| 1686 | |
| 1687 | static int nested_svm_vmrun(struct vcpu_svm *svm, void *arg1, |
| 1688 | void *arg2, void *opaque) |
| 1689 | { |
| 1690 | struct vmcb *nested_vmcb = (struct vmcb *)arg1; |
| 1691 | struct vmcb *hsave = svm->hsave; |
| 1692 | |
| 1693 | /* nested_vmcb is our indicator if nested SVM is activated */ |
| 1694 | svm->nested_vmcb = svm->vmcb->save.rax; |
| 1695 | |
| 1696 | /* Clear internal status */ |
| 1697 | svm->vcpu.arch.exception.pending = false; |
| 1698 | |
| 1699 | /* Save the old vmcb, so we don't need to pick what we save, but |
| 1700 | can restore everything when a VMEXIT occurs */ |
| 1701 | memcpy(hsave, svm->vmcb, sizeof(struct vmcb)); |
| 1702 | /* We need to remember the original CR3 in the SPT case */ |
| 1703 | if (!npt_enabled) |
| 1704 | hsave->save.cr3 = svm->vcpu.arch.cr3; |
| 1705 | hsave->save.cr4 = svm->vcpu.arch.cr4; |
| 1706 | hsave->save.rip = svm->next_rip; |
| 1707 | |
| 1708 | if (svm->vmcb->save.rflags & X86_EFLAGS_IF) |
| 1709 | svm->vcpu.arch.hflags |= HF_HIF_MASK; |
| 1710 | else |
| 1711 | svm->vcpu.arch.hflags &= ~HF_HIF_MASK; |
| 1712 | |
| 1713 | /* Load the nested guest state */ |
| 1714 | svm->vmcb->save.es = nested_vmcb->save.es; |
| 1715 | svm->vmcb->save.cs = nested_vmcb->save.cs; |
| 1716 | svm->vmcb->save.ss = nested_vmcb->save.ss; |
| 1717 | svm->vmcb->save.ds = nested_vmcb->save.ds; |
| 1718 | svm->vmcb->save.gdtr = nested_vmcb->save.gdtr; |
| 1719 | svm->vmcb->save.idtr = nested_vmcb->save.idtr; |
| 1720 | svm->vmcb->save.rflags = nested_vmcb->save.rflags; |
| 1721 | svm_set_efer(&svm->vcpu, nested_vmcb->save.efer); |
| 1722 | svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0); |
| 1723 | svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4); |
| 1724 | if (npt_enabled) { |
| 1725 | svm->vmcb->save.cr3 = nested_vmcb->save.cr3; |
| 1726 | svm->vcpu.arch.cr3 = nested_vmcb->save.cr3; |
| 1727 | } else { |
| 1728 | kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3); |
| 1729 | kvm_mmu_reset_context(&svm->vcpu); |
| 1730 | } |
| 1731 | svm->vmcb->save.cr2 = nested_vmcb->save.cr2; |
| 1732 | kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax); |
| 1733 | kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp); |
| 1734 | kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip); |
| 1735 | /* In case we don't even reach vcpu_run, the fields are not updated */ |
| 1736 | svm->vmcb->save.rax = nested_vmcb->save.rax; |
| 1737 | svm->vmcb->save.rsp = nested_vmcb->save.rsp; |
| 1738 | svm->vmcb->save.rip = nested_vmcb->save.rip; |
| 1739 | svm->vmcb->save.dr7 = nested_vmcb->save.dr7; |
| 1740 | svm->vmcb->save.dr6 = nested_vmcb->save.dr6; |
| 1741 | svm->vmcb->save.cpl = nested_vmcb->save.cpl; |
| 1742 | |
| 1743 | /* We don't want a nested guest to be more powerful than the guest, |
| 1744 | so all intercepts are ORed */ |
| 1745 | svm->vmcb->control.intercept_cr_read |= |
| 1746 | nested_vmcb->control.intercept_cr_read; |
| 1747 | svm->vmcb->control.intercept_cr_write |= |
| 1748 | nested_vmcb->control.intercept_cr_write; |
| 1749 | svm->vmcb->control.intercept_dr_read |= |
| 1750 | nested_vmcb->control.intercept_dr_read; |
| 1751 | svm->vmcb->control.intercept_dr_write |= |
| 1752 | nested_vmcb->control.intercept_dr_write; |
| 1753 | svm->vmcb->control.intercept_exceptions |= |
| 1754 | nested_vmcb->control.intercept_exceptions; |
| 1755 | |
| 1756 | svm->vmcb->control.intercept |= nested_vmcb->control.intercept; |
| 1757 | |
| 1758 | svm->nested_vmcb_msrpm = nested_vmcb->control.msrpm_base_pa; |
| 1759 | |
| 1760 | force_new_asid(&svm->vcpu); |
| 1761 | svm->vmcb->control.exit_int_info = nested_vmcb->control.exit_int_info; |
| 1762 | svm->vmcb->control.exit_int_info_err = nested_vmcb->control.exit_int_info_err; |
| 1763 | svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK; |
| 1764 | if (nested_vmcb->control.int_ctl & V_IRQ_MASK) { |
| 1765 | nsvm_printk("nSVM Injecting Interrupt: 0x%x\n", |
| 1766 | nested_vmcb->control.int_ctl); |
| 1767 | } |
| 1768 | if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK) |
| 1769 | svm->vcpu.arch.hflags |= HF_VINTR_MASK; |
| 1770 | else |
| 1771 | svm->vcpu.arch.hflags &= ~HF_VINTR_MASK; |
| 1772 | |
| 1773 | nsvm_printk("nSVM exit_int_info: 0x%x | int_state: 0x%x\n", |
| 1774 | nested_vmcb->control.exit_int_info, |
| 1775 | nested_vmcb->control.int_state); |
| 1776 | |
| 1777 | svm->vmcb->control.int_vector = nested_vmcb->control.int_vector; |
| 1778 | svm->vmcb->control.int_state = nested_vmcb->control.int_state; |
| 1779 | svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset; |
| 1780 | if (nested_vmcb->control.event_inj & SVM_EVTINJ_VALID) |
| 1781 | nsvm_printk("Injecting Event: 0x%x\n", |
| 1782 | nested_vmcb->control.event_inj); |
| 1783 | svm->vmcb->control.event_inj = nested_vmcb->control.event_inj; |
| 1784 | svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err; |
| 1785 | |
| 1786 | svm->vcpu.arch.hflags |= HF_GIF_MASK; |
| 1787 | |
| 1788 | return 0; |
| 1789 | } |
| 1790 | |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 1791 | static int nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb) |
| 1792 | { |
| 1793 | to_vmcb->save.fs = from_vmcb->save.fs; |
| 1794 | to_vmcb->save.gs = from_vmcb->save.gs; |
| 1795 | to_vmcb->save.tr = from_vmcb->save.tr; |
| 1796 | to_vmcb->save.ldtr = from_vmcb->save.ldtr; |
| 1797 | to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base; |
| 1798 | to_vmcb->save.star = from_vmcb->save.star; |
| 1799 | to_vmcb->save.lstar = from_vmcb->save.lstar; |
| 1800 | to_vmcb->save.cstar = from_vmcb->save.cstar; |
| 1801 | to_vmcb->save.sfmask = from_vmcb->save.sfmask; |
| 1802 | to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs; |
| 1803 | to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp; |
| 1804 | to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip; |
| 1805 | |
| 1806 | return 1; |
| 1807 | } |
| 1808 | |
| 1809 | static int nested_svm_vmload(struct vcpu_svm *svm, void *nested_vmcb, |
| 1810 | void *arg2, void *opaque) |
| 1811 | { |
| 1812 | return nested_svm_vmloadsave((struct vmcb *)nested_vmcb, svm->vmcb); |
| 1813 | } |
| 1814 | |
| 1815 | static int nested_svm_vmsave(struct vcpu_svm *svm, void *nested_vmcb, |
| 1816 | void *arg2, void *opaque) |
| 1817 | { |
| 1818 | return nested_svm_vmloadsave(svm->vmcb, (struct vmcb *)nested_vmcb); |
| 1819 | } |
| 1820 | |
| 1821 | static int vmload_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) |
| 1822 | { |
| 1823 | if (nested_svm_check_permissions(svm)) |
| 1824 | return 1; |
| 1825 | |
| 1826 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
| 1827 | skip_emulated_instruction(&svm->vcpu); |
| 1828 | |
| 1829 | nested_svm_do(svm, svm->vmcb->save.rax, 0, NULL, nested_svm_vmload); |
| 1830 | |
| 1831 | return 1; |
| 1832 | } |
| 1833 | |
| 1834 | static int vmsave_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) |
| 1835 | { |
| 1836 | if (nested_svm_check_permissions(svm)) |
| 1837 | return 1; |
| 1838 | |
| 1839 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
| 1840 | skip_emulated_instruction(&svm->vcpu); |
| 1841 | |
| 1842 | nested_svm_do(svm, svm->vmcb->save.rax, 0, NULL, nested_svm_vmsave); |
| 1843 | |
| 1844 | return 1; |
| 1845 | } |
| 1846 | |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1847 | static int vmrun_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) |
| 1848 | { |
| 1849 | nsvm_printk("VMrun\n"); |
| 1850 | if (nested_svm_check_permissions(svm)) |
| 1851 | return 1; |
| 1852 | |
| 1853 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
| 1854 | skip_emulated_instruction(&svm->vcpu); |
| 1855 | |
| 1856 | if (nested_svm_do(svm, svm->vmcb->save.rax, 0, |
| 1857 | NULL, nested_svm_vmrun)) |
| 1858 | return 1; |
| 1859 | |
| 1860 | if (nested_svm_do(svm, svm->nested_vmcb_msrpm, 0, |
| 1861 | NULL, nested_svm_vmrun_msrpm)) |
| 1862 | return 1; |
| 1863 | |
| 1864 | return 1; |
| 1865 | } |
| 1866 | |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 1867 | static int stgi_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) |
| 1868 | { |
| 1869 | if (nested_svm_check_permissions(svm)) |
| 1870 | return 1; |
| 1871 | |
| 1872 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
| 1873 | skip_emulated_instruction(&svm->vcpu); |
| 1874 | |
| 1875 | svm->vcpu.arch.hflags |= HF_GIF_MASK; |
| 1876 | |
| 1877 | return 1; |
| 1878 | } |
| 1879 | |
| 1880 | static int clgi_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) |
| 1881 | { |
| 1882 | if (nested_svm_check_permissions(svm)) |
| 1883 | return 1; |
| 1884 | |
| 1885 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
| 1886 | skip_emulated_instruction(&svm->vcpu); |
| 1887 | |
| 1888 | svm->vcpu.arch.hflags &= ~HF_GIF_MASK; |
| 1889 | |
| 1890 | /* After a CLGI no interrupts should come */ |
| 1891 | svm_clear_vintr(svm); |
| 1892 | svm->vmcb->control.int_ctl &= ~V_IRQ_MASK; |
| 1893 | |
| 1894 | return 1; |
| 1895 | } |
| 1896 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1897 | static int invalid_op_interception(struct vcpu_svm *svm, |
| 1898 | struct kvm_run *kvm_run) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1899 | { |
Avi Kivity | 7ee5d940 | 2007-11-25 15:22:50 +0200 | [diff] [blame] | 1900 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1901 | return 1; |
| 1902 | } |
| 1903 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1904 | static int task_switch_interception(struct vcpu_svm *svm, |
| 1905 | struct kvm_run *kvm_run) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1906 | { |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 1907 | u16 tss_selector; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 1908 | int reason; |
| 1909 | int int_type = svm->vmcb->control.exit_int_info & |
| 1910 | SVM_EXITINTINFO_TYPE_MASK; |
Gleb Natapov | 8317c29 | 2009-04-12 13:37:02 +0300 | [diff] [blame] | 1911 | int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK; |
Gleb Natapov | fe8e7f8 | 2009-04-23 17:03:48 +0300 | [diff] [blame] | 1912 | uint32_t type = |
| 1913 | svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK; |
| 1914 | uint32_t idt_v = |
| 1915 | svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 1916 | |
| 1917 | tss_selector = (u16)svm->vmcb->control.exit_info_1; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 1918 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 1919 | if (svm->vmcb->control.exit_info_2 & |
| 1920 | (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET)) |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 1921 | reason = TASK_SWITCH_IRET; |
| 1922 | else if (svm->vmcb->control.exit_info_2 & |
| 1923 | (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP)) |
| 1924 | reason = TASK_SWITCH_JMP; |
Gleb Natapov | fe8e7f8 | 2009-04-23 17:03:48 +0300 | [diff] [blame] | 1925 | else if (idt_v) |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 1926 | reason = TASK_SWITCH_GATE; |
| 1927 | else |
| 1928 | reason = TASK_SWITCH_CALL; |
| 1929 | |
Gleb Natapov | fe8e7f8 | 2009-04-23 17:03:48 +0300 | [diff] [blame] | 1930 | if (reason == TASK_SWITCH_GATE) { |
| 1931 | switch (type) { |
| 1932 | case SVM_EXITINTINFO_TYPE_NMI: |
| 1933 | svm->vcpu.arch.nmi_injected = false; |
| 1934 | break; |
| 1935 | case SVM_EXITINTINFO_TYPE_EXEPT: |
| 1936 | kvm_clear_exception_queue(&svm->vcpu); |
| 1937 | break; |
| 1938 | case SVM_EXITINTINFO_TYPE_INTR: |
| 1939 | kvm_clear_interrupt_queue(&svm->vcpu); |
| 1940 | break; |
| 1941 | default: |
| 1942 | break; |
| 1943 | } |
| 1944 | } |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 1945 | |
Gleb Natapov | 8317c29 | 2009-04-12 13:37:02 +0300 | [diff] [blame] | 1946 | if (reason != TASK_SWITCH_GATE || |
| 1947 | int_type == SVM_EXITINTINFO_TYPE_SOFT || |
| 1948 | (int_type == SVM_EXITINTINFO_TYPE_EXEPT && |
Gleb Natapov | f629cf8 | 2009-05-11 13:35:49 +0300 | [diff] [blame] | 1949 | (int_vec == OF_VECTOR || int_vec == BP_VECTOR))) |
| 1950 | skip_emulated_instruction(&svm->vcpu); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 1951 | |
| 1952 | return kvm_task_switch(&svm->vcpu, tss_selector, reason); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1953 | } |
| 1954 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1955 | static int cpuid_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1956 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1957 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 2; |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1958 | kvm_emulate_cpuid(&svm->vcpu); |
Avi Kivity | 06465c5 | 2007-02-28 20:46:53 +0200 | [diff] [blame] | 1959 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1960 | } |
| 1961 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 1962 | static int iret_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) |
| 1963 | { |
| 1964 | ++svm->vcpu.stat.nmi_window_exits; |
| 1965 | svm->vmcb->control.intercept &= ~(1UL << INTERCEPT_IRET); |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1966 | svm->vcpu.arch.hflags |= HF_IRET_MASK; |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 1967 | return 1; |
| 1968 | } |
| 1969 | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 1970 | static int invlpg_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) |
| 1971 | { |
| 1972 | if (emulate_instruction(&svm->vcpu, kvm_run, 0, 0, 0) != EMULATE_DONE) |
| 1973 | pr_unimpl(&svm->vcpu, "%s: failed\n", __func__); |
| 1974 | return 1; |
| 1975 | } |
| 1976 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1977 | static int emulate_on_interception(struct vcpu_svm *svm, |
| 1978 | struct kvm_run *kvm_run) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1979 | { |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 1980 | if (emulate_instruction(&svm->vcpu, NULL, 0, 0, 0) != EMULATE_DONE) |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 1981 | pr_unimpl(&svm->vcpu, "%s: failed\n", __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1982 | return 1; |
| 1983 | } |
| 1984 | |
Joerg Roedel | 1d07543 | 2007-12-06 21:02:25 +0100 | [diff] [blame] | 1985 | static int cr8_write_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) |
| 1986 | { |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 1987 | u8 cr8_prev = kvm_get_cr8(&svm->vcpu); |
| 1988 | /* instruction emulation calls kvm_set_cr8() */ |
Joerg Roedel | 1d07543 | 2007-12-06 21:02:25 +0100 | [diff] [blame] | 1989 | emulate_instruction(&svm->vcpu, NULL, 0, 0, 0); |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 1990 | if (irqchip_in_kernel(svm->vcpu.kvm)) { |
| 1991 | svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK; |
Joerg Roedel | 1d07543 | 2007-12-06 21:02:25 +0100 | [diff] [blame] | 1992 | return 1; |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 1993 | } |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 1994 | if (cr8_prev <= kvm_get_cr8(&svm->vcpu)) |
| 1995 | return 1; |
Joerg Roedel | 1d07543 | 2007-12-06 21:02:25 +0100 | [diff] [blame] | 1996 | kvm_run->exit_reason = KVM_EXIT_SET_TPR; |
| 1997 | return 0; |
| 1998 | } |
| 1999 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2000 | static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data) |
| 2001 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2002 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2003 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2004 | switch (ecx) { |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 2005 | case MSR_IA32_TSC: { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2006 | u64 tsc; |
| 2007 | |
| 2008 | rdtscll(tsc); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2009 | *data = svm->vmcb->control.tsc_offset + tsc; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2010 | break; |
| 2011 | } |
Avi Kivity | 0e859ca | 2006-12-22 01:05:08 -0800 | [diff] [blame] | 2012 | case MSR_K6_STAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2013 | *data = svm->vmcb->save.star; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2014 | break; |
Avi Kivity | 0e859ca | 2006-12-22 01:05:08 -0800 | [diff] [blame] | 2015 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2016 | case MSR_LSTAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2017 | *data = svm->vmcb->save.lstar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2018 | break; |
| 2019 | case MSR_CSTAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2020 | *data = svm->vmcb->save.cstar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2021 | break; |
| 2022 | case MSR_KERNEL_GS_BASE: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2023 | *data = svm->vmcb->save.kernel_gs_base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2024 | break; |
| 2025 | case MSR_SYSCALL_MASK: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2026 | *data = svm->vmcb->save.sfmask; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2027 | break; |
| 2028 | #endif |
| 2029 | case MSR_IA32_SYSENTER_CS: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2030 | *data = svm->vmcb->save.sysenter_cs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2031 | break; |
| 2032 | case MSR_IA32_SYSENTER_EIP: |
Andre Przywara | 017cb99 | 2009-05-28 11:56:31 +0200 | [diff] [blame] | 2033 | *data = svm->sysenter_eip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2034 | break; |
| 2035 | case MSR_IA32_SYSENTER_ESP: |
Andre Przywara | 017cb99 | 2009-05-28 11:56:31 +0200 | [diff] [blame] | 2036 | *data = svm->sysenter_esp; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2037 | break; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 2038 | /* Nobody will change the following 5 values in the VMCB so |
| 2039 | we can safely return them on rdmsr. They will always be 0 |
| 2040 | until LBRV is implemented. */ |
| 2041 | case MSR_IA32_DEBUGCTLMSR: |
| 2042 | *data = svm->vmcb->save.dbgctl; |
| 2043 | break; |
| 2044 | case MSR_IA32_LASTBRANCHFROMIP: |
| 2045 | *data = svm->vmcb->save.br_from; |
| 2046 | break; |
| 2047 | case MSR_IA32_LASTBRANCHTOIP: |
| 2048 | *data = svm->vmcb->save.br_to; |
| 2049 | break; |
| 2050 | case MSR_IA32_LASTINTFROMIP: |
| 2051 | *data = svm->vmcb->save.last_excp_from; |
| 2052 | break; |
| 2053 | case MSR_IA32_LASTINTTOIP: |
| 2054 | *data = svm->vmcb->save.last_excp_to; |
| 2055 | break; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 2056 | case MSR_VM_HSAVE_PA: |
| 2057 | *data = svm->hsave_msr; |
| 2058 | break; |
Joerg Roedel | eb6f302 | 2008-11-25 20:17:09 +0100 | [diff] [blame] | 2059 | case MSR_VM_CR: |
| 2060 | *data = 0; |
| 2061 | break; |
Alexander Graf | c8a73f1 | 2009-01-05 16:02:47 +0100 | [diff] [blame] | 2062 | case MSR_IA32_UCODE_REV: |
| 2063 | *data = 0x01000065; |
| 2064 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2065 | default: |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2066 | return kvm_get_msr_common(vcpu, ecx, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2067 | } |
| 2068 | return 0; |
| 2069 | } |
| 2070 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2071 | static int rdmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2072 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2073 | u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2074 | u64 data; |
| 2075 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2076 | if (svm_get_msr(&svm->vcpu, ecx, &data)) |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 2077 | kvm_inject_gp(&svm->vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2078 | else { |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 2079 | KVMTRACE_3D(MSR_READ, &svm->vcpu, ecx, (u32)data, |
| 2080 | (u32)(data >> 32), handler); |
| 2081 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2082 | svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2083 | svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2084 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 2; |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2085 | skip_emulated_instruction(&svm->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2086 | } |
| 2087 | return 1; |
| 2088 | } |
| 2089 | |
| 2090 | static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data) |
| 2091 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2092 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2093 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2094 | switch (ecx) { |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 2095 | case MSR_IA32_TSC: { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2096 | u64 tsc; |
| 2097 | |
| 2098 | rdtscll(tsc); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2099 | svm->vmcb->control.tsc_offset = data - tsc; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2100 | break; |
| 2101 | } |
Avi Kivity | 0e859ca | 2006-12-22 01:05:08 -0800 | [diff] [blame] | 2102 | case MSR_K6_STAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2103 | svm->vmcb->save.star = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2104 | break; |
Robert P. J. Day | 49b14f2 | 2007-01-29 13:19:50 -0800 | [diff] [blame] | 2105 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2106 | case MSR_LSTAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2107 | svm->vmcb->save.lstar = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2108 | break; |
| 2109 | case MSR_CSTAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2110 | svm->vmcb->save.cstar = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2111 | break; |
| 2112 | case MSR_KERNEL_GS_BASE: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2113 | svm->vmcb->save.kernel_gs_base = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2114 | break; |
| 2115 | case MSR_SYSCALL_MASK: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2116 | svm->vmcb->save.sfmask = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2117 | break; |
| 2118 | #endif |
| 2119 | case MSR_IA32_SYSENTER_CS: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2120 | svm->vmcb->save.sysenter_cs = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2121 | break; |
| 2122 | case MSR_IA32_SYSENTER_EIP: |
Andre Przywara | 017cb99 | 2009-05-28 11:56:31 +0200 | [diff] [blame] | 2123 | svm->sysenter_eip = data; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2124 | svm->vmcb->save.sysenter_eip = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2125 | break; |
| 2126 | case MSR_IA32_SYSENTER_ESP: |
Andre Przywara | 017cb99 | 2009-05-28 11:56:31 +0200 | [diff] [blame] | 2127 | svm->sysenter_esp = data; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2128 | svm->vmcb->save.sysenter_esp = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2129 | break; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 2130 | case MSR_IA32_DEBUGCTLMSR: |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 2131 | if (!svm_has(SVM_FEATURE_LBRV)) { |
| 2132 | pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 2133 | __func__, data); |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 2134 | break; |
| 2135 | } |
| 2136 | if (data & DEBUGCTL_RESERVED_BITS) |
| 2137 | return 1; |
| 2138 | |
| 2139 | svm->vmcb->save.dbgctl = data; |
| 2140 | if (data & (1ULL<<0)) |
| 2141 | svm_enable_lbrv(svm); |
| 2142 | else |
| 2143 | svm_disable_lbrv(svm); |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 2144 | break; |
Joerg Roedel | 62b9aba | 2007-12-11 15:36:57 +0100 | [diff] [blame] | 2145 | case MSR_K7_EVNTSEL0: |
| 2146 | case MSR_K7_EVNTSEL1: |
| 2147 | case MSR_K7_EVNTSEL2: |
| 2148 | case MSR_K7_EVNTSEL3: |
Chris Lalancette | 14ae51b | 2008-05-05 13:05:16 -0400 | [diff] [blame] | 2149 | case MSR_K7_PERFCTR0: |
| 2150 | case MSR_K7_PERFCTR1: |
| 2151 | case MSR_K7_PERFCTR2: |
| 2152 | case MSR_K7_PERFCTR3: |
Joerg Roedel | 62b9aba | 2007-12-11 15:36:57 +0100 | [diff] [blame] | 2153 | /* |
Chris Lalancette | 14ae51b | 2008-05-05 13:05:16 -0400 | [diff] [blame] | 2154 | * Just discard all writes to the performance counters; this |
| 2155 | * should keep both older linux and windows 64-bit guests |
| 2156 | * happy |
Joerg Roedel | 62b9aba | 2007-12-11 15:36:57 +0100 | [diff] [blame] | 2157 | */ |
Chris Lalancette | 14ae51b | 2008-05-05 13:05:16 -0400 | [diff] [blame] | 2158 | pr_unimpl(vcpu, "unimplemented perfctr wrmsr: 0x%x data 0x%llx\n", ecx, data); |
| 2159 | |
Joerg Roedel | 62b9aba | 2007-12-11 15:36:57 +0100 | [diff] [blame] | 2160 | break; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 2161 | case MSR_VM_HSAVE_PA: |
| 2162 | svm->hsave_msr = data; |
| 2163 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2164 | default: |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2165 | return kvm_set_msr_common(vcpu, ecx, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2166 | } |
| 2167 | return 0; |
| 2168 | } |
| 2169 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2170 | static int wrmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2171 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2172 | u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX]; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2173 | u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2174 | | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32); |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 2175 | |
| 2176 | KVMTRACE_3D(MSR_WRITE, &svm->vcpu, ecx, (u32)data, (u32)(data >> 32), |
| 2177 | handler); |
| 2178 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2179 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 2; |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2180 | if (svm_set_msr(&svm->vcpu, ecx, data)) |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 2181 | kvm_inject_gp(&svm->vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2182 | else |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2183 | skip_emulated_instruction(&svm->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2184 | return 1; |
| 2185 | } |
| 2186 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2187 | static int msr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2188 | { |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2189 | if (svm->vmcb->control.exit_info_1) |
| 2190 | return wrmsr_interception(svm, kvm_run); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2191 | else |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2192 | return rdmsr_interception(svm, kvm_run); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2193 | } |
| 2194 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2195 | static int interrupt_window_interception(struct vcpu_svm *svm, |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2196 | struct kvm_run *kvm_run) |
| 2197 | { |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 2198 | KVMTRACE_0D(PEND_INTR, &svm->vcpu, handler); |
| 2199 | |
Alexander Graf | f0b8505 | 2008-11-25 20:17:01 +0100 | [diff] [blame] | 2200 | svm_clear_vintr(svm); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2201 | svm->vmcb->control.int_ctl &= ~V_IRQ_MASK; |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2202 | /* |
| 2203 | * If the user space waits to inject interrupts, exit as soon as |
| 2204 | * possible |
| 2205 | */ |
Gleb Natapov | 8061823 | 2009-04-21 17:44:56 +0300 | [diff] [blame] | 2206 | if (!irqchip_in_kernel(svm->vcpu.kvm) && |
| 2207 | kvm_run->request_interrupt_window && |
| 2208 | !kvm_cpu_has_interrupt(&svm->vcpu)) { |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2209 | ++svm->vcpu.stat.irq_window_exits; |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2210 | kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; |
| 2211 | return 0; |
| 2212 | } |
| 2213 | |
| 2214 | return 1; |
| 2215 | } |
| 2216 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2217 | static int (*svm_exit_handlers[])(struct vcpu_svm *svm, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2218 | struct kvm_run *kvm_run) = { |
| 2219 | [SVM_EXIT_READ_CR0] = emulate_on_interception, |
| 2220 | [SVM_EXIT_READ_CR3] = emulate_on_interception, |
| 2221 | [SVM_EXIT_READ_CR4] = emulate_on_interception, |
Avi Kivity | 80a8119 | 2007-12-06 19:50:00 +0200 | [diff] [blame] | 2222 | [SVM_EXIT_READ_CR8] = emulate_on_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2223 | /* for now: */ |
| 2224 | [SVM_EXIT_WRITE_CR0] = emulate_on_interception, |
| 2225 | [SVM_EXIT_WRITE_CR3] = emulate_on_interception, |
| 2226 | [SVM_EXIT_WRITE_CR4] = emulate_on_interception, |
Joerg Roedel | 1d07543 | 2007-12-06 21:02:25 +0100 | [diff] [blame] | 2227 | [SVM_EXIT_WRITE_CR8] = cr8_write_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2228 | [SVM_EXIT_READ_DR0] = emulate_on_interception, |
| 2229 | [SVM_EXIT_READ_DR1] = emulate_on_interception, |
| 2230 | [SVM_EXIT_READ_DR2] = emulate_on_interception, |
| 2231 | [SVM_EXIT_READ_DR3] = emulate_on_interception, |
| 2232 | [SVM_EXIT_WRITE_DR0] = emulate_on_interception, |
| 2233 | [SVM_EXIT_WRITE_DR1] = emulate_on_interception, |
| 2234 | [SVM_EXIT_WRITE_DR2] = emulate_on_interception, |
| 2235 | [SVM_EXIT_WRITE_DR3] = emulate_on_interception, |
| 2236 | [SVM_EXIT_WRITE_DR5] = emulate_on_interception, |
| 2237 | [SVM_EXIT_WRITE_DR7] = emulate_on_interception, |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2238 | [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception, |
| 2239 | [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception, |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 2240 | [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2241 | [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception, |
Anthony Liguori | 7807fa6 | 2007-04-23 09:17:21 -0500 | [diff] [blame] | 2242 | [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception, |
Joerg Roedel | 53371b5 | 2008-04-09 14:15:30 +0200 | [diff] [blame] | 2243 | [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception, |
Joerg Roedel | a069805 | 2008-04-30 17:56:01 +0200 | [diff] [blame] | 2244 | [SVM_EXIT_INTR] = intr_interception, |
Joerg Roedel | c47f098 | 2008-04-30 17:56:00 +0200 | [diff] [blame] | 2245 | [SVM_EXIT_NMI] = nmi_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2246 | [SVM_EXIT_SMI] = nop_on_interception, |
| 2247 | [SVM_EXIT_INIT] = nop_on_interception, |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2248 | [SVM_EXIT_VINTR] = interrupt_window_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2249 | /* [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, */ |
| 2250 | [SVM_EXIT_CPUID] = cpuid_interception, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2251 | [SVM_EXIT_IRET] = iret_interception, |
Avi Kivity | cf5a94d | 2007-10-28 16:11:58 +0200 | [diff] [blame] | 2252 | [SVM_EXIT_INVD] = emulate_on_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2253 | [SVM_EXIT_HLT] = halt_interception, |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2254 | [SVM_EXIT_INVLPG] = invlpg_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2255 | [SVM_EXIT_INVLPGA] = invalid_op_interception, |
| 2256 | [SVM_EXIT_IOIO] = io_interception, |
| 2257 | [SVM_EXIT_MSR] = msr_interception, |
| 2258 | [SVM_EXIT_TASK_SWITCH] = task_switch_interception, |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 2259 | [SVM_EXIT_SHUTDOWN] = shutdown_interception, |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2260 | [SVM_EXIT_VMRUN] = vmrun_interception, |
Avi Kivity | 02e235b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2261 | [SVM_EXIT_VMMCALL] = vmmcall_interception, |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 2262 | [SVM_EXIT_VMLOAD] = vmload_interception, |
| 2263 | [SVM_EXIT_VMSAVE] = vmsave_interception, |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 2264 | [SVM_EXIT_STGI] = stgi_interception, |
| 2265 | [SVM_EXIT_CLGI] = clgi_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2266 | [SVM_EXIT_SKINIT] = invalid_op_interception, |
Avi Kivity | cf5a94d | 2007-10-28 16:11:58 +0200 | [diff] [blame] | 2267 | [SVM_EXIT_WBINVD] = emulate_on_interception, |
Joerg Roedel | 916ce23 | 2007-03-21 19:47:00 +0100 | [diff] [blame] | 2268 | [SVM_EXIT_MONITOR] = invalid_op_interception, |
| 2269 | [SVM_EXIT_MWAIT] = invalid_op_interception, |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 2270 | [SVM_EXIT_NPF] = pf_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2271 | }; |
| 2272 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2273 | static int handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2274 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2275 | struct vcpu_svm *svm = to_svm(vcpu); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2276 | u32 exit_code = svm->vmcb->control.exit_code; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2277 | |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 2278 | KVMTRACE_3D(VMEXIT, vcpu, exit_code, (u32)svm->vmcb->save.rip, |
| 2279 | (u32)((u64)svm->vmcb->save.rip >> 32), entryexit); |
| 2280 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2281 | if (is_nested(svm)) { |
| 2282 | nsvm_printk("nested handle_exit: 0x%x | 0x%lx | 0x%lx | 0x%lx\n", |
| 2283 | exit_code, svm->vmcb->control.exit_info_1, |
| 2284 | svm->vmcb->control.exit_info_2, svm->vmcb->save.rip); |
| 2285 | if (nested_svm_exit_handled(svm, true)) { |
| 2286 | nested_svm_vmexit(svm); |
| 2287 | nsvm_printk("-> #VMEXIT\n"); |
| 2288 | return 1; |
| 2289 | } |
| 2290 | } |
| 2291 | |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 2292 | if (npt_enabled) { |
| 2293 | int mmu_reload = 0; |
| 2294 | if ((vcpu->arch.cr0 ^ svm->vmcb->save.cr0) & X86_CR0_PG) { |
| 2295 | svm_set_cr0(vcpu, svm->vmcb->save.cr0); |
| 2296 | mmu_reload = 1; |
| 2297 | } |
| 2298 | vcpu->arch.cr0 = svm->vmcb->save.cr0; |
| 2299 | vcpu->arch.cr3 = svm->vmcb->save.cr3; |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 2300 | if (mmu_reload) { |
| 2301 | kvm_mmu_reset_context(vcpu); |
| 2302 | kvm_mmu_load(vcpu); |
| 2303 | } |
| 2304 | } |
| 2305 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2306 | |
| 2307 | if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) { |
| 2308 | kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 2309 | kvm_run->fail_entry.hardware_entry_failure_reason |
| 2310 | = svm->vmcb->control.exit_code; |
| 2311 | return 0; |
| 2312 | } |
| 2313 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2314 | if (is_external_interrupt(svm->vmcb->control.exit_int_info) && |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 2315 | exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR && |
Gleb Natapov | fe8e7f8 | 2009-04-23 17:03:48 +0300 | [diff] [blame] | 2316 | exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2317 | printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x " |
| 2318 | "exit_code 0x%x\n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 2319 | __func__, svm->vmcb->control.exit_int_info, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2320 | exit_code); |
| 2321 | |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 2322 | if (exit_code >= ARRAY_SIZE(svm_exit_handlers) |
Joe Perches | 56919c5 | 2007-11-12 20:06:51 -0800 | [diff] [blame] | 2323 | || !svm_exit_handlers[exit_code]) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2324 | kvm_run->exit_reason = KVM_EXIT_UNKNOWN; |
Avi Kivity | 364b625 | 2007-04-16 14:28:40 +0300 | [diff] [blame] | 2325 | kvm_run->hw.hardware_exit_reason = exit_code; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2326 | return 0; |
| 2327 | } |
| 2328 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2329 | return svm_exit_handlers[exit_code](svm, kvm_run); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2330 | } |
| 2331 | |
| 2332 | static void reload_tss(struct kvm_vcpu *vcpu) |
| 2333 | { |
| 2334 | int cpu = raw_smp_processor_id(); |
| 2335 | |
| 2336 | struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu); |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2337 | svm_data->tss_desc->type = 9; /* available 32/64-bit TSS */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2338 | load_TR_desc(); |
| 2339 | } |
| 2340 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2341 | static void pre_svm_run(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2342 | { |
| 2343 | int cpu = raw_smp_processor_id(); |
| 2344 | |
| 2345 | struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu); |
| 2346 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2347 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING; |
Marcelo Tosatti | 4b656b1 | 2009-07-21 12:47:45 -0300 | [diff] [blame] | 2348 | /* FIXME: handle wraparound of asid_generation */ |
| 2349 | if (svm->asid_generation != svm_data->asid_generation) |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2350 | new_asid(svm, svm_data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2351 | } |
| 2352 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2353 | static void svm_inject_nmi(struct kvm_vcpu *vcpu) |
| 2354 | { |
| 2355 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2356 | |
| 2357 | svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI; |
| 2358 | vcpu->arch.hflags |= HF_NMI_MASK; |
| 2359 | svm->vmcb->control.intercept |= (1UL << INTERCEPT_IRET); |
| 2360 | ++vcpu->stat.nmi_injections; |
| 2361 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2362 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2363 | static inline void svm_inject_irq(struct vcpu_svm *svm, int irq) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2364 | { |
| 2365 | struct vmcb_control_area *control; |
| 2366 | |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 2367 | KVMTRACE_1D(INJ_VIRQ, &svm->vcpu, (u32)irq, handler); |
| 2368 | |
Avi Kivity | fa89a81 | 2008-09-01 15:57:51 +0300 | [diff] [blame] | 2369 | ++svm->vcpu.stat.irq_injections; |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2370 | control = &svm->vmcb->control; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2371 | control->int_vector = irq; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2372 | control->int_ctl &= ~V_INTR_PRIO_MASK; |
| 2373 | control->int_ctl |= V_IRQ_MASK | |
| 2374 | ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT); |
| 2375 | } |
| 2376 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2377 | static void svm_queue_irq(struct kvm_vcpu *vcpu, unsigned nr) |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 2378 | { |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2379 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2380 | |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 2381 | svm->vmcb->control.event_inj = nr | |
| 2382 | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR; |
| 2383 | } |
| 2384 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 2385 | static void svm_set_irq(struct kvm_vcpu *vcpu) |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 2386 | { |
| 2387 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2388 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2389 | nested_svm_intr(svm); |
| 2390 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 2391 | svm_queue_irq(vcpu, vcpu->arch.interrupt.nr); |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 2392 | } |
| 2393 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2394 | static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr) |
| 2395 | { |
| 2396 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2397 | |
| 2398 | if (irr == -1) |
| 2399 | return; |
| 2400 | |
| 2401 | if (tpr >= irr) |
| 2402 | svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK; |
| 2403 | } |
| 2404 | |
| 2405 | static int svm_nmi_allowed(struct kvm_vcpu *vcpu) |
Joerg Roedel | aaacfc9 | 2008-04-16 16:51:18 +0200 | [diff] [blame] | 2406 | { |
| 2407 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2408 | struct vmcb *vmcb = svm->vmcb; |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2409 | return !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) && |
| 2410 | !(svm->vcpu.arch.hflags & HF_NMI_MASK); |
Joerg Roedel | aaacfc9 | 2008-04-16 16:51:18 +0200 | [diff] [blame] | 2411 | } |
| 2412 | |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 2413 | static int svm_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 2414 | { |
| 2415 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2416 | struct vmcb *vmcb = svm->vmcb; |
| 2417 | return (vmcb->save.rflags & X86_EFLAGS_IF) && |
| 2418 | !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) && |
| 2419 | (svm->vcpu.arch.hflags & HF_GIF_MASK); |
| 2420 | } |
| 2421 | |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 2422 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
| 2423 | { |
| 2424 | svm_set_vintr(to_svm(vcpu)); |
| 2425 | svm_inject_irq(to_svm(vcpu), 0x0); |
| 2426 | } |
| 2427 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2428 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2429 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2430 | struct vcpu_svm *svm = to_svm(vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2431 | |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 2432 | if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK)) |
| 2433 | == HF_NMI_MASK) |
| 2434 | return; /* IRET will cause a vm exit */ |
| 2435 | |
| 2436 | /* Something prevents NMI from been injected. Single step over |
| 2437 | possible problem (IRET or exception injection or interrupt |
| 2438 | shadow) */ |
| 2439 | vcpu->arch.singlestep = true; |
| 2440 | svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF); |
| 2441 | update_db_intercept(vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2442 | } |
| 2443 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 2444 | static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 2445 | { |
| 2446 | return 0; |
| 2447 | } |
| 2448 | |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 2449 | static void svm_flush_tlb(struct kvm_vcpu *vcpu) |
| 2450 | { |
| 2451 | force_new_asid(vcpu); |
| 2452 | } |
| 2453 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2454 | static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu) |
| 2455 | { |
| 2456 | } |
| 2457 | |
Joerg Roedel | d7bf822 | 2008-04-16 16:51:17 +0200 | [diff] [blame] | 2458 | static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu) |
| 2459 | { |
| 2460 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2461 | |
| 2462 | if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) { |
| 2463 | int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK; |
Gleb Natapov | 615d519 | 2009-04-21 17:45:05 +0300 | [diff] [blame] | 2464 | kvm_set_cr8(vcpu, cr8); |
Joerg Roedel | d7bf822 | 2008-04-16 16:51:17 +0200 | [diff] [blame] | 2465 | } |
| 2466 | } |
| 2467 | |
Joerg Roedel | 649d686 | 2008-04-16 16:51:15 +0200 | [diff] [blame] | 2468 | static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu) |
| 2469 | { |
| 2470 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2471 | u64 cr8; |
| 2472 | |
Joerg Roedel | 649d686 | 2008-04-16 16:51:15 +0200 | [diff] [blame] | 2473 | cr8 = kvm_get_cr8(vcpu); |
| 2474 | svm->vmcb->control.int_ctl &= ~V_TPR_MASK; |
| 2475 | svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK; |
| 2476 | } |
| 2477 | |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 2478 | static void svm_complete_interrupts(struct vcpu_svm *svm) |
| 2479 | { |
| 2480 | u8 vector; |
| 2481 | int type; |
| 2482 | u32 exitintinfo = svm->vmcb->control.exit_int_info; |
| 2483 | |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 2484 | if (svm->vcpu.arch.hflags & HF_IRET_MASK) |
| 2485 | svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK); |
| 2486 | |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 2487 | svm->vcpu.arch.nmi_injected = false; |
| 2488 | kvm_clear_exception_queue(&svm->vcpu); |
| 2489 | kvm_clear_interrupt_queue(&svm->vcpu); |
| 2490 | |
| 2491 | if (!(exitintinfo & SVM_EXITINTINFO_VALID)) |
| 2492 | return; |
| 2493 | |
| 2494 | vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK; |
| 2495 | type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK; |
| 2496 | |
| 2497 | switch (type) { |
| 2498 | case SVM_EXITINTINFO_TYPE_NMI: |
| 2499 | svm->vcpu.arch.nmi_injected = true; |
| 2500 | break; |
| 2501 | case SVM_EXITINTINFO_TYPE_EXEPT: |
| 2502 | /* In case of software exception do not reinject an exception |
| 2503 | vector, but re-execute and instruction instead */ |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 2504 | if (kvm_exception_is_soft(vector)) |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 2505 | break; |
| 2506 | if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) { |
| 2507 | u32 err = svm->vmcb->control.exit_int_info_err; |
| 2508 | kvm_queue_exception_e(&svm->vcpu, vector, err); |
| 2509 | |
| 2510 | } else |
| 2511 | kvm_queue_exception(&svm->vcpu, vector); |
| 2512 | break; |
| 2513 | case SVM_EXITINTINFO_TYPE_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 2514 | kvm_queue_interrupt(&svm->vcpu, vector, false); |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 2515 | break; |
| 2516 | default: |
| 2517 | break; |
| 2518 | } |
| 2519 | } |
| 2520 | |
Avi Kivity | 80e31d4 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 2521 | #ifdef CONFIG_X86_64 |
| 2522 | #define R "r" |
| 2523 | #else |
| 2524 | #define R "e" |
| 2525 | #endif |
| 2526 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2527 | static void svm_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2528 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2529 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2530 | u16 fs_selector; |
| 2531 | u16 gs_selector; |
| 2532 | u16 ldt_selector; |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 2533 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2534 | svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX]; |
| 2535 | svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP]; |
| 2536 | svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP]; |
| 2537 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2538 | pre_svm_run(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2539 | |
Joerg Roedel | 649d686 | 2008-04-16 16:51:15 +0200 | [diff] [blame] | 2540 | sync_lapic_to_cr8(vcpu); |
| 2541 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2542 | save_host_msrs(vcpu); |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2543 | fs_selector = kvm_read_fs(); |
| 2544 | gs_selector = kvm_read_gs(); |
| 2545 | ldt_selector = kvm_read_ldt(); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2546 | svm->host_cr2 = kvm_read_cr2(); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2547 | if (!is_nested(svm)) |
| 2548 | svm->vmcb->save.cr2 = vcpu->arch.cr2; |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 2549 | /* required for live migration with NPT */ |
| 2550 | if (npt_enabled) |
| 2551 | svm->vmcb->save.cr3 = vcpu->arch.cr3; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2552 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2553 | clgi(); |
| 2554 | |
| 2555 | local_irq_enable(); |
Avi Kivity | 36241b8 | 2006-12-22 01:05:20 -0800 | [diff] [blame] | 2556 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2557 | asm volatile ( |
Avi Kivity | 80e31d4 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 2558 | "push %%"R"bp; \n\t" |
| 2559 | "mov %c[rbx](%[svm]), %%"R"bx \n\t" |
| 2560 | "mov %c[rcx](%[svm]), %%"R"cx \n\t" |
| 2561 | "mov %c[rdx](%[svm]), %%"R"dx \n\t" |
| 2562 | "mov %c[rsi](%[svm]), %%"R"si \n\t" |
| 2563 | "mov %c[rdi](%[svm]), %%"R"di \n\t" |
| 2564 | "mov %c[rbp](%[svm]), %%"R"bp \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2565 | #ifdef CONFIG_X86_64 |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2566 | "mov %c[r8](%[svm]), %%r8 \n\t" |
| 2567 | "mov %c[r9](%[svm]), %%r9 \n\t" |
| 2568 | "mov %c[r10](%[svm]), %%r10 \n\t" |
| 2569 | "mov %c[r11](%[svm]), %%r11 \n\t" |
| 2570 | "mov %c[r12](%[svm]), %%r12 \n\t" |
| 2571 | "mov %c[r13](%[svm]), %%r13 \n\t" |
| 2572 | "mov %c[r14](%[svm]), %%r14 \n\t" |
| 2573 | "mov %c[r15](%[svm]), %%r15 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2574 | #endif |
| 2575 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2576 | /* Enter guest mode */ |
Avi Kivity | 80e31d4 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 2577 | "push %%"R"ax \n\t" |
| 2578 | "mov %c[vmcb](%[svm]), %%"R"ax \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 2579 | __ex(SVM_VMLOAD) "\n\t" |
| 2580 | __ex(SVM_VMRUN) "\n\t" |
| 2581 | __ex(SVM_VMSAVE) "\n\t" |
Avi Kivity | 80e31d4 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 2582 | "pop %%"R"ax \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2583 | |
| 2584 | /* Save guest registers, load host registers */ |
Avi Kivity | 80e31d4 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 2585 | "mov %%"R"bx, %c[rbx](%[svm]) \n\t" |
| 2586 | "mov %%"R"cx, %c[rcx](%[svm]) \n\t" |
| 2587 | "mov %%"R"dx, %c[rdx](%[svm]) \n\t" |
| 2588 | "mov %%"R"si, %c[rsi](%[svm]) \n\t" |
| 2589 | "mov %%"R"di, %c[rdi](%[svm]) \n\t" |
| 2590 | "mov %%"R"bp, %c[rbp](%[svm]) \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2591 | #ifdef CONFIG_X86_64 |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2592 | "mov %%r8, %c[r8](%[svm]) \n\t" |
| 2593 | "mov %%r9, %c[r9](%[svm]) \n\t" |
| 2594 | "mov %%r10, %c[r10](%[svm]) \n\t" |
| 2595 | "mov %%r11, %c[r11](%[svm]) \n\t" |
| 2596 | "mov %%r12, %c[r12](%[svm]) \n\t" |
| 2597 | "mov %%r13, %c[r13](%[svm]) \n\t" |
| 2598 | "mov %%r14, %c[r14](%[svm]) \n\t" |
| 2599 | "mov %%r15, %c[r15](%[svm]) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2600 | #endif |
Avi Kivity | 80e31d4 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 2601 | "pop %%"R"bp" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2602 | : |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2603 | : [svm]"a"(svm), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2604 | [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2605 | [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 2606 | [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 2607 | [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 2608 | [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 2609 | [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 2610 | [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP])) |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2611 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2612 | , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])), |
| 2613 | [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])), |
| 2614 | [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])), |
| 2615 | [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])), |
| 2616 | [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])), |
| 2617 | [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])), |
| 2618 | [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])), |
| 2619 | [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15])) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2620 | #endif |
Laurent Vivier | 54a08c0 | 2007-10-25 14:18:53 +0200 | [diff] [blame] | 2621 | : "cc", "memory" |
Avi Kivity | 80e31d4 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 2622 | , R"bx", R"cx", R"dx", R"si", R"di" |
Laurent Vivier | 54a08c0 | 2007-10-25 14:18:53 +0200 | [diff] [blame] | 2623 | #ifdef CONFIG_X86_64 |
Laurent Vivier | 54a08c0 | 2007-10-25 14:18:53 +0200 | [diff] [blame] | 2624 | , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15" |
| 2625 | #endif |
| 2626 | ); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2627 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2628 | vcpu->arch.cr2 = svm->vmcb->save.cr2; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2629 | vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax; |
| 2630 | vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp; |
| 2631 | vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2632 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2633 | kvm_write_cr2(svm->host_cr2); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2634 | |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2635 | kvm_load_fs(fs_selector); |
| 2636 | kvm_load_gs(gs_selector); |
| 2637 | kvm_load_ldt(ldt_selector); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2638 | load_host_msrs(vcpu); |
| 2639 | |
| 2640 | reload_tss(vcpu); |
| 2641 | |
Avi Kivity | 56ba47d | 2007-11-07 17:14:18 +0200 | [diff] [blame] | 2642 | local_irq_disable(); |
| 2643 | |
| 2644 | stgi(); |
| 2645 | |
Joerg Roedel | d7bf822 | 2008-04-16 16:51:17 +0200 | [diff] [blame] | 2646 | sync_cr8_to_lapic(vcpu); |
| 2647 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2648 | svm->next_rip = 0; |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 2649 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2650 | if (npt_enabled) { |
| 2651 | vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR); |
| 2652 | vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR); |
| 2653 | } |
| 2654 | |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 2655 | svm_complete_interrupts(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2656 | } |
| 2657 | |
Avi Kivity | 80e31d4 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 2658 | #undef R |
| 2659 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2660 | static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root) |
| 2661 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2662 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2663 | |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 2664 | if (npt_enabled) { |
| 2665 | svm->vmcb->control.nested_cr3 = root; |
| 2666 | force_new_asid(vcpu); |
| 2667 | return; |
| 2668 | } |
| 2669 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2670 | svm->vmcb->save.cr3 = root; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2671 | force_new_asid(vcpu); |
Anthony Liguori | 7807fa6 | 2007-04-23 09:17:21 -0500 | [diff] [blame] | 2672 | |
| 2673 | if (vcpu->fpu_active) { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2674 | svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR); |
| 2675 | svm->vmcb->save.cr0 |= X86_CR0_TS; |
Anthony Liguori | 7807fa6 | 2007-04-23 09:17:21 -0500 | [diff] [blame] | 2676 | vcpu->fpu_active = 0; |
| 2677 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2678 | } |
| 2679 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2680 | static int is_disabled(void) |
| 2681 | { |
Joerg Roedel | 6031a61 | 2007-06-22 12:29:50 +0300 | [diff] [blame] | 2682 | u64 vm_cr; |
| 2683 | |
| 2684 | rdmsrl(MSR_VM_CR, vm_cr); |
| 2685 | if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE)) |
| 2686 | return 1; |
| 2687 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2688 | return 0; |
| 2689 | } |
| 2690 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2691 | static void |
| 2692 | svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 2693 | { |
| 2694 | /* |
| 2695 | * Patch in the VMMCALL instruction: |
| 2696 | */ |
| 2697 | hypercall[0] = 0x0f; |
| 2698 | hypercall[1] = 0x01; |
| 2699 | hypercall[2] = 0xd9; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2700 | } |
| 2701 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2702 | static void svm_check_processor_compat(void *rtn) |
| 2703 | { |
| 2704 | *(int *)rtn = 0; |
| 2705 | } |
| 2706 | |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 2707 | static bool svm_cpu_has_accelerated_tpr(void) |
| 2708 | { |
| 2709 | return false; |
| 2710 | } |
| 2711 | |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 2712 | static int get_npt_level(void) |
| 2713 | { |
| 2714 | #ifdef CONFIG_X86_64 |
| 2715 | return PT64_ROOT_LEVEL; |
| 2716 | #else |
| 2717 | return PT32E_ROOT_LEVEL; |
| 2718 | #endif |
| 2719 | } |
| 2720 | |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 2721 | static u64 svm_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] | 2722 | { |
| 2723 | return 0; |
| 2724 | } |
| 2725 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2726 | static struct kvm_x86_ops svm_x86_ops = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2727 | .cpu_has_kvm_support = has_svm, |
| 2728 | .disabled_by_bios = is_disabled, |
| 2729 | .hardware_setup = svm_hardware_setup, |
| 2730 | .hardware_unsetup = svm_hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2731 | .check_processor_compatibility = svm_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2732 | .hardware_enable = svm_hardware_enable, |
| 2733 | .hardware_disable = svm_hardware_disable, |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 2734 | .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2735 | |
| 2736 | .vcpu_create = svm_create_vcpu, |
| 2737 | .vcpu_free = svm_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2738 | .vcpu_reset = svm_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2739 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2740 | .prepare_guest_switch = svm_prepare_guest_switch, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2741 | .vcpu_load = svm_vcpu_load, |
| 2742 | .vcpu_put = svm_vcpu_put, |
| 2743 | |
| 2744 | .set_guest_debug = svm_guest_debug, |
| 2745 | .get_msr = svm_get_msr, |
| 2746 | .set_msr = svm_set_msr, |
| 2747 | .get_segment_base = svm_get_segment_base, |
| 2748 | .get_segment = svm_get_segment, |
| 2749 | .set_segment = svm_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 2750 | .get_cpl = svm_get_cpl, |
Rusty Russell | 1747fb7 | 2007-09-06 01:21:32 +1000 | [diff] [blame] | 2751 | .get_cs_db_l_bits = kvm_get_cs_db_l_bits, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 2752 | .decache_cr4_guest_bits = svm_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2753 | .set_cr0 = svm_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2754 | .set_cr3 = svm_set_cr3, |
| 2755 | .set_cr4 = svm_set_cr4, |
| 2756 | .set_efer = svm_set_efer, |
| 2757 | .get_idt = svm_get_idt, |
| 2758 | .set_idt = svm_set_idt, |
| 2759 | .get_gdt = svm_get_gdt, |
| 2760 | .set_gdt = svm_set_gdt, |
| 2761 | .get_dr = svm_get_dr, |
| 2762 | .set_dr = svm_set_dr, |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2763 | .cache_reg = svm_cache_reg, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2764 | .get_rflags = svm_get_rflags, |
| 2765 | .set_rflags = svm_set_rflags, |
| 2766 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2767 | .tlb_flush = svm_flush_tlb, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2768 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2769 | .run = svm_vcpu_run, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2770 | .handle_exit = handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2771 | .skip_emulated_instruction = skip_emulated_instruction, |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2772 | .set_interrupt_shadow = svm_set_interrupt_shadow, |
| 2773 | .get_interrupt_shadow = svm_get_interrupt_shadow, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2774 | .patch_hypercall = svm_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 2775 | .set_irq = svm_set_irq, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2776 | .set_nmi = svm_inject_nmi, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 2777 | .queue_exception = svm_queue_exception, |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 2778 | .interrupt_allowed = svm_interrupt_allowed, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2779 | .nmi_allowed = svm_nmi_allowed, |
| 2780 | .enable_nmi_window = enable_nmi_window, |
| 2781 | .enable_irq_window = enable_irq_window, |
| 2782 | .update_cr8_intercept = update_cr8_intercept, |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 2783 | |
| 2784 | .set_tss_addr = svm_set_tss_addr, |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 2785 | .get_tdp_level = get_npt_level, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 2786 | .get_mt_mask = svm_get_mt_mask, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2787 | }; |
| 2788 | |
| 2789 | static int __init svm_init(void) |
| 2790 | { |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 2791 | return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm), |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 2792 | THIS_MODULE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2793 | } |
| 2794 | |
| 2795 | static void __exit svm_exit(void) |
| 2796 | { |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 2797 | kvm_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2798 | } |
| 2799 | |
| 2800 | module_init(svm_init) |
| 2801 | module_exit(svm_exit) |