[ penberg@cs.helsinki.fi: use host_cpuid(), cleanups ]
Reported-by: Cyrill Gorcunov <gorcunov@gmail.com>
Signed-off-by: Asias He <asias.hejun@gmail.com>
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
#include <stdlib.h>
#include <assert.h>
-struct cpuid_regs {
- uint32_t eax;
- uint32_t ebx;
- uint32_t ecx;
- uint32_t edx;
-};
-
-static inline void host_cpuid(struct cpuid_regs *regs)
-{
- asm volatile("cpuid"
- : "=a" (regs->eax),
- "=b" (regs->ebx),
- "=c" (regs->ecx),
- "=d" (regs->edx)
- : "0" (regs->eax), "2" (regs->ecx));
-}
-
static struct kvm_cpuid2 *kvm_cpuid__new(unsigned long nent)
{
struct kvm_cpuid2 *self;
#define cpu_feature_enable(reg, feature) \
((reg) | (1 << (feature)))
+struct cpuid_regs {
+ uint32_t eax;
+ uint32_t ebx;
+ uint32_t ecx;
+ uint32_t edx;
+};
+
+static inline void host_cpuid(struct cpuid_regs *regs)
+{
+ asm volatile("cpuid"
+ : "=a" (regs->eax),
+ "=b" (regs->ebx),
+ "=c" (regs->ecx),
+ "=d" (regs->edx)
+ : "0" (regs->eax), "2" (regs->ecx));
+}
+
#endif /* KVM__CPUFEATURE_H */
#include "kvm/kvm.h"
#include "kvm/interrupt.h"
+#include "kvm/cpufeature.h"
#include "kvm/util.h"
#include <linux/kvm.h>
free(self);
}
+static bool kvm__cpu_supports_vm(void)
+{
+ struct cpuid_regs regs;
+
+ regs = (struct cpuid_regs) {
+ .eax = 1,
+ };
+ host_cpuid(®s);
+
+ return regs.ecx & (1 << KVM__X86_FEATURE_VMX);
+}
+
struct kvm *kvm__init(const char *kvm_dev)
{
struct kvm_userspace_memory_region mem;
int mmap_size;
int ret;
+ if (!kvm__cpu_supports_vm())
+ die("Your CPU does not support hardware virtualization");
+
self = kvm__new();
self->sys_fd = open(kvm_dev, O_RDWR);