KVM: SVM: Move Nested SVM Implementation to nested.c
[linux-2.6-microblaze.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         /* cache for intercepts of the guest */
101         u32 intercept_cr;
102         u32 intercept_dr;
103         u32 intercept_exceptions;
104         u64 intercept;
105
106         /* Nested Paging related state */
107         u64 nested_cr3;
108 };
109
110 struct vcpu_svm {
111         struct kvm_vcpu vcpu;
112         struct vmcb *vmcb;
113         unsigned long vmcb_pa;
114         struct svm_cpu_data *svm_data;
115         uint64_t asid_generation;
116         uint64_t sysenter_esp;
117         uint64_t sysenter_eip;
118         uint64_t tsc_aux;
119
120         u64 msr_decfg;
121
122         u64 next_rip;
123
124         u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
125         struct {
126                 u16 fs;
127                 u16 gs;
128                 u16 ldt;
129                 u64 gs_base;
130         } host;
131
132         u64 spec_ctrl;
133         /*
134          * Contains guest-controlled bits of VIRT_SPEC_CTRL, which will be
135          * translated into the appropriate L2_CFG bits on the host to
136          * perform speculative control.
137          */
138         u64 virt_spec_ctrl;
139
140         u32 *msrpm;
141
142         ulong nmi_iret_rip;
143
144         struct nested_state nested;
145
146         bool nmi_singlestep;
147         u64 nmi_singlestep_guest_rflags;
148
149         unsigned int3_injected;
150         unsigned long int3_rip;
151
152         /* cached guest cpuid flags for faster access */
153         bool nrips_enabled      : 1;
154
155         u32 ldr_reg;
156         u32 dfr_reg;
157         struct page *avic_backing_page;
158         u64 *avic_physical_id_cache;
159         bool avic_is_running;
160
161         /*
162          * Per-vcpu list of struct amd_svm_iommu_ir:
163          * This is used mainly to store interrupt remapping information used
164          * when update the vcpu affinity. This avoids the need to scan for
165          * IRTE and try to match ga_tag in the IOMMU driver.
166          */
167         struct list_head ir_list;
168         spinlock_t ir_list_lock;
169
170         /* which host CPU was used for running this vcpu */
171         unsigned int last_cpu;
172 };
173
174 void recalc_intercepts(struct vcpu_svm *svm);
175
176 static inline void mark_all_dirty(struct vmcb *vmcb)
177 {
178         vmcb->control.clean = 0;
179 }
180
181 static inline void mark_all_clean(struct vmcb *vmcb)
182 {
183         vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
184                                & ~VMCB_ALWAYS_DIRTY_MASK;
185 }
186
187 static inline void mark_dirty(struct vmcb *vmcb, int bit)
188 {
189         vmcb->control.clean &= ~(1 << bit);
190 }
191
192 static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
193 {
194         return container_of(vcpu, struct vcpu_svm, vcpu);
195 }
196
197 static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
198 {
199         if (is_guest_mode(&svm->vcpu))
200                 return svm->nested.hsave;
201         else
202                 return svm->vmcb;
203 }
204
205 static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
206 {
207         struct vmcb *vmcb = get_host_vmcb(svm);
208
209         vmcb->control.intercept_cr |= (1U << bit);
210
211         recalc_intercepts(svm);
212 }
213
214 static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
215 {
216         struct vmcb *vmcb = get_host_vmcb(svm);
217
218         vmcb->control.intercept_cr &= ~(1U << bit);
219
220         recalc_intercepts(svm);
221 }
222
223 static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
224 {
225         struct vmcb *vmcb = get_host_vmcb(svm);
226
227         return vmcb->control.intercept_cr & (1U << bit);
228 }
229
230 static inline void set_dr_intercepts(struct vcpu_svm *svm)
231 {
232         struct vmcb *vmcb = get_host_vmcb(svm);
233
234         vmcb->control.intercept_dr = (1 << INTERCEPT_DR0_READ)
235                 | (1 << INTERCEPT_DR1_READ)
236                 | (1 << INTERCEPT_DR2_READ)
237                 | (1 << INTERCEPT_DR3_READ)
238                 | (1 << INTERCEPT_DR4_READ)
239                 | (1 << INTERCEPT_DR5_READ)
240                 | (1 << INTERCEPT_DR6_READ)
241                 | (1 << INTERCEPT_DR7_READ)
242                 | (1 << INTERCEPT_DR0_WRITE)
243                 | (1 << INTERCEPT_DR1_WRITE)
244                 | (1 << INTERCEPT_DR2_WRITE)
245                 | (1 << INTERCEPT_DR3_WRITE)
246                 | (1 << INTERCEPT_DR4_WRITE)
247                 | (1 << INTERCEPT_DR5_WRITE)
248                 | (1 << INTERCEPT_DR6_WRITE)
249                 | (1 << INTERCEPT_DR7_WRITE);
250
251         recalc_intercepts(svm);
252 }
253
254 static inline void clr_dr_intercepts(struct vcpu_svm *svm)
255 {
256         struct vmcb *vmcb = get_host_vmcb(svm);
257
258         vmcb->control.intercept_dr = 0;
259
260         recalc_intercepts(svm);
261 }
262
263 static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
264 {
265         struct vmcb *vmcb = get_host_vmcb(svm);
266
267         vmcb->control.intercept_exceptions |= (1U << bit);
268
269         recalc_intercepts(svm);
270 }
271
272 static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
273 {
274         struct vmcb *vmcb = get_host_vmcb(svm);
275
276         vmcb->control.intercept_exceptions &= ~(1U << bit);
277
278         recalc_intercepts(svm);
279 }
280
281 static inline void set_intercept(struct vcpu_svm *svm, int bit)
282 {
283         struct vmcb *vmcb = get_host_vmcb(svm);
284
285         vmcb->control.intercept |= (1ULL << bit);
286
287         recalc_intercepts(svm);
288 }
289
290 static inline void clr_intercept(struct vcpu_svm *svm, int bit)
291 {
292         struct vmcb *vmcb = get_host_vmcb(svm);
293
294         vmcb->control.intercept &= ~(1ULL << bit);
295
296         recalc_intercepts(svm);
297 }
298
299 static inline bool is_intercept(struct vcpu_svm *svm, int bit)
300 {
301         return (svm->vmcb->control.intercept & (1ULL << bit)) != 0;
302 }
303
304 static inline bool vgif_enabled(struct vcpu_svm *svm)
305 {
306         return !!(svm->vmcb->control.int_ctl & V_GIF_ENABLE_MASK);
307 }
308
309 static inline void enable_gif(struct vcpu_svm *svm)
310 {
311         if (vgif_enabled(svm))
312                 svm->vmcb->control.int_ctl |= V_GIF_MASK;
313         else
314                 svm->vcpu.arch.hflags |= HF_GIF_MASK;
315 }
316
317 static inline void disable_gif(struct vcpu_svm *svm)
318 {
319         if (vgif_enabled(svm))
320                 svm->vmcb->control.int_ctl &= ~V_GIF_MASK;
321         else
322                 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
323 }
324
325 static inline bool gif_set(struct vcpu_svm *svm)
326 {
327         if (vgif_enabled(svm))
328                 return !!(svm->vmcb->control.int_ctl & V_GIF_MASK);
329         else
330                 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
331 }
332
333 /* svm.c */
334 #define MSR_INVALID                     0xffffffffU
335
336 u32 svm_msrpm_offset(u32 msr);
337 void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer);
338 void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
339 int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
340 void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa);
341 void disable_nmi_singlestep(struct vcpu_svm *svm);
342
343 /* nested.c */
344
345 #define NESTED_EXIT_HOST        0       /* Exit handled on host level */
346 #define NESTED_EXIT_DONE        1       /* Exit caused nested vmexit  */
347 #define NESTED_EXIT_CONTINUE    2       /* Further checks needed      */
348
349 /* This function returns true if it is save to enable the nmi window */
350 static inline bool nested_svm_nmi(struct vcpu_svm *svm)
351 {
352         if (!is_guest_mode(&svm->vcpu))
353                 return true;
354
355         if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
356                 return true;
357
358         svm->vmcb->control.exit_code = SVM_EXIT_NMI;
359         svm->nested.exit_required = true;
360
361         return false;
362 }
363
364 static inline bool svm_nested_virtualize_tpr(struct kvm_vcpu *vcpu)
365 {
366         return is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK);
367 }
368
369 void enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa,
370                           struct vmcb *nested_vmcb, struct kvm_host_map *map);
371 int nested_svm_vmrun(struct vcpu_svm *svm);
372 void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb);
373 int nested_svm_vmexit(struct vcpu_svm *svm);
374 int nested_svm_exit_handled(struct vcpu_svm *svm);
375 int nested_svm_check_permissions(struct vcpu_svm *svm);
376 int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
377                                bool has_error_code, u32 error_code);
378 int svm_check_nested_events(struct kvm_vcpu *vcpu);
379 int nested_svm_exit_special(struct vcpu_svm *svm);
380
381 #endif