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 | |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 18 | #include "kvm_svm.h" |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 19 | #include "irq.h" |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 20 | #include "mmu.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 21 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 22 | #include <linux/module.h> |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 23 | #include <linux/kernel.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 24 | #include <linux/vmalloc.h> |
| 25 | #include <linux/highmem.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 26 | #include <linux/sched.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 27 | |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 28 | #include <asm/desc.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 29 | |
| 30 | MODULE_AUTHOR("Qumranet"); |
| 31 | MODULE_LICENSE("GPL"); |
| 32 | |
| 33 | #define IOPM_ALLOC_ORDER 2 |
| 34 | #define MSRPM_ALLOC_ORDER 1 |
| 35 | |
| 36 | #define DB_VECTOR 1 |
| 37 | #define UD_VECTOR 6 |
| 38 | #define GP_VECTOR 13 |
| 39 | |
| 40 | #define DR7_GD_MASK (1 << 13) |
| 41 | #define DR6_BD_MASK (1 << 13) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 42 | |
| 43 | #define SEG_TYPE_LDT 2 |
| 44 | #define SEG_TYPE_BUSY_TSS16 3 |
| 45 | |
Joerg Roedel | 80b7706 | 2007-03-30 17:02:14 +0300 | [diff] [blame] | 46 | #define SVM_FEATURE_NPT (1 << 0) |
| 47 | #define SVM_FEATURE_LBRV (1 << 1) |
| 48 | #define SVM_DEATURE_SVML (1 << 2) |
| 49 | |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame^] | 50 | /* enable NPT for AMD64 and X86 with PAE */ |
| 51 | #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE) |
| 52 | static bool npt_enabled = true; |
| 53 | #else |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 54 | static bool npt_enabled = false; |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame^] | 55 | #endif |
Joerg Roedel | 6c7dac7 | 2008-02-07 13:47:40 +0100 | [diff] [blame] | 56 | static int npt = 1; |
| 57 | |
| 58 | module_param(npt, int, S_IRUGO); |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 59 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 60 | static void kvm_reput_irq(struct vcpu_svm *svm); |
| 61 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 62 | static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu) |
| 63 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 64 | return container_of(vcpu, struct vcpu_svm, vcpu); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 65 | } |
| 66 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 67 | unsigned long iopm_base; |
| 68 | unsigned long msrpm_base; |
| 69 | |
| 70 | struct kvm_ldttss_desc { |
| 71 | u16 limit0; |
| 72 | u16 base0; |
| 73 | unsigned base1 : 8, type : 5, dpl : 2, p : 1; |
| 74 | unsigned limit1 : 4, zero0 : 3, g : 1, base2 : 8; |
| 75 | u32 base3; |
| 76 | u32 zero1; |
| 77 | } __attribute__((packed)); |
| 78 | |
| 79 | struct svm_cpu_data { |
| 80 | int cpu; |
| 81 | |
Avi Kivity | 5008fdf | 2007-04-02 13:05:50 +0300 | [diff] [blame] | 82 | u64 asid_generation; |
| 83 | u32 max_asid; |
| 84 | u32 next_asid; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 85 | struct kvm_ldttss_desc *tss_desc; |
| 86 | |
| 87 | struct page *save_area; |
| 88 | }; |
| 89 | |
| 90 | static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data); |
Joerg Roedel | 80b7706 | 2007-03-30 17:02:14 +0300 | [diff] [blame] | 91 | static uint32_t svm_features; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 92 | |
| 93 | struct svm_init_data { |
| 94 | int cpu; |
| 95 | int r; |
| 96 | }; |
| 97 | |
| 98 | static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000}; |
| 99 | |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 100 | #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 101 | #define MSRS_RANGE_SIZE 2048 |
| 102 | #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2) |
| 103 | |
| 104 | #define MAX_INST_SIZE 15 |
| 105 | |
Joerg Roedel | 80b7706 | 2007-03-30 17:02:14 +0300 | [diff] [blame] | 106 | static inline u32 svm_has(u32 feat) |
| 107 | { |
| 108 | return svm_features & feat; |
| 109 | } |
| 110 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 111 | static inline u8 pop_irq(struct kvm_vcpu *vcpu) |
| 112 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 113 | int word_index = __ffs(vcpu->arch.irq_summary); |
| 114 | int bit_index = __ffs(vcpu->arch.irq_pending[word_index]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 115 | int irq = word_index * BITS_PER_LONG + bit_index; |
| 116 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 117 | clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]); |
| 118 | if (!vcpu->arch.irq_pending[word_index]) |
| 119 | clear_bit(word_index, &vcpu->arch.irq_summary); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 120 | return irq; |
| 121 | } |
| 122 | |
| 123 | static inline void push_irq(struct kvm_vcpu *vcpu, u8 irq) |
| 124 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 125 | set_bit(irq, vcpu->arch.irq_pending); |
| 126 | set_bit(irq / BITS_PER_LONG, &vcpu->arch.irq_summary); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static inline void clgi(void) |
| 130 | { |
| 131 | asm volatile (SVM_CLGI); |
| 132 | } |
| 133 | |
| 134 | static inline void stgi(void) |
| 135 | { |
| 136 | asm volatile (SVM_STGI); |
| 137 | } |
| 138 | |
| 139 | static inline void invlpga(unsigned long addr, u32 asid) |
| 140 | { |
| 141 | asm volatile (SVM_INVLPGA :: "a"(addr), "c"(asid)); |
| 142 | } |
| 143 | |
| 144 | static inline unsigned long kvm_read_cr2(void) |
| 145 | { |
| 146 | unsigned long cr2; |
| 147 | |
| 148 | asm volatile ("mov %%cr2, %0" : "=r" (cr2)); |
| 149 | return cr2; |
| 150 | } |
| 151 | |
| 152 | static inline void kvm_write_cr2(unsigned long val) |
| 153 | { |
| 154 | asm volatile ("mov %0, %%cr2" :: "r" (val)); |
| 155 | } |
| 156 | |
| 157 | static inline unsigned long read_dr6(void) |
| 158 | { |
| 159 | unsigned long dr6; |
| 160 | |
| 161 | asm volatile ("mov %%dr6, %0" : "=r" (dr6)); |
| 162 | return dr6; |
| 163 | } |
| 164 | |
| 165 | static inline void write_dr6(unsigned long val) |
| 166 | { |
| 167 | asm volatile ("mov %0, %%dr6" :: "r" (val)); |
| 168 | } |
| 169 | |
| 170 | static inline unsigned long read_dr7(void) |
| 171 | { |
| 172 | unsigned long dr7; |
| 173 | |
| 174 | asm volatile ("mov %%dr7, %0" : "=r" (dr7)); |
| 175 | return dr7; |
| 176 | } |
| 177 | |
| 178 | static inline void write_dr7(unsigned long val) |
| 179 | { |
| 180 | asm volatile ("mov %0, %%dr7" :: "r" (val)); |
| 181 | } |
| 182 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 183 | static inline void force_new_asid(struct kvm_vcpu *vcpu) |
| 184 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 185 | to_svm(vcpu)->asid_generation--; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static inline void flush_guest_tlb(struct kvm_vcpu *vcpu) |
| 189 | { |
| 190 | force_new_asid(vcpu); |
| 191 | } |
| 192 | |
| 193 | static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 194 | { |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame^] | 195 | if (!npt_enabled && !(efer & EFER_LMA)) |
Carlo Marcelo Arenas Belon | 2b5203e | 2007-12-01 06:17:11 -0600 | [diff] [blame] | 196 | efer &= ~EFER_LME; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 197 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 198 | to_svm(vcpu)->vmcb->save.efer = efer | MSR_EFER_SVME_MASK; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 199 | vcpu->arch.shadow_efer = efer; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 202 | static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr, |
| 203 | bool has_error_code, u32 error_code) |
| 204 | { |
| 205 | struct vcpu_svm *svm = to_svm(vcpu); |
| 206 | |
| 207 | svm->vmcb->control.event_inj = nr |
| 208 | | SVM_EVTINJ_VALID |
| 209 | | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0) |
| 210 | | SVM_EVTINJ_TYPE_EXEPT; |
| 211 | svm->vmcb->control.event_inj_err = error_code; |
| 212 | } |
| 213 | |
| 214 | static bool svm_exception_injected(struct kvm_vcpu *vcpu) |
| 215 | { |
| 216 | struct vcpu_svm *svm = to_svm(vcpu); |
| 217 | |
| 218 | return !(svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID); |
| 219 | } |
| 220 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 221 | static int is_external_interrupt(u32 info) |
| 222 | { |
| 223 | info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID; |
| 224 | return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR); |
| 225 | } |
| 226 | |
| 227 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 228 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 229 | struct vcpu_svm *svm = to_svm(vcpu); |
| 230 | |
| 231 | if (!svm->next_rip) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 232 | printk(KERN_DEBUG "%s: NOP\n", __FUNCTION__); |
| 233 | return; |
| 234 | } |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 235 | if (svm->next_rip - svm->vmcb->save.rip > MAX_INST_SIZE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 236 | printk(KERN_ERR "%s: ip 0x%llx next 0x%llx\n", |
| 237 | __FUNCTION__, |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 238 | svm->vmcb->save.rip, |
| 239 | svm->next_rip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 240 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 241 | vcpu->arch.rip = svm->vmcb->save.rip = svm->next_rip; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 242 | svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK; |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 243 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 244 | vcpu->arch.interrupt_window_open = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | static int has_svm(void) |
| 248 | { |
| 249 | uint32_t eax, ebx, ecx, edx; |
| 250 | |
Avi Kivity | 1e88546 | 2006-12-29 16:49:34 -0800 | [diff] [blame] | 251 | if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 252 | printk(KERN_INFO "has_svm: not amd\n"); |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | cpuid(0x80000000, &eax, &ebx, &ecx, &edx); |
| 257 | if (eax < SVM_CPUID_FUNC) { |
| 258 | printk(KERN_INFO "has_svm: can't execute cpuid_8000000a\n"); |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | cpuid(0x80000001, &eax, &ebx, &ecx, &edx); |
| 263 | if (!(ecx & (1 << SVM_CPUID_FEATURE_SHIFT))) { |
| 264 | printk(KERN_DEBUG "has_svm: svm not available\n"); |
| 265 | return 0; |
| 266 | } |
| 267 | return 1; |
| 268 | } |
| 269 | |
| 270 | static void svm_hardware_disable(void *garbage) |
| 271 | { |
| 272 | struct svm_cpu_data *svm_data |
| 273 | = per_cpu(svm_data, raw_smp_processor_id()); |
| 274 | |
| 275 | if (svm_data) { |
| 276 | uint64_t efer; |
| 277 | |
| 278 | wrmsrl(MSR_VM_HSAVE_PA, 0); |
| 279 | rdmsrl(MSR_EFER, efer); |
| 280 | wrmsrl(MSR_EFER, efer & ~MSR_EFER_SVME_MASK); |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 281 | per_cpu(svm_data, raw_smp_processor_id()) = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 282 | __free_page(svm_data->save_area); |
| 283 | kfree(svm_data); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | static void svm_hardware_enable(void *garbage) |
| 288 | { |
| 289 | |
| 290 | struct svm_cpu_data *svm_data; |
| 291 | uint64_t efer; |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 292 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 293 | struct desc_ptr gdt_descr; |
| 294 | #else |
Glauber de Oliveira Costa | 6b68f01 | 2008-01-30 13:31:12 +0100 | [diff] [blame] | 295 | struct desc_ptr gdt_descr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 296 | #endif |
| 297 | struct desc_struct *gdt; |
| 298 | int me = raw_smp_processor_id(); |
| 299 | |
| 300 | if (!has_svm()) { |
| 301 | printk(KERN_ERR "svm_cpu_init: err EOPNOTSUPP on %d\n", me); |
| 302 | return; |
| 303 | } |
| 304 | svm_data = per_cpu(svm_data, me); |
| 305 | |
| 306 | if (!svm_data) { |
| 307 | printk(KERN_ERR "svm_cpu_init: svm_data is NULL on %d\n", |
| 308 | me); |
| 309 | return; |
| 310 | } |
| 311 | |
| 312 | svm_data->asid_generation = 1; |
| 313 | svm_data->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1; |
| 314 | svm_data->next_asid = svm_data->max_asid + 1; |
| 315 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 316 | asm volatile ("sgdt %0" : "=m"(gdt_descr)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 317 | gdt = (struct desc_struct *)gdt_descr.address; |
| 318 | svm_data->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS); |
| 319 | |
| 320 | rdmsrl(MSR_EFER, efer); |
| 321 | wrmsrl(MSR_EFER, efer | MSR_EFER_SVME_MASK); |
| 322 | |
| 323 | wrmsrl(MSR_VM_HSAVE_PA, |
| 324 | page_to_pfn(svm_data->save_area) << PAGE_SHIFT); |
| 325 | } |
| 326 | |
| 327 | static int svm_cpu_init(int cpu) |
| 328 | { |
| 329 | struct svm_cpu_data *svm_data; |
| 330 | int r; |
| 331 | |
| 332 | svm_data = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL); |
| 333 | if (!svm_data) |
| 334 | return -ENOMEM; |
| 335 | svm_data->cpu = cpu; |
| 336 | svm_data->save_area = alloc_page(GFP_KERNEL); |
| 337 | r = -ENOMEM; |
| 338 | if (!svm_data->save_area) |
| 339 | goto err_1; |
| 340 | |
| 341 | per_cpu(svm_data, cpu) = svm_data; |
| 342 | |
| 343 | return 0; |
| 344 | |
| 345 | err_1: |
| 346 | kfree(svm_data); |
| 347 | return r; |
| 348 | |
| 349 | } |
| 350 | |
Rusty Russell | bfc733a | 2007-07-31 20:42:42 +1000 | [diff] [blame] | 351 | static void set_msr_interception(u32 *msrpm, unsigned msr, |
| 352 | int read, int write) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 353 | { |
| 354 | int i; |
| 355 | |
| 356 | for (i = 0; i < NUM_MSR_MAPS; i++) { |
| 357 | if (msr >= msrpm_ranges[i] && |
| 358 | msr < msrpm_ranges[i] + MSRS_IN_RANGE) { |
| 359 | u32 msr_offset = (i * MSRS_IN_RANGE + msr - |
| 360 | msrpm_ranges[i]) * 2; |
| 361 | |
| 362 | u32 *base = msrpm + (msr_offset / 32); |
| 363 | u32 msr_shift = msr_offset % 32; |
| 364 | u32 mask = ((write) ? 0 : 2) | ((read) ? 0 : 1); |
| 365 | *base = (*base & ~(0x3 << msr_shift)) | |
| 366 | (mask << msr_shift); |
Rusty Russell | bfc733a | 2007-07-31 20:42:42 +1000 | [diff] [blame] | 367 | return; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 368 | } |
| 369 | } |
Rusty Russell | bfc733a | 2007-07-31 20:42:42 +1000 | [diff] [blame] | 370 | BUG(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | static __init int svm_hardware_setup(void) |
| 374 | { |
| 375 | int cpu; |
| 376 | struct page *iopm_pages; |
| 377 | struct page *msrpm_pages; |
Anthony Liguori | c868133 | 2007-04-30 09:48:11 +0300 | [diff] [blame] | 378 | void *iopm_va, *msrpm_va; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 379 | int r; |
| 380 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 381 | iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER); |
| 382 | |
| 383 | if (!iopm_pages) |
| 384 | return -ENOMEM; |
Anthony Liguori | c868133 | 2007-04-30 09:48:11 +0300 | [diff] [blame] | 385 | |
| 386 | iopm_va = page_address(iopm_pages); |
| 387 | memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER)); |
| 388 | clear_bit(0x80, iopm_va); /* allow direct access to PC debug port */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 389 | iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT; |
| 390 | |
| 391 | |
| 392 | msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER); |
| 393 | |
| 394 | r = -ENOMEM; |
| 395 | if (!msrpm_pages) |
| 396 | goto err_1; |
| 397 | |
| 398 | msrpm_va = page_address(msrpm_pages); |
| 399 | memset(msrpm_va, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER)); |
| 400 | msrpm_base = page_to_pfn(msrpm_pages) << PAGE_SHIFT; |
| 401 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 402 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 403 | set_msr_interception(msrpm_va, MSR_GS_BASE, 1, 1); |
| 404 | set_msr_interception(msrpm_va, MSR_FS_BASE, 1, 1); |
| 405 | set_msr_interception(msrpm_va, MSR_KERNEL_GS_BASE, 1, 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 406 | set_msr_interception(msrpm_va, MSR_LSTAR, 1, 1); |
| 407 | set_msr_interception(msrpm_va, MSR_CSTAR, 1, 1); |
| 408 | set_msr_interception(msrpm_va, MSR_SYSCALL_MASK, 1, 1); |
| 409 | #endif |
Avi Kivity | 0e859ca | 2006-12-22 01:05:08 -0800 | [diff] [blame] | 410 | set_msr_interception(msrpm_va, MSR_K6_STAR, 1, 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 411 | set_msr_interception(msrpm_va, MSR_IA32_SYSENTER_CS, 1, 1); |
| 412 | set_msr_interception(msrpm_va, MSR_IA32_SYSENTER_ESP, 1, 1); |
| 413 | set_msr_interception(msrpm_va, MSR_IA32_SYSENTER_EIP, 1, 1); |
| 414 | |
Joerg Roedel | 50a37eb | 2008-01-31 14:57:38 +0100 | [diff] [blame] | 415 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 416 | kvm_enable_efer_bits(EFER_NX); |
| 417 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 418 | for_each_online_cpu(cpu) { |
| 419 | r = svm_cpu_init(cpu); |
| 420 | if (r) |
| 421 | goto err_2; |
| 422 | } |
Joerg Roedel | 33bd6a0 | 2008-02-07 13:47:38 +0100 | [diff] [blame] | 423 | |
| 424 | svm_features = cpuid_edx(SVM_CPUID_FUNC); |
| 425 | |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 426 | if (!svm_has(SVM_FEATURE_NPT)) |
| 427 | npt_enabled = false; |
| 428 | |
Joerg Roedel | 6c7dac7 | 2008-02-07 13:47:40 +0100 | [diff] [blame] | 429 | if (npt_enabled && !npt) { |
| 430 | printk(KERN_INFO "kvm: Nested Paging disabled\n"); |
| 431 | npt_enabled = false; |
| 432 | } |
| 433 | |
Joerg Roedel | 1855267 | 2008-02-07 13:47:41 +0100 | [diff] [blame] | 434 | if (npt_enabled) { |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 435 | printk(KERN_INFO "kvm: Nested Paging enabled\n"); |
Joerg Roedel | 1855267 | 2008-02-07 13:47:41 +0100 | [diff] [blame] | 436 | kvm_enable_tdp(); |
| 437 | } |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 438 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 439 | return 0; |
| 440 | |
| 441 | err_2: |
| 442 | __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER); |
| 443 | msrpm_base = 0; |
| 444 | err_1: |
| 445 | __free_pages(iopm_pages, IOPM_ALLOC_ORDER); |
| 446 | iopm_base = 0; |
| 447 | return r; |
| 448 | } |
| 449 | |
| 450 | static __exit void svm_hardware_unsetup(void) |
| 451 | { |
| 452 | __free_pages(pfn_to_page(msrpm_base >> PAGE_SHIFT), MSRPM_ALLOC_ORDER); |
| 453 | __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER); |
| 454 | iopm_base = msrpm_base = 0; |
| 455 | } |
| 456 | |
| 457 | static void init_seg(struct vmcb_seg *seg) |
| 458 | { |
| 459 | seg->selector = 0; |
| 460 | seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK | |
| 461 | SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */ |
| 462 | seg->limit = 0xffff; |
| 463 | seg->base = 0; |
| 464 | } |
| 465 | |
| 466 | static void init_sys_seg(struct vmcb_seg *seg, uint32_t type) |
| 467 | { |
| 468 | seg->selector = 0; |
| 469 | seg->attrib = SVM_SELECTOR_P_MASK | type; |
| 470 | seg->limit = 0xffff; |
| 471 | seg->base = 0; |
| 472 | } |
| 473 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 474 | static void init_vmcb(struct vmcb *vmcb) |
| 475 | { |
| 476 | struct vmcb_control_area *control = &vmcb->control; |
| 477 | struct vmcb_save_area *save = &vmcb->save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 478 | |
| 479 | control->intercept_cr_read = INTERCEPT_CR0_MASK | |
| 480 | INTERCEPT_CR3_MASK | |
Avi Kivity | 80a8119 | 2007-12-06 19:50:00 +0200 | [diff] [blame] | 481 | INTERCEPT_CR4_MASK | |
| 482 | INTERCEPT_CR8_MASK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 483 | |
| 484 | control->intercept_cr_write = INTERCEPT_CR0_MASK | |
| 485 | INTERCEPT_CR3_MASK | |
Avi Kivity | 80a8119 | 2007-12-06 19:50:00 +0200 | [diff] [blame] | 486 | INTERCEPT_CR4_MASK | |
| 487 | INTERCEPT_CR8_MASK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 488 | |
| 489 | control->intercept_dr_read = INTERCEPT_DR0_MASK | |
| 490 | INTERCEPT_DR1_MASK | |
| 491 | INTERCEPT_DR2_MASK | |
| 492 | INTERCEPT_DR3_MASK; |
| 493 | |
| 494 | control->intercept_dr_write = INTERCEPT_DR0_MASK | |
| 495 | INTERCEPT_DR1_MASK | |
| 496 | INTERCEPT_DR2_MASK | |
| 497 | INTERCEPT_DR3_MASK | |
| 498 | INTERCEPT_DR5_MASK | |
| 499 | INTERCEPT_DR7_MASK; |
| 500 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 501 | control->intercept_exceptions = (1 << PF_VECTOR) | |
| 502 | (1 << UD_VECTOR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 503 | |
| 504 | |
| 505 | control->intercept = (1ULL << INTERCEPT_INTR) | |
| 506 | (1ULL << INTERCEPT_NMI) | |
Joerg Roedel | 0152527 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 507 | (1ULL << INTERCEPT_SMI) | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 508 | /* |
| 509 | * selective cr0 intercept bug? |
| 510 | * 0: 0f 22 d8 mov %eax,%cr3 |
| 511 | * 3: 0f 20 c0 mov %cr0,%eax |
| 512 | * 6: 0d 00 00 00 80 or $0x80000000,%eax |
| 513 | * b: 0f 22 c0 mov %eax,%cr0 |
| 514 | * set cr3 ->interception |
| 515 | * get cr0 ->interception |
| 516 | * set cr0 -> no interception |
| 517 | */ |
| 518 | /* (1ULL << INTERCEPT_SELECTIVE_CR0) | */ |
| 519 | (1ULL << INTERCEPT_CPUID) | |
Avi Kivity | cf5a94d | 2007-10-28 16:11:58 +0200 | [diff] [blame] | 520 | (1ULL << INTERCEPT_INVD) | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 521 | (1ULL << INTERCEPT_HLT) | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 522 | (1ULL << INTERCEPT_INVLPGA) | |
| 523 | (1ULL << INTERCEPT_IOIO_PROT) | |
| 524 | (1ULL << INTERCEPT_MSR_PROT) | |
| 525 | (1ULL << INTERCEPT_TASK_SWITCH) | |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 526 | (1ULL << INTERCEPT_SHUTDOWN) | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 527 | (1ULL << INTERCEPT_VMRUN) | |
| 528 | (1ULL << INTERCEPT_VMMCALL) | |
| 529 | (1ULL << INTERCEPT_VMLOAD) | |
| 530 | (1ULL << INTERCEPT_VMSAVE) | |
| 531 | (1ULL << INTERCEPT_STGI) | |
| 532 | (1ULL << INTERCEPT_CLGI) | |
Joerg Roedel | 916ce23 | 2007-03-21 19:47:00 +0100 | [diff] [blame] | 533 | (1ULL << INTERCEPT_SKINIT) | |
Avi Kivity | cf5a94d | 2007-10-28 16:11:58 +0200 | [diff] [blame] | 534 | (1ULL << INTERCEPT_WBINVD) | |
Joerg Roedel | 916ce23 | 2007-03-21 19:47:00 +0100 | [diff] [blame] | 535 | (1ULL << INTERCEPT_MONITOR) | |
| 536 | (1ULL << INTERCEPT_MWAIT); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 537 | |
| 538 | control->iopm_base_pa = iopm_base; |
| 539 | control->msrpm_base_pa = msrpm_base; |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 540 | control->tsc_offset = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 541 | control->int_ctl = V_INTR_MASKING_MASK; |
| 542 | |
| 543 | init_seg(&save->es); |
| 544 | init_seg(&save->ss); |
| 545 | init_seg(&save->ds); |
| 546 | init_seg(&save->fs); |
| 547 | init_seg(&save->gs); |
| 548 | |
| 549 | save->cs.selector = 0xf000; |
| 550 | /* Executable/Readable Code Segment */ |
| 551 | save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK | |
| 552 | SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK; |
| 553 | save->cs.limit = 0xffff; |
Avi Kivity | d92899a | 2007-02-12 00:54:38 -0800 | [diff] [blame] | 554 | /* |
| 555 | * cs.base should really be 0xffff0000, but vmx can't handle that, so |
| 556 | * be consistent with it. |
| 557 | * |
| 558 | * Replace when we have real mode working for vmx. |
| 559 | */ |
| 560 | save->cs.base = 0xf0000; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 561 | |
| 562 | save->gdtr.limit = 0xffff; |
| 563 | save->idtr.limit = 0xffff; |
| 564 | |
| 565 | init_sys_seg(&save->ldtr, SEG_TYPE_LDT); |
| 566 | init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16); |
| 567 | |
| 568 | save->efer = MSR_EFER_SVME_MASK; |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 569 | save->dr6 = 0xffff0ff0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 570 | save->dr7 = 0x400; |
| 571 | save->rflags = 2; |
| 572 | save->rip = 0x0000fff0; |
| 573 | |
| 574 | /* |
| 575 | * cr0 val on cpu init should be 0x60000010, we enable cpu |
| 576 | * cache by default. the orderly way is to enable cache in bios. |
| 577 | */ |
Rusty Russell | 707d92f | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 578 | save->cr0 = 0x00000010 | X86_CR0_PG | X86_CR0_WP; |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 579 | save->cr4 = X86_CR4_PAE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 580 | /* rdx = ?? */ |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame^] | 581 | |
| 582 | if (npt_enabled) { |
| 583 | /* Setup VMCB for Nested Paging */ |
| 584 | control->nested_ctl = 1; |
| 585 | control->intercept_exceptions &= ~(1 << PF_VECTOR); |
| 586 | control->intercept_cr_read &= ~(INTERCEPT_CR0_MASK| |
| 587 | INTERCEPT_CR3_MASK); |
| 588 | control->intercept_cr_write &= ~(INTERCEPT_CR0_MASK| |
| 589 | INTERCEPT_CR3_MASK); |
| 590 | save->g_pat = 0x0007040600070406ULL; |
| 591 | /* enable caching because the QEMU Bios doesn't enable it */ |
| 592 | save->cr0 = X86_CR0_ET; |
| 593 | save->cr3 = 0; |
| 594 | save->cr4 = 0; |
| 595 | } |
| 596 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 597 | } |
| 598 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 599 | static int svm_vcpu_reset(struct kvm_vcpu *vcpu) |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 600 | { |
| 601 | struct vcpu_svm *svm = to_svm(vcpu); |
| 602 | |
| 603 | init_vmcb(svm->vmcb); |
Avi Kivity | 7043338 | 2007-11-07 12:57:23 +0200 | [diff] [blame] | 604 | |
| 605 | if (vcpu->vcpu_id != 0) { |
| 606 | svm->vmcb->save.rip = 0; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 607 | svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12; |
| 608 | svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8; |
Avi Kivity | 7043338 | 2007-11-07 12:57:23 +0200 | [diff] [blame] | 609 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 610 | |
| 611 | return 0; |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 612 | } |
| 613 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 614 | 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] | 615 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 616 | struct vcpu_svm *svm; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 617 | struct page *page; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 618 | int err; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 619 | |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 620 | svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 621 | if (!svm) { |
| 622 | err = -ENOMEM; |
| 623 | goto out; |
| 624 | } |
| 625 | |
| 626 | err = kvm_vcpu_init(&svm->vcpu, kvm, id); |
| 627 | if (err) |
| 628 | goto free_svm; |
| 629 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 630 | page = alloc_page(GFP_KERNEL); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 631 | if (!page) { |
| 632 | err = -ENOMEM; |
| 633 | goto uninit; |
| 634 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 635 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 636 | svm->vmcb = page_address(page); |
| 637 | clear_page(svm->vmcb); |
| 638 | svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT; |
| 639 | svm->asid_generation = 0; |
| 640 | memset(svm->db_regs, 0, sizeof(svm->db_regs)); |
| 641 | init_vmcb(svm->vmcb); |
| 642 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 643 | fx_init(&svm->vcpu); |
| 644 | svm->vcpu.fpu_active = 1; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 645 | svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 646 | if (svm->vcpu.vcpu_id == 0) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 647 | svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 648 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 649 | return &svm->vcpu; |
Avi Kivity | 36241b8 | 2006-12-22 01:05:20 -0800 | [diff] [blame] | 650 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 651 | uninit: |
| 652 | kvm_vcpu_uninit(&svm->vcpu); |
| 653 | free_svm: |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 654 | kmem_cache_free(kvm_vcpu_cache, svm); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 655 | out: |
| 656 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | static void svm_free_vcpu(struct kvm_vcpu *vcpu) |
| 660 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 661 | struct vcpu_svm *svm = to_svm(vcpu); |
| 662 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 663 | __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT)); |
| 664 | kvm_vcpu_uninit(vcpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 665 | kmem_cache_free(kvm_vcpu_cache, svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 666 | } |
| 667 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 668 | static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 669 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 670 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 671 | int i; |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 672 | |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 673 | if (unlikely(cpu != vcpu->cpu)) { |
| 674 | u64 tsc_this, delta; |
| 675 | |
| 676 | /* |
| 677 | * Make sure that the guest sees a monotonically |
| 678 | * increasing TSC. |
| 679 | */ |
| 680 | rdtscll(tsc_this); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 681 | delta = vcpu->arch.host_tsc - tsc_this; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 682 | svm->vmcb->control.tsc_offset += delta; |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 683 | vcpu->cpu = cpu; |
Eddie Dong | a3d7f85 | 2007-09-03 16:15:12 +0300 | [diff] [blame] | 684 | kvm_migrate_apic_timer(vcpu); |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 685 | } |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 686 | |
| 687 | for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 688 | rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | static void svm_vcpu_put(struct kvm_vcpu *vcpu) |
| 692 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 693 | struct vcpu_svm *svm = to_svm(vcpu); |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 694 | int i; |
| 695 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 696 | ++vcpu->stat.host_state_reload; |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 697 | for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 698 | wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]); |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 699 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 700 | rdtscll(vcpu->arch.host_tsc); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 701 | } |
| 702 | |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 703 | static void svm_vcpu_decache(struct kvm_vcpu *vcpu) |
| 704 | { |
| 705 | } |
| 706 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 707 | static void svm_cache_regs(struct kvm_vcpu *vcpu) |
| 708 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 709 | struct vcpu_svm *svm = to_svm(vcpu); |
| 710 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 711 | vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax; |
| 712 | vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp; |
| 713 | vcpu->arch.rip = svm->vmcb->save.rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | static void svm_decache_regs(struct kvm_vcpu *vcpu) |
| 717 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 718 | struct vcpu_svm *svm = to_svm(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 719 | svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX]; |
| 720 | svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP]; |
| 721 | svm->vmcb->save.rip = vcpu->arch.rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu) |
| 725 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 726 | return to_svm(vcpu)->vmcb->save.rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 730 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 731 | to_svm(vcpu)->vmcb->save.rflags = rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg) |
| 735 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 736 | struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 737 | |
| 738 | switch (seg) { |
| 739 | case VCPU_SREG_CS: return &save->cs; |
| 740 | case VCPU_SREG_DS: return &save->ds; |
| 741 | case VCPU_SREG_ES: return &save->es; |
| 742 | case VCPU_SREG_FS: return &save->fs; |
| 743 | case VCPU_SREG_GS: return &save->gs; |
| 744 | case VCPU_SREG_SS: return &save->ss; |
| 745 | case VCPU_SREG_TR: return &save->tr; |
| 746 | case VCPU_SREG_LDTR: return &save->ldtr; |
| 747 | } |
| 748 | BUG(); |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 749 | return NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 753 | { |
| 754 | struct vmcb_seg *s = svm_seg(vcpu, seg); |
| 755 | |
| 756 | return s->base; |
| 757 | } |
| 758 | |
| 759 | static void svm_get_segment(struct kvm_vcpu *vcpu, |
| 760 | struct kvm_segment *var, int seg) |
| 761 | { |
| 762 | struct vmcb_seg *s = svm_seg(vcpu, seg); |
| 763 | |
| 764 | var->base = s->base; |
| 765 | var->limit = s->limit; |
| 766 | var->selector = s->selector; |
| 767 | var->type = s->attrib & SVM_SELECTOR_TYPE_MASK; |
| 768 | var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1; |
| 769 | var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3; |
| 770 | var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1; |
| 771 | var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1; |
| 772 | var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1; |
| 773 | var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1; |
| 774 | var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1; |
| 775 | var->unusable = !var->present; |
| 776 | } |
| 777 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 778 | static void svm_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) |
| 779 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 780 | struct vcpu_svm *svm = to_svm(vcpu); |
| 781 | |
| 782 | dt->limit = svm->vmcb->save.idtr.limit; |
| 783 | dt->base = svm->vmcb->save.idtr.base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | static void svm_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) |
| 787 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 788 | struct vcpu_svm *svm = to_svm(vcpu); |
| 789 | |
| 790 | svm->vmcb->save.idtr.limit = dt->limit; |
| 791 | svm->vmcb->save.idtr.base = dt->base ; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | static void svm_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) |
| 795 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 796 | struct vcpu_svm *svm = to_svm(vcpu); |
| 797 | |
| 798 | dt->limit = svm->vmcb->save.gdtr.limit; |
| 799 | dt->base = svm->vmcb->save.gdtr.base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | static void svm_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) |
| 803 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 804 | struct vcpu_svm *svm = to_svm(vcpu); |
| 805 | |
| 806 | svm->vmcb->save.gdtr.limit = dt->limit; |
| 807 | svm->vmcb->save.gdtr.base = dt->base ; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 808 | } |
| 809 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 810 | static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 811 | { |
| 812 | } |
| 813 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 814 | static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 815 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 816 | struct vcpu_svm *svm = to_svm(vcpu); |
| 817 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 818 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 819 | if (vcpu->arch.shadow_efer & EFER_LME) { |
Rusty Russell | 707d92f | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 820 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 821 | vcpu->arch.shadow_efer |= EFER_LMA; |
Carlo Marcelo Arenas Belon | 2b5203e | 2007-12-01 06:17:11 -0600 | [diff] [blame] | 822 | svm->vmcb->save.efer |= EFER_LMA | EFER_LME; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 823 | } |
| 824 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 825 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 826 | vcpu->arch.shadow_efer &= ~EFER_LMA; |
Carlo Marcelo Arenas Belon | 2b5203e | 2007-12-01 06:17:11 -0600 | [diff] [blame] | 827 | svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | #endif |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame^] | 831 | if (npt_enabled) |
| 832 | goto set; |
| 833 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 834 | if ((vcpu->arch.cr0 & X86_CR0_TS) && !(cr0 & X86_CR0_TS)) { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 835 | svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR); |
Anthony Liguori | 7807fa6 | 2007-04-23 09:17:21 -0500 | [diff] [blame] | 836 | vcpu->fpu_active = 1; |
| 837 | } |
| 838 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 839 | vcpu->arch.cr0 = cr0; |
Rusty Russell | 707d92f | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 840 | cr0 |= X86_CR0_PG | X86_CR0_WP; |
Joerg Roedel | 6b390b6 | 2008-01-29 13:01:27 +0100 | [diff] [blame] | 841 | if (!vcpu->fpu_active) { |
| 842 | svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR); |
Joerg Roedel | 334df50 | 2008-01-21 13:09:33 +0100 | [diff] [blame] | 843 | cr0 |= X86_CR0_TS; |
Joerg Roedel | 6b390b6 | 2008-01-29 13:01:27 +0100 | [diff] [blame] | 844 | } |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame^] | 845 | set: |
| 846 | /* |
| 847 | * re-enable caching here because the QEMU bios |
| 848 | * does not do it - this results in some delay at |
| 849 | * reboot |
| 850 | */ |
| 851 | cr0 &= ~(X86_CR0_CD | X86_CR0_NW); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 852 | svm->vmcb->save.cr0 = cr0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
| 856 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 857 | vcpu->arch.cr4 = cr4; |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame^] | 858 | if (!npt_enabled) |
| 859 | cr4 |= X86_CR4_PAE; |
| 860 | to_svm(vcpu)->vmcb->save.cr4 = cr4; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | static void svm_set_segment(struct kvm_vcpu *vcpu, |
| 864 | struct kvm_segment *var, int seg) |
| 865 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 866 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 867 | struct vmcb_seg *s = svm_seg(vcpu, seg); |
| 868 | |
| 869 | s->base = var->base; |
| 870 | s->limit = var->limit; |
| 871 | s->selector = var->selector; |
| 872 | if (var->unusable) |
| 873 | s->attrib = 0; |
| 874 | else { |
| 875 | s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK); |
| 876 | s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT; |
| 877 | s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT; |
| 878 | s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT; |
| 879 | s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT; |
| 880 | s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT; |
| 881 | s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT; |
| 882 | s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT; |
| 883 | } |
| 884 | if (seg == VCPU_SREG_CS) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 885 | svm->vmcb->save.cpl |
| 886 | = (svm->vmcb->save.cs.attrib |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 887 | >> SVM_SELECTOR_DPL_SHIFT) & 3; |
| 888 | |
| 889 | } |
| 890 | |
| 891 | /* FIXME: |
| 892 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 893 | svm(vcpu)->vmcb->control.int_ctl &= ~V_TPR_MASK; |
| 894 | svm(vcpu)->vmcb->control.int_ctl |= (sregs->cr8 & V_TPR_MASK); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 895 | |
| 896 | */ |
| 897 | |
| 898 | static int svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg) |
| 899 | { |
| 900 | return -EOPNOTSUPP; |
| 901 | } |
| 902 | |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 903 | static int svm_get_irq(struct kvm_vcpu *vcpu) |
| 904 | { |
| 905 | struct vcpu_svm *svm = to_svm(vcpu); |
| 906 | u32 exit_int_info = svm->vmcb->control.exit_int_info; |
| 907 | |
| 908 | if (is_external_interrupt(exit_int_info)) |
| 909 | return exit_int_info & SVM_EVTINJ_VEC_MASK; |
| 910 | return -1; |
| 911 | } |
| 912 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 913 | static void load_host_msrs(struct kvm_vcpu *vcpu) |
| 914 | { |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 915 | #ifdef CONFIG_X86_64 |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 916 | wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base); |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 917 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | static void save_host_msrs(struct kvm_vcpu *vcpu) |
| 921 | { |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 922 | #ifdef CONFIG_X86_64 |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 923 | rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base); |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 924 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 925 | } |
| 926 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 927 | 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] | 928 | { |
| 929 | if (svm_data->next_asid > svm_data->max_asid) { |
| 930 | ++svm_data->asid_generation; |
| 931 | svm_data->next_asid = 1; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 932 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 933 | } |
| 934 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 935 | svm->vcpu.cpu = svm_data->cpu; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 936 | svm->asid_generation = svm_data->asid_generation; |
| 937 | svm->vmcb->control.asid = svm_data->next_asid++; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 938 | } |
| 939 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 940 | static unsigned long svm_get_dr(struct kvm_vcpu *vcpu, int dr) |
| 941 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 942 | return to_svm(vcpu)->db_regs[dr]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | static void svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value, |
| 946 | int *exception) |
| 947 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 948 | struct vcpu_svm *svm = to_svm(vcpu); |
| 949 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 950 | *exception = 0; |
| 951 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 952 | if (svm->vmcb->save.dr7 & DR7_GD_MASK) { |
| 953 | svm->vmcb->save.dr7 &= ~DR7_GD_MASK; |
| 954 | svm->vmcb->save.dr6 |= DR6_BD_MASK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 955 | *exception = DB_VECTOR; |
| 956 | return; |
| 957 | } |
| 958 | |
| 959 | switch (dr) { |
| 960 | case 0 ... 3: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 961 | svm->db_regs[dr] = value; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 962 | return; |
| 963 | case 4 ... 5: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 964 | if (vcpu->arch.cr4 & X86_CR4_DE) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 965 | *exception = UD_VECTOR; |
| 966 | return; |
| 967 | } |
| 968 | case 7: { |
| 969 | if (value & ~((1ULL << 32) - 1)) { |
| 970 | *exception = GP_VECTOR; |
| 971 | return; |
| 972 | } |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 973 | svm->vmcb->save.dr7 = value; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 974 | return; |
| 975 | } |
| 976 | default: |
| 977 | printk(KERN_DEBUG "%s: unexpected dr %u\n", |
| 978 | __FUNCTION__, dr); |
| 979 | *exception = UD_VECTOR; |
| 980 | return; |
| 981 | } |
| 982 | } |
| 983 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 984 | 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] | 985 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 986 | u32 exit_int_info = svm->vmcb->control.exit_int_info; |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 987 | struct kvm *kvm = svm->vcpu.kvm; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 988 | u64 fault_address; |
| 989 | u32 error_code; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 990 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 991 | if (!irqchip_in_kernel(kvm) && |
| 992 | is_external_interrupt(exit_int_info)) |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 993 | push_irq(&svm->vcpu, exit_int_info & SVM_EVTINJ_VEC_MASK); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 994 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 995 | fault_address = svm->vmcb->control.exit_info_2; |
| 996 | error_code = svm->vmcb->control.exit_info_1; |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 997 | return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 998 | } |
| 999 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1000 | static int ud_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) |
| 1001 | { |
| 1002 | int er; |
| 1003 | |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 1004 | er = emulate_instruction(&svm->vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1005 | if (er != EMULATE_DONE) |
Avi Kivity | 7ee5d940 | 2007-11-25 15:22:50 +0200 | [diff] [blame] | 1006 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1007 | return 1; |
| 1008 | } |
| 1009 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1010 | 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] | 1011 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1012 | svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1013 | if (!(svm->vcpu.arch.cr0 & X86_CR0_TS)) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1014 | svm->vmcb->save.cr0 &= ~X86_CR0_TS; |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1015 | svm->vcpu.fpu_active = 1; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1016 | |
| 1017 | return 1; |
Anthony Liguori | 7807fa6 | 2007-04-23 09:17:21 -0500 | [diff] [blame] | 1018 | } |
| 1019 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1020 | 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] | 1021 | { |
| 1022 | /* |
| 1023 | * VMCB is undefined after a SHUTDOWN intercept |
| 1024 | * so reinitialize it. |
| 1025 | */ |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1026 | clear_page(svm->vmcb); |
| 1027 | init_vmcb(svm->vmcb); |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 1028 | |
| 1029 | kvm_run->exit_reason = KVM_EXIT_SHUTDOWN; |
| 1030 | return 0; |
| 1031 | } |
| 1032 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1033 | 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] | 1034 | { |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1035 | u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */ |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1036 | int size, down, in, string, rep; |
| 1037 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1038 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1039 | ++svm->vcpu.stat.io_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1040 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1041 | svm->next_rip = svm->vmcb->control.exit_info_2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1042 | |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 1043 | string = (io_info & SVM_IOIO_STR_MASK) != 0; |
| 1044 | |
| 1045 | if (string) { |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 1046 | if (emulate_instruction(&svm->vcpu, |
| 1047 | kvm_run, 0, 0, 0) == EMULATE_DO_MMIO) |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 1048 | return 0; |
| 1049 | return 1; |
| 1050 | } |
| 1051 | |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1052 | in = (io_info & SVM_IOIO_TYPE_MASK) != 0; |
| 1053 | port = io_info >> 16; |
| 1054 | size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1055 | rep = (io_info & SVM_IOIO_REP_MASK) != 0; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1056 | down = (svm->vmcb->save.rflags & X86_EFLAGS_DF) != 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1057 | |
Laurent Vivier | 3090dd7 | 2007-08-05 10:43:32 +0300 | [diff] [blame] | 1058 | return kvm_emulate_pio(&svm->vcpu, kvm_run, in, size, port); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1059 | } |
| 1060 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1061 | 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] | 1062 | { |
| 1063 | return 1; |
| 1064 | } |
| 1065 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1066 | 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] | 1067 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1068 | svm->next_rip = svm->vmcb->save.rip + 1; |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1069 | skip_emulated_instruction(&svm->vcpu); |
| 1070 | return kvm_emulate_halt(&svm->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1071 | } |
| 1072 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1073 | 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] | 1074 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1075 | svm->next_rip = svm->vmcb->save.rip + 3; |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1076 | skip_emulated_instruction(&svm->vcpu); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1077 | kvm_emulate_hypercall(&svm->vcpu); |
| 1078 | return 1; |
Avi Kivity | 02e235b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1079 | } |
| 1080 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1081 | static int invalid_op_interception(struct vcpu_svm *svm, |
| 1082 | struct kvm_run *kvm_run) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1083 | { |
Avi Kivity | 7ee5d940 | 2007-11-25 15:22:50 +0200 | [diff] [blame] | 1084 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1085 | return 1; |
| 1086 | } |
| 1087 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1088 | static int task_switch_interception(struct vcpu_svm *svm, |
| 1089 | struct kvm_run *kvm_run) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1090 | { |
Rusty Russell | f024247 | 2007-08-01 10:48:02 +1000 | [diff] [blame] | 1091 | pr_unimpl(&svm->vcpu, "%s: task switch is unsupported\n", __FUNCTION__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1092 | kvm_run->exit_reason = KVM_EXIT_UNKNOWN; |
| 1093 | return 0; |
| 1094 | } |
| 1095 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1096 | 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] | 1097 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1098 | svm->next_rip = svm->vmcb->save.rip + 2; |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1099 | kvm_emulate_cpuid(&svm->vcpu); |
Avi Kivity | 06465c5 | 2007-02-28 20:46:53 +0200 | [diff] [blame] | 1100 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1101 | } |
| 1102 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1103 | static int emulate_on_interception(struct vcpu_svm *svm, |
| 1104 | struct kvm_run *kvm_run) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1105 | { |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 1106 | if (emulate_instruction(&svm->vcpu, NULL, 0, 0, 0) != EMULATE_DONE) |
Rusty Russell | f024247 | 2007-08-01 10:48:02 +1000 | [diff] [blame] | 1107 | pr_unimpl(&svm->vcpu, "%s: failed\n", __FUNCTION__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1108 | return 1; |
| 1109 | } |
| 1110 | |
Joerg Roedel | 1d07543 | 2007-12-06 21:02:25 +0100 | [diff] [blame] | 1111 | static int cr8_write_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) |
| 1112 | { |
| 1113 | emulate_instruction(&svm->vcpu, NULL, 0, 0, 0); |
| 1114 | if (irqchip_in_kernel(svm->vcpu.kvm)) |
| 1115 | return 1; |
| 1116 | kvm_run->exit_reason = KVM_EXIT_SET_TPR; |
| 1117 | return 0; |
| 1118 | } |
| 1119 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1120 | static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data) |
| 1121 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1122 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1123 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1124 | switch (ecx) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1125 | case MSR_IA32_TIME_STAMP_COUNTER: { |
| 1126 | u64 tsc; |
| 1127 | |
| 1128 | rdtscll(tsc); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1129 | *data = svm->vmcb->control.tsc_offset + tsc; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1130 | break; |
| 1131 | } |
Avi Kivity | 0e859ca | 2006-12-22 01:05:08 -0800 | [diff] [blame] | 1132 | case MSR_K6_STAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1133 | *data = svm->vmcb->save.star; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1134 | break; |
Avi Kivity | 0e859ca | 2006-12-22 01:05:08 -0800 | [diff] [blame] | 1135 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1136 | case MSR_LSTAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1137 | *data = svm->vmcb->save.lstar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1138 | break; |
| 1139 | case MSR_CSTAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1140 | *data = svm->vmcb->save.cstar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1141 | break; |
| 1142 | case MSR_KERNEL_GS_BASE: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1143 | *data = svm->vmcb->save.kernel_gs_base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1144 | break; |
| 1145 | case MSR_SYSCALL_MASK: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1146 | *data = svm->vmcb->save.sfmask; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1147 | break; |
| 1148 | #endif |
| 1149 | case MSR_IA32_SYSENTER_CS: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1150 | *data = svm->vmcb->save.sysenter_cs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1151 | break; |
| 1152 | case MSR_IA32_SYSENTER_EIP: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1153 | *data = svm->vmcb->save.sysenter_eip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1154 | break; |
| 1155 | case MSR_IA32_SYSENTER_ESP: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1156 | *data = svm->vmcb->save.sysenter_esp; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1157 | break; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 1158 | /* Nobody will change the following 5 values in the VMCB so |
| 1159 | we can safely return them on rdmsr. They will always be 0 |
| 1160 | until LBRV is implemented. */ |
| 1161 | case MSR_IA32_DEBUGCTLMSR: |
| 1162 | *data = svm->vmcb->save.dbgctl; |
| 1163 | break; |
| 1164 | case MSR_IA32_LASTBRANCHFROMIP: |
| 1165 | *data = svm->vmcb->save.br_from; |
| 1166 | break; |
| 1167 | case MSR_IA32_LASTBRANCHTOIP: |
| 1168 | *data = svm->vmcb->save.br_to; |
| 1169 | break; |
| 1170 | case MSR_IA32_LASTINTFROMIP: |
| 1171 | *data = svm->vmcb->save.last_excp_from; |
| 1172 | break; |
| 1173 | case MSR_IA32_LASTINTTOIP: |
| 1174 | *data = svm->vmcb->save.last_excp_to; |
| 1175 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1176 | default: |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1177 | return kvm_get_msr_common(vcpu, ecx, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1178 | } |
| 1179 | return 0; |
| 1180 | } |
| 1181 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1182 | 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] | 1183 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1184 | u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1185 | u64 data; |
| 1186 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1187 | if (svm_get_msr(&svm->vcpu, ecx, &data)) |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 1188 | kvm_inject_gp(&svm->vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1189 | else { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1190 | svm->vmcb->save.rax = data & 0xffffffff; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1191 | svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1192 | svm->next_rip = svm->vmcb->save.rip + 2; |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1193 | skip_emulated_instruction(&svm->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1194 | } |
| 1195 | return 1; |
| 1196 | } |
| 1197 | |
| 1198 | static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data) |
| 1199 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1200 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1201 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1202 | switch (ecx) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1203 | case MSR_IA32_TIME_STAMP_COUNTER: { |
| 1204 | u64 tsc; |
| 1205 | |
| 1206 | rdtscll(tsc); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1207 | svm->vmcb->control.tsc_offset = data - tsc; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1208 | break; |
| 1209 | } |
Avi Kivity | 0e859ca | 2006-12-22 01:05:08 -0800 | [diff] [blame] | 1210 | case MSR_K6_STAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1211 | svm->vmcb->save.star = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1212 | break; |
Robert P. J. Day | 49b14f2 | 2007-01-29 13:19:50 -0800 | [diff] [blame] | 1213 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1214 | case MSR_LSTAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1215 | svm->vmcb->save.lstar = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1216 | break; |
| 1217 | case MSR_CSTAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1218 | svm->vmcb->save.cstar = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1219 | break; |
| 1220 | case MSR_KERNEL_GS_BASE: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1221 | svm->vmcb->save.kernel_gs_base = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1222 | break; |
| 1223 | case MSR_SYSCALL_MASK: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1224 | svm->vmcb->save.sfmask = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1225 | break; |
| 1226 | #endif |
| 1227 | case MSR_IA32_SYSENTER_CS: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1228 | svm->vmcb->save.sysenter_cs = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1229 | break; |
| 1230 | case MSR_IA32_SYSENTER_EIP: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1231 | svm->vmcb->save.sysenter_eip = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1232 | break; |
| 1233 | case MSR_IA32_SYSENTER_ESP: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1234 | svm->vmcb->save.sysenter_esp = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1235 | break; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 1236 | case MSR_IA32_DEBUGCTLMSR: |
| 1237 | pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n", |
| 1238 | __FUNCTION__, data); |
| 1239 | break; |
Joerg Roedel | 62b9aba | 2007-12-11 15:36:57 +0100 | [diff] [blame] | 1240 | case MSR_K7_EVNTSEL0: |
| 1241 | case MSR_K7_EVNTSEL1: |
| 1242 | case MSR_K7_EVNTSEL2: |
| 1243 | case MSR_K7_EVNTSEL3: |
| 1244 | /* |
| 1245 | * only support writing 0 to the performance counters for now |
| 1246 | * to make Windows happy. Should be replaced by a real |
| 1247 | * performance counter emulation later. |
| 1248 | */ |
| 1249 | if (data != 0) |
| 1250 | goto unhandled; |
| 1251 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1252 | default: |
Joerg Roedel | 62b9aba | 2007-12-11 15:36:57 +0100 | [diff] [blame] | 1253 | unhandled: |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1254 | return kvm_set_msr_common(vcpu, ecx, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1255 | } |
| 1256 | return 0; |
| 1257 | } |
| 1258 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1259 | 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] | 1260 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1261 | u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX]; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1262 | u64 data = (svm->vmcb->save.rax & -1u) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1263 | | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1264 | svm->next_rip = svm->vmcb->save.rip + 2; |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1265 | if (svm_set_msr(&svm->vcpu, ecx, data)) |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 1266 | kvm_inject_gp(&svm->vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1267 | else |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1268 | skip_emulated_instruction(&svm->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1269 | return 1; |
| 1270 | } |
| 1271 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1272 | 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] | 1273 | { |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1274 | if (svm->vmcb->control.exit_info_1) |
| 1275 | return wrmsr_interception(svm, kvm_run); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1276 | else |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1277 | return rdmsr_interception(svm, kvm_run); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1278 | } |
| 1279 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1280 | static int interrupt_window_interception(struct vcpu_svm *svm, |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1281 | struct kvm_run *kvm_run) |
| 1282 | { |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 1283 | svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR); |
| 1284 | svm->vmcb->control.int_ctl &= ~V_IRQ_MASK; |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1285 | /* |
| 1286 | * If the user space waits to inject interrupts, exit as soon as |
| 1287 | * possible |
| 1288 | */ |
| 1289 | if (kvm_run->request_interrupt_window && |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1290 | !svm->vcpu.arch.irq_summary) { |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1291 | ++svm->vcpu.stat.irq_window_exits; |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1292 | kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; |
| 1293 | return 0; |
| 1294 | } |
| 1295 | |
| 1296 | return 1; |
| 1297 | } |
| 1298 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1299 | static int (*svm_exit_handlers[])(struct vcpu_svm *svm, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1300 | struct kvm_run *kvm_run) = { |
| 1301 | [SVM_EXIT_READ_CR0] = emulate_on_interception, |
| 1302 | [SVM_EXIT_READ_CR3] = emulate_on_interception, |
| 1303 | [SVM_EXIT_READ_CR4] = emulate_on_interception, |
Avi Kivity | 80a8119 | 2007-12-06 19:50:00 +0200 | [diff] [blame] | 1304 | [SVM_EXIT_READ_CR8] = emulate_on_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1305 | /* for now: */ |
| 1306 | [SVM_EXIT_WRITE_CR0] = emulate_on_interception, |
| 1307 | [SVM_EXIT_WRITE_CR3] = emulate_on_interception, |
| 1308 | [SVM_EXIT_WRITE_CR4] = emulate_on_interception, |
Joerg Roedel | 1d07543 | 2007-12-06 21:02:25 +0100 | [diff] [blame] | 1309 | [SVM_EXIT_WRITE_CR8] = cr8_write_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1310 | [SVM_EXIT_READ_DR0] = emulate_on_interception, |
| 1311 | [SVM_EXIT_READ_DR1] = emulate_on_interception, |
| 1312 | [SVM_EXIT_READ_DR2] = emulate_on_interception, |
| 1313 | [SVM_EXIT_READ_DR3] = emulate_on_interception, |
| 1314 | [SVM_EXIT_WRITE_DR0] = emulate_on_interception, |
| 1315 | [SVM_EXIT_WRITE_DR1] = emulate_on_interception, |
| 1316 | [SVM_EXIT_WRITE_DR2] = emulate_on_interception, |
| 1317 | [SVM_EXIT_WRITE_DR3] = emulate_on_interception, |
| 1318 | [SVM_EXIT_WRITE_DR5] = emulate_on_interception, |
| 1319 | [SVM_EXIT_WRITE_DR7] = emulate_on_interception, |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1320 | [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1321 | [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception, |
Anthony Liguori | 7807fa6 | 2007-04-23 09:17:21 -0500 | [diff] [blame] | 1322 | [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1323 | [SVM_EXIT_INTR] = nop_on_interception, |
| 1324 | [SVM_EXIT_NMI] = nop_on_interception, |
| 1325 | [SVM_EXIT_SMI] = nop_on_interception, |
| 1326 | [SVM_EXIT_INIT] = nop_on_interception, |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1327 | [SVM_EXIT_VINTR] = interrupt_window_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1328 | /* [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, */ |
| 1329 | [SVM_EXIT_CPUID] = cpuid_interception, |
Avi Kivity | cf5a94d | 2007-10-28 16:11:58 +0200 | [diff] [blame] | 1330 | [SVM_EXIT_INVD] = emulate_on_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1331 | [SVM_EXIT_HLT] = halt_interception, |
| 1332 | [SVM_EXIT_INVLPG] = emulate_on_interception, |
| 1333 | [SVM_EXIT_INVLPGA] = invalid_op_interception, |
| 1334 | [SVM_EXIT_IOIO] = io_interception, |
| 1335 | [SVM_EXIT_MSR] = msr_interception, |
| 1336 | [SVM_EXIT_TASK_SWITCH] = task_switch_interception, |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 1337 | [SVM_EXIT_SHUTDOWN] = shutdown_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1338 | [SVM_EXIT_VMRUN] = invalid_op_interception, |
Avi Kivity | 02e235b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1339 | [SVM_EXIT_VMMCALL] = vmmcall_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1340 | [SVM_EXIT_VMLOAD] = invalid_op_interception, |
| 1341 | [SVM_EXIT_VMSAVE] = invalid_op_interception, |
| 1342 | [SVM_EXIT_STGI] = invalid_op_interception, |
| 1343 | [SVM_EXIT_CLGI] = invalid_op_interception, |
| 1344 | [SVM_EXIT_SKINIT] = invalid_op_interception, |
Avi Kivity | cf5a94d | 2007-10-28 16:11:58 +0200 | [diff] [blame] | 1345 | [SVM_EXIT_WBINVD] = emulate_on_interception, |
Joerg Roedel | 916ce23 | 2007-03-21 19:47:00 +0100 | [diff] [blame] | 1346 | [SVM_EXIT_MONITOR] = invalid_op_interception, |
| 1347 | [SVM_EXIT_MWAIT] = invalid_op_interception, |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame^] | 1348 | [SVM_EXIT_NPF] = pf_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1349 | }; |
| 1350 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1351 | 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] | 1352 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1353 | struct vcpu_svm *svm = to_svm(vcpu); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1354 | u32 exit_code = svm->vmcb->control.exit_code; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1355 | |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame^] | 1356 | if (npt_enabled) { |
| 1357 | int mmu_reload = 0; |
| 1358 | if ((vcpu->arch.cr0 ^ svm->vmcb->save.cr0) & X86_CR0_PG) { |
| 1359 | svm_set_cr0(vcpu, svm->vmcb->save.cr0); |
| 1360 | mmu_reload = 1; |
| 1361 | } |
| 1362 | vcpu->arch.cr0 = svm->vmcb->save.cr0; |
| 1363 | vcpu->arch.cr3 = svm->vmcb->save.cr3; |
| 1364 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
| 1365 | if (!load_pdptrs(vcpu, vcpu->arch.cr3)) { |
| 1366 | kvm_inject_gp(vcpu, 0); |
| 1367 | return 1; |
| 1368 | } |
| 1369 | } |
| 1370 | if (mmu_reload) { |
| 1371 | kvm_mmu_reset_context(vcpu); |
| 1372 | kvm_mmu_load(vcpu); |
| 1373 | } |
| 1374 | } |
| 1375 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1376 | kvm_reput_irq(svm); |
| 1377 | |
| 1378 | if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) { |
| 1379 | kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 1380 | kvm_run->fail_entry.hardware_entry_failure_reason |
| 1381 | = svm->vmcb->control.exit_code; |
| 1382 | return 0; |
| 1383 | } |
| 1384 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1385 | if (is_external_interrupt(svm->vmcb->control.exit_int_info) && |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame^] | 1386 | exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR && |
| 1387 | exit_code != SVM_EXIT_NPF) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1388 | printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x " |
| 1389 | "exit_code 0x%x\n", |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1390 | __FUNCTION__, svm->vmcb->control.exit_int_info, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1391 | exit_code); |
| 1392 | |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 1393 | if (exit_code >= ARRAY_SIZE(svm_exit_handlers) |
Joe Perches | 56919c5 | 2007-11-12 20:06:51 -0800 | [diff] [blame] | 1394 | || !svm_exit_handlers[exit_code]) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1395 | kvm_run->exit_reason = KVM_EXIT_UNKNOWN; |
Avi Kivity | 364b625 | 2007-04-16 14:28:40 +0300 | [diff] [blame] | 1396 | kvm_run->hw.hardware_exit_reason = exit_code; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1397 | return 0; |
| 1398 | } |
| 1399 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1400 | return svm_exit_handlers[exit_code](svm, kvm_run); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | static void reload_tss(struct kvm_vcpu *vcpu) |
| 1404 | { |
| 1405 | int cpu = raw_smp_processor_id(); |
| 1406 | |
| 1407 | struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu); |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1408 | svm_data->tss_desc->type = 9; /* available 32/64-bit TSS */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1409 | load_TR_desc(); |
| 1410 | } |
| 1411 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1412 | static void pre_svm_run(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1413 | { |
| 1414 | int cpu = raw_smp_processor_id(); |
| 1415 | |
| 1416 | struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu); |
| 1417 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1418 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING; |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1419 | if (svm->vcpu.cpu != cpu || |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1420 | svm->asid_generation != svm_data->asid_generation) |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1421 | new_asid(svm, svm_data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 1425 | static inline void svm_inject_irq(struct vcpu_svm *svm, int irq) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1426 | { |
| 1427 | struct vmcb_control_area *control; |
| 1428 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1429 | control = &svm->vmcb->control; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 1430 | control->int_vector = irq; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1431 | control->int_ctl &= ~V_INTR_PRIO_MASK; |
| 1432 | control->int_ctl |= V_IRQ_MASK | |
| 1433 | ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT); |
| 1434 | } |
| 1435 | |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 1436 | static void svm_set_irq(struct kvm_vcpu *vcpu, int irq) |
| 1437 | { |
| 1438 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1439 | |
| 1440 | svm_inject_irq(svm, irq); |
| 1441 | } |
| 1442 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1443 | static void svm_intr_assist(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1444 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1445 | struct vcpu_svm *svm = to_svm(vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 1446 | struct vmcb *vmcb = svm->vmcb; |
| 1447 | int intr_vector = -1; |
| 1448 | |
| 1449 | if ((vmcb->control.exit_int_info & SVM_EVTINJ_VALID) && |
| 1450 | ((vmcb->control.exit_int_info & SVM_EVTINJ_TYPE_MASK) == 0)) { |
| 1451 | intr_vector = vmcb->control.exit_int_info & |
| 1452 | SVM_EVTINJ_VEC_MASK; |
| 1453 | vmcb->control.exit_int_info = 0; |
| 1454 | svm_inject_irq(svm, intr_vector); |
| 1455 | return; |
| 1456 | } |
| 1457 | |
| 1458 | if (vmcb->control.int_ctl & V_IRQ_MASK) |
| 1459 | return; |
| 1460 | |
Eddie Dong | 1b9778d | 2007-09-03 16:56:58 +0300 | [diff] [blame] | 1461 | if (!kvm_cpu_has_interrupt(vcpu)) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 1462 | return; |
| 1463 | |
| 1464 | if (!(vmcb->save.rflags & X86_EFLAGS_IF) || |
| 1465 | (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) || |
| 1466 | (vmcb->control.event_inj & SVM_EVTINJ_VALID)) { |
| 1467 | /* unable to deliver irq, set pending irq */ |
| 1468 | vmcb->control.intercept |= (1ULL << INTERCEPT_VINTR); |
| 1469 | svm_inject_irq(svm, 0x0); |
| 1470 | return; |
| 1471 | } |
| 1472 | /* Okay, we can deliver the interrupt: grab it and update PIC state. */ |
Eddie Dong | 1b9778d | 2007-09-03 16:56:58 +0300 | [diff] [blame] | 1473 | intr_vector = kvm_cpu_get_interrupt(vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 1474 | svm_inject_irq(svm, intr_vector); |
Eddie Dong | 1b9778d | 2007-09-03 16:56:58 +0300 | [diff] [blame] | 1475 | kvm_timer_intr_post(vcpu, intr_vector); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | static void kvm_reput_irq(struct vcpu_svm *svm) |
| 1479 | { |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1480 | struct vmcb_control_area *control = &svm->vmcb->control; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1481 | |
Eddie Dong | 7017fc3 | 2007-07-18 11:34:57 +0300 | [diff] [blame] | 1482 | if ((control->int_ctl & V_IRQ_MASK) |
| 1483 | && !irqchip_in_kernel(svm->vcpu.kvm)) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1484 | control->int_ctl &= ~V_IRQ_MASK; |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1485 | push_irq(&svm->vcpu, control->int_vector); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1486 | } |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1487 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1488 | svm->vcpu.arch.interrupt_window_open = |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1489 | !(control->int_state & SVM_INTERRUPT_SHADOW_MASK); |
| 1490 | } |
| 1491 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 1492 | static void svm_do_inject_vector(struct vcpu_svm *svm) |
| 1493 | { |
| 1494 | struct kvm_vcpu *vcpu = &svm->vcpu; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1495 | int word_index = __ffs(vcpu->arch.irq_summary); |
| 1496 | int bit_index = __ffs(vcpu->arch.irq_pending[word_index]); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 1497 | int irq = word_index * BITS_PER_LONG + bit_index; |
| 1498 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1499 | clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]); |
| 1500 | if (!vcpu->arch.irq_pending[word_index]) |
| 1501 | clear_bit(word_index, &vcpu->arch.irq_summary); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 1502 | svm_inject_irq(svm, irq); |
| 1503 | } |
| 1504 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1505 | static void do_interrupt_requests(struct kvm_vcpu *vcpu, |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1506 | struct kvm_run *kvm_run) |
| 1507 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1508 | struct vcpu_svm *svm = to_svm(vcpu); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1509 | struct vmcb_control_area *control = &svm->vmcb->control; |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1510 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1511 | svm->vcpu.arch.interrupt_window_open = |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1512 | (!(control->int_state & SVM_INTERRUPT_SHADOW_MASK) && |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1513 | (svm->vmcb->save.rflags & X86_EFLAGS_IF)); |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1514 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1515 | if (svm->vcpu.arch.interrupt_window_open && svm->vcpu.arch.irq_summary) |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1516 | /* |
| 1517 | * If interrupts enabled, and not blocked by sti or mov ss. Good. |
| 1518 | */ |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 1519 | svm_do_inject_vector(svm); |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1520 | |
| 1521 | /* |
| 1522 | * Interrupts blocked. Wait for unblock. |
| 1523 | */ |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1524 | if (!svm->vcpu.arch.interrupt_window_open && |
| 1525 | (svm->vcpu.arch.irq_summary || kvm_run->request_interrupt_window)) |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1526 | control->intercept |= 1ULL << INTERCEPT_VINTR; |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1527 | else |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1528 | control->intercept &= ~(1ULL << INTERCEPT_VINTR); |
| 1529 | } |
| 1530 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 1531 | static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 1532 | { |
| 1533 | return 0; |
| 1534 | } |
| 1535 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1536 | static void save_db_regs(unsigned long *db_regs) |
| 1537 | { |
Avi Kivity | 5aff458 | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1538 | asm volatile ("mov %%dr0, %0" : "=r"(db_regs[0])); |
| 1539 | asm volatile ("mov %%dr1, %0" : "=r"(db_regs[1])); |
| 1540 | asm volatile ("mov %%dr2, %0" : "=r"(db_regs[2])); |
| 1541 | asm volatile ("mov %%dr3, %0" : "=r"(db_regs[3])); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | static void load_db_regs(unsigned long *db_regs) |
| 1545 | { |
Avi Kivity | 5aff458 | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1546 | asm volatile ("mov %0, %%dr0" : : "r"(db_regs[0])); |
| 1547 | asm volatile ("mov %0, %%dr1" : : "r"(db_regs[1])); |
| 1548 | asm volatile ("mov %0, %%dr2" : : "r"(db_regs[2])); |
| 1549 | asm volatile ("mov %0, %%dr3" : : "r"(db_regs[3])); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1550 | } |
| 1551 | |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 1552 | static void svm_flush_tlb(struct kvm_vcpu *vcpu) |
| 1553 | { |
| 1554 | force_new_asid(vcpu); |
| 1555 | } |
| 1556 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1557 | static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu) |
| 1558 | { |
| 1559 | } |
| 1560 | |
| 1561 | 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] | 1562 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1563 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1564 | u16 fs_selector; |
| 1565 | u16 gs_selector; |
| 1566 | u16 ldt_selector; |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 1567 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1568 | pre_svm_run(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1569 | |
| 1570 | save_host_msrs(vcpu); |
| 1571 | fs_selector = read_fs(); |
| 1572 | gs_selector = read_gs(); |
| 1573 | ldt_selector = read_ldt(); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1574 | svm->host_cr2 = kvm_read_cr2(); |
| 1575 | svm->host_dr6 = read_dr6(); |
| 1576 | svm->host_dr7 = read_dr7(); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1577 | svm->vmcb->save.cr2 = vcpu->arch.cr2; |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame^] | 1578 | /* required for live migration with NPT */ |
| 1579 | if (npt_enabled) |
| 1580 | svm->vmcb->save.cr3 = vcpu->arch.cr3; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1581 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1582 | if (svm->vmcb->save.dr7 & 0xff) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1583 | write_dr7(0); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1584 | save_db_regs(svm->host_db_regs); |
| 1585 | load_db_regs(svm->db_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1586 | } |
Avi Kivity | 36241b8 | 2006-12-22 01:05:20 -0800 | [diff] [blame] | 1587 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1588 | clgi(); |
| 1589 | |
| 1590 | local_irq_enable(); |
Avi Kivity | 36241b8 | 2006-12-22 01:05:20 -0800 | [diff] [blame] | 1591 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1592 | asm volatile ( |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1593 | #ifdef CONFIG_X86_64 |
Laurent Vivier | 54a08c0 | 2007-10-25 14:18:53 +0200 | [diff] [blame] | 1594 | "push %%rbp; \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1595 | #else |
Laurent Vivier | fe7935d | 2007-10-25 14:18:54 +0200 | [diff] [blame] | 1596 | "push %%ebp; \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1597 | #endif |
| 1598 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1599 | #ifdef CONFIG_X86_64 |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1600 | "mov %c[rbx](%[svm]), %%rbx \n\t" |
| 1601 | "mov %c[rcx](%[svm]), %%rcx \n\t" |
| 1602 | "mov %c[rdx](%[svm]), %%rdx \n\t" |
| 1603 | "mov %c[rsi](%[svm]), %%rsi \n\t" |
| 1604 | "mov %c[rdi](%[svm]), %%rdi \n\t" |
| 1605 | "mov %c[rbp](%[svm]), %%rbp \n\t" |
| 1606 | "mov %c[r8](%[svm]), %%r8 \n\t" |
| 1607 | "mov %c[r9](%[svm]), %%r9 \n\t" |
| 1608 | "mov %c[r10](%[svm]), %%r10 \n\t" |
| 1609 | "mov %c[r11](%[svm]), %%r11 \n\t" |
| 1610 | "mov %c[r12](%[svm]), %%r12 \n\t" |
| 1611 | "mov %c[r13](%[svm]), %%r13 \n\t" |
| 1612 | "mov %c[r14](%[svm]), %%r14 \n\t" |
| 1613 | "mov %c[r15](%[svm]), %%r15 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1614 | #else |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1615 | "mov %c[rbx](%[svm]), %%ebx \n\t" |
| 1616 | "mov %c[rcx](%[svm]), %%ecx \n\t" |
| 1617 | "mov %c[rdx](%[svm]), %%edx \n\t" |
| 1618 | "mov %c[rsi](%[svm]), %%esi \n\t" |
| 1619 | "mov %c[rdi](%[svm]), %%edi \n\t" |
| 1620 | "mov %c[rbp](%[svm]), %%ebp \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1621 | #endif |
| 1622 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1623 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1624 | /* Enter guest mode */ |
| 1625 | "push %%rax \n\t" |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1626 | "mov %c[vmcb](%[svm]), %%rax \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1627 | SVM_VMLOAD "\n\t" |
| 1628 | SVM_VMRUN "\n\t" |
| 1629 | SVM_VMSAVE "\n\t" |
| 1630 | "pop %%rax \n\t" |
| 1631 | #else |
| 1632 | /* Enter guest mode */ |
| 1633 | "push %%eax \n\t" |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1634 | "mov %c[vmcb](%[svm]), %%eax \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1635 | SVM_VMLOAD "\n\t" |
| 1636 | SVM_VMRUN "\n\t" |
| 1637 | SVM_VMSAVE "\n\t" |
| 1638 | "pop %%eax \n\t" |
| 1639 | #endif |
| 1640 | |
| 1641 | /* Save guest registers, load host registers */ |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1642 | #ifdef CONFIG_X86_64 |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1643 | "mov %%rbx, %c[rbx](%[svm]) \n\t" |
| 1644 | "mov %%rcx, %c[rcx](%[svm]) \n\t" |
| 1645 | "mov %%rdx, %c[rdx](%[svm]) \n\t" |
| 1646 | "mov %%rsi, %c[rsi](%[svm]) \n\t" |
| 1647 | "mov %%rdi, %c[rdi](%[svm]) \n\t" |
| 1648 | "mov %%rbp, %c[rbp](%[svm]) \n\t" |
| 1649 | "mov %%r8, %c[r8](%[svm]) \n\t" |
| 1650 | "mov %%r9, %c[r9](%[svm]) \n\t" |
| 1651 | "mov %%r10, %c[r10](%[svm]) \n\t" |
| 1652 | "mov %%r11, %c[r11](%[svm]) \n\t" |
| 1653 | "mov %%r12, %c[r12](%[svm]) \n\t" |
| 1654 | "mov %%r13, %c[r13](%[svm]) \n\t" |
| 1655 | "mov %%r14, %c[r14](%[svm]) \n\t" |
| 1656 | "mov %%r15, %c[r15](%[svm]) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1657 | |
Laurent Vivier | 54a08c0 | 2007-10-25 14:18:53 +0200 | [diff] [blame] | 1658 | "pop %%rbp; \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1659 | #else |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1660 | "mov %%ebx, %c[rbx](%[svm]) \n\t" |
| 1661 | "mov %%ecx, %c[rcx](%[svm]) \n\t" |
| 1662 | "mov %%edx, %c[rdx](%[svm]) \n\t" |
| 1663 | "mov %%esi, %c[rsi](%[svm]) \n\t" |
| 1664 | "mov %%edi, %c[rdi](%[svm]) \n\t" |
| 1665 | "mov %%ebp, %c[rbp](%[svm]) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1666 | |
Laurent Vivier | fe7935d | 2007-10-25 14:18:54 +0200 | [diff] [blame] | 1667 | "pop %%ebp; \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1668 | #endif |
| 1669 | : |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1670 | : [svm]"a"(svm), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1671 | [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1672 | [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 1673 | [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 1674 | [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 1675 | [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 1676 | [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 1677 | [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP])) |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1678 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1679 | , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])), |
| 1680 | [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])), |
| 1681 | [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])), |
| 1682 | [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])), |
| 1683 | [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])), |
| 1684 | [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])), |
| 1685 | [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])), |
| 1686 | [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15])) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1687 | #endif |
Laurent Vivier | 54a08c0 | 2007-10-25 14:18:53 +0200 | [diff] [blame] | 1688 | : "cc", "memory" |
| 1689 | #ifdef CONFIG_X86_64 |
| 1690 | , "rbx", "rcx", "rdx", "rsi", "rdi" |
| 1691 | , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15" |
Laurent Vivier | fe7935d | 2007-10-25 14:18:54 +0200 | [diff] [blame] | 1692 | #else |
| 1693 | , "ebx", "ecx", "edx" , "esi", "edi" |
Laurent Vivier | 54a08c0 | 2007-10-25 14:18:53 +0200 | [diff] [blame] | 1694 | #endif |
| 1695 | ); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1696 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1697 | if ((svm->vmcb->save.dr7 & 0xff)) |
| 1698 | load_db_regs(svm->host_db_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1699 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1700 | vcpu->arch.cr2 = svm->vmcb->save.cr2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1701 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1702 | write_dr6(svm->host_dr6); |
| 1703 | write_dr7(svm->host_dr7); |
| 1704 | kvm_write_cr2(svm->host_cr2); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1705 | |
| 1706 | load_fs(fs_selector); |
| 1707 | load_gs(gs_selector); |
| 1708 | load_ldt(ldt_selector); |
| 1709 | load_host_msrs(vcpu); |
| 1710 | |
| 1711 | reload_tss(vcpu); |
| 1712 | |
Avi Kivity | 56ba47d | 2007-11-07 17:14:18 +0200 | [diff] [blame] | 1713 | local_irq_disable(); |
| 1714 | |
| 1715 | stgi(); |
| 1716 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1717 | svm->next_rip = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1718 | } |
| 1719 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1720 | static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root) |
| 1721 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1722 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1723 | |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame^] | 1724 | if (npt_enabled) { |
| 1725 | svm->vmcb->control.nested_cr3 = root; |
| 1726 | force_new_asid(vcpu); |
| 1727 | return; |
| 1728 | } |
| 1729 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1730 | svm->vmcb->save.cr3 = root; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1731 | force_new_asid(vcpu); |
Anthony Liguori | 7807fa6 | 2007-04-23 09:17:21 -0500 | [diff] [blame] | 1732 | |
| 1733 | if (vcpu->fpu_active) { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1734 | svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR); |
| 1735 | svm->vmcb->save.cr0 |= X86_CR0_TS; |
Anthony Liguori | 7807fa6 | 2007-04-23 09:17:21 -0500 | [diff] [blame] | 1736 | vcpu->fpu_active = 0; |
| 1737 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1738 | } |
| 1739 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1740 | static int is_disabled(void) |
| 1741 | { |
Joerg Roedel | 6031a61 | 2007-06-22 12:29:50 +0300 | [diff] [blame] | 1742 | u64 vm_cr; |
| 1743 | |
| 1744 | rdmsrl(MSR_VM_CR, vm_cr); |
| 1745 | if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE)) |
| 1746 | return 1; |
| 1747 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1748 | return 0; |
| 1749 | } |
| 1750 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1751 | static void |
| 1752 | svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 1753 | { |
| 1754 | /* |
| 1755 | * Patch in the VMMCALL instruction: |
| 1756 | */ |
| 1757 | hypercall[0] = 0x0f; |
| 1758 | hypercall[1] = 0x01; |
| 1759 | hypercall[2] = 0xd9; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1760 | } |
| 1761 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1762 | static void svm_check_processor_compat(void *rtn) |
| 1763 | { |
| 1764 | *(int *)rtn = 0; |
| 1765 | } |
| 1766 | |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 1767 | static bool svm_cpu_has_accelerated_tpr(void) |
| 1768 | { |
| 1769 | return false; |
| 1770 | } |
| 1771 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1772 | static struct kvm_x86_ops svm_x86_ops = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1773 | .cpu_has_kvm_support = has_svm, |
| 1774 | .disabled_by_bios = is_disabled, |
| 1775 | .hardware_setup = svm_hardware_setup, |
| 1776 | .hardware_unsetup = svm_hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1777 | .check_processor_compatibility = svm_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1778 | .hardware_enable = svm_hardware_enable, |
| 1779 | .hardware_disable = svm_hardware_disable, |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 1780 | .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1781 | |
| 1782 | .vcpu_create = svm_create_vcpu, |
| 1783 | .vcpu_free = svm_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1784 | .vcpu_reset = svm_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1785 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1786 | .prepare_guest_switch = svm_prepare_guest_switch, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1787 | .vcpu_load = svm_vcpu_load, |
| 1788 | .vcpu_put = svm_vcpu_put, |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1789 | .vcpu_decache = svm_vcpu_decache, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1790 | |
| 1791 | .set_guest_debug = svm_guest_debug, |
| 1792 | .get_msr = svm_get_msr, |
| 1793 | .set_msr = svm_set_msr, |
| 1794 | .get_segment_base = svm_get_segment_base, |
| 1795 | .get_segment = svm_get_segment, |
| 1796 | .set_segment = svm_set_segment, |
Rusty Russell | 1747fb7 | 2007-09-06 01:21:32 +1000 | [diff] [blame] | 1797 | .get_cs_db_l_bits = kvm_get_cs_db_l_bits, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 1798 | .decache_cr4_guest_bits = svm_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1799 | .set_cr0 = svm_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1800 | .set_cr3 = svm_set_cr3, |
| 1801 | .set_cr4 = svm_set_cr4, |
| 1802 | .set_efer = svm_set_efer, |
| 1803 | .get_idt = svm_get_idt, |
| 1804 | .set_idt = svm_set_idt, |
| 1805 | .get_gdt = svm_get_gdt, |
| 1806 | .set_gdt = svm_set_gdt, |
| 1807 | .get_dr = svm_get_dr, |
| 1808 | .set_dr = svm_set_dr, |
| 1809 | .cache_regs = svm_cache_regs, |
| 1810 | .decache_regs = svm_decache_regs, |
| 1811 | .get_rflags = svm_get_rflags, |
| 1812 | .set_rflags = svm_set_rflags, |
| 1813 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1814 | .tlb_flush = svm_flush_tlb, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1815 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1816 | .run = svm_vcpu_run, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1817 | .handle_exit = handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1818 | .skip_emulated_instruction = skip_emulated_instruction, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1819 | .patch_hypercall = svm_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 1820 | .get_irq = svm_get_irq, |
| 1821 | .set_irq = svm_set_irq, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 1822 | .queue_exception = svm_queue_exception, |
| 1823 | .exception_injected = svm_exception_injected, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1824 | .inject_pending_irq = svm_intr_assist, |
| 1825 | .inject_pending_vectors = do_interrupt_requests, |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 1826 | |
| 1827 | .set_tss_addr = svm_set_tss_addr, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1828 | }; |
| 1829 | |
| 1830 | static int __init svm_init(void) |
| 1831 | { |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 1832 | return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm), |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 1833 | THIS_MODULE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1834 | } |
| 1835 | |
| 1836 | static void __exit svm_exit(void) |
| 1837 | { |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 1838 | kvm_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1839 | } |
| 1840 | |
| 1841 | module_init(svm_init) |
| 1842 | module_exit(svm_exit) |