xref: /openbmc/linux/arch/x86/kvm/svm/svm.h (revision 657c45b3)
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 "kvm_cache_regs.h"
26 
27 #define __sme_page_pa(x) __sme_set(page_to_pfn(x) << PAGE_SHIFT)
28 
29 #define	IOPM_SIZE PAGE_SIZE * 3
30 #define	MSRPM_SIZE PAGE_SIZE * 2
31 
32 #define MAX_DIRECT_ACCESS_MSRS	46
33 #define MSRPM_OFFSETS	32
34 extern u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
35 extern bool npt_enabled;
36 extern int vgif;
37 extern bool intercept_smi;
38 extern bool x2avic_enabled;
39 extern bool vnmi;
40 
41 /*
42  * Clean bits in VMCB.
43  * VMCB_ALL_CLEAN_MASK might also need to
44  * be updated if this enum is modified.
45  */
46 enum {
47 	VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
48 			    pause filter count */
49 	VMCB_PERM_MAP,   /* IOPM Base and MSRPM Base */
50 	VMCB_ASID,	 /* ASID */
51 	VMCB_INTR,	 /* int_ctl, int_vector */
52 	VMCB_NPT,        /* npt_en, nCR3, gPAT */
53 	VMCB_CR,	 /* CR0, CR3, CR4, EFER */
54 	VMCB_DR,         /* DR6, DR7 */
55 	VMCB_DT,         /* GDT, IDT */
56 	VMCB_SEG,        /* CS, DS, SS, ES, CPL */
57 	VMCB_CR2,        /* CR2 only */
58 	VMCB_LBR,        /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
59 	VMCB_AVIC,       /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE,
60 			  * AVIC PHYSICAL_TABLE pointer,
61 			  * AVIC LOGICAL_TABLE pointer
62 			  */
63 	VMCB_SW = 31,    /* Reserved for hypervisor/software use */
64 };
65 
66 #define VMCB_ALL_CLEAN_MASK (					\
67 	(1U << VMCB_INTERCEPTS) | (1U << VMCB_PERM_MAP) |	\
68 	(1U << VMCB_ASID) | (1U << VMCB_INTR) |			\
69 	(1U << VMCB_NPT) | (1U << VMCB_CR) | (1U << VMCB_DR) |	\
70 	(1U << VMCB_DT) | (1U << VMCB_SEG) | (1U << VMCB_CR2) |	\
71 	(1U << VMCB_LBR) | (1U << VMCB_AVIC) |			\
72 	(1U << VMCB_SW))
73 
74 /* TPR and CR2 are always written before VMRUN */
75 #define VMCB_ALWAYS_DIRTY_MASK	((1U << VMCB_INTR) | (1U << VMCB_CR2))
76 
77 struct kvm_sev_info {
78 	bool active;		/* SEV enabled guest */
79 	bool es_active;		/* SEV-ES enabled guest */
80 	unsigned int asid;	/* ASID used for this guest */
81 	unsigned int handle;	/* SEV firmware handle */
82 	int fd;			/* SEV device fd */
83 	unsigned long pages_locked; /* Number of pages locked */
84 	struct list_head regions_list;  /* List of registered regions */
85 	u64 ap_jump_table;	/* SEV-ES AP Jump Table address */
86 	struct kvm *enc_context_owner; /* Owner of copied encryption context */
87 	struct list_head mirror_vms; /* List of VMs mirroring */
88 	struct list_head mirror_entry; /* Use as a list entry of mirrors */
89 	struct misc_cg *misc_cg; /* For misc cgroup accounting */
90 	atomic_t migration_in_progress;
91 };
92 
93 struct kvm_svm {
94 	struct kvm kvm;
95 
96 	/* Struct members for AVIC */
97 	u32 avic_vm_id;
98 	struct page *avic_logical_id_table_page;
99 	struct page *avic_physical_id_table_page;
100 	struct hlist_node hnode;
101 
102 	struct kvm_sev_info sev_info;
103 };
104 
105 struct kvm_vcpu;
106 
107 struct kvm_vmcb_info {
108 	struct vmcb *ptr;
109 	unsigned long pa;
110 	int cpu;
111 	uint64_t asid_generation;
112 };
113 
114 struct vmcb_save_area_cached {
115 	u64 efer;
116 	u64 cr4;
117 	u64 cr3;
118 	u64 cr0;
119 	u64 dr7;
120 	u64 dr6;
121 };
122 
123 struct vmcb_ctrl_area_cached {
124 	u32 intercepts[MAX_INTERCEPT];
125 	u16 pause_filter_thresh;
126 	u16 pause_filter_count;
127 	u64 iopm_base_pa;
128 	u64 msrpm_base_pa;
129 	u64 tsc_offset;
130 	u32 asid;
131 	u8 tlb_ctl;
132 	u32 int_ctl;
133 	u32 int_vector;
134 	u32 int_state;
135 	u32 exit_code;
136 	u32 exit_code_hi;
137 	u64 exit_info_1;
138 	u64 exit_info_2;
139 	u32 exit_int_info;
140 	u32 exit_int_info_err;
141 	u64 nested_ctl;
142 	u32 event_inj;
143 	u32 event_inj_err;
144 	u64 next_rip;
145 	u64 nested_cr3;
146 	u64 virt_ext;
147 	u32 clean;
148 	union {
149 		struct hv_vmcb_enlightenments hv_enlightenments;
150 		u8 reserved_sw[32];
151 	};
152 };
153 
154 struct svm_nested_state {
155 	struct kvm_vmcb_info vmcb02;
156 	u64 hsave_msr;
157 	u64 vm_cr_msr;
158 	u64 vmcb12_gpa;
159 	u64 last_vmcb12_gpa;
160 
161 	/* These are the merged vectors */
162 	u32 *msrpm;
163 
164 	/* A VMRUN has started but has not yet been performed, so
165 	 * we cannot inject a nested vmexit yet.  */
166 	bool nested_run_pending;
167 
168 	/* cache for control fields of the guest */
169 	struct vmcb_ctrl_area_cached ctl;
170 
171 	/*
172 	 * Note: this struct is not kept up-to-date while L2 runs; it is only
173 	 * valid within nested_svm_vmrun.
174 	 */
175 	struct vmcb_save_area_cached save;
176 
177 	bool initialized;
178 
179 	/*
180 	 * Indicates whether MSR bitmap for L2 needs to be rebuilt due to
181 	 * changes in MSR bitmap for L1 or switching to a different L2. Note,
182 	 * this flag can only be used reliably in conjunction with a paravirt L1
183 	 * which informs L0 whether any changes to MSR bitmap for L2 were done
184 	 * on its side.
185 	 */
186 	bool force_msr_bitmap_recalc;
187 };
188 
189 struct vcpu_sev_es_state {
190 	/* SEV-ES support */
191 	struct sev_es_save_area *vmsa;
192 	struct ghcb *ghcb;
193 	struct kvm_host_map ghcb_map;
194 	bool received_first_sipi;
195 
196 	/* SEV-ES scratch area support */
197 	void *ghcb_sa;
198 	u32 ghcb_sa_len;
199 	bool ghcb_sa_sync;
200 	bool ghcb_sa_free;
201 };
202 
203 struct vcpu_svm {
204 	struct kvm_vcpu vcpu;
205 	/* vmcb always points at current_vmcb->ptr, it's purely a shorthand. */
206 	struct vmcb *vmcb;
207 	struct kvm_vmcb_info vmcb01;
208 	struct kvm_vmcb_info *current_vmcb;
209 	u32 asid;
210 	u32 sysenter_esp_hi;
211 	u32 sysenter_eip_hi;
212 	uint64_t tsc_aux;
213 
214 	u64 msr_decfg;
215 
216 	u64 next_rip;
217 
218 	u64 spec_ctrl;
219 
220 	u64 tsc_ratio_msr;
221 	/*
222 	 * Contains guest-controlled bits of VIRT_SPEC_CTRL, which will be
223 	 * translated into the appropriate L2_CFG bits on the host to
224 	 * perform speculative control.
225 	 */
226 	u64 virt_spec_ctrl;
227 
228 	u32 *msrpm;
229 
230 	ulong nmi_iret_rip;
231 
232 	struct svm_nested_state nested;
233 
234 	/* NMI mask value, used when vNMI is not enabled */
235 	bool nmi_masked;
236 
237 	/*
238 	 * True when NMIs are still masked but guest IRET was just intercepted
239 	 * and KVM is waiting for RIP to change, which will signal that the
240 	 * intercepted IRET was retired and thus NMI can be unmasked.
241 	 */
242 	bool awaiting_iret_completion;
243 
244 	/*
245 	 * Set when KVM is awaiting IRET completion and needs to inject NMIs as
246 	 * soon as the IRET completes (e.g. NMI is pending injection).  KVM
247 	 * temporarily steals RFLAGS.TF to single-step the guest in this case
248 	 * in order to regain control as soon as the NMI-blocking condition
249 	 * goes away.
250 	 */
251 	bool nmi_singlestep;
252 	u64 nmi_singlestep_guest_rflags;
253 
254 	bool nmi_l1_to_l2;
255 
256 	unsigned long soft_int_csbase;
257 	unsigned long soft_int_old_rip;
258 	unsigned long soft_int_next_rip;
259 	bool soft_int_injected;
260 
261 	/* optional nested SVM features that are enabled for this guest  */
262 	bool nrips_enabled                : 1;
263 	bool tsc_scaling_enabled          : 1;
264 	bool v_vmload_vmsave_enabled      : 1;
265 	bool lbrv_enabled                 : 1;
266 	bool pause_filter_enabled         : 1;
267 	bool pause_threshold_enabled      : 1;
268 	bool vgif_enabled                 : 1;
269 	bool vnmi_enabled                 : 1;
270 
271 	u32 ldr_reg;
272 	u32 dfr_reg;
273 	struct page *avic_backing_page;
274 	u64 *avic_physical_id_cache;
275 
276 	/*
277 	 * Per-vcpu list of struct amd_svm_iommu_ir:
278 	 * This is used mainly to store interrupt remapping information used
279 	 * when update the vcpu affinity. This avoids the need to scan for
280 	 * IRTE and try to match ga_tag in the IOMMU driver.
281 	 */
282 	struct list_head ir_list;
283 	spinlock_t ir_list_lock;
284 
285 	/* Save desired MSR intercept (read: pass-through) state */
286 	struct {
287 		DECLARE_BITMAP(read, MAX_DIRECT_ACCESS_MSRS);
288 		DECLARE_BITMAP(write, MAX_DIRECT_ACCESS_MSRS);
289 	} shadow_msr_intercept;
290 
291 	struct vcpu_sev_es_state sev_es;
292 
293 	bool guest_state_loaded;
294 
295 	bool x2avic_msrs_intercepted;
296 
297 	/* Guest GIF value, used when vGIF is not enabled */
298 	bool guest_gif;
299 };
300 
301 struct svm_cpu_data {
302 	u64 asid_generation;
303 	u32 max_asid;
304 	u32 next_asid;
305 	u32 min_asid;
306 
307 	struct page *save_area;
308 	unsigned long save_area_pa;
309 
310 	struct vmcb *current_vmcb;
311 
312 	/* index = sev_asid, value = vmcb pointer */
313 	struct vmcb **sev_vmcbs;
314 };
315 
316 DECLARE_PER_CPU(struct svm_cpu_data, svm_data);
317 
318 void recalc_intercepts(struct vcpu_svm *svm);
319 
320 static __always_inline struct kvm_svm *to_kvm_svm(struct kvm *kvm)
321 {
322 	return container_of(kvm, struct kvm_svm, kvm);
323 }
324 
325 static __always_inline bool sev_guest(struct kvm *kvm)
326 {
327 #ifdef CONFIG_KVM_AMD_SEV
328 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
329 
330 	return sev->active;
331 #else
332 	return false;
333 #endif
334 }
335 
336 static __always_inline bool sev_es_guest(struct kvm *kvm)
337 {
338 #ifdef CONFIG_KVM_AMD_SEV
339 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
340 
341 	return sev->es_active && !WARN_ON_ONCE(!sev->active);
342 #else
343 	return false;
344 #endif
345 }
346 
347 static inline void vmcb_mark_all_dirty(struct vmcb *vmcb)
348 {
349 	vmcb->control.clean = 0;
350 }
351 
352 static inline void vmcb_mark_all_clean(struct vmcb *vmcb)
353 {
354 	vmcb->control.clean = VMCB_ALL_CLEAN_MASK
355 			       & ~VMCB_ALWAYS_DIRTY_MASK;
356 }
357 
358 static inline void vmcb_mark_dirty(struct vmcb *vmcb, int bit)
359 {
360 	vmcb->control.clean &= ~(1 << bit);
361 }
362 
363 static inline bool vmcb_is_dirty(struct vmcb *vmcb, int bit)
364 {
365         return !test_bit(bit, (unsigned long *)&vmcb->control.clean);
366 }
367 
368 static __always_inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
369 {
370 	return container_of(vcpu, struct vcpu_svm, vcpu);
371 }
372 
373 /*
374  * Only the PDPTRs are loaded on demand into the shadow MMU.  All other
375  * fields are synchronized on VM-Exit, because accessing the VMCB is cheap.
376  *
377  * CR3 might be out of date in the VMCB but it is not marked dirty; instead,
378  * KVM_REQ_LOAD_MMU_PGD is always requested when the cached vcpu->arch.cr3
379  * is changed.  svm_load_mmu_pgd() then syncs the new CR3 value into the VMCB.
380  */
381 #define SVM_REGS_LAZY_LOAD_SET	(1 << VCPU_EXREG_PDPTR)
382 
383 static inline void vmcb_set_intercept(struct vmcb_control_area *control, u32 bit)
384 {
385 	WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
386 	__set_bit(bit, (unsigned long *)&control->intercepts);
387 }
388 
389 static inline void vmcb_clr_intercept(struct vmcb_control_area *control, u32 bit)
390 {
391 	WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
392 	__clear_bit(bit, (unsigned long *)&control->intercepts);
393 }
394 
395 static inline bool vmcb_is_intercept(struct vmcb_control_area *control, u32 bit)
396 {
397 	WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
398 	return test_bit(bit, (unsigned long *)&control->intercepts);
399 }
400 
401 static inline bool vmcb12_is_intercept(struct vmcb_ctrl_area_cached *control, u32 bit)
402 {
403 	WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
404 	return test_bit(bit, (unsigned long *)&control->intercepts);
405 }
406 
407 static inline void set_dr_intercepts(struct vcpu_svm *svm)
408 {
409 	struct vmcb *vmcb = svm->vmcb01.ptr;
410 
411 	if (!sev_es_guest(svm->vcpu.kvm)) {
412 		vmcb_set_intercept(&vmcb->control, INTERCEPT_DR0_READ);
413 		vmcb_set_intercept(&vmcb->control, INTERCEPT_DR1_READ);
414 		vmcb_set_intercept(&vmcb->control, INTERCEPT_DR2_READ);
415 		vmcb_set_intercept(&vmcb->control, INTERCEPT_DR3_READ);
416 		vmcb_set_intercept(&vmcb->control, INTERCEPT_DR4_READ);
417 		vmcb_set_intercept(&vmcb->control, INTERCEPT_DR5_READ);
418 		vmcb_set_intercept(&vmcb->control, INTERCEPT_DR6_READ);
419 		vmcb_set_intercept(&vmcb->control, INTERCEPT_DR0_WRITE);
420 		vmcb_set_intercept(&vmcb->control, INTERCEPT_DR1_WRITE);
421 		vmcb_set_intercept(&vmcb->control, INTERCEPT_DR2_WRITE);
422 		vmcb_set_intercept(&vmcb->control, INTERCEPT_DR3_WRITE);
423 		vmcb_set_intercept(&vmcb->control, INTERCEPT_DR4_WRITE);
424 		vmcb_set_intercept(&vmcb->control, INTERCEPT_DR5_WRITE);
425 		vmcb_set_intercept(&vmcb->control, INTERCEPT_DR6_WRITE);
426 	}
427 
428 	vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_READ);
429 	vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_WRITE);
430 
431 	recalc_intercepts(svm);
432 }
433 
434 static inline void clr_dr_intercepts(struct vcpu_svm *svm)
435 {
436 	struct vmcb *vmcb = svm->vmcb01.ptr;
437 
438 	vmcb->control.intercepts[INTERCEPT_DR] = 0;
439 
440 	/* DR7 access must remain intercepted for an SEV-ES guest */
441 	if (sev_es_guest(svm->vcpu.kvm)) {
442 		vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_READ);
443 		vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_WRITE);
444 	}
445 
446 	recalc_intercepts(svm);
447 }
448 
449 static inline void set_exception_intercept(struct vcpu_svm *svm, u32 bit)
450 {
451 	struct vmcb *vmcb = svm->vmcb01.ptr;
452 
453 	WARN_ON_ONCE(bit >= 32);
454 	vmcb_set_intercept(&vmcb->control, INTERCEPT_EXCEPTION_OFFSET + bit);
455 
456 	recalc_intercepts(svm);
457 }
458 
459 static inline void clr_exception_intercept(struct vcpu_svm *svm, u32 bit)
460 {
461 	struct vmcb *vmcb = svm->vmcb01.ptr;
462 
463 	WARN_ON_ONCE(bit >= 32);
464 	vmcb_clr_intercept(&vmcb->control, INTERCEPT_EXCEPTION_OFFSET + bit);
465 
466 	recalc_intercepts(svm);
467 }
468 
469 static inline void svm_set_intercept(struct vcpu_svm *svm, int bit)
470 {
471 	struct vmcb *vmcb = svm->vmcb01.ptr;
472 
473 	vmcb_set_intercept(&vmcb->control, bit);
474 
475 	recalc_intercepts(svm);
476 }
477 
478 static inline void svm_clr_intercept(struct vcpu_svm *svm, int bit)
479 {
480 	struct vmcb *vmcb = svm->vmcb01.ptr;
481 
482 	vmcb_clr_intercept(&vmcb->control, bit);
483 
484 	recalc_intercepts(svm);
485 }
486 
487 static inline bool svm_is_intercept(struct vcpu_svm *svm, int bit)
488 {
489 	return vmcb_is_intercept(&svm->vmcb->control, bit);
490 }
491 
492 static inline bool nested_vgif_enabled(struct vcpu_svm *svm)
493 {
494 	return svm->vgif_enabled && (svm->nested.ctl.int_ctl & V_GIF_ENABLE_MASK);
495 }
496 
497 static inline struct vmcb *get_vgif_vmcb(struct vcpu_svm *svm)
498 {
499 	if (!vgif)
500 		return NULL;
501 
502 	if (is_guest_mode(&svm->vcpu) && !nested_vgif_enabled(svm))
503 		return svm->nested.vmcb02.ptr;
504 	else
505 		return svm->vmcb01.ptr;
506 }
507 
508 static inline void enable_gif(struct vcpu_svm *svm)
509 {
510 	struct vmcb *vmcb = get_vgif_vmcb(svm);
511 
512 	if (vmcb)
513 		vmcb->control.int_ctl |= V_GIF_MASK;
514 	else
515 		svm->guest_gif = true;
516 }
517 
518 static inline void disable_gif(struct vcpu_svm *svm)
519 {
520 	struct vmcb *vmcb = get_vgif_vmcb(svm);
521 
522 	if (vmcb)
523 		vmcb->control.int_ctl &= ~V_GIF_MASK;
524 	else
525 		svm->guest_gif = false;
526 }
527 
528 static inline bool gif_set(struct vcpu_svm *svm)
529 {
530 	struct vmcb *vmcb = get_vgif_vmcb(svm);
531 
532 	if (vmcb)
533 		return !!(vmcb->control.int_ctl & V_GIF_MASK);
534 	else
535 		return svm->guest_gif;
536 }
537 
538 static inline bool nested_npt_enabled(struct vcpu_svm *svm)
539 {
540 	return svm->nested.ctl.nested_ctl & SVM_NESTED_CTL_NP_ENABLE;
541 }
542 
543 static inline bool nested_vnmi_enabled(struct vcpu_svm *svm)
544 {
545 	return svm->vnmi_enabled &&
546 	       (svm->nested.ctl.int_ctl & V_NMI_ENABLE_MASK);
547 }
548 
549 static inline bool is_x2apic_msrpm_offset(u32 offset)
550 {
551 	/* 4 msrs per u8, and 4 u8 in u32 */
552 	u32 msr = offset * 16;
553 
554 	return (msr >= APIC_BASE_MSR) &&
555 	       (msr < (APIC_BASE_MSR + 0x100));
556 }
557 
558 static inline struct vmcb *get_vnmi_vmcb_l1(struct vcpu_svm *svm)
559 {
560 	if (!vnmi)
561 		return NULL;
562 
563 	if (is_guest_mode(&svm->vcpu))
564 		return NULL;
565 	else
566 		return svm->vmcb01.ptr;
567 }
568 
569 static inline bool is_vnmi_enabled(struct vcpu_svm *svm)
570 {
571 	struct vmcb *vmcb = get_vnmi_vmcb_l1(svm);
572 
573 	if (vmcb)
574 		return !!(vmcb->control.int_ctl & V_NMI_ENABLE_MASK);
575 	else
576 		return false;
577 }
578 
579 /* svm.c */
580 #define MSR_INVALID				0xffffffffU
581 
582 #define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
583 
584 extern bool dump_invalid_vmcb;
585 
586 u32 svm_msrpm_offset(u32 msr);
587 u32 *svm_vcpu_alloc_msrpm(void);
588 void svm_vcpu_init_msrpm(struct kvm_vcpu *vcpu, u32 *msrpm);
589 void svm_vcpu_free_msrpm(u32 *msrpm);
590 void svm_copy_lbrs(struct vmcb *to_vmcb, struct vmcb *from_vmcb);
591 void svm_update_lbrv(struct kvm_vcpu *vcpu);
592 
593 int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer);
594 void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
595 void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
596 void disable_nmi_singlestep(struct vcpu_svm *svm);
597 bool svm_smi_blocked(struct kvm_vcpu *vcpu);
598 bool svm_nmi_blocked(struct kvm_vcpu *vcpu);
599 bool svm_interrupt_blocked(struct kvm_vcpu *vcpu);
600 void svm_set_gif(struct vcpu_svm *svm, bool value);
601 int svm_invoke_exit_handler(struct kvm_vcpu *vcpu, u64 exit_code);
602 void set_msr_interception(struct kvm_vcpu *vcpu, u32 *msrpm, u32 msr,
603 			  int read, int write);
604 void svm_set_x2apic_msr_interception(struct vcpu_svm *svm, bool disable);
605 void svm_complete_interrupt_delivery(struct kvm_vcpu *vcpu, int delivery_mode,
606 				     int trig_mode, int vec);
607 
608 /* nested.c */
609 
610 #define NESTED_EXIT_HOST	0	/* Exit handled on host level */
611 #define NESTED_EXIT_DONE	1	/* Exit caused nested vmexit  */
612 #define NESTED_EXIT_CONTINUE	2	/* Further checks needed      */
613 
614 static inline bool nested_svm_virtualize_tpr(struct kvm_vcpu *vcpu)
615 {
616 	struct vcpu_svm *svm = to_svm(vcpu);
617 
618 	return is_guest_mode(vcpu) && (svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK);
619 }
620 
621 static inline bool nested_exit_on_smi(struct vcpu_svm *svm)
622 {
623 	return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_SMI);
624 }
625 
626 static inline bool nested_exit_on_intr(struct vcpu_svm *svm)
627 {
628 	return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_INTR);
629 }
630 
631 static inline bool nested_exit_on_nmi(struct vcpu_svm *svm)
632 {
633 	return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_NMI);
634 }
635 
636 int enter_svm_guest_mode(struct kvm_vcpu *vcpu,
637 			 u64 vmcb_gpa, struct vmcb *vmcb12, bool from_vmrun);
638 void svm_leave_nested(struct kvm_vcpu *vcpu);
639 void svm_free_nested(struct vcpu_svm *svm);
640 int svm_allocate_nested(struct vcpu_svm *svm);
641 int nested_svm_vmrun(struct kvm_vcpu *vcpu);
642 void svm_copy_vmrun_state(struct vmcb_save_area *to_save,
643 			  struct vmcb_save_area *from_save);
644 void svm_copy_vmloadsave_state(struct vmcb *to_vmcb, struct vmcb *from_vmcb);
645 int nested_svm_vmexit(struct vcpu_svm *svm);
646 
647 static inline int nested_svm_simple_vmexit(struct vcpu_svm *svm, u32 exit_code)
648 {
649 	svm->vmcb->control.exit_code   = exit_code;
650 	svm->vmcb->control.exit_info_1 = 0;
651 	svm->vmcb->control.exit_info_2 = 0;
652 	return nested_svm_vmexit(svm);
653 }
654 
655 int nested_svm_exit_handled(struct vcpu_svm *svm);
656 int nested_svm_check_permissions(struct kvm_vcpu *vcpu);
657 int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
658 			       bool has_error_code, u32 error_code);
659 int nested_svm_exit_special(struct vcpu_svm *svm);
660 void nested_svm_update_tsc_ratio_msr(struct kvm_vcpu *vcpu);
661 void __svm_write_tsc_multiplier(u64 multiplier);
662 void nested_copy_vmcb_control_to_cache(struct vcpu_svm *svm,
663 				       struct vmcb_control_area *control);
664 void nested_copy_vmcb_save_to_cache(struct vcpu_svm *svm,
665 				    struct vmcb_save_area *save);
666 void nested_sync_control_from_vmcb02(struct vcpu_svm *svm);
667 void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm);
668 void svm_switch_vmcb(struct vcpu_svm *svm, struct kvm_vmcb_info *target_vmcb);
669 
670 extern struct kvm_x86_nested_ops svm_nested_ops;
671 
672 /* avic.c */
673 #define AVIC_REQUIRED_APICV_INHIBITS			\
674 (							\
675 	BIT(APICV_INHIBIT_REASON_DISABLE) |		\
676 	BIT(APICV_INHIBIT_REASON_ABSENT) |		\
677 	BIT(APICV_INHIBIT_REASON_HYPERV) |		\
678 	BIT(APICV_INHIBIT_REASON_NESTED) |		\
679 	BIT(APICV_INHIBIT_REASON_IRQWIN) |		\
680 	BIT(APICV_INHIBIT_REASON_PIT_REINJ) |		\
681 	BIT(APICV_INHIBIT_REASON_BLOCKIRQ) |		\
682 	BIT(APICV_INHIBIT_REASON_SEV)      |		\
683 	BIT(APICV_INHIBIT_REASON_PHYSICAL_ID_ALIASED) |	\
684 	BIT(APICV_INHIBIT_REASON_APIC_ID_MODIFIED) |	\
685 	BIT(APICV_INHIBIT_REASON_APIC_BASE_MODIFIED) |	\
686 	BIT(APICV_INHIBIT_REASON_LOGICAL_ID_ALIASED)	\
687 )
688 
689 bool avic_hardware_setup(void);
690 int avic_ga_log_notifier(u32 ga_tag);
691 void avic_vm_destroy(struct kvm *kvm);
692 int avic_vm_init(struct kvm *kvm);
693 void avic_init_vmcb(struct vcpu_svm *svm, struct vmcb *vmcb);
694 int avic_incomplete_ipi_interception(struct kvm_vcpu *vcpu);
695 int avic_unaccelerated_access_interception(struct kvm_vcpu *vcpu);
696 int avic_init_vcpu(struct vcpu_svm *svm);
697 void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
698 void avic_vcpu_put(struct kvm_vcpu *vcpu);
699 void avic_apicv_post_state_restore(struct kvm_vcpu *vcpu);
700 void avic_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu);
701 int avic_pi_update_irte(struct kvm *kvm, unsigned int host_irq,
702 			uint32_t guest_irq, bool set);
703 void avic_vcpu_blocking(struct kvm_vcpu *vcpu);
704 void avic_vcpu_unblocking(struct kvm_vcpu *vcpu);
705 void avic_ring_doorbell(struct kvm_vcpu *vcpu);
706 unsigned long avic_vcpu_get_apicv_inhibit_reasons(struct kvm_vcpu *vcpu);
707 void avic_refresh_virtual_apic_mode(struct kvm_vcpu *vcpu);
708 
709 
710 /* sev.c */
711 
712 #define GHCB_VERSION_MAX	1ULL
713 #define GHCB_VERSION_MIN	1ULL
714 
715 
716 extern unsigned int max_sev_asid;
717 
718 void sev_vm_destroy(struct kvm *kvm);
719 int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp);
720 int sev_mem_enc_register_region(struct kvm *kvm,
721 				struct kvm_enc_region *range);
722 int sev_mem_enc_unregister_region(struct kvm *kvm,
723 				  struct kvm_enc_region *range);
724 int sev_vm_copy_enc_context_from(struct kvm *kvm, unsigned int source_fd);
725 int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd);
726 void sev_guest_memory_reclaimed(struct kvm *kvm);
727 
728 void pre_sev_run(struct vcpu_svm *svm, int cpu);
729 void __init sev_set_cpu_caps(void);
730 void __init sev_hardware_setup(void);
731 void sev_hardware_unsetup(void);
732 int sev_cpu_init(struct svm_cpu_data *sd);
733 void sev_init_vmcb(struct vcpu_svm *svm);
734 void sev_free_vcpu(struct kvm_vcpu *vcpu);
735 int sev_handle_vmgexit(struct kvm_vcpu *vcpu);
736 int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in);
737 void sev_es_vcpu_reset(struct vcpu_svm *svm);
738 void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector);
739 void sev_es_prepare_switch_to_guest(struct sev_es_save_area *hostsa);
740 void sev_es_unmap_ghcb(struct vcpu_svm *svm);
741 
742 /* vmenter.S */
743 
744 void __svm_sev_es_vcpu_run(struct vcpu_svm *svm, bool spec_ctrl_intercepted);
745 void __svm_vcpu_run(struct vcpu_svm *svm, bool spec_ctrl_intercepted);
746 
747 #endif
748