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