Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Kernel-based Virtual Machine driver for Linux |
| 4 | * |
| 5 | * AMD SVM support |
| 6 | * |
| 7 | * Copyright (C) 2006 Qumranet, Inc. |
| 8 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
| 9 | * |
| 10 | * Authors: |
| 11 | * Yaniv Kamay <yaniv@qumranet.com> |
| 12 | * Avi Kivity <avi@qumranet.com> |
| 13 | */ |
| 14 | |
| 15 | #ifndef __SVM_SVM_H |
| 16 | #define __SVM_SVM_H |
| 17 | |
| 18 | #include <linux/kvm_types.h> |
| 19 | #include <linux/kvm_host.h> |
| 20 | |
| 21 | #include <asm/svm.h> |
| 22 | |
| 23 | static const u32 host_save_user_msrs[] = { |
| 24 | #ifdef CONFIG_X86_64 |
| 25 | MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE, |
| 26 | MSR_FS_BASE, |
| 27 | #endif |
| 28 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, |
| 29 | MSR_TSC_AUX, |
| 30 | }; |
| 31 | |
| 32 | #define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs) |
| 33 | |
| 34 | #define MSRPM_OFFSETS 16 |
| 35 | extern u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly; |
| 36 | extern bool npt_enabled; |
| 37 | |
| 38 | enum { |
| 39 | VMCB_INTERCEPTS, /* Intercept vectors, TSC offset, |
| 40 | pause filter count */ |
| 41 | VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */ |
| 42 | VMCB_ASID, /* ASID */ |
| 43 | VMCB_INTR, /* int_ctl, int_vector */ |
| 44 | VMCB_NPT, /* npt_en, nCR3, gPAT */ |
| 45 | VMCB_CR, /* CR0, CR3, CR4, EFER */ |
| 46 | VMCB_DR, /* DR6, DR7 */ |
| 47 | VMCB_DT, /* GDT, IDT */ |
| 48 | VMCB_SEG, /* CS, DS, SS, ES, CPL */ |
| 49 | VMCB_CR2, /* CR2 only */ |
| 50 | VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */ |
| 51 | VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE, |
| 52 | * AVIC PHYSICAL_TABLE pointer, |
| 53 | * AVIC LOGICAL_TABLE pointer |
| 54 | */ |
| 55 | VMCB_DIRTY_MAX, |
| 56 | }; |
| 57 | |
| 58 | /* TPR and CR2 are always written before VMRUN */ |
| 59 | #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2)) |
| 60 | |
| 61 | struct kvm_sev_info { |
| 62 | bool active; /* SEV enabled guest */ |
| 63 | unsigned int asid; /* ASID used for this guest */ |
| 64 | unsigned int handle; /* SEV firmware handle */ |
| 65 | int fd; /* SEV device fd */ |
| 66 | unsigned long pages_locked; /* Number of pages locked */ |
| 67 | struct list_head regions_list; /* List of registered regions */ |
| 68 | }; |
| 69 | |
| 70 | struct kvm_svm { |
| 71 | struct kvm kvm; |
| 72 | |
| 73 | /* Struct members for AVIC */ |
| 74 | u32 avic_vm_id; |
| 75 | struct page *avic_logical_id_table_page; |
| 76 | struct page *avic_physical_id_table_page; |
| 77 | struct hlist_node hnode; |
| 78 | |
| 79 | struct kvm_sev_info sev_info; |
| 80 | }; |
| 81 | |
| 82 | struct kvm_vcpu; |
| 83 | |
Joerg Roedel | 7693b3e | 2020-06-25 10:03:22 +0200 | [diff] [blame] | 84 | struct svm_nested_state { |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 85 | struct vmcb *hsave; |
| 86 | u64 hsave_msr; |
| 87 | u64 vm_cr_msr; |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 88 | u64 vmcb12_gpa; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 89 | |
| 90 | /* These are the merged vectors */ |
| 91 | u32 *msrpm; |
| 92 | |
Paolo Bonzini | f74f941 | 2020-04-23 13:22:27 -0400 | [diff] [blame] | 93 | /* A VMRUN has started but has not yet been performed, so |
| 94 | * we cannot inject a nested vmexit yet. */ |
| 95 | bool nested_run_pending; |
| 96 | |
Paolo Bonzini | e670bf6 | 2020-05-13 13:16:12 -0400 | [diff] [blame] | 97 | /* cache for control fields of the guest */ |
| 98 | struct vmcb_control_area ctl; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | struct vcpu_svm { |
| 102 | struct kvm_vcpu vcpu; |
| 103 | struct vmcb *vmcb; |
| 104 | unsigned long vmcb_pa; |
| 105 | struct svm_cpu_data *svm_data; |
| 106 | uint64_t asid_generation; |
| 107 | uint64_t sysenter_esp; |
| 108 | uint64_t sysenter_eip; |
| 109 | uint64_t tsc_aux; |
| 110 | |
| 111 | u64 msr_decfg; |
| 112 | |
| 113 | u64 next_rip; |
| 114 | |
| 115 | u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS]; |
| 116 | struct { |
| 117 | u16 fs; |
| 118 | u16 gs; |
| 119 | u16 ldt; |
| 120 | u64 gs_base; |
| 121 | } host; |
| 122 | |
| 123 | u64 spec_ctrl; |
| 124 | /* |
| 125 | * Contains guest-controlled bits of VIRT_SPEC_CTRL, which will be |
| 126 | * translated into the appropriate L2_CFG bits on the host to |
| 127 | * perform speculative control. |
| 128 | */ |
| 129 | u64 virt_spec_ctrl; |
| 130 | |
| 131 | u32 *msrpm; |
| 132 | |
| 133 | ulong nmi_iret_rip; |
| 134 | |
Joerg Roedel | 7693b3e | 2020-06-25 10:03:22 +0200 | [diff] [blame] | 135 | struct svm_nested_state nested; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 136 | |
| 137 | bool nmi_singlestep; |
| 138 | u64 nmi_singlestep_guest_rflags; |
| 139 | |
| 140 | unsigned int3_injected; |
| 141 | unsigned long int3_rip; |
| 142 | |
| 143 | /* cached guest cpuid flags for faster access */ |
| 144 | bool nrips_enabled : 1; |
| 145 | |
| 146 | u32 ldr_reg; |
| 147 | u32 dfr_reg; |
| 148 | struct page *avic_backing_page; |
| 149 | u64 *avic_physical_id_cache; |
| 150 | bool avic_is_running; |
| 151 | |
| 152 | /* |
| 153 | * Per-vcpu list of struct amd_svm_iommu_ir: |
| 154 | * This is used mainly to store interrupt remapping information used |
| 155 | * when update the vcpu affinity. This avoids the need to scan for |
| 156 | * IRTE and try to match ga_tag in the IOMMU driver. |
| 157 | */ |
| 158 | struct list_head ir_list; |
| 159 | spinlock_t ir_list_lock; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 160 | }; |
| 161 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 162 | struct svm_cpu_data { |
| 163 | int cpu; |
| 164 | |
| 165 | u64 asid_generation; |
| 166 | u32 max_asid; |
| 167 | u32 next_asid; |
| 168 | u32 min_asid; |
| 169 | struct kvm_ldttss_desc *tss_desc; |
| 170 | |
| 171 | struct page *save_area; |
| 172 | struct vmcb *current_vmcb; |
| 173 | |
| 174 | /* index = sev_asid, value = vmcb pointer */ |
| 175 | struct vmcb **sev_vmcbs; |
| 176 | }; |
| 177 | |
| 178 | DECLARE_PER_CPU(struct svm_cpu_data *, svm_data); |
| 179 | |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 180 | void recalc_intercepts(struct vcpu_svm *svm); |
| 181 | |
Joerg Roedel | ef0f649 | 2020-03-31 12:17:38 -0400 | [diff] [blame] | 182 | static inline struct kvm_svm *to_kvm_svm(struct kvm *kvm) |
| 183 | { |
| 184 | return container_of(kvm, struct kvm_svm, kvm); |
| 185 | } |
| 186 | |
Joerg Roedel | 06e7852 | 2020-06-25 10:03:23 +0200 | [diff] [blame] | 187 | static inline void vmcb_mark_all_dirty(struct vmcb *vmcb) |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 188 | { |
| 189 | vmcb->control.clean = 0; |
| 190 | } |
| 191 | |
Joerg Roedel | 06e7852 | 2020-06-25 10:03:23 +0200 | [diff] [blame] | 192 | static inline void vmcb_mark_all_clean(struct vmcb *vmcb) |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 193 | { |
| 194 | vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1) |
| 195 | & ~VMCB_ALWAYS_DIRTY_MASK; |
| 196 | } |
| 197 | |
Joerg Roedel | 06e7852 | 2020-06-25 10:03:23 +0200 | [diff] [blame] | 198 | static inline void vmcb_mark_dirty(struct vmcb *vmcb, int bit) |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 199 | { |
| 200 | vmcb->control.clean &= ~(1 << bit); |
| 201 | } |
| 202 | |
| 203 | static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu) |
| 204 | { |
| 205 | return container_of(vcpu, struct vcpu_svm, vcpu); |
| 206 | } |
| 207 | |
| 208 | static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm) |
| 209 | { |
| 210 | if (is_guest_mode(&svm->vcpu)) |
| 211 | return svm->nested.hsave; |
| 212 | else |
| 213 | return svm->vmcb; |
| 214 | } |
| 215 | |
Babu Moger | c45ad72 | 2020-09-11 14:27:58 -0500 | [diff] [blame] | 216 | static inline void vmcb_set_intercept(struct vmcb_control_area *control, u32 bit) |
| 217 | { |
| 218 | WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT); |
| 219 | __set_bit(bit, (unsigned long *)&control->intercepts); |
| 220 | } |
| 221 | |
| 222 | static inline void vmcb_clr_intercept(struct vmcb_control_area *control, u32 bit) |
| 223 | { |
| 224 | WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT); |
| 225 | __clear_bit(bit, (unsigned long *)&control->intercepts); |
| 226 | } |
| 227 | |
| 228 | static inline bool vmcb_is_intercept(struct vmcb_control_area *control, u32 bit) |
| 229 | { |
| 230 | WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT); |
| 231 | return test_bit(bit, (unsigned long *)&control->intercepts); |
| 232 | } |
| 233 | |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 234 | static inline void set_cr_intercept(struct vcpu_svm *svm, int bit) |
| 235 | { |
| 236 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 237 | |
Babu Moger | 03bfeeb | 2020-09-11 14:28:05 -0500 | [diff] [blame] | 238 | vmcb_set_intercept(&vmcb->control, bit); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 239 | |
| 240 | recalc_intercepts(svm); |
| 241 | } |
| 242 | |
| 243 | static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit) |
| 244 | { |
| 245 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 246 | |
Babu Moger | 03bfeeb | 2020-09-11 14:28:05 -0500 | [diff] [blame] | 247 | vmcb_clr_intercept(&vmcb->control, bit); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 248 | |
| 249 | recalc_intercepts(svm); |
| 250 | } |
| 251 | |
| 252 | static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit) |
| 253 | { |
| 254 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 255 | |
Babu Moger | 03bfeeb | 2020-09-11 14:28:05 -0500 | [diff] [blame] | 256 | return vmcb_is_intercept(&vmcb->control, bit); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | static inline void set_dr_intercepts(struct vcpu_svm *svm) |
| 260 | { |
| 261 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 262 | |
Babu Moger | 30abaa88 | 2020-09-11 14:28:12 -0500 | [diff] [blame^] | 263 | vmcb_set_intercept(&vmcb->control, INTERCEPT_DR0_READ); |
| 264 | vmcb_set_intercept(&vmcb->control, INTERCEPT_DR1_READ); |
| 265 | vmcb_set_intercept(&vmcb->control, INTERCEPT_DR2_READ); |
| 266 | vmcb_set_intercept(&vmcb->control, INTERCEPT_DR3_READ); |
| 267 | vmcb_set_intercept(&vmcb->control, INTERCEPT_DR4_READ); |
| 268 | vmcb_set_intercept(&vmcb->control, INTERCEPT_DR5_READ); |
| 269 | vmcb_set_intercept(&vmcb->control, INTERCEPT_DR6_READ); |
| 270 | vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_READ); |
| 271 | vmcb_set_intercept(&vmcb->control, INTERCEPT_DR0_WRITE); |
| 272 | vmcb_set_intercept(&vmcb->control, INTERCEPT_DR1_WRITE); |
| 273 | vmcb_set_intercept(&vmcb->control, INTERCEPT_DR2_WRITE); |
| 274 | vmcb_set_intercept(&vmcb->control, INTERCEPT_DR3_WRITE); |
| 275 | vmcb_set_intercept(&vmcb->control, INTERCEPT_DR4_WRITE); |
| 276 | vmcb_set_intercept(&vmcb->control, INTERCEPT_DR5_WRITE); |
| 277 | vmcb_set_intercept(&vmcb->control, INTERCEPT_DR6_WRITE); |
| 278 | vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_WRITE); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 279 | |
| 280 | recalc_intercepts(svm); |
| 281 | } |
| 282 | |
| 283 | static inline void clr_dr_intercepts(struct vcpu_svm *svm) |
| 284 | { |
| 285 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 286 | |
Babu Moger | 30abaa88 | 2020-09-11 14:28:12 -0500 | [diff] [blame^] | 287 | vmcb->control.intercepts[INTERCEPT_DR] = 0; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 288 | |
| 289 | recalc_intercepts(svm); |
| 290 | } |
| 291 | |
| 292 | static inline void set_exception_intercept(struct vcpu_svm *svm, int bit) |
| 293 | { |
| 294 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 295 | |
| 296 | vmcb->control.intercept_exceptions |= (1U << bit); |
| 297 | |
| 298 | recalc_intercepts(svm); |
| 299 | } |
| 300 | |
| 301 | static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit) |
| 302 | { |
| 303 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 304 | |
| 305 | vmcb->control.intercept_exceptions &= ~(1U << bit); |
| 306 | |
| 307 | recalc_intercepts(svm); |
| 308 | } |
| 309 | |
Joerg Roedel | a284ba5 | 2020-06-25 10:03:24 +0200 | [diff] [blame] | 310 | static inline void svm_set_intercept(struct vcpu_svm *svm, int bit) |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 311 | { |
| 312 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 313 | |
| 314 | vmcb->control.intercept |= (1ULL << bit); |
| 315 | |
| 316 | recalc_intercepts(svm); |
| 317 | } |
| 318 | |
Joerg Roedel | a284ba5 | 2020-06-25 10:03:24 +0200 | [diff] [blame] | 319 | static inline void svm_clr_intercept(struct vcpu_svm *svm, int bit) |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 320 | { |
| 321 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 322 | |
| 323 | vmcb->control.intercept &= ~(1ULL << bit); |
| 324 | |
| 325 | recalc_intercepts(svm); |
| 326 | } |
| 327 | |
Joerg Roedel | a284ba5 | 2020-06-25 10:03:24 +0200 | [diff] [blame] | 328 | static inline bool svm_is_intercept(struct vcpu_svm *svm, int bit) |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 329 | { |
| 330 | return (svm->vmcb->control.intercept & (1ULL << bit)) != 0; |
| 331 | } |
| 332 | |
| 333 | static inline bool vgif_enabled(struct vcpu_svm *svm) |
| 334 | { |
| 335 | return !!(svm->vmcb->control.int_ctl & V_GIF_ENABLE_MASK); |
| 336 | } |
| 337 | |
| 338 | static inline void enable_gif(struct vcpu_svm *svm) |
| 339 | { |
| 340 | if (vgif_enabled(svm)) |
| 341 | svm->vmcb->control.int_ctl |= V_GIF_MASK; |
| 342 | else |
| 343 | svm->vcpu.arch.hflags |= HF_GIF_MASK; |
| 344 | } |
| 345 | |
| 346 | static inline void disable_gif(struct vcpu_svm *svm) |
| 347 | { |
| 348 | if (vgif_enabled(svm)) |
| 349 | svm->vmcb->control.int_ctl &= ~V_GIF_MASK; |
| 350 | else |
| 351 | svm->vcpu.arch.hflags &= ~HF_GIF_MASK; |
| 352 | } |
| 353 | |
| 354 | static inline bool gif_set(struct vcpu_svm *svm) |
| 355 | { |
| 356 | if (vgif_enabled(svm)) |
| 357 | return !!(svm->vmcb->control.int_ctl & V_GIF_MASK); |
| 358 | else |
| 359 | return !!(svm->vcpu.arch.hflags & HF_GIF_MASK); |
| 360 | } |
| 361 | |
| 362 | /* svm.c */ |
Krish Sadhukhan | 761e416 | 2020-07-08 00:39:56 +0000 | [diff] [blame] | 363 | #define MSR_CR3_LEGACY_RESERVED_MASK 0xfe7U |
| 364 | #define MSR_CR3_LEGACY_PAE_RESERVED_MASK 0x7U |
| 365 | #define MSR_CR3_LONG_RESERVED_MASK 0xfff0000000000fe7U |
| 366 | #define MSR_INVALID 0xffffffffU |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 367 | |
| 368 | u32 svm_msrpm_offset(u32 msr); |
| 369 | void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer); |
| 370 | void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0); |
| 371 | int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); |
Sean Christopherson | f55ac30 | 2020-03-20 14:28:12 -0700 | [diff] [blame] | 372 | void svm_flush_tlb(struct kvm_vcpu *vcpu); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 373 | void disable_nmi_singlestep(struct vcpu_svm *svm); |
Paolo Bonzini | cae96af | 2020-04-23 14:19:26 -0400 | [diff] [blame] | 374 | bool svm_smi_blocked(struct kvm_vcpu *vcpu); |
| 375 | bool svm_nmi_blocked(struct kvm_vcpu *vcpu); |
| 376 | bool svm_interrupt_blocked(struct kvm_vcpu *vcpu); |
Paolo Bonzini | ffdf7f9 | 2020-05-22 12:18:27 -0400 | [diff] [blame] | 377 | void svm_set_gif(struct vcpu_svm *svm, bool value); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 378 | |
| 379 | /* nested.c */ |
| 380 | |
| 381 | #define NESTED_EXIT_HOST 0 /* Exit handled on host level */ |
| 382 | #define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */ |
| 383 | #define NESTED_EXIT_CONTINUE 2 /* Further checks needed */ |
| 384 | |
Joerg Roedel | 01c3b2b | 2020-06-25 10:03:25 +0200 | [diff] [blame] | 385 | static inline bool nested_svm_virtualize_tpr(struct kvm_vcpu *vcpu) |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 386 | { |
Paolo Bonzini | e9fd761 | 2020-05-13 13:28:23 -0400 | [diff] [blame] | 387 | struct vcpu_svm *svm = to_svm(vcpu); |
| 388 | |
| 389 | return is_guest_mode(vcpu) && (svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 390 | } |
| 391 | |
Paolo Bonzini | 55714cd | 2020-04-23 08:17:28 -0400 | [diff] [blame] | 392 | static inline bool nested_exit_on_smi(struct vcpu_svm *svm) |
| 393 | { |
Paolo Bonzini | e670bf6 | 2020-05-13 13:16:12 -0400 | [diff] [blame] | 394 | return (svm->nested.ctl.intercept & (1ULL << INTERCEPT_SMI)); |
Paolo Bonzini | 55714cd | 2020-04-23 08:17:28 -0400 | [diff] [blame] | 395 | } |
| 396 | |
Paolo Bonzini | fc6f7c0 | 2020-04-23 18:02:45 -0400 | [diff] [blame] | 397 | static inline bool nested_exit_on_intr(struct vcpu_svm *svm) |
| 398 | { |
Paolo Bonzini | e670bf6 | 2020-05-13 13:16:12 -0400 | [diff] [blame] | 399 | return (svm->nested.ctl.intercept & (1ULL << INTERCEPT_INTR)); |
Paolo Bonzini | fc6f7c0 | 2020-04-23 18:02:45 -0400 | [diff] [blame] | 400 | } |
| 401 | |
Paolo Bonzini | bbdad0b | 2020-04-23 08:06:43 -0400 | [diff] [blame] | 402 | static inline bool nested_exit_on_nmi(struct vcpu_svm *svm) |
| 403 | { |
Paolo Bonzini | e670bf6 | 2020-05-13 13:16:12 -0400 | [diff] [blame] | 404 | return (svm->nested.ctl.intercept & (1ULL << INTERCEPT_NMI)); |
Paolo Bonzini | bbdad0b | 2020-04-23 08:06:43 -0400 | [diff] [blame] | 405 | } |
| 406 | |
Vitaly Kuznetsov | 59cd9bc | 2020-07-10 16:11:52 +0200 | [diff] [blame] | 407 | int enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa, |
| 408 | struct vmcb *nested_vmcb); |
Paolo Bonzini | c513f48 | 2020-05-18 13:08:37 -0400 | [diff] [blame] | 409 | void svm_leave_nested(struct vcpu_svm *svm); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 410 | int nested_svm_vmrun(struct vcpu_svm *svm); |
| 411 | void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb); |
| 412 | int nested_svm_vmexit(struct vcpu_svm *svm); |
| 413 | int nested_svm_exit_handled(struct vcpu_svm *svm); |
| 414 | int nested_svm_check_permissions(struct vcpu_svm *svm); |
| 415 | int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr, |
| 416 | bool has_error_code, u32 error_code); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 417 | int nested_svm_exit_special(struct vcpu_svm *svm); |
Paolo Bonzini | 2d8a42b | 2020-05-22 03:50:14 -0400 | [diff] [blame] | 418 | void sync_nested_vmcb_control(struct vcpu_svm *svm); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 419 | |
Paolo Bonzini | 33b2217 | 2020-04-17 10:24:18 -0400 | [diff] [blame] | 420 | extern struct kvm_x86_nested_ops svm_nested_ops; |
| 421 | |
Joerg Roedel | ef0f649 | 2020-03-31 12:17:38 -0400 | [diff] [blame] | 422 | /* avic.c */ |
| 423 | |
| 424 | #define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFF) |
| 425 | #define AVIC_LOGICAL_ID_ENTRY_VALID_BIT 31 |
| 426 | #define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31) |
| 427 | |
| 428 | #define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK (0xFFULL) |
| 429 | #define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK (0xFFFFFFFFFFULL << 12) |
| 430 | #define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK (1ULL << 62) |
| 431 | #define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK (1ULL << 63) |
| 432 | |
| 433 | #define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL |
| 434 | |
| 435 | extern int avic; |
| 436 | |
| 437 | static inline void avic_update_vapic_bar(struct vcpu_svm *svm, u64 data) |
| 438 | { |
| 439 | svm->vmcb->control.avic_vapic_bar = data & VMCB_AVIC_APIC_BAR_MASK; |
Joerg Roedel | 06e7852 | 2020-06-25 10:03:23 +0200 | [diff] [blame] | 440 | vmcb_mark_dirty(svm->vmcb, VMCB_AVIC); |
Joerg Roedel | ef0f649 | 2020-03-31 12:17:38 -0400 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | static inline bool avic_vcpu_is_running(struct kvm_vcpu *vcpu) |
| 444 | { |
| 445 | struct vcpu_svm *svm = to_svm(vcpu); |
| 446 | u64 *entry = svm->avic_physical_id_cache; |
| 447 | |
| 448 | if (!entry) |
| 449 | return false; |
| 450 | |
| 451 | return (READ_ONCE(*entry) & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK); |
| 452 | } |
| 453 | |
| 454 | int avic_ga_log_notifier(u32 ga_tag); |
| 455 | void avic_vm_destroy(struct kvm *kvm); |
| 456 | int avic_vm_init(struct kvm *kvm); |
| 457 | void avic_init_vmcb(struct vcpu_svm *svm); |
| 458 | void svm_toggle_avic_for_irq_window(struct kvm_vcpu *vcpu, bool activate); |
| 459 | int avic_incomplete_ipi_interception(struct vcpu_svm *svm); |
| 460 | int avic_unaccelerated_access_interception(struct vcpu_svm *svm); |
| 461 | int avic_init_vcpu(struct vcpu_svm *svm); |
| 462 | void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu); |
| 463 | void avic_vcpu_put(struct kvm_vcpu *vcpu); |
| 464 | void avic_post_state_restore(struct kvm_vcpu *vcpu); |
| 465 | void svm_set_virtual_apic_mode(struct kvm_vcpu *vcpu); |
| 466 | void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu); |
| 467 | bool svm_check_apicv_inhibit_reasons(ulong bit); |
| 468 | void svm_pre_update_apicv_exec_ctrl(struct kvm *kvm, bool activate); |
| 469 | void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap); |
| 470 | void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr); |
| 471 | void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr); |
| 472 | int svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec); |
| 473 | bool svm_dy_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu); |
| 474 | int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq, |
| 475 | uint32_t guest_irq, bool set); |
| 476 | void svm_vcpu_blocking(struct kvm_vcpu *vcpu); |
| 477 | void svm_vcpu_unblocking(struct kvm_vcpu *vcpu); |
| 478 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 479 | /* sev.c */ |
| 480 | |
| 481 | extern unsigned int max_sev_asid; |
| 482 | |
| 483 | static inline bool sev_guest(struct kvm *kvm) |
| 484 | { |
| 485 | #ifdef CONFIG_KVM_AMD_SEV |
| 486 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
| 487 | |
| 488 | return sev->active; |
| 489 | #else |
| 490 | return false; |
| 491 | #endif |
| 492 | } |
| 493 | |
| 494 | static inline bool svm_sev_enabled(void) |
| 495 | { |
| 496 | return IS_ENABLED(CONFIG_KVM_AMD_SEV) ? max_sev_asid : 0; |
| 497 | } |
| 498 | |
| 499 | void sev_vm_destroy(struct kvm *kvm); |
| 500 | int svm_mem_enc_op(struct kvm *kvm, void __user *argp); |
| 501 | int svm_register_enc_region(struct kvm *kvm, |
| 502 | struct kvm_enc_region *range); |
| 503 | int svm_unregister_enc_region(struct kvm *kvm, |
| 504 | struct kvm_enc_region *range); |
| 505 | void pre_sev_run(struct vcpu_svm *svm, int cpu); |
| 506 | int __init sev_hardware_setup(void); |
| 507 | void sev_hardware_teardown(void); |
| 508 | |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 509 | #endif |