blob: 9806aaebc37ffc434dad499b2c550b7d679b301c [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>
23
Tom Lendacky85ca8be2020-12-10 11:10:04 -060024#define __sme_page_pa(x) __sme_set(page_to_pfn(x) << PAGE_SHIFT)
25
Michael Roth553cc152021-02-02 13:01:25 -060026static const u32 host_save_user_msrs[] = {
27 MSR_TSC_AUX,
Joerg Roedel883b0a92020-03-24 10:41:52 +010028};
Joerg Roedel883b0a92020-03-24 10:41:52 +010029#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
30
Tom Lendacky376c6d22020-12-10 11:10:06 -060031#define MAX_DIRECT_ACCESS_MSRS 18
Joerg Roedel883b0a92020-03-24 10:41:52 +010032#define MSRPM_OFFSETS 16
33extern u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
34extern bool npt_enabled;
35
36enum {
37 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
38 pause filter count */
39 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
40 VMCB_ASID, /* ASID */
41 VMCB_INTR, /* int_ctl, int_vector */
42 VMCB_NPT, /* npt_en, nCR3, gPAT */
43 VMCB_CR, /* CR0, CR3, CR4, EFER */
44 VMCB_DR, /* DR6, DR7 */
45 VMCB_DT, /* GDT, IDT */
46 VMCB_SEG, /* CS, DS, SS, ES, CPL */
47 VMCB_CR2, /* CR2 only */
48 VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
49 VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE,
50 * AVIC PHYSICAL_TABLE pointer,
51 * AVIC LOGICAL_TABLE pointer
52 */
53 VMCB_DIRTY_MAX,
54};
55
56/* TPR and CR2 are always written before VMRUN */
57#define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
58
59struct kvm_sev_info {
60 bool active; /* SEV enabled guest */
Tom Lendacky916391a2020-12-10 11:09:38 -060061 bool es_active; /* SEV-ES enabled guest */
Joerg Roedel883b0a92020-03-24 10:41:52 +010062 unsigned int asid; /* ASID used for this guest */
63 unsigned int handle; /* SEV firmware handle */
64 int fd; /* SEV device fd */
65 unsigned long pages_locked; /* Number of pages locked */
66 struct list_head regions_list; /* List of registered regions */
Tom Lendacky8640ca52020-12-15 12:44:07 -050067 u64 ap_jump_table; /* SEV-ES AP Jump Table address */
Vipin Sharma7aef27f2021-03-29 21:42:06 -070068 struct misc_cg *misc_cg; /* For misc cgroup accounting */
Joerg Roedel883b0a92020-03-24 10:41:52 +010069};
70
71struct kvm_svm {
72 struct kvm kvm;
73
74 /* Struct members for AVIC */
75 u32 avic_vm_id;
76 struct page *avic_logical_id_table_page;
77 struct page *avic_physical_id_table_page;
78 struct hlist_node hnode;
79
80 struct kvm_sev_info sev_info;
81};
82
83struct kvm_vcpu;
84
Joerg Roedel7693b3e2020-06-25 10:03:22 +020085struct svm_nested_state {
Joerg Roedel883b0a92020-03-24 10:41:52 +010086 struct vmcb *hsave;
87 u64 hsave_msr;
88 u64 vm_cr_msr;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +030089 u64 vmcb12_gpa;
Joerg Roedel883b0a92020-03-24 10:41:52 +010090
91 /* These are the merged vectors */
92 u32 *msrpm;
93
Paolo Bonzinif74f9412020-04-23 13:22:27 -040094 /* 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
Paolo Bonzinie670bf62020-05-13 13:16:12 -040098 /* cache for control fields of the guest */
99 struct vmcb_control_area ctl;
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300100
101 bool initialized;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100102};
103
104struct vcpu_svm {
105 struct kvm_vcpu vcpu;
106 struct vmcb *vmcb;
107 unsigned long vmcb_pa;
108 struct svm_cpu_data *svm_data;
Cathy Avery7e8e6ee2020-10-11 14:48:17 -0400109 u32 asid;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100110 uint64_t asid_generation;
111 uint64_t sysenter_esp;
112 uint64_t sysenter_eip;
113 uint64_t tsc_aux;
114
115 u64 msr_decfg;
116
117 u64 next_rip;
118
119 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
Joerg Roedel883b0a92020-03-24 10:41:52 +0100120
121 u64 spec_ctrl;
122 /*
123 * Contains guest-controlled bits of VIRT_SPEC_CTRL, which will be
124 * translated into the appropriate L2_CFG bits on the host to
125 * perform speculative control.
126 */
127 u64 virt_spec_ctrl;
128
129 u32 *msrpm;
130
131 ulong nmi_iret_rip;
132
Joerg Roedel7693b3e2020-06-25 10:03:22 +0200133 struct svm_nested_state nested;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100134
135 bool nmi_singlestep;
136 u64 nmi_singlestep_guest_rflags;
137
138 unsigned int3_injected;
139 unsigned long int3_rip;
140
141 /* cached guest cpuid flags for faster access */
142 bool nrips_enabled : 1;
143
144 u32 ldr_reg;
145 u32 dfr_reg;
146 struct page *avic_backing_page;
147 u64 *avic_physical_id_cache;
148 bool avic_is_running;
149
150 /*
151 * Per-vcpu list of struct amd_svm_iommu_ir:
152 * This is used mainly to store interrupt remapping information used
153 * when update the vcpu affinity. This avoids the need to scan for
154 * IRTE and try to match ga_tag in the IOMMU driver.
155 */
156 struct list_head ir_list;
157 spinlock_t ir_list_lock;
Alexander Graffd6fa732020-09-25 16:34:19 +0200158
159 /* Save desired MSR intercept (read: pass-through) state */
160 struct {
161 DECLARE_BITMAP(read, MAX_DIRECT_ACCESS_MSRS);
162 DECLARE_BITMAP(write, MAX_DIRECT_ACCESS_MSRS);
163 } shadow_msr_intercept;
Tom Lendackyadd5e2f2020-12-10 11:09:40 -0600164
165 /* SEV-ES support */
166 struct vmcb_save_area *vmsa;
167 struct ghcb *ghcb;
Tom Lendacky291bd202020-12-10 11:09:47 -0600168 struct kvm_host_map ghcb_map;
Tom Lendacky647daca2021-01-04 14:20:01 -0600169 bool received_first_sipi;
Tom Lendacky8f423a82020-12-10 11:09:53 -0600170
171 /* SEV-ES scratch area support */
172 void *ghcb_sa;
173 u64 ghcb_sa_len;
174 bool ghcb_sa_sync;
175 bool ghcb_sa_free;
Michael Rotha7fc06d2021-02-02 13:01:26 -0600176
177 bool guest_state_loaded;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100178};
179
Joerg Roedeleaf78262020-03-24 10:41:54 +0100180struct svm_cpu_data {
181 int cpu;
182
183 u64 asid_generation;
184 u32 max_asid;
185 u32 next_asid;
186 u32 min_asid;
187 struct kvm_ldttss_desc *tss_desc;
188
189 struct page *save_area;
190 struct vmcb *current_vmcb;
191
192 /* index = sev_asid, value = vmcb pointer */
193 struct vmcb **sev_vmcbs;
194};
195
196DECLARE_PER_CPU(struct svm_cpu_data *, svm_data);
197
Joerg Roedel883b0a92020-03-24 10:41:52 +0100198void recalc_intercepts(struct vcpu_svm *svm);
199
Joerg Roedelef0f6492020-03-31 12:17:38 -0400200static inline struct kvm_svm *to_kvm_svm(struct kvm *kvm)
201{
202 return container_of(kvm, struct kvm_svm, kvm);
203}
204
Tom Lendacky916391a2020-12-10 11:09:38 -0600205static inline bool sev_guest(struct kvm *kvm)
206{
207#ifdef CONFIG_KVM_AMD_SEV
208 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
209
210 return sev->active;
211#else
212 return false;
213#endif
214}
215
216static inline bool sev_es_guest(struct kvm *kvm)
217{
218#ifdef CONFIG_KVM_AMD_SEV
219 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
220
221 return sev_guest(kvm) && sev->es_active;
222#else
223 return false;
224#endif
225}
226
Joerg Roedel06e78522020-06-25 10:03:23 +0200227static inline void vmcb_mark_all_dirty(struct vmcb *vmcb)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100228{
229 vmcb->control.clean = 0;
230}
231
Joerg Roedel06e78522020-06-25 10:03:23 +0200232static inline void vmcb_mark_all_clean(struct vmcb *vmcb)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100233{
234 vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
235 & ~VMCB_ALWAYS_DIRTY_MASK;
236}
237
Joerg Roedel06e78522020-06-25 10:03:23 +0200238static inline void vmcb_mark_dirty(struct vmcb *vmcb, int bit)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100239{
240 vmcb->control.clean &= ~(1 << bit);
241}
242
243static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
244{
245 return container_of(vcpu, struct vcpu_svm, vcpu);
246}
247
248static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
249{
250 if (is_guest_mode(&svm->vcpu))
251 return svm->nested.hsave;
252 else
253 return svm->vmcb;
254}
255
Babu Mogerc45ad722020-09-11 14:27:58 -0500256static inline void vmcb_set_intercept(struct vmcb_control_area *control, u32 bit)
257{
258 WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
259 __set_bit(bit, (unsigned long *)&control->intercepts);
260}
261
262static inline void vmcb_clr_intercept(struct vmcb_control_area *control, u32 bit)
263{
264 WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
265 __clear_bit(bit, (unsigned long *)&control->intercepts);
266}
267
268static inline bool vmcb_is_intercept(struct vmcb_control_area *control, u32 bit)
269{
270 WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
271 return test_bit(bit, (unsigned long *)&control->intercepts);
272}
273
Joerg Roedel883b0a92020-03-24 10:41:52 +0100274static inline void set_dr_intercepts(struct vcpu_svm *svm)
275{
276 struct vmcb *vmcb = get_host_vmcb(svm);
277
Tom Lendacky8d4846b2020-12-10 11:09:43 -0600278 if (!sev_es_guest(svm->vcpu.kvm)) {
279 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR0_READ);
280 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR1_READ);
281 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR2_READ);
282 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR3_READ);
283 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR4_READ);
284 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR5_READ);
285 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR6_READ);
286 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR0_WRITE);
287 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR1_WRITE);
288 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR2_WRITE);
289 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR3_WRITE);
290 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR4_WRITE);
291 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR5_WRITE);
292 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR6_WRITE);
293 }
294
Babu Moger30abaa882020-09-11 14:28:12 -0500295 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_READ);
Babu Moger30abaa882020-09-11 14:28:12 -0500296 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_WRITE);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100297
298 recalc_intercepts(svm);
299}
300
301static inline void clr_dr_intercepts(struct vcpu_svm *svm)
302{
303 struct vmcb *vmcb = get_host_vmcb(svm);
304
Babu Moger30abaa882020-09-11 14:28:12 -0500305 vmcb->control.intercepts[INTERCEPT_DR] = 0;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100306
Tom Lendacky8d4846b2020-12-10 11:09:43 -0600307 /* DR7 access must remain intercepted for an SEV-ES guest */
308 if (sev_es_guest(svm->vcpu.kvm)) {
309 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_READ);
310 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_WRITE);
311 }
312
Joerg Roedel883b0a92020-03-24 10:41:52 +0100313 recalc_intercepts(svm);
314}
315
Babu Moger9780d512020-09-11 14:28:20 -0500316static inline void set_exception_intercept(struct vcpu_svm *svm, u32 bit)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100317{
318 struct vmcb *vmcb = get_host_vmcb(svm);
319
Babu Moger9780d512020-09-11 14:28:20 -0500320 WARN_ON_ONCE(bit >= 32);
321 vmcb_set_intercept(&vmcb->control, INTERCEPT_EXCEPTION_OFFSET + bit);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100322
323 recalc_intercepts(svm);
324}
325
Babu Moger9780d512020-09-11 14:28:20 -0500326static inline void clr_exception_intercept(struct vcpu_svm *svm, u32 bit)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100327{
328 struct vmcb *vmcb = get_host_vmcb(svm);
329
Babu Moger9780d512020-09-11 14:28:20 -0500330 WARN_ON_ONCE(bit >= 32);
331 vmcb_clr_intercept(&vmcb->control, INTERCEPT_EXCEPTION_OFFSET + bit);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100332
333 recalc_intercepts(svm);
334}
335
Joerg Roedela284ba52020-06-25 10:03:24 +0200336static inline void svm_set_intercept(struct vcpu_svm *svm, int bit)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100337{
338 struct vmcb *vmcb = get_host_vmcb(svm);
339
Babu Mogerc62e2e92020-09-11 14:28:28 -0500340 vmcb_set_intercept(&vmcb->control, bit);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100341
342 recalc_intercepts(svm);
343}
344
Joerg Roedela284ba52020-06-25 10:03:24 +0200345static inline void svm_clr_intercept(struct vcpu_svm *svm, int bit)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100346{
347 struct vmcb *vmcb = get_host_vmcb(svm);
348
Babu Mogerc62e2e92020-09-11 14:28:28 -0500349 vmcb_clr_intercept(&vmcb->control, bit);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100350
351 recalc_intercepts(svm);
352}
353
Joerg Roedela284ba52020-06-25 10:03:24 +0200354static inline bool svm_is_intercept(struct vcpu_svm *svm, int bit)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100355{
Babu Mogerc62e2e92020-09-11 14:28:28 -0500356 return vmcb_is_intercept(&svm->vmcb->control, bit);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100357}
358
359static inline bool vgif_enabled(struct vcpu_svm *svm)
360{
361 return !!(svm->vmcb->control.int_ctl & V_GIF_ENABLE_MASK);
362}
363
364static inline void enable_gif(struct vcpu_svm *svm)
365{
366 if (vgif_enabled(svm))
367 svm->vmcb->control.int_ctl |= V_GIF_MASK;
368 else
369 svm->vcpu.arch.hflags |= HF_GIF_MASK;
370}
371
372static inline void disable_gif(struct vcpu_svm *svm)
373{
374 if (vgif_enabled(svm))
375 svm->vmcb->control.int_ctl &= ~V_GIF_MASK;
376 else
377 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
378}
379
380static inline bool gif_set(struct vcpu_svm *svm)
381{
382 if (vgif_enabled(svm))
383 return !!(svm->vmcb->control.int_ctl & V_GIF_MASK);
384 else
385 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
386}
387
388/* svm.c */
Krish Sadhukhan761e4162020-07-08 00:39:56 +0000389#define MSR_INVALID 0xffffffffU
Joerg Roedel883b0a92020-03-24 10:41:52 +0100390
Tom Lendacky916391a2020-12-10 11:09:38 -0600391extern int sev;
392extern int sev_es;
Tom Lendacky291bd202020-12-10 11:09:47 -0600393extern bool dump_invalid_vmcb;
Tom Lendacky916391a2020-12-10 11:09:38 -0600394
Joerg Roedel883b0a92020-03-24 10:41:52 +0100395u32 svm_msrpm_offset(u32 msr);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300396u32 *svm_vcpu_alloc_msrpm(void);
397void svm_vcpu_init_msrpm(struct kvm_vcpu *vcpu, u32 *msrpm);
398void svm_vcpu_free_msrpm(u32 *msrpm);
399
Maxim Levitsky72f211e2020-10-01 14:29:53 +0300400int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100401void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
Sean Christophersonc2fe3cd2020-10-06 18:44:15 -0700402void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sean Christophersonf55ac302020-03-20 14:28:12 -0700403void svm_flush_tlb(struct kvm_vcpu *vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100404void disable_nmi_singlestep(struct vcpu_svm *svm);
Paolo Bonzinicae96af2020-04-23 14:19:26 -0400405bool svm_smi_blocked(struct kvm_vcpu *vcpu);
406bool svm_nmi_blocked(struct kvm_vcpu *vcpu);
407bool svm_interrupt_blocked(struct kvm_vcpu *vcpu);
Paolo Bonziniffdf7f92020-05-22 12:18:27 -0400408void svm_set_gif(struct vcpu_svm *svm, bool value);
Tom Lendacky291bd202020-12-10 11:09:47 -0600409int svm_invoke_exit_handler(struct vcpu_svm *svm, u64 exit_code);
Tom Lendacky376c6d22020-12-10 11:10:06 -0600410void set_msr_interception(struct kvm_vcpu *vcpu, u32 *msrpm, u32 msr,
411 int read, int write);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100412
413/* nested.c */
414
415#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
416#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
417#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
418
Joerg Roedel01c3b2b2020-06-25 10:03:25 +0200419static inline bool nested_svm_virtualize_tpr(struct kvm_vcpu *vcpu)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100420{
Paolo Bonzinie9fd7612020-05-13 13:28:23 -0400421 struct vcpu_svm *svm = to_svm(vcpu);
422
423 return is_guest_mode(vcpu) && (svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100424}
425
Paolo Bonzini55714cd2020-04-23 08:17:28 -0400426static inline bool nested_exit_on_smi(struct vcpu_svm *svm)
427{
Babu Mogerc62e2e92020-09-11 14:28:28 -0500428 return vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_SMI);
Paolo Bonzini55714cd2020-04-23 08:17:28 -0400429}
430
Paolo Bonzinifc6f7c02020-04-23 18:02:45 -0400431static inline bool nested_exit_on_intr(struct vcpu_svm *svm)
432{
Babu Mogerc62e2e92020-09-11 14:28:28 -0500433 return vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_INTR);
Paolo Bonzinifc6f7c02020-04-23 18:02:45 -0400434}
435
Paolo Bonzinibbdad0b2020-04-23 08:06:43 -0400436static inline bool nested_exit_on_nmi(struct vcpu_svm *svm)
437{
Babu Mogerc62e2e92020-09-11 14:28:28 -0500438 return vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_NMI);
Paolo Bonzinibbdad0b2020-04-23 08:06:43 -0400439}
440
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200441int enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa,
442 struct vmcb *nested_vmcb);
Paolo Bonzinic513f482020-05-18 13:08:37 -0400443void svm_leave_nested(struct vcpu_svm *svm);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300444void svm_free_nested(struct vcpu_svm *svm);
445int svm_allocate_nested(struct vcpu_svm *svm);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100446int nested_svm_vmrun(struct vcpu_svm *svm);
447void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb);
448int nested_svm_vmexit(struct vcpu_svm *svm);
449int nested_svm_exit_handled(struct vcpu_svm *svm);
450int nested_svm_check_permissions(struct vcpu_svm *svm);
451int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
452 bool has_error_code, u32 error_code);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100453int nested_svm_exit_special(struct vcpu_svm *svm);
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400454void sync_nested_vmcb_control(struct vcpu_svm *svm);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100455
Paolo Bonzini33b22172020-04-17 10:24:18 -0400456extern struct kvm_x86_nested_ops svm_nested_ops;
457
Joerg Roedelef0f6492020-03-31 12:17:38 -0400458/* avic.c */
459
460#define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFF)
461#define AVIC_LOGICAL_ID_ENTRY_VALID_BIT 31
462#define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31)
463
464#define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK (0xFFULL)
465#define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK (0xFFFFFFFFFFULL << 12)
466#define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK (1ULL << 62)
467#define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK (1ULL << 63)
468
469#define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL
470
471extern int avic;
472
473static inline void avic_update_vapic_bar(struct vcpu_svm *svm, u64 data)
474{
475 svm->vmcb->control.avic_vapic_bar = data & VMCB_AVIC_APIC_BAR_MASK;
Joerg Roedel06e78522020-06-25 10:03:23 +0200476 vmcb_mark_dirty(svm->vmcb, VMCB_AVIC);
Joerg Roedelef0f6492020-03-31 12:17:38 -0400477}
478
479static inline bool avic_vcpu_is_running(struct kvm_vcpu *vcpu)
480{
481 struct vcpu_svm *svm = to_svm(vcpu);
482 u64 *entry = svm->avic_physical_id_cache;
483
484 if (!entry)
485 return false;
486
487 return (READ_ONCE(*entry) & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
488}
489
490int avic_ga_log_notifier(u32 ga_tag);
491void avic_vm_destroy(struct kvm *kvm);
492int avic_vm_init(struct kvm *kvm);
493void avic_init_vmcb(struct vcpu_svm *svm);
494void svm_toggle_avic_for_irq_window(struct kvm_vcpu *vcpu, bool activate);
495int avic_incomplete_ipi_interception(struct vcpu_svm *svm);
496int avic_unaccelerated_access_interception(struct vcpu_svm *svm);
497int avic_init_vcpu(struct vcpu_svm *svm);
498void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
499void avic_vcpu_put(struct kvm_vcpu *vcpu);
500void avic_post_state_restore(struct kvm_vcpu *vcpu);
501void svm_set_virtual_apic_mode(struct kvm_vcpu *vcpu);
502void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu);
503bool svm_check_apicv_inhibit_reasons(ulong bit);
504void svm_pre_update_apicv_exec_ctrl(struct kvm *kvm, bool activate);
505void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap);
506void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr);
507void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr);
508int svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec);
509bool svm_dy_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu);
510int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
511 uint32_t guest_irq, bool set);
512void svm_vcpu_blocking(struct kvm_vcpu *vcpu);
513void svm_vcpu_unblocking(struct kvm_vcpu *vcpu);
514
Joerg Roedeleaf78262020-03-24 10:41:54 +0100515/* sev.c */
516
Tom Lendacky1edc1452020-12-10 11:09:49 -0600517#define GHCB_VERSION_MAX 1ULL
518#define GHCB_VERSION_MIN 1ULL
519
Tom Lendacky291bd202020-12-10 11:09:47 -0600520#define GHCB_MSR_INFO_POS 0
521#define GHCB_MSR_INFO_MASK (BIT_ULL(12) - 1)
522
Tom Lendacky1edc1452020-12-10 11:09:49 -0600523#define GHCB_MSR_SEV_INFO_RESP 0x001
524#define GHCB_MSR_SEV_INFO_REQ 0x002
525#define GHCB_MSR_VER_MAX_POS 48
526#define GHCB_MSR_VER_MAX_MASK 0xffff
527#define GHCB_MSR_VER_MIN_POS 32
528#define GHCB_MSR_VER_MIN_MASK 0xffff
529#define GHCB_MSR_CBIT_POS 24
530#define GHCB_MSR_CBIT_MASK 0xff
531#define GHCB_MSR_SEV_INFO(_max, _min, _cbit) \
532 ((((_max) & GHCB_MSR_VER_MAX_MASK) << GHCB_MSR_VER_MAX_POS) | \
533 (((_min) & GHCB_MSR_VER_MIN_MASK) << GHCB_MSR_VER_MIN_POS) | \
534 (((_cbit) & GHCB_MSR_CBIT_MASK) << GHCB_MSR_CBIT_POS) | \
535 GHCB_MSR_SEV_INFO_RESP)
536
Tom Lendackyd3694662020-12-10 11:09:50 -0600537#define GHCB_MSR_CPUID_REQ 0x004
538#define GHCB_MSR_CPUID_RESP 0x005
539#define GHCB_MSR_CPUID_FUNC_POS 32
540#define GHCB_MSR_CPUID_FUNC_MASK 0xffffffff
541#define GHCB_MSR_CPUID_VALUE_POS 32
542#define GHCB_MSR_CPUID_VALUE_MASK 0xffffffff
543#define GHCB_MSR_CPUID_REG_POS 30
544#define GHCB_MSR_CPUID_REG_MASK 0x3
545
Tom Lendackye1d71112020-12-10 11:09:51 -0600546#define GHCB_MSR_TERM_REQ 0x100
547#define GHCB_MSR_TERM_REASON_SET_POS 12
548#define GHCB_MSR_TERM_REASON_SET_MASK 0xf
549#define GHCB_MSR_TERM_REASON_POS 16
550#define GHCB_MSR_TERM_REASON_MASK 0xff
551
Joerg Roedeleaf78262020-03-24 10:41:54 +0100552extern unsigned int max_sev_asid;
553
Joerg Roedeleaf78262020-03-24 10:41:54 +0100554static inline bool svm_sev_enabled(void)
555{
556 return IS_ENABLED(CONFIG_KVM_AMD_SEV) ? max_sev_asid : 0;
557}
558
559void sev_vm_destroy(struct kvm *kvm);
560int svm_mem_enc_op(struct kvm *kvm, void __user *argp);
561int svm_register_enc_region(struct kvm *kvm,
562 struct kvm_enc_region *range);
563int svm_unregister_enc_region(struct kvm *kvm,
564 struct kvm_enc_region *range);
565void pre_sev_run(struct vcpu_svm *svm, int cpu);
Tom Lendacky916391a2020-12-10 11:09:38 -0600566void __init sev_hardware_setup(void);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100567void sev_hardware_teardown(void);
Tom Lendackyadd5e2f2020-12-10 11:09:40 -0600568void sev_free_vcpu(struct kvm_vcpu *vcpu);
Tom Lendacky291bd202020-12-10 11:09:47 -0600569int sev_handle_vmgexit(struct vcpu_svm *svm);
Tom Lendacky7ed9abf2020-12-10 11:09:54 -0600570int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in);
Tom Lendacky376c6d22020-12-10 11:10:06 -0600571void sev_es_init_vmcb(struct vcpu_svm *svm);
572void sev_es_create_vcpu(struct vcpu_svm *svm);
Tom Lendacky647daca2021-01-04 14:20:01 -0600573void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector);
Michael Rotha7fc06d2021-02-02 13:01:26 -0600574void sev_es_prepare_guest_switch(struct vcpu_svm *svm, unsigned int cpu);
Joerg Roedeleaf78262020-03-24 10:41:54 +0100575
Tom Lendacky16809ec2020-12-10 11:10:08 -0600576/* vmenter.S */
577
578void __svm_sev_es_vcpu_run(unsigned long vmcb_pa);
579void __svm_vcpu_run(unsigned long vmcb_pa, unsigned long *regs);
580
Joerg Roedel883b0a92020-03-24 10:41:52 +0100581#endif