linux/virt/kvm/kvm_main.c
<<
>>
Prefs
   1/*
   2 * Kernel-based Virtual Machine driver for Linux
   3 *
   4 * This module enables machines with Intel VT-x extensions to run virtual
   5 * machines without emulation or binary translation.
   6 *
   7 * Copyright (C) 2006 Qumranet, Inc.
   8 *
   9 * Authors:
  10 *   Avi Kivity   <avi@qumranet.com>
  11 *   Yaniv Kamay  <yaniv@qumranet.com>
  12 *
  13 * This work is licensed under the terms of the GNU GPL, version 2.  See
  14 * the COPYING file in the top-level directory.
  15 *
  16 */
  17
  18#include "iodev.h"
  19
  20#include <linux/kvm_host.h>
  21#include <linux/kvm.h>
  22#include <linux/module.h>
  23#include <linux/errno.h>
  24#include <linux/percpu.h>
  25#include <linux/gfp.h>
  26#include <linux/mm.h>
  27#include <linux/miscdevice.h>
  28#include <linux/vmalloc.h>
  29#include <linux/reboot.h>
  30#include <linux/debugfs.h>
  31#include <linux/highmem.h>
  32#include <linux/file.h>
  33#include <linux/sysdev.h>
  34#include <linux/cpu.h>
  35#include <linux/sched.h>
  36#include <linux/cpumask.h>
  37#include <linux/smp.h>
  38#include <linux/anon_inodes.h>
  39#include <linux/profile.h>
  40#include <linux/kvm_para.h>
  41#include <linux/pagemap.h>
  42#include <linux/mman.h>
  43#include <linux/swap.h>
  44
  45#include <asm/processor.h>
  46#include <asm/io.h>
  47#include <asm/uaccess.h>
  48#include <asm/pgtable.h>
  49
  50#ifdef CONFIG_X86
  51#include <asm/msidef.h>
  52#endif
  53
  54#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  55#include "coalesced_mmio.h"
  56#endif
  57
  58#ifdef KVM_CAP_DEVICE_ASSIGNMENT
  59#include <linux/pci.h>
  60#include <linux/interrupt.h>
  61#include "irq.h"
  62#endif
  63
  64MODULE_AUTHOR("Qumranet");
  65MODULE_LICENSE("GPL");
  66
  67static int msi2intx = 1;
  68module_param(msi2intx, bool, 0);
  69
  70DEFINE_SPINLOCK(kvm_lock);
  71LIST_HEAD(vm_list);
  72
  73static cpumask_var_t cpus_hardware_enabled;
  74
  75struct kmem_cache *kvm_vcpu_cache;
  76EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
  77
  78static __read_mostly struct preempt_ops kvm_preempt_ops;
  79
  80struct dentry *kvm_debugfs_dir;
  81
  82static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
  83                           unsigned long arg);
  84
  85static bool kvm_rebooting;
  86
  87#ifdef KVM_CAP_DEVICE_ASSIGNMENT
  88
  89#ifdef CONFIG_X86
  90static void assigned_device_msi_dispatch(struct kvm_assigned_dev_kernel *dev)
  91{
  92        int vcpu_id;
  93        struct kvm_vcpu *vcpu;
  94        struct kvm_ioapic *ioapic = ioapic_irqchip(dev->kvm);
  95        int dest_id = (dev->guest_msi.address_lo & MSI_ADDR_DEST_ID_MASK)
  96                        >> MSI_ADDR_DEST_ID_SHIFT;
  97        int vector = (dev->guest_msi.data & MSI_DATA_VECTOR_MASK)
  98                        >> MSI_DATA_VECTOR_SHIFT;
  99        int dest_mode = test_bit(MSI_ADDR_DEST_MODE_SHIFT,
 100                                (unsigned long *)&dev->guest_msi.address_lo);
 101        int trig_mode = test_bit(MSI_DATA_TRIGGER_SHIFT,
 102                                (unsigned long *)&dev->guest_msi.data);
 103        int delivery_mode = test_bit(MSI_DATA_DELIVERY_MODE_SHIFT,
 104                                (unsigned long *)&dev->guest_msi.data);
 105        u32 deliver_bitmask;
 106
 107        BUG_ON(!ioapic);
 108
 109        deliver_bitmask = kvm_ioapic_get_delivery_bitmask(ioapic,
 110                                dest_id, dest_mode);
 111        /* IOAPIC delivery mode value is the same as MSI here */
 112        switch (delivery_mode) {
 113        case IOAPIC_LOWEST_PRIORITY:
 114                vcpu = kvm_get_lowest_prio_vcpu(ioapic->kvm, vector,
 115                                deliver_bitmask);
 116                if (vcpu != NULL)
 117                        kvm_apic_set_irq(vcpu, vector, trig_mode);
 118                else
 119                        printk(KERN_INFO "kvm: null lowest priority vcpu!\n");
 120                break;
 121        case IOAPIC_FIXED:
 122                for (vcpu_id = 0; deliver_bitmask != 0; vcpu_id++) {
 123                        if (!(deliver_bitmask & (1 << vcpu_id)))
 124                                continue;
 125                        deliver_bitmask &= ~(1 << vcpu_id);
 126                        vcpu = ioapic->kvm->vcpus[vcpu_id];
 127                        if (vcpu)
 128                                kvm_apic_set_irq(vcpu, vector, trig_mode);
 129                }
 130                break;
 131        default:
 132                printk(KERN_INFO "kvm: unsupported MSI delivery mode\n");
 133        }
 134}
 135#else
 136static void assigned_device_msi_dispatch(struct kvm_assigned_dev_kernel *dev) {}
 137#endif
 138
 139static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
 140                                                      int assigned_dev_id)
 141{
 142        struct list_head *ptr;
 143        struct kvm_assigned_dev_kernel *match;
 144
 145        list_for_each(ptr, head) {
 146                match = list_entry(ptr, struct kvm_assigned_dev_kernel, list);
 147                if (match->assigned_dev_id == assigned_dev_id)
 148                        return match;
 149        }
 150        return NULL;
 151}
 152
 153static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
 154{
 155        struct kvm_assigned_dev_kernel *assigned_dev;
 156
 157        assigned_dev = container_of(work, struct kvm_assigned_dev_kernel,
 158                                    interrupt_work);
 159
 160        /* This is taken to safely inject irq inside the guest. When
 161         * the interrupt injection (or the ioapic code) uses a
 162         * finer-grained lock, update this
 163         */
 164        mutex_lock(&assigned_dev->kvm->lock);
 165        if (assigned_dev->irq_requested_type & KVM_ASSIGNED_DEV_GUEST_INTX)
 166                kvm_set_irq(assigned_dev->kvm,
 167                            assigned_dev->irq_source_id,
 168                            assigned_dev->guest_irq, 1);
 169        else if (assigned_dev->irq_requested_type &
 170                                KVM_ASSIGNED_DEV_GUEST_MSI) {
 171                assigned_device_msi_dispatch(assigned_dev);
 172                enable_irq(assigned_dev->host_irq);
 173                assigned_dev->host_irq_disabled = false;
 174        }
 175        mutex_unlock(&assigned_dev->kvm->lock);
 176}
 177
 178static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
 179{
 180        struct kvm_assigned_dev_kernel *assigned_dev =
 181                (struct kvm_assigned_dev_kernel *) dev_id;
 182
 183        schedule_work(&assigned_dev->interrupt_work);
 184
 185        disable_irq_nosync(irq);
 186        assigned_dev->host_irq_disabled = true;
 187
 188        return IRQ_HANDLED;
 189}
 190
 191/* Ack the irq line for an assigned device */
 192static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
 193{
 194        struct kvm_assigned_dev_kernel *dev;
 195
 196        if (kian->gsi == -1)
 197                return;
 198
 199        dev = container_of(kian, struct kvm_assigned_dev_kernel,
 200                           ack_notifier);
 201
 202        kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0);
 203
 204        /* The guest irq may be shared so this ack may be
 205         * from another device.
 206         */
 207        if (dev->host_irq_disabled) {
 208                enable_irq(dev->host_irq);
 209                dev->host_irq_disabled = false;
 210        }
 211}
 212
 213/* The function implicit hold kvm->lock mutex due to cancel_work_sync() */
 214static void kvm_free_assigned_irq(struct kvm *kvm,
 215                                  struct kvm_assigned_dev_kernel *assigned_dev)
 216{
 217        if (!irqchip_in_kernel(kvm))
 218                return;
 219
 220        kvm_unregister_irq_ack_notifier(&assigned_dev->ack_notifier);
 221
 222        if (assigned_dev->irq_source_id != -1)
 223                kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
 224        assigned_dev->irq_source_id = -1;
 225
 226        if (!assigned_dev->irq_requested_type)
 227                return;
 228
 229        /*
 230         * In kvm_free_device_irq, cancel_work_sync return true if:
 231         * 1. work is scheduled, and then cancelled.
 232         * 2. work callback is executed.
 233         *
 234         * The first one ensured that the irq is disabled and no more events
 235         * would happen. But for the second one, the irq may be enabled (e.g.
 236         * for MSI). So we disable irq here to prevent further events.
 237         *
 238         * Notice this maybe result in nested disable if the interrupt type is
 239         * INTx, but it's OK for we are going to free it.
 240         *
 241         * If this function is a part of VM destroy, please ensure that till
 242         * now, the kvm state is still legal for probably we also have to wait
 243         * interrupt_work done.
 244         */
 245        disable_irq_nosync(assigned_dev->host_irq);
 246        cancel_work_sync(&assigned_dev->interrupt_work);
 247
 248        free_irq(assigned_dev->host_irq, (void *)assigned_dev);
 249
 250        if (assigned_dev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)
 251                pci_disable_msi(assigned_dev->dev);
 252
 253        assigned_dev->irq_requested_type = 0;
 254}
 255
 256
 257static void kvm_free_assigned_device(struct kvm *kvm,
 258                                     struct kvm_assigned_dev_kernel
 259                                     *assigned_dev)
 260{
 261        kvm_free_assigned_irq(kvm, assigned_dev);
 262
 263        pci_reset_function(assigned_dev->dev);
 264
 265        pci_release_regions(assigned_dev->dev);
 266        pci_disable_device(assigned_dev->dev);
 267        pci_dev_put(assigned_dev->dev);
 268
 269        list_del(&assigned_dev->list);
 270        kfree(assigned_dev);
 271}
 272
 273void kvm_free_all_assigned_devices(struct kvm *kvm)
 274{
 275        struct list_head *ptr, *ptr2;
 276        struct kvm_assigned_dev_kernel *assigned_dev;
 277
 278        list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
 279                assigned_dev = list_entry(ptr,
 280                                          struct kvm_assigned_dev_kernel,
 281                                          list);
 282
 283                kvm_free_assigned_device(kvm, assigned_dev);
 284        }
 285}
 286
 287static int assigned_device_update_intx(struct kvm *kvm,
 288                        struct kvm_assigned_dev_kernel *adev,
 289                        struct kvm_assigned_irq *airq)
 290{
 291        adev->guest_irq = airq->guest_irq;
 292        adev->ack_notifier.gsi = airq->guest_irq;
 293
 294        if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_INTX)
 295                return 0;
 296
 297        if (irqchip_in_kernel(kvm)) {
 298                if (!msi2intx &&
 299                    (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)) {
 300                        free_irq(adev->host_irq, (void *)adev);
 301                        pci_disable_msi(adev->dev);
 302                }
 303
 304                if (!capable(CAP_SYS_RAWIO))
 305                        return -EPERM;
 306
 307                if (airq->host_irq)
 308                        adev->host_irq = airq->host_irq;
 309                else
 310                        adev->host_irq = adev->dev->irq;
 311
 312                /* Even though this is PCI, we don't want to use shared
 313                 * interrupts. Sharing host devices with guest-assigned devices
 314                 * on the same interrupt line is not a happy situation: there
 315                 * are going to be long delays in accepting, acking, etc.
 316                 */
 317                if (request_irq(adev->host_irq, kvm_assigned_dev_intr,
 318                                0, "kvm_assigned_intx_device", (void *)adev))
 319                        return -EIO;
 320        }
 321
 322        adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_INTX |
 323                                   KVM_ASSIGNED_DEV_HOST_INTX;
 324        return 0;
 325}
 326
 327#ifdef CONFIG_X86
 328static int assigned_device_update_msi(struct kvm *kvm,
 329                        struct kvm_assigned_dev_kernel *adev,
 330                        struct kvm_assigned_irq *airq)
 331{
 332        int r;
 333
 334        if (airq->flags & KVM_DEV_IRQ_ASSIGN_ENABLE_MSI) {
 335                /* x86 don't care upper address of guest msi message addr */
 336                adev->irq_requested_type |= KVM_ASSIGNED_DEV_GUEST_MSI;
 337                adev->irq_requested_type &= ~KVM_ASSIGNED_DEV_GUEST_INTX;
 338                adev->guest_msi.address_lo = airq->guest_msi.addr_lo;
 339                adev->guest_msi.data = airq->guest_msi.data;
 340                adev->ack_notifier.gsi = -1;
 341        } else if (msi2intx) {
 342                adev->irq_requested_type |= KVM_ASSIGNED_DEV_GUEST_INTX;
 343                adev->irq_requested_type &= ~KVM_ASSIGNED_DEV_GUEST_MSI;
 344                adev->guest_irq = airq->guest_irq;
 345                adev->ack_notifier.gsi = airq->guest_irq;
 346        }
 347
 348        if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)
 349                return 0;
 350
 351        if (irqchip_in_kernel(kvm)) {
 352                if (!msi2intx) {
 353                        if (adev->irq_requested_type &
 354                                        KVM_ASSIGNED_DEV_HOST_INTX)
 355                                free_irq(adev->host_irq, (void *)adev);
 356
 357                        r = pci_enable_msi(adev->dev);
 358                        if (r)
 359                                return r;
 360                }
 361
 362                adev->host_irq = adev->dev->irq;
 363                if (request_irq(adev->host_irq, kvm_assigned_dev_intr, 0,
 364                                "kvm_assigned_msi_device", (void *)adev))
 365                        return -EIO;
 366        }
 367
 368        if (!msi2intx)
 369                adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_MSI;
 370
 371        adev->irq_requested_type |= KVM_ASSIGNED_DEV_HOST_MSI;
 372        return 0;
 373}
 374#endif
 375
 376static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
 377                                   struct kvm_assigned_irq
 378                                   *assigned_irq)
 379{
 380        int r = 0;
 381        struct kvm_assigned_dev_kernel *match;
 382
 383        mutex_lock(&kvm->lock);
 384
 385        match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
 386                                      assigned_irq->assigned_dev_id);
 387        if (!match) {
 388                mutex_unlock(&kvm->lock);
 389                return -EINVAL;
 390        }
 391
 392        if (!match->irq_requested_type) {
 393                INIT_WORK(&match->interrupt_work,
 394                                kvm_assigned_dev_interrupt_work_handler);
 395                if (irqchip_in_kernel(kvm)) {
 396                        /* Register ack nofitier */
 397                        match->ack_notifier.gsi = -1;
 398                        match->ack_notifier.irq_acked =
 399                                        kvm_assigned_dev_ack_irq;
 400                        kvm_register_irq_ack_notifier(kvm,
 401                                        &match->ack_notifier);
 402
 403                        /* Request IRQ source ID */
 404                        r = kvm_request_irq_source_id(kvm);
 405                        if (r < 0)
 406                                goto out_release;
 407                        else
 408                                match->irq_source_id = r;
 409
 410#ifdef CONFIG_X86
 411                        /* Determine host device irq type, we can know the
 412                         * result from dev->msi_enabled */
 413                        if (msi2intx)
 414                                pci_enable_msi(match->dev);
 415#endif
 416                }
 417        }
 418
 419        if ((!msi2intx &&
 420             (assigned_irq->flags & KVM_DEV_IRQ_ASSIGN_ENABLE_MSI)) ||
 421            (msi2intx && match->dev->msi_enabled)) {
 422#ifdef CONFIG_X86
 423                r = assigned_device_update_msi(kvm, match, assigned_irq);
 424                if (r) {
 425                        printk(KERN_WARNING "kvm: failed to enable "
 426                                        "MSI device!\n");
 427                        goto out_release;
 428                }
 429#else
 430                r = -ENOTTY;
 431#endif
 432        } else if (assigned_irq->host_irq == 0 && match->dev->irq == 0) {
 433                /* Host device IRQ 0 means don't support INTx */
 434                if (!msi2intx) {
 435                        printk(KERN_WARNING
 436                               "kvm: wait device to enable MSI!\n");
 437                        r = 0;
 438                } else {
 439                        printk(KERN_WARNING
 440                               "kvm: failed to enable MSI device!\n");
 441                        r = -ENOTTY;
 442                        goto out_release;
 443                }
 444        } else {
 445                /* Non-sharing INTx mode */
 446                r = assigned_device_update_intx(kvm, match, assigned_irq);
 447                if (r) {
 448                        printk(KERN_WARNING "kvm: failed to enable "
 449                                        "INTx device!\n");
 450                        goto out_release;
 451                }
 452        }
 453
 454        mutex_unlock(&kvm->lock);
 455        return r;
 456out_release:
 457        mutex_unlock(&kvm->lock);
 458        kvm_free_assigned_device(kvm, match);
 459        return r;
 460}
 461
 462static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
 463                                      struct kvm_assigned_pci_dev *assigned_dev)
 464{
 465        int r = 0;
 466        struct kvm_assigned_dev_kernel *match;
 467        struct pci_dev *dev;
 468
 469        down_read(&kvm->slots_lock);
 470        mutex_lock(&kvm->lock);
 471
 472        match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
 473                                      assigned_dev->assigned_dev_id);
 474        if (match) {
 475                /* device already assigned */
 476                r = -EINVAL;
 477                goto out;
 478        }
 479
 480        match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
 481        if (match == NULL) {
 482                printk(KERN_INFO "%s: Couldn't allocate memory\n",
 483                       __func__);
 484                r = -ENOMEM;
 485                goto out;
 486        }
 487        dev = pci_get_bus_and_slot(assigned_dev->busnr,
 488                                   assigned_dev->devfn);
 489        if (!dev) {
 490                printk(KERN_INFO "%s: host device not found\n", __func__);
 491                r = -EINVAL;
 492                goto out_free;
 493        }
 494        if (pci_enable_device(dev)) {
 495                printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
 496                r = -EBUSY;
 497                goto out_put;
 498        }
 499        r = pci_request_regions(dev, "kvm_assigned_device");
 500        if (r) {
 501                printk(KERN_INFO "%s: Could not get access to device regions\n",
 502                       __func__);
 503                goto out_disable;
 504        }
 505
 506        pci_reset_function(dev);
 507
 508        match->assigned_dev_id = assigned_dev->assigned_dev_id;
 509        match->host_busnr = assigned_dev->busnr;
 510        match->host_devfn = assigned_dev->devfn;
 511        match->flags = assigned_dev->flags;
 512        match->dev = dev;
 513        match->irq_source_id = -1;
 514        match->kvm = kvm;
 515
 516        list_add(&match->list, &kvm->arch.assigned_dev_head);
 517
 518        if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
 519                if (!kvm->arch.iommu_domain) {
 520                        r = kvm_iommu_map_guest(kvm);
 521                        if (r)
 522                                goto out_list_del;
 523                }
 524                r = kvm_assign_device(kvm, match);
 525                if (r)
 526                        goto out_list_del;
 527        }
 528
 529out:
 530        mutex_unlock(&kvm->lock);
 531        up_read(&kvm->slots_lock);
 532        return r;
 533out_list_del:
 534        list_del(&match->list);
 535        pci_release_regions(dev);
 536out_disable:
 537        pci_disable_device(dev);
 538out_put:
 539        pci_dev_put(dev);
 540out_free:
 541        kfree(match);
 542        mutex_unlock(&kvm->lock);
 543        up_read(&kvm->slots_lock);
 544        return r;
 545}
 546#endif
 547
 548#ifdef KVM_CAP_DEVICE_DEASSIGNMENT
 549static int kvm_vm_ioctl_deassign_device(struct kvm *kvm,
 550                struct kvm_assigned_pci_dev *assigned_dev)
 551{
 552        int r = 0;
 553        struct kvm_assigned_dev_kernel *match;
 554
 555        mutex_lock(&kvm->lock);
 556
 557        match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
 558                                      assigned_dev->assigned_dev_id);
 559        if (!match) {
 560                printk(KERN_INFO "%s: device hasn't been assigned before, "
 561                  "so cannot be deassigned\n", __func__);
 562                r = -EINVAL;
 563                goto out;
 564        }
 565
 566        if (match->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU)
 567                kvm_deassign_device(kvm, match);
 568
 569        kvm_free_assigned_device(kvm, match);
 570
 571out:
 572        mutex_unlock(&kvm->lock);
 573        return r;
 574}
 575#endif
 576
 577static inline int valid_vcpu(int n)
 578{
 579        return likely(n >= 0 && n < KVM_MAX_VCPUS);
 580}
 581
 582inline int kvm_is_mmio_pfn(pfn_t pfn)
 583{
 584        if (pfn_valid(pfn)) {
 585                struct page *page = compound_head(pfn_to_page(pfn));
 586                return PageReserved(page);
 587        }
 588
 589        return true;
 590}
 591
 592/*
 593 * Switches to specified vcpu, until a matching vcpu_put()
 594 */
 595void vcpu_load(struct kvm_vcpu *vcpu)
 596{
 597        int cpu;
 598
 599        mutex_lock(&vcpu->mutex);
 600        cpu = get_cpu();
 601        preempt_notifier_register(&vcpu->preempt_notifier);
 602        kvm_arch_vcpu_load(vcpu, cpu);
 603        put_cpu();
 604}
 605
 606void vcpu_put(struct kvm_vcpu *vcpu)
 607{
 608        preempt_disable();
 609        kvm_arch_vcpu_put(vcpu);
 610        preempt_notifier_unregister(&vcpu->preempt_notifier);
 611        preempt_enable();
 612        mutex_unlock(&vcpu->mutex);
 613}
 614
 615static void ack_flush(void *_completed)
 616{
 617}
 618
 619static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
 620{
 621        int i, cpu, me;
 622        cpumask_var_t cpus;
 623        bool called = true;
 624        struct kvm_vcpu *vcpu;
 625
 626        if (alloc_cpumask_var(&cpus, GFP_ATOMIC))
 627                cpumask_clear(cpus);
 628
 629        me = get_cpu();
 630        for (i = 0; i < KVM_MAX_VCPUS; ++i) {
 631                vcpu = kvm->vcpus[i];
 632                if (!vcpu)
 633                        continue;
 634                if (test_and_set_bit(req, &vcpu->requests))
 635                        continue;
 636                cpu = vcpu->cpu;
 637                if (cpus != NULL && cpu != -1 && cpu != me)
 638                        cpumask_set_cpu(cpu, cpus);
 639        }
 640        if (unlikely(cpus == NULL))
 641                smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
 642        else if (!cpumask_empty(cpus))
 643                smp_call_function_many(cpus, ack_flush, NULL, 1);
 644        else
 645                called = false;
 646        put_cpu();
 647        free_cpumask_var(cpus);
 648        return called;
 649}
 650
 651void kvm_flush_remote_tlbs(struct kvm *kvm)
 652{
 653        if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
 654                ++kvm->stat.remote_tlb_flush;
 655}
 656
 657void kvm_reload_remote_mmus(struct kvm *kvm)
 658{
 659        make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
 660}
 661
 662int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
 663{
 664        struct page *page;
 665        int r;
 666
 667        mutex_init(&vcpu->mutex);
 668        vcpu->cpu = -1;
 669        vcpu->kvm = kvm;
 670        vcpu->vcpu_id = id;
 671        init_waitqueue_head(&vcpu->wq);
 672
 673        page = alloc_page(GFP_KERNEL | __GFP_ZERO);
 674        if (!page) {
 675                r = -ENOMEM;
 676                goto fail;
 677        }
 678        vcpu->run = page_address(page);
 679
 680        r = kvm_arch_vcpu_init(vcpu);
 681        if (r < 0)
 682                goto fail_free_run;
 683        return 0;
 684
 685fail_free_run:
 686        free_page((unsigned long)vcpu->run);
 687fail:
 688        return r;
 689}
 690EXPORT_SYMBOL_GPL(kvm_vcpu_init);
 691
 692void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
 693{
 694        kvm_arch_vcpu_uninit(vcpu);
 695        free_page((unsigned long)vcpu->run);
 696}
 697EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
 698
 699#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
 700static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
 701{
 702        return container_of(mn, struct kvm, mmu_notifier);
 703}
 704
 705static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
 706                                             struct mm_struct *mm,
 707                                             unsigned long address)
 708{
 709        struct kvm *kvm = mmu_notifier_to_kvm(mn);
 710        int need_tlb_flush;
 711
 712        /*
 713         * When ->invalidate_page runs, the linux pte has been zapped
 714         * already but the page is still allocated until
 715         * ->invalidate_page returns. So if we increase the sequence
 716         * here the kvm page fault will notice if the spte can't be
 717         * established because the page is going to be freed. If
 718         * instead the kvm page fault establishes the spte before
 719         * ->invalidate_page runs, kvm_unmap_hva will release it
 720         * before returning.
 721         *
 722         * The sequence increase only need to be seen at spin_unlock
 723         * time, and not at spin_lock time.
 724         *
 725         * Increasing the sequence after the spin_unlock would be
 726         * unsafe because the kvm page fault could then establish the
 727         * pte after kvm_unmap_hva returned, without noticing the page
 728         * is going to be freed.
 729         */
 730        spin_lock(&kvm->mmu_lock);
 731        kvm->mmu_notifier_seq++;
 732        need_tlb_flush = kvm_unmap_hva(kvm, address);
 733        spin_unlock(&kvm->mmu_lock);
 734
 735        /* we've to flush the tlb before the pages can be freed */
 736        if (need_tlb_flush)
 737                kvm_flush_remote_tlbs(kvm);
 738
 739}
 740
 741static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
 742                                                    struct mm_struct *mm,
 743                                                    unsigned long start,
 744                                                    unsigned long end)
 745{
 746        struct kvm *kvm = mmu_notifier_to_kvm(mn);
 747        int need_tlb_flush = 0;
 748
 749        spin_lock(&kvm->mmu_lock);
 750        /*
 751         * The count increase must become visible at unlock time as no
 752         * spte can be established without taking the mmu_lock and
 753         * count is also read inside the mmu_lock critical section.
 754         */
 755        kvm->mmu_notifier_count++;
 756        for (; start < end; start += PAGE_SIZE)
 757                need_tlb_flush |= kvm_unmap_hva(kvm, start);
 758        spin_unlock(&kvm->mmu_lock);
 759
 760        /* we've to flush the tlb before the pages can be freed */
 761        if (need_tlb_flush)
 762                kvm_flush_remote_tlbs(kvm);
 763}
 764
 765static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
 766                                                  struct mm_struct *mm,
 767                                                  unsigned long start,
 768                                                  unsigned long end)
 769{
 770        struct kvm *kvm = mmu_notifier_to_kvm(mn);
 771
 772        spin_lock(&kvm->mmu_lock);
 773        /*
 774         * This sequence increase will notify the kvm page fault that
 775         * the page that is going to be mapped in the spte could have
 776         * been freed.
 777         */
 778        kvm->mmu_notifier_seq++;
 779        /*
 780         * The above sequence increase must be visible before the
 781         * below count decrease but both values are read by the kvm
 782         * page fault under mmu_lock spinlock so we don't need to add
 783         * a smb_wmb() here in between the two.
 784         */
 785        kvm->mmu_notifier_count--;
 786        spin_unlock(&kvm->mmu_lock);
 787
 788        BUG_ON(kvm->mmu_notifier_count < 0);
 789}
 790
 791static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
 792                                              struct mm_struct *mm,
 793                                              unsigned long address)
 794{
 795        struct kvm *kvm = mmu_notifier_to_kvm(mn);
 796        int young;
 797
 798        spin_lock(&kvm->mmu_lock);
 799        young = kvm_age_hva(kvm, address);
 800        spin_unlock(&kvm->mmu_lock);
 801
 802        if (young)
 803                kvm_flush_remote_tlbs(kvm);
 804
 805        return young;
 806}
 807
 808static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
 809                                     struct mm_struct *mm)
 810{
 811        struct kvm *kvm = mmu_notifier_to_kvm(mn);
 812        kvm_arch_flush_shadow(kvm);
 813}
 814
 815static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
 816        .invalidate_page        = kvm_mmu_notifier_invalidate_page,
 817        .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
 818        .invalidate_range_end   = kvm_mmu_notifier_invalidate_range_end,
 819        .clear_flush_young      = kvm_mmu_notifier_clear_flush_young,
 820        .release                = kvm_mmu_notifier_release,
 821};
 822#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
 823
 824static struct kvm *kvm_create_vm(void)
 825{
 826        struct kvm *kvm = kvm_arch_create_vm();
 827#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
 828        struct page *page;
 829#endif
 830
 831        if (IS_ERR(kvm))
 832                goto out;
 833#ifdef CONFIG_HAVE_KVM_IRQCHIP
 834        INIT_HLIST_HEAD(&kvm->mask_notifier_list);
 835#endif
 836
 837#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
 838        page = alloc_page(GFP_KERNEL | __GFP_ZERO);
 839        if (!page) {
 840                kfree(kvm);
 841                return ERR_PTR(-ENOMEM);
 842        }
 843        kvm->coalesced_mmio_ring =
 844                        (struct kvm_coalesced_mmio_ring *)page_address(page);
 845#endif
 846
 847#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
 848        {
 849                int err;
 850                kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
 851                err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
 852                if (err) {
 853#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
 854                        put_page(page);
 855#endif
 856                        kfree(kvm);
 857                        return ERR_PTR(err);
 858                }
 859        }
 860#endif
 861
 862        kvm->mm = current->mm;
 863        atomic_inc(&kvm->mm->mm_count);
 864        spin_lock_init(&kvm->mmu_lock);
 865        kvm_io_bus_init(&kvm->pio_bus);
 866        mutex_init(&kvm->lock);
 867        kvm_io_bus_init(&kvm->mmio_bus);
 868        init_rwsem(&kvm->slots_lock);
 869        atomic_set(&kvm->users_count, 1);
 870        spin_lock(&kvm_lock);
 871        list_add(&kvm->vm_list, &vm_list);
 872        spin_unlock(&kvm_lock);
 873#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
 874        kvm_coalesced_mmio_init(kvm);
 875#endif
 876out:
 877        return kvm;
 878}
 879
 880/*
 881 * Free any memory in @free but not in @dont.
 882 */
 883static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
 884                                  struct kvm_memory_slot *dont)
 885{
 886        if (!dont || free->rmap != dont->rmap)
 887                vfree(free->rmap);
 888
 889        if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
 890                vfree(free->dirty_bitmap);
 891
 892        if (!dont || free->lpage_info != dont->lpage_info)
 893                vfree(free->lpage_info);
 894
 895        free->npages = 0;
 896        free->dirty_bitmap = NULL;
 897        free->rmap = NULL;
 898        free->lpage_info = NULL;
 899}
 900
 901void kvm_free_physmem(struct kvm *kvm)
 902{
 903        int i;
 904
 905        for (i = 0; i < kvm->nmemslots; ++i)
 906                kvm_free_physmem_slot(&kvm->memslots[i], NULL);
 907}
 908
 909static void kvm_destroy_vm(struct kvm *kvm)
 910{
 911        struct mm_struct *mm = kvm->mm;
 912
 913        kvm_arch_sync_events(kvm);
 914        spin_lock(&kvm_lock);
 915        list_del(&kvm->vm_list);
 916        spin_unlock(&kvm_lock);
 917        kvm_io_bus_destroy(&kvm->pio_bus);
 918        kvm_io_bus_destroy(&kvm->mmio_bus);
 919#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
 920        if (kvm->coalesced_mmio_ring != NULL)
 921                free_page((unsigned long)kvm->coalesced_mmio_ring);
 922#endif
 923#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
 924        mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
 925#endif
 926        kvm_arch_destroy_vm(kvm);
 927        mmdrop(mm);
 928}
 929
 930void kvm_get_kvm(struct kvm *kvm)
 931{
 932        atomic_inc(&kvm->users_count);
 933}
 934EXPORT_SYMBOL_GPL(kvm_get_kvm);
 935
 936void kvm_put_kvm(struct kvm *kvm)
 937{
 938        if (atomic_dec_and_test(&kvm->users_count))
 939                kvm_destroy_vm(kvm);
 940}
 941EXPORT_SYMBOL_GPL(kvm_put_kvm);
 942
 943
 944static int kvm_vm_release(struct inode *inode, struct file *filp)
 945{
 946        struct kvm *kvm = filp->private_data;
 947
 948        kvm_put_kvm(kvm);
 949        return 0;
 950}
 951
 952/*
 953 * Allocate some memory and give it an address in the guest physical address
 954 * space.
 955 *
 956 * Discontiguous memory is allowed, mostly for framebuffers.
 957 *
 958 * Must be called holding mmap_sem for write.
 959 */
 960int __kvm_set_memory_region(struct kvm *kvm,
 961                            struct kvm_userspace_memory_region *mem,
 962                            int user_alloc)
 963{
 964        int r;
 965        gfn_t base_gfn;
 966        unsigned long npages;
 967        int largepages;
 968        unsigned long i;
 969        struct kvm_memory_slot *memslot;
 970        struct kvm_memory_slot old, new;
 971
 972        r = -EINVAL;
 973        /* General sanity checks */
 974        if (mem->memory_size & (PAGE_SIZE - 1))
 975                goto out;
 976        if (mem->guest_phys_addr & (PAGE_SIZE - 1))
 977                goto out;
 978        if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
 979                goto out;
 980        if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
 981                goto out;
 982        if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
 983                goto out;
 984
 985        memslot = &kvm->memslots[mem->slot];
 986        base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
 987        npages = mem->memory_size >> PAGE_SHIFT;
 988
 989        if (!npages)
 990                mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
 991
 992        new = old = *memslot;
 993
 994        new.base_gfn = base_gfn;
 995        new.npages = npages;
 996        new.flags = mem->flags;
 997
 998        /* Disallow changing a memory slot's size. */
 999        r = -EINVAL;
1000        if (npages && old.npages && npages != old.npages)
1001                goto out_free;
1002
1003        /* Check for overlaps */
1004        r = -EEXIST;
1005        for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1006                struct kvm_memory_slot *s = &kvm->memslots[i];
1007
1008                if (s == memslot || !s->npages)
1009                        continue;
1010                if (!((base_gfn + npages <= s->base_gfn) ||
1011                      (base_gfn >= s->base_gfn + s->npages)))
1012                        goto out_free;
1013        }
1014
1015        /* Free page dirty bitmap if unneeded */
1016        if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
1017                new.dirty_bitmap = NULL;
1018
1019        r = -ENOMEM;
1020
1021        /* Allocate if a slot is being created */
1022#ifndef CONFIG_S390
1023        if (npages && !new.rmap) {
1024                new.rmap = vmalloc(npages * sizeof(struct page *));
1025
1026                if (!new.rmap)
1027                        goto out_free;
1028
1029                memset(new.rmap, 0, npages * sizeof(*new.rmap));
1030
1031                new.user_alloc = user_alloc;
1032                /*
1033                 * hva_to_rmmap() serialzies with the mmu_lock and to be
1034                 * safe it has to ignore memslots with !user_alloc &&
1035                 * !userspace_addr.
1036                 */
1037                if (user_alloc)
1038                        new.userspace_addr = mem->userspace_addr;
1039                else
1040                        new.userspace_addr = 0;
1041        }
1042        if (npages && !new.lpage_info) {
1043                largepages = 1 + (base_gfn + npages - 1) / KVM_PAGES_PER_HPAGE;
1044                largepages -= base_gfn / KVM_PAGES_PER_HPAGE;
1045
1046                new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
1047
1048                if (!new.lpage_info)
1049                        goto out_free;
1050
1051                memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
1052
1053                if (base_gfn % KVM_PAGES_PER_HPAGE)
1054                        new.lpage_info[0].write_count = 1;
1055                if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
1056                        new.lpage_info[largepages-1].write_count = 1;
1057        }
1058
1059        /* Allocate page dirty bitmap if needed */
1060        if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
1061                unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
1062
1063                new.dirty_bitmap = vmalloc(dirty_bytes);
1064                if (!new.dirty_bitmap)
1065                        goto out_free;
1066                memset(new.dirty_bitmap, 0, dirty_bytes);
1067        }
1068#endif /* not defined CONFIG_S390 */
1069
1070        if (!npages)
1071                kvm_arch_flush_shadow(kvm);
1072
1073        spin_lock(&kvm->mmu_lock);
1074        if (mem->slot >= kvm->nmemslots)
1075                kvm->nmemslots = mem->slot + 1;
1076
1077        *memslot = new;
1078        spin_unlock(&kvm->mmu_lock);
1079
1080        r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
1081        if (r) {
1082                spin_lock(&kvm->mmu_lock);
1083                *memslot = old;
1084                spin_unlock(&kvm->mmu_lock);
1085                goto out_free;
1086        }
1087
1088        kvm_free_physmem_slot(&old, npages ? &new : NULL);
1089        /* Slot deletion case: we have to update the current slot */
1090        if (!npages)
1091                *memslot = old;
1092#ifdef CONFIG_DMAR
1093        /* map the pages in iommu page table */
1094        r = kvm_iommu_map_pages(kvm, base_gfn, npages);
1095        if (r)
1096                goto out;
1097#endif
1098        return 0;
1099
1100out_free:
1101        kvm_free_physmem_slot(&new, &old);
1102out:
1103        return r;
1104
1105}
1106EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
1107
1108int kvm_set_memory_region(struct kvm *kvm,
1109                          struct kvm_userspace_memory_region *mem,
1110                          int user_alloc)
1111{
1112        int r;
1113
1114        down_write(&kvm->slots_lock);
1115        r = __kvm_set_memory_region(kvm, mem, user_alloc);
1116        up_write(&kvm->slots_lock);
1117        return r;
1118}
1119EXPORT_SYMBOL_GPL(kvm_set_memory_region);
1120
1121int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
1122                                   struct
1123                                   kvm_userspace_memory_region *mem,
1124                                   int user_alloc)
1125{
1126        if (mem->slot >= KVM_MEMORY_SLOTS)
1127                return -EINVAL;
1128        return kvm_set_memory_region(kvm, mem, user_alloc);
1129}
1130
1131int kvm_get_dirty_log(struct kvm *kvm,
1132                        struct kvm_dirty_log *log, int *is_dirty)
1133{
1134        struct kvm_memory_slot *memslot;
1135        int r, i;
1136        int n;
1137        unsigned long any = 0;
1138
1139        r = -EINVAL;
1140        if (log->slot >= KVM_MEMORY_SLOTS)
1141                goto out;
1142
1143        memslot = &kvm->memslots[log->slot];
1144        r = -ENOENT;
1145        if (!memslot->dirty_bitmap)
1146                goto out;
1147
1148        n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1149
1150        for (i = 0; !any && i < n/sizeof(long); ++i)
1151                any = memslot->dirty_bitmap[i];
1152
1153        r = -EFAULT;
1154        if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1155                goto out;
1156
1157        if (any)
1158                *is_dirty = 1;
1159
1160        r = 0;
1161out:
1162        return r;
1163}
1164
1165int is_error_page(struct page *page)
1166{
1167        return page == bad_page;
1168}
1169EXPORT_SYMBOL_GPL(is_error_page);
1170
1171int is_error_pfn(pfn_t pfn)
1172{
1173        return pfn == bad_pfn;
1174}
1175EXPORT_SYMBOL_GPL(is_error_pfn);
1176
1177static inline unsigned long bad_hva(void)
1178{
1179        return PAGE_OFFSET;
1180}
1181
1182int kvm_is_error_hva(unsigned long addr)
1183{
1184        return addr == bad_hva();
1185}
1186EXPORT_SYMBOL_GPL(kvm_is_error_hva);
1187
1188struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
1189{
1190        int i;
1191
1192        for (i = 0; i < kvm->nmemslots; ++i) {
1193                struct kvm_memory_slot *memslot = &kvm->memslots[i];
1194
1195                if (gfn >= memslot->base_gfn
1196                    && gfn < memslot->base_gfn + memslot->npages)
1197                        return memslot;
1198        }
1199        return NULL;
1200}
1201EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
1202
1203struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1204{
1205        gfn = unalias_gfn(kvm, gfn);
1206        return gfn_to_memslot_unaliased(kvm, gfn);
1207}
1208
1209int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1210{
1211        int i;
1212
1213        gfn = unalias_gfn(kvm, gfn);
1214        for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1215                struct kvm_memory_slot *memslot = &kvm->memslots[i];
1216
1217                if (gfn >= memslot->base_gfn
1218                    && gfn < memslot->base_gfn + memslot->npages)
1219                        return 1;
1220        }
1221        return 0;
1222}
1223EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1224
1225unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1226{
1227        struct kvm_memory_slot *slot;
1228
1229        gfn = unalias_gfn(kvm, gfn);
1230        slot = gfn_to_memslot_unaliased(kvm, gfn);
1231        if (!slot)
1232                return bad_hva();
1233        return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
1234}
1235EXPORT_SYMBOL_GPL(gfn_to_hva);
1236
1237pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
1238{
1239        struct page *page[1];
1240        unsigned long addr;
1241        int npages;
1242        pfn_t pfn;
1243
1244        might_sleep();
1245
1246        addr = gfn_to_hva(kvm, gfn);
1247        if (kvm_is_error_hva(addr)) {
1248                get_page(bad_page);
1249                return page_to_pfn(bad_page);
1250        }
1251
1252        npages = get_user_pages_fast(addr, 1, 1, page);
1253
1254        if (unlikely(npages != 1)) {
1255                struct vm_area_struct *vma;
1256
1257                down_read(&current->mm->mmap_sem);
1258                vma = find_vma(current->mm, addr);
1259
1260                if (vma == NULL || addr < vma->vm_start ||
1261                    !(vma->vm_flags & VM_PFNMAP)) {
1262                        up_read(&current->mm->mmap_sem);
1263                        get_page(bad_page);
1264                        return page_to_pfn(bad_page);
1265                }
1266
1267                pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1268                up_read(&current->mm->mmap_sem);
1269                BUG_ON(!kvm_is_mmio_pfn(pfn));
1270        } else
1271                pfn = page_to_pfn(page[0]);
1272
1273        return pfn;
1274}
1275
1276EXPORT_SYMBOL_GPL(gfn_to_pfn);
1277
1278struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1279{
1280        pfn_t pfn;
1281
1282        pfn = gfn_to_pfn(kvm, gfn);
1283        if (!kvm_is_mmio_pfn(pfn))
1284                return pfn_to_page(pfn);
1285
1286        WARN_ON(kvm_is_mmio_pfn(pfn));
1287
1288        get_page(bad_page);
1289        return bad_page;
1290}
1291
1292EXPORT_SYMBOL_GPL(gfn_to_page);
1293
1294void kvm_release_page_clean(struct page *page)
1295{
1296        kvm_release_pfn_clean(page_to_pfn(page));
1297}
1298EXPORT_SYMBOL_GPL(kvm_release_page_clean);
1299
1300void kvm_release_pfn_clean(pfn_t pfn)
1301{
1302        if (!kvm_is_mmio_pfn(pfn))
1303                put_page(pfn_to_page(pfn));
1304}
1305EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
1306
1307void kvm_release_page_dirty(struct page *page)
1308{
1309        kvm_release_pfn_dirty(page_to_pfn(page));
1310}
1311EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
1312
1313void kvm_release_pfn_dirty(pfn_t pfn)
1314{
1315        kvm_set_pfn_dirty(pfn);
1316        kvm_release_pfn_clean(pfn);
1317}
1318EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
1319
1320void kvm_set_page_dirty(struct page *page)
1321{
1322        kvm_set_pfn_dirty(page_to_pfn(page));
1323}
1324EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
1325
1326void kvm_set_pfn_dirty(pfn_t pfn)
1327{
1328        if (!kvm_is_mmio_pfn(pfn)) {
1329                struct page *page = pfn_to_page(pfn);
1330                if (!PageReserved(page))
1331                        SetPageDirty(page);
1332        }
1333}
1334EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
1335
1336void kvm_set_pfn_accessed(pfn_t pfn)
1337{
1338        if (!kvm_is_mmio_pfn(pfn))
1339                mark_page_accessed(pfn_to_page(pfn));
1340}
1341EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
1342
1343void kvm_get_pfn(pfn_t pfn)
1344{
1345        if (!kvm_is_mmio_pfn(pfn))
1346                get_page(pfn_to_page(pfn));
1347}
1348EXPORT_SYMBOL_GPL(kvm_get_pfn);
1349
1350static int next_segment(unsigned long len, int offset)
1351{
1352        if (len > PAGE_SIZE - offset)
1353                return PAGE_SIZE - offset;
1354        else
1355                return len;
1356}
1357
1358int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1359                        int len)
1360{
1361        int r;
1362        unsigned long addr;
1363
1364        addr = gfn_to_hva(kvm, gfn);
1365        if (kvm_is_error_hva(addr))
1366                return -EFAULT;
1367        r = copy_from_user(data, (void __user *)addr + offset, len);
1368        if (r)
1369                return -EFAULT;
1370        return 0;
1371}
1372EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1373
1374int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1375{
1376        gfn_t gfn = gpa >> PAGE_SHIFT;
1377        int seg;
1378        int offset = offset_in_page(gpa);
1379        int ret;
1380
1381        while ((seg = next_segment(len, offset)) != 0) {
1382                ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1383                if (ret < 0)
1384                        return ret;
1385                offset = 0;
1386                len -= seg;
1387                data += seg;
1388                ++gfn;
1389        }
1390        return 0;
1391}
1392EXPORT_SYMBOL_GPL(kvm_read_guest);
1393
1394int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
1395                          unsigned long len)
1396{
1397        int r;
1398        unsigned long addr;
1399        gfn_t gfn = gpa >> PAGE_SHIFT;
1400        int offset = offset_in_page(gpa);
1401
1402        addr = gfn_to_hva(kvm, gfn);
1403        if (kvm_is_error_hva(addr))
1404                return -EFAULT;
1405        pagefault_disable();
1406        r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
1407        pagefault_enable();
1408        if (r)
1409                return -EFAULT;
1410        return 0;
1411}
1412EXPORT_SYMBOL(kvm_read_guest_atomic);
1413
1414int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1415                         int offset, int len)
1416{
1417        int r;
1418        unsigned long addr;
1419
1420        addr = gfn_to_hva(kvm, gfn);
1421        if (kvm_is_error_hva(addr))
1422                return -EFAULT;
1423        r = copy_to_user((void __user *)addr + offset, data, len);
1424        if (r)
1425                return -EFAULT;
1426        mark_page_dirty(kvm, gfn);
1427        return 0;
1428}
1429EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1430
1431int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1432                    unsigned long len)
1433{
1434        gfn_t gfn = gpa >> PAGE_SHIFT;
1435        int seg;
1436        int offset = offset_in_page(gpa);
1437        int ret;
1438
1439        while ((seg = next_segment(len, offset)) != 0) {
1440                ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1441                if (ret < 0)
1442                        return ret;
1443                offset = 0;
1444                len -= seg;
1445                data += seg;
1446                ++gfn;
1447        }
1448        return 0;
1449}
1450
1451int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1452{
1453        return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
1454}
1455EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1456
1457int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1458{
1459        gfn_t gfn = gpa >> PAGE_SHIFT;
1460        int seg;
1461        int offset = offset_in_page(gpa);
1462        int ret;
1463
1464        while ((seg = next_segment(len, offset)) != 0) {
1465                ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1466                if (ret < 0)
1467                        return ret;
1468                offset = 0;
1469                len -= seg;
1470                ++gfn;
1471        }
1472        return 0;
1473}
1474EXPORT_SYMBOL_GPL(kvm_clear_guest);
1475
1476void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1477{
1478        struct kvm_memory_slot *memslot;
1479
1480        gfn = unalias_gfn(kvm, gfn);
1481        memslot = gfn_to_memslot_unaliased(kvm, gfn);
1482        if (memslot && memslot->dirty_bitmap) {
1483                unsigned long rel_gfn = gfn - memslot->base_gfn;
1484
1485                /* avoid RMW */
1486                if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1487                        set_bit(rel_gfn, memslot->dirty_bitmap);
1488        }
1489}
1490
1491/*
1492 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1493 */
1494void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1495{
1496        DEFINE_WAIT(wait);
1497
1498        for (;;) {
1499                prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
1500
1501                if (kvm_cpu_has_interrupt(vcpu) ||
1502                    kvm_cpu_has_pending_timer(vcpu) ||
1503                    kvm_arch_vcpu_runnable(vcpu)) {
1504                        set_bit(KVM_REQ_UNHALT, &vcpu->requests);
1505                        break;
1506                }
1507                if (signal_pending(current))
1508                        break;
1509
1510                vcpu_put(vcpu);
1511                schedule();
1512                vcpu_load(vcpu);
1513        }
1514
1515        finish_wait(&vcpu->wq, &wait);
1516}
1517
1518void kvm_resched(struct kvm_vcpu *vcpu)
1519{
1520        if (!need_resched())
1521                return;
1522        cond_resched();
1523}
1524EXPORT_SYMBOL_GPL(kvm_resched);
1525
1526static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1527{
1528        struct kvm_vcpu *vcpu = vma->vm_file->private_data;
1529        struct page *page;
1530
1531        if (vmf->pgoff == 0)
1532                page = virt_to_page(vcpu->run);
1533#ifdef CONFIG_X86
1534        else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
1535                page = virt_to_page(vcpu->arch.pio_data);
1536#endif
1537#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1538        else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
1539                page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
1540#endif
1541        else
1542                return VM_FAULT_SIGBUS;
1543        get_page(page);
1544        vmf->page = page;
1545        return 0;
1546}
1547
1548static struct vm_operations_struct kvm_vcpu_vm_ops = {
1549        .fault = kvm_vcpu_fault,
1550};
1551
1552static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1553{
1554        vma->vm_ops = &kvm_vcpu_vm_ops;
1555        return 0;
1556}
1557
1558static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1559{
1560        struct kvm_vcpu *vcpu = filp->private_data;
1561
1562        kvm_put_kvm(vcpu->kvm);
1563        return 0;
1564}
1565
1566static struct file_operations kvm_vcpu_fops = {
1567        .release        = kvm_vcpu_release,
1568        .unlocked_ioctl = kvm_vcpu_ioctl,
1569        .compat_ioctl   = kvm_vcpu_ioctl,
1570        .mmap           = kvm_vcpu_mmap,
1571};
1572
1573/*
1574 * Allocates an inode for the vcpu.
1575 */
1576static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1577{
1578        int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
1579        if (fd < 0)
1580                kvm_put_kvm(vcpu->kvm);
1581        return fd;
1582}
1583
1584/*
1585 * Creates some virtual cpus.  Good luck creating more than one.
1586 */
1587static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1588{
1589        int r;
1590        struct kvm_vcpu *vcpu;
1591
1592        if (!valid_vcpu(n))
1593                return -EINVAL;
1594
1595        vcpu = kvm_arch_vcpu_create(kvm, n);
1596        if (IS_ERR(vcpu))
1597                return PTR_ERR(vcpu);
1598
1599        preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
1600
1601        r = kvm_arch_vcpu_setup(vcpu);
1602        if (r)
1603                return r;
1604
1605        mutex_lock(&kvm->lock);
1606        if (kvm->vcpus[n]) {
1607                r = -EEXIST;
1608                goto vcpu_destroy;
1609        }
1610        kvm->vcpus[n] = vcpu;
1611        mutex_unlock(&kvm->lock);
1612
1613        /* Now it's all set up, let userspace reach it */
1614        kvm_get_kvm(kvm);
1615        r = create_vcpu_fd(vcpu);
1616        if (r < 0)
1617                goto unlink;
1618        return r;
1619
1620unlink:
1621        mutex_lock(&kvm->lock);
1622        kvm->vcpus[n] = NULL;
1623vcpu_destroy:
1624        mutex_unlock(&kvm->lock);
1625        kvm_arch_vcpu_destroy(vcpu);
1626        return r;
1627}
1628
1629static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
1630{
1631        if (sigset) {
1632                sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
1633                vcpu->sigset_active = 1;
1634                vcpu->sigset = *sigset;
1635        } else
1636                vcpu->sigset_active = 0;
1637        return 0;
1638}
1639
1640static long kvm_vcpu_ioctl(struct file *filp,
1641                           unsigned int ioctl, unsigned long arg)
1642{
1643        struct kvm_vcpu *vcpu = filp->private_data;
1644        void __user *argp = (void __user *)arg;
1645        int r;
1646        struct kvm_fpu *fpu = NULL;
1647        struct kvm_sregs *kvm_sregs = NULL;
1648
1649        if (vcpu->kvm->mm != current->mm)
1650                return -EIO;
1651        switch (ioctl) {
1652        case KVM_RUN:
1653                r = -EINVAL;
1654                if (arg)
1655                        goto out;
1656                r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
1657                break;
1658        case KVM_GET_REGS: {
1659                struct kvm_regs *kvm_regs;
1660
1661                r = -ENOMEM;
1662                kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1663                if (!kvm_regs)
1664                        goto out;
1665                r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
1666                if (r)
1667                        goto out_free1;
1668                r = -EFAULT;
1669                if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
1670                        goto out_free1;
1671                r = 0;
1672out_free1:
1673                kfree(kvm_regs);
1674                break;
1675        }
1676        case KVM_SET_REGS: {
1677                struct kvm_regs *kvm_regs;
1678
1679                r = -ENOMEM;
1680                kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
1681                if (!kvm_regs)
1682                        goto out;
1683                r = -EFAULT;
1684                if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
1685                        goto out_free2;
1686                r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
1687                if (r)
1688                        goto out_free2;
1689                r = 0;
1690out_free2:
1691                kfree(kvm_regs);
1692                break;
1693        }
1694        case KVM_GET_SREGS: {
1695                kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1696                r = -ENOMEM;
1697                if (!kvm_sregs)
1698                        goto out;
1699                r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
1700                if (r)
1701                        goto out;
1702                r = -EFAULT;
1703                if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
1704                        goto out;
1705                r = 0;
1706                break;
1707        }
1708        case KVM_SET_SREGS: {
1709                kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
1710                r = -ENOMEM;
1711                if (!kvm_sregs)
1712                        goto out;
1713                r = -EFAULT;
1714                if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
1715                        goto out;
1716                r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
1717                if (r)
1718                        goto out;
1719                r = 0;
1720                break;
1721        }
1722        case KVM_GET_MP_STATE: {
1723                struct kvm_mp_state mp_state;
1724
1725                r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
1726                if (r)
1727                        goto out;
1728                r = -EFAULT;
1729                if (copy_to_user(argp, &mp_state, sizeof mp_state))
1730                        goto out;
1731                r = 0;
1732                break;
1733        }
1734        case KVM_SET_MP_STATE: {
1735                struct kvm_mp_state mp_state;
1736
1737                r = -EFAULT;
1738                if (copy_from_user(&mp_state, argp, sizeof mp_state))
1739                        goto out;
1740                r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
1741                if (r)
1742                        goto out;
1743                r = 0;
1744                break;
1745        }
1746        case KVM_TRANSLATE: {
1747                struct kvm_translation tr;
1748
1749                r = -EFAULT;
1750                if (copy_from_user(&tr, argp, sizeof tr))
1751                        goto out;
1752                r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
1753                if (r)
1754                        goto out;
1755                r = -EFAULT;
1756                if (copy_to_user(argp, &tr, sizeof tr))
1757                        goto out;
1758                r = 0;
1759                break;
1760        }
1761        case KVM_DEBUG_GUEST: {
1762                struct kvm_debug_guest dbg;
1763
1764                r = -EFAULT;
1765                if (copy_from_user(&dbg, argp, sizeof dbg))
1766                        goto out;
1767                r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
1768                if (r)
1769                        goto out;
1770                r = 0;
1771                break;
1772        }
1773        case KVM_SET_SIGNAL_MASK: {
1774                struct kvm_signal_mask __user *sigmask_arg = argp;
1775                struct kvm_signal_mask kvm_sigmask;
1776                sigset_t sigset, *p;
1777
1778                p = NULL;
1779                if (argp) {
1780                        r = -EFAULT;
1781                        if (copy_from_user(&kvm_sigmask, argp,
1782                                           sizeof kvm_sigmask))
1783                                goto out;
1784                        r = -EINVAL;
1785                        if (kvm_sigmask.len != sizeof sigset)
1786                                goto out;
1787                        r = -EFAULT;
1788                        if (copy_from_user(&sigset, sigmask_arg->sigset,
1789                                           sizeof sigset))
1790                                goto out;
1791                        p = &sigset;
1792                }
1793                r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
1794                break;
1795        }
1796        case KVM_GET_FPU: {
1797                fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1798                r = -ENOMEM;
1799                if (!fpu)
1800                        goto out;
1801                r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
1802                if (r)
1803                        goto out;
1804                r = -EFAULT;
1805                if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
1806                        goto out;
1807                r = 0;
1808                break;
1809        }
1810        case KVM_SET_FPU: {
1811                fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
1812                r = -ENOMEM;
1813                if (!fpu)
1814                        goto out;
1815                r = -EFAULT;
1816                if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
1817                        goto out;
1818                r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
1819                if (r)
1820                        goto out;
1821                r = 0;
1822                break;
1823        }
1824        default:
1825                r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
1826        }
1827out:
1828        kfree(fpu);
1829        kfree(kvm_sregs);
1830        return r;
1831}
1832
1833static long kvm_vm_ioctl(struct file *filp,
1834                           unsigned int ioctl, unsigned long arg)
1835{
1836        struct kvm *kvm = filp->private_data;
1837        void __user *argp = (void __user *)arg;
1838        int r;
1839
1840        if (kvm->mm != current->mm)
1841                return -EIO;
1842        switch (ioctl) {
1843        case KVM_CREATE_VCPU:
1844                r = kvm_vm_ioctl_create_vcpu(kvm, arg);
1845                if (r < 0)
1846                        goto out;
1847                break;
1848        case KVM_SET_USER_MEMORY_REGION: {
1849                struct kvm_userspace_memory_region kvm_userspace_mem;
1850
1851                r = -EFAULT;
1852                if (copy_from_user(&kvm_userspace_mem, argp,
1853                                                sizeof kvm_userspace_mem))
1854                        goto out;
1855
1856                r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
1857                if (r)
1858                        goto out;
1859                break;
1860        }
1861        case KVM_GET_DIRTY_LOG: {
1862                struct kvm_dirty_log log;
1863
1864                r = -EFAULT;
1865                if (copy_from_user(&log, argp, sizeof log))
1866                        goto out;
1867                r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
1868                if (r)
1869                        goto out;
1870                break;
1871        }
1872#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
1873        case KVM_REGISTER_COALESCED_MMIO: {
1874                struct kvm_coalesced_mmio_zone zone;
1875                r = -EFAULT;
1876                if (copy_from_user(&zone, argp, sizeof zone))
1877                        goto out;
1878                r = -ENXIO;
1879                r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
1880                if (r)
1881                        goto out;
1882                r = 0;
1883                break;
1884        }
1885        case KVM_UNREGISTER_COALESCED_MMIO: {
1886                struct kvm_coalesced_mmio_zone zone;
1887                r = -EFAULT;
1888                if (copy_from_user(&zone, argp, sizeof zone))
1889                        goto out;
1890                r = -ENXIO;
1891                r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
1892                if (r)
1893                        goto out;
1894                r = 0;
1895                break;
1896        }
1897#endif
1898#ifdef KVM_CAP_DEVICE_ASSIGNMENT
1899        case KVM_ASSIGN_PCI_DEVICE: {
1900                struct kvm_assigned_pci_dev assigned_dev;
1901
1902                r = -EFAULT;
1903                if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
1904                        goto out;
1905                r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
1906                if (r)
1907                        goto out;
1908                break;
1909        }
1910        case KVM_ASSIGN_IRQ: {
1911                struct kvm_assigned_irq assigned_irq;
1912
1913                r = -EFAULT;
1914                if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
1915                        goto out;
1916                r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
1917                if (r)
1918                        goto out;
1919                break;
1920        }
1921#endif
1922#ifdef KVM_CAP_DEVICE_DEASSIGNMENT
1923        case KVM_DEASSIGN_PCI_DEVICE: {
1924                struct kvm_assigned_pci_dev assigned_dev;
1925
1926                r = -EFAULT;
1927                if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
1928                        goto out;
1929                r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev);
1930                if (r)
1931                        goto out;
1932                break;
1933        }
1934#endif
1935        default:
1936                r = kvm_arch_vm_ioctl(filp, ioctl, arg);
1937        }
1938out:
1939        return r;
1940}
1941
1942static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1943{
1944        struct page *page[1];
1945        unsigned long addr;
1946        int npages;
1947        gfn_t gfn = vmf->pgoff;
1948        struct kvm *kvm = vma->vm_file->private_data;
1949
1950        addr = gfn_to_hva(kvm, gfn);
1951        if (kvm_is_error_hva(addr))
1952                return VM_FAULT_SIGBUS;
1953
1954        npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
1955                                NULL);
1956        if (unlikely(npages != 1))
1957                return VM_FAULT_SIGBUS;
1958
1959        vmf->page = page[0];
1960        return 0;
1961}
1962
1963static struct vm_operations_struct kvm_vm_vm_ops = {
1964        .fault = kvm_vm_fault,
1965};
1966
1967static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
1968{
1969        vma->vm_ops = &kvm_vm_vm_ops;
1970        return 0;
1971}
1972
1973static struct file_operations kvm_vm_fops = {
1974        .release        = kvm_vm_release,
1975        .unlocked_ioctl = kvm_vm_ioctl,
1976        .compat_ioctl   = kvm_vm_ioctl,
1977        .mmap           = kvm_vm_mmap,
1978};
1979
1980static int kvm_dev_ioctl_create_vm(void)
1981{
1982        int fd;
1983        struct kvm *kvm;
1984
1985        kvm = kvm_create_vm();
1986        if (IS_ERR(kvm))
1987                return PTR_ERR(kvm);
1988        fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
1989        if (fd < 0)
1990                kvm_put_kvm(kvm);
1991
1992        return fd;
1993}
1994
1995static long kvm_dev_ioctl_check_extension_generic(long arg)
1996{
1997        switch (arg) {
1998        case KVM_CAP_USER_MEMORY:
1999        case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
2000        case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
2001                return 1;
2002        default:
2003                break;
2004        }
2005        return kvm_dev_ioctl_check_extension(arg);
2006}
2007
2008static long kvm_dev_ioctl(struct file *filp,
2009                          unsigned int ioctl, unsigned long arg)
2010{
2011        long r = -EINVAL;
2012
2013        switch (ioctl) {
2014        case KVM_GET_API_VERSION:
2015                r = -EINVAL;
2016                if (arg)
2017                        goto out;
2018                r = KVM_API_VERSION;
2019                break;
2020        case KVM_CREATE_VM:
2021                r = -EINVAL;
2022                if (arg)
2023                        goto out;
2024                r = kvm_dev_ioctl_create_vm();
2025                break;
2026        case KVM_CHECK_EXTENSION:
2027                r = kvm_dev_ioctl_check_extension_generic(arg);
2028                break;
2029        case KVM_GET_VCPU_MMAP_SIZE:
2030                r = -EINVAL;
2031                if (arg)
2032                        goto out;
2033                r = PAGE_SIZE;     /* struct kvm_run */
2034#ifdef CONFIG_X86
2035                r += PAGE_SIZE;    /* pio data page */
2036#endif
2037#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
2038                r += PAGE_SIZE;    /* coalesced mmio ring page */
2039#endif
2040                break;
2041        case KVM_TRACE_ENABLE:
2042        case KVM_TRACE_PAUSE:
2043        case KVM_TRACE_DISABLE:
2044                r = kvm_trace_ioctl(ioctl, arg);
2045                break;
2046        default:
2047                return kvm_arch_dev_ioctl(filp, ioctl, arg);
2048        }
2049out:
2050        return r;
2051}
2052
2053static struct file_operations kvm_chardev_ops = {
2054        .unlocked_ioctl = kvm_dev_ioctl,
2055        .compat_ioctl   = kvm_dev_ioctl,
2056};
2057
2058static struct miscdevice kvm_dev = {
2059        KVM_MINOR,
2060        "kvm",
2061        &kvm_chardev_ops,
2062};
2063
2064static void hardware_enable(void *junk)
2065{
2066        int cpu = raw_smp_processor_id();
2067
2068        if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
2069                return;
2070        cpumask_set_cpu(cpu, cpus_hardware_enabled);
2071        kvm_arch_hardware_enable(NULL);
2072}
2073
2074static void hardware_disable(void *junk)
2075{
2076        int cpu = raw_smp_processor_id();
2077
2078        if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
2079                return;
2080        cpumask_clear_cpu(cpu, cpus_hardware_enabled);
2081        kvm_arch_hardware_disable(NULL);
2082}
2083
2084static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2085                           void *v)
2086{
2087        int cpu = (long)v;
2088
2089        val &= ~CPU_TASKS_FROZEN;
2090        switch (val) {
2091        case CPU_DYING:
2092                printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2093                       cpu);
2094                hardware_disable(NULL);
2095                break;
2096        case CPU_UP_CANCELED:
2097                printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2098                       cpu);
2099                smp_call_function_single(cpu, hardware_disable, NULL, 1);
2100                break;
2101        case CPU_ONLINE:
2102                printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2103                       cpu);
2104                smp_call_function_single(cpu, hardware_enable, NULL, 1);
2105                break;
2106        }
2107        return NOTIFY_OK;
2108}
2109
2110
2111asmlinkage void kvm_handle_fault_on_reboot(void)
2112{
2113        if (kvm_rebooting)
2114                /* spin while reset goes on */
2115                while (true)
2116                        ;
2117        /* Fault while not rebooting.  We want the trace. */
2118        BUG();
2119}
2120EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
2121
2122static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2123                      void *v)
2124{
2125        if (val == SYS_RESTART) {
2126                /*
2127                 * Some (well, at least mine) BIOSes hang on reboot if
2128                 * in vmx root mode.
2129                 */
2130                printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2131                kvm_rebooting = true;
2132                on_each_cpu(hardware_disable, NULL, 1);
2133        }
2134        return NOTIFY_OK;
2135}
2136
2137static struct notifier_block kvm_reboot_notifier = {
2138        .notifier_call = kvm_reboot,
2139        .priority = 0,
2140};
2141
2142void kvm_io_bus_init(struct kvm_io_bus *bus)
2143{
2144        memset(bus, 0, sizeof(*bus));
2145}
2146
2147void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2148{
2149        int i;
2150
2151        for (i = 0; i < bus->dev_count; i++) {
2152                struct kvm_io_device *pos = bus->devs[i];
2153
2154                kvm_iodevice_destructor(pos);
2155        }
2156}
2157
2158struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
2159                                          gpa_t addr, int len, int is_write)
2160{
2161        int i;
2162
2163        for (i = 0; i < bus->dev_count; i++) {
2164                struct kvm_io_device *pos = bus->devs[i];
2165
2166                if (pos->in_range(pos, addr, len, is_write))
2167                        return pos;
2168        }
2169
2170        return NULL;
2171}
2172
2173void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
2174{
2175        BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
2176
2177        bus->devs[bus->dev_count++] = dev;
2178}
2179
2180static struct notifier_block kvm_cpu_notifier = {
2181        .notifier_call = kvm_cpu_hotplug,
2182        .priority = 20, /* must be > scheduler priority */
2183};
2184
2185static int vm_stat_get(void *_offset, u64 *val)
2186{
2187        unsigned offset = (long)_offset;
2188        struct kvm *kvm;
2189
2190        *val = 0;
2191        spin_lock(&kvm_lock);
2192        list_for_each_entry(kvm, &vm_list, vm_list)
2193                *val += *(u32 *)((void *)kvm + offset);
2194        spin_unlock(&kvm_lock);
2195        return 0;
2196}
2197
2198DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
2199
2200static int vcpu_stat_get(void *_offset, u64 *val)
2201{
2202        unsigned offset = (long)_offset;
2203        struct kvm *kvm;
2204        struct kvm_vcpu *vcpu;
2205        int i;
2206
2207        *val = 0;
2208        spin_lock(&kvm_lock);
2209        list_for_each_entry(kvm, &vm_list, vm_list)
2210                for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2211                        vcpu = kvm->vcpus[i];
2212                        if (vcpu)
2213                                *val += *(u32 *)((void *)vcpu + offset);
2214                }
2215        spin_unlock(&kvm_lock);
2216        return 0;
2217}
2218
2219DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
2220
2221static struct file_operations *stat_fops[] = {
2222        [KVM_STAT_VCPU] = &vcpu_stat_fops,
2223        [KVM_STAT_VM]   = &vm_stat_fops,
2224};
2225
2226static void kvm_init_debug(void)
2227{
2228        struct kvm_stats_debugfs_item *p;
2229
2230        kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
2231        for (p = debugfs_entries; p->name; ++p)
2232                p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
2233                                                (void *)(long)p->offset,
2234                                                stat_fops[p->kind]);
2235}
2236
2237static void kvm_exit_debug(void)
2238{
2239        struct kvm_stats_debugfs_item *p;
2240
2241        for (p = debugfs_entries; p->name; ++p)
2242                debugfs_remove(p->dentry);
2243        debugfs_remove(kvm_debugfs_dir);
2244}
2245
2246static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2247{
2248        hardware_disable(NULL);
2249        return 0;
2250}
2251
2252static int kvm_resume(struct sys_device *dev)
2253{
2254        hardware_enable(NULL);
2255        return 0;
2256}
2257
2258static struct sysdev_class kvm_sysdev_class = {
2259        .name = "kvm",
2260        .suspend = kvm_suspend,
2261        .resume = kvm_resume,
2262};
2263
2264static struct sys_device kvm_sysdev = {
2265        .id = 0,
2266        .cls = &kvm_sysdev_class,
2267};
2268
2269struct page *bad_page;
2270pfn_t bad_pfn;
2271
2272static inline
2273struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2274{
2275        return container_of(pn, struct kvm_vcpu, preempt_notifier);
2276}
2277
2278static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2279{
2280        struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2281
2282        kvm_arch_vcpu_load(vcpu, cpu);
2283}
2284
2285static void kvm_sched_out(struct preempt_notifier *pn,
2286                          struct task_struct *next)
2287{
2288        struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2289
2290        kvm_arch_vcpu_put(vcpu);
2291}
2292
2293int kvm_init(void *opaque, unsigned int vcpu_size,
2294                  struct module *module)
2295{
2296        int r;
2297        int cpu;
2298
2299        kvm_init_debug();
2300
2301        r = kvm_arch_init(opaque);
2302        if (r)
2303                goto out_fail;
2304
2305        bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2306
2307        if (bad_page == NULL) {
2308                r = -ENOMEM;
2309                goto out;
2310        }
2311
2312        bad_pfn = page_to_pfn(bad_page);
2313
2314        if (!alloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
2315                r = -ENOMEM;
2316                goto out_free_0;
2317        }
2318        cpumask_clear(cpus_hardware_enabled);
2319
2320        r = kvm_arch_hardware_setup();
2321        if (r < 0)
2322                goto out_free_0a;
2323
2324        for_each_online_cpu(cpu) {
2325                smp_call_function_single(cpu,
2326                                kvm_arch_check_processor_compat,
2327                                &r, 1);
2328                if (r < 0)
2329                        goto out_free_1;
2330        }
2331
2332        on_each_cpu(hardware_enable, NULL, 1);
2333        r = register_cpu_notifier(&kvm_cpu_notifier);
2334        if (r)
2335                goto out_free_2;
2336        register_reboot_notifier(&kvm_reboot_notifier);
2337
2338        r = sysdev_class_register(&kvm_sysdev_class);
2339        if (r)
2340                goto out_free_3;
2341
2342        r = sysdev_register(&kvm_sysdev);
2343        if (r)
2344                goto out_free_4;
2345
2346        /* A kmem cache lets us meet the alignment requirements of fx_save. */
2347        kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
2348                                           __alignof__(struct kvm_vcpu),
2349                                           0, NULL);
2350        if (!kvm_vcpu_cache) {
2351                r = -ENOMEM;
2352                goto out_free_5;
2353        }
2354
2355        kvm_chardev_ops.owner = module;
2356        kvm_vm_fops.owner = module;
2357        kvm_vcpu_fops.owner = module;
2358
2359        r = misc_register(&kvm_dev);
2360        if (r) {
2361                printk(KERN_ERR "kvm: misc device register failed\n");
2362                goto out_free;
2363        }
2364
2365        kvm_preempt_ops.sched_in = kvm_sched_in;
2366        kvm_preempt_ops.sched_out = kvm_sched_out;
2367#ifndef CONFIG_X86
2368        msi2intx = 0;
2369#endif
2370
2371        return 0;
2372
2373out_free:
2374        kmem_cache_destroy(kvm_vcpu_cache);
2375out_free_5:
2376        sysdev_unregister(&kvm_sysdev);
2377out_free_4:
2378        sysdev_class_unregister(&kvm_sysdev_class);
2379out_free_3:
2380        unregister_reboot_notifier(&kvm_reboot_notifier);
2381        unregister_cpu_notifier(&kvm_cpu_notifier);
2382out_free_2:
2383        on_each_cpu(hardware_disable, NULL, 1);
2384out_free_1:
2385        kvm_arch_hardware_unsetup();
2386out_free_0a:
2387        free_cpumask_var(cpus_hardware_enabled);
2388out_free_0:
2389        __free_page(bad_page);
2390out:
2391        kvm_arch_exit();
2392        kvm_exit_debug();
2393out_fail:
2394        return r;
2395}
2396EXPORT_SYMBOL_GPL(kvm_init);
2397
2398void kvm_exit(void)
2399{
2400        kvm_trace_cleanup();
2401        misc_deregister(&kvm_dev);
2402        kmem_cache_destroy(kvm_vcpu_cache);
2403        sysdev_unregister(&kvm_sysdev);
2404        sysdev_class_unregister(&kvm_sysdev_class);
2405        unregister_reboot_notifier(&kvm_reboot_notifier);
2406        unregister_cpu_notifier(&kvm_cpu_notifier);
2407        on_each_cpu(hardware_disable, NULL, 1);
2408        kvm_arch_hardware_unsetup();
2409        kvm_arch_exit();
2410        kvm_exit_debug();
2411        free_cpumask_var(cpus_hardware_enabled);
2412        __free_page(bad_page);
2413}
2414EXPORT_SYMBOL_GPL(kvm_exit);
2415
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.