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