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