1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* CPU virtualization extensions handling
4 * This should carry the code for handling CPU virtualization extensions
5 * that needs to live in the kernel core.
7 * Author: Eduardo Habkost <ehabkost@redhat.com>
9 * Copyright (C) 2008, Red Hat Inc.
11 * Contains code from KVM, Copyright (C) 2006 Qumranet, Inc.
13 #ifndef _ASM_X86_VIRTEX_H
14 #define _ASM_X86_VIRTEX_H
16 #include <asm/processor.h>
20 #include <asm/tlbflush.h>
26 static inline int cpu_has_vmx(void)
28 unsigned long ecx
= cpuid_ecx(1);
29 return test_bit(5, &ecx
); /* CPUID.1:ECX.VMX[bit 5] -> VT */
33 /** Disable VMX on the current CPU
35 * vmxoff causes a undefined-opcode exception if vmxon was not run
36 * on the CPU previously. Only call this function if you know VMX
39 static inline void cpu_vmxoff(void)
41 asm volatile ("vmxoff");
42 cr4_clear_bits(X86_CR4_VMXE
);
45 static inline int cpu_vmx_enabled(void)
47 return __read_cr4() & X86_CR4_VMXE
;
50 /** Disable VMX if it is enabled on the current CPU
52 * You shouldn't call this if cpu_has_vmx() returns 0.
54 static inline void __cpu_emergency_vmxoff(void)
56 if (cpu_vmx_enabled())
60 /** Disable VMX if it is supported and enabled on the current CPU
62 static inline void cpu_emergency_vmxoff(void)
65 __cpu_emergency_vmxoff();
75 /** Check if the CPU has SVM support
77 * You can use the 'msg' arg to get a message describing the problem,
78 * if the function returns zero. Simply pass NULL if you are not interested
79 * on the messages; gcc should take care of not generating code for
80 * the messages on this case.
82 static inline int cpu_has_svm(const char **msg
)
84 if (boot_cpu_data
.x86_vendor
!= X86_VENDOR_AMD
&&
85 boot_cpu_data
.x86_vendor
!= X86_VENDOR_HYGON
) {
87 *msg
= "not amd or hygon";
91 if (boot_cpu_data
.extended_cpuid_level
< SVM_CPUID_FUNC
) {
93 *msg
= "can't execute cpuid_8000000a";
97 if (!boot_cpu_has(X86_FEATURE_SVM
)) {
99 *msg
= "svm not available";
106 /** Disable SVM on the current CPU
108 * You should call this only if cpu_has_svm() returned true.
110 static inline void cpu_svm_disable(void)
114 wrmsrl(MSR_VM_HSAVE_PA
, 0);
115 rdmsrl(MSR_EFER
, efer
);
116 wrmsrl(MSR_EFER
, efer
& ~EFER_SVME
);
119 /** Makes sure SVM is disabled, if it is supported on the CPU
121 static inline void cpu_emergency_svm_disable(void)
123 if (cpu_has_svm(NULL
))
127 #endif /* _ASM_X86_VIRTEX_H */