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