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. |
Avi Kivity | 221d059 | 2010-05-23 18:37:00 +0300 | [diff] [blame] | 7 | * Copyright 2010 Red Hat, Inc. and/or its affilates. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8 | * |
| 9 | * Authors: |
| 10 | * Yaniv Kamay <yaniv@qumranet.com> |
| 11 | * Avi Kivity <avi@qumranet.com> |
| 12 | * |
| 13 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 14 | * the COPYING file in the top-level directory. |
| 15 | * |
| 16 | */ |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 17 | #include <linux/kvm_host.h> |
| 18 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 19 | #include "irq.h" |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 20 | #include "mmu.h" |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 21 | #include "kvm_cache_regs.h" |
Gleb Natapov | fe4c7b1 | 2009-03-23 11:23:18 +0200 | [diff] [blame] | 22 | #include "x86.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 23 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 24 | #include <linux/module.h> |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 25 | #include <linux/kernel.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 26 | #include <linux/vmalloc.h> |
| 27 | #include <linux/highmem.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 28 | #include <linux/sched.h> |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 29 | #include <linux/ftrace_event.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 31 | |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 32 | #include <asm/tlbflush.h> |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 33 | #include <asm/desc.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 34 | |
Eduardo Habkost | 63d1142 | 2008-11-17 19:03:20 -0200 | [diff] [blame] | 35 | #include <asm/virtext.h> |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 36 | #include "trace.h" |
Eduardo Habkost | 63d1142 | 2008-11-17 19:03:20 -0200 | [diff] [blame] | 37 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 38 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
| 39 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 40 | MODULE_AUTHOR("Qumranet"); |
| 41 | MODULE_LICENSE("GPL"); |
| 42 | |
| 43 | #define IOPM_ALLOC_ORDER 2 |
| 44 | #define MSRPM_ALLOC_ORDER 1 |
| 45 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 46 | #define SEG_TYPE_LDT 2 |
| 47 | #define SEG_TYPE_BUSY_TSS16 3 |
| 48 | |
Andre Przywara | 6bc31bd | 2010-04-11 23:07:28 +0200 | [diff] [blame] | 49 | #define SVM_FEATURE_NPT (1 << 0) |
| 50 | #define SVM_FEATURE_LBRV (1 << 1) |
| 51 | #define SVM_FEATURE_SVML (1 << 2) |
| 52 | #define SVM_FEATURE_NRIP (1 << 3) |
| 53 | #define SVM_FEATURE_PAUSE_FILTER (1 << 10) |
Joerg Roedel | 80b7706 | 2007-03-30 17:02:14 +0300 | [diff] [blame] | 54 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 55 | #define NESTED_EXIT_HOST 0 /* Exit handled on host level */ |
| 56 | #define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */ |
| 57 | #define NESTED_EXIT_CONTINUE 2 /* Further checks needed */ |
| 58 | |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 59 | #define DEBUGCTL_RESERVED_BITS (~(0x3fULL)) |
| 60 | |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 61 | static bool erratum_383_found __read_mostly; |
| 62 | |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 63 | static const u32 host_save_user_msrs[] = { |
| 64 | #ifdef CONFIG_X86_64 |
| 65 | MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE, |
| 66 | MSR_FS_BASE, |
| 67 | #endif |
| 68 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, |
| 69 | }; |
| 70 | |
| 71 | #define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs) |
| 72 | |
| 73 | struct kvm_vcpu; |
| 74 | |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 75 | struct nested_state { |
| 76 | struct vmcb *hsave; |
| 77 | u64 hsave_msr; |
Joerg Roedel | 4a81018 | 2010-02-24 18:59:15 +0100 | [diff] [blame] | 78 | u64 vm_cr_msr; |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 79 | u64 vmcb; |
| 80 | |
| 81 | /* These are the merged vectors */ |
| 82 | u32 *msrpm; |
| 83 | |
| 84 | /* gpa pointers to the real vectors */ |
| 85 | u64 vmcb_msrpm; |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 86 | u64 vmcb_iopm; |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 87 | |
Joerg Roedel | cd3ff65 | 2009-10-09 16:08:26 +0200 | [diff] [blame] | 88 | /* A VMEXIT is required but not yet emulated */ |
| 89 | bool exit_required; |
| 90 | |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 91 | /* cache for intercepts of the guest */ |
| 92 | u16 intercept_cr_read; |
| 93 | u16 intercept_cr_write; |
| 94 | u16 intercept_dr_read; |
| 95 | u16 intercept_dr_write; |
| 96 | u32 intercept_exceptions; |
| 97 | u64 intercept; |
| 98 | |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 99 | }; |
| 100 | |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 101 | #define MSRPM_OFFSETS 16 |
| 102 | static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly; |
| 103 | |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 104 | struct vcpu_svm { |
| 105 | struct kvm_vcpu vcpu; |
| 106 | struct vmcb *vmcb; |
| 107 | unsigned long vmcb_pa; |
| 108 | struct svm_cpu_data *svm_data; |
| 109 | uint64_t asid_generation; |
| 110 | uint64_t sysenter_esp; |
| 111 | uint64_t sysenter_eip; |
| 112 | |
| 113 | u64 next_rip; |
| 114 | |
| 115 | u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS]; |
| 116 | u64 host_gs_base; |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 117 | |
| 118 | u32 *msrpm; |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 119 | |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 120 | struct nested_state nested; |
Jan Kiszka | 6be7d30 | 2009-10-18 13:24:54 +0200 | [diff] [blame] | 121 | |
| 122 | bool nmi_singlestep; |
Jan Kiszka | 66b7138 | 2010-02-23 17:47:56 +0100 | [diff] [blame] | 123 | |
| 124 | unsigned int3_injected; |
| 125 | unsigned long int3_rip; |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 126 | }; |
| 127 | |
Joerg Roedel | 455716f | 2010-03-01 15:34:35 +0100 | [diff] [blame] | 128 | #define MSR_INVALID 0xffffffffU |
| 129 | |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 130 | static struct svm_direct_access_msrs { |
| 131 | u32 index; /* Index of the MSR */ |
| 132 | bool always; /* True if intercept is always on */ |
| 133 | } direct_access_msrs[] = { |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 134 | { .index = MSR_STAR, .always = true }, |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 135 | { .index = MSR_IA32_SYSENTER_CS, .always = true }, |
| 136 | #ifdef CONFIG_X86_64 |
| 137 | { .index = MSR_GS_BASE, .always = true }, |
| 138 | { .index = MSR_FS_BASE, .always = true }, |
| 139 | { .index = MSR_KERNEL_GS_BASE, .always = true }, |
| 140 | { .index = MSR_LSTAR, .always = true }, |
| 141 | { .index = MSR_CSTAR, .always = true }, |
| 142 | { .index = MSR_SYSCALL_MASK, .always = true }, |
| 143 | #endif |
| 144 | { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false }, |
| 145 | { .index = MSR_IA32_LASTBRANCHTOIP, .always = false }, |
| 146 | { .index = MSR_IA32_LASTINTFROMIP, .always = false }, |
| 147 | { .index = MSR_IA32_LASTINTTOIP, .always = false }, |
| 148 | { .index = MSR_INVALID, .always = false }, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | /* enable NPT for AMD64 and X86 with PAE */ |
| 152 | #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE) |
| 153 | static bool npt_enabled = true; |
| 154 | #else |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 155 | static bool npt_enabled; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 156 | #endif |
| 157 | static int npt = 1; |
| 158 | |
| 159 | module_param(npt, int, S_IRUGO); |
| 160 | |
Joerg Roedel | 4b6e4dc | 2009-08-07 11:49:48 +0200 | [diff] [blame] | 161 | static int nested = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 162 | module_param(nested, int, S_IRUGO); |
| 163 | |
| 164 | static void svm_flush_tlb(struct kvm_vcpu *vcpu); |
Joerg Roedel | a5c3832 | 2009-08-07 11:49:32 +0200 | [diff] [blame] | 165 | static void svm_complete_interrupts(struct vcpu_svm *svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 166 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 167 | static int nested_svm_exit_handled(struct vcpu_svm *svm); |
Joerg Roedel | b8e88bc | 2010-02-19 16:23:02 +0100 | [diff] [blame] | 168 | static int nested_svm_intercept(struct vcpu_svm *svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 169 | static int nested_svm_vmexit(struct vcpu_svm *svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 170 | static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr, |
| 171 | bool has_error_code, u32 error_code); |
| 172 | |
| 173 | static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu) |
| 174 | { |
| 175 | return container_of(vcpu, struct vcpu_svm, vcpu); |
| 176 | } |
| 177 | |
| 178 | static inline bool is_nested(struct vcpu_svm *svm) |
| 179 | { |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 180 | return svm->nested.vmcb; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 183 | static inline void enable_gif(struct vcpu_svm *svm) |
| 184 | { |
| 185 | svm->vcpu.arch.hflags |= HF_GIF_MASK; |
| 186 | } |
| 187 | |
| 188 | static inline void disable_gif(struct vcpu_svm *svm) |
| 189 | { |
| 190 | svm->vcpu.arch.hflags &= ~HF_GIF_MASK; |
| 191 | } |
| 192 | |
| 193 | static inline bool gif_set(struct vcpu_svm *svm) |
| 194 | { |
| 195 | return !!(svm->vcpu.arch.hflags & HF_GIF_MASK); |
| 196 | } |
| 197 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 198 | static unsigned long iopm_base; |
| 199 | |
| 200 | struct kvm_ldttss_desc { |
| 201 | u16 limit0; |
| 202 | u16 base0; |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 203 | unsigned base1:8, type:5, dpl:2, p:1; |
| 204 | unsigned limit1:4, zero0:3, g:1, base2:8; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 205 | u32 base3; |
| 206 | u32 zero1; |
| 207 | } __attribute__((packed)); |
| 208 | |
| 209 | struct svm_cpu_data { |
| 210 | int cpu; |
| 211 | |
Avi Kivity | 5008fdf | 2007-04-02 13:05:50 +0300 | [diff] [blame] | 212 | u64 asid_generation; |
| 213 | u32 max_asid; |
| 214 | u32 next_asid; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 215 | struct kvm_ldttss_desc *tss_desc; |
| 216 | |
| 217 | struct page *save_area; |
| 218 | }; |
| 219 | |
| 220 | static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data); |
Joerg Roedel | 80b7706 | 2007-03-30 17:02:14 +0300 | [diff] [blame] | 221 | static uint32_t svm_features; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 222 | |
| 223 | struct svm_init_data { |
| 224 | int cpu; |
| 225 | int r; |
| 226 | }; |
| 227 | |
| 228 | static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000}; |
| 229 | |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 230 | #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 231 | #define MSRS_RANGE_SIZE 2048 |
| 232 | #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2) |
| 233 | |
Joerg Roedel | 455716f | 2010-03-01 15:34:35 +0100 | [diff] [blame] | 234 | static u32 svm_msrpm_offset(u32 msr) |
| 235 | { |
| 236 | u32 offset; |
| 237 | int i; |
| 238 | |
| 239 | for (i = 0; i < NUM_MSR_MAPS; i++) { |
| 240 | if (msr < msrpm_ranges[i] || |
| 241 | msr >= msrpm_ranges[i] + MSRS_IN_RANGE) |
| 242 | continue; |
| 243 | |
| 244 | offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */ |
| 245 | offset += (i * MSRS_RANGE_SIZE); /* add range offset */ |
| 246 | |
| 247 | /* Now we have the u8 offset - but need the u32 offset */ |
| 248 | return offset / 4; |
| 249 | } |
| 250 | |
| 251 | /* MSR not in any range */ |
| 252 | return MSR_INVALID; |
| 253 | } |
| 254 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 255 | #define MAX_INST_SIZE 15 |
| 256 | |
Joerg Roedel | 80b7706 | 2007-03-30 17:02:14 +0300 | [diff] [blame] | 257 | static inline u32 svm_has(u32 feat) |
| 258 | { |
| 259 | return svm_features & feat; |
| 260 | } |
| 261 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 262 | static inline void clgi(void) |
| 263 | { |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 264 | asm volatile (__ex(SVM_CLGI)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | static inline void stgi(void) |
| 268 | { |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 269 | asm volatile (__ex(SVM_STGI)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | static inline void invlpga(unsigned long addr, u32 asid) |
| 273 | { |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 274 | asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 275 | } |
| 276 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 277 | static inline void force_new_asid(struct kvm_vcpu *vcpu) |
| 278 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 279 | to_svm(vcpu)->asid_generation--; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | static inline void flush_guest_tlb(struct kvm_vcpu *vcpu) |
| 283 | { |
| 284 | force_new_asid(vcpu); |
| 285 | } |
| 286 | |
| 287 | static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 288 | { |
Zachary Amsden | 6dc696d | 2010-05-26 15:09:43 -1000 | [diff] [blame] | 289 | vcpu->arch.efer = efer; |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 290 | if (!npt_enabled && !(efer & EFER_LMA)) |
Carlo Marcelo Arenas Belon | 2b5203e | 2007-12-01 06:17:11 -0600 | [diff] [blame] | 291 | efer &= ~EFER_LME; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 292 | |
Alexander Graf | 9962d03 | 2008-11-25 20:17:02 +0100 | [diff] [blame] | 293 | to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 296 | static int is_external_interrupt(u32 info) |
| 297 | { |
| 298 | info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID; |
| 299 | return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR); |
| 300 | } |
| 301 | |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 302 | static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 303 | { |
| 304 | struct vcpu_svm *svm = to_svm(vcpu); |
| 305 | u32 ret = 0; |
| 306 | |
| 307 | if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 308 | ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 309 | return ret & mask; |
| 310 | } |
| 311 | |
| 312 | static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 313 | { |
| 314 | struct vcpu_svm *svm = to_svm(vcpu); |
| 315 | |
| 316 | if (mask == 0) |
| 317 | svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK; |
| 318 | else |
| 319 | svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK; |
| 320 | |
| 321 | } |
| 322 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 323 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 324 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 325 | struct vcpu_svm *svm = to_svm(vcpu); |
| 326 | |
Andre Przywara | 6bc31bd | 2010-04-11 23:07:28 +0200 | [diff] [blame] | 327 | if (svm->vmcb->control.next_rip != 0) |
| 328 | svm->next_rip = svm->vmcb->control.next_rip; |
| 329 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 330 | if (!svm->next_rip) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 331 | if (emulate_instruction(vcpu, 0, 0, EMULTYPE_SKIP) != |
Gleb Natapov | f629cf8 | 2009-05-11 13:35:49 +0300 | [diff] [blame] | 332 | EMULATE_DONE) |
| 333 | printk(KERN_DEBUG "%s: NOP\n", __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 334 | return; |
| 335 | } |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 336 | if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE) |
| 337 | printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n", |
| 338 | __func__, kvm_rip_read(vcpu), svm->next_rip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 339 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 340 | kvm_rip_write(vcpu, svm->next_rip); |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 341 | svm_set_interrupt_shadow(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 342 | } |
| 343 | |
Jan Kiszka | 116a475 | 2010-02-23 17:47:54 +0100 | [diff] [blame] | 344 | static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr, |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 345 | bool has_error_code, u32 error_code, |
| 346 | bool reinject) |
Jan Kiszka | 116a475 | 2010-02-23 17:47:54 +0100 | [diff] [blame] | 347 | { |
| 348 | struct vcpu_svm *svm = to_svm(vcpu); |
| 349 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 350 | /* |
| 351 | * If we are within a nested VM we'd better #VMEXIT and let the guest |
| 352 | * handle the exception |
| 353 | */ |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 354 | if (!reinject && |
| 355 | nested_svm_check_exception(svm, nr, has_error_code, error_code)) |
Jan Kiszka | 116a475 | 2010-02-23 17:47:54 +0100 | [diff] [blame] | 356 | return; |
| 357 | |
Jan Kiszka | 66b7138 | 2010-02-23 17:47:56 +0100 | [diff] [blame] | 358 | if (nr == BP_VECTOR && !svm_has(SVM_FEATURE_NRIP)) { |
| 359 | unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu); |
| 360 | |
| 361 | /* |
| 362 | * For guest debugging where we have to reinject #BP if some |
| 363 | * INT3 is guest-owned: |
| 364 | * Emulate nRIP by moving RIP forward. Will fail if injection |
| 365 | * raises a fault that is not intercepted. Still better than |
| 366 | * failing in all cases. |
| 367 | */ |
| 368 | skip_emulated_instruction(&svm->vcpu); |
| 369 | rip = kvm_rip_read(&svm->vcpu); |
| 370 | svm->int3_rip = rip + svm->vmcb->save.cs.base; |
| 371 | svm->int3_injected = rip - old_rip; |
| 372 | } |
| 373 | |
Jan Kiszka | 116a475 | 2010-02-23 17:47:54 +0100 | [diff] [blame] | 374 | svm->vmcb->control.event_inj = nr |
| 375 | | SVM_EVTINJ_VALID |
| 376 | | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0) |
| 377 | | SVM_EVTINJ_TYPE_EXEPT; |
| 378 | svm->vmcb->control.event_inj_err = error_code; |
| 379 | } |
| 380 | |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 381 | static void svm_init_erratum_383(void) |
| 382 | { |
| 383 | u32 low, high; |
| 384 | int err; |
| 385 | u64 val; |
| 386 | |
Hans Rosenfeld | 1be85a6 | 2010-07-28 19:09:32 +0200 | [diff] [blame] | 387 | if (!cpu_has_amd_erratum(amd_erratum_383)) |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 388 | return; |
| 389 | |
| 390 | /* Use _safe variants to not break nested virtualization */ |
| 391 | val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err); |
| 392 | if (err) |
| 393 | return; |
| 394 | |
| 395 | val |= (1ULL << 47); |
| 396 | |
| 397 | low = lower_32_bits(val); |
| 398 | high = upper_32_bits(val); |
| 399 | |
| 400 | native_write_msr_safe(MSR_AMD64_DC_CFG, low, high); |
| 401 | |
| 402 | erratum_383_found = true; |
| 403 | } |
| 404 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 405 | static int has_svm(void) |
| 406 | { |
Eduardo Habkost | 63d1142 | 2008-11-17 19:03:20 -0200 | [diff] [blame] | 407 | const char *msg; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 408 | |
Eduardo Habkost | 63d1142 | 2008-11-17 19:03:20 -0200 | [diff] [blame] | 409 | if (!cpu_has_svm(&msg)) { |
Joe Perches | ff81ff1 | 2009-01-08 11:05:17 -0800 | [diff] [blame] | 410 | printk(KERN_INFO "has_svm: %s\n", msg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 411 | return 0; |
| 412 | } |
| 413 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 414 | return 1; |
| 415 | } |
| 416 | |
| 417 | static void svm_hardware_disable(void *garbage) |
| 418 | { |
Eduardo Habkost | 2c8dcee | 2008-11-17 19:03:21 -0200 | [diff] [blame] | 419 | cpu_svm_disable(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 420 | } |
| 421 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 422 | static int svm_hardware_enable(void *garbage) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 423 | { |
| 424 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 425 | struct svm_cpu_data *sd; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 426 | uint64_t efer; |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 427 | struct desc_ptr gdt_descr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 428 | struct desc_struct *gdt; |
| 429 | int me = raw_smp_processor_id(); |
| 430 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 431 | rdmsrl(MSR_EFER, efer); |
| 432 | if (efer & EFER_SVME) |
| 433 | return -EBUSY; |
| 434 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 435 | if (!has_svm()) { |
Zachary Amsden | e6732a5 | 2009-09-29 11:38:36 -1000 | [diff] [blame] | 436 | printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n", |
| 437 | me); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 438 | return -EINVAL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 439 | } |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 440 | sd = per_cpu(svm_data, me); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 441 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 442 | if (!sd) { |
Zachary Amsden | e6732a5 | 2009-09-29 11:38:36 -1000 | [diff] [blame] | 443 | printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n", |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 444 | me); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 445 | return -EINVAL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 446 | } |
| 447 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 448 | sd->asid_generation = 1; |
| 449 | sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1; |
| 450 | sd->next_asid = sd->max_asid + 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 451 | |
Gleb Natapov | d6ab1ed | 2010-02-25 12:43:07 +0200 | [diff] [blame] | 452 | native_store_gdt(&gdt_descr); |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 453 | gdt = (struct desc_struct *)gdt_descr.address; |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 454 | sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 455 | |
Alexander Graf | 9962d03 | 2008-11-25 20:17:02 +0100 | [diff] [blame] | 456 | wrmsrl(MSR_EFER, efer | EFER_SVME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 457 | |
Linus Torvalds | d031655 | 2009-12-14 09:58:24 -0800 | [diff] [blame] | 458 | wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 459 | |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 460 | svm_init_erratum_383(); |
| 461 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 462 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 463 | } |
| 464 | |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 465 | static void svm_cpu_uninit(int cpu) |
| 466 | { |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 467 | struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id()); |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 468 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 469 | if (!sd) |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 470 | return; |
| 471 | |
| 472 | per_cpu(svm_data, raw_smp_processor_id()) = NULL; |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 473 | __free_page(sd->save_area); |
| 474 | kfree(sd); |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 475 | } |
| 476 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 477 | static int svm_cpu_init(int cpu) |
| 478 | { |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 479 | struct svm_cpu_data *sd; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 480 | int r; |
| 481 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 482 | sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL); |
| 483 | if (!sd) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 484 | return -ENOMEM; |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 485 | sd->cpu = cpu; |
| 486 | sd->save_area = alloc_page(GFP_KERNEL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 487 | r = -ENOMEM; |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 488 | if (!sd->save_area) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 489 | goto err_1; |
| 490 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 491 | per_cpu(svm_data, cpu) = sd; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 492 | |
| 493 | return 0; |
| 494 | |
| 495 | err_1: |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 496 | kfree(sd); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 497 | return r; |
| 498 | |
| 499 | } |
| 500 | |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 501 | static bool valid_msr_intercept(u32 index) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 502 | { |
| 503 | int i; |
| 504 | |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 505 | for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) |
| 506 | if (direct_access_msrs[i].index == index) |
| 507 | return true; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 508 | |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 509 | return false; |
| 510 | } |
| 511 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 512 | static void set_msr_interception(u32 *msrpm, unsigned msr, |
| 513 | int read, int write) |
| 514 | { |
Joerg Roedel | 455716f | 2010-03-01 15:34:35 +0100 | [diff] [blame] | 515 | u8 bit_read, bit_write; |
| 516 | unsigned long tmp; |
| 517 | u32 offset; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 518 | |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 519 | /* |
| 520 | * If this warning triggers extend the direct_access_msrs list at the |
| 521 | * beginning of the file |
| 522 | */ |
| 523 | WARN_ON(!valid_msr_intercept(msr)); |
| 524 | |
Joerg Roedel | 455716f | 2010-03-01 15:34:35 +0100 | [diff] [blame] | 525 | offset = svm_msrpm_offset(msr); |
| 526 | bit_read = 2 * (msr & 0x0f); |
| 527 | bit_write = 2 * (msr & 0x0f) + 1; |
| 528 | tmp = msrpm[offset]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 529 | |
Joerg Roedel | 455716f | 2010-03-01 15:34:35 +0100 | [diff] [blame] | 530 | BUG_ON(offset == MSR_INVALID); |
| 531 | |
| 532 | read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp); |
| 533 | write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp); |
| 534 | |
| 535 | msrpm[offset] = tmp; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 536 | } |
| 537 | |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 538 | static void svm_vcpu_init_msrpm(u32 *msrpm) |
| 539 | { |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 540 | int i; |
| 541 | |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 542 | memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER)); |
| 543 | |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 544 | for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) { |
| 545 | if (!direct_access_msrs[i].always) |
| 546 | continue; |
| 547 | |
| 548 | set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1); |
| 549 | } |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 550 | } |
| 551 | |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 552 | static void add_msr_offset(u32 offset) |
| 553 | { |
| 554 | int i; |
| 555 | |
| 556 | for (i = 0; i < MSRPM_OFFSETS; ++i) { |
| 557 | |
| 558 | /* Offset already in list? */ |
| 559 | if (msrpm_offsets[i] == offset) |
| 560 | return; |
| 561 | |
| 562 | /* Slot used by another offset? */ |
| 563 | if (msrpm_offsets[i] != MSR_INVALID) |
| 564 | continue; |
| 565 | |
| 566 | /* Add offset to list */ |
| 567 | msrpm_offsets[i] = offset; |
| 568 | |
| 569 | return; |
| 570 | } |
| 571 | |
| 572 | /* |
| 573 | * If this BUG triggers the msrpm_offsets table has an overflow. Just |
| 574 | * increase MSRPM_OFFSETS in this case. |
| 575 | */ |
| 576 | BUG(); |
| 577 | } |
| 578 | |
| 579 | static void init_msrpm_offsets(void) |
| 580 | { |
| 581 | int i; |
| 582 | |
| 583 | memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets)); |
| 584 | |
| 585 | for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) { |
| 586 | u32 offset; |
| 587 | |
| 588 | offset = svm_msrpm_offset(direct_access_msrs[i].index); |
| 589 | BUG_ON(offset == MSR_INVALID); |
| 590 | |
| 591 | add_msr_offset(offset); |
| 592 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 593 | } |
| 594 | |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 595 | static void svm_enable_lbrv(struct vcpu_svm *svm) |
| 596 | { |
| 597 | u32 *msrpm = svm->msrpm; |
| 598 | |
| 599 | svm->vmcb->control.lbr_ctl = 1; |
| 600 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1); |
| 601 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1); |
| 602 | set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1); |
| 603 | set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1); |
| 604 | } |
| 605 | |
| 606 | static void svm_disable_lbrv(struct vcpu_svm *svm) |
| 607 | { |
| 608 | u32 *msrpm = svm->msrpm; |
| 609 | |
| 610 | svm->vmcb->control.lbr_ctl = 0; |
| 611 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0); |
| 612 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0); |
| 613 | set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0); |
| 614 | set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0); |
| 615 | } |
| 616 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 617 | static __init int svm_hardware_setup(void) |
| 618 | { |
| 619 | int cpu; |
| 620 | struct page *iopm_pages; |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 621 | void *iopm_va; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 622 | int r; |
| 623 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 624 | iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER); |
| 625 | |
| 626 | if (!iopm_pages) |
| 627 | return -ENOMEM; |
Anthony Liguori | c868133 | 2007-04-30 09:48:11 +0300 | [diff] [blame] | 628 | |
| 629 | iopm_va = page_address(iopm_pages); |
| 630 | memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 631 | iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT; |
| 632 | |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 633 | init_msrpm_offsets(); |
| 634 | |
Joerg Roedel | 50a37eb | 2008-01-31 14:57:38 +0100 | [diff] [blame] | 635 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 636 | kvm_enable_efer_bits(EFER_NX); |
| 637 | |
Alexander Graf | 1b2fd70 | 2009-02-02 16:23:51 +0100 | [diff] [blame] | 638 | if (boot_cpu_has(X86_FEATURE_FXSR_OPT)) |
| 639 | kvm_enable_efer_bits(EFER_FFXSR); |
| 640 | |
Alexander Graf | 236de05 | 2008-11-25 20:17:10 +0100 | [diff] [blame] | 641 | if (nested) { |
| 642 | printk(KERN_INFO "kvm: Nested Virtualization enabled\n"); |
Joerg Roedel | eec4b14 | 2010-05-05 16:04:44 +0200 | [diff] [blame] | 643 | kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE); |
Alexander Graf | 236de05 | 2008-11-25 20:17:10 +0100 | [diff] [blame] | 644 | } |
| 645 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 646 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 647 | r = svm_cpu_init(cpu); |
| 648 | if (r) |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 649 | goto err; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 650 | } |
Joerg Roedel | 33bd6a0 | 2008-02-07 13:47:38 +0100 | [diff] [blame] | 651 | |
| 652 | svm_features = cpuid_edx(SVM_CPUID_FUNC); |
| 653 | |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 654 | if (!svm_has(SVM_FEATURE_NPT)) |
| 655 | npt_enabled = false; |
| 656 | |
Joerg Roedel | 6c7dac7 | 2008-02-07 13:47:40 +0100 | [diff] [blame] | 657 | if (npt_enabled && !npt) { |
| 658 | printk(KERN_INFO "kvm: Nested Paging disabled\n"); |
| 659 | npt_enabled = false; |
| 660 | } |
| 661 | |
Joerg Roedel | 1855267 | 2008-02-07 13:47:41 +0100 | [diff] [blame] | 662 | if (npt_enabled) { |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 663 | printk(KERN_INFO "kvm: Nested Paging enabled\n"); |
Joerg Roedel | 1855267 | 2008-02-07 13:47:41 +0100 | [diff] [blame] | 664 | kvm_enable_tdp(); |
Joerg Roedel | 5f4cb66 | 2008-07-14 20:36:36 +0200 | [diff] [blame] | 665 | } else |
| 666 | kvm_disable_tdp(); |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 667 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 668 | return 0; |
| 669 | |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 670 | err: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 671 | __free_pages(iopm_pages, IOPM_ALLOC_ORDER); |
| 672 | iopm_base = 0; |
| 673 | return r; |
| 674 | } |
| 675 | |
| 676 | static __exit void svm_hardware_unsetup(void) |
| 677 | { |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 678 | int cpu; |
| 679 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 680 | for_each_possible_cpu(cpu) |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 681 | svm_cpu_uninit(cpu); |
| 682 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 683 | __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER); |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 684 | iopm_base = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | static void init_seg(struct vmcb_seg *seg) |
| 688 | { |
| 689 | seg->selector = 0; |
| 690 | seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 691 | SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 692 | seg->limit = 0xffff; |
| 693 | seg->base = 0; |
| 694 | } |
| 695 | |
| 696 | static void init_sys_seg(struct vmcb_seg *seg, uint32_t type) |
| 697 | { |
| 698 | seg->selector = 0; |
| 699 | seg->attrib = SVM_SELECTOR_P_MASK | type; |
| 700 | seg->limit = 0xffff; |
| 701 | seg->base = 0; |
| 702 | } |
| 703 | |
Zachary Amsden | f4e1b3c | 2010-08-19 22:07:16 -1000 | [diff] [blame] | 704 | static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
| 705 | { |
| 706 | struct vcpu_svm *svm = to_svm(vcpu); |
| 707 | u64 g_tsc_offset = 0; |
| 708 | |
| 709 | if (is_nested(svm)) { |
| 710 | g_tsc_offset = svm->vmcb->control.tsc_offset - |
| 711 | svm->nested.hsave->control.tsc_offset; |
| 712 | svm->nested.hsave->control.tsc_offset = offset; |
| 713 | } |
| 714 | |
| 715 | svm->vmcb->control.tsc_offset = offset + g_tsc_offset; |
| 716 | } |
| 717 | |
Joerg Roedel | e6101a9 | 2008-02-13 18:58:45 +0100 | [diff] [blame] | 718 | static void init_vmcb(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 719 | { |
Joerg Roedel | e6101a9 | 2008-02-13 18:58:45 +0100 | [diff] [blame] | 720 | struct vmcb_control_area *control = &svm->vmcb->control; |
| 721 | struct vmcb_save_area *save = &svm->vmcb->save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 722 | |
Avi Kivity | bff7827 | 2010-01-07 13:16:08 +0200 | [diff] [blame] | 723 | svm->vcpu.fpu_active = 1; |
| 724 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 725 | control->intercept_cr_read = INTERCEPT_CR0_MASK | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 726 | INTERCEPT_CR3_MASK | |
Joerg Roedel | 649d686 | 2008-04-16 16:51:15 +0200 | [diff] [blame] | 727 | INTERCEPT_CR4_MASK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 728 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 729 | control->intercept_cr_write = INTERCEPT_CR0_MASK | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 730 | INTERCEPT_CR3_MASK | |
Avi Kivity | 80a8119 | 2007-12-06 19:50:00 +0200 | [diff] [blame] | 731 | INTERCEPT_CR4_MASK | |
| 732 | INTERCEPT_CR8_MASK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 733 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 734 | control->intercept_dr_read = INTERCEPT_DR0_MASK | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 735 | INTERCEPT_DR1_MASK | |
| 736 | INTERCEPT_DR2_MASK | |
Jan Kiszka | 727f5a2 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 737 | INTERCEPT_DR3_MASK | |
| 738 | INTERCEPT_DR4_MASK | |
| 739 | INTERCEPT_DR5_MASK | |
| 740 | INTERCEPT_DR6_MASK | |
| 741 | INTERCEPT_DR7_MASK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 742 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 743 | control->intercept_dr_write = INTERCEPT_DR0_MASK | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 744 | INTERCEPT_DR1_MASK | |
| 745 | INTERCEPT_DR2_MASK | |
| 746 | INTERCEPT_DR3_MASK | |
Jan Kiszka | 727f5a2 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 747 | INTERCEPT_DR4_MASK | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 748 | INTERCEPT_DR5_MASK | |
Jan Kiszka | 727f5a2 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 749 | INTERCEPT_DR6_MASK | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 750 | INTERCEPT_DR7_MASK; |
| 751 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 752 | control->intercept_exceptions = (1 << PF_VECTOR) | |
Joerg Roedel | 53371b5 | 2008-04-09 14:15:30 +0200 | [diff] [blame] | 753 | (1 << UD_VECTOR) | |
| 754 | (1 << MC_VECTOR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 755 | |
| 756 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 757 | control->intercept = (1ULL << INTERCEPT_INTR) | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 758 | (1ULL << INTERCEPT_NMI) | |
Joerg Roedel | 0152527 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 759 | (1ULL << INTERCEPT_SMI) | |
Avi Kivity | d225157 | 2010-01-06 10:55:27 +0200 | [diff] [blame] | 760 | (1ULL << INTERCEPT_SELECTIVE_CR0) | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 761 | (1ULL << INTERCEPT_CPUID) | |
Avi Kivity | cf5a94d | 2007-10-28 16:11:58 +0200 | [diff] [blame] | 762 | (1ULL << INTERCEPT_INVD) | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 763 | (1ULL << INTERCEPT_HLT) | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 764 | (1ULL << INTERCEPT_INVLPG) | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 765 | (1ULL << INTERCEPT_INVLPGA) | |
| 766 | (1ULL << INTERCEPT_IOIO_PROT) | |
| 767 | (1ULL << INTERCEPT_MSR_PROT) | |
| 768 | (1ULL << INTERCEPT_TASK_SWITCH) | |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 769 | (1ULL << INTERCEPT_SHUTDOWN) | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 770 | (1ULL << INTERCEPT_VMRUN) | |
| 771 | (1ULL << INTERCEPT_VMMCALL) | |
| 772 | (1ULL << INTERCEPT_VMLOAD) | |
| 773 | (1ULL << INTERCEPT_VMSAVE) | |
| 774 | (1ULL << INTERCEPT_STGI) | |
| 775 | (1ULL << INTERCEPT_CLGI) | |
Joerg Roedel | 916ce23 | 2007-03-21 19:47:00 +0100 | [diff] [blame] | 776 | (1ULL << INTERCEPT_SKINIT) | |
Avi Kivity | cf5a94d | 2007-10-28 16:11:58 +0200 | [diff] [blame] | 777 | (1ULL << INTERCEPT_WBINVD) | |
Joerg Roedel | 916ce23 | 2007-03-21 19:47:00 +0100 | [diff] [blame] | 778 | (1ULL << INTERCEPT_MONITOR) | |
| 779 | (1ULL << INTERCEPT_MWAIT); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 780 | |
| 781 | control->iopm_base_pa = iopm_base; |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 782 | control->msrpm_base_pa = __pa(svm->msrpm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 783 | control->int_ctl = V_INTR_MASKING_MASK; |
| 784 | |
| 785 | init_seg(&save->es); |
| 786 | init_seg(&save->ss); |
| 787 | init_seg(&save->ds); |
| 788 | init_seg(&save->fs); |
| 789 | init_seg(&save->gs); |
| 790 | |
| 791 | save->cs.selector = 0xf000; |
| 792 | /* Executable/Readable Code Segment */ |
| 793 | save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK | |
| 794 | SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK; |
| 795 | save->cs.limit = 0xffff; |
Avi Kivity | d92899a | 2007-02-12 00:54:38 -0800 | [diff] [blame] | 796 | /* |
| 797 | * cs.base should really be 0xffff0000, but vmx can't handle that, so |
| 798 | * be consistent with it. |
| 799 | * |
| 800 | * Replace when we have real mode working for vmx. |
| 801 | */ |
| 802 | save->cs.base = 0xf0000; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 803 | |
| 804 | save->gdtr.limit = 0xffff; |
| 805 | save->idtr.limit = 0xffff; |
| 806 | |
| 807 | init_sys_seg(&save->ldtr, SEG_TYPE_LDT); |
| 808 | init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16); |
| 809 | |
Alexander Graf | 9962d03 | 2008-11-25 20:17:02 +0100 | [diff] [blame] | 810 | save->efer = EFER_SVME; |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 811 | save->dr6 = 0xffff0ff0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 812 | save->dr7 = 0x400; |
| 813 | save->rflags = 2; |
| 814 | save->rip = 0x0000fff0; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 815 | svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 816 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 817 | /* |
| 818 | * This is the guest-visible cr0 value. |
Eduardo Habkost | 18fa000 | 2009-10-24 02:49:59 -0200 | [diff] [blame] | 819 | * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 820 | */ |
Eduardo Habkost | 18fa000 | 2009-10-24 02:49:59 -0200 | [diff] [blame] | 821 | svm->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET; |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 822 | (void)kvm_set_cr0(&svm->vcpu, svm->vcpu.arch.cr0); |
Eduardo Habkost | 18fa000 | 2009-10-24 02:49:59 -0200 | [diff] [blame] | 823 | |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 824 | save->cr4 = X86_CR4_PAE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 825 | /* rdx = ?? */ |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 826 | |
| 827 | if (npt_enabled) { |
| 828 | /* Setup VMCB for Nested Paging */ |
| 829 | control->nested_ctl = 1; |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 830 | control->intercept &= ~((1ULL << INTERCEPT_TASK_SWITCH) | |
| 831 | (1ULL << INTERCEPT_INVLPG)); |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 832 | control->intercept_exceptions &= ~(1 << PF_VECTOR); |
Avi Kivity | 888f9f3 | 2010-01-10 12:14:04 +0200 | [diff] [blame] | 833 | control->intercept_cr_read &= ~INTERCEPT_CR3_MASK; |
| 834 | control->intercept_cr_write &= ~INTERCEPT_CR3_MASK; |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 835 | save->g_pat = 0x0007040600070406ULL; |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 836 | save->cr3 = 0; |
| 837 | save->cr4 = 0; |
| 838 | } |
Avi Kivity | a79d2f1 | 2008-04-14 13:10:21 +0300 | [diff] [blame] | 839 | force_new_asid(&svm->vcpu); |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 840 | |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 841 | svm->nested.vmcb = 0; |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 842 | svm->vcpu.arch.hflags = 0; |
| 843 | |
Mark Langsdorf | 565d099 | 2009-10-06 14:25:02 -0500 | [diff] [blame] | 844 | if (svm_has(SVM_FEATURE_PAUSE_FILTER)) { |
| 845 | control->pause_filter_count = 3000; |
| 846 | control->intercept |= (1ULL << INTERCEPT_PAUSE); |
| 847 | } |
| 848 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 849 | enable_gif(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 850 | } |
| 851 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 852 | static int svm_vcpu_reset(struct kvm_vcpu *vcpu) |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 853 | { |
| 854 | struct vcpu_svm *svm = to_svm(vcpu); |
| 855 | |
Joerg Roedel | e6101a9 | 2008-02-13 18:58:45 +0100 | [diff] [blame] | 856 | init_vmcb(svm); |
Avi Kivity | 7043338 | 2007-11-07 12:57:23 +0200 | [diff] [blame] | 857 | |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 858 | if (!kvm_vcpu_is_bsp(vcpu)) { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 859 | kvm_rip_write(vcpu, 0); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 860 | svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12; |
| 861 | svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8; |
Avi Kivity | 7043338 | 2007-11-07 12:57:23 +0200 | [diff] [blame] | 862 | } |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 863 | vcpu->arch.regs_avail = ~0; |
| 864 | vcpu->arch.regs_dirty = ~0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 865 | |
| 866 | return 0; |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 867 | } |
| 868 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 869 | 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] | 870 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 871 | struct vcpu_svm *svm; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 872 | struct page *page; |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 873 | struct page *msrpm_pages; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 874 | struct page *hsave_page; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 875 | struct page *nested_msrpm_pages; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 876 | int err; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 877 | |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 878 | svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 879 | if (!svm) { |
| 880 | err = -ENOMEM; |
| 881 | goto out; |
| 882 | } |
| 883 | |
| 884 | err = kvm_vcpu_init(&svm->vcpu, kvm, id); |
| 885 | if (err) |
| 886 | goto free_svm; |
| 887 | |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 888 | err = -ENOMEM; |
Takuya Yoshikawa | b7af404 | 2010-03-09 14:55:19 +0900 | [diff] [blame] | 889 | page = alloc_page(GFP_KERNEL); |
| 890 | if (!page) |
| 891 | goto uninit; |
| 892 | |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 893 | msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER); |
| 894 | if (!msrpm_pages) |
Takuya Yoshikawa | b7af404 | 2010-03-09 14:55:19 +0900 | [diff] [blame] | 895 | goto free_page1; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 896 | |
| 897 | nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER); |
| 898 | if (!nested_msrpm_pages) |
Takuya Yoshikawa | b7af404 | 2010-03-09 14:55:19 +0900 | [diff] [blame] | 899 | goto free_page2; |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 900 | |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 901 | hsave_page = alloc_page(GFP_KERNEL); |
| 902 | if (!hsave_page) |
Takuya Yoshikawa | b7af404 | 2010-03-09 14:55:19 +0900 | [diff] [blame] | 903 | goto free_page3; |
| 904 | |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 905 | svm->nested.hsave = page_address(hsave_page); |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 906 | |
Takuya Yoshikawa | b7af404 | 2010-03-09 14:55:19 +0900 | [diff] [blame] | 907 | svm->msrpm = page_address(msrpm_pages); |
| 908 | svm_vcpu_init_msrpm(svm->msrpm); |
| 909 | |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 910 | svm->nested.msrpm = page_address(nested_msrpm_pages); |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 911 | svm_vcpu_init_msrpm(svm->nested.msrpm); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 912 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 913 | svm->vmcb = page_address(page); |
| 914 | clear_page(svm->vmcb); |
| 915 | svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT; |
| 916 | svm->asid_generation = 0; |
Joerg Roedel | e6101a9 | 2008-02-13 18:58:45 +0100 | [diff] [blame] | 917 | init_vmcb(svm); |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame^] | 918 | kvm_write_tsc(&svm->vcpu, 0); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 919 | |
Jan Kiszka | 10ab25c | 2010-05-25 16:01:50 +0200 | [diff] [blame] | 920 | err = fx_init(&svm->vcpu); |
| 921 | if (err) |
| 922 | goto free_page4; |
| 923 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 924 | svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE; |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 925 | if (kvm_vcpu_is_bsp(&svm->vcpu)) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 926 | svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 927 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 928 | return &svm->vcpu; |
Avi Kivity | 36241b8 | 2006-12-22 01:05:20 -0800 | [diff] [blame] | 929 | |
Jan Kiszka | 10ab25c | 2010-05-25 16:01:50 +0200 | [diff] [blame] | 930 | free_page4: |
| 931 | __free_page(hsave_page); |
Takuya Yoshikawa | b7af404 | 2010-03-09 14:55:19 +0900 | [diff] [blame] | 932 | free_page3: |
| 933 | __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER); |
| 934 | free_page2: |
| 935 | __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER); |
| 936 | free_page1: |
| 937 | __free_page(page); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 938 | uninit: |
| 939 | kvm_vcpu_uninit(&svm->vcpu); |
| 940 | free_svm: |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 941 | kmem_cache_free(kvm_vcpu_cache, svm); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 942 | out: |
| 943 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | static void svm_free_vcpu(struct kvm_vcpu *vcpu) |
| 947 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 948 | struct vcpu_svm *svm = to_svm(vcpu); |
| 949 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 950 | __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT)); |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 951 | __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER); |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 952 | __free_page(virt_to_page(svm->nested.hsave)); |
| 953 | __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 954 | kvm_vcpu_uninit(vcpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 955 | kmem_cache_free(kvm_vcpu_cache, svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 956 | } |
| 957 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 958 | static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 959 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 960 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 961 | int i; |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 962 | |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 963 | if (unlikely(cpu != vcpu->cpu)) { |
Joerg Roedel | e935d48 | 2009-09-16 15:24:19 +0200 | [diff] [blame] | 964 | u64 delta; |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 965 | |
Joerg Roedel | 953899b | 2009-12-14 12:22:20 +0100 | [diff] [blame] | 966 | if (check_tsc_unstable()) { |
| 967 | /* |
| 968 | * Make sure that the guest sees a monotonically |
| 969 | * increasing TSC. |
| 970 | */ |
| 971 | delta = vcpu->arch.host_tsc - native_read_tsc(); |
| 972 | svm->vmcb->control.tsc_offset += delta; |
| 973 | if (is_nested(svm)) |
| 974 | svm->nested.hsave->control.tsc_offset += delta; |
| 975 | } |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 976 | vcpu->cpu = cpu; |
Marcelo Tosatti | 2f59971 | 2008-05-27 12:10:20 -0300 | [diff] [blame] | 977 | kvm_migrate_timers(vcpu); |
Marcelo Tosatti | 4b656b1 | 2009-07-21 12:47:45 -0300 | [diff] [blame] | 978 | svm->asid_generation = 0; |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 979 | } |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 980 | |
| 981 | for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 982 | rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | static void svm_vcpu_put(struct kvm_vcpu *vcpu) |
| 986 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 987 | struct vcpu_svm *svm = to_svm(vcpu); |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 988 | int i; |
| 989 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 990 | ++vcpu->stat.host_state_reload; |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 991 | for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 992 | wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]); |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 993 | |
Joerg Roedel | e935d48 | 2009-09-16 15:24:19 +0200 | [diff] [blame] | 994 | vcpu->arch.host_tsc = native_read_tsc(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 995 | } |
| 996 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 997 | static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu) |
| 998 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 999 | return to_svm(vcpu)->vmcb->save.rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 1003 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1004 | to_svm(vcpu)->vmcb->save.rflags = rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1005 | } |
| 1006 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 1007 | static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg) |
| 1008 | { |
| 1009 | switch (reg) { |
| 1010 | case VCPU_EXREG_PDPTR: |
| 1011 | BUG_ON(!npt_enabled); |
| 1012 | load_pdptrs(vcpu, vcpu->arch.cr3); |
| 1013 | break; |
| 1014 | default: |
| 1015 | BUG(); |
| 1016 | } |
| 1017 | } |
| 1018 | |
Alexander Graf | f0b8505 | 2008-11-25 20:17:01 +0100 | [diff] [blame] | 1019 | static void svm_set_vintr(struct vcpu_svm *svm) |
| 1020 | { |
| 1021 | svm->vmcb->control.intercept |= 1ULL << INTERCEPT_VINTR; |
| 1022 | } |
| 1023 | |
| 1024 | static void svm_clear_vintr(struct vcpu_svm *svm) |
| 1025 | { |
| 1026 | svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR); |
| 1027 | } |
| 1028 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1029 | static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg) |
| 1030 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1031 | struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1032 | |
| 1033 | switch (seg) { |
| 1034 | case VCPU_SREG_CS: return &save->cs; |
| 1035 | case VCPU_SREG_DS: return &save->ds; |
| 1036 | case VCPU_SREG_ES: return &save->es; |
| 1037 | case VCPU_SREG_FS: return &save->fs; |
| 1038 | case VCPU_SREG_GS: return &save->gs; |
| 1039 | case VCPU_SREG_SS: return &save->ss; |
| 1040 | case VCPU_SREG_TR: return &save->tr; |
| 1041 | case VCPU_SREG_LDTR: return &save->ldtr; |
| 1042 | } |
| 1043 | BUG(); |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 1044 | return NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 1048 | { |
| 1049 | struct vmcb_seg *s = svm_seg(vcpu, seg); |
| 1050 | |
| 1051 | return s->base; |
| 1052 | } |
| 1053 | |
| 1054 | static void svm_get_segment(struct kvm_vcpu *vcpu, |
| 1055 | struct kvm_segment *var, int seg) |
| 1056 | { |
| 1057 | struct vmcb_seg *s = svm_seg(vcpu, seg); |
| 1058 | |
| 1059 | var->base = s->base; |
| 1060 | var->limit = s->limit; |
| 1061 | var->selector = s->selector; |
| 1062 | var->type = s->attrib & SVM_SELECTOR_TYPE_MASK; |
| 1063 | var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1; |
| 1064 | var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3; |
| 1065 | var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1; |
| 1066 | var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1; |
| 1067 | var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1; |
| 1068 | var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1; |
| 1069 | var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1; |
Amit Shah | 25022ac | 2008-10-27 09:04:17 +0000 | [diff] [blame] | 1070 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 1071 | /* |
| 1072 | * AMD's VMCB does not have an explicit unusable field, so emulate it |
Andre Przywara | 19bca6a | 2009-04-28 12:45:30 +0200 | [diff] [blame] | 1073 | * for cross vendor migration purposes by "not present" |
| 1074 | */ |
| 1075 | var->unusable = !var->present || (var->type == 0); |
| 1076 | |
Andre Przywara | 1fbdc7a | 2009-01-11 22:39:44 +0100 | [diff] [blame] | 1077 | switch (seg) { |
| 1078 | case VCPU_SREG_CS: |
| 1079 | /* |
| 1080 | * SVM always stores 0 for the 'G' bit in the CS selector in |
| 1081 | * the VMCB on a VMEXIT. This hurts cross-vendor migration: |
| 1082 | * Intel's VMENTRY has a check on the 'G' bit. |
| 1083 | */ |
Amit Shah | 25022ac | 2008-10-27 09:04:17 +0000 | [diff] [blame] | 1084 | var->g = s->limit > 0xfffff; |
Andre Przywara | 1fbdc7a | 2009-01-11 22:39:44 +0100 | [diff] [blame] | 1085 | break; |
| 1086 | case VCPU_SREG_TR: |
| 1087 | /* |
| 1088 | * Work around a bug where the busy flag in the tr selector |
| 1089 | * isn't exposed |
| 1090 | */ |
Amit Shah | c0d0982 | 2008-10-27 09:04:18 +0000 | [diff] [blame] | 1091 | var->type |= 0x2; |
Andre Przywara | 1fbdc7a | 2009-01-11 22:39:44 +0100 | [diff] [blame] | 1092 | break; |
| 1093 | case VCPU_SREG_DS: |
| 1094 | case VCPU_SREG_ES: |
| 1095 | case VCPU_SREG_FS: |
| 1096 | case VCPU_SREG_GS: |
| 1097 | /* |
| 1098 | * The accessed bit must always be set in the segment |
| 1099 | * descriptor cache, although it can be cleared in the |
| 1100 | * descriptor, the cached bit always remains at 1. Since |
| 1101 | * Intel has a check on this, set it here to support |
| 1102 | * cross-vendor migration. |
| 1103 | */ |
| 1104 | if (!var->unusable) |
| 1105 | var->type |= 0x1; |
| 1106 | break; |
Andre Przywara | b586eb0 | 2009-04-28 12:45:43 +0200 | [diff] [blame] | 1107 | case VCPU_SREG_SS: |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 1108 | /* |
| 1109 | * On AMD CPUs sometimes the DB bit in the segment |
Andre Przywara | b586eb0 | 2009-04-28 12:45:43 +0200 | [diff] [blame] | 1110 | * descriptor is left as 1, although the whole segment has |
| 1111 | * been made unusable. Clear it here to pass an Intel VMX |
| 1112 | * entry check when cross vendor migrating. |
| 1113 | */ |
| 1114 | if (var->unusable) |
| 1115 | var->db = 0; |
| 1116 | break; |
Andre Przywara | 1fbdc7a | 2009-01-11 22:39:44 +0100 | [diff] [blame] | 1117 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1118 | } |
| 1119 | |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 1120 | static int svm_get_cpl(struct kvm_vcpu *vcpu) |
| 1121 | { |
| 1122 | struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save; |
| 1123 | |
| 1124 | return save->cpl; |
| 1125 | } |
| 1126 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 1127 | static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1128 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1129 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1130 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 1131 | dt->size = svm->vmcb->save.idtr.limit; |
| 1132 | dt->address = svm->vmcb->save.idtr.base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1133 | } |
| 1134 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 1135 | static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1136 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1137 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1138 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 1139 | svm->vmcb->save.idtr.limit = dt->size; |
| 1140 | svm->vmcb->save.idtr.base = dt->address ; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1141 | } |
| 1142 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 1143 | static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1144 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1145 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1146 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 1147 | dt->size = svm->vmcb->save.gdtr.limit; |
| 1148 | dt->address = svm->vmcb->save.gdtr.base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1149 | } |
| 1150 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 1151 | static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1152 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1153 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1154 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 1155 | svm->vmcb->save.gdtr.limit = dt->size; |
| 1156 | svm->vmcb->save.gdtr.base = dt->address ; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1157 | } |
| 1158 | |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 1159 | static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
| 1160 | { |
| 1161 | } |
| 1162 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 1163 | static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 1164 | { |
| 1165 | } |
| 1166 | |
Avi Kivity | d225157 | 2010-01-06 10:55:27 +0200 | [diff] [blame] | 1167 | static void update_cr0_intercept(struct vcpu_svm *svm) |
| 1168 | { |
Joerg Roedel | 66a562f | 2010-02-19 16:23:08 +0100 | [diff] [blame] | 1169 | struct vmcb *vmcb = svm->vmcb; |
Avi Kivity | d225157 | 2010-01-06 10:55:27 +0200 | [diff] [blame] | 1170 | ulong gcr0 = svm->vcpu.arch.cr0; |
| 1171 | u64 *hcr0 = &svm->vmcb->save.cr0; |
| 1172 | |
| 1173 | if (!svm->vcpu.fpu_active) |
| 1174 | *hcr0 |= SVM_CR0_SELECTIVE_MASK; |
| 1175 | else |
| 1176 | *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK) |
| 1177 | | (gcr0 & SVM_CR0_SELECTIVE_MASK); |
| 1178 | |
| 1179 | |
| 1180 | if (gcr0 == *hcr0 && svm->vcpu.fpu_active) { |
Joerg Roedel | 66a562f | 2010-02-19 16:23:08 +0100 | [diff] [blame] | 1181 | vmcb->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK; |
| 1182 | vmcb->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK; |
| 1183 | if (is_nested(svm)) { |
| 1184 | struct vmcb *hsave = svm->nested.hsave; |
| 1185 | |
| 1186 | hsave->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK; |
| 1187 | hsave->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK; |
| 1188 | vmcb->control.intercept_cr_read |= svm->nested.intercept_cr_read; |
| 1189 | vmcb->control.intercept_cr_write |= svm->nested.intercept_cr_write; |
| 1190 | } |
Avi Kivity | d225157 | 2010-01-06 10:55:27 +0200 | [diff] [blame] | 1191 | } else { |
| 1192 | svm->vmcb->control.intercept_cr_read |= INTERCEPT_CR0_MASK; |
| 1193 | svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR0_MASK; |
Joerg Roedel | 66a562f | 2010-02-19 16:23:08 +0100 | [diff] [blame] | 1194 | if (is_nested(svm)) { |
| 1195 | struct vmcb *hsave = svm->nested.hsave; |
| 1196 | |
| 1197 | hsave->control.intercept_cr_read |= INTERCEPT_CR0_MASK; |
| 1198 | hsave->control.intercept_cr_write |= INTERCEPT_CR0_MASK; |
| 1199 | } |
Avi Kivity | d225157 | 2010-01-06 10:55:27 +0200 | [diff] [blame] | 1200 | } |
| 1201 | } |
| 1202 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1203 | static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 1204 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1205 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1206 | |
Joerg Roedel | 7f5d8b5 | 2010-02-24 18:59:18 +0100 | [diff] [blame] | 1207 | if (is_nested(svm)) { |
| 1208 | /* |
| 1209 | * We are here because we run in nested mode, the host kvm |
| 1210 | * intercepts cr0 writes but the l1 hypervisor does not. |
| 1211 | * But the L1 hypervisor may intercept selective cr0 writes. |
| 1212 | * This needs to be checked here. |
| 1213 | */ |
| 1214 | unsigned long old, new; |
| 1215 | |
| 1216 | /* Remove bits that would trigger a real cr0 write intercept */ |
| 1217 | old = vcpu->arch.cr0 & SVM_CR0_SELECTIVE_MASK; |
| 1218 | new = cr0 & SVM_CR0_SELECTIVE_MASK; |
| 1219 | |
| 1220 | if (old == new) { |
| 1221 | /* cr0 write with ts and mp unchanged */ |
| 1222 | svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE; |
| 1223 | if (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE) |
| 1224 | return; |
| 1225 | } |
| 1226 | } |
| 1227 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1228 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 1229 | if (vcpu->arch.efer & EFER_LME) { |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 1230 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) { |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 1231 | vcpu->arch.efer |= EFER_LMA; |
Carlo Marcelo Arenas Belon | 2b5203e | 2007-12-01 06:17:11 -0600 | [diff] [blame] | 1232 | svm->vmcb->save.efer |= EFER_LMA | EFER_LME; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1233 | } |
| 1234 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1235 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) { |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 1236 | vcpu->arch.efer &= ~EFER_LMA; |
Carlo Marcelo Arenas Belon | 2b5203e | 2007-12-01 06:17:11 -0600 | [diff] [blame] | 1237 | svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1238 | } |
| 1239 | } |
| 1240 | #endif |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1241 | vcpu->arch.cr0 = cr0; |
Avi Kivity | 888f9f3 | 2010-01-10 12:14:04 +0200 | [diff] [blame] | 1242 | |
| 1243 | if (!npt_enabled) |
| 1244 | cr0 |= X86_CR0_PG | X86_CR0_WP; |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 1245 | |
| 1246 | if (!vcpu->fpu_active) |
Joerg Roedel | 334df50 | 2008-01-21 13:09:33 +0100 | [diff] [blame] | 1247 | cr0 |= X86_CR0_TS; |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 1248 | /* |
| 1249 | * re-enable caching here because the QEMU bios |
| 1250 | * does not do it - this results in some delay at |
| 1251 | * reboot |
| 1252 | */ |
| 1253 | cr0 &= ~(X86_CR0_CD | X86_CR0_NW); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1254 | svm->vmcb->save.cr0 = cr0; |
Avi Kivity | d225157 | 2010-01-06 10:55:27 +0200 | [diff] [blame] | 1255 | update_cr0_intercept(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
| 1259 | { |
Joerg Roedel | 6394b64 | 2008-04-09 14:15:29 +0200 | [diff] [blame] | 1260 | unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE; |
Joerg Roedel | e5eab0c | 2008-09-09 19:11:51 +0200 | [diff] [blame] | 1261 | unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4; |
| 1262 | |
| 1263 | if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE)) |
| 1264 | force_new_asid(vcpu); |
Joerg Roedel | 6394b64 | 2008-04-09 14:15:29 +0200 | [diff] [blame] | 1265 | |
Joerg Roedel | ec07726 | 2008-04-09 14:15:28 +0200 | [diff] [blame] | 1266 | vcpu->arch.cr4 = cr4; |
| 1267 | if (!npt_enabled) |
| 1268 | cr4 |= X86_CR4_PAE; |
Joerg Roedel | 6394b64 | 2008-04-09 14:15:29 +0200 | [diff] [blame] | 1269 | cr4 |= host_cr4_mce; |
Joerg Roedel | ec07726 | 2008-04-09 14:15:28 +0200 | [diff] [blame] | 1270 | to_svm(vcpu)->vmcb->save.cr4 = cr4; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | static void svm_set_segment(struct kvm_vcpu *vcpu, |
| 1274 | struct kvm_segment *var, int seg) |
| 1275 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1276 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1277 | struct vmcb_seg *s = svm_seg(vcpu, seg); |
| 1278 | |
| 1279 | s->base = var->base; |
| 1280 | s->limit = var->limit; |
| 1281 | s->selector = var->selector; |
| 1282 | if (var->unusable) |
| 1283 | s->attrib = 0; |
| 1284 | else { |
| 1285 | s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK); |
| 1286 | s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT; |
| 1287 | s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT; |
| 1288 | s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT; |
| 1289 | s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT; |
| 1290 | s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT; |
| 1291 | s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT; |
| 1292 | s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT; |
| 1293 | } |
| 1294 | if (seg == VCPU_SREG_CS) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1295 | svm->vmcb->save.cpl |
| 1296 | = (svm->vmcb->save.cs.attrib |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1297 | >> SVM_SELECTOR_DPL_SHIFT) & 3; |
| 1298 | |
| 1299 | } |
| 1300 | |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1301 | static void update_db_intercept(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1302 | { |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1303 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1304 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1305 | svm->vmcb->control.intercept_exceptions &= |
| 1306 | ~((1 << DB_VECTOR) | (1 << BP_VECTOR)); |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1307 | |
Jan Kiszka | 6be7d30 | 2009-10-18 13:24:54 +0200 | [diff] [blame] | 1308 | if (svm->nmi_singlestep) |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1309 | svm->vmcb->control.intercept_exceptions |= (1 << DB_VECTOR); |
| 1310 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1311 | if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) { |
| 1312 | if (vcpu->guest_debug & |
| 1313 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 1314 | svm->vmcb->control.intercept_exceptions |= |
| 1315 | 1 << DB_VECTOR; |
| 1316 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
| 1317 | svm->vmcb->control.intercept_exceptions |= |
| 1318 | 1 << BP_VECTOR; |
| 1319 | } else |
| 1320 | vcpu->guest_debug = 0; |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1321 | } |
| 1322 | |
Jan Kiszka | 355be0b | 2009-10-03 00:31:21 +0200 | [diff] [blame] | 1323 | static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg) |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1324 | { |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1325 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1326 | |
Jan Kiszka | ae675ef | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1327 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) |
| 1328 | svm->vmcb->save.dr7 = dbg->arch.debugreg[7]; |
| 1329 | else |
| 1330 | svm->vmcb->save.dr7 = vcpu->arch.dr7; |
| 1331 | |
Jan Kiszka | 355be0b | 2009-10-03 00:31:21 +0200 | [diff] [blame] | 1332 | update_db_intercept(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | static void load_host_msrs(struct kvm_vcpu *vcpu) |
| 1336 | { |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 1337 | #ifdef CONFIG_X86_64 |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1338 | wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base); |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 1339 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | static void save_host_msrs(struct kvm_vcpu *vcpu) |
| 1343 | { |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 1344 | #ifdef CONFIG_X86_64 |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1345 | rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base); |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 1346 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1347 | } |
| 1348 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 1349 | static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1350 | { |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 1351 | if (sd->next_asid > sd->max_asid) { |
| 1352 | ++sd->asid_generation; |
| 1353 | sd->next_asid = 1; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1354 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1355 | } |
| 1356 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 1357 | svm->asid_generation = sd->asid_generation; |
| 1358 | svm->vmcb->control.asid = sd->next_asid++; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1359 | } |
| 1360 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 1361 | static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1362 | { |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1363 | struct vcpu_svm *svm = to_svm(vcpu); |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1364 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 1365 | svm->vmcb->save.dr7 = value; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1366 | } |
| 1367 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1368 | static int pf_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1369 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1370 | u64 fault_address; |
| 1371 | u32 error_code; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1372 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1373 | fault_address = svm->vmcb->control.exit_info_2; |
| 1374 | error_code = svm->vmcb->control.exit_info_1; |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 1375 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 1376 | trace_kvm_page_fault(fault_address, error_code); |
Avi Kivity | 52c7847 | 2009-08-10 15:42:41 +0300 | [diff] [blame] | 1377 | if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu)) |
| 1378 | kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address); |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 1379 | return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1380 | } |
| 1381 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1382 | static int db_interception(struct vcpu_svm *svm) |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1383 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1384 | struct kvm_run *kvm_run = svm->vcpu.run; |
| 1385 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1386 | if (!(svm->vcpu.guest_debug & |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1387 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) && |
Jan Kiszka | 6be7d30 | 2009-10-18 13:24:54 +0200 | [diff] [blame] | 1388 | !svm->nmi_singlestep) { |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1389 | kvm_queue_exception(&svm->vcpu, DB_VECTOR); |
| 1390 | return 1; |
| 1391 | } |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1392 | |
Jan Kiszka | 6be7d30 | 2009-10-18 13:24:54 +0200 | [diff] [blame] | 1393 | if (svm->nmi_singlestep) { |
| 1394 | svm->nmi_singlestep = false; |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1395 | if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) |
| 1396 | svm->vmcb->save.rflags &= |
| 1397 | ~(X86_EFLAGS_TF | X86_EFLAGS_RF); |
| 1398 | update_db_intercept(&svm->vcpu); |
| 1399 | } |
| 1400 | |
| 1401 | if (svm->vcpu.guest_debug & |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 1402 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) { |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1403 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
| 1404 | kvm_run->debug.arch.pc = |
| 1405 | svm->vmcb->save.cs.base + svm->vmcb->save.rip; |
| 1406 | kvm_run->debug.arch.exception = DB_VECTOR; |
| 1407 | return 0; |
| 1408 | } |
| 1409 | |
| 1410 | return 1; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1411 | } |
| 1412 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1413 | static int bp_interception(struct vcpu_svm *svm) |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1414 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1415 | struct kvm_run *kvm_run = svm->vcpu.run; |
| 1416 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1417 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
| 1418 | kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip; |
| 1419 | kvm_run->debug.arch.exception = BP_VECTOR; |
| 1420 | return 0; |
| 1421 | } |
| 1422 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1423 | static int ud_interception(struct vcpu_svm *svm) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1424 | { |
| 1425 | int er; |
| 1426 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1427 | er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1428 | if (er != EMULATE_DONE) |
Avi Kivity | 7ee5d940 | 2007-11-25 15:22:50 +0200 | [diff] [blame] | 1429 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1430 | return 1; |
| 1431 | } |
| 1432 | |
Avi Kivity | 6b52d18 | 2010-01-21 15:31:47 +0200 | [diff] [blame] | 1433 | static void svm_fpu_activate(struct kvm_vcpu *vcpu) |
Anthony Liguori | 7807fa6 | 2007-04-23 09:17:21 -0500 | [diff] [blame] | 1434 | { |
Avi Kivity | 6b52d18 | 2010-01-21 15:31:47 +0200 | [diff] [blame] | 1435 | struct vcpu_svm *svm = to_svm(vcpu); |
Joerg Roedel | 66a562f | 2010-02-19 16:23:08 +0100 | [diff] [blame] | 1436 | u32 excp; |
| 1437 | |
| 1438 | if (is_nested(svm)) { |
| 1439 | u32 h_excp, n_excp; |
| 1440 | |
| 1441 | h_excp = svm->nested.hsave->control.intercept_exceptions; |
| 1442 | n_excp = svm->nested.intercept_exceptions; |
| 1443 | h_excp &= ~(1 << NM_VECTOR); |
| 1444 | excp = h_excp | n_excp; |
| 1445 | } else { |
| 1446 | excp = svm->vmcb->control.intercept_exceptions; |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 1447 | excp &= ~(1 << NM_VECTOR); |
Joerg Roedel | 66a562f | 2010-02-19 16:23:08 +0100 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | svm->vmcb->control.intercept_exceptions = excp; |
| 1451 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1452 | svm->vcpu.fpu_active = 1; |
Avi Kivity | d225157 | 2010-01-06 10:55:27 +0200 | [diff] [blame] | 1453 | update_cr0_intercept(svm); |
Avi Kivity | 6b52d18 | 2010-01-21 15:31:47 +0200 | [diff] [blame] | 1454 | } |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1455 | |
Avi Kivity | 6b52d18 | 2010-01-21 15:31:47 +0200 | [diff] [blame] | 1456 | static int nm_interception(struct vcpu_svm *svm) |
| 1457 | { |
| 1458 | svm_fpu_activate(&svm->vcpu); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1459 | return 1; |
Anthony Liguori | 7807fa6 | 2007-04-23 09:17:21 -0500 | [diff] [blame] | 1460 | } |
| 1461 | |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 1462 | static bool is_erratum_383(void) |
| 1463 | { |
| 1464 | int err, i; |
| 1465 | u64 value; |
| 1466 | |
| 1467 | if (!erratum_383_found) |
| 1468 | return false; |
| 1469 | |
| 1470 | value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err); |
| 1471 | if (err) |
| 1472 | return false; |
| 1473 | |
| 1474 | /* Bit 62 may or may not be set for this mce */ |
| 1475 | value &= ~(1ULL << 62); |
| 1476 | |
| 1477 | if (value != 0xb600000000010015ULL) |
| 1478 | return false; |
| 1479 | |
| 1480 | /* Clear MCi_STATUS registers */ |
| 1481 | for (i = 0; i < 6; ++i) |
| 1482 | native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0); |
| 1483 | |
| 1484 | value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err); |
| 1485 | if (!err) { |
| 1486 | u32 low, high; |
| 1487 | |
| 1488 | value &= ~(1ULL << 2); |
| 1489 | low = lower_32_bits(value); |
| 1490 | high = upper_32_bits(value); |
| 1491 | |
| 1492 | native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high); |
| 1493 | } |
| 1494 | |
| 1495 | /* Flush tlb to evict multi-match entries */ |
| 1496 | __flush_tlb_all(); |
| 1497 | |
| 1498 | return true; |
| 1499 | } |
| 1500 | |
Joerg Roedel | fe5913e | 2010-05-17 14:43:34 +0200 | [diff] [blame] | 1501 | static void svm_handle_mce(struct vcpu_svm *svm) |
Joerg Roedel | 53371b5 | 2008-04-09 14:15:30 +0200 | [diff] [blame] | 1502 | { |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 1503 | if (is_erratum_383()) { |
| 1504 | /* |
| 1505 | * Erratum 383 triggered. Guest state is corrupt so kill the |
| 1506 | * guest. |
| 1507 | */ |
| 1508 | pr_err("KVM: Guest triggered AMD Erratum 383\n"); |
| 1509 | |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 1510 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu); |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 1511 | |
| 1512 | return; |
| 1513 | } |
| 1514 | |
Joerg Roedel | 53371b5 | 2008-04-09 14:15:30 +0200 | [diff] [blame] | 1515 | /* |
| 1516 | * On an #MC intercept the MCE handler is not called automatically in |
| 1517 | * the host. So do it by hand here. |
| 1518 | */ |
| 1519 | asm volatile ( |
| 1520 | "int $0x12\n"); |
| 1521 | /* not sure if we ever come back to this point */ |
| 1522 | |
Joerg Roedel | fe5913e | 2010-05-17 14:43:34 +0200 | [diff] [blame] | 1523 | return; |
| 1524 | } |
| 1525 | |
| 1526 | static int mc_interception(struct vcpu_svm *svm) |
| 1527 | { |
Joerg Roedel | 53371b5 | 2008-04-09 14:15:30 +0200 | [diff] [blame] | 1528 | return 1; |
| 1529 | } |
| 1530 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1531 | static int shutdown_interception(struct vcpu_svm *svm) |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 1532 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1533 | struct kvm_run *kvm_run = svm->vcpu.run; |
| 1534 | |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 1535 | /* |
| 1536 | * VMCB is undefined after a SHUTDOWN intercept |
| 1537 | * so reinitialize it. |
| 1538 | */ |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1539 | clear_page(svm->vmcb); |
Joerg Roedel | e6101a9 | 2008-02-13 18:58:45 +0100 | [diff] [blame] | 1540 | init_vmcb(svm); |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 1541 | |
| 1542 | kvm_run->exit_reason = KVM_EXIT_SHUTDOWN; |
| 1543 | return 0; |
| 1544 | } |
| 1545 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1546 | static int io_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1547 | { |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 1548 | struct kvm_vcpu *vcpu = &svm->vcpu; |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1549 | u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */ |
Jan Kiszka | 34c33d1 | 2009-02-08 13:28:15 +0100 | [diff] [blame] | 1550 | int size, in, string; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1551 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1552 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1553 | ++svm->vcpu.stat.io_exits; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 1554 | string = (io_info & SVM_IOIO_STR_MASK) != 0; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1555 | in = (io_info & SVM_IOIO_TYPE_MASK) != 0; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 1556 | if (string || in) |
Gleb Natapov | 6d77dbf | 2010-05-10 11:16:56 +0300 | [diff] [blame] | 1557 | return emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 1558 | |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1559 | port = io_info >> 16; |
| 1560 | size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 1561 | svm->next_rip = svm->vmcb->control.exit_info_2; |
Guillaume Thouvenin | e93f36b | 2008-10-28 10:51:30 +0100 | [diff] [blame] | 1562 | skip_emulated_instruction(&svm->vcpu); |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 1563 | |
| 1564 | return kvm_fast_pio_out(vcpu, size, port); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1565 | } |
| 1566 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1567 | static int nmi_interception(struct vcpu_svm *svm) |
Joerg Roedel | c47f098 | 2008-04-30 17:56:00 +0200 | [diff] [blame] | 1568 | { |
| 1569 | return 1; |
| 1570 | } |
| 1571 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1572 | static int intr_interception(struct vcpu_svm *svm) |
Joerg Roedel | a069805 | 2008-04-30 17:56:01 +0200 | [diff] [blame] | 1573 | { |
| 1574 | ++svm->vcpu.stat.irq_exits; |
| 1575 | return 1; |
| 1576 | } |
| 1577 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1578 | static int nop_on_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1579 | { |
| 1580 | return 1; |
| 1581 | } |
| 1582 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1583 | static int halt_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1584 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1585 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 1; |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1586 | skip_emulated_instruction(&svm->vcpu); |
| 1587 | return kvm_emulate_halt(&svm->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1588 | } |
| 1589 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1590 | static int vmmcall_interception(struct vcpu_svm *svm) |
Avi Kivity | 02e235b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1591 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1592 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1593 | skip_emulated_instruction(&svm->vcpu); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1594 | kvm_emulate_hypercall(&svm->vcpu); |
| 1595 | return 1; |
Avi Kivity | 02e235b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1596 | } |
| 1597 | |
Alexander Graf | c072542 | 2008-11-25 20:17:03 +0100 | [diff] [blame] | 1598 | static int nested_svm_check_permissions(struct vcpu_svm *svm) |
| 1599 | { |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 1600 | if (!(svm->vcpu.arch.efer & EFER_SVME) |
Alexander Graf | c072542 | 2008-11-25 20:17:03 +0100 | [diff] [blame] | 1601 | || !is_paging(&svm->vcpu)) { |
| 1602 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
| 1603 | return 1; |
| 1604 | } |
| 1605 | |
| 1606 | if (svm->vmcb->save.cpl) { |
| 1607 | kvm_inject_gp(&svm->vcpu, 0); |
| 1608 | return 1; |
| 1609 | } |
| 1610 | |
| 1611 | return 0; |
| 1612 | } |
| 1613 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1614 | static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr, |
| 1615 | bool has_error_code, u32 error_code) |
| 1616 | { |
Joerg Roedel | b8e88bc | 2010-02-19 16:23:02 +0100 | [diff] [blame] | 1617 | int vmexit; |
| 1618 | |
Joerg Roedel | 0295ad7 | 2009-08-07 11:49:37 +0200 | [diff] [blame] | 1619 | if (!is_nested(svm)) |
| 1620 | return 0; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1621 | |
Joerg Roedel | 0295ad7 | 2009-08-07 11:49:37 +0200 | [diff] [blame] | 1622 | svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr; |
| 1623 | svm->vmcb->control.exit_code_hi = 0; |
| 1624 | svm->vmcb->control.exit_info_1 = error_code; |
| 1625 | svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2; |
| 1626 | |
Joerg Roedel | b8e88bc | 2010-02-19 16:23:02 +0100 | [diff] [blame] | 1627 | vmexit = nested_svm_intercept(svm); |
| 1628 | if (vmexit == NESTED_EXIT_DONE) |
| 1629 | svm->nested.exit_required = true; |
| 1630 | |
| 1631 | return vmexit; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1632 | } |
| 1633 | |
Joerg Roedel | 8fe5465 | 2010-02-19 16:23:01 +0100 | [diff] [blame] | 1634 | /* This function returns true if it is save to enable the irq window */ |
| 1635 | static inline bool nested_svm_intr(struct vcpu_svm *svm) |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1636 | { |
Joerg Roedel | 2666695 | 2009-08-07 11:49:46 +0200 | [diff] [blame] | 1637 | if (!is_nested(svm)) |
Joerg Roedel | 8fe5465 | 2010-02-19 16:23:01 +0100 | [diff] [blame] | 1638 | return true; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1639 | |
Joerg Roedel | 2666695 | 2009-08-07 11:49:46 +0200 | [diff] [blame] | 1640 | if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK)) |
Joerg Roedel | 8fe5465 | 2010-02-19 16:23:01 +0100 | [diff] [blame] | 1641 | return true; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1642 | |
Joerg Roedel | 2666695 | 2009-08-07 11:49:46 +0200 | [diff] [blame] | 1643 | if (!(svm->vcpu.arch.hflags & HF_HIF_MASK)) |
Joerg Roedel | 8fe5465 | 2010-02-19 16:23:01 +0100 | [diff] [blame] | 1644 | return false; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1645 | |
Joerg Roedel | 197717d | 2010-02-24 18:59:19 +0100 | [diff] [blame] | 1646 | svm->vmcb->control.exit_code = SVM_EXIT_INTR; |
| 1647 | svm->vmcb->control.exit_info_1 = 0; |
| 1648 | svm->vmcb->control.exit_info_2 = 0; |
Joerg Roedel | 2666695 | 2009-08-07 11:49:46 +0200 | [diff] [blame] | 1649 | |
Joerg Roedel | cd3ff65 | 2009-10-09 16:08:26 +0200 | [diff] [blame] | 1650 | if (svm->nested.intercept & 1ULL) { |
| 1651 | /* |
| 1652 | * The #vmexit can't be emulated here directly because this |
| 1653 | * code path runs with irqs and preemtion disabled. A |
| 1654 | * #vmexit emulation might sleep. Only signal request for |
| 1655 | * the #vmexit here. |
| 1656 | */ |
| 1657 | svm->nested.exit_required = true; |
Joerg Roedel | 236649d | 2009-10-09 16:08:30 +0200 | [diff] [blame] | 1658 | trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip); |
Joerg Roedel | 8fe5465 | 2010-02-19 16:23:01 +0100 | [diff] [blame] | 1659 | return false; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1660 | } |
| 1661 | |
Joerg Roedel | 8fe5465 | 2010-02-19 16:23:01 +0100 | [diff] [blame] | 1662 | return true; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1663 | } |
| 1664 | |
Joerg Roedel | 887f500 | 2010-02-24 18:59:12 +0100 | [diff] [blame] | 1665 | /* This function returns true if it is save to enable the nmi window */ |
| 1666 | static inline bool nested_svm_nmi(struct vcpu_svm *svm) |
| 1667 | { |
| 1668 | if (!is_nested(svm)) |
| 1669 | return true; |
| 1670 | |
| 1671 | if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI))) |
| 1672 | return true; |
| 1673 | |
| 1674 | svm->vmcb->control.exit_code = SVM_EXIT_NMI; |
| 1675 | svm->nested.exit_required = true; |
| 1676 | |
| 1677 | return false; |
| 1678 | } |
| 1679 | |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 1680 | static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page) |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 1681 | { |
| 1682 | struct page *page; |
| 1683 | |
Joerg Roedel | 6c3bd3d | 2010-02-19 16:23:04 +0100 | [diff] [blame] | 1684 | might_sleep(); |
| 1685 | |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 1686 | page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT); |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 1687 | if (is_error_page(page)) |
| 1688 | goto error; |
| 1689 | |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 1690 | *_page = page; |
| 1691 | |
| 1692 | return kmap(page); |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 1693 | |
| 1694 | error: |
| 1695 | kvm_release_page_clean(page); |
| 1696 | kvm_inject_gp(&svm->vcpu, 0); |
| 1697 | |
| 1698 | return NULL; |
| 1699 | } |
| 1700 | |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 1701 | static void nested_svm_unmap(struct page *page) |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 1702 | { |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 1703 | kunmap(page); |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 1704 | kvm_release_page_dirty(page); |
| 1705 | } |
| 1706 | |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 1707 | static int nested_svm_intercept_ioio(struct vcpu_svm *svm) |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1708 | { |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 1709 | unsigned port; |
| 1710 | u8 val, bit; |
| 1711 | u64 gpa; |
| 1712 | |
| 1713 | if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT))) |
| 1714 | return NESTED_EXIT_HOST; |
| 1715 | |
| 1716 | port = svm->vmcb->control.exit_info_1 >> 16; |
| 1717 | gpa = svm->nested.vmcb_iopm + (port / 8); |
| 1718 | bit = port % 8; |
| 1719 | val = 0; |
| 1720 | |
| 1721 | if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1)) |
| 1722 | val &= (1 << bit); |
| 1723 | |
| 1724 | return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST; |
| 1725 | } |
| 1726 | |
Joerg Roedel | d247782 | 2010-03-01 15:34:34 +0100 | [diff] [blame] | 1727 | static int nested_svm_exit_handled_msr(struct vcpu_svm *svm) |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1728 | { |
Joerg Roedel | 0d6b353 | 2010-03-01 15:34:38 +0100 | [diff] [blame] | 1729 | u32 offset, msr, value; |
| 1730 | int write, mask; |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 1731 | |
Joerg Roedel | 3d62d9a | 2009-08-07 11:49:39 +0200 | [diff] [blame] | 1732 | if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT))) |
Joerg Roedel | d247782 | 2010-03-01 15:34:34 +0100 | [diff] [blame] | 1733 | return NESTED_EXIT_HOST; |
Joerg Roedel | 3d62d9a | 2009-08-07 11:49:39 +0200 | [diff] [blame] | 1734 | |
Joerg Roedel | 0d6b353 | 2010-03-01 15:34:38 +0100 | [diff] [blame] | 1735 | msr = svm->vcpu.arch.regs[VCPU_REGS_RCX]; |
| 1736 | offset = svm_msrpm_offset(msr); |
| 1737 | write = svm->vmcb->control.exit_info_1 & 1; |
| 1738 | mask = 1 << ((2 * (msr & 0xf)) + write); |
Joerg Roedel | 3d62d9a | 2009-08-07 11:49:39 +0200 | [diff] [blame] | 1739 | |
Joerg Roedel | 0d6b353 | 2010-03-01 15:34:38 +0100 | [diff] [blame] | 1740 | if (offset == MSR_INVALID) |
| 1741 | return NESTED_EXIT_DONE; |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 1742 | |
Joerg Roedel | 0d6b353 | 2010-03-01 15:34:38 +0100 | [diff] [blame] | 1743 | /* Offset is in 32 bit units but need in 8 bit units */ |
| 1744 | offset *= 4; |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 1745 | |
Joerg Roedel | 0d6b353 | 2010-03-01 15:34:38 +0100 | [diff] [blame] | 1746 | if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4)) |
| 1747 | return NESTED_EXIT_DONE; |
Joerg Roedel | 3d62d9a | 2009-08-07 11:49:39 +0200 | [diff] [blame] | 1748 | |
Joerg Roedel | 0d6b353 | 2010-03-01 15:34:38 +0100 | [diff] [blame] | 1749 | return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST; |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 1750 | } |
| 1751 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 1752 | static int nested_svm_exit_special(struct vcpu_svm *svm) |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 1753 | { |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1754 | u32 exit_code = svm->vmcb->control.exit_code; |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 1755 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 1756 | switch (exit_code) { |
| 1757 | case SVM_EXIT_INTR: |
| 1758 | case SVM_EXIT_NMI: |
Joerg Roedel | ff47a49 | 2010-04-22 12:33:14 +0200 | [diff] [blame] | 1759 | case SVM_EXIT_EXCP_BASE + MC_VECTOR: |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 1760 | return NESTED_EXIT_HOST; |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 1761 | case SVM_EXIT_NPF: |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 1762 | /* For now we are always handling NPFs when using them */ |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 1763 | if (npt_enabled) |
| 1764 | return NESTED_EXIT_HOST; |
| 1765 | break; |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 1766 | case SVM_EXIT_EXCP_BASE + PF_VECTOR: |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 1767 | /* When we're shadowing, trap PFs */ |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 1768 | if (!npt_enabled) |
| 1769 | return NESTED_EXIT_HOST; |
| 1770 | break; |
Joerg Roedel | 66a562f | 2010-02-19 16:23:08 +0100 | [diff] [blame] | 1771 | case SVM_EXIT_EXCP_BASE + NM_VECTOR: |
| 1772 | nm_interception(svm); |
| 1773 | break; |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 1774 | default: |
| 1775 | break; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1776 | } |
| 1777 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 1778 | return NESTED_EXIT_CONTINUE; |
| 1779 | } |
| 1780 | |
| 1781 | /* |
| 1782 | * If this function returns true, this #vmexit was already handled |
| 1783 | */ |
Joerg Roedel | b8e88bc | 2010-02-19 16:23:02 +0100 | [diff] [blame] | 1784 | static int nested_svm_intercept(struct vcpu_svm *svm) |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 1785 | { |
| 1786 | u32 exit_code = svm->vmcb->control.exit_code; |
| 1787 | int vmexit = NESTED_EXIT_HOST; |
| 1788 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1789 | switch (exit_code) { |
Joerg Roedel | 9c4e40b9 | 2009-08-07 11:49:36 +0200 | [diff] [blame] | 1790 | case SVM_EXIT_MSR: |
Joerg Roedel | 3d62d9a | 2009-08-07 11:49:39 +0200 | [diff] [blame] | 1791 | vmexit = nested_svm_exit_handled_msr(svm); |
Joerg Roedel | 9c4e40b9 | 2009-08-07 11:49:36 +0200 | [diff] [blame] | 1792 | break; |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 1793 | case SVM_EXIT_IOIO: |
| 1794 | vmexit = nested_svm_intercept_ioio(svm); |
| 1795 | break; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1796 | case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR8: { |
| 1797 | u32 cr_bits = 1 << (exit_code - SVM_EXIT_READ_CR0); |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 1798 | if (svm->nested.intercept_cr_read & cr_bits) |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 1799 | vmexit = NESTED_EXIT_DONE; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1800 | break; |
| 1801 | } |
| 1802 | case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR8: { |
| 1803 | u32 cr_bits = 1 << (exit_code - SVM_EXIT_WRITE_CR0); |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 1804 | if (svm->nested.intercept_cr_write & cr_bits) |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 1805 | vmexit = NESTED_EXIT_DONE; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1806 | break; |
| 1807 | } |
| 1808 | case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR7: { |
| 1809 | u32 dr_bits = 1 << (exit_code - SVM_EXIT_READ_DR0); |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 1810 | if (svm->nested.intercept_dr_read & dr_bits) |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 1811 | vmexit = NESTED_EXIT_DONE; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1812 | break; |
| 1813 | } |
| 1814 | case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR7: { |
| 1815 | u32 dr_bits = 1 << (exit_code - SVM_EXIT_WRITE_DR0); |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 1816 | if (svm->nested.intercept_dr_write & dr_bits) |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 1817 | vmexit = NESTED_EXIT_DONE; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1818 | break; |
| 1819 | } |
| 1820 | case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: { |
| 1821 | u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE); |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 1822 | if (svm->nested.intercept_exceptions & excp_bits) |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 1823 | vmexit = NESTED_EXIT_DONE; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1824 | break; |
| 1825 | } |
Joerg Roedel | 228070b | 2010-04-22 12:33:10 +0200 | [diff] [blame] | 1826 | case SVM_EXIT_ERR: { |
| 1827 | vmexit = NESTED_EXIT_DONE; |
| 1828 | break; |
| 1829 | } |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1830 | default: { |
| 1831 | u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR); |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 1832 | if (svm->nested.intercept & exit_bits) |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 1833 | vmexit = NESTED_EXIT_DONE; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1834 | } |
| 1835 | } |
| 1836 | |
Joerg Roedel | b8e88bc | 2010-02-19 16:23:02 +0100 | [diff] [blame] | 1837 | return vmexit; |
| 1838 | } |
| 1839 | |
| 1840 | static int nested_svm_exit_handled(struct vcpu_svm *svm) |
| 1841 | { |
| 1842 | int vmexit; |
| 1843 | |
| 1844 | vmexit = nested_svm_intercept(svm); |
| 1845 | |
| 1846 | if (vmexit == NESTED_EXIT_DONE) |
Joerg Roedel | 9c4e40b9 | 2009-08-07 11:49:36 +0200 | [diff] [blame] | 1847 | nested_svm_vmexit(svm); |
Joerg Roedel | 9c4e40b9 | 2009-08-07 11:49:36 +0200 | [diff] [blame] | 1848 | |
| 1849 | return vmexit; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1850 | } |
| 1851 | |
Joerg Roedel | 0460a97 | 2009-08-07 11:49:31 +0200 | [diff] [blame] | 1852 | static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb) |
| 1853 | { |
| 1854 | struct vmcb_control_area *dst = &dst_vmcb->control; |
| 1855 | struct vmcb_control_area *from = &from_vmcb->control; |
| 1856 | |
| 1857 | dst->intercept_cr_read = from->intercept_cr_read; |
| 1858 | dst->intercept_cr_write = from->intercept_cr_write; |
| 1859 | dst->intercept_dr_read = from->intercept_dr_read; |
| 1860 | dst->intercept_dr_write = from->intercept_dr_write; |
| 1861 | dst->intercept_exceptions = from->intercept_exceptions; |
| 1862 | dst->intercept = from->intercept; |
| 1863 | dst->iopm_base_pa = from->iopm_base_pa; |
| 1864 | dst->msrpm_base_pa = from->msrpm_base_pa; |
| 1865 | dst->tsc_offset = from->tsc_offset; |
| 1866 | dst->asid = from->asid; |
| 1867 | dst->tlb_ctl = from->tlb_ctl; |
| 1868 | dst->int_ctl = from->int_ctl; |
| 1869 | dst->int_vector = from->int_vector; |
| 1870 | dst->int_state = from->int_state; |
| 1871 | dst->exit_code = from->exit_code; |
| 1872 | dst->exit_code_hi = from->exit_code_hi; |
| 1873 | dst->exit_info_1 = from->exit_info_1; |
| 1874 | dst->exit_info_2 = from->exit_info_2; |
| 1875 | dst->exit_int_info = from->exit_int_info; |
| 1876 | dst->exit_int_info_err = from->exit_int_info_err; |
| 1877 | dst->nested_ctl = from->nested_ctl; |
| 1878 | dst->event_inj = from->event_inj; |
| 1879 | dst->event_inj_err = from->event_inj_err; |
| 1880 | dst->nested_cr3 = from->nested_cr3; |
| 1881 | dst->lbr_ctl = from->lbr_ctl; |
| 1882 | } |
| 1883 | |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 1884 | static int nested_svm_vmexit(struct vcpu_svm *svm) |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1885 | { |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 1886 | struct vmcb *nested_vmcb; |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 1887 | struct vmcb *hsave = svm->nested.hsave; |
Joerg Roedel | 33740e4 | 2009-08-07 11:49:29 +0200 | [diff] [blame] | 1888 | struct vmcb *vmcb = svm->vmcb; |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 1889 | struct page *page; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1890 | |
Joerg Roedel | 17897f3 | 2009-10-09 16:08:29 +0200 | [diff] [blame] | 1891 | trace_kvm_nested_vmexit_inject(vmcb->control.exit_code, |
| 1892 | vmcb->control.exit_info_1, |
| 1893 | vmcb->control.exit_info_2, |
| 1894 | vmcb->control.exit_int_info, |
| 1895 | vmcb->control.exit_int_info_err); |
| 1896 | |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 1897 | nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page); |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 1898 | if (!nested_vmcb) |
| 1899 | return 1; |
| 1900 | |
Joerg Roedel | 06fc7772 | 2010-02-19 16:23:07 +0100 | [diff] [blame] | 1901 | /* Exit nested SVM mode */ |
| 1902 | svm->nested.vmcb = 0; |
| 1903 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1904 | /* Give the current vmcb to the guest */ |
Joerg Roedel | 33740e4 | 2009-08-07 11:49:29 +0200 | [diff] [blame] | 1905 | disable_gif(svm); |
| 1906 | |
| 1907 | nested_vmcb->save.es = vmcb->save.es; |
| 1908 | nested_vmcb->save.cs = vmcb->save.cs; |
| 1909 | nested_vmcb->save.ss = vmcb->save.ss; |
| 1910 | nested_vmcb->save.ds = vmcb->save.ds; |
| 1911 | nested_vmcb->save.gdtr = vmcb->save.gdtr; |
| 1912 | nested_vmcb->save.idtr = vmcb->save.idtr; |
Joerg Roedel | 3f6a9d1 | 2010-07-27 18:14:20 +0200 | [diff] [blame] | 1913 | nested_vmcb->save.efer = svm->vcpu.arch.efer; |
Joerg Roedel | cdbbdc1 | 2010-02-19 16:23:03 +0100 | [diff] [blame] | 1914 | nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu); |
Joerg Roedel | 2be4fc7 | 2010-04-22 12:33:09 +0200 | [diff] [blame] | 1915 | nested_vmcb->save.cr3 = svm->vcpu.arch.cr3; |
Joerg Roedel | 33740e4 | 2009-08-07 11:49:29 +0200 | [diff] [blame] | 1916 | nested_vmcb->save.cr2 = vmcb->save.cr2; |
Joerg Roedel | cdbbdc1 | 2010-02-19 16:23:03 +0100 | [diff] [blame] | 1917 | nested_vmcb->save.cr4 = svm->vcpu.arch.cr4; |
Joerg Roedel | 33740e4 | 2009-08-07 11:49:29 +0200 | [diff] [blame] | 1918 | nested_vmcb->save.rflags = vmcb->save.rflags; |
| 1919 | nested_vmcb->save.rip = vmcb->save.rip; |
| 1920 | nested_vmcb->save.rsp = vmcb->save.rsp; |
| 1921 | nested_vmcb->save.rax = vmcb->save.rax; |
| 1922 | nested_vmcb->save.dr7 = vmcb->save.dr7; |
| 1923 | nested_vmcb->save.dr6 = vmcb->save.dr6; |
| 1924 | nested_vmcb->save.cpl = vmcb->save.cpl; |
| 1925 | |
| 1926 | nested_vmcb->control.int_ctl = vmcb->control.int_ctl; |
| 1927 | nested_vmcb->control.int_vector = vmcb->control.int_vector; |
| 1928 | nested_vmcb->control.int_state = vmcb->control.int_state; |
| 1929 | nested_vmcb->control.exit_code = vmcb->control.exit_code; |
| 1930 | nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi; |
| 1931 | nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1; |
| 1932 | nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2; |
| 1933 | nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info; |
| 1934 | nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err; |
Joerg Roedel | 7a19066 | 2010-07-27 18:14:21 +0200 | [diff] [blame] | 1935 | nested_vmcb->control.next_rip = vmcb->control.next_rip; |
Alexander Graf | 8d23c46 | 2009-10-09 16:08:25 +0200 | [diff] [blame] | 1936 | |
| 1937 | /* |
| 1938 | * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have |
| 1939 | * to make sure that we do not lose injected events. So check event_inj |
| 1940 | * here and copy it to exit_int_info if it is valid. |
| 1941 | * Exit_int_info and event_inj can't be both valid because the case |
| 1942 | * below only happens on a VMRUN instruction intercept which has |
| 1943 | * no valid exit_int_info set. |
| 1944 | */ |
| 1945 | if (vmcb->control.event_inj & SVM_EVTINJ_VALID) { |
| 1946 | struct vmcb_control_area *nc = &nested_vmcb->control; |
| 1947 | |
| 1948 | nc->exit_int_info = vmcb->control.event_inj; |
| 1949 | nc->exit_int_info_err = vmcb->control.event_inj_err; |
| 1950 | } |
| 1951 | |
Joerg Roedel | 33740e4 | 2009-08-07 11:49:29 +0200 | [diff] [blame] | 1952 | nested_vmcb->control.tlb_ctl = 0; |
| 1953 | nested_vmcb->control.event_inj = 0; |
| 1954 | nested_vmcb->control.event_inj_err = 0; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1955 | |
| 1956 | /* We always set V_INTR_MASKING and remember the old value in hflags */ |
| 1957 | if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK)) |
| 1958 | nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK; |
| 1959 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1960 | /* Restore the original control entries */ |
Joerg Roedel | 0460a97 | 2009-08-07 11:49:31 +0200 | [diff] [blame] | 1961 | copy_vmcb_control_area(vmcb, hsave); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1962 | |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 1963 | kvm_clear_exception_queue(&svm->vcpu); |
| 1964 | kvm_clear_interrupt_queue(&svm->vcpu); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1965 | |
| 1966 | /* Restore selected save entries */ |
| 1967 | svm->vmcb->save.es = hsave->save.es; |
| 1968 | svm->vmcb->save.cs = hsave->save.cs; |
| 1969 | svm->vmcb->save.ss = hsave->save.ss; |
| 1970 | svm->vmcb->save.ds = hsave->save.ds; |
| 1971 | svm->vmcb->save.gdtr = hsave->save.gdtr; |
| 1972 | svm->vmcb->save.idtr = hsave->save.idtr; |
| 1973 | svm->vmcb->save.rflags = hsave->save.rflags; |
| 1974 | svm_set_efer(&svm->vcpu, hsave->save.efer); |
| 1975 | svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE); |
| 1976 | svm_set_cr4(&svm->vcpu, hsave->save.cr4); |
| 1977 | if (npt_enabled) { |
| 1978 | svm->vmcb->save.cr3 = hsave->save.cr3; |
| 1979 | svm->vcpu.arch.cr3 = hsave->save.cr3; |
| 1980 | } else { |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 1981 | (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1982 | } |
| 1983 | kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax); |
| 1984 | kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp); |
| 1985 | kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip); |
| 1986 | svm->vmcb->save.dr7 = 0; |
| 1987 | svm->vmcb->save.cpl = 0; |
| 1988 | svm->vmcb->control.exit_int_info = 0; |
| 1989 | |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 1990 | nested_svm_unmap(page); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1991 | |
| 1992 | kvm_mmu_reset_context(&svm->vcpu); |
| 1993 | kvm_mmu_load(&svm->vcpu); |
| 1994 | |
| 1995 | return 0; |
| 1996 | } |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1997 | |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 1998 | static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm) |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1999 | { |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 2000 | /* |
| 2001 | * This function merges the msr permission bitmaps of kvm and the |
| 2002 | * nested vmcb. It is omptimized in that it only merges the parts where |
| 2003 | * the kvm msr permission bitmap may contain zero bits |
| 2004 | */ |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2005 | int i; |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 2006 | |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 2007 | if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT))) |
| 2008 | return true; |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 2009 | |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 2010 | for (i = 0; i < MSRPM_OFFSETS; i++) { |
| 2011 | u32 value, p; |
| 2012 | u64 offset; |
| 2013 | |
| 2014 | if (msrpm_offsets[i] == 0xffffffff) |
| 2015 | break; |
| 2016 | |
Joerg Roedel | 0d6b353 | 2010-03-01 15:34:38 +0100 | [diff] [blame] | 2017 | p = msrpm_offsets[i]; |
| 2018 | offset = svm->nested.vmcb_msrpm + (p * 4); |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 2019 | |
| 2020 | if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4)) |
| 2021 | return false; |
| 2022 | |
| 2023 | svm->nested.msrpm[p] = svm->msrpm[p] | value; |
| 2024 | } |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 2025 | |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 2026 | svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2027 | |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 2028 | return true; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2029 | } |
| 2030 | |
Joerg Roedel | 52c65a30 | 2010-08-02 16:46:44 +0200 | [diff] [blame] | 2031 | static bool nested_vmcb_checks(struct vmcb *vmcb) |
| 2032 | { |
| 2033 | if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0) |
| 2034 | return false; |
| 2035 | |
Joerg Roedel | dbe7758 | 2010-08-02 16:46:45 +0200 | [diff] [blame] | 2036 | if (vmcb->control.asid == 0) |
| 2037 | return false; |
| 2038 | |
Joerg Roedel | 52c65a30 | 2010-08-02 16:46:44 +0200 | [diff] [blame] | 2039 | return true; |
| 2040 | } |
| 2041 | |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 2042 | static bool nested_svm_vmrun(struct vcpu_svm *svm) |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2043 | { |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 2044 | struct vmcb *nested_vmcb; |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 2045 | struct vmcb *hsave = svm->nested.hsave; |
Joerg Roedel | defbba5 | 2009-08-07 11:49:30 +0200 | [diff] [blame] | 2046 | struct vmcb *vmcb = svm->vmcb; |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 2047 | struct page *page; |
Joerg Roedel | 06fc7772 | 2010-02-19 16:23:07 +0100 | [diff] [blame] | 2048 | u64 vmcb_gpa; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2049 | |
Joerg Roedel | 06fc7772 | 2010-02-19 16:23:07 +0100 | [diff] [blame] | 2050 | vmcb_gpa = svm->vmcb->save.rax; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2051 | |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 2052 | nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page); |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 2053 | if (!nested_vmcb) |
| 2054 | return false; |
| 2055 | |
Joerg Roedel | 52c65a30 | 2010-08-02 16:46:44 +0200 | [diff] [blame] | 2056 | if (!nested_vmcb_checks(nested_vmcb)) { |
| 2057 | nested_vmcb->control.exit_code = SVM_EXIT_ERR; |
| 2058 | nested_vmcb->control.exit_code_hi = 0; |
| 2059 | nested_vmcb->control.exit_info_1 = 0; |
| 2060 | nested_vmcb->control.exit_info_2 = 0; |
| 2061 | |
| 2062 | nested_svm_unmap(page); |
| 2063 | |
| 2064 | return false; |
| 2065 | } |
| 2066 | |
Joerg Roedel | ecf1405 | 2010-02-24 18:59:13 +0100 | [diff] [blame] | 2067 | trace_kvm_nested_vmrun(svm->vmcb->save.rip - 3, vmcb_gpa, |
Joerg Roedel | 0ac406d | 2009-10-09 16:08:27 +0200 | [diff] [blame] | 2068 | nested_vmcb->save.rip, |
| 2069 | nested_vmcb->control.int_ctl, |
| 2070 | nested_vmcb->control.event_inj, |
| 2071 | nested_vmcb->control.nested_ctl); |
| 2072 | |
Joerg Roedel | 2e554e8 | 2010-02-24 18:59:14 +0100 | [diff] [blame] | 2073 | trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr_read, |
| 2074 | nested_vmcb->control.intercept_cr_write, |
| 2075 | nested_vmcb->control.intercept_exceptions, |
| 2076 | nested_vmcb->control.intercept); |
| 2077 | |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2078 | /* Clear internal status */ |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 2079 | kvm_clear_exception_queue(&svm->vcpu); |
| 2080 | kvm_clear_interrupt_queue(&svm->vcpu); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2081 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 2082 | /* |
| 2083 | * Save the old vmcb, so we don't need to pick what we save, but can |
| 2084 | * restore everything when a VMEXIT occurs |
| 2085 | */ |
Joerg Roedel | defbba5 | 2009-08-07 11:49:30 +0200 | [diff] [blame] | 2086 | hsave->save.es = vmcb->save.es; |
| 2087 | hsave->save.cs = vmcb->save.cs; |
| 2088 | hsave->save.ss = vmcb->save.ss; |
| 2089 | hsave->save.ds = vmcb->save.ds; |
| 2090 | hsave->save.gdtr = vmcb->save.gdtr; |
| 2091 | hsave->save.idtr = vmcb->save.idtr; |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 2092 | hsave->save.efer = svm->vcpu.arch.efer; |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 2093 | hsave->save.cr0 = kvm_read_cr0(&svm->vcpu); |
Joerg Roedel | defbba5 | 2009-08-07 11:49:30 +0200 | [diff] [blame] | 2094 | hsave->save.cr4 = svm->vcpu.arch.cr4; |
| 2095 | hsave->save.rflags = vmcb->save.rflags; |
| 2096 | hsave->save.rip = svm->next_rip; |
| 2097 | hsave->save.rsp = vmcb->save.rsp; |
| 2098 | hsave->save.rax = vmcb->save.rax; |
| 2099 | if (npt_enabled) |
| 2100 | hsave->save.cr3 = vmcb->save.cr3; |
| 2101 | else |
| 2102 | hsave->save.cr3 = svm->vcpu.arch.cr3; |
| 2103 | |
Joerg Roedel | 0460a97 | 2009-08-07 11:49:31 +0200 | [diff] [blame] | 2104 | copy_vmcb_control_area(hsave, vmcb); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2105 | |
| 2106 | if (svm->vmcb->save.rflags & X86_EFLAGS_IF) |
| 2107 | svm->vcpu.arch.hflags |= HF_HIF_MASK; |
| 2108 | else |
| 2109 | svm->vcpu.arch.hflags &= ~HF_HIF_MASK; |
| 2110 | |
| 2111 | /* Load the nested guest state */ |
| 2112 | svm->vmcb->save.es = nested_vmcb->save.es; |
| 2113 | svm->vmcb->save.cs = nested_vmcb->save.cs; |
| 2114 | svm->vmcb->save.ss = nested_vmcb->save.ss; |
| 2115 | svm->vmcb->save.ds = nested_vmcb->save.ds; |
| 2116 | svm->vmcb->save.gdtr = nested_vmcb->save.gdtr; |
| 2117 | svm->vmcb->save.idtr = nested_vmcb->save.idtr; |
| 2118 | svm->vmcb->save.rflags = nested_vmcb->save.rflags; |
| 2119 | svm_set_efer(&svm->vcpu, nested_vmcb->save.efer); |
| 2120 | svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0); |
| 2121 | svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4); |
| 2122 | if (npt_enabled) { |
| 2123 | svm->vmcb->save.cr3 = nested_vmcb->save.cr3; |
| 2124 | svm->vcpu.arch.cr3 = nested_vmcb->save.cr3; |
Joerg Roedel | 0e5cbe3 | 2010-02-24 18:59:11 +0100 | [diff] [blame] | 2125 | } else |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 2126 | (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3); |
Joerg Roedel | 0e5cbe3 | 2010-02-24 18:59:11 +0100 | [diff] [blame] | 2127 | |
| 2128 | /* Guest paging mode is active - reset mmu */ |
| 2129 | kvm_mmu_reset_context(&svm->vcpu); |
| 2130 | |
Joerg Roedel | defbba5 | 2009-08-07 11:49:30 +0200 | [diff] [blame] | 2131 | svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2132 | kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax); |
| 2133 | kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp); |
| 2134 | kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip); |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 2135 | |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2136 | /* In case we don't even reach vcpu_run, the fields are not updated */ |
| 2137 | svm->vmcb->save.rax = nested_vmcb->save.rax; |
| 2138 | svm->vmcb->save.rsp = nested_vmcb->save.rsp; |
| 2139 | svm->vmcb->save.rip = nested_vmcb->save.rip; |
| 2140 | svm->vmcb->save.dr7 = nested_vmcb->save.dr7; |
| 2141 | svm->vmcb->save.dr6 = nested_vmcb->save.dr6; |
| 2142 | svm->vmcb->save.cpl = nested_vmcb->save.cpl; |
| 2143 | |
Joerg Roedel | f713853 | 2010-03-01 15:34:40 +0100 | [diff] [blame] | 2144 | svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL; |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 2145 | svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2146 | |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 2147 | /* cache intercepts */ |
| 2148 | svm->nested.intercept_cr_read = nested_vmcb->control.intercept_cr_read; |
| 2149 | svm->nested.intercept_cr_write = nested_vmcb->control.intercept_cr_write; |
| 2150 | svm->nested.intercept_dr_read = nested_vmcb->control.intercept_dr_read; |
| 2151 | svm->nested.intercept_dr_write = nested_vmcb->control.intercept_dr_write; |
| 2152 | svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions; |
| 2153 | svm->nested.intercept = nested_vmcb->control.intercept; |
| 2154 | |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2155 | force_new_asid(&svm->vcpu); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2156 | svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2157 | if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK) |
| 2158 | svm->vcpu.arch.hflags |= HF_VINTR_MASK; |
| 2159 | else |
| 2160 | svm->vcpu.arch.hflags &= ~HF_VINTR_MASK; |
| 2161 | |
Joerg Roedel | 88ab24a | 2010-02-19 16:23:06 +0100 | [diff] [blame] | 2162 | if (svm->vcpu.arch.hflags & HF_VINTR_MASK) { |
| 2163 | /* We only want the cr8 intercept bits of the guest */ |
| 2164 | svm->vmcb->control.intercept_cr_read &= ~INTERCEPT_CR8_MASK; |
| 2165 | svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK; |
| 2166 | } |
| 2167 | |
Joerg Roedel | 0d945bd | 2010-05-05 16:04:45 +0200 | [diff] [blame] | 2168 | /* We don't want to see VMMCALLs from a nested guest */ |
| 2169 | svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VMMCALL); |
| 2170 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 2171 | /* |
| 2172 | * We don't want a nested guest to be more powerful than the guest, so |
| 2173 | * all intercepts are ORed |
| 2174 | */ |
Joerg Roedel | 88ab24a | 2010-02-19 16:23:06 +0100 | [diff] [blame] | 2175 | svm->vmcb->control.intercept_cr_read |= |
| 2176 | nested_vmcb->control.intercept_cr_read; |
| 2177 | svm->vmcb->control.intercept_cr_write |= |
| 2178 | nested_vmcb->control.intercept_cr_write; |
| 2179 | svm->vmcb->control.intercept_dr_read |= |
| 2180 | nested_vmcb->control.intercept_dr_read; |
| 2181 | svm->vmcb->control.intercept_dr_write |= |
| 2182 | nested_vmcb->control.intercept_dr_write; |
| 2183 | svm->vmcb->control.intercept_exceptions |= |
| 2184 | nested_vmcb->control.intercept_exceptions; |
| 2185 | |
| 2186 | svm->vmcb->control.intercept |= nested_vmcb->control.intercept; |
| 2187 | |
| 2188 | svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2189 | svm->vmcb->control.int_vector = nested_vmcb->control.int_vector; |
| 2190 | svm->vmcb->control.int_state = nested_vmcb->control.int_state; |
| 2191 | svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2192 | svm->vmcb->control.event_inj = nested_vmcb->control.event_inj; |
| 2193 | svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err; |
| 2194 | |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 2195 | nested_svm_unmap(page); |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 2196 | |
Joerg Roedel | 06fc7772 | 2010-02-19 16:23:07 +0100 | [diff] [blame] | 2197 | /* nested_vmcb is our indicator if nested SVM is activated */ |
| 2198 | svm->nested.vmcb = vmcb_gpa; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2199 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 2200 | enable_gif(svm); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2201 | |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 2202 | return true; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2203 | } |
| 2204 | |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 2205 | static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb) |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 2206 | { |
| 2207 | to_vmcb->save.fs = from_vmcb->save.fs; |
| 2208 | to_vmcb->save.gs = from_vmcb->save.gs; |
| 2209 | to_vmcb->save.tr = from_vmcb->save.tr; |
| 2210 | to_vmcb->save.ldtr = from_vmcb->save.ldtr; |
| 2211 | to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base; |
| 2212 | to_vmcb->save.star = from_vmcb->save.star; |
| 2213 | to_vmcb->save.lstar = from_vmcb->save.lstar; |
| 2214 | to_vmcb->save.cstar = from_vmcb->save.cstar; |
| 2215 | to_vmcb->save.sfmask = from_vmcb->save.sfmask; |
| 2216 | to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs; |
| 2217 | to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp; |
| 2218 | to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip; |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 2219 | } |
| 2220 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2221 | static int vmload_interception(struct vcpu_svm *svm) |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 2222 | { |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 2223 | struct vmcb *nested_vmcb; |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 2224 | struct page *page; |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 2225 | |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 2226 | if (nested_svm_check_permissions(svm)) |
| 2227 | return 1; |
| 2228 | |
| 2229 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
| 2230 | skip_emulated_instruction(&svm->vcpu); |
| 2231 | |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 2232 | nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page); |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 2233 | if (!nested_vmcb) |
| 2234 | return 1; |
| 2235 | |
| 2236 | nested_svm_vmloadsave(nested_vmcb, svm->vmcb); |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 2237 | nested_svm_unmap(page); |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 2238 | |
| 2239 | return 1; |
| 2240 | } |
| 2241 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2242 | static int vmsave_interception(struct vcpu_svm *svm) |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 2243 | { |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 2244 | struct vmcb *nested_vmcb; |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 2245 | struct page *page; |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 2246 | |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 2247 | if (nested_svm_check_permissions(svm)) |
| 2248 | return 1; |
| 2249 | |
| 2250 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
| 2251 | skip_emulated_instruction(&svm->vcpu); |
| 2252 | |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 2253 | nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page); |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 2254 | if (!nested_vmcb) |
| 2255 | return 1; |
| 2256 | |
| 2257 | nested_svm_vmloadsave(svm->vmcb, nested_vmcb); |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 2258 | nested_svm_unmap(page); |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 2259 | |
| 2260 | return 1; |
| 2261 | } |
| 2262 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2263 | static int vmrun_interception(struct vcpu_svm *svm) |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2264 | { |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2265 | if (nested_svm_check_permissions(svm)) |
| 2266 | return 1; |
| 2267 | |
| 2268 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
| 2269 | skip_emulated_instruction(&svm->vcpu); |
| 2270 | |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 2271 | if (!nested_svm_vmrun(svm)) |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2272 | return 1; |
| 2273 | |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 2274 | if (!nested_svm_vmrun_msrpm(svm)) |
Joerg Roedel | 1f8da47 | 2009-08-07 11:49:43 +0200 | [diff] [blame] | 2275 | goto failed; |
| 2276 | |
| 2277 | return 1; |
| 2278 | |
| 2279 | failed: |
| 2280 | |
| 2281 | svm->vmcb->control.exit_code = SVM_EXIT_ERR; |
| 2282 | svm->vmcb->control.exit_code_hi = 0; |
| 2283 | svm->vmcb->control.exit_info_1 = 0; |
| 2284 | svm->vmcb->control.exit_info_2 = 0; |
| 2285 | |
| 2286 | nested_svm_vmexit(svm); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2287 | |
| 2288 | return 1; |
| 2289 | } |
| 2290 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2291 | static int stgi_interception(struct vcpu_svm *svm) |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 2292 | { |
| 2293 | if (nested_svm_check_permissions(svm)) |
| 2294 | return 1; |
| 2295 | |
| 2296 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
| 2297 | skip_emulated_instruction(&svm->vcpu); |
| 2298 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 2299 | enable_gif(svm); |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 2300 | |
| 2301 | return 1; |
| 2302 | } |
| 2303 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2304 | static int clgi_interception(struct vcpu_svm *svm) |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 2305 | { |
| 2306 | if (nested_svm_check_permissions(svm)) |
| 2307 | return 1; |
| 2308 | |
| 2309 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
| 2310 | skip_emulated_instruction(&svm->vcpu); |
| 2311 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 2312 | disable_gif(svm); |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 2313 | |
| 2314 | /* After a CLGI no interrupts should come */ |
| 2315 | svm_clear_vintr(svm); |
| 2316 | svm->vmcb->control.int_ctl &= ~V_IRQ_MASK; |
| 2317 | |
| 2318 | return 1; |
| 2319 | } |
| 2320 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2321 | static int invlpga_interception(struct vcpu_svm *svm) |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 2322 | { |
| 2323 | struct kvm_vcpu *vcpu = &svm->vcpu; |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 2324 | |
Joerg Roedel | ec1ff79 | 2009-10-09 16:08:31 +0200 | [diff] [blame] | 2325 | trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX], |
| 2326 | vcpu->arch.regs[VCPU_REGS_RAX]); |
| 2327 | |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 2328 | /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */ |
| 2329 | kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]); |
| 2330 | |
| 2331 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
| 2332 | skip_emulated_instruction(&svm->vcpu); |
| 2333 | return 1; |
| 2334 | } |
| 2335 | |
Joerg Roedel | 532a46b | 2009-10-09 16:08:32 +0200 | [diff] [blame] | 2336 | static int skinit_interception(struct vcpu_svm *svm) |
| 2337 | { |
| 2338 | trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]); |
| 2339 | |
| 2340 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
| 2341 | return 1; |
| 2342 | } |
| 2343 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2344 | static int invalid_op_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2345 | { |
Avi Kivity | 7ee5d940 | 2007-11-25 15:22:50 +0200 | [diff] [blame] | 2346 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2347 | return 1; |
| 2348 | } |
| 2349 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2350 | static int task_switch_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2351 | { |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 2352 | u16 tss_selector; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 2353 | int reason; |
| 2354 | int int_type = svm->vmcb->control.exit_int_info & |
| 2355 | SVM_EXITINTINFO_TYPE_MASK; |
Gleb Natapov | 8317c29 | 2009-04-12 13:37:02 +0300 | [diff] [blame] | 2356 | int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK; |
Gleb Natapov | fe8e7f8 | 2009-04-23 17:03:48 +0300 | [diff] [blame] | 2357 | uint32_t type = |
| 2358 | svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK; |
| 2359 | uint32_t idt_v = |
| 2360 | svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID; |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2361 | bool has_error_code = false; |
| 2362 | u32 error_code = 0; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 2363 | |
| 2364 | tss_selector = (u16)svm->vmcb->control.exit_info_1; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 2365 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 2366 | if (svm->vmcb->control.exit_info_2 & |
| 2367 | (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET)) |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 2368 | reason = TASK_SWITCH_IRET; |
| 2369 | else if (svm->vmcb->control.exit_info_2 & |
| 2370 | (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP)) |
| 2371 | reason = TASK_SWITCH_JMP; |
Gleb Natapov | fe8e7f8 | 2009-04-23 17:03:48 +0300 | [diff] [blame] | 2372 | else if (idt_v) |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 2373 | reason = TASK_SWITCH_GATE; |
| 2374 | else |
| 2375 | reason = TASK_SWITCH_CALL; |
| 2376 | |
Gleb Natapov | fe8e7f8 | 2009-04-23 17:03:48 +0300 | [diff] [blame] | 2377 | if (reason == TASK_SWITCH_GATE) { |
| 2378 | switch (type) { |
| 2379 | case SVM_EXITINTINFO_TYPE_NMI: |
| 2380 | svm->vcpu.arch.nmi_injected = false; |
| 2381 | break; |
| 2382 | case SVM_EXITINTINFO_TYPE_EXEPT: |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2383 | if (svm->vmcb->control.exit_info_2 & |
| 2384 | (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) { |
| 2385 | has_error_code = true; |
| 2386 | error_code = |
| 2387 | (u32)svm->vmcb->control.exit_info_2; |
| 2388 | } |
Gleb Natapov | fe8e7f8 | 2009-04-23 17:03:48 +0300 | [diff] [blame] | 2389 | kvm_clear_exception_queue(&svm->vcpu); |
| 2390 | break; |
| 2391 | case SVM_EXITINTINFO_TYPE_INTR: |
| 2392 | kvm_clear_interrupt_queue(&svm->vcpu); |
| 2393 | break; |
| 2394 | default: |
| 2395 | break; |
| 2396 | } |
| 2397 | } |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 2398 | |
Gleb Natapov | 8317c29 | 2009-04-12 13:37:02 +0300 | [diff] [blame] | 2399 | if (reason != TASK_SWITCH_GATE || |
| 2400 | int_type == SVM_EXITINTINFO_TYPE_SOFT || |
| 2401 | (int_type == SVM_EXITINTINFO_TYPE_EXEPT && |
Gleb Natapov | f629cf8 | 2009-05-11 13:35:49 +0300 | [diff] [blame] | 2402 | (int_vec == OF_VECTOR || int_vec == BP_VECTOR))) |
| 2403 | skip_emulated_instruction(&svm->vcpu); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 2404 | |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 2405 | if (kvm_task_switch(&svm->vcpu, tss_selector, reason, |
| 2406 | has_error_code, error_code) == EMULATE_FAIL) { |
| 2407 | svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 2408 | svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 2409 | svm->vcpu.run->internal.ndata = 0; |
| 2410 | return 0; |
| 2411 | } |
| 2412 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2413 | } |
| 2414 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2415 | static int cpuid_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2416 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2417 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 2; |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2418 | kvm_emulate_cpuid(&svm->vcpu); |
Avi Kivity | 06465c5 | 2007-02-28 20:46:53 +0200 | [diff] [blame] | 2419 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2420 | } |
| 2421 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2422 | static int iret_interception(struct vcpu_svm *svm) |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2423 | { |
| 2424 | ++svm->vcpu.stat.nmi_window_exits; |
Joerg Roedel | 061e2fd | 2010-05-05 16:04:43 +0200 | [diff] [blame] | 2425 | svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_IRET); |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 2426 | svm->vcpu.arch.hflags |= HF_IRET_MASK; |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2427 | return 1; |
| 2428 | } |
| 2429 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2430 | static int invlpg_interception(struct vcpu_svm *svm) |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2431 | { |
Gleb Natapov | 6d77dbf | 2010-05-10 11:16:56 +0300 | [diff] [blame] | 2432 | return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE; |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2433 | } |
| 2434 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2435 | static int emulate_on_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2436 | { |
Gleb Natapov | 6d77dbf | 2010-05-10 11:16:56 +0300 | [diff] [blame] | 2437 | return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2438 | } |
| 2439 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2440 | static int cr8_write_interception(struct vcpu_svm *svm) |
Joerg Roedel | 1d07543 | 2007-12-06 21:02:25 +0100 | [diff] [blame] | 2441 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2442 | struct kvm_run *kvm_run = svm->vcpu.run; |
| 2443 | |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 2444 | u8 cr8_prev = kvm_get_cr8(&svm->vcpu); |
| 2445 | /* instruction emulation calls kvm_set_cr8() */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2446 | emulate_instruction(&svm->vcpu, 0, 0, 0); |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2447 | if (irqchip_in_kernel(svm->vcpu.kvm)) { |
| 2448 | svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK; |
Joerg Roedel | 1d07543 | 2007-12-06 21:02:25 +0100 | [diff] [blame] | 2449 | return 1; |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2450 | } |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 2451 | if (cr8_prev <= kvm_get_cr8(&svm->vcpu)) |
| 2452 | return 1; |
Joerg Roedel | 1d07543 | 2007-12-06 21:02:25 +0100 | [diff] [blame] | 2453 | kvm_run->exit_reason = KVM_EXIT_SET_TPR; |
| 2454 | return 0; |
| 2455 | } |
| 2456 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2457 | static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data) |
| 2458 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2459 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2460 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2461 | switch (ecx) { |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 2462 | case MSR_IA32_TSC: { |
Joerg Roedel | 20824f3 | 2009-09-16 15:24:18 +0200 | [diff] [blame] | 2463 | u64 tsc_offset; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2464 | |
Joerg Roedel | 20824f3 | 2009-09-16 15:24:18 +0200 | [diff] [blame] | 2465 | if (is_nested(svm)) |
| 2466 | tsc_offset = svm->nested.hsave->control.tsc_offset; |
| 2467 | else |
| 2468 | tsc_offset = svm->vmcb->control.tsc_offset; |
| 2469 | |
| 2470 | *data = tsc_offset + native_read_tsc(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2471 | break; |
| 2472 | } |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 2473 | case MSR_STAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2474 | *data = svm->vmcb->save.star; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2475 | break; |
Avi Kivity | 0e859ca | 2006-12-22 01:05:08 -0800 | [diff] [blame] | 2476 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2477 | case MSR_LSTAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2478 | *data = svm->vmcb->save.lstar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2479 | break; |
| 2480 | case MSR_CSTAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2481 | *data = svm->vmcb->save.cstar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2482 | break; |
| 2483 | case MSR_KERNEL_GS_BASE: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2484 | *data = svm->vmcb->save.kernel_gs_base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2485 | break; |
| 2486 | case MSR_SYSCALL_MASK: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2487 | *data = svm->vmcb->save.sfmask; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2488 | break; |
| 2489 | #endif |
| 2490 | case MSR_IA32_SYSENTER_CS: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2491 | *data = svm->vmcb->save.sysenter_cs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2492 | break; |
| 2493 | case MSR_IA32_SYSENTER_EIP: |
Andre Przywara | 017cb99 | 2009-05-28 11:56:31 +0200 | [diff] [blame] | 2494 | *data = svm->sysenter_eip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2495 | break; |
| 2496 | case MSR_IA32_SYSENTER_ESP: |
Andre Przywara | 017cb99 | 2009-05-28 11:56:31 +0200 | [diff] [blame] | 2497 | *data = svm->sysenter_esp; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2498 | break; |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 2499 | /* |
| 2500 | * Nobody will change the following 5 values in the VMCB so we can |
| 2501 | * safely return them on rdmsr. They will always be 0 until LBRV is |
| 2502 | * implemented. |
| 2503 | */ |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 2504 | case MSR_IA32_DEBUGCTLMSR: |
| 2505 | *data = svm->vmcb->save.dbgctl; |
| 2506 | break; |
| 2507 | case MSR_IA32_LASTBRANCHFROMIP: |
| 2508 | *data = svm->vmcb->save.br_from; |
| 2509 | break; |
| 2510 | case MSR_IA32_LASTBRANCHTOIP: |
| 2511 | *data = svm->vmcb->save.br_to; |
| 2512 | break; |
| 2513 | case MSR_IA32_LASTINTFROMIP: |
| 2514 | *data = svm->vmcb->save.last_excp_from; |
| 2515 | break; |
| 2516 | case MSR_IA32_LASTINTTOIP: |
| 2517 | *data = svm->vmcb->save.last_excp_to; |
| 2518 | break; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 2519 | case MSR_VM_HSAVE_PA: |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 2520 | *data = svm->nested.hsave_msr; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 2521 | break; |
Joerg Roedel | eb6f302 | 2008-11-25 20:17:09 +0100 | [diff] [blame] | 2522 | case MSR_VM_CR: |
Joerg Roedel | 4a81018 | 2010-02-24 18:59:15 +0100 | [diff] [blame] | 2523 | *data = svm->nested.vm_cr_msr; |
Joerg Roedel | eb6f302 | 2008-11-25 20:17:09 +0100 | [diff] [blame] | 2524 | break; |
Alexander Graf | c8a73f1 | 2009-01-05 16:02:47 +0100 | [diff] [blame] | 2525 | case MSR_IA32_UCODE_REV: |
| 2526 | *data = 0x01000065; |
| 2527 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2528 | default: |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2529 | return kvm_get_msr_common(vcpu, ecx, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2530 | } |
| 2531 | return 0; |
| 2532 | } |
| 2533 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2534 | static int rdmsr_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2535 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2536 | u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2537 | u64 data; |
| 2538 | |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 2539 | if (svm_get_msr(&svm->vcpu, ecx, &data)) { |
| 2540 | trace_kvm_msr_read_ex(ecx); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 2541 | kvm_inject_gp(&svm->vcpu, 0); |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 2542 | } else { |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 2543 | trace_kvm_msr_read(ecx, data); |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 2544 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2545 | svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2546 | svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2547 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 2; |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2548 | skip_emulated_instruction(&svm->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2549 | } |
| 2550 | return 1; |
| 2551 | } |
| 2552 | |
Joerg Roedel | 4a81018 | 2010-02-24 18:59:15 +0100 | [diff] [blame] | 2553 | static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data) |
| 2554 | { |
| 2555 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2556 | int svm_dis, chg_mask; |
| 2557 | |
| 2558 | if (data & ~SVM_VM_CR_VALID_MASK) |
| 2559 | return 1; |
| 2560 | |
| 2561 | chg_mask = SVM_VM_CR_VALID_MASK; |
| 2562 | |
| 2563 | if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK) |
| 2564 | chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK); |
| 2565 | |
| 2566 | svm->nested.vm_cr_msr &= ~chg_mask; |
| 2567 | svm->nested.vm_cr_msr |= (data & chg_mask); |
| 2568 | |
| 2569 | svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK; |
| 2570 | |
| 2571 | /* check for svm_disable while efer.svme is set */ |
| 2572 | if (svm_dis && (vcpu->arch.efer & EFER_SVME)) |
| 2573 | return 1; |
| 2574 | |
| 2575 | return 0; |
| 2576 | } |
| 2577 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2578 | static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data) |
| 2579 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2580 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2581 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2582 | switch (ecx) { |
Zachary Amsden | f4e1b3c | 2010-08-19 22:07:16 -1000 | [diff] [blame] | 2583 | case MSR_IA32_TSC: |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame^] | 2584 | kvm_write_tsc(vcpu, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2585 | break; |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 2586 | case MSR_STAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2587 | svm->vmcb->save.star = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2588 | break; |
Robert P. J. Day | 49b14f2 | 2007-01-29 13:19:50 -0800 | [diff] [blame] | 2589 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2590 | case MSR_LSTAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2591 | svm->vmcb->save.lstar = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2592 | break; |
| 2593 | case MSR_CSTAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2594 | svm->vmcb->save.cstar = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2595 | break; |
| 2596 | case MSR_KERNEL_GS_BASE: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2597 | svm->vmcb->save.kernel_gs_base = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2598 | break; |
| 2599 | case MSR_SYSCALL_MASK: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2600 | svm->vmcb->save.sfmask = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2601 | break; |
| 2602 | #endif |
| 2603 | case MSR_IA32_SYSENTER_CS: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2604 | svm->vmcb->save.sysenter_cs = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2605 | break; |
| 2606 | case MSR_IA32_SYSENTER_EIP: |
Andre Przywara | 017cb99 | 2009-05-28 11:56:31 +0200 | [diff] [blame] | 2607 | svm->sysenter_eip = data; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2608 | svm->vmcb->save.sysenter_eip = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2609 | break; |
| 2610 | case MSR_IA32_SYSENTER_ESP: |
Andre Przywara | 017cb99 | 2009-05-28 11:56:31 +0200 | [diff] [blame] | 2611 | svm->sysenter_esp = data; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2612 | svm->vmcb->save.sysenter_esp = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2613 | break; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 2614 | case MSR_IA32_DEBUGCTLMSR: |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 2615 | if (!svm_has(SVM_FEATURE_LBRV)) { |
| 2616 | pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 2617 | __func__, data); |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 2618 | break; |
| 2619 | } |
| 2620 | if (data & DEBUGCTL_RESERVED_BITS) |
| 2621 | return 1; |
| 2622 | |
| 2623 | svm->vmcb->save.dbgctl = data; |
| 2624 | if (data & (1ULL<<0)) |
| 2625 | svm_enable_lbrv(svm); |
| 2626 | else |
| 2627 | svm_disable_lbrv(svm); |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 2628 | break; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 2629 | case MSR_VM_HSAVE_PA: |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 2630 | svm->nested.hsave_msr = data; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 2631 | break; |
Alexander Graf | 3c5d0a4 | 2009-06-15 15:21:23 +0200 | [diff] [blame] | 2632 | case MSR_VM_CR: |
Joerg Roedel | 4a81018 | 2010-02-24 18:59:15 +0100 | [diff] [blame] | 2633 | return svm_set_vm_cr(vcpu, data); |
Alexander Graf | 3c5d0a4 | 2009-06-15 15:21:23 +0200 | [diff] [blame] | 2634 | case MSR_VM_IGNNE: |
Alexander Graf | 3c5d0a4 | 2009-06-15 15:21:23 +0200 | [diff] [blame] | 2635 | pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data); |
| 2636 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2637 | default: |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2638 | return kvm_set_msr_common(vcpu, ecx, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2639 | } |
| 2640 | return 0; |
| 2641 | } |
| 2642 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2643 | static int wrmsr_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2644 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2645 | u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX]; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2646 | u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2647 | | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32); |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 2648 | |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 2649 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2650 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 2; |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 2651 | if (svm_set_msr(&svm->vcpu, ecx, data)) { |
| 2652 | trace_kvm_msr_write_ex(ecx, data); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 2653 | kvm_inject_gp(&svm->vcpu, 0); |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 2654 | } else { |
| 2655 | trace_kvm_msr_write(ecx, data); |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2656 | skip_emulated_instruction(&svm->vcpu); |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 2657 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2658 | return 1; |
| 2659 | } |
| 2660 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2661 | static int msr_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2662 | { |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2663 | if (svm->vmcb->control.exit_info_1) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2664 | return wrmsr_interception(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2665 | else |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2666 | return rdmsr_interception(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2667 | } |
| 2668 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2669 | static int interrupt_window_interception(struct vcpu_svm *svm) |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2670 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2671 | struct kvm_run *kvm_run = svm->vcpu.run; |
| 2672 | |
Alexander Graf | f0b8505 | 2008-11-25 20:17:01 +0100 | [diff] [blame] | 2673 | svm_clear_vintr(svm); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2674 | svm->vmcb->control.int_ctl &= ~V_IRQ_MASK; |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2675 | /* |
| 2676 | * If the user space waits to inject interrupts, exit as soon as |
| 2677 | * possible |
| 2678 | */ |
Gleb Natapov | 8061823 | 2009-04-21 17:44:56 +0300 | [diff] [blame] | 2679 | if (!irqchip_in_kernel(svm->vcpu.kvm) && |
| 2680 | kvm_run->request_interrupt_window && |
| 2681 | !kvm_cpu_has_interrupt(&svm->vcpu)) { |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2682 | ++svm->vcpu.stat.irq_window_exits; |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2683 | kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; |
| 2684 | return 0; |
| 2685 | } |
| 2686 | |
| 2687 | return 1; |
| 2688 | } |
| 2689 | |
Mark Langsdorf | 565d099 | 2009-10-06 14:25:02 -0500 | [diff] [blame] | 2690 | static int pause_interception(struct vcpu_svm *svm) |
| 2691 | { |
| 2692 | kvm_vcpu_on_spin(&(svm->vcpu)); |
| 2693 | return 1; |
| 2694 | } |
| 2695 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2696 | static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = { |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 2697 | [SVM_EXIT_READ_CR0] = emulate_on_interception, |
| 2698 | [SVM_EXIT_READ_CR3] = emulate_on_interception, |
| 2699 | [SVM_EXIT_READ_CR4] = emulate_on_interception, |
| 2700 | [SVM_EXIT_READ_CR8] = emulate_on_interception, |
Avi Kivity | d225157 | 2010-01-06 10:55:27 +0200 | [diff] [blame] | 2701 | [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 2702 | [SVM_EXIT_WRITE_CR0] = emulate_on_interception, |
| 2703 | [SVM_EXIT_WRITE_CR3] = emulate_on_interception, |
| 2704 | [SVM_EXIT_WRITE_CR4] = emulate_on_interception, |
| 2705 | [SVM_EXIT_WRITE_CR8] = cr8_write_interception, |
| 2706 | [SVM_EXIT_READ_DR0] = emulate_on_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2707 | [SVM_EXIT_READ_DR1] = emulate_on_interception, |
| 2708 | [SVM_EXIT_READ_DR2] = emulate_on_interception, |
| 2709 | [SVM_EXIT_READ_DR3] = emulate_on_interception, |
Jan Kiszka | 727f5a2 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 2710 | [SVM_EXIT_READ_DR4] = emulate_on_interception, |
| 2711 | [SVM_EXIT_READ_DR5] = emulate_on_interception, |
| 2712 | [SVM_EXIT_READ_DR6] = emulate_on_interception, |
| 2713 | [SVM_EXIT_READ_DR7] = emulate_on_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2714 | [SVM_EXIT_WRITE_DR0] = emulate_on_interception, |
| 2715 | [SVM_EXIT_WRITE_DR1] = emulate_on_interception, |
| 2716 | [SVM_EXIT_WRITE_DR2] = emulate_on_interception, |
| 2717 | [SVM_EXIT_WRITE_DR3] = emulate_on_interception, |
Jan Kiszka | 727f5a2 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 2718 | [SVM_EXIT_WRITE_DR4] = emulate_on_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2719 | [SVM_EXIT_WRITE_DR5] = emulate_on_interception, |
Jan Kiszka | 727f5a2 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 2720 | [SVM_EXIT_WRITE_DR6] = emulate_on_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2721 | [SVM_EXIT_WRITE_DR7] = emulate_on_interception, |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2722 | [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception, |
| 2723 | [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception, |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 2724 | [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception, |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 2725 | [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception, |
| 2726 | [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception, |
| 2727 | [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception, |
| 2728 | [SVM_EXIT_INTR] = intr_interception, |
Joerg Roedel | c47f098 | 2008-04-30 17:56:00 +0200 | [diff] [blame] | 2729 | [SVM_EXIT_NMI] = nmi_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2730 | [SVM_EXIT_SMI] = nop_on_interception, |
| 2731 | [SVM_EXIT_INIT] = nop_on_interception, |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2732 | [SVM_EXIT_VINTR] = interrupt_window_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2733 | [SVM_EXIT_CPUID] = cpuid_interception, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2734 | [SVM_EXIT_IRET] = iret_interception, |
Avi Kivity | cf5a94d | 2007-10-28 16:11:58 +0200 | [diff] [blame] | 2735 | [SVM_EXIT_INVD] = emulate_on_interception, |
Mark Langsdorf | 565d099 | 2009-10-06 14:25:02 -0500 | [diff] [blame] | 2736 | [SVM_EXIT_PAUSE] = pause_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2737 | [SVM_EXIT_HLT] = halt_interception, |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2738 | [SVM_EXIT_INVLPG] = invlpg_interception, |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 2739 | [SVM_EXIT_INVLPGA] = invlpga_interception, |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 2740 | [SVM_EXIT_IOIO] = io_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2741 | [SVM_EXIT_MSR] = msr_interception, |
| 2742 | [SVM_EXIT_TASK_SWITCH] = task_switch_interception, |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 2743 | [SVM_EXIT_SHUTDOWN] = shutdown_interception, |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2744 | [SVM_EXIT_VMRUN] = vmrun_interception, |
Avi Kivity | 02e235b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2745 | [SVM_EXIT_VMMCALL] = vmmcall_interception, |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 2746 | [SVM_EXIT_VMLOAD] = vmload_interception, |
| 2747 | [SVM_EXIT_VMSAVE] = vmsave_interception, |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 2748 | [SVM_EXIT_STGI] = stgi_interception, |
| 2749 | [SVM_EXIT_CLGI] = clgi_interception, |
Joerg Roedel | 532a46b | 2009-10-09 16:08:32 +0200 | [diff] [blame] | 2750 | [SVM_EXIT_SKINIT] = skinit_interception, |
Avi Kivity | cf5a94d | 2007-10-28 16:11:58 +0200 | [diff] [blame] | 2751 | [SVM_EXIT_WBINVD] = emulate_on_interception, |
Joerg Roedel | 916ce23 | 2007-03-21 19:47:00 +0100 | [diff] [blame] | 2752 | [SVM_EXIT_MONITOR] = invalid_op_interception, |
| 2753 | [SVM_EXIT_MWAIT] = invalid_op_interception, |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 2754 | [SVM_EXIT_NPF] = pf_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2755 | }; |
| 2756 | |
Joerg Roedel | 3f10c84 | 2010-05-05 16:04:42 +0200 | [diff] [blame] | 2757 | void dump_vmcb(struct kvm_vcpu *vcpu) |
| 2758 | { |
| 2759 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2760 | struct vmcb_control_area *control = &svm->vmcb->control; |
| 2761 | struct vmcb_save_area *save = &svm->vmcb->save; |
| 2762 | |
| 2763 | pr_err("VMCB Control Area:\n"); |
| 2764 | pr_err("cr_read: %04x\n", control->intercept_cr_read); |
| 2765 | pr_err("cr_write: %04x\n", control->intercept_cr_write); |
| 2766 | pr_err("dr_read: %04x\n", control->intercept_dr_read); |
| 2767 | pr_err("dr_write: %04x\n", control->intercept_dr_write); |
| 2768 | pr_err("exceptions: %08x\n", control->intercept_exceptions); |
| 2769 | pr_err("intercepts: %016llx\n", control->intercept); |
| 2770 | pr_err("pause filter count: %d\n", control->pause_filter_count); |
| 2771 | pr_err("iopm_base_pa: %016llx\n", control->iopm_base_pa); |
| 2772 | pr_err("msrpm_base_pa: %016llx\n", control->msrpm_base_pa); |
| 2773 | pr_err("tsc_offset: %016llx\n", control->tsc_offset); |
| 2774 | pr_err("asid: %d\n", control->asid); |
| 2775 | pr_err("tlb_ctl: %d\n", control->tlb_ctl); |
| 2776 | pr_err("int_ctl: %08x\n", control->int_ctl); |
| 2777 | pr_err("int_vector: %08x\n", control->int_vector); |
| 2778 | pr_err("int_state: %08x\n", control->int_state); |
| 2779 | pr_err("exit_code: %08x\n", control->exit_code); |
| 2780 | pr_err("exit_info1: %016llx\n", control->exit_info_1); |
| 2781 | pr_err("exit_info2: %016llx\n", control->exit_info_2); |
| 2782 | pr_err("exit_int_info: %08x\n", control->exit_int_info); |
| 2783 | pr_err("exit_int_info_err: %08x\n", control->exit_int_info_err); |
| 2784 | pr_err("nested_ctl: %lld\n", control->nested_ctl); |
| 2785 | pr_err("nested_cr3: %016llx\n", control->nested_cr3); |
| 2786 | pr_err("event_inj: %08x\n", control->event_inj); |
| 2787 | pr_err("event_inj_err: %08x\n", control->event_inj_err); |
| 2788 | pr_err("lbr_ctl: %lld\n", control->lbr_ctl); |
| 2789 | pr_err("next_rip: %016llx\n", control->next_rip); |
| 2790 | pr_err("VMCB State Save Area:\n"); |
| 2791 | pr_err("es: s: %04x a: %04x l: %08x b: %016llx\n", |
| 2792 | save->es.selector, save->es.attrib, |
| 2793 | save->es.limit, save->es.base); |
| 2794 | pr_err("cs: s: %04x a: %04x l: %08x b: %016llx\n", |
| 2795 | save->cs.selector, save->cs.attrib, |
| 2796 | save->cs.limit, save->cs.base); |
| 2797 | pr_err("ss: s: %04x a: %04x l: %08x b: %016llx\n", |
| 2798 | save->ss.selector, save->ss.attrib, |
| 2799 | save->ss.limit, save->ss.base); |
| 2800 | pr_err("ds: s: %04x a: %04x l: %08x b: %016llx\n", |
| 2801 | save->ds.selector, save->ds.attrib, |
| 2802 | save->ds.limit, save->ds.base); |
| 2803 | pr_err("fs: s: %04x a: %04x l: %08x b: %016llx\n", |
| 2804 | save->fs.selector, save->fs.attrib, |
| 2805 | save->fs.limit, save->fs.base); |
| 2806 | pr_err("gs: s: %04x a: %04x l: %08x b: %016llx\n", |
| 2807 | save->gs.selector, save->gs.attrib, |
| 2808 | save->gs.limit, save->gs.base); |
| 2809 | pr_err("gdtr: s: %04x a: %04x l: %08x b: %016llx\n", |
| 2810 | save->gdtr.selector, save->gdtr.attrib, |
| 2811 | save->gdtr.limit, save->gdtr.base); |
| 2812 | pr_err("ldtr: s: %04x a: %04x l: %08x b: %016llx\n", |
| 2813 | save->ldtr.selector, save->ldtr.attrib, |
| 2814 | save->ldtr.limit, save->ldtr.base); |
| 2815 | pr_err("idtr: s: %04x a: %04x l: %08x b: %016llx\n", |
| 2816 | save->idtr.selector, save->idtr.attrib, |
| 2817 | save->idtr.limit, save->idtr.base); |
| 2818 | pr_err("tr: s: %04x a: %04x l: %08x b: %016llx\n", |
| 2819 | save->tr.selector, save->tr.attrib, |
| 2820 | save->tr.limit, save->tr.base); |
| 2821 | pr_err("cpl: %d efer: %016llx\n", |
| 2822 | save->cpl, save->efer); |
| 2823 | pr_err("cr0: %016llx cr2: %016llx\n", |
| 2824 | save->cr0, save->cr2); |
| 2825 | pr_err("cr3: %016llx cr4: %016llx\n", |
| 2826 | save->cr3, save->cr4); |
| 2827 | pr_err("dr6: %016llx dr7: %016llx\n", |
| 2828 | save->dr6, save->dr7); |
| 2829 | pr_err("rip: %016llx rflags: %016llx\n", |
| 2830 | save->rip, save->rflags); |
| 2831 | pr_err("rsp: %016llx rax: %016llx\n", |
| 2832 | save->rsp, save->rax); |
| 2833 | pr_err("star: %016llx lstar: %016llx\n", |
| 2834 | save->star, save->lstar); |
| 2835 | pr_err("cstar: %016llx sfmask: %016llx\n", |
| 2836 | save->cstar, save->sfmask); |
| 2837 | pr_err("kernel_gs_base: %016llx sysenter_cs: %016llx\n", |
| 2838 | save->kernel_gs_base, save->sysenter_cs); |
| 2839 | pr_err("sysenter_esp: %016llx sysenter_eip: %016llx\n", |
| 2840 | save->sysenter_esp, save->sysenter_eip); |
| 2841 | pr_err("gpat: %016llx dbgctl: %016llx\n", |
| 2842 | save->g_pat, save->dbgctl); |
| 2843 | pr_err("br_from: %016llx br_to: %016llx\n", |
| 2844 | save->br_from, save->br_to); |
| 2845 | pr_err("excp_from: %016llx excp_to: %016llx\n", |
| 2846 | save->last_excp_from, save->last_excp_to); |
| 2847 | |
| 2848 | } |
| 2849 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2850 | static int handle_exit(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2851 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2852 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2853 | struct kvm_run *kvm_run = vcpu->run; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2854 | u32 exit_code = svm->vmcb->control.exit_code; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2855 | |
Avi Kivity | 5bfd8b5 | 2010-03-11 10:50:44 +0200 | [diff] [blame] | 2856 | trace_kvm_exit(exit_code, vcpu); |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 2857 | |
Joerg Roedel | 2be4fc7 | 2010-04-22 12:33:09 +0200 | [diff] [blame] | 2858 | if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR0_MASK)) |
| 2859 | vcpu->arch.cr0 = svm->vmcb->save.cr0; |
| 2860 | if (npt_enabled) |
| 2861 | vcpu->arch.cr3 = svm->vmcb->save.cr3; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2862 | |
Joerg Roedel | cd3ff65 | 2009-10-09 16:08:26 +0200 | [diff] [blame] | 2863 | if (unlikely(svm->nested.exit_required)) { |
| 2864 | nested_svm_vmexit(svm); |
| 2865 | svm->nested.exit_required = false; |
| 2866 | |
| 2867 | return 1; |
| 2868 | } |
| 2869 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2870 | if (is_nested(svm)) { |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 2871 | int vmexit; |
| 2872 | |
Joerg Roedel | d8cabdd | 2009-10-09 16:08:28 +0200 | [diff] [blame] | 2873 | trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code, |
| 2874 | svm->vmcb->control.exit_info_1, |
| 2875 | svm->vmcb->control.exit_info_2, |
| 2876 | svm->vmcb->control.exit_int_info, |
| 2877 | svm->vmcb->control.exit_int_info_err); |
| 2878 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 2879 | vmexit = nested_svm_exit_special(svm); |
| 2880 | |
| 2881 | if (vmexit == NESTED_EXIT_CONTINUE) |
| 2882 | vmexit = nested_svm_exit_handled(svm); |
| 2883 | |
| 2884 | if (vmexit == NESTED_EXIT_DONE) |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2885 | return 1; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2886 | } |
| 2887 | |
Joerg Roedel | a5c3832 | 2009-08-07 11:49:32 +0200 | [diff] [blame] | 2888 | svm_complete_interrupts(svm); |
| 2889 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2890 | if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) { |
| 2891 | kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 2892 | kvm_run->fail_entry.hardware_entry_failure_reason |
| 2893 | = svm->vmcb->control.exit_code; |
Joerg Roedel | 3f10c84 | 2010-05-05 16:04:42 +0200 | [diff] [blame] | 2894 | pr_err("KVM: FAILED VMRUN WITH VMCB:\n"); |
| 2895 | dump_vmcb(vcpu); |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2896 | return 0; |
| 2897 | } |
| 2898 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2899 | if (is_external_interrupt(svm->vmcb->control.exit_int_info) && |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 2900 | exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR && |
Gleb Natapov | fe8e7f8 | 2009-04-23 17:03:48 +0300 | [diff] [blame] | 2901 | exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2902 | printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x " |
| 2903 | "exit_code 0x%x\n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 2904 | __func__, svm->vmcb->control.exit_int_info, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2905 | exit_code); |
| 2906 | |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 2907 | if (exit_code >= ARRAY_SIZE(svm_exit_handlers) |
Joe Perches | 56919c5 | 2007-11-12 20:06:51 -0800 | [diff] [blame] | 2908 | || !svm_exit_handlers[exit_code]) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2909 | kvm_run->exit_reason = KVM_EXIT_UNKNOWN; |
Avi Kivity | 364b625 | 2007-04-16 14:28:40 +0300 | [diff] [blame] | 2910 | kvm_run->hw.hardware_exit_reason = exit_code; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2911 | return 0; |
| 2912 | } |
| 2913 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2914 | return svm_exit_handlers[exit_code](svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2915 | } |
| 2916 | |
| 2917 | static void reload_tss(struct kvm_vcpu *vcpu) |
| 2918 | { |
| 2919 | int cpu = raw_smp_processor_id(); |
| 2920 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 2921 | struct svm_cpu_data *sd = per_cpu(svm_data, cpu); |
| 2922 | sd->tss_desc->type = 9; /* available 32/64-bit TSS */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2923 | load_TR_desc(); |
| 2924 | } |
| 2925 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2926 | static void pre_svm_run(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2927 | { |
| 2928 | int cpu = raw_smp_processor_id(); |
| 2929 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 2930 | struct svm_cpu_data *sd = per_cpu(svm_data, cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2931 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2932 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING; |
Marcelo Tosatti | 4b656b1 | 2009-07-21 12:47:45 -0300 | [diff] [blame] | 2933 | /* FIXME: handle wraparound of asid_generation */ |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 2934 | if (svm->asid_generation != sd->asid_generation) |
| 2935 | new_asid(svm, sd); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2936 | } |
| 2937 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2938 | static void svm_inject_nmi(struct kvm_vcpu *vcpu) |
| 2939 | { |
| 2940 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2941 | |
| 2942 | svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI; |
| 2943 | vcpu->arch.hflags |= HF_NMI_MASK; |
Joerg Roedel | 061e2fd | 2010-05-05 16:04:43 +0200 | [diff] [blame] | 2944 | svm->vmcb->control.intercept |= (1ULL << INTERCEPT_IRET); |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2945 | ++vcpu->stat.nmi_injections; |
| 2946 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2947 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2948 | static inline void svm_inject_irq(struct vcpu_svm *svm, int irq) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2949 | { |
| 2950 | struct vmcb_control_area *control; |
| 2951 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2952 | control = &svm->vmcb->control; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2953 | control->int_vector = irq; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2954 | control->int_ctl &= ~V_INTR_PRIO_MASK; |
| 2955 | control->int_ctl |= V_IRQ_MASK | |
| 2956 | ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT); |
| 2957 | } |
| 2958 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 2959 | static void svm_set_irq(struct kvm_vcpu *vcpu) |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 2960 | { |
| 2961 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2962 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 2963 | BUG_ON(!(gif_set(svm))); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2964 | |
Gleb Natapov | 9fb2d2b | 2010-05-23 14:28:26 +0300 | [diff] [blame] | 2965 | trace_kvm_inj_virq(vcpu->arch.interrupt.nr); |
| 2966 | ++vcpu->stat.irq_injections; |
| 2967 | |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 2968 | svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr | |
| 2969 | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR; |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 2970 | } |
| 2971 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2972 | static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr) |
| 2973 | { |
| 2974 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2975 | |
Joerg Roedel | 88ab24a | 2010-02-19 16:23:06 +0100 | [diff] [blame] | 2976 | if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK)) |
| 2977 | return; |
| 2978 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2979 | if (irr == -1) |
| 2980 | return; |
| 2981 | |
| 2982 | if (tpr >= irr) |
| 2983 | svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK; |
| 2984 | } |
| 2985 | |
| 2986 | static int svm_nmi_allowed(struct kvm_vcpu *vcpu) |
Joerg Roedel | aaacfc9 | 2008-04-16 16:51:18 +0200 | [diff] [blame] | 2987 | { |
| 2988 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2989 | struct vmcb *vmcb = svm->vmcb; |
Joerg Roedel | 924584c | 2010-04-22 12:33:07 +0200 | [diff] [blame] | 2990 | int ret; |
| 2991 | ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) && |
| 2992 | !(svm->vcpu.arch.hflags & HF_NMI_MASK); |
| 2993 | ret = ret && gif_set(svm) && nested_svm_nmi(svm); |
| 2994 | |
| 2995 | return ret; |
Joerg Roedel | aaacfc9 | 2008-04-16 16:51:18 +0200 | [diff] [blame] | 2996 | } |
| 2997 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2998 | static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu) |
| 2999 | { |
| 3000 | struct vcpu_svm *svm = to_svm(vcpu); |
| 3001 | |
| 3002 | return !!(svm->vcpu.arch.hflags & HF_NMI_MASK); |
| 3003 | } |
| 3004 | |
| 3005 | static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) |
| 3006 | { |
| 3007 | struct vcpu_svm *svm = to_svm(vcpu); |
| 3008 | |
| 3009 | if (masked) { |
| 3010 | svm->vcpu.arch.hflags |= HF_NMI_MASK; |
Joerg Roedel | 061e2fd | 2010-05-05 16:04:43 +0200 | [diff] [blame] | 3011 | svm->vmcb->control.intercept |= (1ULL << INTERCEPT_IRET); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3012 | } else { |
| 3013 | svm->vcpu.arch.hflags &= ~HF_NMI_MASK; |
Joerg Roedel | 061e2fd | 2010-05-05 16:04:43 +0200 | [diff] [blame] | 3014 | svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_IRET); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3015 | } |
| 3016 | } |
| 3017 | |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 3018 | static int svm_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 3019 | { |
| 3020 | struct vcpu_svm *svm = to_svm(vcpu); |
| 3021 | struct vmcb *vmcb = svm->vmcb; |
Joerg Roedel | 7fcdb51 | 2009-09-16 15:24:15 +0200 | [diff] [blame] | 3022 | int ret; |
| 3023 | |
| 3024 | if (!gif_set(svm) || |
| 3025 | (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)) |
| 3026 | return 0; |
| 3027 | |
| 3028 | ret = !!(vmcb->save.rflags & X86_EFLAGS_IF); |
| 3029 | |
| 3030 | if (is_nested(svm)) |
| 3031 | return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK); |
| 3032 | |
| 3033 | return ret; |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 3034 | } |
| 3035 | |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 3036 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
| 3037 | { |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 3038 | struct vcpu_svm *svm = to_svm(vcpu); |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 3039 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 3040 | /* |
| 3041 | * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes |
| 3042 | * 1, because that's a separate STGI/VMRUN intercept. The next time we |
| 3043 | * get that intercept, this function will be called again though and |
| 3044 | * we'll get the vintr intercept. |
| 3045 | */ |
Joerg Roedel | 8fe5465 | 2010-02-19 16:23:01 +0100 | [diff] [blame] | 3046 | if (gif_set(svm) && nested_svm_intr(svm)) { |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 3047 | svm_set_vintr(svm); |
| 3048 | svm_inject_irq(svm, 0x0); |
| 3049 | } |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 3050 | } |
| 3051 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 3052 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3053 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 3054 | struct vcpu_svm *svm = to_svm(vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 3055 | |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 3056 | if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK)) |
| 3057 | == HF_NMI_MASK) |
| 3058 | return; /* IRET will cause a vm exit */ |
| 3059 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 3060 | /* |
| 3061 | * Something prevents NMI from been injected. Single step over possible |
| 3062 | * problem (IRET or exception injection or interrupt shadow) |
| 3063 | */ |
Jan Kiszka | 6be7d30 | 2009-10-18 13:24:54 +0200 | [diff] [blame] | 3064 | svm->nmi_singlestep = true; |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 3065 | svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF); |
| 3066 | update_db_intercept(vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 3067 | } |
| 3068 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 3069 | static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 3070 | { |
| 3071 | return 0; |
| 3072 | } |
| 3073 | |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 3074 | static void svm_flush_tlb(struct kvm_vcpu *vcpu) |
| 3075 | { |
| 3076 | force_new_asid(vcpu); |
| 3077 | } |
| 3078 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 3079 | static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu) |
| 3080 | { |
| 3081 | } |
| 3082 | |
Joerg Roedel | d7bf822 | 2008-04-16 16:51:17 +0200 | [diff] [blame] | 3083 | static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu) |
| 3084 | { |
| 3085 | struct vcpu_svm *svm = to_svm(vcpu); |
| 3086 | |
Joerg Roedel | 88ab24a | 2010-02-19 16:23:06 +0100 | [diff] [blame] | 3087 | if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK)) |
| 3088 | return; |
| 3089 | |
Joerg Roedel | d7bf822 | 2008-04-16 16:51:17 +0200 | [diff] [blame] | 3090 | if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) { |
| 3091 | int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK; |
Gleb Natapov | 615d519 | 2009-04-21 17:45:05 +0300 | [diff] [blame] | 3092 | kvm_set_cr8(vcpu, cr8); |
Joerg Roedel | d7bf822 | 2008-04-16 16:51:17 +0200 | [diff] [blame] | 3093 | } |
| 3094 | } |
| 3095 | |
Joerg Roedel | 649d686 | 2008-04-16 16:51:15 +0200 | [diff] [blame] | 3096 | static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu) |
| 3097 | { |
| 3098 | struct vcpu_svm *svm = to_svm(vcpu); |
| 3099 | u64 cr8; |
| 3100 | |
Joerg Roedel | 88ab24a | 2010-02-19 16:23:06 +0100 | [diff] [blame] | 3101 | if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK)) |
| 3102 | return; |
| 3103 | |
Joerg Roedel | 649d686 | 2008-04-16 16:51:15 +0200 | [diff] [blame] | 3104 | cr8 = kvm_get_cr8(vcpu); |
| 3105 | svm->vmcb->control.int_ctl &= ~V_TPR_MASK; |
| 3106 | svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK; |
| 3107 | } |
| 3108 | |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 3109 | static void svm_complete_interrupts(struct vcpu_svm *svm) |
| 3110 | { |
| 3111 | u8 vector; |
| 3112 | int type; |
| 3113 | u32 exitintinfo = svm->vmcb->control.exit_int_info; |
Jan Kiszka | 66b7138 | 2010-02-23 17:47:56 +0100 | [diff] [blame] | 3114 | unsigned int3_injected = svm->int3_injected; |
| 3115 | |
| 3116 | svm->int3_injected = 0; |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 3117 | |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 3118 | if (svm->vcpu.arch.hflags & HF_IRET_MASK) |
| 3119 | svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK); |
| 3120 | |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 3121 | svm->vcpu.arch.nmi_injected = false; |
| 3122 | kvm_clear_exception_queue(&svm->vcpu); |
| 3123 | kvm_clear_interrupt_queue(&svm->vcpu); |
| 3124 | |
| 3125 | if (!(exitintinfo & SVM_EXITINTINFO_VALID)) |
| 3126 | return; |
| 3127 | |
| 3128 | vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK; |
| 3129 | type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK; |
| 3130 | |
| 3131 | switch (type) { |
| 3132 | case SVM_EXITINTINFO_TYPE_NMI: |
| 3133 | svm->vcpu.arch.nmi_injected = true; |
| 3134 | break; |
| 3135 | case SVM_EXITINTINFO_TYPE_EXEPT: |
Jan Kiszka | 66b7138 | 2010-02-23 17:47:56 +0100 | [diff] [blame] | 3136 | /* |
| 3137 | * In case of software exceptions, do not reinject the vector, |
| 3138 | * but re-execute the instruction instead. Rewind RIP first |
| 3139 | * if we emulated INT3 before. |
| 3140 | */ |
| 3141 | if (kvm_exception_is_soft(vector)) { |
| 3142 | if (vector == BP_VECTOR && int3_injected && |
| 3143 | kvm_is_linear_rip(&svm->vcpu, svm->int3_rip)) |
| 3144 | kvm_rip_write(&svm->vcpu, |
| 3145 | kvm_rip_read(&svm->vcpu) - |
| 3146 | int3_injected); |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 3147 | break; |
Jan Kiszka | 66b7138 | 2010-02-23 17:47:56 +0100 | [diff] [blame] | 3148 | } |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 3149 | if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) { |
| 3150 | u32 err = svm->vmcb->control.exit_int_info_err; |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 3151 | kvm_requeue_exception_e(&svm->vcpu, vector, err); |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 3152 | |
| 3153 | } else |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 3154 | kvm_requeue_exception(&svm->vcpu, vector); |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 3155 | break; |
| 3156 | case SVM_EXITINTINFO_TYPE_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 3157 | kvm_queue_interrupt(&svm->vcpu, vector, false); |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 3158 | break; |
| 3159 | default: |
| 3160 | break; |
| 3161 | } |
| 3162 | } |
| 3163 | |
Avi Kivity | 80e31d4 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 3164 | #ifdef CONFIG_X86_64 |
| 3165 | #define R "r" |
| 3166 | #else |
| 3167 | #define R "e" |
| 3168 | #endif |
| 3169 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3170 | static void svm_vcpu_run(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3171 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3172 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3173 | u16 fs_selector; |
| 3174 | u16 gs_selector; |
| 3175 | u16 ldt_selector; |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 3176 | |
Joerg Roedel | 2041a06 | 2010-04-22 12:33:08 +0200 | [diff] [blame] | 3177 | svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX]; |
| 3178 | svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP]; |
| 3179 | svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP]; |
| 3180 | |
Joerg Roedel | cd3ff65 | 2009-10-09 16:08:26 +0200 | [diff] [blame] | 3181 | /* |
| 3182 | * A vmexit emulation is required before the vcpu can be executed |
| 3183 | * again. |
| 3184 | */ |
| 3185 | if (unlikely(svm->nested.exit_required)) |
| 3186 | return; |
| 3187 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 3188 | pre_svm_run(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3189 | |
Joerg Roedel | 649d686 | 2008-04-16 16:51:15 +0200 | [diff] [blame] | 3190 | sync_lapic_to_cr8(vcpu); |
| 3191 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3192 | save_host_msrs(vcpu); |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 3193 | savesegment(fs, fs_selector); |
| 3194 | savesegment(gs, gs_selector); |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 3195 | ldt_selector = kvm_read_ldt(); |
Joerg Roedel | cda0ffd | 2009-08-07 11:49:45 +0200 | [diff] [blame] | 3196 | svm->vmcb->save.cr2 = vcpu->arch.cr2; |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 3197 | /* required for live migration with NPT */ |
| 3198 | if (npt_enabled) |
| 3199 | svm->vmcb->save.cr3 = vcpu->arch.cr3; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3200 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 3201 | clgi(); |
| 3202 | |
| 3203 | local_irq_enable(); |
Avi Kivity | 36241b8 | 2006-12-22 01:05:20 -0800 | [diff] [blame] | 3204 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3205 | asm volatile ( |
Avi Kivity | 80e31d4 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 3206 | "push %%"R"bp; \n\t" |
| 3207 | "mov %c[rbx](%[svm]), %%"R"bx \n\t" |
| 3208 | "mov %c[rcx](%[svm]), %%"R"cx \n\t" |
| 3209 | "mov %c[rdx](%[svm]), %%"R"dx \n\t" |
| 3210 | "mov %c[rsi](%[svm]), %%"R"si \n\t" |
| 3211 | "mov %c[rdi](%[svm]), %%"R"di \n\t" |
| 3212 | "mov %c[rbp](%[svm]), %%"R"bp \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3213 | #ifdef CONFIG_X86_64 |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3214 | "mov %c[r8](%[svm]), %%r8 \n\t" |
| 3215 | "mov %c[r9](%[svm]), %%r9 \n\t" |
| 3216 | "mov %c[r10](%[svm]), %%r10 \n\t" |
| 3217 | "mov %c[r11](%[svm]), %%r11 \n\t" |
| 3218 | "mov %c[r12](%[svm]), %%r12 \n\t" |
| 3219 | "mov %c[r13](%[svm]), %%r13 \n\t" |
| 3220 | "mov %c[r14](%[svm]), %%r14 \n\t" |
| 3221 | "mov %c[r15](%[svm]), %%r15 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3222 | #endif |
| 3223 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3224 | /* Enter guest mode */ |
Avi Kivity | 80e31d4 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 3225 | "push %%"R"ax \n\t" |
| 3226 | "mov %c[vmcb](%[svm]), %%"R"ax \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 3227 | __ex(SVM_VMLOAD) "\n\t" |
| 3228 | __ex(SVM_VMRUN) "\n\t" |
| 3229 | __ex(SVM_VMSAVE) "\n\t" |
Avi Kivity | 80e31d4 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 3230 | "pop %%"R"ax \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3231 | |
| 3232 | /* Save guest registers, load host registers */ |
Avi Kivity | 80e31d4 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 3233 | "mov %%"R"bx, %c[rbx](%[svm]) \n\t" |
| 3234 | "mov %%"R"cx, %c[rcx](%[svm]) \n\t" |
| 3235 | "mov %%"R"dx, %c[rdx](%[svm]) \n\t" |
| 3236 | "mov %%"R"si, %c[rsi](%[svm]) \n\t" |
| 3237 | "mov %%"R"di, %c[rdi](%[svm]) \n\t" |
| 3238 | "mov %%"R"bp, %c[rbp](%[svm]) \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3239 | #ifdef CONFIG_X86_64 |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3240 | "mov %%r8, %c[r8](%[svm]) \n\t" |
| 3241 | "mov %%r9, %c[r9](%[svm]) \n\t" |
| 3242 | "mov %%r10, %c[r10](%[svm]) \n\t" |
| 3243 | "mov %%r11, %c[r11](%[svm]) \n\t" |
| 3244 | "mov %%r12, %c[r12](%[svm]) \n\t" |
| 3245 | "mov %%r13, %c[r13](%[svm]) \n\t" |
| 3246 | "mov %%r14, %c[r14](%[svm]) \n\t" |
| 3247 | "mov %%r15, %c[r15](%[svm]) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3248 | #endif |
Avi Kivity | 80e31d4 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 3249 | "pop %%"R"bp" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3250 | : |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3251 | : [svm]"a"(svm), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3252 | [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3253 | [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 3254 | [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 3255 | [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 3256 | [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 3257 | [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 3258 | [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP])) |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3259 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3260 | , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])), |
| 3261 | [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])), |
| 3262 | [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])), |
| 3263 | [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])), |
| 3264 | [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])), |
| 3265 | [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])), |
| 3266 | [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])), |
| 3267 | [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15])) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3268 | #endif |
Laurent Vivier | 54a08c0 | 2007-10-25 14:18:53 +0200 | [diff] [blame] | 3269 | : "cc", "memory" |
Avi Kivity | 80e31d4 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 3270 | , R"bx", R"cx", R"dx", R"si", R"di" |
Laurent Vivier | 54a08c0 | 2007-10-25 14:18:53 +0200 | [diff] [blame] | 3271 | #ifdef CONFIG_X86_64 |
Laurent Vivier | 54a08c0 | 2007-10-25 14:18:53 +0200 | [diff] [blame] | 3272 | , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15" |
| 3273 | #endif |
| 3274 | ); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3275 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3276 | vcpu->arch.cr2 = svm->vmcb->save.cr2; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3277 | vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax; |
| 3278 | vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp; |
| 3279 | vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3280 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3281 | load_host_msrs(vcpu); |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 3282 | loadsegment(fs, fs_selector); |
| 3283 | #ifdef CONFIG_X86_64 |
| 3284 | load_gs_index(gs_selector); |
| 3285 | wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs); |
| 3286 | #else |
| 3287 | loadsegment(gs, gs_selector); |
| 3288 | #endif |
| 3289 | kvm_load_ldt(ldt_selector); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3290 | |
| 3291 | reload_tss(vcpu); |
| 3292 | |
Avi Kivity | 56ba47d | 2007-11-07 17:14:18 +0200 | [diff] [blame] | 3293 | local_irq_disable(); |
| 3294 | |
| 3295 | stgi(); |
| 3296 | |
Joerg Roedel | d7bf822 | 2008-04-16 16:51:17 +0200 | [diff] [blame] | 3297 | sync_cr8_to_lapic(vcpu); |
| 3298 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3299 | svm->next_rip = 0; |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 3300 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 3301 | if (npt_enabled) { |
| 3302 | vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR); |
| 3303 | vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR); |
| 3304 | } |
Joerg Roedel | fe5913e | 2010-05-17 14:43:34 +0200 | [diff] [blame] | 3305 | |
| 3306 | /* |
| 3307 | * We need to handle MC intercepts here before the vcpu has a chance to |
| 3308 | * change the physical cpu |
| 3309 | */ |
| 3310 | if (unlikely(svm->vmcb->control.exit_code == |
| 3311 | SVM_EXIT_EXCP_BASE + MC_VECTOR)) |
| 3312 | svm_handle_mce(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3313 | } |
| 3314 | |
Avi Kivity | 80e31d4 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 3315 | #undef R |
| 3316 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3317 | static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root) |
| 3318 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3319 | struct vcpu_svm *svm = to_svm(vcpu); |
| 3320 | |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 3321 | if (npt_enabled) { |
| 3322 | svm->vmcb->control.nested_cr3 = root; |
| 3323 | force_new_asid(vcpu); |
| 3324 | return; |
| 3325 | } |
| 3326 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3327 | svm->vmcb->save.cr3 = root; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3328 | force_new_asid(vcpu); |
| 3329 | } |
| 3330 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3331 | static int is_disabled(void) |
| 3332 | { |
Joerg Roedel | 6031a61 | 2007-06-22 12:29:50 +0300 | [diff] [blame] | 3333 | u64 vm_cr; |
| 3334 | |
| 3335 | rdmsrl(MSR_VM_CR, vm_cr); |
| 3336 | if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE)) |
| 3337 | return 1; |
| 3338 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3339 | return 0; |
| 3340 | } |
| 3341 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 3342 | static void |
| 3343 | svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 3344 | { |
| 3345 | /* |
| 3346 | * Patch in the VMMCALL instruction: |
| 3347 | */ |
| 3348 | hypercall[0] = 0x0f; |
| 3349 | hypercall[1] = 0x01; |
| 3350 | hypercall[2] = 0xd9; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 3351 | } |
| 3352 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3353 | static void svm_check_processor_compat(void *rtn) |
| 3354 | { |
| 3355 | *(int *)rtn = 0; |
| 3356 | } |
| 3357 | |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 3358 | static bool svm_cpu_has_accelerated_tpr(void) |
| 3359 | { |
| 3360 | return false; |
| 3361 | } |
| 3362 | |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 3363 | static int get_npt_level(void) |
| 3364 | { |
| 3365 | #ifdef CONFIG_X86_64 |
| 3366 | return PT64_ROOT_LEVEL; |
| 3367 | #else |
| 3368 | return PT32E_ROOT_LEVEL; |
| 3369 | #endif |
| 3370 | } |
| 3371 | |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 3372 | static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio) |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 3373 | { |
| 3374 | return 0; |
| 3375 | } |
| 3376 | |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 3377 | static void svm_cpuid_update(struct kvm_vcpu *vcpu) |
| 3378 | { |
| 3379 | } |
| 3380 | |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 3381 | static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
| 3382 | { |
Joerg Roedel | c2c63a4 | 2010-04-22 12:33:12 +0200 | [diff] [blame] | 3383 | switch (func) { |
| 3384 | case 0x8000000A: |
| 3385 | entry->eax = 1; /* SVM revision 1 */ |
| 3386 | entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper |
| 3387 | ASID emulation to nested SVM */ |
| 3388 | entry->ecx = 0; /* Reserved */ |
Joerg Roedel | 7a19066 | 2010-07-27 18:14:21 +0200 | [diff] [blame] | 3389 | entry->edx = 0; /* Per default do not support any |
| 3390 | additional features */ |
| 3391 | |
| 3392 | /* Support next_rip if host supports it */ |
| 3393 | if (svm_has(SVM_FEATURE_NRIP)) |
| 3394 | entry->edx |= SVM_FEATURE_NRIP; |
Joerg Roedel | c2c63a4 | 2010-04-22 12:33:12 +0200 | [diff] [blame] | 3395 | |
| 3396 | break; |
| 3397 | } |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 3398 | } |
| 3399 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 3400 | static const struct trace_print_flags svm_exit_reasons_str[] = { |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 3401 | { SVM_EXIT_READ_CR0, "read_cr0" }, |
| 3402 | { SVM_EXIT_READ_CR3, "read_cr3" }, |
| 3403 | { SVM_EXIT_READ_CR4, "read_cr4" }, |
| 3404 | { SVM_EXIT_READ_CR8, "read_cr8" }, |
| 3405 | { SVM_EXIT_WRITE_CR0, "write_cr0" }, |
| 3406 | { SVM_EXIT_WRITE_CR3, "write_cr3" }, |
| 3407 | { SVM_EXIT_WRITE_CR4, "write_cr4" }, |
| 3408 | { SVM_EXIT_WRITE_CR8, "write_cr8" }, |
| 3409 | { SVM_EXIT_READ_DR0, "read_dr0" }, |
| 3410 | { SVM_EXIT_READ_DR1, "read_dr1" }, |
| 3411 | { SVM_EXIT_READ_DR2, "read_dr2" }, |
| 3412 | { SVM_EXIT_READ_DR3, "read_dr3" }, |
| 3413 | { SVM_EXIT_WRITE_DR0, "write_dr0" }, |
| 3414 | { SVM_EXIT_WRITE_DR1, "write_dr1" }, |
| 3415 | { SVM_EXIT_WRITE_DR2, "write_dr2" }, |
| 3416 | { SVM_EXIT_WRITE_DR3, "write_dr3" }, |
| 3417 | { SVM_EXIT_WRITE_DR5, "write_dr5" }, |
| 3418 | { SVM_EXIT_WRITE_DR7, "write_dr7" }, |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 3419 | { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" }, |
| 3420 | { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" }, |
| 3421 | { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" }, |
| 3422 | { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" }, |
| 3423 | { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" }, |
| 3424 | { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" }, |
| 3425 | { SVM_EXIT_INTR, "interrupt" }, |
| 3426 | { SVM_EXIT_NMI, "nmi" }, |
| 3427 | { SVM_EXIT_SMI, "smi" }, |
| 3428 | { SVM_EXIT_INIT, "init" }, |
| 3429 | { SVM_EXIT_VINTR, "vintr" }, |
| 3430 | { SVM_EXIT_CPUID, "cpuid" }, |
| 3431 | { SVM_EXIT_INVD, "invd" }, |
| 3432 | { SVM_EXIT_HLT, "hlt" }, |
| 3433 | { SVM_EXIT_INVLPG, "invlpg" }, |
| 3434 | { SVM_EXIT_INVLPGA, "invlpga" }, |
| 3435 | { SVM_EXIT_IOIO, "io" }, |
| 3436 | { SVM_EXIT_MSR, "msr" }, |
| 3437 | { SVM_EXIT_TASK_SWITCH, "task_switch" }, |
| 3438 | { SVM_EXIT_SHUTDOWN, "shutdown" }, |
| 3439 | { SVM_EXIT_VMRUN, "vmrun" }, |
| 3440 | { SVM_EXIT_VMMCALL, "hypercall" }, |
| 3441 | { SVM_EXIT_VMLOAD, "vmload" }, |
| 3442 | { SVM_EXIT_VMSAVE, "vmsave" }, |
| 3443 | { SVM_EXIT_STGI, "stgi" }, |
| 3444 | { SVM_EXIT_CLGI, "clgi" }, |
| 3445 | { SVM_EXIT_SKINIT, "skinit" }, |
| 3446 | { SVM_EXIT_WBINVD, "wbinvd" }, |
| 3447 | { SVM_EXIT_MONITOR, "monitor" }, |
| 3448 | { SVM_EXIT_MWAIT, "mwait" }, |
| 3449 | { SVM_EXIT_NPF, "npf" }, |
| 3450 | { -1, NULL } |
| 3451 | }; |
| 3452 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 3453 | static int svm_get_lpage_level(void) |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 3454 | { |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 3455 | return PT_PDPE_LEVEL; |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 3456 | } |
| 3457 | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3458 | static bool svm_rdtscp_supported(void) |
| 3459 | { |
| 3460 | return false; |
| 3461 | } |
| 3462 | |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 3463 | static bool svm_has_wbinvd_exit(void) |
| 3464 | { |
| 3465 | return true; |
| 3466 | } |
| 3467 | |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 3468 | static void svm_fpu_deactivate(struct kvm_vcpu *vcpu) |
| 3469 | { |
| 3470 | struct vcpu_svm *svm = to_svm(vcpu); |
| 3471 | |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 3472 | svm->vmcb->control.intercept_exceptions |= 1 << NM_VECTOR; |
Joerg Roedel | 66a562f | 2010-02-19 16:23:08 +0100 | [diff] [blame] | 3473 | if (is_nested(svm)) |
| 3474 | svm->nested.hsave->control.intercept_exceptions |= 1 << NM_VECTOR; |
| 3475 | update_cr0_intercept(svm); |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 3476 | } |
| 3477 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 3478 | static struct kvm_x86_ops svm_x86_ops = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3479 | .cpu_has_kvm_support = has_svm, |
| 3480 | .disabled_by_bios = is_disabled, |
| 3481 | .hardware_setup = svm_hardware_setup, |
| 3482 | .hardware_unsetup = svm_hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3483 | .check_processor_compatibility = svm_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3484 | .hardware_enable = svm_hardware_enable, |
| 3485 | .hardware_disable = svm_hardware_disable, |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 3486 | .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3487 | |
| 3488 | .vcpu_create = svm_create_vcpu, |
| 3489 | .vcpu_free = svm_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 3490 | .vcpu_reset = svm_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3491 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 3492 | .prepare_guest_switch = svm_prepare_guest_switch, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3493 | .vcpu_load = svm_vcpu_load, |
| 3494 | .vcpu_put = svm_vcpu_put, |
| 3495 | |
| 3496 | .set_guest_debug = svm_guest_debug, |
| 3497 | .get_msr = svm_get_msr, |
| 3498 | .set_msr = svm_set_msr, |
| 3499 | .get_segment_base = svm_get_segment_base, |
| 3500 | .get_segment = svm_get_segment, |
| 3501 | .set_segment = svm_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 3502 | .get_cpl = svm_get_cpl, |
Rusty Russell | 1747fb7 | 2007-09-06 01:21:32 +1000 | [diff] [blame] | 3503 | .get_cs_db_l_bits = kvm_get_cs_db_l_bits, |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 3504 | .decache_cr0_guest_bits = svm_decache_cr0_guest_bits, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 3505 | .decache_cr4_guest_bits = svm_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3506 | .set_cr0 = svm_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3507 | .set_cr3 = svm_set_cr3, |
| 3508 | .set_cr4 = svm_set_cr4, |
| 3509 | .set_efer = svm_set_efer, |
| 3510 | .get_idt = svm_get_idt, |
| 3511 | .set_idt = svm_set_idt, |
| 3512 | .get_gdt = svm_get_gdt, |
| 3513 | .set_gdt = svm_set_gdt, |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 3514 | .set_dr7 = svm_set_dr7, |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 3515 | .cache_reg = svm_cache_reg, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3516 | .get_rflags = svm_get_rflags, |
| 3517 | .set_rflags = svm_set_rflags, |
Avi Kivity | 6b52d18 | 2010-01-21 15:31:47 +0200 | [diff] [blame] | 3518 | .fpu_activate = svm_fpu_activate, |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 3519 | .fpu_deactivate = svm_fpu_deactivate, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3520 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3521 | .tlb_flush = svm_flush_tlb, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3522 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3523 | .run = svm_vcpu_run, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 3524 | .handle_exit = handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3525 | .skip_emulated_instruction = skip_emulated_instruction, |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 3526 | .set_interrupt_shadow = svm_set_interrupt_shadow, |
| 3527 | .get_interrupt_shadow = svm_get_interrupt_shadow, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 3528 | .patch_hypercall = svm_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 3529 | .set_irq = svm_set_irq, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 3530 | .set_nmi = svm_inject_nmi, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 3531 | .queue_exception = svm_queue_exception, |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 3532 | .interrupt_allowed = svm_interrupt_allowed, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 3533 | .nmi_allowed = svm_nmi_allowed, |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3534 | .get_nmi_mask = svm_get_nmi_mask, |
| 3535 | .set_nmi_mask = svm_set_nmi_mask, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 3536 | .enable_nmi_window = enable_nmi_window, |
| 3537 | .enable_irq_window = enable_irq_window, |
| 3538 | .update_cr8_intercept = update_cr8_intercept, |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 3539 | |
| 3540 | .set_tss_addr = svm_set_tss_addr, |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 3541 | .get_tdp_level = get_npt_level, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 3542 | .get_mt_mask = svm_get_mt_mask, |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 3543 | |
| 3544 | .exit_reasons_str = svm_exit_reasons_str, |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 3545 | .get_lpage_level = svm_get_lpage_level, |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 3546 | |
| 3547 | .cpuid_update = svm_cpuid_update, |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3548 | |
| 3549 | .rdtscp_supported = svm_rdtscp_supported, |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 3550 | |
| 3551 | .set_supported_cpuid = svm_set_supported_cpuid, |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 3552 | |
| 3553 | .has_wbinvd_exit = svm_has_wbinvd_exit, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame^] | 3554 | |
| 3555 | .write_tsc_offset = svm_write_tsc_offset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3556 | }; |
| 3557 | |
| 3558 | static int __init svm_init(void) |
| 3559 | { |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 3560 | return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm), |
Avi Kivity | 0ee75be | 2010-04-28 15:39:01 +0300 | [diff] [blame] | 3561 | __alignof__(struct vcpu_svm), THIS_MODULE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3562 | } |
| 3563 | |
| 3564 | static void __exit svm_exit(void) |
| 3565 | { |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 3566 | kvm_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3567 | } |
| 3568 | |
| 3569 | module_init(svm_init) |
| 3570 | module_exit(svm_exit) |