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