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 "../kvm_util.h"
20 #define X86_EFLAGS_FIXED (1u << 1)
22 #define X86_CR4_VME (1ul << 0)
23 #define X86_CR4_PVI (1ul << 1)
24 #define X86_CR4_TSD (1ul << 2)
25 #define X86_CR4_DE (1ul << 3)
26 #define X86_CR4_PSE (1ul << 4)
27 #define X86_CR4_PAE (1ul << 5)
28 #define X86_CR4_MCE (1ul << 6)
29 #define X86_CR4_PGE (1ul << 7)
30 #define X86_CR4_PCE (1ul << 8)
31 #define X86_CR4_OSFXSR (1ul << 9)
32 #define X86_CR4_OSXMMEXCPT (1ul << 10)
33 #define X86_CR4_UMIP (1ul << 11)
34 #define X86_CR4_LA57 (1ul << 12)
35 #define X86_CR4_VMXE (1ul << 13)
36 #define X86_CR4_SMXE (1ul << 14)
37 #define X86_CR4_FSGSBASE (1ul << 16)
38 #define X86_CR4_PCIDE (1ul << 17)
39 #define X86_CR4_OSXSAVE (1ul << 18)
40 #define X86_CR4_SMEP (1ul << 20)
41 #define X86_CR4_SMAP (1ul << 21)
42 #define X86_CR4_PKE (1ul << 22)
45 #define CPUID_VMX (1ul << 5)
46 #define CPUID_SMX (1ul << 6)
47 #define CPUID_PCID (1ul << 17)
48 #define CPUID_XSAVE (1ul << 26)
51 #define CPUID_FSGSBASE (1ul << 0)
52 #define CPUID_SMEP (1ul << 7)
53 #define CPUID_SMAP (1ul << 20)
56 #define CPUID_UMIP (1ul << 2)
57 #define CPUID_PKU (1ul << 3)
58 #define CPUID_LA57 (1ul << 16)
60 /* CPUID.0x8000_0001.EDX */
61 #define CPUID_GBPAGES (1ul << 26)
63 /* Page table bitfield declarations */
64 #define PTE_PRESENT_MASK BIT_ULL(0)
65 #define PTE_WRITABLE_MASK BIT_ULL(1)
66 #define PTE_USER_MASK BIT_ULL(2)
67 #define PTE_ACCESSED_MASK BIT_ULL(5)
68 #define PTE_DIRTY_MASK BIT_ULL(6)
69 #define PTE_LARGE_MASK BIT_ULL(7)
70 #define PTE_GLOBAL_MASK BIT_ULL(8)
71 #define PTE_NX_MASK BIT_ULL(63)
74 #define PAGE_SIZE (1ULL << PAGE_SHIFT)
75 #define PAGE_MASK (~(PAGE_SIZE-1))
77 #define PHYSICAL_PAGE_MASK GENMASK_ULL(51, 12)
78 #define PTE_GET_PFN(pte) (((pte) & PHYSICAL_PAGE_MASK) >> PAGE_SHIFT)
80 /* General Registers in 64-Bit Mode */
103 unsigned base1:8, type:4, s:1, dpl:2, p:1;
104 unsigned limit1:4, avl:1, l:1, db:1, g:1, base2:8;
107 } __attribute__((packed));
112 } __attribute__((packed));
114 struct kvm_x86_state {
115 struct kvm_xsave *xsave;
116 struct kvm_vcpu_events events;
117 struct kvm_mp_state mp_state;
118 struct kvm_regs regs;
119 struct kvm_xcrs xcrs;
120 struct kvm_sregs sregs;
121 struct kvm_debugregs debugregs;
123 struct kvm_nested_state nested;
126 struct kvm_msrs msrs;
129 static inline uint64_t get_desc64_base(const struct desc64 *desc)
131 return ((uint64_t)desc->base3 << 32) |
132 (desc->base0 | ((desc->base1) << 16) | ((desc->base2) << 24));
135 static inline uint64_t rdtsc(void)
140 * The lfence is to wait (on Intel CPUs) until all previous
141 * instructions have been executed. If software requires RDTSC to be
142 * executed prior to execution of any subsequent instruction, it can
143 * execute LFENCE immediately after RDTSC
145 __asm__ __volatile__("lfence; rdtsc; lfence" : "=a"(eax), "=d"(edx));
146 tsc_val = ((uint64_t)edx) << 32 | eax;
150 static inline uint64_t rdtscp(uint32_t *aux)
154 __asm__ __volatile__("rdtscp" : "=a"(eax), "=d"(edx), "=c"(*aux));
155 return ((uint64_t)edx) << 32 | eax;
158 static inline uint64_t rdmsr(uint32_t msr)
162 __asm__ __volatile__("rdmsr" : "=a"(a), "=d"(d) : "c"(msr) : "memory");
164 return a | ((uint64_t) d << 32);
167 static inline void wrmsr(uint32_t msr, uint64_t value)
170 uint32_t d = value >> 32;
172 __asm__ __volatile__("wrmsr" :: "a"(a), "d"(d), "c"(msr) : "memory");
176 static inline uint16_t inw(uint16_t port)
180 __asm__ __volatile__("in %%dx, %%ax"
181 : /* output */ "=a" (tmp)
182 : /* input */ "d" (port));
187 static inline uint16_t get_es(void)
191 __asm__ __volatile__("mov %%es, %[es]"
192 : /* output */ [es]"=rm"(es));
196 static inline uint16_t get_cs(void)
200 __asm__ __volatile__("mov %%cs, %[cs]"
201 : /* output */ [cs]"=rm"(cs));
205 static inline uint16_t get_ss(void)
209 __asm__ __volatile__("mov %%ss, %[ss]"
210 : /* output */ [ss]"=rm"(ss));
214 static inline uint16_t get_ds(void)
218 __asm__ __volatile__("mov %%ds, %[ds]"
219 : /* output */ [ds]"=rm"(ds));
223 static inline uint16_t get_fs(void)
227 __asm__ __volatile__("mov %%fs, %[fs]"
228 : /* output */ [fs]"=rm"(fs));
232 static inline uint16_t get_gs(void)
236 __asm__ __volatile__("mov %%gs, %[gs]"
237 : /* output */ [gs]"=rm"(gs));
241 static inline uint16_t get_tr(void)
245 __asm__ __volatile__("str %[tr]"
246 : /* output */ [tr]"=rm"(tr));
250 static inline uint64_t get_cr0(void)
254 __asm__ __volatile__("mov %%cr0, %[cr0]"
255 : /* output */ [cr0]"=r"(cr0));
259 static inline uint64_t get_cr3(void)
263 __asm__ __volatile__("mov %%cr3, %[cr3]"
264 : /* output */ [cr3]"=r"(cr3));
268 static inline uint64_t get_cr4(void)
272 __asm__ __volatile__("mov %%cr4, %[cr4]"
273 : /* output */ [cr4]"=r"(cr4));
277 static inline void set_cr4(uint64_t val)
279 __asm__ __volatile__("mov %0, %%cr4" : : "r" (val) : "memory");
282 static inline struct desc_ptr get_gdt(void)
285 __asm__ __volatile__("sgdt %[gdt]"
286 : /* output */ [gdt]"=m"(gdt));
290 static inline struct desc_ptr get_idt(void)
293 __asm__ __volatile__("sidt %[idt]"
294 : /* output */ [idt]"=m"(idt));
298 static inline void outl(uint16_t port, uint32_t value)
300 __asm__ __volatile__("outl %%eax, %%dx" : : "d"(port), "a"(value));
303 static inline void cpuid(uint32_t *eax, uint32_t *ebx,
304 uint32_t *ecx, uint32_t *edx)
306 /* ecx is often an input as well as an output. */
312 : "0" (*eax), "2" (*ecx)
316 #define SET_XMM(__var, __xmm) \
317 asm volatile("movq %0, %%"#__xmm : : "r"(__var) : #__xmm)
319 static inline void set_xmm(int n, unsigned long val)
349 #define GET_XMM(__xmm) \
351 unsigned long __val; \
352 asm volatile("movq %%"#__xmm", %0" : "=r"(__val)); \
356 static inline unsigned long get_xmm(int n)
358 assert(n >= 0 && n <= 7);
362 return GET_XMM(xmm0);
364 return GET_XMM(xmm1);
366 return GET_XMM(xmm2);
368 return GET_XMM(xmm3);
370 return GET_XMM(xmm4);
372 return GET_XMM(xmm5);
374 return GET_XMM(xmm6);
376 return GET_XMM(xmm7);
383 static inline void cpu_relax(void)
385 asm volatile("rep; nop" ::: "memory");
388 bool is_intel_cpu(void);
389 bool is_amd_cpu(void);
391 static inline unsigned int x86_family(unsigned int eax)
395 x86 = (eax >> 8) & 0xf;
398 x86 += (eax >> 20) & 0xff;
403 static inline unsigned int x86_model(unsigned int eax)
405 return ((eax >> 12) & 0xf0) | ((eax >> 4) & 0x0f);
408 struct kvm_x86_state *vcpu_save_state(struct kvm_vm *vm, uint32_t vcpuid);
409 void vcpu_load_state(struct kvm_vm *vm, uint32_t vcpuid,
410 struct kvm_x86_state *state);
411 void kvm_x86_state_cleanup(struct kvm_x86_state *state);
413 struct kvm_msr_list *kvm_get_msr_index_list(void);
414 uint64_t kvm_get_feature_msr(uint64_t msr_index);
415 struct kvm_cpuid2 *kvm_get_supported_cpuid(void);
417 struct kvm_cpuid2 *vcpu_get_cpuid(struct kvm_vm *vm, uint32_t vcpuid);
418 int __vcpu_set_cpuid(struct kvm_vm *vm, uint32_t vcpuid,
419 struct kvm_cpuid2 *cpuid);
420 void vcpu_set_cpuid(struct kvm_vm *vm, uint32_t vcpuid,
421 struct kvm_cpuid2 *cpuid);
423 struct kvm_cpuid_entry2 *
424 kvm_get_supported_cpuid_index(uint32_t function, uint32_t index);
426 static inline struct kvm_cpuid_entry2 *
427 kvm_get_supported_cpuid_entry(uint32_t function)
429 return kvm_get_supported_cpuid_index(function, 0);
432 uint64_t vcpu_get_msr(struct kvm_vm *vm, uint32_t vcpuid, uint64_t msr_index);
433 int _vcpu_set_msr(struct kvm_vm *vm, uint32_t vcpuid, uint64_t msr_index,
435 void vcpu_set_msr(struct kvm_vm *vm, uint32_t vcpuid, uint64_t msr_index,
438 uint32_t kvm_get_cpuid_max_basic(void);
439 uint32_t kvm_get_cpuid_max_extended(void);
440 void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits);
443 uint64_t rax, rcx, rdx, rbx;
444 uint64_t rbp, rsi, rdi;
445 uint64_t r8, r9, r10, r11;
446 uint64_t r12, r13, r14, r15;
454 void vm_init_descriptor_tables(struct kvm_vm *vm);
455 void vcpu_init_descriptor_tables(struct kvm_vm *vm, uint32_t vcpuid);
456 void vm_install_exception_handler(struct kvm_vm *vm, int vector,
457 void (*handler)(struct ex_regs *));
459 uint64_t vm_get_page_table_entry(struct kvm_vm *vm, int vcpuid, uint64_t vaddr);
460 void vm_set_page_table_entry(struct kvm_vm *vm, int vcpuid, uint64_t vaddr,
464 * get_cpuid() - find matching CPUID entry and return pointer to it.
466 struct kvm_cpuid_entry2 *get_cpuid(struct kvm_cpuid2 *cpuid, uint32_t function,
469 * set_cpuid() - overwrites a matching cpuid entry with the provided value.
470 * matches based on ent->function && ent->index. returns true
471 * if a match was found and successfully overwritten.
472 * @cpuid: the kvm cpuid list to modify.
473 * @ent: cpuid entry to insert
475 bool set_cpuid(struct kvm_cpuid2 *cpuid, struct kvm_cpuid_entry2 *ent);
477 uint64_t kvm_hypercall(uint64_t nr, uint64_t a0, uint64_t a1, uint64_t a2,
480 struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(void);
481 void vcpu_set_hv_cpuid(struct kvm_vm *vm, uint32_t vcpuid);
482 struct kvm_cpuid2 *vcpu_get_supported_hv_cpuid(struct kvm_vm *vm, uint32_t vcpuid);
483 void vm_xsave_req_perm(int bit);
486 X86_PAGE_SIZE_4K = 0,
490 void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
491 enum x86_page_size page_size);
494 * Basic CPU control in CR0
496 #define X86_CR0_PE (1UL<<0) /* Protection Enable */
497 #define X86_CR0_MP (1UL<<1) /* Monitor Coprocessor */
498 #define X86_CR0_EM (1UL<<2) /* Emulation */
499 #define X86_CR0_TS (1UL<<3) /* Task Switched */
500 #define X86_CR0_ET (1UL<<4) /* Extension Type */
501 #define X86_CR0_NE (1UL<<5) /* Numeric Error */
502 #define X86_CR0_WP (1UL<<16) /* Write Protect */
503 #define X86_CR0_AM (1UL<<18) /* Alignment Mask */
504 #define X86_CR0_NW (1UL<<29) /* Not Write-through */
505 #define X86_CR0_CD (1UL<<30) /* Cache Disable */
506 #define X86_CR0_PG (1UL<<31) /* Paging */
508 /* VMX_EPT_VPID_CAP bits */
509 #define VMX_EPT_VPID_CAP_AD_BITS (1ULL << 21)
511 #define XSTATE_XTILE_CFG_BIT 17
512 #define XSTATE_XTILE_DATA_BIT 18
514 #define XSTATE_XTILE_CFG_MASK (1ULL << XSTATE_XTILE_CFG_BIT)
515 #define XSTATE_XTILE_DATA_MASK (1ULL << XSTATE_XTILE_DATA_BIT)
516 #define XFEATURE_XTILE_MASK (XSTATE_XTILE_CFG_MASK | \
517 XSTATE_XTILE_DATA_MASK)
518 #endif /* SELFTEST_KVM_PROCESSOR_H */