KVM: x86: allow kvm_x86_ops.set_efer to return an error value
[linux-block.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 MAX_DIRECT_ACCESS_MSRS  15
35 #define MSRPM_OFFSETS   16
36 extern u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
37 extern bool npt_enabled;
38
39 enum {
40         VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
41                             pause filter count */
42         VMCB_PERM_MAP,   /* IOPM Base and MSRPM Base */
43         VMCB_ASID,       /* ASID */
44         VMCB_INTR,       /* int_ctl, int_vector */
45         VMCB_NPT,        /* npt_en, nCR3, gPAT */
46         VMCB_CR,         /* CR0, CR3, CR4, EFER */
47         VMCB_DR,         /* DR6, DR7 */
48         VMCB_DT,         /* GDT, IDT */
49         VMCB_SEG,        /* CS, DS, SS, ES, CPL */
50         VMCB_CR2,        /* CR2 only */
51         VMCB_LBR,        /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
52         VMCB_AVIC,       /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE,
53                           * AVIC PHYSICAL_TABLE pointer,
54                           * AVIC LOGICAL_TABLE pointer
55                           */
56         VMCB_DIRTY_MAX,
57 };
58
59 /* TPR and CR2 are always written before VMRUN */
60 #define VMCB_ALWAYS_DIRTY_MASK  ((1U << VMCB_INTR) | (1U << VMCB_CR2))
61
62 struct kvm_sev_info {
63         bool active;            /* SEV enabled guest */
64         unsigned int asid;      /* ASID used for this guest */
65         unsigned int handle;    /* SEV firmware handle */
66         int fd;                 /* SEV device fd */
67         unsigned long pages_locked; /* Number of pages locked */
68         struct list_head regions_list;  /* List of registered regions */
69 };
70
71 struct 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
83 struct kvm_vcpu;
84
85 struct svm_nested_state {
86         struct vmcb *hsave;
87         u64 hsave_msr;
88         u64 vm_cr_msr;
89         u64 vmcb12_gpa;
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 svm_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         /* Save desired MSR intercept (read: pass-through) state */
163         struct {
164                 DECLARE_BITMAP(read, MAX_DIRECT_ACCESS_MSRS);
165                 DECLARE_BITMAP(write, MAX_DIRECT_ACCESS_MSRS);
166         } shadow_msr_intercept;
167 };
168
169 struct svm_cpu_data {
170         int cpu;
171
172         u64 asid_generation;
173         u32 max_asid;
174         u32 next_asid;
175         u32 min_asid;
176         struct kvm_ldttss_desc *tss_desc;
177
178         struct page *save_area;
179         struct vmcb *current_vmcb;
180
181         /* index = sev_asid, value = vmcb pointer */
182         struct vmcb **sev_vmcbs;
183 };
184
185 DECLARE_PER_CPU(struct svm_cpu_data *, svm_data);
186
187 void recalc_intercepts(struct vcpu_svm *svm);
188
189 static inline struct kvm_svm *to_kvm_svm(struct kvm *kvm)
190 {
191         return container_of(kvm, struct kvm_svm, kvm);
192 }
193
194 static inline void vmcb_mark_all_dirty(struct vmcb *vmcb)
195 {
196         vmcb->control.clean = 0;
197 }
198
199 static inline void vmcb_mark_all_clean(struct vmcb *vmcb)
200 {
201         vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
202                                & ~VMCB_ALWAYS_DIRTY_MASK;
203 }
204
205 static inline void vmcb_mark_dirty(struct vmcb *vmcb, int bit)
206 {
207         vmcb->control.clean &= ~(1 << bit);
208 }
209
210 static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
211 {
212         return container_of(vcpu, struct vcpu_svm, vcpu);
213 }
214
215 static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
216 {
217         if (is_guest_mode(&svm->vcpu))
218                 return svm->nested.hsave;
219         else
220                 return svm->vmcb;
221 }
222
223 static inline void vmcb_set_intercept(struct vmcb_control_area *control, u32 bit)
224 {
225         WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
226         __set_bit(bit, (unsigned long *)&control->intercepts);
227 }
228
229 static inline void vmcb_clr_intercept(struct vmcb_control_area *control, u32 bit)
230 {
231         WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
232         __clear_bit(bit, (unsigned long *)&control->intercepts);
233 }
234
235 static inline bool vmcb_is_intercept(struct vmcb_control_area *control, u32 bit)
236 {
237         WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
238         return test_bit(bit, (unsigned long *)&control->intercepts);
239 }
240
241 static inline void set_dr_intercepts(struct vcpu_svm *svm)
242 {
243         struct vmcb *vmcb = get_host_vmcb(svm);
244
245         vmcb_set_intercept(&vmcb->control, INTERCEPT_DR0_READ);
246         vmcb_set_intercept(&vmcb->control, INTERCEPT_DR1_READ);
247         vmcb_set_intercept(&vmcb->control, INTERCEPT_DR2_READ);
248         vmcb_set_intercept(&vmcb->control, INTERCEPT_DR3_READ);
249         vmcb_set_intercept(&vmcb->control, INTERCEPT_DR4_READ);
250         vmcb_set_intercept(&vmcb->control, INTERCEPT_DR5_READ);
251         vmcb_set_intercept(&vmcb->control, INTERCEPT_DR6_READ);
252         vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_READ);
253         vmcb_set_intercept(&vmcb->control, INTERCEPT_DR0_WRITE);
254         vmcb_set_intercept(&vmcb->control, INTERCEPT_DR1_WRITE);
255         vmcb_set_intercept(&vmcb->control, INTERCEPT_DR2_WRITE);
256         vmcb_set_intercept(&vmcb->control, INTERCEPT_DR3_WRITE);
257         vmcb_set_intercept(&vmcb->control, INTERCEPT_DR4_WRITE);
258         vmcb_set_intercept(&vmcb->control, INTERCEPT_DR5_WRITE);
259         vmcb_set_intercept(&vmcb->control, INTERCEPT_DR6_WRITE);
260         vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_WRITE);
261
262         recalc_intercepts(svm);
263 }
264
265 static inline void clr_dr_intercepts(struct vcpu_svm *svm)
266 {
267         struct vmcb *vmcb = get_host_vmcb(svm);
268
269         vmcb->control.intercepts[INTERCEPT_DR] = 0;
270
271         recalc_intercepts(svm);
272 }
273
274 static inline void set_exception_intercept(struct vcpu_svm *svm, u32 bit)
275 {
276         struct vmcb *vmcb = get_host_vmcb(svm);
277
278         WARN_ON_ONCE(bit >= 32);
279         vmcb_set_intercept(&vmcb->control, INTERCEPT_EXCEPTION_OFFSET + bit);
280
281         recalc_intercepts(svm);
282 }
283
284 static inline void clr_exception_intercept(struct vcpu_svm *svm, u32 bit)
285 {
286         struct vmcb *vmcb = get_host_vmcb(svm);
287
288         WARN_ON_ONCE(bit >= 32);
289         vmcb_clr_intercept(&vmcb->control, INTERCEPT_EXCEPTION_OFFSET + bit);
290
291         recalc_intercepts(svm);
292 }
293
294 static inline void svm_set_intercept(struct vcpu_svm *svm, int bit)
295 {
296         struct vmcb *vmcb = get_host_vmcb(svm);
297
298         vmcb_set_intercept(&vmcb->control, bit);
299
300         recalc_intercepts(svm);
301 }
302
303 static inline void svm_clr_intercept(struct vcpu_svm *svm, int bit)
304 {
305         struct vmcb *vmcb = get_host_vmcb(svm);
306
307         vmcb_clr_intercept(&vmcb->control, bit);
308
309         recalc_intercepts(svm);
310 }
311
312 static inline bool svm_is_intercept(struct vcpu_svm *svm, int bit)
313 {
314         return vmcb_is_intercept(&svm->vmcb->control, bit);
315 }
316
317 static inline bool vgif_enabled(struct vcpu_svm *svm)
318 {
319         return !!(svm->vmcb->control.int_ctl & V_GIF_ENABLE_MASK);
320 }
321
322 static inline void enable_gif(struct vcpu_svm *svm)
323 {
324         if (vgif_enabled(svm))
325                 svm->vmcb->control.int_ctl |= V_GIF_MASK;
326         else
327                 svm->vcpu.arch.hflags |= HF_GIF_MASK;
328 }
329
330 static inline void disable_gif(struct vcpu_svm *svm)
331 {
332         if (vgif_enabled(svm))
333                 svm->vmcb->control.int_ctl &= ~V_GIF_MASK;
334         else
335                 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
336 }
337
338 static inline bool gif_set(struct vcpu_svm *svm)
339 {
340         if (vgif_enabled(svm))
341                 return !!(svm->vmcb->control.int_ctl & V_GIF_MASK);
342         else
343                 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
344 }
345
346 /* svm.c */
347 #define MSR_CR3_LEGACY_RESERVED_MASK            0xfe7U
348 #define MSR_CR3_LEGACY_PAE_RESERVED_MASK        0x7U
349 #define MSR_CR3_LONG_MBZ_MASK                   0xfff0000000000000U
350 #define MSR_INVALID                             0xffffffffU
351
352 u32 svm_msrpm_offset(u32 msr);
353 int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer);
354 void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
355 int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
356 void svm_flush_tlb(struct kvm_vcpu *vcpu);
357 void disable_nmi_singlestep(struct vcpu_svm *svm);
358 bool svm_smi_blocked(struct kvm_vcpu *vcpu);
359 bool svm_nmi_blocked(struct kvm_vcpu *vcpu);
360 bool svm_interrupt_blocked(struct kvm_vcpu *vcpu);
361 void svm_set_gif(struct vcpu_svm *svm, bool value);
362
363 /* nested.c */
364
365 #define NESTED_EXIT_HOST        0       /* Exit handled on host level */
366 #define NESTED_EXIT_DONE        1       /* Exit caused nested vmexit  */
367 #define NESTED_EXIT_CONTINUE    2       /* Further checks needed      */
368
369 static inline bool nested_svm_virtualize_tpr(struct kvm_vcpu *vcpu)
370 {
371         struct vcpu_svm *svm = to_svm(vcpu);
372
373         return is_guest_mode(vcpu) && (svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK);
374 }
375
376 static inline bool nested_exit_on_smi(struct vcpu_svm *svm)
377 {
378         return vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_SMI);
379 }
380
381 static inline bool nested_exit_on_intr(struct vcpu_svm *svm)
382 {
383         return vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_INTR);
384 }
385
386 static inline bool nested_exit_on_nmi(struct vcpu_svm *svm)
387 {
388         return vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_NMI);
389 }
390
391 int enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa,
392                          struct vmcb *nested_vmcb);
393 void svm_leave_nested(struct vcpu_svm *svm);
394 int nested_svm_vmrun(struct vcpu_svm *svm);
395 void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb);
396 int nested_svm_vmexit(struct vcpu_svm *svm);
397 int nested_svm_exit_handled(struct vcpu_svm *svm);
398 int nested_svm_check_permissions(struct vcpu_svm *svm);
399 int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
400                                bool has_error_code, u32 error_code);
401 int nested_svm_exit_special(struct vcpu_svm *svm);
402 void sync_nested_vmcb_control(struct vcpu_svm *svm);
403
404 extern struct kvm_x86_nested_ops svm_nested_ops;
405
406 /* avic.c */
407
408 #define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK    (0xFF)
409 #define AVIC_LOGICAL_ID_ENTRY_VALID_BIT                 31
410 #define AVIC_LOGICAL_ID_ENTRY_VALID_MASK                (1 << 31)
411
412 #define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK    (0xFFULL)
413 #define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK        (0xFFFFFFFFFFULL << 12)
414 #define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK          (1ULL << 62)
415 #define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK               (1ULL << 63)
416
417 #define VMCB_AVIC_APIC_BAR_MASK         0xFFFFFFFFFF000ULL
418
419 extern int avic;
420
421 static inline void avic_update_vapic_bar(struct vcpu_svm *svm, u64 data)
422 {
423         svm->vmcb->control.avic_vapic_bar = data & VMCB_AVIC_APIC_BAR_MASK;
424         vmcb_mark_dirty(svm->vmcb, VMCB_AVIC);
425 }
426
427 static inline bool avic_vcpu_is_running(struct kvm_vcpu *vcpu)
428 {
429         struct vcpu_svm *svm = to_svm(vcpu);
430         u64 *entry = svm->avic_physical_id_cache;
431
432         if (!entry)
433                 return false;
434
435         return (READ_ONCE(*entry) & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
436 }
437
438 int avic_ga_log_notifier(u32 ga_tag);
439 void avic_vm_destroy(struct kvm *kvm);
440 int avic_vm_init(struct kvm *kvm);
441 void avic_init_vmcb(struct vcpu_svm *svm);
442 void svm_toggle_avic_for_irq_window(struct kvm_vcpu *vcpu, bool activate);
443 int avic_incomplete_ipi_interception(struct vcpu_svm *svm);
444 int avic_unaccelerated_access_interception(struct vcpu_svm *svm);
445 int avic_init_vcpu(struct vcpu_svm *svm);
446 void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
447 void avic_vcpu_put(struct kvm_vcpu *vcpu);
448 void avic_post_state_restore(struct kvm_vcpu *vcpu);
449 void svm_set_virtual_apic_mode(struct kvm_vcpu *vcpu);
450 void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu);
451 bool svm_check_apicv_inhibit_reasons(ulong bit);
452 void svm_pre_update_apicv_exec_ctrl(struct kvm *kvm, bool activate);
453 void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap);
454 void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr);
455 void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr);
456 int svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec);
457 bool svm_dy_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu);
458 int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
459                        uint32_t guest_irq, bool set);
460 void svm_vcpu_blocking(struct kvm_vcpu *vcpu);
461 void svm_vcpu_unblocking(struct kvm_vcpu *vcpu);
462
463 /* sev.c */
464
465 extern unsigned int max_sev_asid;
466
467 static inline bool sev_guest(struct kvm *kvm)
468 {
469 #ifdef CONFIG_KVM_AMD_SEV
470         struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
471
472         return sev->active;
473 #else
474         return false;
475 #endif
476 }
477
478 static inline bool svm_sev_enabled(void)
479 {
480         return IS_ENABLED(CONFIG_KVM_AMD_SEV) ? max_sev_asid : 0;
481 }
482
483 void sev_vm_destroy(struct kvm *kvm);
484 int svm_mem_enc_op(struct kvm *kvm, void __user *argp);
485 int svm_register_enc_region(struct kvm *kvm,
486                             struct kvm_enc_region *range);
487 int svm_unregister_enc_region(struct kvm *kvm,
488                               struct kvm_enc_region *range);
489 void pre_sev_run(struct vcpu_svm *svm, int cpu);
490 int __init sev_hardware_setup(void);
491 void sev_hardware_teardown(void);
492
493 #endif