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