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