xref: /openbmc/linux/arch/x86/kvm/svm/svm.h (revision 305c8388)
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 
84 struct nested_state {
85 	struct vmcb *hsave;
86 	u64 hsave_msr;
87 	u64 vm_cr_msr;
88 	u64 vmcb;
89 	u32 host_intercept_exceptions;
90 
91 	/* These are the merged vectors */
92 	u32 *msrpm;
93 
94 	/* A VMRUN has started but has not yet been performed, so
95 	 * we cannot inject a nested vmexit yet.  */
96 	bool nested_run_pending;
97 
98 	/* cache for control fields of the guest */
99 	struct vmcb_control_area ctl;
100 };
101 
102 struct vcpu_svm {
103 	struct kvm_vcpu vcpu;
104 	struct vmcb *vmcb;
105 	unsigned long vmcb_pa;
106 	struct svm_cpu_data *svm_data;
107 	uint64_t asid_generation;
108 	uint64_t sysenter_esp;
109 	uint64_t sysenter_eip;
110 	uint64_t tsc_aux;
111 
112 	u64 msr_decfg;
113 
114 	u64 next_rip;
115 
116 	u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
117 	struct {
118 		u16 fs;
119 		u16 gs;
120 		u16 ldt;
121 		u64 gs_base;
122 	} host;
123 
124 	u64 spec_ctrl;
125 	/*
126 	 * Contains guest-controlled bits of VIRT_SPEC_CTRL, which will be
127 	 * translated into the appropriate L2_CFG bits on the host to
128 	 * perform speculative control.
129 	 */
130 	u64 virt_spec_ctrl;
131 
132 	u32 *msrpm;
133 
134 	ulong nmi_iret_rip;
135 
136 	struct nested_state nested;
137 
138 	bool nmi_singlestep;
139 	u64 nmi_singlestep_guest_rflags;
140 
141 	unsigned int3_injected;
142 	unsigned long int3_rip;
143 
144 	/* cached guest cpuid flags for faster access */
145 	bool nrips_enabled	: 1;
146 
147 	u32 ldr_reg;
148 	u32 dfr_reg;
149 	struct page *avic_backing_page;
150 	u64 *avic_physical_id_cache;
151 	bool avic_is_running;
152 
153 	/*
154 	 * Per-vcpu list of struct amd_svm_iommu_ir:
155 	 * This is used mainly to store interrupt remapping information used
156 	 * when update the vcpu affinity. This avoids the need to scan for
157 	 * IRTE and try to match ga_tag in the IOMMU driver.
158 	 */
159 	struct list_head ir_list;
160 	spinlock_t ir_list_lock;
161 
162 	/* which host CPU was used for running this vcpu */
163 	unsigned int last_cpu;
164 };
165 
166 struct svm_cpu_data {
167 	int cpu;
168 
169 	u64 asid_generation;
170 	u32 max_asid;
171 	u32 next_asid;
172 	u32 min_asid;
173 	struct kvm_ldttss_desc *tss_desc;
174 
175 	struct page *save_area;
176 	struct vmcb *current_vmcb;
177 
178 	/* index = sev_asid, value = vmcb pointer */
179 	struct vmcb **sev_vmcbs;
180 };
181 
182 DECLARE_PER_CPU(struct svm_cpu_data *, svm_data);
183 
184 void recalc_intercepts(struct vcpu_svm *svm);
185 
186 static inline struct kvm_svm *to_kvm_svm(struct kvm *kvm)
187 {
188 	return container_of(kvm, struct kvm_svm, kvm);
189 }
190 
191 static inline void mark_all_dirty(struct vmcb *vmcb)
192 {
193 	vmcb->control.clean = 0;
194 }
195 
196 static inline void mark_all_clean(struct vmcb *vmcb)
197 {
198 	vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
199 			       & ~VMCB_ALWAYS_DIRTY_MASK;
200 }
201 
202 static inline void mark_dirty(struct vmcb *vmcb, int bit)
203 {
204 	vmcb->control.clean &= ~(1 << bit);
205 }
206 
207 static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
208 {
209 	return container_of(vcpu, struct vcpu_svm, vcpu);
210 }
211 
212 static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
213 {
214 	if (is_guest_mode(&svm->vcpu))
215 		return svm->nested.hsave;
216 	else
217 		return svm->vmcb;
218 }
219 
220 static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
221 {
222 	struct vmcb *vmcb = get_host_vmcb(svm);
223 
224 	vmcb->control.intercept_cr |= (1U << bit);
225 
226 	recalc_intercepts(svm);
227 }
228 
229 static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
230 {
231 	struct vmcb *vmcb = get_host_vmcb(svm);
232 
233 	vmcb->control.intercept_cr &= ~(1U << bit);
234 
235 	recalc_intercepts(svm);
236 }
237 
238 static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
239 {
240 	struct vmcb *vmcb = get_host_vmcb(svm);
241 
242 	return vmcb->control.intercept_cr & (1U << bit);
243 }
244 
245 static inline void set_dr_intercepts(struct vcpu_svm *svm)
246 {
247 	struct vmcb *vmcb = get_host_vmcb(svm);
248 
249 	vmcb->control.intercept_dr = (1 << INTERCEPT_DR0_READ)
250 		| (1 << INTERCEPT_DR1_READ)
251 		| (1 << INTERCEPT_DR2_READ)
252 		| (1 << INTERCEPT_DR3_READ)
253 		| (1 << INTERCEPT_DR4_READ)
254 		| (1 << INTERCEPT_DR5_READ)
255 		| (1 << INTERCEPT_DR6_READ)
256 		| (1 << INTERCEPT_DR7_READ)
257 		| (1 << INTERCEPT_DR0_WRITE)
258 		| (1 << INTERCEPT_DR1_WRITE)
259 		| (1 << INTERCEPT_DR2_WRITE)
260 		| (1 << INTERCEPT_DR3_WRITE)
261 		| (1 << INTERCEPT_DR4_WRITE)
262 		| (1 << INTERCEPT_DR5_WRITE)
263 		| (1 << INTERCEPT_DR6_WRITE)
264 		| (1 << INTERCEPT_DR7_WRITE);
265 
266 	recalc_intercepts(svm);
267 }
268 
269 static inline void clr_dr_intercepts(struct vcpu_svm *svm)
270 {
271 	struct vmcb *vmcb = get_host_vmcb(svm);
272 
273 	vmcb->control.intercept_dr = 0;
274 
275 	recalc_intercepts(svm);
276 }
277 
278 static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
279 {
280 	struct vmcb *vmcb = get_host_vmcb(svm);
281 
282 	vmcb->control.intercept_exceptions |= (1U << bit);
283 
284 	recalc_intercepts(svm);
285 }
286 
287 static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
288 {
289 	struct vmcb *vmcb = get_host_vmcb(svm);
290 
291 	vmcb->control.intercept_exceptions &= ~(1U << bit);
292 
293 	recalc_intercepts(svm);
294 }
295 
296 static inline void set_intercept(struct vcpu_svm *svm, int bit)
297 {
298 	struct vmcb *vmcb = get_host_vmcb(svm);
299 
300 	vmcb->control.intercept |= (1ULL << bit);
301 
302 	recalc_intercepts(svm);
303 }
304 
305 static inline void clr_intercept(struct vcpu_svm *svm, int bit)
306 {
307 	struct vmcb *vmcb = get_host_vmcb(svm);
308 
309 	vmcb->control.intercept &= ~(1ULL << bit);
310 
311 	recalc_intercepts(svm);
312 }
313 
314 static inline bool is_intercept(struct vcpu_svm *svm, int bit)
315 {
316 	return (svm->vmcb->control.intercept & (1ULL << bit)) != 0;
317 }
318 
319 static inline bool vgif_enabled(struct vcpu_svm *svm)
320 {
321 	return !!(svm->vmcb->control.int_ctl & V_GIF_ENABLE_MASK);
322 }
323 
324 static inline void enable_gif(struct vcpu_svm *svm)
325 {
326 	if (vgif_enabled(svm))
327 		svm->vmcb->control.int_ctl |= V_GIF_MASK;
328 	else
329 		svm->vcpu.arch.hflags |= HF_GIF_MASK;
330 }
331 
332 static inline void disable_gif(struct vcpu_svm *svm)
333 {
334 	if (vgif_enabled(svm))
335 		svm->vmcb->control.int_ctl &= ~V_GIF_MASK;
336 	else
337 		svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
338 }
339 
340 static inline bool gif_set(struct vcpu_svm *svm)
341 {
342 	if (vgif_enabled(svm))
343 		return !!(svm->vmcb->control.int_ctl & V_GIF_MASK);
344 	else
345 		return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
346 }
347 
348 /* svm.c */
349 #define MSR_INVALID			0xffffffffU
350 
351 u32 svm_msrpm_offset(u32 msr);
352 void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer);
353 void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
354 int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
355 void svm_flush_tlb(struct kvm_vcpu *vcpu);
356 void disable_nmi_singlestep(struct vcpu_svm *svm);
357 bool svm_smi_blocked(struct kvm_vcpu *vcpu);
358 bool svm_nmi_blocked(struct kvm_vcpu *vcpu);
359 bool svm_interrupt_blocked(struct kvm_vcpu *vcpu);
360 void svm_set_gif(struct vcpu_svm *svm, bool value);
361 
362 /* nested.c */
363 
364 #define NESTED_EXIT_HOST	0	/* Exit handled on host level */
365 #define NESTED_EXIT_DONE	1	/* Exit caused nested vmexit  */
366 #define NESTED_EXIT_CONTINUE	2	/* Further checks needed      */
367 
368 static inline bool svm_nested_virtualize_tpr(struct kvm_vcpu *vcpu)
369 {
370 	struct vcpu_svm *svm = to_svm(vcpu);
371 
372 	return is_guest_mode(vcpu) && (svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK);
373 }
374 
375 static inline bool nested_exit_on_smi(struct vcpu_svm *svm)
376 {
377 	return (svm->nested.ctl.intercept & (1ULL << INTERCEPT_SMI));
378 }
379 
380 static inline bool nested_exit_on_intr(struct vcpu_svm *svm)
381 {
382 	return (svm->nested.ctl.intercept & (1ULL << INTERCEPT_INTR));
383 }
384 
385 static inline bool nested_exit_on_nmi(struct vcpu_svm *svm)
386 {
387 	return (svm->nested.ctl.intercept & (1ULL << INTERCEPT_NMI));
388 }
389 
390 void enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa,
391 			  struct vmcb *nested_vmcb);
392 void svm_leave_nested(struct vcpu_svm *svm);
393 int nested_svm_vmrun(struct vcpu_svm *svm);
394 void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb);
395 int nested_svm_vmexit(struct vcpu_svm *svm);
396 int nested_svm_exit_handled(struct vcpu_svm *svm);
397 int nested_svm_check_permissions(struct vcpu_svm *svm);
398 int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
399 			       bool has_error_code, u32 error_code);
400 int nested_svm_exit_special(struct vcpu_svm *svm);
401 void sync_nested_vmcb_control(struct vcpu_svm *svm);
402 
403 extern struct kvm_x86_nested_ops svm_nested_ops;
404 
405 /* avic.c */
406 
407 #define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK	(0xFF)
408 #define AVIC_LOGICAL_ID_ENTRY_VALID_BIT			31
409 #define AVIC_LOGICAL_ID_ENTRY_VALID_MASK		(1 << 31)
410 
411 #define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK	(0xFFULL)
412 #define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK	(0xFFFFFFFFFFULL << 12)
413 #define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK		(1ULL << 62)
414 #define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK		(1ULL << 63)
415 
416 #define VMCB_AVIC_APIC_BAR_MASK		0xFFFFFFFFFF000ULL
417 
418 extern int avic;
419 
420 static inline void avic_update_vapic_bar(struct vcpu_svm *svm, u64 data)
421 {
422 	svm->vmcb->control.avic_vapic_bar = data & VMCB_AVIC_APIC_BAR_MASK;
423 	mark_dirty(svm->vmcb, VMCB_AVIC);
424 }
425 
426 static inline bool avic_vcpu_is_running(struct kvm_vcpu *vcpu)
427 {
428 	struct vcpu_svm *svm = to_svm(vcpu);
429 	u64 *entry = svm->avic_physical_id_cache;
430 
431 	if (!entry)
432 		return false;
433 
434 	return (READ_ONCE(*entry) & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
435 }
436 
437 int avic_ga_log_notifier(u32 ga_tag);
438 void avic_vm_destroy(struct kvm *kvm);
439 int avic_vm_init(struct kvm *kvm);
440 void avic_init_vmcb(struct vcpu_svm *svm);
441 void svm_toggle_avic_for_irq_window(struct kvm_vcpu *vcpu, bool activate);
442 int avic_incomplete_ipi_interception(struct vcpu_svm *svm);
443 int avic_unaccelerated_access_interception(struct vcpu_svm *svm);
444 int avic_init_vcpu(struct vcpu_svm *svm);
445 void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
446 void avic_vcpu_put(struct kvm_vcpu *vcpu);
447 void avic_post_state_restore(struct kvm_vcpu *vcpu);
448 void svm_set_virtual_apic_mode(struct kvm_vcpu *vcpu);
449 void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu);
450 bool svm_check_apicv_inhibit_reasons(ulong bit);
451 void svm_pre_update_apicv_exec_ctrl(struct kvm *kvm, bool activate);
452 void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap);
453 void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr);
454 void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr);
455 int svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec);
456 bool svm_dy_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu);
457 int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
458 		       uint32_t guest_irq, bool set);
459 void svm_vcpu_blocking(struct kvm_vcpu *vcpu);
460 void svm_vcpu_unblocking(struct kvm_vcpu *vcpu);
461 
462 /* sev.c */
463 
464 extern unsigned int max_sev_asid;
465 
466 static inline bool sev_guest(struct kvm *kvm)
467 {
468 #ifdef CONFIG_KVM_AMD_SEV
469 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
470 
471 	return sev->active;
472 #else
473 	return false;
474 #endif
475 }
476 
477 static inline bool svm_sev_enabled(void)
478 {
479 	return IS_ENABLED(CONFIG_KVM_AMD_SEV) ? max_sev_asid : 0;
480 }
481 
482 void sev_vm_destroy(struct kvm *kvm);
483 int svm_mem_enc_op(struct kvm *kvm, void __user *argp);
484 int svm_register_enc_region(struct kvm *kvm,
485 			    struct kvm_enc_region *range);
486 int svm_unregister_enc_region(struct kvm *kvm,
487 			      struct kvm_enc_region *range);
488 void pre_sev_run(struct vcpu_svm *svm, int cpu);
489 int __init sev_hardware_setup(void);
490 void sev_hardware_teardown(void);
491 
492 #endif
493