kvm userspace: ksm support
[qemu-kvm/fedora.git] / kvm / include / x86 / asm / virtext.h
blobe6f17fb45965242f09eddc41d3725869d1627df6
1 #ifndef KVM_UNIFDEF_H
2 #define KVM_UNIFDEF_H
4 #ifdef __i386__
5 #ifndef CONFIG_X86_32
6 #define CONFIG_X86_32 1
7 #endif
8 #endif
10 #ifdef __x86_64__
11 #ifndef CONFIG_X86_64
12 #define CONFIG_X86_64 1
13 #endif
14 #endif
16 #if defined(__i386__) || defined (__x86_64__)
17 #ifndef CONFIG_X86
18 #define CONFIG_X86 1
19 #endif
20 #endif
22 #ifdef __ia64__
23 #ifndef CONFIG_IA64
24 #define CONFIG_IA64 1
25 #endif
26 #endif
28 #ifdef __PPC__
29 #ifndef CONFIG_PPC
30 #define CONFIG_PPC 1
31 #endif
32 #endif
34 #ifdef __s390__
35 #ifndef CONFIG_S390
36 #define CONFIG_S390 1
37 #endif
38 #endif
40 #endif
41 /* CPU virtualization extensions handling
43 * This should carry the code for handling CPU virtualization extensions
44 * that needs to live in the kernel core.
46 * Author: Eduardo Habkost <ehabkost@redhat.com>
48 * Copyright (C) 2008, Red Hat Inc.
50 * Contains code from KVM, Copyright (C) 2006 Qumranet, Inc.
52 * This work is licensed under the terms of the GNU GPL, version 2. See
53 * the COPYING file in the top-level directory.
55 #ifndef _ASM_X86_VIRTEX_H
56 #define _ASM_X86_VIRTEX_H
58 #include <asm/processor.h>
59 #include <asm/system.h>
61 #include <asm/vmx.h>
62 #include <asm/svm.h>
65 * VMX functions:
68 static inline int cpu_has_vmx(void)
70 unsigned long ecx = cpuid_ecx(1);
71 return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */
75 /** Disable VMX on the current CPU
77 * vmxoff causes a undefined-opcode exception if vmxon was not run
78 * on the CPU previously. Only call this function if you know VMX
79 * is enabled.
81 static inline void cpu_vmxoff(void)
83 asm volatile (ASM_VMX_VMXOFF : : : "cc");
84 write_cr4(read_cr4() & ~X86_CR4_VMXE);
87 static inline int cpu_vmx_enabled(void)
89 return read_cr4() & X86_CR4_VMXE;
92 /** Disable VMX if it is enabled on the current CPU
94 * You shouldn't call this if cpu_has_vmx() returns 0.
96 static inline void __cpu_emergency_vmxoff(void)
98 if (cpu_vmx_enabled())
99 cpu_vmxoff();
102 /** Disable VMX if it is supported and enabled on the current CPU
104 static inline void cpu_emergency_vmxoff(void)
106 if (cpu_has_vmx())
107 __cpu_emergency_vmxoff();
114 * SVM functions:
117 /** Check if the CPU has SVM support
119 * You can use the 'msg' arg to get a message describing the problem,
120 * if the function returns zero. Simply pass NULL if you are not interested
121 * on the messages; gcc should take care of not generating code for
122 * the messages on this case.
124 static inline int cpu_has_svm(const char **msg)
126 uint32_t eax, ebx, ecx, edx;
128 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD) {
129 if (msg)
130 *msg = "not amd";
131 return 0;
134 cpuid(0x80000000, &eax, &ebx, &ecx, &edx);
135 if (eax < SVM_CPUID_FUNC) {
136 if (msg)
137 *msg = "can't execute cpuid_8000000a";
138 return 0;
141 cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
142 if (!(ecx & (1 << SVM_CPUID_FEATURE_SHIFT))) {
143 if (msg)
144 *msg = "svm not available";
145 return 0;
147 return 1;
151 /** Disable SVM on the current CPU
153 * You should call this only if cpu_has_svm() returned true.
155 static inline void cpu_svm_disable(void)
157 uint64_t efer;
159 wrmsrl(MSR_VM_HSAVE_PA, 0);
160 rdmsrl(MSR_EFER, efer);
161 wrmsrl(MSR_EFER, efer & ~EFER_SVME);
164 /** Makes sure SVM is disabled, if it is supported on the CPU
166 static inline void cpu_emergency_svm_disable(void)
168 if (cpu_has_svm(NULL))
169 cpu_svm_disable();
172 #endif /* _ASM_X86_VIRTEX_H */