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 #include <linux/bits.h>
21
22 #include <asm/svm.h>
23 #include <asm/sev-common.h>
24
25 #include "cpuid.h"
26 #include "regs.h"
27 #include "x86.h"
28 #include "pmu.h"
29
30 /*
31 * Helpers to convert to/from physical addresses for pages whose address is
32 * consumed directly by hardware. Even though it's a physical address, SVM
33 * often restricts the address to the natural width, hence 'unsigned long'
34 * instead of 'hpa_t'.
35 */
__sme_page_pa(struct page * page)36 static inline unsigned long __sme_page_pa(struct page *page)
37 {
38 return __sme_set(page_to_pfn(page) << PAGE_SHIFT);
39 }
40
__sme_pa_to_page(unsigned long pa)41 static inline struct page *__sme_pa_to_page(unsigned long pa)
42 {
43 return pfn_to_page(__sme_clr(pa) >> PAGE_SHIFT);
44 }
45
46 #define IOPM_SIZE PAGE_SIZE * 3
47 #define MSRPM_SIZE PAGE_SIZE * 2
48
49 extern bool gmet_enabled;
50 extern bool npt_enabled;
51 extern int nrips;
52 extern int vgif;
53 extern bool intercept_smi;
54 extern bool vnmi;
55 extern int lbrv;
56
57 extern int tsc_aux_uret_slot __ro_after_init;
58
59 extern struct kvm_x86_ops svm_x86_ops __initdata;
60
61 /*
62 * Clean bits in VMCB.
63 * VMCB_ALL_CLEAN_MASK might also need to
64 * be updated if this enum is modified.
65 */
66 enum {
67 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
68 pause filter count */
69 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
70 VMCB_ASID, /* ASID */
71 VMCB_INTR, /* int_ctl, int_vector */
72 VMCB_NPT, /* npt_en, nCR3, gPAT */
73 VMCB_CR, /* CR0, CR3, CR4, EFER */
74 VMCB_DR, /* DR6, DR7 */
75 VMCB_DT, /* GDT, IDT */
76 VMCB_SEG, /* CS, DS, SS, ES, CPL */
77 VMCB_CR2, /* CR2 only */
78 VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
79 VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE,
80 * AVIC PHYSICAL_TABLE pointer,
81 * AVIC LOGICAL_TABLE pointer
82 */
83 VMCB_CET, /* S_CET, SSP, ISST_ADDR */
84 VMCB_SW = 31, /* Reserved for hypervisor/software use */
85 };
86
87 #define VMCB_ALL_CLEAN_MASK ( \
88 (1U << VMCB_INTERCEPTS) | (1U << VMCB_PERM_MAP) | \
89 (1U << VMCB_ASID) | (1U << VMCB_INTR) | \
90 (1U << VMCB_NPT) | (1U << VMCB_CR) | (1U << VMCB_DR) | \
91 (1U << VMCB_DT) | (1U << VMCB_SEG) | (1U << VMCB_CR2) | \
92 (1U << VMCB_LBR) | (1U << VMCB_AVIC) | (1U << VMCB_CET) | \
93 (1U << VMCB_SW))
94
95 /* TPR and CR2 are always written before VMRUN */
96 #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
97
98 #ifdef CONFIG_KVM_AMD_SEV
99 struct kvm_sev_info {
100 bool active; /* SEV enabled guest */
101 bool es_active; /* SEV-ES enabled guest */
102 bool need_init; /* waiting for SEV_INIT2 */
103 unsigned int asid; /* ASID used for this guest */
104 unsigned int handle; /* SEV firmware handle */
105 int fd; /* SEV device fd */
106 unsigned long policy;
107 unsigned long pages_locked; /* Number of pages locked */
108 struct list_head regions_list; /* List of registered regions */
109 u64 ap_jump_table; /* SEV-ES AP Jump Table address */
110 u64 vmsa_features;
111 u16 ghcb_version; /* Highest guest GHCB protocol version allowed */
112 /* The three fields below are protected by sev_mirror_lock */
113 struct kvm *enc_context_owner; /* Owner of copied encryption context */
114 struct list_head mirror_vms; /* List of VMs mirroring */
115 struct list_head mirror_entry; /* Use as a list entry of mirrors */
116 struct misc_cg *misc_cg; /* For misc cgroup accounting */
117 atomic_t migration_in_progress;
118 void *snp_context; /* SNP guest context page */
119 void *guest_req_buf; /* Bounce buffer for SNP Guest Request input */
120 void *guest_resp_buf; /* Bounce buffer for SNP Guest Request output */
121 struct mutex guest_req_mutex; /* Must acquire before using bounce buffers */
122 cpumask_var_t have_run_cpus; /* CPUs that have done VMRUN for this VM. */
123 bool snp_certs_enabled; /* SNP certificate-fetching support. */
124 };
125 #endif
126
127 struct kvm_svm {
128 struct kvm kvm;
129
130 /* Struct members for AVIC */
131 u32 avic_vm_id;
132 u32 *avic_logical_id_table;
133 u64 *avic_physical_id_table;
134 struct hlist_node hnode;
135
136 #ifdef CONFIG_KVM_AMD_SEV
137 struct kvm_sev_info sev_info;
138 #endif
139 };
140
141 struct kvm_vcpu;
142
143 struct kvm_vmcb_info {
144 struct vmcb *ptr;
145 unsigned long pa;
146 int cpu;
147 uint64_t asid_generation;
148 };
149
150 struct vmcb_save_area_cached {
151 struct vmcb_seg es;
152 struct vmcb_seg cs;
153 struct vmcb_seg ss;
154 struct vmcb_seg ds;
155 struct vmcb_seg gdtr;
156 struct vmcb_seg idtr;
157 u8 cpl;
158 u64 efer;
159 u64 cr4;
160 u64 cr3;
161 u64 cr0;
162 u64 dr7;
163 u64 dr6;
164 u64 rflags;
165 u64 rip;
166 u64 rsp;
167 u64 s_cet;
168 u64 ssp;
169 u64 isst_addr;
170 u64 rax;
171 u64 cr2;
172 u64 g_pat;
173 u64 dbgctl;
174 u64 br_from;
175 u64 br_to;
176 u64 last_excp_from;
177 u64 last_excp_to;
178 };
179
180 struct vmcb_ctrl_area_cached {
181 u32 intercepts[MAX_INTERCEPT];
182 u16 pause_filter_thresh;
183 u16 pause_filter_count;
184 u64 iopm_base_pa;
185 u64 msrpm_base_pa;
186 u64 tsc_offset;
187 u32 asid;
188 u8 tlb_ctl;
189 u8 erap_ctl;
190 u32 int_ctl;
191 u32 int_vector;
192 u32 int_state;
193 u64 exit_code;
194 u64 exit_info_1;
195 u64 exit_info_2;
196 u32 exit_int_info;
197 u32 exit_int_info_err;
198 u64 misc_ctl;
199 u32 event_inj;
200 u32 event_inj_err;
201 u64 next_rip;
202 u64 nested_cr3;
203 u64 misc_ctl2;
204 u32 clean;
205 union {
206 #if IS_ENABLED(CONFIG_HYPERV) || IS_ENABLED(CONFIG_KVM_HYPERV)
207 struct hv_vmcb_enlightenments hv_enlightenments;
208 #endif
209 u8 reserved_sw[32];
210 };
211 };
212
213 struct svm_nested_state {
214 struct kvm_vmcb_info vmcb02;
215 u64 hsave_msr;
216 u64 vm_cr_msr;
217 u64 vmcb12_gpa;
218 u64 last_vmcb12_gpa;
219 u64 last_bus_lock_rip;
220
221 /*
222 * The MSR permissions map used for vmcb02, which is the merge result
223 * of vmcb01 and vmcb12
224 */
225 void *msrpm;
226
227 /* cache for control fields of the guest */
228 struct vmcb_ctrl_area_cached ctl;
229
230 /*
231 * Note: this struct is not kept up-to-date while L2 runs; it is only
232 * valid within nested_svm_vmrun.
233 */
234 struct vmcb_save_area_cached save;
235
236 bool initialized;
237
238 /*
239 * Indicates whether MSR bitmap for L2 needs to be rebuilt due to
240 * changes in MSR bitmap for L1 or switching to a different L2. Note,
241 * this flag can only be used reliably in conjunction with a paravirt L1
242 * which informs L0 whether any changes to MSR bitmap for L2 were done
243 * on its side.
244 */
245 bool force_msr_bitmap_recalc;
246 };
247
248 struct vcpu_sev_es_state {
249 /* SEV-ES support */
250 struct sev_es_save_area *vmsa;
251 struct ghcb *ghcb;
252 u8 valid_bitmap[16];
253 struct kvm_host_map ghcb_map;
254 bool received_first_sipi;
255 unsigned int ap_reset_hold_type;
256
257 /* SEV-ES scratch area support */
258 u64 sw_scratch;
259 void *ghcb_sa;
260 u32 ghcb_sa_len;
261 bool ghcb_sa_sync;
262 bool ghcb_sa_free;
263
264 /* SNP Page-State-Change buffer entries currently being processed */
265 struct {
266 u16 cur_idx;
267 u16 end_idx;
268 u16 batch_size;
269 bool is_2m;
270 } psc;
271
272 u64 ghcb_registered_gpa;
273
274 struct mutex snp_vmsa_mutex; /* Used to handle concurrent updates of VMSA. */
275 gpa_t snp_pending_vmsa_gpa;
276 gpa_t snp_guest_vmsa_gpa;
277 bool snp_ap_waiting_for_reset;
278 bool snp_has_guest_vmsa;
279 };
280
281 struct vcpu_svm {
282 struct kvm_vcpu vcpu;
283 /* vmcb always points at current_vmcb->ptr, it's purely a shorthand. */
284 struct vmcb *vmcb;
285 struct kvm_vmcb_info vmcb01;
286 struct kvm_vmcb_info *current_vmcb;
287 u32 asid;
288 u32 sysenter_esp_hi;
289 u32 sysenter_eip_hi;
290 uint64_t tsc_aux;
291
292 u64 msr_decfg;
293
294 u64 next_rip;
295
296 u64 spec_ctrl;
297
298 u64 tsc_ratio_msr;
299 /*
300 * Contains guest-controlled bits of VIRT_SPEC_CTRL, which will be
301 * translated into the appropriate L2_CFG bits on the host to
302 * perform speculative control.
303 */
304 u64 virt_spec_ctrl;
305
306 void *msrpm;
307
308 ulong nmi_iret_rip;
309
310 struct svm_nested_state nested;
311
312 /* NMI mask value, used when vNMI is not enabled */
313 bool nmi_masked;
314
315 /*
316 * True when NMIs are still masked but guest IRET was just intercepted
317 * and KVM is waiting for RIP to change, which will signal that the
318 * intercepted IRET was retired and thus NMI can be unmasked.
319 */
320 bool awaiting_iret_completion;
321
322 /*
323 * Set when KVM is awaiting IRET completion and needs to inject NMIs as
324 * soon as the IRET completes (e.g. NMI is pending injection). KVM
325 * temporarily steals RFLAGS.TF to single-step the guest in this case
326 * in order to regain control as soon as the NMI-blocking condition
327 * goes away.
328 */
329 bool nmi_singlestep;
330 u64 nmi_singlestep_guest_rflags;
331
332 bool nmi_l1_to_l2;
333
334 unsigned long soft_int_csbase;
335 unsigned long soft_int_old_rip;
336 unsigned long soft_int_next_rip;
337 bool soft_int_injected;
338
339 u32 ldr_reg;
340 u32 dfr_reg;
341
342 /* This is essentially a shadow of the vCPU's actual entry in the
343 * Physical ID table that is programmed into the VMCB, i.e. that is
344 * seen by the CPU. If IPI virtualization is disabled, IsRunning is
345 * only ever set in the shadow, i.e. is never propagated to the "real"
346 * table, so that hardware never sees IsRunning=1.
347 */
348 u64 avic_physical_id_entry;
349
350 /*
351 * Per-vCPU list of irqfds that are eligible to post IRQs directly to
352 * the vCPU (a.k.a. device posted IRQs, a.k.a. IRQ bypass). The list
353 * is used to reconfigure IRTEs when the vCPU is loaded/put (to set the
354 * target pCPU), when AVIC is toggled on/off (to (de)activate bypass),
355 * and if the irqfd becomes ineligible for posting (to put the IRTE
356 * back into remapped mode).
357 */
358 struct list_head ir_list;
359 raw_spinlock_t ir_list_lock;
360
361 struct vcpu_sev_es_state sev_es;
362
363 bool guest_state_loaded;
364
365 bool avic_irq_window;
366 bool x2avic_msrs_intercepted;
367 bool lbr_msrs_intercepted;
368
369 /* Guest GIF value, used when vGIF is not enabled */
370 bool guest_gif;
371 };
372
373 struct svm_cpu_data {
374 u64 asid_generation;
375 u32 max_asid;
376 u32 next_asid;
377 u32 min_asid;
378
379 bool flush_all_asids;
380 bool bp_spec_reduce_set;
381
382 struct vmcb *save_area;
383 unsigned long save_area_pa;
384
385 /* index = sev_asid, value = vmcb pointer */
386 struct vmcb **sev_vmcbs;
387 };
388
389 DECLARE_PER_CPU(struct svm_cpu_data, svm_data);
390
to_kvm_svm(struct kvm * kvm)391 static __always_inline struct kvm_svm *to_kvm_svm(struct kvm *kvm)
392 {
393 return container_of(kvm, struct kvm_svm, kvm);
394 }
395
396 #ifdef CONFIG_KVM_AMD_SEV
to_kvm_sev_info(struct kvm * kvm)397 static __always_inline struct kvm_sev_info *to_kvm_sev_info(struct kvm *kvm)
398 {
399 return &to_kvm_svm(kvm)->sev_info;
400 }
401
____sev_guest(struct kvm * kvm)402 static __always_inline bool ____sev_guest(struct kvm *kvm)
403 {
404 return to_kvm_sev_info(kvm)->active;
405 }
____sev_es_guest(struct kvm * kvm)406 static __always_inline bool ____sev_es_guest(struct kvm *kvm)
407 {
408 struct kvm_sev_info *sev = to_kvm_sev_info(kvm);
409
410 return sev->es_active && !WARN_ON_ONCE(!sev->active);
411 }
412
____sev_snp_guest(struct kvm * kvm)413 static __always_inline bool ____sev_snp_guest(struct kvm *kvm)
414 {
415 struct kvm_sev_info *sev = to_kvm_sev_info(kvm);
416
417 return (sev->vmsa_features & SVM_SEV_FEAT_SNP_ACTIVE) &&
418 !WARN_ON_ONCE(!____sev_es_guest(kvm));
419 }
420
is_sev_guest(struct kvm_vcpu * vcpu)421 static __always_inline bool is_sev_guest(struct kvm_vcpu *vcpu)
422 {
423 return ____sev_guest(vcpu->kvm);
424 }
is_sev_es_guest(struct kvm_vcpu * vcpu)425 static __always_inline bool is_sev_es_guest(struct kvm_vcpu *vcpu)
426 {
427 return ____sev_es_guest(vcpu->kvm);
428 }
429
is_sev_snp_guest(struct kvm_vcpu * vcpu)430 static __always_inline bool is_sev_snp_guest(struct kvm_vcpu *vcpu)
431 {
432 return ____sev_snp_guest(vcpu->kvm);
433 }
434 #else
is_sev_guest(struct kvm_vcpu * vcpu)435 static __always_inline bool is_sev_guest(struct kvm_vcpu *vcpu)
436 {
437 return false;
438 }
is_sev_es_guest(struct kvm_vcpu * vcpu)439 static __always_inline bool is_sev_es_guest(struct kvm_vcpu *vcpu)
440 {
441 return false;
442 }
443
is_sev_snp_guest(struct kvm_vcpu * vcpu)444 static __always_inline bool is_sev_snp_guest(struct kvm_vcpu *vcpu)
445 {
446 return false;
447 }
448 #endif
449
ghcb_gpa_is_registered(struct vcpu_svm * svm,u64 val)450 static inline bool ghcb_gpa_is_registered(struct vcpu_svm *svm, u64 val)
451 {
452 return svm->sev_es.ghcb_registered_gpa == val;
453 }
454
vmcb_mark_all_dirty(struct vmcb * vmcb)455 static inline void vmcb_mark_all_dirty(struct vmcb *vmcb)
456 {
457 vmcb->control.clean = 0;
458 }
459
vmcb_mark_all_clean(struct vmcb * vmcb)460 static inline void vmcb_mark_all_clean(struct vmcb *vmcb)
461 {
462 vmcb->control.clean = VMCB_ALL_CLEAN_MASK
463 & ~VMCB_ALWAYS_DIRTY_MASK;
464 }
465
vmcb_mark_dirty(struct vmcb * vmcb,int bit)466 static inline void vmcb_mark_dirty(struct vmcb *vmcb, int bit)
467 {
468 vmcb->control.clean &= ~(1 << bit);
469 }
470
vmcb12_is_dirty(struct vmcb_ctrl_area_cached * control,int bit)471 static inline bool vmcb12_is_dirty(struct vmcb_ctrl_area_cached *control, int bit)
472 {
473 return !test_bit(bit, (unsigned long *)&control->clean);
474 }
475
vmcb_set_gpat(struct vmcb * vmcb,u64 data)476 static inline void vmcb_set_gpat(struct vmcb *vmcb, u64 data)
477 {
478 vmcb->save.g_pat = data;
479 vmcb_mark_dirty(vmcb, VMCB_NPT);
480 }
481
to_svm(struct kvm_vcpu * vcpu)482 static __always_inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
483 {
484 return container_of(vcpu, struct vcpu_svm, vcpu);
485 }
486
svm_is_vmrun_failure(u64 exit_code)487 static inline bool svm_is_vmrun_failure(u64 exit_code)
488 {
489 if (cpu_feature_enabled(X86_FEATURE_HYPERVISOR))
490 return (u32)exit_code == (u32)SVM_EXIT_ERR;
491
492 return exit_code == SVM_EXIT_ERR;
493 }
494
495 /*
496 * Only the PDPTRs are loaded on demand into the shadow MMU. All other
497 * fields are synchronized on VM-Exit, because accessing the VMCB is cheap.
498 *
499 * CR3 might be out of date in the VMCB but it is not marked dirty; instead,
500 * KVM_REQ_LOAD_MMU_PGD is always requested when the cached vcpu->arch.cr3
501 * is changed. svm_load_mmu_pgd() then syncs the new CR3 value into the VMCB.
502 */
503 #define SVM_REGS_LAZY_LOAD_SET (BIT(VCPU_REG_PDPTR))
504
__vmcb_set_intercept(unsigned long * intercepts,u32 bit)505 static inline void __vmcb_set_intercept(unsigned long *intercepts, u32 bit)
506 {
507 WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
508 __set_bit(bit, intercepts);
509 }
510
__vmcb_clr_intercept(unsigned long * intercepts,u32 bit)511 static inline void __vmcb_clr_intercept(unsigned long *intercepts, u32 bit)
512 {
513 WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
514 __clear_bit(bit, intercepts);
515 }
516
__vmcb_is_intercept(unsigned long * intercepts,u32 bit)517 static inline bool __vmcb_is_intercept(unsigned long *intercepts, u32 bit)
518 {
519 WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
520 return test_bit(bit, intercepts);
521 }
522
vmcb_set_intercept(struct vmcb_control_area * control,u32 bit)523 static inline void vmcb_set_intercept(struct vmcb_control_area *control, u32 bit)
524 {
525 __vmcb_set_intercept((unsigned long *)&control->intercepts, bit);
526 }
527
vmcb_clr_intercept(struct vmcb_control_area * control,u32 bit)528 static inline void vmcb_clr_intercept(struct vmcb_control_area *control, u32 bit)
529 {
530 __vmcb_clr_intercept((unsigned long *)&control->intercepts, bit);
531 }
532
vmcb_is_intercept(struct vmcb_control_area * control,u32 bit)533 static inline bool vmcb_is_intercept(struct vmcb_control_area *control, u32 bit)
534 {
535 return __vmcb_is_intercept((unsigned long *)&control->intercepts, bit);
536 }
537
vmcb12_clr_intercept(struct vmcb_ctrl_area_cached * control,u32 bit)538 static inline void vmcb12_clr_intercept(struct vmcb_ctrl_area_cached *control, u32 bit)
539 {
540 __vmcb_clr_intercept((unsigned long *)&control->intercepts, bit);
541 }
542
vmcb12_is_intercept(struct vmcb_ctrl_area_cached * control,u32 bit)543 static inline bool vmcb12_is_intercept(struct vmcb_ctrl_area_cached *control, u32 bit)
544 {
545 return __vmcb_is_intercept((unsigned long *)&control->intercepts, bit);
546 }
547
548 void nested_vmcb02_recalc_intercepts(struct vcpu_svm *svm);
549
svm_mark_intercepts_dirty(struct vcpu_svm * svm)550 static inline void svm_mark_intercepts_dirty(struct vcpu_svm *svm)
551 {
552 vmcb_mark_dirty(svm->vmcb01.ptr, VMCB_INTERCEPTS);
553
554 /*
555 * If L2 is active, recalculate the intercepts for vmcb02 to account
556 * for the changes made to vmcb01. All intercept configuration is done
557 * for vmcb01 and then propagated to vmcb02 to combine KVM's intercepts
558 * with L1's intercepts (from the vmcb12 snapshot).
559 */
560 if (is_guest_mode(&svm->vcpu))
561 nested_vmcb02_recalc_intercepts(svm);
562 }
563
set_exception_intercept(struct vcpu_svm * svm,u32 bit)564 static inline void set_exception_intercept(struct vcpu_svm *svm, u32 bit)
565 {
566 struct vmcb *vmcb = svm->vmcb01.ptr;
567
568 WARN_ON_ONCE(bit >= 32);
569 vmcb_set_intercept(&vmcb->control, INTERCEPT_EXCEPTION_OFFSET + bit);
570
571 svm_mark_intercepts_dirty(svm);
572 }
573
clr_exception_intercept(struct vcpu_svm * svm,u32 bit)574 static inline void clr_exception_intercept(struct vcpu_svm *svm, u32 bit)
575 {
576 struct vmcb *vmcb = svm->vmcb01.ptr;
577
578 WARN_ON_ONCE(bit >= 32);
579 vmcb_clr_intercept(&vmcb->control, INTERCEPT_EXCEPTION_OFFSET + bit);
580
581 svm_mark_intercepts_dirty(svm);
582 }
583
svm_set_intercept(struct vcpu_svm * svm,int bit)584 static inline void svm_set_intercept(struct vcpu_svm *svm, int bit)
585 {
586 struct vmcb *vmcb = svm->vmcb01.ptr;
587
588 vmcb_set_intercept(&vmcb->control, bit);
589
590 svm_mark_intercepts_dirty(svm);
591 }
592
svm_clr_intercept(struct vcpu_svm * svm,int bit)593 static inline void svm_clr_intercept(struct vcpu_svm *svm, int bit)
594 {
595 struct vmcb *vmcb = svm->vmcb01.ptr;
596
597 vmcb_clr_intercept(&vmcb->control, bit);
598
599 svm_mark_intercepts_dirty(svm);
600 }
601
svm_is_intercept(struct vcpu_svm * svm,int bit)602 static inline bool svm_is_intercept(struct vcpu_svm *svm, int bit)
603 {
604 return vmcb_is_intercept(&svm->vmcb->control, bit);
605 }
606
nested_vgif_enabled(struct vcpu_svm * svm)607 static inline bool nested_vgif_enabled(struct vcpu_svm *svm)
608 {
609 return guest_cpu_cap_has(&svm->vcpu, X86_FEATURE_VGIF) &&
610 (svm->nested.ctl.int_ctl & V_GIF_ENABLE_MASK);
611 }
612
get_vgif_vmcb(struct vcpu_svm * svm)613 static inline struct vmcb *get_vgif_vmcb(struct vcpu_svm *svm)
614 {
615 if (!vgif)
616 return NULL;
617
618 if (is_guest_mode(&svm->vcpu) && !nested_vgif_enabled(svm))
619 return svm->nested.vmcb02.ptr;
620 else
621 return svm->vmcb01.ptr;
622 }
623
enable_gif(struct vcpu_svm * svm)624 static inline void enable_gif(struct vcpu_svm *svm)
625 {
626 struct vmcb *vmcb = get_vgif_vmcb(svm);
627
628 if (vmcb)
629 vmcb->control.int_ctl |= V_GIF_MASK;
630 else
631 svm->guest_gif = true;
632 }
633
disable_gif(struct vcpu_svm * svm)634 static inline void disable_gif(struct vcpu_svm *svm)
635 {
636 struct vmcb *vmcb = get_vgif_vmcb(svm);
637
638 if (vmcb)
639 vmcb->control.int_ctl &= ~V_GIF_MASK;
640 else
641 svm->guest_gif = false;
642 }
643
gif_set(struct vcpu_svm * svm)644 static inline bool gif_set(struct vcpu_svm *svm)
645 {
646 struct vmcb *vmcb = get_vgif_vmcb(svm);
647
648 if (vmcb)
649 return !!(vmcb->control.int_ctl & V_GIF_MASK);
650 else
651 return svm->guest_gif;
652 }
653
nested_npt_enabled(struct vcpu_svm * svm)654 static inline bool nested_npt_enabled(struct vcpu_svm *svm)
655 {
656 return svm->nested.ctl.misc_ctl & SVM_MISC_ENABLE_NP;
657 }
658
l2_has_separate_pat(struct kvm_vcpu * vcpu)659 static inline bool l2_has_separate_pat(struct kvm_vcpu *vcpu)
660 {
661 /*
662 * If KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT is disabled while a vCPU
663 * is running, the L2 IA32_PAT semantics for that vCPU are undefined.
664 */
665 return nested_npt_enabled(to_svm(vcpu)) &&
666 !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT);
667 }
668
nested_vnmi_enabled(struct vcpu_svm * svm)669 static inline bool nested_vnmi_enabled(struct vcpu_svm *svm)
670 {
671 return guest_cpu_cap_has(&svm->vcpu, X86_FEATURE_VNMI) &&
672 (svm->nested.ctl.int_ctl & V_NMI_ENABLE_MASK);
673 }
674
is_x2apic_msrpm_offset(u32 offset)675 static inline bool is_x2apic_msrpm_offset(u32 offset)
676 {
677 /* 4 msrs per u8, and 4 u8 in u32 */
678 u32 msr = offset * 16;
679
680 return (msr >= APIC_BASE_MSR) &&
681 (msr < (APIC_BASE_MSR + 0x100));
682 }
683
get_vnmi_vmcb_l1(struct vcpu_svm * svm)684 static inline struct vmcb *get_vnmi_vmcb_l1(struct vcpu_svm *svm)
685 {
686 if (!vnmi)
687 return NULL;
688
689 if (is_guest_mode(&svm->vcpu))
690 return NULL;
691 else
692 return svm->vmcb01.ptr;
693 }
694
is_vnmi_enabled(struct vcpu_svm * svm)695 static inline bool is_vnmi_enabled(struct vcpu_svm *svm)
696 {
697 struct vmcb *vmcb = get_vnmi_vmcb_l1(svm);
698
699 if (vmcb)
700 return !!(vmcb->control.int_ctl & V_NMI_ENABLE_MASK);
701 else
702 return false;
703 }
704
svm_vmgexit_set_return_code(struct vcpu_svm * svm,u64 response,u64 data)705 static inline void svm_vmgexit_set_return_code(struct vcpu_svm *svm,
706 u64 response, u64 data)
707 {
708 ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, response);
709 ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, data);
710 }
711
svm_vmgexit_inject_exception(struct vcpu_svm * svm,u8 vector)712 static inline void svm_vmgexit_inject_exception(struct vcpu_svm *svm, u8 vector)
713 {
714 u64 data = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT | vector;
715
716 svm_vmgexit_set_return_code(svm, GHCB_HV_RESP_ISSUE_EXCEPTION, data);
717 }
718
svm_vmgexit_bad_input(struct vcpu_svm * svm,u64 suberror)719 static inline void svm_vmgexit_bad_input(struct vcpu_svm *svm, u64 suberror)
720 {
721 svm_vmgexit_set_return_code(svm, GHCB_HV_RESP_MALFORMED_INPUT, suberror);
722 }
723
svm_vmgexit_success(struct vcpu_svm * svm,u64 data)724 static inline void svm_vmgexit_success(struct vcpu_svm *svm, u64 data)
725 {
726 svm_vmgexit_set_return_code(svm, GHCB_HV_RESP_NO_ACTION, data);
727 }
728
svm_vmgexit_no_action(struct vcpu_svm * svm,u64 data)729 static inline void svm_vmgexit_no_action(struct vcpu_svm *svm, u64 data)
730 {
731 svm_vmgexit_set_return_code(svm, GHCB_HV_RESP_NO_ACTION, data);
732 }
733
734 /*
735 * The MSRPM is 8KiB in size, divided into four 2KiB ranges (the fourth range
736 * is reserved). Each MSR within a range is covered by two bits, one each for
737 * read (bit 0) and write (bit 1), where a bit value of '1' means intercepted.
738 */
739 #define SVM_MSRPM_BYTES_PER_RANGE 2048
740 #define SVM_BITS_PER_MSR 2
741 #define SVM_MSRS_PER_BYTE (BITS_PER_BYTE / SVM_BITS_PER_MSR)
742 #define SVM_MSRS_PER_RANGE (SVM_MSRPM_BYTES_PER_RANGE * SVM_MSRS_PER_BYTE)
743 static_assert(SVM_MSRS_PER_RANGE == 8192);
744 #define SVM_MSRPM_OFFSET_MASK (SVM_MSRS_PER_RANGE - 1)
745
svm_msrpm_bit_nr(u32 msr)746 static __always_inline int svm_msrpm_bit_nr(u32 msr)
747 {
748 int range_nr;
749
750 switch (msr & ~SVM_MSRPM_OFFSET_MASK) {
751 case 0:
752 range_nr = 0;
753 break;
754 case 0xc0000000:
755 range_nr = 1;
756 break;
757 case 0xc0010000:
758 range_nr = 2;
759 break;
760 default:
761 return -EINVAL;
762 }
763
764 return range_nr * SVM_MSRPM_BYTES_PER_RANGE * BITS_PER_BYTE +
765 (msr & SVM_MSRPM_OFFSET_MASK) * SVM_BITS_PER_MSR;
766 }
767
768 #define __BUILD_SVM_MSR_BITMAP_HELPER(rtype, action, bitop, access, bit_rw) \
769 static inline rtype svm_##action##_msr_bitmap_##access(unsigned long *bitmap, \
770 u32 msr) \
771 { \
772 int bit_nr; \
773 \
774 bit_nr = svm_msrpm_bit_nr(msr); \
775 if (bit_nr < 0) \
776 return (rtype)true; \
777 \
778 return bitop##_bit(bit_nr + bit_rw, bitmap); \
779 }
780
781 #define BUILD_SVM_MSR_BITMAP_HELPERS(ret_type, action, bitop) \
782 __BUILD_SVM_MSR_BITMAP_HELPER(ret_type, action, bitop, read, 0) \
783 __BUILD_SVM_MSR_BITMAP_HELPER(ret_type, action, bitop, write, 1)
784
785 BUILD_SVM_MSR_BITMAP_HELPERS(bool, test, test)
786 BUILD_SVM_MSR_BITMAP_HELPERS(void, clear, __clear)
787 BUILD_SVM_MSR_BITMAP_HELPERS(void, set, __set)
788
789 #define DEBUGCTL_RESERVED_BITS (~DEBUGCTLMSR_LBR)
790
791 /* svm.c */
792 extern bool dump_invalid_vmcb;
793
794 void *svm_alloc_permissions_map(unsigned long size, gfp_t gfp_mask);
795
svm_vcpu_alloc_msrpm(void)796 static inline void *svm_vcpu_alloc_msrpm(void)
797 {
798 return svm_alloc_permissions_map(MSRPM_SIZE, GFP_KERNEL_ACCOUNT);
799 }
800
801 #define svm_copy_lbrs(to, from) \
802 do { \
803 (to)->dbgctl = (from)->dbgctl; \
804 (to)->br_from = (from)->br_from; \
805 (to)->br_to = (from)->br_to; \
806 (to)->last_excp_from = (from)->last_excp_from; \
807 (to)->last_excp_to = (from)->last_excp_to; \
808 } while (0)
809
810 void svm_vcpu_free_msrpm(void *msrpm);
811 void svm_enable_lbrv(struct kvm_vcpu *vcpu);
812 void svm_update_lbrv(struct kvm_vcpu *vcpu);
813
814 int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer);
815 void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
816 void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
817 void disable_nmi_singlestep(struct vcpu_svm *svm);
818 bool svm_smi_blocked(struct kvm_vcpu *vcpu);
819 bool svm_nmi_blocked(struct kvm_vcpu *vcpu);
820 bool svm_interrupt_blocked(struct kvm_vcpu *vcpu);
821 void svm_set_gif(struct vcpu_svm *svm, bool value);
822 int svm_invoke_exit_handler(struct kvm_vcpu *vcpu, u64 exit_code);
823 void set_msr_interception(struct kvm_vcpu *vcpu, u32 *msrpm, u32 msr,
824 int read, int write);
825 void svm_complete_interrupt_delivery(struct kvm_vcpu *vcpu, int delivery_mode,
826 int trig_mode, int vec);
827
828 void svm_set_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type, bool set);
829
svm_disable_intercept_for_msr(struct kvm_vcpu * vcpu,u32 msr,int type)830 static inline void svm_disable_intercept_for_msr(struct kvm_vcpu *vcpu,
831 u32 msr, int type)
832 {
833 svm_set_intercept_for_msr(vcpu, msr, type, false);
834 }
835
svm_enable_intercept_for_msr(struct kvm_vcpu * vcpu,u32 msr,int type)836 static inline void svm_enable_intercept_for_msr(struct kvm_vcpu *vcpu,
837 u32 msr, int type)
838 {
839 svm_set_intercept_for_msr(vcpu, msr, type, true);
840 }
841
842 int svm_skip_emulated_instruction(struct kvm_vcpu *vcpu);
843
844 /* nested.c */
845
846 #define NESTED_EXIT_HOST 0 /* Exit handled on host level */
847 #define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
848 #define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
849
nested_svm_virtualize_tpr(struct kvm_vcpu * vcpu)850 static inline bool nested_svm_virtualize_tpr(struct kvm_vcpu *vcpu)
851 {
852 struct vcpu_svm *svm = to_svm(vcpu);
853
854 return is_guest_mode(vcpu) && (svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK);
855 }
856
nested_exit_on_smi(struct vcpu_svm * svm)857 static inline bool nested_exit_on_smi(struct vcpu_svm *svm)
858 {
859 return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_SMI);
860 }
861
nested_exit_on_intr(struct vcpu_svm * svm)862 static inline bool nested_exit_on_intr(struct vcpu_svm *svm)
863 {
864 return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_INTR);
865 }
866
nested_exit_on_nmi(struct vcpu_svm * svm)867 static inline bool nested_exit_on_nmi(struct vcpu_svm *svm)
868 {
869 return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_NMI);
870 }
871
872 int __init nested_svm_init_msrpm_merge_offsets(void);
873
874 int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb_gpa, bool from_vmrun);
875 void svm_leave_nested(struct kvm_vcpu *vcpu);
876 void svm_free_nested(struct vcpu_svm *svm);
877 int svm_allocate_nested(struct vcpu_svm *svm);
878 int nested_svm_vmrun(struct kvm_vcpu *vcpu);
879 void svm_copy_vmrun_state(struct vmcb_save_area *to_save,
880 struct vmcb_save_area *from_save);
881 void svm_copy_vmloadsave_state(struct vmcb *to_vmcb, struct vmcb *from_vmcb);
882 void nested_svm_vmexit(struct vcpu_svm *svm);
883
nested_svm_simple_vmexit(struct vcpu_svm * svm,u32 exit_code)884 static inline void nested_svm_simple_vmexit(struct vcpu_svm *svm, u32 exit_code)
885 {
886 svm->vmcb->control.exit_code = exit_code;
887 svm->vmcb->control.exit_info_1 = 0;
888 svm->vmcb->control.exit_info_2 = 0;
889 nested_svm_vmexit(svm);
890 }
891
892 int nested_svm_exit_handled(struct vcpu_svm *svm);
893 int nested_svm_check_permissions(struct kvm_vcpu *vcpu);
894 int nested_svm_check_cached_vmcb12(struct kvm_vcpu *vcpu);
895 int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
896 bool has_error_code, u32 error_code);
897 int nested_svm_exit_special(struct vcpu_svm *svm);
898 void nested_svm_update_tsc_ratio_msr(struct kvm_vcpu *vcpu);
899 void svm_write_tsc_multiplier(struct kvm_vcpu *vcpu);
900 void nested_copy_vmcb_control_to_cache(struct vcpu_svm *svm,
901 struct vmcb_control_area *control);
902 void nested_copy_vmcb_save_to_cache(struct vcpu_svm *svm,
903 struct vmcb_save_area *save);
904 void nested_sync_control_from_vmcb02(struct vcpu_svm *svm);
905 void svm_switch_vmcb(struct vcpu_svm *svm, struct kvm_vmcb_info *target_vmcb);
906
907
__svm_pmu_handle_nested_transition(struct vcpu_svm * svm,bool defer)908 static inline void __svm_pmu_handle_nested_transition(struct vcpu_svm *svm,
909 bool defer)
910 {
911 struct kvm_pmu *pmu = vcpu_to_pmu(&svm->vcpu);
912 u64 counters = *(u64 *)pmu->pmc_has_mode_specific_enables;
913
914 __kvm_pmu_reprogram_counters(pmu, counters, defer);
915 }
916
svm_pmu_handle_nested_transition(struct vcpu_svm * svm)917 static inline void svm_pmu_handle_nested_transition(struct vcpu_svm *svm)
918 {
919 /*
920 * Do NOT defer reprogramming the counters by default. Instructions
921 * causing a state change are counted based on the _new_ CPU state
922 * (e.g. a successful VMRUN is counted in guest mode). Hence, the
923 * counters should be reprogrammed with the new state _before_ the
924 * instruction is potentially counted upon emulation completion.
925 */
926 __svm_pmu_handle_nested_transition(svm, false);
927 }
928
929 extern struct kvm_x86_nested_ops svm_nested_ops;
930
931 /* avic.c */
932 #define AVIC_REQUIRED_APICV_INHIBITS \
933 ( \
934 BIT(APICV_INHIBIT_REASON_DISABLED) | \
935 BIT(APICV_INHIBIT_REASON_ABSENT) | \
936 BIT(APICV_INHIBIT_REASON_HYPERV) | \
937 BIT(APICV_INHIBIT_REASON_NESTED) | \
938 BIT(APICV_INHIBIT_REASON_IRQWIN) | \
939 BIT(APICV_INHIBIT_REASON_PIT_REINJ) | \
940 BIT(APICV_INHIBIT_REASON_BLOCKIRQ) | \
941 BIT(APICV_INHIBIT_REASON_SEV) | \
942 BIT(APICV_INHIBIT_REASON_PHYSICAL_ID_ALIASED) | \
943 BIT(APICV_INHIBIT_REASON_APIC_ID_MODIFIED) | \
944 BIT(APICV_INHIBIT_REASON_APIC_BASE_MODIFIED) | \
945 BIT(APICV_INHIBIT_REASON_LOGICAL_ID_ALIASED) | \
946 BIT(APICV_INHIBIT_REASON_PHYSICAL_ID_TOO_BIG) \
947 )
948
949 bool __init avic_hardware_setup(void);
950 void avic_hardware_unsetup(void);
951 int avic_vcpu_precreate(struct kvm *kvm);
952 void avic_vm_pre_destroy(struct kvm *kvm);
953 void avic_vm_destroy(struct kvm *kvm);
954 void avic_init_vmcb(struct vcpu_svm *svm, struct vmcb *vmcb);
955 int avic_incomplete_ipi_interception(struct kvm_vcpu *vcpu);
956 int avic_unaccelerated_access_interception(struct kvm_vcpu *vcpu);
957 int avic_init_vcpu(struct vcpu_svm *svm);
958 void avic_vcpu_free(struct kvm_vcpu *vcpu);
959 void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
960 void avic_vcpu_put(struct kvm_vcpu *vcpu);
961 void avic_apicv_post_state_restore(struct kvm_vcpu *vcpu);
962 void avic_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu);
963 int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
964 unsigned int host_irq, uint32_t guest_irq,
965 struct kvm_vcpu *vcpu, u32 vector);
966 void avic_vcpu_blocking(struct kvm_vcpu *vcpu);
967 void avic_vcpu_unblocking(struct kvm_vcpu *vcpu);
968 void avic_ring_doorbell(struct kvm_vcpu *vcpu);
969 unsigned long avic_vcpu_get_apicv_inhibit_reasons(struct kvm_vcpu *vcpu);
970 void avic_refresh_virtual_apic_mode(struct kvm_vcpu *vcpu);
971
972
973 /* sev.c */
974
975 int pre_sev_run(struct vcpu_svm *svm, int cpu);
976 void sev_init_vmcb(struct vcpu_svm *svm, bool init_event);
977 void sev_vcpu_after_set_cpuid(struct vcpu_svm *svm);
978 int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in);
979 void sev_es_recalc_msr_intercepts(struct kvm_vcpu *vcpu);
980 void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector);
981 void sev_es_prepare_switch_to_guest(struct vcpu_svm *svm, struct sev_es_save_area *hostsa);
982 void sev_es_unmap_ghcb(struct vcpu_svm *svm);
983
984 #ifdef CONFIG_KVM_AMD_SEV
985 bool sev_vcpu_needs_initialization(struct kvm_vcpu *vcpu);
986 int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp);
987 int sev_mem_enc_register_region(struct kvm *kvm,
988 struct kvm_enc_region *range);
989 int sev_mem_enc_unregister_region(struct kvm *kvm,
990 struct kvm_enc_region *range);
991 int sev_vm_copy_enc_context_from(struct kvm *kvm, unsigned int source_fd);
992 int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd);
993 void sev_guest_memory_reclaimed(struct kvm *kvm);
994 int sev_handle_vmgexit(struct kvm_vcpu *vcpu);
995
996 /* These symbols are used in common code and are stubbed below. */
997
998 struct page *snp_safe_alloc_page_node(int node, gfp_t gfp);
snp_safe_alloc_page(void)999 static inline struct page *snp_safe_alloc_page(void)
1000 {
1001 return snp_safe_alloc_page_node(numa_node_id(), GFP_KERNEL_ACCOUNT);
1002 }
1003 void sev_snp_reload_vmsa(struct kvm_vcpu *vcpu);
1004
1005 int sev_vcpu_create(struct kvm_vcpu *vcpu);
1006 void sev_free_vcpu(struct kvm_vcpu *vcpu);
1007 void sev_vm_init(struct kvm *kvm);
1008 void sev_vm_destroy(struct kvm *kvm);
1009 void __init sev_set_cpu_caps(void);
1010 void __init sev_hardware_setup(void);
1011 void sev_hardware_unsetup(void);
1012 int sev_cpu_init(struct svm_cpu_data *sd);
1013 int sev_dev_get_attr(u32 group, u64 attr, u64 *val);
1014 extern unsigned int max_sev_asid;
1015 void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code);
1016 int sev_gmem_make_private(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, kvm_pfn_t nr_pages);
1017 void sev_gmem_make_shared(kvm_pfn_t pfn, kvm_pfn_t nr_pages);
1018 void sev_gmem_invalidate_range(struct kvm *kvm, struct kvm_gfn_range *range);
1019 int sev_gmem_max_mapping_level(struct kvm *kvm, kvm_pfn_t pfn, bool is_private);
1020 struct vmcb_save_area *sev_decrypt_vmsa(struct kvm_vcpu *vcpu);
1021 void sev_free_decrypted_vmsa(struct kvm_vcpu *vcpu, struct vmcb_save_area *vmsa);
1022 #else
snp_safe_alloc_page_node(int node,gfp_t gfp)1023 static inline struct page *snp_safe_alloc_page_node(int node, gfp_t gfp)
1024 {
1025 return alloc_pages_node(node, gfp | __GFP_ZERO, 0);
1026 }
1027
snp_safe_alloc_page(void)1028 static inline struct page *snp_safe_alloc_page(void)
1029 {
1030 return snp_safe_alloc_page_node(numa_node_id(), GFP_KERNEL_ACCOUNT);
1031 }
1032
sev_vcpu_create(struct kvm_vcpu * vcpu)1033 static inline int sev_vcpu_create(struct kvm_vcpu *vcpu) { return 0; }
sev_free_vcpu(struct kvm_vcpu * vcpu)1034 static inline void sev_free_vcpu(struct kvm_vcpu *vcpu) {}
sev_vm_init(struct kvm * kvm)1035 static inline void sev_vm_init(struct kvm *kvm) {}
sev_vm_destroy(struct kvm * kvm)1036 static inline void sev_vm_destroy(struct kvm *kvm) {}
sev_set_cpu_caps(void)1037 static inline void __init sev_set_cpu_caps(void) {}
sev_hardware_setup(void)1038 static inline void __init sev_hardware_setup(void) {}
sev_hardware_unsetup(void)1039 static inline void sev_hardware_unsetup(void) {}
sev_cpu_init(struct svm_cpu_data * sd)1040 static inline int sev_cpu_init(struct svm_cpu_data *sd) { return 0; }
sev_dev_get_attr(u32 group,u64 attr,u64 * val)1041 static inline int sev_dev_get_attr(u32 group, u64 attr, u64 *val) { return -ENXIO; }
1042 #define max_sev_asid 0
sev_handle_rmp_fault(struct kvm_vcpu * vcpu,gpa_t gpa,u64 error_code)1043 static inline void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code) {}
sev_decrypt_vmsa(struct kvm_vcpu * vcpu)1044 static inline struct vmcb_save_area *sev_decrypt_vmsa(struct kvm_vcpu *vcpu)
1045 {
1046 return NULL;
1047 }
sev_free_decrypted_vmsa(struct kvm_vcpu * vcpu,struct vmcb_save_area * vmsa)1048 static inline void sev_free_decrypted_vmsa(struct kvm_vcpu *vcpu, struct vmcb_save_area *vmsa) {}
1049 #endif
1050
1051 /* vmenter.S */
1052
1053 void __svm_sev_es_vcpu_run(struct vcpu_svm *svm, unsigned int flags,
1054 struct sev_es_save_area *hostsa);
1055 void __svm_vcpu_run(struct vcpu_svm *svm, unsigned int flags);
1056
1057 #define DEFINE_KVM_GHCB_ACCESSORS(field) \
1058 static __always_inline u64 kvm_ghcb_get_##field(struct vcpu_svm *svm) \
1059 { \
1060 return READ_ONCE(svm->sev_es.ghcb->save.field); \
1061 } \
1062 \
1063 static __always_inline bool kvm_ghcb_##field##_is_valid(const struct vcpu_svm *svm) \
1064 { \
1065 return test_bit(GHCB_BITMAP_IDX(field), \
1066 (unsigned long *)&svm->sev_es.valid_bitmap); \
1067 } \
1068 \
1069 static __always_inline u64 kvm_ghcb_get_##field##_if_valid(struct vcpu_svm *svm) \
1070 { \
1071 return kvm_ghcb_##field##_is_valid(svm) ? kvm_ghcb_get_##field(svm) : 0; \
1072 }
1073
1074 DEFINE_KVM_GHCB_ACCESSORS(cpl)
1075 DEFINE_KVM_GHCB_ACCESSORS(rax)
1076 DEFINE_KVM_GHCB_ACCESSORS(rcx)
1077 DEFINE_KVM_GHCB_ACCESSORS(rdx)
1078 DEFINE_KVM_GHCB_ACCESSORS(rbx)
1079 DEFINE_KVM_GHCB_ACCESSORS(rsi)
1080 DEFINE_KVM_GHCB_ACCESSORS(sw_exit_code)
1081 DEFINE_KVM_GHCB_ACCESSORS(sw_exit_info_1)
1082 DEFINE_KVM_GHCB_ACCESSORS(sw_exit_info_2)
1083 DEFINE_KVM_GHCB_ACCESSORS(sw_scratch)
1084 DEFINE_KVM_GHCB_ACCESSORS(xcr0)
1085 DEFINE_KVM_GHCB_ACCESSORS(xss)
1086
1087 #endif
1088