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