blob: c19859e62dec0d8a3df8551a42333b363a3eda32 [file] [log] [blame]
Joerg Roedel883b0a92020-03-24 10:41:52 +01001// 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>
Tom Lendacky291bd202020-12-10 11:09:47 -060020#include <linux/bits.h>
Joerg Roedel883b0a92020-03-24 10:41:52 +010021
22#include <asm/svm.h>
Brijesh Singhb81fc742021-04-27 06:16:35 -050023#include <asm/sev-common.h>
Joerg Roedel883b0a92020-03-24 10:41:52 +010024
Tom Lendacky85ca8be2020-12-10 11:10:04 -060025#define __sme_page_pa(x) __sme_set(page_to_pfn(x) << PAGE_SHIFT)
26
Krish Sadhukhan47903dc2021-04-12 17:56:05 -040027#define IOPM_SIZE PAGE_SIZE * 3
28#define MSRPM_SIZE PAGE_SIZE * 2
29
Maxim Levitskyadc2a232021-04-01 14:19:28 +030030#define MAX_DIRECT_ACCESS_MSRS 20
Joerg Roedel883b0a92020-03-24 10:41:52 +010031#define MSRPM_OFFSETS 16
32extern u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
33extern bool npt_enabled;
Maxim Levitsky4b639a92021-07-07 15:51:00 +030034extern bool intercept_smi;
Joerg Roedel883b0a92020-03-24 10:41:52 +010035
Vineeth Pillai59d21d62021-06-03 15:14:37 +000036/*
37 * Clean bits in VMCB.
38 * VMCB_ALL_CLEAN_MASK might also need to
39 * be updated if this enum is modified.
40 */
Joerg Roedel883b0a92020-03-24 10:41:52 +010041enum {
42 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
43 pause filter count */
44 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
45 VMCB_ASID, /* ASID */
46 VMCB_INTR, /* int_ctl, int_vector */
47 VMCB_NPT, /* npt_en, nCR3, gPAT */
48 VMCB_CR, /* CR0, CR3, CR4, EFER */
49 VMCB_DR, /* DR6, DR7 */
50 VMCB_DT, /* GDT, IDT */
51 VMCB_SEG, /* CS, DS, SS, ES, CPL */
52 VMCB_CR2, /* CR2 only */
53 VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
54 VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE,
55 * AVIC PHYSICAL_TABLE pointer,
56 * AVIC LOGICAL_TABLE pointer
57 */
Vineeth Pillai59d21d62021-06-03 15:14:37 +000058 VMCB_SW = 31, /* Reserved for hypervisor/software use */
Joerg Roedel883b0a92020-03-24 10:41:52 +010059};
60
Vineeth Pillai59d21d62021-06-03 15:14:37 +000061#define VMCB_ALL_CLEAN_MASK ( \
62 (1U << VMCB_INTERCEPTS) | (1U << VMCB_PERM_MAP) | \
63 (1U << VMCB_ASID) | (1U << VMCB_INTR) | \
64 (1U << VMCB_NPT) | (1U << VMCB_CR) | (1U << VMCB_DR) | \
65 (1U << VMCB_DT) | (1U << VMCB_SEG) | (1U << VMCB_CR2) | \
66 (1U << VMCB_LBR) | (1U << VMCB_AVIC) | \
67 (1U << VMCB_SW))
68
Joerg Roedel883b0a92020-03-24 10:41:52 +010069/* TPR and CR2 are always written before VMRUN */
70#define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
71
72struct kvm_sev_info {
73 bool active; /* SEV enabled guest */
Tom Lendacky916391a2020-12-10 11:09:38 -060074 bool es_active; /* SEV-ES enabled guest */
Joerg Roedel883b0a92020-03-24 10:41:52 +010075 unsigned int asid; /* ASID used for this guest */
76 unsigned int handle; /* SEV firmware handle */
77 int fd; /* SEV device fd */
78 unsigned long pages_locked; /* Number of pages locked */
79 struct list_head regions_list; /* List of registered regions */
Tom Lendacky8640ca52020-12-15 12:44:07 -050080 u64 ap_jump_table; /* SEV-ES AP Jump Table address */
Nathan Tempelman54526d12021-04-08 22:32:14 +000081 struct kvm *enc_context_owner; /* Owner of copied encryption context */
Vipin Sharma7aef27f2021-03-29 21:42:06 -070082 struct misc_cg *misc_cg; /* For misc cgroup accounting */
Joerg Roedel883b0a92020-03-24 10:41:52 +010083};
84
85struct kvm_svm {
86 struct kvm kvm;
87
88 /* Struct members for AVIC */
89 u32 avic_vm_id;
90 struct page *avic_logical_id_table_page;
91 struct page *avic_physical_id_table_page;
92 struct hlist_node hnode;
93
94 struct kvm_sev_info sev_info;
95};
96
97struct kvm_vcpu;
98
Cathy Avery4995a362021-01-13 07:07:52 -050099struct kvm_vmcb_info {
100 struct vmcb *ptr;
101 unsigned long pa;
Cathy Averyaf18fa72021-01-12 11:43:12 -0500102 int cpu;
Cathy Avery193015a2021-01-12 11:43:13 -0500103 uint64_t asid_generation;
Cathy Avery4995a362021-01-13 07:07:52 -0500104};
105
Joerg Roedel7693b3e2020-06-25 10:03:22 +0200106struct svm_nested_state {
Cathy Avery4995a362021-01-13 07:07:52 -0500107 struct kvm_vmcb_info vmcb02;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100108 u64 hsave_msr;
109 u64 vm_cr_msr;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300110 u64 vmcb12_gpa;
Cathy Avery81733962021-03-01 15:08:44 -0500111 u64 last_vmcb12_gpa;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100112
113 /* These are the merged vectors */
114 u32 *msrpm;
115
Paolo Bonzinif74f9412020-04-23 13:22:27 -0400116 /* A VMRUN has started but has not yet been performed, so
117 * we cannot inject a nested vmexit yet. */
118 bool nested_run_pending;
119
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400120 /* cache for control fields of the guest */
121 struct vmcb_control_area ctl;
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300122
123 bool initialized;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100124};
125
126struct vcpu_svm {
127 struct kvm_vcpu vcpu;
Sean Christopherson554cf312021-04-06 10:18:10 -0700128 /* vmcb always points at current_vmcb->ptr, it's purely a shorthand. */
Joerg Roedel883b0a92020-03-24 10:41:52 +0100129 struct vmcb *vmcb;
Cathy Avery4995a362021-01-13 07:07:52 -0500130 struct kvm_vmcb_info vmcb01;
131 struct kvm_vmcb_info *current_vmcb;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100132 struct svm_cpu_data *svm_data;
Cathy Avery7e8e6ee2020-10-11 14:48:17 -0400133 u32 asid;
Maxim Levitskyadc2a232021-04-01 14:19:28 +0300134 u32 sysenter_esp_hi;
135 u32 sysenter_eip_hi;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100136 uint64_t tsc_aux;
137
138 u64 msr_decfg;
139
140 u64 next_rip;
141
Joerg Roedel883b0a92020-03-24 10:41:52 +0100142 u64 spec_ctrl;
143 /*
144 * Contains guest-controlled bits of VIRT_SPEC_CTRL, which will be
145 * translated into the appropriate L2_CFG bits on the host to
146 * perform speculative control.
147 */
148 u64 virt_spec_ctrl;
149
150 u32 *msrpm;
151
152 ulong nmi_iret_rip;
153
Joerg Roedel7693b3e2020-06-25 10:03:22 +0200154 struct svm_nested_state nested;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100155
156 bool nmi_singlestep;
157 u64 nmi_singlestep_guest_rflags;
158
159 unsigned int3_injected;
160 unsigned long int3_rip;
161
162 /* cached guest cpuid flags for faster access */
163 bool nrips_enabled : 1;
164
165 u32 ldr_reg;
166 u32 dfr_reg;
167 struct page *avic_backing_page;
168 u64 *avic_physical_id_cache;
169 bool avic_is_running;
170
171 /*
172 * Per-vcpu list of struct amd_svm_iommu_ir:
173 * This is used mainly to store interrupt remapping information used
174 * when update the vcpu affinity. This avoids the need to scan for
175 * IRTE and try to match ga_tag in the IOMMU driver.
176 */
177 struct list_head ir_list;
178 spinlock_t ir_list_lock;
Alexander Graffd6fa732020-09-25 16:34:19 +0200179
180 /* Save desired MSR intercept (read: pass-through) state */
181 struct {
182 DECLARE_BITMAP(read, MAX_DIRECT_ACCESS_MSRS);
183 DECLARE_BITMAP(write, MAX_DIRECT_ACCESS_MSRS);
184 } shadow_msr_intercept;
Tom Lendackyadd5e2f2020-12-10 11:09:40 -0600185
186 /* SEV-ES support */
187 struct vmcb_save_area *vmsa;
188 struct ghcb *ghcb;
Tom Lendacky291bd202020-12-10 11:09:47 -0600189 struct kvm_host_map ghcb_map;
Tom Lendacky647daca2021-01-04 14:20:01 -0600190 bool received_first_sipi;
Tom Lendacky8f423a82020-12-10 11:09:53 -0600191
192 /* SEV-ES scratch area support */
193 void *ghcb_sa;
194 u64 ghcb_sa_len;
195 bool ghcb_sa_sync;
196 bool ghcb_sa_free;
Michael Rotha7fc06d2021-02-02 13:01:26 -0600197
198 bool guest_state_loaded;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100199};
200
Joerg Roedeleaf78262020-03-24 10:41:54 +0100201struct svm_cpu_data {
202 int cpu;
203
204 u64 asid_generation;
205 u32 max_asid;
206 u32 next_asid;
207 u32 min_asid;
208 struct kvm_ldttss_desc *tss_desc;
209
210 struct page *save_area;
211 struct vmcb *current_vmcb;
212
213 /* index = sev_asid, value = vmcb pointer */
214 struct vmcb **sev_vmcbs;
215};
216
217DECLARE_PER_CPU(struct svm_cpu_data *, svm_data);
218
Joerg Roedel883b0a92020-03-24 10:41:52 +0100219void recalc_intercepts(struct vcpu_svm *svm);
220
Joerg Roedelef0f6492020-03-31 12:17:38 -0400221static inline struct kvm_svm *to_kvm_svm(struct kvm *kvm)
222{
223 return container_of(kvm, struct kvm_svm, kvm);
224}
225
Tom Lendacky916391a2020-12-10 11:09:38 -0600226static inline bool sev_guest(struct kvm *kvm)
227{
228#ifdef CONFIG_KVM_AMD_SEV
229 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
230
231 return sev->active;
232#else
233 return false;
234#endif
235}
236
237static inline bool sev_es_guest(struct kvm *kvm)
238{
239#ifdef CONFIG_KVM_AMD_SEV
240 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
241
242 return sev_guest(kvm) && sev->es_active;
243#else
244 return false;
245#endif
246}
247
Joerg Roedel06e78522020-06-25 10:03:23 +0200248static inline void vmcb_mark_all_dirty(struct vmcb *vmcb)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100249{
250 vmcb->control.clean = 0;
251}
252
Joerg Roedel06e78522020-06-25 10:03:23 +0200253static inline void vmcb_mark_all_clean(struct vmcb *vmcb)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100254{
Vineeth Pillai59d21d62021-06-03 15:14:37 +0000255 vmcb->control.clean = VMCB_ALL_CLEAN_MASK
Joerg Roedel883b0a92020-03-24 10:41:52 +0100256 & ~VMCB_ALWAYS_DIRTY_MASK;
257}
258
Vineeth Pillaic4327f12021-06-03 15:14:39 +0000259static inline bool vmcb_is_clean(struct vmcb *vmcb, int bit)
260{
261 return (vmcb->control.clean & (1 << bit));
262}
263
Joerg Roedel06e78522020-06-25 10:03:23 +0200264static inline void vmcb_mark_dirty(struct vmcb *vmcb, int bit)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100265{
266 vmcb->control.clean &= ~(1 << bit);
267}
268
Cathy Avery81733962021-03-01 15:08:44 -0500269static inline bool vmcb_is_dirty(struct vmcb *vmcb, int bit)
270{
271 return !test_bit(bit, (unsigned long *)&vmcb->control.clean);
272}
273
Joerg Roedel883b0a92020-03-24 10:41:52 +0100274static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
275{
276 return container_of(vcpu, struct vcpu_svm, vcpu);
277}
278
Babu Mogerc45ad722020-09-11 14:27:58 -0500279static inline void vmcb_set_intercept(struct vmcb_control_area *control, u32 bit)
280{
281 WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
282 __set_bit(bit, (unsigned long *)&control->intercepts);
283}
284
285static inline void vmcb_clr_intercept(struct vmcb_control_area *control, u32 bit)
286{
287 WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
288 __clear_bit(bit, (unsigned long *)&control->intercepts);
289}
290
291static inline bool vmcb_is_intercept(struct vmcb_control_area *control, u32 bit)
292{
293 WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
294 return test_bit(bit, (unsigned long *)&control->intercepts);
295}
296
Joerg Roedel883b0a92020-03-24 10:41:52 +0100297static inline void set_dr_intercepts(struct vcpu_svm *svm)
298{
Cathy Avery4995a362021-01-13 07:07:52 -0500299 struct vmcb *vmcb = svm->vmcb01.ptr;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100300
Tom Lendacky8d4846b2020-12-10 11:09:43 -0600301 if (!sev_es_guest(svm->vcpu.kvm)) {
302 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR0_READ);
303 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR1_READ);
304 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR2_READ);
305 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR3_READ);
306 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR4_READ);
307 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR5_READ);
308 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR6_READ);
309 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR0_WRITE);
310 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR1_WRITE);
311 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR2_WRITE);
312 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR3_WRITE);
313 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR4_WRITE);
314 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR5_WRITE);
315 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR6_WRITE);
316 }
317
Babu Moger30abaa882020-09-11 14:28:12 -0500318 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_READ);
Babu Moger30abaa882020-09-11 14:28:12 -0500319 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_WRITE);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100320
321 recalc_intercepts(svm);
322}
323
324static inline void clr_dr_intercepts(struct vcpu_svm *svm)
325{
Cathy Avery4995a362021-01-13 07:07:52 -0500326 struct vmcb *vmcb = svm->vmcb01.ptr;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100327
Babu Moger30abaa882020-09-11 14:28:12 -0500328 vmcb->control.intercepts[INTERCEPT_DR] = 0;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100329
Tom Lendacky8d4846b2020-12-10 11:09:43 -0600330 /* DR7 access must remain intercepted for an SEV-ES guest */
331 if (sev_es_guest(svm->vcpu.kvm)) {
332 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_READ);
333 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_WRITE);
334 }
335
Joerg Roedel883b0a92020-03-24 10:41:52 +0100336 recalc_intercepts(svm);
337}
338
Babu Moger9780d512020-09-11 14:28:20 -0500339static inline void set_exception_intercept(struct vcpu_svm *svm, u32 bit)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100340{
Cathy Avery4995a362021-01-13 07:07:52 -0500341 struct vmcb *vmcb = svm->vmcb01.ptr;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100342
Babu Moger9780d512020-09-11 14:28:20 -0500343 WARN_ON_ONCE(bit >= 32);
344 vmcb_set_intercept(&vmcb->control, INTERCEPT_EXCEPTION_OFFSET + bit);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100345
346 recalc_intercepts(svm);
347}
348
Babu Moger9780d512020-09-11 14:28:20 -0500349static inline void clr_exception_intercept(struct vcpu_svm *svm, u32 bit)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100350{
Cathy Avery4995a362021-01-13 07:07:52 -0500351 struct vmcb *vmcb = svm->vmcb01.ptr;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100352
Babu Moger9780d512020-09-11 14:28:20 -0500353 WARN_ON_ONCE(bit >= 32);
354 vmcb_clr_intercept(&vmcb->control, INTERCEPT_EXCEPTION_OFFSET + bit);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100355
356 recalc_intercepts(svm);
357}
358
Joerg Roedela284ba52020-06-25 10:03:24 +0200359static inline void svm_set_intercept(struct vcpu_svm *svm, int bit)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100360{
Cathy Avery4995a362021-01-13 07:07:52 -0500361 struct vmcb *vmcb = svm->vmcb01.ptr;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100362
Babu Mogerc62e2e92020-09-11 14:28:28 -0500363 vmcb_set_intercept(&vmcb->control, bit);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100364
365 recalc_intercepts(svm);
366}
367
Joerg Roedela284ba52020-06-25 10:03:24 +0200368static inline void svm_clr_intercept(struct vcpu_svm *svm, int bit)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100369{
Cathy Avery4995a362021-01-13 07:07:52 -0500370 struct vmcb *vmcb = svm->vmcb01.ptr;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100371
Babu Mogerc62e2e92020-09-11 14:28:28 -0500372 vmcb_clr_intercept(&vmcb->control, bit);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100373
374 recalc_intercepts(svm);
375}
376
Joerg Roedela284ba52020-06-25 10:03:24 +0200377static inline bool svm_is_intercept(struct vcpu_svm *svm, int bit)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100378{
Babu Mogerc62e2e92020-09-11 14:28:28 -0500379 return vmcb_is_intercept(&svm->vmcb->control, bit);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100380}
381
382static inline bool vgif_enabled(struct vcpu_svm *svm)
383{
384 return !!(svm->vmcb->control.int_ctl & V_GIF_ENABLE_MASK);
385}
386
387static inline void enable_gif(struct vcpu_svm *svm)
388{
389 if (vgif_enabled(svm))
390 svm->vmcb->control.int_ctl |= V_GIF_MASK;
391 else
392 svm->vcpu.arch.hflags |= HF_GIF_MASK;
393}
394
395static inline void disable_gif(struct vcpu_svm *svm)
396{
397 if (vgif_enabled(svm))
398 svm->vmcb->control.int_ctl &= ~V_GIF_MASK;
399 else
400 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
401}
402
403static inline bool gif_set(struct vcpu_svm *svm)
404{
405 if (vgif_enabled(svm))
406 return !!(svm->vmcb->control.int_ctl & V_GIF_MASK);
407 else
408 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
409}
410
411/* svm.c */
Krish Sadhukhan761e4162020-07-08 00:39:56 +0000412#define MSR_INVALID 0xffffffffU
Joerg Roedel883b0a92020-03-24 10:41:52 +0100413
Tom Lendacky291bd202020-12-10 11:09:47 -0600414extern bool dump_invalid_vmcb;
Tom Lendacky916391a2020-12-10 11:09:38 -0600415
Joerg Roedel883b0a92020-03-24 10:41:52 +0100416u32 svm_msrpm_offset(u32 msr);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300417u32 *svm_vcpu_alloc_msrpm(void);
418void svm_vcpu_init_msrpm(struct kvm_vcpu *vcpu, u32 *msrpm);
419void svm_vcpu_free_msrpm(u32 *msrpm);
420
Maxim Levitsky72f211e2020-10-01 14:29:53 +0300421int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100422void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
Sean Christophersonc2fe3cd2020-10-06 18:44:15 -0700423void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sean Christophersonf55ac302020-03-20 14:28:12 -0700424void svm_flush_tlb(struct kvm_vcpu *vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100425void disable_nmi_singlestep(struct vcpu_svm *svm);
Paolo Bonzinicae96af2020-04-23 14:19:26 -0400426bool svm_smi_blocked(struct kvm_vcpu *vcpu);
427bool svm_nmi_blocked(struct kvm_vcpu *vcpu);
428bool svm_interrupt_blocked(struct kvm_vcpu *vcpu);
Paolo Bonziniffdf7f92020-05-22 12:18:27 -0400429void svm_set_gif(struct vcpu_svm *svm, bool value);
Paolo Bonzini63129752021-03-02 14:40:39 -0500430int svm_invoke_exit_handler(struct kvm_vcpu *vcpu, u64 exit_code);
Tom Lendacky376c6d22020-12-10 11:10:06 -0600431void set_msr_interception(struct kvm_vcpu *vcpu, u32 *msrpm, u32 msr,
432 int read, int write);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100433
434/* nested.c */
435
436#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
437#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
438#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
439
Joerg Roedel01c3b2b2020-06-25 10:03:25 +0200440static inline bool nested_svm_virtualize_tpr(struct kvm_vcpu *vcpu)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100441{
Paolo Bonzinie9fd7612020-05-13 13:28:23 -0400442 struct vcpu_svm *svm = to_svm(vcpu);
443
444 return is_guest_mode(vcpu) && (svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100445}
446
Paolo Bonzini55714cd2020-04-23 08:17:28 -0400447static inline bool nested_exit_on_smi(struct vcpu_svm *svm)
448{
Babu Mogerc62e2e92020-09-11 14:28:28 -0500449 return vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_SMI);
Paolo Bonzini55714cd2020-04-23 08:17:28 -0400450}
451
Paolo Bonzinifc6f7c02020-04-23 18:02:45 -0400452static inline bool nested_exit_on_intr(struct vcpu_svm *svm)
453{
Babu Mogerc62e2e92020-09-11 14:28:28 -0500454 return vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_INTR);
Paolo Bonzinifc6f7c02020-04-23 18:02:45 -0400455}
456
Paolo Bonzinibbdad0b2020-04-23 08:06:43 -0400457static inline bool nested_exit_on_nmi(struct vcpu_svm *svm)
458{
Babu Mogerc62e2e92020-09-11 14:28:28 -0500459 return vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_NMI);
Paolo Bonzinibbdad0b2020-04-23 08:06:43 -0400460}
461
Maxim Levitskye85d3e72021-09-13 17:09:51 +0300462int enter_svm_guest_mode(struct kvm_vcpu *vcpu,
463 u64 vmcb_gpa, struct vmcb *vmcb12, bool from_vmrun);
Paolo Bonzinic513f482020-05-18 13:08:37 -0400464void svm_leave_nested(struct vcpu_svm *svm);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300465void svm_free_nested(struct vcpu_svm *svm);
466int svm_allocate_nested(struct vcpu_svm *svm);
Paolo Bonzini63129752021-03-02 14:40:39 -0500467int nested_svm_vmrun(struct kvm_vcpu *vcpu);
Vitaly Kuznetsov2bb16be2021-07-19 11:03:22 +0200468void svm_copy_vmrun_state(struct vmcb_save_area *to_save,
469 struct vmcb_save_area *from_save);
470void svm_copy_vmloadsave_state(struct vmcb *to_vmcb, struct vmcb *from_vmcb);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100471int nested_svm_vmexit(struct vcpu_svm *svm);
Sean Christopherson3a87c7e2021-03-02 09:45:15 -0800472
473static inline int nested_svm_simple_vmexit(struct vcpu_svm *svm, u32 exit_code)
474{
475 svm->vmcb->control.exit_code = exit_code;
476 svm->vmcb->control.exit_info_1 = 0;
477 svm->vmcb->control.exit_info_2 = 0;
478 return nested_svm_vmexit(svm);
479}
480
Joerg Roedel883b0a92020-03-24 10:41:52 +0100481int nested_svm_exit_handled(struct vcpu_svm *svm);
Paolo Bonzini63129752021-03-02 14:40:39 -0500482int nested_svm_check_permissions(struct kvm_vcpu *vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100483int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
484 bool has_error_code, u32 error_code);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100485int nested_svm_exit_special(struct vcpu_svm *svm);
Vitaly Kuznetsovbb00bd92021-06-28 12:44:24 +0200486void nested_load_control_from_vmcb12(struct vcpu_svm *svm,
487 struct vmcb_control_area *control);
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500488void nested_sync_control_from_vmcb02(struct vcpu_svm *svm);
Cathy Avery4995a362021-01-13 07:07:52 -0500489void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm);
490void svm_switch_vmcb(struct vcpu_svm *svm, struct kvm_vmcb_info *target_vmcb);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100491
Paolo Bonzini33b22172020-04-17 10:24:18 -0400492extern struct kvm_x86_nested_ops svm_nested_ops;
493
Joerg Roedelef0f6492020-03-31 12:17:38 -0400494/* avic.c */
495
496#define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFF)
497#define AVIC_LOGICAL_ID_ENTRY_VALID_BIT 31
498#define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31)
499
500#define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK (0xFFULL)
501#define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK (0xFFFFFFFFFFULL << 12)
502#define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK (1ULL << 62)
503#define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK (1ULL << 63)
504
505#define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL
506
Joerg Roedelef0f6492020-03-31 12:17:38 -0400507static inline bool avic_vcpu_is_running(struct kvm_vcpu *vcpu)
508{
509 struct vcpu_svm *svm = to_svm(vcpu);
510 u64 *entry = svm->avic_physical_id_cache;
511
512 if (!entry)
513 return false;
514
515 return (READ_ONCE(*entry) & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
516}
517
518int avic_ga_log_notifier(u32 ga_tag);
519void avic_vm_destroy(struct kvm *kvm);
520int avic_vm_init(struct kvm *kvm);
521void avic_init_vmcb(struct vcpu_svm *svm);
Paolo Bonzini63129752021-03-02 14:40:39 -0500522int avic_incomplete_ipi_interception(struct kvm_vcpu *vcpu);
523int avic_unaccelerated_access_interception(struct kvm_vcpu *vcpu);
Joerg Roedelef0f6492020-03-31 12:17:38 -0400524int avic_init_vcpu(struct vcpu_svm *svm);
525void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
526void avic_vcpu_put(struct kvm_vcpu *vcpu);
527void avic_post_state_restore(struct kvm_vcpu *vcpu);
528void svm_set_virtual_apic_mode(struct kvm_vcpu *vcpu);
529void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu);
530bool svm_check_apicv_inhibit_reasons(ulong bit);
Joerg Roedelef0f6492020-03-31 12:17:38 -0400531void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap);
532void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr);
533void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr);
534int svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec);
535bool svm_dy_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu);
536int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
537 uint32_t guest_irq, bool set);
538void svm_vcpu_blocking(struct kvm_vcpu *vcpu);
539void svm_vcpu_unblocking(struct kvm_vcpu *vcpu);
540
Joerg Roedeleaf78262020-03-24 10:41:54 +0100541/* sev.c */
542
Brijesh Singhb81fc742021-04-27 06:16:35 -0500543#define GHCB_VERSION_MAX 1ULL
544#define GHCB_VERSION_MIN 1ULL
Tom Lendacky1edc1452020-12-10 11:09:49 -0600545
Tom Lendackye1d71112020-12-10 11:09:51 -0600546
Joerg Roedeleaf78262020-03-24 10:41:54 +0100547extern unsigned int max_sev_asid;
548
Joerg Roedeleaf78262020-03-24 10:41:54 +0100549void sev_vm_destroy(struct kvm *kvm);
550int svm_mem_enc_op(struct kvm *kvm, void __user *argp);
551int svm_register_enc_region(struct kvm *kvm,
552 struct kvm_enc_region *range);
553int svm_unregister_enc_region(struct kvm *kvm,
554 struct kvm_enc_region *range);
Nathan Tempelman54526d12021-04-08 22:32:14 +0000555int svm_vm_copy_asid_from(struct kvm *kvm, unsigned int source_fd);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100556void pre_sev_run(struct vcpu_svm *svm, int cpu);
Paolo Bonzinid9db0fd2021-04-21 19:11:15 -0700557void __init sev_set_cpu_caps(void);
Tom Lendacky916391a2020-12-10 11:09:38 -0600558void __init sev_hardware_setup(void);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100559void sev_hardware_teardown(void);
Sean Christophersonb95c2212021-04-21 19:11:22 -0700560int sev_cpu_init(struct svm_cpu_data *sd);
Tom Lendackyadd5e2f2020-12-10 11:09:40 -0600561void sev_free_vcpu(struct kvm_vcpu *vcpu);
Paolo Bonzini63129752021-03-02 14:40:39 -0500562int sev_handle_vmgexit(struct kvm_vcpu *vcpu);
Tom Lendacky7ed9abf2020-12-10 11:09:54 -0600563int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in);
Tom Lendacky376c6d22020-12-10 11:10:06 -0600564void sev_es_init_vmcb(struct vcpu_svm *svm);
Sean Christopherson9ebe5302021-09-20 17:03:02 -0700565void sev_es_vcpu_reset(struct vcpu_svm *svm);
Tom Lendacky647daca2021-01-04 14:20:01 -0600566void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector);
Michael Rotha7fc06d2021-02-02 13:01:26 -0600567void sev_es_prepare_guest_switch(struct vcpu_svm *svm, unsigned int cpu);
Tom Lendackyce7ea0c2021-05-06 15:14:41 -0500568void sev_es_unmap_ghcb(struct vcpu_svm *svm);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100569
Tom Lendacky16809ec2020-12-10 11:10:08 -0600570/* vmenter.S */
571
572void __svm_sev_es_vcpu_run(unsigned long vmcb_pa);
573void __svm_vcpu_run(unsigned long vmcb_pa, unsigned long *regs);
574
Joerg Roedel883b0a92020-03-24 10:41:52 +0100575#endif