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> |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 37 | #include <linux/amd-iommu.h> |
| 38 | #include <linux/hashtable.h> |
Josh Poimboeuf | c207aee | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 39 | #include <linux/frame.h> |
Brijesh Singh | e9df094 | 2017-12-04 10:57:33 -0600 | [diff] [blame] | 40 | #include <linux/psp-sev.h> |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 41 | #include <linux/file.h> |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 42 | #include <linux/pagemap.h> |
| 43 | #include <linux/swap.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 44 | |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 45 | #include <asm/apic.h> |
Joerg Roedel | 1018faa | 2012-02-29 14:57:32 +0100 | [diff] [blame] | 46 | #include <asm/perf_event.h> |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 47 | #include <asm/tlbflush.h> |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 48 | #include <asm/desc.h> |
Paolo Bonzini | facb013 | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 49 | #include <asm/debugreg.h> |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 50 | #include <asm/kvm_para.h> |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 51 | #include <asm/irq_remapping.h> |
David Woodhouse | 117cc7a | 2018-01-12 11:11:27 +0000 | [diff] [blame] | 52 | #include <asm/nospec-branch.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 53 | |
Eduardo Habkost | 63d1142 | 2008-11-17 19:03:20 -0200 | [diff] [blame] | 54 | #include <asm/virtext.h> |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 55 | #include "trace.h" |
Eduardo Habkost | 63d1142 | 2008-11-17 19:03:20 -0200 | [diff] [blame] | 56 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 57 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
| 58 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 59 | MODULE_AUTHOR("Qumranet"); |
| 60 | MODULE_LICENSE("GPL"); |
| 61 | |
Josh Triplett | ae75954 | 2012-03-28 11:32:28 -0700 | [diff] [blame] | 62 | static const struct x86_cpu_id svm_cpu_id[] = { |
| 63 | X86_FEATURE_MATCH(X86_FEATURE_SVM), |
| 64 | {} |
| 65 | }; |
| 66 | MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id); |
| 67 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 68 | #define IOPM_ALLOC_ORDER 2 |
| 69 | #define MSRPM_ALLOC_ORDER 1 |
| 70 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 71 | #define SEG_TYPE_LDT 2 |
| 72 | #define SEG_TYPE_BUSY_TSS16 3 |
| 73 | |
Andre Przywara | 6bc31bd | 2010-04-11 23:07:28 +0200 | [diff] [blame] | 74 | #define SVM_FEATURE_NPT (1 << 0) |
| 75 | #define SVM_FEATURE_LBRV (1 << 1) |
| 76 | #define SVM_FEATURE_SVML (1 << 2) |
| 77 | #define SVM_FEATURE_NRIP (1 << 3) |
Andre Przywara | ddce97a | 2010-12-21 11:12:03 +0100 | [diff] [blame] | 78 | #define SVM_FEATURE_TSC_RATE (1 << 4) |
| 79 | #define SVM_FEATURE_VMCB_CLEAN (1 << 5) |
| 80 | #define SVM_FEATURE_FLUSH_ASID (1 << 6) |
| 81 | #define SVM_FEATURE_DECODE_ASSIST (1 << 7) |
Andre Przywara | 6bc31bd | 2010-04-11 23:07:28 +0200 | [diff] [blame] | 82 | #define SVM_FEATURE_PAUSE_FILTER (1 << 10) |
Joerg Roedel | 80b7706 | 2007-03-30 17:02:14 +0300 | [diff] [blame] | 83 | |
Suravee Suthikulpanit | 340d3bc | 2016-05-04 14:09:47 -0500 | [diff] [blame] | 84 | #define SVM_AVIC_DOORBELL 0xc001011b |
| 85 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 86 | #define NESTED_EXIT_HOST 0 /* Exit handled on host level */ |
| 87 | #define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */ |
| 88 | #define NESTED_EXIT_CONTINUE 2 /* Further checks needed */ |
| 89 | |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 90 | #define DEBUGCTL_RESERVED_BITS (~(0x3fULL)) |
| 91 | |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 92 | #define TSC_RATIO_RSVD 0xffffff0000000000ULL |
Joerg Roedel | 92a1f12 | 2011-03-25 09:44:51 +0100 | [diff] [blame] | 93 | #define TSC_RATIO_MIN 0x0000000000000001ULL |
| 94 | #define TSC_RATIO_MAX 0x000000ffffffffffULL |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 95 | |
Dan Carpenter | 5446a97 | 2016-05-23 13:20:10 +0300 | [diff] [blame] | 96 | #define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF) |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 97 | |
| 98 | /* |
| 99 | * 0xff is broadcast, so the max index allowed for physical APIC ID |
| 100 | * table is 0xfe. APIC IDs above 0xff are reserved. |
| 101 | */ |
| 102 | #define AVIC_MAX_PHYSICAL_ID_COUNT 255 |
| 103 | |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 104 | #define AVIC_UNACCEL_ACCESS_WRITE_MASK 1 |
| 105 | #define AVIC_UNACCEL_ACCESS_OFFSET_MASK 0xFF0 |
| 106 | #define AVIC_UNACCEL_ACCESS_VECTOR_MASK 0xFFFFFFFF |
| 107 | |
Suravee Suthikulpanit | 5ea11f2 | 2016-08-23 13:52:41 -0500 | [diff] [blame] | 108 | /* AVIC GATAG is encoded using VM and VCPU IDs */ |
| 109 | #define AVIC_VCPU_ID_BITS 8 |
| 110 | #define AVIC_VCPU_ID_MASK ((1 << AVIC_VCPU_ID_BITS) - 1) |
| 111 | |
| 112 | #define AVIC_VM_ID_BITS 24 |
| 113 | #define AVIC_VM_ID_NR (1 << AVIC_VM_ID_BITS) |
| 114 | #define AVIC_VM_ID_MASK ((1 << AVIC_VM_ID_BITS) - 1) |
| 115 | |
| 116 | #define AVIC_GATAG(x, y) (((x & AVIC_VM_ID_MASK) << AVIC_VCPU_ID_BITS) | \ |
| 117 | (y & AVIC_VCPU_ID_MASK)) |
| 118 | #define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_VCPU_ID_BITS) & AVIC_VM_ID_MASK) |
| 119 | #define AVIC_GATAG_TO_VCPUID(x) (x & AVIC_VCPU_ID_MASK) |
| 120 | |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 121 | static bool erratum_383_found __read_mostly; |
| 122 | |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 123 | static const u32 host_save_user_msrs[] = { |
| 124 | #ifdef CONFIG_X86_64 |
| 125 | MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE, |
| 126 | MSR_FS_BASE, |
| 127 | #endif |
| 128 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, |
Paolo Bonzini | 46896c7 | 2015-11-12 14:49:16 +0100 | [diff] [blame] | 129 | MSR_TSC_AUX, |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | #define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs) |
| 133 | |
| 134 | struct kvm_vcpu; |
| 135 | |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 136 | struct nested_state { |
| 137 | struct vmcb *hsave; |
| 138 | u64 hsave_msr; |
Joerg Roedel | 4a81018 | 2010-02-24 18:59:15 +0100 | [diff] [blame] | 139 | u64 vm_cr_msr; |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 140 | u64 vmcb; |
| 141 | |
| 142 | /* These are the merged vectors */ |
| 143 | u32 *msrpm; |
| 144 | |
| 145 | /* gpa pointers to the real vectors */ |
| 146 | u64 vmcb_msrpm; |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 147 | u64 vmcb_iopm; |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 148 | |
Joerg Roedel | cd3ff65 | 2009-10-09 16:08:26 +0200 | [diff] [blame] | 149 | /* A VMEXIT is required but not yet emulated */ |
| 150 | bool exit_required; |
| 151 | |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 152 | /* cache for intercepts of the guest */ |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 153 | u32 intercept_cr; |
Joerg Roedel | 3aed041 | 2010-11-30 18:03:58 +0100 | [diff] [blame] | 154 | u32 intercept_dr; |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 155 | u32 intercept_exceptions; |
| 156 | u64 intercept; |
| 157 | |
Joerg Roedel | 5bd2edc | 2010-09-10 17:31:02 +0200 | [diff] [blame] | 158 | /* Nested Paging related state */ |
| 159 | u64 nested_cr3; |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 160 | }; |
| 161 | |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 162 | #define MSRPM_OFFSETS 16 |
| 163 | static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly; |
| 164 | |
Boris Ostrovsky | 2b036c6 | 2012-01-09 14:00:35 -0500 | [diff] [blame] | 165 | /* |
| 166 | * Set osvw_len to higher value when updated Revision Guides |
| 167 | * are published and we know what the new status bits are |
| 168 | */ |
| 169 | static uint64_t osvw_len = 4, osvw_status; |
| 170 | |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 171 | struct vcpu_svm { |
| 172 | struct kvm_vcpu vcpu; |
| 173 | struct vmcb *vmcb; |
| 174 | unsigned long vmcb_pa; |
| 175 | struct svm_cpu_data *svm_data; |
| 176 | uint64_t asid_generation; |
| 177 | uint64_t sysenter_esp; |
| 178 | uint64_t sysenter_eip; |
Paolo Bonzini | 46896c7 | 2015-11-12 14:49:16 +0100 | [diff] [blame] | 179 | uint64_t tsc_aux; |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 180 | |
| 181 | u64 next_rip; |
| 182 | |
| 183 | u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS]; |
Avi Kivity | afe9e66 | 2010-10-21 12:20:32 +0200 | [diff] [blame] | 184 | struct { |
Avi Kivity | dacccfd | 2010-10-21 12:20:33 +0200 | [diff] [blame] | 185 | u16 fs; |
| 186 | u16 gs; |
| 187 | u16 ldt; |
Avi Kivity | afe9e66 | 2010-10-21 12:20:32 +0200 | [diff] [blame] | 188 | u64 gs_base; |
| 189 | } host; |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 190 | |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 191 | u64 spec_ctrl; |
| 192 | |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 193 | u32 *msrpm; |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 194 | |
Avi Kivity | bd3d1ec | 2011-02-03 15:29:52 +0200 | [diff] [blame] | 195 | ulong nmi_iret_rip; |
| 196 | |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 197 | struct nested_state nested; |
Jan Kiszka | 6be7d30 | 2009-10-18 13:24:54 +0200 | [diff] [blame] | 198 | |
| 199 | bool nmi_singlestep; |
Ladi Prosek | ab2f4d73 | 2017-06-21 09:06:58 +0200 | [diff] [blame] | 200 | u64 nmi_singlestep_guest_rflags; |
Jan Kiszka | 66b7138 | 2010-02-23 17:47:56 +0100 | [diff] [blame] | 201 | |
| 202 | unsigned int3_injected; |
| 203 | unsigned long int3_rip; |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 204 | |
Joerg Roedel | 6092d3d | 2015-10-14 15:10:54 +0200 | [diff] [blame] | 205 | /* cached guest cpuid flags for faster access */ |
| 206 | bool nrips_enabled : 1; |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 207 | |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 208 | u32 ldr_reg; |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 209 | struct page *avic_backing_page; |
| 210 | u64 *avic_physical_id_cache; |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 211 | bool avic_is_running; |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 212 | |
| 213 | /* |
| 214 | * Per-vcpu list of struct amd_svm_iommu_ir: |
| 215 | * This is used mainly to store interrupt remapping information used |
| 216 | * when update the vcpu affinity. This avoids the need to scan for |
| 217 | * IRTE and try to match ga_tag in the IOMMU driver. |
| 218 | */ |
| 219 | struct list_head ir_list; |
| 220 | spinlock_t ir_list_lock; |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 221 | |
| 222 | /* which host CPU was used for running this vcpu */ |
| 223 | unsigned int last_cpu; |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 224 | }; |
| 225 | |
| 226 | /* |
| 227 | * This is a wrapper of struct amd_iommu_ir_data. |
| 228 | */ |
| 229 | struct amd_svm_iommu_ir { |
| 230 | struct list_head node; /* Used by SVM for per-vcpu ir_list */ |
| 231 | void *data; /* Storing pointer to struct amd_ir_data */ |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 232 | }; |
| 233 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 234 | #define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFF) |
| 235 | #define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31) |
| 236 | |
| 237 | #define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK (0xFFULL) |
| 238 | #define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK (0xFFFFFFFFFFULL << 12) |
| 239 | #define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK (1ULL << 62) |
| 240 | #define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK (1ULL << 63) |
| 241 | |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 242 | static DEFINE_PER_CPU(u64, current_tsc_ratio); |
| 243 | #define TSC_RATIO_DEFAULT 0x0100000000ULL |
| 244 | |
Joerg Roedel | 455716f | 2010-03-01 15:34:35 +0100 | [diff] [blame] | 245 | #define MSR_INVALID 0xffffffffU |
| 246 | |
Mathias Krause | 09941fb | 2012-08-30 01:30:20 +0200 | [diff] [blame] | 247 | static const struct svm_direct_access_msrs { |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 248 | u32 index; /* Index of the MSR */ |
| 249 | bool always; /* True if intercept is always on */ |
| 250 | } direct_access_msrs[] = { |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 251 | { .index = MSR_STAR, .always = true }, |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 252 | { .index = MSR_IA32_SYSENTER_CS, .always = true }, |
| 253 | #ifdef CONFIG_X86_64 |
| 254 | { .index = MSR_GS_BASE, .always = true }, |
| 255 | { .index = MSR_FS_BASE, .always = true }, |
| 256 | { .index = MSR_KERNEL_GS_BASE, .always = true }, |
| 257 | { .index = MSR_LSTAR, .always = true }, |
| 258 | { .index = MSR_CSTAR, .always = true }, |
| 259 | { .index = MSR_SYSCALL_MASK, .always = true }, |
| 260 | #endif |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 261 | { .index = MSR_IA32_SPEC_CTRL, .always = false }, |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 262 | { .index = MSR_IA32_PRED_CMD, .always = false }, |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 263 | { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false }, |
| 264 | { .index = MSR_IA32_LASTBRANCHTOIP, .always = false }, |
| 265 | { .index = MSR_IA32_LASTINTFROMIP, .always = false }, |
| 266 | { .index = MSR_IA32_LASTINTTOIP, .always = false }, |
| 267 | { .index = MSR_INVALID, .always = false }, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 268 | }; |
| 269 | |
| 270 | /* enable NPT for AMD64 and X86 with PAE */ |
| 271 | #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE) |
| 272 | static bool npt_enabled = true; |
| 273 | #else |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 274 | static bool npt_enabled; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 275 | #endif |
| 276 | |
Davidlohr Bueso | e235885 | 2012-01-17 14:09:50 +0100 | [diff] [blame] | 277 | /* allow nested paging (virtualized MMU) for all guests */ |
| 278 | static int npt = true; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 279 | module_param(npt, int, S_IRUGO); |
| 280 | |
Davidlohr Bueso | e235885 | 2012-01-17 14:09:50 +0100 | [diff] [blame] | 281 | /* allow nested virtualization in KVM/SVM */ |
| 282 | static int nested = true; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 283 | module_param(nested, int, S_IRUGO); |
| 284 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 285 | /* enable / disable AVIC */ |
| 286 | static int avic; |
Suravee Suthikulpanit | 5b8abf1 | 2016-06-15 17:24:36 -0500 | [diff] [blame] | 287 | #ifdef CONFIG_X86_LOCAL_APIC |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 288 | module_param(avic, int, S_IRUGO); |
Suravee Suthikulpanit | 5b8abf1 | 2016-06-15 17:24:36 -0500 | [diff] [blame] | 289 | #endif |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 290 | |
Janakarajan Natarajan | 89c8a49 | 2017-07-06 15:50:47 -0500 | [diff] [blame] | 291 | /* enable/disable Virtual VMLOAD VMSAVE */ |
| 292 | static int vls = true; |
| 293 | module_param(vls, int, 0444); |
| 294 | |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 295 | /* enable/disable Virtual GIF */ |
| 296 | static int vgif = true; |
| 297 | module_param(vgif, int, 0444); |
Suravee Suthikulpanit | 5ea11f2 | 2016-08-23 13:52:41 -0500 | [diff] [blame] | 298 | |
Brijesh Singh | e9df094 | 2017-12-04 10:57:33 -0600 | [diff] [blame] | 299 | /* enable/disable SEV support */ |
| 300 | static int sev = IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT); |
| 301 | module_param(sev, int, 0444); |
| 302 | |
Paolo Bonzini | 79a8059 | 2015-09-21 07:46:55 +0200 | [diff] [blame] | 303 | static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0); |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 304 | static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa); |
Joerg Roedel | a5c3832 | 2009-08-07 11:49:32 +0200 | [diff] [blame] | 305 | static void svm_complete_interrupts(struct vcpu_svm *svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 306 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 307 | static int nested_svm_exit_handled(struct vcpu_svm *svm); |
Joerg Roedel | b8e88bc | 2010-02-19 16:23:02 +0100 | [diff] [blame] | 308 | static int nested_svm_intercept(struct vcpu_svm *svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 309 | static int nested_svm_vmexit(struct vcpu_svm *svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 310 | static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr, |
| 311 | bool has_error_code, u32 error_code); |
| 312 | |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 313 | enum { |
Joerg Roedel | 116a0a2 | 2010-12-03 11:45:49 +0100 | [diff] [blame] | 314 | VMCB_INTERCEPTS, /* Intercept vectors, TSC offset, |
| 315 | pause filter count */ |
Joerg Roedel | f56838e | 2010-12-03 11:45:50 +0100 | [diff] [blame] | 316 | VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */ |
Joerg Roedel | d48086d | 2010-12-03 11:45:51 +0100 | [diff] [blame] | 317 | VMCB_ASID, /* ASID */ |
Joerg Roedel | decdbf6 | 2010-12-03 11:45:52 +0100 | [diff] [blame] | 318 | VMCB_INTR, /* int_ctl, int_vector */ |
Joerg Roedel | b274716 | 2010-12-03 11:45:53 +0100 | [diff] [blame] | 319 | VMCB_NPT, /* npt_en, nCR3, gPAT */ |
Joerg Roedel | dcca1a6 | 2010-12-03 11:45:54 +0100 | [diff] [blame] | 320 | VMCB_CR, /* CR0, CR3, CR4, EFER */ |
Joerg Roedel | 72214b9 | 2010-12-03 11:45:55 +0100 | [diff] [blame] | 321 | VMCB_DR, /* DR6, DR7 */ |
Joerg Roedel | 17a703c | 2010-12-03 11:45:56 +0100 | [diff] [blame] | 322 | VMCB_DT, /* GDT, IDT */ |
Joerg Roedel | 060d0c9 | 2010-12-03 11:45:57 +0100 | [diff] [blame] | 323 | VMCB_SEG, /* CS, DS, SS, ES, CPL */ |
Joerg Roedel | 0574dec | 2010-12-03 11:45:58 +0100 | [diff] [blame] | 324 | VMCB_CR2, /* CR2 only */ |
Joerg Roedel | b53ba3f | 2010-12-03 11:45:59 +0100 | [diff] [blame] | 325 | 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] | 326 | VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE, |
| 327 | * AVIC PHYSICAL_TABLE pointer, |
| 328 | * AVIC LOGICAL_TABLE pointer |
| 329 | */ |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 330 | VMCB_DIRTY_MAX, |
| 331 | }; |
| 332 | |
Joerg Roedel | 0574dec | 2010-12-03 11:45:58 +0100 | [diff] [blame] | 333 | /* TPR and CR2 are always written before VMRUN */ |
| 334 | #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2)) |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 335 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 336 | #define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL |
| 337 | |
Brijesh Singh | ed3cd23 | 2017-12-04 10:57:32 -0600 | [diff] [blame] | 338 | static unsigned int max_sev_asid; |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 339 | static unsigned int min_sev_asid; |
| 340 | static unsigned long *sev_asid_bitmap; |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 341 | #define __sme_page_pa(x) __sme_set(page_to_pfn(x) << PAGE_SHIFT) |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 342 | |
Brijesh Singh | 1e80fdc | 2017-12-04 10:57:38 -0600 | [diff] [blame] | 343 | struct enc_region { |
| 344 | struct list_head list; |
| 345 | unsigned long npages; |
| 346 | struct page **pages; |
| 347 | unsigned long uaddr; |
| 348 | unsigned long size; |
| 349 | }; |
| 350 | |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 351 | static inline bool svm_sev_enabled(void) |
| 352 | { |
| 353 | return max_sev_asid; |
| 354 | } |
| 355 | |
| 356 | static inline bool sev_guest(struct kvm *kvm) |
| 357 | { |
| 358 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
| 359 | |
| 360 | return sev->active; |
| 361 | } |
Brijesh Singh | ed3cd23 | 2017-12-04 10:57:32 -0600 | [diff] [blame] | 362 | |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 363 | static inline int sev_get_asid(struct kvm *kvm) |
| 364 | { |
| 365 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
| 366 | |
| 367 | return sev->asid; |
| 368 | } |
| 369 | |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 370 | static inline void mark_all_dirty(struct vmcb *vmcb) |
| 371 | { |
| 372 | vmcb->control.clean = 0; |
| 373 | } |
| 374 | |
| 375 | static inline void mark_all_clean(struct vmcb *vmcb) |
| 376 | { |
| 377 | vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1) |
| 378 | & ~VMCB_ALWAYS_DIRTY_MASK; |
| 379 | } |
| 380 | |
| 381 | static inline void mark_dirty(struct vmcb *vmcb, int bit) |
| 382 | { |
| 383 | vmcb->control.clean &= ~(1 << bit); |
| 384 | } |
| 385 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 386 | static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu) |
| 387 | { |
| 388 | return container_of(vcpu, struct vcpu_svm, vcpu); |
| 389 | } |
| 390 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 391 | static inline void avic_update_vapic_bar(struct vcpu_svm *svm, u64 data) |
| 392 | { |
| 393 | svm->vmcb->control.avic_vapic_bar = data & VMCB_AVIC_APIC_BAR_MASK; |
| 394 | mark_dirty(svm->vmcb, VMCB_AVIC); |
| 395 | } |
| 396 | |
Suravee Suthikulpanit | 340d3bc | 2016-05-04 14:09:47 -0500 | [diff] [blame] | 397 | static inline bool avic_vcpu_is_running(struct kvm_vcpu *vcpu) |
| 398 | { |
| 399 | struct vcpu_svm *svm = to_svm(vcpu); |
| 400 | u64 *entry = svm->avic_physical_id_cache; |
| 401 | |
| 402 | if (!entry) |
| 403 | return false; |
| 404 | |
| 405 | return (READ_ONCE(*entry) & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK); |
| 406 | } |
| 407 | |
Joerg Roedel | 384c636 | 2010-11-30 18:03:56 +0100 | [diff] [blame] | 408 | static void recalc_intercepts(struct vcpu_svm *svm) |
| 409 | { |
| 410 | struct vmcb_control_area *c, *h; |
| 411 | struct nested_state *g; |
| 412 | |
Joerg Roedel | 116a0a2 | 2010-12-03 11:45:49 +0100 | [diff] [blame] | 413 | mark_dirty(svm->vmcb, VMCB_INTERCEPTS); |
| 414 | |
Joerg Roedel | 384c636 | 2010-11-30 18:03:56 +0100 | [diff] [blame] | 415 | if (!is_guest_mode(&svm->vcpu)) |
| 416 | return; |
| 417 | |
| 418 | c = &svm->vmcb->control; |
| 419 | h = &svm->nested.hsave->control; |
| 420 | g = &svm->nested; |
| 421 | |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 422 | c->intercept_cr = h->intercept_cr | g->intercept_cr; |
Joerg Roedel | 3aed041 | 2010-11-30 18:03:58 +0100 | [diff] [blame] | 423 | c->intercept_dr = h->intercept_dr | g->intercept_dr; |
Paolo Bonzini | bd89525 | 2018-01-11 16:55:24 +0100 | [diff] [blame] | 424 | c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions; |
Joerg Roedel | 384c636 | 2010-11-30 18:03:56 +0100 | [diff] [blame] | 425 | c->intercept = h->intercept | g->intercept; |
| 426 | } |
| 427 | |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 428 | static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm) |
| 429 | { |
| 430 | if (is_guest_mode(&svm->vcpu)) |
| 431 | return svm->nested.hsave; |
| 432 | else |
| 433 | return svm->vmcb; |
| 434 | } |
| 435 | |
| 436 | static inline void set_cr_intercept(struct vcpu_svm *svm, int bit) |
| 437 | { |
| 438 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 439 | |
| 440 | vmcb->control.intercept_cr |= (1U << bit); |
| 441 | |
| 442 | recalc_intercepts(svm); |
| 443 | } |
| 444 | |
| 445 | static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit) |
| 446 | { |
| 447 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 448 | |
| 449 | vmcb->control.intercept_cr &= ~(1U << bit); |
| 450 | |
| 451 | recalc_intercepts(svm); |
| 452 | } |
| 453 | |
| 454 | static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit) |
| 455 | { |
| 456 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 457 | |
| 458 | return vmcb->control.intercept_cr & (1U << bit); |
| 459 | } |
| 460 | |
Paolo Bonzini | 5315c71 | 2014-03-03 13:08:29 +0100 | [diff] [blame] | 461 | static inline void set_dr_intercepts(struct vcpu_svm *svm) |
Joerg Roedel | 3aed041 | 2010-11-30 18:03:58 +0100 | [diff] [blame] | 462 | { |
| 463 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 464 | |
Paolo Bonzini | 5315c71 | 2014-03-03 13:08:29 +0100 | [diff] [blame] | 465 | vmcb->control.intercept_dr = (1 << INTERCEPT_DR0_READ) |
| 466 | | (1 << INTERCEPT_DR1_READ) |
| 467 | | (1 << INTERCEPT_DR2_READ) |
| 468 | | (1 << INTERCEPT_DR3_READ) |
| 469 | | (1 << INTERCEPT_DR4_READ) |
| 470 | | (1 << INTERCEPT_DR5_READ) |
| 471 | | (1 << INTERCEPT_DR6_READ) |
| 472 | | (1 << INTERCEPT_DR7_READ) |
| 473 | | (1 << INTERCEPT_DR0_WRITE) |
| 474 | | (1 << INTERCEPT_DR1_WRITE) |
| 475 | | (1 << INTERCEPT_DR2_WRITE) |
| 476 | | (1 << INTERCEPT_DR3_WRITE) |
| 477 | | (1 << INTERCEPT_DR4_WRITE) |
| 478 | | (1 << INTERCEPT_DR5_WRITE) |
| 479 | | (1 << INTERCEPT_DR6_WRITE) |
| 480 | | (1 << INTERCEPT_DR7_WRITE); |
Joerg Roedel | 3aed041 | 2010-11-30 18:03:58 +0100 | [diff] [blame] | 481 | |
| 482 | recalc_intercepts(svm); |
| 483 | } |
| 484 | |
Paolo Bonzini | 5315c71 | 2014-03-03 13:08:29 +0100 | [diff] [blame] | 485 | static inline void clr_dr_intercepts(struct vcpu_svm *svm) |
Joerg Roedel | 3aed041 | 2010-11-30 18:03:58 +0100 | [diff] [blame] | 486 | { |
| 487 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 488 | |
Paolo Bonzini | 5315c71 | 2014-03-03 13:08:29 +0100 | [diff] [blame] | 489 | vmcb->control.intercept_dr = 0; |
Joerg Roedel | 3aed041 | 2010-11-30 18:03:58 +0100 | [diff] [blame] | 490 | |
| 491 | recalc_intercepts(svm); |
| 492 | } |
| 493 | |
Joerg Roedel | 18c918c | 2010-11-30 18:03:59 +0100 | [diff] [blame] | 494 | static inline void set_exception_intercept(struct vcpu_svm *svm, int bit) |
| 495 | { |
| 496 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 497 | |
| 498 | vmcb->control.intercept_exceptions |= (1U << bit); |
| 499 | |
| 500 | recalc_intercepts(svm); |
| 501 | } |
| 502 | |
| 503 | static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit) |
| 504 | { |
| 505 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 506 | |
| 507 | vmcb->control.intercept_exceptions &= ~(1U << bit); |
| 508 | |
| 509 | recalc_intercepts(svm); |
| 510 | } |
| 511 | |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 512 | static inline void set_intercept(struct vcpu_svm *svm, int bit) |
| 513 | { |
| 514 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 515 | |
| 516 | vmcb->control.intercept |= (1ULL << bit); |
| 517 | |
| 518 | recalc_intercepts(svm); |
| 519 | } |
| 520 | |
| 521 | static inline void clr_intercept(struct vcpu_svm *svm, int bit) |
| 522 | { |
| 523 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 524 | |
| 525 | vmcb->control.intercept &= ~(1ULL << bit); |
| 526 | |
| 527 | recalc_intercepts(svm); |
| 528 | } |
| 529 | |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 530 | static inline bool vgif_enabled(struct vcpu_svm *svm) |
| 531 | { |
| 532 | return !!(svm->vmcb->control.int_ctl & V_GIF_ENABLE_MASK); |
| 533 | } |
| 534 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 535 | static inline void enable_gif(struct vcpu_svm *svm) |
| 536 | { |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 537 | if (vgif_enabled(svm)) |
| 538 | svm->vmcb->control.int_ctl |= V_GIF_MASK; |
| 539 | else |
| 540 | svm->vcpu.arch.hflags |= HF_GIF_MASK; |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | static inline void disable_gif(struct vcpu_svm *svm) |
| 544 | { |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 545 | if (vgif_enabled(svm)) |
| 546 | svm->vmcb->control.int_ctl &= ~V_GIF_MASK; |
| 547 | else |
| 548 | svm->vcpu.arch.hflags &= ~HF_GIF_MASK; |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | static inline bool gif_set(struct vcpu_svm *svm) |
| 552 | { |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 553 | if (vgif_enabled(svm)) |
| 554 | return !!(svm->vmcb->control.int_ctl & V_GIF_MASK); |
| 555 | else |
| 556 | return !!(svm->vcpu.arch.hflags & HF_GIF_MASK); |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 557 | } |
| 558 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 559 | static unsigned long iopm_base; |
| 560 | |
| 561 | struct kvm_ldttss_desc { |
| 562 | u16 limit0; |
| 563 | u16 base0; |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 564 | unsigned base1:8, type:5, dpl:2, p:1; |
| 565 | unsigned limit1:4, zero0:3, g:1, base2:8; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 566 | u32 base3; |
| 567 | u32 zero1; |
| 568 | } __attribute__((packed)); |
| 569 | |
| 570 | struct svm_cpu_data { |
| 571 | int cpu; |
| 572 | |
Avi Kivity | 5008fdf | 2007-04-02 13:05:50 +0300 | [diff] [blame] | 573 | u64 asid_generation; |
| 574 | u32 max_asid; |
| 575 | u32 next_asid; |
Brijesh Singh | 4faefff | 2017-12-04 10:57:25 -0600 | [diff] [blame] | 576 | u32 min_asid; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 577 | struct kvm_ldttss_desc *tss_desc; |
| 578 | |
| 579 | struct page *save_area; |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 580 | struct vmcb *current_vmcb; |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 581 | |
| 582 | /* index = sev_asid, value = vmcb pointer */ |
| 583 | struct vmcb **sev_vmcbs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 584 | }; |
| 585 | |
| 586 | static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data); |
| 587 | |
| 588 | struct svm_init_data { |
| 589 | int cpu; |
| 590 | int r; |
| 591 | }; |
| 592 | |
Mathias Krause | 09941fb | 2012-08-30 01:30:20 +0200 | [diff] [blame] | 593 | static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000}; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 594 | |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 595 | #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 596 | #define MSRS_RANGE_SIZE 2048 |
| 597 | #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2) |
| 598 | |
Joerg Roedel | 455716f | 2010-03-01 15:34:35 +0100 | [diff] [blame] | 599 | static u32 svm_msrpm_offset(u32 msr) |
| 600 | { |
| 601 | u32 offset; |
| 602 | int i; |
| 603 | |
| 604 | for (i = 0; i < NUM_MSR_MAPS; i++) { |
| 605 | if (msr < msrpm_ranges[i] || |
| 606 | msr >= msrpm_ranges[i] + MSRS_IN_RANGE) |
| 607 | continue; |
| 608 | |
| 609 | offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */ |
| 610 | offset += (i * MSRS_RANGE_SIZE); /* add range offset */ |
| 611 | |
| 612 | /* Now we have the u8 offset - but need the u32 offset */ |
| 613 | return offset / 4; |
| 614 | } |
| 615 | |
| 616 | /* MSR not in any range */ |
| 617 | return MSR_INVALID; |
| 618 | } |
| 619 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 620 | #define MAX_INST_SIZE 15 |
| 621 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 622 | static inline void clgi(void) |
| 623 | { |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 624 | asm volatile (__ex(SVM_CLGI)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | static inline void stgi(void) |
| 628 | { |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 629 | asm volatile (__ex(SVM_STGI)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | static inline void invlpga(unsigned long addr, u32 asid) |
| 633 | { |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 634 | asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 635 | } |
| 636 | |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 637 | static int get_npt_level(struct kvm_vcpu *vcpu) |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 638 | { |
| 639 | #ifdef CONFIG_X86_64 |
Yu Zhang | 2a7266a | 2017-08-24 20:27:54 +0800 | [diff] [blame] | 640 | return PT64_ROOT_4LEVEL; |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 641 | #else |
| 642 | return PT32E_ROOT_LEVEL; |
| 643 | #endif |
| 644 | } |
| 645 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 646 | static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 647 | { |
Zachary Amsden | 6dc696d | 2010-05-26 15:09:43 -1000 | [diff] [blame] | 648 | vcpu->arch.efer = efer; |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 649 | if (!npt_enabled && !(efer & EFER_LMA)) |
Carlo Marcelo Arenas Belon | 2b5203e | 2007-12-01 06:17:11 -0600 | [diff] [blame] | 650 | efer &= ~EFER_LME; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 651 | |
Alexander Graf | 9962d03 | 2008-11-25 20:17:02 +0100 | [diff] [blame] | 652 | to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME; |
Joerg Roedel | dcca1a6 | 2010-12-03 11:45:54 +0100 | [diff] [blame] | 653 | mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 654 | } |
| 655 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 656 | static int is_external_interrupt(u32 info) |
| 657 | { |
| 658 | info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID; |
| 659 | return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR); |
| 660 | } |
| 661 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 662 | static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 663 | { |
| 664 | struct vcpu_svm *svm = to_svm(vcpu); |
| 665 | u32 ret = 0; |
| 666 | |
| 667 | if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 668 | ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS; |
| 669 | return ret; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 673 | { |
| 674 | struct vcpu_svm *svm = to_svm(vcpu); |
| 675 | |
| 676 | if (mask == 0) |
| 677 | svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK; |
| 678 | else |
| 679 | svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK; |
| 680 | |
| 681 | } |
| 682 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 683 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 684 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 685 | struct vcpu_svm *svm = to_svm(vcpu); |
| 686 | |
Bandan Das | f104765 | 2015-06-11 02:05:33 -0400 | [diff] [blame] | 687 | if (svm->vmcb->control.next_rip != 0) { |
Dirk Müller | d292242 | 2015-10-01 13:43:42 +0200 | [diff] [blame] | 688 | WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS)); |
Andre Przywara | 6bc31bd | 2010-04-11 23:07:28 +0200 | [diff] [blame] | 689 | svm->next_rip = svm->vmcb->control.next_rip; |
Bandan Das | f104765 | 2015-06-11 02:05:33 -0400 | [diff] [blame] | 690 | } |
Andre Przywara | 6bc31bd | 2010-04-11 23:07:28 +0200 | [diff] [blame] | 691 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 692 | if (!svm->next_rip) { |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 693 | if (emulate_instruction(vcpu, EMULTYPE_SKIP) != |
Gleb Natapov | f629cf8 | 2009-05-11 13:35:49 +0300 | [diff] [blame] | 694 | EMULATE_DONE) |
| 695 | printk(KERN_DEBUG "%s: NOP\n", __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 696 | return; |
| 697 | } |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 698 | if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE) |
| 699 | printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n", |
| 700 | __func__, kvm_rip_read(vcpu), svm->next_rip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 701 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 702 | kvm_rip_write(vcpu, svm->next_rip); |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 703 | svm_set_interrupt_shadow(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 704 | } |
| 705 | |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 706 | static void svm_queue_exception(struct kvm_vcpu *vcpu) |
Jan Kiszka | 116a475 | 2010-02-23 17:47:54 +0100 | [diff] [blame] | 707 | { |
| 708 | struct vcpu_svm *svm = to_svm(vcpu); |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 709 | unsigned nr = vcpu->arch.exception.nr; |
| 710 | bool has_error_code = vcpu->arch.exception.has_error_code; |
Wanpeng Li | 664f8e2 | 2017-08-24 03:35:09 -0700 | [diff] [blame] | 711 | bool reinject = vcpu->arch.exception.injected; |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 712 | u32 error_code = vcpu->arch.exception.error_code; |
Jan Kiszka | 116a475 | 2010-02-23 17:47:54 +0100 | [diff] [blame] | 713 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 714 | /* |
| 715 | * If we are within a nested VM we'd better #VMEXIT and let the guest |
| 716 | * handle the exception |
| 717 | */ |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 718 | if (!reinject && |
| 719 | nested_svm_check_exception(svm, nr, has_error_code, error_code)) |
Jan Kiszka | 116a475 | 2010-02-23 17:47:54 +0100 | [diff] [blame] | 720 | return; |
| 721 | |
Avi Kivity | 2a6b20b | 2010-11-09 16:15:42 +0200 | [diff] [blame] | 722 | if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) { |
Jan Kiszka | 66b7138 | 2010-02-23 17:47:56 +0100 | [diff] [blame] | 723 | unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu); |
| 724 | |
| 725 | /* |
| 726 | * For guest debugging where we have to reinject #BP if some |
| 727 | * INT3 is guest-owned: |
| 728 | * Emulate nRIP by moving RIP forward. Will fail if injection |
| 729 | * raises a fault that is not intercepted. Still better than |
| 730 | * failing in all cases. |
| 731 | */ |
| 732 | skip_emulated_instruction(&svm->vcpu); |
| 733 | rip = kvm_rip_read(&svm->vcpu); |
| 734 | svm->int3_rip = rip + svm->vmcb->save.cs.base; |
| 735 | svm->int3_injected = rip - old_rip; |
| 736 | } |
| 737 | |
Jan Kiszka | 116a475 | 2010-02-23 17:47:54 +0100 | [diff] [blame] | 738 | svm->vmcb->control.event_inj = nr |
| 739 | | SVM_EVTINJ_VALID |
| 740 | | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0) |
| 741 | | SVM_EVTINJ_TYPE_EXEPT; |
| 742 | svm->vmcb->control.event_inj_err = error_code; |
| 743 | } |
| 744 | |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 745 | static void svm_init_erratum_383(void) |
| 746 | { |
| 747 | u32 low, high; |
| 748 | int err; |
| 749 | u64 val; |
| 750 | |
Borislav Petkov | e6ee94d | 2013-03-20 15:07:27 +0100 | [diff] [blame] | 751 | if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH)) |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 752 | return; |
| 753 | |
| 754 | /* Use _safe variants to not break nested virtualization */ |
| 755 | val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err); |
| 756 | if (err) |
| 757 | return; |
| 758 | |
| 759 | val |= (1ULL << 47); |
| 760 | |
| 761 | low = lower_32_bits(val); |
| 762 | high = upper_32_bits(val); |
| 763 | |
| 764 | native_write_msr_safe(MSR_AMD64_DC_CFG, low, high); |
| 765 | |
| 766 | erratum_383_found = true; |
| 767 | } |
| 768 | |
Boris Ostrovsky | 2b036c6 | 2012-01-09 14:00:35 -0500 | [diff] [blame] | 769 | static void svm_init_osvw(struct kvm_vcpu *vcpu) |
| 770 | { |
| 771 | /* |
| 772 | * Guests should see errata 400 and 415 as fixed (assuming that |
| 773 | * HLT and IO instructions are intercepted). |
| 774 | */ |
| 775 | vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3; |
| 776 | vcpu->arch.osvw.status = osvw_status & ~(6ULL); |
| 777 | |
| 778 | /* |
| 779 | * By increasing VCPU's osvw.length to 3 we are telling the guest that |
| 780 | * all osvw.status bits inside that length, including bit 0 (which is |
| 781 | * reserved for erratum 298), are valid. However, if host processor's |
| 782 | * osvw_len is 0 then osvw_status[0] carries no information. We need to |
| 783 | * be conservative here and therefore we tell the guest that erratum 298 |
| 784 | * is present (because we really don't know). |
| 785 | */ |
| 786 | if (osvw_len == 0 && boot_cpu_data.x86 == 0x10) |
| 787 | vcpu->arch.osvw.status |= 1; |
| 788 | } |
| 789 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 790 | static int has_svm(void) |
| 791 | { |
Eduardo Habkost | 63d1142 | 2008-11-17 19:03:20 -0200 | [diff] [blame] | 792 | const char *msg; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 793 | |
Eduardo Habkost | 63d1142 | 2008-11-17 19:03:20 -0200 | [diff] [blame] | 794 | if (!cpu_has_svm(&msg)) { |
Joe Perches | ff81ff1 | 2009-01-08 11:05:17 -0800 | [diff] [blame] | 795 | printk(KERN_INFO "has_svm: %s\n", msg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 796 | return 0; |
| 797 | } |
| 798 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 799 | return 1; |
| 800 | } |
| 801 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 802 | static void svm_hardware_disable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 803 | { |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 804 | /* Make sure we clean up behind us */ |
| 805 | if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) |
| 806 | wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT); |
| 807 | |
Eduardo Habkost | 2c8dcee | 2008-11-17 19:03:21 -0200 | [diff] [blame] | 808 | cpu_svm_disable(); |
Joerg Roedel | 1018faa | 2012-02-29 14:57:32 +0100 | [diff] [blame] | 809 | |
| 810 | amd_pmu_disable_virt(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 811 | } |
| 812 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 813 | static int svm_hardware_enable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 814 | { |
| 815 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 816 | struct svm_cpu_data *sd; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 817 | uint64_t efer; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 818 | struct desc_struct *gdt; |
| 819 | int me = raw_smp_processor_id(); |
| 820 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 821 | rdmsrl(MSR_EFER, efer); |
| 822 | if (efer & EFER_SVME) |
| 823 | return -EBUSY; |
| 824 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 825 | if (!has_svm()) { |
Borislav Petkov | 1f5b77f | 2012-10-20 20:20:04 +0200 | [diff] [blame] | 826 | pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 827 | return -EINVAL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 828 | } |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 829 | sd = per_cpu(svm_data, me); |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 830 | if (!sd) { |
Borislav Petkov | 1f5b77f | 2012-10-20 20:20:04 +0200 | [diff] [blame] | 831 | pr_err("%s: svm_data is NULL on %d\n", __func__, me); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 832 | return -EINVAL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 833 | } |
| 834 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 835 | sd->asid_generation = 1; |
| 836 | sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1; |
| 837 | sd->next_asid = sd->max_asid + 1; |
Brijesh Singh | ed3cd23 | 2017-12-04 10:57:32 -0600 | [diff] [blame] | 838 | sd->min_asid = max_sev_asid + 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 839 | |
Thomas Garnier | 45fc875 | 2017-03-14 10:05:08 -0700 | [diff] [blame] | 840 | gdt = get_current_gdt_rw(); |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 841 | sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 842 | |
Alexander Graf | 9962d03 | 2008-11-25 20:17:02 +0100 | [diff] [blame] | 843 | wrmsrl(MSR_EFER, efer | EFER_SVME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 844 | |
Linus Torvalds | d031655 | 2009-12-14 09:58:24 -0800 | [diff] [blame] | 845 | 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] | 846 | |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 847 | if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) { |
| 848 | wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT); |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 849 | __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT); |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 850 | } |
| 851 | |
Boris Ostrovsky | 2b036c6 | 2012-01-09 14:00:35 -0500 | [diff] [blame] | 852 | |
| 853 | /* |
| 854 | * Get OSVW bits. |
| 855 | * |
| 856 | * Note that it is possible to have a system with mixed processor |
| 857 | * revisions and therefore different OSVW bits. If bits are not the same |
| 858 | * on different processors then choose the worst case (i.e. if erratum |
| 859 | * is present on one processor and not on another then assume that the |
| 860 | * erratum is present everywhere). |
| 861 | */ |
| 862 | if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) { |
| 863 | uint64_t len, status = 0; |
| 864 | int err; |
| 865 | |
| 866 | len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err); |
| 867 | if (!err) |
| 868 | status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS, |
| 869 | &err); |
| 870 | |
| 871 | if (err) |
| 872 | osvw_status = osvw_len = 0; |
| 873 | else { |
| 874 | if (len < osvw_len) |
| 875 | osvw_len = len; |
| 876 | osvw_status |= status; |
| 877 | osvw_status &= (1ULL << osvw_len) - 1; |
| 878 | } |
| 879 | } else |
| 880 | osvw_status = osvw_len = 0; |
| 881 | |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 882 | svm_init_erratum_383(); |
| 883 | |
Joerg Roedel | 1018faa | 2012-02-29 14:57:32 +0100 | [diff] [blame] | 884 | amd_pmu_enable_virt(); |
| 885 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 886 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 887 | } |
| 888 | |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 889 | static void svm_cpu_uninit(int cpu) |
| 890 | { |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 891 | 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] | 892 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 893 | if (!sd) |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 894 | return; |
| 895 | |
| 896 | per_cpu(svm_data, raw_smp_processor_id()) = NULL; |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 897 | kfree(sd->sev_vmcbs); |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 898 | __free_page(sd->save_area); |
| 899 | kfree(sd); |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 900 | } |
| 901 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 902 | static int svm_cpu_init(int cpu) |
| 903 | { |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 904 | struct svm_cpu_data *sd; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 905 | int r; |
| 906 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 907 | sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL); |
| 908 | if (!sd) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 909 | return -ENOMEM; |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 910 | sd->cpu = cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 911 | r = -ENOMEM; |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 912 | sd->save_area = alloc_page(GFP_KERNEL); |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 913 | if (!sd->save_area) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 914 | goto err_1; |
| 915 | |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 916 | if (svm_sev_enabled()) { |
| 917 | r = -ENOMEM; |
| 918 | sd->sev_vmcbs = kmalloc((max_sev_asid + 1) * sizeof(void *), GFP_KERNEL); |
| 919 | if (!sd->sev_vmcbs) |
| 920 | goto err_1; |
| 921 | } |
| 922 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 923 | per_cpu(svm_data, cpu) = sd; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 924 | |
| 925 | return 0; |
| 926 | |
| 927 | err_1: |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 928 | kfree(sd); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 929 | return r; |
| 930 | |
| 931 | } |
| 932 | |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 933 | static bool valid_msr_intercept(u32 index) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 934 | { |
| 935 | int i; |
| 936 | |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 937 | for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) |
| 938 | if (direct_access_msrs[i].index == index) |
| 939 | return true; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 940 | |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 941 | return false; |
| 942 | } |
| 943 | |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 944 | static bool msr_write_intercepted(struct kvm_vcpu *vcpu, unsigned msr) |
| 945 | { |
| 946 | u8 bit_write; |
| 947 | unsigned long tmp; |
| 948 | u32 offset; |
| 949 | u32 *msrpm; |
| 950 | |
| 951 | msrpm = is_guest_mode(vcpu) ? to_svm(vcpu)->nested.msrpm: |
| 952 | to_svm(vcpu)->msrpm; |
| 953 | |
| 954 | offset = svm_msrpm_offset(msr); |
| 955 | bit_write = 2 * (msr & 0x0f) + 1; |
| 956 | tmp = msrpm[offset]; |
| 957 | |
| 958 | BUG_ON(offset == MSR_INVALID); |
| 959 | |
| 960 | return !!test_bit(bit_write, &tmp); |
| 961 | } |
| 962 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 963 | static void set_msr_interception(u32 *msrpm, unsigned msr, |
| 964 | int read, int write) |
| 965 | { |
Joerg Roedel | 455716f | 2010-03-01 15:34:35 +0100 | [diff] [blame] | 966 | u8 bit_read, bit_write; |
| 967 | unsigned long tmp; |
| 968 | u32 offset; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 969 | |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 970 | /* |
| 971 | * If this warning triggers extend the direct_access_msrs list at the |
| 972 | * beginning of the file |
| 973 | */ |
| 974 | WARN_ON(!valid_msr_intercept(msr)); |
| 975 | |
Joerg Roedel | 455716f | 2010-03-01 15:34:35 +0100 | [diff] [blame] | 976 | offset = svm_msrpm_offset(msr); |
| 977 | bit_read = 2 * (msr & 0x0f); |
| 978 | bit_write = 2 * (msr & 0x0f) + 1; |
| 979 | tmp = msrpm[offset]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 980 | |
Joerg Roedel | 455716f | 2010-03-01 15:34:35 +0100 | [diff] [blame] | 981 | BUG_ON(offset == MSR_INVALID); |
| 982 | |
| 983 | read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp); |
| 984 | write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp); |
| 985 | |
| 986 | msrpm[offset] = tmp; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 987 | } |
| 988 | |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 989 | static void svm_vcpu_init_msrpm(u32 *msrpm) |
| 990 | { |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 991 | int i; |
| 992 | |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 993 | memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER)); |
| 994 | |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 995 | for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) { |
| 996 | if (!direct_access_msrs[i].always) |
| 997 | continue; |
| 998 | |
| 999 | set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1); |
| 1000 | } |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 1001 | } |
| 1002 | |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 1003 | static void add_msr_offset(u32 offset) |
| 1004 | { |
| 1005 | int i; |
| 1006 | |
| 1007 | for (i = 0; i < MSRPM_OFFSETS; ++i) { |
| 1008 | |
| 1009 | /* Offset already in list? */ |
| 1010 | if (msrpm_offsets[i] == offset) |
| 1011 | return; |
| 1012 | |
| 1013 | /* Slot used by another offset? */ |
| 1014 | if (msrpm_offsets[i] != MSR_INVALID) |
| 1015 | continue; |
| 1016 | |
| 1017 | /* Add offset to list */ |
| 1018 | msrpm_offsets[i] = offset; |
| 1019 | |
| 1020 | return; |
| 1021 | } |
| 1022 | |
| 1023 | /* |
| 1024 | * If this BUG triggers the msrpm_offsets table has an overflow. Just |
| 1025 | * increase MSRPM_OFFSETS in this case. |
| 1026 | */ |
| 1027 | BUG(); |
| 1028 | } |
| 1029 | |
| 1030 | static void init_msrpm_offsets(void) |
| 1031 | { |
| 1032 | int i; |
| 1033 | |
| 1034 | memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets)); |
| 1035 | |
| 1036 | for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) { |
| 1037 | u32 offset; |
| 1038 | |
| 1039 | offset = svm_msrpm_offset(direct_access_msrs[i].index); |
| 1040 | BUG_ON(offset == MSR_INVALID); |
| 1041 | |
| 1042 | add_msr_offset(offset); |
| 1043 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1044 | } |
| 1045 | |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 1046 | static void svm_enable_lbrv(struct vcpu_svm *svm) |
| 1047 | { |
| 1048 | u32 *msrpm = svm->msrpm; |
| 1049 | |
Janakarajan Natarajan | 0dc9211 | 2017-07-06 15:50:45 -0500 | [diff] [blame] | 1050 | svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK; |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 1051 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1); |
| 1052 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1); |
| 1053 | set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1); |
| 1054 | set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1); |
| 1055 | } |
| 1056 | |
| 1057 | static void svm_disable_lbrv(struct vcpu_svm *svm) |
| 1058 | { |
| 1059 | u32 *msrpm = svm->msrpm; |
| 1060 | |
Janakarajan Natarajan | 0dc9211 | 2017-07-06 15:50:45 -0500 | [diff] [blame] | 1061 | svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK; |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 1062 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0); |
| 1063 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0); |
| 1064 | set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0); |
| 1065 | set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0); |
| 1066 | } |
| 1067 | |
Ladi Prosek | 4aebd0e | 2017-06-21 09:06:57 +0200 | [diff] [blame] | 1068 | static void disable_nmi_singlestep(struct vcpu_svm *svm) |
| 1069 | { |
| 1070 | svm->nmi_singlestep = false; |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 1071 | |
Ladi Prosek | ab2f4d73 | 2017-06-21 09:06:58 +0200 | [diff] [blame] | 1072 | if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) { |
| 1073 | /* Clear our flags if they were not set by the guest */ |
| 1074 | if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF)) |
| 1075 | svm->vmcb->save.rflags &= ~X86_EFLAGS_TF; |
| 1076 | if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF)) |
| 1077 | svm->vmcb->save.rflags &= ~X86_EFLAGS_RF; |
| 1078 | } |
Ladi Prosek | 4aebd0e | 2017-06-21 09:06:57 +0200 | [diff] [blame] | 1079 | } |
| 1080 | |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1081 | /* Note: |
| 1082 | * This hash table is used to map VM_ID to a struct kvm_arch, |
| 1083 | * when handling AMD IOMMU GALOG notification to schedule in |
| 1084 | * a particular vCPU. |
| 1085 | */ |
| 1086 | #define SVM_VM_DATA_HASH_BITS 8 |
David Hildenbrand | 681bcea | 2017-01-24 22:21:16 +0100 | [diff] [blame] | 1087 | static DEFINE_HASHTABLE(svm_vm_data_hash, SVM_VM_DATA_HASH_BITS); |
Denys Vlasenko | 3f0d4db | 2017-08-11 22:11:58 +0200 | [diff] [blame] | 1088 | static u32 next_vm_id = 0; |
| 1089 | static bool next_vm_id_wrapped = 0; |
David Hildenbrand | 681bcea | 2017-01-24 22:21:16 +0100 | [diff] [blame] | 1090 | static DEFINE_SPINLOCK(svm_vm_data_hash_lock); |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1091 | |
| 1092 | /* Note: |
| 1093 | * This function is called from IOMMU driver to notify |
| 1094 | * SVM to schedule in a particular vCPU of a particular VM. |
| 1095 | */ |
| 1096 | static int avic_ga_log_notifier(u32 ga_tag) |
| 1097 | { |
| 1098 | unsigned long flags; |
| 1099 | struct kvm_arch *ka = NULL; |
| 1100 | struct kvm_vcpu *vcpu = NULL; |
| 1101 | u32 vm_id = AVIC_GATAG_TO_VMID(ga_tag); |
| 1102 | u32 vcpu_id = AVIC_GATAG_TO_VCPUID(ga_tag); |
| 1103 | |
| 1104 | pr_debug("SVM: %s: vm_id=%#x, vcpu_id=%#x\n", __func__, vm_id, vcpu_id); |
| 1105 | |
| 1106 | spin_lock_irqsave(&svm_vm_data_hash_lock, flags); |
| 1107 | hash_for_each_possible(svm_vm_data_hash, ka, hnode, vm_id) { |
| 1108 | struct kvm *kvm = container_of(ka, struct kvm, arch); |
| 1109 | struct kvm_arch *vm_data = &kvm->arch; |
| 1110 | |
| 1111 | if (vm_data->avic_vm_id != vm_id) |
| 1112 | continue; |
| 1113 | vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id); |
| 1114 | break; |
| 1115 | } |
| 1116 | spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags); |
| 1117 | |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1118 | /* Note: |
| 1119 | * At this point, the IOMMU should have already set the pending |
| 1120 | * bit in the vAPIC backing page. So, we just need to schedule |
| 1121 | * in the vcpu. |
| 1122 | */ |
Paolo Bonzini | 1cf5358 | 2017-10-10 12:51:56 +0200 | [diff] [blame] | 1123 | if (vcpu) |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1124 | kvm_vcpu_wake_up(vcpu); |
| 1125 | |
| 1126 | return 0; |
| 1127 | } |
| 1128 | |
Brijesh Singh | e9df094 | 2017-12-04 10:57:33 -0600 | [diff] [blame] | 1129 | static __init int sev_hardware_setup(void) |
| 1130 | { |
| 1131 | struct sev_user_data_status *status; |
| 1132 | int rc; |
| 1133 | |
| 1134 | /* Maximum number of encrypted guests supported simultaneously */ |
| 1135 | max_sev_asid = cpuid_ecx(0x8000001F); |
| 1136 | |
| 1137 | if (!max_sev_asid) |
| 1138 | return 1; |
| 1139 | |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1140 | /* Minimum ASID value that should be used for SEV guest */ |
| 1141 | min_sev_asid = cpuid_edx(0x8000001F); |
| 1142 | |
| 1143 | /* Initialize SEV ASID bitmap */ |
| 1144 | sev_asid_bitmap = kcalloc(BITS_TO_LONGS(max_sev_asid), |
| 1145 | sizeof(unsigned long), GFP_KERNEL); |
| 1146 | if (!sev_asid_bitmap) |
| 1147 | return 1; |
| 1148 | |
Brijesh Singh | e9df094 | 2017-12-04 10:57:33 -0600 | [diff] [blame] | 1149 | status = kmalloc(sizeof(*status), GFP_KERNEL); |
| 1150 | if (!status) |
| 1151 | return 1; |
| 1152 | |
| 1153 | /* |
| 1154 | * Check SEV platform status. |
| 1155 | * |
| 1156 | * PLATFORM_STATUS can be called in any state, if we failed to query |
| 1157 | * the PLATFORM status then either PSP firmware does not support SEV |
| 1158 | * feature or SEV firmware is dead. |
| 1159 | */ |
| 1160 | rc = sev_platform_status(status, NULL); |
| 1161 | if (rc) |
| 1162 | goto err; |
| 1163 | |
| 1164 | pr_info("SEV supported\n"); |
| 1165 | |
| 1166 | err: |
| 1167 | kfree(status); |
| 1168 | return rc; |
| 1169 | } |
| 1170 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1171 | static __init int svm_hardware_setup(void) |
| 1172 | { |
| 1173 | int cpu; |
| 1174 | struct page *iopm_pages; |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 1175 | void *iopm_va; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1176 | int r; |
| 1177 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1178 | iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER); |
| 1179 | |
| 1180 | if (!iopm_pages) |
| 1181 | return -ENOMEM; |
Anthony Liguori | c868133 | 2007-04-30 09:48:11 +0300 | [diff] [blame] | 1182 | |
| 1183 | iopm_va = page_address(iopm_pages); |
| 1184 | memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1185 | iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT; |
| 1186 | |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 1187 | init_msrpm_offsets(); |
| 1188 | |
Joerg Roedel | 50a37eb | 2008-01-31 14:57:38 +0100 | [diff] [blame] | 1189 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 1190 | kvm_enable_efer_bits(EFER_NX); |
| 1191 | |
Alexander Graf | 1b2fd70 | 2009-02-02 16:23:51 +0100 | [diff] [blame] | 1192 | if (boot_cpu_has(X86_FEATURE_FXSR_OPT)) |
| 1193 | kvm_enable_efer_bits(EFER_FFXSR); |
| 1194 | |
Joerg Roedel | 92a1f12 | 2011-03-25 09:44:51 +0100 | [diff] [blame] | 1195 | if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) { |
Joerg Roedel | 92a1f12 | 2011-03-25 09:44:51 +0100 | [diff] [blame] | 1196 | kvm_has_tsc_control = true; |
Haozhong Zhang | bc9b961 | 2015-10-20 15:39:01 +0800 | [diff] [blame] | 1197 | kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX; |
| 1198 | kvm_tsc_scaling_ratio_frac_bits = 32; |
Joerg Roedel | 92a1f12 | 2011-03-25 09:44:51 +0100 | [diff] [blame] | 1199 | } |
| 1200 | |
Alexander Graf | 236de05 | 2008-11-25 20:17:10 +0100 | [diff] [blame] | 1201 | if (nested) { |
| 1202 | printk(KERN_INFO "kvm: Nested Virtualization enabled\n"); |
Joerg Roedel | eec4b14 | 2010-05-05 16:04:44 +0200 | [diff] [blame] | 1203 | kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE); |
Alexander Graf | 236de05 | 2008-11-25 20:17:10 +0100 | [diff] [blame] | 1204 | } |
| 1205 | |
Brijesh Singh | e9df094 | 2017-12-04 10:57:33 -0600 | [diff] [blame] | 1206 | if (sev) { |
| 1207 | if (boot_cpu_has(X86_FEATURE_SEV) && |
| 1208 | IS_ENABLED(CONFIG_KVM_AMD_SEV)) { |
| 1209 | r = sev_hardware_setup(); |
| 1210 | if (r) |
| 1211 | sev = false; |
| 1212 | } else { |
| 1213 | sev = false; |
| 1214 | } |
| 1215 | } |
| 1216 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 1217 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1218 | r = svm_cpu_init(cpu); |
| 1219 | if (r) |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 1220 | goto err; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1221 | } |
Joerg Roedel | 33bd6a0 | 2008-02-07 13:47:38 +0100 | [diff] [blame] | 1222 | |
Avi Kivity | 2a6b20b | 2010-11-09 16:15:42 +0200 | [diff] [blame] | 1223 | if (!boot_cpu_has(X86_FEATURE_NPT)) |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 1224 | npt_enabled = false; |
| 1225 | |
Joerg Roedel | 6c7dac7 | 2008-02-07 13:47:40 +0100 | [diff] [blame] | 1226 | if (npt_enabled && !npt) { |
| 1227 | printk(KERN_INFO "kvm: Nested Paging disabled\n"); |
| 1228 | npt_enabled = false; |
| 1229 | } |
| 1230 | |
Joerg Roedel | 1855267 | 2008-02-07 13:47:41 +0100 | [diff] [blame] | 1231 | if (npt_enabled) { |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 1232 | printk(KERN_INFO "kvm: Nested Paging enabled\n"); |
Joerg Roedel | 1855267 | 2008-02-07 13:47:41 +0100 | [diff] [blame] | 1233 | kvm_enable_tdp(); |
Joerg Roedel | 5f4cb66 | 2008-07-14 20:36:36 +0200 | [diff] [blame] | 1234 | } else |
| 1235 | kvm_disable_tdp(); |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 1236 | |
Suravee Suthikulpanit | 5b8abf1 | 2016-06-15 17:24:36 -0500 | [diff] [blame] | 1237 | if (avic) { |
| 1238 | if (!npt_enabled || |
| 1239 | !boot_cpu_has(X86_FEATURE_AVIC) || |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1240 | !IS_ENABLED(CONFIG_X86_LOCAL_APIC)) { |
Suravee Suthikulpanit | 5b8abf1 | 2016-06-15 17:24:36 -0500 | [diff] [blame] | 1241 | avic = false; |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1242 | } else { |
Suravee Suthikulpanit | 5b8abf1 | 2016-06-15 17:24:36 -0500 | [diff] [blame] | 1243 | pr_info("AVIC enabled\n"); |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1244 | |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1245 | amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier); |
| 1246 | } |
Suravee Suthikulpanit | 5b8abf1 | 2016-06-15 17:24:36 -0500 | [diff] [blame] | 1247 | } |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1248 | |
Janakarajan Natarajan | 89c8a49 | 2017-07-06 15:50:47 -0500 | [diff] [blame] | 1249 | if (vls) { |
| 1250 | if (!npt_enabled || |
Borislav Petkov | 5442c26 | 2017-08-01 20:55:52 +0200 | [diff] [blame] | 1251 | !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) || |
Janakarajan Natarajan | 89c8a49 | 2017-07-06 15:50:47 -0500 | [diff] [blame] | 1252 | !IS_ENABLED(CONFIG_X86_64)) { |
| 1253 | vls = false; |
| 1254 | } else { |
| 1255 | pr_info("Virtual VMLOAD VMSAVE supported\n"); |
| 1256 | } |
| 1257 | } |
| 1258 | |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 1259 | if (vgif) { |
| 1260 | if (!boot_cpu_has(X86_FEATURE_VGIF)) |
| 1261 | vgif = false; |
| 1262 | else |
| 1263 | pr_info("Virtual GIF supported\n"); |
| 1264 | } |
| 1265 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1266 | return 0; |
| 1267 | |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 1268 | err: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1269 | __free_pages(iopm_pages, IOPM_ALLOC_ORDER); |
| 1270 | iopm_base = 0; |
| 1271 | return r; |
| 1272 | } |
| 1273 | |
| 1274 | static __exit void svm_hardware_unsetup(void) |
| 1275 | { |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 1276 | int cpu; |
| 1277 | |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1278 | if (svm_sev_enabled()) |
| 1279 | kfree(sev_asid_bitmap); |
| 1280 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 1281 | for_each_possible_cpu(cpu) |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 1282 | svm_cpu_uninit(cpu); |
| 1283 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1284 | __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER); |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 1285 | iopm_base = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | static void init_seg(struct vmcb_seg *seg) |
| 1289 | { |
| 1290 | seg->selector = 0; |
| 1291 | seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 1292 | SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1293 | seg->limit = 0xffff; |
| 1294 | seg->base = 0; |
| 1295 | } |
| 1296 | |
| 1297 | static void init_sys_seg(struct vmcb_seg *seg, uint32_t type) |
| 1298 | { |
| 1299 | seg->selector = 0; |
| 1300 | seg->attrib = SVM_SELECTOR_P_MASK | type; |
| 1301 | seg->limit = 0xffff; |
| 1302 | seg->base = 0; |
| 1303 | } |
| 1304 | |
Zachary Amsden | f4e1b3c | 2010-08-19 22:07:16 -1000 | [diff] [blame] | 1305 | static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
| 1306 | { |
| 1307 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1308 | u64 g_tsc_offset = 0; |
| 1309 | |
Joerg Roedel | 2030753 | 2010-11-29 17:51:48 +0100 | [diff] [blame] | 1310 | if (is_guest_mode(vcpu)) { |
Zachary Amsden | f4e1b3c | 2010-08-19 22:07:16 -1000 | [diff] [blame] | 1311 | g_tsc_offset = svm->vmcb->control.tsc_offset - |
| 1312 | svm->nested.hsave->control.tsc_offset; |
| 1313 | svm->nested.hsave->control.tsc_offset = offset; |
Yoshihiro YUNOMAE | 489223e | 2013-06-12 16:43:44 +0900 | [diff] [blame] | 1314 | } else |
| 1315 | trace_kvm_write_tsc_offset(vcpu->vcpu_id, |
| 1316 | svm->vmcb->control.tsc_offset, |
| 1317 | offset); |
Zachary Amsden | f4e1b3c | 2010-08-19 22:07:16 -1000 | [diff] [blame] | 1318 | |
| 1319 | svm->vmcb->control.tsc_offset = offset + g_tsc_offset; |
Joerg Roedel | 116a0a2 | 2010-12-03 11:45:49 +0100 | [diff] [blame] | 1320 | |
| 1321 | mark_dirty(svm->vmcb, VMCB_INTERCEPTS); |
Zachary Amsden | f4e1b3c | 2010-08-19 22:07:16 -1000 | [diff] [blame] | 1322 | } |
| 1323 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1324 | static void avic_init_vmcb(struct vcpu_svm *svm) |
| 1325 | { |
| 1326 | struct vmcb *vmcb = svm->vmcb; |
| 1327 | struct kvm_arch *vm_data = &svm->vcpu.kvm->arch; |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 1328 | phys_addr_t bpa = __sme_set(page_to_phys(svm->avic_backing_page)); |
| 1329 | phys_addr_t lpa = __sme_set(page_to_phys(vm_data->avic_logical_id_table_page)); |
| 1330 | phys_addr_t ppa = __sme_set(page_to_phys(vm_data->avic_physical_id_table_page)); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1331 | |
| 1332 | vmcb->control.avic_backing_page = bpa & AVIC_HPA_MASK; |
| 1333 | vmcb->control.avic_logical_id = lpa & AVIC_HPA_MASK; |
| 1334 | vmcb->control.avic_physical_id = ppa & AVIC_HPA_MASK; |
| 1335 | vmcb->control.avic_physical_id |= AVIC_MAX_PHYSICAL_ID_COUNT; |
| 1336 | vmcb->control.int_ctl |= AVIC_ENABLE_MASK; |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1337 | } |
| 1338 | |
Paolo Bonzini | 5690891 | 2015-10-19 11:30:19 +0200 | [diff] [blame] | 1339 | static void init_vmcb(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1340 | { |
Joerg Roedel | e6101a9 | 2008-02-13 18:58:45 +0100 | [diff] [blame] | 1341 | struct vmcb_control_area *control = &svm->vmcb->control; |
| 1342 | struct vmcb_save_area *save = &svm->vmcb->save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1343 | |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 1344 | svm->vcpu.arch.hflags = 0; |
Avi Kivity | bff7827 | 2010-01-07 13:16:08 +0200 | [diff] [blame] | 1345 | |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 1346 | set_cr_intercept(svm, INTERCEPT_CR0_READ); |
| 1347 | set_cr_intercept(svm, INTERCEPT_CR3_READ); |
| 1348 | set_cr_intercept(svm, INTERCEPT_CR4_READ); |
| 1349 | set_cr_intercept(svm, INTERCEPT_CR0_WRITE); |
| 1350 | set_cr_intercept(svm, INTERCEPT_CR3_WRITE); |
| 1351 | set_cr_intercept(svm, INTERCEPT_CR4_WRITE); |
Suravee Suthikulpanit | 3bbf356 | 2016-05-04 14:09:51 -0500 | [diff] [blame] | 1352 | if (!kvm_vcpu_apicv_active(&svm->vcpu)) |
| 1353 | set_cr_intercept(svm, INTERCEPT_CR8_WRITE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1354 | |
Paolo Bonzini | 5315c71 | 2014-03-03 13:08:29 +0100 | [diff] [blame] | 1355 | set_dr_intercepts(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1356 | |
Joerg Roedel | 18c918c | 2010-11-30 18:03:59 +0100 | [diff] [blame] | 1357 | set_exception_intercept(svm, PF_VECTOR); |
| 1358 | set_exception_intercept(svm, UD_VECTOR); |
| 1359 | set_exception_intercept(svm, MC_VECTOR); |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 1360 | set_exception_intercept(svm, AC_VECTOR); |
Paolo Bonzini | cbdb967 | 2015-11-10 09:14:39 +0100 | [diff] [blame] | 1361 | set_exception_intercept(svm, DB_VECTOR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1362 | |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 1363 | set_intercept(svm, INTERCEPT_INTR); |
| 1364 | set_intercept(svm, INTERCEPT_NMI); |
| 1365 | set_intercept(svm, INTERCEPT_SMI); |
| 1366 | set_intercept(svm, INTERCEPT_SELECTIVE_CR0); |
Avi Kivity | 332b56e | 2011-11-10 14:57:24 +0200 | [diff] [blame] | 1367 | set_intercept(svm, INTERCEPT_RDPMC); |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 1368 | set_intercept(svm, INTERCEPT_CPUID); |
| 1369 | set_intercept(svm, INTERCEPT_INVD); |
| 1370 | set_intercept(svm, INTERCEPT_HLT); |
| 1371 | set_intercept(svm, INTERCEPT_INVLPG); |
| 1372 | set_intercept(svm, INTERCEPT_INVLPGA); |
| 1373 | set_intercept(svm, INTERCEPT_IOIO_PROT); |
| 1374 | set_intercept(svm, INTERCEPT_MSR_PROT); |
| 1375 | set_intercept(svm, INTERCEPT_TASK_SWITCH); |
| 1376 | set_intercept(svm, INTERCEPT_SHUTDOWN); |
| 1377 | set_intercept(svm, INTERCEPT_VMRUN); |
| 1378 | set_intercept(svm, INTERCEPT_VMMCALL); |
| 1379 | set_intercept(svm, INTERCEPT_VMLOAD); |
| 1380 | set_intercept(svm, INTERCEPT_VMSAVE); |
| 1381 | set_intercept(svm, INTERCEPT_STGI); |
| 1382 | set_intercept(svm, INTERCEPT_CLGI); |
| 1383 | set_intercept(svm, INTERCEPT_SKINIT); |
| 1384 | set_intercept(svm, INTERCEPT_WBINVD); |
Joerg Roedel | 81dd35d | 2010-12-07 17:15:06 +0100 | [diff] [blame] | 1385 | set_intercept(svm, INTERCEPT_XSETBV); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1386 | |
Michael S. Tsirkin | 668fffa | 2017-04-21 12:27:17 +0200 | [diff] [blame] | 1387 | if (!kvm_mwait_in_guest()) { |
| 1388 | set_intercept(svm, INTERCEPT_MONITOR); |
| 1389 | set_intercept(svm, INTERCEPT_MWAIT); |
| 1390 | } |
| 1391 | |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 1392 | control->iopm_base_pa = __sme_set(iopm_base); |
| 1393 | control->msrpm_base_pa = __sme_set(__pa(svm->msrpm)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1394 | control->int_ctl = V_INTR_MASKING_MASK; |
| 1395 | |
| 1396 | init_seg(&save->es); |
| 1397 | init_seg(&save->ss); |
| 1398 | init_seg(&save->ds); |
| 1399 | init_seg(&save->fs); |
| 1400 | init_seg(&save->gs); |
| 1401 | |
| 1402 | save->cs.selector = 0xf000; |
Paolo Bonzini | 04b6683 | 2013-03-19 16:30:26 +0100 | [diff] [blame] | 1403 | save->cs.base = 0xffff0000; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1404 | /* Executable/Readable Code Segment */ |
| 1405 | save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK | |
| 1406 | SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK; |
| 1407 | save->cs.limit = 0xffff; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1408 | |
| 1409 | save->gdtr.limit = 0xffff; |
| 1410 | save->idtr.limit = 0xffff; |
| 1411 | |
| 1412 | init_sys_seg(&save->ldtr, SEG_TYPE_LDT); |
| 1413 | init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16); |
| 1414 | |
Paolo Bonzini | 5690891 | 2015-10-19 11:30:19 +0200 | [diff] [blame] | 1415 | svm_set_efer(&svm->vcpu, 0); |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1416 | save->dr6 = 0xffff0ff0; |
Avi Kivity | f6e7847 | 2010-08-02 15:30:20 +0300 | [diff] [blame] | 1417 | kvm_set_rflags(&svm->vcpu, 2); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1418 | save->rip = 0x0000fff0; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1419 | svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1420 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 1421 | /* |
Eduardo Habkost | 18fa000 | 2009-10-24 02:49:59 -0200 | [diff] [blame] | 1422 | * 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] | 1423 | * It also updates the guest-visible cr0 value. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1424 | */ |
Paolo Bonzini | 79a8059 | 2015-09-21 07:46:55 +0200 | [diff] [blame] | 1425 | 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] | 1426 | kvm_mmu_reset_context(&svm->vcpu); |
Eduardo Habkost | 18fa000 | 2009-10-24 02:49:59 -0200 | [diff] [blame] | 1427 | |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 1428 | save->cr4 = X86_CR4_PAE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1429 | /* rdx = ?? */ |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 1430 | |
| 1431 | if (npt_enabled) { |
| 1432 | /* Setup VMCB for Nested Paging */ |
Tom Lendacky | cea3a19 | 2017-12-04 10:57:24 -0600 | [diff] [blame] | 1433 | control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE; |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 1434 | clr_intercept(svm, INTERCEPT_INVLPG); |
Joerg Roedel | 18c918c | 2010-11-30 18:03:59 +0100 | [diff] [blame] | 1435 | clr_exception_intercept(svm, PF_VECTOR); |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 1436 | clr_cr_intercept(svm, INTERCEPT_CR3_READ); |
| 1437 | clr_cr_intercept(svm, INTERCEPT_CR3_WRITE); |
Radim Krčmář | 7454570 | 2015-04-27 15:11:25 +0200 | [diff] [blame] | 1438 | save->g_pat = svm->vcpu.arch.pat; |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 1439 | save->cr3 = 0; |
| 1440 | save->cr4 = 0; |
| 1441 | } |
Joerg Roedel | f40f6a4 | 2010-12-03 15:25:15 +0100 | [diff] [blame] | 1442 | svm->asid_generation = 0; |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 1443 | |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 1444 | svm->nested.vmcb = 0; |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 1445 | svm->vcpu.arch.hflags = 0; |
| 1446 | |
Avi Kivity | 2a6b20b | 2010-11-09 16:15:42 +0200 | [diff] [blame] | 1447 | if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) { |
Mark Langsdorf | 565d099 | 2009-10-06 14:25:02 -0500 | [diff] [blame] | 1448 | control->pause_filter_count = 3000; |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 1449 | set_intercept(svm, INTERCEPT_PAUSE); |
Mark Langsdorf | 565d099 | 2009-10-06 14:25:02 -0500 | [diff] [blame] | 1450 | } |
| 1451 | |
Suravee Suthikulpanit | 67034bb | 2017-09-12 10:42:42 -0500 | [diff] [blame] | 1452 | if (kvm_vcpu_apicv_active(&svm->vcpu)) |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1453 | avic_init_vmcb(svm); |
| 1454 | |
Janakarajan Natarajan | 89c8a49 | 2017-07-06 15:50:47 -0500 | [diff] [blame] | 1455 | /* |
| 1456 | * If hardware supports Virtual VMLOAD VMSAVE then enable it |
| 1457 | * in VMCB and clear intercepts to avoid #VMEXIT. |
| 1458 | */ |
| 1459 | if (vls) { |
| 1460 | clr_intercept(svm, INTERCEPT_VMLOAD); |
| 1461 | clr_intercept(svm, INTERCEPT_VMSAVE); |
| 1462 | svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK; |
| 1463 | } |
| 1464 | |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 1465 | if (vgif) { |
| 1466 | clr_intercept(svm, INTERCEPT_STGI); |
| 1467 | clr_intercept(svm, INTERCEPT_CLGI); |
| 1468 | svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK; |
| 1469 | } |
| 1470 | |
Brijesh Singh | 35c6f649 | 2017-12-04 10:57:39 -0600 | [diff] [blame] | 1471 | if (sev_guest(svm->vcpu.kvm)) { |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1472 | svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE; |
Brijesh Singh | 35c6f649 | 2017-12-04 10:57:39 -0600 | [diff] [blame] | 1473 | clr_exception_intercept(svm, UD_VECTOR); |
| 1474 | } |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1475 | |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 1476 | mark_all_dirty(svm->vmcb); |
| 1477 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 1478 | enable_gif(svm); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1479 | |
| 1480 | } |
| 1481 | |
Dan Carpenter | d3e7dec | 2017-05-18 10:38:53 +0300 | [diff] [blame] | 1482 | static u64 *avic_get_physical_id_entry(struct kvm_vcpu *vcpu, |
| 1483 | unsigned int index) |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1484 | { |
| 1485 | u64 *avic_physical_id_table; |
| 1486 | struct kvm_arch *vm_data = &vcpu->kvm->arch; |
| 1487 | |
| 1488 | if (index >= AVIC_MAX_PHYSICAL_ID_COUNT) |
| 1489 | return NULL; |
| 1490 | |
| 1491 | avic_physical_id_table = page_address(vm_data->avic_physical_id_table_page); |
| 1492 | |
| 1493 | return &avic_physical_id_table[index]; |
| 1494 | } |
| 1495 | |
| 1496 | /** |
| 1497 | * Note: |
| 1498 | * AVIC hardware walks the nested page table to check permissions, |
| 1499 | * but does not use the SPA address specified in the leaf page |
| 1500 | * table entry since it uses address in the AVIC_BACKING_PAGE pointer |
| 1501 | * field of the VMCB. Therefore, we set up the |
| 1502 | * APIC_ACCESS_PAGE_PRIVATE_MEMSLOT (4KB) here. |
| 1503 | */ |
| 1504 | static int avic_init_access_page(struct kvm_vcpu *vcpu) |
| 1505 | { |
| 1506 | struct kvm *kvm = vcpu->kvm; |
| 1507 | int ret; |
| 1508 | |
| 1509 | if (kvm->arch.apic_access_page_done) |
| 1510 | return 0; |
| 1511 | |
| 1512 | ret = x86_set_memory_region(kvm, |
| 1513 | APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, |
| 1514 | APIC_DEFAULT_PHYS_BASE, |
| 1515 | PAGE_SIZE); |
| 1516 | if (ret) |
| 1517 | return ret; |
| 1518 | |
| 1519 | kvm->arch.apic_access_page_done = true; |
| 1520 | return 0; |
| 1521 | } |
| 1522 | |
| 1523 | static int avic_init_backing_page(struct kvm_vcpu *vcpu) |
| 1524 | { |
| 1525 | int ret; |
| 1526 | u64 *entry, new_entry; |
| 1527 | int id = vcpu->vcpu_id; |
| 1528 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1529 | |
| 1530 | ret = avic_init_access_page(vcpu); |
| 1531 | if (ret) |
| 1532 | return ret; |
| 1533 | |
| 1534 | if (id >= AVIC_MAX_PHYSICAL_ID_COUNT) |
| 1535 | return -EINVAL; |
| 1536 | |
| 1537 | if (!svm->vcpu.arch.apic->regs) |
| 1538 | return -EINVAL; |
| 1539 | |
| 1540 | svm->avic_backing_page = virt_to_page(svm->vcpu.arch.apic->regs); |
| 1541 | |
| 1542 | /* Setting AVIC backing page address in the phy APIC ID table */ |
| 1543 | entry = avic_get_physical_id_entry(vcpu, id); |
| 1544 | if (!entry) |
| 1545 | return -EINVAL; |
| 1546 | |
| 1547 | new_entry = READ_ONCE(*entry); |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 1548 | new_entry = __sme_set((page_to_phys(svm->avic_backing_page) & |
| 1549 | AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK) | |
| 1550 | AVIC_PHYSICAL_ID_ENTRY_VALID_MASK); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1551 | WRITE_ONCE(*entry, new_entry); |
| 1552 | |
| 1553 | svm->avic_physical_id_cache = entry; |
| 1554 | |
| 1555 | return 0; |
| 1556 | } |
| 1557 | |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1558 | static void __sev_asid_free(int asid) |
| 1559 | { |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1560 | struct svm_cpu_data *sd; |
| 1561 | int cpu, pos; |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1562 | |
| 1563 | pos = asid - 1; |
| 1564 | clear_bit(pos, sev_asid_bitmap); |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1565 | |
| 1566 | for_each_possible_cpu(cpu) { |
| 1567 | sd = per_cpu(svm_data, cpu); |
| 1568 | sd->sev_vmcbs[pos] = NULL; |
| 1569 | } |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | static void sev_asid_free(struct kvm *kvm) |
| 1573 | { |
| 1574 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
| 1575 | |
| 1576 | __sev_asid_free(sev->asid); |
| 1577 | } |
| 1578 | |
Brijesh Singh | 59414c9 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 1579 | static void sev_unbind_asid(struct kvm *kvm, unsigned int handle) |
| 1580 | { |
| 1581 | struct sev_data_decommission *decommission; |
| 1582 | struct sev_data_deactivate *data; |
| 1583 | |
| 1584 | if (!handle) |
| 1585 | return; |
| 1586 | |
| 1587 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 1588 | if (!data) |
| 1589 | return; |
| 1590 | |
| 1591 | /* deactivate handle */ |
| 1592 | data->handle = handle; |
| 1593 | sev_guest_deactivate(data, NULL); |
| 1594 | |
| 1595 | wbinvd_on_all_cpus(); |
| 1596 | sev_guest_df_flush(NULL); |
| 1597 | kfree(data); |
| 1598 | |
| 1599 | decommission = kzalloc(sizeof(*decommission), GFP_KERNEL); |
| 1600 | if (!decommission) |
| 1601 | return; |
| 1602 | |
| 1603 | /* decommission handle */ |
| 1604 | decommission->handle = handle; |
| 1605 | sev_guest_decommission(decommission, NULL); |
| 1606 | |
| 1607 | kfree(decommission); |
| 1608 | } |
| 1609 | |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 1610 | static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr, |
| 1611 | unsigned long ulen, unsigned long *n, |
| 1612 | int write) |
| 1613 | { |
| 1614 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
| 1615 | unsigned long npages, npinned, size; |
| 1616 | unsigned long locked, lock_limit; |
| 1617 | struct page **pages; |
| 1618 | int first, last; |
| 1619 | |
| 1620 | /* Calculate number of pages. */ |
| 1621 | first = (uaddr & PAGE_MASK) >> PAGE_SHIFT; |
| 1622 | last = ((uaddr + ulen - 1) & PAGE_MASK) >> PAGE_SHIFT; |
| 1623 | npages = (last - first + 1); |
| 1624 | |
| 1625 | locked = sev->pages_locked + npages; |
| 1626 | lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 1627 | if (locked > lock_limit && !capable(CAP_IPC_LOCK)) { |
| 1628 | pr_err("SEV: %lu locked pages exceed the lock limit of %lu.\n", locked, lock_limit); |
| 1629 | return NULL; |
| 1630 | } |
| 1631 | |
| 1632 | /* Avoid using vmalloc for smaller buffers. */ |
| 1633 | size = npages * sizeof(struct page *); |
| 1634 | if (size > PAGE_SIZE) |
| 1635 | pages = vmalloc(size); |
| 1636 | else |
| 1637 | pages = kmalloc(size, GFP_KERNEL); |
| 1638 | |
| 1639 | if (!pages) |
| 1640 | return NULL; |
| 1641 | |
| 1642 | /* Pin the user virtual address. */ |
| 1643 | npinned = get_user_pages_fast(uaddr, npages, write ? FOLL_WRITE : 0, pages); |
| 1644 | if (npinned != npages) { |
| 1645 | pr_err("SEV: Failure locking %lu pages.\n", npages); |
| 1646 | goto err; |
| 1647 | } |
| 1648 | |
| 1649 | *n = npages; |
| 1650 | sev->pages_locked = locked; |
| 1651 | |
| 1652 | return pages; |
| 1653 | |
| 1654 | err: |
| 1655 | if (npinned > 0) |
| 1656 | release_pages(pages, npinned); |
| 1657 | |
| 1658 | kvfree(pages); |
| 1659 | return NULL; |
| 1660 | } |
| 1661 | |
| 1662 | static void sev_unpin_memory(struct kvm *kvm, struct page **pages, |
| 1663 | unsigned long npages) |
| 1664 | { |
| 1665 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
| 1666 | |
| 1667 | release_pages(pages, npages); |
| 1668 | kvfree(pages); |
| 1669 | sev->pages_locked -= npages; |
| 1670 | } |
| 1671 | |
| 1672 | static void sev_clflush_pages(struct page *pages[], unsigned long npages) |
| 1673 | { |
| 1674 | uint8_t *page_virtual; |
| 1675 | unsigned long i; |
| 1676 | |
| 1677 | if (npages == 0 || pages == NULL) |
| 1678 | return; |
| 1679 | |
| 1680 | for (i = 0; i < npages; i++) { |
| 1681 | page_virtual = kmap_atomic(pages[i]); |
| 1682 | clflush_cache_range(page_virtual, PAGE_SIZE); |
| 1683 | kunmap_atomic(page_virtual); |
| 1684 | } |
| 1685 | } |
| 1686 | |
Brijesh Singh | 1e80fdc | 2017-12-04 10:57:38 -0600 | [diff] [blame] | 1687 | static void __unregister_enc_region_locked(struct kvm *kvm, |
| 1688 | struct enc_region *region) |
| 1689 | { |
| 1690 | /* |
| 1691 | * The guest may change the memory encryption attribute from C=0 -> C=1 |
| 1692 | * or vice versa for this memory range. Lets make sure caches are |
| 1693 | * flushed to ensure that guest data gets written into memory with |
| 1694 | * correct C-bit. |
| 1695 | */ |
| 1696 | sev_clflush_pages(region->pages, region->npages); |
| 1697 | |
| 1698 | sev_unpin_memory(kvm, region->pages, region->npages); |
| 1699 | list_del(®ion->list); |
| 1700 | kfree(region); |
| 1701 | } |
| 1702 | |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1703 | static void sev_vm_destroy(struct kvm *kvm) |
| 1704 | { |
Brijesh Singh | 59414c9 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 1705 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
Brijesh Singh | 1e80fdc | 2017-12-04 10:57:38 -0600 | [diff] [blame] | 1706 | struct list_head *head = &sev->regions_list; |
| 1707 | struct list_head *pos, *q; |
Brijesh Singh | 59414c9 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 1708 | |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1709 | if (!sev_guest(kvm)) |
| 1710 | return; |
| 1711 | |
Brijesh Singh | 1e80fdc | 2017-12-04 10:57:38 -0600 | [diff] [blame] | 1712 | mutex_lock(&kvm->lock); |
| 1713 | |
| 1714 | /* |
| 1715 | * if userspace was terminated before unregistering the memory regions |
| 1716 | * then lets unpin all the registered memory. |
| 1717 | */ |
| 1718 | if (!list_empty(head)) { |
| 1719 | list_for_each_safe(pos, q, head) { |
| 1720 | __unregister_enc_region_locked(kvm, |
| 1721 | list_entry(pos, struct enc_region, list)); |
| 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | mutex_unlock(&kvm->lock); |
| 1726 | |
Brijesh Singh | 59414c9 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 1727 | sev_unbind_asid(kvm, sev->handle); |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1728 | sev_asid_free(kvm); |
| 1729 | } |
| 1730 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1731 | static void avic_vm_destroy(struct kvm *kvm) |
| 1732 | { |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1733 | unsigned long flags; |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1734 | struct kvm_arch *vm_data = &kvm->arch; |
| 1735 | |
Dmitry Vyukov | 3863dff | 2017-01-24 14:06:48 +0100 | [diff] [blame] | 1736 | if (!avic) |
| 1737 | return; |
| 1738 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1739 | if (vm_data->avic_logical_id_table_page) |
| 1740 | __free_page(vm_data->avic_logical_id_table_page); |
| 1741 | if (vm_data->avic_physical_id_table_page) |
| 1742 | __free_page(vm_data->avic_physical_id_table_page); |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1743 | |
| 1744 | spin_lock_irqsave(&svm_vm_data_hash_lock, flags); |
| 1745 | hash_del(&vm_data->hnode); |
| 1746 | spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1747 | } |
| 1748 | |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1749 | static void svm_vm_destroy(struct kvm *kvm) |
| 1750 | { |
| 1751 | avic_vm_destroy(kvm); |
| 1752 | sev_vm_destroy(kvm); |
| 1753 | } |
| 1754 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1755 | static int avic_vm_init(struct kvm *kvm) |
| 1756 | { |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1757 | unsigned long flags; |
Denys Vlasenko | 3f0d4db | 2017-08-11 22:11:58 +0200 | [diff] [blame] | 1758 | int err = -ENOMEM; |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1759 | struct kvm_arch *vm_data = &kvm->arch; |
| 1760 | struct page *p_page; |
| 1761 | struct page *l_page; |
Denys Vlasenko | 3f0d4db | 2017-08-11 22:11:58 +0200 | [diff] [blame] | 1762 | struct kvm_arch *ka; |
| 1763 | u32 vm_id; |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1764 | |
| 1765 | if (!avic) |
| 1766 | return 0; |
| 1767 | |
| 1768 | /* Allocating physical APIC ID table (4KB) */ |
| 1769 | p_page = alloc_page(GFP_KERNEL); |
| 1770 | if (!p_page) |
| 1771 | goto free_avic; |
| 1772 | |
| 1773 | vm_data->avic_physical_id_table_page = p_page; |
| 1774 | clear_page(page_address(p_page)); |
| 1775 | |
| 1776 | /* Allocating logical APIC ID table (4KB) */ |
| 1777 | l_page = alloc_page(GFP_KERNEL); |
| 1778 | if (!l_page) |
| 1779 | goto free_avic; |
| 1780 | |
| 1781 | vm_data->avic_logical_id_table_page = l_page; |
| 1782 | clear_page(page_address(l_page)); |
| 1783 | |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1784 | spin_lock_irqsave(&svm_vm_data_hash_lock, flags); |
Denys Vlasenko | 3f0d4db | 2017-08-11 22:11:58 +0200 | [diff] [blame] | 1785 | again: |
| 1786 | vm_id = next_vm_id = (next_vm_id + 1) & AVIC_VM_ID_MASK; |
| 1787 | if (vm_id == 0) { /* id is 1-based, zero is not okay */ |
| 1788 | next_vm_id_wrapped = 1; |
| 1789 | goto again; |
| 1790 | } |
| 1791 | /* Is it still in use? Only possible if wrapped at least once */ |
| 1792 | if (next_vm_id_wrapped) { |
| 1793 | hash_for_each_possible(svm_vm_data_hash, ka, hnode, vm_id) { |
| 1794 | struct kvm *k2 = container_of(ka, struct kvm, arch); |
| 1795 | struct kvm_arch *vd2 = &k2->arch; |
| 1796 | if (vd2->avic_vm_id == vm_id) |
| 1797 | goto again; |
| 1798 | } |
| 1799 | } |
| 1800 | vm_data->avic_vm_id = vm_id; |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1801 | hash_add(svm_vm_data_hash, &vm_data->hnode, vm_data->avic_vm_id); |
| 1802 | spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags); |
| 1803 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1804 | return 0; |
| 1805 | |
| 1806 | free_avic: |
| 1807 | avic_vm_destroy(kvm); |
| 1808 | return err; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1809 | } |
| 1810 | |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 1811 | static inline int |
| 1812 | avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r) |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 1813 | { |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 1814 | int ret = 0; |
| 1815 | unsigned long flags; |
| 1816 | struct amd_svm_iommu_ir *ir; |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 1817 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1818 | |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 1819 | if (!kvm_arch_has_assigned_device(vcpu->kvm)) |
| 1820 | return 0; |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 1821 | |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 1822 | /* |
| 1823 | * Here, we go through the per-vcpu ir_list to update all existing |
| 1824 | * interrupt remapping table entry targeting this vcpu. |
| 1825 | */ |
| 1826 | spin_lock_irqsave(&svm->ir_list_lock, flags); |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 1827 | |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 1828 | if (list_empty(&svm->ir_list)) |
| 1829 | goto out; |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 1830 | |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 1831 | list_for_each_entry(ir, &svm->ir_list, node) { |
| 1832 | ret = amd_iommu_update_ga(cpu, r, ir->data); |
| 1833 | if (ret) |
| 1834 | break; |
| 1835 | } |
| 1836 | out: |
| 1837 | spin_unlock_irqrestore(&svm->ir_list_lock, flags); |
| 1838 | return ret; |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 1839 | } |
| 1840 | |
| 1841 | static void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 1842 | { |
| 1843 | u64 entry; |
| 1844 | /* ID = 0xff (broadcast), ID > 0xff (reserved) */ |
Suravee Suthikulpanit | 7d669f5 | 2016-06-15 17:23:45 -0500 | [diff] [blame] | 1845 | int h_physical_id = kvm_cpu_get_apicid(cpu); |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 1846 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1847 | |
| 1848 | if (!kvm_vcpu_apicv_active(vcpu)) |
| 1849 | return; |
| 1850 | |
| 1851 | if (WARN_ON(h_physical_id >= AVIC_MAX_PHYSICAL_ID_COUNT)) |
| 1852 | return; |
| 1853 | |
| 1854 | entry = READ_ONCE(*(svm->avic_physical_id_cache)); |
| 1855 | WARN_ON(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK); |
| 1856 | |
| 1857 | entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK; |
| 1858 | entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK); |
| 1859 | |
| 1860 | entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; |
| 1861 | if (svm->avic_is_running) |
| 1862 | entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; |
| 1863 | |
| 1864 | WRITE_ONCE(*(svm->avic_physical_id_cache), entry); |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 1865 | avic_update_iommu_vcpu_affinity(vcpu, h_physical_id, |
| 1866 | svm->avic_is_running); |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | static void avic_vcpu_put(struct kvm_vcpu *vcpu) |
| 1870 | { |
| 1871 | u64 entry; |
| 1872 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1873 | |
| 1874 | if (!kvm_vcpu_apicv_active(vcpu)) |
| 1875 | return; |
| 1876 | |
| 1877 | entry = READ_ONCE(*(svm->avic_physical_id_cache)); |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 1878 | if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK) |
| 1879 | avic_update_iommu_vcpu_affinity(vcpu, -1, 0); |
| 1880 | |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 1881 | entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; |
| 1882 | WRITE_ONCE(*(svm->avic_physical_id_cache), entry); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1883 | } |
| 1884 | |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 1885 | /** |
| 1886 | * This function is called during VCPU halt/unhalt. |
| 1887 | */ |
| 1888 | static void avic_set_running(struct kvm_vcpu *vcpu, bool is_run) |
| 1889 | { |
| 1890 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1891 | |
| 1892 | svm->avic_is_running = is_run; |
| 1893 | if (is_run) |
| 1894 | avic_vcpu_load(vcpu, vcpu->cpu); |
| 1895 | else |
| 1896 | avic_vcpu_put(vcpu); |
| 1897 | } |
| 1898 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 1899 | static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1900 | { |
| 1901 | struct vcpu_svm *svm = to_svm(vcpu); |
Julian Stecklina | 66f7b72 | 2012-12-05 15:26:19 +0100 | [diff] [blame] | 1902 | u32 dummy; |
| 1903 | u32 eax = 1; |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1904 | |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 1905 | svm->spec_ctrl = 0; |
| 1906 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 1907 | if (!init_event) { |
| 1908 | svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE | |
| 1909 | MSR_IA32_APICBASE_ENABLE; |
| 1910 | if (kvm_vcpu_is_reset_bsp(&svm->vcpu)) |
| 1911 | svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP; |
| 1912 | } |
Paolo Bonzini | 5690891 | 2015-10-19 11:30:19 +0200 | [diff] [blame] | 1913 | init_vmcb(svm); |
Avi Kivity | 7043338 | 2007-11-07 12:57:23 +0200 | [diff] [blame] | 1914 | |
Yu Zhang | e911eb3 | 2017-08-24 20:27:52 +0800 | [diff] [blame] | 1915 | kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, true); |
Julian Stecklina | 66f7b72 | 2012-12-05 15:26:19 +0100 | [diff] [blame] | 1916 | kvm_register_write(vcpu, VCPU_REGS_RDX, eax); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1917 | |
| 1918 | if (kvm_vcpu_apicv_active(vcpu) && !init_event) |
| 1919 | avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE); |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1920 | } |
| 1921 | |
Suravee Suthikulpanit | dfa2009 | 2017-09-12 10:42:40 -0500 | [diff] [blame] | 1922 | static int avic_init_vcpu(struct vcpu_svm *svm) |
| 1923 | { |
| 1924 | int ret; |
| 1925 | |
Suravee Suthikulpanit | 67034bb | 2017-09-12 10:42:42 -0500 | [diff] [blame] | 1926 | if (!kvm_vcpu_apicv_active(&svm->vcpu)) |
Suravee Suthikulpanit | dfa2009 | 2017-09-12 10:42:40 -0500 | [diff] [blame] | 1927 | return 0; |
| 1928 | |
| 1929 | ret = avic_init_backing_page(&svm->vcpu); |
| 1930 | if (ret) |
| 1931 | return ret; |
| 1932 | |
| 1933 | INIT_LIST_HEAD(&svm->ir_list); |
| 1934 | spin_lock_init(&svm->ir_list_lock); |
| 1935 | |
| 1936 | return ret; |
| 1937 | } |
| 1938 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1939 | 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] | 1940 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1941 | struct vcpu_svm *svm; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1942 | struct page *page; |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 1943 | struct page *msrpm_pages; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 1944 | struct page *hsave_page; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1945 | struct page *nested_msrpm_pages; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1946 | int err; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1947 | |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 1948 | svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1949 | if (!svm) { |
| 1950 | err = -ENOMEM; |
| 1951 | goto out; |
| 1952 | } |
| 1953 | |
| 1954 | err = kvm_vcpu_init(&svm->vcpu, kvm, id); |
| 1955 | if (err) |
| 1956 | goto free_svm; |
| 1957 | |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 1958 | err = -ENOMEM; |
Takuya Yoshikawa | b7af404 | 2010-03-09 14:55:19 +0900 | [diff] [blame] | 1959 | page = alloc_page(GFP_KERNEL); |
| 1960 | if (!page) |
| 1961 | goto uninit; |
| 1962 | |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 1963 | msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER); |
| 1964 | if (!msrpm_pages) |
Takuya Yoshikawa | b7af404 | 2010-03-09 14:55:19 +0900 | [diff] [blame] | 1965 | goto free_page1; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1966 | |
| 1967 | nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER); |
| 1968 | if (!nested_msrpm_pages) |
Takuya Yoshikawa | b7af404 | 2010-03-09 14:55:19 +0900 | [diff] [blame] | 1969 | goto free_page2; |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 1970 | |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 1971 | hsave_page = alloc_page(GFP_KERNEL); |
| 1972 | if (!hsave_page) |
Takuya Yoshikawa | b7af404 | 2010-03-09 14:55:19 +0900 | [diff] [blame] | 1973 | goto free_page3; |
| 1974 | |
Suravee Suthikulpanit | dfa2009 | 2017-09-12 10:42:40 -0500 | [diff] [blame] | 1975 | err = avic_init_vcpu(svm); |
| 1976 | if (err) |
| 1977 | goto free_page4; |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1978 | |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 1979 | /* We initialize this flag to true to make sure that the is_running |
| 1980 | * bit would be set the first time the vcpu is loaded. |
| 1981 | */ |
| 1982 | svm->avic_is_running = true; |
| 1983 | |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 1984 | svm->nested.hsave = page_address(hsave_page); |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 1985 | |
Takuya Yoshikawa | b7af404 | 2010-03-09 14:55:19 +0900 | [diff] [blame] | 1986 | svm->msrpm = page_address(msrpm_pages); |
| 1987 | svm_vcpu_init_msrpm(svm->msrpm); |
| 1988 | |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 1989 | svm->nested.msrpm = page_address(nested_msrpm_pages); |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 1990 | svm_vcpu_init_msrpm(svm->nested.msrpm); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1991 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1992 | svm->vmcb = page_address(page); |
| 1993 | clear_page(svm->vmcb); |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 1994 | svm->vmcb_pa = __sme_set(page_to_pfn(page) << PAGE_SHIFT); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1995 | svm->asid_generation = 0; |
Paolo Bonzini | 5690891 | 2015-10-19 11:30:19 +0200 | [diff] [blame] | 1996 | init_vmcb(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1997 | |
Boris Ostrovsky | 2b036c6 | 2012-01-09 14:00:35 -0500 | [diff] [blame] | 1998 | svm_init_osvw(&svm->vcpu); |
| 1999 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2000 | return &svm->vcpu; |
Avi Kivity | 36241b8 | 2006-12-22 01:05:20 -0800 | [diff] [blame] | 2001 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 2002 | free_page4: |
| 2003 | __free_page(hsave_page); |
Takuya Yoshikawa | b7af404 | 2010-03-09 14:55:19 +0900 | [diff] [blame] | 2004 | free_page3: |
| 2005 | __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER); |
| 2006 | free_page2: |
| 2007 | __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER); |
| 2008 | free_page1: |
| 2009 | __free_page(page); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2010 | uninit: |
| 2011 | kvm_vcpu_uninit(&svm->vcpu); |
| 2012 | free_svm: |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 2013 | kmem_cache_free(kvm_vcpu_cache, svm); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2014 | out: |
| 2015 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2016 | } |
| 2017 | |
| 2018 | static void svm_free_vcpu(struct kvm_vcpu *vcpu) |
| 2019 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2020 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2021 | |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 2022 | __free_page(pfn_to_page(__sme_clr(svm->vmcb_pa) >> PAGE_SHIFT)); |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 2023 | __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER); |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 2024 | __free_page(virt_to_page(svm->nested.hsave)); |
| 2025 | __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2026 | kvm_vcpu_uninit(vcpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 2027 | kmem_cache_free(kvm_vcpu_cache, svm); |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2028 | /* |
| 2029 | * The vmcb page can be recycled, causing a false negative in |
| 2030 | * svm_vcpu_load(). So do a full IBPB now. |
| 2031 | */ |
| 2032 | indirect_branch_prediction_barrier(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2033 | } |
| 2034 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2035 | static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2036 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2037 | struct vcpu_svm *svm = to_svm(vcpu); |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2038 | struct svm_cpu_data *sd = per_cpu(svm_data, cpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2039 | int i; |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 2040 | |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 2041 | if (unlikely(cpu != vcpu->cpu)) { |
Marcelo Tosatti | 4b656b1 | 2009-07-21 12:47:45 -0300 | [diff] [blame] | 2042 | svm->asid_generation = 0; |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 2043 | mark_all_dirty(svm->vmcb); |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 2044 | } |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 2045 | |
Avi Kivity | 82ca2d1 | 2010-10-21 12:20:34 +0200 | [diff] [blame] | 2046 | #ifdef CONFIG_X86_64 |
| 2047 | rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base); |
| 2048 | #endif |
Avi Kivity | dacccfd | 2010-10-21 12:20:33 +0200 | [diff] [blame] | 2049 | savesegment(fs, svm->host.fs); |
| 2050 | savesegment(gs, svm->host.gs); |
| 2051 | svm->host.ldt = kvm_read_ldt(); |
| 2052 | |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 2053 | for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2054 | rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]); |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 2055 | |
Haozhong Zhang | ad721883 | 2015-10-20 15:39:02 +0800 | [diff] [blame] | 2056 | if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) { |
| 2057 | u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio; |
| 2058 | if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) { |
| 2059 | __this_cpu_write(current_tsc_ratio, tsc_ratio); |
| 2060 | wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio); |
| 2061 | } |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 2062 | } |
Paolo Bonzini | 46896c7 | 2015-11-12 14:49:16 +0100 | [diff] [blame] | 2063 | /* This assumes that the kernel never uses MSR_TSC_AUX */ |
| 2064 | if (static_cpu_has(X86_FEATURE_RDTSCP)) |
| 2065 | wrmsrl(MSR_TSC_AUX, svm->tsc_aux); |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 2066 | |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2067 | if (sd->current_vmcb != svm->vmcb) { |
| 2068 | sd->current_vmcb = svm->vmcb; |
| 2069 | indirect_branch_prediction_barrier(); |
| 2070 | } |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 2071 | avic_vcpu_load(vcpu, cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2072 | } |
| 2073 | |
| 2074 | static void svm_vcpu_put(struct kvm_vcpu *vcpu) |
| 2075 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2076 | struct vcpu_svm *svm = to_svm(vcpu); |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 2077 | int i; |
| 2078 | |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 2079 | avic_vcpu_put(vcpu); |
| 2080 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 2081 | ++vcpu->stat.host_state_reload; |
Avi Kivity | dacccfd | 2010-10-21 12:20:33 +0200 | [diff] [blame] | 2082 | kvm_load_ldt(svm->host.ldt); |
| 2083 | #ifdef CONFIG_X86_64 |
| 2084 | loadsegment(fs, svm->host.fs); |
Andy Lutomirski | 296f781 | 2016-04-26 12:23:29 -0700 | [diff] [blame] | 2085 | wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gsbase); |
Joerg Roedel | 893a5ab | 2011-01-14 16:45:01 +0100 | [diff] [blame] | 2086 | load_gs_index(svm->host.gs); |
Avi Kivity | dacccfd | 2010-10-21 12:20:33 +0200 | [diff] [blame] | 2087 | #else |
Avi Kivity | 831ca60 | 2011-03-08 16:09:51 +0200 | [diff] [blame] | 2088 | #ifdef CONFIG_X86_32_LAZY_GS |
Avi Kivity | dacccfd | 2010-10-21 12:20:33 +0200 | [diff] [blame] | 2089 | loadsegment(gs, svm->host.gs); |
| 2090 | #endif |
Avi Kivity | 831ca60 | 2011-03-08 16:09:51 +0200 | [diff] [blame] | 2091 | #endif |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 2092 | for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2093 | wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2094 | } |
| 2095 | |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 2096 | static void svm_vcpu_blocking(struct kvm_vcpu *vcpu) |
| 2097 | { |
| 2098 | avic_set_running(vcpu, false); |
| 2099 | } |
| 2100 | |
| 2101 | static void svm_vcpu_unblocking(struct kvm_vcpu *vcpu) |
| 2102 | { |
| 2103 | avic_set_running(vcpu, true); |
| 2104 | } |
| 2105 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2106 | static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu) |
| 2107 | { |
Ladi Prosek | 9b61174 | 2017-06-21 09:06:59 +0200 | [diff] [blame] | 2108 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2109 | unsigned long rflags = svm->vmcb->save.rflags; |
| 2110 | |
| 2111 | if (svm->nmi_singlestep) { |
| 2112 | /* Hide our flags if they were not set by the guest */ |
| 2113 | if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF)) |
| 2114 | rflags &= ~X86_EFLAGS_TF; |
| 2115 | if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF)) |
| 2116 | rflags &= ~X86_EFLAGS_RF; |
| 2117 | } |
| 2118 | return rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2119 | } |
| 2120 | |
| 2121 | static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 2122 | { |
Ladi Prosek | 9b61174 | 2017-06-21 09:06:59 +0200 | [diff] [blame] | 2123 | if (to_svm(vcpu)->nmi_singlestep) |
| 2124 | rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF); |
| 2125 | |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 2126 | /* |
Andrea Gelmini | bb3541f | 2016-05-21 14:14:44 +0200 | [diff] [blame] | 2127 | * Any change of EFLAGS.VM is accompanied by a reload of SS |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 2128 | * (caused by either a task switch or an inter-privilege IRET), |
| 2129 | * so we do not need to update the CPL here. |
| 2130 | */ |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2131 | to_svm(vcpu)->vmcb->save.rflags = rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2132 | } |
| 2133 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2134 | static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg) |
| 2135 | { |
| 2136 | switch (reg) { |
| 2137 | case VCPU_EXREG_PDPTR: |
| 2138 | BUG_ON(!npt_enabled); |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 2139 | load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu)); |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2140 | break; |
| 2141 | default: |
| 2142 | BUG(); |
| 2143 | } |
| 2144 | } |
| 2145 | |
Alexander Graf | f0b8505 | 2008-11-25 20:17:01 +0100 | [diff] [blame] | 2146 | static void svm_set_vintr(struct vcpu_svm *svm) |
| 2147 | { |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 2148 | set_intercept(svm, INTERCEPT_VINTR); |
Alexander Graf | f0b8505 | 2008-11-25 20:17:01 +0100 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | static void svm_clear_vintr(struct vcpu_svm *svm) |
| 2152 | { |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 2153 | clr_intercept(svm, INTERCEPT_VINTR); |
Alexander Graf | f0b8505 | 2008-11-25 20:17:01 +0100 | [diff] [blame] | 2154 | } |
| 2155 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2156 | static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg) |
| 2157 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2158 | struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2159 | |
| 2160 | switch (seg) { |
| 2161 | case VCPU_SREG_CS: return &save->cs; |
| 2162 | case VCPU_SREG_DS: return &save->ds; |
| 2163 | case VCPU_SREG_ES: return &save->es; |
| 2164 | case VCPU_SREG_FS: return &save->fs; |
| 2165 | case VCPU_SREG_GS: return &save->gs; |
| 2166 | case VCPU_SREG_SS: return &save->ss; |
| 2167 | case VCPU_SREG_TR: return &save->tr; |
| 2168 | case VCPU_SREG_LDTR: return &save->ldtr; |
| 2169 | } |
| 2170 | BUG(); |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 2171 | return NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2172 | } |
| 2173 | |
| 2174 | static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 2175 | { |
| 2176 | struct vmcb_seg *s = svm_seg(vcpu, seg); |
| 2177 | |
| 2178 | return s->base; |
| 2179 | } |
| 2180 | |
| 2181 | static void svm_get_segment(struct kvm_vcpu *vcpu, |
| 2182 | struct kvm_segment *var, int seg) |
| 2183 | { |
| 2184 | struct vmcb_seg *s = svm_seg(vcpu, seg); |
| 2185 | |
| 2186 | var->base = s->base; |
| 2187 | var->limit = s->limit; |
| 2188 | var->selector = s->selector; |
| 2189 | var->type = s->attrib & SVM_SELECTOR_TYPE_MASK; |
| 2190 | var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1; |
| 2191 | var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3; |
| 2192 | var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1; |
| 2193 | var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1; |
| 2194 | var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1; |
| 2195 | var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1; |
Jim Mattson | 80112c8 | 2014-07-08 09:47:41 +0530 | [diff] [blame] | 2196 | |
| 2197 | /* |
| 2198 | * AMD CPUs circa 2014 track the G bit for all segments except CS. |
| 2199 | * However, the SVM spec states that the G bit is not observed by the |
| 2200 | * CPU, and some VMware virtual CPUs drop the G bit for all segments. |
| 2201 | * So let's synthesize a legal G bit for all segments, this helps |
| 2202 | * running KVM nested. It also helps cross-vendor migration, because |
| 2203 | * Intel's vmentry has a check on the 'G' bit. |
| 2204 | */ |
| 2205 | var->g = s->limit > 0xfffff; |
Amit Shah | 25022ac | 2008-10-27 09:04:17 +0000 | [diff] [blame] | 2206 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 2207 | /* |
| 2208 | * 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] | 2209 | * for cross vendor migration purposes by "not present" |
| 2210 | */ |
Gioh Kim | 8eae957 | 2017-05-30 15:24:45 +0200 | [diff] [blame] | 2211 | var->unusable = !var->present; |
Andre Przywara | 19bca6a | 2009-04-28 12:45:30 +0200 | [diff] [blame] | 2212 | |
Andre Przywara | 1fbdc7a | 2009-01-11 22:39:44 +0100 | [diff] [blame] | 2213 | switch (seg) { |
Andre Przywara | 1fbdc7a | 2009-01-11 22:39:44 +0100 | [diff] [blame] | 2214 | case VCPU_SREG_TR: |
| 2215 | /* |
| 2216 | * Work around a bug where the busy flag in the tr selector |
| 2217 | * isn't exposed |
| 2218 | */ |
Amit Shah | c0d0982 | 2008-10-27 09:04:18 +0000 | [diff] [blame] | 2219 | var->type |= 0x2; |
Andre Przywara | 1fbdc7a | 2009-01-11 22:39:44 +0100 | [diff] [blame] | 2220 | break; |
| 2221 | case VCPU_SREG_DS: |
| 2222 | case VCPU_SREG_ES: |
| 2223 | case VCPU_SREG_FS: |
| 2224 | case VCPU_SREG_GS: |
| 2225 | /* |
| 2226 | * The accessed bit must always be set in the segment |
| 2227 | * descriptor cache, although it can be cleared in the |
| 2228 | * descriptor, the cached bit always remains at 1. Since |
| 2229 | * Intel has a check on this, set it here to support |
| 2230 | * cross-vendor migration. |
| 2231 | */ |
| 2232 | if (!var->unusable) |
| 2233 | var->type |= 0x1; |
| 2234 | break; |
Andre Przywara | b586eb0 | 2009-04-28 12:45:43 +0200 | [diff] [blame] | 2235 | case VCPU_SREG_SS: |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 2236 | /* |
| 2237 | * On AMD CPUs sometimes the DB bit in the segment |
Andre Przywara | b586eb0 | 2009-04-28 12:45:43 +0200 | [diff] [blame] | 2238 | * descriptor is left as 1, although the whole segment has |
| 2239 | * been made unusable. Clear it here to pass an Intel VMX |
| 2240 | * entry check when cross vendor migrating. |
| 2241 | */ |
| 2242 | if (var->unusable) |
| 2243 | var->db = 0; |
Roman Pen | d9c1b54 | 2017-06-01 10:55:03 +0200 | [diff] [blame] | 2244 | /* This is symmetric with svm_set_segment() */ |
Jan Kiszka | 33b458d | 2014-06-29 17:12:43 +0200 | [diff] [blame] | 2245 | var->dpl = to_svm(vcpu)->vmcb->save.cpl; |
Andre Przywara | b586eb0 | 2009-04-28 12:45:43 +0200 | [diff] [blame] | 2246 | break; |
Andre Przywara | 1fbdc7a | 2009-01-11 22:39:44 +0100 | [diff] [blame] | 2247 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2248 | } |
| 2249 | |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 2250 | static int svm_get_cpl(struct kvm_vcpu *vcpu) |
| 2251 | { |
| 2252 | struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save; |
| 2253 | |
| 2254 | return save->cpl; |
| 2255 | } |
| 2256 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2257 | 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] | 2258 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2259 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2260 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2261 | dt->size = svm->vmcb->save.idtr.limit; |
| 2262 | dt->address = svm->vmcb->save.idtr.base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2263 | } |
| 2264 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2265 | 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] | 2266 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2267 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2268 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2269 | svm->vmcb->save.idtr.limit = dt->size; |
| 2270 | svm->vmcb->save.idtr.base = dt->address ; |
Joerg Roedel | 17a703c | 2010-12-03 11:45:56 +0100 | [diff] [blame] | 2271 | mark_dirty(svm->vmcb, VMCB_DT); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2272 | } |
| 2273 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2274 | 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] | 2275 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2276 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2277 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2278 | dt->size = svm->vmcb->save.gdtr.limit; |
| 2279 | dt->address = svm->vmcb->save.gdtr.base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2280 | } |
| 2281 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2282 | 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] | 2283 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2284 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2285 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2286 | svm->vmcb->save.gdtr.limit = dt->size; |
| 2287 | svm->vmcb->save.gdtr.base = dt->address ; |
Joerg Roedel | 17a703c | 2010-12-03 11:45:56 +0100 | [diff] [blame] | 2288 | mark_dirty(svm->vmcb, VMCB_DT); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2289 | } |
| 2290 | |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 2291 | static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
| 2292 | { |
| 2293 | } |
| 2294 | |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 2295 | static void svm_decache_cr3(struct kvm_vcpu *vcpu) |
| 2296 | { |
| 2297 | } |
| 2298 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 2299 | static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 2300 | { |
| 2301 | } |
| 2302 | |
Avi Kivity | d225157 | 2010-01-06 10:55:27 +0200 | [diff] [blame] | 2303 | static void update_cr0_intercept(struct vcpu_svm *svm) |
| 2304 | { |
| 2305 | ulong gcr0 = svm->vcpu.arch.cr0; |
| 2306 | u64 *hcr0 = &svm->vmcb->save.cr0; |
| 2307 | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 2308 | *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK) |
| 2309 | | (gcr0 & SVM_CR0_SELECTIVE_MASK); |
Avi Kivity | d225157 | 2010-01-06 10:55:27 +0200 | [diff] [blame] | 2310 | |
Joerg Roedel | dcca1a6 | 2010-12-03 11:45:54 +0100 | [diff] [blame] | 2311 | mark_dirty(svm->vmcb, VMCB_CR); |
Avi Kivity | d225157 | 2010-01-06 10:55:27 +0200 | [diff] [blame] | 2312 | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 2313 | if (gcr0 == *hcr0) { |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 2314 | clr_cr_intercept(svm, INTERCEPT_CR0_READ); |
| 2315 | clr_cr_intercept(svm, INTERCEPT_CR0_WRITE); |
Avi Kivity | d225157 | 2010-01-06 10:55:27 +0200 | [diff] [blame] | 2316 | } else { |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 2317 | set_cr_intercept(svm, INTERCEPT_CR0_READ); |
| 2318 | set_cr_intercept(svm, INTERCEPT_CR0_WRITE); |
Avi Kivity | d225157 | 2010-01-06 10:55:27 +0200 | [diff] [blame] | 2319 | } |
| 2320 | } |
| 2321 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2322 | static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 2323 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2324 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2325 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2326 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 2327 | if (vcpu->arch.efer & EFER_LME) { |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 2328 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) { |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 2329 | vcpu->arch.efer |= EFER_LMA; |
Carlo Marcelo Arenas Belon | 2b5203e | 2007-12-01 06:17:11 -0600 | [diff] [blame] | 2330 | svm->vmcb->save.efer |= EFER_LMA | EFER_LME; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2331 | } |
| 2332 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2333 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) { |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 2334 | vcpu->arch.efer &= ~EFER_LMA; |
Carlo Marcelo Arenas Belon | 2b5203e | 2007-12-01 06:17:11 -0600 | [diff] [blame] | 2335 | svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2336 | } |
| 2337 | } |
| 2338 | #endif |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2339 | vcpu->arch.cr0 = cr0; |
Avi Kivity | 888f9f3 | 2010-01-10 12:14:04 +0200 | [diff] [blame] | 2340 | |
| 2341 | if (!npt_enabled) |
| 2342 | cr0 |= X86_CR0_PG | X86_CR0_WP; |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 2343 | |
Paolo Bonzini | bcf166a | 2015-10-01 13:19:55 +0200 | [diff] [blame] | 2344 | /* |
| 2345 | * re-enable caching here because the QEMU bios |
| 2346 | * does not do it - this results in some delay at |
| 2347 | * reboot |
| 2348 | */ |
| 2349 | if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED)) |
| 2350 | cr0 &= ~(X86_CR0_CD | X86_CR0_NW); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2351 | svm->vmcb->save.cr0 = cr0; |
Joerg Roedel | dcca1a6 | 2010-12-03 11:45:54 +0100 | [diff] [blame] | 2352 | mark_dirty(svm->vmcb, VMCB_CR); |
Avi Kivity | d225157 | 2010-01-06 10:55:27 +0200 | [diff] [blame] | 2353 | update_cr0_intercept(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2354 | } |
| 2355 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 2356 | static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2357 | { |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 2358 | unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE; |
Joerg Roedel | e5eab0c | 2008-09-09 19:11:51 +0200 | [diff] [blame] | 2359 | unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4; |
| 2360 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 2361 | if (cr4 & X86_CR4_VMXE) |
| 2362 | return 1; |
| 2363 | |
Joerg Roedel | e5eab0c | 2008-09-09 19:11:51 +0200 | [diff] [blame] | 2364 | if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE)) |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 2365 | svm_flush_tlb(vcpu, true); |
Joerg Roedel | 6394b64 | 2008-04-09 14:15:29 +0200 | [diff] [blame] | 2366 | |
Joerg Roedel | ec07726 | 2008-04-09 14:15:28 +0200 | [diff] [blame] | 2367 | vcpu->arch.cr4 = cr4; |
| 2368 | if (!npt_enabled) |
| 2369 | cr4 |= X86_CR4_PAE; |
Joerg Roedel | 6394b64 | 2008-04-09 14:15:29 +0200 | [diff] [blame] | 2370 | cr4 |= host_cr4_mce; |
Joerg Roedel | ec07726 | 2008-04-09 14:15:28 +0200 | [diff] [blame] | 2371 | to_svm(vcpu)->vmcb->save.cr4 = cr4; |
Joerg Roedel | dcca1a6 | 2010-12-03 11:45:54 +0100 | [diff] [blame] | 2372 | mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR); |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 2373 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2374 | } |
| 2375 | |
| 2376 | static void svm_set_segment(struct kvm_vcpu *vcpu, |
| 2377 | struct kvm_segment *var, int seg) |
| 2378 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2379 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2380 | struct vmcb_seg *s = svm_seg(vcpu, seg); |
| 2381 | |
| 2382 | s->base = var->base; |
| 2383 | s->limit = var->limit; |
| 2384 | s->selector = var->selector; |
Roman Pen | d9c1b54 | 2017-06-01 10:55:03 +0200 | [diff] [blame] | 2385 | s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK); |
| 2386 | s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT; |
| 2387 | s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT; |
| 2388 | s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT; |
| 2389 | s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT; |
| 2390 | s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT; |
| 2391 | s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT; |
| 2392 | s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT; |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 2393 | |
| 2394 | /* |
| 2395 | * This is always accurate, except if SYSRET returned to a segment |
| 2396 | * with SS.DPL != 3. Intel does not have this quirk, and always |
| 2397 | * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it |
| 2398 | * would entail passing the CPL to userspace and back. |
| 2399 | */ |
| 2400 | if (seg == VCPU_SREG_SS) |
Roman Pen | d9c1b54 | 2017-06-01 10:55:03 +0200 | [diff] [blame] | 2401 | /* This is symmetric with svm_get_segment() */ |
| 2402 | svm->vmcb->save.cpl = (var->dpl & 3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2403 | |
Joerg Roedel | 060d0c9 | 2010-12-03 11:45:57 +0100 | [diff] [blame] | 2404 | mark_dirty(svm->vmcb, VMCB_SEG); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2405 | } |
| 2406 | |
Paolo Bonzini | cbdb967 | 2015-11-10 09:14:39 +0100 | [diff] [blame] | 2407 | static void update_bp_intercept(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2408 | { |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2409 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2410 | |
Joerg Roedel | 18c918c | 2010-11-30 18:03:59 +0100 | [diff] [blame] | 2411 | clr_exception_intercept(svm, BP_VECTOR); |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 2412 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2413 | if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) { |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2414 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
Joerg Roedel | 18c918c | 2010-11-30 18:03:59 +0100 | [diff] [blame] | 2415 | set_exception_intercept(svm, BP_VECTOR); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2416 | } else |
| 2417 | vcpu->guest_debug = 0; |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 2418 | } |
| 2419 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 2420 | 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] | 2421 | { |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 2422 | if (sd->next_asid > sd->max_asid) { |
| 2423 | ++sd->asid_generation; |
Brijesh Singh | 4faefff | 2017-12-04 10:57:25 -0600 | [diff] [blame] | 2424 | sd->next_asid = sd->min_asid; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2425 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2426 | } |
| 2427 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 2428 | svm->asid_generation = sd->asid_generation; |
| 2429 | svm->vmcb->control.asid = sd->next_asid++; |
Joerg Roedel | d48086d | 2010-12-03 11:45:51 +0100 | [diff] [blame] | 2430 | |
| 2431 | mark_dirty(svm->vmcb, VMCB_ASID); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2432 | } |
| 2433 | |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 2434 | static u64 svm_get_dr6(struct kvm_vcpu *vcpu) |
| 2435 | { |
| 2436 | return to_svm(vcpu)->vmcb->save.dr6; |
| 2437 | } |
| 2438 | |
| 2439 | static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value) |
| 2440 | { |
| 2441 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2442 | |
| 2443 | svm->vmcb->save.dr6 = value; |
| 2444 | mark_dirty(svm->vmcb, VMCB_DR); |
| 2445 | } |
| 2446 | |
Paolo Bonzini | facb013 | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 2447 | static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) |
| 2448 | { |
| 2449 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2450 | |
| 2451 | get_debugreg(vcpu->arch.db[0], 0); |
| 2452 | get_debugreg(vcpu->arch.db[1], 1); |
| 2453 | get_debugreg(vcpu->arch.db[2], 2); |
| 2454 | get_debugreg(vcpu->arch.db[3], 3); |
| 2455 | vcpu->arch.dr6 = svm_get_dr6(vcpu); |
| 2456 | vcpu->arch.dr7 = svm->vmcb->save.dr7; |
| 2457 | |
| 2458 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; |
| 2459 | set_dr_intercepts(svm); |
| 2460 | } |
| 2461 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 2462 | static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2463 | { |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2464 | struct vcpu_svm *svm = to_svm(vcpu); |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2465 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 2466 | svm->vmcb->save.dr7 = value; |
Joerg Roedel | 72214b9 | 2010-12-03 11:45:55 +0100 | [diff] [blame] | 2467 | mark_dirty(svm->vmcb, VMCB_DR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2468 | } |
| 2469 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2470 | static int pf_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2471 | { |
Brijesh Singh | 0ede79e | 2017-12-04 10:57:39 -0600 | [diff] [blame] | 2472 | u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2); |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 2473 | u64 error_code = svm->vmcb->control.exit_info_1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2474 | |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 2475 | return kvm_handle_page_fault(&svm->vcpu, error_code, fault_address, |
Brijesh Singh | 00b10fe | 2017-12-04 10:57:40 -0600 | [diff] [blame] | 2476 | static_cpu_has(X86_FEATURE_DECODEASSISTS) ? |
| 2477 | svm->vmcb->control.insn_bytes : NULL, |
Paolo Bonzini | d000653 | 2017-08-11 18:36:43 +0200 | [diff] [blame] | 2478 | svm->vmcb->control.insn_len); |
| 2479 | } |
| 2480 | |
| 2481 | static int npf_interception(struct vcpu_svm *svm) |
| 2482 | { |
Brijesh Singh | 0ede79e | 2017-12-04 10:57:39 -0600 | [diff] [blame] | 2483 | u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2); |
Paolo Bonzini | d000653 | 2017-08-11 18:36:43 +0200 | [diff] [blame] | 2484 | u64 error_code = svm->vmcb->control.exit_info_1; |
| 2485 | |
| 2486 | trace_kvm_page_fault(fault_address, error_code); |
| 2487 | return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code, |
Brijesh Singh | 00b10fe | 2017-12-04 10:57:40 -0600 | [diff] [blame] | 2488 | static_cpu_has(X86_FEATURE_DECODEASSISTS) ? |
| 2489 | svm->vmcb->control.insn_bytes : NULL, |
Paolo Bonzini | d000653 | 2017-08-11 18:36:43 +0200 | [diff] [blame] | 2490 | svm->vmcb->control.insn_len); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2491 | } |
| 2492 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2493 | static int db_interception(struct vcpu_svm *svm) |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2494 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2495 | struct kvm_run *kvm_run = svm->vcpu.run; |
| 2496 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2497 | if (!(svm->vcpu.guest_debug & |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 2498 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) && |
Jan Kiszka | 6be7d30 | 2009-10-18 13:24:54 +0200 | [diff] [blame] | 2499 | !svm->nmi_singlestep) { |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2500 | kvm_queue_exception(&svm->vcpu, DB_VECTOR); |
| 2501 | return 1; |
| 2502 | } |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 2503 | |
Jan Kiszka | 6be7d30 | 2009-10-18 13:24:54 +0200 | [diff] [blame] | 2504 | if (svm->nmi_singlestep) { |
Ladi Prosek | 4aebd0e | 2017-06-21 09:06:57 +0200 | [diff] [blame] | 2505 | disable_nmi_singlestep(svm); |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 2506 | } |
| 2507 | |
| 2508 | if (svm->vcpu.guest_debug & |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 2509 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) { |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 2510 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
| 2511 | kvm_run->debug.arch.pc = |
| 2512 | svm->vmcb->save.cs.base + svm->vmcb->save.rip; |
| 2513 | kvm_run->debug.arch.exception = DB_VECTOR; |
| 2514 | return 0; |
| 2515 | } |
| 2516 | |
| 2517 | return 1; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2518 | } |
| 2519 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2520 | static int bp_interception(struct vcpu_svm *svm) |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2521 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2522 | struct kvm_run *kvm_run = svm->vcpu.run; |
| 2523 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2524 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
| 2525 | kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip; |
| 2526 | kvm_run->debug.arch.exception = BP_VECTOR; |
| 2527 | return 0; |
| 2528 | } |
| 2529 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2530 | static int ud_interception(struct vcpu_svm *svm) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 2531 | { |
| 2532 | int er; |
| 2533 | |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 2534 | er = emulate_instruction(&svm->vcpu, EMULTYPE_TRAP_UD); |
Liran Alon | 61cb57c | 2017-11-05 16:56:32 +0200 | [diff] [blame] | 2535 | if (er == EMULATE_USER_EXIT) |
| 2536 | return 0; |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 2537 | if (er != EMULATE_DONE) |
Avi Kivity | 7ee5d940 | 2007-11-25 15:22:50 +0200 | [diff] [blame] | 2538 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 2539 | return 1; |
| 2540 | } |
| 2541 | |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 2542 | static int ac_interception(struct vcpu_svm *svm) |
| 2543 | { |
| 2544 | kvm_queue_exception_e(&svm->vcpu, AC_VECTOR, 0); |
| 2545 | return 1; |
| 2546 | } |
| 2547 | |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 2548 | static bool is_erratum_383(void) |
| 2549 | { |
| 2550 | int err, i; |
| 2551 | u64 value; |
| 2552 | |
| 2553 | if (!erratum_383_found) |
| 2554 | return false; |
| 2555 | |
| 2556 | value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err); |
| 2557 | if (err) |
| 2558 | return false; |
| 2559 | |
| 2560 | /* Bit 62 may or may not be set for this mce */ |
| 2561 | value &= ~(1ULL << 62); |
| 2562 | |
| 2563 | if (value != 0xb600000000010015ULL) |
| 2564 | return false; |
| 2565 | |
| 2566 | /* Clear MCi_STATUS registers */ |
| 2567 | for (i = 0; i < 6; ++i) |
| 2568 | native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0); |
| 2569 | |
| 2570 | value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err); |
| 2571 | if (!err) { |
| 2572 | u32 low, high; |
| 2573 | |
| 2574 | value &= ~(1ULL << 2); |
| 2575 | low = lower_32_bits(value); |
| 2576 | high = upper_32_bits(value); |
| 2577 | |
| 2578 | native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high); |
| 2579 | } |
| 2580 | |
| 2581 | /* Flush tlb to evict multi-match entries */ |
| 2582 | __flush_tlb_all(); |
| 2583 | |
| 2584 | return true; |
| 2585 | } |
| 2586 | |
Joerg Roedel | fe5913e | 2010-05-17 14:43:34 +0200 | [diff] [blame] | 2587 | static void svm_handle_mce(struct vcpu_svm *svm) |
Joerg Roedel | 53371b5 | 2008-04-09 14:15:30 +0200 | [diff] [blame] | 2588 | { |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 2589 | if (is_erratum_383()) { |
| 2590 | /* |
| 2591 | * Erratum 383 triggered. Guest state is corrupt so kill the |
| 2592 | * guest. |
| 2593 | */ |
| 2594 | pr_err("KVM: Guest triggered AMD Erratum 383\n"); |
| 2595 | |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 2596 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu); |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 2597 | |
| 2598 | return; |
| 2599 | } |
| 2600 | |
Joerg Roedel | 53371b5 | 2008-04-09 14:15:30 +0200 | [diff] [blame] | 2601 | /* |
| 2602 | * On an #MC intercept the MCE handler is not called automatically in |
| 2603 | * the host. So do it by hand here. |
| 2604 | */ |
| 2605 | asm volatile ( |
| 2606 | "int $0x12\n"); |
| 2607 | /* not sure if we ever come back to this point */ |
| 2608 | |
Joerg Roedel | fe5913e | 2010-05-17 14:43:34 +0200 | [diff] [blame] | 2609 | return; |
| 2610 | } |
| 2611 | |
| 2612 | static int mc_interception(struct vcpu_svm *svm) |
| 2613 | { |
Joerg Roedel | 53371b5 | 2008-04-09 14:15:30 +0200 | [diff] [blame] | 2614 | return 1; |
| 2615 | } |
| 2616 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2617 | static int shutdown_interception(struct vcpu_svm *svm) |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 2618 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2619 | struct kvm_run *kvm_run = svm->vcpu.run; |
| 2620 | |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 2621 | /* |
| 2622 | * VMCB is undefined after a SHUTDOWN intercept |
| 2623 | * so reinitialize it. |
| 2624 | */ |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2625 | clear_page(svm->vmcb); |
Paolo Bonzini | 5690891 | 2015-10-19 11:30:19 +0200 | [diff] [blame] | 2626 | init_vmcb(svm); |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 2627 | |
| 2628 | kvm_run->exit_reason = KVM_EXIT_SHUTDOWN; |
| 2629 | return 0; |
| 2630 | } |
| 2631 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2632 | static int io_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2633 | { |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 2634 | struct kvm_vcpu *vcpu = &svm->vcpu; |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2635 | u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */ |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 2636 | int size, in, string, ret; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 2637 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2638 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2639 | ++svm->vcpu.stat.io_exits; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 2640 | string = (io_info & SVM_IOIO_STR_MASK) != 0; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 2641 | in = (io_info & SVM_IOIO_TYPE_MASK) != 0; |
Tom Lendacky | 8370c3d | 2016-11-23 12:01:50 -0500 | [diff] [blame] | 2642 | if (string) |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 2643 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 2644 | |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 2645 | port = io_info >> 16; |
| 2646 | size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 2647 | svm->next_rip = svm->vmcb->control.exit_info_2; |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 2648 | ret = kvm_skip_emulated_instruction(&svm->vcpu); |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 2649 | |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 2650 | /* |
| 2651 | * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered |
| 2652 | * KVM_EXIT_DEBUG here. |
| 2653 | */ |
| 2654 | if (in) |
| 2655 | return kvm_fast_pio_in(vcpu, size, port) && ret; |
| 2656 | else |
| 2657 | return kvm_fast_pio_out(vcpu, size, port) && ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2658 | } |
| 2659 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2660 | static int nmi_interception(struct vcpu_svm *svm) |
Joerg Roedel | c47f098 | 2008-04-30 17:56:00 +0200 | [diff] [blame] | 2661 | { |
| 2662 | return 1; |
| 2663 | } |
| 2664 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2665 | static int intr_interception(struct vcpu_svm *svm) |
Joerg Roedel | a069805 | 2008-04-30 17:56:01 +0200 | [diff] [blame] | 2666 | { |
| 2667 | ++svm->vcpu.stat.irq_exits; |
| 2668 | return 1; |
| 2669 | } |
| 2670 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2671 | static int nop_on_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2672 | { |
| 2673 | return 1; |
| 2674 | } |
| 2675 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2676 | static int halt_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2677 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2678 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 1; |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2679 | return kvm_emulate_halt(&svm->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2680 | } |
| 2681 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2682 | static int vmmcall_interception(struct vcpu_svm *svm) |
Avi Kivity | 02e235b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2683 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2684 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
Andrey Smetanin | 0d9c055 | 2016-02-11 16:44:59 +0300 | [diff] [blame] | 2685 | return kvm_emulate_hypercall(&svm->vcpu); |
Avi Kivity | 02e235b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2686 | } |
| 2687 | |
Joerg Roedel | 5bd2edc | 2010-09-10 17:31:02 +0200 | [diff] [blame] | 2688 | static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu) |
| 2689 | { |
| 2690 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2691 | |
| 2692 | return svm->nested.nested_cr3; |
| 2693 | } |
| 2694 | |
Avi Kivity | e4e517b | 2011-07-28 11:36:17 +0300 | [diff] [blame] | 2695 | static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index) |
| 2696 | { |
| 2697 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2698 | u64 cr3 = svm->nested.nested_cr3; |
| 2699 | u64 pdpte; |
| 2700 | int ret; |
| 2701 | |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 2702 | ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(__sme_clr(cr3)), &pdpte, |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 2703 | offset_in_page(cr3) + index * 8, 8); |
Avi Kivity | e4e517b | 2011-07-28 11:36:17 +0300 | [diff] [blame] | 2704 | if (ret) |
| 2705 | return 0; |
| 2706 | return pdpte; |
| 2707 | } |
| 2708 | |
Joerg Roedel | 5bd2edc | 2010-09-10 17:31:02 +0200 | [diff] [blame] | 2709 | static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu, |
| 2710 | unsigned long root) |
| 2711 | { |
| 2712 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2713 | |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 2714 | svm->vmcb->control.nested_cr3 = __sme_set(root); |
Joerg Roedel | b274716 | 2010-12-03 11:45:53 +0100 | [diff] [blame] | 2715 | mark_dirty(svm->vmcb, VMCB_NPT); |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 2716 | svm_flush_tlb(vcpu, true); |
Joerg Roedel | 5bd2edc | 2010-09-10 17:31:02 +0200 | [diff] [blame] | 2717 | } |
| 2718 | |
Avi Kivity | 6389ee9 | 2010-11-29 16:12:30 +0200 | [diff] [blame] | 2719 | static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu, |
| 2720 | struct x86_exception *fault) |
Joerg Roedel | 5bd2edc | 2010-09-10 17:31:02 +0200 | [diff] [blame] | 2721 | { |
| 2722 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2723 | |
Paolo Bonzini | 5e35251 | 2014-09-02 13:18:37 +0200 | [diff] [blame] | 2724 | if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) { |
| 2725 | /* |
| 2726 | * TODO: track the cause of the nested page fault, and |
| 2727 | * correctly fill in the high bits of exit_info_1. |
| 2728 | */ |
| 2729 | svm->vmcb->control.exit_code = SVM_EXIT_NPF; |
| 2730 | svm->vmcb->control.exit_code_hi = 0; |
| 2731 | svm->vmcb->control.exit_info_1 = (1ULL << 32); |
| 2732 | svm->vmcb->control.exit_info_2 = fault->address; |
| 2733 | } |
| 2734 | |
| 2735 | svm->vmcb->control.exit_info_1 &= ~0xffffffffULL; |
| 2736 | svm->vmcb->control.exit_info_1 |= fault->error_code; |
| 2737 | |
| 2738 | /* |
| 2739 | * The present bit is always zero for page structure faults on real |
| 2740 | * hardware. |
| 2741 | */ |
| 2742 | if (svm->vmcb->control.exit_info_1 & (2ULL << 32)) |
| 2743 | svm->vmcb->control.exit_info_1 &= ~1; |
Joerg Roedel | 5bd2edc | 2010-09-10 17:31:02 +0200 | [diff] [blame] | 2744 | |
| 2745 | nested_svm_vmexit(svm); |
| 2746 | } |
| 2747 | |
Paolo Bonzini | 8a3c1a33 | 2013-10-02 16:56:13 +0200 | [diff] [blame] | 2748 | static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu) |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 2749 | { |
Paolo Bonzini | ad896af | 2013-10-02 16:56:14 +0200 | [diff] [blame] | 2750 | WARN_ON(mmu_is_nested(vcpu)); |
| 2751 | kvm_init_shadow_mmu(vcpu); |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 2752 | vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3; |
| 2753 | vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3; |
Avi Kivity | e4e517b | 2011-07-28 11:36:17 +0300 | [diff] [blame] | 2754 | vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr; |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 2755 | vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit; |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 2756 | vcpu->arch.mmu.shadow_root_level = get_npt_level(vcpu); |
Xiao Guangrong | c258b62 | 2015-08-05 12:04:24 +0800 | [diff] [blame] | 2757 | reset_shadow_zero_bits_mask(vcpu, &vcpu->arch.mmu); |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 2758 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 2759 | } |
| 2760 | |
| 2761 | static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu) |
| 2762 | { |
| 2763 | vcpu->arch.walk_mmu = &vcpu->arch.mmu; |
| 2764 | } |
| 2765 | |
Alexander Graf | c072542 | 2008-11-25 20:17:03 +0100 | [diff] [blame] | 2766 | static int nested_svm_check_permissions(struct vcpu_svm *svm) |
| 2767 | { |
Dan Carpenter | e9196ce | 2017-05-18 10:39:53 +0300 | [diff] [blame] | 2768 | if (!(svm->vcpu.arch.efer & EFER_SVME) || |
| 2769 | !is_paging(&svm->vcpu)) { |
Alexander Graf | c072542 | 2008-11-25 20:17:03 +0100 | [diff] [blame] | 2770 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
| 2771 | return 1; |
| 2772 | } |
| 2773 | |
| 2774 | if (svm->vmcb->save.cpl) { |
| 2775 | kvm_inject_gp(&svm->vcpu, 0); |
| 2776 | return 1; |
| 2777 | } |
| 2778 | |
Dan Carpenter | e9196ce | 2017-05-18 10:39:53 +0300 | [diff] [blame] | 2779 | return 0; |
Alexander Graf | c072542 | 2008-11-25 20:17:03 +0100 | [diff] [blame] | 2780 | } |
| 2781 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2782 | static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr, |
| 2783 | bool has_error_code, u32 error_code) |
| 2784 | { |
Joerg Roedel | b8e88bc | 2010-02-19 16:23:02 +0100 | [diff] [blame] | 2785 | int vmexit; |
| 2786 | |
Joerg Roedel | 2030753 | 2010-11-29 17:51:48 +0100 | [diff] [blame] | 2787 | if (!is_guest_mode(&svm->vcpu)) |
Joerg Roedel | 0295ad7 | 2009-08-07 11:49:37 +0200 | [diff] [blame] | 2788 | return 0; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2789 | |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 2790 | vmexit = nested_svm_intercept(svm); |
| 2791 | if (vmexit != NESTED_EXIT_DONE) |
| 2792 | return 0; |
| 2793 | |
Joerg Roedel | 0295ad7 | 2009-08-07 11:49:37 +0200 | [diff] [blame] | 2794 | svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr; |
| 2795 | svm->vmcb->control.exit_code_hi = 0; |
| 2796 | svm->vmcb->control.exit_info_1 = error_code; |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 2797 | |
| 2798 | /* |
| 2799 | * FIXME: we should not write CR2 when L1 intercepts an L2 #PF exception. |
| 2800 | * The fix is to add the ancillary datum (CR2 or DR6) to structs |
| 2801 | * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6 can be |
| 2802 | * written only when inject_pending_event runs (DR6 would written here |
| 2803 | * too). This should be conditional on a new capability---if the |
| 2804 | * capability is disabled, kvm_multiple_exception would write the |
| 2805 | * ancillary information to CR2 or DR6, for backwards ABI-compatibility. |
| 2806 | */ |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 2807 | if (svm->vcpu.arch.exception.nested_apf) |
| 2808 | svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token; |
| 2809 | else |
| 2810 | svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2; |
Joerg Roedel | 0295ad7 | 2009-08-07 11:49:37 +0200 | [diff] [blame] | 2811 | |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 2812 | svm->nested.exit_required = true; |
Joerg Roedel | b8e88bc | 2010-02-19 16:23:02 +0100 | [diff] [blame] | 2813 | return vmexit; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2814 | } |
| 2815 | |
Joerg Roedel | 8fe5465 | 2010-02-19 16:23:01 +0100 | [diff] [blame] | 2816 | /* This function returns true if it is save to enable the irq window */ |
| 2817 | static inline bool nested_svm_intr(struct vcpu_svm *svm) |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2818 | { |
Joerg Roedel | 2030753 | 2010-11-29 17:51:48 +0100 | [diff] [blame] | 2819 | if (!is_guest_mode(&svm->vcpu)) |
Joerg Roedel | 8fe5465 | 2010-02-19 16:23:01 +0100 | [diff] [blame] | 2820 | return true; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2821 | |
Joerg Roedel | 2666695 | 2009-08-07 11:49:46 +0200 | [diff] [blame] | 2822 | if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK)) |
Joerg Roedel | 8fe5465 | 2010-02-19 16:23:01 +0100 | [diff] [blame] | 2823 | return true; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2824 | |
Joerg Roedel | 2666695 | 2009-08-07 11:49:46 +0200 | [diff] [blame] | 2825 | if (!(svm->vcpu.arch.hflags & HF_HIF_MASK)) |
Joerg Roedel | 8fe5465 | 2010-02-19 16:23:01 +0100 | [diff] [blame] | 2826 | return false; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2827 | |
Gleb Natapov | a0a07cd | 2010-09-20 10:15:32 +0200 | [diff] [blame] | 2828 | /* |
| 2829 | * if vmexit was already requested (by intercepted exception |
| 2830 | * for instance) do not overwrite it with "external interrupt" |
| 2831 | * vmexit. |
| 2832 | */ |
| 2833 | if (svm->nested.exit_required) |
| 2834 | return false; |
| 2835 | |
Joerg Roedel | 197717d | 2010-02-24 18:59:19 +0100 | [diff] [blame] | 2836 | svm->vmcb->control.exit_code = SVM_EXIT_INTR; |
| 2837 | svm->vmcb->control.exit_info_1 = 0; |
| 2838 | svm->vmcb->control.exit_info_2 = 0; |
Joerg Roedel | 2666695 | 2009-08-07 11:49:46 +0200 | [diff] [blame] | 2839 | |
Joerg Roedel | cd3ff65 | 2009-10-09 16:08:26 +0200 | [diff] [blame] | 2840 | if (svm->nested.intercept & 1ULL) { |
| 2841 | /* |
| 2842 | * The #vmexit can't be emulated here directly because this |
Guo Chao | c5ec2e5 | 2012-06-28 15:16:43 +0800 | [diff] [blame] | 2843 | * code path runs with irqs and preemption disabled. A |
Joerg Roedel | cd3ff65 | 2009-10-09 16:08:26 +0200 | [diff] [blame] | 2844 | * #vmexit emulation might sleep. Only signal request for |
| 2845 | * the #vmexit here. |
| 2846 | */ |
| 2847 | svm->nested.exit_required = true; |
Joerg Roedel | 236649d | 2009-10-09 16:08:30 +0200 | [diff] [blame] | 2848 | trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip); |
Joerg Roedel | 8fe5465 | 2010-02-19 16:23:01 +0100 | [diff] [blame] | 2849 | return false; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2850 | } |
| 2851 | |
Joerg Roedel | 8fe5465 | 2010-02-19 16:23:01 +0100 | [diff] [blame] | 2852 | return true; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2853 | } |
| 2854 | |
Joerg Roedel | 887f500 | 2010-02-24 18:59:12 +0100 | [diff] [blame] | 2855 | /* This function returns true if it is save to enable the nmi window */ |
| 2856 | static inline bool nested_svm_nmi(struct vcpu_svm *svm) |
| 2857 | { |
Joerg Roedel | 2030753 | 2010-11-29 17:51:48 +0100 | [diff] [blame] | 2858 | if (!is_guest_mode(&svm->vcpu)) |
Joerg Roedel | 887f500 | 2010-02-24 18:59:12 +0100 | [diff] [blame] | 2859 | return true; |
| 2860 | |
| 2861 | if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI))) |
| 2862 | return true; |
| 2863 | |
| 2864 | svm->vmcb->control.exit_code = SVM_EXIT_NMI; |
| 2865 | svm->nested.exit_required = true; |
| 2866 | |
| 2867 | return false; |
| 2868 | } |
| 2869 | |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 2870 | 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] | 2871 | { |
| 2872 | struct page *page; |
| 2873 | |
Joerg Roedel | 6c3bd3d | 2010-02-19 16:23:04 +0100 | [diff] [blame] | 2874 | might_sleep(); |
| 2875 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 2876 | page = kvm_vcpu_gfn_to_page(&svm->vcpu, gpa >> PAGE_SHIFT); |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 2877 | if (is_error_page(page)) |
| 2878 | goto error; |
| 2879 | |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 2880 | *_page = page; |
| 2881 | |
| 2882 | return kmap(page); |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 2883 | |
| 2884 | error: |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 2885 | kvm_inject_gp(&svm->vcpu, 0); |
| 2886 | |
| 2887 | return NULL; |
| 2888 | } |
| 2889 | |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 2890 | static void nested_svm_unmap(struct page *page) |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 2891 | { |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 2892 | kunmap(page); |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 2893 | kvm_release_page_dirty(page); |
| 2894 | } |
| 2895 | |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 2896 | static int nested_svm_intercept_ioio(struct vcpu_svm *svm) |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2897 | { |
Jan Kiszka | 9bf4183 | 2014-06-30 10:54:17 +0200 | [diff] [blame] | 2898 | unsigned port, size, iopm_len; |
| 2899 | u16 val, mask; |
| 2900 | u8 start_bit; |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 2901 | u64 gpa; |
| 2902 | |
| 2903 | if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT))) |
| 2904 | return NESTED_EXIT_HOST; |
| 2905 | |
| 2906 | port = svm->vmcb->control.exit_info_1 >> 16; |
Jan Kiszka | 9bf4183 | 2014-06-30 10:54:17 +0200 | [diff] [blame] | 2907 | size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >> |
| 2908 | SVM_IOIO_SIZE_SHIFT; |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 2909 | gpa = svm->nested.vmcb_iopm + (port / 8); |
Jan Kiszka | 9bf4183 | 2014-06-30 10:54:17 +0200 | [diff] [blame] | 2910 | start_bit = port % 8; |
| 2911 | iopm_len = (start_bit + size > 8) ? 2 : 1; |
| 2912 | mask = (0xf >> (4 - size)) << start_bit; |
| 2913 | val = 0; |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 2914 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 2915 | if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len)) |
Jan Kiszka | 9bf4183 | 2014-06-30 10:54:17 +0200 | [diff] [blame] | 2916 | return NESTED_EXIT_DONE; |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 2917 | |
Jan Kiszka | 9bf4183 | 2014-06-30 10:54:17 +0200 | [diff] [blame] | 2918 | return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST; |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 2919 | } |
| 2920 | |
Joerg Roedel | d247782 | 2010-03-01 15:34:34 +0100 | [diff] [blame] | 2921 | static int nested_svm_exit_handled_msr(struct vcpu_svm *svm) |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2922 | { |
Joerg Roedel | 0d6b353 | 2010-03-01 15:34:38 +0100 | [diff] [blame] | 2923 | u32 offset, msr, value; |
| 2924 | int write, mask; |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 2925 | |
Joerg Roedel | 3d62d9a | 2009-08-07 11:49:39 +0200 | [diff] [blame] | 2926 | if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT))) |
Joerg Roedel | d247782 | 2010-03-01 15:34:34 +0100 | [diff] [blame] | 2927 | return NESTED_EXIT_HOST; |
Joerg Roedel | 3d62d9a | 2009-08-07 11:49:39 +0200 | [diff] [blame] | 2928 | |
Joerg Roedel | 0d6b353 | 2010-03-01 15:34:38 +0100 | [diff] [blame] | 2929 | msr = svm->vcpu.arch.regs[VCPU_REGS_RCX]; |
| 2930 | offset = svm_msrpm_offset(msr); |
| 2931 | write = svm->vmcb->control.exit_info_1 & 1; |
| 2932 | mask = 1 << ((2 * (msr & 0xf)) + write); |
Joerg Roedel | 3d62d9a | 2009-08-07 11:49:39 +0200 | [diff] [blame] | 2933 | |
Joerg Roedel | 0d6b353 | 2010-03-01 15:34:38 +0100 | [diff] [blame] | 2934 | if (offset == MSR_INVALID) |
| 2935 | return NESTED_EXIT_DONE; |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 2936 | |
Joerg Roedel | 0d6b353 | 2010-03-01 15:34:38 +0100 | [diff] [blame] | 2937 | /* Offset is in 32 bit units but need in 8 bit units */ |
| 2938 | offset *= 4; |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 2939 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 2940 | 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] | 2941 | return NESTED_EXIT_DONE; |
Joerg Roedel | 3d62d9a | 2009-08-07 11:49:39 +0200 | [diff] [blame] | 2942 | |
Joerg Roedel | 0d6b353 | 2010-03-01 15:34:38 +0100 | [diff] [blame] | 2943 | return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST; |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 2944 | } |
| 2945 | |
Ladi Prosek | ab2f4d73 | 2017-06-21 09:06:58 +0200 | [diff] [blame] | 2946 | /* DB exceptions for our internal use must not cause vmexit */ |
| 2947 | static int nested_svm_intercept_db(struct vcpu_svm *svm) |
| 2948 | { |
| 2949 | unsigned long dr6; |
| 2950 | |
| 2951 | /* if we're not singlestepping, it's not ours */ |
| 2952 | if (!svm->nmi_singlestep) |
| 2953 | return NESTED_EXIT_DONE; |
| 2954 | |
| 2955 | /* if it's not a singlestep exception, it's not ours */ |
| 2956 | if (kvm_get_dr(&svm->vcpu, 6, &dr6)) |
| 2957 | return NESTED_EXIT_DONE; |
| 2958 | if (!(dr6 & DR6_BS)) |
| 2959 | return NESTED_EXIT_DONE; |
| 2960 | |
| 2961 | /* if the guest is singlestepping, it should get the vmexit */ |
| 2962 | if (svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF) { |
| 2963 | disable_nmi_singlestep(svm); |
| 2964 | return NESTED_EXIT_DONE; |
| 2965 | } |
| 2966 | |
| 2967 | /* it's ours, the nested hypervisor must not see this one */ |
| 2968 | return NESTED_EXIT_HOST; |
| 2969 | } |
| 2970 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 2971 | static int nested_svm_exit_special(struct vcpu_svm *svm) |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 2972 | { |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2973 | u32 exit_code = svm->vmcb->control.exit_code; |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 2974 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 2975 | switch (exit_code) { |
| 2976 | case SVM_EXIT_INTR: |
| 2977 | case SVM_EXIT_NMI: |
Joerg Roedel | ff47a49 | 2010-04-22 12:33:14 +0200 | [diff] [blame] | 2978 | case SVM_EXIT_EXCP_BASE + MC_VECTOR: |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 2979 | return NESTED_EXIT_HOST; |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 2980 | case SVM_EXIT_NPF: |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 2981 | /* For now we are always handling NPFs when using them */ |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 2982 | if (npt_enabled) |
| 2983 | return NESTED_EXIT_HOST; |
| 2984 | break; |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 2985 | case SVM_EXIT_EXCP_BASE + PF_VECTOR: |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 2986 | /* When we're shadowing, trap PFs, but not async PF */ |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 2987 | if (!npt_enabled && svm->vcpu.arch.apf.host_apf_reason == 0) |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 2988 | return NESTED_EXIT_HOST; |
| 2989 | break; |
| 2990 | default: |
| 2991 | break; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2992 | } |
| 2993 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 2994 | return NESTED_EXIT_CONTINUE; |
| 2995 | } |
| 2996 | |
| 2997 | /* |
| 2998 | * If this function returns true, this #vmexit was already handled |
| 2999 | */ |
Joerg Roedel | b8e88bc | 2010-02-19 16:23:02 +0100 | [diff] [blame] | 3000 | static int nested_svm_intercept(struct vcpu_svm *svm) |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 3001 | { |
| 3002 | u32 exit_code = svm->vmcb->control.exit_code; |
| 3003 | int vmexit = NESTED_EXIT_HOST; |
| 3004 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3005 | switch (exit_code) { |
Joerg Roedel | 9c4e40b9 | 2009-08-07 11:49:36 +0200 | [diff] [blame] | 3006 | case SVM_EXIT_MSR: |
Joerg Roedel | 3d62d9a | 2009-08-07 11:49:39 +0200 | [diff] [blame] | 3007 | vmexit = nested_svm_exit_handled_msr(svm); |
Joerg Roedel | 9c4e40b9 | 2009-08-07 11:49:36 +0200 | [diff] [blame] | 3008 | break; |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 3009 | case SVM_EXIT_IOIO: |
| 3010 | vmexit = nested_svm_intercept_ioio(svm); |
| 3011 | break; |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 3012 | case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: { |
| 3013 | u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0); |
| 3014 | if (svm->nested.intercept_cr & bit) |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 3015 | vmexit = NESTED_EXIT_DONE; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3016 | break; |
| 3017 | } |
Joerg Roedel | 3aed041 | 2010-11-30 18:03:58 +0100 | [diff] [blame] | 3018 | case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: { |
| 3019 | u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0); |
| 3020 | if (svm->nested.intercept_dr & bit) |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 3021 | vmexit = NESTED_EXIT_DONE; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3022 | break; |
| 3023 | } |
| 3024 | case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: { |
| 3025 | u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE); |
Ladi Prosek | ab2f4d73 | 2017-06-21 09:06:58 +0200 | [diff] [blame] | 3026 | if (svm->nested.intercept_exceptions & excp_bits) { |
| 3027 | if (exit_code == SVM_EXIT_EXCP_BASE + DB_VECTOR) |
| 3028 | vmexit = nested_svm_intercept_db(svm); |
| 3029 | else |
| 3030 | vmexit = NESTED_EXIT_DONE; |
| 3031 | } |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 3032 | /* async page fault always cause vmexit */ |
| 3033 | else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) && |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 3034 | svm->vcpu.arch.exception.nested_apf != 0) |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 3035 | vmexit = NESTED_EXIT_DONE; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3036 | break; |
| 3037 | } |
Joerg Roedel | 228070b | 2010-04-22 12:33:10 +0200 | [diff] [blame] | 3038 | case SVM_EXIT_ERR: { |
| 3039 | vmexit = NESTED_EXIT_DONE; |
| 3040 | break; |
| 3041 | } |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3042 | default: { |
| 3043 | u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR); |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 3044 | if (svm->nested.intercept & exit_bits) |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 3045 | vmexit = NESTED_EXIT_DONE; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3046 | } |
| 3047 | } |
| 3048 | |
Joerg Roedel | b8e88bc | 2010-02-19 16:23:02 +0100 | [diff] [blame] | 3049 | return vmexit; |
| 3050 | } |
| 3051 | |
| 3052 | static int nested_svm_exit_handled(struct vcpu_svm *svm) |
| 3053 | { |
| 3054 | int vmexit; |
| 3055 | |
| 3056 | vmexit = nested_svm_intercept(svm); |
| 3057 | |
| 3058 | if (vmexit == NESTED_EXIT_DONE) |
Joerg Roedel | 9c4e40b9 | 2009-08-07 11:49:36 +0200 | [diff] [blame] | 3059 | nested_svm_vmexit(svm); |
Joerg Roedel | 9c4e40b9 | 2009-08-07 11:49:36 +0200 | [diff] [blame] | 3060 | |
| 3061 | return vmexit; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3062 | } |
| 3063 | |
Joerg Roedel | 0460a97 | 2009-08-07 11:49:31 +0200 | [diff] [blame] | 3064 | static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb) |
| 3065 | { |
| 3066 | struct vmcb_control_area *dst = &dst_vmcb->control; |
| 3067 | struct vmcb_control_area *from = &from_vmcb->control; |
| 3068 | |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 3069 | dst->intercept_cr = from->intercept_cr; |
Joerg Roedel | 3aed041 | 2010-11-30 18:03:58 +0100 | [diff] [blame] | 3070 | dst->intercept_dr = from->intercept_dr; |
Joerg Roedel | 0460a97 | 2009-08-07 11:49:31 +0200 | [diff] [blame] | 3071 | dst->intercept_exceptions = from->intercept_exceptions; |
| 3072 | dst->intercept = from->intercept; |
| 3073 | dst->iopm_base_pa = from->iopm_base_pa; |
| 3074 | dst->msrpm_base_pa = from->msrpm_base_pa; |
| 3075 | dst->tsc_offset = from->tsc_offset; |
| 3076 | dst->asid = from->asid; |
| 3077 | dst->tlb_ctl = from->tlb_ctl; |
| 3078 | dst->int_ctl = from->int_ctl; |
| 3079 | dst->int_vector = from->int_vector; |
| 3080 | dst->int_state = from->int_state; |
| 3081 | dst->exit_code = from->exit_code; |
| 3082 | dst->exit_code_hi = from->exit_code_hi; |
| 3083 | dst->exit_info_1 = from->exit_info_1; |
| 3084 | dst->exit_info_2 = from->exit_info_2; |
| 3085 | dst->exit_int_info = from->exit_int_info; |
| 3086 | dst->exit_int_info_err = from->exit_int_info_err; |
| 3087 | dst->nested_ctl = from->nested_ctl; |
| 3088 | dst->event_inj = from->event_inj; |
| 3089 | dst->event_inj_err = from->event_inj_err; |
| 3090 | dst->nested_cr3 = from->nested_cr3; |
Janakarajan Natarajan | 0dc9211 | 2017-07-06 15:50:45 -0500 | [diff] [blame] | 3091 | dst->virt_ext = from->virt_ext; |
Joerg Roedel | 0460a97 | 2009-08-07 11:49:31 +0200 | [diff] [blame] | 3092 | } |
| 3093 | |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 3094 | static int nested_svm_vmexit(struct vcpu_svm *svm) |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3095 | { |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 3096 | struct vmcb *nested_vmcb; |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 3097 | struct vmcb *hsave = svm->nested.hsave; |
Joerg Roedel | 33740e4 | 2009-08-07 11:49:29 +0200 | [diff] [blame] | 3098 | struct vmcb *vmcb = svm->vmcb; |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 3099 | struct page *page; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3100 | |
Joerg Roedel | 17897f3 | 2009-10-09 16:08:29 +0200 | [diff] [blame] | 3101 | trace_kvm_nested_vmexit_inject(vmcb->control.exit_code, |
| 3102 | vmcb->control.exit_info_1, |
| 3103 | vmcb->control.exit_info_2, |
| 3104 | vmcb->control.exit_int_info, |
Stefan Hajnoczi | e097e5f | 2011-07-22 12:46:52 +0100 | [diff] [blame] | 3105 | vmcb->control.exit_int_info_err, |
| 3106 | KVM_ISA_SVM); |
Joerg Roedel | 17897f3 | 2009-10-09 16:08:29 +0200 | [diff] [blame] | 3107 | |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 3108 | nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page); |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 3109 | if (!nested_vmcb) |
| 3110 | return 1; |
| 3111 | |
Joerg Roedel | 2030753 | 2010-11-29 17:51:48 +0100 | [diff] [blame] | 3112 | /* Exit Guest-Mode */ |
| 3113 | leave_guest_mode(&svm->vcpu); |
Joerg Roedel | 06fc7772 | 2010-02-19 16:23:07 +0100 | [diff] [blame] | 3114 | svm->nested.vmcb = 0; |
| 3115 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3116 | /* Give the current vmcb to the guest */ |
Joerg Roedel | 33740e4 | 2009-08-07 11:49:29 +0200 | [diff] [blame] | 3117 | disable_gif(svm); |
| 3118 | |
| 3119 | nested_vmcb->save.es = vmcb->save.es; |
| 3120 | nested_vmcb->save.cs = vmcb->save.cs; |
| 3121 | nested_vmcb->save.ss = vmcb->save.ss; |
| 3122 | nested_vmcb->save.ds = vmcb->save.ds; |
| 3123 | nested_vmcb->save.gdtr = vmcb->save.gdtr; |
| 3124 | nested_vmcb->save.idtr = vmcb->save.idtr; |
Joerg Roedel | 3f6a9d1 | 2010-07-27 18:14:20 +0200 | [diff] [blame] | 3125 | nested_vmcb->save.efer = svm->vcpu.arch.efer; |
Joerg Roedel | cdbbdc1 | 2010-02-19 16:23:03 +0100 | [diff] [blame] | 3126 | nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu); |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 3127 | nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu); |
Joerg Roedel | 33740e4 | 2009-08-07 11:49:29 +0200 | [diff] [blame] | 3128 | nested_vmcb->save.cr2 = vmcb->save.cr2; |
Joerg Roedel | cdbbdc1 | 2010-02-19 16:23:03 +0100 | [diff] [blame] | 3129 | nested_vmcb->save.cr4 = svm->vcpu.arch.cr4; |
Avi Kivity | f6e7847 | 2010-08-02 15:30:20 +0300 | [diff] [blame] | 3130 | nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu); |
Joerg Roedel | 33740e4 | 2009-08-07 11:49:29 +0200 | [diff] [blame] | 3131 | nested_vmcb->save.rip = vmcb->save.rip; |
| 3132 | nested_vmcb->save.rsp = vmcb->save.rsp; |
| 3133 | nested_vmcb->save.rax = vmcb->save.rax; |
| 3134 | nested_vmcb->save.dr7 = vmcb->save.dr7; |
| 3135 | nested_vmcb->save.dr6 = vmcb->save.dr6; |
| 3136 | nested_vmcb->save.cpl = vmcb->save.cpl; |
| 3137 | |
| 3138 | nested_vmcb->control.int_ctl = vmcb->control.int_ctl; |
| 3139 | nested_vmcb->control.int_vector = vmcb->control.int_vector; |
| 3140 | nested_vmcb->control.int_state = vmcb->control.int_state; |
| 3141 | nested_vmcb->control.exit_code = vmcb->control.exit_code; |
| 3142 | nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi; |
| 3143 | nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1; |
| 3144 | nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2; |
| 3145 | nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info; |
| 3146 | 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] | 3147 | |
| 3148 | if (svm->nrips_enabled) |
| 3149 | nested_vmcb->control.next_rip = vmcb->control.next_rip; |
Alexander Graf | 8d23c46 | 2009-10-09 16:08:25 +0200 | [diff] [blame] | 3150 | |
| 3151 | /* |
| 3152 | * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have |
| 3153 | * to make sure that we do not lose injected events. So check event_inj |
| 3154 | * here and copy it to exit_int_info if it is valid. |
| 3155 | * Exit_int_info and event_inj can't be both valid because the case |
| 3156 | * below only happens on a VMRUN instruction intercept which has |
| 3157 | * no valid exit_int_info set. |
| 3158 | */ |
| 3159 | if (vmcb->control.event_inj & SVM_EVTINJ_VALID) { |
| 3160 | struct vmcb_control_area *nc = &nested_vmcb->control; |
| 3161 | |
| 3162 | nc->exit_int_info = vmcb->control.event_inj; |
| 3163 | nc->exit_int_info_err = vmcb->control.event_inj_err; |
| 3164 | } |
| 3165 | |
Joerg Roedel | 33740e4 | 2009-08-07 11:49:29 +0200 | [diff] [blame] | 3166 | nested_vmcb->control.tlb_ctl = 0; |
| 3167 | nested_vmcb->control.event_inj = 0; |
| 3168 | nested_vmcb->control.event_inj_err = 0; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3169 | |
| 3170 | /* We always set V_INTR_MASKING and remember the old value in hflags */ |
| 3171 | if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK)) |
| 3172 | nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK; |
| 3173 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3174 | /* Restore the original control entries */ |
Joerg Roedel | 0460a97 | 2009-08-07 11:49:31 +0200 | [diff] [blame] | 3175 | copy_vmcb_control_area(vmcb, hsave); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3176 | |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 3177 | kvm_clear_exception_queue(&svm->vcpu); |
| 3178 | kvm_clear_interrupt_queue(&svm->vcpu); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3179 | |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 3180 | svm->nested.nested_cr3 = 0; |
| 3181 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3182 | /* Restore selected save entries */ |
| 3183 | svm->vmcb->save.es = hsave->save.es; |
| 3184 | svm->vmcb->save.cs = hsave->save.cs; |
| 3185 | svm->vmcb->save.ss = hsave->save.ss; |
| 3186 | svm->vmcb->save.ds = hsave->save.ds; |
| 3187 | svm->vmcb->save.gdtr = hsave->save.gdtr; |
| 3188 | svm->vmcb->save.idtr = hsave->save.idtr; |
Avi Kivity | f6e7847 | 2010-08-02 15:30:20 +0300 | [diff] [blame] | 3189 | kvm_set_rflags(&svm->vcpu, hsave->save.rflags); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3190 | svm_set_efer(&svm->vcpu, hsave->save.efer); |
| 3191 | svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE); |
| 3192 | svm_set_cr4(&svm->vcpu, hsave->save.cr4); |
| 3193 | if (npt_enabled) { |
| 3194 | svm->vmcb->save.cr3 = hsave->save.cr3; |
| 3195 | svm->vcpu.arch.cr3 = hsave->save.cr3; |
| 3196 | } else { |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 3197 | (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3198 | } |
| 3199 | kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax); |
| 3200 | kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp); |
| 3201 | kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip); |
| 3202 | svm->vmcb->save.dr7 = 0; |
| 3203 | svm->vmcb->save.cpl = 0; |
| 3204 | svm->vmcb->control.exit_int_info = 0; |
| 3205 | |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 3206 | mark_all_dirty(svm->vmcb); |
| 3207 | |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 3208 | nested_svm_unmap(page); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3209 | |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 3210 | nested_svm_uninit_mmu_context(&svm->vcpu); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3211 | kvm_mmu_reset_context(&svm->vcpu); |
| 3212 | kvm_mmu_load(&svm->vcpu); |
| 3213 | |
| 3214 | return 0; |
| 3215 | } |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3216 | |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 3217 | static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm) |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3218 | { |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 3219 | /* |
| 3220 | * This function merges the msr permission bitmaps of kvm and the |
Guo Chao | c5ec2e5 | 2012-06-28 15:16:43 +0800 | [diff] [blame] | 3221 | * 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] | 3222 | * the kvm msr permission bitmap may contain zero bits |
| 3223 | */ |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3224 | int i; |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 3225 | |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 3226 | if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT))) |
| 3227 | return true; |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 3228 | |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 3229 | for (i = 0; i < MSRPM_OFFSETS; i++) { |
| 3230 | u32 value, p; |
| 3231 | u64 offset; |
| 3232 | |
| 3233 | if (msrpm_offsets[i] == 0xffffffff) |
| 3234 | break; |
| 3235 | |
Joerg Roedel | 0d6b353 | 2010-03-01 15:34:38 +0100 | [diff] [blame] | 3236 | p = msrpm_offsets[i]; |
| 3237 | offset = svm->nested.vmcb_msrpm + (p * 4); |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 3238 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 3239 | if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4)) |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 3240 | return false; |
| 3241 | |
| 3242 | svm->nested.msrpm[p] = svm->msrpm[p] | value; |
| 3243 | } |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 3244 | |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 3245 | svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm)); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3246 | |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 3247 | return true; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3248 | } |
| 3249 | |
Joerg Roedel | 52c65a30 | 2010-08-02 16:46:44 +0200 | [diff] [blame] | 3250 | static bool nested_vmcb_checks(struct vmcb *vmcb) |
| 3251 | { |
| 3252 | if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0) |
| 3253 | return false; |
| 3254 | |
Joerg Roedel | dbe7758 | 2010-08-02 16:46:45 +0200 | [diff] [blame] | 3255 | if (vmcb->control.asid == 0) |
| 3256 | return false; |
| 3257 | |
Tom Lendacky | cea3a19 | 2017-12-04 10:57:24 -0600 | [diff] [blame] | 3258 | if ((vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) && |
| 3259 | !npt_enabled) |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 3260 | return false; |
| 3261 | |
Joerg Roedel | 52c65a30 | 2010-08-02 16:46:44 +0200 | [diff] [blame] | 3262 | return true; |
| 3263 | } |
| 3264 | |
Ladi Prosek | c263406 | 2017-10-11 16:54:44 +0200 | [diff] [blame] | 3265 | static void enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa, |
| 3266 | struct vmcb *nested_vmcb, struct page *page) |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3267 | { |
Avi Kivity | f6e7847 | 2010-08-02 15:30:20 +0300 | [diff] [blame] | 3268 | if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF) |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3269 | svm->vcpu.arch.hflags |= HF_HIF_MASK; |
| 3270 | else |
| 3271 | svm->vcpu.arch.hflags &= ~HF_HIF_MASK; |
| 3272 | |
Tom Lendacky | cea3a19 | 2017-12-04 10:57:24 -0600 | [diff] [blame] | 3273 | if (nested_vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) { |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 3274 | kvm_mmu_unload(&svm->vcpu); |
| 3275 | svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3; |
| 3276 | nested_svm_init_mmu_context(&svm->vcpu); |
| 3277 | } |
| 3278 | |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3279 | /* Load the nested guest state */ |
| 3280 | svm->vmcb->save.es = nested_vmcb->save.es; |
| 3281 | svm->vmcb->save.cs = nested_vmcb->save.cs; |
| 3282 | svm->vmcb->save.ss = nested_vmcb->save.ss; |
| 3283 | svm->vmcb->save.ds = nested_vmcb->save.ds; |
| 3284 | svm->vmcb->save.gdtr = nested_vmcb->save.gdtr; |
| 3285 | svm->vmcb->save.idtr = nested_vmcb->save.idtr; |
Avi Kivity | f6e7847 | 2010-08-02 15:30:20 +0300 | [diff] [blame] | 3286 | kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3287 | svm_set_efer(&svm->vcpu, nested_vmcb->save.efer); |
| 3288 | svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0); |
| 3289 | svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4); |
| 3290 | if (npt_enabled) { |
| 3291 | svm->vmcb->save.cr3 = nested_vmcb->save.cr3; |
| 3292 | svm->vcpu.arch.cr3 = nested_vmcb->save.cr3; |
Joerg Roedel | 0e5cbe3 | 2010-02-24 18:59:11 +0100 | [diff] [blame] | 3293 | } else |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 3294 | (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3); |
Joerg Roedel | 0e5cbe3 | 2010-02-24 18:59:11 +0100 | [diff] [blame] | 3295 | |
| 3296 | /* Guest paging mode is active - reset mmu */ |
| 3297 | kvm_mmu_reset_context(&svm->vcpu); |
| 3298 | |
Joerg Roedel | defbba5 | 2009-08-07 11:49:30 +0200 | [diff] [blame] | 3299 | svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3300 | kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax); |
| 3301 | kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp); |
| 3302 | kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip); |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 3303 | |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3304 | /* In case we don't even reach vcpu_run, the fields are not updated */ |
| 3305 | svm->vmcb->save.rax = nested_vmcb->save.rax; |
| 3306 | svm->vmcb->save.rsp = nested_vmcb->save.rsp; |
| 3307 | svm->vmcb->save.rip = nested_vmcb->save.rip; |
| 3308 | svm->vmcb->save.dr7 = nested_vmcb->save.dr7; |
| 3309 | svm->vmcb->save.dr6 = nested_vmcb->save.dr6; |
| 3310 | svm->vmcb->save.cpl = nested_vmcb->save.cpl; |
| 3311 | |
Joerg Roedel | f713853 | 2010-03-01 15:34:40 +0100 | [diff] [blame] | 3312 | svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL; |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 3313 | svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3314 | |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 3315 | /* cache intercepts */ |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 3316 | svm->nested.intercept_cr = nested_vmcb->control.intercept_cr; |
Joerg Roedel | 3aed041 | 2010-11-30 18:03:58 +0100 | [diff] [blame] | 3317 | svm->nested.intercept_dr = nested_vmcb->control.intercept_dr; |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 3318 | svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions; |
| 3319 | svm->nested.intercept = nested_vmcb->control.intercept; |
| 3320 | |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 3321 | svm_flush_tlb(&svm->vcpu, true); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3322 | 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] | 3323 | if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK) |
| 3324 | svm->vcpu.arch.hflags |= HF_VINTR_MASK; |
| 3325 | else |
| 3326 | svm->vcpu.arch.hflags &= ~HF_VINTR_MASK; |
| 3327 | |
Joerg Roedel | 88ab24a | 2010-02-19 16:23:06 +0100 | [diff] [blame] | 3328 | if (svm->vcpu.arch.hflags & HF_VINTR_MASK) { |
| 3329 | /* We only want the cr8 intercept bits of the guest */ |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 3330 | clr_cr_intercept(svm, INTERCEPT_CR8_READ); |
| 3331 | clr_cr_intercept(svm, INTERCEPT_CR8_WRITE); |
Joerg Roedel | 88ab24a | 2010-02-19 16:23:06 +0100 | [diff] [blame] | 3332 | } |
| 3333 | |
Joerg Roedel | 0d945bd | 2010-05-05 16:04:45 +0200 | [diff] [blame] | 3334 | /* We don't want to see VMMCALLs from a nested guest */ |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 3335 | clr_intercept(svm, INTERCEPT_VMMCALL); |
Joerg Roedel | 0d945bd | 2010-05-05 16:04:45 +0200 | [diff] [blame] | 3336 | |
Janakarajan Natarajan | 0dc9211 | 2017-07-06 15:50:45 -0500 | [diff] [blame] | 3337 | svm->vmcb->control.virt_ext = nested_vmcb->control.virt_ext; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3338 | svm->vmcb->control.int_vector = nested_vmcb->control.int_vector; |
| 3339 | svm->vmcb->control.int_state = nested_vmcb->control.int_state; |
| 3340 | svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3341 | svm->vmcb->control.event_inj = nested_vmcb->control.event_inj; |
| 3342 | svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err; |
| 3343 | |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 3344 | nested_svm_unmap(page); |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 3345 | |
Joerg Roedel | 2030753 | 2010-11-29 17:51:48 +0100 | [diff] [blame] | 3346 | /* Enter Guest-Mode */ |
| 3347 | enter_guest_mode(&svm->vcpu); |
| 3348 | |
Joerg Roedel | 384c636 | 2010-11-30 18:03:56 +0100 | [diff] [blame] | 3349 | /* |
| 3350 | * Merge guest and host intercepts - must be called with vcpu in |
| 3351 | * guest-mode to take affect here |
| 3352 | */ |
| 3353 | recalc_intercepts(svm); |
| 3354 | |
Joerg Roedel | 06fc7772 | 2010-02-19 16:23:07 +0100 | [diff] [blame] | 3355 | svm->nested.vmcb = vmcb_gpa; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3356 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 3357 | enable_gif(svm); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3358 | |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 3359 | mark_all_dirty(svm->vmcb); |
Ladi Prosek | c263406 | 2017-10-11 16:54:44 +0200 | [diff] [blame] | 3360 | } |
| 3361 | |
| 3362 | static bool nested_svm_vmrun(struct vcpu_svm *svm) |
| 3363 | { |
| 3364 | struct vmcb *nested_vmcb; |
| 3365 | struct vmcb *hsave = svm->nested.hsave; |
| 3366 | struct vmcb *vmcb = svm->vmcb; |
| 3367 | struct page *page; |
| 3368 | u64 vmcb_gpa; |
| 3369 | |
| 3370 | vmcb_gpa = svm->vmcb->save.rax; |
| 3371 | |
| 3372 | nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page); |
| 3373 | if (!nested_vmcb) |
| 3374 | return false; |
| 3375 | |
| 3376 | if (!nested_vmcb_checks(nested_vmcb)) { |
| 3377 | nested_vmcb->control.exit_code = SVM_EXIT_ERR; |
| 3378 | nested_vmcb->control.exit_code_hi = 0; |
| 3379 | nested_vmcb->control.exit_info_1 = 0; |
| 3380 | nested_vmcb->control.exit_info_2 = 0; |
| 3381 | |
| 3382 | nested_svm_unmap(page); |
| 3383 | |
| 3384 | return false; |
| 3385 | } |
| 3386 | |
| 3387 | trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa, |
| 3388 | nested_vmcb->save.rip, |
| 3389 | nested_vmcb->control.int_ctl, |
| 3390 | nested_vmcb->control.event_inj, |
| 3391 | nested_vmcb->control.nested_ctl); |
| 3392 | |
| 3393 | trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff, |
| 3394 | nested_vmcb->control.intercept_cr >> 16, |
| 3395 | nested_vmcb->control.intercept_exceptions, |
| 3396 | nested_vmcb->control.intercept); |
| 3397 | |
| 3398 | /* Clear internal status */ |
| 3399 | kvm_clear_exception_queue(&svm->vcpu); |
| 3400 | kvm_clear_interrupt_queue(&svm->vcpu); |
| 3401 | |
| 3402 | /* |
| 3403 | * Save the old vmcb, so we don't need to pick what we save, but can |
| 3404 | * restore everything when a VMEXIT occurs |
| 3405 | */ |
| 3406 | hsave->save.es = vmcb->save.es; |
| 3407 | hsave->save.cs = vmcb->save.cs; |
| 3408 | hsave->save.ss = vmcb->save.ss; |
| 3409 | hsave->save.ds = vmcb->save.ds; |
| 3410 | hsave->save.gdtr = vmcb->save.gdtr; |
| 3411 | hsave->save.idtr = vmcb->save.idtr; |
| 3412 | hsave->save.efer = svm->vcpu.arch.efer; |
| 3413 | hsave->save.cr0 = kvm_read_cr0(&svm->vcpu); |
| 3414 | hsave->save.cr4 = svm->vcpu.arch.cr4; |
| 3415 | hsave->save.rflags = kvm_get_rflags(&svm->vcpu); |
| 3416 | hsave->save.rip = kvm_rip_read(&svm->vcpu); |
| 3417 | hsave->save.rsp = vmcb->save.rsp; |
| 3418 | hsave->save.rax = vmcb->save.rax; |
| 3419 | if (npt_enabled) |
| 3420 | hsave->save.cr3 = vmcb->save.cr3; |
| 3421 | else |
| 3422 | hsave->save.cr3 = kvm_read_cr3(&svm->vcpu); |
| 3423 | |
| 3424 | copy_vmcb_control_area(hsave, vmcb); |
| 3425 | |
| 3426 | enter_svm_guest_mode(svm, vmcb_gpa, nested_vmcb, page); |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 3427 | |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 3428 | return true; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3429 | } |
| 3430 | |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 3431 | 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] | 3432 | { |
| 3433 | to_vmcb->save.fs = from_vmcb->save.fs; |
| 3434 | to_vmcb->save.gs = from_vmcb->save.gs; |
| 3435 | to_vmcb->save.tr = from_vmcb->save.tr; |
| 3436 | to_vmcb->save.ldtr = from_vmcb->save.ldtr; |
| 3437 | to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base; |
| 3438 | to_vmcb->save.star = from_vmcb->save.star; |
| 3439 | to_vmcb->save.lstar = from_vmcb->save.lstar; |
| 3440 | to_vmcb->save.cstar = from_vmcb->save.cstar; |
| 3441 | to_vmcb->save.sfmask = from_vmcb->save.sfmask; |
| 3442 | to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs; |
| 3443 | to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp; |
| 3444 | to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip; |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 3445 | } |
| 3446 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3447 | static int vmload_interception(struct vcpu_svm *svm) |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 3448 | { |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 3449 | struct vmcb *nested_vmcb; |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 3450 | struct page *page; |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3451 | int ret; |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 3452 | |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 3453 | if (nested_svm_check_permissions(svm)) |
| 3454 | return 1; |
| 3455 | |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 3456 | nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page); |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 3457 | if (!nested_vmcb) |
| 3458 | return 1; |
| 3459 | |
Joerg Roedel | e3e9ed3 | 2011-04-06 12:30:03 +0200 | [diff] [blame] | 3460 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3461 | ret = kvm_skip_emulated_instruction(&svm->vcpu); |
Joerg Roedel | e3e9ed3 | 2011-04-06 12:30:03 +0200 | [diff] [blame] | 3462 | |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 3463 | nested_svm_vmloadsave(nested_vmcb, svm->vmcb); |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 3464 | nested_svm_unmap(page); |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 3465 | |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3466 | return ret; |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 3467 | } |
| 3468 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3469 | static int vmsave_interception(struct vcpu_svm *svm) |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 3470 | { |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 3471 | struct vmcb *nested_vmcb; |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 3472 | struct page *page; |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3473 | int ret; |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 3474 | |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 3475 | if (nested_svm_check_permissions(svm)) |
| 3476 | return 1; |
| 3477 | |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 3478 | nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page); |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 3479 | if (!nested_vmcb) |
| 3480 | return 1; |
| 3481 | |
Joerg Roedel | e3e9ed3 | 2011-04-06 12:30:03 +0200 | [diff] [blame] | 3482 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3483 | ret = kvm_skip_emulated_instruction(&svm->vcpu); |
Joerg Roedel | e3e9ed3 | 2011-04-06 12:30:03 +0200 | [diff] [blame] | 3484 | |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 3485 | nested_svm_vmloadsave(svm->vmcb, nested_vmcb); |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 3486 | nested_svm_unmap(page); |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 3487 | |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3488 | return ret; |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 3489 | } |
| 3490 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3491 | static int vmrun_interception(struct vcpu_svm *svm) |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3492 | { |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3493 | if (nested_svm_check_permissions(svm)) |
| 3494 | return 1; |
| 3495 | |
Roedel, Joerg | b75f4eb | 2010-09-03 14:21:40 +0200 | [diff] [blame] | 3496 | /* Save rip after vmrun instruction */ |
| 3497 | kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3498 | |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 3499 | if (!nested_svm_vmrun(svm)) |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3500 | return 1; |
| 3501 | |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 3502 | if (!nested_svm_vmrun_msrpm(svm)) |
Joerg Roedel | 1f8da47 | 2009-08-07 11:49:43 +0200 | [diff] [blame] | 3503 | goto failed; |
| 3504 | |
| 3505 | return 1; |
| 3506 | |
| 3507 | failed: |
| 3508 | |
| 3509 | svm->vmcb->control.exit_code = SVM_EXIT_ERR; |
| 3510 | svm->vmcb->control.exit_code_hi = 0; |
| 3511 | svm->vmcb->control.exit_info_1 = 0; |
| 3512 | svm->vmcb->control.exit_info_2 = 0; |
| 3513 | |
| 3514 | nested_svm_vmexit(svm); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3515 | |
| 3516 | return 1; |
| 3517 | } |
| 3518 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3519 | static int stgi_interception(struct vcpu_svm *svm) |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3520 | { |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3521 | int ret; |
| 3522 | |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3523 | if (nested_svm_check_permissions(svm)) |
| 3524 | return 1; |
| 3525 | |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 3526 | /* |
| 3527 | * If VGIF is enabled, the STGI intercept is only added to |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 3528 | * detect the opening of the SMI/NMI window; remove it now. |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 3529 | */ |
| 3530 | if (vgif_enabled(svm)) |
| 3531 | clr_intercept(svm, INTERCEPT_STGI); |
| 3532 | |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3533 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3534 | ret = kvm_skip_emulated_instruction(&svm->vcpu); |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 3535 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3536 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 3537 | enable_gif(svm); |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3538 | |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3539 | return ret; |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3540 | } |
| 3541 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3542 | static int clgi_interception(struct vcpu_svm *svm) |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3543 | { |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3544 | int ret; |
| 3545 | |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3546 | if (nested_svm_check_permissions(svm)) |
| 3547 | return 1; |
| 3548 | |
| 3549 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3550 | ret = kvm_skip_emulated_instruction(&svm->vcpu); |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3551 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 3552 | disable_gif(svm); |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3553 | |
| 3554 | /* After a CLGI no interrupts should come */ |
Suravee Suthikulpanit | 340d3bc | 2016-05-04 14:09:47 -0500 | [diff] [blame] | 3555 | if (!kvm_vcpu_apicv_active(&svm->vcpu)) { |
| 3556 | svm_clear_vintr(svm); |
| 3557 | svm->vmcb->control.int_ctl &= ~V_IRQ_MASK; |
| 3558 | mark_dirty(svm->vmcb, VMCB_INTR); |
| 3559 | } |
Joerg Roedel | decdbf6 | 2010-12-03 11:45:52 +0100 | [diff] [blame] | 3560 | |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3561 | return ret; |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3562 | } |
| 3563 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3564 | static int invlpga_interception(struct vcpu_svm *svm) |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 3565 | { |
| 3566 | struct kvm_vcpu *vcpu = &svm->vcpu; |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 3567 | |
David Kaplan | 668f198 | 2015-02-20 16:02:10 -0600 | [diff] [blame] | 3568 | trace_kvm_invlpga(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RCX), |
| 3569 | kvm_register_read(&svm->vcpu, VCPU_REGS_RAX)); |
Joerg Roedel | ec1ff79 | 2009-10-09 16:08:31 +0200 | [diff] [blame] | 3570 | |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 3571 | /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */ |
David Kaplan | 668f198 | 2015-02-20 16:02:10 -0600 | [diff] [blame] | 3572 | kvm_mmu_invlpg(vcpu, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX)); |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 3573 | |
| 3574 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3575 | return kvm_skip_emulated_instruction(&svm->vcpu); |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 3576 | } |
| 3577 | |
Joerg Roedel | 532a46b | 2009-10-09 16:08:32 +0200 | [diff] [blame] | 3578 | static int skinit_interception(struct vcpu_svm *svm) |
| 3579 | { |
David Kaplan | 668f198 | 2015-02-20 16:02:10 -0600 | [diff] [blame] | 3580 | 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] | 3581 | |
| 3582 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
| 3583 | return 1; |
| 3584 | } |
| 3585 | |
David Kaplan | dab429a | 2015-03-02 13:43:37 -0600 | [diff] [blame] | 3586 | static int wbinvd_interception(struct vcpu_svm *svm) |
| 3587 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 3588 | return kvm_emulate_wbinvd(&svm->vcpu); |
David Kaplan | dab429a | 2015-03-02 13:43:37 -0600 | [diff] [blame] | 3589 | } |
| 3590 | |
Joerg Roedel | 81dd35d | 2010-12-07 17:15:06 +0100 | [diff] [blame] | 3591 | static int xsetbv_interception(struct vcpu_svm *svm) |
| 3592 | { |
| 3593 | u64 new_bv = kvm_read_edx_eax(&svm->vcpu); |
| 3594 | u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX); |
| 3595 | |
| 3596 | if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) { |
| 3597 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3598 | return kvm_skip_emulated_instruction(&svm->vcpu); |
Joerg Roedel | 81dd35d | 2010-12-07 17:15:06 +0100 | [diff] [blame] | 3599 | } |
| 3600 | |
| 3601 | return 1; |
| 3602 | } |
| 3603 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3604 | static int task_switch_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3605 | { |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3606 | u16 tss_selector; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 3607 | int reason; |
| 3608 | int int_type = svm->vmcb->control.exit_int_info & |
| 3609 | SVM_EXITINTINFO_TYPE_MASK; |
Gleb Natapov | 8317c29 | 2009-04-12 13:37:02 +0300 | [diff] [blame] | 3610 | 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] | 3611 | uint32_t type = |
| 3612 | svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK; |
| 3613 | uint32_t idt_v = |
| 3614 | svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID; |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 3615 | bool has_error_code = false; |
| 3616 | u32 error_code = 0; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3617 | |
| 3618 | tss_selector = (u16)svm->vmcb->control.exit_info_1; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 3619 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3620 | if (svm->vmcb->control.exit_info_2 & |
| 3621 | (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET)) |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 3622 | reason = TASK_SWITCH_IRET; |
| 3623 | else if (svm->vmcb->control.exit_info_2 & |
| 3624 | (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP)) |
| 3625 | reason = TASK_SWITCH_JMP; |
Gleb Natapov | fe8e7f8 | 2009-04-23 17:03:48 +0300 | [diff] [blame] | 3626 | else if (idt_v) |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 3627 | reason = TASK_SWITCH_GATE; |
| 3628 | else |
| 3629 | reason = TASK_SWITCH_CALL; |
| 3630 | |
Gleb Natapov | fe8e7f8 | 2009-04-23 17:03:48 +0300 | [diff] [blame] | 3631 | if (reason == TASK_SWITCH_GATE) { |
| 3632 | switch (type) { |
| 3633 | case SVM_EXITINTINFO_TYPE_NMI: |
| 3634 | svm->vcpu.arch.nmi_injected = false; |
| 3635 | break; |
| 3636 | case SVM_EXITINTINFO_TYPE_EXEPT: |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 3637 | if (svm->vmcb->control.exit_info_2 & |
| 3638 | (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) { |
| 3639 | has_error_code = true; |
| 3640 | error_code = |
| 3641 | (u32)svm->vmcb->control.exit_info_2; |
| 3642 | } |
Gleb Natapov | fe8e7f8 | 2009-04-23 17:03:48 +0300 | [diff] [blame] | 3643 | kvm_clear_exception_queue(&svm->vcpu); |
| 3644 | break; |
| 3645 | case SVM_EXITINTINFO_TYPE_INTR: |
| 3646 | kvm_clear_interrupt_queue(&svm->vcpu); |
| 3647 | break; |
| 3648 | default: |
| 3649 | break; |
| 3650 | } |
| 3651 | } |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 3652 | |
Gleb Natapov | 8317c29 | 2009-04-12 13:37:02 +0300 | [diff] [blame] | 3653 | if (reason != TASK_SWITCH_GATE || |
| 3654 | int_type == SVM_EXITINTINFO_TYPE_SOFT || |
| 3655 | (int_type == SVM_EXITINTINFO_TYPE_EXEPT && |
Gleb Natapov | f629cf8 | 2009-05-11 13:35:49 +0300 | [diff] [blame] | 3656 | (int_vec == OF_VECTOR || int_vec == BP_VECTOR))) |
| 3657 | skip_emulated_instruction(&svm->vcpu); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 3658 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 3659 | if (int_type != SVM_EXITINTINFO_TYPE_SOFT) |
| 3660 | int_vec = -1; |
| 3661 | |
| 3662 | if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason, |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 3663 | has_error_code, error_code) == EMULATE_FAIL) { |
| 3664 | svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 3665 | svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 3666 | svm->vcpu.run->internal.ndata = 0; |
| 3667 | return 0; |
| 3668 | } |
| 3669 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3670 | } |
| 3671 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3672 | static int cpuid_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3673 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3674 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 2; |
Kyle Huey | 6a908b6 | 2016-11-29 12:40:37 -0800 | [diff] [blame] | 3675 | return kvm_emulate_cpuid(&svm->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3676 | } |
| 3677 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3678 | static int iret_interception(struct vcpu_svm *svm) |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 3679 | { |
| 3680 | ++svm->vcpu.stat.nmi_window_exits; |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 3681 | clr_intercept(svm, INTERCEPT_IRET); |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 3682 | svm->vcpu.arch.hflags |= HF_IRET_MASK; |
Avi Kivity | bd3d1ec | 2011-02-03 15:29:52 +0200 | [diff] [blame] | 3683 | svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu); |
Radim Krčmář | f303b4c | 2014-01-17 20:52:42 +0100 | [diff] [blame] | 3684 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 3685 | return 1; |
| 3686 | } |
| 3687 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3688 | static int invlpg_interception(struct vcpu_svm *svm) |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3689 | { |
Andre Przywara | df4f3108 | 2010-12-21 11:12:06 +0100 | [diff] [blame] | 3690 | if (!static_cpu_has(X86_FEATURE_DECODEASSISTS)) |
| 3691 | return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE; |
| 3692 | |
| 3693 | kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1); |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3694 | return kvm_skip_emulated_instruction(&svm->vcpu); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3695 | } |
| 3696 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3697 | static int emulate_on_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3698 | { |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 3699 | return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3700 | } |
| 3701 | |
Avi Kivity | 332b56e | 2011-11-10 14:57:24 +0200 | [diff] [blame] | 3702 | static int rdpmc_interception(struct vcpu_svm *svm) |
| 3703 | { |
| 3704 | int err; |
| 3705 | |
| 3706 | if (!static_cpu_has(X86_FEATURE_NRIPS)) |
| 3707 | return emulate_on_interception(svm); |
| 3708 | |
| 3709 | err = kvm_rdpmc(&svm->vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 3710 | return kvm_complete_insn_gp(&svm->vcpu, err); |
Avi Kivity | 332b56e | 2011-11-10 14:57:24 +0200 | [diff] [blame] | 3711 | } |
| 3712 | |
Xiubo Li | 52eb5a6 | 2015-03-13 17:39:45 +0800 | [diff] [blame] | 3713 | static bool check_selective_cr0_intercepted(struct vcpu_svm *svm, |
| 3714 | unsigned long val) |
Joerg Roedel | 628afd2 | 2011-04-04 12:39:36 +0200 | [diff] [blame] | 3715 | { |
| 3716 | unsigned long cr0 = svm->vcpu.arch.cr0; |
| 3717 | bool ret = false; |
| 3718 | u64 intercept; |
| 3719 | |
| 3720 | intercept = svm->nested.intercept; |
| 3721 | |
| 3722 | if (!is_guest_mode(&svm->vcpu) || |
| 3723 | (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))) |
| 3724 | return false; |
| 3725 | |
| 3726 | cr0 &= ~SVM_CR0_SELECTIVE_MASK; |
| 3727 | val &= ~SVM_CR0_SELECTIVE_MASK; |
| 3728 | |
| 3729 | if (cr0 ^ val) { |
| 3730 | svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE; |
| 3731 | ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE); |
| 3732 | } |
| 3733 | |
| 3734 | return ret; |
| 3735 | } |
| 3736 | |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 3737 | #define CR_VALID (1ULL << 63) |
| 3738 | |
| 3739 | static int cr_interception(struct vcpu_svm *svm) |
| 3740 | { |
| 3741 | int reg, cr; |
| 3742 | unsigned long val; |
| 3743 | int err; |
| 3744 | |
| 3745 | if (!static_cpu_has(X86_FEATURE_DECODEASSISTS)) |
| 3746 | return emulate_on_interception(svm); |
| 3747 | |
| 3748 | if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0)) |
| 3749 | return emulate_on_interception(svm); |
| 3750 | |
| 3751 | reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK; |
David Kaplan | 5e57518 | 2015-03-06 14:44:35 -0600 | [diff] [blame] | 3752 | if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE) |
| 3753 | cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0; |
| 3754 | else |
| 3755 | cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0; |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 3756 | |
| 3757 | err = 0; |
| 3758 | if (cr >= 16) { /* mov to cr */ |
| 3759 | cr -= 16; |
| 3760 | val = kvm_register_read(&svm->vcpu, reg); |
| 3761 | switch (cr) { |
| 3762 | case 0: |
Joerg Roedel | 628afd2 | 2011-04-04 12:39:36 +0200 | [diff] [blame] | 3763 | if (!check_selective_cr0_intercepted(svm, val)) |
| 3764 | err = kvm_set_cr0(&svm->vcpu, val); |
Joerg Roedel | 977b2d0 | 2011-04-18 11:42:52 +0200 | [diff] [blame] | 3765 | else |
| 3766 | return 1; |
| 3767 | |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 3768 | break; |
| 3769 | case 3: |
| 3770 | err = kvm_set_cr3(&svm->vcpu, val); |
| 3771 | break; |
| 3772 | case 4: |
| 3773 | err = kvm_set_cr4(&svm->vcpu, val); |
| 3774 | break; |
| 3775 | case 8: |
| 3776 | err = kvm_set_cr8(&svm->vcpu, val); |
| 3777 | break; |
| 3778 | default: |
| 3779 | WARN(1, "unhandled write to CR%d", cr); |
| 3780 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
| 3781 | return 1; |
| 3782 | } |
| 3783 | } else { /* mov from cr */ |
| 3784 | switch (cr) { |
| 3785 | case 0: |
| 3786 | val = kvm_read_cr0(&svm->vcpu); |
| 3787 | break; |
| 3788 | case 2: |
| 3789 | val = svm->vcpu.arch.cr2; |
| 3790 | break; |
| 3791 | case 3: |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 3792 | val = kvm_read_cr3(&svm->vcpu); |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 3793 | break; |
| 3794 | case 4: |
| 3795 | val = kvm_read_cr4(&svm->vcpu); |
| 3796 | break; |
| 3797 | case 8: |
| 3798 | val = kvm_get_cr8(&svm->vcpu); |
| 3799 | break; |
| 3800 | default: |
| 3801 | WARN(1, "unhandled read from CR%d", cr); |
| 3802 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
| 3803 | return 1; |
| 3804 | } |
| 3805 | kvm_register_write(&svm->vcpu, reg, val); |
| 3806 | } |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 3807 | return kvm_complete_insn_gp(&svm->vcpu, err); |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 3808 | } |
| 3809 | |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 3810 | static int dr_interception(struct vcpu_svm *svm) |
| 3811 | { |
| 3812 | int reg, dr; |
| 3813 | unsigned long val; |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 3814 | |
Paolo Bonzini | facb013 | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 3815 | if (svm->vcpu.guest_debug == 0) { |
| 3816 | /* |
| 3817 | * No more DR vmexits; force a reload of the debug registers |
| 3818 | * and reenter on this instruction. The next vmexit will |
| 3819 | * retrieve the full state of the debug registers. |
| 3820 | */ |
| 3821 | clr_dr_intercepts(svm); |
| 3822 | svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; |
| 3823 | return 1; |
| 3824 | } |
| 3825 | |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 3826 | if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS)) |
| 3827 | return emulate_on_interception(svm); |
| 3828 | |
| 3829 | reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK; |
| 3830 | dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0; |
| 3831 | |
| 3832 | if (dr >= 16) { /* mov to DRn */ |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 3833 | if (!kvm_require_dr(&svm->vcpu, dr - 16)) |
| 3834 | return 1; |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 3835 | val = kvm_register_read(&svm->vcpu, reg); |
| 3836 | kvm_set_dr(&svm->vcpu, dr - 16, val); |
| 3837 | } else { |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 3838 | if (!kvm_require_dr(&svm->vcpu, dr)) |
| 3839 | return 1; |
| 3840 | kvm_get_dr(&svm->vcpu, dr, &val); |
| 3841 | kvm_register_write(&svm->vcpu, reg, val); |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 3842 | } |
| 3843 | |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3844 | return kvm_skip_emulated_instruction(&svm->vcpu); |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 3845 | } |
| 3846 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3847 | static int cr8_write_interception(struct vcpu_svm *svm) |
Joerg Roedel | 1d07543 | 2007-12-06 21:02:25 +0100 | [diff] [blame] | 3848 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3849 | struct kvm_run *kvm_run = svm->vcpu.run; |
Andre Przywara | eea1cff | 2010-12-21 11:12:00 +0100 | [diff] [blame] | 3850 | int r; |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3851 | |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 3852 | u8 cr8_prev = kvm_get_cr8(&svm->vcpu); |
| 3853 | /* instruction emulation calls kvm_set_cr8() */ |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 3854 | r = cr_interception(svm); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 3855 | if (lapic_in_kernel(&svm->vcpu)) |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 3856 | return r; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 3857 | if (cr8_prev <= kvm_get_cr8(&svm->vcpu)) |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 3858 | return r; |
Joerg Roedel | 1d07543 | 2007-12-06 21:02:25 +0100 | [diff] [blame] | 3859 | kvm_run->exit_reason = KVM_EXIT_SET_TPR; |
| 3860 | return 0; |
| 3861 | } |
| 3862 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3863 | 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] | 3864 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3865 | struct vcpu_svm *svm = to_svm(vcpu); |
| 3866 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3867 | switch (msr_info->index) { |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 3868 | case MSR_IA32_TSC: { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3869 | msr_info->data = svm->vmcb->control.tsc_offset + |
Haozhong Zhang | 35181e8 | 2015-10-20 15:39:03 +0800 | [diff] [blame] | 3870 | kvm_scale_tsc(vcpu, rdtsc()); |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 3871 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3872 | break; |
| 3873 | } |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 3874 | case MSR_STAR: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3875 | msr_info->data = svm->vmcb->save.star; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3876 | break; |
Avi Kivity | 0e859ca | 2006-12-22 01:05:08 -0800 | [diff] [blame] | 3877 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3878 | case MSR_LSTAR: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3879 | msr_info->data = svm->vmcb->save.lstar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3880 | break; |
| 3881 | case MSR_CSTAR: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3882 | msr_info->data = svm->vmcb->save.cstar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3883 | break; |
| 3884 | case MSR_KERNEL_GS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3885 | msr_info->data = svm->vmcb->save.kernel_gs_base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3886 | break; |
| 3887 | case MSR_SYSCALL_MASK: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3888 | msr_info->data = svm->vmcb->save.sfmask; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3889 | break; |
| 3890 | #endif |
| 3891 | case MSR_IA32_SYSENTER_CS: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3892 | msr_info->data = svm->vmcb->save.sysenter_cs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3893 | break; |
| 3894 | case MSR_IA32_SYSENTER_EIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3895 | msr_info->data = svm->sysenter_eip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3896 | break; |
| 3897 | case MSR_IA32_SYSENTER_ESP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3898 | msr_info->data = svm->sysenter_esp; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3899 | break; |
Paolo Bonzini | 46896c7 | 2015-11-12 14:49:16 +0100 | [diff] [blame] | 3900 | case MSR_TSC_AUX: |
| 3901 | if (!boot_cpu_has(X86_FEATURE_RDTSCP)) |
| 3902 | return 1; |
| 3903 | msr_info->data = svm->tsc_aux; |
| 3904 | break; |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 3905 | /* |
| 3906 | * Nobody will change the following 5 values in the VMCB so we can |
| 3907 | * safely return them on rdmsr. They will always be 0 until LBRV is |
| 3908 | * implemented. |
| 3909 | */ |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 3910 | case MSR_IA32_DEBUGCTLMSR: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3911 | msr_info->data = svm->vmcb->save.dbgctl; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 3912 | break; |
| 3913 | case MSR_IA32_LASTBRANCHFROMIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3914 | msr_info->data = svm->vmcb->save.br_from; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 3915 | break; |
| 3916 | case MSR_IA32_LASTBRANCHTOIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3917 | msr_info->data = svm->vmcb->save.br_to; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 3918 | break; |
| 3919 | case MSR_IA32_LASTINTFROMIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3920 | msr_info->data = svm->vmcb->save.last_excp_from; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 3921 | break; |
| 3922 | case MSR_IA32_LASTINTTOIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3923 | msr_info->data = svm->vmcb->save.last_excp_to; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 3924 | break; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 3925 | case MSR_VM_HSAVE_PA: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3926 | msr_info->data = svm->nested.hsave_msr; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 3927 | break; |
Joerg Roedel | eb6f302 | 2008-11-25 20:17:09 +0100 | [diff] [blame] | 3928 | case MSR_VM_CR: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3929 | msr_info->data = svm->nested.vm_cr_msr; |
Joerg Roedel | eb6f302 | 2008-11-25 20:17:09 +0100 | [diff] [blame] | 3930 | break; |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 3931 | case MSR_IA32_SPEC_CTRL: |
| 3932 | if (!msr_info->host_initiated && |
| 3933 | !guest_cpuid_has(vcpu, X86_FEATURE_IBRS)) |
| 3934 | return 1; |
| 3935 | |
| 3936 | msr_info->data = svm->spec_ctrl; |
| 3937 | break; |
Alexander Graf | c8a73f1 | 2009-01-05 16:02:47 +0100 | [diff] [blame] | 3938 | case MSR_IA32_UCODE_REV: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3939 | msr_info->data = 0x01000065; |
Alexander Graf | c8a73f1 | 2009-01-05 16:02:47 +0100 | [diff] [blame] | 3940 | break; |
Borislav Petkov | ae8b787 | 2015-11-23 11:12:23 +0100 | [diff] [blame] | 3941 | case MSR_F15H_IC_CFG: { |
| 3942 | |
| 3943 | int family, model; |
| 3944 | |
| 3945 | family = guest_cpuid_family(vcpu); |
| 3946 | model = guest_cpuid_model(vcpu); |
| 3947 | |
| 3948 | if (family < 0 || model < 0) |
| 3949 | return kvm_get_msr_common(vcpu, msr_info); |
| 3950 | |
| 3951 | msr_info->data = 0; |
| 3952 | |
| 3953 | if (family == 0x15 && |
| 3954 | (model >= 0x2 && model < 0x20)) |
| 3955 | msr_info->data = 0x1E; |
| 3956 | } |
| 3957 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3958 | default: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3959 | return kvm_get_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3960 | } |
| 3961 | return 0; |
| 3962 | } |
| 3963 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3964 | static int rdmsr_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3965 | { |
David Kaplan | 668f198 | 2015-02-20 16:02:10 -0600 | [diff] [blame] | 3966 | u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX); |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3967 | struct msr_data msr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3968 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3969 | msr_info.index = ecx; |
| 3970 | msr_info.host_initiated = false; |
| 3971 | if (svm_get_msr(&svm->vcpu, &msr_info)) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 3972 | trace_kvm_msr_read_ex(ecx); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 3973 | kvm_inject_gp(&svm->vcpu, 0); |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3974 | return 1; |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 3975 | } else { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3976 | trace_kvm_msr_read(ecx, msr_info.data); |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 3977 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3978 | kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, |
| 3979 | msr_info.data & 0xffffffff); |
| 3980 | kvm_register_write(&svm->vcpu, VCPU_REGS_RDX, |
| 3981 | msr_info.data >> 32); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3982 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 2; |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3983 | return kvm_skip_emulated_instruction(&svm->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3984 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3985 | } |
| 3986 | |
Joerg Roedel | 4a81018 | 2010-02-24 18:59:15 +0100 | [diff] [blame] | 3987 | static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data) |
| 3988 | { |
| 3989 | struct vcpu_svm *svm = to_svm(vcpu); |
| 3990 | int svm_dis, chg_mask; |
| 3991 | |
| 3992 | if (data & ~SVM_VM_CR_VALID_MASK) |
| 3993 | return 1; |
| 3994 | |
| 3995 | chg_mask = SVM_VM_CR_VALID_MASK; |
| 3996 | |
| 3997 | if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK) |
| 3998 | chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK); |
| 3999 | |
| 4000 | svm->nested.vm_cr_msr &= ~chg_mask; |
| 4001 | svm->nested.vm_cr_msr |= (data & chg_mask); |
| 4002 | |
| 4003 | svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK; |
| 4004 | |
| 4005 | /* check for svm_disable while efer.svme is set */ |
| 4006 | if (svm_dis && (vcpu->arch.efer & EFER_SVME)) |
| 4007 | return 1; |
| 4008 | |
| 4009 | return 0; |
| 4010 | } |
| 4011 | |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4012 | 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] | 4013 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4014 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4015 | |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4016 | u32 ecx = msr->index; |
| 4017 | u64 data = msr->data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4018 | switch (ecx) { |
Paolo Bonzini | 15038e1 | 2017-10-26 09:13:27 +0200 | [diff] [blame] | 4019 | case MSR_IA32_CR_PAT: |
| 4020 | if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data)) |
| 4021 | return 1; |
| 4022 | vcpu->arch.pat = data; |
| 4023 | svm->vmcb->save.g_pat = data; |
| 4024 | mark_dirty(svm->vmcb, VMCB_NPT); |
| 4025 | break; |
Zachary Amsden | f4e1b3c | 2010-08-19 22:07:16 -1000 | [diff] [blame] | 4026 | case MSR_IA32_TSC: |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4027 | kvm_write_tsc(vcpu, msr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4028 | break; |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 4029 | case MSR_IA32_SPEC_CTRL: |
| 4030 | if (!msr->host_initiated && |
| 4031 | !guest_cpuid_has(vcpu, X86_FEATURE_IBRS)) |
| 4032 | return 1; |
| 4033 | |
| 4034 | /* The STIBP bit doesn't fault even if it's not advertised */ |
| 4035 | if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP)) |
| 4036 | return 1; |
| 4037 | |
| 4038 | svm->spec_ctrl = data; |
| 4039 | |
| 4040 | if (!data) |
| 4041 | break; |
| 4042 | |
| 4043 | /* |
| 4044 | * For non-nested: |
| 4045 | * When it's written (to non-zero) for the first time, pass |
| 4046 | * it through. |
| 4047 | * |
| 4048 | * For nested: |
| 4049 | * The handling of the MSR bitmap for L2 guests is done in |
| 4050 | * nested_svm_vmrun_msrpm. |
| 4051 | * We update the L1 MSR bit as well since it will end up |
| 4052 | * touching the MSR anyway now. |
| 4053 | */ |
| 4054 | set_msr_interception(svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1); |
| 4055 | break; |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 4056 | case MSR_IA32_PRED_CMD: |
| 4057 | if (!msr->host_initiated && |
| 4058 | !guest_cpuid_has(vcpu, X86_FEATURE_IBPB)) |
| 4059 | return 1; |
| 4060 | |
| 4061 | if (data & ~PRED_CMD_IBPB) |
| 4062 | return 1; |
| 4063 | |
| 4064 | if (!data) |
| 4065 | break; |
| 4066 | |
| 4067 | wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB); |
| 4068 | if (is_guest_mode(vcpu)) |
| 4069 | break; |
| 4070 | set_msr_interception(svm->msrpm, MSR_IA32_PRED_CMD, 0, 1); |
| 4071 | break; |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 4072 | case MSR_STAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4073 | svm->vmcb->save.star = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4074 | break; |
Robert P. J. Day | 49b14f2 | 2007-01-29 13:19:50 -0800 | [diff] [blame] | 4075 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4076 | case MSR_LSTAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4077 | svm->vmcb->save.lstar = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4078 | break; |
| 4079 | case MSR_CSTAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4080 | svm->vmcb->save.cstar = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4081 | break; |
| 4082 | case MSR_KERNEL_GS_BASE: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4083 | svm->vmcb->save.kernel_gs_base = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4084 | break; |
| 4085 | case MSR_SYSCALL_MASK: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4086 | svm->vmcb->save.sfmask = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4087 | break; |
| 4088 | #endif |
| 4089 | case MSR_IA32_SYSENTER_CS: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4090 | svm->vmcb->save.sysenter_cs = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4091 | break; |
| 4092 | case MSR_IA32_SYSENTER_EIP: |
Andre Przywara | 017cb99 | 2009-05-28 11:56:31 +0200 | [diff] [blame] | 4093 | svm->sysenter_eip = data; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4094 | svm->vmcb->save.sysenter_eip = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4095 | break; |
| 4096 | case MSR_IA32_SYSENTER_ESP: |
Andre Przywara | 017cb99 | 2009-05-28 11:56:31 +0200 | [diff] [blame] | 4097 | svm->sysenter_esp = data; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4098 | svm->vmcb->save.sysenter_esp = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4099 | break; |
Paolo Bonzini | 46896c7 | 2015-11-12 14:49:16 +0100 | [diff] [blame] | 4100 | case MSR_TSC_AUX: |
| 4101 | if (!boot_cpu_has(X86_FEATURE_RDTSCP)) |
| 4102 | return 1; |
| 4103 | |
| 4104 | /* |
| 4105 | * This is rare, so we update the MSR here instead of using |
| 4106 | * direct_access_msrs. Doing that would require a rdmsr in |
| 4107 | * svm_vcpu_put. |
| 4108 | */ |
| 4109 | svm->tsc_aux = data; |
| 4110 | wrmsrl(MSR_TSC_AUX, svm->tsc_aux); |
| 4111 | break; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 4112 | case MSR_IA32_DEBUGCTLMSR: |
Avi Kivity | 2a6b20b | 2010-11-09 16:15:42 +0200 | [diff] [blame] | 4113 | if (!boot_cpu_has(X86_FEATURE_LBRV)) { |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 4114 | vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n", |
| 4115 | __func__, data); |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 4116 | break; |
| 4117 | } |
| 4118 | if (data & DEBUGCTL_RESERVED_BITS) |
| 4119 | return 1; |
| 4120 | |
| 4121 | svm->vmcb->save.dbgctl = data; |
Joerg Roedel | b53ba3f | 2010-12-03 11:45:59 +0100 | [diff] [blame] | 4122 | mark_dirty(svm->vmcb, VMCB_LBR); |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 4123 | if (data & (1ULL<<0)) |
| 4124 | svm_enable_lbrv(svm); |
| 4125 | else |
| 4126 | svm_disable_lbrv(svm); |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 4127 | break; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 4128 | case MSR_VM_HSAVE_PA: |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 4129 | svm->nested.hsave_msr = data; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 4130 | break; |
Alexander Graf | 3c5d0a4 | 2009-06-15 15:21:23 +0200 | [diff] [blame] | 4131 | case MSR_VM_CR: |
Joerg Roedel | 4a81018 | 2010-02-24 18:59:15 +0100 | [diff] [blame] | 4132 | return svm_set_vm_cr(vcpu, data); |
Alexander Graf | 3c5d0a4 | 2009-06-15 15:21:23 +0200 | [diff] [blame] | 4133 | case MSR_VM_IGNNE: |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 4134 | 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] | 4135 | break; |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 4136 | case MSR_IA32_APICBASE: |
| 4137 | if (kvm_vcpu_apicv_active(vcpu)) |
| 4138 | avic_update_vapic_bar(to_svm(vcpu), data); |
| 4139 | /* Follow through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4140 | default: |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4141 | return kvm_set_msr_common(vcpu, msr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4142 | } |
| 4143 | return 0; |
| 4144 | } |
| 4145 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4146 | static int wrmsr_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4147 | { |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4148 | struct msr_data msr; |
David Kaplan | 668f198 | 2015-02-20 16:02:10 -0600 | [diff] [blame] | 4149 | u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX); |
| 4150 | u64 data = kvm_read_edx_eax(&svm->vcpu); |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 4151 | |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4152 | msr.data = data; |
| 4153 | msr.index = ecx; |
| 4154 | msr.host_initiated = false; |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 4155 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4156 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 2; |
Nadav Amit | 854e8bb | 2014-09-16 03:24:05 +0300 | [diff] [blame] | 4157 | if (kvm_set_msr(&svm->vcpu, &msr)) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 4158 | trace_kvm_msr_write_ex(ecx, data); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 4159 | kvm_inject_gp(&svm->vcpu, 0); |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 4160 | return 1; |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 4161 | } else { |
| 4162 | trace_kvm_msr_write(ecx, data); |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 4163 | return kvm_skip_emulated_instruction(&svm->vcpu); |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 4164 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4165 | } |
| 4166 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4167 | static int msr_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4168 | { |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 4169 | if (svm->vmcb->control.exit_info_1) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4170 | return wrmsr_interception(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4171 | else |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4172 | return rdmsr_interception(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4173 | } |
| 4174 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4175 | static int interrupt_window_interception(struct vcpu_svm *svm) |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 4176 | { |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 4177 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
Alexander Graf | f0b8505 | 2008-11-25 20:17:01 +0100 | [diff] [blame] | 4178 | svm_clear_vintr(svm); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 4179 | svm->vmcb->control.int_ctl &= ~V_IRQ_MASK; |
Joerg Roedel | decdbf6 | 2010-12-03 11:45:52 +0100 | [diff] [blame] | 4180 | mark_dirty(svm->vmcb, VMCB_INTR); |
Jason Wang | 675acb7 | 2012-03-08 18:07:56 +0800 | [diff] [blame] | 4181 | ++svm->vcpu.stat.irq_window_exits; |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 4182 | return 1; |
| 4183 | } |
| 4184 | |
Mark Langsdorf | 565d099 | 2009-10-06 14:25:02 -0500 | [diff] [blame] | 4185 | static int pause_interception(struct vcpu_svm *svm) |
| 4186 | { |
Longpeng(Mike) | de63ad4 | 2017-08-08 12:05:33 +0800 | [diff] [blame] | 4187 | struct kvm_vcpu *vcpu = &svm->vcpu; |
| 4188 | bool in_kernel = (svm_get_cpl(vcpu) == 0); |
| 4189 | |
| 4190 | kvm_vcpu_on_spin(vcpu, in_kernel); |
Mark Langsdorf | 565d099 | 2009-10-06 14:25:02 -0500 | [diff] [blame] | 4191 | return 1; |
| 4192 | } |
| 4193 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 4194 | static int nop_interception(struct vcpu_svm *svm) |
| 4195 | { |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 4196 | return kvm_skip_emulated_instruction(&(svm->vcpu)); |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 4197 | } |
| 4198 | |
| 4199 | static int monitor_interception(struct vcpu_svm *svm) |
| 4200 | { |
| 4201 | printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n"); |
| 4202 | return nop_interception(svm); |
| 4203 | } |
| 4204 | |
| 4205 | static int mwait_interception(struct vcpu_svm *svm) |
| 4206 | { |
| 4207 | printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n"); |
| 4208 | return nop_interception(svm); |
| 4209 | } |
| 4210 | |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 4211 | enum avic_ipi_failure_cause { |
| 4212 | AVIC_IPI_FAILURE_INVALID_INT_TYPE, |
| 4213 | AVIC_IPI_FAILURE_TARGET_NOT_RUNNING, |
| 4214 | AVIC_IPI_FAILURE_INVALID_TARGET, |
| 4215 | AVIC_IPI_FAILURE_INVALID_BACKING_PAGE, |
| 4216 | }; |
| 4217 | |
| 4218 | static int avic_incomplete_ipi_interception(struct vcpu_svm *svm) |
| 4219 | { |
| 4220 | u32 icrh = svm->vmcb->control.exit_info_1 >> 32; |
| 4221 | u32 icrl = svm->vmcb->control.exit_info_1; |
| 4222 | u32 id = svm->vmcb->control.exit_info_2 >> 32; |
Dan Carpenter | 5446a97 | 2016-05-23 13:20:10 +0300 | [diff] [blame] | 4223 | u32 index = svm->vmcb->control.exit_info_2 & 0xFF; |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 4224 | struct kvm_lapic *apic = svm->vcpu.arch.apic; |
| 4225 | |
| 4226 | trace_kvm_avic_incomplete_ipi(svm->vcpu.vcpu_id, icrh, icrl, id, index); |
| 4227 | |
| 4228 | switch (id) { |
| 4229 | case AVIC_IPI_FAILURE_INVALID_INT_TYPE: |
| 4230 | /* |
| 4231 | * AVIC hardware handles the generation of |
| 4232 | * IPIs when the specified Message Type is Fixed |
| 4233 | * (also known as fixed delivery mode) and |
| 4234 | * the Trigger Mode is edge-triggered. The hardware |
| 4235 | * also supports self and broadcast delivery modes |
| 4236 | * specified via the Destination Shorthand(DSH) |
| 4237 | * field of the ICRL. Logical and physical APIC ID |
| 4238 | * formats are supported. All other IPI types cause |
| 4239 | * a #VMEXIT, which needs to emulated. |
| 4240 | */ |
| 4241 | kvm_lapic_reg_write(apic, APIC_ICR2, icrh); |
| 4242 | kvm_lapic_reg_write(apic, APIC_ICR, icrl); |
| 4243 | break; |
| 4244 | case AVIC_IPI_FAILURE_TARGET_NOT_RUNNING: { |
| 4245 | int i; |
| 4246 | struct kvm_vcpu *vcpu; |
| 4247 | struct kvm *kvm = svm->vcpu.kvm; |
| 4248 | struct kvm_lapic *apic = svm->vcpu.arch.apic; |
| 4249 | |
| 4250 | /* |
| 4251 | * At this point, we expect that the AVIC HW has already |
| 4252 | * set the appropriate IRR bits on the valid target |
| 4253 | * vcpus. So, we just need to kick the appropriate vcpu. |
| 4254 | */ |
| 4255 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 4256 | bool m = kvm_apic_match_dest(vcpu, apic, |
| 4257 | icrl & KVM_APIC_SHORT_MASK, |
| 4258 | GET_APIC_DEST_FIELD(icrh), |
| 4259 | icrl & KVM_APIC_DEST_MASK); |
| 4260 | |
| 4261 | if (m && !avic_vcpu_is_running(vcpu)) |
| 4262 | kvm_vcpu_wake_up(vcpu); |
| 4263 | } |
| 4264 | break; |
| 4265 | } |
| 4266 | case AVIC_IPI_FAILURE_INVALID_TARGET: |
| 4267 | break; |
| 4268 | case AVIC_IPI_FAILURE_INVALID_BACKING_PAGE: |
| 4269 | WARN_ONCE(1, "Invalid backing page\n"); |
| 4270 | break; |
| 4271 | default: |
| 4272 | pr_err("Unknown IPI interception\n"); |
| 4273 | } |
| 4274 | |
| 4275 | return 1; |
| 4276 | } |
| 4277 | |
| 4278 | static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat) |
| 4279 | { |
| 4280 | struct kvm_arch *vm_data = &vcpu->kvm->arch; |
| 4281 | int index; |
| 4282 | u32 *logical_apic_id_table; |
| 4283 | int dlid = GET_APIC_LOGICAL_ID(ldr); |
| 4284 | |
| 4285 | if (!dlid) |
| 4286 | return NULL; |
| 4287 | |
| 4288 | if (flat) { /* flat */ |
| 4289 | index = ffs(dlid) - 1; |
| 4290 | if (index > 7) |
| 4291 | return NULL; |
| 4292 | } else { /* cluster */ |
| 4293 | int cluster = (dlid & 0xf0) >> 4; |
| 4294 | int apic = ffs(dlid & 0x0f) - 1; |
| 4295 | |
| 4296 | if ((apic < 0) || (apic > 7) || |
| 4297 | (cluster >= 0xf)) |
| 4298 | return NULL; |
| 4299 | index = (cluster << 2) + apic; |
| 4300 | } |
| 4301 | |
| 4302 | logical_apic_id_table = (u32 *) page_address(vm_data->avic_logical_id_table_page); |
| 4303 | |
| 4304 | return &logical_apic_id_table[index]; |
| 4305 | } |
| 4306 | |
| 4307 | static int avic_ldr_write(struct kvm_vcpu *vcpu, u8 g_physical_id, u32 ldr, |
| 4308 | bool valid) |
| 4309 | { |
| 4310 | bool flat; |
| 4311 | u32 *entry, new_entry; |
| 4312 | |
| 4313 | flat = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR) == APIC_DFR_FLAT; |
| 4314 | entry = avic_get_logical_id_entry(vcpu, ldr, flat); |
| 4315 | if (!entry) |
| 4316 | return -EINVAL; |
| 4317 | |
| 4318 | new_entry = READ_ONCE(*entry); |
| 4319 | new_entry &= ~AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK; |
| 4320 | new_entry |= (g_physical_id & AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK); |
| 4321 | if (valid) |
| 4322 | new_entry |= AVIC_LOGICAL_ID_ENTRY_VALID_MASK; |
| 4323 | else |
| 4324 | new_entry &= ~AVIC_LOGICAL_ID_ENTRY_VALID_MASK; |
| 4325 | WRITE_ONCE(*entry, new_entry); |
| 4326 | |
| 4327 | return 0; |
| 4328 | } |
| 4329 | |
| 4330 | static int avic_handle_ldr_update(struct kvm_vcpu *vcpu) |
| 4331 | { |
| 4332 | int ret; |
| 4333 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4334 | u32 ldr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LDR); |
| 4335 | |
| 4336 | if (!ldr) |
| 4337 | return 1; |
| 4338 | |
| 4339 | ret = avic_ldr_write(vcpu, vcpu->vcpu_id, ldr, true); |
| 4340 | if (ret && svm->ldr_reg) { |
| 4341 | avic_ldr_write(vcpu, 0, svm->ldr_reg, false); |
| 4342 | svm->ldr_reg = 0; |
| 4343 | } else { |
| 4344 | svm->ldr_reg = ldr; |
| 4345 | } |
| 4346 | return ret; |
| 4347 | } |
| 4348 | |
| 4349 | static int avic_handle_apic_id_update(struct kvm_vcpu *vcpu) |
| 4350 | { |
| 4351 | u64 *old, *new; |
| 4352 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4353 | u32 apic_id_reg = kvm_lapic_get_reg(vcpu->arch.apic, APIC_ID); |
| 4354 | u32 id = (apic_id_reg >> 24) & 0xff; |
| 4355 | |
| 4356 | if (vcpu->vcpu_id == id) |
| 4357 | return 0; |
| 4358 | |
| 4359 | old = avic_get_physical_id_entry(vcpu, vcpu->vcpu_id); |
| 4360 | new = avic_get_physical_id_entry(vcpu, id); |
| 4361 | if (!new || !old) |
| 4362 | return 1; |
| 4363 | |
| 4364 | /* We need to move physical_id_entry to new offset */ |
| 4365 | *new = *old; |
| 4366 | *old = 0ULL; |
| 4367 | to_svm(vcpu)->avic_physical_id_cache = new; |
| 4368 | |
| 4369 | /* |
| 4370 | * Also update the guest physical APIC ID in the logical |
| 4371 | * APIC ID table entry if already setup the LDR. |
| 4372 | */ |
| 4373 | if (svm->ldr_reg) |
| 4374 | avic_handle_ldr_update(vcpu); |
| 4375 | |
| 4376 | return 0; |
| 4377 | } |
| 4378 | |
| 4379 | static int avic_handle_dfr_update(struct kvm_vcpu *vcpu) |
| 4380 | { |
| 4381 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4382 | struct kvm_arch *vm_data = &vcpu->kvm->arch; |
| 4383 | u32 dfr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR); |
| 4384 | u32 mod = (dfr >> 28) & 0xf; |
| 4385 | |
| 4386 | /* |
| 4387 | * We assume that all local APICs are using the same type. |
| 4388 | * If this changes, we need to flush the AVIC logical |
| 4389 | * APID id table. |
| 4390 | */ |
| 4391 | if (vm_data->ldr_mode == mod) |
| 4392 | return 0; |
| 4393 | |
| 4394 | clear_page(page_address(vm_data->avic_logical_id_table_page)); |
| 4395 | vm_data->ldr_mode = mod; |
| 4396 | |
| 4397 | if (svm->ldr_reg) |
| 4398 | avic_handle_ldr_update(vcpu); |
| 4399 | return 0; |
| 4400 | } |
| 4401 | |
| 4402 | static int avic_unaccel_trap_write(struct vcpu_svm *svm) |
| 4403 | { |
| 4404 | struct kvm_lapic *apic = svm->vcpu.arch.apic; |
| 4405 | u32 offset = svm->vmcb->control.exit_info_1 & |
| 4406 | AVIC_UNACCEL_ACCESS_OFFSET_MASK; |
| 4407 | |
| 4408 | switch (offset) { |
| 4409 | case APIC_ID: |
| 4410 | if (avic_handle_apic_id_update(&svm->vcpu)) |
| 4411 | return 0; |
| 4412 | break; |
| 4413 | case APIC_LDR: |
| 4414 | if (avic_handle_ldr_update(&svm->vcpu)) |
| 4415 | return 0; |
| 4416 | break; |
| 4417 | case APIC_DFR: |
| 4418 | avic_handle_dfr_update(&svm->vcpu); |
| 4419 | break; |
| 4420 | default: |
| 4421 | break; |
| 4422 | } |
| 4423 | |
| 4424 | kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset)); |
| 4425 | |
| 4426 | return 1; |
| 4427 | } |
| 4428 | |
| 4429 | static bool is_avic_unaccelerated_access_trap(u32 offset) |
| 4430 | { |
| 4431 | bool ret = false; |
| 4432 | |
| 4433 | switch (offset) { |
| 4434 | case APIC_ID: |
| 4435 | case APIC_EOI: |
| 4436 | case APIC_RRR: |
| 4437 | case APIC_LDR: |
| 4438 | case APIC_DFR: |
| 4439 | case APIC_SPIV: |
| 4440 | case APIC_ESR: |
| 4441 | case APIC_ICR: |
| 4442 | case APIC_LVTT: |
| 4443 | case APIC_LVTTHMR: |
| 4444 | case APIC_LVTPC: |
| 4445 | case APIC_LVT0: |
| 4446 | case APIC_LVT1: |
| 4447 | case APIC_LVTERR: |
| 4448 | case APIC_TMICT: |
| 4449 | case APIC_TDCR: |
| 4450 | ret = true; |
| 4451 | break; |
| 4452 | default: |
| 4453 | break; |
| 4454 | } |
| 4455 | return ret; |
| 4456 | } |
| 4457 | |
| 4458 | static int avic_unaccelerated_access_interception(struct vcpu_svm *svm) |
| 4459 | { |
| 4460 | int ret = 0; |
| 4461 | u32 offset = svm->vmcb->control.exit_info_1 & |
| 4462 | AVIC_UNACCEL_ACCESS_OFFSET_MASK; |
| 4463 | u32 vector = svm->vmcb->control.exit_info_2 & |
| 4464 | AVIC_UNACCEL_ACCESS_VECTOR_MASK; |
| 4465 | bool write = (svm->vmcb->control.exit_info_1 >> 32) & |
| 4466 | AVIC_UNACCEL_ACCESS_WRITE_MASK; |
| 4467 | bool trap = is_avic_unaccelerated_access_trap(offset); |
| 4468 | |
| 4469 | trace_kvm_avic_unaccelerated_access(svm->vcpu.vcpu_id, offset, |
| 4470 | trap, write, vector); |
| 4471 | if (trap) { |
| 4472 | /* Handling Trap */ |
| 4473 | WARN_ONCE(!write, "svm: Handling trap read.\n"); |
| 4474 | ret = avic_unaccel_trap_write(svm); |
| 4475 | } else { |
| 4476 | /* Handling Fault */ |
| 4477 | ret = (emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE); |
| 4478 | } |
| 4479 | |
| 4480 | return ret; |
| 4481 | } |
| 4482 | |
Mathias Krause | 09941fb | 2012-08-30 01:30:20 +0200 | [diff] [blame] | 4483 | static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = { |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 4484 | [SVM_EXIT_READ_CR0] = cr_interception, |
| 4485 | [SVM_EXIT_READ_CR3] = cr_interception, |
| 4486 | [SVM_EXIT_READ_CR4] = cr_interception, |
| 4487 | [SVM_EXIT_READ_CR8] = cr_interception, |
David Kaplan | 5e57518 | 2015-03-06 14:44:35 -0600 | [diff] [blame] | 4488 | [SVM_EXIT_CR0_SEL_WRITE] = cr_interception, |
Joerg Roedel | 628afd2 | 2011-04-04 12:39:36 +0200 | [diff] [blame] | 4489 | [SVM_EXIT_WRITE_CR0] = cr_interception, |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 4490 | [SVM_EXIT_WRITE_CR3] = cr_interception, |
| 4491 | [SVM_EXIT_WRITE_CR4] = cr_interception, |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 4492 | [SVM_EXIT_WRITE_CR8] = cr8_write_interception, |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 4493 | [SVM_EXIT_READ_DR0] = dr_interception, |
| 4494 | [SVM_EXIT_READ_DR1] = dr_interception, |
| 4495 | [SVM_EXIT_READ_DR2] = dr_interception, |
| 4496 | [SVM_EXIT_READ_DR3] = dr_interception, |
| 4497 | [SVM_EXIT_READ_DR4] = dr_interception, |
| 4498 | [SVM_EXIT_READ_DR5] = dr_interception, |
| 4499 | [SVM_EXIT_READ_DR6] = dr_interception, |
| 4500 | [SVM_EXIT_READ_DR7] = dr_interception, |
| 4501 | [SVM_EXIT_WRITE_DR0] = dr_interception, |
| 4502 | [SVM_EXIT_WRITE_DR1] = dr_interception, |
| 4503 | [SVM_EXIT_WRITE_DR2] = dr_interception, |
| 4504 | [SVM_EXIT_WRITE_DR3] = dr_interception, |
| 4505 | [SVM_EXIT_WRITE_DR4] = dr_interception, |
| 4506 | [SVM_EXIT_WRITE_DR5] = dr_interception, |
| 4507 | [SVM_EXIT_WRITE_DR6] = dr_interception, |
| 4508 | [SVM_EXIT_WRITE_DR7] = dr_interception, |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4509 | [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception, |
| 4510 | [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception, |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 4511 | [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception, |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 4512 | [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception, |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 4513 | [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception, |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 4514 | [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception, |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 4515 | [SVM_EXIT_INTR] = intr_interception, |
Joerg Roedel | c47f098 | 2008-04-30 17:56:00 +0200 | [diff] [blame] | 4516 | [SVM_EXIT_NMI] = nmi_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4517 | [SVM_EXIT_SMI] = nop_on_interception, |
| 4518 | [SVM_EXIT_INIT] = nop_on_interception, |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 4519 | [SVM_EXIT_VINTR] = interrupt_window_interception, |
Avi Kivity | 332b56e | 2011-11-10 14:57:24 +0200 | [diff] [blame] | 4520 | [SVM_EXIT_RDPMC] = rdpmc_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4521 | [SVM_EXIT_CPUID] = cpuid_interception, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4522 | [SVM_EXIT_IRET] = iret_interception, |
Avi Kivity | cf5a94d | 2007-10-28 16:11:58 +0200 | [diff] [blame] | 4523 | [SVM_EXIT_INVD] = emulate_on_interception, |
Mark Langsdorf | 565d099 | 2009-10-06 14:25:02 -0500 | [diff] [blame] | 4524 | [SVM_EXIT_PAUSE] = pause_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4525 | [SVM_EXIT_HLT] = halt_interception, |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4526 | [SVM_EXIT_INVLPG] = invlpg_interception, |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 4527 | [SVM_EXIT_INVLPGA] = invlpga_interception, |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 4528 | [SVM_EXIT_IOIO] = io_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4529 | [SVM_EXIT_MSR] = msr_interception, |
| 4530 | [SVM_EXIT_TASK_SWITCH] = task_switch_interception, |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 4531 | [SVM_EXIT_SHUTDOWN] = shutdown_interception, |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 4532 | [SVM_EXIT_VMRUN] = vmrun_interception, |
Avi Kivity | 02e235b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4533 | [SVM_EXIT_VMMCALL] = vmmcall_interception, |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 4534 | [SVM_EXIT_VMLOAD] = vmload_interception, |
| 4535 | [SVM_EXIT_VMSAVE] = vmsave_interception, |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 4536 | [SVM_EXIT_STGI] = stgi_interception, |
| 4537 | [SVM_EXIT_CLGI] = clgi_interception, |
Joerg Roedel | 532a46b | 2009-10-09 16:08:32 +0200 | [diff] [blame] | 4538 | [SVM_EXIT_SKINIT] = skinit_interception, |
David Kaplan | dab429a | 2015-03-02 13:43:37 -0600 | [diff] [blame] | 4539 | [SVM_EXIT_WBINVD] = wbinvd_interception, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 4540 | [SVM_EXIT_MONITOR] = monitor_interception, |
| 4541 | [SVM_EXIT_MWAIT] = mwait_interception, |
Joerg Roedel | 81dd35d | 2010-12-07 17:15:06 +0100 | [diff] [blame] | 4542 | [SVM_EXIT_XSETBV] = xsetbv_interception, |
Paolo Bonzini | d000653 | 2017-08-11 18:36:43 +0200 | [diff] [blame] | 4543 | [SVM_EXIT_NPF] = npf_interception, |
Paolo Bonzini | 64d6067 | 2015-05-07 11:36:11 +0200 | [diff] [blame] | 4544 | [SVM_EXIT_RSM] = emulate_on_interception, |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 4545 | [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception, |
| 4546 | [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4547 | }; |
| 4548 | |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 4549 | static void dump_vmcb(struct kvm_vcpu *vcpu) |
Joerg Roedel | 3f10c84 | 2010-05-05 16:04:42 +0200 | [diff] [blame] | 4550 | { |
| 4551 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4552 | struct vmcb_control_area *control = &svm->vmcb->control; |
| 4553 | struct vmcb_save_area *save = &svm->vmcb->save; |
| 4554 | |
| 4555 | pr_err("VMCB Control Area:\n"); |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 4556 | pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff); |
| 4557 | pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16); |
| 4558 | pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff); |
| 4559 | pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16); |
| 4560 | pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions); |
| 4561 | pr_err("%-20s%016llx\n", "intercepts:", control->intercept); |
| 4562 | pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count); |
| 4563 | pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa); |
| 4564 | pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa); |
| 4565 | pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset); |
| 4566 | pr_err("%-20s%d\n", "asid:", control->asid); |
| 4567 | pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl); |
| 4568 | pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl); |
| 4569 | pr_err("%-20s%08x\n", "int_vector:", control->int_vector); |
| 4570 | pr_err("%-20s%08x\n", "int_state:", control->int_state); |
| 4571 | pr_err("%-20s%08x\n", "exit_code:", control->exit_code); |
| 4572 | pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1); |
| 4573 | pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2); |
| 4574 | pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info); |
| 4575 | pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err); |
| 4576 | pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl); |
| 4577 | pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 4578 | pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar); |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 4579 | pr_err("%-20s%08x\n", "event_inj:", control->event_inj); |
| 4580 | pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err); |
Janakarajan Natarajan | 0dc9211 | 2017-07-06 15:50:45 -0500 | [diff] [blame] | 4581 | pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext); |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 4582 | pr_err("%-20s%016llx\n", "next_rip:", control->next_rip); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 4583 | pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page); |
| 4584 | pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id); |
| 4585 | pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id); |
Joerg Roedel | 3f10c84 | 2010-05-05 16:04:42 +0200 | [diff] [blame] | 4586 | pr_err("VMCB State Save Area:\n"); |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 4587 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4588 | "es:", |
| 4589 | save->es.selector, save->es.attrib, |
| 4590 | save->es.limit, save->es.base); |
| 4591 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4592 | "cs:", |
| 4593 | save->cs.selector, save->cs.attrib, |
| 4594 | save->cs.limit, save->cs.base); |
| 4595 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4596 | "ss:", |
| 4597 | save->ss.selector, save->ss.attrib, |
| 4598 | save->ss.limit, save->ss.base); |
| 4599 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4600 | "ds:", |
| 4601 | save->ds.selector, save->ds.attrib, |
| 4602 | save->ds.limit, save->ds.base); |
| 4603 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4604 | "fs:", |
| 4605 | save->fs.selector, save->fs.attrib, |
| 4606 | save->fs.limit, save->fs.base); |
| 4607 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4608 | "gs:", |
| 4609 | save->gs.selector, save->gs.attrib, |
| 4610 | save->gs.limit, save->gs.base); |
| 4611 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4612 | "gdtr:", |
| 4613 | save->gdtr.selector, save->gdtr.attrib, |
| 4614 | save->gdtr.limit, save->gdtr.base); |
| 4615 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4616 | "ldtr:", |
| 4617 | save->ldtr.selector, save->ldtr.attrib, |
| 4618 | save->ldtr.limit, save->ldtr.base); |
| 4619 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4620 | "idtr:", |
| 4621 | save->idtr.selector, save->idtr.attrib, |
| 4622 | save->idtr.limit, save->idtr.base); |
| 4623 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4624 | "tr:", |
| 4625 | save->tr.selector, save->tr.attrib, |
| 4626 | save->tr.limit, save->tr.base); |
Joerg Roedel | 3f10c84 | 2010-05-05 16:04:42 +0200 | [diff] [blame] | 4627 | pr_err("cpl: %d efer: %016llx\n", |
| 4628 | save->cpl, save->efer); |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 4629 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4630 | "cr0:", save->cr0, "cr2:", save->cr2); |
| 4631 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4632 | "cr3:", save->cr3, "cr4:", save->cr4); |
| 4633 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4634 | "dr6:", save->dr6, "dr7:", save->dr7); |
| 4635 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4636 | "rip:", save->rip, "rflags:", save->rflags); |
| 4637 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4638 | "rsp:", save->rsp, "rax:", save->rax); |
| 4639 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4640 | "star:", save->star, "lstar:", save->lstar); |
| 4641 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4642 | "cstar:", save->cstar, "sfmask:", save->sfmask); |
| 4643 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4644 | "kernel_gs_base:", save->kernel_gs_base, |
| 4645 | "sysenter_cs:", save->sysenter_cs); |
| 4646 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4647 | "sysenter_esp:", save->sysenter_esp, |
| 4648 | "sysenter_eip:", save->sysenter_eip); |
| 4649 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4650 | "gpat:", save->g_pat, "dbgctl:", save->dbgctl); |
| 4651 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4652 | "br_from:", save->br_from, "br_to:", save->br_to); |
| 4653 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4654 | "excp_from:", save->last_excp_from, |
| 4655 | "excp_to:", save->last_excp_to); |
Joerg Roedel | 3f10c84 | 2010-05-05 16:04:42 +0200 | [diff] [blame] | 4656 | } |
| 4657 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 4658 | static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
| 4659 | { |
| 4660 | struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control; |
| 4661 | |
| 4662 | *info1 = control->exit_info_1; |
| 4663 | *info2 = control->exit_info_2; |
| 4664 | } |
| 4665 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4666 | static int handle_exit(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4667 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 4668 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4669 | struct kvm_run *kvm_run = vcpu->run; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4670 | u32 exit_code = svm->vmcb->control.exit_code; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4671 | |
Paolo Bonzini | 8b89fe1 | 2015-12-10 18:37:32 +0100 | [diff] [blame] | 4672 | trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM); |
| 4673 | |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 4674 | if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE)) |
Joerg Roedel | 2be4fc7 | 2010-04-22 12:33:09 +0200 | [diff] [blame] | 4675 | vcpu->arch.cr0 = svm->vmcb->save.cr0; |
| 4676 | if (npt_enabled) |
| 4677 | vcpu->arch.cr3 = svm->vmcb->save.cr3; |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 4678 | |
Joerg Roedel | cd3ff65 | 2009-10-09 16:08:26 +0200 | [diff] [blame] | 4679 | if (unlikely(svm->nested.exit_required)) { |
| 4680 | nested_svm_vmexit(svm); |
| 4681 | svm->nested.exit_required = false; |
| 4682 | |
| 4683 | return 1; |
| 4684 | } |
| 4685 | |
Joerg Roedel | 2030753 | 2010-11-29 17:51:48 +0100 | [diff] [blame] | 4686 | if (is_guest_mode(vcpu)) { |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 4687 | int vmexit; |
| 4688 | |
Joerg Roedel | d8cabdd | 2009-10-09 16:08:28 +0200 | [diff] [blame] | 4689 | trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code, |
| 4690 | svm->vmcb->control.exit_info_1, |
| 4691 | svm->vmcb->control.exit_info_2, |
| 4692 | svm->vmcb->control.exit_int_info, |
Stefan Hajnoczi | e097e5f | 2011-07-22 12:46:52 +0100 | [diff] [blame] | 4693 | svm->vmcb->control.exit_int_info_err, |
| 4694 | KVM_ISA_SVM); |
Joerg Roedel | d8cabdd | 2009-10-09 16:08:28 +0200 | [diff] [blame] | 4695 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 4696 | vmexit = nested_svm_exit_special(svm); |
| 4697 | |
| 4698 | if (vmexit == NESTED_EXIT_CONTINUE) |
| 4699 | vmexit = nested_svm_exit_handled(svm); |
| 4700 | |
| 4701 | if (vmexit == NESTED_EXIT_DONE) |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 4702 | return 1; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 4703 | } |
| 4704 | |
Joerg Roedel | a5c3832 | 2009-08-07 11:49:32 +0200 | [diff] [blame] | 4705 | svm_complete_interrupts(svm); |
| 4706 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 4707 | if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) { |
| 4708 | kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 4709 | kvm_run->fail_entry.hardware_entry_failure_reason |
| 4710 | = svm->vmcb->control.exit_code; |
Joerg Roedel | 3f10c84 | 2010-05-05 16:04:42 +0200 | [diff] [blame] | 4711 | pr_err("KVM: FAILED VMRUN WITH VMCB:\n"); |
| 4712 | dump_vmcb(vcpu); |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 4713 | return 0; |
| 4714 | } |
| 4715 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4716 | if (is_external_interrupt(svm->vmcb->control.exit_int_info) && |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 4717 | exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR && |
Joerg Roedel | 55c5e46 | 2010-09-10 17:31:04 +0200 | [diff] [blame] | 4718 | exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH && |
| 4719 | exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI) |
Borislav Petkov | 6614c7d | 2013-04-26 00:22:01 +0200 | [diff] [blame] | 4720 | printk(KERN_ERR "%s: unexpected exit_int_info 0x%x " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4721 | "exit_code 0x%x\n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 4722 | __func__, svm->vmcb->control.exit_int_info, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4723 | exit_code); |
| 4724 | |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 4725 | if (exit_code >= ARRAY_SIZE(svm_exit_handlers) |
Joe Perches | 56919c5 | 2007-11-12 20:06:51 -0800 | [diff] [blame] | 4726 | || !svm_exit_handlers[exit_code]) { |
Bandan Das | faac245 | 2015-03-16 17:18:25 -0400 | [diff] [blame] | 4727 | 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] | 4728 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4729 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4730 | } |
| 4731 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4732 | return svm_exit_handlers[exit_code](svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4733 | } |
| 4734 | |
| 4735 | static void reload_tss(struct kvm_vcpu *vcpu) |
| 4736 | { |
| 4737 | int cpu = raw_smp_processor_id(); |
| 4738 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 4739 | struct svm_cpu_data *sd = per_cpu(svm_data, cpu); |
| 4740 | sd->tss_desc->type = 9; /* available 32/64-bit TSS */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4741 | load_TR_desc(); |
| 4742 | } |
| 4743 | |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 4744 | static void pre_sev_run(struct vcpu_svm *svm, int cpu) |
| 4745 | { |
| 4746 | struct svm_cpu_data *sd = per_cpu(svm_data, cpu); |
| 4747 | int asid = sev_get_asid(svm->vcpu.kvm); |
| 4748 | |
| 4749 | /* Assign the asid allocated with this SEV guest */ |
| 4750 | svm->vmcb->control.asid = asid; |
| 4751 | |
| 4752 | /* |
| 4753 | * Flush guest TLB: |
| 4754 | * |
| 4755 | * 1) when different VMCB for the same ASID is to be run on the same host CPU. |
| 4756 | * 2) or this VMCB was executed on different host CPU in previous VMRUNs. |
| 4757 | */ |
| 4758 | if (sd->sev_vmcbs[asid] == svm->vmcb && |
| 4759 | svm->last_cpu == cpu) |
| 4760 | return; |
| 4761 | |
| 4762 | svm->last_cpu = cpu; |
| 4763 | sd->sev_vmcbs[asid] = svm->vmcb; |
| 4764 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID; |
| 4765 | mark_dirty(svm->vmcb, VMCB_ASID); |
| 4766 | } |
| 4767 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 4768 | static void pre_svm_run(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4769 | { |
| 4770 | int cpu = raw_smp_processor_id(); |
| 4771 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 4772 | struct svm_cpu_data *sd = per_cpu(svm_data, cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4773 | |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 4774 | if (sev_guest(svm->vcpu.kvm)) |
| 4775 | return pre_sev_run(svm, cpu); |
| 4776 | |
Marcelo Tosatti | 4b656b1 | 2009-07-21 12:47:45 -0300 | [diff] [blame] | 4777 | /* FIXME: handle wraparound of asid_generation */ |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 4778 | if (svm->asid_generation != sd->asid_generation) |
| 4779 | new_asid(svm, sd); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4780 | } |
| 4781 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4782 | static void svm_inject_nmi(struct kvm_vcpu *vcpu) |
| 4783 | { |
| 4784 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4785 | |
| 4786 | svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI; |
| 4787 | vcpu->arch.hflags |= HF_NMI_MASK; |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 4788 | set_intercept(svm, INTERCEPT_IRET); |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4789 | ++vcpu->stat.nmi_injections; |
| 4790 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4791 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 4792 | static inline void svm_inject_irq(struct vcpu_svm *svm, int irq) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4793 | { |
| 4794 | struct vmcb_control_area *control; |
| 4795 | |
Suravee Suthikulpanit | 340d3bc | 2016-05-04 14:09:47 -0500 | [diff] [blame] | 4796 | /* The following fields are ignored when AVIC is enabled */ |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 4797 | control = &svm->vmcb->control; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 4798 | control->int_vector = irq; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4799 | control->int_ctl &= ~V_INTR_PRIO_MASK; |
| 4800 | control->int_ctl |= V_IRQ_MASK | |
| 4801 | ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT); |
Joerg Roedel | decdbf6 | 2010-12-03 11:45:52 +0100 | [diff] [blame] | 4802 | mark_dirty(svm->vmcb, VMCB_INTR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4803 | } |
| 4804 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 4805 | static void svm_set_irq(struct kvm_vcpu *vcpu) |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 4806 | { |
| 4807 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4808 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 4809 | BUG_ON(!(gif_set(svm))); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 4810 | |
Gleb Natapov | 9fb2d2b | 2010-05-23 14:28:26 +0300 | [diff] [blame] | 4811 | trace_kvm_inj_virq(vcpu->arch.interrupt.nr); |
| 4812 | ++vcpu->stat.irq_injections; |
| 4813 | |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 4814 | svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr | |
| 4815 | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR; |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 4816 | } |
| 4817 | |
Suravee Suthikulpanit | 3bbf356 | 2016-05-04 14:09:51 -0500 | [diff] [blame] | 4818 | static inline bool svm_nested_virtualize_tpr(struct kvm_vcpu *vcpu) |
| 4819 | { |
| 4820 | return is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK); |
| 4821 | } |
| 4822 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4823 | static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr) |
| 4824 | { |
| 4825 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4826 | |
Suravee Suthikulpanit | 3bbf356 | 2016-05-04 14:09:51 -0500 | [diff] [blame] | 4827 | if (svm_nested_virtualize_tpr(vcpu) || |
| 4828 | kvm_vcpu_apicv_active(vcpu)) |
Joerg Roedel | 88ab24a | 2010-02-19 16:23:06 +0100 | [diff] [blame] | 4829 | return; |
| 4830 | |
Radim Krčmář | 596f314 | 2014-03-11 19:11:18 +0100 | [diff] [blame] | 4831 | clr_cr_intercept(svm, INTERCEPT_CR8_WRITE); |
| 4832 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4833 | if (irr == -1) |
| 4834 | return; |
| 4835 | |
| 4836 | if (tpr >= irr) |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 4837 | set_cr_intercept(svm, INTERCEPT_CR8_WRITE); |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4838 | } |
| 4839 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4840 | static void svm_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set) |
| 4841 | { |
| 4842 | return; |
| 4843 | } |
| 4844 | |
Suravee Suthikulpanit | b2a05fe | 2017-09-12 10:42:41 -0500 | [diff] [blame] | 4845 | static bool svm_get_enable_apicv(struct kvm_vcpu *vcpu) |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4846 | { |
Suravee Suthikulpanit | 67034bb | 2017-09-12 10:42:42 -0500 | [diff] [blame] | 4847 | return avic && irqchip_split(vcpu->kvm); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4848 | } |
| 4849 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 4850 | static void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr) |
| 4851 | { |
| 4852 | } |
| 4853 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 4854 | static void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr) |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 4855 | { |
| 4856 | } |
| 4857 | |
| 4858 | /* Note: Currently only used by Hyper-V. */ |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 4859 | static void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) |
| 4860 | { |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 4861 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4862 | struct vmcb *vmcb = svm->vmcb; |
| 4863 | |
Suravee Suthikulpanit | 67034bb | 2017-09-12 10:42:42 -0500 | [diff] [blame] | 4864 | if (!kvm_vcpu_apicv_active(&svm->vcpu)) |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 4865 | return; |
| 4866 | |
| 4867 | vmcb->control.int_ctl &= ~AVIC_ENABLE_MASK; |
| 4868 | mark_dirty(vmcb, VMCB_INTR); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4869 | } |
| 4870 | |
Andrey Smetanin | 6308630 | 2015-11-10 15:36:32 +0300 | [diff] [blame] | 4871 | 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] | 4872 | { |
| 4873 | return; |
| 4874 | } |
| 4875 | |
Suravee Suthikulpanit | 340d3bc | 2016-05-04 14:09:47 -0500 | [diff] [blame] | 4876 | static void svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec) |
| 4877 | { |
| 4878 | kvm_lapic_set_irr(vec, vcpu->arch.apic); |
| 4879 | smp_mb__after_atomic(); |
| 4880 | |
| 4881 | if (avic_vcpu_is_running(vcpu)) |
| 4882 | wrmsrl(SVM_AVIC_DOORBELL, |
Suravee Suthikulpanit | 7d669f5 | 2016-06-15 17:23:45 -0500 | [diff] [blame] | 4883 | kvm_cpu_get_apicid(vcpu->cpu)); |
Suravee Suthikulpanit | 340d3bc | 2016-05-04 14:09:47 -0500 | [diff] [blame] | 4884 | else |
| 4885 | kvm_vcpu_wake_up(vcpu); |
| 4886 | } |
| 4887 | |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 4888 | static void svm_ir_list_del(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi) |
| 4889 | { |
| 4890 | unsigned long flags; |
| 4891 | struct amd_svm_iommu_ir *cur; |
| 4892 | |
| 4893 | spin_lock_irqsave(&svm->ir_list_lock, flags); |
| 4894 | list_for_each_entry(cur, &svm->ir_list, node) { |
| 4895 | if (cur->data != pi->ir_data) |
| 4896 | continue; |
| 4897 | list_del(&cur->node); |
| 4898 | kfree(cur); |
| 4899 | break; |
| 4900 | } |
| 4901 | spin_unlock_irqrestore(&svm->ir_list_lock, flags); |
| 4902 | } |
| 4903 | |
| 4904 | static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi) |
| 4905 | { |
| 4906 | int ret = 0; |
| 4907 | unsigned long flags; |
| 4908 | struct amd_svm_iommu_ir *ir; |
| 4909 | |
| 4910 | /** |
| 4911 | * In some cases, the existing irte is updaed and re-set, |
| 4912 | * so we need to check here if it's already been * added |
| 4913 | * to the ir_list. |
| 4914 | */ |
| 4915 | if (pi->ir_data && (pi->prev_ga_tag != 0)) { |
| 4916 | struct kvm *kvm = svm->vcpu.kvm; |
| 4917 | u32 vcpu_id = AVIC_GATAG_TO_VCPUID(pi->prev_ga_tag); |
| 4918 | struct kvm_vcpu *prev_vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id); |
| 4919 | struct vcpu_svm *prev_svm; |
| 4920 | |
| 4921 | if (!prev_vcpu) { |
| 4922 | ret = -EINVAL; |
| 4923 | goto out; |
| 4924 | } |
| 4925 | |
| 4926 | prev_svm = to_svm(prev_vcpu); |
| 4927 | svm_ir_list_del(prev_svm, pi); |
| 4928 | } |
| 4929 | |
| 4930 | /** |
| 4931 | * Allocating new amd_iommu_pi_data, which will get |
| 4932 | * add to the per-vcpu ir_list. |
| 4933 | */ |
| 4934 | ir = kzalloc(sizeof(struct amd_svm_iommu_ir), GFP_KERNEL); |
| 4935 | if (!ir) { |
| 4936 | ret = -ENOMEM; |
| 4937 | goto out; |
| 4938 | } |
| 4939 | ir->data = pi->ir_data; |
| 4940 | |
| 4941 | spin_lock_irqsave(&svm->ir_list_lock, flags); |
| 4942 | list_add(&ir->node, &svm->ir_list); |
| 4943 | spin_unlock_irqrestore(&svm->ir_list_lock, flags); |
| 4944 | out: |
| 4945 | return ret; |
| 4946 | } |
| 4947 | |
| 4948 | /** |
| 4949 | * Note: |
| 4950 | * The HW cannot support posting multicast/broadcast |
| 4951 | * interrupts to a vCPU. So, we still use legacy interrupt |
| 4952 | * remapping for these kind of interrupts. |
| 4953 | * |
| 4954 | * For lowest-priority interrupts, we only support |
| 4955 | * those with single CPU as the destination, e.g. user |
| 4956 | * configures the interrupts via /proc/irq or uses |
| 4957 | * irqbalance to make the interrupts single-CPU. |
| 4958 | */ |
| 4959 | static int |
| 4960 | get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e, |
| 4961 | struct vcpu_data *vcpu_info, struct vcpu_svm **svm) |
| 4962 | { |
| 4963 | struct kvm_lapic_irq irq; |
| 4964 | struct kvm_vcpu *vcpu = NULL; |
| 4965 | |
| 4966 | kvm_set_msi_irq(kvm, e, &irq); |
| 4967 | |
| 4968 | if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) { |
| 4969 | pr_debug("SVM: %s: use legacy intr remap mode for irq %u\n", |
| 4970 | __func__, irq.vector); |
| 4971 | return -1; |
| 4972 | } |
| 4973 | |
| 4974 | pr_debug("SVM: %s: use GA mode for irq %u\n", __func__, |
| 4975 | irq.vector); |
| 4976 | *svm = to_svm(vcpu); |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 4977 | vcpu_info->pi_desc_addr = __sme_set(page_to_phys((*svm)->avic_backing_page)); |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 4978 | vcpu_info->vector = irq.vector; |
| 4979 | |
| 4980 | return 0; |
| 4981 | } |
| 4982 | |
| 4983 | /* |
| 4984 | * svm_update_pi_irte - set IRTE for Posted-Interrupts |
| 4985 | * |
| 4986 | * @kvm: kvm |
| 4987 | * @host_irq: host irq of the interrupt |
| 4988 | * @guest_irq: gsi of the interrupt |
| 4989 | * @set: set or unset PI |
| 4990 | * returns 0 on success, < 0 on failure |
| 4991 | */ |
| 4992 | static int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq, |
| 4993 | uint32_t guest_irq, bool set) |
| 4994 | { |
| 4995 | struct kvm_kernel_irq_routing_entry *e; |
| 4996 | struct kvm_irq_routing_table *irq_rt; |
| 4997 | int idx, ret = -EINVAL; |
| 4998 | |
| 4999 | if (!kvm_arch_has_assigned_device(kvm) || |
| 5000 | !irq_remapping_cap(IRQ_POSTING_CAP)) |
| 5001 | return 0; |
| 5002 | |
| 5003 | pr_debug("SVM: %s: host_irq=%#x, guest_irq=%#x, set=%#x\n", |
| 5004 | __func__, host_irq, guest_irq, set); |
| 5005 | |
| 5006 | idx = srcu_read_lock(&kvm->irq_srcu); |
| 5007 | irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); |
| 5008 | WARN_ON(guest_irq >= irq_rt->nr_rt_entries); |
| 5009 | |
| 5010 | hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { |
| 5011 | struct vcpu_data vcpu_info; |
| 5012 | struct vcpu_svm *svm = NULL; |
| 5013 | |
| 5014 | if (e->type != KVM_IRQ_ROUTING_MSI) |
| 5015 | continue; |
| 5016 | |
| 5017 | /** |
| 5018 | * Here, we setup with legacy mode in the following cases: |
| 5019 | * 1. When cannot target interrupt to a specific vcpu. |
| 5020 | * 2. Unsetting posted interrupt. |
| 5021 | * 3. APIC virtialization is disabled for the vcpu. |
| 5022 | */ |
| 5023 | if (!get_pi_vcpu_info(kvm, e, &vcpu_info, &svm) && set && |
| 5024 | kvm_vcpu_apicv_active(&svm->vcpu)) { |
| 5025 | struct amd_iommu_pi_data pi; |
| 5026 | |
| 5027 | /* Try to enable guest_mode in IRTE */ |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 5028 | pi.base = __sme_set(page_to_phys(svm->avic_backing_page) & |
| 5029 | AVIC_HPA_MASK); |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 5030 | pi.ga_tag = AVIC_GATAG(kvm->arch.avic_vm_id, |
| 5031 | svm->vcpu.vcpu_id); |
| 5032 | pi.is_guest_mode = true; |
| 5033 | pi.vcpu_data = &vcpu_info; |
| 5034 | ret = irq_set_vcpu_affinity(host_irq, &pi); |
| 5035 | |
| 5036 | /** |
| 5037 | * Here, we successfully setting up vcpu affinity in |
| 5038 | * IOMMU guest mode. Now, we need to store the posted |
| 5039 | * interrupt information in a per-vcpu ir_list so that |
| 5040 | * we can reference to them directly when we update vcpu |
| 5041 | * scheduling information in IOMMU irte. |
| 5042 | */ |
| 5043 | if (!ret && pi.is_guest_mode) |
| 5044 | svm_ir_list_add(svm, &pi); |
| 5045 | } else { |
| 5046 | /* Use legacy mode in IRTE */ |
| 5047 | struct amd_iommu_pi_data pi; |
| 5048 | |
| 5049 | /** |
| 5050 | * Here, pi is used to: |
| 5051 | * - Tell IOMMU to use legacy mode for this interrupt. |
| 5052 | * - Retrieve ga_tag of prior interrupt remapping data. |
| 5053 | */ |
| 5054 | pi.is_guest_mode = false; |
| 5055 | ret = irq_set_vcpu_affinity(host_irq, &pi); |
| 5056 | |
| 5057 | /** |
| 5058 | * Check if the posted interrupt was previously |
| 5059 | * setup with the guest_mode by checking if the ga_tag |
| 5060 | * was cached. If so, we need to clean up the per-vcpu |
| 5061 | * ir_list. |
| 5062 | */ |
| 5063 | if (!ret && pi.prev_ga_tag) { |
| 5064 | int id = AVIC_GATAG_TO_VCPUID(pi.prev_ga_tag); |
| 5065 | struct kvm_vcpu *vcpu; |
| 5066 | |
| 5067 | vcpu = kvm_get_vcpu_by_id(kvm, id); |
| 5068 | if (vcpu) |
| 5069 | svm_ir_list_del(to_svm(vcpu), &pi); |
| 5070 | } |
| 5071 | } |
| 5072 | |
| 5073 | if (!ret && svm) { |
| 5074 | trace_kvm_pi_irte_update(svm->vcpu.vcpu_id, |
| 5075 | host_irq, e->gsi, |
| 5076 | vcpu_info.vector, |
| 5077 | vcpu_info.pi_desc_addr, set); |
| 5078 | } |
| 5079 | |
| 5080 | if (ret < 0) { |
| 5081 | pr_err("%s: failed to update PI IRTE\n", __func__); |
| 5082 | goto out; |
| 5083 | } |
| 5084 | } |
| 5085 | |
| 5086 | ret = 0; |
| 5087 | out: |
| 5088 | srcu_read_unlock(&kvm->irq_srcu, idx); |
| 5089 | return ret; |
| 5090 | } |
| 5091 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 5092 | static int svm_nmi_allowed(struct kvm_vcpu *vcpu) |
Joerg Roedel | aaacfc9 | 2008-04-16 16:51:18 +0200 | [diff] [blame] | 5093 | { |
| 5094 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5095 | struct vmcb *vmcb = svm->vmcb; |
Joerg Roedel | 924584c | 2010-04-22 12:33:07 +0200 | [diff] [blame] | 5096 | int ret; |
| 5097 | ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) && |
| 5098 | !(svm->vcpu.arch.hflags & HF_NMI_MASK); |
| 5099 | ret = ret && gif_set(svm) && nested_svm_nmi(svm); |
| 5100 | |
| 5101 | return ret; |
Joerg Roedel | aaacfc9 | 2008-04-16 16:51:18 +0200 | [diff] [blame] | 5102 | } |
| 5103 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 5104 | static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu) |
| 5105 | { |
| 5106 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5107 | |
| 5108 | return !!(svm->vcpu.arch.hflags & HF_NMI_MASK); |
| 5109 | } |
| 5110 | |
| 5111 | static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) |
| 5112 | { |
| 5113 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5114 | |
| 5115 | if (masked) { |
| 5116 | svm->vcpu.arch.hflags |= HF_NMI_MASK; |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 5117 | set_intercept(svm, INTERCEPT_IRET); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 5118 | } else { |
| 5119 | svm->vcpu.arch.hflags &= ~HF_NMI_MASK; |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 5120 | clr_intercept(svm, INTERCEPT_IRET); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 5121 | } |
| 5122 | } |
| 5123 | |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 5124 | static int svm_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 5125 | { |
| 5126 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5127 | struct vmcb *vmcb = svm->vmcb; |
Joerg Roedel | 7fcdb51 | 2009-09-16 15:24:15 +0200 | [diff] [blame] | 5128 | int ret; |
| 5129 | |
| 5130 | if (!gif_set(svm) || |
| 5131 | (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)) |
| 5132 | return 0; |
| 5133 | |
Avi Kivity | f6e7847 | 2010-08-02 15:30:20 +0300 | [diff] [blame] | 5134 | ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF); |
Joerg Roedel | 7fcdb51 | 2009-09-16 15:24:15 +0200 | [diff] [blame] | 5135 | |
Joerg Roedel | 2030753 | 2010-11-29 17:51:48 +0100 | [diff] [blame] | 5136 | if (is_guest_mode(vcpu)) |
Joerg Roedel | 7fcdb51 | 2009-09-16 15:24:15 +0200 | [diff] [blame] | 5137 | return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK); |
| 5138 | |
| 5139 | return ret; |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 5140 | } |
| 5141 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 5142 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5143 | { |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 5144 | struct vcpu_svm *svm = to_svm(vcpu); |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 5145 | |
Suravee Suthikulpanit | 340d3bc | 2016-05-04 14:09:47 -0500 | [diff] [blame] | 5146 | if (kvm_vcpu_apicv_active(vcpu)) |
| 5147 | return; |
| 5148 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 5149 | /* |
| 5150 | * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes |
| 5151 | * 1, because that's a separate STGI/VMRUN intercept. The next time we |
| 5152 | * get that intercept, this function will be called again though and |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 5153 | * we'll get the vintr intercept. However, if the vGIF feature is |
| 5154 | * enabled, the STGI interception will not occur. Enable the irq |
| 5155 | * window under the assumption that the hardware will set the GIF. |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 5156 | */ |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 5157 | if ((vgif_enabled(svm) || gif_set(svm)) && nested_svm_intr(svm)) { |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 5158 | svm_set_vintr(svm); |
| 5159 | svm_inject_irq(svm, 0x0); |
| 5160 | } |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5161 | } |
| 5162 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 5163 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5164 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 5165 | struct vcpu_svm *svm = to_svm(vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 5166 | |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 5167 | if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK)) |
| 5168 | == HF_NMI_MASK) |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 5169 | return; /* IRET will cause a vm exit */ |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 5170 | |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 5171 | if (!gif_set(svm)) { |
| 5172 | if (vgif_enabled(svm)) |
| 5173 | set_intercept(svm, INTERCEPT_STGI); |
Ladi Prosek | 1a5e185 | 2017-06-21 09:07:01 +0200 | [diff] [blame] | 5174 | return; /* STGI will cause a vm exit */ |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 5175 | } |
Ladi Prosek | 1a5e185 | 2017-06-21 09:07:01 +0200 | [diff] [blame] | 5176 | |
| 5177 | if (svm->nested.exit_required) |
| 5178 | return; /* we're not going to run the guest yet */ |
| 5179 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 5180 | /* |
| 5181 | * Something prevents NMI from been injected. Single step over possible |
| 5182 | * problem (IRET or exception injection or interrupt shadow) |
| 5183 | */ |
Ladi Prosek | ab2f4d73 | 2017-06-21 09:06:58 +0200 | [diff] [blame] | 5184 | svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu); |
Jan Kiszka | 6be7d30 | 2009-10-18 13:24:54 +0200 | [diff] [blame] | 5185 | svm->nmi_singlestep = true; |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 5186 | svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 5187 | } |
| 5188 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 5189 | static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 5190 | { |
| 5191 | return 0; |
| 5192 | } |
| 5193 | |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 5194 | static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa) |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 5195 | { |
Joerg Roedel | 38e5e92 | 2010-12-03 15:25:16 +0100 | [diff] [blame] | 5196 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5197 | |
| 5198 | if (static_cpu_has(X86_FEATURE_FLUSHBYASID)) |
| 5199 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID; |
| 5200 | else |
| 5201 | svm->asid_generation--; |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 5202 | } |
| 5203 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 5204 | static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu) |
| 5205 | { |
| 5206 | } |
| 5207 | |
Joerg Roedel | d7bf822 | 2008-04-16 16:51:17 +0200 | [diff] [blame] | 5208 | static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu) |
| 5209 | { |
| 5210 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5211 | |
Suravee Suthikulpanit | 3bbf356 | 2016-05-04 14:09:51 -0500 | [diff] [blame] | 5212 | if (svm_nested_virtualize_tpr(vcpu)) |
Joerg Roedel | 88ab24a | 2010-02-19 16:23:06 +0100 | [diff] [blame] | 5213 | return; |
| 5214 | |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 5215 | if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) { |
Joerg Roedel | d7bf822 | 2008-04-16 16:51:17 +0200 | [diff] [blame] | 5216 | int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK; |
Gleb Natapov | 615d519 | 2009-04-21 17:45:05 +0300 | [diff] [blame] | 5217 | kvm_set_cr8(vcpu, cr8); |
Joerg Roedel | d7bf822 | 2008-04-16 16:51:17 +0200 | [diff] [blame] | 5218 | } |
| 5219 | } |
| 5220 | |
Joerg Roedel | 649d686 | 2008-04-16 16:51:15 +0200 | [diff] [blame] | 5221 | static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu) |
| 5222 | { |
| 5223 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5224 | u64 cr8; |
| 5225 | |
Suravee Suthikulpanit | 3bbf356 | 2016-05-04 14:09:51 -0500 | [diff] [blame] | 5226 | if (svm_nested_virtualize_tpr(vcpu) || |
| 5227 | kvm_vcpu_apicv_active(vcpu)) |
Joerg Roedel | 88ab24a | 2010-02-19 16:23:06 +0100 | [diff] [blame] | 5228 | return; |
| 5229 | |
Joerg Roedel | 649d686 | 2008-04-16 16:51:15 +0200 | [diff] [blame] | 5230 | cr8 = kvm_get_cr8(vcpu); |
| 5231 | svm->vmcb->control.int_ctl &= ~V_TPR_MASK; |
| 5232 | svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK; |
| 5233 | } |
| 5234 | |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5235 | static void svm_complete_interrupts(struct vcpu_svm *svm) |
| 5236 | { |
| 5237 | u8 vector; |
| 5238 | int type; |
| 5239 | u32 exitintinfo = svm->vmcb->control.exit_int_info; |
Jan Kiszka | 66b7138 | 2010-02-23 17:47:56 +0100 | [diff] [blame] | 5240 | unsigned int3_injected = svm->int3_injected; |
| 5241 | |
| 5242 | svm->int3_injected = 0; |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5243 | |
Avi Kivity | bd3d1ec | 2011-02-03 15:29:52 +0200 | [diff] [blame] | 5244 | /* |
| 5245 | * If we've made progress since setting HF_IRET_MASK, we've |
| 5246 | * executed an IRET and can allow NMI injection. |
| 5247 | */ |
| 5248 | if ((svm->vcpu.arch.hflags & HF_IRET_MASK) |
| 5249 | && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) { |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 5250 | svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK); |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 5251 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
| 5252 | } |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 5253 | |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5254 | svm->vcpu.arch.nmi_injected = false; |
| 5255 | kvm_clear_exception_queue(&svm->vcpu); |
| 5256 | kvm_clear_interrupt_queue(&svm->vcpu); |
| 5257 | |
| 5258 | if (!(exitintinfo & SVM_EXITINTINFO_VALID)) |
| 5259 | return; |
| 5260 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 5261 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
| 5262 | |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5263 | vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK; |
| 5264 | type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK; |
| 5265 | |
| 5266 | switch (type) { |
| 5267 | case SVM_EXITINTINFO_TYPE_NMI: |
| 5268 | svm->vcpu.arch.nmi_injected = true; |
| 5269 | break; |
| 5270 | case SVM_EXITINTINFO_TYPE_EXEPT: |
Jan Kiszka | 66b7138 | 2010-02-23 17:47:56 +0100 | [diff] [blame] | 5271 | /* |
| 5272 | * In case of software exceptions, do not reinject the vector, |
| 5273 | * but re-execute the instruction instead. Rewind RIP first |
| 5274 | * if we emulated INT3 before. |
| 5275 | */ |
| 5276 | if (kvm_exception_is_soft(vector)) { |
| 5277 | if (vector == BP_VECTOR && int3_injected && |
| 5278 | kvm_is_linear_rip(&svm->vcpu, svm->int3_rip)) |
| 5279 | kvm_rip_write(&svm->vcpu, |
| 5280 | kvm_rip_read(&svm->vcpu) - |
| 5281 | int3_injected); |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 5282 | break; |
Jan Kiszka | 66b7138 | 2010-02-23 17:47:56 +0100 | [diff] [blame] | 5283 | } |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5284 | if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) { |
| 5285 | u32 err = svm->vmcb->control.exit_int_info_err; |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 5286 | kvm_requeue_exception_e(&svm->vcpu, vector, err); |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5287 | |
| 5288 | } else |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 5289 | kvm_requeue_exception(&svm->vcpu, vector); |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5290 | break; |
| 5291 | case SVM_EXITINTINFO_TYPE_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5292 | kvm_queue_interrupt(&svm->vcpu, vector, false); |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5293 | break; |
| 5294 | default: |
| 5295 | break; |
| 5296 | } |
| 5297 | } |
| 5298 | |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 5299 | static void svm_cancel_injection(struct kvm_vcpu *vcpu) |
| 5300 | { |
| 5301 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5302 | struct vmcb_control_area *control = &svm->vmcb->control; |
| 5303 | |
| 5304 | control->exit_int_info = control->event_inj; |
| 5305 | control->exit_int_info_err = control->event_inj_err; |
| 5306 | control->event_inj = 0; |
| 5307 | svm_complete_interrupts(svm); |
| 5308 | } |
| 5309 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5310 | static void svm_vcpu_run(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5311 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 5312 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 5313 | |
Joerg Roedel | 2041a06 | 2010-04-22 12:33:08 +0200 | [diff] [blame] | 5314 | svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX]; |
| 5315 | svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP]; |
| 5316 | svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP]; |
| 5317 | |
Joerg Roedel | cd3ff65 | 2009-10-09 16:08:26 +0200 | [diff] [blame] | 5318 | /* |
| 5319 | * A vmexit emulation is required before the vcpu can be executed |
| 5320 | * again. |
| 5321 | */ |
| 5322 | if (unlikely(svm->nested.exit_required)) |
| 5323 | return; |
| 5324 | |
Ladi Prosek | a12713c | 2017-06-21 09:07:00 +0200 | [diff] [blame] | 5325 | /* |
| 5326 | * Disable singlestep if we're injecting an interrupt/exception. |
| 5327 | * We don't want our modified rflags to be pushed on the stack where |
| 5328 | * we might not be able to easily reset them if we disabled NMI |
| 5329 | * singlestep later. |
| 5330 | */ |
| 5331 | if (svm->nmi_singlestep && svm->vmcb->control.event_inj) { |
| 5332 | /* |
| 5333 | * Event injection happens before external interrupts cause a |
| 5334 | * vmexit and interrupts are disabled here, so smp_send_reschedule |
| 5335 | * is enough to force an immediate vmexit. |
| 5336 | */ |
| 5337 | disable_nmi_singlestep(svm); |
| 5338 | smp_send_reschedule(vcpu->cpu); |
| 5339 | } |
| 5340 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 5341 | pre_svm_run(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5342 | |
Joerg Roedel | 649d686 | 2008-04-16 16:51:15 +0200 | [diff] [blame] | 5343 | sync_lapic_to_cr8(vcpu); |
| 5344 | |
Joerg Roedel | cda0ffd | 2009-08-07 11:49:45 +0200 | [diff] [blame] | 5345 | svm->vmcb->save.cr2 = vcpu->arch.cr2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5346 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 5347 | clgi(); |
| 5348 | |
| 5349 | local_irq_enable(); |
Avi Kivity | 36241b8 | 2006-12-22 01:05:20 -0800 | [diff] [blame] | 5350 | |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 5351 | /* |
| 5352 | * If this vCPU has touched SPEC_CTRL, restore the guest's value if |
| 5353 | * it's non-zero. Since vmentry is serialising on affected CPUs, there |
| 5354 | * is no need to worry about the conditional branch over the wrmsr |
| 5355 | * being speculatively taken. |
| 5356 | */ |
| 5357 | if (svm->spec_ctrl) |
| 5358 | wrmsrl(MSR_IA32_SPEC_CTRL, svm->spec_ctrl); |
| 5359 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5360 | asm volatile ( |
Avi Kivity | 7454766 | 2012-09-16 15:10:59 +0300 | [diff] [blame] | 5361 | "push %%" _ASM_BP "; \n\t" |
| 5362 | "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t" |
| 5363 | "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t" |
| 5364 | "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t" |
| 5365 | "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t" |
| 5366 | "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t" |
| 5367 | "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 5368 | #ifdef CONFIG_X86_64 |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 5369 | "mov %c[r8](%[svm]), %%r8 \n\t" |
| 5370 | "mov %c[r9](%[svm]), %%r9 \n\t" |
| 5371 | "mov %c[r10](%[svm]), %%r10 \n\t" |
| 5372 | "mov %c[r11](%[svm]), %%r11 \n\t" |
| 5373 | "mov %c[r12](%[svm]), %%r12 \n\t" |
| 5374 | "mov %c[r13](%[svm]), %%r13 \n\t" |
| 5375 | "mov %c[r14](%[svm]), %%r14 \n\t" |
| 5376 | "mov %c[r15](%[svm]), %%r15 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5377 | #endif |
| 5378 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5379 | /* Enter guest mode */ |
Avi Kivity | 7454766 | 2012-09-16 15:10:59 +0300 | [diff] [blame] | 5380 | "push %%" _ASM_AX " \n\t" |
| 5381 | "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 5382 | __ex(SVM_VMLOAD) "\n\t" |
| 5383 | __ex(SVM_VMRUN) "\n\t" |
| 5384 | __ex(SVM_VMSAVE) "\n\t" |
Avi Kivity | 7454766 | 2012-09-16 15:10:59 +0300 | [diff] [blame] | 5385 | "pop %%" _ASM_AX " \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5386 | |
| 5387 | /* Save guest registers, load host registers */ |
Avi Kivity | 7454766 | 2012-09-16 15:10:59 +0300 | [diff] [blame] | 5388 | "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t" |
| 5389 | "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t" |
| 5390 | "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t" |
| 5391 | "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t" |
| 5392 | "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t" |
| 5393 | "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 5394 | #ifdef CONFIG_X86_64 |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 5395 | "mov %%r8, %c[r8](%[svm]) \n\t" |
| 5396 | "mov %%r9, %c[r9](%[svm]) \n\t" |
| 5397 | "mov %%r10, %c[r10](%[svm]) \n\t" |
| 5398 | "mov %%r11, %c[r11](%[svm]) \n\t" |
| 5399 | "mov %%r12, %c[r12](%[svm]) \n\t" |
| 5400 | "mov %%r13, %c[r13](%[svm]) \n\t" |
| 5401 | "mov %%r14, %c[r14](%[svm]) \n\t" |
| 5402 | "mov %%r15, %c[r15](%[svm]) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5403 | #endif |
Jim Mattson | 0cb5b30 | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 5404 | /* |
| 5405 | * Clear host registers marked as clobbered to prevent |
| 5406 | * speculative use. |
| 5407 | */ |
| 5408 | "xor %%" _ASM_BX ", %%" _ASM_BX " \n\t" |
| 5409 | "xor %%" _ASM_CX ", %%" _ASM_CX " \n\t" |
| 5410 | "xor %%" _ASM_DX ", %%" _ASM_DX " \n\t" |
| 5411 | "xor %%" _ASM_SI ", %%" _ASM_SI " \n\t" |
| 5412 | "xor %%" _ASM_DI ", %%" _ASM_DI " \n\t" |
| 5413 | #ifdef CONFIG_X86_64 |
| 5414 | "xor %%r8, %%r8 \n\t" |
| 5415 | "xor %%r9, %%r9 \n\t" |
| 5416 | "xor %%r10, %%r10 \n\t" |
| 5417 | "xor %%r11, %%r11 \n\t" |
| 5418 | "xor %%r12, %%r12 \n\t" |
| 5419 | "xor %%r13, %%r13 \n\t" |
| 5420 | "xor %%r14, %%r14 \n\t" |
| 5421 | "xor %%r15, %%r15 \n\t" |
| 5422 | #endif |
Avi Kivity | 7454766 | 2012-09-16 15:10:59 +0300 | [diff] [blame] | 5423 | "pop %%" _ASM_BP |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5424 | : |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 5425 | : [svm]"a"(svm), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5426 | [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5427 | [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 5428 | [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 5429 | [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 5430 | [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 5431 | [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 5432 | [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP])) |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 5433 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5434 | , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])), |
| 5435 | [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])), |
| 5436 | [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])), |
| 5437 | [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])), |
| 5438 | [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])), |
| 5439 | [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])), |
| 5440 | [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])), |
| 5441 | [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15])) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5442 | #endif |
Laurent Vivier | 54a08c0 | 2007-10-25 14:18:53 +0200 | [diff] [blame] | 5443 | : "cc", "memory" |
| 5444 | #ifdef CONFIG_X86_64 |
Avi Kivity | 7454766 | 2012-09-16 15:10:59 +0300 | [diff] [blame] | 5445 | , "rbx", "rcx", "rdx", "rsi", "rdi" |
Laurent Vivier | 54a08c0 | 2007-10-25 14:18:53 +0200 | [diff] [blame] | 5446 | , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15" |
Avi Kivity | 7454766 | 2012-09-16 15:10:59 +0300 | [diff] [blame] | 5447 | #else |
| 5448 | , "ebx", "ecx", "edx", "esi", "edi" |
Laurent Vivier | 54a08c0 | 2007-10-25 14:18:53 +0200 | [diff] [blame] | 5449 | #endif |
| 5450 | ); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5451 | |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 5452 | /* |
| 5453 | * We do not use IBRS in the kernel. If this vCPU has used the |
| 5454 | * SPEC_CTRL MSR it may have left it on; save the value and |
| 5455 | * turn it off. This is much more efficient than blindly adding |
| 5456 | * it to the atomic save/restore list. Especially as the former |
| 5457 | * (Saving guest MSRs on vmexit) doesn't even exist in KVM. |
| 5458 | * |
| 5459 | * For non-nested case: |
| 5460 | * If the L01 MSR bitmap does not intercept the MSR, then we need to |
| 5461 | * save it. |
| 5462 | * |
| 5463 | * For nested case: |
| 5464 | * If the L02 MSR bitmap does not intercept the MSR, then we need to |
| 5465 | * save it. |
| 5466 | */ |
| 5467 | if (!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)) |
| 5468 | rdmsrl(MSR_IA32_SPEC_CTRL, svm->spec_ctrl); |
| 5469 | |
| 5470 | if (svm->spec_ctrl) |
| 5471 | wrmsrl(MSR_IA32_SPEC_CTRL, 0); |
| 5472 | |
David Woodhouse | 117cc7a | 2018-01-12 11:11:27 +0000 | [diff] [blame] | 5473 | /* Eliminate branch target predictions from guest mode */ |
| 5474 | vmexit_fill_RSB(); |
| 5475 | |
Avi Kivity | 82ca2d1 | 2010-10-21 12:20:34 +0200 | [diff] [blame] | 5476 | #ifdef CONFIG_X86_64 |
| 5477 | wrmsrl(MSR_GS_BASE, svm->host.gs_base); |
| 5478 | #else |
Avi Kivity | dacccfd | 2010-10-21 12:20:33 +0200 | [diff] [blame] | 5479 | loadsegment(fs, svm->host.fs); |
Avi Kivity | 831ca60 | 2011-03-08 16:09:51 +0200 | [diff] [blame] | 5480 | #ifndef CONFIG_X86_32_LAZY_GS |
| 5481 | loadsegment(gs, svm->host.gs); |
| 5482 | #endif |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 5483 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5484 | |
| 5485 | reload_tss(vcpu); |
| 5486 | |
Avi Kivity | 56ba47d | 2007-11-07 17:14:18 +0200 | [diff] [blame] | 5487 | local_irq_disable(); |
| 5488 | |
Avi Kivity | 13c34e0 | 2010-10-21 12:20:31 +0200 | [diff] [blame] | 5489 | vcpu->arch.cr2 = svm->vmcb->save.cr2; |
| 5490 | vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax; |
| 5491 | vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp; |
| 5492 | vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip; |
| 5493 | |
Joerg Roedel | 3781c01 | 2011-01-14 16:45:02 +0100 | [diff] [blame] | 5494 | if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI)) |
| 5495 | kvm_before_handle_nmi(&svm->vcpu); |
| 5496 | |
| 5497 | stgi(); |
| 5498 | |
| 5499 | /* Any pending NMI will happen here */ |
| 5500 | |
| 5501 | if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI)) |
| 5502 | kvm_after_handle_nmi(&svm->vcpu); |
| 5503 | |
Joerg Roedel | d7bf822 | 2008-04-16 16:51:17 +0200 | [diff] [blame] | 5504 | sync_cr8_to_lapic(vcpu); |
| 5505 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 5506 | svm->next_rip = 0; |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5507 | |
Joerg Roedel | 38e5e92 | 2010-12-03 15:25:16 +0100 | [diff] [blame] | 5508 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING; |
| 5509 | |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 5510 | /* if exit due to PF check for async PF */ |
| 5511 | if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 5512 | svm->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason(); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 5513 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 5514 | if (npt_enabled) { |
| 5515 | vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR); |
| 5516 | vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR); |
| 5517 | } |
Joerg Roedel | fe5913e | 2010-05-17 14:43:34 +0200 | [diff] [blame] | 5518 | |
| 5519 | /* |
| 5520 | * We need to handle MC intercepts here before the vcpu has a chance to |
| 5521 | * change the physical cpu |
| 5522 | */ |
| 5523 | if (unlikely(svm->vmcb->control.exit_code == |
| 5524 | SVM_EXIT_EXCP_BASE + MC_VECTOR)) |
| 5525 | svm_handle_mce(svm); |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 5526 | |
| 5527 | mark_all_clean(svm->vmcb); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5528 | } |
Josh Poimboeuf | c207aee | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 5529 | STACK_FRAME_NON_STANDARD(svm_vcpu_run); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5530 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5531 | static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root) |
| 5532 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 5533 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5534 | |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 5535 | svm->vmcb->save.cr3 = __sme_set(root); |
Joerg Roedel | dcca1a6 | 2010-12-03 11:45:54 +0100 | [diff] [blame] | 5536 | mark_dirty(svm->vmcb, VMCB_CR); |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 5537 | svm_flush_tlb(vcpu, true); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5538 | } |
| 5539 | |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 5540 | static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root) |
| 5541 | { |
| 5542 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5543 | |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 5544 | svm->vmcb->control.nested_cr3 = __sme_set(root); |
Joerg Roedel | b274716 | 2010-12-03 11:45:53 +0100 | [diff] [blame] | 5545 | mark_dirty(svm->vmcb, VMCB_NPT); |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 5546 | |
| 5547 | /* Also sync guest cr3 here in case we live migrate */ |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 5548 | svm->vmcb->save.cr3 = kvm_read_cr3(vcpu); |
Joerg Roedel | dcca1a6 | 2010-12-03 11:45:54 +0100 | [diff] [blame] | 5549 | mark_dirty(svm->vmcb, VMCB_CR); |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 5550 | |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 5551 | svm_flush_tlb(vcpu, true); |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 5552 | } |
| 5553 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5554 | static int is_disabled(void) |
| 5555 | { |
Joerg Roedel | 6031a61 | 2007-06-22 12:29:50 +0300 | [diff] [blame] | 5556 | u64 vm_cr; |
| 5557 | |
| 5558 | rdmsrl(MSR_VM_CR, vm_cr); |
| 5559 | if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE)) |
| 5560 | return 1; |
| 5561 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5562 | return 0; |
| 5563 | } |
| 5564 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 5565 | static void |
| 5566 | svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 5567 | { |
| 5568 | /* |
| 5569 | * Patch in the VMMCALL instruction: |
| 5570 | */ |
| 5571 | hypercall[0] = 0x0f; |
| 5572 | hypercall[1] = 0x01; |
| 5573 | hypercall[2] = 0xd9; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 5574 | } |
| 5575 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 5576 | static void svm_check_processor_compat(void *rtn) |
| 5577 | { |
| 5578 | *(int *)rtn = 0; |
| 5579 | } |
| 5580 | |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 5581 | static bool svm_cpu_has_accelerated_tpr(void) |
| 5582 | { |
| 5583 | return false; |
| 5584 | } |
| 5585 | |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 5586 | static bool svm_has_high_real_mode_segbase(void) |
| 5587 | { |
| 5588 | return true; |
| 5589 | } |
| 5590 | |
Paolo Bonzini | fc07e76 | 2015-10-01 13:20:22 +0200 | [diff] [blame] | 5591 | static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio) |
| 5592 | { |
| 5593 | return 0; |
| 5594 | } |
| 5595 | |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 5596 | static void svm_cpuid_update(struct kvm_vcpu *vcpu) |
| 5597 | { |
Joerg Roedel | 6092d3d | 2015-10-14 15:10:54 +0200 | [diff] [blame] | 5598 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5599 | |
| 5600 | /* Update nrips enabled cache */ |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 5601 | svm->nrips_enabled = !!guest_cpuid_has(&svm->vcpu, X86_FEATURE_NRIPS); |
Suravee Suthikulpanit | 46781ea | 2016-05-04 14:09:50 -0500 | [diff] [blame] | 5602 | |
| 5603 | if (!kvm_vcpu_apicv_active(vcpu)) |
| 5604 | return; |
| 5605 | |
Radim Krčmář | 1b4d56b | 2017-08-05 00:12:50 +0200 | [diff] [blame] | 5606 | guest_cpuid_clear(vcpu, X86_FEATURE_X2APIC); |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 5607 | } |
| 5608 | |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 5609 | static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
| 5610 | { |
Joerg Roedel | c2c63a4 | 2010-04-22 12:33:12 +0200 | [diff] [blame] | 5611 | switch (func) { |
Suravee Suthikulpanit | 46781ea | 2016-05-04 14:09:50 -0500 | [diff] [blame] | 5612 | case 0x1: |
| 5613 | if (avic) |
| 5614 | entry->ecx &= ~bit(X86_FEATURE_X2APIC); |
| 5615 | break; |
Joerg Roedel | 4c62a2d | 2010-09-10 17:31:06 +0200 | [diff] [blame] | 5616 | case 0x80000001: |
| 5617 | if (nested) |
| 5618 | entry->ecx |= (1 << 2); /* Set SVM bit */ |
| 5619 | break; |
Joerg Roedel | c2c63a4 | 2010-04-22 12:33:12 +0200 | [diff] [blame] | 5620 | case 0x8000000A: |
| 5621 | entry->eax = 1; /* SVM revision 1 */ |
| 5622 | entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper |
| 5623 | ASID emulation to nested SVM */ |
| 5624 | entry->ecx = 0; /* Reserved */ |
Joerg Roedel | 7a19066 | 2010-07-27 18:14:21 +0200 | [diff] [blame] | 5625 | entry->edx = 0; /* Per default do not support any |
| 5626 | additional features */ |
| 5627 | |
| 5628 | /* Support next_rip if host supports it */ |
Avi Kivity | 2a6b20b | 2010-11-09 16:15:42 +0200 | [diff] [blame] | 5629 | if (boot_cpu_has(X86_FEATURE_NRIPS)) |
Joerg Roedel | 7a19066 | 2010-07-27 18:14:21 +0200 | [diff] [blame] | 5630 | entry->edx |= SVM_FEATURE_NRIP; |
Joerg Roedel | c2c63a4 | 2010-04-22 12:33:12 +0200 | [diff] [blame] | 5631 | |
Joerg Roedel | 3d4aeaa | 2010-09-10 17:31:05 +0200 | [diff] [blame] | 5632 | /* Support NPT for the guest if enabled */ |
| 5633 | if (npt_enabled) |
| 5634 | entry->edx |= SVM_FEATURE_NPT; |
| 5635 | |
Joerg Roedel | c2c63a4 | 2010-04-22 12:33:12 +0200 | [diff] [blame] | 5636 | break; |
Brijesh Singh | 8765d75 | 2017-12-04 10:57:25 -0600 | [diff] [blame] | 5637 | case 0x8000001F: |
| 5638 | /* Support memory encryption cpuid if host supports it */ |
| 5639 | if (boot_cpu_has(X86_FEATURE_SEV)) |
| 5640 | cpuid(0x8000001f, &entry->eax, &entry->ebx, |
| 5641 | &entry->ecx, &entry->edx); |
| 5642 | |
Joerg Roedel | c2c63a4 | 2010-04-22 12:33:12 +0200 | [diff] [blame] | 5643 | } |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 5644 | } |
| 5645 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 5646 | static int svm_get_lpage_level(void) |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 5647 | { |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 5648 | return PT_PDPE_LEVEL; |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 5649 | } |
| 5650 | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 5651 | static bool svm_rdtscp_supported(void) |
| 5652 | { |
Paolo Bonzini | 46896c7 | 2015-11-12 14:49:16 +0100 | [diff] [blame] | 5653 | return boot_cpu_has(X86_FEATURE_RDTSCP); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 5654 | } |
| 5655 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 5656 | static bool svm_invpcid_supported(void) |
| 5657 | { |
| 5658 | return false; |
| 5659 | } |
| 5660 | |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 5661 | static bool svm_mpx_supported(void) |
| 5662 | { |
| 5663 | return false; |
| 5664 | } |
| 5665 | |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 5666 | static bool svm_xsaves_supported(void) |
| 5667 | { |
| 5668 | return false; |
| 5669 | } |
| 5670 | |
Paolo Bonzini | 66336ca | 2016-07-12 10:36:41 +0200 | [diff] [blame] | 5671 | static bool svm_umip_emulated(void) |
| 5672 | { |
| 5673 | return false; |
| 5674 | } |
| 5675 | |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 5676 | static bool svm_has_wbinvd_exit(void) |
| 5677 | { |
| 5678 | return true; |
| 5679 | } |
| 5680 | |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 5681 | #define PRE_EX(exit) { .exit_code = (exit), \ |
Avi Kivity | 40e19b5 | 2011-04-21 12:35:41 +0300 | [diff] [blame] | 5682 | .stage = X86_ICPT_PRE_EXCEPT, } |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 5683 | #define POST_EX(exit) { .exit_code = (exit), \ |
Avi Kivity | 40e19b5 | 2011-04-21 12:35:41 +0300 | [diff] [blame] | 5684 | .stage = X86_ICPT_POST_EXCEPT, } |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 5685 | #define POST_MEM(exit) { .exit_code = (exit), \ |
Avi Kivity | 40e19b5 | 2011-04-21 12:35:41 +0300 | [diff] [blame] | 5686 | .stage = X86_ICPT_POST_MEMACCESS, } |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 5687 | |
Mathias Krause | 09941fb | 2012-08-30 01:30:20 +0200 | [diff] [blame] | 5688 | static const struct __x86_intercept { |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 5689 | u32 exit_code; |
| 5690 | enum x86_intercept_stage stage; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 5691 | } x86_intercept_map[] = { |
| 5692 | [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0), |
| 5693 | [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0), |
| 5694 | [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0), |
| 5695 | [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0), |
| 5696 | [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0), |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 5697 | [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0), |
| 5698 | [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0), |
Joerg Roedel | dee6bb7 | 2011-04-04 12:39:30 +0200 | [diff] [blame] | 5699 | [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ), |
| 5700 | [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ), |
| 5701 | [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE), |
| 5702 | [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE), |
| 5703 | [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ), |
| 5704 | [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ), |
| 5705 | [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE), |
| 5706 | [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE), |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 5707 | [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN), |
| 5708 | [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL), |
| 5709 | [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD), |
| 5710 | [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE), |
| 5711 | [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI), |
| 5712 | [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI), |
| 5713 | [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT), |
| 5714 | [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA), |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 5715 | [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP), |
| 5716 | [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR), |
| 5717 | [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT), |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 5718 | [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG), |
| 5719 | [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD), |
| 5720 | [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD), |
| 5721 | [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR), |
| 5722 | [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC), |
| 5723 | [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR), |
| 5724 | [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC), |
| 5725 | [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID), |
| 5726 | [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM), |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 5727 | [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE), |
| 5728 | [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF), |
| 5729 | [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF), |
| 5730 | [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT), |
| 5731 | [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET), |
| 5732 | [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP), |
| 5733 | [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT), |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 5734 | [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO), |
| 5735 | [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO), |
| 5736 | [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO), |
| 5737 | [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO), |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 5738 | }; |
| 5739 | |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 5740 | #undef PRE_EX |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 5741 | #undef POST_EX |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 5742 | #undef POST_MEM |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 5743 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 5744 | static int svm_check_intercept(struct kvm_vcpu *vcpu, |
| 5745 | struct x86_instruction_info *info, |
| 5746 | enum x86_intercept_stage stage) |
| 5747 | { |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 5748 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5749 | int vmexit, ret = X86EMUL_CONTINUE; |
| 5750 | struct __x86_intercept icpt_info; |
| 5751 | struct vmcb *vmcb = svm->vmcb; |
| 5752 | |
| 5753 | if (info->intercept >= ARRAY_SIZE(x86_intercept_map)) |
| 5754 | goto out; |
| 5755 | |
| 5756 | icpt_info = x86_intercept_map[info->intercept]; |
| 5757 | |
Avi Kivity | 40e19b5 | 2011-04-21 12:35:41 +0300 | [diff] [blame] | 5758 | if (stage != icpt_info.stage) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 5759 | goto out; |
| 5760 | |
| 5761 | switch (icpt_info.exit_code) { |
| 5762 | case SVM_EXIT_READ_CR0: |
| 5763 | if (info->intercept == x86_intercept_cr_read) |
| 5764 | icpt_info.exit_code += info->modrm_reg; |
| 5765 | break; |
| 5766 | case SVM_EXIT_WRITE_CR0: { |
| 5767 | unsigned long cr0, val; |
| 5768 | u64 intercept; |
| 5769 | |
| 5770 | if (info->intercept == x86_intercept_cr_write) |
| 5771 | icpt_info.exit_code += info->modrm_reg; |
| 5772 | |
Jan Kiszka | 62baf44 | 2014-06-29 21:55:53 +0200 | [diff] [blame] | 5773 | if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 || |
| 5774 | info->intercept == x86_intercept_clts) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 5775 | break; |
| 5776 | |
| 5777 | intercept = svm->nested.intercept; |
| 5778 | |
| 5779 | if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))) |
| 5780 | break; |
| 5781 | |
| 5782 | cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK; |
| 5783 | val = info->src_val & ~SVM_CR0_SELECTIVE_MASK; |
| 5784 | |
| 5785 | if (info->intercept == x86_intercept_lmsw) { |
| 5786 | cr0 &= 0xfUL; |
| 5787 | val &= 0xfUL; |
| 5788 | /* lmsw can't clear PE - catch this here */ |
| 5789 | if (cr0 & X86_CR0_PE) |
| 5790 | val |= X86_CR0_PE; |
| 5791 | } |
| 5792 | |
| 5793 | if (cr0 ^ val) |
| 5794 | icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE; |
| 5795 | |
| 5796 | break; |
| 5797 | } |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 5798 | case SVM_EXIT_READ_DR0: |
| 5799 | case SVM_EXIT_WRITE_DR0: |
| 5800 | icpt_info.exit_code += info->modrm_reg; |
| 5801 | break; |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 5802 | case SVM_EXIT_MSR: |
| 5803 | if (info->intercept == x86_intercept_wrmsr) |
| 5804 | vmcb->control.exit_info_1 = 1; |
| 5805 | else |
| 5806 | vmcb->control.exit_info_1 = 0; |
| 5807 | break; |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 5808 | case SVM_EXIT_PAUSE: |
| 5809 | /* |
| 5810 | * We get this for NOP only, but pause |
| 5811 | * is rep not, check this here |
| 5812 | */ |
| 5813 | if (info->rep_prefix != REPE_PREFIX) |
| 5814 | goto out; |
Jan H. Schönherr | 49a8afc | 2017-09-05 23:58:44 +0200 | [diff] [blame] | 5815 | break; |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 5816 | case SVM_EXIT_IOIO: { |
| 5817 | u64 exit_info; |
| 5818 | u32 bytes; |
| 5819 | |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 5820 | if (info->intercept == x86_intercept_in || |
| 5821 | info->intercept == x86_intercept_ins) { |
Jan Kiszka | 6cbc5f5 | 2014-06-30 12:52:55 +0200 | [diff] [blame] | 5822 | exit_info = ((info->src_val & 0xffff) << 16) | |
| 5823 | SVM_IOIO_TYPE_MASK; |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 5824 | bytes = info->dst_bytes; |
Jan Kiszka | 6493f15 | 2014-06-30 11:07:05 +0200 | [diff] [blame] | 5825 | } else { |
Jan Kiszka | 6cbc5f5 | 2014-06-30 12:52:55 +0200 | [diff] [blame] | 5826 | exit_info = (info->dst_val & 0xffff) << 16; |
Jan Kiszka | 6493f15 | 2014-06-30 11:07:05 +0200 | [diff] [blame] | 5827 | bytes = info->src_bytes; |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 5828 | } |
| 5829 | |
| 5830 | if (info->intercept == x86_intercept_outs || |
| 5831 | info->intercept == x86_intercept_ins) |
| 5832 | exit_info |= SVM_IOIO_STR_MASK; |
| 5833 | |
| 5834 | if (info->rep_prefix) |
| 5835 | exit_info |= SVM_IOIO_REP_MASK; |
| 5836 | |
| 5837 | bytes = min(bytes, 4u); |
| 5838 | |
| 5839 | exit_info |= bytes << SVM_IOIO_SIZE_SHIFT; |
| 5840 | |
| 5841 | exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1); |
| 5842 | |
| 5843 | vmcb->control.exit_info_1 = exit_info; |
| 5844 | vmcb->control.exit_info_2 = info->next_rip; |
| 5845 | |
| 5846 | break; |
| 5847 | } |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 5848 | default: |
| 5849 | break; |
| 5850 | } |
| 5851 | |
Bandan Das | f104765 | 2015-06-11 02:05:33 -0400 | [diff] [blame] | 5852 | /* TODO: Advertise NRIPS to guest hypervisor unconditionally */ |
| 5853 | if (static_cpu_has(X86_FEATURE_NRIPS)) |
| 5854 | vmcb->control.next_rip = info->next_rip; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 5855 | vmcb->control.exit_code = icpt_info.exit_code; |
| 5856 | vmexit = nested_svm_exit_handled(svm); |
| 5857 | |
| 5858 | ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED |
| 5859 | : X86EMUL_CONTINUE; |
| 5860 | |
| 5861 | out: |
| 5862 | return ret; |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 5863 | } |
| 5864 | |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5865 | static void svm_handle_external_intr(struct kvm_vcpu *vcpu) |
| 5866 | { |
| 5867 | local_irq_enable(); |
Paolo Bonzini | f2485b3 | 2016-06-15 15:23:11 +0200 | [diff] [blame] | 5868 | /* |
| 5869 | * We must have an instruction with interrupts enabled, so |
| 5870 | * the timer interrupt isn't delayed by the interrupt shadow. |
| 5871 | */ |
| 5872 | asm("nop"); |
| 5873 | local_irq_disable(); |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5874 | } |
| 5875 | |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 5876 | static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu) |
| 5877 | { |
| 5878 | } |
| 5879 | |
Suravee Suthikulpanit | be8ca17 | 2016-05-04 14:09:49 -0500 | [diff] [blame] | 5880 | static inline void avic_post_state_restore(struct kvm_vcpu *vcpu) |
| 5881 | { |
| 5882 | if (avic_handle_apic_id_update(vcpu) != 0) |
| 5883 | return; |
| 5884 | if (avic_handle_dfr_update(vcpu) != 0) |
| 5885 | return; |
| 5886 | avic_handle_ldr_update(vcpu); |
| 5887 | } |
| 5888 | |
Borislav Petkov | 74f1690 | 2017-03-26 23:51:24 +0200 | [diff] [blame] | 5889 | static void svm_setup_mce(struct kvm_vcpu *vcpu) |
| 5890 | { |
| 5891 | /* [63:9] are reserved. */ |
| 5892 | vcpu->arch.mcg_cap &= 0x1ff; |
| 5893 | } |
| 5894 | |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 5895 | static int svm_smi_allowed(struct kvm_vcpu *vcpu) |
| 5896 | { |
Ladi Prosek | 05cade7 | 2017-10-11 16:54:45 +0200 | [diff] [blame] | 5897 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5898 | |
| 5899 | /* Per APM Vol.2 15.22.2 "Response to SMI" */ |
| 5900 | if (!gif_set(svm)) |
| 5901 | return 0; |
| 5902 | |
| 5903 | if (is_guest_mode(&svm->vcpu) && |
| 5904 | svm->nested.intercept & (1ULL << INTERCEPT_SMI)) { |
| 5905 | /* TODO: Might need to set exit_info_1 and exit_info_2 here */ |
| 5906 | svm->vmcb->control.exit_code = SVM_EXIT_SMI; |
| 5907 | svm->nested.exit_required = true; |
| 5908 | return 0; |
| 5909 | } |
| 5910 | |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 5911 | return 1; |
| 5912 | } |
| 5913 | |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 5914 | static int svm_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate) |
| 5915 | { |
Ladi Prosek | 05cade7 | 2017-10-11 16:54:45 +0200 | [diff] [blame] | 5916 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5917 | int ret; |
| 5918 | |
| 5919 | if (is_guest_mode(vcpu)) { |
| 5920 | /* FED8h - SVM Guest */ |
| 5921 | put_smstate(u64, smstate, 0x7ed8, 1); |
| 5922 | /* FEE0h - SVM Guest VMCB Physical Address */ |
| 5923 | put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb); |
| 5924 | |
| 5925 | svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX]; |
| 5926 | svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP]; |
| 5927 | svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP]; |
| 5928 | |
| 5929 | ret = nested_svm_vmexit(svm); |
| 5930 | if (ret) |
| 5931 | return ret; |
| 5932 | } |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 5933 | return 0; |
| 5934 | } |
| 5935 | |
| 5936 | static int svm_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase) |
| 5937 | { |
Ladi Prosek | 05cade7 | 2017-10-11 16:54:45 +0200 | [diff] [blame] | 5938 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5939 | struct vmcb *nested_vmcb; |
| 5940 | struct page *page; |
| 5941 | struct { |
| 5942 | u64 guest; |
| 5943 | u64 vmcb; |
| 5944 | } svm_state_save; |
| 5945 | int ret; |
| 5946 | |
| 5947 | ret = kvm_vcpu_read_guest(vcpu, smbase + 0xfed8, &svm_state_save, |
| 5948 | sizeof(svm_state_save)); |
| 5949 | if (ret) |
| 5950 | return ret; |
| 5951 | |
| 5952 | if (svm_state_save.guest) { |
| 5953 | vcpu->arch.hflags &= ~HF_SMM_MASK; |
| 5954 | nested_vmcb = nested_svm_map(svm, svm_state_save.vmcb, &page); |
| 5955 | if (nested_vmcb) |
| 5956 | enter_svm_guest_mode(svm, svm_state_save.vmcb, nested_vmcb, page); |
| 5957 | else |
| 5958 | ret = 1; |
| 5959 | vcpu->arch.hflags |= HF_SMM_MASK; |
| 5960 | } |
| 5961 | return ret; |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 5962 | } |
| 5963 | |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 5964 | static int enable_smi_window(struct kvm_vcpu *vcpu) |
| 5965 | { |
| 5966 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5967 | |
| 5968 | if (!gif_set(svm)) { |
| 5969 | if (vgif_enabled(svm)) |
| 5970 | set_intercept(svm, INTERCEPT_STGI); |
| 5971 | /* STGI will cause a vm exit */ |
| 5972 | return 1; |
| 5973 | } |
| 5974 | return 0; |
| 5975 | } |
| 5976 | |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 5977 | static int sev_asid_new(void) |
| 5978 | { |
| 5979 | int pos; |
| 5980 | |
| 5981 | /* |
| 5982 | * SEV-enabled guest must use asid from min_sev_asid to max_sev_asid. |
| 5983 | */ |
| 5984 | pos = find_next_zero_bit(sev_asid_bitmap, max_sev_asid, min_sev_asid - 1); |
| 5985 | if (pos >= max_sev_asid) |
| 5986 | return -EBUSY; |
| 5987 | |
| 5988 | set_bit(pos, sev_asid_bitmap); |
| 5989 | return pos + 1; |
| 5990 | } |
| 5991 | |
| 5992 | static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 5993 | { |
| 5994 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
| 5995 | int asid, ret; |
| 5996 | |
| 5997 | ret = -EBUSY; |
| 5998 | asid = sev_asid_new(); |
| 5999 | if (asid < 0) |
| 6000 | return ret; |
| 6001 | |
| 6002 | ret = sev_platform_init(&argp->error); |
| 6003 | if (ret) |
| 6004 | goto e_free; |
| 6005 | |
| 6006 | sev->active = true; |
| 6007 | sev->asid = asid; |
Brijesh Singh | 1e80fdc | 2017-12-04 10:57:38 -0600 | [diff] [blame] | 6008 | INIT_LIST_HEAD(&sev->regions_list); |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 6009 | |
| 6010 | return 0; |
| 6011 | |
| 6012 | e_free: |
| 6013 | __sev_asid_free(asid); |
| 6014 | return ret; |
| 6015 | } |
| 6016 | |
Brijesh Singh | 59414c9 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 6017 | static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error) |
| 6018 | { |
| 6019 | struct sev_data_activate *data; |
| 6020 | int asid = sev_get_asid(kvm); |
| 6021 | int ret; |
| 6022 | |
| 6023 | wbinvd_on_all_cpus(); |
| 6024 | |
| 6025 | ret = sev_guest_df_flush(error); |
| 6026 | if (ret) |
| 6027 | return ret; |
| 6028 | |
| 6029 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 6030 | if (!data) |
| 6031 | return -ENOMEM; |
| 6032 | |
| 6033 | /* activate ASID on the given handle */ |
| 6034 | data->handle = handle; |
| 6035 | data->asid = asid; |
| 6036 | ret = sev_guest_activate(data, error); |
| 6037 | kfree(data); |
| 6038 | |
| 6039 | return ret; |
| 6040 | } |
| 6041 | |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 6042 | static int __sev_issue_cmd(int fd, int id, void *data, int *error) |
Brijesh Singh | 59414c9 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 6043 | { |
| 6044 | struct fd f; |
| 6045 | int ret; |
| 6046 | |
| 6047 | f = fdget(fd); |
| 6048 | if (!f.file) |
| 6049 | return -EBADF; |
| 6050 | |
| 6051 | ret = sev_issue_cmd_external_user(f.file, id, data, error); |
| 6052 | |
| 6053 | fdput(f); |
| 6054 | return ret; |
| 6055 | } |
| 6056 | |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 6057 | static int sev_issue_cmd(struct kvm *kvm, int id, void *data, int *error) |
| 6058 | { |
| 6059 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
| 6060 | |
| 6061 | return __sev_issue_cmd(sev->fd, id, data, error); |
| 6062 | } |
| 6063 | |
Brijesh Singh | 59414c9 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 6064 | static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 6065 | { |
| 6066 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
| 6067 | struct sev_data_launch_start *start; |
| 6068 | struct kvm_sev_launch_start params; |
| 6069 | void *dh_blob, *session_blob; |
| 6070 | int *error = &argp->error; |
| 6071 | int ret; |
| 6072 | |
| 6073 | if (!sev_guest(kvm)) |
| 6074 | return -ENOTTY; |
| 6075 | |
| 6076 | if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) |
| 6077 | return -EFAULT; |
| 6078 | |
| 6079 | start = kzalloc(sizeof(*start), GFP_KERNEL); |
| 6080 | if (!start) |
| 6081 | return -ENOMEM; |
| 6082 | |
| 6083 | dh_blob = NULL; |
| 6084 | if (params.dh_uaddr) { |
| 6085 | dh_blob = psp_copy_user_blob(params.dh_uaddr, params.dh_len); |
| 6086 | if (IS_ERR(dh_blob)) { |
| 6087 | ret = PTR_ERR(dh_blob); |
| 6088 | goto e_free; |
| 6089 | } |
| 6090 | |
| 6091 | start->dh_cert_address = __sme_set(__pa(dh_blob)); |
| 6092 | start->dh_cert_len = params.dh_len; |
| 6093 | } |
| 6094 | |
| 6095 | session_blob = NULL; |
| 6096 | if (params.session_uaddr) { |
| 6097 | session_blob = psp_copy_user_blob(params.session_uaddr, params.session_len); |
| 6098 | if (IS_ERR(session_blob)) { |
| 6099 | ret = PTR_ERR(session_blob); |
| 6100 | goto e_free_dh; |
| 6101 | } |
| 6102 | |
| 6103 | start->session_address = __sme_set(__pa(session_blob)); |
| 6104 | start->session_len = params.session_len; |
| 6105 | } |
| 6106 | |
| 6107 | start->handle = params.handle; |
| 6108 | start->policy = params.policy; |
| 6109 | |
| 6110 | /* create memory encryption context */ |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 6111 | ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_LAUNCH_START, start, error); |
Brijesh Singh | 59414c9 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 6112 | if (ret) |
| 6113 | goto e_free_session; |
| 6114 | |
| 6115 | /* Bind ASID to this guest */ |
| 6116 | ret = sev_bind_asid(kvm, start->handle, error); |
| 6117 | if (ret) |
| 6118 | goto e_free_session; |
| 6119 | |
| 6120 | /* return handle to userspace */ |
| 6121 | params.handle = start->handle; |
| 6122 | if (copy_to_user((void __user *)(uintptr_t)argp->data, ¶ms, sizeof(params))) { |
| 6123 | sev_unbind_asid(kvm, start->handle); |
| 6124 | ret = -EFAULT; |
| 6125 | goto e_free_session; |
| 6126 | } |
| 6127 | |
| 6128 | sev->handle = start->handle; |
| 6129 | sev->fd = argp->sev_fd; |
| 6130 | |
| 6131 | e_free_session: |
| 6132 | kfree(session_blob); |
| 6133 | e_free_dh: |
| 6134 | kfree(dh_blob); |
| 6135 | e_free: |
| 6136 | kfree(start); |
| 6137 | return ret; |
| 6138 | } |
| 6139 | |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 6140 | static int get_num_contig_pages(int idx, struct page **inpages, |
| 6141 | unsigned long npages) |
| 6142 | { |
| 6143 | unsigned long paddr, next_paddr; |
| 6144 | int i = idx + 1, pages = 1; |
| 6145 | |
| 6146 | /* find the number of contiguous pages starting from idx */ |
| 6147 | paddr = __sme_page_pa(inpages[idx]); |
| 6148 | while (i < npages) { |
| 6149 | next_paddr = __sme_page_pa(inpages[i++]); |
| 6150 | if ((paddr + PAGE_SIZE) == next_paddr) { |
| 6151 | pages++; |
| 6152 | paddr = next_paddr; |
| 6153 | continue; |
| 6154 | } |
| 6155 | break; |
| 6156 | } |
| 6157 | |
| 6158 | return pages; |
| 6159 | } |
| 6160 | |
| 6161 | static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 6162 | { |
| 6163 | unsigned long vaddr, vaddr_end, next_vaddr, npages, size; |
| 6164 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
| 6165 | struct kvm_sev_launch_update_data params; |
| 6166 | struct sev_data_launch_update_data *data; |
| 6167 | struct page **inpages; |
| 6168 | int i, ret, pages; |
| 6169 | |
| 6170 | if (!sev_guest(kvm)) |
| 6171 | return -ENOTTY; |
| 6172 | |
| 6173 | if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) |
| 6174 | return -EFAULT; |
| 6175 | |
| 6176 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 6177 | if (!data) |
| 6178 | return -ENOMEM; |
| 6179 | |
| 6180 | vaddr = params.uaddr; |
| 6181 | size = params.len; |
| 6182 | vaddr_end = vaddr + size; |
| 6183 | |
| 6184 | /* Lock the user memory. */ |
| 6185 | inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1); |
| 6186 | if (!inpages) { |
| 6187 | ret = -ENOMEM; |
| 6188 | goto e_free; |
| 6189 | } |
| 6190 | |
| 6191 | /* |
| 6192 | * The LAUNCH_UPDATE command will perform in-place encryption of the |
| 6193 | * memory content (i.e it will write the same memory region with C=1). |
| 6194 | * It's possible that the cache may contain the data with C=0, i.e., |
| 6195 | * unencrypted so invalidate it first. |
| 6196 | */ |
| 6197 | sev_clflush_pages(inpages, npages); |
| 6198 | |
| 6199 | for (i = 0; vaddr < vaddr_end; vaddr = next_vaddr, i += pages) { |
| 6200 | int offset, len; |
| 6201 | |
| 6202 | /* |
| 6203 | * If the user buffer is not page-aligned, calculate the offset |
| 6204 | * within the page. |
| 6205 | */ |
| 6206 | offset = vaddr & (PAGE_SIZE - 1); |
| 6207 | |
| 6208 | /* Calculate the number of pages that can be encrypted in one go. */ |
| 6209 | pages = get_num_contig_pages(i, inpages, npages); |
| 6210 | |
| 6211 | len = min_t(size_t, ((pages * PAGE_SIZE) - offset), size); |
| 6212 | |
| 6213 | data->handle = sev->handle; |
| 6214 | data->len = len; |
| 6215 | data->address = __sme_page_pa(inpages[i]) + offset; |
| 6216 | ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_DATA, data, &argp->error); |
| 6217 | if (ret) |
| 6218 | goto e_unpin; |
| 6219 | |
| 6220 | size -= len; |
| 6221 | next_vaddr = vaddr + len; |
| 6222 | } |
| 6223 | |
| 6224 | e_unpin: |
| 6225 | /* content of memory is updated, mark pages dirty */ |
| 6226 | for (i = 0; i < npages; i++) { |
| 6227 | set_page_dirty_lock(inpages[i]); |
| 6228 | mark_page_accessed(inpages[i]); |
| 6229 | } |
| 6230 | /* unlock the user pages */ |
| 6231 | sev_unpin_memory(kvm, inpages, npages); |
| 6232 | e_free: |
| 6233 | kfree(data); |
| 6234 | return ret; |
| 6235 | } |
| 6236 | |
Brijesh Singh | 0d0736f | 2017-12-04 10:57:36 -0600 | [diff] [blame] | 6237 | static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 6238 | { |
Brijesh Singh | 3e23338 | 2018-02-23 12:36:50 -0600 | [diff] [blame^] | 6239 | void __user *measure = (void __user *)(uintptr_t)argp->data; |
Brijesh Singh | 0d0736f | 2017-12-04 10:57:36 -0600 | [diff] [blame] | 6240 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
| 6241 | struct sev_data_launch_measure *data; |
| 6242 | struct kvm_sev_launch_measure params; |
Brijesh Singh | 3e23338 | 2018-02-23 12:36:50 -0600 | [diff] [blame^] | 6243 | void __user *p = NULL; |
Brijesh Singh | 0d0736f | 2017-12-04 10:57:36 -0600 | [diff] [blame] | 6244 | void *blob = NULL; |
| 6245 | int ret; |
| 6246 | |
| 6247 | if (!sev_guest(kvm)) |
| 6248 | return -ENOTTY; |
| 6249 | |
Brijesh Singh | 3e23338 | 2018-02-23 12:36:50 -0600 | [diff] [blame^] | 6250 | if (copy_from_user(¶ms, measure, sizeof(params))) |
Brijesh Singh | 0d0736f | 2017-12-04 10:57:36 -0600 | [diff] [blame] | 6251 | return -EFAULT; |
| 6252 | |
| 6253 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 6254 | if (!data) |
| 6255 | return -ENOMEM; |
| 6256 | |
| 6257 | /* User wants to query the blob length */ |
| 6258 | if (!params.len) |
| 6259 | goto cmd; |
| 6260 | |
Brijesh Singh | 3e23338 | 2018-02-23 12:36:50 -0600 | [diff] [blame^] | 6261 | p = (void __user *)(uintptr_t)params.uaddr; |
| 6262 | if (p) { |
Brijesh Singh | 0d0736f | 2017-12-04 10:57:36 -0600 | [diff] [blame] | 6263 | if (params.len > SEV_FW_BLOB_MAX_SIZE) { |
| 6264 | ret = -EINVAL; |
| 6265 | goto e_free; |
| 6266 | } |
| 6267 | |
Brijesh Singh | 0d0736f | 2017-12-04 10:57:36 -0600 | [diff] [blame] | 6268 | ret = -ENOMEM; |
| 6269 | blob = kmalloc(params.len, GFP_KERNEL); |
| 6270 | if (!blob) |
| 6271 | goto e_free; |
| 6272 | |
| 6273 | data->address = __psp_pa(blob); |
| 6274 | data->len = params.len; |
| 6275 | } |
| 6276 | |
| 6277 | cmd: |
| 6278 | data->handle = sev->handle; |
| 6279 | ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_MEASURE, data, &argp->error); |
| 6280 | |
| 6281 | /* |
| 6282 | * If we query the session length, FW responded with expected data. |
| 6283 | */ |
| 6284 | if (!params.len) |
| 6285 | goto done; |
| 6286 | |
| 6287 | if (ret) |
| 6288 | goto e_free_blob; |
| 6289 | |
| 6290 | if (blob) { |
Brijesh Singh | 3e23338 | 2018-02-23 12:36:50 -0600 | [diff] [blame^] | 6291 | if (copy_to_user(p, blob, params.len)) |
Brijesh Singh | 0d0736f | 2017-12-04 10:57:36 -0600 | [diff] [blame] | 6292 | ret = -EFAULT; |
| 6293 | } |
| 6294 | |
| 6295 | done: |
| 6296 | params.len = data->len; |
Brijesh Singh | 3e23338 | 2018-02-23 12:36:50 -0600 | [diff] [blame^] | 6297 | if (copy_to_user(measure, ¶ms, sizeof(params))) |
Brijesh Singh | 0d0736f | 2017-12-04 10:57:36 -0600 | [diff] [blame] | 6298 | ret = -EFAULT; |
| 6299 | e_free_blob: |
| 6300 | kfree(blob); |
| 6301 | e_free: |
| 6302 | kfree(data); |
| 6303 | return ret; |
| 6304 | } |
| 6305 | |
Brijesh Singh | 5bdb0e2 | 2017-12-04 10:57:36 -0600 | [diff] [blame] | 6306 | static int sev_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 6307 | { |
| 6308 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
| 6309 | struct sev_data_launch_finish *data; |
| 6310 | int ret; |
| 6311 | |
| 6312 | if (!sev_guest(kvm)) |
| 6313 | return -ENOTTY; |
| 6314 | |
| 6315 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 6316 | if (!data) |
| 6317 | return -ENOMEM; |
| 6318 | |
| 6319 | data->handle = sev->handle; |
| 6320 | ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_FINISH, data, &argp->error); |
| 6321 | |
| 6322 | kfree(data); |
| 6323 | return ret; |
| 6324 | } |
| 6325 | |
Brijesh Singh | 255d9e7 | 2017-12-04 10:57:37 -0600 | [diff] [blame] | 6326 | static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 6327 | { |
| 6328 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
| 6329 | struct kvm_sev_guest_status params; |
| 6330 | struct sev_data_guest_status *data; |
| 6331 | int ret; |
| 6332 | |
| 6333 | if (!sev_guest(kvm)) |
| 6334 | return -ENOTTY; |
| 6335 | |
| 6336 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 6337 | if (!data) |
| 6338 | return -ENOMEM; |
| 6339 | |
| 6340 | data->handle = sev->handle; |
| 6341 | ret = sev_issue_cmd(kvm, SEV_CMD_GUEST_STATUS, data, &argp->error); |
| 6342 | if (ret) |
| 6343 | goto e_free; |
| 6344 | |
| 6345 | params.policy = data->policy; |
| 6346 | params.state = data->state; |
| 6347 | params.handle = data->handle; |
| 6348 | |
| 6349 | if (copy_to_user((void __user *)(uintptr_t)argp->data, ¶ms, sizeof(params))) |
| 6350 | ret = -EFAULT; |
| 6351 | e_free: |
| 6352 | kfree(data); |
| 6353 | return ret; |
| 6354 | } |
| 6355 | |
Brijesh Singh | 24f41fb | 2017-12-04 10:57:37 -0600 | [diff] [blame] | 6356 | static int __sev_issue_dbg_cmd(struct kvm *kvm, unsigned long src, |
| 6357 | unsigned long dst, int size, |
| 6358 | int *error, bool enc) |
| 6359 | { |
| 6360 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
| 6361 | struct sev_data_dbg *data; |
| 6362 | int ret; |
| 6363 | |
| 6364 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 6365 | if (!data) |
| 6366 | return -ENOMEM; |
| 6367 | |
| 6368 | data->handle = sev->handle; |
| 6369 | data->dst_addr = dst; |
| 6370 | data->src_addr = src; |
| 6371 | data->len = size; |
| 6372 | |
| 6373 | ret = sev_issue_cmd(kvm, |
| 6374 | enc ? SEV_CMD_DBG_ENCRYPT : SEV_CMD_DBG_DECRYPT, |
| 6375 | data, error); |
| 6376 | kfree(data); |
| 6377 | return ret; |
| 6378 | } |
| 6379 | |
| 6380 | static int __sev_dbg_decrypt(struct kvm *kvm, unsigned long src_paddr, |
| 6381 | unsigned long dst_paddr, int sz, int *err) |
| 6382 | { |
| 6383 | int offset; |
| 6384 | |
| 6385 | /* |
| 6386 | * Its safe to read more than we are asked, caller should ensure that |
| 6387 | * destination has enough space. |
| 6388 | */ |
| 6389 | src_paddr = round_down(src_paddr, 16); |
| 6390 | offset = src_paddr & 15; |
| 6391 | sz = round_up(sz + offset, 16); |
| 6392 | |
| 6393 | return __sev_issue_dbg_cmd(kvm, src_paddr, dst_paddr, sz, err, false); |
| 6394 | } |
| 6395 | |
| 6396 | static int __sev_dbg_decrypt_user(struct kvm *kvm, unsigned long paddr, |
| 6397 | unsigned long __user dst_uaddr, |
| 6398 | unsigned long dst_paddr, |
| 6399 | int size, int *err) |
| 6400 | { |
| 6401 | struct page *tpage = NULL; |
| 6402 | int ret, offset; |
| 6403 | |
| 6404 | /* if inputs are not 16-byte then use intermediate buffer */ |
| 6405 | if (!IS_ALIGNED(dst_paddr, 16) || |
| 6406 | !IS_ALIGNED(paddr, 16) || |
| 6407 | !IS_ALIGNED(size, 16)) { |
| 6408 | tpage = (void *)alloc_page(GFP_KERNEL); |
| 6409 | if (!tpage) |
| 6410 | return -ENOMEM; |
| 6411 | |
| 6412 | dst_paddr = __sme_page_pa(tpage); |
| 6413 | } |
| 6414 | |
| 6415 | ret = __sev_dbg_decrypt(kvm, paddr, dst_paddr, size, err); |
| 6416 | if (ret) |
| 6417 | goto e_free; |
| 6418 | |
| 6419 | if (tpage) { |
| 6420 | offset = paddr & 15; |
| 6421 | if (copy_to_user((void __user *)(uintptr_t)dst_uaddr, |
| 6422 | page_address(tpage) + offset, size)) |
| 6423 | ret = -EFAULT; |
| 6424 | } |
| 6425 | |
| 6426 | e_free: |
| 6427 | if (tpage) |
| 6428 | __free_page(tpage); |
| 6429 | |
| 6430 | return ret; |
| 6431 | } |
| 6432 | |
Brijesh Singh | 7d1594f | 2017-12-04 10:57:37 -0600 | [diff] [blame] | 6433 | static int __sev_dbg_encrypt_user(struct kvm *kvm, unsigned long paddr, |
| 6434 | unsigned long __user vaddr, |
| 6435 | unsigned long dst_paddr, |
| 6436 | unsigned long __user dst_vaddr, |
| 6437 | int size, int *error) |
| 6438 | { |
| 6439 | struct page *src_tpage = NULL; |
| 6440 | struct page *dst_tpage = NULL; |
| 6441 | int ret, len = size; |
| 6442 | |
| 6443 | /* If source buffer is not aligned then use an intermediate buffer */ |
| 6444 | if (!IS_ALIGNED(vaddr, 16)) { |
| 6445 | src_tpage = alloc_page(GFP_KERNEL); |
| 6446 | if (!src_tpage) |
| 6447 | return -ENOMEM; |
| 6448 | |
| 6449 | if (copy_from_user(page_address(src_tpage), |
| 6450 | (void __user *)(uintptr_t)vaddr, size)) { |
| 6451 | __free_page(src_tpage); |
| 6452 | return -EFAULT; |
| 6453 | } |
| 6454 | |
| 6455 | paddr = __sme_page_pa(src_tpage); |
| 6456 | } |
| 6457 | |
| 6458 | /* |
| 6459 | * If destination buffer or length is not aligned then do read-modify-write: |
| 6460 | * - decrypt destination in an intermediate buffer |
| 6461 | * - copy the source buffer in an intermediate buffer |
| 6462 | * - use the intermediate buffer as source buffer |
| 6463 | */ |
| 6464 | if (!IS_ALIGNED(dst_vaddr, 16) || !IS_ALIGNED(size, 16)) { |
| 6465 | int dst_offset; |
| 6466 | |
| 6467 | dst_tpage = alloc_page(GFP_KERNEL); |
| 6468 | if (!dst_tpage) { |
| 6469 | ret = -ENOMEM; |
| 6470 | goto e_free; |
| 6471 | } |
| 6472 | |
| 6473 | ret = __sev_dbg_decrypt(kvm, dst_paddr, |
| 6474 | __sme_page_pa(dst_tpage), size, error); |
| 6475 | if (ret) |
| 6476 | goto e_free; |
| 6477 | |
| 6478 | /* |
| 6479 | * If source is kernel buffer then use memcpy() otherwise |
| 6480 | * copy_from_user(). |
| 6481 | */ |
| 6482 | dst_offset = dst_paddr & 15; |
| 6483 | |
| 6484 | if (src_tpage) |
| 6485 | memcpy(page_address(dst_tpage) + dst_offset, |
| 6486 | page_address(src_tpage), size); |
| 6487 | else { |
| 6488 | if (copy_from_user(page_address(dst_tpage) + dst_offset, |
| 6489 | (void __user *)(uintptr_t)vaddr, size)) { |
| 6490 | ret = -EFAULT; |
| 6491 | goto e_free; |
| 6492 | } |
| 6493 | } |
| 6494 | |
| 6495 | paddr = __sme_page_pa(dst_tpage); |
| 6496 | dst_paddr = round_down(dst_paddr, 16); |
| 6497 | len = round_up(size, 16); |
| 6498 | } |
| 6499 | |
| 6500 | ret = __sev_issue_dbg_cmd(kvm, paddr, dst_paddr, len, error, true); |
| 6501 | |
| 6502 | e_free: |
| 6503 | if (src_tpage) |
| 6504 | __free_page(src_tpage); |
| 6505 | if (dst_tpage) |
| 6506 | __free_page(dst_tpage); |
| 6507 | return ret; |
| 6508 | } |
| 6509 | |
Brijesh Singh | 24f41fb | 2017-12-04 10:57:37 -0600 | [diff] [blame] | 6510 | static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec) |
| 6511 | { |
| 6512 | unsigned long vaddr, vaddr_end, next_vaddr; |
| 6513 | unsigned long dst_vaddr, dst_vaddr_end; |
| 6514 | struct page **src_p, **dst_p; |
| 6515 | struct kvm_sev_dbg debug; |
| 6516 | unsigned long n; |
| 6517 | int ret, size; |
| 6518 | |
| 6519 | if (!sev_guest(kvm)) |
| 6520 | return -ENOTTY; |
| 6521 | |
| 6522 | if (copy_from_user(&debug, (void __user *)(uintptr_t)argp->data, sizeof(debug))) |
| 6523 | return -EFAULT; |
| 6524 | |
| 6525 | vaddr = debug.src_uaddr; |
| 6526 | size = debug.len; |
| 6527 | vaddr_end = vaddr + size; |
| 6528 | dst_vaddr = debug.dst_uaddr; |
| 6529 | dst_vaddr_end = dst_vaddr + size; |
| 6530 | |
| 6531 | for (; vaddr < vaddr_end; vaddr = next_vaddr) { |
| 6532 | int len, s_off, d_off; |
| 6533 | |
| 6534 | /* lock userspace source and destination page */ |
| 6535 | src_p = sev_pin_memory(kvm, vaddr & PAGE_MASK, PAGE_SIZE, &n, 0); |
| 6536 | if (!src_p) |
| 6537 | return -EFAULT; |
| 6538 | |
| 6539 | dst_p = sev_pin_memory(kvm, dst_vaddr & PAGE_MASK, PAGE_SIZE, &n, 1); |
| 6540 | if (!dst_p) { |
| 6541 | sev_unpin_memory(kvm, src_p, n); |
| 6542 | return -EFAULT; |
| 6543 | } |
| 6544 | |
| 6545 | /* |
| 6546 | * The DBG_{DE,EN}CRYPT commands will perform {dec,en}cryption of the |
| 6547 | * memory content (i.e it will write the same memory region with C=1). |
| 6548 | * It's possible that the cache may contain the data with C=0, i.e., |
| 6549 | * unencrypted so invalidate it first. |
| 6550 | */ |
| 6551 | sev_clflush_pages(src_p, 1); |
| 6552 | sev_clflush_pages(dst_p, 1); |
| 6553 | |
| 6554 | /* |
| 6555 | * Since user buffer may not be page aligned, calculate the |
| 6556 | * offset within the page. |
| 6557 | */ |
| 6558 | s_off = vaddr & ~PAGE_MASK; |
| 6559 | d_off = dst_vaddr & ~PAGE_MASK; |
| 6560 | len = min_t(size_t, (PAGE_SIZE - s_off), size); |
| 6561 | |
Brijesh Singh | 7d1594f | 2017-12-04 10:57:37 -0600 | [diff] [blame] | 6562 | if (dec) |
| 6563 | ret = __sev_dbg_decrypt_user(kvm, |
| 6564 | __sme_page_pa(src_p[0]) + s_off, |
| 6565 | dst_vaddr, |
| 6566 | __sme_page_pa(dst_p[0]) + d_off, |
| 6567 | len, &argp->error); |
| 6568 | else |
| 6569 | ret = __sev_dbg_encrypt_user(kvm, |
| 6570 | __sme_page_pa(src_p[0]) + s_off, |
| 6571 | vaddr, |
| 6572 | __sme_page_pa(dst_p[0]) + d_off, |
| 6573 | dst_vaddr, |
| 6574 | len, &argp->error); |
Brijesh Singh | 24f41fb | 2017-12-04 10:57:37 -0600 | [diff] [blame] | 6575 | |
| 6576 | sev_unpin_memory(kvm, src_p, 1); |
| 6577 | sev_unpin_memory(kvm, dst_p, 1); |
| 6578 | |
| 6579 | if (ret) |
| 6580 | goto err; |
| 6581 | |
| 6582 | next_vaddr = vaddr + len; |
| 6583 | dst_vaddr = dst_vaddr + len; |
| 6584 | size -= len; |
| 6585 | } |
| 6586 | err: |
| 6587 | return ret; |
| 6588 | } |
| 6589 | |
Brijesh Singh | 9f5b5b9 | 2017-12-04 10:57:38 -0600 | [diff] [blame] | 6590 | static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 6591 | { |
| 6592 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
| 6593 | struct sev_data_launch_secret *data; |
| 6594 | struct kvm_sev_launch_secret params; |
| 6595 | struct page **pages; |
| 6596 | void *blob, *hdr; |
| 6597 | unsigned long n; |
| 6598 | int ret; |
| 6599 | |
| 6600 | if (!sev_guest(kvm)) |
| 6601 | return -ENOTTY; |
| 6602 | |
| 6603 | if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) |
| 6604 | return -EFAULT; |
| 6605 | |
| 6606 | pages = sev_pin_memory(kvm, params.guest_uaddr, params.guest_len, &n, 1); |
| 6607 | if (!pages) |
| 6608 | return -ENOMEM; |
| 6609 | |
| 6610 | /* |
| 6611 | * The secret must be copied into contiguous memory region, lets verify |
| 6612 | * that userspace memory pages are contiguous before we issue command. |
| 6613 | */ |
| 6614 | if (get_num_contig_pages(0, pages, n) != n) { |
| 6615 | ret = -EINVAL; |
| 6616 | goto e_unpin_memory; |
| 6617 | } |
| 6618 | |
| 6619 | ret = -ENOMEM; |
| 6620 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 6621 | if (!data) |
| 6622 | goto e_unpin_memory; |
| 6623 | |
| 6624 | blob = psp_copy_user_blob(params.trans_uaddr, params.trans_len); |
| 6625 | if (IS_ERR(blob)) { |
| 6626 | ret = PTR_ERR(blob); |
| 6627 | goto e_free; |
| 6628 | } |
| 6629 | |
| 6630 | data->trans_address = __psp_pa(blob); |
| 6631 | data->trans_len = params.trans_len; |
| 6632 | |
| 6633 | hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len); |
| 6634 | if (IS_ERR(hdr)) { |
| 6635 | ret = PTR_ERR(hdr); |
| 6636 | goto e_free_blob; |
| 6637 | } |
| 6638 | data->trans_address = __psp_pa(blob); |
| 6639 | data->trans_len = params.trans_len; |
| 6640 | |
| 6641 | data->handle = sev->handle; |
| 6642 | ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_SECRET, data, &argp->error); |
| 6643 | |
| 6644 | kfree(hdr); |
| 6645 | |
| 6646 | e_free_blob: |
| 6647 | kfree(blob); |
| 6648 | e_free: |
| 6649 | kfree(data); |
| 6650 | e_unpin_memory: |
| 6651 | sev_unpin_memory(kvm, pages, n); |
| 6652 | return ret; |
| 6653 | } |
| 6654 | |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 6655 | static int svm_mem_enc_op(struct kvm *kvm, void __user *argp) |
| 6656 | { |
| 6657 | struct kvm_sev_cmd sev_cmd; |
| 6658 | int r; |
| 6659 | |
| 6660 | if (!svm_sev_enabled()) |
| 6661 | return -ENOTTY; |
| 6662 | |
| 6663 | if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd))) |
| 6664 | return -EFAULT; |
| 6665 | |
| 6666 | mutex_lock(&kvm->lock); |
| 6667 | |
| 6668 | switch (sev_cmd.id) { |
| 6669 | case KVM_SEV_INIT: |
| 6670 | r = sev_guest_init(kvm, &sev_cmd); |
| 6671 | break; |
Brijesh Singh | 59414c9 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 6672 | case KVM_SEV_LAUNCH_START: |
| 6673 | r = sev_launch_start(kvm, &sev_cmd); |
| 6674 | break; |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 6675 | case KVM_SEV_LAUNCH_UPDATE_DATA: |
| 6676 | r = sev_launch_update_data(kvm, &sev_cmd); |
| 6677 | break; |
Brijesh Singh | 0d0736f | 2017-12-04 10:57:36 -0600 | [diff] [blame] | 6678 | case KVM_SEV_LAUNCH_MEASURE: |
| 6679 | r = sev_launch_measure(kvm, &sev_cmd); |
| 6680 | break; |
Brijesh Singh | 5bdb0e2 | 2017-12-04 10:57:36 -0600 | [diff] [blame] | 6681 | case KVM_SEV_LAUNCH_FINISH: |
| 6682 | r = sev_launch_finish(kvm, &sev_cmd); |
| 6683 | break; |
Brijesh Singh | 255d9e7 | 2017-12-04 10:57:37 -0600 | [diff] [blame] | 6684 | case KVM_SEV_GUEST_STATUS: |
| 6685 | r = sev_guest_status(kvm, &sev_cmd); |
| 6686 | break; |
Brijesh Singh | 24f41fb | 2017-12-04 10:57:37 -0600 | [diff] [blame] | 6687 | case KVM_SEV_DBG_DECRYPT: |
| 6688 | r = sev_dbg_crypt(kvm, &sev_cmd, true); |
| 6689 | break; |
Brijesh Singh | 7d1594f | 2017-12-04 10:57:37 -0600 | [diff] [blame] | 6690 | case KVM_SEV_DBG_ENCRYPT: |
| 6691 | r = sev_dbg_crypt(kvm, &sev_cmd, false); |
| 6692 | break; |
Brijesh Singh | 9f5b5b9 | 2017-12-04 10:57:38 -0600 | [diff] [blame] | 6693 | case KVM_SEV_LAUNCH_SECRET: |
| 6694 | r = sev_launch_secret(kvm, &sev_cmd); |
| 6695 | break; |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 6696 | default: |
| 6697 | r = -EINVAL; |
| 6698 | goto out; |
| 6699 | } |
| 6700 | |
| 6701 | if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd))) |
| 6702 | r = -EFAULT; |
| 6703 | |
| 6704 | out: |
| 6705 | mutex_unlock(&kvm->lock); |
| 6706 | return r; |
| 6707 | } |
| 6708 | |
Brijesh Singh | 1e80fdc | 2017-12-04 10:57:38 -0600 | [diff] [blame] | 6709 | static int svm_register_enc_region(struct kvm *kvm, |
| 6710 | struct kvm_enc_region *range) |
| 6711 | { |
| 6712 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
| 6713 | struct enc_region *region; |
| 6714 | int ret = 0; |
| 6715 | |
| 6716 | if (!sev_guest(kvm)) |
| 6717 | return -ENOTTY; |
| 6718 | |
| 6719 | region = kzalloc(sizeof(*region), GFP_KERNEL); |
| 6720 | if (!region) |
| 6721 | return -ENOMEM; |
| 6722 | |
| 6723 | region->pages = sev_pin_memory(kvm, range->addr, range->size, ®ion->npages, 1); |
| 6724 | if (!region->pages) { |
| 6725 | ret = -ENOMEM; |
| 6726 | goto e_free; |
| 6727 | } |
| 6728 | |
| 6729 | /* |
| 6730 | * The guest may change the memory encryption attribute from C=0 -> C=1 |
| 6731 | * or vice versa for this memory range. Lets make sure caches are |
| 6732 | * flushed to ensure that guest data gets written into memory with |
| 6733 | * correct C-bit. |
| 6734 | */ |
| 6735 | sev_clflush_pages(region->pages, region->npages); |
| 6736 | |
| 6737 | region->uaddr = range->addr; |
| 6738 | region->size = range->size; |
| 6739 | |
| 6740 | mutex_lock(&kvm->lock); |
| 6741 | list_add_tail(®ion->list, &sev->regions_list); |
| 6742 | mutex_unlock(&kvm->lock); |
| 6743 | |
| 6744 | return ret; |
| 6745 | |
| 6746 | e_free: |
| 6747 | kfree(region); |
| 6748 | return ret; |
| 6749 | } |
| 6750 | |
| 6751 | static struct enc_region * |
| 6752 | find_enc_region(struct kvm *kvm, struct kvm_enc_region *range) |
| 6753 | { |
| 6754 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
| 6755 | struct list_head *head = &sev->regions_list; |
| 6756 | struct enc_region *i; |
| 6757 | |
| 6758 | list_for_each_entry(i, head, list) { |
| 6759 | if (i->uaddr == range->addr && |
| 6760 | i->size == range->size) |
| 6761 | return i; |
| 6762 | } |
| 6763 | |
| 6764 | return NULL; |
| 6765 | } |
| 6766 | |
| 6767 | |
| 6768 | static int svm_unregister_enc_region(struct kvm *kvm, |
| 6769 | struct kvm_enc_region *range) |
| 6770 | { |
| 6771 | struct enc_region *region; |
| 6772 | int ret; |
| 6773 | |
| 6774 | mutex_lock(&kvm->lock); |
| 6775 | |
| 6776 | if (!sev_guest(kvm)) { |
| 6777 | ret = -ENOTTY; |
| 6778 | goto failed; |
| 6779 | } |
| 6780 | |
| 6781 | region = find_enc_region(kvm, range); |
| 6782 | if (!region) { |
| 6783 | ret = -EINVAL; |
| 6784 | goto failed; |
| 6785 | } |
| 6786 | |
| 6787 | __unregister_enc_region_locked(kvm, region); |
| 6788 | |
| 6789 | mutex_unlock(&kvm->lock); |
| 6790 | return 0; |
| 6791 | |
| 6792 | failed: |
| 6793 | mutex_unlock(&kvm->lock); |
| 6794 | return ret; |
| 6795 | } |
| 6796 | |
Kees Cook | 404f6aa | 2016-08-08 16:29:06 -0700 | [diff] [blame] | 6797 | static struct kvm_x86_ops svm_x86_ops __ro_after_init = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6798 | .cpu_has_kvm_support = has_svm, |
| 6799 | .disabled_by_bios = is_disabled, |
| 6800 | .hardware_setup = svm_hardware_setup, |
| 6801 | .hardware_unsetup = svm_hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 6802 | .check_processor_compatibility = svm_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6803 | .hardware_enable = svm_hardware_enable, |
| 6804 | .hardware_disable = svm_hardware_disable, |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 6805 | .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr, |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 6806 | .cpu_has_high_real_mode_segbase = svm_has_high_real_mode_segbase, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6807 | |
| 6808 | .vcpu_create = svm_create_vcpu, |
| 6809 | .vcpu_free = svm_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 6810 | .vcpu_reset = svm_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6811 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 6812 | .vm_init = avic_vm_init, |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 6813 | .vm_destroy = svm_vm_destroy, |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 6814 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 6815 | .prepare_guest_switch = svm_prepare_guest_switch, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6816 | .vcpu_load = svm_vcpu_load, |
| 6817 | .vcpu_put = svm_vcpu_put, |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 6818 | .vcpu_blocking = svm_vcpu_blocking, |
| 6819 | .vcpu_unblocking = svm_vcpu_unblocking, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6820 | |
Paolo Bonzini | a96036b | 2015-11-10 11:55:36 +0100 | [diff] [blame] | 6821 | .update_bp_intercept = update_bp_intercept, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6822 | .get_msr = svm_get_msr, |
| 6823 | .set_msr = svm_set_msr, |
| 6824 | .get_segment_base = svm_get_segment_base, |
| 6825 | .get_segment = svm_get_segment, |
| 6826 | .set_segment = svm_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 6827 | .get_cpl = svm_get_cpl, |
Rusty Russell | 1747fb7 | 2007-09-06 01:21:32 +1000 | [diff] [blame] | 6828 | .get_cs_db_l_bits = kvm_get_cs_db_l_bits, |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 6829 | .decache_cr0_guest_bits = svm_decache_cr0_guest_bits, |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 6830 | .decache_cr3 = svm_decache_cr3, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 6831 | .decache_cr4_guest_bits = svm_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6832 | .set_cr0 = svm_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6833 | .set_cr3 = svm_set_cr3, |
| 6834 | .set_cr4 = svm_set_cr4, |
| 6835 | .set_efer = svm_set_efer, |
| 6836 | .get_idt = svm_get_idt, |
| 6837 | .set_idt = svm_set_idt, |
| 6838 | .get_gdt = svm_get_gdt, |
| 6839 | .set_gdt = svm_set_gdt, |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 6840 | .get_dr6 = svm_get_dr6, |
| 6841 | .set_dr6 = svm_set_dr6, |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 6842 | .set_dr7 = svm_set_dr7, |
Paolo Bonzini | facb013 | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 6843 | .sync_dirty_debug_regs = svm_sync_dirty_debug_regs, |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 6844 | .cache_reg = svm_cache_reg, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6845 | .get_rflags = svm_get_rflags, |
| 6846 | .set_rflags = svm_set_rflags, |
Huaitong Han | be94f6b | 2016-03-22 16:51:20 +0800 | [diff] [blame] | 6847 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6848 | .tlb_flush = svm_flush_tlb, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6849 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6850 | .run = svm_vcpu_run, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 6851 | .handle_exit = handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6852 | .skip_emulated_instruction = skip_emulated_instruction, |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 6853 | .set_interrupt_shadow = svm_set_interrupt_shadow, |
| 6854 | .get_interrupt_shadow = svm_get_interrupt_shadow, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 6855 | .patch_hypercall = svm_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 6856 | .set_irq = svm_set_irq, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 6857 | .set_nmi = svm_inject_nmi, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 6858 | .queue_exception = svm_queue_exception, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 6859 | .cancel_injection = svm_cancel_injection, |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 6860 | .interrupt_allowed = svm_interrupt_allowed, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 6861 | .nmi_allowed = svm_nmi_allowed, |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 6862 | .get_nmi_mask = svm_get_nmi_mask, |
| 6863 | .set_nmi_mask = svm_set_nmi_mask, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 6864 | .enable_nmi_window = enable_nmi_window, |
| 6865 | .enable_irq_window = enable_irq_window, |
| 6866 | .update_cr8_intercept = update_cr8_intercept, |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 6867 | .set_virtual_x2apic_mode = svm_set_virtual_x2apic_mode, |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 6868 | .get_enable_apicv = svm_get_enable_apicv, |
| 6869 | .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6870 | .load_eoi_exitmap = svm_load_eoi_exitmap, |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 6871 | .hwapic_irr_update = svm_hwapic_irr_update, |
| 6872 | .hwapic_isr_update = svm_hwapic_isr_update, |
Liran Alon | fa59cc0 | 2017-12-24 18:12:53 +0200 | [diff] [blame] | 6873 | .sync_pir_to_irr = kvm_lapic_find_highest_irr, |
Suravee Suthikulpanit | be8ca17 | 2016-05-04 14:09:49 -0500 | [diff] [blame] | 6874 | .apicv_post_state_restore = avic_post_state_restore, |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6875 | |
| 6876 | .set_tss_addr = svm_set_tss_addr, |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 6877 | .get_tdp_level = get_npt_level, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 6878 | .get_mt_mask = svm_get_mt_mask, |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 6879 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 6880 | .get_exit_info = svm_get_exit_info, |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 6881 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 6882 | .get_lpage_level = svm_get_lpage_level, |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 6883 | |
| 6884 | .cpuid_update = svm_cpuid_update, |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 6885 | |
| 6886 | .rdtscp_supported = svm_rdtscp_supported, |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 6887 | .invpcid_supported = svm_invpcid_supported, |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 6888 | .mpx_supported = svm_mpx_supported, |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 6889 | .xsaves_supported = svm_xsaves_supported, |
Paolo Bonzini | 66336ca | 2016-07-12 10:36:41 +0200 | [diff] [blame] | 6890 | .umip_emulated = svm_umip_emulated, |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 6891 | |
| 6892 | .set_supported_cpuid = svm_set_supported_cpuid, |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 6893 | |
| 6894 | .has_wbinvd_exit = svm_has_wbinvd_exit, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 6895 | |
| 6896 | .write_tsc_offset = svm_write_tsc_offset, |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 6897 | |
| 6898 | .set_tdp_cr3 = set_tdp_cr3, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 6899 | |
| 6900 | .check_intercept = svm_check_intercept, |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 6901 | .handle_external_intr = svm_handle_external_intr, |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 6902 | |
| 6903 | .sched_in = svm_sched_in, |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 6904 | |
| 6905 | .pmu_ops = &amd_pmu_ops, |
Suravee Suthikulpanit | 340d3bc | 2016-05-04 14:09:47 -0500 | [diff] [blame] | 6906 | .deliver_posted_interrupt = svm_deliver_avic_intr, |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 6907 | .update_pi_irte = svm_update_pi_irte, |
Borislav Petkov | 74f1690 | 2017-03-26 23:51:24 +0200 | [diff] [blame] | 6908 | .setup_mce = svm_setup_mce, |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 6909 | |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 6910 | .smi_allowed = svm_smi_allowed, |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 6911 | .pre_enter_smm = svm_pre_enter_smm, |
| 6912 | .pre_leave_smm = svm_pre_leave_smm, |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 6913 | .enable_smi_window = enable_smi_window, |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 6914 | |
| 6915 | .mem_enc_op = svm_mem_enc_op, |
Brijesh Singh | 1e80fdc | 2017-12-04 10:57:38 -0600 | [diff] [blame] | 6916 | .mem_enc_reg_region = svm_register_enc_region, |
| 6917 | .mem_enc_unreg_region = svm_unregister_enc_region, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6918 | }; |
| 6919 | |
| 6920 | static int __init svm_init(void) |
| 6921 | { |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 6922 | return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm), |
Avi Kivity | 0ee75be | 2010-04-28 15:39:01 +0300 | [diff] [blame] | 6923 | __alignof__(struct vcpu_svm), THIS_MODULE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6924 | } |
| 6925 | |
| 6926 | static void __exit svm_exit(void) |
| 6927 | { |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 6928 | kvm_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6929 | } |
| 6930 | |
| 6931 | module_init(svm_init) |
| 6932 | module_exit(svm_exit) |