1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * tools/testing/selftests/kvm/include/x86_64/processor.h
5 * Copyright (C) 2018, Google LLC.
8 #ifndef SELFTEST_KVM_PROCESSOR_H
9 #define SELFTEST_KVM_PROCESSOR_H
15 #include <asm/msr-index.h>
16 #include <asm/prctl.h>
18 #include <linux/stringify.h>
20 #include "../kvm_util.h"
22 #define NMI_VECTOR 0x02
24 #define X86_EFLAGS_FIXED (1u << 1)
26 #define X86_CR4_VME (1ul << 0)
27 #define X86_CR4_PVI (1ul << 1)
28 #define X86_CR4_TSD (1ul << 2)
29 #define X86_CR4_DE (1ul << 3)
30 #define X86_CR4_PSE (1ul << 4)
31 #define X86_CR4_PAE (1ul << 5)
32 #define X86_CR4_MCE (1ul << 6)
33 #define X86_CR4_PGE (1ul << 7)
34 #define X86_CR4_PCE (1ul << 8)
35 #define X86_CR4_OSFXSR (1ul << 9)
36 #define X86_CR4_OSXMMEXCPT (1ul << 10)
37 #define X86_CR4_UMIP (1ul << 11)
38 #define X86_CR4_LA57 (1ul << 12)
39 #define X86_CR4_VMXE (1ul << 13)
40 #define X86_CR4_SMXE (1ul << 14)
41 #define X86_CR4_FSGSBASE (1ul << 16)
42 #define X86_CR4_PCIDE (1ul << 17)
43 #define X86_CR4_OSXSAVE (1ul << 18)
44 #define X86_CR4_SMEP (1ul << 20)
45 #define X86_CR4_SMAP (1ul << 21)
46 #define X86_CR4_PKE (1ul << 22)
48 /* Note, these are ordered alphabetically to match kvm_cpuid_entry2. Eww. */
49 enum cpuid_output_regs {
57 * Pack the information into a 64-bit value so that each X86_FEATURE_XXX can be
58 * passed by value with no overhead.
60 struct kvm_x86_cpu_feature {
66 #define KVM_X86_CPU_FEATURE(fn, idx, gpr, __bit) \
68 struct kvm_x86_cpu_feature feature = { \
71 .reg = KVM_CPUID_##gpr, \
79 * Basic Leafs, a.k.a. Intel defined
81 #define X86_FEATURE_MWAIT KVM_X86_CPU_FEATURE(0x1, 0, ECX, 3)
82 #define X86_FEATURE_VMX KVM_X86_CPU_FEATURE(0x1, 0, ECX, 5)
83 #define X86_FEATURE_SMX KVM_X86_CPU_FEATURE(0x1, 0, ECX, 6)
84 #define X86_FEATURE_PDCM KVM_X86_CPU_FEATURE(0x1, 0, ECX, 15)
85 #define X86_FEATURE_PCID KVM_X86_CPU_FEATURE(0x1, 0, ECX, 17)
86 #define X86_FEATURE_X2APIC KVM_X86_CPU_FEATURE(0x1, 0, ECX, 21)
87 #define X86_FEATURE_MOVBE KVM_X86_CPU_FEATURE(0x1, 0, ECX, 22)
88 #define X86_FEATURE_TSC_DEADLINE_TIMER KVM_X86_CPU_FEATURE(0x1, 0, ECX, 24)
89 #define X86_FEATURE_XSAVE KVM_X86_CPU_FEATURE(0x1, 0, ECX, 26)
90 #define X86_FEATURE_OSXSAVE KVM_X86_CPU_FEATURE(0x1, 0, ECX, 27)
91 #define X86_FEATURE_RDRAND KVM_X86_CPU_FEATURE(0x1, 0, ECX, 30)
92 #define X86_FEATURE_MCE KVM_X86_CPU_FEATURE(0x1, 0, EDX, 7)
93 #define X86_FEATURE_APIC KVM_X86_CPU_FEATURE(0x1, 0, EDX, 9)
94 #define X86_FEATURE_CLFLUSH KVM_X86_CPU_FEATURE(0x1, 0, EDX, 19)
95 #define X86_FEATURE_XMM KVM_X86_CPU_FEATURE(0x1, 0, EDX, 25)
96 #define X86_FEATURE_XMM2 KVM_X86_CPU_FEATURE(0x1, 0, EDX, 26)
97 #define X86_FEATURE_FSGSBASE KVM_X86_CPU_FEATURE(0x7, 0, EBX, 0)
98 #define X86_FEATURE_TSC_ADJUST KVM_X86_CPU_FEATURE(0x7, 0, EBX, 1)
99 #define X86_FEATURE_HLE KVM_X86_CPU_FEATURE(0x7, 0, EBX, 4)
100 #define X86_FEATURE_SMEP KVM_X86_CPU_FEATURE(0x7, 0, EBX, 7)
101 #define X86_FEATURE_INVPCID KVM_X86_CPU_FEATURE(0x7, 0, EBX, 10)
102 #define X86_FEATURE_RTM KVM_X86_CPU_FEATURE(0x7, 0, EBX, 11)
103 #define X86_FEATURE_MPX KVM_X86_CPU_FEATURE(0x7, 0, EBX, 14)
104 #define X86_FEATURE_SMAP KVM_X86_CPU_FEATURE(0x7, 0, EBX, 20)
105 #define X86_FEATURE_PCOMMIT KVM_X86_CPU_FEATURE(0x7, 0, EBX, 22)
106 #define X86_FEATURE_CLFLUSHOPT KVM_X86_CPU_FEATURE(0x7, 0, EBX, 23)
107 #define X86_FEATURE_CLWB KVM_X86_CPU_FEATURE(0x7, 0, EBX, 24)
108 #define X86_FEATURE_UMIP KVM_X86_CPU_FEATURE(0x7, 0, ECX, 2)
109 #define X86_FEATURE_PKU KVM_X86_CPU_FEATURE(0x7, 0, ECX, 3)
110 #define X86_FEATURE_LA57 KVM_X86_CPU_FEATURE(0x7, 0, ECX, 16)
111 #define X86_FEATURE_RDPID KVM_X86_CPU_FEATURE(0x7, 0, ECX, 22)
112 #define X86_FEATURE_SHSTK KVM_X86_CPU_FEATURE(0x7, 0, ECX, 7)
113 #define X86_FEATURE_IBT KVM_X86_CPU_FEATURE(0x7, 0, EDX, 20)
114 #define X86_FEATURE_AMX_TILE KVM_X86_CPU_FEATURE(0x7, 0, EDX, 24)
115 #define X86_FEATURE_SPEC_CTRL KVM_X86_CPU_FEATURE(0x7, 0, EDX, 26)
116 #define X86_FEATURE_ARCH_CAPABILITIES KVM_X86_CPU_FEATURE(0x7, 0, EDX, 29)
117 #define X86_FEATURE_PKS KVM_X86_CPU_FEATURE(0x7, 0, ECX, 31)
118 #define X86_FEATURE_XTILECFG KVM_X86_CPU_FEATURE(0xD, 0, EAX, 17)
119 #define X86_FEATURE_XTILEDATA KVM_X86_CPU_FEATURE(0xD, 0, EAX, 18)
120 #define X86_FEATURE_XSAVES KVM_X86_CPU_FEATURE(0xD, 1, EAX, 3)
121 #define X86_FEATURE_XFD KVM_X86_CPU_FEATURE(0xD, 1, EAX, 4)
124 * Extended Leafs, a.k.a. AMD defined
126 #define X86_FEATURE_SVM KVM_X86_CPU_FEATURE(0x80000001, 0, ECX, 2)
127 #define X86_FEATURE_NX KVM_X86_CPU_FEATURE(0x80000001, 0, EDX, 20)
128 #define X86_FEATURE_GBPAGES KVM_X86_CPU_FEATURE(0x80000001, 0, EDX, 26)
129 #define X86_FEATURE_RDTSCP KVM_X86_CPU_FEATURE(0x80000001, 0, EDX, 27)
130 #define X86_FEATURE_LM KVM_X86_CPU_FEATURE(0x80000001, 0, EDX, 29)
131 #define X86_FEATURE_RDPRU KVM_X86_CPU_FEATURE(0x80000008, 0, EBX, 4)
132 #define X86_FEATURE_AMD_IBPB KVM_X86_CPU_FEATURE(0x80000008, 0, EBX, 12)
133 #define X86_FEATURE_NPT KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 0)
134 #define X86_FEATURE_LBRV KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 1)
135 #define X86_FEATURE_NRIPS KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 3)
136 #define X86_FEATURE_TSCRATEMSR KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 4)
137 #define X86_FEATURE_PAUSEFILTER KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 10)
138 #define X86_FEATURE_PFTHRESHOLD KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 12)
139 #define X86_FEATURE_VGIF KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 16)
140 #define X86_FEATURE_SEV KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 1)
141 #define X86_FEATURE_SEV_ES KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 3)
144 * KVM defined paravirt features.
146 #define X86_FEATURE_KVM_CLOCKSOURCE KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 0)
147 #define X86_FEATURE_KVM_NOP_IO_DELAY KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 1)
148 #define X86_FEATURE_KVM_MMU_OP KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 2)
149 #define X86_FEATURE_KVM_CLOCKSOURCE2 KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 3)
150 #define X86_FEATURE_KVM_ASYNC_PF KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 4)
151 #define X86_FEATURE_KVM_STEAL_TIME KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 5)
152 #define X86_FEATURE_KVM_PV_EOI KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 6)
153 #define X86_FEATURE_KVM_PV_UNHALT KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 7)
154 /* Bit 8 apparently isn't used?!?! */
155 #define X86_FEATURE_KVM_PV_TLB_FLUSH KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 9)
156 #define X86_FEATURE_KVM_ASYNC_PF_VMEXIT KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 10)
157 #define X86_FEATURE_KVM_PV_SEND_IPI KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 11)
158 #define X86_FEATURE_KVM_POLL_CONTROL KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 12)
159 #define X86_FEATURE_KVM_PV_SCHED_YIELD KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 13)
160 #define X86_FEATURE_KVM_ASYNC_PF_INT KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 14)
161 #define X86_FEATURE_KVM_MSI_EXT_DEST_ID KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 15)
162 #define X86_FEATURE_KVM_HC_MAP_GPA_RANGE KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 16)
163 #define X86_FEATURE_KVM_MIGRATION_CONTROL KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 17)
165 /* Page table bitfield declarations */
166 #define PTE_PRESENT_MASK BIT_ULL(0)
167 #define PTE_WRITABLE_MASK BIT_ULL(1)
168 #define PTE_USER_MASK BIT_ULL(2)
169 #define PTE_ACCESSED_MASK BIT_ULL(5)
170 #define PTE_DIRTY_MASK BIT_ULL(6)
171 #define PTE_LARGE_MASK BIT_ULL(7)
172 #define PTE_GLOBAL_MASK BIT_ULL(8)
173 #define PTE_NX_MASK BIT_ULL(63)
175 #define PAGE_SHIFT 12
176 #define PAGE_SIZE (1ULL << PAGE_SHIFT)
177 #define PAGE_MASK (~(PAGE_SIZE-1))
179 #define PHYSICAL_PAGE_MASK GENMASK_ULL(51, 12)
180 #define PTE_GET_PFN(pte) (((pte) & PHYSICAL_PAGE_MASK) >> PAGE_SHIFT)
182 /* General Registers in 64-Bit Mode */
205 unsigned base1:8, type:4, s:1, dpl:2, p:1;
206 unsigned limit1:4, avl:1, l:1, db:1, g:1, base2:8;
209 } __attribute__((packed));
214 } __attribute__((packed));
216 struct kvm_x86_state {
217 struct kvm_xsave *xsave;
218 struct kvm_vcpu_events events;
219 struct kvm_mp_state mp_state;
220 struct kvm_regs regs;
221 struct kvm_xcrs xcrs;
222 struct kvm_sregs sregs;
223 struct kvm_debugregs debugregs;
225 struct kvm_nested_state nested;
228 struct kvm_msrs msrs;
231 static inline uint64_t get_desc64_base(const struct desc64 *desc)
233 return ((uint64_t)desc->base3 << 32) |
234 (desc->base0 | ((desc->base1) << 16) | ((desc->base2) << 24));
237 static inline uint64_t rdtsc(void)
242 * The lfence is to wait (on Intel CPUs) until all previous
243 * instructions have been executed. If software requires RDTSC to be
244 * executed prior to execution of any subsequent instruction, it can
245 * execute LFENCE immediately after RDTSC
247 __asm__ __volatile__("lfence; rdtsc; lfence" : "=a"(eax), "=d"(edx));
248 tsc_val = ((uint64_t)edx) << 32 | eax;
252 static inline uint64_t rdtscp(uint32_t *aux)
256 __asm__ __volatile__("rdtscp" : "=a"(eax), "=d"(edx), "=c"(*aux));
257 return ((uint64_t)edx) << 32 | eax;
260 static inline uint64_t rdmsr(uint32_t msr)
264 __asm__ __volatile__("rdmsr" : "=a"(a), "=d"(d) : "c"(msr) : "memory");
266 return a | ((uint64_t) d << 32);
269 static inline void wrmsr(uint32_t msr, uint64_t value)
272 uint32_t d = value >> 32;
274 __asm__ __volatile__("wrmsr" :: "a"(a), "d"(d), "c"(msr) : "memory");
278 static inline uint16_t inw(uint16_t port)
282 __asm__ __volatile__("in %%dx, %%ax"
283 : /* output */ "=a" (tmp)
284 : /* input */ "d" (port));
289 static inline uint16_t get_es(void)
293 __asm__ __volatile__("mov %%es, %[es]"
294 : /* output */ [es]"=rm"(es));
298 static inline uint16_t get_cs(void)
302 __asm__ __volatile__("mov %%cs, %[cs]"
303 : /* output */ [cs]"=rm"(cs));
307 static inline uint16_t get_ss(void)
311 __asm__ __volatile__("mov %%ss, %[ss]"
312 : /* output */ [ss]"=rm"(ss));
316 static inline uint16_t get_ds(void)
320 __asm__ __volatile__("mov %%ds, %[ds]"
321 : /* output */ [ds]"=rm"(ds));
325 static inline uint16_t get_fs(void)
329 __asm__ __volatile__("mov %%fs, %[fs]"
330 : /* output */ [fs]"=rm"(fs));
334 static inline uint16_t get_gs(void)
338 __asm__ __volatile__("mov %%gs, %[gs]"
339 : /* output */ [gs]"=rm"(gs));
343 static inline uint16_t get_tr(void)
347 __asm__ __volatile__("str %[tr]"
348 : /* output */ [tr]"=rm"(tr));
352 static inline uint64_t get_cr0(void)
356 __asm__ __volatile__("mov %%cr0, %[cr0]"
357 : /* output */ [cr0]"=r"(cr0));
361 static inline uint64_t get_cr3(void)
365 __asm__ __volatile__("mov %%cr3, %[cr3]"
366 : /* output */ [cr3]"=r"(cr3));
370 static inline uint64_t get_cr4(void)
374 __asm__ __volatile__("mov %%cr4, %[cr4]"
375 : /* output */ [cr4]"=r"(cr4));
379 static inline void set_cr4(uint64_t val)
381 __asm__ __volatile__("mov %0, %%cr4" : : "r" (val) : "memory");
384 static inline struct desc_ptr get_gdt(void)
387 __asm__ __volatile__("sgdt %[gdt]"
388 : /* output */ [gdt]"=m"(gdt));
392 static inline struct desc_ptr get_idt(void)
395 __asm__ __volatile__("sidt %[idt]"
396 : /* output */ [idt]"=m"(idt));
400 static inline void outl(uint16_t port, uint32_t value)
402 __asm__ __volatile__("outl %%eax, %%dx" : : "d"(port), "a"(value));
405 static inline void __cpuid(uint32_t function, uint32_t index,
406 uint32_t *eax, uint32_t *ebx,
407 uint32_t *ecx, uint32_t *edx)
417 : "0" (*eax), "2" (*ecx)
421 static inline void cpuid(uint32_t function,
422 uint32_t *eax, uint32_t *ebx,
423 uint32_t *ecx, uint32_t *edx)
425 return __cpuid(function, 0, eax, ebx, ecx, edx);
428 static inline bool this_cpu_has(struct kvm_x86_cpu_feature feature)
432 __cpuid(feature.function, feature.index,
433 &gprs[KVM_CPUID_EAX], &gprs[KVM_CPUID_EBX],
434 &gprs[KVM_CPUID_ECX], &gprs[KVM_CPUID_EDX]);
436 return gprs[feature.reg] & BIT(feature.bit);
439 #define SET_XMM(__var, __xmm) \
440 asm volatile("movq %0, %%"#__xmm : : "r"(__var) : #__xmm)
442 static inline void set_xmm(int n, unsigned long val)
472 #define GET_XMM(__xmm) \
474 unsigned long __val; \
475 asm volatile("movq %%"#__xmm", %0" : "=r"(__val)); \
479 static inline unsigned long get_xmm(int n)
481 assert(n >= 0 && n <= 7);
485 return GET_XMM(xmm0);
487 return GET_XMM(xmm1);
489 return GET_XMM(xmm2);
491 return GET_XMM(xmm3);
493 return GET_XMM(xmm4);
495 return GET_XMM(xmm5);
497 return GET_XMM(xmm6);
499 return GET_XMM(xmm7);
506 static inline void cpu_relax(void)
508 asm volatile("rep; nop" ::: "memory");
512 __asm__ __volatile__( \
517 __asm__ __volatile__( \
522 __asm__ __volatile__( \
526 bool is_intel_cpu(void);
527 bool is_amd_cpu(void);
529 static inline unsigned int x86_family(unsigned int eax)
533 x86 = (eax >> 8) & 0xf;
536 x86 += (eax >> 20) & 0xff;
541 static inline unsigned int x86_model(unsigned int eax)
543 return ((eax >> 12) & 0xf0) | ((eax >> 4) & 0x0f);
546 struct kvm_x86_state *vcpu_save_state(struct kvm_vcpu *vcpu);
547 void vcpu_load_state(struct kvm_vcpu *vcpu, struct kvm_x86_state *state);
548 void kvm_x86_state_cleanup(struct kvm_x86_state *state);
550 const struct kvm_msr_list *kvm_get_msr_index_list(void);
551 const struct kvm_msr_list *kvm_get_feature_msr_index_list(void);
552 bool kvm_msr_is_in_save_restore_list(uint32_t msr_index);
553 uint64_t kvm_get_feature_msr(uint64_t msr_index);
555 static inline void vcpu_msrs_get(struct kvm_vcpu *vcpu,
556 struct kvm_msrs *msrs)
558 int r = __vcpu_ioctl(vcpu, KVM_GET_MSRS, msrs);
560 TEST_ASSERT(r == msrs->nmsrs,
561 "KVM_GET_MSRS failed, r: %i (failed on MSR %x)",
562 r, r < 0 || r >= msrs->nmsrs ? -1 : msrs->entries[r].index);
564 static inline void vcpu_msrs_set(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs)
566 int r = __vcpu_ioctl(vcpu, KVM_SET_MSRS, msrs);
568 TEST_ASSERT(r == msrs->nmsrs,
569 "KVM_GET_MSRS failed, r: %i (failed on MSR %x)",
570 r, r < 0 || r >= msrs->nmsrs ? -1 : msrs->entries[r].index);
572 static inline void vcpu_debugregs_get(struct kvm_vcpu *vcpu,
573 struct kvm_debugregs *debugregs)
575 vcpu_ioctl(vcpu, KVM_GET_DEBUGREGS, debugregs);
577 static inline void vcpu_debugregs_set(struct kvm_vcpu *vcpu,
578 struct kvm_debugregs *debugregs)
580 vcpu_ioctl(vcpu, KVM_SET_DEBUGREGS, debugregs);
582 static inline void vcpu_xsave_get(struct kvm_vcpu *vcpu,
583 struct kvm_xsave *xsave)
585 vcpu_ioctl(vcpu, KVM_GET_XSAVE, xsave);
587 static inline void vcpu_xsave2_get(struct kvm_vcpu *vcpu,
588 struct kvm_xsave *xsave)
590 vcpu_ioctl(vcpu, KVM_GET_XSAVE2, xsave);
592 static inline void vcpu_xsave_set(struct kvm_vcpu *vcpu,
593 struct kvm_xsave *xsave)
595 vcpu_ioctl(vcpu, KVM_SET_XSAVE, xsave);
597 static inline void vcpu_xcrs_get(struct kvm_vcpu *vcpu,
598 struct kvm_xcrs *xcrs)
600 vcpu_ioctl(vcpu, KVM_GET_XCRS, xcrs);
602 static inline void vcpu_xcrs_set(struct kvm_vcpu *vcpu, struct kvm_xcrs *xcrs)
604 vcpu_ioctl(vcpu, KVM_SET_XCRS, xcrs);
607 const struct kvm_cpuid2 *kvm_get_supported_cpuid(void);
608 const struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(void);
609 const struct kvm_cpuid2 *vcpu_get_supported_hv_cpuid(struct kvm_vcpu *vcpu);
611 bool kvm_cpuid_has(const struct kvm_cpuid2 *cpuid,
612 struct kvm_x86_cpu_feature feature);
614 static inline bool kvm_cpu_has(struct kvm_x86_cpu_feature feature)
616 return kvm_cpuid_has(kvm_get_supported_cpuid(), feature);
619 static inline size_t kvm_cpuid2_size(int nr_entries)
621 return sizeof(struct kvm_cpuid2) +
622 sizeof(struct kvm_cpuid_entry2) * nr_entries;
626 * Allocate a "struct kvm_cpuid2* instance, with the 0-length arrary of
627 * entries sized to hold @nr_entries. The caller is responsible for freeing
630 static inline struct kvm_cpuid2 *allocate_kvm_cpuid2(int nr_entries)
632 struct kvm_cpuid2 *cpuid;
634 cpuid = malloc(kvm_cpuid2_size(nr_entries));
635 TEST_ASSERT(cpuid, "-ENOMEM when allocating kvm_cpuid2");
637 cpuid->nent = nr_entries;
642 const struct kvm_cpuid_entry2 *get_cpuid_entry(const struct kvm_cpuid2 *cpuid,
643 uint32_t function, uint32_t index);
644 void vcpu_init_cpuid(struct kvm_vcpu *vcpu, const struct kvm_cpuid2 *cpuid);
645 void vcpu_set_hv_cpuid(struct kvm_vcpu *vcpu);
647 static inline struct kvm_cpuid_entry2 *__vcpu_get_cpuid_entry(struct kvm_vcpu *vcpu,
651 return (struct kvm_cpuid_entry2 *)get_cpuid_entry(vcpu->cpuid,
655 static inline struct kvm_cpuid_entry2 *vcpu_get_cpuid_entry(struct kvm_vcpu *vcpu,
658 return __vcpu_get_cpuid_entry(vcpu, function, 0);
661 static inline int __vcpu_set_cpuid(struct kvm_vcpu *vcpu)
665 TEST_ASSERT(vcpu->cpuid, "Must do vcpu_init_cpuid() first");
666 r = __vcpu_ioctl(vcpu, KVM_SET_CPUID2, vcpu->cpuid);
670 /* On success, refresh the cache to pick up adjustments made by KVM. */
671 vcpu_ioctl(vcpu, KVM_GET_CPUID2, vcpu->cpuid);
675 static inline void vcpu_set_cpuid(struct kvm_vcpu *vcpu)
677 TEST_ASSERT(vcpu->cpuid, "Must do vcpu_init_cpuid() first");
678 vcpu_ioctl(vcpu, KVM_SET_CPUID2, vcpu->cpuid);
680 /* Refresh the cache to pick up adjustments made by KVM. */
681 vcpu_ioctl(vcpu, KVM_GET_CPUID2, vcpu->cpuid);
684 void vcpu_set_cpuid_maxphyaddr(struct kvm_vcpu *vcpu, uint8_t maxphyaddr);
686 void vcpu_clear_cpuid_entry(struct kvm_vcpu *vcpu, uint32_t function);
687 void vcpu_set_or_clear_cpuid_feature(struct kvm_vcpu *vcpu,
688 struct kvm_x86_cpu_feature feature,
691 static inline void vcpu_set_cpuid_feature(struct kvm_vcpu *vcpu,
692 struct kvm_x86_cpu_feature feature)
694 vcpu_set_or_clear_cpuid_feature(vcpu, feature, true);
698 static inline void vcpu_clear_cpuid_feature(struct kvm_vcpu *vcpu,
699 struct kvm_x86_cpu_feature feature)
701 vcpu_set_or_clear_cpuid_feature(vcpu, feature, false);
704 static inline const struct kvm_cpuid_entry2 *__kvm_get_supported_cpuid_entry(uint32_t function,
707 return get_cpuid_entry(kvm_get_supported_cpuid(), function, index);
710 static inline const struct kvm_cpuid_entry2 *kvm_get_supported_cpuid_entry(uint32_t function)
712 return __kvm_get_supported_cpuid_entry(function, 0);
715 uint64_t vcpu_get_msr(struct kvm_vcpu *vcpu, uint64_t msr_index);
716 int _vcpu_set_msr(struct kvm_vcpu *vcpu, uint64_t msr_index, uint64_t msr_value);
718 static inline void vcpu_set_msr(struct kvm_vcpu *vcpu, uint64_t msr_index,
721 int r = _vcpu_set_msr(vcpu, msr_index, msr_value);
723 TEST_ASSERT(r == 1, KVM_IOCTL_ERROR(KVM_SET_MSRS, r));
726 static inline uint32_t kvm_get_cpuid_max_basic(void)
728 return kvm_get_supported_cpuid_entry(0)->eax;
731 static inline uint32_t kvm_get_cpuid_max_extended(void)
733 return kvm_get_supported_cpuid_entry(0x80000000)->eax;
736 void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits);
737 bool vm_is_unrestricted_guest(struct kvm_vm *vm);
740 uint64_t rax, rcx, rdx, rbx;
741 uint64_t rbp, rsi, rdi;
742 uint64_t r8, r9, r10, r11;
743 uint64_t r12, r13, r14, r15;
751 void vm_init_descriptor_tables(struct kvm_vm *vm);
752 void vcpu_init_descriptor_tables(struct kvm_vcpu *vcpu);
753 void vm_install_exception_handler(struct kvm_vm *vm, int vector,
754 void (*handler)(struct ex_regs *));
756 /* If a toddler were to say "abracadabra". */
757 #define KVM_EXCEPTION_MAGIC 0xabacadabaull
760 * KVM selftest exception fixup uses registers to coordinate with the exception
761 * handler, versus the kernel's in-memory tables and KVM-Unit-Tests's in-memory
762 * per-CPU data. Using only registers avoids having to map memory into the
763 * guest, doesn't require a valid, stable GS.base, and reduces the risk of
764 * for recursive faults when accessing memory in the handler. The downside to
765 * using registers is that it restricts what registers can be used by the actual
766 * instruction. But, selftests are 64-bit only, making register* pressure a
767 * minor concern. Use r9-r11 as they are volatile, i.e. don't need* to be saved
768 * by the callee, and except for r11 are not implicit parameters to any
769 * instructions. Ideally, fixup would use r8-r10 and thus avoid implicit
770 * parameters entirely, but Hyper-V's hypercall ABI uses r8 and testing Hyper-V
771 * is higher priority than testing non-faulting SYSCALL/SYSRET.
773 * Note, the fixup handler deliberately does not handle #DE, i.e. the vector
774 * is guaranteed to be non-zero on fault.
779 * r11 = new RIP on fault
782 * r9 = exception vector (non-zero)
784 #define KVM_ASM_SAFE(insn) \
785 "mov $" __stringify(KVM_EXCEPTION_MAGIC) ", %%r9\n\t" \
786 "lea 1f(%%rip), %%r10\n\t" \
787 "lea 2f(%%rip), %%r11\n\t" \
789 "mov $0, %[vector]\n\t" \
792 "mov %%r9b, %[vector]\n\t" \
795 #define KVM_ASM_SAFE_OUTPUTS(v) [vector] "=qm"(v)
796 #define KVM_ASM_SAFE_CLOBBERS "r9", "r10", "r11"
798 #define kvm_asm_safe(insn, inputs...) \
802 asm volatile(KVM_ASM_SAFE(insn) \
803 : KVM_ASM_SAFE_OUTPUTS(vector) \
805 : KVM_ASM_SAFE_CLOBBERS); \
809 static inline uint8_t rdmsr_safe(uint32_t msr, uint64_t *val)
814 asm volatile(KVM_ASM_SAFE("rdmsr")
815 : "=a"(a), "=d"(d), KVM_ASM_SAFE_OUTPUTS(vector)
817 : KVM_ASM_SAFE_CLOBBERS);
819 *val = (uint64_t)a | ((uint64_t)d << 32);
823 static inline uint8_t wrmsr_safe(uint32_t msr, uint64_t val)
825 return kvm_asm_safe("wrmsr", "a"(val & -1u), "d"(val >> 32), "c"(msr));
828 uint64_t vm_get_page_table_entry(struct kvm_vm *vm, struct kvm_vcpu *vcpu,
830 void vm_set_page_table_entry(struct kvm_vm *vm, struct kvm_vcpu *vcpu,
831 uint64_t vaddr, uint64_t pte);
833 uint64_t kvm_hypercall(uint64_t nr, uint64_t a0, uint64_t a1, uint64_t a2,
836 void __vm_xsave_require_permission(int bit, const char *name);
838 #define vm_xsave_require_permission(perm) \
839 __vm_xsave_require_permission(perm, #perm)
850 #define PG_LEVEL_SHIFT(_level) ((_level - 1) * 9 + 12)
851 #define PG_LEVEL_SIZE(_level) (1ull << PG_LEVEL_SHIFT(_level))
853 #define PG_SIZE_4K PG_LEVEL_SIZE(PG_LEVEL_4K)
854 #define PG_SIZE_2M PG_LEVEL_SIZE(PG_LEVEL_2M)
855 #define PG_SIZE_1G PG_LEVEL_SIZE(PG_LEVEL_1G)
857 void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level);
860 * Basic CPU control in CR0
862 #define X86_CR0_PE (1UL<<0) /* Protection Enable */
863 #define X86_CR0_MP (1UL<<1) /* Monitor Coprocessor */
864 #define X86_CR0_EM (1UL<<2) /* Emulation */
865 #define X86_CR0_TS (1UL<<3) /* Task Switched */
866 #define X86_CR0_ET (1UL<<4) /* Extension Type */
867 #define X86_CR0_NE (1UL<<5) /* Numeric Error */
868 #define X86_CR0_WP (1UL<<16) /* Write Protect */
869 #define X86_CR0_AM (1UL<<18) /* Alignment Mask */
870 #define X86_CR0_NW (1UL<<29) /* Not Write-through */
871 #define X86_CR0_CD (1UL<<30) /* Cache Disable */
872 #define X86_CR0_PG (1UL<<31) /* Paging */
874 #define XSTATE_XTILE_CFG_BIT 17
875 #define XSTATE_XTILE_DATA_BIT 18
877 #define XSTATE_XTILE_CFG_MASK (1ULL << XSTATE_XTILE_CFG_BIT)
878 #define XSTATE_XTILE_DATA_MASK (1ULL << XSTATE_XTILE_DATA_BIT)
879 #define XFEATURE_XTILE_MASK (XSTATE_XTILE_CFG_MASK | \
880 XSTATE_XTILE_DATA_MASK)
881 #endif /* SELFTEST_KVM_PROCESSOR_H */