KVM: vmx/pmu: Add PMU_CAP_LBR_FMT check when guest LBR is enabled
[linux-2.6-microblaze.git] / arch / x86 / kvm / vmx / vmx.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __KVM_X86_VMX_H
3 #define __KVM_X86_VMX_H
4
5 #include <linux/kvm_host.h>
6
7 #include <asm/kvm.h>
8 #include <asm/intel_pt.h>
9
10 #include "capabilities.h"
11 #include "kvm_cache_regs.h"
12 #include "posted_intr.h"
13 #include "vmcs.h"
14 #include "vmx_ops.h"
15 #include "cpuid.h"
16
17 extern const u32 vmx_msr_index[];
18
19 #define MSR_TYPE_R      1
20 #define MSR_TYPE_W      2
21 #define MSR_TYPE_RW     3
22
23 #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
24
25 #ifdef CONFIG_X86_64
26 #define MAX_NR_USER_RETURN_MSRS 7
27 #else
28 #define MAX_NR_USER_RETURN_MSRS 4
29 #endif
30
31 #define MAX_NR_LOADSTORE_MSRS   8
32
33 struct vmx_msrs {
34         unsigned int            nr;
35         struct vmx_msr_entry    val[MAX_NR_LOADSTORE_MSRS];
36 };
37
38 struct vmx_uret_msr {
39         unsigned int slot; /* The MSR's slot in kvm_user_return_msrs. */
40         u64 data;
41         u64 mask;
42 };
43
44 enum segment_cache_field {
45         SEG_FIELD_SEL = 0,
46         SEG_FIELD_BASE = 1,
47         SEG_FIELD_LIMIT = 2,
48         SEG_FIELD_AR = 3,
49
50         SEG_FIELD_NR = 4
51 };
52
53 #define RTIT_ADDR_RANGE         4
54
55 struct pt_ctx {
56         u64 ctl;
57         u64 status;
58         u64 output_base;
59         u64 output_mask;
60         u64 cr3_match;
61         u64 addr_a[RTIT_ADDR_RANGE];
62         u64 addr_b[RTIT_ADDR_RANGE];
63 };
64
65 struct pt_desc {
66         u64 ctl_bitmask;
67         u32 addr_range;
68         u32 caps[PT_CPUID_REGS_NUM * PT_CPUID_LEAVES];
69         struct pt_ctx host;
70         struct pt_ctx guest;
71 };
72
73 union vmx_exit_reason {
74         struct {
75                 u32     basic                   : 16;
76                 u32     reserved16              : 1;
77                 u32     reserved17              : 1;
78                 u32     reserved18              : 1;
79                 u32     reserved19              : 1;
80                 u32     reserved20              : 1;
81                 u32     reserved21              : 1;
82                 u32     reserved22              : 1;
83                 u32     reserved23              : 1;
84                 u32     reserved24              : 1;
85                 u32     reserved25              : 1;
86                 u32     bus_lock_detected       : 1;
87                 u32     enclave_mode            : 1;
88                 u32     smi_pending_mtf         : 1;
89                 u32     smi_from_vmx_root       : 1;
90                 u32     reserved30              : 1;
91                 u32     failed_vmentry          : 1;
92         };
93         u32 full;
94 };
95
96 #define vcpu_to_lbr_desc(vcpu) (&to_vmx(vcpu)->lbr_desc)
97 #define vcpu_to_lbr_records(vcpu) (&to_vmx(vcpu)->lbr_desc.records)
98
99 bool intel_pmu_lbr_is_compatible(struct kvm_vcpu *vcpu);
100
101 struct lbr_desc {
102         /* Basic info about guest LBR records. */
103         struct x86_pmu_lbr records;
104 };
105
106 /*
107  * The nested_vmx structure is part of vcpu_vmx, and holds information we need
108  * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
109  */
110 struct nested_vmx {
111         /* Has the level1 guest done vmxon? */
112         bool vmxon;
113         gpa_t vmxon_ptr;
114         bool pml_full;
115
116         /* The guest-physical address of the current VMCS L1 keeps for L2 */
117         gpa_t current_vmptr;
118         /*
119          * Cache of the guest's VMCS, existing outside of guest memory.
120          * Loaded from guest memory during VMPTRLD. Flushed to guest
121          * memory during VMCLEAR and VMPTRLD.
122          */
123         struct vmcs12 *cached_vmcs12;
124         /*
125          * Cache of the guest's shadow VMCS, existing outside of guest
126          * memory. Loaded from guest memory during VM entry. Flushed
127          * to guest memory during VM exit.
128          */
129         struct vmcs12 *cached_shadow_vmcs12;
130
131         /*
132          * Indicates if the shadow vmcs or enlightened vmcs must be updated
133          * with the data held by struct vmcs12.
134          */
135         bool need_vmcs12_to_shadow_sync;
136         bool dirty_vmcs12;
137
138         /*
139          * Indicates lazily loaded guest state has not yet been decached from
140          * vmcs02.
141          */
142         bool need_sync_vmcs02_to_vmcs12_rare;
143
144         /*
145          * vmcs02 has been initialized, i.e. state that is constant for
146          * vmcs02 has been written to the backing VMCS.  Initialization
147          * is delayed until L1 actually attempts to run a nested VM.
148          */
149         bool vmcs02_initialized;
150
151         bool change_vmcs01_virtual_apic_mode;
152         bool reload_vmcs01_apic_access_page;
153
154         /*
155          * Enlightened VMCS has been enabled. It does not mean that L1 has to
156          * use it. However, VMX features available to L1 will be limited based
157          * on what the enlightened VMCS supports.
158          */
159         bool enlightened_vmcs_enabled;
160
161         /* L2 must run next, and mustn't decide to exit to L1. */
162         bool nested_run_pending;
163
164         /* Pending MTF VM-exit into L1.  */
165         bool mtf_pending;
166
167         struct loaded_vmcs vmcs02;
168
169         /*
170          * Guest pages referred to in the vmcs02 with host-physical
171          * pointers, so we must keep them pinned while L2 runs.
172          */
173         struct page *apic_access_page;
174         struct kvm_host_map virtual_apic_map;
175         struct kvm_host_map pi_desc_map;
176
177         struct kvm_host_map msr_bitmap_map;
178
179         struct pi_desc *pi_desc;
180         bool pi_pending;
181         u16 posted_intr_nv;
182
183         struct hrtimer preemption_timer;
184         u64 preemption_timer_deadline;
185         bool has_preemption_timer_deadline;
186         bool preemption_timer_expired;
187
188         /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
189         u64 vmcs01_debugctl;
190         u64 vmcs01_guest_bndcfgs;
191
192         /* to migrate it to L1 if L2 writes to L1's CR8 directly */
193         int l1_tpr_threshold;
194
195         u16 vpid02;
196         u16 last_vpid;
197
198         struct nested_vmx_msrs msrs;
199
200         /* SMM related state */
201         struct {
202                 /* in VMX operation on SMM entry? */
203                 bool vmxon;
204                 /* in guest mode on SMM entry? */
205                 bool guest_mode;
206         } smm;
207
208         gpa_t hv_evmcs_vmptr;
209         struct kvm_host_map hv_evmcs_map;
210         struct hv_enlightened_vmcs *hv_evmcs;
211 };
212
213 struct vcpu_vmx {
214         struct kvm_vcpu       vcpu;
215         u8                    fail;
216         u8                    msr_bitmap_mode;
217
218         /*
219          * If true, host state has been stored in vmx->loaded_vmcs for
220          * the CPU registers that only need to be switched when transitioning
221          * to/from the kernel, and the registers have been loaded with guest
222          * values.  If false, host state is loaded in the CPU registers
223          * and vmx->loaded_vmcs->host_state is invalid.
224          */
225         bool                  guest_state_loaded;
226
227         unsigned long         exit_qualification;
228         u32                   exit_intr_info;
229         u32                   idt_vectoring_info;
230         ulong                 rflags;
231
232         struct vmx_uret_msr   guest_uret_msrs[MAX_NR_USER_RETURN_MSRS];
233         int                   nr_uret_msrs;
234         int                   nr_active_uret_msrs;
235         bool                  guest_uret_msrs_loaded;
236 #ifdef CONFIG_X86_64
237         u64                   msr_host_kernel_gs_base;
238         u64                   msr_guest_kernel_gs_base;
239 #endif
240
241         u64                   spec_ctrl;
242         u32                   msr_ia32_umwait_control;
243
244         u32 secondary_exec_control;
245
246         /*
247          * loaded_vmcs points to the VMCS currently used in this vcpu. For a
248          * non-nested (L1) guest, it always points to vmcs01. For a nested
249          * guest (L2), it points to a different VMCS.
250          */
251         struct loaded_vmcs    vmcs01;
252         struct loaded_vmcs   *loaded_vmcs;
253
254         struct msr_autoload {
255                 struct vmx_msrs guest;
256                 struct vmx_msrs host;
257         } msr_autoload;
258
259         struct msr_autostore {
260                 struct vmx_msrs guest;
261         } msr_autostore;
262
263         struct {
264                 int vm86_active;
265                 ulong save_rflags;
266                 struct kvm_segment segs[8];
267         } rmode;
268         struct {
269                 u32 bitmask; /* 4 bits per segment (1 bit per field) */
270                 struct kvm_save_segment {
271                         u16 selector;
272                         unsigned long base;
273                         u32 limit;
274                         u32 ar;
275                 } seg[8];
276         } segment_cache;
277         int vpid;
278         bool emulation_required;
279
280         union vmx_exit_reason exit_reason;
281
282         /* Posted interrupt descriptor */
283         struct pi_desc pi_desc;
284
285         /* Support for a guest hypervisor (nested VMX) */
286         struct nested_vmx nested;
287
288         /* Dynamic PLE window. */
289         unsigned int ple_window;
290         bool ple_window_dirty;
291
292         bool req_immediate_exit;
293
294         /* Support for PML */
295 #define PML_ENTITY_NUM          512
296         struct page *pml_pg;
297
298         /* apic deadline value in host tsc */
299         u64 hv_deadline_tsc;
300
301         u64 current_tsc_ratio;
302
303         unsigned long host_debugctlmsr;
304
305         /*
306          * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
307          * msr_ia32_feature_control. FEAT_CTL_LOCKED is always included
308          * in msr_ia32_feature_control_valid_bits.
309          */
310         u64 msr_ia32_feature_control;
311         u64 msr_ia32_feature_control_valid_bits;
312         u64 ept_pointer;
313
314         struct pt_desc pt_desc;
315         struct lbr_desc lbr_desc;
316
317         /* Save desired MSR intercept (read: pass-through) state */
318 #define MAX_POSSIBLE_PASSTHROUGH_MSRS   13
319         struct {
320                 DECLARE_BITMAP(read, MAX_POSSIBLE_PASSTHROUGH_MSRS);
321                 DECLARE_BITMAP(write, MAX_POSSIBLE_PASSTHROUGH_MSRS);
322         } shadow_msr_intercept;
323 };
324
325 enum ept_pointers_status {
326         EPT_POINTERS_CHECK = 0,
327         EPT_POINTERS_MATCH = 1,
328         EPT_POINTERS_MISMATCH = 2
329 };
330
331 struct kvm_vmx {
332         struct kvm kvm;
333
334         unsigned int tss_addr;
335         bool ept_identity_pagetable_done;
336         gpa_t ept_identity_map_addr;
337
338         enum ept_pointers_status ept_pointers_match;
339         spinlock_t ept_pointer_lock;
340 };
341
342 bool nested_vmx_allowed(struct kvm_vcpu *vcpu);
343 void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu,
344                         struct loaded_vmcs *buddy);
345 int allocate_vpid(void);
346 void free_vpid(int vpid);
347 void vmx_set_constant_host_state(struct vcpu_vmx *vmx);
348 void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu);
349 void vmx_set_host_fs_gs(struct vmcs_host_state *host, u16 fs_sel, u16 gs_sel,
350                         unsigned long fs_base, unsigned long gs_base);
351 int vmx_get_cpl(struct kvm_vcpu *vcpu);
352 unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu);
353 void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
354 u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu);
355 void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask);
356 int vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer);
357 void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
358 void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
359 void set_cr4_guest_host_mask(struct vcpu_vmx *vmx);
360 void ept_save_pdptrs(struct kvm_vcpu *vcpu);
361 void vmx_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg);
362 void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg);
363 u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa,
364                    int root_level);
365
366 void update_exception_bitmap(struct kvm_vcpu *vcpu);
367 void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
368 bool vmx_nmi_blocked(struct kvm_vcpu *vcpu);
369 bool vmx_interrupt_blocked(struct kvm_vcpu *vcpu);
370 bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
371 void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
372 void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu);
373 struct vmx_uret_msr *vmx_find_uret_msr(struct vcpu_vmx *vmx, u32 msr);
374 void pt_update_intercept_for_msr(struct kvm_vcpu *vcpu);
375 void vmx_update_host_rsp(struct vcpu_vmx *vmx, unsigned long host_rsp);
376 int vmx_find_loadstore_msr_slot(struct vmx_msrs *m, u32 msr);
377 void vmx_ept_load_pdptrs(struct kvm_vcpu *vcpu);
378 void vmx_set_intercept_for_msr(struct kvm_vcpu *vcpu,
379         u32 msr, int type, bool value);
380
381 static inline u8 vmx_get_rvi(void)
382 {
383         return vmcs_read16(GUEST_INTR_STATUS) & 0xff;
384 }
385
386 #define BUILD_CONTROLS_SHADOW(lname, uname)                                 \
387 static inline void lname##_controls_set(struct vcpu_vmx *vmx, u32 val)      \
388 {                                                                           \
389         if (vmx->loaded_vmcs->controls_shadow.lname != val) {               \
390                 vmcs_write32(uname, val);                                   \
391                 vmx->loaded_vmcs->controls_shadow.lname = val;              \
392         }                                                                   \
393 }                                                                           \
394 static inline u32 lname##_controls_get(struct vcpu_vmx *vmx)                \
395 {                                                                           \
396         return vmx->loaded_vmcs->controls_shadow.lname;                     \
397 }                                                                           \
398 static inline void lname##_controls_setbit(struct vcpu_vmx *vmx, u32 val)   \
399 {                                                                           \
400         lname##_controls_set(vmx, lname##_controls_get(vmx) | val);         \
401 }                                                                           \
402 static inline void lname##_controls_clearbit(struct vcpu_vmx *vmx, u32 val) \
403 {                                                                           \
404         lname##_controls_set(vmx, lname##_controls_get(vmx) & ~val);        \
405 }
406 BUILD_CONTROLS_SHADOW(vm_entry, VM_ENTRY_CONTROLS)
407 BUILD_CONTROLS_SHADOW(vm_exit, VM_EXIT_CONTROLS)
408 BUILD_CONTROLS_SHADOW(pin, PIN_BASED_VM_EXEC_CONTROL)
409 BUILD_CONTROLS_SHADOW(exec, CPU_BASED_VM_EXEC_CONTROL)
410 BUILD_CONTROLS_SHADOW(secondary_exec, SECONDARY_VM_EXEC_CONTROL)
411
412 static inline void vmx_register_cache_reset(struct kvm_vcpu *vcpu)
413 {
414         vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
415                                   | (1 << VCPU_EXREG_RFLAGS)
416                                   | (1 << VCPU_EXREG_PDPTR)
417                                   | (1 << VCPU_EXREG_SEGMENTS)
418                                   | (1 << VCPU_EXREG_CR0)
419                                   | (1 << VCPU_EXREG_CR3)
420                                   | (1 << VCPU_EXREG_CR4)
421                                   | (1 << VCPU_EXREG_EXIT_INFO_1)
422                                   | (1 << VCPU_EXREG_EXIT_INFO_2));
423         vcpu->arch.regs_dirty = 0;
424 }
425
426 static inline u32 vmx_vmentry_ctrl(void)
427 {
428         u32 vmentry_ctrl = vmcs_config.vmentry_ctrl;
429         if (vmx_pt_mode_is_system())
430                 vmentry_ctrl &= ~(VM_ENTRY_PT_CONCEAL_PIP |
431                                   VM_ENTRY_LOAD_IA32_RTIT_CTL);
432         /* Loading of EFER and PERF_GLOBAL_CTRL are toggled dynamically */
433         return vmentry_ctrl &
434                 ~(VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL | VM_ENTRY_LOAD_IA32_EFER);
435 }
436
437 static inline u32 vmx_vmexit_ctrl(void)
438 {
439         u32 vmexit_ctrl = vmcs_config.vmexit_ctrl;
440         if (vmx_pt_mode_is_system())
441                 vmexit_ctrl &= ~(VM_EXIT_PT_CONCEAL_PIP |
442                                  VM_EXIT_CLEAR_IA32_RTIT_CTL);
443         /* Loading of EFER and PERF_GLOBAL_CTRL are toggled dynamically */
444         return vmexit_ctrl &
445                 ~(VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL | VM_EXIT_LOAD_IA32_EFER);
446 }
447
448 u32 vmx_exec_control(struct vcpu_vmx *vmx);
449 u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx);
450
451 static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
452 {
453         return container_of(kvm, struct kvm_vmx, kvm);
454 }
455
456 static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
457 {
458         return container_of(vcpu, struct vcpu_vmx, vcpu);
459 }
460
461 static inline unsigned long vmx_get_exit_qual(struct kvm_vcpu *vcpu)
462 {
463         struct vcpu_vmx *vmx = to_vmx(vcpu);
464
465         if (!kvm_register_is_available(vcpu, VCPU_EXREG_EXIT_INFO_1)) {
466                 kvm_register_mark_available(vcpu, VCPU_EXREG_EXIT_INFO_1);
467                 vmx->exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
468         }
469         return vmx->exit_qualification;
470 }
471
472 static inline u32 vmx_get_intr_info(struct kvm_vcpu *vcpu)
473 {
474         struct vcpu_vmx *vmx = to_vmx(vcpu);
475
476         if (!kvm_register_is_available(vcpu, VCPU_EXREG_EXIT_INFO_2)) {
477                 kvm_register_mark_available(vcpu, VCPU_EXREG_EXIT_INFO_2);
478                 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
479         }
480         return vmx->exit_intr_info;
481 }
482
483 struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu, gfp_t flags);
484 void free_vmcs(struct vmcs *vmcs);
485 int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs);
486 void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs);
487 void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs);
488
489 static inline struct vmcs *alloc_vmcs(bool shadow)
490 {
491         return alloc_vmcs_cpu(shadow, raw_smp_processor_id(),
492                               GFP_KERNEL_ACCOUNT);
493 }
494
495 static inline void decache_tsc_multiplier(struct vcpu_vmx *vmx)
496 {
497         vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
498         vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
499 }
500
501 static inline bool vmx_has_waitpkg(struct vcpu_vmx *vmx)
502 {
503         return vmx->secondary_exec_control &
504                 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE;
505 }
506
507 static inline bool vmx_need_pf_intercept(struct kvm_vcpu *vcpu)
508 {
509         if (!enable_ept)
510                 return true;
511
512         return allow_smaller_maxphyaddr && cpuid_maxphyaddr(vcpu) < boot_cpu_data.x86_phys_bits;
513 }
514
515 static inline bool is_unrestricted_guest(struct kvm_vcpu *vcpu)
516 {
517         return enable_unrestricted_guest && (!is_guest_mode(vcpu) ||
518             (secondary_exec_controls_get(to_vmx(vcpu)) &
519             SECONDARY_EXEC_UNRESTRICTED_GUEST));
520 }
521
522 bool __vmx_guest_state_valid(struct kvm_vcpu *vcpu);
523 static inline bool vmx_guest_state_valid(struct kvm_vcpu *vcpu)
524 {
525         return is_unrestricted_guest(vcpu) || __vmx_guest_state_valid(vcpu);
526 }
527
528 void dump_vmcs(void);
529
530 #endif /* __KVM_X86_VMX_H */