rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Kernel-based Virtual Machine driver for Linux |
| 3 | * |
| 4 | * AMD SVM support |
| 5 | * |
| 6 | * Copyright (C) 2006 Qumranet, Inc. |
| 7 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
| 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 | */ |
| 17 | |
| 18 | #define pr_fmt(fmt) "SVM: " fmt |
| 19 | |
| 20 | #include <linux/kvm_host.h> |
| 21 | |
| 22 | #include "irq.h" |
| 23 | #include "mmu.h" |
| 24 | #include "kvm_cache_regs.h" |
| 25 | #include "x86.h" |
| 26 | #include "cpuid.h" |
| 27 | #include "pmu.h" |
| 28 | |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/mod_devicetable.h> |
| 31 | #include <linux/kernel.h> |
| 32 | #include <linux/vmalloc.h> |
| 33 | #include <linux/highmem.h> |
| 34 | #include <linux/sched.h> |
| 35 | #include <linux/trace_events.h> |
| 36 | #include <linux/slab.h> |
| 37 | #include <linux/amd-iommu.h> |
| 38 | #include <linux/hashtable.h> |
| 39 | #include <linux/frame.h> |
| 40 | |
| 41 | #include <asm/apic.h> |
| 42 | #include <asm/perf_event.h> |
| 43 | #include <asm/tlbflush.h> |
| 44 | #include <asm/desc.h> |
| 45 | #include <asm/debugreg.h> |
| 46 | #include <asm/kvm_para.h> |
| 47 | #include <asm/irq_remapping.h> |
| 48 | #include <asm/microcode.h> |
| 49 | #include <asm/spec-ctrl.h> |
| 50 | |
| 51 | #include <asm/virtext.h> |
| 52 | #include "trace.h" |
| 53 | |
| 54 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
| 55 | |
| 56 | MODULE_AUTHOR("Qumranet"); |
| 57 | MODULE_LICENSE("GPL"); |
| 58 | |
| 59 | static const struct x86_cpu_id svm_cpu_id[] = { |
| 60 | X86_FEATURE_MATCH(X86_FEATURE_SVM), |
| 61 | {} |
| 62 | }; |
| 63 | MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id); |
| 64 | |
| 65 | #define IOPM_ALLOC_ORDER 2 |
| 66 | #define MSRPM_ALLOC_ORDER 1 |
| 67 | |
| 68 | #define SEG_TYPE_LDT 2 |
| 69 | #define SEG_TYPE_BUSY_TSS16 3 |
| 70 | |
| 71 | #define SVM_FEATURE_NPT (1 << 0) |
| 72 | #define SVM_FEATURE_LBRV (1 << 1) |
| 73 | #define SVM_FEATURE_SVML (1 << 2) |
| 74 | #define SVM_FEATURE_NRIP (1 << 3) |
| 75 | #define SVM_FEATURE_TSC_RATE (1 << 4) |
| 76 | #define SVM_FEATURE_VMCB_CLEAN (1 << 5) |
| 77 | #define SVM_FEATURE_FLUSH_ASID (1 << 6) |
| 78 | #define SVM_FEATURE_DECODE_ASSIST (1 << 7) |
| 79 | #define SVM_FEATURE_PAUSE_FILTER (1 << 10) |
| 80 | |
| 81 | #define SVM_AVIC_DOORBELL 0xc001011b |
| 82 | |
| 83 | #define NESTED_EXIT_HOST 0 /* Exit handled on host level */ |
| 84 | #define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */ |
| 85 | #define NESTED_EXIT_CONTINUE 2 /* Further checks needed */ |
| 86 | |
| 87 | #define DEBUGCTL_RESERVED_BITS (~(0x3fULL)) |
| 88 | |
| 89 | #define TSC_RATIO_RSVD 0xffffff0000000000ULL |
| 90 | #define TSC_RATIO_MIN 0x0000000000000001ULL |
| 91 | #define TSC_RATIO_MAX 0x000000ffffffffffULL |
| 92 | |
| 93 | #define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF) |
| 94 | |
| 95 | /* |
| 96 | * 0xff is broadcast, so the max index allowed for physical APIC ID |
| 97 | * table is 0xfe. APIC IDs above 0xff are reserved. |
| 98 | */ |
| 99 | #define AVIC_MAX_PHYSICAL_ID_COUNT 255 |
| 100 | |
| 101 | #define AVIC_UNACCEL_ACCESS_WRITE_MASK 1 |
| 102 | #define AVIC_UNACCEL_ACCESS_OFFSET_MASK 0xFF0 |
| 103 | #define AVIC_UNACCEL_ACCESS_VECTOR_MASK 0xFFFFFFFF |
| 104 | |
| 105 | /* AVIC GATAG is encoded using VM and VCPU IDs */ |
| 106 | #define AVIC_VCPU_ID_BITS 8 |
| 107 | #define AVIC_VCPU_ID_MASK ((1 << AVIC_VCPU_ID_BITS) - 1) |
| 108 | |
| 109 | #define AVIC_VM_ID_BITS 24 |
| 110 | #define AVIC_VM_ID_NR (1 << AVIC_VM_ID_BITS) |
| 111 | #define AVIC_VM_ID_MASK ((1 << AVIC_VM_ID_BITS) - 1) |
| 112 | |
| 113 | #define AVIC_GATAG(x, y) (((x & AVIC_VM_ID_MASK) << AVIC_VCPU_ID_BITS) | \ |
| 114 | (y & AVIC_VCPU_ID_MASK)) |
| 115 | #define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_VCPU_ID_BITS) & AVIC_VM_ID_MASK) |
| 116 | #define AVIC_GATAG_TO_VCPUID(x) (x & AVIC_VCPU_ID_MASK) |
| 117 | |
| 118 | static bool erratum_383_found __read_mostly; |
| 119 | |
| 120 | static const u32 host_save_user_msrs[] = { |
| 121 | #ifdef CONFIG_X86_64 |
| 122 | MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE, |
| 123 | MSR_FS_BASE, |
| 124 | #endif |
| 125 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, |
| 126 | MSR_TSC_AUX, |
| 127 | }; |
| 128 | |
| 129 | #define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs) |
| 130 | |
| 131 | struct kvm_vcpu; |
| 132 | |
| 133 | struct nested_state { |
| 134 | struct vmcb *hsave; |
| 135 | u64 hsave_msr; |
| 136 | u64 vm_cr_msr; |
| 137 | u64 vmcb; |
| 138 | |
| 139 | /* These are the merged vectors */ |
| 140 | u32 *msrpm; |
| 141 | |
| 142 | /* gpa pointers to the real vectors */ |
| 143 | u64 vmcb_msrpm; |
| 144 | u64 vmcb_iopm; |
| 145 | |
| 146 | /* A VMEXIT is required but not yet emulated */ |
| 147 | bool exit_required; |
| 148 | |
| 149 | /* cache for intercepts of the guest */ |
| 150 | u32 intercept_cr; |
| 151 | u32 intercept_dr; |
| 152 | u32 intercept_exceptions; |
| 153 | u64 intercept; |
| 154 | |
| 155 | /* Nested Paging related state */ |
| 156 | u64 nested_cr3; |
| 157 | }; |
| 158 | |
| 159 | #define MSRPM_OFFSETS 16 |
| 160 | static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly; |
| 161 | |
| 162 | /* |
| 163 | * Set osvw_len to higher value when updated Revision Guides |
| 164 | * are published and we know what the new status bits are |
| 165 | */ |
| 166 | static uint64_t osvw_len = 4, osvw_status; |
| 167 | |
| 168 | struct vcpu_svm { |
| 169 | struct kvm_vcpu vcpu; |
| 170 | struct vmcb *vmcb; |
| 171 | unsigned long vmcb_pa; |
| 172 | struct svm_cpu_data *svm_data; |
| 173 | uint64_t asid_generation; |
| 174 | uint64_t sysenter_esp; |
| 175 | uint64_t sysenter_eip; |
| 176 | uint64_t tsc_aux; |
| 177 | |
| 178 | u64 msr_decfg; |
| 179 | |
| 180 | u64 next_rip; |
| 181 | |
| 182 | u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS]; |
| 183 | struct { |
| 184 | u16 fs; |
| 185 | u16 gs; |
| 186 | u16 ldt; |
| 187 | u64 gs_base; |
| 188 | } host; |
| 189 | |
| 190 | u64 spec_ctrl; |
| 191 | /* |
| 192 | * Contains guest-controlled bits of VIRT_SPEC_CTRL, which will be |
| 193 | * translated into the appropriate L2_CFG bits on the host to |
| 194 | * perform speculative control. |
| 195 | */ |
| 196 | u64 virt_spec_ctrl; |
| 197 | |
| 198 | u32 *msrpm; |
| 199 | |
| 200 | ulong nmi_iret_rip; |
| 201 | |
| 202 | struct nested_state nested; |
| 203 | |
| 204 | bool nmi_singlestep; |
| 205 | u64 nmi_singlestep_guest_rflags; |
| 206 | |
| 207 | unsigned int3_injected; |
| 208 | unsigned long int3_rip; |
| 209 | |
| 210 | /* cached guest cpuid flags for faster access */ |
| 211 | bool nrips_enabled : 1; |
| 212 | |
| 213 | u32 ldr_reg; |
| 214 | struct page *avic_backing_page; |
| 215 | u64 *avic_physical_id_cache; |
| 216 | bool avic_is_running; |
| 217 | |
| 218 | /* |
| 219 | * Per-vcpu list of struct amd_svm_iommu_ir: |
| 220 | * This is used mainly to store interrupt remapping information used |
| 221 | * when update the vcpu affinity. This avoids the need to scan for |
| 222 | * IRTE and try to match ga_tag in the IOMMU driver. |
| 223 | */ |
| 224 | struct list_head ir_list; |
| 225 | spinlock_t ir_list_lock; |
| 226 | }; |
| 227 | |
| 228 | /* |
| 229 | * This is a wrapper of struct amd_iommu_ir_data. |
| 230 | */ |
| 231 | struct amd_svm_iommu_ir { |
| 232 | struct list_head node; /* Used by SVM for per-vcpu ir_list */ |
| 233 | void *data; /* Storing pointer to struct amd_ir_data */ |
| 234 | }; |
| 235 | |
| 236 | #define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFF) |
| 237 | #define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31) |
| 238 | |
| 239 | #define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK (0xFFULL) |
| 240 | #define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK (0xFFFFFFFFFFULL << 12) |
| 241 | #define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK (1ULL << 62) |
| 242 | #define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK (1ULL << 63) |
| 243 | |
| 244 | static DEFINE_PER_CPU(u64, current_tsc_ratio); |
| 245 | #define TSC_RATIO_DEFAULT 0x0100000000ULL |
| 246 | |
| 247 | #define MSR_INVALID 0xffffffffU |
| 248 | |
| 249 | static const struct svm_direct_access_msrs { |
| 250 | u32 index; /* Index of the MSR */ |
| 251 | bool always; /* True if intercept is always on */ |
| 252 | } direct_access_msrs[] = { |
| 253 | { .index = MSR_STAR, .always = true }, |
| 254 | { .index = MSR_IA32_SYSENTER_CS, .always = true }, |
| 255 | #ifdef CONFIG_X86_64 |
| 256 | { .index = MSR_GS_BASE, .always = true }, |
| 257 | { .index = MSR_FS_BASE, .always = true }, |
| 258 | { .index = MSR_KERNEL_GS_BASE, .always = true }, |
| 259 | { .index = MSR_LSTAR, .always = true }, |
| 260 | { .index = MSR_CSTAR, .always = true }, |
| 261 | { .index = MSR_SYSCALL_MASK, .always = true }, |
| 262 | #endif |
| 263 | { .index = MSR_IA32_SPEC_CTRL, .always = false }, |
| 264 | { .index = MSR_IA32_PRED_CMD, .always = false }, |
| 265 | { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false }, |
| 266 | { .index = MSR_IA32_LASTBRANCHTOIP, .always = false }, |
| 267 | { .index = MSR_IA32_LASTINTFROMIP, .always = false }, |
| 268 | { .index = MSR_IA32_LASTINTTOIP, .always = false }, |
| 269 | { .index = MSR_INVALID, .always = false }, |
| 270 | }; |
| 271 | |
| 272 | /* enable NPT for AMD64 and X86 with PAE */ |
| 273 | #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE) |
| 274 | static bool npt_enabled = true; |
| 275 | #else |
| 276 | static bool npt_enabled; |
| 277 | #endif |
| 278 | |
| 279 | /* allow nested paging (virtualized MMU) for all guests */ |
| 280 | static int npt = true; |
| 281 | module_param(npt, int, S_IRUGO); |
| 282 | |
| 283 | /* allow nested virtualization in KVM/SVM */ |
| 284 | static int nested = true; |
| 285 | module_param(nested, int, S_IRUGO); |
| 286 | |
| 287 | /* enable / disable AVIC */ |
| 288 | static int avic; |
| 289 | #ifdef CONFIG_X86_LOCAL_APIC |
| 290 | module_param(avic, int, S_IRUGO); |
| 291 | #endif |
| 292 | |
| 293 | /* enable/disable Virtual VMLOAD VMSAVE */ |
| 294 | static int vls = true; |
| 295 | module_param(vls, int, 0444); |
| 296 | |
| 297 | /* enable/disable Virtual GIF */ |
| 298 | static int vgif = true; |
| 299 | module_param(vgif, int, 0444); |
| 300 | |
| 301 | static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0); |
| 302 | static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa); |
| 303 | static void svm_complete_interrupts(struct vcpu_svm *svm); |
| 304 | |
| 305 | static int nested_svm_exit_handled(struct vcpu_svm *svm); |
| 306 | static int nested_svm_intercept(struct vcpu_svm *svm); |
| 307 | static int nested_svm_vmexit(struct vcpu_svm *svm); |
| 308 | static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr, |
| 309 | bool has_error_code, u32 error_code); |
| 310 | |
| 311 | enum { |
| 312 | VMCB_INTERCEPTS, /* Intercept vectors, TSC offset, |
| 313 | pause filter count */ |
| 314 | VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */ |
| 315 | VMCB_ASID, /* ASID */ |
| 316 | VMCB_INTR, /* int_ctl, int_vector */ |
| 317 | VMCB_NPT, /* npt_en, nCR3, gPAT */ |
| 318 | VMCB_CR, /* CR0, CR3, CR4, EFER */ |
| 319 | VMCB_DR, /* DR6, DR7 */ |
| 320 | VMCB_DT, /* GDT, IDT */ |
| 321 | VMCB_SEG, /* CS, DS, SS, ES, CPL */ |
| 322 | VMCB_CR2, /* CR2 only */ |
| 323 | VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */ |
| 324 | VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE, |
| 325 | * AVIC PHYSICAL_TABLE pointer, |
| 326 | * AVIC LOGICAL_TABLE pointer |
| 327 | */ |
| 328 | VMCB_DIRTY_MAX, |
| 329 | }; |
| 330 | |
| 331 | /* TPR and CR2 are always written before VMRUN */ |
| 332 | #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2)) |
| 333 | |
| 334 | #define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL |
| 335 | |
| 336 | static inline void mark_all_dirty(struct vmcb *vmcb) |
| 337 | { |
| 338 | vmcb->control.clean = 0; |
| 339 | } |
| 340 | |
| 341 | static inline void mark_all_clean(struct vmcb *vmcb) |
| 342 | { |
| 343 | vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1) |
| 344 | & ~VMCB_ALWAYS_DIRTY_MASK; |
| 345 | } |
| 346 | |
| 347 | static inline void mark_dirty(struct vmcb *vmcb, int bit) |
| 348 | { |
| 349 | vmcb->control.clean &= ~(1 << bit); |
| 350 | } |
| 351 | |
| 352 | static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu) |
| 353 | { |
| 354 | return container_of(vcpu, struct vcpu_svm, vcpu); |
| 355 | } |
| 356 | |
| 357 | static inline void avic_update_vapic_bar(struct vcpu_svm *svm, u64 data) |
| 358 | { |
| 359 | svm->vmcb->control.avic_vapic_bar = data & VMCB_AVIC_APIC_BAR_MASK; |
| 360 | mark_dirty(svm->vmcb, VMCB_AVIC); |
| 361 | } |
| 362 | |
| 363 | static inline bool avic_vcpu_is_running(struct kvm_vcpu *vcpu) |
| 364 | { |
| 365 | struct vcpu_svm *svm = to_svm(vcpu); |
| 366 | u64 *entry = svm->avic_physical_id_cache; |
| 367 | |
| 368 | if (!entry) |
| 369 | return false; |
| 370 | |
| 371 | return (READ_ONCE(*entry) & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK); |
| 372 | } |
| 373 | |
| 374 | static void recalc_intercepts(struct vcpu_svm *svm) |
| 375 | { |
| 376 | struct vmcb_control_area *c, *h; |
| 377 | struct nested_state *g; |
| 378 | |
| 379 | mark_dirty(svm->vmcb, VMCB_INTERCEPTS); |
| 380 | |
| 381 | if (!is_guest_mode(&svm->vcpu)) |
| 382 | return; |
| 383 | |
| 384 | c = &svm->vmcb->control; |
| 385 | h = &svm->nested.hsave->control; |
| 386 | g = &svm->nested; |
| 387 | |
| 388 | c->intercept_cr = h->intercept_cr | g->intercept_cr; |
| 389 | c->intercept_dr = h->intercept_dr | g->intercept_dr; |
| 390 | c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions; |
| 391 | c->intercept = h->intercept | g->intercept; |
| 392 | } |
| 393 | |
| 394 | static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm) |
| 395 | { |
| 396 | if (is_guest_mode(&svm->vcpu)) |
| 397 | return svm->nested.hsave; |
| 398 | else |
| 399 | return svm->vmcb; |
| 400 | } |
| 401 | |
| 402 | static inline void set_cr_intercept(struct vcpu_svm *svm, int bit) |
| 403 | { |
| 404 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 405 | |
| 406 | vmcb->control.intercept_cr |= (1U << bit); |
| 407 | |
| 408 | recalc_intercepts(svm); |
| 409 | } |
| 410 | |
| 411 | static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit) |
| 412 | { |
| 413 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 414 | |
| 415 | vmcb->control.intercept_cr &= ~(1U << bit); |
| 416 | |
| 417 | recalc_intercepts(svm); |
| 418 | } |
| 419 | |
| 420 | static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit) |
| 421 | { |
| 422 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 423 | |
| 424 | return vmcb->control.intercept_cr & (1U << bit); |
| 425 | } |
| 426 | |
| 427 | static inline void set_dr_intercepts(struct vcpu_svm *svm) |
| 428 | { |
| 429 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 430 | |
| 431 | vmcb->control.intercept_dr = (1 << INTERCEPT_DR0_READ) |
| 432 | | (1 << INTERCEPT_DR1_READ) |
| 433 | | (1 << INTERCEPT_DR2_READ) |
| 434 | | (1 << INTERCEPT_DR3_READ) |
| 435 | | (1 << INTERCEPT_DR4_READ) |
| 436 | | (1 << INTERCEPT_DR5_READ) |
| 437 | | (1 << INTERCEPT_DR6_READ) |
| 438 | | (1 << INTERCEPT_DR7_READ) |
| 439 | | (1 << INTERCEPT_DR0_WRITE) |
| 440 | | (1 << INTERCEPT_DR1_WRITE) |
| 441 | | (1 << INTERCEPT_DR2_WRITE) |
| 442 | | (1 << INTERCEPT_DR3_WRITE) |
| 443 | | (1 << INTERCEPT_DR4_WRITE) |
| 444 | | (1 << INTERCEPT_DR5_WRITE) |
| 445 | | (1 << INTERCEPT_DR6_WRITE) |
| 446 | | (1 << INTERCEPT_DR7_WRITE); |
| 447 | |
| 448 | recalc_intercepts(svm); |
| 449 | } |
| 450 | |
| 451 | static inline void clr_dr_intercepts(struct vcpu_svm *svm) |
| 452 | { |
| 453 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 454 | |
| 455 | vmcb->control.intercept_dr = 0; |
| 456 | |
| 457 | recalc_intercepts(svm); |
| 458 | } |
| 459 | |
| 460 | static inline void set_exception_intercept(struct vcpu_svm *svm, int bit) |
| 461 | { |
| 462 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 463 | |
| 464 | vmcb->control.intercept_exceptions |= (1U << bit); |
| 465 | |
| 466 | recalc_intercepts(svm); |
| 467 | } |
| 468 | |
| 469 | static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit) |
| 470 | { |
| 471 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 472 | |
| 473 | vmcb->control.intercept_exceptions &= ~(1U << bit); |
| 474 | |
| 475 | recalc_intercepts(svm); |
| 476 | } |
| 477 | |
| 478 | static inline void set_intercept(struct vcpu_svm *svm, int bit) |
| 479 | { |
| 480 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 481 | |
| 482 | vmcb->control.intercept |= (1ULL << bit); |
| 483 | |
| 484 | recalc_intercepts(svm); |
| 485 | } |
| 486 | |
| 487 | static inline void clr_intercept(struct vcpu_svm *svm, int bit) |
| 488 | { |
| 489 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 490 | |
| 491 | vmcb->control.intercept &= ~(1ULL << bit); |
| 492 | |
| 493 | recalc_intercepts(svm); |
| 494 | } |
| 495 | |
| 496 | static inline bool vgif_enabled(struct vcpu_svm *svm) |
| 497 | { |
| 498 | return !!(svm->vmcb->control.int_ctl & V_GIF_ENABLE_MASK); |
| 499 | } |
| 500 | |
| 501 | static inline void enable_gif(struct vcpu_svm *svm) |
| 502 | { |
| 503 | if (vgif_enabled(svm)) |
| 504 | svm->vmcb->control.int_ctl |= V_GIF_MASK; |
| 505 | else |
| 506 | svm->vcpu.arch.hflags |= HF_GIF_MASK; |
| 507 | } |
| 508 | |
| 509 | static inline void disable_gif(struct vcpu_svm *svm) |
| 510 | { |
| 511 | if (vgif_enabled(svm)) |
| 512 | svm->vmcb->control.int_ctl &= ~V_GIF_MASK; |
| 513 | else |
| 514 | svm->vcpu.arch.hflags &= ~HF_GIF_MASK; |
| 515 | } |
| 516 | |
| 517 | static inline bool gif_set(struct vcpu_svm *svm) |
| 518 | { |
| 519 | if (vgif_enabled(svm)) |
| 520 | return !!(svm->vmcb->control.int_ctl & V_GIF_MASK); |
| 521 | else |
| 522 | return !!(svm->vcpu.arch.hflags & HF_GIF_MASK); |
| 523 | } |
| 524 | |
| 525 | static unsigned long iopm_base; |
| 526 | |
| 527 | struct kvm_ldttss_desc { |
| 528 | u16 limit0; |
| 529 | u16 base0; |
| 530 | unsigned base1:8, type:5, dpl:2, p:1; |
| 531 | unsigned limit1:4, zero0:3, g:1, base2:8; |
| 532 | u32 base3; |
| 533 | u32 zero1; |
| 534 | } __attribute__((packed)); |
| 535 | |
| 536 | struct svm_cpu_data { |
| 537 | int cpu; |
| 538 | |
| 539 | u64 asid_generation; |
| 540 | u32 max_asid; |
| 541 | u32 next_asid; |
| 542 | struct kvm_ldttss_desc *tss_desc; |
| 543 | |
| 544 | struct page *save_area; |
| 545 | struct vmcb *current_vmcb; |
| 546 | }; |
| 547 | |
| 548 | static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data); |
| 549 | |
| 550 | struct svm_init_data { |
| 551 | int cpu; |
| 552 | int r; |
| 553 | }; |
| 554 | |
| 555 | static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000}; |
| 556 | |
| 557 | #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges) |
| 558 | #define MSRS_RANGE_SIZE 2048 |
| 559 | #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2) |
| 560 | |
| 561 | static u32 svm_msrpm_offset(u32 msr) |
| 562 | { |
| 563 | u32 offset; |
| 564 | int i; |
| 565 | |
| 566 | for (i = 0; i < NUM_MSR_MAPS; i++) { |
| 567 | if (msr < msrpm_ranges[i] || |
| 568 | msr >= msrpm_ranges[i] + MSRS_IN_RANGE) |
| 569 | continue; |
| 570 | |
| 571 | offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */ |
| 572 | offset += (i * MSRS_RANGE_SIZE); /* add range offset */ |
| 573 | |
| 574 | /* Now we have the u8 offset - but need the u32 offset */ |
| 575 | return offset / 4; |
| 576 | } |
| 577 | |
| 578 | /* MSR not in any range */ |
| 579 | return MSR_INVALID; |
| 580 | } |
| 581 | |
| 582 | #define MAX_INST_SIZE 15 |
| 583 | |
| 584 | static inline void clgi(void) |
| 585 | { |
| 586 | asm volatile (__ex(SVM_CLGI)); |
| 587 | } |
| 588 | |
| 589 | static inline void stgi(void) |
| 590 | { |
| 591 | asm volatile (__ex(SVM_STGI)); |
| 592 | } |
| 593 | |
| 594 | static inline void invlpga(unsigned long addr, u32 asid) |
| 595 | { |
| 596 | asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid)); |
| 597 | } |
| 598 | |
| 599 | static int get_npt_level(struct kvm_vcpu *vcpu) |
| 600 | { |
| 601 | #ifdef CONFIG_X86_64 |
| 602 | return PT64_ROOT_4LEVEL; |
| 603 | #else |
| 604 | return PT32E_ROOT_LEVEL; |
| 605 | #endif |
| 606 | } |
| 607 | |
| 608 | static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 609 | { |
| 610 | vcpu->arch.efer = efer; |
| 611 | |
| 612 | if (!npt_enabled) { |
| 613 | /* Shadow paging assumes NX to be available. */ |
| 614 | efer |= EFER_NX; |
| 615 | |
| 616 | if (!(efer & EFER_LMA)) |
| 617 | efer &= ~EFER_LME; |
| 618 | } |
| 619 | |
| 620 | to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME; |
| 621 | mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR); |
| 622 | } |
| 623 | |
| 624 | static int is_external_interrupt(u32 info) |
| 625 | { |
| 626 | info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID; |
| 627 | return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR); |
| 628 | } |
| 629 | |
| 630 | static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu) |
| 631 | { |
| 632 | struct vcpu_svm *svm = to_svm(vcpu); |
| 633 | u32 ret = 0; |
| 634 | |
| 635 | if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) |
| 636 | ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS; |
| 637 | return ret; |
| 638 | } |
| 639 | |
| 640 | static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 641 | { |
| 642 | struct vcpu_svm *svm = to_svm(vcpu); |
| 643 | |
| 644 | if (mask == 0) |
| 645 | svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK; |
| 646 | else |
| 647 | svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK; |
| 648 | |
| 649 | } |
| 650 | |
| 651 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 652 | { |
| 653 | struct vcpu_svm *svm = to_svm(vcpu); |
| 654 | |
| 655 | if (svm->vmcb->control.next_rip != 0) { |
| 656 | WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS)); |
| 657 | svm->next_rip = svm->vmcb->control.next_rip; |
| 658 | } |
| 659 | |
| 660 | if (!svm->next_rip) { |
| 661 | if (emulate_instruction(vcpu, EMULTYPE_SKIP) != |
| 662 | EMULATE_DONE) |
| 663 | printk(KERN_DEBUG "%s: NOP\n", __func__); |
| 664 | return; |
| 665 | } |
| 666 | if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE) |
| 667 | printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n", |
| 668 | __func__, kvm_rip_read(vcpu), svm->next_rip); |
| 669 | |
| 670 | kvm_rip_write(vcpu, svm->next_rip); |
| 671 | svm_set_interrupt_shadow(vcpu, 0); |
| 672 | } |
| 673 | |
| 674 | static void svm_queue_exception(struct kvm_vcpu *vcpu) |
| 675 | { |
| 676 | struct vcpu_svm *svm = to_svm(vcpu); |
| 677 | unsigned nr = vcpu->arch.exception.nr; |
| 678 | bool has_error_code = vcpu->arch.exception.has_error_code; |
| 679 | bool reinject = vcpu->arch.exception.injected; |
| 680 | u32 error_code = vcpu->arch.exception.error_code; |
| 681 | |
| 682 | /* |
| 683 | * If we are within a nested VM we'd better #VMEXIT and let the guest |
| 684 | * handle the exception |
| 685 | */ |
| 686 | if (!reinject && |
| 687 | nested_svm_check_exception(svm, nr, has_error_code, error_code)) |
| 688 | return; |
| 689 | |
| 690 | if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) { |
| 691 | unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu); |
| 692 | |
| 693 | /* |
| 694 | * For guest debugging where we have to reinject #BP if some |
| 695 | * INT3 is guest-owned: |
| 696 | * Emulate nRIP by moving RIP forward. Will fail if injection |
| 697 | * raises a fault that is not intercepted. Still better than |
| 698 | * failing in all cases. |
| 699 | */ |
| 700 | skip_emulated_instruction(&svm->vcpu); |
| 701 | rip = kvm_rip_read(&svm->vcpu); |
| 702 | svm->int3_rip = rip + svm->vmcb->save.cs.base; |
| 703 | svm->int3_injected = rip - old_rip; |
| 704 | } |
| 705 | |
| 706 | svm->vmcb->control.event_inj = nr |
| 707 | | SVM_EVTINJ_VALID |
| 708 | | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0) |
| 709 | | SVM_EVTINJ_TYPE_EXEPT; |
| 710 | svm->vmcb->control.event_inj_err = error_code; |
| 711 | } |
| 712 | |
| 713 | static void svm_init_erratum_383(void) |
| 714 | { |
| 715 | u32 low, high; |
| 716 | int err; |
| 717 | u64 val; |
| 718 | |
| 719 | if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH)) |
| 720 | return; |
| 721 | |
| 722 | /* Use _safe variants to not break nested virtualization */ |
| 723 | val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err); |
| 724 | if (err) |
| 725 | return; |
| 726 | |
| 727 | val |= (1ULL << 47); |
| 728 | |
| 729 | low = lower_32_bits(val); |
| 730 | high = upper_32_bits(val); |
| 731 | |
| 732 | native_write_msr_safe(MSR_AMD64_DC_CFG, low, high); |
| 733 | |
| 734 | erratum_383_found = true; |
| 735 | } |
| 736 | |
| 737 | static void svm_init_osvw(struct kvm_vcpu *vcpu) |
| 738 | { |
| 739 | /* |
| 740 | * Guests should see errata 400 and 415 as fixed (assuming that |
| 741 | * HLT and IO instructions are intercepted). |
| 742 | */ |
| 743 | vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3; |
| 744 | vcpu->arch.osvw.status = osvw_status & ~(6ULL); |
| 745 | |
| 746 | /* |
| 747 | * By increasing VCPU's osvw.length to 3 we are telling the guest that |
| 748 | * all osvw.status bits inside that length, including bit 0 (which is |
| 749 | * reserved for erratum 298), are valid. However, if host processor's |
| 750 | * osvw_len is 0 then osvw_status[0] carries no information. We need to |
| 751 | * be conservative here and therefore we tell the guest that erratum 298 |
| 752 | * is present (because we really don't know). |
| 753 | */ |
| 754 | if (osvw_len == 0 && boot_cpu_data.x86 == 0x10) |
| 755 | vcpu->arch.osvw.status |= 1; |
| 756 | } |
| 757 | |
| 758 | static int has_svm(void) |
| 759 | { |
| 760 | const char *msg; |
| 761 | |
| 762 | if (!cpu_has_svm(&msg)) { |
| 763 | printk(KERN_INFO "has_svm: %s\n", msg); |
| 764 | return 0; |
| 765 | } |
| 766 | |
| 767 | return 1; |
| 768 | } |
| 769 | |
| 770 | static void svm_hardware_disable(void) |
| 771 | { |
| 772 | /* Make sure we clean up behind us */ |
| 773 | if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) |
| 774 | wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT); |
| 775 | |
| 776 | cpu_svm_disable(); |
| 777 | |
| 778 | amd_pmu_disable_virt(); |
| 779 | } |
| 780 | |
| 781 | static int svm_hardware_enable(void) |
| 782 | { |
| 783 | |
| 784 | struct svm_cpu_data *sd; |
| 785 | uint64_t efer; |
| 786 | struct desc_struct *gdt; |
| 787 | int me = raw_smp_processor_id(); |
| 788 | |
| 789 | rdmsrl(MSR_EFER, efer); |
| 790 | if (efer & EFER_SVME) |
| 791 | return -EBUSY; |
| 792 | |
| 793 | if (!has_svm()) { |
| 794 | pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me); |
| 795 | return -EINVAL; |
| 796 | } |
| 797 | sd = per_cpu(svm_data, me); |
| 798 | if (!sd) { |
| 799 | pr_err("%s: svm_data is NULL on %d\n", __func__, me); |
| 800 | return -EINVAL; |
| 801 | } |
| 802 | |
| 803 | sd->asid_generation = 1; |
| 804 | sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1; |
| 805 | sd->next_asid = sd->max_asid + 1; |
| 806 | |
| 807 | gdt = get_current_gdt_rw(); |
| 808 | sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS); |
| 809 | |
| 810 | wrmsrl(MSR_EFER, efer | EFER_SVME); |
| 811 | |
| 812 | wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT); |
| 813 | |
| 814 | if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) { |
| 815 | wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT); |
| 816 | __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT); |
| 817 | } |
| 818 | |
| 819 | |
| 820 | /* |
| 821 | * Get OSVW bits. |
| 822 | * |
| 823 | * Note that it is possible to have a system with mixed processor |
| 824 | * revisions and therefore different OSVW bits. If bits are not the same |
| 825 | * on different processors then choose the worst case (i.e. if erratum |
| 826 | * is present on one processor and not on another then assume that the |
| 827 | * erratum is present everywhere). |
| 828 | */ |
| 829 | if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) { |
| 830 | uint64_t len, status = 0; |
| 831 | int err; |
| 832 | |
| 833 | len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err); |
| 834 | if (!err) |
| 835 | status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS, |
| 836 | &err); |
| 837 | |
| 838 | if (err) |
| 839 | osvw_status = osvw_len = 0; |
| 840 | else { |
| 841 | if (len < osvw_len) |
| 842 | osvw_len = len; |
| 843 | osvw_status |= status; |
| 844 | osvw_status &= (1ULL << osvw_len) - 1; |
| 845 | } |
| 846 | } else |
| 847 | osvw_status = osvw_len = 0; |
| 848 | |
| 849 | svm_init_erratum_383(); |
| 850 | |
| 851 | amd_pmu_enable_virt(); |
| 852 | |
| 853 | return 0; |
| 854 | } |
| 855 | |
| 856 | static void svm_cpu_uninit(int cpu) |
| 857 | { |
| 858 | struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id()); |
| 859 | |
| 860 | if (!sd) |
| 861 | return; |
| 862 | |
| 863 | per_cpu(svm_data, raw_smp_processor_id()) = NULL; |
| 864 | __free_page(sd->save_area); |
| 865 | kfree(sd); |
| 866 | } |
| 867 | |
| 868 | static int svm_cpu_init(int cpu) |
| 869 | { |
| 870 | struct svm_cpu_data *sd; |
| 871 | int r; |
| 872 | |
| 873 | sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL); |
| 874 | if (!sd) |
| 875 | return -ENOMEM; |
| 876 | sd->cpu = cpu; |
| 877 | sd->save_area = alloc_page(GFP_KERNEL); |
| 878 | r = -ENOMEM; |
| 879 | if (!sd->save_area) |
| 880 | goto err_1; |
| 881 | |
| 882 | per_cpu(svm_data, cpu) = sd; |
| 883 | |
| 884 | return 0; |
| 885 | |
| 886 | err_1: |
| 887 | kfree(sd); |
| 888 | return r; |
| 889 | |
| 890 | } |
| 891 | |
| 892 | static bool valid_msr_intercept(u32 index) |
| 893 | { |
| 894 | int i; |
| 895 | |
| 896 | for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) |
| 897 | if (direct_access_msrs[i].index == index) |
| 898 | return true; |
| 899 | |
| 900 | return false; |
| 901 | } |
| 902 | |
| 903 | static bool msr_write_intercepted(struct kvm_vcpu *vcpu, unsigned msr) |
| 904 | { |
| 905 | u8 bit_write; |
| 906 | unsigned long tmp; |
| 907 | u32 offset; |
| 908 | u32 *msrpm; |
| 909 | |
| 910 | msrpm = is_guest_mode(vcpu) ? to_svm(vcpu)->nested.msrpm: |
| 911 | to_svm(vcpu)->msrpm; |
| 912 | |
| 913 | offset = svm_msrpm_offset(msr); |
| 914 | bit_write = 2 * (msr & 0x0f) + 1; |
| 915 | tmp = msrpm[offset]; |
| 916 | |
| 917 | BUG_ON(offset == MSR_INVALID); |
| 918 | |
| 919 | return !!test_bit(bit_write, &tmp); |
| 920 | } |
| 921 | |
| 922 | static void set_msr_interception(u32 *msrpm, unsigned msr, |
| 923 | int read, int write) |
| 924 | { |
| 925 | u8 bit_read, bit_write; |
| 926 | unsigned long tmp; |
| 927 | u32 offset; |
| 928 | |
| 929 | /* |
| 930 | * If this warning triggers extend the direct_access_msrs list at the |
| 931 | * beginning of the file |
| 932 | */ |
| 933 | WARN_ON(!valid_msr_intercept(msr)); |
| 934 | |
| 935 | offset = svm_msrpm_offset(msr); |
| 936 | bit_read = 2 * (msr & 0x0f); |
| 937 | bit_write = 2 * (msr & 0x0f) + 1; |
| 938 | tmp = msrpm[offset]; |
| 939 | |
| 940 | BUG_ON(offset == MSR_INVALID); |
| 941 | |
| 942 | read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp); |
| 943 | write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp); |
| 944 | |
| 945 | msrpm[offset] = tmp; |
| 946 | } |
| 947 | |
| 948 | static void svm_vcpu_init_msrpm(u32 *msrpm) |
| 949 | { |
| 950 | int i; |
| 951 | |
| 952 | memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER)); |
| 953 | |
| 954 | for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) { |
| 955 | if (!direct_access_msrs[i].always) |
| 956 | continue; |
| 957 | |
| 958 | set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1); |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | static void add_msr_offset(u32 offset) |
| 963 | { |
| 964 | int i; |
| 965 | |
| 966 | for (i = 0; i < MSRPM_OFFSETS; ++i) { |
| 967 | |
| 968 | /* Offset already in list? */ |
| 969 | if (msrpm_offsets[i] == offset) |
| 970 | return; |
| 971 | |
| 972 | /* Slot used by another offset? */ |
| 973 | if (msrpm_offsets[i] != MSR_INVALID) |
| 974 | continue; |
| 975 | |
| 976 | /* Add offset to list */ |
| 977 | msrpm_offsets[i] = offset; |
| 978 | |
| 979 | return; |
| 980 | } |
| 981 | |
| 982 | /* |
| 983 | * If this BUG triggers the msrpm_offsets table has an overflow. Just |
| 984 | * increase MSRPM_OFFSETS in this case. |
| 985 | */ |
| 986 | BUG(); |
| 987 | } |
| 988 | |
| 989 | static void init_msrpm_offsets(void) |
| 990 | { |
| 991 | int i; |
| 992 | |
| 993 | memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets)); |
| 994 | |
| 995 | for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) { |
| 996 | u32 offset; |
| 997 | |
| 998 | offset = svm_msrpm_offset(direct_access_msrs[i].index); |
| 999 | BUG_ON(offset == MSR_INVALID); |
| 1000 | |
| 1001 | add_msr_offset(offset); |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | static void svm_enable_lbrv(struct vcpu_svm *svm) |
| 1006 | { |
| 1007 | u32 *msrpm = svm->msrpm; |
| 1008 | |
| 1009 | svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK; |
| 1010 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1); |
| 1011 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1); |
| 1012 | set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1); |
| 1013 | set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1); |
| 1014 | } |
| 1015 | |
| 1016 | static void svm_disable_lbrv(struct vcpu_svm *svm) |
| 1017 | { |
| 1018 | u32 *msrpm = svm->msrpm; |
| 1019 | |
| 1020 | svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK; |
| 1021 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0); |
| 1022 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0); |
| 1023 | set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0); |
| 1024 | set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0); |
| 1025 | } |
| 1026 | |
| 1027 | static void disable_nmi_singlestep(struct vcpu_svm *svm) |
| 1028 | { |
| 1029 | svm->nmi_singlestep = false; |
| 1030 | |
| 1031 | if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) { |
| 1032 | /* Clear our flags if they were not set by the guest */ |
| 1033 | if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF)) |
| 1034 | svm->vmcb->save.rflags &= ~X86_EFLAGS_TF; |
| 1035 | if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF)) |
| 1036 | svm->vmcb->save.rflags &= ~X86_EFLAGS_RF; |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | /* Note: |
| 1041 | * This hash table is used to map VM_ID to a struct kvm_arch, |
| 1042 | * when handling AMD IOMMU GALOG notification to schedule in |
| 1043 | * a particular vCPU. |
| 1044 | */ |
| 1045 | #define SVM_VM_DATA_HASH_BITS 8 |
| 1046 | static DEFINE_HASHTABLE(svm_vm_data_hash, SVM_VM_DATA_HASH_BITS); |
| 1047 | static u32 next_vm_id = 0; |
| 1048 | static bool next_vm_id_wrapped = 0; |
| 1049 | static DEFINE_SPINLOCK(svm_vm_data_hash_lock); |
| 1050 | |
| 1051 | /* Note: |
| 1052 | * This function is called from IOMMU driver to notify |
| 1053 | * SVM to schedule in a particular vCPU of a particular VM. |
| 1054 | */ |
| 1055 | static int avic_ga_log_notifier(u32 ga_tag) |
| 1056 | { |
| 1057 | unsigned long flags; |
| 1058 | struct kvm_arch *ka = NULL; |
| 1059 | struct kvm_vcpu *vcpu = NULL; |
| 1060 | u32 vm_id = AVIC_GATAG_TO_VMID(ga_tag); |
| 1061 | u32 vcpu_id = AVIC_GATAG_TO_VCPUID(ga_tag); |
| 1062 | |
| 1063 | pr_debug("SVM: %s: vm_id=%#x, vcpu_id=%#x\n", __func__, vm_id, vcpu_id); |
| 1064 | |
| 1065 | spin_lock_irqsave(&svm_vm_data_hash_lock, flags); |
| 1066 | hash_for_each_possible(svm_vm_data_hash, ka, hnode, vm_id) { |
| 1067 | struct kvm *kvm = container_of(ka, struct kvm, arch); |
| 1068 | struct kvm_arch *vm_data = &kvm->arch; |
| 1069 | |
| 1070 | if (vm_data->avic_vm_id != vm_id) |
| 1071 | continue; |
| 1072 | vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id); |
| 1073 | break; |
| 1074 | } |
| 1075 | spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags); |
| 1076 | |
| 1077 | if (!vcpu) |
| 1078 | return 0; |
| 1079 | |
| 1080 | /* Note: |
| 1081 | * At this point, the IOMMU should have already set the pending |
| 1082 | * bit in the vAPIC backing page. So, we just need to schedule |
| 1083 | * in the vcpu. |
| 1084 | */ |
| 1085 | if (vcpu->mode == OUTSIDE_GUEST_MODE) |
| 1086 | kvm_vcpu_wake_up(vcpu); |
| 1087 | |
| 1088 | return 0; |
| 1089 | } |
| 1090 | |
| 1091 | /* |
| 1092 | * The default MMIO mask is a single bit (excluding the present bit), |
| 1093 | * which could conflict with the memory encryption bit. Check for |
| 1094 | * memory encryption support and override the default MMIO mask if |
| 1095 | * memory encryption is enabled. |
| 1096 | */ |
| 1097 | static __init void svm_adjust_mmio_mask(void) |
| 1098 | { |
| 1099 | unsigned int enc_bit, mask_bit; |
| 1100 | u64 msr, mask; |
| 1101 | |
| 1102 | /* If there is no memory encryption support, use existing mask */ |
| 1103 | if (cpuid_eax(0x80000000) < 0x8000001f) |
| 1104 | return; |
| 1105 | |
| 1106 | /* If memory encryption is not enabled, use existing mask */ |
| 1107 | rdmsrl(MSR_K8_SYSCFG, msr); |
| 1108 | if (!(msr & MSR_K8_SYSCFG_MEM_ENCRYPT)) |
| 1109 | return; |
| 1110 | |
| 1111 | enc_bit = cpuid_ebx(0x8000001f) & 0x3f; |
| 1112 | mask_bit = boot_cpu_data.x86_phys_bits; |
| 1113 | |
| 1114 | /* Increment the mask bit if it is the same as the encryption bit */ |
| 1115 | if (enc_bit == mask_bit) |
| 1116 | mask_bit++; |
| 1117 | |
| 1118 | /* |
| 1119 | * If the mask bit location is below 52, then some bits above the |
| 1120 | * physical addressing limit will always be reserved, so use the |
| 1121 | * rsvd_bits() function to generate the mask. This mask, along with |
| 1122 | * the present bit, will be used to generate a page fault with |
| 1123 | * PFER.RSV = 1. |
| 1124 | * |
| 1125 | * If the mask bit location is 52 (or above), then clear the mask. |
| 1126 | */ |
| 1127 | mask = (mask_bit < 52) ? rsvd_bits(mask_bit, 51) | PT_PRESENT_MASK : 0; |
| 1128 | |
| 1129 | kvm_mmu_set_mmio_spte_mask(mask, mask); |
| 1130 | } |
| 1131 | |
| 1132 | static __init int svm_hardware_setup(void) |
| 1133 | { |
| 1134 | int cpu; |
| 1135 | struct page *iopm_pages; |
| 1136 | void *iopm_va; |
| 1137 | int r; |
| 1138 | |
| 1139 | iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER); |
| 1140 | |
| 1141 | if (!iopm_pages) |
| 1142 | return -ENOMEM; |
| 1143 | |
| 1144 | iopm_va = page_address(iopm_pages); |
| 1145 | memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER)); |
| 1146 | iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT; |
| 1147 | |
| 1148 | init_msrpm_offsets(); |
| 1149 | |
| 1150 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 1151 | kvm_enable_efer_bits(EFER_NX); |
| 1152 | |
| 1153 | if (boot_cpu_has(X86_FEATURE_FXSR_OPT)) |
| 1154 | kvm_enable_efer_bits(EFER_FFXSR); |
| 1155 | |
| 1156 | if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) { |
| 1157 | kvm_has_tsc_control = true; |
| 1158 | kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX; |
| 1159 | kvm_tsc_scaling_ratio_frac_bits = 32; |
| 1160 | } |
| 1161 | |
| 1162 | if (nested) { |
| 1163 | printk(KERN_INFO "kvm: Nested Virtualization enabled\n"); |
| 1164 | kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE); |
| 1165 | } |
| 1166 | |
| 1167 | svm_adjust_mmio_mask(); |
| 1168 | |
| 1169 | for_each_possible_cpu(cpu) { |
| 1170 | r = svm_cpu_init(cpu); |
| 1171 | if (r) |
| 1172 | goto err; |
| 1173 | } |
| 1174 | |
| 1175 | if (!boot_cpu_has(X86_FEATURE_NPT)) |
| 1176 | npt_enabled = false; |
| 1177 | |
| 1178 | if (npt_enabled && !npt) { |
| 1179 | printk(KERN_INFO "kvm: Nested Paging disabled\n"); |
| 1180 | npt_enabled = false; |
| 1181 | } |
| 1182 | |
| 1183 | if (npt_enabled) { |
| 1184 | printk(KERN_INFO "kvm: Nested Paging enabled\n"); |
| 1185 | kvm_enable_tdp(); |
| 1186 | } else |
| 1187 | kvm_disable_tdp(); |
| 1188 | |
| 1189 | if (avic) { |
| 1190 | if (!npt_enabled || |
| 1191 | !boot_cpu_has(X86_FEATURE_AVIC) || |
| 1192 | !IS_ENABLED(CONFIG_X86_LOCAL_APIC)) { |
| 1193 | avic = false; |
| 1194 | } else { |
| 1195 | pr_info("AVIC enabled\n"); |
| 1196 | |
| 1197 | amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier); |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | if (vls) { |
| 1202 | if (!npt_enabled || |
| 1203 | !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) || |
| 1204 | !IS_ENABLED(CONFIG_X86_64)) { |
| 1205 | vls = false; |
| 1206 | } else { |
| 1207 | pr_info("Virtual VMLOAD VMSAVE supported\n"); |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | if (vgif) { |
| 1212 | if (!boot_cpu_has(X86_FEATURE_VGIF)) |
| 1213 | vgif = false; |
| 1214 | else |
| 1215 | pr_info("Virtual GIF supported\n"); |
| 1216 | } |
| 1217 | |
| 1218 | return 0; |
| 1219 | |
| 1220 | err: |
| 1221 | __free_pages(iopm_pages, IOPM_ALLOC_ORDER); |
| 1222 | iopm_base = 0; |
| 1223 | return r; |
| 1224 | } |
| 1225 | |
| 1226 | static __exit void svm_hardware_unsetup(void) |
| 1227 | { |
| 1228 | int cpu; |
| 1229 | |
| 1230 | for_each_possible_cpu(cpu) |
| 1231 | svm_cpu_uninit(cpu); |
| 1232 | |
| 1233 | __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER); |
| 1234 | iopm_base = 0; |
| 1235 | } |
| 1236 | |
| 1237 | static void init_seg(struct vmcb_seg *seg) |
| 1238 | { |
| 1239 | seg->selector = 0; |
| 1240 | seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK | |
| 1241 | SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */ |
| 1242 | seg->limit = 0xffff; |
| 1243 | seg->base = 0; |
| 1244 | } |
| 1245 | |
| 1246 | static void init_sys_seg(struct vmcb_seg *seg, uint32_t type) |
| 1247 | { |
| 1248 | seg->selector = 0; |
| 1249 | seg->attrib = SVM_SELECTOR_P_MASK | type; |
| 1250 | seg->limit = 0xffff; |
| 1251 | seg->base = 0; |
| 1252 | } |
| 1253 | |
| 1254 | static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
| 1255 | { |
| 1256 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1257 | u64 g_tsc_offset = 0; |
| 1258 | |
| 1259 | if (is_guest_mode(vcpu)) { |
| 1260 | g_tsc_offset = svm->vmcb->control.tsc_offset - |
| 1261 | svm->nested.hsave->control.tsc_offset; |
| 1262 | svm->nested.hsave->control.tsc_offset = offset; |
| 1263 | } else |
| 1264 | trace_kvm_write_tsc_offset(vcpu->vcpu_id, |
| 1265 | svm->vmcb->control.tsc_offset, |
| 1266 | offset); |
| 1267 | |
| 1268 | svm->vmcb->control.tsc_offset = offset + g_tsc_offset; |
| 1269 | |
| 1270 | mark_dirty(svm->vmcb, VMCB_INTERCEPTS); |
| 1271 | } |
| 1272 | |
| 1273 | static void avic_init_vmcb(struct vcpu_svm *svm) |
| 1274 | { |
| 1275 | struct vmcb *vmcb = svm->vmcb; |
| 1276 | struct kvm_arch *vm_data = &svm->vcpu.kvm->arch; |
| 1277 | phys_addr_t bpa = __sme_set(page_to_phys(svm->avic_backing_page)); |
| 1278 | phys_addr_t lpa = __sme_set(page_to_phys(vm_data->avic_logical_id_table_page)); |
| 1279 | phys_addr_t ppa = __sme_set(page_to_phys(vm_data->avic_physical_id_table_page)); |
| 1280 | |
| 1281 | vmcb->control.avic_backing_page = bpa & AVIC_HPA_MASK; |
| 1282 | vmcb->control.avic_logical_id = lpa & AVIC_HPA_MASK; |
| 1283 | vmcb->control.avic_physical_id = ppa & AVIC_HPA_MASK; |
| 1284 | vmcb->control.avic_physical_id |= AVIC_MAX_PHYSICAL_ID_COUNT; |
| 1285 | vmcb->control.int_ctl |= AVIC_ENABLE_MASK; |
| 1286 | } |
| 1287 | |
| 1288 | static void init_vmcb(struct vcpu_svm *svm) |
| 1289 | { |
| 1290 | struct vmcb_control_area *control = &svm->vmcb->control; |
| 1291 | struct vmcb_save_area *save = &svm->vmcb->save; |
| 1292 | |
| 1293 | svm->vcpu.arch.hflags = 0; |
| 1294 | |
| 1295 | set_cr_intercept(svm, INTERCEPT_CR0_READ); |
| 1296 | set_cr_intercept(svm, INTERCEPT_CR3_READ); |
| 1297 | set_cr_intercept(svm, INTERCEPT_CR4_READ); |
| 1298 | set_cr_intercept(svm, INTERCEPT_CR0_WRITE); |
| 1299 | set_cr_intercept(svm, INTERCEPT_CR3_WRITE); |
| 1300 | set_cr_intercept(svm, INTERCEPT_CR4_WRITE); |
| 1301 | if (!kvm_vcpu_apicv_active(&svm->vcpu)) |
| 1302 | set_cr_intercept(svm, INTERCEPT_CR8_WRITE); |
| 1303 | |
| 1304 | set_dr_intercepts(svm); |
| 1305 | |
| 1306 | set_exception_intercept(svm, PF_VECTOR); |
| 1307 | set_exception_intercept(svm, UD_VECTOR); |
| 1308 | set_exception_intercept(svm, MC_VECTOR); |
| 1309 | set_exception_intercept(svm, AC_VECTOR); |
| 1310 | set_exception_intercept(svm, DB_VECTOR); |
| 1311 | |
| 1312 | set_intercept(svm, INTERCEPT_INTR); |
| 1313 | set_intercept(svm, INTERCEPT_NMI); |
| 1314 | set_intercept(svm, INTERCEPT_SMI); |
| 1315 | set_intercept(svm, INTERCEPT_SELECTIVE_CR0); |
| 1316 | set_intercept(svm, INTERCEPT_RDPMC); |
| 1317 | set_intercept(svm, INTERCEPT_CPUID); |
| 1318 | set_intercept(svm, INTERCEPT_INVD); |
| 1319 | set_intercept(svm, INTERCEPT_HLT); |
| 1320 | set_intercept(svm, INTERCEPT_INVLPG); |
| 1321 | set_intercept(svm, INTERCEPT_INVLPGA); |
| 1322 | set_intercept(svm, INTERCEPT_IOIO_PROT); |
| 1323 | set_intercept(svm, INTERCEPT_MSR_PROT); |
| 1324 | set_intercept(svm, INTERCEPT_TASK_SWITCH); |
| 1325 | set_intercept(svm, INTERCEPT_SHUTDOWN); |
| 1326 | set_intercept(svm, INTERCEPT_VMRUN); |
| 1327 | set_intercept(svm, INTERCEPT_VMMCALL); |
| 1328 | set_intercept(svm, INTERCEPT_VMLOAD); |
| 1329 | set_intercept(svm, INTERCEPT_VMSAVE); |
| 1330 | set_intercept(svm, INTERCEPT_STGI); |
| 1331 | set_intercept(svm, INTERCEPT_CLGI); |
| 1332 | set_intercept(svm, INTERCEPT_SKINIT); |
| 1333 | set_intercept(svm, INTERCEPT_WBINVD); |
| 1334 | set_intercept(svm, INTERCEPT_XSETBV); |
| 1335 | |
| 1336 | if (!kvm_mwait_in_guest()) { |
| 1337 | set_intercept(svm, INTERCEPT_MONITOR); |
| 1338 | set_intercept(svm, INTERCEPT_MWAIT); |
| 1339 | } |
| 1340 | |
| 1341 | control->iopm_base_pa = __sme_set(iopm_base); |
| 1342 | control->msrpm_base_pa = __sme_set(__pa(svm->msrpm)); |
| 1343 | control->int_ctl = V_INTR_MASKING_MASK; |
| 1344 | |
| 1345 | init_seg(&save->es); |
| 1346 | init_seg(&save->ss); |
| 1347 | init_seg(&save->ds); |
| 1348 | init_seg(&save->fs); |
| 1349 | init_seg(&save->gs); |
| 1350 | |
| 1351 | save->cs.selector = 0xf000; |
| 1352 | save->cs.base = 0xffff0000; |
| 1353 | /* Executable/Readable Code Segment */ |
| 1354 | save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK | |
| 1355 | SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK; |
| 1356 | save->cs.limit = 0xffff; |
| 1357 | |
| 1358 | save->gdtr.limit = 0xffff; |
| 1359 | save->idtr.limit = 0xffff; |
| 1360 | |
| 1361 | init_sys_seg(&save->ldtr, SEG_TYPE_LDT); |
| 1362 | init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16); |
| 1363 | |
| 1364 | svm_set_efer(&svm->vcpu, 0); |
| 1365 | save->dr6 = 0xffff0ff0; |
| 1366 | kvm_set_rflags(&svm->vcpu, 2); |
| 1367 | save->rip = 0x0000fff0; |
| 1368 | svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip; |
| 1369 | |
| 1370 | /* |
| 1371 | * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0. |
| 1372 | * It also updates the guest-visible cr0 value. |
| 1373 | */ |
| 1374 | svm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET); |
| 1375 | kvm_mmu_reset_context(&svm->vcpu); |
| 1376 | |
| 1377 | save->cr4 = X86_CR4_PAE; |
| 1378 | /* rdx = ?? */ |
| 1379 | |
| 1380 | if (npt_enabled) { |
| 1381 | /* Setup VMCB for Nested Paging */ |
| 1382 | control->nested_ctl = 1; |
| 1383 | clr_intercept(svm, INTERCEPT_INVLPG); |
| 1384 | clr_exception_intercept(svm, PF_VECTOR); |
| 1385 | clr_cr_intercept(svm, INTERCEPT_CR3_READ); |
| 1386 | clr_cr_intercept(svm, INTERCEPT_CR3_WRITE); |
| 1387 | save->g_pat = svm->vcpu.arch.pat; |
| 1388 | save->cr3 = 0; |
| 1389 | save->cr4 = 0; |
| 1390 | } |
| 1391 | svm->asid_generation = 0; |
| 1392 | |
| 1393 | svm->nested.vmcb = 0; |
| 1394 | svm->vcpu.arch.hflags = 0; |
| 1395 | |
| 1396 | if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) { |
| 1397 | control->pause_filter_count = 3000; |
| 1398 | set_intercept(svm, INTERCEPT_PAUSE); |
| 1399 | } |
| 1400 | |
| 1401 | if (kvm_vcpu_apicv_active(&svm->vcpu)) |
| 1402 | avic_init_vmcb(svm); |
| 1403 | |
| 1404 | /* |
| 1405 | * If hardware supports Virtual VMLOAD VMSAVE then enable it |
| 1406 | * in VMCB and clear intercepts to avoid #VMEXIT. |
| 1407 | */ |
| 1408 | if (vls) { |
| 1409 | clr_intercept(svm, INTERCEPT_VMLOAD); |
| 1410 | clr_intercept(svm, INTERCEPT_VMSAVE); |
| 1411 | svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK; |
| 1412 | } |
| 1413 | |
| 1414 | if (vgif) { |
| 1415 | clr_intercept(svm, INTERCEPT_STGI); |
| 1416 | clr_intercept(svm, INTERCEPT_CLGI); |
| 1417 | svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK; |
| 1418 | } |
| 1419 | |
| 1420 | mark_all_dirty(svm->vmcb); |
| 1421 | |
| 1422 | enable_gif(svm); |
| 1423 | |
| 1424 | } |
| 1425 | |
| 1426 | static u64 *avic_get_physical_id_entry(struct kvm_vcpu *vcpu, |
| 1427 | unsigned int index) |
| 1428 | { |
| 1429 | u64 *avic_physical_id_table; |
| 1430 | struct kvm_arch *vm_data = &vcpu->kvm->arch; |
| 1431 | |
| 1432 | if (index >= AVIC_MAX_PHYSICAL_ID_COUNT) |
| 1433 | return NULL; |
| 1434 | |
| 1435 | avic_physical_id_table = page_address(vm_data->avic_physical_id_table_page); |
| 1436 | |
| 1437 | return &avic_physical_id_table[index]; |
| 1438 | } |
| 1439 | |
| 1440 | /** |
| 1441 | * Note: |
| 1442 | * AVIC hardware walks the nested page table to check permissions, |
| 1443 | * but does not use the SPA address specified in the leaf page |
| 1444 | * table entry since it uses address in the AVIC_BACKING_PAGE pointer |
| 1445 | * field of the VMCB. Therefore, we set up the |
| 1446 | * APIC_ACCESS_PAGE_PRIVATE_MEMSLOT (4KB) here. |
| 1447 | */ |
| 1448 | static int avic_init_access_page(struct kvm_vcpu *vcpu) |
| 1449 | { |
| 1450 | struct kvm *kvm = vcpu->kvm; |
| 1451 | int ret = 0; |
| 1452 | |
| 1453 | mutex_lock(&kvm->slots_lock); |
| 1454 | if (kvm->arch.apic_access_page_done) |
| 1455 | goto out; |
| 1456 | |
| 1457 | ret = __x86_set_memory_region(kvm, |
| 1458 | APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, |
| 1459 | APIC_DEFAULT_PHYS_BASE, |
| 1460 | PAGE_SIZE); |
| 1461 | if (ret) |
| 1462 | goto out; |
| 1463 | |
| 1464 | kvm->arch.apic_access_page_done = true; |
| 1465 | out: |
| 1466 | mutex_unlock(&kvm->slots_lock); |
| 1467 | return ret; |
| 1468 | } |
| 1469 | |
| 1470 | static int avic_init_backing_page(struct kvm_vcpu *vcpu) |
| 1471 | { |
| 1472 | int ret; |
| 1473 | u64 *entry, new_entry; |
| 1474 | int id = vcpu->vcpu_id; |
| 1475 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1476 | |
| 1477 | ret = avic_init_access_page(vcpu); |
| 1478 | if (ret) |
| 1479 | return ret; |
| 1480 | |
| 1481 | if (id >= AVIC_MAX_PHYSICAL_ID_COUNT) |
| 1482 | return -EINVAL; |
| 1483 | |
| 1484 | if (!svm->vcpu.arch.apic->regs) |
| 1485 | return -EINVAL; |
| 1486 | |
| 1487 | svm->avic_backing_page = virt_to_page(svm->vcpu.arch.apic->regs); |
| 1488 | |
| 1489 | /* Setting AVIC backing page address in the phy APIC ID table */ |
| 1490 | entry = avic_get_physical_id_entry(vcpu, id); |
| 1491 | if (!entry) |
| 1492 | return -EINVAL; |
| 1493 | |
| 1494 | new_entry = READ_ONCE(*entry); |
| 1495 | new_entry = __sme_set((page_to_phys(svm->avic_backing_page) & |
| 1496 | AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK) | |
| 1497 | AVIC_PHYSICAL_ID_ENTRY_VALID_MASK); |
| 1498 | WRITE_ONCE(*entry, new_entry); |
| 1499 | |
| 1500 | svm->avic_physical_id_cache = entry; |
| 1501 | |
| 1502 | return 0; |
| 1503 | } |
| 1504 | |
| 1505 | static void avic_vm_destroy(struct kvm *kvm) |
| 1506 | { |
| 1507 | unsigned long flags; |
| 1508 | struct kvm_arch *vm_data = &kvm->arch; |
| 1509 | |
| 1510 | if (!avic) |
| 1511 | return; |
| 1512 | |
| 1513 | if (vm_data->avic_logical_id_table_page) |
| 1514 | __free_page(vm_data->avic_logical_id_table_page); |
| 1515 | if (vm_data->avic_physical_id_table_page) |
| 1516 | __free_page(vm_data->avic_physical_id_table_page); |
| 1517 | |
| 1518 | spin_lock_irqsave(&svm_vm_data_hash_lock, flags); |
| 1519 | hash_del(&vm_data->hnode); |
| 1520 | spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags); |
| 1521 | } |
| 1522 | |
| 1523 | static int avic_vm_init(struct kvm *kvm) |
| 1524 | { |
| 1525 | unsigned long flags; |
| 1526 | int err = -ENOMEM; |
| 1527 | struct kvm_arch *vm_data = &kvm->arch; |
| 1528 | struct page *p_page; |
| 1529 | struct page *l_page; |
| 1530 | struct kvm_arch *ka; |
| 1531 | u32 vm_id; |
| 1532 | |
| 1533 | if (!avic) |
| 1534 | return 0; |
| 1535 | |
| 1536 | /* Allocating physical APIC ID table (4KB) */ |
| 1537 | p_page = alloc_page(GFP_KERNEL); |
| 1538 | if (!p_page) |
| 1539 | goto free_avic; |
| 1540 | |
| 1541 | vm_data->avic_physical_id_table_page = p_page; |
| 1542 | clear_page(page_address(p_page)); |
| 1543 | |
| 1544 | /* Allocating logical APIC ID table (4KB) */ |
| 1545 | l_page = alloc_page(GFP_KERNEL); |
| 1546 | if (!l_page) |
| 1547 | goto free_avic; |
| 1548 | |
| 1549 | vm_data->avic_logical_id_table_page = l_page; |
| 1550 | clear_page(page_address(l_page)); |
| 1551 | |
| 1552 | spin_lock_irqsave(&svm_vm_data_hash_lock, flags); |
| 1553 | again: |
| 1554 | vm_id = next_vm_id = (next_vm_id + 1) & AVIC_VM_ID_MASK; |
| 1555 | if (vm_id == 0) { /* id is 1-based, zero is not okay */ |
| 1556 | next_vm_id_wrapped = 1; |
| 1557 | goto again; |
| 1558 | } |
| 1559 | /* Is it still in use? Only possible if wrapped at least once */ |
| 1560 | if (next_vm_id_wrapped) { |
| 1561 | hash_for_each_possible(svm_vm_data_hash, ka, hnode, vm_id) { |
| 1562 | struct kvm *k2 = container_of(ka, struct kvm, arch); |
| 1563 | struct kvm_arch *vd2 = &k2->arch; |
| 1564 | if (vd2->avic_vm_id == vm_id) |
| 1565 | goto again; |
| 1566 | } |
| 1567 | } |
| 1568 | vm_data->avic_vm_id = vm_id; |
| 1569 | hash_add(svm_vm_data_hash, &vm_data->hnode, vm_data->avic_vm_id); |
| 1570 | spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags); |
| 1571 | |
| 1572 | return 0; |
| 1573 | |
| 1574 | free_avic: |
| 1575 | avic_vm_destroy(kvm); |
| 1576 | return err; |
| 1577 | } |
| 1578 | |
| 1579 | static inline int |
| 1580 | avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r) |
| 1581 | { |
| 1582 | int ret = 0; |
| 1583 | unsigned long flags; |
| 1584 | struct amd_svm_iommu_ir *ir; |
| 1585 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1586 | |
| 1587 | if (!kvm_arch_has_assigned_device(vcpu->kvm)) |
| 1588 | return 0; |
| 1589 | |
| 1590 | /* |
| 1591 | * Here, we go through the per-vcpu ir_list to update all existing |
| 1592 | * interrupt remapping table entry targeting this vcpu. |
| 1593 | */ |
| 1594 | spin_lock_irqsave(&svm->ir_list_lock, flags); |
| 1595 | |
| 1596 | if (list_empty(&svm->ir_list)) |
| 1597 | goto out; |
| 1598 | |
| 1599 | list_for_each_entry(ir, &svm->ir_list, node) { |
| 1600 | ret = amd_iommu_update_ga(cpu, r, ir->data); |
| 1601 | if (ret) |
| 1602 | break; |
| 1603 | } |
| 1604 | out: |
| 1605 | spin_unlock_irqrestore(&svm->ir_list_lock, flags); |
| 1606 | return ret; |
| 1607 | } |
| 1608 | |
| 1609 | static void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 1610 | { |
| 1611 | u64 entry; |
| 1612 | /* ID = 0xff (broadcast), ID > 0xff (reserved) */ |
| 1613 | int h_physical_id = kvm_cpu_get_apicid(cpu); |
| 1614 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1615 | |
| 1616 | if (!kvm_vcpu_apicv_active(vcpu)) |
| 1617 | return; |
| 1618 | |
| 1619 | /* |
| 1620 | * Since the host physical APIC id is 8 bits, |
| 1621 | * we can support host APIC ID upto 255. |
| 1622 | */ |
| 1623 | if (WARN_ON(h_physical_id > AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK)) |
| 1624 | return; |
| 1625 | |
| 1626 | entry = READ_ONCE(*(svm->avic_physical_id_cache)); |
| 1627 | WARN_ON(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK); |
| 1628 | |
| 1629 | entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK; |
| 1630 | entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK); |
| 1631 | |
| 1632 | entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; |
| 1633 | if (svm->avic_is_running) |
| 1634 | entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; |
| 1635 | |
| 1636 | WRITE_ONCE(*(svm->avic_physical_id_cache), entry); |
| 1637 | avic_update_iommu_vcpu_affinity(vcpu, h_physical_id, |
| 1638 | svm->avic_is_running); |
| 1639 | } |
| 1640 | |
| 1641 | static void avic_vcpu_put(struct kvm_vcpu *vcpu) |
| 1642 | { |
| 1643 | u64 entry; |
| 1644 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1645 | |
| 1646 | if (!kvm_vcpu_apicv_active(vcpu)) |
| 1647 | return; |
| 1648 | |
| 1649 | entry = READ_ONCE(*(svm->avic_physical_id_cache)); |
| 1650 | if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK) |
| 1651 | avic_update_iommu_vcpu_affinity(vcpu, -1, 0); |
| 1652 | |
| 1653 | entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; |
| 1654 | WRITE_ONCE(*(svm->avic_physical_id_cache), entry); |
| 1655 | } |
| 1656 | |
| 1657 | /** |
| 1658 | * This function is called during VCPU halt/unhalt. |
| 1659 | */ |
| 1660 | static void avic_set_running(struct kvm_vcpu *vcpu, bool is_run) |
| 1661 | { |
| 1662 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1663 | |
| 1664 | svm->avic_is_running = is_run; |
| 1665 | if (is_run) |
| 1666 | avic_vcpu_load(vcpu, vcpu->cpu); |
| 1667 | else |
| 1668 | avic_vcpu_put(vcpu); |
| 1669 | } |
| 1670 | |
| 1671 | static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
| 1672 | { |
| 1673 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1674 | u32 dummy; |
| 1675 | u32 eax = 1; |
| 1676 | |
| 1677 | vcpu->arch.microcode_version = 0x01000065; |
| 1678 | svm->spec_ctrl = 0; |
| 1679 | svm->virt_spec_ctrl = 0; |
| 1680 | |
| 1681 | if (!init_event) { |
| 1682 | svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE | |
| 1683 | MSR_IA32_APICBASE_ENABLE; |
| 1684 | if (kvm_vcpu_is_reset_bsp(&svm->vcpu)) |
| 1685 | svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP; |
| 1686 | } |
| 1687 | init_vmcb(svm); |
| 1688 | |
| 1689 | kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, true); |
| 1690 | kvm_register_write(vcpu, VCPU_REGS_RDX, eax); |
| 1691 | |
| 1692 | if (kvm_vcpu_apicv_active(vcpu) && !init_event) |
| 1693 | avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE); |
| 1694 | } |
| 1695 | |
| 1696 | static int avic_init_vcpu(struct vcpu_svm *svm) |
| 1697 | { |
| 1698 | int ret; |
| 1699 | |
| 1700 | if (!kvm_vcpu_apicv_active(&svm->vcpu)) |
| 1701 | return 0; |
| 1702 | |
| 1703 | ret = avic_init_backing_page(&svm->vcpu); |
| 1704 | if (ret) |
| 1705 | return ret; |
| 1706 | |
| 1707 | INIT_LIST_HEAD(&svm->ir_list); |
| 1708 | spin_lock_init(&svm->ir_list_lock); |
| 1709 | |
| 1710 | return ret; |
| 1711 | } |
| 1712 | |
| 1713 | static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id) |
| 1714 | { |
| 1715 | struct vcpu_svm *svm; |
| 1716 | struct page *page; |
| 1717 | struct page *msrpm_pages; |
| 1718 | struct page *hsave_page; |
| 1719 | struct page *nested_msrpm_pages; |
| 1720 | int err; |
| 1721 | |
| 1722 | svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
| 1723 | if (!svm) { |
| 1724 | err = -ENOMEM; |
| 1725 | goto out; |
| 1726 | } |
| 1727 | |
| 1728 | err = kvm_vcpu_init(&svm->vcpu, kvm, id); |
| 1729 | if (err) |
| 1730 | goto free_svm; |
| 1731 | |
| 1732 | err = -ENOMEM; |
| 1733 | page = alloc_page(GFP_KERNEL); |
| 1734 | if (!page) |
| 1735 | goto uninit; |
| 1736 | |
| 1737 | msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER); |
| 1738 | if (!msrpm_pages) |
| 1739 | goto free_page1; |
| 1740 | |
| 1741 | nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER); |
| 1742 | if (!nested_msrpm_pages) |
| 1743 | goto free_page2; |
| 1744 | |
| 1745 | hsave_page = alloc_page(GFP_KERNEL); |
| 1746 | if (!hsave_page) |
| 1747 | goto free_page3; |
| 1748 | |
| 1749 | err = avic_init_vcpu(svm); |
| 1750 | if (err) |
| 1751 | goto free_page4; |
| 1752 | |
| 1753 | /* We initialize this flag to true to make sure that the is_running |
| 1754 | * bit would be set the first time the vcpu is loaded. |
| 1755 | */ |
| 1756 | svm->avic_is_running = true; |
| 1757 | |
| 1758 | svm->nested.hsave = page_address(hsave_page); |
| 1759 | |
| 1760 | svm->msrpm = page_address(msrpm_pages); |
| 1761 | svm_vcpu_init_msrpm(svm->msrpm); |
| 1762 | |
| 1763 | svm->nested.msrpm = page_address(nested_msrpm_pages); |
| 1764 | svm_vcpu_init_msrpm(svm->nested.msrpm); |
| 1765 | |
| 1766 | svm->vmcb = page_address(page); |
| 1767 | clear_page(svm->vmcb); |
| 1768 | svm->vmcb_pa = __sme_set(page_to_pfn(page) << PAGE_SHIFT); |
| 1769 | svm->asid_generation = 0; |
| 1770 | init_vmcb(svm); |
| 1771 | |
| 1772 | svm_init_osvw(&svm->vcpu); |
| 1773 | |
| 1774 | return &svm->vcpu; |
| 1775 | |
| 1776 | free_page4: |
| 1777 | __free_page(hsave_page); |
| 1778 | free_page3: |
| 1779 | __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER); |
| 1780 | free_page2: |
| 1781 | __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER); |
| 1782 | free_page1: |
| 1783 | __free_page(page); |
| 1784 | uninit: |
| 1785 | kvm_vcpu_uninit(&svm->vcpu); |
| 1786 | free_svm: |
| 1787 | kmem_cache_free(kvm_vcpu_cache, svm); |
| 1788 | out: |
| 1789 | return ERR_PTR(err); |
| 1790 | } |
| 1791 | |
| 1792 | static void svm_clear_current_vmcb(struct vmcb *vmcb) |
| 1793 | { |
| 1794 | int i; |
| 1795 | |
| 1796 | for_each_online_cpu(i) |
| 1797 | cmpxchg(&per_cpu(svm_data, i)->current_vmcb, vmcb, NULL); |
| 1798 | } |
| 1799 | |
| 1800 | static void svm_free_vcpu(struct kvm_vcpu *vcpu) |
| 1801 | { |
| 1802 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1803 | |
| 1804 | /* |
| 1805 | * The vmcb page can be recycled, causing a false negative in |
| 1806 | * svm_vcpu_load(). So, ensure that no logical CPU has this |
| 1807 | * vmcb page recorded as its current vmcb. |
| 1808 | */ |
| 1809 | svm_clear_current_vmcb(svm->vmcb); |
| 1810 | |
| 1811 | __free_page(pfn_to_page(__sme_clr(svm->vmcb_pa) >> PAGE_SHIFT)); |
| 1812 | __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER); |
| 1813 | __free_page(virt_to_page(svm->nested.hsave)); |
| 1814 | __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER); |
| 1815 | kvm_vcpu_uninit(vcpu); |
| 1816 | kmem_cache_free(kvm_vcpu_cache, svm); |
| 1817 | } |
| 1818 | |
| 1819 | static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 1820 | { |
| 1821 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1822 | struct svm_cpu_data *sd = per_cpu(svm_data, cpu); |
| 1823 | int i; |
| 1824 | |
| 1825 | if (unlikely(cpu != vcpu->cpu)) { |
| 1826 | svm->asid_generation = 0; |
| 1827 | mark_all_dirty(svm->vmcb); |
| 1828 | } |
| 1829 | |
| 1830 | #ifdef CONFIG_X86_64 |
| 1831 | rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base); |
| 1832 | #endif |
| 1833 | savesegment(fs, svm->host.fs); |
| 1834 | savesegment(gs, svm->host.gs); |
| 1835 | svm->host.ldt = kvm_read_ldt(); |
| 1836 | |
| 1837 | for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++) |
| 1838 | rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]); |
| 1839 | |
| 1840 | if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) { |
| 1841 | u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio; |
| 1842 | if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) { |
| 1843 | __this_cpu_write(current_tsc_ratio, tsc_ratio); |
| 1844 | wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio); |
| 1845 | } |
| 1846 | } |
| 1847 | /* This assumes that the kernel never uses MSR_TSC_AUX */ |
| 1848 | if (static_cpu_has(X86_FEATURE_RDTSCP)) |
| 1849 | wrmsrl(MSR_TSC_AUX, svm->tsc_aux); |
| 1850 | |
| 1851 | if (sd->current_vmcb != svm->vmcb) { |
| 1852 | sd->current_vmcb = svm->vmcb; |
| 1853 | indirect_branch_prediction_barrier(); |
| 1854 | } |
| 1855 | avic_vcpu_load(vcpu, cpu); |
| 1856 | } |
| 1857 | |
| 1858 | static void svm_vcpu_put(struct kvm_vcpu *vcpu) |
| 1859 | { |
| 1860 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1861 | int i; |
| 1862 | |
| 1863 | avic_vcpu_put(vcpu); |
| 1864 | |
| 1865 | ++vcpu->stat.host_state_reload; |
| 1866 | kvm_load_ldt(svm->host.ldt); |
| 1867 | #ifdef CONFIG_X86_64 |
| 1868 | loadsegment(fs, svm->host.fs); |
| 1869 | wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gsbase); |
| 1870 | load_gs_index(svm->host.gs); |
| 1871 | #else |
| 1872 | #ifdef CONFIG_X86_32_LAZY_GS |
| 1873 | loadsegment(gs, svm->host.gs); |
| 1874 | #endif |
| 1875 | #endif |
| 1876 | for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++) |
| 1877 | wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]); |
| 1878 | } |
| 1879 | |
| 1880 | static void svm_vcpu_blocking(struct kvm_vcpu *vcpu) |
| 1881 | { |
| 1882 | avic_set_running(vcpu, false); |
| 1883 | } |
| 1884 | |
| 1885 | static void svm_vcpu_unblocking(struct kvm_vcpu *vcpu) |
| 1886 | { |
| 1887 | avic_set_running(vcpu, true); |
| 1888 | } |
| 1889 | |
| 1890 | static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu) |
| 1891 | { |
| 1892 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1893 | unsigned long rflags = svm->vmcb->save.rflags; |
| 1894 | |
| 1895 | if (svm->nmi_singlestep) { |
| 1896 | /* Hide our flags if they were not set by the guest */ |
| 1897 | if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF)) |
| 1898 | rflags &= ~X86_EFLAGS_TF; |
| 1899 | if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF)) |
| 1900 | rflags &= ~X86_EFLAGS_RF; |
| 1901 | } |
| 1902 | return rflags; |
| 1903 | } |
| 1904 | |
| 1905 | static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 1906 | { |
| 1907 | if (to_svm(vcpu)->nmi_singlestep) |
| 1908 | rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF); |
| 1909 | |
| 1910 | /* |
| 1911 | * Any change of EFLAGS.VM is accompanied by a reload of SS |
| 1912 | * (caused by either a task switch or an inter-privilege IRET), |
| 1913 | * so we do not need to update the CPL here. |
| 1914 | */ |
| 1915 | to_svm(vcpu)->vmcb->save.rflags = rflags; |
| 1916 | } |
| 1917 | |
| 1918 | static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg) |
| 1919 | { |
| 1920 | switch (reg) { |
| 1921 | case VCPU_EXREG_PDPTR: |
| 1922 | BUG_ON(!npt_enabled); |
| 1923 | load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu)); |
| 1924 | break; |
| 1925 | default: |
| 1926 | BUG(); |
| 1927 | } |
| 1928 | } |
| 1929 | |
| 1930 | static void svm_set_vintr(struct vcpu_svm *svm) |
| 1931 | { |
| 1932 | set_intercept(svm, INTERCEPT_VINTR); |
| 1933 | } |
| 1934 | |
| 1935 | static void svm_clear_vintr(struct vcpu_svm *svm) |
| 1936 | { |
| 1937 | clr_intercept(svm, INTERCEPT_VINTR); |
| 1938 | } |
| 1939 | |
| 1940 | static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg) |
| 1941 | { |
| 1942 | struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save; |
| 1943 | |
| 1944 | switch (seg) { |
| 1945 | case VCPU_SREG_CS: return &save->cs; |
| 1946 | case VCPU_SREG_DS: return &save->ds; |
| 1947 | case VCPU_SREG_ES: return &save->es; |
| 1948 | case VCPU_SREG_FS: return &save->fs; |
| 1949 | case VCPU_SREG_GS: return &save->gs; |
| 1950 | case VCPU_SREG_SS: return &save->ss; |
| 1951 | case VCPU_SREG_TR: return &save->tr; |
| 1952 | case VCPU_SREG_LDTR: return &save->ldtr; |
| 1953 | } |
| 1954 | BUG(); |
| 1955 | return NULL; |
| 1956 | } |
| 1957 | |
| 1958 | static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 1959 | { |
| 1960 | struct vmcb_seg *s = svm_seg(vcpu, seg); |
| 1961 | |
| 1962 | return s->base; |
| 1963 | } |
| 1964 | |
| 1965 | static void svm_get_segment(struct kvm_vcpu *vcpu, |
| 1966 | struct kvm_segment *var, int seg) |
| 1967 | { |
| 1968 | struct vmcb_seg *s = svm_seg(vcpu, seg); |
| 1969 | |
| 1970 | var->base = s->base; |
| 1971 | var->limit = s->limit; |
| 1972 | var->selector = s->selector; |
| 1973 | var->type = s->attrib & SVM_SELECTOR_TYPE_MASK; |
| 1974 | var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1; |
| 1975 | var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3; |
| 1976 | var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1; |
| 1977 | var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1; |
| 1978 | var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1; |
| 1979 | var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1; |
| 1980 | |
| 1981 | /* |
| 1982 | * AMD CPUs circa 2014 track the G bit for all segments except CS. |
| 1983 | * However, the SVM spec states that the G bit is not observed by the |
| 1984 | * CPU, and some VMware virtual CPUs drop the G bit for all segments. |
| 1985 | * So let's synthesize a legal G bit for all segments, this helps |
| 1986 | * running KVM nested. It also helps cross-vendor migration, because |
| 1987 | * Intel's vmentry has a check on the 'G' bit. |
| 1988 | */ |
| 1989 | var->g = s->limit > 0xfffff; |
| 1990 | |
| 1991 | /* |
| 1992 | * AMD's VMCB does not have an explicit unusable field, so emulate it |
| 1993 | * for cross vendor migration purposes by "not present" |
| 1994 | */ |
| 1995 | var->unusable = !var->present; |
| 1996 | |
| 1997 | switch (seg) { |
| 1998 | case VCPU_SREG_TR: |
| 1999 | /* |
| 2000 | * Work around a bug where the busy flag in the tr selector |
| 2001 | * isn't exposed |
| 2002 | */ |
| 2003 | var->type |= 0x2; |
| 2004 | break; |
| 2005 | case VCPU_SREG_DS: |
| 2006 | case VCPU_SREG_ES: |
| 2007 | case VCPU_SREG_FS: |
| 2008 | case VCPU_SREG_GS: |
| 2009 | /* |
| 2010 | * The accessed bit must always be set in the segment |
| 2011 | * descriptor cache, although it can be cleared in the |
| 2012 | * descriptor, the cached bit always remains at 1. Since |
| 2013 | * Intel has a check on this, set it here to support |
| 2014 | * cross-vendor migration. |
| 2015 | */ |
| 2016 | if (!var->unusable) |
| 2017 | var->type |= 0x1; |
| 2018 | break; |
| 2019 | case VCPU_SREG_SS: |
| 2020 | /* |
| 2021 | * On AMD CPUs sometimes the DB bit in the segment |
| 2022 | * descriptor is left as 1, although the whole segment has |
| 2023 | * been made unusable. Clear it here to pass an Intel VMX |
| 2024 | * entry check when cross vendor migrating. |
| 2025 | */ |
| 2026 | if (var->unusable) |
| 2027 | var->db = 0; |
| 2028 | /* This is symmetric with svm_set_segment() */ |
| 2029 | var->dpl = to_svm(vcpu)->vmcb->save.cpl; |
| 2030 | break; |
| 2031 | } |
| 2032 | } |
| 2033 | |
| 2034 | static int svm_get_cpl(struct kvm_vcpu *vcpu) |
| 2035 | { |
| 2036 | struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save; |
| 2037 | |
| 2038 | return save->cpl; |
| 2039 | } |
| 2040 | |
| 2041 | static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
| 2042 | { |
| 2043 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2044 | |
| 2045 | dt->size = svm->vmcb->save.idtr.limit; |
| 2046 | dt->address = svm->vmcb->save.idtr.base; |
| 2047 | } |
| 2048 | |
| 2049 | static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
| 2050 | { |
| 2051 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2052 | |
| 2053 | svm->vmcb->save.idtr.limit = dt->size; |
| 2054 | svm->vmcb->save.idtr.base = dt->address ; |
| 2055 | mark_dirty(svm->vmcb, VMCB_DT); |
| 2056 | } |
| 2057 | |
| 2058 | static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
| 2059 | { |
| 2060 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2061 | |
| 2062 | dt->size = svm->vmcb->save.gdtr.limit; |
| 2063 | dt->address = svm->vmcb->save.gdtr.base; |
| 2064 | } |
| 2065 | |
| 2066 | static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
| 2067 | { |
| 2068 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2069 | |
| 2070 | svm->vmcb->save.gdtr.limit = dt->size; |
| 2071 | svm->vmcb->save.gdtr.base = dt->address ; |
| 2072 | mark_dirty(svm->vmcb, VMCB_DT); |
| 2073 | } |
| 2074 | |
| 2075 | static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
| 2076 | { |
| 2077 | } |
| 2078 | |
| 2079 | static void svm_decache_cr3(struct kvm_vcpu *vcpu) |
| 2080 | { |
| 2081 | } |
| 2082 | |
| 2083 | static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
| 2084 | { |
| 2085 | } |
| 2086 | |
| 2087 | static void update_cr0_intercept(struct vcpu_svm *svm) |
| 2088 | { |
| 2089 | ulong gcr0 = svm->vcpu.arch.cr0; |
| 2090 | u64 *hcr0 = &svm->vmcb->save.cr0; |
| 2091 | |
| 2092 | *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK) |
| 2093 | | (gcr0 & SVM_CR0_SELECTIVE_MASK); |
| 2094 | |
| 2095 | mark_dirty(svm->vmcb, VMCB_CR); |
| 2096 | |
| 2097 | if (gcr0 == *hcr0) { |
| 2098 | clr_cr_intercept(svm, INTERCEPT_CR0_READ); |
| 2099 | clr_cr_intercept(svm, INTERCEPT_CR0_WRITE); |
| 2100 | } else { |
| 2101 | set_cr_intercept(svm, INTERCEPT_CR0_READ); |
| 2102 | set_cr_intercept(svm, INTERCEPT_CR0_WRITE); |
| 2103 | } |
| 2104 | } |
| 2105 | |
| 2106 | static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 2107 | { |
| 2108 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2109 | |
| 2110 | #ifdef CONFIG_X86_64 |
| 2111 | if (vcpu->arch.efer & EFER_LME) { |
| 2112 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) { |
| 2113 | vcpu->arch.efer |= EFER_LMA; |
| 2114 | svm->vmcb->save.efer |= EFER_LMA | EFER_LME; |
| 2115 | } |
| 2116 | |
| 2117 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) { |
| 2118 | vcpu->arch.efer &= ~EFER_LMA; |
| 2119 | svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME); |
| 2120 | } |
| 2121 | } |
| 2122 | #endif |
| 2123 | vcpu->arch.cr0 = cr0; |
| 2124 | |
| 2125 | if (!npt_enabled) |
| 2126 | cr0 |= X86_CR0_PG | X86_CR0_WP; |
| 2127 | |
| 2128 | /* |
| 2129 | * re-enable caching here because the QEMU bios |
| 2130 | * does not do it - this results in some delay at |
| 2131 | * reboot |
| 2132 | */ |
| 2133 | if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED)) |
| 2134 | cr0 &= ~(X86_CR0_CD | X86_CR0_NW); |
| 2135 | svm->vmcb->save.cr0 = cr0; |
| 2136 | mark_dirty(svm->vmcb, VMCB_CR); |
| 2137 | update_cr0_intercept(svm); |
| 2138 | } |
| 2139 | |
| 2140 | static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
| 2141 | { |
| 2142 | unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE; |
| 2143 | unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4; |
| 2144 | |
| 2145 | if (cr4 & X86_CR4_VMXE) |
| 2146 | return 1; |
| 2147 | |
| 2148 | if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE)) |
| 2149 | svm_flush_tlb(vcpu, true); |
| 2150 | |
| 2151 | vcpu->arch.cr4 = cr4; |
| 2152 | if (!npt_enabled) |
| 2153 | cr4 |= X86_CR4_PAE; |
| 2154 | cr4 |= host_cr4_mce; |
| 2155 | to_svm(vcpu)->vmcb->save.cr4 = cr4; |
| 2156 | mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR); |
| 2157 | return 0; |
| 2158 | } |
| 2159 | |
| 2160 | static void svm_set_segment(struct kvm_vcpu *vcpu, |
| 2161 | struct kvm_segment *var, int seg) |
| 2162 | { |
| 2163 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2164 | struct vmcb_seg *s = svm_seg(vcpu, seg); |
| 2165 | |
| 2166 | s->base = var->base; |
| 2167 | s->limit = var->limit; |
| 2168 | s->selector = var->selector; |
| 2169 | s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK); |
| 2170 | s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT; |
| 2171 | s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT; |
| 2172 | s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT; |
| 2173 | s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT; |
| 2174 | s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT; |
| 2175 | s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT; |
| 2176 | s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT; |
| 2177 | |
| 2178 | /* |
| 2179 | * This is always accurate, except if SYSRET returned to a segment |
| 2180 | * with SS.DPL != 3. Intel does not have this quirk, and always |
| 2181 | * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it |
| 2182 | * would entail passing the CPL to userspace and back. |
| 2183 | */ |
| 2184 | if (seg == VCPU_SREG_SS) |
| 2185 | /* This is symmetric with svm_get_segment() */ |
| 2186 | svm->vmcb->save.cpl = (var->dpl & 3); |
| 2187 | |
| 2188 | mark_dirty(svm->vmcb, VMCB_SEG); |
| 2189 | } |
| 2190 | |
| 2191 | static void update_bp_intercept(struct kvm_vcpu *vcpu) |
| 2192 | { |
| 2193 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2194 | |
| 2195 | clr_exception_intercept(svm, BP_VECTOR); |
| 2196 | |
| 2197 | if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) { |
| 2198 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
| 2199 | set_exception_intercept(svm, BP_VECTOR); |
| 2200 | } else |
| 2201 | vcpu->guest_debug = 0; |
| 2202 | } |
| 2203 | |
| 2204 | static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd) |
| 2205 | { |
| 2206 | if (sd->next_asid > sd->max_asid) { |
| 2207 | ++sd->asid_generation; |
| 2208 | sd->next_asid = 1; |
| 2209 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID; |
| 2210 | } |
| 2211 | |
| 2212 | svm->asid_generation = sd->asid_generation; |
| 2213 | svm->vmcb->control.asid = sd->next_asid++; |
| 2214 | |
| 2215 | mark_dirty(svm->vmcb, VMCB_ASID); |
| 2216 | } |
| 2217 | |
| 2218 | static u64 svm_get_dr6(struct kvm_vcpu *vcpu) |
| 2219 | { |
| 2220 | return to_svm(vcpu)->vmcb->save.dr6; |
| 2221 | } |
| 2222 | |
| 2223 | static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value) |
| 2224 | { |
| 2225 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2226 | |
| 2227 | svm->vmcb->save.dr6 = value; |
| 2228 | mark_dirty(svm->vmcb, VMCB_DR); |
| 2229 | } |
| 2230 | |
| 2231 | static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) |
| 2232 | { |
| 2233 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2234 | |
| 2235 | get_debugreg(vcpu->arch.db[0], 0); |
| 2236 | get_debugreg(vcpu->arch.db[1], 1); |
| 2237 | get_debugreg(vcpu->arch.db[2], 2); |
| 2238 | get_debugreg(vcpu->arch.db[3], 3); |
| 2239 | vcpu->arch.dr6 = svm_get_dr6(vcpu); |
| 2240 | vcpu->arch.dr7 = svm->vmcb->save.dr7; |
| 2241 | |
| 2242 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; |
| 2243 | set_dr_intercepts(svm); |
| 2244 | } |
| 2245 | |
| 2246 | static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value) |
| 2247 | { |
| 2248 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2249 | |
| 2250 | svm->vmcb->save.dr7 = value; |
| 2251 | mark_dirty(svm->vmcb, VMCB_DR); |
| 2252 | } |
| 2253 | |
| 2254 | static int pf_interception(struct vcpu_svm *svm) |
| 2255 | { |
| 2256 | u64 fault_address = svm->vmcb->control.exit_info_2; |
| 2257 | u64 error_code = svm->vmcb->control.exit_info_1; |
| 2258 | |
| 2259 | return kvm_handle_page_fault(&svm->vcpu, error_code, fault_address, |
| 2260 | svm->vmcb->control.insn_bytes, |
| 2261 | svm->vmcb->control.insn_len, !npt_enabled); |
| 2262 | } |
| 2263 | |
| 2264 | static int db_interception(struct vcpu_svm *svm) |
| 2265 | { |
| 2266 | struct kvm_run *kvm_run = svm->vcpu.run; |
| 2267 | struct kvm_vcpu *vcpu = &svm->vcpu; |
| 2268 | |
| 2269 | if (!(svm->vcpu.guest_debug & |
| 2270 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) && |
| 2271 | !svm->nmi_singlestep) { |
| 2272 | kvm_queue_exception(&svm->vcpu, DB_VECTOR); |
| 2273 | return 1; |
| 2274 | } |
| 2275 | |
| 2276 | if (svm->nmi_singlestep) { |
| 2277 | disable_nmi_singlestep(svm); |
| 2278 | /* Make sure we check for pending NMIs upon entry */ |
| 2279 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 2280 | } |
| 2281 | |
| 2282 | if (svm->vcpu.guest_debug & |
| 2283 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) { |
| 2284 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
| 2285 | kvm_run->debug.arch.pc = |
| 2286 | svm->vmcb->save.cs.base + svm->vmcb->save.rip; |
| 2287 | kvm_run->debug.arch.exception = DB_VECTOR; |
| 2288 | return 0; |
| 2289 | } |
| 2290 | |
| 2291 | return 1; |
| 2292 | } |
| 2293 | |
| 2294 | static int bp_interception(struct vcpu_svm *svm) |
| 2295 | { |
| 2296 | struct kvm_run *kvm_run = svm->vcpu.run; |
| 2297 | |
| 2298 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
| 2299 | kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip; |
| 2300 | kvm_run->debug.arch.exception = BP_VECTOR; |
| 2301 | return 0; |
| 2302 | } |
| 2303 | |
| 2304 | static int ud_interception(struct vcpu_svm *svm) |
| 2305 | { |
| 2306 | int er; |
| 2307 | |
| 2308 | er = emulate_instruction(&svm->vcpu, EMULTYPE_TRAP_UD); |
| 2309 | if (er == EMULATE_USER_EXIT) |
| 2310 | return 0; |
| 2311 | if (er != EMULATE_DONE) |
| 2312 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
| 2313 | return 1; |
| 2314 | } |
| 2315 | |
| 2316 | static int ac_interception(struct vcpu_svm *svm) |
| 2317 | { |
| 2318 | kvm_queue_exception_e(&svm->vcpu, AC_VECTOR, 0); |
| 2319 | return 1; |
| 2320 | } |
| 2321 | |
| 2322 | static bool is_erratum_383(void) |
| 2323 | { |
| 2324 | int err, i; |
| 2325 | u64 value; |
| 2326 | |
| 2327 | if (!erratum_383_found) |
| 2328 | return false; |
| 2329 | |
| 2330 | value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err); |
| 2331 | if (err) |
| 2332 | return false; |
| 2333 | |
| 2334 | /* Bit 62 may or may not be set for this mce */ |
| 2335 | value &= ~(1ULL << 62); |
| 2336 | |
| 2337 | if (value != 0xb600000000010015ULL) |
| 2338 | return false; |
| 2339 | |
| 2340 | /* Clear MCi_STATUS registers */ |
| 2341 | for (i = 0; i < 6; ++i) |
| 2342 | native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0); |
| 2343 | |
| 2344 | value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err); |
| 2345 | if (!err) { |
| 2346 | u32 low, high; |
| 2347 | |
| 2348 | value &= ~(1ULL << 2); |
| 2349 | low = lower_32_bits(value); |
| 2350 | high = upper_32_bits(value); |
| 2351 | |
| 2352 | native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high); |
| 2353 | } |
| 2354 | |
| 2355 | /* Flush tlb to evict multi-match entries */ |
| 2356 | __flush_tlb_all(); |
| 2357 | |
| 2358 | return true; |
| 2359 | } |
| 2360 | |
| 2361 | static void svm_handle_mce(struct vcpu_svm *svm) |
| 2362 | { |
| 2363 | if (is_erratum_383()) { |
| 2364 | /* |
| 2365 | * Erratum 383 triggered. Guest state is corrupt so kill the |
| 2366 | * guest. |
| 2367 | */ |
| 2368 | pr_err("KVM: Guest triggered AMD Erratum 383\n"); |
| 2369 | |
| 2370 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu); |
| 2371 | |
| 2372 | return; |
| 2373 | } |
| 2374 | |
| 2375 | /* |
| 2376 | * On an #MC intercept the MCE handler is not called automatically in |
| 2377 | * the host. So do it by hand here. |
| 2378 | */ |
| 2379 | asm volatile ( |
| 2380 | "int $0x12\n"); |
| 2381 | /* not sure if we ever come back to this point */ |
| 2382 | |
| 2383 | return; |
| 2384 | } |
| 2385 | |
| 2386 | static int mc_interception(struct vcpu_svm *svm) |
| 2387 | { |
| 2388 | return 1; |
| 2389 | } |
| 2390 | |
| 2391 | static int shutdown_interception(struct vcpu_svm *svm) |
| 2392 | { |
| 2393 | struct kvm_run *kvm_run = svm->vcpu.run; |
| 2394 | |
| 2395 | /* |
| 2396 | * VMCB is undefined after a SHUTDOWN intercept |
| 2397 | * so reinitialize it. |
| 2398 | */ |
| 2399 | clear_page(svm->vmcb); |
| 2400 | init_vmcb(svm); |
| 2401 | |
| 2402 | kvm_run->exit_reason = KVM_EXIT_SHUTDOWN; |
| 2403 | return 0; |
| 2404 | } |
| 2405 | |
| 2406 | static int io_interception(struct vcpu_svm *svm) |
| 2407 | { |
| 2408 | struct kvm_vcpu *vcpu = &svm->vcpu; |
| 2409 | u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */ |
| 2410 | int size, in, string, ret; |
| 2411 | unsigned port; |
| 2412 | |
| 2413 | ++svm->vcpu.stat.io_exits; |
| 2414 | string = (io_info & SVM_IOIO_STR_MASK) != 0; |
| 2415 | in = (io_info & SVM_IOIO_TYPE_MASK) != 0; |
| 2416 | if (string) |
| 2417 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
| 2418 | |
| 2419 | port = io_info >> 16; |
| 2420 | size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT; |
| 2421 | svm->next_rip = svm->vmcb->control.exit_info_2; |
| 2422 | ret = kvm_skip_emulated_instruction(&svm->vcpu); |
| 2423 | |
| 2424 | /* |
| 2425 | * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered |
| 2426 | * KVM_EXIT_DEBUG here. |
| 2427 | */ |
| 2428 | if (in) |
| 2429 | return kvm_fast_pio_in(vcpu, size, port) && ret; |
| 2430 | else |
| 2431 | return kvm_fast_pio_out(vcpu, size, port) && ret; |
| 2432 | } |
| 2433 | |
| 2434 | static int nmi_interception(struct vcpu_svm *svm) |
| 2435 | { |
| 2436 | return 1; |
| 2437 | } |
| 2438 | |
| 2439 | static int intr_interception(struct vcpu_svm *svm) |
| 2440 | { |
| 2441 | ++svm->vcpu.stat.irq_exits; |
| 2442 | return 1; |
| 2443 | } |
| 2444 | |
| 2445 | static int nop_on_interception(struct vcpu_svm *svm) |
| 2446 | { |
| 2447 | return 1; |
| 2448 | } |
| 2449 | |
| 2450 | static int halt_interception(struct vcpu_svm *svm) |
| 2451 | { |
| 2452 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 1; |
| 2453 | return kvm_emulate_halt(&svm->vcpu); |
| 2454 | } |
| 2455 | |
| 2456 | static int vmmcall_interception(struct vcpu_svm *svm) |
| 2457 | { |
| 2458 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
| 2459 | return kvm_emulate_hypercall(&svm->vcpu); |
| 2460 | } |
| 2461 | |
| 2462 | static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu) |
| 2463 | { |
| 2464 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2465 | |
| 2466 | return svm->nested.nested_cr3; |
| 2467 | } |
| 2468 | |
| 2469 | static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index) |
| 2470 | { |
| 2471 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2472 | u64 cr3 = svm->nested.nested_cr3; |
| 2473 | u64 pdpte; |
| 2474 | int ret; |
| 2475 | |
| 2476 | ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(__sme_clr(cr3)), &pdpte, |
| 2477 | offset_in_page(cr3) + index * 8, 8); |
| 2478 | if (ret) |
| 2479 | return 0; |
| 2480 | return pdpte; |
| 2481 | } |
| 2482 | |
| 2483 | static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu, |
| 2484 | unsigned long root) |
| 2485 | { |
| 2486 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2487 | |
| 2488 | svm->vmcb->control.nested_cr3 = __sme_set(root); |
| 2489 | mark_dirty(svm->vmcb, VMCB_NPT); |
| 2490 | svm_flush_tlb(vcpu, true); |
| 2491 | } |
| 2492 | |
| 2493 | static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu, |
| 2494 | struct x86_exception *fault) |
| 2495 | { |
| 2496 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2497 | |
| 2498 | if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) { |
| 2499 | /* |
| 2500 | * TODO: track the cause of the nested page fault, and |
| 2501 | * correctly fill in the high bits of exit_info_1. |
| 2502 | */ |
| 2503 | svm->vmcb->control.exit_code = SVM_EXIT_NPF; |
| 2504 | svm->vmcb->control.exit_code_hi = 0; |
| 2505 | svm->vmcb->control.exit_info_1 = (1ULL << 32); |
| 2506 | svm->vmcb->control.exit_info_2 = fault->address; |
| 2507 | } |
| 2508 | |
| 2509 | svm->vmcb->control.exit_info_1 &= ~0xffffffffULL; |
| 2510 | svm->vmcb->control.exit_info_1 |= fault->error_code; |
| 2511 | |
| 2512 | /* |
| 2513 | * The present bit is always zero for page structure faults on real |
| 2514 | * hardware. |
| 2515 | */ |
| 2516 | if (svm->vmcb->control.exit_info_1 & (2ULL << 32)) |
| 2517 | svm->vmcb->control.exit_info_1 &= ~1; |
| 2518 | |
| 2519 | nested_svm_vmexit(svm); |
| 2520 | } |
| 2521 | |
| 2522 | static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu) |
| 2523 | { |
| 2524 | WARN_ON(mmu_is_nested(vcpu)); |
| 2525 | kvm_init_shadow_mmu(vcpu); |
| 2526 | vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3; |
| 2527 | vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3; |
| 2528 | vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr; |
| 2529 | vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit; |
| 2530 | vcpu->arch.mmu.shadow_root_level = get_npt_level(vcpu); |
| 2531 | reset_shadow_zero_bits_mask(vcpu, &vcpu->arch.mmu); |
| 2532 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; |
| 2533 | } |
| 2534 | |
| 2535 | static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu) |
| 2536 | { |
| 2537 | vcpu->arch.walk_mmu = &vcpu->arch.mmu; |
| 2538 | } |
| 2539 | |
| 2540 | static int nested_svm_check_permissions(struct vcpu_svm *svm) |
| 2541 | { |
| 2542 | if (!(svm->vcpu.arch.efer & EFER_SVME) || |
| 2543 | !is_paging(&svm->vcpu)) { |
| 2544 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
| 2545 | return 1; |
| 2546 | } |
| 2547 | |
| 2548 | if (svm->vmcb->save.cpl) { |
| 2549 | kvm_inject_gp(&svm->vcpu, 0); |
| 2550 | return 1; |
| 2551 | } |
| 2552 | |
| 2553 | return 0; |
| 2554 | } |
| 2555 | |
| 2556 | static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr, |
| 2557 | bool has_error_code, u32 error_code) |
| 2558 | { |
| 2559 | int vmexit; |
| 2560 | |
| 2561 | if (!is_guest_mode(&svm->vcpu)) |
| 2562 | return 0; |
| 2563 | |
| 2564 | vmexit = nested_svm_intercept(svm); |
| 2565 | if (vmexit != NESTED_EXIT_DONE) |
| 2566 | return 0; |
| 2567 | |
| 2568 | svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr; |
| 2569 | svm->vmcb->control.exit_code_hi = 0; |
| 2570 | svm->vmcb->control.exit_info_1 = error_code; |
| 2571 | |
| 2572 | /* |
| 2573 | * FIXME: we should not write CR2 when L1 intercepts an L2 #PF exception. |
| 2574 | * The fix is to add the ancillary datum (CR2 or DR6) to structs |
| 2575 | * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6 can be |
| 2576 | * written only when inject_pending_event runs (DR6 would written here |
| 2577 | * too). This should be conditional on a new capability---if the |
| 2578 | * capability is disabled, kvm_multiple_exception would write the |
| 2579 | * ancillary information to CR2 or DR6, for backwards ABI-compatibility. |
| 2580 | */ |
| 2581 | if (svm->vcpu.arch.exception.nested_apf) |
| 2582 | svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token; |
| 2583 | else |
| 2584 | svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2; |
| 2585 | |
| 2586 | svm->nested.exit_required = true; |
| 2587 | return vmexit; |
| 2588 | } |
| 2589 | |
| 2590 | /* This function returns true if it is save to enable the irq window */ |
| 2591 | static inline bool nested_svm_intr(struct vcpu_svm *svm) |
| 2592 | { |
| 2593 | if (!is_guest_mode(&svm->vcpu)) |
| 2594 | return true; |
| 2595 | |
| 2596 | if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK)) |
| 2597 | return true; |
| 2598 | |
| 2599 | if (!(svm->vcpu.arch.hflags & HF_HIF_MASK)) |
| 2600 | return false; |
| 2601 | |
| 2602 | /* |
| 2603 | * if vmexit was already requested (by intercepted exception |
| 2604 | * for instance) do not overwrite it with "external interrupt" |
| 2605 | * vmexit. |
| 2606 | */ |
| 2607 | if (svm->nested.exit_required) |
| 2608 | return false; |
| 2609 | |
| 2610 | svm->vmcb->control.exit_code = SVM_EXIT_INTR; |
| 2611 | svm->vmcb->control.exit_info_1 = 0; |
| 2612 | svm->vmcb->control.exit_info_2 = 0; |
| 2613 | |
| 2614 | if (svm->nested.intercept & 1ULL) { |
| 2615 | /* |
| 2616 | * The #vmexit can't be emulated here directly because this |
| 2617 | * code path runs with irqs and preemption disabled. A |
| 2618 | * #vmexit emulation might sleep. Only signal request for |
| 2619 | * the #vmexit here. |
| 2620 | */ |
| 2621 | svm->nested.exit_required = true; |
| 2622 | trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip); |
| 2623 | return false; |
| 2624 | } |
| 2625 | |
| 2626 | return true; |
| 2627 | } |
| 2628 | |
| 2629 | /* This function returns true if it is save to enable the nmi window */ |
| 2630 | static inline bool nested_svm_nmi(struct vcpu_svm *svm) |
| 2631 | { |
| 2632 | if (!is_guest_mode(&svm->vcpu)) |
| 2633 | return true; |
| 2634 | |
| 2635 | if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI))) |
| 2636 | return true; |
| 2637 | |
| 2638 | svm->vmcb->control.exit_code = SVM_EXIT_NMI; |
| 2639 | svm->nested.exit_required = true; |
| 2640 | |
| 2641 | return false; |
| 2642 | } |
| 2643 | |
| 2644 | static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page) |
| 2645 | { |
| 2646 | struct page *page; |
| 2647 | |
| 2648 | might_sleep(); |
| 2649 | |
| 2650 | page = kvm_vcpu_gfn_to_page(&svm->vcpu, gpa >> PAGE_SHIFT); |
| 2651 | if (is_error_page(page)) |
| 2652 | goto error; |
| 2653 | |
| 2654 | *_page = page; |
| 2655 | |
| 2656 | return kmap(page); |
| 2657 | |
| 2658 | error: |
| 2659 | kvm_inject_gp(&svm->vcpu, 0); |
| 2660 | |
| 2661 | return NULL; |
| 2662 | } |
| 2663 | |
| 2664 | static void nested_svm_unmap(struct page *page) |
| 2665 | { |
| 2666 | kunmap(page); |
| 2667 | kvm_release_page_dirty(page); |
| 2668 | } |
| 2669 | |
| 2670 | static int nested_svm_intercept_ioio(struct vcpu_svm *svm) |
| 2671 | { |
| 2672 | unsigned port, size, iopm_len; |
| 2673 | u16 val, mask; |
| 2674 | u8 start_bit; |
| 2675 | u64 gpa; |
| 2676 | |
| 2677 | if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT))) |
| 2678 | return NESTED_EXIT_HOST; |
| 2679 | |
| 2680 | port = svm->vmcb->control.exit_info_1 >> 16; |
| 2681 | size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >> |
| 2682 | SVM_IOIO_SIZE_SHIFT; |
| 2683 | gpa = svm->nested.vmcb_iopm + (port / 8); |
| 2684 | start_bit = port % 8; |
| 2685 | iopm_len = (start_bit + size > 8) ? 2 : 1; |
| 2686 | mask = (0xf >> (4 - size)) << start_bit; |
| 2687 | val = 0; |
| 2688 | |
| 2689 | if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len)) |
| 2690 | return NESTED_EXIT_DONE; |
| 2691 | |
| 2692 | return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST; |
| 2693 | } |
| 2694 | |
| 2695 | static int nested_svm_exit_handled_msr(struct vcpu_svm *svm) |
| 2696 | { |
| 2697 | u32 offset, msr, value; |
| 2698 | int write, mask; |
| 2699 | |
| 2700 | if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT))) |
| 2701 | return NESTED_EXIT_HOST; |
| 2702 | |
| 2703 | msr = svm->vcpu.arch.regs[VCPU_REGS_RCX]; |
| 2704 | offset = svm_msrpm_offset(msr); |
| 2705 | write = svm->vmcb->control.exit_info_1 & 1; |
| 2706 | mask = 1 << ((2 * (msr & 0xf)) + write); |
| 2707 | |
| 2708 | if (offset == MSR_INVALID) |
| 2709 | return NESTED_EXIT_DONE; |
| 2710 | |
| 2711 | /* Offset is in 32 bit units but need in 8 bit units */ |
| 2712 | offset *= 4; |
| 2713 | |
| 2714 | if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.vmcb_msrpm + offset, &value, 4)) |
| 2715 | return NESTED_EXIT_DONE; |
| 2716 | |
| 2717 | return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST; |
| 2718 | } |
| 2719 | |
| 2720 | /* DB exceptions for our internal use must not cause vmexit */ |
| 2721 | static int nested_svm_intercept_db(struct vcpu_svm *svm) |
| 2722 | { |
| 2723 | unsigned long dr6; |
| 2724 | |
| 2725 | /* if we're not singlestepping, it's not ours */ |
| 2726 | if (!svm->nmi_singlestep) |
| 2727 | return NESTED_EXIT_DONE; |
| 2728 | |
| 2729 | /* if it's not a singlestep exception, it's not ours */ |
| 2730 | if (kvm_get_dr(&svm->vcpu, 6, &dr6)) |
| 2731 | return NESTED_EXIT_DONE; |
| 2732 | if (!(dr6 & DR6_BS)) |
| 2733 | return NESTED_EXIT_DONE; |
| 2734 | |
| 2735 | /* if the guest is singlestepping, it should get the vmexit */ |
| 2736 | if (svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF) { |
| 2737 | disable_nmi_singlestep(svm); |
| 2738 | return NESTED_EXIT_DONE; |
| 2739 | } |
| 2740 | |
| 2741 | /* it's ours, the nested hypervisor must not see this one */ |
| 2742 | return NESTED_EXIT_HOST; |
| 2743 | } |
| 2744 | |
| 2745 | static int nested_svm_exit_special(struct vcpu_svm *svm) |
| 2746 | { |
| 2747 | u32 exit_code = svm->vmcb->control.exit_code; |
| 2748 | |
| 2749 | switch (exit_code) { |
| 2750 | case SVM_EXIT_INTR: |
| 2751 | case SVM_EXIT_NMI: |
| 2752 | case SVM_EXIT_EXCP_BASE + MC_VECTOR: |
| 2753 | return NESTED_EXIT_HOST; |
| 2754 | case SVM_EXIT_NPF: |
| 2755 | /* For now we are always handling NPFs when using them */ |
| 2756 | if (npt_enabled) |
| 2757 | return NESTED_EXIT_HOST; |
| 2758 | break; |
| 2759 | case SVM_EXIT_EXCP_BASE + PF_VECTOR: |
| 2760 | /* Trap async PF even if not shadowing */ |
| 2761 | if (!npt_enabled || svm->vcpu.arch.apf.host_apf_reason) |
| 2762 | return NESTED_EXIT_HOST; |
| 2763 | break; |
| 2764 | default: |
| 2765 | break; |
| 2766 | } |
| 2767 | |
| 2768 | return NESTED_EXIT_CONTINUE; |
| 2769 | } |
| 2770 | |
| 2771 | /* |
| 2772 | * If this function returns true, this #vmexit was already handled |
| 2773 | */ |
| 2774 | static int nested_svm_intercept(struct vcpu_svm *svm) |
| 2775 | { |
| 2776 | u32 exit_code = svm->vmcb->control.exit_code; |
| 2777 | int vmexit = NESTED_EXIT_HOST; |
| 2778 | |
| 2779 | switch (exit_code) { |
| 2780 | case SVM_EXIT_MSR: |
| 2781 | vmexit = nested_svm_exit_handled_msr(svm); |
| 2782 | break; |
| 2783 | case SVM_EXIT_IOIO: |
| 2784 | vmexit = nested_svm_intercept_ioio(svm); |
| 2785 | break; |
| 2786 | case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: { |
| 2787 | u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0); |
| 2788 | if (svm->nested.intercept_cr & bit) |
| 2789 | vmexit = NESTED_EXIT_DONE; |
| 2790 | break; |
| 2791 | } |
| 2792 | case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: { |
| 2793 | u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0); |
| 2794 | if (svm->nested.intercept_dr & bit) |
| 2795 | vmexit = NESTED_EXIT_DONE; |
| 2796 | break; |
| 2797 | } |
| 2798 | case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: { |
| 2799 | u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE); |
| 2800 | if (svm->nested.intercept_exceptions & excp_bits) { |
| 2801 | if (exit_code == SVM_EXIT_EXCP_BASE + DB_VECTOR) |
| 2802 | vmexit = nested_svm_intercept_db(svm); |
| 2803 | else |
| 2804 | vmexit = NESTED_EXIT_DONE; |
| 2805 | } |
| 2806 | /* async page fault always cause vmexit */ |
| 2807 | else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) && |
| 2808 | svm->vcpu.arch.exception.nested_apf != 0) |
| 2809 | vmexit = NESTED_EXIT_DONE; |
| 2810 | break; |
| 2811 | } |
| 2812 | case SVM_EXIT_ERR: { |
| 2813 | vmexit = NESTED_EXIT_DONE; |
| 2814 | break; |
| 2815 | } |
| 2816 | default: { |
| 2817 | u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR); |
| 2818 | if (svm->nested.intercept & exit_bits) |
| 2819 | vmexit = NESTED_EXIT_DONE; |
| 2820 | } |
| 2821 | } |
| 2822 | |
| 2823 | return vmexit; |
| 2824 | } |
| 2825 | |
| 2826 | static int nested_svm_exit_handled(struct vcpu_svm *svm) |
| 2827 | { |
| 2828 | int vmexit; |
| 2829 | |
| 2830 | vmexit = nested_svm_intercept(svm); |
| 2831 | |
| 2832 | if (vmexit == NESTED_EXIT_DONE) |
| 2833 | nested_svm_vmexit(svm); |
| 2834 | |
| 2835 | return vmexit; |
| 2836 | } |
| 2837 | |
| 2838 | static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb) |
| 2839 | { |
| 2840 | struct vmcb_control_area *dst = &dst_vmcb->control; |
| 2841 | struct vmcb_control_area *from = &from_vmcb->control; |
| 2842 | |
| 2843 | dst->intercept_cr = from->intercept_cr; |
| 2844 | dst->intercept_dr = from->intercept_dr; |
| 2845 | dst->intercept_exceptions = from->intercept_exceptions; |
| 2846 | dst->intercept = from->intercept; |
| 2847 | dst->iopm_base_pa = from->iopm_base_pa; |
| 2848 | dst->msrpm_base_pa = from->msrpm_base_pa; |
| 2849 | dst->tsc_offset = from->tsc_offset; |
| 2850 | /* asid not copied, it is handled manually for svm->vmcb. */ |
| 2851 | dst->tlb_ctl = from->tlb_ctl; |
| 2852 | dst->int_ctl = from->int_ctl; |
| 2853 | dst->int_vector = from->int_vector; |
| 2854 | dst->int_state = from->int_state; |
| 2855 | dst->exit_code = from->exit_code; |
| 2856 | dst->exit_code_hi = from->exit_code_hi; |
| 2857 | dst->exit_info_1 = from->exit_info_1; |
| 2858 | dst->exit_info_2 = from->exit_info_2; |
| 2859 | dst->exit_int_info = from->exit_int_info; |
| 2860 | dst->exit_int_info_err = from->exit_int_info_err; |
| 2861 | dst->nested_ctl = from->nested_ctl; |
| 2862 | dst->event_inj = from->event_inj; |
| 2863 | dst->event_inj_err = from->event_inj_err; |
| 2864 | dst->nested_cr3 = from->nested_cr3; |
| 2865 | dst->virt_ext = from->virt_ext; |
| 2866 | } |
| 2867 | |
| 2868 | static int nested_svm_vmexit(struct vcpu_svm *svm) |
| 2869 | { |
| 2870 | struct vmcb *nested_vmcb; |
| 2871 | struct vmcb *hsave = svm->nested.hsave; |
| 2872 | struct vmcb *vmcb = svm->vmcb; |
| 2873 | struct page *page; |
| 2874 | |
| 2875 | trace_kvm_nested_vmexit_inject(vmcb->control.exit_code, |
| 2876 | vmcb->control.exit_info_1, |
| 2877 | vmcb->control.exit_info_2, |
| 2878 | vmcb->control.exit_int_info, |
| 2879 | vmcb->control.exit_int_info_err, |
| 2880 | KVM_ISA_SVM); |
| 2881 | |
| 2882 | nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page); |
| 2883 | if (!nested_vmcb) |
| 2884 | return 1; |
| 2885 | |
| 2886 | /* Exit Guest-Mode */ |
| 2887 | leave_guest_mode(&svm->vcpu); |
| 2888 | svm->nested.vmcb = 0; |
| 2889 | |
| 2890 | /* Give the current vmcb to the guest */ |
| 2891 | disable_gif(svm); |
| 2892 | |
| 2893 | nested_vmcb->save.es = vmcb->save.es; |
| 2894 | nested_vmcb->save.cs = vmcb->save.cs; |
| 2895 | nested_vmcb->save.ss = vmcb->save.ss; |
| 2896 | nested_vmcb->save.ds = vmcb->save.ds; |
| 2897 | nested_vmcb->save.gdtr = vmcb->save.gdtr; |
| 2898 | nested_vmcb->save.idtr = vmcb->save.idtr; |
| 2899 | nested_vmcb->save.efer = svm->vcpu.arch.efer; |
| 2900 | nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu); |
| 2901 | nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu); |
| 2902 | nested_vmcb->save.cr2 = vmcb->save.cr2; |
| 2903 | nested_vmcb->save.cr4 = svm->vcpu.arch.cr4; |
| 2904 | nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu); |
| 2905 | nested_vmcb->save.rip = vmcb->save.rip; |
| 2906 | nested_vmcb->save.rsp = vmcb->save.rsp; |
| 2907 | nested_vmcb->save.rax = vmcb->save.rax; |
| 2908 | nested_vmcb->save.dr7 = vmcb->save.dr7; |
| 2909 | nested_vmcb->save.dr6 = vmcb->save.dr6; |
| 2910 | nested_vmcb->save.cpl = vmcb->save.cpl; |
| 2911 | |
| 2912 | nested_vmcb->control.int_ctl = vmcb->control.int_ctl; |
| 2913 | nested_vmcb->control.int_vector = vmcb->control.int_vector; |
| 2914 | nested_vmcb->control.int_state = vmcb->control.int_state; |
| 2915 | nested_vmcb->control.exit_code = vmcb->control.exit_code; |
| 2916 | nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi; |
| 2917 | nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1; |
| 2918 | nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2; |
| 2919 | nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info; |
| 2920 | nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err; |
| 2921 | |
| 2922 | if (svm->nrips_enabled) |
| 2923 | nested_vmcb->control.next_rip = vmcb->control.next_rip; |
| 2924 | |
| 2925 | /* |
| 2926 | * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have |
| 2927 | * to make sure that we do not lose injected events. So check event_inj |
| 2928 | * here and copy it to exit_int_info if it is valid. |
| 2929 | * Exit_int_info and event_inj can't be both valid because the case |
| 2930 | * below only happens on a VMRUN instruction intercept which has |
| 2931 | * no valid exit_int_info set. |
| 2932 | */ |
| 2933 | if (vmcb->control.event_inj & SVM_EVTINJ_VALID) { |
| 2934 | struct vmcb_control_area *nc = &nested_vmcb->control; |
| 2935 | |
| 2936 | nc->exit_int_info = vmcb->control.event_inj; |
| 2937 | nc->exit_int_info_err = vmcb->control.event_inj_err; |
| 2938 | } |
| 2939 | |
| 2940 | nested_vmcb->control.tlb_ctl = 0; |
| 2941 | nested_vmcb->control.event_inj = 0; |
| 2942 | nested_vmcb->control.event_inj_err = 0; |
| 2943 | |
| 2944 | /* We always set V_INTR_MASKING and remember the old value in hflags */ |
| 2945 | if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK)) |
| 2946 | nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK; |
| 2947 | |
| 2948 | /* Restore the original control entries */ |
| 2949 | copy_vmcb_control_area(vmcb, hsave); |
| 2950 | |
| 2951 | kvm_clear_exception_queue(&svm->vcpu); |
| 2952 | kvm_clear_interrupt_queue(&svm->vcpu); |
| 2953 | |
| 2954 | svm->nested.nested_cr3 = 0; |
| 2955 | |
| 2956 | /* Restore selected save entries */ |
| 2957 | svm->vmcb->save.es = hsave->save.es; |
| 2958 | svm->vmcb->save.cs = hsave->save.cs; |
| 2959 | svm->vmcb->save.ss = hsave->save.ss; |
| 2960 | svm->vmcb->save.ds = hsave->save.ds; |
| 2961 | svm->vmcb->save.gdtr = hsave->save.gdtr; |
| 2962 | svm->vmcb->save.idtr = hsave->save.idtr; |
| 2963 | kvm_set_rflags(&svm->vcpu, hsave->save.rflags); |
| 2964 | svm_set_efer(&svm->vcpu, hsave->save.efer); |
| 2965 | svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE); |
| 2966 | svm_set_cr4(&svm->vcpu, hsave->save.cr4); |
| 2967 | if (npt_enabled) { |
| 2968 | svm->vmcb->save.cr3 = hsave->save.cr3; |
| 2969 | svm->vcpu.arch.cr3 = hsave->save.cr3; |
| 2970 | } else { |
| 2971 | (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3); |
| 2972 | } |
| 2973 | kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax); |
| 2974 | kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp); |
| 2975 | kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip); |
| 2976 | svm->vmcb->save.dr7 = 0; |
| 2977 | svm->vmcb->save.cpl = 0; |
| 2978 | svm->vmcb->control.exit_int_info = 0; |
| 2979 | |
| 2980 | mark_all_dirty(svm->vmcb); |
| 2981 | |
| 2982 | nested_svm_unmap(page); |
| 2983 | |
| 2984 | nested_svm_uninit_mmu_context(&svm->vcpu); |
| 2985 | kvm_mmu_reset_context(&svm->vcpu); |
| 2986 | kvm_mmu_load(&svm->vcpu); |
| 2987 | |
| 2988 | /* |
| 2989 | * Drop what we picked up for L2 via svm_complete_interrupts() so it |
| 2990 | * doesn't end up in L1. |
| 2991 | */ |
| 2992 | svm->vcpu.arch.nmi_injected = false; |
| 2993 | kvm_clear_exception_queue(&svm->vcpu); |
| 2994 | kvm_clear_interrupt_queue(&svm->vcpu); |
| 2995 | |
| 2996 | return 0; |
| 2997 | } |
| 2998 | |
| 2999 | static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm) |
| 3000 | { |
| 3001 | /* |
| 3002 | * This function merges the msr permission bitmaps of kvm and the |
| 3003 | * nested vmcb. It is optimized in that it only merges the parts where |
| 3004 | * the kvm msr permission bitmap may contain zero bits |
| 3005 | */ |
| 3006 | int i; |
| 3007 | |
| 3008 | if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT))) |
| 3009 | return true; |
| 3010 | |
| 3011 | for (i = 0; i < MSRPM_OFFSETS; i++) { |
| 3012 | u32 value, p; |
| 3013 | u64 offset; |
| 3014 | |
| 3015 | if (msrpm_offsets[i] == 0xffffffff) |
| 3016 | break; |
| 3017 | |
| 3018 | p = msrpm_offsets[i]; |
| 3019 | offset = svm->nested.vmcb_msrpm + (p * 4); |
| 3020 | |
| 3021 | if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4)) |
| 3022 | return false; |
| 3023 | |
| 3024 | svm->nested.msrpm[p] = svm->msrpm[p] | value; |
| 3025 | } |
| 3026 | |
| 3027 | svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm)); |
| 3028 | |
| 3029 | return true; |
| 3030 | } |
| 3031 | |
| 3032 | static bool nested_vmcb_checks(struct vmcb *vmcb) |
| 3033 | { |
| 3034 | if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0) |
| 3035 | return false; |
| 3036 | |
| 3037 | if (vmcb->control.asid == 0) |
| 3038 | return false; |
| 3039 | |
| 3040 | if (vmcb->control.nested_ctl && !npt_enabled) |
| 3041 | return false; |
| 3042 | |
| 3043 | return true; |
| 3044 | } |
| 3045 | |
| 3046 | static bool nested_svm_vmrun(struct vcpu_svm *svm) |
| 3047 | { |
| 3048 | struct vmcb *nested_vmcb; |
| 3049 | struct vmcb *hsave = svm->nested.hsave; |
| 3050 | struct vmcb *vmcb = svm->vmcb; |
| 3051 | struct page *page; |
| 3052 | u64 vmcb_gpa; |
| 3053 | |
| 3054 | vmcb_gpa = svm->vmcb->save.rax; |
| 3055 | |
| 3056 | nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page); |
| 3057 | if (!nested_vmcb) |
| 3058 | return false; |
| 3059 | |
| 3060 | if (!nested_vmcb_checks(nested_vmcb)) { |
| 3061 | nested_vmcb->control.exit_code = SVM_EXIT_ERR; |
| 3062 | nested_vmcb->control.exit_code_hi = 0; |
| 3063 | nested_vmcb->control.exit_info_1 = 0; |
| 3064 | nested_vmcb->control.exit_info_2 = 0; |
| 3065 | |
| 3066 | nested_svm_unmap(page); |
| 3067 | |
| 3068 | return false; |
| 3069 | } |
| 3070 | |
| 3071 | trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa, |
| 3072 | nested_vmcb->save.rip, |
| 3073 | nested_vmcb->control.int_ctl, |
| 3074 | nested_vmcb->control.event_inj, |
| 3075 | nested_vmcb->control.nested_ctl); |
| 3076 | |
| 3077 | trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff, |
| 3078 | nested_vmcb->control.intercept_cr >> 16, |
| 3079 | nested_vmcb->control.intercept_exceptions, |
| 3080 | nested_vmcb->control.intercept); |
| 3081 | |
| 3082 | /* Clear internal status */ |
| 3083 | kvm_clear_exception_queue(&svm->vcpu); |
| 3084 | kvm_clear_interrupt_queue(&svm->vcpu); |
| 3085 | |
| 3086 | /* |
| 3087 | * Save the old vmcb, so we don't need to pick what we save, but can |
| 3088 | * restore everything when a VMEXIT occurs |
| 3089 | */ |
| 3090 | hsave->save.es = vmcb->save.es; |
| 3091 | hsave->save.cs = vmcb->save.cs; |
| 3092 | hsave->save.ss = vmcb->save.ss; |
| 3093 | hsave->save.ds = vmcb->save.ds; |
| 3094 | hsave->save.gdtr = vmcb->save.gdtr; |
| 3095 | hsave->save.idtr = vmcb->save.idtr; |
| 3096 | hsave->save.efer = svm->vcpu.arch.efer; |
| 3097 | hsave->save.cr0 = kvm_read_cr0(&svm->vcpu); |
| 3098 | hsave->save.cr4 = svm->vcpu.arch.cr4; |
| 3099 | hsave->save.rflags = kvm_get_rflags(&svm->vcpu); |
| 3100 | hsave->save.rip = kvm_rip_read(&svm->vcpu); |
| 3101 | hsave->save.rsp = vmcb->save.rsp; |
| 3102 | hsave->save.rax = vmcb->save.rax; |
| 3103 | if (npt_enabled) |
| 3104 | hsave->save.cr3 = vmcb->save.cr3; |
| 3105 | else |
| 3106 | hsave->save.cr3 = kvm_read_cr3(&svm->vcpu); |
| 3107 | |
| 3108 | copy_vmcb_control_area(hsave, vmcb); |
| 3109 | |
| 3110 | if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF) |
| 3111 | svm->vcpu.arch.hflags |= HF_HIF_MASK; |
| 3112 | else |
| 3113 | svm->vcpu.arch.hflags &= ~HF_HIF_MASK; |
| 3114 | |
| 3115 | if (nested_vmcb->control.nested_ctl) { |
| 3116 | kvm_mmu_unload(&svm->vcpu); |
| 3117 | svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3; |
| 3118 | nested_svm_init_mmu_context(&svm->vcpu); |
| 3119 | } |
| 3120 | |
| 3121 | /* Load the nested guest state */ |
| 3122 | svm->vmcb->save.es = nested_vmcb->save.es; |
| 3123 | svm->vmcb->save.cs = nested_vmcb->save.cs; |
| 3124 | svm->vmcb->save.ss = nested_vmcb->save.ss; |
| 3125 | svm->vmcb->save.ds = nested_vmcb->save.ds; |
| 3126 | svm->vmcb->save.gdtr = nested_vmcb->save.gdtr; |
| 3127 | svm->vmcb->save.idtr = nested_vmcb->save.idtr; |
| 3128 | kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags); |
| 3129 | svm_set_efer(&svm->vcpu, nested_vmcb->save.efer); |
| 3130 | svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0); |
| 3131 | svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4); |
| 3132 | if (npt_enabled) { |
| 3133 | svm->vmcb->save.cr3 = nested_vmcb->save.cr3; |
| 3134 | svm->vcpu.arch.cr3 = nested_vmcb->save.cr3; |
| 3135 | } else |
| 3136 | (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3); |
| 3137 | |
| 3138 | /* Guest paging mode is active - reset mmu */ |
| 3139 | kvm_mmu_reset_context(&svm->vcpu); |
| 3140 | |
| 3141 | svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2; |
| 3142 | kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax); |
| 3143 | kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp); |
| 3144 | kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip); |
| 3145 | |
| 3146 | /* In case we don't even reach vcpu_run, the fields are not updated */ |
| 3147 | svm->vmcb->save.rax = nested_vmcb->save.rax; |
| 3148 | svm->vmcb->save.rsp = nested_vmcb->save.rsp; |
| 3149 | svm->vmcb->save.rip = nested_vmcb->save.rip; |
| 3150 | svm->vmcb->save.dr7 = nested_vmcb->save.dr7; |
| 3151 | svm->vmcb->save.dr6 = nested_vmcb->save.dr6; |
| 3152 | svm->vmcb->save.cpl = nested_vmcb->save.cpl; |
| 3153 | |
| 3154 | svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL; |
| 3155 | svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL; |
| 3156 | |
| 3157 | /* cache intercepts */ |
| 3158 | svm->nested.intercept_cr = nested_vmcb->control.intercept_cr; |
| 3159 | svm->nested.intercept_dr = nested_vmcb->control.intercept_dr; |
| 3160 | svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions; |
| 3161 | svm->nested.intercept = nested_vmcb->control.intercept; |
| 3162 | |
| 3163 | svm_flush_tlb(&svm->vcpu, true); |
| 3164 | svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK; |
| 3165 | if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK) |
| 3166 | svm->vcpu.arch.hflags |= HF_VINTR_MASK; |
| 3167 | else |
| 3168 | svm->vcpu.arch.hflags &= ~HF_VINTR_MASK; |
| 3169 | |
| 3170 | if (svm->vcpu.arch.hflags & HF_VINTR_MASK) { |
| 3171 | /* We only want the cr8 intercept bits of the guest */ |
| 3172 | clr_cr_intercept(svm, INTERCEPT_CR8_READ); |
| 3173 | clr_cr_intercept(svm, INTERCEPT_CR8_WRITE); |
| 3174 | } |
| 3175 | |
| 3176 | /* We don't want to see VMMCALLs from a nested guest */ |
| 3177 | clr_intercept(svm, INTERCEPT_VMMCALL); |
| 3178 | |
| 3179 | svm->vmcb->control.virt_ext = nested_vmcb->control.virt_ext; |
| 3180 | svm->vmcb->control.int_vector = nested_vmcb->control.int_vector; |
| 3181 | svm->vmcb->control.int_state = nested_vmcb->control.int_state; |
| 3182 | svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset; |
| 3183 | svm->vmcb->control.event_inj = nested_vmcb->control.event_inj; |
| 3184 | svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err; |
| 3185 | |
| 3186 | nested_svm_unmap(page); |
| 3187 | |
| 3188 | /* Enter Guest-Mode */ |
| 3189 | enter_guest_mode(&svm->vcpu); |
| 3190 | |
| 3191 | /* |
| 3192 | * Merge guest and host intercepts - must be called with vcpu in |
| 3193 | * guest-mode to take affect here |
| 3194 | */ |
| 3195 | recalc_intercepts(svm); |
| 3196 | |
| 3197 | svm->nested.vmcb = vmcb_gpa; |
| 3198 | |
| 3199 | enable_gif(svm); |
| 3200 | |
| 3201 | mark_all_dirty(svm->vmcb); |
| 3202 | |
| 3203 | return true; |
| 3204 | } |
| 3205 | |
| 3206 | static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb) |
| 3207 | { |
| 3208 | to_vmcb->save.fs = from_vmcb->save.fs; |
| 3209 | to_vmcb->save.gs = from_vmcb->save.gs; |
| 3210 | to_vmcb->save.tr = from_vmcb->save.tr; |
| 3211 | to_vmcb->save.ldtr = from_vmcb->save.ldtr; |
| 3212 | to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base; |
| 3213 | to_vmcb->save.star = from_vmcb->save.star; |
| 3214 | to_vmcb->save.lstar = from_vmcb->save.lstar; |
| 3215 | to_vmcb->save.cstar = from_vmcb->save.cstar; |
| 3216 | to_vmcb->save.sfmask = from_vmcb->save.sfmask; |
| 3217 | to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs; |
| 3218 | to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp; |
| 3219 | to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip; |
| 3220 | } |
| 3221 | |
| 3222 | static int vmload_interception(struct vcpu_svm *svm) |
| 3223 | { |
| 3224 | struct vmcb *nested_vmcb; |
| 3225 | struct page *page; |
| 3226 | int ret; |
| 3227 | |
| 3228 | if (nested_svm_check_permissions(svm)) |
| 3229 | return 1; |
| 3230 | |
| 3231 | nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page); |
| 3232 | if (!nested_vmcb) |
| 3233 | return 1; |
| 3234 | |
| 3235 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
| 3236 | ret = kvm_skip_emulated_instruction(&svm->vcpu); |
| 3237 | |
| 3238 | nested_svm_vmloadsave(nested_vmcb, svm->vmcb); |
| 3239 | nested_svm_unmap(page); |
| 3240 | |
| 3241 | return ret; |
| 3242 | } |
| 3243 | |
| 3244 | static int vmsave_interception(struct vcpu_svm *svm) |
| 3245 | { |
| 3246 | struct vmcb *nested_vmcb; |
| 3247 | struct page *page; |
| 3248 | int ret; |
| 3249 | |
| 3250 | if (nested_svm_check_permissions(svm)) |
| 3251 | return 1; |
| 3252 | |
| 3253 | nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page); |
| 3254 | if (!nested_vmcb) |
| 3255 | return 1; |
| 3256 | |
| 3257 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
| 3258 | ret = kvm_skip_emulated_instruction(&svm->vcpu); |
| 3259 | |
| 3260 | nested_svm_vmloadsave(svm->vmcb, nested_vmcb); |
| 3261 | nested_svm_unmap(page); |
| 3262 | |
| 3263 | return ret; |
| 3264 | } |
| 3265 | |
| 3266 | static int vmrun_interception(struct vcpu_svm *svm) |
| 3267 | { |
| 3268 | if (nested_svm_check_permissions(svm)) |
| 3269 | return 1; |
| 3270 | |
| 3271 | /* Save rip after vmrun instruction */ |
| 3272 | kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3); |
| 3273 | |
| 3274 | if (!nested_svm_vmrun(svm)) |
| 3275 | return 1; |
| 3276 | |
| 3277 | if (!nested_svm_vmrun_msrpm(svm)) |
| 3278 | goto failed; |
| 3279 | |
| 3280 | return 1; |
| 3281 | |
| 3282 | failed: |
| 3283 | |
| 3284 | svm->vmcb->control.exit_code = SVM_EXIT_ERR; |
| 3285 | svm->vmcb->control.exit_code_hi = 0; |
| 3286 | svm->vmcb->control.exit_info_1 = 0; |
| 3287 | svm->vmcb->control.exit_info_2 = 0; |
| 3288 | |
| 3289 | nested_svm_vmexit(svm); |
| 3290 | |
| 3291 | return 1; |
| 3292 | } |
| 3293 | |
| 3294 | static int stgi_interception(struct vcpu_svm *svm) |
| 3295 | { |
| 3296 | int ret; |
| 3297 | |
| 3298 | if (nested_svm_check_permissions(svm)) |
| 3299 | return 1; |
| 3300 | |
| 3301 | /* |
| 3302 | * If VGIF is enabled, the STGI intercept is only added to |
| 3303 | * detect the opening of the NMI window; remove it now. |
| 3304 | */ |
| 3305 | if (vgif_enabled(svm)) |
| 3306 | clr_intercept(svm, INTERCEPT_STGI); |
| 3307 | |
| 3308 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
| 3309 | ret = kvm_skip_emulated_instruction(&svm->vcpu); |
| 3310 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
| 3311 | |
| 3312 | enable_gif(svm); |
| 3313 | |
| 3314 | return ret; |
| 3315 | } |
| 3316 | |
| 3317 | static int clgi_interception(struct vcpu_svm *svm) |
| 3318 | { |
| 3319 | int ret; |
| 3320 | |
| 3321 | if (nested_svm_check_permissions(svm)) |
| 3322 | return 1; |
| 3323 | |
| 3324 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
| 3325 | ret = kvm_skip_emulated_instruction(&svm->vcpu); |
| 3326 | |
| 3327 | disable_gif(svm); |
| 3328 | |
| 3329 | /* After a CLGI no interrupts should come */ |
| 3330 | if (!kvm_vcpu_apicv_active(&svm->vcpu)) { |
| 3331 | svm_clear_vintr(svm); |
| 3332 | svm->vmcb->control.int_ctl &= ~V_IRQ_MASK; |
| 3333 | mark_dirty(svm->vmcb, VMCB_INTR); |
| 3334 | } |
| 3335 | |
| 3336 | return ret; |
| 3337 | } |
| 3338 | |
| 3339 | static int invlpga_interception(struct vcpu_svm *svm) |
| 3340 | { |
| 3341 | struct kvm_vcpu *vcpu = &svm->vcpu; |
| 3342 | |
| 3343 | trace_kvm_invlpga(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RCX), |
| 3344 | kvm_register_read(&svm->vcpu, VCPU_REGS_RAX)); |
| 3345 | |
| 3346 | /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */ |
| 3347 | kvm_mmu_invlpg(vcpu, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX)); |
| 3348 | |
| 3349 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
| 3350 | return kvm_skip_emulated_instruction(&svm->vcpu); |
| 3351 | } |
| 3352 | |
| 3353 | static int skinit_interception(struct vcpu_svm *svm) |
| 3354 | { |
| 3355 | trace_kvm_skinit(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX)); |
| 3356 | |
| 3357 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
| 3358 | return 1; |
| 3359 | } |
| 3360 | |
| 3361 | static int wbinvd_interception(struct vcpu_svm *svm) |
| 3362 | { |
| 3363 | return kvm_emulate_wbinvd(&svm->vcpu); |
| 3364 | } |
| 3365 | |
| 3366 | static int xsetbv_interception(struct vcpu_svm *svm) |
| 3367 | { |
| 3368 | u64 new_bv = kvm_read_edx_eax(&svm->vcpu); |
| 3369 | u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX); |
| 3370 | |
| 3371 | if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) { |
| 3372 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
| 3373 | return kvm_skip_emulated_instruction(&svm->vcpu); |
| 3374 | } |
| 3375 | |
| 3376 | return 1; |
| 3377 | } |
| 3378 | |
| 3379 | static int task_switch_interception(struct vcpu_svm *svm) |
| 3380 | { |
| 3381 | u16 tss_selector; |
| 3382 | int reason; |
| 3383 | int int_type = svm->vmcb->control.exit_int_info & |
| 3384 | SVM_EXITINTINFO_TYPE_MASK; |
| 3385 | int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK; |
| 3386 | uint32_t type = |
| 3387 | svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK; |
| 3388 | uint32_t idt_v = |
| 3389 | svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID; |
| 3390 | bool has_error_code = false; |
| 3391 | u32 error_code = 0; |
| 3392 | |
| 3393 | tss_selector = (u16)svm->vmcb->control.exit_info_1; |
| 3394 | |
| 3395 | if (svm->vmcb->control.exit_info_2 & |
| 3396 | (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET)) |
| 3397 | reason = TASK_SWITCH_IRET; |
| 3398 | else if (svm->vmcb->control.exit_info_2 & |
| 3399 | (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP)) |
| 3400 | reason = TASK_SWITCH_JMP; |
| 3401 | else if (idt_v) |
| 3402 | reason = TASK_SWITCH_GATE; |
| 3403 | else |
| 3404 | reason = TASK_SWITCH_CALL; |
| 3405 | |
| 3406 | if (reason == TASK_SWITCH_GATE) { |
| 3407 | switch (type) { |
| 3408 | case SVM_EXITINTINFO_TYPE_NMI: |
| 3409 | svm->vcpu.arch.nmi_injected = false; |
| 3410 | break; |
| 3411 | case SVM_EXITINTINFO_TYPE_EXEPT: |
| 3412 | if (svm->vmcb->control.exit_info_2 & |
| 3413 | (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) { |
| 3414 | has_error_code = true; |
| 3415 | error_code = |
| 3416 | (u32)svm->vmcb->control.exit_info_2; |
| 3417 | } |
| 3418 | kvm_clear_exception_queue(&svm->vcpu); |
| 3419 | break; |
| 3420 | case SVM_EXITINTINFO_TYPE_INTR: |
| 3421 | kvm_clear_interrupt_queue(&svm->vcpu); |
| 3422 | break; |
| 3423 | default: |
| 3424 | break; |
| 3425 | } |
| 3426 | } |
| 3427 | |
| 3428 | if (reason != TASK_SWITCH_GATE || |
| 3429 | int_type == SVM_EXITINTINFO_TYPE_SOFT || |
| 3430 | (int_type == SVM_EXITINTINFO_TYPE_EXEPT && |
| 3431 | (int_vec == OF_VECTOR || int_vec == BP_VECTOR))) |
| 3432 | skip_emulated_instruction(&svm->vcpu); |
| 3433 | |
| 3434 | if (int_type != SVM_EXITINTINFO_TYPE_SOFT) |
| 3435 | int_vec = -1; |
| 3436 | |
| 3437 | if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason, |
| 3438 | has_error_code, error_code) == EMULATE_FAIL) { |
| 3439 | svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 3440 | svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 3441 | svm->vcpu.run->internal.ndata = 0; |
| 3442 | return 0; |
| 3443 | } |
| 3444 | return 1; |
| 3445 | } |
| 3446 | |
| 3447 | static int cpuid_interception(struct vcpu_svm *svm) |
| 3448 | { |
| 3449 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 2; |
| 3450 | return kvm_emulate_cpuid(&svm->vcpu); |
| 3451 | } |
| 3452 | |
| 3453 | static int iret_interception(struct vcpu_svm *svm) |
| 3454 | { |
| 3455 | ++svm->vcpu.stat.nmi_window_exits; |
| 3456 | clr_intercept(svm, INTERCEPT_IRET); |
| 3457 | svm->vcpu.arch.hflags |= HF_IRET_MASK; |
| 3458 | svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu); |
| 3459 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
| 3460 | return 1; |
| 3461 | } |
| 3462 | |
| 3463 | static int invlpg_interception(struct vcpu_svm *svm) |
| 3464 | { |
| 3465 | if (!static_cpu_has(X86_FEATURE_DECODEASSISTS)) |
| 3466 | return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE; |
| 3467 | |
| 3468 | kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1); |
| 3469 | return kvm_skip_emulated_instruction(&svm->vcpu); |
| 3470 | } |
| 3471 | |
| 3472 | static int emulate_on_interception(struct vcpu_svm *svm) |
| 3473 | { |
| 3474 | return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE; |
| 3475 | } |
| 3476 | |
| 3477 | static int rdpmc_interception(struct vcpu_svm *svm) |
| 3478 | { |
| 3479 | int err; |
| 3480 | |
| 3481 | if (!static_cpu_has(X86_FEATURE_NRIPS)) |
| 3482 | return emulate_on_interception(svm); |
| 3483 | |
| 3484 | err = kvm_rdpmc(&svm->vcpu); |
| 3485 | return kvm_complete_insn_gp(&svm->vcpu, err); |
| 3486 | } |
| 3487 | |
| 3488 | static bool check_selective_cr0_intercepted(struct vcpu_svm *svm, |
| 3489 | unsigned long val) |
| 3490 | { |
| 3491 | unsigned long cr0 = svm->vcpu.arch.cr0; |
| 3492 | bool ret = false; |
| 3493 | u64 intercept; |
| 3494 | |
| 3495 | intercept = svm->nested.intercept; |
| 3496 | |
| 3497 | if (!is_guest_mode(&svm->vcpu) || |
| 3498 | (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))) |
| 3499 | return false; |
| 3500 | |
| 3501 | cr0 &= ~SVM_CR0_SELECTIVE_MASK; |
| 3502 | val &= ~SVM_CR0_SELECTIVE_MASK; |
| 3503 | |
| 3504 | if (cr0 ^ val) { |
| 3505 | svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE; |
| 3506 | ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE); |
| 3507 | } |
| 3508 | |
| 3509 | return ret; |
| 3510 | } |
| 3511 | |
| 3512 | #define CR_VALID (1ULL << 63) |
| 3513 | |
| 3514 | static int cr_interception(struct vcpu_svm *svm) |
| 3515 | { |
| 3516 | int reg, cr; |
| 3517 | unsigned long val; |
| 3518 | int err; |
| 3519 | |
| 3520 | if (!static_cpu_has(X86_FEATURE_DECODEASSISTS)) |
| 3521 | return emulate_on_interception(svm); |
| 3522 | |
| 3523 | if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0)) |
| 3524 | return emulate_on_interception(svm); |
| 3525 | |
| 3526 | reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK; |
| 3527 | if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE) |
| 3528 | cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0; |
| 3529 | else |
| 3530 | cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0; |
| 3531 | |
| 3532 | err = 0; |
| 3533 | if (cr >= 16) { /* mov to cr */ |
| 3534 | cr -= 16; |
| 3535 | val = kvm_register_read(&svm->vcpu, reg); |
| 3536 | switch (cr) { |
| 3537 | case 0: |
| 3538 | if (!check_selective_cr0_intercepted(svm, val)) |
| 3539 | err = kvm_set_cr0(&svm->vcpu, val); |
| 3540 | else |
| 3541 | return 1; |
| 3542 | |
| 3543 | break; |
| 3544 | case 3: |
| 3545 | err = kvm_set_cr3(&svm->vcpu, val); |
| 3546 | break; |
| 3547 | case 4: |
| 3548 | err = kvm_set_cr4(&svm->vcpu, val); |
| 3549 | break; |
| 3550 | case 8: |
| 3551 | err = kvm_set_cr8(&svm->vcpu, val); |
| 3552 | break; |
| 3553 | default: |
| 3554 | WARN(1, "unhandled write to CR%d", cr); |
| 3555 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
| 3556 | return 1; |
| 3557 | } |
| 3558 | } else { /* mov from cr */ |
| 3559 | switch (cr) { |
| 3560 | case 0: |
| 3561 | val = kvm_read_cr0(&svm->vcpu); |
| 3562 | break; |
| 3563 | case 2: |
| 3564 | val = svm->vcpu.arch.cr2; |
| 3565 | break; |
| 3566 | case 3: |
| 3567 | val = kvm_read_cr3(&svm->vcpu); |
| 3568 | break; |
| 3569 | case 4: |
| 3570 | val = kvm_read_cr4(&svm->vcpu); |
| 3571 | break; |
| 3572 | case 8: |
| 3573 | val = kvm_get_cr8(&svm->vcpu); |
| 3574 | break; |
| 3575 | default: |
| 3576 | WARN(1, "unhandled read from CR%d", cr); |
| 3577 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
| 3578 | return 1; |
| 3579 | } |
| 3580 | kvm_register_write(&svm->vcpu, reg, val); |
| 3581 | } |
| 3582 | return kvm_complete_insn_gp(&svm->vcpu, err); |
| 3583 | } |
| 3584 | |
| 3585 | static int dr_interception(struct vcpu_svm *svm) |
| 3586 | { |
| 3587 | int reg, dr; |
| 3588 | unsigned long val; |
| 3589 | |
| 3590 | if (svm->vcpu.guest_debug == 0) { |
| 3591 | /* |
| 3592 | * No more DR vmexits; force a reload of the debug registers |
| 3593 | * and reenter on this instruction. The next vmexit will |
| 3594 | * retrieve the full state of the debug registers. |
| 3595 | */ |
| 3596 | clr_dr_intercepts(svm); |
| 3597 | svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; |
| 3598 | return 1; |
| 3599 | } |
| 3600 | |
| 3601 | if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS)) |
| 3602 | return emulate_on_interception(svm); |
| 3603 | |
| 3604 | reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK; |
| 3605 | dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0; |
| 3606 | |
| 3607 | if (dr >= 16) { /* mov to DRn */ |
| 3608 | if (!kvm_require_dr(&svm->vcpu, dr - 16)) |
| 3609 | return 1; |
| 3610 | val = kvm_register_read(&svm->vcpu, reg); |
| 3611 | kvm_set_dr(&svm->vcpu, dr - 16, val); |
| 3612 | } else { |
| 3613 | if (!kvm_require_dr(&svm->vcpu, dr)) |
| 3614 | return 1; |
| 3615 | kvm_get_dr(&svm->vcpu, dr, &val); |
| 3616 | kvm_register_write(&svm->vcpu, reg, val); |
| 3617 | } |
| 3618 | |
| 3619 | return kvm_skip_emulated_instruction(&svm->vcpu); |
| 3620 | } |
| 3621 | |
| 3622 | static int cr8_write_interception(struct vcpu_svm *svm) |
| 3623 | { |
| 3624 | struct kvm_run *kvm_run = svm->vcpu.run; |
| 3625 | int r; |
| 3626 | |
| 3627 | u8 cr8_prev = kvm_get_cr8(&svm->vcpu); |
| 3628 | /* instruction emulation calls kvm_set_cr8() */ |
| 3629 | r = cr_interception(svm); |
| 3630 | if (lapic_in_kernel(&svm->vcpu)) |
| 3631 | return r; |
| 3632 | if (cr8_prev <= kvm_get_cr8(&svm->vcpu)) |
| 3633 | return r; |
| 3634 | kvm_run->exit_reason = KVM_EXIT_SET_TPR; |
| 3635 | return 0; |
| 3636 | } |
| 3637 | |
| 3638 | static int svm_get_msr_feature(struct kvm_msr_entry *msr) |
| 3639 | { |
| 3640 | msr->data = 0; |
| 3641 | |
| 3642 | switch (msr->index) { |
| 3643 | case MSR_F10H_DECFG: |
| 3644 | if (boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) |
| 3645 | msr->data |= MSR_F10H_DECFG_LFENCE_SERIALIZE; |
| 3646 | break; |
| 3647 | default: |
| 3648 | return 1; |
| 3649 | } |
| 3650 | |
| 3651 | return 0; |
| 3652 | } |
| 3653 | |
| 3654 | static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
| 3655 | { |
| 3656 | struct vcpu_svm *svm = to_svm(vcpu); |
| 3657 | |
| 3658 | switch (msr_info->index) { |
| 3659 | case MSR_IA32_TSC: { |
| 3660 | msr_info->data = svm->vmcb->control.tsc_offset + |
| 3661 | kvm_scale_tsc(vcpu, rdtsc()); |
| 3662 | |
| 3663 | break; |
| 3664 | } |
| 3665 | case MSR_STAR: |
| 3666 | msr_info->data = svm->vmcb->save.star; |
| 3667 | break; |
| 3668 | #ifdef CONFIG_X86_64 |
| 3669 | case MSR_LSTAR: |
| 3670 | msr_info->data = svm->vmcb->save.lstar; |
| 3671 | break; |
| 3672 | case MSR_CSTAR: |
| 3673 | msr_info->data = svm->vmcb->save.cstar; |
| 3674 | break; |
| 3675 | case MSR_KERNEL_GS_BASE: |
| 3676 | msr_info->data = svm->vmcb->save.kernel_gs_base; |
| 3677 | break; |
| 3678 | case MSR_SYSCALL_MASK: |
| 3679 | msr_info->data = svm->vmcb->save.sfmask; |
| 3680 | break; |
| 3681 | #endif |
| 3682 | case MSR_IA32_SYSENTER_CS: |
| 3683 | msr_info->data = svm->vmcb->save.sysenter_cs; |
| 3684 | break; |
| 3685 | case MSR_IA32_SYSENTER_EIP: |
| 3686 | msr_info->data = svm->sysenter_eip; |
| 3687 | break; |
| 3688 | case MSR_IA32_SYSENTER_ESP: |
| 3689 | msr_info->data = svm->sysenter_esp; |
| 3690 | break; |
| 3691 | case MSR_TSC_AUX: |
| 3692 | if (!boot_cpu_has(X86_FEATURE_RDTSCP)) |
| 3693 | return 1; |
| 3694 | msr_info->data = svm->tsc_aux; |
| 3695 | break; |
| 3696 | /* |
| 3697 | * Nobody will change the following 5 values in the VMCB so we can |
| 3698 | * safely return them on rdmsr. They will always be 0 until LBRV is |
| 3699 | * implemented. |
| 3700 | */ |
| 3701 | case MSR_IA32_DEBUGCTLMSR: |
| 3702 | msr_info->data = svm->vmcb->save.dbgctl; |
| 3703 | break; |
| 3704 | case MSR_IA32_LASTBRANCHFROMIP: |
| 3705 | msr_info->data = svm->vmcb->save.br_from; |
| 3706 | break; |
| 3707 | case MSR_IA32_LASTBRANCHTOIP: |
| 3708 | msr_info->data = svm->vmcb->save.br_to; |
| 3709 | break; |
| 3710 | case MSR_IA32_LASTINTFROMIP: |
| 3711 | msr_info->data = svm->vmcb->save.last_excp_from; |
| 3712 | break; |
| 3713 | case MSR_IA32_LASTINTTOIP: |
| 3714 | msr_info->data = svm->vmcb->save.last_excp_to; |
| 3715 | break; |
| 3716 | case MSR_VM_HSAVE_PA: |
| 3717 | msr_info->data = svm->nested.hsave_msr; |
| 3718 | break; |
| 3719 | case MSR_VM_CR: |
| 3720 | msr_info->data = svm->nested.vm_cr_msr; |
| 3721 | break; |
| 3722 | case MSR_IA32_SPEC_CTRL: |
| 3723 | if (!msr_info->host_initiated && |
| 3724 | !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS) && |
| 3725 | !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD)) |
| 3726 | return 1; |
| 3727 | |
| 3728 | msr_info->data = svm->spec_ctrl; |
| 3729 | break; |
| 3730 | case MSR_AMD64_VIRT_SPEC_CTRL: |
| 3731 | if (!msr_info->host_initiated && |
| 3732 | !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD)) |
| 3733 | return 1; |
| 3734 | |
| 3735 | msr_info->data = svm->virt_spec_ctrl; |
| 3736 | break; |
| 3737 | case MSR_F15H_IC_CFG: { |
| 3738 | |
| 3739 | int family, model; |
| 3740 | |
| 3741 | family = guest_cpuid_family(vcpu); |
| 3742 | model = guest_cpuid_model(vcpu); |
| 3743 | |
| 3744 | if (family < 0 || model < 0) |
| 3745 | return kvm_get_msr_common(vcpu, msr_info); |
| 3746 | |
| 3747 | msr_info->data = 0; |
| 3748 | |
| 3749 | if (family == 0x15 && |
| 3750 | (model >= 0x2 && model < 0x20)) |
| 3751 | msr_info->data = 0x1E; |
| 3752 | } |
| 3753 | break; |
| 3754 | case MSR_F10H_DECFG: |
| 3755 | msr_info->data = svm->msr_decfg; |
| 3756 | break; |
| 3757 | default: |
| 3758 | return kvm_get_msr_common(vcpu, msr_info); |
| 3759 | } |
| 3760 | return 0; |
| 3761 | } |
| 3762 | |
| 3763 | static int rdmsr_interception(struct vcpu_svm *svm) |
| 3764 | { |
| 3765 | u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX); |
| 3766 | struct msr_data msr_info; |
| 3767 | |
| 3768 | msr_info.index = ecx; |
| 3769 | msr_info.host_initiated = false; |
| 3770 | if (svm_get_msr(&svm->vcpu, &msr_info)) { |
| 3771 | trace_kvm_msr_read_ex(ecx); |
| 3772 | kvm_inject_gp(&svm->vcpu, 0); |
| 3773 | return 1; |
| 3774 | } else { |
| 3775 | trace_kvm_msr_read(ecx, msr_info.data); |
| 3776 | |
| 3777 | kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, |
| 3778 | msr_info.data & 0xffffffff); |
| 3779 | kvm_register_write(&svm->vcpu, VCPU_REGS_RDX, |
| 3780 | msr_info.data >> 32); |
| 3781 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 2; |
| 3782 | return kvm_skip_emulated_instruction(&svm->vcpu); |
| 3783 | } |
| 3784 | } |
| 3785 | |
| 3786 | static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data) |
| 3787 | { |
| 3788 | struct vcpu_svm *svm = to_svm(vcpu); |
| 3789 | int svm_dis, chg_mask; |
| 3790 | |
| 3791 | if (data & ~SVM_VM_CR_VALID_MASK) |
| 3792 | return 1; |
| 3793 | |
| 3794 | chg_mask = SVM_VM_CR_VALID_MASK; |
| 3795 | |
| 3796 | if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK) |
| 3797 | chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK); |
| 3798 | |
| 3799 | svm->nested.vm_cr_msr &= ~chg_mask; |
| 3800 | svm->nested.vm_cr_msr |= (data & chg_mask); |
| 3801 | |
| 3802 | svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK; |
| 3803 | |
| 3804 | /* check for svm_disable while efer.svme is set */ |
| 3805 | if (svm_dis && (vcpu->arch.efer & EFER_SVME)) |
| 3806 | return 1; |
| 3807 | |
| 3808 | return 0; |
| 3809 | } |
| 3810 | |
| 3811 | static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr) |
| 3812 | { |
| 3813 | struct vcpu_svm *svm = to_svm(vcpu); |
| 3814 | |
| 3815 | u32 ecx = msr->index; |
| 3816 | u64 data = msr->data; |
| 3817 | switch (ecx) { |
| 3818 | case MSR_IA32_CR_PAT: |
| 3819 | if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data)) |
| 3820 | return 1; |
| 3821 | vcpu->arch.pat = data; |
| 3822 | svm->vmcb->save.g_pat = data; |
| 3823 | mark_dirty(svm->vmcb, VMCB_NPT); |
| 3824 | break; |
| 3825 | case MSR_IA32_TSC: |
| 3826 | kvm_write_tsc(vcpu, msr); |
| 3827 | break; |
| 3828 | case MSR_IA32_SPEC_CTRL: |
| 3829 | if (!msr->host_initiated && |
| 3830 | !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS) && |
| 3831 | !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD)) |
| 3832 | return 1; |
| 3833 | |
| 3834 | /* The STIBP bit doesn't fault even if it's not advertised */ |
| 3835 | if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD)) |
| 3836 | return 1; |
| 3837 | |
| 3838 | svm->spec_ctrl = data; |
| 3839 | |
| 3840 | if (!data) |
| 3841 | break; |
| 3842 | |
| 3843 | /* |
| 3844 | * For non-nested: |
| 3845 | * When it's written (to non-zero) for the first time, pass |
| 3846 | * it through. |
| 3847 | * |
| 3848 | * For nested: |
| 3849 | * The handling of the MSR bitmap for L2 guests is done in |
| 3850 | * nested_svm_vmrun_msrpm. |
| 3851 | * We update the L1 MSR bit as well since it will end up |
| 3852 | * touching the MSR anyway now. |
| 3853 | */ |
| 3854 | set_msr_interception(svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1); |
| 3855 | break; |
| 3856 | case MSR_IA32_PRED_CMD: |
| 3857 | if (!msr->host_initiated && |
| 3858 | !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBPB)) |
| 3859 | return 1; |
| 3860 | |
| 3861 | if (data & ~PRED_CMD_IBPB) |
| 3862 | return 1; |
| 3863 | |
| 3864 | if (!data) |
| 3865 | break; |
| 3866 | |
| 3867 | wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB); |
| 3868 | if (is_guest_mode(vcpu)) |
| 3869 | break; |
| 3870 | set_msr_interception(svm->msrpm, MSR_IA32_PRED_CMD, 0, 1); |
| 3871 | break; |
| 3872 | case MSR_AMD64_VIRT_SPEC_CTRL: |
| 3873 | if (!msr->host_initiated && |
| 3874 | !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD)) |
| 3875 | return 1; |
| 3876 | |
| 3877 | if (data & ~SPEC_CTRL_SSBD) |
| 3878 | return 1; |
| 3879 | |
| 3880 | svm->virt_spec_ctrl = data; |
| 3881 | break; |
| 3882 | case MSR_STAR: |
| 3883 | svm->vmcb->save.star = data; |
| 3884 | break; |
| 3885 | #ifdef CONFIG_X86_64 |
| 3886 | case MSR_LSTAR: |
| 3887 | svm->vmcb->save.lstar = data; |
| 3888 | break; |
| 3889 | case MSR_CSTAR: |
| 3890 | svm->vmcb->save.cstar = data; |
| 3891 | break; |
| 3892 | case MSR_KERNEL_GS_BASE: |
| 3893 | svm->vmcb->save.kernel_gs_base = data; |
| 3894 | break; |
| 3895 | case MSR_SYSCALL_MASK: |
| 3896 | svm->vmcb->save.sfmask = data; |
| 3897 | break; |
| 3898 | #endif |
| 3899 | case MSR_IA32_SYSENTER_CS: |
| 3900 | svm->vmcb->save.sysenter_cs = data; |
| 3901 | break; |
| 3902 | case MSR_IA32_SYSENTER_EIP: |
| 3903 | svm->sysenter_eip = data; |
| 3904 | svm->vmcb->save.sysenter_eip = data; |
| 3905 | break; |
| 3906 | case MSR_IA32_SYSENTER_ESP: |
| 3907 | svm->sysenter_esp = data; |
| 3908 | svm->vmcb->save.sysenter_esp = data; |
| 3909 | break; |
| 3910 | case MSR_TSC_AUX: |
| 3911 | if (!boot_cpu_has(X86_FEATURE_RDTSCP)) |
| 3912 | return 1; |
| 3913 | |
| 3914 | /* |
| 3915 | * This is rare, so we update the MSR here instead of using |
| 3916 | * direct_access_msrs. Doing that would require a rdmsr in |
| 3917 | * svm_vcpu_put. |
| 3918 | */ |
| 3919 | svm->tsc_aux = data; |
| 3920 | wrmsrl(MSR_TSC_AUX, svm->tsc_aux); |
| 3921 | break; |
| 3922 | case MSR_IA32_DEBUGCTLMSR: |
| 3923 | if (!boot_cpu_has(X86_FEATURE_LBRV)) { |
| 3924 | vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n", |
| 3925 | __func__, data); |
| 3926 | break; |
| 3927 | } |
| 3928 | if (data & DEBUGCTL_RESERVED_BITS) |
| 3929 | return 1; |
| 3930 | |
| 3931 | svm->vmcb->save.dbgctl = data; |
| 3932 | mark_dirty(svm->vmcb, VMCB_LBR); |
| 3933 | if (data & (1ULL<<0)) |
| 3934 | svm_enable_lbrv(svm); |
| 3935 | else |
| 3936 | svm_disable_lbrv(svm); |
| 3937 | break; |
| 3938 | case MSR_VM_HSAVE_PA: |
| 3939 | svm->nested.hsave_msr = data; |
| 3940 | break; |
| 3941 | case MSR_VM_CR: |
| 3942 | return svm_set_vm_cr(vcpu, data); |
| 3943 | case MSR_VM_IGNNE: |
| 3944 | vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data); |
| 3945 | break; |
| 3946 | case MSR_F10H_DECFG: { |
| 3947 | struct kvm_msr_entry msr_entry; |
| 3948 | |
| 3949 | msr_entry.index = msr->index; |
| 3950 | if (svm_get_msr_feature(&msr_entry)) |
| 3951 | return 1; |
| 3952 | |
| 3953 | /* Check the supported bits */ |
| 3954 | if (data & ~msr_entry.data) |
| 3955 | return 1; |
| 3956 | |
| 3957 | /* Don't allow the guest to change a bit, #GP */ |
| 3958 | if (!msr->host_initiated && (data ^ msr_entry.data)) |
| 3959 | return 1; |
| 3960 | |
| 3961 | svm->msr_decfg = data; |
| 3962 | break; |
| 3963 | } |
| 3964 | case MSR_IA32_APICBASE: |
| 3965 | if (kvm_vcpu_apicv_active(vcpu)) |
| 3966 | avic_update_vapic_bar(to_svm(vcpu), data); |
| 3967 | /* Follow through */ |
| 3968 | default: |
| 3969 | return kvm_set_msr_common(vcpu, msr); |
| 3970 | } |
| 3971 | return 0; |
| 3972 | } |
| 3973 | |
| 3974 | static int wrmsr_interception(struct vcpu_svm *svm) |
| 3975 | { |
| 3976 | struct msr_data msr; |
| 3977 | u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX); |
| 3978 | u64 data = kvm_read_edx_eax(&svm->vcpu); |
| 3979 | |
| 3980 | msr.data = data; |
| 3981 | msr.index = ecx; |
| 3982 | msr.host_initiated = false; |
| 3983 | |
| 3984 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 2; |
| 3985 | if (kvm_set_msr(&svm->vcpu, &msr)) { |
| 3986 | trace_kvm_msr_write_ex(ecx, data); |
| 3987 | kvm_inject_gp(&svm->vcpu, 0); |
| 3988 | return 1; |
| 3989 | } else { |
| 3990 | trace_kvm_msr_write(ecx, data); |
| 3991 | return kvm_skip_emulated_instruction(&svm->vcpu); |
| 3992 | } |
| 3993 | } |
| 3994 | |
| 3995 | static int msr_interception(struct vcpu_svm *svm) |
| 3996 | { |
| 3997 | if (svm->vmcb->control.exit_info_1) |
| 3998 | return wrmsr_interception(svm); |
| 3999 | else |
| 4000 | return rdmsr_interception(svm); |
| 4001 | } |
| 4002 | |
| 4003 | static int interrupt_window_interception(struct vcpu_svm *svm) |
| 4004 | { |
| 4005 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
| 4006 | svm_clear_vintr(svm); |
| 4007 | svm->vmcb->control.int_ctl &= ~V_IRQ_MASK; |
| 4008 | mark_dirty(svm->vmcb, VMCB_INTR); |
| 4009 | ++svm->vcpu.stat.irq_window_exits; |
| 4010 | return 1; |
| 4011 | } |
| 4012 | |
| 4013 | static int pause_interception(struct vcpu_svm *svm) |
| 4014 | { |
| 4015 | struct kvm_vcpu *vcpu = &svm->vcpu; |
| 4016 | bool in_kernel = (svm_get_cpl(vcpu) == 0); |
| 4017 | |
| 4018 | kvm_vcpu_on_spin(vcpu, in_kernel); |
| 4019 | return 1; |
| 4020 | } |
| 4021 | |
| 4022 | static int nop_interception(struct vcpu_svm *svm) |
| 4023 | { |
| 4024 | return kvm_skip_emulated_instruction(&(svm->vcpu)); |
| 4025 | } |
| 4026 | |
| 4027 | static int monitor_interception(struct vcpu_svm *svm) |
| 4028 | { |
| 4029 | printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n"); |
| 4030 | return nop_interception(svm); |
| 4031 | } |
| 4032 | |
| 4033 | static int mwait_interception(struct vcpu_svm *svm) |
| 4034 | { |
| 4035 | printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n"); |
| 4036 | return nop_interception(svm); |
| 4037 | } |
| 4038 | |
| 4039 | enum avic_ipi_failure_cause { |
| 4040 | AVIC_IPI_FAILURE_INVALID_INT_TYPE, |
| 4041 | AVIC_IPI_FAILURE_TARGET_NOT_RUNNING, |
| 4042 | AVIC_IPI_FAILURE_INVALID_TARGET, |
| 4043 | AVIC_IPI_FAILURE_INVALID_BACKING_PAGE, |
| 4044 | }; |
| 4045 | |
| 4046 | static int avic_incomplete_ipi_interception(struct vcpu_svm *svm) |
| 4047 | { |
| 4048 | u32 icrh = svm->vmcb->control.exit_info_1 >> 32; |
| 4049 | u32 icrl = svm->vmcb->control.exit_info_1; |
| 4050 | u32 id = svm->vmcb->control.exit_info_2 >> 32; |
| 4051 | u32 index = svm->vmcb->control.exit_info_2 & 0xFF; |
| 4052 | struct kvm_lapic *apic = svm->vcpu.arch.apic; |
| 4053 | |
| 4054 | trace_kvm_avic_incomplete_ipi(svm->vcpu.vcpu_id, icrh, icrl, id, index); |
| 4055 | |
| 4056 | switch (id) { |
| 4057 | case AVIC_IPI_FAILURE_INVALID_INT_TYPE: |
| 4058 | /* |
| 4059 | * AVIC hardware handles the generation of |
| 4060 | * IPIs when the specified Message Type is Fixed |
| 4061 | * (also known as fixed delivery mode) and |
| 4062 | * the Trigger Mode is edge-triggered. The hardware |
| 4063 | * also supports self and broadcast delivery modes |
| 4064 | * specified via the Destination Shorthand(DSH) |
| 4065 | * field of the ICRL. Logical and physical APIC ID |
| 4066 | * formats are supported. All other IPI types cause |
| 4067 | * a #VMEXIT, which needs to emulated. |
| 4068 | */ |
| 4069 | kvm_lapic_reg_write(apic, APIC_ICR2, icrh); |
| 4070 | kvm_lapic_reg_write(apic, APIC_ICR, icrl); |
| 4071 | break; |
| 4072 | case AVIC_IPI_FAILURE_TARGET_NOT_RUNNING: { |
| 4073 | int i; |
| 4074 | struct kvm_vcpu *vcpu; |
| 4075 | struct kvm *kvm = svm->vcpu.kvm; |
| 4076 | struct kvm_lapic *apic = svm->vcpu.arch.apic; |
| 4077 | |
| 4078 | /* |
| 4079 | * At this point, we expect that the AVIC HW has already |
| 4080 | * set the appropriate IRR bits on the valid target |
| 4081 | * vcpus. So, we just need to kick the appropriate vcpu. |
| 4082 | */ |
| 4083 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 4084 | bool m = kvm_apic_match_dest(vcpu, apic, |
| 4085 | icrl & KVM_APIC_SHORT_MASK, |
| 4086 | GET_APIC_DEST_FIELD(icrh), |
| 4087 | icrl & KVM_APIC_DEST_MASK); |
| 4088 | |
| 4089 | if (m && !avic_vcpu_is_running(vcpu)) |
| 4090 | kvm_vcpu_wake_up(vcpu); |
| 4091 | } |
| 4092 | break; |
| 4093 | } |
| 4094 | case AVIC_IPI_FAILURE_INVALID_TARGET: |
| 4095 | break; |
| 4096 | case AVIC_IPI_FAILURE_INVALID_BACKING_PAGE: |
| 4097 | WARN_ONCE(1, "Invalid backing page\n"); |
| 4098 | break; |
| 4099 | default: |
| 4100 | pr_err("Unknown IPI interception\n"); |
| 4101 | } |
| 4102 | |
| 4103 | return 1; |
| 4104 | } |
| 4105 | |
| 4106 | static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat) |
| 4107 | { |
| 4108 | struct kvm_arch *vm_data = &vcpu->kvm->arch; |
| 4109 | int index; |
| 4110 | u32 *logical_apic_id_table; |
| 4111 | int dlid = GET_APIC_LOGICAL_ID(ldr); |
| 4112 | |
| 4113 | if (!dlid) |
| 4114 | return NULL; |
| 4115 | |
| 4116 | if (flat) { /* flat */ |
| 4117 | index = ffs(dlid) - 1; |
| 4118 | if (index > 7) |
| 4119 | return NULL; |
| 4120 | } else { /* cluster */ |
| 4121 | int cluster = (dlid & 0xf0) >> 4; |
| 4122 | int apic = ffs(dlid & 0x0f) - 1; |
| 4123 | |
| 4124 | if ((apic < 0) || (apic > 7) || |
| 4125 | (cluster >= 0xf)) |
| 4126 | return NULL; |
| 4127 | index = (cluster << 2) + apic; |
| 4128 | } |
| 4129 | |
| 4130 | logical_apic_id_table = (u32 *) page_address(vm_data->avic_logical_id_table_page); |
| 4131 | |
| 4132 | return &logical_apic_id_table[index]; |
| 4133 | } |
| 4134 | |
| 4135 | static int avic_ldr_write(struct kvm_vcpu *vcpu, u8 g_physical_id, u32 ldr, |
| 4136 | bool valid) |
| 4137 | { |
| 4138 | bool flat; |
| 4139 | u32 *entry, new_entry; |
| 4140 | |
| 4141 | flat = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR) == APIC_DFR_FLAT; |
| 4142 | entry = avic_get_logical_id_entry(vcpu, ldr, flat); |
| 4143 | if (!entry) |
| 4144 | return -EINVAL; |
| 4145 | |
| 4146 | new_entry = READ_ONCE(*entry); |
| 4147 | new_entry &= ~AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK; |
| 4148 | new_entry |= (g_physical_id & AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK); |
| 4149 | if (valid) |
| 4150 | new_entry |= AVIC_LOGICAL_ID_ENTRY_VALID_MASK; |
| 4151 | else |
| 4152 | new_entry &= ~AVIC_LOGICAL_ID_ENTRY_VALID_MASK; |
| 4153 | WRITE_ONCE(*entry, new_entry); |
| 4154 | |
| 4155 | return 0; |
| 4156 | } |
| 4157 | |
| 4158 | static int avic_handle_ldr_update(struct kvm_vcpu *vcpu) |
| 4159 | { |
| 4160 | int ret; |
| 4161 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4162 | u32 ldr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LDR); |
| 4163 | |
| 4164 | if (!ldr) |
| 4165 | return 1; |
| 4166 | |
| 4167 | ret = avic_ldr_write(vcpu, vcpu->vcpu_id, ldr, true); |
| 4168 | if (ret && svm->ldr_reg) { |
| 4169 | avic_ldr_write(vcpu, 0, svm->ldr_reg, false); |
| 4170 | svm->ldr_reg = 0; |
| 4171 | } else { |
| 4172 | svm->ldr_reg = ldr; |
| 4173 | } |
| 4174 | return ret; |
| 4175 | } |
| 4176 | |
| 4177 | static int avic_handle_apic_id_update(struct kvm_vcpu *vcpu) |
| 4178 | { |
| 4179 | u64 *old, *new; |
| 4180 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4181 | u32 apic_id_reg = kvm_lapic_get_reg(vcpu->arch.apic, APIC_ID); |
| 4182 | u32 id = (apic_id_reg >> 24) & 0xff; |
| 4183 | |
| 4184 | if (vcpu->vcpu_id == id) |
| 4185 | return 0; |
| 4186 | |
| 4187 | old = avic_get_physical_id_entry(vcpu, vcpu->vcpu_id); |
| 4188 | new = avic_get_physical_id_entry(vcpu, id); |
| 4189 | if (!new || !old) |
| 4190 | return 1; |
| 4191 | |
| 4192 | /* We need to move physical_id_entry to new offset */ |
| 4193 | *new = *old; |
| 4194 | *old = 0ULL; |
| 4195 | to_svm(vcpu)->avic_physical_id_cache = new; |
| 4196 | |
| 4197 | /* |
| 4198 | * Also update the guest physical APIC ID in the logical |
| 4199 | * APIC ID table entry if already setup the LDR. |
| 4200 | */ |
| 4201 | if (svm->ldr_reg) |
| 4202 | avic_handle_ldr_update(vcpu); |
| 4203 | |
| 4204 | return 0; |
| 4205 | } |
| 4206 | |
| 4207 | static int avic_handle_dfr_update(struct kvm_vcpu *vcpu) |
| 4208 | { |
| 4209 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4210 | struct kvm_arch *vm_data = &vcpu->kvm->arch; |
| 4211 | u32 dfr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR); |
| 4212 | u32 mod = (dfr >> 28) & 0xf; |
| 4213 | |
| 4214 | /* |
| 4215 | * We assume that all local APICs are using the same type. |
| 4216 | * If this changes, we need to flush the AVIC logical |
| 4217 | * APID id table. |
| 4218 | */ |
| 4219 | if (vm_data->ldr_mode == mod) |
| 4220 | return 0; |
| 4221 | |
| 4222 | clear_page(page_address(vm_data->avic_logical_id_table_page)); |
| 4223 | vm_data->ldr_mode = mod; |
| 4224 | |
| 4225 | if (svm->ldr_reg) |
| 4226 | avic_handle_ldr_update(vcpu); |
| 4227 | return 0; |
| 4228 | } |
| 4229 | |
| 4230 | static int avic_unaccel_trap_write(struct vcpu_svm *svm) |
| 4231 | { |
| 4232 | struct kvm_lapic *apic = svm->vcpu.arch.apic; |
| 4233 | u32 offset = svm->vmcb->control.exit_info_1 & |
| 4234 | AVIC_UNACCEL_ACCESS_OFFSET_MASK; |
| 4235 | |
| 4236 | switch (offset) { |
| 4237 | case APIC_ID: |
| 4238 | if (avic_handle_apic_id_update(&svm->vcpu)) |
| 4239 | return 0; |
| 4240 | break; |
| 4241 | case APIC_LDR: |
| 4242 | if (avic_handle_ldr_update(&svm->vcpu)) |
| 4243 | return 0; |
| 4244 | break; |
| 4245 | case APIC_DFR: |
| 4246 | avic_handle_dfr_update(&svm->vcpu); |
| 4247 | break; |
| 4248 | default: |
| 4249 | break; |
| 4250 | } |
| 4251 | |
| 4252 | kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset)); |
| 4253 | |
| 4254 | return 1; |
| 4255 | } |
| 4256 | |
| 4257 | static bool is_avic_unaccelerated_access_trap(u32 offset) |
| 4258 | { |
| 4259 | bool ret = false; |
| 4260 | |
| 4261 | switch (offset) { |
| 4262 | case APIC_ID: |
| 4263 | case APIC_EOI: |
| 4264 | case APIC_RRR: |
| 4265 | case APIC_LDR: |
| 4266 | case APIC_DFR: |
| 4267 | case APIC_SPIV: |
| 4268 | case APIC_ESR: |
| 4269 | case APIC_ICR: |
| 4270 | case APIC_LVTT: |
| 4271 | case APIC_LVTTHMR: |
| 4272 | case APIC_LVTPC: |
| 4273 | case APIC_LVT0: |
| 4274 | case APIC_LVT1: |
| 4275 | case APIC_LVTERR: |
| 4276 | case APIC_TMICT: |
| 4277 | case APIC_TDCR: |
| 4278 | ret = true; |
| 4279 | break; |
| 4280 | default: |
| 4281 | break; |
| 4282 | } |
| 4283 | return ret; |
| 4284 | } |
| 4285 | |
| 4286 | static int avic_unaccelerated_access_interception(struct vcpu_svm *svm) |
| 4287 | { |
| 4288 | int ret = 0; |
| 4289 | u32 offset = svm->vmcb->control.exit_info_1 & |
| 4290 | AVIC_UNACCEL_ACCESS_OFFSET_MASK; |
| 4291 | u32 vector = svm->vmcb->control.exit_info_2 & |
| 4292 | AVIC_UNACCEL_ACCESS_VECTOR_MASK; |
| 4293 | bool write = (svm->vmcb->control.exit_info_1 >> 32) & |
| 4294 | AVIC_UNACCEL_ACCESS_WRITE_MASK; |
| 4295 | bool trap = is_avic_unaccelerated_access_trap(offset); |
| 4296 | |
| 4297 | trace_kvm_avic_unaccelerated_access(svm->vcpu.vcpu_id, offset, |
| 4298 | trap, write, vector); |
| 4299 | if (trap) { |
| 4300 | /* Handling Trap */ |
| 4301 | WARN_ONCE(!write, "svm: Handling trap read.\n"); |
| 4302 | ret = avic_unaccel_trap_write(svm); |
| 4303 | } else { |
| 4304 | /* Handling Fault */ |
| 4305 | ret = (emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE); |
| 4306 | } |
| 4307 | |
| 4308 | return ret; |
| 4309 | } |
| 4310 | |
| 4311 | static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = { |
| 4312 | [SVM_EXIT_READ_CR0] = cr_interception, |
| 4313 | [SVM_EXIT_READ_CR3] = cr_interception, |
| 4314 | [SVM_EXIT_READ_CR4] = cr_interception, |
| 4315 | [SVM_EXIT_READ_CR8] = cr_interception, |
| 4316 | [SVM_EXIT_CR0_SEL_WRITE] = cr_interception, |
| 4317 | [SVM_EXIT_WRITE_CR0] = cr_interception, |
| 4318 | [SVM_EXIT_WRITE_CR3] = cr_interception, |
| 4319 | [SVM_EXIT_WRITE_CR4] = cr_interception, |
| 4320 | [SVM_EXIT_WRITE_CR8] = cr8_write_interception, |
| 4321 | [SVM_EXIT_READ_DR0] = dr_interception, |
| 4322 | [SVM_EXIT_READ_DR1] = dr_interception, |
| 4323 | [SVM_EXIT_READ_DR2] = dr_interception, |
| 4324 | [SVM_EXIT_READ_DR3] = dr_interception, |
| 4325 | [SVM_EXIT_READ_DR4] = dr_interception, |
| 4326 | [SVM_EXIT_READ_DR5] = dr_interception, |
| 4327 | [SVM_EXIT_READ_DR6] = dr_interception, |
| 4328 | [SVM_EXIT_READ_DR7] = dr_interception, |
| 4329 | [SVM_EXIT_WRITE_DR0] = dr_interception, |
| 4330 | [SVM_EXIT_WRITE_DR1] = dr_interception, |
| 4331 | [SVM_EXIT_WRITE_DR2] = dr_interception, |
| 4332 | [SVM_EXIT_WRITE_DR3] = dr_interception, |
| 4333 | [SVM_EXIT_WRITE_DR4] = dr_interception, |
| 4334 | [SVM_EXIT_WRITE_DR5] = dr_interception, |
| 4335 | [SVM_EXIT_WRITE_DR6] = dr_interception, |
| 4336 | [SVM_EXIT_WRITE_DR7] = dr_interception, |
| 4337 | [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception, |
| 4338 | [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception, |
| 4339 | [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception, |
| 4340 | [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception, |
| 4341 | [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception, |
| 4342 | [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception, |
| 4343 | [SVM_EXIT_INTR] = intr_interception, |
| 4344 | [SVM_EXIT_NMI] = nmi_interception, |
| 4345 | [SVM_EXIT_SMI] = nop_on_interception, |
| 4346 | [SVM_EXIT_INIT] = nop_on_interception, |
| 4347 | [SVM_EXIT_VINTR] = interrupt_window_interception, |
| 4348 | [SVM_EXIT_RDPMC] = rdpmc_interception, |
| 4349 | [SVM_EXIT_CPUID] = cpuid_interception, |
| 4350 | [SVM_EXIT_IRET] = iret_interception, |
| 4351 | [SVM_EXIT_INVD] = emulate_on_interception, |
| 4352 | [SVM_EXIT_PAUSE] = pause_interception, |
| 4353 | [SVM_EXIT_HLT] = halt_interception, |
| 4354 | [SVM_EXIT_INVLPG] = invlpg_interception, |
| 4355 | [SVM_EXIT_INVLPGA] = invlpga_interception, |
| 4356 | [SVM_EXIT_IOIO] = io_interception, |
| 4357 | [SVM_EXIT_MSR] = msr_interception, |
| 4358 | [SVM_EXIT_TASK_SWITCH] = task_switch_interception, |
| 4359 | [SVM_EXIT_SHUTDOWN] = shutdown_interception, |
| 4360 | [SVM_EXIT_VMRUN] = vmrun_interception, |
| 4361 | [SVM_EXIT_VMMCALL] = vmmcall_interception, |
| 4362 | [SVM_EXIT_VMLOAD] = vmload_interception, |
| 4363 | [SVM_EXIT_VMSAVE] = vmsave_interception, |
| 4364 | [SVM_EXIT_STGI] = stgi_interception, |
| 4365 | [SVM_EXIT_CLGI] = clgi_interception, |
| 4366 | [SVM_EXIT_SKINIT] = skinit_interception, |
| 4367 | [SVM_EXIT_WBINVD] = wbinvd_interception, |
| 4368 | [SVM_EXIT_MONITOR] = monitor_interception, |
| 4369 | [SVM_EXIT_MWAIT] = mwait_interception, |
| 4370 | [SVM_EXIT_XSETBV] = xsetbv_interception, |
| 4371 | [SVM_EXIT_NPF] = pf_interception, |
| 4372 | [SVM_EXIT_RSM] = emulate_on_interception, |
| 4373 | [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception, |
| 4374 | [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception, |
| 4375 | }; |
| 4376 | |
| 4377 | static void dump_vmcb(struct kvm_vcpu *vcpu) |
| 4378 | { |
| 4379 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4380 | struct vmcb_control_area *control = &svm->vmcb->control; |
| 4381 | struct vmcb_save_area *save = &svm->vmcb->save; |
| 4382 | |
| 4383 | pr_err("VMCB Control Area:\n"); |
| 4384 | pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff); |
| 4385 | pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16); |
| 4386 | pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff); |
| 4387 | pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16); |
| 4388 | pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions); |
| 4389 | pr_err("%-20s%016llx\n", "intercepts:", control->intercept); |
| 4390 | pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count); |
| 4391 | pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa); |
| 4392 | pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa); |
| 4393 | pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset); |
| 4394 | pr_err("%-20s%d\n", "asid:", control->asid); |
| 4395 | pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl); |
| 4396 | pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl); |
| 4397 | pr_err("%-20s%08x\n", "int_vector:", control->int_vector); |
| 4398 | pr_err("%-20s%08x\n", "int_state:", control->int_state); |
| 4399 | pr_err("%-20s%08x\n", "exit_code:", control->exit_code); |
| 4400 | pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1); |
| 4401 | pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2); |
| 4402 | pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info); |
| 4403 | pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err); |
| 4404 | pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl); |
| 4405 | pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3); |
| 4406 | pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar); |
| 4407 | pr_err("%-20s%08x\n", "event_inj:", control->event_inj); |
| 4408 | pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err); |
| 4409 | pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext); |
| 4410 | pr_err("%-20s%016llx\n", "next_rip:", control->next_rip); |
| 4411 | pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page); |
| 4412 | pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id); |
| 4413 | pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id); |
| 4414 | pr_err("VMCB State Save Area:\n"); |
| 4415 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4416 | "es:", |
| 4417 | save->es.selector, save->es.attrib, |
| 4418 | save->es.limit, save->es.base); |
| 4419 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4420 | "cs:", |
| 4421 | save->cs.selector, save->cs.attrib, |
| 4422 | save->cs.limit, save->cs.base); |
| 4423 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4424 | "ss:", |
| 4425 | save->ss.selector, save->ss.attrib, |
| 4426 | save->ss.limit, save->ss.base); |
| 4427 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4428 | "ds:", |
| 4429 | save->ds.selector, save->ds.attrib, |
| 4430 | save->ds.limit, save->ds.base); |
| 4431 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4432 | "fs:", |
| 4433 | save->fs.selector, save->fs.attrib, |
| 4434 | save->fs.limit, save->fs.base); |
| 4435 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4436 | "gs:", |
| 4437 | save->gs.selector, save->gs.attrib, |
| 4438 | save->gs.limit, save->gs.base); |
| 4439 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4440 | "gdtr:", |
| 4441 | save->gdtr.selector, save->gdtr.attrib, |
| 4442 | save->gdtr.limit, save->gdtr.base); |
| 4443 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4444 | "ldtr:", |
| 4445 | save->ldtr.selector, save->ldtr.attrib, |
| 4446 | save->ldtr.limit, save->ldtr.base); |
| 4447 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4448 | "idtr:", |
| 4449 | save->idtr.selector, save->idtr.attrib, |
| 4450 | save->idtr.limit, save->idtr.base); |
| 4451 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4452 | "tr:", |
| 4453 | save->tr.selector, save->tr.attrib, |
| 4454 | save->tr.limit, save->tr.base); |
| 4455 | pr_err("cpl: %d efer: %016llx\n", |
| 4456 | save->cpl, save->efer); |
| 4457 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4458 | "cr0:", save->cr0, "cr2:", save->cr2); |
| 4459 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4460 | "cr3:", save->cr3, "cr4:", save->cr4); |
| 4461 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4462 | "dr6:", save->dr6, "dr7:", save->dr7); |
| 4463 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4464 | "rip:", save->rip, "rflags:", save->rflags); |
| 4465 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4466 | "rsp:", save->rsp, "rax:", save->rax); |
| 4467 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4468 | "star:", save->star, "lstar:", save->lstar); |
| 4469 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4470 | "cstar:", save->cstar, "sfmask:", save->sfmask); |
| 4471 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4472 | "kernel_gs_base:", save->kernel_gs_base, |
| 4473 | "sysenter_cs:", save->sysenter_cs); |
| 4474 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4475 | "sysenter_esp:", save->sysenter_esp, |
| 4476 | "sysenter_eip:", save->sysenter_eip); |
| 4477 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4478 | "gpat:", save->g_pat, "dbgctl:", save->dbgctl); |
| 4479 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4480 | "br_from:", save->br_from, "br_to:", save->br_to); |
| 4481 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4482 | "excp_from:", save->last_excp_from, |
| 4483 | "excp_to:", save->last_excp_to); |
| 4484 | } |
| 4485 | |
| 4486 | static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
| 4487 | { |
| 4488 | struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control; |
| 4489 | |
| 4490 | *info1 = control->exit_info_1; |
| 4491 | *info2 = control->exit_info_2; |
| 4492 | } |
| 4493 | |
| 4494 | static int handle_exit(struct kvm_vcpu *vcpu) |
| 4495 | { |
| 4496 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4497 | struct kvm_run *kvm_run = vcpu->run; |
| 4498 | u32 exit_code = svm->vmcb->control.exit_code; |
| 4499 | |
| 4500 | trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM); |
| 4501 | |
| 4502 | if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE)) |
| 4503 | vcpu->arch.cr0 = svm->vmcb->save.cr0; |
| 4504 | if (npt_enabled) |
| 4505 | vcpu->arch.cr3 = svm->vmcb->save.cr3; |
| 4506 | |
| 4507 | if (unlikely(svm->nested.exit_required)) { |
| 4508 | nested_svm_vmexit(svm); |
| 4509 | svm->nested.exit_required = false; |
| 4510 | |
| 4511 | return 1; |
| 4512 | } |
| 4513 | |
| 4514 | if (is_guest_mode(vcpu)) { |
| 4515 | int vmexit; |
| 4516 | |
| 4517 | trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code, |
| 4518 | svm->vmcb->control.exit_info_1, |
| 4519 | svm->vmcb->control.exit_info_2, |
| 4520 | svm->vmcb->control.exit_int_info, |
| 4521 | svm->vmcb->control.exit_int_info_err, |
| 4522 | KVM_ISA_SVM); |
| 4523 | |
| 4524 | vmexit = nested_svm_exit_special(svm); |
| 4525 | |
| 4526 | if (vmexit == NESTED_EXIT_CONTINUE) |
| 4527 | vmexit = nested_svm_exit_handled(svm); |
| 4528 | |
| 4529 | if (vmexit == NESTED_EXIT_DONE) |
| 4530 | return 1; |
| 4531 | } |
| 4532 | |
| 4533 | svm_complete_interrupts(svm); |
| 4534 | |
| 4535 | if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) { |
| 4536 | kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 4537 | kvm_run->fail_entry.hardware_entry_failure_reason |
| 4538 | = svm->vmcb->control.exit_code; |
| 4539 | pr_err("KVM: FAILED VMRUN WITH VMCB:\n"); |
| 4540 | dump_vmcb(vcpu); |
| 4541 | return 0; |
| 4542 | } |
| 4543 | |
| 4544 | if (is_external_interrupt(svm->vmcb->control.exit_int_info) && |
| 4545 | exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR && |
| 4546 | exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH && |
| 4547 | exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI) |
| 4548 | printk(KERN_ERR "%s: unexpected exit_int_info 0x%x " |
| 4549 | "exit_code 0x%x\n", |
| 4550 | __func__, svm->vmcb->control.exit_int_info, |
| 4551 | exit_code); |
| 4552 | |
| 4553 | if (exit_code >= ARRAY_SIZE(svm_exit_handlers) |
| 4554 | || !svm_exit_handlers[exit_code]) { |
| 4555 | WARN_ONCE(1, "svm: unexpected exit reason 0x%x\n", exit_code); |
| 4556 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4557 | return 1; |
| 4558 | } |
| 4559 | |
| 4560 | return svm_exit_handlers[exit_code](svm); |
| 4561 | } |
| 4562 | |
| 4563 | static void reload_tss(struct kvm_vcpu *vcpu) |
| 4564 | { |
| 4565 | int cpu = raw_smp_processor_id(); |
| 4566 | |
| 4567 | struct svm_cpu_data *sd = per_cpu(svm_data, cpu); |
| 4568 | sd->tss_desc->type = 9; /* available 32/64-bit TSS */ |
| 4569 | load_TR_desc(); |
| 4570 | } |
| 4571 | |
| 4572 | static void pre_svm_run(struct vcpu_svm *svm) |
| 4573 | { |
| 4574 | int cpu = raw_smp_processor_id(); |
| 4575 | |
| 4576 | struct svm_cpu_data *sd = per_cpu(svm_data, cpu); |
| 4577 | |
| 4578 | /* FIXME: handle wraparound of asid_generation */ |
| 4579 | if (svm->asid_generation != sd->asid_generation) |
| 4580 | new_asid(svm, sd); |
| 4581 | } |
| 4582 | |
| 4583 | static void svm_inject_nmi(struct kvm_vcpu *vcpu) |
| 4584 | { |
| 4585 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4586 | |
| 4587 | svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI; |
| 4588 | vcpu->arch.hflags |= HF_NMI_MASK; |
| 4589 | set_intercept(svm, INTERCEPT_IRET); |
| 4590 | ++vcpu->stat.nmi_injections; |
| 4591 | } |
| 4592 | |
| 4593 | static inline void svm_inject_irq(struct vcpu_svm *svm, int irq) |
| 4594 | { |
| 4595 | struct vmcb_control_area *control; |
| 4596 | |
| 4597 | /* The following fields are ignored when AVIC is enabled */ |
| 4598 | control = &svm->vmcb->control; |
| 4599 | control->int_vector = irq; |
| 4600 | control->int_ctl &= ~V_INTR_PRIO_MASK; |
| 4601 | control->int_ctl |= V_IRQ_MASK | |
| 4602 | ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT); |
| 4603 | mark_dirty(svm->vmcb, VMCB_INTR); |
| 4604 | } |
| 4605 | |
| 4606 | static void svm_set_irq(struct kvm_vcpu *vcpu) |
| 4607 | { |
| 4608 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4609 | |
| 4610 | BUG_ON(!(gif_set(svm))); |
| 4611 | |
| 4612 | trace_kvm_inj_virq(vcpu->arch.interrupt.nr); |
| 4613 | ++vcpu->stat.irq_injections; |
| 4614 | |
| 4615 | svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr | |
| 4616 | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR; |
| 4617 | } |
| 4618 | |
| 4619 | static inline bool svm_nested_virtualize_tpr(struct kvm_vcpu *vcpu) |
| 4620 | { |
| 4621 | return is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK); |
| 4622 | } |
| 4623 | |
| 4624 | static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr) |
| 4625 | { |
| 4626 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4627 | |
| 4628 | if (svm_nested_virtualize_tpr(vcpu) || |
| 4629 | kvm_vcpu_apicv_active(vcpu)) |
| 4630 | return; |
| 4631 | |
| 4632 | clr_cr_intercept(svm, INTERCEPT_CR8_WRITE); |
| 4633 | |
| 4634 | if (irr == -1) |
| 4635 | return; |
| 4636 | |
| 4637 | if (tpr >= irr) |
| 4638 | set_cr_intercept(svm, INTERCEPT_CR8_WRITE); |
| 4639 | } |
| 4640 | |
| 4641 | static void svm_set_virtual_apic_mode(struct kvm_vcpu *vcpu) |
| 4642 | { |
| 4643 | return; |
| 4644 | } |
| 4645 | |
| 4646 | static bool svm_get_enable_apicv(struct kvm_vcpu *vcpu) |
| 4647 | { |
| 4648 | return avic && irqchip_split(vcpu->kvm); |
| 4649 | } |
| 4650 | |
| 4651 | static void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr) |
| 4652 | { |
| 4653 | } |
| 4654 | |
| 4655 | static void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr) |
| 4656 | { |
| 4657 | } |
| 4658 | |
| 4659 | /* Note: Currently only used by Hyper-V. */ |
| 4660 | static void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) |
| 4661 | { |
| 4662 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4663 | struct vmcb *vmcb = svm->vmcb; |
| 4664 | |
| 4665 | if (!kvm_vcpu_apicv_active(&svm->vcpu)) |
| 4666 | return; |
| 4667 | |
| 4668 | vmcb->control.int_ctl &= ~AVIC_ENABLE_MASK; |
| 4669 | mark_dirty(vmcb, VMCB_INTR); |
| 4670 | } |
| 4671 | |
| 4672 | static void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap) |
| 4673 | { |
| 4674 | return; |
| 4675 | } |
| 4676 | |
| 4677 | static int svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec) |
| 4678 | { |
| 4679 | if (!vcpu->arch.apicv_active) |
| 4680 | return -1; |
| 4681 | |
| 4682 | kvm_lapic_set_irr(vec, vcpu->arch.apic); |
| 4683 | smp_mb__after_atomic(); |
| 4684 | |
| 4685 | if (avic_vcpu_is_running(vcpu)) |
| 4686 | wrmsrl(SVM_AVIC_DOORBELL, |
| 4687 | kvm_cpu_get_apicid(vcpu->cpu)); |
| 4688 | else |
| 4689 | kvm_vcpu_wake_up(vcpu); |
| 4690 | |
| 4691 | return 0; |
| 4692 | } |
| 4693 | |
| 4694 | static bool svm_dy_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu) |
| 4695 | { |
| 4696 | return false; |
| 4697 | } |
| 4698 | |
| 4699 | static void svm_ir_list_del(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi) |
| 4700 | { |
| 4701 | unsigned long flags; |
| 4702 | struct amd_svm_iommu_ir *cur; |
| 4703 | |
| 4704 | spin_lock_irqsave(&svm->ir_list_lock, flags); |
| 4705 | list_for_each_entry(cur, &svm->ir_list, node) { |
| 4706 | if (cur->data != pi->ir_data) |
| 4707 | continue; |
| 4708 | list_del(&cur->node); |
| 4709 | kfree(cur); |
| 4710 | break; |
| 4711 | } |
| 4712 | spin_unlock_irqrestore(&svm->ir_list_lock, flags); |
| 4713 | } |
| 4714 | |
| 4715 | static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi) |
| 4716 | { |
| 4717 | int ret = 0; |
| 4718 | unsigned long flags; |
| 4719 | struct amd_svm_iommu_ir *ir; |
| 4720 | |
| 4721 | /** |
| 4722 | * In some cases, the existing irte is updaed and re-set, |
| 4723 | * so we need to check here if it's already been * added |
| 4724 | * to the ir_list. |
| 4725 | */ |
| 4726 | if (pi->ir_data && (pi->prev_ga_tag != 0)) { |
| 4727 | struct kvm *kvm = svm->vcpu.kvm; |
| 4728 | u32 vcpu_id = AVIC_GATAG_TO_VCPUID(pi->prev_ga_tag); |
| 4729 | struct kvm_vcpu *prev_vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id); |
| 4730 | struct vcpu_svm *prev_svm; |
| 4731 | |
| 4732 | if (!prev_vcpu) { |
| 4733 | ret = -EINVAL; |
| 4734 | goto out; |
| 4735 | } |
| 4736 | |
| 4737 | prev_svm = to_svm(prev_vcpu); |
| 4738 | svm_ir_list_del(prev_svm, pi); |
| 4739 | } |
| 4740 | |
| 4741 | /** |
| 4742 | * Allocating new amd_iommu_pi_data, which will get |
| 4743 | * add to the per-vcpu ir_list. |
| 4744 | */ |
| 4745 | ir = kzalloc(sizeof(struct amd_svm_iommu_ir), GFP_KERNEL); |
| 4746 | if (!ir) { |
| 4747 | ret = -ENOMEM; |
| 4748 | goto out; |
| 4749 | } |
| 4750 | ir->data = pi->ir_data; |
| 4751 | |
| 4752 | spin_lock_irqsave(&svm->ir_list_lock, flags); |
| 4753 | list_add(&ir->node, &svm->ir_list); |
| 4754 | spin_unlock_irqrestore(&svm->ir_list_lock, flags); |
| 4755 | out: |
| 4756 | return ret; |
| 4757 | } |
| 4758 | |
| 4759 | /** |
| 4760 | * Note: |
| 4761 | * The HW cannot support posting multicast/broadcast |
| 4762 | * interrupts to a vCPU. So, we still use legacy interrupt |
| 4763 | * remapping for these kind of interrupts. |
| 4764 | * |
| 4765 | * For lowest-priority interrupts, we only support |
| 4766 | * those with single CPU as the destination, e.g. user |
| 4767 | * configures the interrupts via /proc/irq or uses |
| 4768 | * irqbalance to make the interrupts single-CPU. |
| 4769 | */ |
| 4770 | static int |
| 4771 | get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e, |
| 4772 | struct vcpu_data *vcpu_info, struct vcpu_svm **svm) |
| 4773 | { |
| 4774 | struct kvm_lapic_irq irq; |
| 4775 | struct kvm_vcpu *vcpu = NULL; |
| 4776 | |
| 4777 | kvm_set_msi_irq(kvm, e, &irq); |
| 4778 | |
| 4779 | if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) { |
| 4780 | pr_debug("SVM: %s: use legacy intr remap mode for irq %u\n", |
| 4781 | __func__, irq.vector); |
| 4782 | return -1; |
| 4783 | } |
| 4784 | |
| 4785 | pr_debug("SVM: %s: use GA mode for irq %u\n", __func__, |
| 4786 | irq.vector); |
| 4787 | *svm = to_svm(vcpu); |
| 4788 | vcpu_info->pi_desc_addr = __sme_set(page_to_phys((*svm)->avic_backing_page)); |
| 4789 | vcpu_info->vector = irq.vector; |
| 4790 | |
| 4791 | return 0; |
| 4792 | } |
| 4793 | |
| 4794 | /* |
| 4795 | * svm_update_pi_irte - set IRTE for Posted-Interrupts |
| 4796 | * |
| 4797 | * @kvm: kvm |
| 4798 | * @host_irq: host irq of the interrupt |
| 4799 | * @guest_irq: gsi of the interrupt |
| 4800 | * @set: set or unset PI |
| 4801 | * returns 0 on success, < 0 on failure |
| 4802 | */ |
| 4803 | static int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq, |
| 4804 | uint32_t guest_irq, bool set) |
| 4805 | { |
| 4806 | struct kvm_kernel_irq_routing_entry *e; |
| 4807 | struct kvm_irq_routing_table *irq_rt; |
| 4808 | int idx, ret = -EINVAL; |
| 4809 | |
| 4810 | if (!kvm_arch_has_assigned_device(kvm) || |
| 4811 | !irq_remapping_cap(IRQ_POSTING_CAP)) |
| 4812 | return 0; |
| 4813 | |
| 4814 | pr_debug("SVM: %s: host_irq=%#x, guest_irq=%#x, set=%#x\n", |
| 4815 | __func__, host_irq, guest_irq, set); |
| 4816 | |
| 4817 | idx = srcu_read_lock(&kvm->irq_srcu); |
| 4818 | irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); |
| 4819 | WARN_ON(guest_irq >= irq_rt->nr_rt_entries); |
| 4820 | |
| 4821 | hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { |
| 4822 | struct vcpu_data vcpu_info; |
| 4823 | struct vcpu_svm *svm = NULL; |
| 4824 | |
| 4825 | if (e->type != KVM_IRQ_ROUTING_MSI) |
| 4826 | continue; |
| 4827 | |
| 4828 | /** |
| 4829 | * Here, we setup with legacy mode in the following cases: |
| 4830 | * 1. When cannot target interrupt to a specific vcpu. |
| 4831 | * 2. Unsetting posted interrupt. |
| 4832 | * 3. APIC virtialization is disabled for the vcpu. |
| 4833 | */ |
| 4834 | if (!get_pi_vcpu_info(kvm, e, &vcpu_info, &svm) && set && |
| 4835 | kvm_vcpu_apicv_active(&svm->vcpu)) { |
| 4836 | struct amd_iommu_pi_data pi; |
| 4837 | |
| 4838 | /* Try to enable guest_mode in IRTE */ |
| 4839 | pi.base = __sme_set(page_to_phys(svm->avic_backing_page) & |
| 4840 | AVIC_HPA_MASK); |
| 4841 | pi.ga_tag = AVIC_GATAG(kvm->arch.avic_vm_id, |
| 4842 | svm->vcpu.vcpu_id); |
| 4843 | pi.is_guest_mode = true; |
| 4844 | pi.vcpu_data = &vcpu_info; |
| 4845 | ret = irq_set_vcpu_affinity(host_irq, &pi); |
| 4846 | |
| 4847 | /** |
| 4848 | * Here, we successfully setting up vcpu affinity in |
| 4849 | * IOMMU guest mode. Now, we need to store the posted |
| 4850 | * interrupt information in a per-vcpu ir_list so that |
| 4851 | * we can reference to them directly when we update vcpu |
| 4852 | * scheduling information in IOMMU irte. |
| 4853 | */ |
| 4854 | if (!ret && pi.is_guest_mode) |
| 4855 | svm_ir_list_add(svm, &pi); |
| 4856 | } else { |
| 4857 | /* Use legacy mode in IRTE */ |
| 4858 | struct amd_iommu_pi_data pi; |
| 4859 | |
| 4860 | /** |
| 4861 | * Here, pi is used to: |
| 4862 | * - Tell IOMMU to use legacy mode for this interrupt. |
| 4863 | * - Retrieve ga_tag of prior interrupt remapping data. |
| 4864 | */ |
| 4865 | pi.is_guest_mode = false; |
| 4866 | ret = irq_set_vcpu_affinity(host_irq, &pi); |
| 4867 | |
| 4868 | /** |
| 4869 | * Check if the posted interrupt was previously |
| 4870 | * setup with the guest_mode by checking if the ga_tag |
| 4871 | * was cached. If so, we need to clean up the per-vcpu |
| 4872 | * ir_list. |
| 4873 | */ |
| 4874 | if (!ret && pi.prev_ga_tag) { |
| 4875 | int id = AVIC_GATAG_TO_VCPUID(pi.prev_ga_tag); |
| 4876 | struct kvm_vcpu *vcpu; |
| 4877 | |
| 4878 | vcpu = kvm_get_vcpu_by_id(kvm, id); |
| 4879 | if (vcpu) |
| 4880 | svm_ir_list_del(to_svm(vcpu), &pi); |
| 4881 | } |
| 4882 | } |
| 4883 | |
| 4884 | if (!ret && svm) { |
| 4885 | trace_kvm_pi_irte_update(host_irq, svm->vcpu.vcpu_id, |
| 4886 | e->gsi, vcpu_info.vector, |
| 4887 | vcpu_info.pi_desc_addr, set); |
| 4888 | } |
| 4889 | |
| 4890 | if (ret < 0) { |
| 4891 | pr_err("%s: failed to update PI IRTE\n", __func__); |
| 4892 | goto out; |
| 4893 | } |
| 4894 | } |
| 4895 | |
| 4896 | ret = 0; |
| 4897 | out: |
| 4898 | srcu_read_unlock(&kvm->irq_srcu, idx); |
| 4899 | return ret; |
| 4900 | } |
| 4901 | |
| 4902 | static int svm_nmi_allowed(struct kvm_vcpu *vcpu) |
| 4903 | { |
| 4904 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4905 | struct vmcb *vmcb = svm->vmcb; |
| 4906 | int ret; |
| 4907 | ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) && |
| 4908 | !(svm->vcpu.arch.hflags & HF_NMI_MASK); |
| 4909 | ret = ret && gif_set(svm) && nested_svm_nmi(svm); |
| 4910 | |
| 4911 | return ret; |
| 4912 | } |
| 4913 | |
| 4914 | static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu) |
| 4915 | { |
| 4916 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4917 | |
| 4918 | return !!(svm->vcpu.arch.hflags & HF_NMI_MASK); |
| 4919 | } |
| 4920 | |
| 4921 | static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) |
| 4922 | { |
| 4923 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4924 | |
| 4925 | if (masked) { |
| 4926 | svm->vcpu.arch.hflags |= HF_NMI_MASK; |
| 4927 | set_intercept(svm, INTERCEPT_IRET); |
| 4928 | } else { |
| 4929 | svm->vcpu.arch.hflags &= ~HF_NMI_MASK; |
| 4930 | clr_intercept(svm, INTERCEPT_IRET); |
| 4931 | } |
| 4932 | } |
| 4933 | |
| 4934 | static int svm_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 4935 | { |
| 4936 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4937 | struct vmcb *vmcb = svm->vmcb; |
| 4938 | int ret; |
| 4939 | |
| 4940 | if (!gif_set(svm) || |
| 4941 | (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)) |
| 4942 | return 0; |
| 4943 | |
| 4944 | ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF); |
| 4945 | |
| 4946 | if (is_guest_mode(vcpu)) |
| 4947 | return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK); |
| 4948 | |
| 4949 | return ret; |
| 4950 | } |
| 4951 | |
| 4952 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
| 4953 | { |
| 4954 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4955 | |
| 4956 | if (kvm_vcpu_apicv_active(vcpu)) |
| 4957 | return; |
| 4958 | |
| 4959 | /* |
| 4960 | * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes |
| 4961 | * 1, because that's a separate STGI/VMRUN intercept. The next time we |
| 4962 | * get that intercept, this function will be called again though and |
| 4963 | * we'll get the vintr intercept. However, if the vGIF feature is |
| 4964 | * enabled, the STGI interception will not occur. Enable the irq |
| 4965 | * window under the assumption that the hardware will set the GIF. |
| 4966 | */ |
| 4967 | if ((vgif_enabled(svm) || gif_set(svm)) && nested_svm_intr(svm)) { |
| 4968 | svm_set_vintr(svm); |
| 4969 | svm_inject_irq(svm, 0x0); |
| 4970 | } |
| 4971 | } |
| 4972 | |
| 4973 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
| 4974 | { |
| 4975 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4976 | |
| 4977 | if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK)) |
| 4978 | == HF_NMI_MASK) |
| 4979 | return; /* IRET will cause a vm exit */ |
| 4980 | |
| 4981 | if (!gif_set(svm)) { |
| 4982 | if (vgif_enabled(svm)) |
| 4983 | set_intercept(svm, INTERCEPT_STGI); |
| 4984 | return; /* STGI will cause a vm exit */ |
| 4985 | } |
| 4986 | |
| 4987 | if (svm->nested.exit_required) |
| 4988 | return; /* we're not going to run the guest yet */ |
| 4989 | |
| 4990 | /* |
| 4991 | * Something prevents NMI from been injected. Single step over possible |
| 4992 | * problem (IRET or exception injection or interrupt shadow) |
| 4993 | */ |
| 4994 | svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu); |
| 4995 | svm->nmi_singlestep = true; |
| 4996 | svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF); |
| 4997 | } |
| 4998 | |
| 4999 | static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 5000 | { |
| 5001 | return 0; |
| 5002 | } |
| 5003 | |
| 5004 | static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa) |
| 5005 | { |
| 5006 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5007 | |
| 5008 | if (static_cpu_has(X86_FEATURE_FLUSHBYASID)) |
| 5009 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID; |
| 5010 | else |
| 5011 | svm->asid_generation--; |
| 5012 | } |
| 5013 | |
| 5014 | static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu) |
| 5015 | { |
| 5016 | } |
| 5017 | |
| 5018 | static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu) |
| 5019 | { |
| 5020 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5021 | |
| 5022 | if (svm_nested_virtualize_tpr(vcpu)) |
| 5023 | return; |
| 5024 | |
| 5025 | if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) { |
| 5026 | int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK; |
| 5027 | kvm_set_cr8(vcpu, cr8); |
| 5028 | } |
| 5029 | } |
| 5030 | |
| 5031 | static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu) |
| 5032 | { |
| 5033 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5034 | u64 cr8; |
| 5035 | |
| 5036 | if (svm_nested_virtualize_tpr(vcpu) || |
| 5037 | kvm_vcpu_apicv_active(vcpu)) |
| 5038 | return; |
| 5039 | |
| 5040 | cr8 = kvm_get_cr8(vcpu); |
| 5041 | svm->vmcb->control.int_ctl &= ~V_TPR_MASK; |
| 5042 | svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK; |
| 5043 | } |
| 5044 | |
| 5045 | static void svm_complete_interrupts(struct vcpu_svm *svm) |
| 5046 | { |
| 5047 | u8 vector; |
| 5048 | int type; |
| 5049 | u32 exitintinfo = svm->vmcb->control.exit_int_info; |
| 5050 | unsigned int3_injected = svm->int3_injected; |
| 5051 | |
| 5052 | svm->int3_injected = 0; |
| 5053 | |
| 5054 | /* |
| 5055 | * If we've made progress since setting HF_IRET_MASK, we've |
| 5056 | * executed an IRET and can allow NMI injection. |
| 5057 | */ |
| 5058 | if ((svm->vcpu.arch.hflags & HF_IRET_MASK) |
| 5059 | && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) { |
| 5060 | svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK); |
| 5061 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
| 5062 | } |
| 5063 | |
| 5064 | svm->vcpu.arch.nmi_injected = false; |
| 5065 | kvm_clear_exception_queue(&svm->vcpu); |
| 5066 | kvm_clear_interrupt_queue(&svm->vcpu); |
| 5067 | |
| 5068 | if (!(exitintinfo & SVM_EXITINTINFO_VALID)) |
| 5069 | return; |
| 5070 | |
| 5071 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
| 5072 | |
| 5073 | vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK; |
| 5074 | type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK; |
| 5075 | |
| 5076 | switch (type) { |
| 5077 | case SVM_EXITINTINFO_TYPE_NMI: |
| 5078 | svm->vcpu.arch.nmi_injected = true; |
| 5079 | break; |
| 5080 | case SVM_EXITINTINFO_TYPE_EXEPT: |
| 5081 | /* |
| 5082 | * In case of software exceptions, do not reinject the vector, |
| 5083 | * but re-execute the instruction instead. Rewind RIP first |
| 5084 | * if we emulated INT3 before. |
| 5085 | */ |
| 5086 | if (kvm_exception_is_soft(vector)) { |
| 5087 | if (vector == BP_VECTOR && int3_injected && |
| 5088 | kvm_is_linear_rip(&svm->vcpu, svm->int3_rip)) |
| 5089 | kvm_rip_write(&svm->vcpu, |
| 5090 | kvm_rip_read(&svm->vcpu) - |
| 5091 | int3_injected); |
| 5092 | break; |
| 5093 | } |
| 5094 | if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) { |
| 5095 | u32 err = svm->vmcb->control.exit_int_info_err; |
| 5096 | kvm_requeue_exception_e(&svm->vcpu, vector, err); |
| 5097 | |
| 5098 | } else |
| 5099 | kvm_requeue_exception(&svm->vcpu, vector); |
| 5100 | break; |
| 5101 | case SVM_EXITINTINFO_TYPE_INTR: |
| 5102 | kvm_queue_interrupt(&svm->vcpu, vector, false); |
| 5103 | break; |
| 5104 | default: |
| 5105 | break; |
| 5106 | } |
| 5107 | } |
| 5108 | |
| 5109 | static void svm_cancel_injection(struct kvm_vcpu *vcpu) |
| 5110 | { |
| 5111 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5112 | struct vmcb_control_area *control = &svm->vmcb->control; |
| 5113 | |
| 5114 | control->exit_int_info = control->event_inj; |
| 5115 | control->exit_int_info_err = control->event_inj_err; |
| 5116 | control->event_inj = 0; |
| 5117 | svm_complete_interrupts(svm); |
| 5118 | } |
| 5119 | |
| 5120 | static void svm_vcpu_run(struct kvm_vcpu *vcpu) |
| 5121 | { |
| 5122 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5123 | |
| 5124 | svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX]; |
| 5125 | svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP]; |
| 5126 | svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP]; |
| 5127 | |
| 5128 | /* |
| 5129 | * A vmexit emulation is required before the vcpu can be executed |
| 5130 | * again. |
| 5131 | */ |
| 5132 | if (unlikely(svm->nested.exit_required)) |
| 5133 | return; |
| 5134 | |
| 5135 | /* |
| 5136 | * Disable singlestep if we're injecting an interrupt/exception. |
| 5137 | * We don't want our modified rflags to be pushed on the stack where |
| 5138 | * we might not be able to easily reset them if we disabled NMI |
| 5139 | * singlestep later. |
| 5140 | */ |
| 5141 | if (svm->nmi_singlestep && svm->vmcb->control.event_inj) { |
| 5142 | /* |
| 5143 | * Event injection happens before external interrupts cause a |
| 5144 | * vmexit and interrupts are disabled here, so smp_send_reschedule |
| 5145 | * is enough to force an immediate vmexit. |
| 5146 | */ |
| 5147 | disable_nmi_singlestep(svm); |
| 5148 | smp_send_reschedule(vcpu->cpu); |
| 5149 | } |
| 5150 | |
| 5151 | pre_svm_run(svm); |
| 5152 | |
| 5153 | sync_lapic_to_cr8(vcpu); |
| 5154 | |
| 5155 | svm->vmcb->save.cr2 = vcpu->arch.cr2; |
| 5156 | |
| 5157 | clgi(); |
| 5158 | |
| 5159 | /* |
| 5160 | * If this vCPU has touched SPEC_CTRL, restore the guest's value if |
| 5161 | * it's non-zero. Since vmentry is serialising on affected CPUs, there |
| 5162 | * is no need to worry about the conditional branch over the wrmsr |
| 5163 | * being speculatively taken. |
| 5164 | */ |
| 5165 | x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl); |
| 5166 | |
| 5167 | local_irq_enable(); |
| 5168 | |
| 5169 | asm volatile ( |
| 5170 | "push %%" _ASM_BP "; \n\t" |
| 5171 | "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t" |
| 5172 | "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t" |
| 5173 | "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t" |
| 5174 | "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t" |
| 5175 | "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t" |
| 5176 | "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t" |
| 5177 | #ifdef CONFIG_X86_64 |
| 5178 | "mov %c[r8](%[svm]), %%r8 \n\t" |
| 5179 | "mov %c[r9](%[svm]), %%r9 \n\t" |
| 5180 | "mov %c[r10](%[svm]), %%r10 \n\t" |
| 5181 | "mov %c[r11](%[svm]), %%r11 \n\t" |
| 5182 | "mov %c[r12](%[svm]), %%r12 \n\t" |
| 5183 | "mov %c[r13](%[svm]), %%r13 \n\t" |
| 5184 | "mov %c[r14](%[svm]), %%r14 \n\t" |
| 5185 | "mov %c[r15](%[svm]), %%r15 \n\t" |
| 5186 | #endif |
| 5187 | |
| 5188 | /* Enter guest mode */ |
| 5189 | "push %%" _ASM_AX " \n\t" |
| 5190 | "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t" |
| 5191 | __ex(SVM_VMLOAD) "\n\t" |
| 5192 | __ex(SVM_VMRUN) "\n\t" |
| 5193 | __ex(SVM_VMSAVE) "\n\t" |
| 5194 | "pop %%" _ASM_AX " \n\t" |
| 5195 | |
| 5196 | /* Save guest registers, load host registers */ |
| 5197 | "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t" |
| 5198 | "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t" |
| 5199 | "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t" |
| 5200 | "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t" |
| 5201 | "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t" |
| 5202 | "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t" |
| 5203 | #ifdef CONFIG_X86_64 |
| 5204 | "mov %%r8, %c[r8](%[svm]) \n\t" |
| 5205 | "mov %%r9, %c[r9](%[svm]) \n\t" |
| 5206 | "mov %%r10, %c[r10](%[svm]) \n\t" |
| 5207 | "mov %%r11, %c[r11](%[svm]) \n\t" |
| 5208 | "mov %%r12, %c[r12](%[svm]) \n\t" |
| 5209 | "mov %%r13, %c[r13](%[svm]) \n\t" |
| 5210 | "mov %%r14, %c[r14](%[svm]) \n\t" |
| 5211 | "mov %%r15, %c[r15](%[svm]) \n\t" |
| 5212 | #endif |
| 5213 | /* |
| 5214 | * Clear host registers marked as clobbered to prevent |
| 5215 | * speculative use. |
| 5216 | */ |
| 5217 | "xor %%" _ASM_BX ", %%" _ASM_BX " \n\t" |
| 5218 | "xor %%" _ASM_CX ", %%" _ASM_CX " \n\t" |
| 5219 | "xor %%" _ASM_DX ", %%" _ASM_DX " \n\t" |
| 5220 | "xor %%" _ASM_SI ", %%" _ASM_SI " \n\t" |
| 5221 | "xor %%" _ASM_DI ", %%" _ASM_DI " \n\t" |
| 5222 | #ifdef CONFIG_X86_64 |
| 5223 | "xor %%r8, %%r8 \n\t" |
| 5224 | "xor %%r9, %%r9 \n\t" |
| 5225 | "xor %%r10, %%r10 \n\t" |
| 5226 | "xor %%r11, %%r11 \n\t" |
| 5227 | "xor %%r12, %%r12 \n\t" |
| 5228 | "xor %%r13, %%r13 \n\t" |
| 5229 | "xor %%r14, %%r14 \n\t" |
| 5230 | "xor %%r15, %%r15 \n\t" |
| 5231 | #endif |
| 5232 | "pop %%" _ASM_BP |
| 5233 | : |
| 5234 | : [svm]"a"(svm), |
| 5235 | [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)), |
| 5236 | [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 5237 | [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 5238 | [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 5239 | [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 5240 | [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 5241 | [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP])) |
| 5242 | #ifdef CONFIG_X86_64 |
| 5243 | , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])), |
| 5244 | [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])), |
| 5245 | [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])), |
| 5246 | [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])), |
| 5247 | [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])), |
| 5248 | [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])), |
| 5249 | [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])), |
| 5250 | [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15])) |
| 5251 | #endif |
| 5252 | : "cc", "memory" |
| 5253 | #ifdef CONFIG_X86_64 |
| 5254 | , "rbx", "rcx", "rdx", "rsi", "rdi" |
| 5255 | , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15" |
| 5256 | #else |
| 5257 | , "ebx", "ecx", "edx", "esi", "edi" |
| 5258 | #endif |
| 5259 | ); |
| 5260 | |
| 5261 | /* Eliminate branch target predictions from guest mode */ |
| 5262 | vmexit_fill_RSB(); |
| 5263 | |
| 5264 | #ifdef CONFIG_X86_64 |
| 5265 | wrmsrl(MSR_GS_BASE, svm->host.gs_base); |
| 5266 | #else |
| 5267 | loadsegment(fs, svm->host.fs); |
| 5268 | #ifndef CONFIG_X86_32_LAZY_GS |
| 5269 | loadsegment(gs, svm->host.gs); |
| 5270 | #endif |
| 5271 | #endif |
| 5272 | |
| 5273 | /* |
| 5274 | * We do not use IBRS in the kernel. If this vCPU has used the |
| 5275 | * SPEC_CTRL MSR it may have left it on; save the value and |
| 5276 | * turn it off. This is much more efficient than blindly adding |
| 5277 | * it to the atomic save/restore list. Especially as the former |
| 5278 | * (Saving guest MSRs on vmexit) doesn't even exist in KVM. |
| 5279 | * |
| 5280 | * For non-nested case: |
| 5281 | * If the L01 MSR bitmap does not intercept the MSR, then we need to |
| 5282 | * save it. |
| 5283 | * |
| 5284 | * For nested case: |
| 5285 | * If the L02 MSR bitmap does not intercept the MSR, then we need to |
| 5286 | * save it. |
| 5287 | */ |
| 5288 | if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL))) |
| 5289 | svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL); |
| 5290 | |
| 5291 | reload_tss(vcpu); |
| 5292 | |
| 5293 | local_irq_disable(); |
| 5294 | |
| 5295 | x86_spec_ctrl_restore_host(svm->spec_ctrl, svm->virt_spec_ctrl); |
| 5296 | |
| 5297 | vcpu->arch.cr2 = svm->vmcb->save.cr2; |
| 5298 | vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax; |
| 5299 | vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp; |
| 5300 | vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip; |
| 5301 | |
| 5302 | if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI)) |
| 5303 | kvm_before_handle_nmi(&svm->vcpu); |
| 5304 | |
| 5305 | stgi(); |
| 5306 | |
| 5307 | /* Any pending NMI will happen here */ |
| 5308 | |
| 5309 | if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI)) |
| 5310 | kvm_after_handle_nmi(&svm->vcpu); |
| 5311 | |
| 5312 | sync_cr8_to_lapic(vcpu); |
| 5313 | |
| 5314 | svm->next_rip = 0; |
| 5315 | |
| 5316 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING; |
| 5317 | |
| 5318 | /* if exit due to PF check for async PF */ |
| 5319 | if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) |
| 5320 | svm->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason(); |
| 5321 | |
| 5322 | if (npt_enabled) { |
| 5323 | vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR); |
| 5324 | vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR); |
| 5325 | } |
| 5326 | |
| 5327 | /* |
| 5328 | * We need to handle MC intercepts here before the vcpu has a chance to |
| 5329 | * change the physical cpu |
| 5330 | */ |
| 5331 | if (unlikely(svm->vmcb->control.exit_code == |
| 5332 | SVM_EXIT_EXCP_BASE + MC_VECTOR)) |
| 5333 | svm_handle_mce(svm); |
| 5334 | |
| 5335 | mark_all_clean(svm->vmcb); |
| 5336 | } |
| 5337 | STACK_FRAME_NON_STANDARD(svm_vcpu_run); |
| 5338 | |
| 5339 | static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root) |
| 5340 | { |
| 5341 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5342 | |
| 5343 | svm->vmcb->save.cr3 = __sme_set(root); |
| 5344 | mark_dirty(svm->vmcb, VMCB_CR); |
| 5345 | svm_flush_tlb(vcpu, true); |
| 5346 | } |
| 5347 | |
| 5348 | static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root) |
| 5349 | { |
| 5350 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5351 | |
| 5352 | svm->vmcb->control.nested_cr3 = __sme_set(root); |
| 5353 | mark_dirty(svm->vmcb, VMCB_NPT); |
| 5354 | |
| 5355 | /* Also sync guest cr3 here in case we live migrate */ |
| 5356 | svm->vmcb->save.cr3 = kvm_read_cr3(vcpu); |
| 5357 | mark_dirty(svm->vmcb, VMCB_CR); |
| 5358 | |
| 5359 | svm_flush_tlb(vcpu, true); |
| 5360 | } |
| 5361 | |
| 5362 | static int is_disabled(void) |
| 5363 | { |
| 5364 | u64 vm_cr; |
| 5365 | |
| 5366 | rdmsrl(MSR_VM_CR, vm_cr); |
| 5367 | if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE)) |
| 5368 | return 1; |
| 5369 | |
| 5370 | return 0; |
| 5371 | } |
| 5372 | |
| 5373 | static void |
| 5374 | svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 5375 | { |
| 5376 | /* |
| 5377 | * Patch in the VMMCALL instruction: |
| 5378 | */ |
| 5379 | hypercall[0] = 0x0f; |
| 5380 | hypercall[1] = 0x01; |
| 5381 | hypercall[2] = 0xd9; |
| 5382 | } |
| 5383 | |
| 5384 | static void svm_check_processor_compat(void *rtn) |
| 5385 | { |
| 5386 | *(int *)rtn = 0; |
| 5387 | } |
| 5388 | |
| 5389 | static bool svm_cpu_has_accelerated_tpr(void) |
| 5390 | { |
| 5391 | return false; |
| 5392 | } |
| 5393 | |
| 5394 | static bool svm_has_emulated_msr(int index) |
| 5395 | { |
| 5396 | switch (index) { |
| 5397 | case MSR_IA32_MCG_EXT_CTL: |
| 5398 | return false; |
| 5399 | default: |
| 5400 | break; |
| 5401 | } |
| 5402 | |
| 5403 | return true; |
| 5404 | } |
| 5405 | |
| 5406 | static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio) |
| 5407 | { |
| 5408 | return 0; |
| 5409 | } |
| 5410 | |
| 5411 | static void svm_cpuid_update(struct kvm_vcpu *vcpu) |
| 5412 | { |
| 5413 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5414 | |
| 5415 | /* Update nrips enabled cache */ |
| 5416 | svm->nrips_enabled = !!guest_cpuid_has(&svm->vcpu, X86_FEATURE_NRIPS); |
| 5417 | |
| 5418 | if (!kvm_vcpu_apicv_active(vcpu)) |
| 5419 | return; |
| 5420 | |
| 5421 | guest_cpuid_clear(vcpu, X86_FEATURE_X2APIC); |
| 5422 | } |
| 5423 | |
| 5424 | static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
| 5425 | { |
| 5426 | switch (func) { |
| 5427 | case 0x1: |
| 5428 | if (avic) |
| 5429 | entry->ecx &= ~bit(X86_FEATURE_X2APIC); |
| 5430 | break; |
| 5431 | case 0x80000001: |
| 5432 | if (nested) |
| 5433 | entry->ecx |= (1 << 2); /* Set SVM bit */ |
| 5434 | break; |
| 5435 | case 0x8000000A: |
| 5436 | entry->eax = 1; /* SVM revision 1 */ |
| 5437 | entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper |
| 5438 | ASID emulation to nested SVM */ |
| 5439 | entry->ecx = 0; /* Reserved */ |
| 5440 | entry->edx = 0; /* Per default do not support any |
| 5441 | additional features */ |
| 5442 | |
| 5443 | /* Support next_rip if host supports it */ |
| 5444 | if (boot_cpu_has(X86_FEATURE_NRIPS)) |
| 5445 | entry->edx |= SVM_FEATURE_NRIP; |
| 5446 | |
| 5447 | /* Support NPT for the guest if enabled */ |
| 5448 | if (npt_enabled) |
| 5449 | entry->edx |= SVM_FEATURE_NPT; |
| 5450 | |
| 5451 | break; |
| 5452 | } |
| 5453 | } |
| 5454 | |
| 5455 | static int svm_get_lpage_level(void) |
| 5456 | { |
| 5457 | return PT_PDPE_LEVEL; |
| 5458 | } |
| 5459 | |
| 5460 | static bool svm_rdtscp_supported(void) |
| 5461 | { |
| 5462 | return boot_cpu_has(X86_FEATURE_RDTSCP); |
| 5463 | } |
| 5464 | |
| 5465 | static bool svm_invpcid_supported(void) |
| 5466 | { |
| 5467 | return false; |
| 5468 | } |
| 5469 | |
| 5470 | static bool svm_mpx_supported(void) |
| 5471 | { |
| 5472 | return false; |
| 5473 | } |
| 5474 | |
| 5475 | static bool svm_xsaves_supported(void) |
| 5476 | { |
| 5477 | return false; |
| 5478 | } |
| 5479 | |
| 5480 | static bool svm_has_wbinvd_exit(void) |
| 5481 | { |
| 5482 | return true; |
| 5483 | } |
| 5484 | |
| 5485 | #define PRE_EX(exit) { .exit_code = (exit), \ |
| 5486 | .stage = X86_ICPT_PRE_EXCEPT, } |
| 5487 | #define POST_EX(exit) { .exit_code = (exit), \ |
| 5488 | .stage = X86_ICPT_POST_EXCEPT, } |
| 5489 | #define POST_MEM(exit) { .exit_code = (exit), \ |
| 5490 | .stage = X86_ICPT_POST_MEMACCESS, } |
| 5491 | |
| 5492 | static const struct __x86_intercept { |
| 5493 | u32 exit_code; |
| 5494 | enum x86_intercept_stage stage; |
| 5495 | } x86_intercept_map[] = { |
| 5496 | [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0), |
| 5497 | [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0), |
| 5498 | [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0), |
| 5499 | [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0), |
| 5500 | [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0), |
| 5501 | [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0), |
| 5502 | [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0), |
| 5503 | [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ), |
| 5504 | [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ), |
| 5505 | [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE), |
| 5506 | [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE), |
| 5507 | [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ), |
| 5508 | [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ), |
| 5509 | [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE), |
| 5510 | [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE), |
| 5511 | [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN), |
| 5512 | [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL), |
| 5513 | [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD), |
| 5514 | [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE), |
| 5515 | [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI), |
| 5516 | [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI), |
| 5517 | [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT), |
| 5518 | [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA), |
| 5519 | [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP), |
| 5520 | [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR), |
| 5521 | [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT), |
| 5522 | [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG), |
| 5523 | [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD), |
| 5524 | [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD), |
| 5525 | [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR), |
| 5526 | [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC), |
| 5527 | [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR), |
| 5528 | [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC), |
| 5529 | [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID), |
| 5530 | [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM), |
| 5531 | [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE), |
| 5532 | [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF), |
| 5533 | [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF), |
| 5534 | [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT), |
| 5535 | [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET), |
| 5536 | [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP), |
| 5537 | [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT), |
| 5538 | [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO), |
| 5539 | [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO), |
| 5540 | [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO), |
| 5541 | [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO), |
| 5542 | }; |
| 5543 | |
| 5544 | #undef PRE_EX |
| 5545 | #undef POST_EX |
| 5546 | #undef POST_MEM |
| 5547 | |
| 5548 | static int svm_check_intercept(struct kvm_vcpu *vcpu, |
| 5549 | struct x86_instruction_info *info, |
| 5550 | enum x86_intercept_stage stage) |
| 5551 | { |
| 5552 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5553 | int vmexit, ret = X86EMUL_CONTINUE; |
| 5554 | struct __x86_intercept icpt_info; |
| 5555 | struct vmcb *vmcb = svm->vmcb; |
| 5556 | |
| 5557 | if (info->intercept >= ARRAY_SIZE(x86_intercept_map)) |
| 5558 | goto out; |
| 5559 | |
| 5560 | icpt_info = x86_intercept_map[info->intercept]; |
| 5561 | |
| 5562 | if (stage != icpt_info.stage) |
| 5563 | goto out; |
| 5564 | |
| 5565 | switch (icpt_info.exit_code) { |
| 5566 | case SVM_EXIT_READ_CR0: |
| 5567 | if (info->intercept == x86_intercept_cr_read) |
| 5568 | icpt_info.exit_code += info->modrm_reg; |
| 5569 | break; |
| 5570 | case SVM_EXIT_WRITE_CR0: { |
| 5571 | unsigned long cr0, val; |
| 5572 | u64 intercept; |
| 5573 | |
| 5574 | if (info->intercept == x86_intercept_cr_write) |
| 5575 | icpt_info.exit_code += info->modrm_reg; |
| 5576 | |
| 5577 | if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 || |
| 5578 | info->intercept == x86_intercept_clts) |
| 5579 | break; |
| 5580 | |
| 5581 | intercept = svm->nested.intercept; |
| 5582 | |
| 5583 | if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))) |
| 5584 | break; |
| 5585 | |
| 5586 | cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK; |
| 5587 | val = info->src_val & ~SVM_CR0_SELECTIVE_MASK; |
| 5588 | |
| 5589 | if (info->intercept == x86_intercept_lmsw) { |
| 5590 | cr0 &= 0xfUL; |
| 5591 | val &= 0xfUL; |
| 5592 | /* lmsw can't clear PE - catch this here */ |
| 5593 | if (cr0 & X86_CR0_PE) |
| 5594 | val |= X86_CR0_PE; |
| 5595 | } |
| 5596 | |
| 5597 | if (cr0 ^ val) |
| 5598 | icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE; |
| 5599 | |
| 5600 | break; |
| 5601 | } |
| 5602 | case SVM_EXIT_READ_DR0: |
| 5603 | case SVM_EXIT_WRITE_DR0: |
| 5604 | icpt_info.exit_code += info->modrm_reg; |
| 5605 | break; |
| 5606 | case SVM_EXIT_MSR: |
| 5607 | if (info->intercept == x86_intercept_wrmsr) |
| 5608 | vmcb->control.exit_info_1 = 1; |
| 5609 | else |
| 5610 | vmcb->control.exit_info_1 = 0; |
| 5611 | break; |
| 5612 | case SVM_EXIT_PAUSE: |
| 5613 | /* |
| 5614 | * We get this for NOP only, but pause |
| 5615 | * is rep not, check this here |
| 5616 | */ |
| 5617 | if (info->rep_prefix != REPE_PREFIX) |
| 5618 | goto out; |
| 5619 | break; |
| 5620 | case SVM_EXIT_IOIO: { |
| 5621 | u64 exit_info; |
| 5622 | u32 bytes; |
| 5623 | |
| 5624 | if (info->intercept == x86_intercept_in || |
| 5625 | info->intercept == x86_intercept_ins) { |
| 5626 | exit_info = ((info->src_val & 0xffff) << 16) | |
| 5627 | SVM_IOIO_TYPE_MASK; |
| 5628 | bytes = info->dst_bytes; |
| 5629 | } else { |
| 5630 | exit_info = (info->dst_val & 0xffff) << 16; |
| 5631 | bytes = info->src_bytes; |
| 5632 | } |
| 5633 | |
| 5634 | if (info->intercept == x86_intercept_outs || |
| 5635 | info->intercept == x86_intercept_ins) |
| 5636 | exit_info |= SVM_IOIO_STR_MASK; |
| 5637 | |
| 5638 | if (info->rep_prefix) |
| 5639 | exit_info |= SVM_IOIO_REP_MASK; |
| 5640 | |
| 5641 | bytes = min(bytes, 4u); |
| 5642 | |
| 5643 | exit_info |= bytes << SVM_IOIO_SIZE_SHIFT; |
| 5644 | |
| 5645 | exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1); |
| 5646 | |
| 5647 | vmcb->control.exit_info_1 = exit_info; |
| 5648 | vmcb->control.exit_info_2 = info->next_rip; |
| 5649 | |
| 5650 | break; |
| 5651 | } |
| 5652 | default: |
| 5653 | break; |
| 5654 | } |
| 5655 | |
| 5656 | /* TODO: Advertise NRIPS to guest hypervisor unconditionally */ |
| 5657 | if (static_cpu_has(X86_FEATURE_NRIPS)) |
| 5658 | vmcb->control.next_rip = info->next_rip; |
| 5659 | vmcb->control.exit_code = icpt_info.exit_code; |
| 5660 | vmexit = nested_svm_exit_handled(svm); |
| 5661 | |
| 5662 | ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED |
| 5663 | : X86EMUL_CONTINUE; |
| 5664 | |
| 5665 | out: |
| 5666 | return ret; |
| 5667 | } |
| 5668 | |
| 5669 | static void svm_handle_external_intr(struct kvm_vcpu *vcpu) |
| 5670 | { |
| 5671 | local_irq_enable(); |
| 5672 | /* |
| 5673 | * We must have an instruction with interrupts enabled, so |
| 5674 | * the timer interrupt isn't delayed by the interrupt shadow. |
| 5675 | */ |
| 5676 | asm("nop"); |
| 5677 | local_irq_disable(); |
| 5678 | } |
| 5679 | |
| 5680 | static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu) |
| 5681 | { |
| 5682 | } |
| 5683 | |
| 5684 | static inline void avic_post_state_restore(struct kvm_vcpu *vcpu) |
| 5685 | { |
| 5686 | if (avic_handle_apic_id_update(vcpu) != 0) |
| 5687 | return; |
| 5688 | if (avic_handle_dfr_update(vcpu) != 0) |
| 5689 | return; |
| 5690 | avic_handle_ldr_update(vcpu); |
| 5691 | } |
| 5692 | |
| 5693 | static void svm_setup_mce(struct kvm_vcpu *vcpu) |
| 5694 | { |
| 5695 | /* [63:9] are reserved. */ |
| 5696 | vcpu->arch.mcg_cap &= 0x1ff; |
| 5697 | } |
| 5698 | |
| 5699 | static struct kvm_x86_ops svm_x86_ops __ro_after_init = { |
| 5700 | .cpu_has_kvm_support = has_svm, |
| 5701 | .disabled_by_bios = is_disabled, |
| 5702 | .hardware_setup = svm_hardware_setup, |
| 5703 | .hardware_unsetup = svm_hardware_unsetup, |
| 5704 | .check_processor_compatibility = svm_check_processor_compat, |
| 5705 | .hardware_enable = svm_hardware_enable, |
| 5706 | .hardware_disable = svm_hardware_disable, |
| 5707 | .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr, |
| 5708 | .has_emulated_msr = svm_has_emulated_msr, |
| 5709 | |
| 5710 | .vcpu_create = svm_create_vcpu, |
| 5711 | .vcpu_free = svm_free_vcpu, |
| 5712 | .vcpu_reset = svm_vcpu_reset, |
| 5713 | |
| 5714 | .vm_init = avic_vm_init, |
| 5715 | .vm_destroy = avic_vm_destroy, |
| 5716 | |
| 5717 | .prepare_guest_switch = svm_prepare_guest_switch, |
| 5718 | .vcpu_load = svm_vcpu_load, |
| 5719 | .vcpu_put = svm_vcpu_put, |
| 5720 | .vcpu_blocking = svm_vcpu_blocking, |
| 5721 | .vcpu_unblocking = svm_vcpu_unblocking, |
| 5722 | |
| 5723 | .update_bp_intercept = update_bp_intercept, |
| 5724 | .get_msr_feature = svm_get_msr_feature, |
| 5725 | .get_msr = svm_get_msr, |
| 5726 | .set_msr = svm_set_msr, |
| 5727 | .get_segment_base = svm_get_segment_base, |
| 5728 | .get_segment = svm_get_segment, |
| 5729 | .set_segment = svm_set_segment, |
| 5730 | .get_cpl = svm_get_cpl, |
| 5731 | .get_cs_db_l_bits = kvm_get_cs_db_l_bits, |
| 5732 | .decache_cr0_guest_bits = svm_decache_cr0_guest_bits, |
| 5733 | .decache_cr3 = svm_decache_cr3, |
| 5734 | .decache_cr4_guest_bits = svm_decache_cr4_guest_bits, |
| 5735 | .set_cr0 = svm_set_cr0, |
| 5736 | .set_cr3 = svm_set_cr3, |
| 5737 | .set_cr4 = svm_set_cr4, |
| 5738 | .set_efer = svm_set_efer, |
| 5739 | .get_idt = svm_get_idt, |
| 5740 | .set_idt = svm_set_idt, |
| 5741 | .get_gdt = svm_get_gdt, |
| 5742 | .set_gdt = svm_set_gdt, |
| 5743 | .get_dr6 = svm_get_dr6, |
| 5744 | .set_dr6 = svm_set_dr6, |
| 5745 | .set_dr7 = svm_set_dr7, |
| 5746 | .sync_dirty_debug_regs = svm_sync_dirty_debug_regs, |
| 5747 | .cache_reg = svm_cache_reg, |
| 5748 | .get_rflags = svm_get_rflags, |
| 5749 | .set_rflags = svm_set_rflags, |
| 5750 | |
| 5751 | .tlb_flush = svm_flush_tlb, |
| 5752 | |
| 5753 | .run = svm_vcpu_run, |
| 5754 | .handle_exit = handle_exit, |
| 5755 | .skip_emulated_instruction = skip_emulated_instruction, |
| 5756 | .set_interrupt_shadow = svm_set_interrupt_shadow, |
| 5757 | .get_interrupt_shadow = svm_get_interrupt_shadow, |
| 5758 | .patch_hypercall = svm_patch_hypercall, |
| 5759 | .set_irq = svm_set_irq, |
| 5760 | .set_nmi = svm_inject_nmi, |
| 5761 | .queue_exception = svm_queue_exception, |
| 5762 | .cancel_injection = svm_cancel_injection, |
| 5763 | .interrupt_allowed = svm_interrupt_allowed, |
| 5764 | .nmi_allowed = svm_nmi_allowed, |
| 5765 | .get_nmi_mask = svm_get_nmi_mask, |
| 5766 | .set_nmi_mask = svm_set_nmi_mask, |
| 5767 | .enable_nmi_window = enable_nmi_window, |
| 5768 | .enable_irq_window = enable_irq_window, |
| 5769 | .update_cr8_intercept = update_cr8_intercept, |
| 5770 | .set_virtual_apic_mode = svm_set_virtual_apic_mode, |
| 5771 | .get_enable_apicv = svm_get_enable_apicv, |
| 5772 | .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl, |
| 5773 | .load_eoi_exitmap = svm_load_eoi_exitmap, |
| 5774 | .hwapic_irr_update = svm_hwapic_irr_update, |
| 5775 | .hwapic_isr_update = svm_hwapic_isr_update, |
| 5776 | .apicv_post_state_restore = avic_post_state_restore, |
| 5777 | |
| 5778 | .set_tss_addr = svm_set_tss_addr, |
| 5779 | .get_tdp_level = get_npt_level, |
| 5780 | .get_mt_mask = svm_get_mt_mask, |
| 5781 | |
| 5782 | .get_exit_info = svm_get_exit_info, |
| 5783 | |
| 5784 | .get_lpage_level = svm_get_lpage_level, |
| 5785 | |
| 5786 | .cpuid_update = svm_cpuid_update, |
| 5787 | |
| 5788 | .rdtscp_supported = svm_rdtscp_supported, |
| 5789 | .invpcid_supported = svm_invpcid_supported, |
| 5790 | .mpx_supported = svm_mpx_supported, |
| 5791 | .xsaves_supported = svm_xsaves_supported, |
| 5792 | |
| 5793 | .set_supported_cpuid = svm_set_supported_cpuid, |
| 5794 | |
| 5795 | .has_wbinvd_exit = svm_has_wbinvd_exit, |
| 5796 | |
| 5797 | .write_tsc_offset = svm_write_tsc_offset, |
| 5798 | |
| 5799 | .set_tdp_cr3 = set_tdp_cr3, |
| 5800 | |
| 5801 | .check_intercept = svm_check_intercept, |
| 5802 | .handle_external_intr = svm_handle_external_intr, |
| 5803 | |
| 5804 | .sched_in = svm_sched_in, |
| 5805 | |
| 5806 | .pmu_ops = &amd_pmu_ops, |
| 5807 | .deliver_posted_interrupt = svm_deliver_avic_intr, |
| 5808 | .dy_apicv_has_pending_interrupt = svm_dy_apicv_has_pending_interrupt, |
| 5809 | .update_pi_irte = svm_update_pi_irte, |
| 5810 | .setup_mce = svm_setup_mce, |
| 5811 | }; |
| 5812 | |
| 5813 | static int __init svm_init(void) |
| 5814 | { |
| 5815 | return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm), |
| 5816 | __alignof__(struct vcpu_svm), THIS_MODULE); |
| 5817 | } |
| 5818 | |
| 5819 | static void __exit svm_exit(void) |
| 5820 | { |
| 5821 | kvm_exit(); |
| 5822 | } |
| 5823 | |
| 5824 | module_init(svm_init) |
| 5825 | module_exit(svm_exit) |