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