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