/dev/kvm

2009 June 11
tags:
by admin

I’ll show the very basic path into the KVM from the User Space. If you loaded ‘kvm’ module, you can find kvm as a device node, usually in /dev/ directory. This character device node provides the way to control Virtualization features such as checking the ability, creating VM, and destroying VM.

While kvm module loading, it registers character device as a misc device.

1
2
3
4
5
6
7
8
9
10
11
12
r = misc_register(&kvm_dev);
 
static struct file_operations kvm_chardev_ops = {
        .unlocked_ioctl = kvm_dev_ioctl,
        .compat_ioctl   = kvm_dev_ioctl,
};
 
static struct miscdevice kvm_dev = {
        KVM_MINOR,
        "kvm",
        &kvm_chardev_ops,
};

As you can see here, they only implemented ioctl call. It means that you can’t find anything useful with general read/write call or redirection on the command line. You always have to write an application to use kvm’s feature.

kvm_dev_ioctl function is implemented as following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
static long kvm_dev_ioctl(struct file *filp,
                          unsigned int ioctl, unsigned long arg)
{
        long r = -EINVAL;
 
        switch (ioctl) {
        case KVM_GET_API_VERSION:
                r = -EINVAL;
                if (arg)
                        goto out;
                r = KVM_API_VERSION;
                break;
        case KVM_CREATE_VM:
                r = -EINVAL;
                if (arg)
                        goto out;
                r = kvm_dev_ioctl_create_vm();
                break;
        case KVM_CHECK_EXTENSION:
                r = kvm_dev_ioctl_check_extension_generic(arg);
                break;
        case KVM_GET_VCPU_MMAP_SIZE:
                r = -EINVAL;
                if (arg)
                        goto out;
                r = PAGE_SIZE;     /* struct kvm_run */
#ifdef CONFIG_X86
                r += PAGE_SIZE;    /* pio data page */
#endif
#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
                r += PAGE_SIZE;    /* coalesced mmio ring page */
#endif
                break;
        case KVM_TRACE_ENABLE:
        case KVM_TRACE_PAUSE:
        case KVM_TRACE_DISABLE:
                r = kvm_trace_ioctl(ioctl, arg);
                break;
        default:
                return kvm_arch_dev_ioctl(filp, ioctl, arg);
        }
out:
        return r;
}

There are 7 predefined commands. If it is not recognized by this function, it goes into kvm_arch_dev_ioctl() function which do additional check with actual hardware module such as kvm_intel and kvm_adm.

KVM_GET_API_VERSION
It returns the version of KVM. In kernel 2.6.30, it is 12 (defined in kvm.h as a KVM_API_VERSION).

KVM_CREATE_VM

KVM_CHECK_EXTENSION

KVM_GET_VCPU_MMAP_SIZE

KVM_TRACE_ENABLE
KVM_TRACE_PAUSE
KVM_TRACE_DISABLE

No comments yet

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS

Easy AdSense by Unreal