Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 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. |
Nicolas Kaiser | 9611c18 | 2010-10-06 14:23:22 +0200 | [diff] [blame] | 8 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9 | * |
| 10 | * Authors: |
| 11 | * Avi Kivity <avi@qumranet.com> |
| 12 | * Yaniv Kamay <yaniv@qumranet.com> |
| 13 | * |
| 14 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 15 | * the COPYING file in the top-level directory. |
| 16 | * |
| 17 | */ |
| 18 | |
Hollis Blanchard | e217402 | 2007-12-03 15:30:24 -0600 | [diff] [blame] | 19 | #include "iodev.h" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 20 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 21 | #include <linux/kvm_host.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 22 | #include <linux/kvm.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/errno.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 25 | #include <linux/percpu.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 26 | #include <linux/mm.h> |
| 27 | #include <linux/miscdevice.h> |
| 28 | #include <linux/vmalloc.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 29 | #include <linux/reboot.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 30 | #include <linux/debugfs.h> |
| 31 | #include <linux/highmem.h> |
| 32 | #include <linux/file.h> |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 33 | #include <linux/sysdev.h> |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 34 | #include <linux/cpu.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 35 | #include <linux/sched.h> |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 36 | #include <linux/cpumask.h> |
| 37 | #include <linux/smp.h> |
Avi Kivity | d6d2816 | 2007-06-28 08:38:16 -0400 | [diff] [blame] | 38 | #include <linux/anon_inodes.h> |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 39 | #include <linux/profile.h> |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 40 | #include <linux/kvm_para.h> |
Izik Eidus | 6fc138d | 2007-10-09 19:20:39 +0200 | [diff] [blame] | 41 | #include <linux/pagemap.h> |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 42 | #include <linux/mman.h> |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 43 | #include <linux/swap.h> |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 44 | #include <linux/bitops.h> |
Marcelo Tosatti | 547de29 | 2009-05-07 17:55:13 -0300 | [diff] [blame] | 45 | #include <linux/spinlock.h> |
Arnd Bergmann | 6ff5894 | 2009-10-22 14:19:27 +0200 | [diff] [blame] | 46 | #include <linux/compat.h> |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 47 | #include <linux/srcu.h> |
Joerg Roedel | 8f0b1ab | 2010-01-28 12:37:56 +0100 | [diff] [blame] | 48 | #include <linux/hugetlb.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 49 | #include <linux/slab.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 50 | |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 51 | #include <asm/processor.h> |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 52 | #include <asm/io.h> |
| 53 | #include <asm/uaccess.h> |
Izik Eidus | 3e021bf | 2007-11-19 11:16:57 +0200 | [diff] [blame] | 54 | #include <asm/pgtable.h> |
Alexander Graf | c8240bd | 2009-10-30 05:47:26 +0000 | [diff] [blame] | 55 | #include <asm-generic/bitops/le.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 56 | |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 57 | #include "coalesced_mmio.h" |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 58 | #include "async_pf.h" |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 59 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 60 | #define CREATE_TRACE_POINTS |
| 61 | #include <trace/events/kvm.h> |
| 62 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 63 | MODULE_AUTHOR("Qumranet"); |
| 64 | MODULE_LICENSE("GPL"); |
| 65 | |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 66 | /* |
| 67 | * Ordering of locks: |
| 68 | * |
Sheng Yang | fae3a35 | 2009-12-15 10:28:07 +0800 | [diff] [blame] | 69 | * kvm->lock --> kvm->slots_lock --> kvm->irq_lock |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 70 | */ |
| 71 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 72 | DEFINE_SPINLOCK(kvm_lock); |
| 73 | LIST_HEAD(vm_list); |
Avi Kivity | 133de90 | 2007-02-12 00:54:44 -0800 | [diff] [blame] | 74 | |
Rusty Russell | 7f59f49 | 2008-12-07 21:25:45 +1030 | [diff] [blame] | 75 | static cpumask_var_t cpus_hardware_enabled; |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 76 | static int kvm_usage_count = 0; |
| 77 | static atomic_t hardware_enable_failed; |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 78 | |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 79 | struct kmem_cache *kvm_vcpu_cache; |
| 80 | EXPORT_SYMBOL_GPL(kvm_vcpu_cache); |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 81 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 82 | static __read_mostly struct preempt_ops kvm_preempt_ops; |
| 83 | |
Hollis Blanchard | 76f7c87 | 2008-04-15 16:05:42 -0500 | [diff] [blame] | 84 | struct dentry *kvm_debugfs_dir; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 85 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 86 | static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl, |
| 87 | unsigned long arg); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 88 | static int hardware_enable_all(void); |
| 89 | static void hardware_disable_all(void); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 90 | |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 91 | static void kvm_io_bus_destroy(struct kvm_io_bus *bus); |
| 92 | |
Hannes Eder | e8ba5d3 | 2008-11-28 17:02:42 +0100 | [diff] [blame] | 93 | static bool kvm_rebooting; |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 94 | |
Marcelo Tosatti | 54dee99 | 2009-06-11 12:07:44 -0300 | [diff] [blame] | 95 | static bool largepages_enabled = true; |
| 96 | |
Gleb Natapov | fa7bff8 | 2010-07-07 20:16:44 +0300 | [diff] [blame] | 97 | static struct page *hwpoison_page; |
| 98 | static pfn_t hwpoison_pfn; |
Huang Ying | bf99815 | 2010-05-31 14:28:19 +0800 | [diff] [blame] | 99 | |
Gleb Natapov | edba23e | 2010-07-07 20:16:45 +0300 | [diff] [blame] | 100 | static struct page *fault_page; |
| 101 | static pfn_t fault_pfn; |
| 102 | |
Xiantao Zhang | c77fb9d | 2008-09-27 10:55:40 +0800 | [diff] [blame] | 103 | inline int kvm_is_mmio_pfn(pfn_t pfn) |
Ben-Ami Yassour | cbff90a | 2008-07-28 19:26:24 +0300 | [diff] [blame] | 104 | { |
Joerg Roedel | fc5659c | 2009-02-18 14:08:58 +0100 | [diff] [blame] | 105 | if (pfn_valid(pfn)) { |
| 106 | struct page *page = compound_head(pfn_to_page(pfn)); |
| 107 | return PageReserved(page); |
| 108 | } |
Ben-Ami Yassour | cbff90a | 2008-07-28 19:26:24 +0300 | [diff] [blame] | 109 | |
| 110 | return true; |
| 111 | } |
| 112 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 113 | /* |
| 114 | * Switches to specified vcpu, until a matching vcpu_put() |
| 115 | */ |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 116 | void vcpu_load(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 117 | { |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 118 | int cpu; |
| 119 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 120 | mutex_lock(&vcpu->mutex); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 121 | cpu = get_cpu(); |
| 122 | preempt_notifier_register(&vcpu->preempt_notifier); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 123 | kvm_arch_vcpu_load(vcpu, cpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 124 | put_cpu(); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 127 | void vcpu_put(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 128 | { |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 129 | preempt_disable(); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 130 | kvm_arch_vcpu_put(vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 131 | preempt_notifier_unregister(&vcpu->preempt_notifier); |
| 132 | preempt_enable(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 133 | mutex_unlock(&vcpu->mutex); |
| 134 | } |
| 135 | |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 136 | static void ack_flush(void *_completed) |
| 137 | { |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 138 | } |
| 139 | |
Rusty Russell | 4984689 | 2008-12-08 20:26:24 +1030 | [diff] [blame] | 140 | static bool make_all_cpus_request(struct kvm *kvm, unsigned int req) |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 141 | { |
Avi Kivity | 597a5f5 | 2008-07-20 14:24:22 +0300 | [diff] [blame] | 142 | int i, cpu, me; |
Rusty Russell | 6ef7a1b | 2008-12-08 20:28:04 +1030 | [diff] [blame] | 143 | cpumask_var_t cpus; |
| 144 | bool called = true; |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 145 | struct kvm_vcpu *vcpu; |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 146 | |
Li Zefan | 79f5599 | 2009-06-15 14:58:26 +0800 | [diff] [blame] | 147 | zalloc_cpumask_var(&cpus, GFP_ATOMIC); |
Rusty Russell | 6ef7a1b | 2008-12-08 20:28:04 +1030 | [diff] [blame] | 148 | |
Avi Kivity | 70e335e | 2010-02-18 11:25:22 +0200 | [diff] [blame] | 149 | raw_spin_lock(&kvm->requests_lock); |
Jan Kiszka | e601e3b | 2009-07-20 11:30:12 +0200 | [diff] [blame] | 150 | me = smp_processor_id(); |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 151 | kvm_for_each_vcpu(i, vcpu, kvm) { |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 152 | if (kvm_make_check_request(req, vcpu)) |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 153 | continue; |
| 154 | cpu = vcpu->cpu; |
Rusty Russell | 6ef7a1b | 2008-12-08 20:28:04 +1030 | [diff] [blame] | 155 | if (cpus != NULL && cpu != -1 && cpu != me) |
| 156 | cpumask_set_cpu(cpu, cpus); |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 157 | } |
Rusty Russell | 6ef7a1b | 2008-12-08 20:28:04 +1030 | [diff] [blame] | 158 | if (unlikely(cpus == NULL)) |
| 159 | smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1); |
| 160 | else if (!cpumask_empty(cpus)) |
| 161 | smp_call_function_many(cpus, ack_flush, NULL, 1); |
| 162 | else |
| 163 | called = false; |
Avi Kivity | 70e335e | 2010-02-18 11:25:22 +0200 | [diff] [blame] | 164 | raw_spin_unlock(&kvm->requests_lock); |
Rusty Russell | 6ef7a1b | 2008-12-08 20:28:04 +1030 | [diff] [blame] | 165 | free_cpumask_var(cpus); |
Rusty Russell | 4984689 | 2008-12-08 20:26:24 +1030 | [diff] [blame] | 166 | return called; |
| 167 | } |
| 168 | |
| 169 | void kvm_flush_remote_tlbs(struct kvm *kvm) |
| 170 | { |
| 171 | if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH)) |
| 172 | ++kvm->stat.remote_tlb_flush; |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 173 | } |
| 174 | |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 175 | void kvm_reload_remote_mmus(struct kvm *kvm) |
| 176 | { |
Rusty Russell | 4984689 | 2008-12-08 20:26:24 +1030 | [diff] [blame] | 177 | make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD); |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 178 | } |
| 179 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 180 | int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id) |
| 181 | { |
| 182 | struct page *page; |
| 183 | int r; |
| 184 | |
| 185 | mutex_init(&vcpu->mutex); |
| 186 | vcpu->cpu = -1; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 187 | vcpu->kvm = kvm; |
| 188 | vcpu->vcpu_id = id; |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 189 | init_waitqueue_head(&vcpu->wq); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 190 | kvm_async_pf_vcpu_init(vcpu); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 191 | |
| 192 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 193 | if (!page) { |
| 194 | r = -ENOMEM; |
| 195 | goto fail; |
| 196 | } |
| 197 | vcpu->run = page_address(page); |
| 198 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 199 | r = kvm_arch_vcpu_init(vcpu); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 200 | if (r < 0) |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 201 | goto fail_free_run; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 202 | return 0; |
| 203 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 204 | fail_free_run: |
| 205 | free_page((unsigned long)vcpu->run); |
| 206 | fail: |
Rusty Russell | 76fafa5 | 2007-10-08 10:50:48 +1000 | [diff] [blame] | 207 | return r; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 208 | } |
| 209 | EXPORT_SYMBOL_GPL(kvm_vcpu_init); |
| 210 | |
| 211 | void kvm_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 212 | { |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 213 | kvm_arch_vcpu_uninit(vcpu); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 214 | free_page((unsigned long)vcpu->run); |
| 215 | } |
| 216 | EXPORT_SYMBOL_GPL(kvm_vcpu_uninit); |
| 217 | |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 218 | #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) |
| 219 | static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn) |
| 220 | { |
| 221 | return container_of(mn, struct kvm, mmu_notifier); |
| 222 | } |
| 223 | |
| 224 | static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn, |
| 225 | struct mm_struct *mm, |
| 226 | unsigned long address) |
| 227 | { |
| 228 | struct kvm *kvm = mmu_notifier_to_kvm(mn); |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 229 | int need_tlb_flush, idx; |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 230 | |
| 231 | /* |
| 232 | * When ->invalidate_page runs, the linux pte has been zapped |
| 233 | * already but the page is still allocated until |
| 234 | * ->invalidate_page returns. So if we increase the sequence |
| 235 | * here the kvm page fault will notice if the spte can't be |
| 236 | * established because the page is going to be freed. If |
| 237 | * instead the kvm page fault establishes the spte before |
| 238 | * ->invalidate_page runs, kvm_unmap_hva will release it |
| 239 | * before returning. |
| 240 | * |
| 241 | * The sequence increase only need to be seen at spin_unlock |
| 242 | * time, and not at spin_lock time. |
| 243 | * |
| 244 | * Increasing the sequence after the spin_unlock would be |
| 245 | * unsafe because the kvm page fault could then establish the |
| 246 | * pte after kvm_unmap_hva returned, without noticing the page |
| 247 | * is going to be freed. |
| 248 | */ |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 249 | idx = srcu_read_lock(&kvm->srcu); |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 250 | spin_lock(&kvm->mmu_lock); |
| 251 | kvm->mmu_notifier_seq++; |
| 252 | need_tlb_flush = kvm_unmap_hva(kvm, address); |
| 253 | spin_unlock(&kvm->mmu_lock); |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 254 | srcu_read_unlock(&kvm->srcu, idx); |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 255 | |
| 256 | /* we've to flush the tlb before the pages can be freed */ |
| 257 | if (need_tlb_flush) |
| 258 | kvm_flush_remote_tlbs(kvm); |
| 259 | |
| 260 | } |
| 261 | |
Izik Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 262 | static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn, |
| 263 | struct mm_struct *mm, |
| 264 | unsigned long address, |
| 265 | pte_t pte) |
| 266 | { |
| 267 | struct kvm *kvm = mmu_notifier_to_kvm(mn); |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 268 | int idx; |
Izik Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 269 | |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 270 | idx = srcu_read_lock(&kvm->srcu); |
Izik Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 271 | spin_lock(&kvm->mmu_lock); |
| 272 | kvm->mmu_notifier_seq++; |
| 273 | kvm_set_spte_hva(kvm, address, pte); |
| 274 | spin_unlock(&kvm->mmu_lock); |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 275 | srcu_read_unlock(&kvm->srcu, idx); |
Izik Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 276 | } |
| 277 | |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 278 | static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, |
| 279 | struct mm_struct *mm, |
| 280 | unsigned long start, |
| 281 | unsigned long end) |
| 282 | { |
| 283 | struct kvm *kvm = mmu_notifier_to_kvm(mn); |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 284 | int need_tlb_flush = 0, idx; |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 285 | |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 286 | idx = srcu_read_lock(&kvm->srcu); |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 287 | spin_lock(&kvm->mmu_lock); |
| 288 | /* |
| 289 | * The count increase must become visible at unlock time as no |
| 290 | * spte can be established without taking the mmu_lock and |
| 291 | * count is also read inside the mmu_lock critical section. |
| 292 | */ |
| 293 | kvm->mmu_notifier_count++; |
| 294 | for (; start < end; start += PAGE_SIZE) |
| 295 | need_tlb_flush |= kvm_unmap_hva(kvm, start); |
| 296 | spin_unlock(&kvm->mmu_lock); |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 297 | srcu_read_unlock(&kvm->srcu, idx); |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 298 | |
| 299 | /* we've to flush the tlb before the pages can be freed */ |
| 300 | if (need_tlb_flush) |
| 301 | kvm_flush_remote_tlbs(kvm); |
| 302 | } |
| 303 | |
| 304 | static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn, |
| 305 | struct mm_struct *mm, |
| 306 | unsigned long start, |
| 307 | unsigned long end) |
| 308 | { |
| 309 | struct kvm *kvm = mmu_notifier_to_kvm(mn); |
| 310 | |
| 311 | spin_lock(&kvm->mmu_lock); |
| 312 | /* |
| 313 | * This sequence increase will notify the kvm page fault that |
| 314 | * the page that is going to be mapped in the spte could have |
| 315 | * been freed. |
| 316 | */ |
| 317 | kvm->mmu_notifier_seq++; |
| 318 | /* |
| 319 | * The above sequence increase must be visible before the |
| 320 | * below count decrease but both values are read by the kvm |
| 321 | * page fault under mmu_lock spinlock so we don't need to add |
| 322 | * a smb_wmb() here in between the two. |
| 323 | */ |
| 324 | kvm->mmu_notifier_count--; |
| 325 | spin_unlock(&kvm->mmu_lock); |
| 326 | |
| 327 | BUG_ON(kvm->mmu_notifier_count < 0); |
| 328 | } |
| 329 | |
| 330 | static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn, |
| 331 | struct mm_struct *mm, |
| 332 | unsigned long address) |
| 333 | { |
| 334 | struct kvm *kvm = mmu_notifier_to_kvm(mn); |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 335 | int young, idx; |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 336 | |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 337 | idx = srcu_read_lock(&kvm->srcu); |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 338 | spin_lock(&kvm->mmu_lock); |
| 339 | young = kvm_age_hva(kvm, address); |
| 340 | spin_unlock(&kvm->mmu_lock); |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 341 | srcu_read_unlock(&kvm->srcu, idx); |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 342 | |
| 343 | if (young) |
| 344 | kvm_flush_remote_tlbs(kvm); |
| 345 | |
| 346 | return young; |
| 347 | } |
| 348 | |
Marcelo Tosatti | 85db06e | 2008-12-10 21:23:26 +0100 | [diff] [blame] | 349 | static void kvm_mmu_notifier_release(struct mmu_notifier *mn, |
| 350 | struct mm_struct *mm) |
| 351 | { |
| 352 | struct kvm *kvm = mmu_notifier_to_kvm(mn); |
Lai Jiangshan | eda2bed | 2010-04-20 14:29:29 +0800 | [diff] [blame] | 353 | int idx; |
| 354 | |
| 355 | idx = srcu_read_lock(&kvm->srcu); |
Marcelo Tosatti | 85db06e | 2008-12-10 21:23:26 +0100 | [diff] [blame] | 356 | kvm_arch_flush_shadow(kvm); |
Lai Jiangshan | eda2bed | 2010-04-20 14:29:29 +0800 | [diff] [blame] | 357 | srcu_read_unlock(&kvm->srcu, idx); |
Marcelo Tosatti | 85db06e | 2008-12-10 21:23:26 +0100 | [diff] [blame] | 358 | } |
| 359 | |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 360 | static const struct mmu_notifier_ops kvm_mmu_notifier_ops = { |
| 361 | .invalidate_page = kvm_mmu_notifier_invalidate_page, |
| 362 | .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start, |
| 363 | .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end, |
| 364 | .clear_flush_young = kvm_mmu_notifier_clear_flush_young, |
Izik Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 365 | .change_pte = kvm_mmu_notifier_change_pte, |
Marcelo Tosatti | 85db06e | 2008-12-10 21:23:26 +0100 | [diff] [blame] | 366 | .release = kvm_mmu_notifier_release, |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 367 | }; |
Avi Kivity | 4c07b0a | 2009-12-20 14:54:04 +0200 | [diff] [blame] | 368 | |
| 369 | static int kvm_init_mmu_notifier(struct kvm *kvm) |
| 370 | { |
| 371 | kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops; |
| 372 | return mmu_notifier_register(&kvm->mmu_notifier, current->mm); |
| 373 | } |
| 374 | |
| 375 | #else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */ |
| 376 | |
| 377 | static int kvm_init_mmu_notifier(struct kvm *kvm) |
| 378 | { |
| 379 | return 0; |
| 380 | } |
| 381 | |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 382 | #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */ |
| 383 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 384 | static struct kvm *kvm_create_vm(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 385 | { |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 386 | int r = 0, i; |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 387 | struct kvm *kvm = kvm_arch_create_vm(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 388 | |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 389 | if (IS_ERR(kvm)) |
| 390 | goto out; |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 391 | |
| 392 | r = hardware_enable_all(); |
| 393 | if (r) |
| 394 | goto out_err_nodisable; |
| 395 | |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 396 | #ifdef CONFIG_HAVE_KVM_IRQCHIP |
| 397 | INIT_HLIST_HEAD(&kvm->mask_notifier_list); |
Gleb Natapov | 136bdfe | 2009-08-24 11:54:23 +0300 | [diff] [blame] | 398 | INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list); |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 399 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 400 | |
Marcelo Tosatti | 46a26bf | 2009-12-23 14:35:16 -0200 | [diff] [blame] | 401 | r = -ENOMEM; |
| 402 | kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL); |
| 403 | if (!kvm->memslots) |
| 404 | goto out_err; |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 405 | if (init_srcu_struct(&kvm->srcu)) |
| 406 | goto out_err; |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 407 | for (i = 0; i < KVM_NR_BUSES; i++) { |
| 408 | kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus), |
| 409 | GFP_KERNEL); |
| 410 | if (!kvm->buses[i]) { |
| 411 | cleanup_srcu_struct(&kvm->srcu); |
| 412 | goto out_err; |
| 413 | } |
| 414 | } |
Marcelo Tosatti | 46a26bf | 2009-12-23 14:35:16 -0200 | [diff] [blame] | 415 | |
Avi Kivity | 4c07b0a | 2009-12-20 14:54:04 +0200 | [diff] [blame] | 416 | r = kvm_init_mmu_notifier(kvm); |
Avi Kivity | 283d0c6 | 2009-12-20 14:25:19 +0200 | [diff] [blame] | 417 | if (r) { |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 418 | cleanup_srcu_struct(&kvm->srcu); |
Avi Kivity | 283d0c6 | 2009-12-20 14:25:19 +0200 | [diff] [blame] | 419 | goto out_err; |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 420 | } |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 421 | |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 422 | kvm->mm = current->mm; |
| 423 | atomic_inc(&kvm->mm->mm_count); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 424 | spin_lock_init(&kvm->mmu_lock); |
Avi Kivity | 70e335e | 2010-02-18 11:25:22 +0200 | [diff] [blame] | 425 | raw_spin_lock_init(&kvm->requests_lock); |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 426 | kvm_eventfd_init(kvm); |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 427 | mutex_init(&kvm->lock); |
Marcelo Tosatti | 60eead7 | 2009-06-04 15:08:23 -0300 | [diff] [blame] | 428 | mutex_init(&kvm->irq_lock); |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 429 | mutex_init(&kvm->slots_lock); |
Izik Eidus | d39f13b | 2008-03-30 16:01:25 +0300 | [diff] [blame] | 430 | atomic_set(&kvm->users_count, 1); |
Rusty Russell | 5e58cfe | 2007-07-23 17:08:21 +1000 | [diff] [blame] | 431 | spin_lock(&kvm_lock); |
| 432 | list_add(&kvm->vm_list, &vm_list); |
| 433 | spin_unlock(&kvm_lock); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 434 | out: |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 435 | return kvm; |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 436 | |
| 437 | out_err: |
| 438 | hardware_disable_all(); |
| 439 | out_err_nodisable: |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 440 | for (i = 0; i < KVM_NR_BUSES; i++) |
| 441 | kfree(kvm->buses[i]); |
Marcelo Tosatti | 46a26bf | 2009-12-23 14:35:16 -0200 | [diff] [blame] | 442 | kfree(kvm->memslots); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 443 | kfree(kvm); |
| 444 | return ERR_PTR(r); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 445 | } |
| 446 | |
Takuya Yoshikawa | a36a57b1 | 2010-10-27 18:22:19 +0900 | [diff] [blame] | 447 | static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot) |
| 448 | { |
| 449 | if (!memslot->dirty_bitmap) |
| 450 | return; |
| 451 | |
Takuya Yoshikawa | 515a012 | 2010-10-27 18:23:54 +0900 | [diff] [blame^] | 452 | vfree(memslot->dirty_bitmap_head); |
Takuya Yoshikawa | a36a57b1 | 2010-10-27 18:22:19 +0900 | [diff] [blame] | 453 | memslot->dirty_bitmap = NULL; |
Takuya Yoshikawa | 515a012 | 2010-10-27 18:23:54 +0900 | [diff] [blame^] | 454 | memslot->dirty_bitmap_head = NULL; |
Takuya Yoshikawa | a36a57b1 | 2010-10-27 18:22:19 +0900 | [diff] [blame] | 455 | } |
| 456 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 457 | /* |
| 458 | * Free any memory in @free but not in @dont. |
| 459 | */ |
| 460 | static void kvm_free_physmem_slot(struct kvm_memory_slot *free, |
| 461 | struct kvm_memory_slot *dont) |
| 462 | { |
Joerg Roedel | ec04b26 | 2009-06-19 15:16:23 +0200 | [diff] [blame] | 463 | int i; |
| 464 | |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 465 | if (!dont || free->rmap != dont->rmap) |
| 466 | vfree(free->rmap); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 467 | |
| 468 | if (!dont || free->dirty_bitmap != dont->dirty_bitmap) |
Takuya Yoshikawa | a36a57b1 | 2010-10-27 18:22:19 +0900 | [diff] [blame] | 469 | kvm_destroy_dirty_bitmap(free); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 470 | |
Joerg Roedel | ec04b26 | 2009-06-19 15:16:23 +0200 | [diff] [blame] | 471 | |
| 472 | for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) { |
| 473 | if (!dont || free->lpage_info[i] != dont->lpage_info[i]) { |
| 474 | vfree(free->lpage_info[i]); |
| 475 | free->lpage_info[i] = NULL; |
| 476 | } |
| 477 | } |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 478 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 479 | free->npages = 0; |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 480 | free->rmap = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 481 | } |
| 482 | |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 483 | void kvm_free_physmem(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 484 | { |
| 485 | int i; |
Marcelo Tosatti | 46a26bf | 2009-12-23 14:35:16 -0200 | [diff] [blame] | 486 | struct kvm_memslots *slots = kvm->memslots; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 487 | |
Marcelo Tosatti | 46a26bf | 2009-12-23 14:35:16 -0200 | [diff] [blame] | 488 | for (i = 0; i < slots->nmemslots; ++i) |
| 489 | kvm_free_physmem_slot(&slots->memslots[i], NULL); |
| 490 | |
| 491 | kfree(kvm->memslots); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 492 | } |
| 493 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 494 | static void kvm_destroy_vm(struct kvm *kvm) |
| 495 | { |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 496 | int i; |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 497 | struct mm_struct *mm = kvm->mm; |
| 498 | |
Sheng Yang | ad8ba2c | 2009-01-06 10:03:02 +0800 | [diff] [blame] | 499 | kvm_arch_sync_events(kvm); |
Avi Kivity | 133de90 | 2007-02-12 00:54:44 -0800 | [diff] [blame] | 500 | spin_lock(&kvm_lock); |
| 501 | list_del(&kvm->vm_list); |
| 502 | spin_unlock(&kvm_lock); |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 503 | kvm_free_irq_routing(kvm); |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 504 | for (i = 0; i < KVM_NR_BUSES; i++) |
| 505 | kvm_io_bus_destroy(kvm->buses[i]); |
Avi Kivity | 980da6c | 2009-12-20 15:13:43 +0200 | [diff] [blame] | 506 | kvm_coalesced_mmio_free(kvm); |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 507 | #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) |
| 508 | mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm); |
Gleb Natapov | f00be0c | 2009-03-19 12:20:36 +0200 | [diff] [blame] | 509 | #else |
| 510 | kvm_arch_flush_shadow(kvm); |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 511 | #endif |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 512 | kvm_arch_destroy_vm(kvm); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 513 | hardware_disable_all(); |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 514 | mmdrop(mm); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 515 | } |
| 516 | |
Izik Eidus | d39f13b | 2008-03-30 16:01:25 +0300 | [diff] [blame] | 517 | void kvm_get_kvm(struct kvm *kvm) |
| 518 | { |
| 519 | atomic_inc(&kvm->users_count); |
| 520 | } |
| 521 | EXPORT_SYMBOL_GPL(kvm_get_kvm); |
| 522 | |
| 523 | void kvm_put_kvm(struct kvm *kvm) |
| 524 | { |
| 525 | if (atomic_dec_and_test(&kvm->users_count)) |
| 526 | kvm_destroy_vm(kvm); |
| 527 | } |
| 528 | EXPORT_SYMBOL_GPL(kvm_put_kvm); |
| 529 | |
| 530 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 531 | static int kvm_vm_release(struct inode *inode, struct file *filp) |
| 532 | { |
| 533 | struct kvm *kvm = filp->private_data; |
| 534 | |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 535 | kvm_irqfd_release(kvm); |
| 536 | |
Izik Eidus | d39f13b | 2008-03-30 16:01:25 +0300 | [diff] [blame] | 537 | kvm_put_kvm(kvm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 538 | return 0; |
| 539 | } |
| 540 | |
Takuya Yoshikawa | 515a012 | 2010-10-27 18:23:54 +0900 | [diff] [blame^] | 541 | /* |
| 542 | * Allocation size is twice as large as the actual dirty bitmap size. |
| 543 | * This makes it possible to do double buffering: see x86's |
| 544 | * kvm_vm_ioctl_get_dirty_log(). |
| 545 | */ |
Takuya Yoshikawa | a36a57b1 | 2010-10-27 18:22:19 +0900 | [diff] [blame] | 546 | static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot) |
| 547 | { |
Takuya Yoshikawa | 515a012 | 2010-10-27 18:23:54 +0900 | [diff] [blame^] | 548 | unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot); |
Takuya Yoshikawa | a36a57b1 | 2010-10-27 18:22:19 +0900 | [diff] [blame] | 549 | |
| 550 | memslot->dirty_bitmap = vmalloc(dirty_bytes); |
| 551 | if (!memslot->dirty_bitmap) |
| 552 | return -ENOMEM; |
| 553 | |
| 554 | memset(memslot->dirty_bitmap, 0, dirty_bytes); |
Takuya Yoshikawa | 515a012 | 2010-10-27 18:23:54 +0900 | [diff] [blame^] | 555 | memslot->dirty_bitmap_head = memslot->dirty_bitmap; |
Takuya Yoshikawa | a36a57b1 | 2010-10-27 18:22:19 +0900 | [diff] [blame] | 556 | return 0; |
| 557 | } |
| 558 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 559 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 560 | * Allocate some memory and give it an address in the guest physical address |
| 561 | * space. |
| 562 | * |
| 563 | * Discontiguous memory is allowed, mostly for framebuffers. |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 564 | * |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 565 | * Must be called holding mmap_sem for write. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 566 | */ |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 567 | int __kvm_set_memory_region(struct kvm *kvm, |
| 568 | struct kvm_userspace_memory_region *mem, |
| 569 | int user_alloc) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 570 | { |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 571 | int r, flush_shadow = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 572 | gfn_t base_gfn; |
Heiko Carstens | 28bcb11 | 2009-09-03 17:35:35 +0200 | [diff] [blame] | 573 | unsigned long npages; |
| 574 | unsigned long i; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 575 | struct kvm_memory_slot *memslot; |
| 576 | struct kvm_memory_slot old, new; |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 577 | struct kvm_memslots *slots, *old_memslots; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 578 | |
| 579 | r = -EINVAL; |
| 580 | /* General sanity checks */ |
| 581 | if (mem->memory_size & (PAGE_SIZE - 1)) |
| 582 | goto out; |
| 583 | if (mem->guest_phys_addr & (PAGE_SIZE - 1)) |
| 584 | goto out; |
Sheng Yang | e7cacd4 | 2008-11-11 15:30:40 +0800 | [diff] [blame] | 585 | if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1))) |
Hollis Blanchard | 7874980 | 2008-11-07 13:32:12 -0600 | [diff] [blame] | 586 | goto out; |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 587 | if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 588 | goto out; |
| 589 | if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr) |
| 590 | goto out; |
| 591 | |
Marcelo Tosatti | 46a26bf | 2009-12-23 14:35:16 -0200 | [diff] [blame] | 592 | memslot = &kvm->memslots->memslots[mem->slot]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 593 | base_gfn = mem->guest_phys_addr >> PAGE_SHIFT; |
| 594 | npages = mem->memory_size >> PAGE_SHIFT; |
| 595 | |
Takuya Yoshikawa | 660c22c | 2010-04-13 22:47:24 +0900 | [diff] [blame] | 596 | r = -EINVAL; |
| 597 | if (npages > KVM_MEM_MAX_NR_PAGES) |
| 598 | goto out; |
| 599 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 600 | if (!npages) |
| 601 | mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES; |
| 602 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 603 | new = old = *memslot; |
| 604 | |
Avi Kivity | e36d96f | 2010-06-21 10:56:36 +0300 | [diff] [blame] | 605 | new.id = mem->slot; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 606 | new.base_gfn = base_gfn; |
| 607 | new.npages = npages; |
| 608 | new.flags = mem->flags; |
| 609 | |
| 610 | /* Disallow changing a memory slot's size. */ |
| 611 | r = -EINVAL; |
| 612 | if (npages && old.npages && npages != old.npages) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 613 | goto out_free; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 614 | |
| 615 | /* Check for overlaps */ |
| 616 | r = -EEXIST; |
| 617 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { |
Marcelo Tosatti | 46a26bf | 2009-12-23 14:35:16 -0200 | [diff] [blame] | 618 | struct kvm_memory_slot *s = &kvm->memslots->memslots[i]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 619 | |
Jan Kiszka | 4cd481f | 2009-04-13 11:59:32 +0200 | [diff] [blame] | 620 | if (s == memslot || !s->npages) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 621 | continue; |
| 622 | if (!((base_gfn + npages <= s->base_gfn) || |
| 623 | (base_gfn >= s->base_gfn + s->npages))) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 624 | goto out_free; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 625 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 626 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 627 | /* Free page dirty bitmap if unneeded */ |
| 628 | if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES)) |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 629 | new.dirty_bitmap = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 630 | |
| 631 | r = -ENOMEM; |
| 632 | |
| 633 | /* Allocate if a slot is being created */ |
Carsten Otte | eff0114 | 2008-06-27 15:05:31 +0200 | [diff] [blame] | 634 | #ifndef CONFIG_S390 |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 635 | if (npages && !new.rmap) { |
Lai Jiangshan | 3bd8900 | 2010-06-02 17:01:23 +0800 | [diff] [blame] | 636 | new.rmap = vmalloc(npages * sizeof(*new.rmap)); |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 637 | |
| 638 | if (!new.rmap) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 639 | goto out_free; |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 640 | |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 641 | memset(new.rmap, 0, npages * sizeof(*new.rmap)); |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 642 | |
Izik Eidus | 80b14b5 | 2007-10-25 11:54:04 +0200 | [diff] [blame] | 643 | new.user_alloc = user_alloc; |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 644 | new.userspace_addr = mem->userspace_addr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 645 | } |
Joerg Roedel | ec04b26 | 2009-06-19 15:16:23 +0200 | [diff] [blame] | 646 | if (!npages) |
| 647 | goto skip_lpage; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 648 | |
Joerg Roedel | ec04b26 | 2009-06-19 15:16:23 +0200 | [diff] [blame] | 649 | for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) { |
Heiko Carstens | 28bcb11 | 2009-09-03 17:35:35 +0200 | [diff] [blame] | 650 | unsigned long ugfn; |
| 651 | unsigned long j; |
| 652 | int lpages; |
Joerg Roedel | ec04b26 | 2009-06-19 15:16:23 +0200 | [diff] [blame] | 653 | int level = i + 2; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 654 | |
Joerg Roedel | ec04b26 | 2009-06-19 15:16:23 +0200 | [diff] [blame] | 655 | /* Avoid unused variable warning if no large pages */ |
| 656 | (void)level; |
| 657 | |
| 658 | if (new.lpage_info[i]) |
| 659 | continue; |
| 660 | |
Joerg Roedel | 8285541 | 2010-07-01 16:00:11 +0200 | [diff] [blame] | 661 | lpages = 1 + ((base_gfn + npages - 1) |
| 662 | >> KVM_HPAGE_GFN_SHIFT(level)); |
| 663 | lpages -= base_gfn >> KVM_HPAGE_GFN_SHIFT(level); |
Joerg Roedel | ec04b26 | 2009-06-19 15:16:23 +0200 | [diff] [blame] | 664 | |
| 665 | new.lpage_info[i] = vmalloc(lpages * sizeof(*new.lpage_info[i])); |
| 666 | |
| 667 | if (!new.lpage_info[i]) |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 668 | goto out_free; |
| 669 | |
Joerg Roedel | ec04b26 | 2009-06-19 15:16:23 +0200 | [diff] [blame] | 670 | memset(new.lpage_info[i], 0, |
| 671 | lpages * sizeof(*new.lpage_info[i])); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 672 | |
Joerg Roedel | 8285541 | 2010-07-01 16:00:11 +0200 | [diff] [blame] | 673 | if (base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1)) |
Joerg Roedel | ec04b26 | 2009-06-19 15:16:23 +0200 | [diff] [blame] | 674 | new.lpage_info[i][0].write_count = 1; |
Joerg Roedel | 8285541 | 2010-07-01 16:00:11 +0200 | [diff] [blame] | 675 | if ((base_gfn+npages) & (KVM_PAGES_PER_HPAGE(level) - 1)) |
Joerg Roedel | ec04b26 | 2009-06-19 15:16:23 +0200 | [diff] [blame] | 676 | new.lpage_info[i][lpages - 1].write_count = 1; |
Avi Kivity | ac04527 | 2009-06-08 15:52:39 +0300 | [diff] [blame] | 677 | ugfn = new.userspace_addr >> PAGE_SHIFT; |
| 678 | /* |
| 679 | * If the gfn and userspace address are not aligned wrt each |
Marcelo Tosatti | 54dee99 | 2009-06-11 12:07:44 -0300 | [diff] [blame] | 680 | * other, or if explicitly asked to, disable large page |
| 681 | * support for this slot |
Avi Kivity | ac04527 | 2009-06-08 15:52:39 +0300 | [diff] [blame] | 682 | */ |
Joerg Roedel | ec04b26 | 2009-06-19 15:16:23 +0200 | [diff] [blame] | 683 | if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) || |
Marcelo Tosatti | 54dee99 | 2009-06-11 12:07:44 -0300 | [diff] [blame] | 684 | !largepages_enabled) |
Joerg Roedel | ec04b26 | 2009-06-19 15:16:23 +0200 | [diff] [blame] | 685 | for (j = 0; j < lpages; ++j) |
| 686 | new.lpage_info[i][j].write_count = 1; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 687 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 688 | |
Joerg Roedel | ec04b26 | 2009-06-19 15:16:23 +0200 | [diff] [blame] | 689 | skip_lpage: |
| 690 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 691 | /* Allocate page dirty bitmap if needed */ |
| 692 | if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) { |
Takuya Yoshikawa | a36a57b1 | 2010-10-27 18:22:19 +0900 | [diff] [blame] | 693 | if (kvm_create_dirty_bitmap(&new) < 0) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 694 | goto out_free; |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 695 | /* destroy any largepage mappings for dirty tracking */ |
Izik Eidus | e244584 | 2009-06-10 19:23:24 +0300 | [diff] [blame] | 696 | if (old.npages) |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 697 | flush_shadow = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 698 | } |
Christian Borntraeger | 3eea843 | 2009-06-23 17:24:06 +0200 | [diff] [blame] | 699 | #else /* not defined CONFIG_S390 */ |
| 700 | new.user_alloc = user_alloc; |
| 701 | if (user_alloc) |
| 702 | new.userspace_addr = mem->userspace_addr; |
Carsten Otte | eff0114 | 2008-06-27 15:05:31 +0200 | [diff] [blame] | 703 | #endif /* not defined CONFIG_S390 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 704 | |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 705 | if (!npages) { |
| 706 | r = -ENOMEM; |
| 707 | slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL); |
| 708 | if (!slots) |
| 709 | goto out_free; |
| 710 | memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots)); |
| 711 | if (mem->slot >= slots->nmemslots) |
| 712 | slots->nmemslots = mem->slot + 1; |
Gleb Natapov | 49c7754 | 2010-10-18 15:22:23 +0200 | [diff] [blame] | 713 | slots->generation++; |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 714 | slots->memslots[mem->slot].flags |= KVM_MEMSLOT_INVALID; |
| 715 | |
| 716 | old_memslots = kvm->memslots; |
| 717 | rcu_assign_pointer(kvm->memslots, slots); |
| 718 | synchronize_srcu_expedited(&kvm->srcu); |
| 719 | /* From this point no new shadow pages pointing to a deleted |
| 720 | * memslot will be created. |
| 721 | * |
| 722 | * validation of sp->gfn happens in: |
| 723 | * - gfn_to_hva (kvm_read_guest, gfn_to_pfn) |
| 724 | * - kvm_is_visible_gfn (mmu_check_roots) |
| 725 | */ |
Marcelo Tosatti | 34d4cb8 | 2008-07-10 20:49:31 -0300 | [diff] [blame] | 726 | kvm_arch_flush_shadow(kvm); |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 727 | kfree(old_memslots); |
| 728 | } |
Marcelo Tosatti | 34d4cb8 | 2008-07-10 20:49:31 -0300 | [diff] [blame] | 729 | |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 730 | r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc); |
| 731 | if (r) |
| 732 | goto out_free; |
| 733 | |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 734 | /* map the pages in iommu page table */ |
| 735 | if (npages) { |
| 736 | r = kvm_iommu_map_pages(kvm, &new); |
| 737 | if (r) |
| 738 | goto out_free; |
| 739 | } |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 740 | |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 741 | r = -ENOMEM; |
| 742 | slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL); |
| 743 | if (!slots) |
| 744 | goto out_free; |
| 745 | memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots)); |
| 746 | if (mem->slot >= slots->nmemslots) |
| 747 | slots->nmemslots = mem->slot + 1; |
Gleb Natapov | 49c7754 | 2010-10-18 15:22:23 +0200 | [diff] [blame] | 748 | slots->generation++; |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 749 | |
| 750 | /* actual memory is freed via old in kvm_free_physmem_slot below */ |
| 751 | if (!npages) { |
| 752 | new.rmap = NULL; |
| 753 | new.dirty_bitmap = NULL; |
| 754 | for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) |
| 755 | new.lpage_info[i] = NULL; |
| 756 | } |
| 757 | |
| 758 | slots->memslots[mem->slot] = new; |
| 759 | old_memslots = kvm->memslots; |
| 760 | rcu_assign_pointer(kvm->memslots, slots); |
| 761 | synchronize_srcu_expedited(&kvm->srcu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 762 | |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 763 | kvm_arch_commit_memory_region(kvm, mem, old, user_alloc); |
Zhang Xiantao | 3ad82a7 | 2007-11-20 13:11:38 +0800 | [diff] [blame] | 764 | |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 765 | kvm_free_physmem_slot(&old, &new); |
| 766 | kfree(old_memslots); |
| 767 | |
| 768 | if (flush_shadow) |
| 769 | kvm_arch_flush_shadow(kvm); |
| 770 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 771 | return 0; |
| 772 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 773 | out_free: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 774 | kvm_free_physmem_slot(&new, &old); |
| 775 | out: |
| 776 | return r; |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 777 | |
| 778 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 779 | EXPORT_SYMBOL_GPL(__kvm_set_memory_region); |
| 780 | |
| 781 | int kvm_set_memory_region(struct kvm *kvm, |
| 782 | struct kvm_userspace_memory_region *mem, |
| 783 | int user_alloc) |
| 784 | { |
| 785 | int r; |
| 786 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 787 | mutex_lock(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 788 | r = __kvm_set_memory_region(kvm, mem, user_alloc); |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 789 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 790 | return r; |
| 791 | } |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 792 | EXPORT_SYMBOL_GPL(kvm_set_memory_region); |
| 793 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 794 | int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, |
| 795 | struct |
| 796 | kvm_userspace_memory_region *mem, |
| 797 | int user_alloc) |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 798 | { |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 799 | if (mem->slot >= KVM_MEMORY_SLOTS) |
| 800 | return -EINVAL; |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 801 | return kvm_set_memory_region(kvm, mem, user_alloc); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 802 | } |
| 803 | |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 804 | int kvm_get_dirty_log(struct kvm *kvm, |
| 805 | struct kvm_dirty_log *log, int *is_dirty) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 806 | { |
| 807 | struct kvm_memory_slot *memslot; |
| 808 | int r, i; |
Takuya Yoshikawa | 87bf6e7 | 2010-04-12 19:35:35 +0900 | [diff] [blame] | 809 | unsigned long n; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 810 | unsigned long any = 0; |
| 811 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 812 | r = -EINVAL; |
| 813 | if (log->slot >= KVM_MEMORY_SLOTS) |
| 814 | goto out; |
| 815 | |
Marcelo Tosatti | 46a26bf | 2009-12-23 14:35:16 -0200 | [diff] [blame] | 816 | memslot = &kvm->memslots->memslots[log->slot]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 817 | r = -ENOENT; |
| 818 | if (!memslot->dirty_bitmap) |
| 819 | goto out; |
| 820 | |
Takuya Yoshikawa | 87bf6e7 | 2010-04-12 19:35:35 +0900 | [diff] [blame] | 821 | n = kvm_dirty_bitmap_bytes(memslot); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 822 | |
Uri Lublin | cd1a4a9 | 2007-02-22 16:43:09 +0200 | [diff] [blame] | 823 | for (i = 0; !any && i < n/sizeof(long); ++i) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 824 | any = memslot->dirty_bitmap[i]; |
| 825 | |
| 826 | r = -EFAULT; |
| 827 | if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n)) |
| 828 | goto out; |
| 829 | |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 830 | if (any) |
| 831 | *is_dirty = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 832 | |
| 833 | r = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 834 | out: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 835 | return r; |
| 836 | } |
| 837 | |
Marcelo Tosatti | 54dee99 | 2009-06-11 12:07:44 -0300 | [diff] [blame] | 838 | void kvm_disable_largepages(void) |
| 839 | { |
| 840 | largepages_enabled = false; |
| 841 | } |
| 842 | EXPORT_SYMBOL_GPL(kvm_disable_largepages); |
| 843 | |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 844 | int is_error_page(struct page *page) |
| 845 | { |
Gleb Natapov | edba23e | 2010-07-07 20:16:45 +0300 | [diff] [blame] | 846 | return page == bad_page || page == hwpoison_page || page == fault_page; |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 847 | } |
| 848 | EXPORT_SYMBOL_GPL(is_error_page); |
| 849 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 850 | int is_error_pfn(pfn_t pfn) |
| 851 | { |
Gleb Natapov | edba23e | 2010-07-07 20:16:45 +0300 | [diff] [blame] | 852 | return pfn == bad_pfn || pfn == hwpoison_pfn || pfn == fault_pfn; |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 853 | } |
| 854 | EXPORT_SYMBOL_GPL(is_error_pfn); |
| 855 | |
Huang Ying | bf99815 | 2010-05-31 14:28:19 +0800 | [diff] [blame] | 856 | int is_hwpoison_pfn(pfn_t pfn) |
| 857 | { |
| 858 | return pfn == hwpoison_pfn; |
| 859 | } |
| 860 | EXPORT_SYMBOL_GPL(is_hwpoison_pfn); |
| 861 | |
Gleb Natapov | edba23e | 2010-07-07 20:16:45 +0300 | [diff] [blame] | 862 | int is_fault_pfn(pfn_t pfn) |
| 863 | { |
| 864 | return pfn == fault_pfn; |
| 865 | } |
| 866 | EXPORT_SYMBOL_GPL(is_fault_pfn); |
| 867 | |
Izik Eidus | f9d46eb | 2007-11-11 22:02:22 +0200 | [diff] [blame] | 868 | static inline unsigned long bad_hva(void) |
| 869 | { |
| 870 | return PAGE_OFFSET; |
| 871 | } |
| 872 | |
| 873 | int kvm_is_error_hva(unsigned long addr) |
| 874 | { |
| 875 | return addr == bad_hva(); |
| 876 | } |
| 877 | EXPORT_SYMBOL_GPL(kvm_is_error_hva); |
| 878 | |
Gleb Natapov | 49c7754 | 2010-10-18 15:22:23 +0200 | [diff] [blame] | 879 | static struct kvm_memory_slot *__gfn_to_memslot(struct kvm_memslots *slots, |
| 880 | gfn_t gfn) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 881 | { |
| 882 | int i; |
| 883 | |
Marcelo Tosatti | 46a26bf | 2009-12-23 14:35:16 -0200 | [diff] [blame] | 884 | for (i = 0; i < slots->nmemslots; ++i) { |
| 885 | struct kvm_memory_slot *memslot = &slots->memslots[i]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 886 | |
| 887 | if (gfn >= memslot->base_gfn |
| 888 | && gfn < memslot->base_gfn + memslot->npages) |
| 889 | return memslot; |
| 890 | } |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 891 | return NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 892 | } |
Gleb Natapov | 49c7754 | 2010-10-18 15:22:23 +0200 | [diff] [blame] | 893 | |
| 894 | struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn) |
| 895 | { |
| 896 | return __gfn_to_memslot(kvm_memslots(kvm), gfn); |
| 897 | } |
Avi Kivity | a1f4d395 | 2010-06-21 11:44:20 +0300 | [diff] [blame] | 898 | EXPORT_SYMBOL_GPL(gfn_to_memslot); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 899 | |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 900 | int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn) |
| 901 | { |
| 902 | int i; |
Lai Jiangshan | 90d83dc | 2010-04-19 17:41:23 +0800 | [diff] [blame] | 903 | struct kvm_memslots *slots = kvm_memslots(kvm); |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 904 | |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 905 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { |
Marcelo Tosatti | 46a26bf | 2009-12-23 14:35:16 -0200 | [diff] [blame] | 906 | struct kvm_memory_slot *memslot = &slots->memslots[i]; |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 907 | |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 908 | if (memslot->flags & KVM_MEMSLOT_INVALID) |
| 909 | continue; |
| 910 | |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 911 | if (gfn >= memslot->base_gfn |
| 912 | && gfn < memslot->base_gfn + memslot->npages) |
| 913 | return 1; |
| 914 | } |
| 915 | return 0; |
| 916 | } |
| 917 | EXPORT_SYMBOL_GPL(kvm_is_visible_gfn); |
| 918 | |
Joerg Roedel | 8f0b1ab | 2010-01-28 12:37:56 +0100 | [diff] [blame] | 919 | unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn) |
| 920 | { |
| 921 | struct vm_area_struct *vma; |
| 922 | unsigned long addr, size; |
| 923 | |
| 924 | size = PAGE_SIZE; |
| 925 | |
| 926 | addr = gfn_to_hva(kvm, gfn); |
| 927 | if (kvm_is_error_hva(addr)) |
| 928 | return PAGE_SIZE; |
| 929 | |
| 930 | down_read(¤t->mm->mmap_sem); |
| 931 | vma = find_vma(current->mm, addr); |
| 932 | if (!vma) |
| 933 | goto out; |
| 934 | |
| 935 | size = vma_kernel_pagesize(vma); |
| 936 | |
| 937 | out: |
| 938 | up_read(¤t->mm->mmap_sem); |
| 939 | |
| 940 | return size; |
| 941 | } |
| 942 | |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 943 | int memslot_id(struct kvm *kvm, gfn_t gfn) |
| 944 | { |
| 945 | int i; |
Lai Jiangshan | 90d83dc | 2010-04-19 17:41:23 +0800 | [diff] [blame] | 946 | struct kvm_memslots *slots = kvm_memslots(kvm); |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 947 | struct kvm_memory_slot *memslot = NULL; |
| 948 | |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 949 | for (i = 0; i < slots->nmemslots; ++i) { |
| 950 | memslot = &slots->memslots[i]; |
| 951 | |
| 952 | if (gfn >= memslot->base_gfn |
| 953 | && gfn < memslot->base_gfn + memslot->npages) |
| 954 | break; |
| 955 | } |
| 956 | |
| 957 | return memslot - slots->memslots; |
| 958 | } |
| 959 | |
Gleb Natapov | 49c7754 | 2010-10-18 15:22:23 +0200 | [diff] [blame] | 960 | static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn, |
Xiao Guangrong | 4898778 | 2010-08-22 19:11:43 +0800 | [diff] [blame] | 961 | gfn_t *nr_pages) |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 962 | { |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 963 | if (!slot || slot->flags & KVM_MEMSLOT_INVALID) |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 964 | return bad_hva(); |
Xiao Guangrong | 4898778 | 2010-08-22 19:11:43 +0800 | [diff] [blame] | 965 | |
| 966 | if (nr_pages) |
| 967 | *nr_pages = slot->npages - (gfn - slot->base_gfn); |
| 968 | |
Takuya Yoshikawa | f5c9803 | 2010-02-25 11:33:19 +0900 | [diff] [blame] | 969 | return gfn_to_hva_memslot(slot, gfn); |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 970 | } |
Xiao Guangrong | 4898778 | 2010-08-22 19:11:43 +0800 | [diff] [blame] | 971 | |
| 972 | unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn) |
| 973 | { |
Gleb Natapov | 49c7754 | 2010-10-18 15:22:23 +0200 | [diff] [blame] | 974 | return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL); |
Xiao Guangrong | 4898778 | 2010-08-22 19:11:43 +0800 | [diff] [blame] | 975 | } |
Sheng Yang | 0d15029 | 2008-04-25 21:44:50 +0800 | [diff] [blame] | 976 | EXPORT_SYMBOL_GPL(gfn_to_hva); |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 977 | |
Gleb Natapov | 8030089 | 2010-10-19 18:13:41 +0200 | [diff] [blame] | 978 | static pfn_t get_fault_pfn(void) |
| 979 | { |
| 980 | get_page(fault_page); |
| 981 | return fault_pfn; |
| 982 | } |
| 983 | |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 984 | static pfn_t hva_to_pfn(struct kvm *kvm, unsigned long addr, bool atomic, |
Marcelo Tosatti | 612819c | 2010-10-22 14:18:18 -0200 | [diff] [blame] | 985 | bool *async, bool write_fault, bool *writable) |
Avi Kivity | 954bbbc | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 986 | { |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 987 | struct page *page[1]; |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 988 | int npages = 0; |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 989 | pfn_t pfn; |
Avi Kivity | 954bbbc | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 990 | |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 991 | /* we can do it either atomically or asynchronously, not both */ |
| 992 | BUG_ON(atomic && async); |
| 993 | |
Marcelo Tosatti | 612819c | 2010-10-22 14:18:18 -0200 | [diff] [blame] | 994 | BUG_ON(!write_fault && !writable); |
| 995 | |
| 996 | if (writable) |
| 997 | *writable = true; |
| 998 | |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 999 | if (atomic || async) |
Xiao Guangrong | 887c08a | 2010-08-22 19:10:28 +0800 | [diff] [blame] | 1000 | npages = __get_user_pages_fast(addr, 1, 1, page); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 1001 | |
| 1002 | if (unlikely(npages != 1) && !atomic) { |
Xiao Guangrong | 887c08a | 2010-08-22 19:10:28 +0800 | [diff] [blame] | 1003 | might_sleep(); |
Marcelo Tosatti | 612819c | 2010-10-22 14:18:18 -0200 | [diff] [blame] | 1004 | |
| 1005 | if (writable) |
| 1006 | *writable = write_fault; |
| 1007 | |
| 1008 | npages = get_user_pages_fast(addr, 1, write_fault, page); |
| 1009 | |
| 1010 | /* map read fault as writable if possible */ |
| 1011 | if (unlikely(!write_fault) && npages == 1) { |
| 1012 | struct page *wpage[1]; |
| 1013 | |
| 1014 | npages = __get_user_pages_fast(addr, 1, 1, wpage); |
| 1015 | if (npages == 1) { |
| 1016 | *writable = true; |
| 1017 | put_page(page[0]); |
| 1018 | page[0] = wpage[0]; |
| 1019 | } |
| 1020 | npages = 1; |
| 1021 | } |
Xiao Guangrong | 887c08a | 2010-08-22 19:10:28 +0800 | [diff] [blame] | 1022 | } |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 1023 | |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 1024 | if (unlikely(npages != 1)) { |
| 1025 | struct vm_area_struct *vma; |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 1026 | |
Xiao Guangrong | 887c08a | 2010-08-22 19:10:28 +0800 | [diff] [blame] | 1027 | if (atomic) |
Gleb Natapov | 8030089 | 2010-10-19 18:13:41 +0200 | [diff] [blame] | 1028 | return get_fault_pfn(); |
Xiao Guangrong | 887c08a | 2010-08-22 19:10:28 +0800 | [diff] [blame] | 1029 | |
Huang Ying | bbeb340 | 2010-06-22 14:23:11 +0800 | [diff] [blame] | 1030 | down_read(¤t->mm->mmap_sem); |
Huang Ying | bf99815 | 2010-05-31 14:28:19 +0800 | [diff] [blame] | 1031 | if (is_hwpoison_address(addr)) { |
Huang Ying | bbeb340 | 2010-06-22 14:23:11 +0800 | [diff] [blame] | 1032 | up_read(¤t->mm->mmap_sem); |
Huang Ying | bf99815 | 2010-05-31 14:28:19 +0800 | [diff] [blame] | 1033 | get_page(hwpoison_page); |
| 1034 | return page_to_pfn(hwpoison_page); |
| 1035 | } |
| 1036 | |
Gleb Natapov | 8030089 | 2010-10-19 18:13:41 +0200 | [diff] [blame] | 1037 | vma = find_vma_intersection(current->mm, addr, addr+1); |
Marcelo Tosatti | 4c2155c | 2008-09-16 20:54:47 -0300 | [diff] [blame] | 1038 | |
Gleb Natapov | 8030089 | 2010-10-19 18:13:41 +0200 | [diff] [blame] | 1039 | if (vma == NULL) |
| 1040 | pfn = get_fault_pfn(); |
| 1041 | else if ((vma->vm_flags & VM_PFNMAP)) { |
| 1042 | pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + |
| 1043 | vma->vm_pgoff; |
| 1044 | BUG_ON(!kvm_is_mmio_pfn(pfn)); |
| 1045 | } else { |
| 1046 | if (async && (vma->vm_flags & VM_WRITE)) |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 1047 | *async = true; |
Gleb Natapov | 8030089 | 2010-10-19 18:13:41 +0200 | [diff] [blame] | 1048 | pfn = get_fault_pfn(); |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 1049 | } |
Marcelo Tosatti | 4c2155c | 2008-09-16 20:54:47 -0300 | [diff] [blame] | 1050 | up_read(¤t->mm->mmap_sem); |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 1051 | } else |
| 1052 | pfn = page_to_pfn(page[0]); |
| 1053 | |
| 1054 | return pfn; |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1055 | } |
| 1056 | |
Xiao Guangrong | 887c08a | 2010-08-22 19:10:28 +0800 | [diff] [blame] | 1057 | pfn_t hva_to_pfn_atomic(struct kvm *kvm, unsigned long addr) |
| 1058 | { |
Marcelo Tosatti | 612819c | 2010-10-22 14:18:18 -0200 | [diff] [blame] | 1059 | return hva_to_pfn(kvm, addr, true, NULL, true, NULL); |
Xiao Guangrong | 887c08a | 2010-08-22 19:10:28 +0800 | [diff] [blame] | 1060 | } |
| 1061 | EXPORT_SYMBOL_GPL(hva_to_pfn_atomic); |
| 1062 | |
Marcelo Tosatti | 612819c | 2010-10-22 14:18:18 -0200 | [diff] [blame] | 1063 | static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async, |
| 1064 | bool write_fault, bool *writable) |
Marcelo Tosatti | 506f0d6 | 2009-12-23 14:35:19 -0200 | [diff] [blame] | 1065 | { |
| 1066 | unsigned long addr; |
| 1067 | |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 1068 | if (async) |
| 1069 | *async = false; |
| 1070 | |
Marcelo Tosatti | 506f0d6 | 2009-12-23 14:35:19 -0200 | [diff] [blame] | 1071 | addr = gfn_to_hva(kvm, gfn); |
| 1072 | if (kvm_is_error_hva(addr)) { |
| 1073 | get_page(bad_page); |
| 1074 | return page_to_pfn(bad_page); |
| 1075 | } |
| 1076 | |
Marcelo Tosatti | 612819c | 2010-10-22 14:18:18 -0200 | [diff] [blame] | 1077 | return hva_to_pfn(kvm, addr, atomic, async, write_fault, writable); |
Xiao Guangrong | 365fb3f | 2010-08-28 19:24:13 +0800 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn) |
| 1081 | { |
Marcelo Tosatti | 612819c | 2010-10-22 14:18:18 -0200 | [diff] [blame] | 1082 | return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL); |
Xiao Guangrong | 365fb3f | 2010-08-28 19:24:13 +0800 | [diff] [blame] | 1083 | } |
| 1084 | EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic); |
| 1085 | |
Marcelo Tosatti | 612819c | 2010-10-22 14:18:18 -0200 | [diff] [blame] | 1086 | pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async, |
| 1087 | bool write_fault, bool *writable) |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 1088 | { |
Marcelo Tosatti | 612819c | 2010-10-22 14:18:18 -0200 | [diff] [blame] | 1089 | return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 1090 | } |
| 1091 | EXPORT_SYMBOL_GPL(gfn_to_pfn_async); |
| 1092 | |
Xiao Guangrong | 365fb3f | 2010-08-28 19:24:13 +0800 | [diff] [blame] | 1093 | pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn) |
| 1094 | { |
Marcelo Tosatti | 612819c | 2010-10-22 14:18:18 -0200 | [diff] [blame] | 1095 | return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL); |
Marcelo Tosatti | 506f0d6 | 2009-12-23 14:35:19 -0200 | [diff] [blame] | 1096 | } |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1097 | EXPORT_SYMBOL_GPL(gfn_to_pfn); |
| 1098 | |
Marcelo Tosatti | 612819c | 2010-10-22 14:18:18 -0200 | [diff] [blame] | 1099 | pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault, |
| 1100 | bool *writable) |
| 1101 | { |
| 1102 | return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable); |
| 1103 | } |
| 1104 | EXPORT_SYMBOL_GPL(gfn_to_pfn_prot); |
| 1105 | |
Marcelo Tosatti | 506f0d6 | 2009-12-23 14:35:19 -0200 | [diff] [blame] | 1106 | pfn_t gfn_to_pfn_memslot(struct kvm *kvm, |
| 1107 | struct kvm_memory_slot *slot, gfn_t gfn) |
| 1108 | { |
| 1109 | unsigned long addr = gfn_to_hva_memslot(slot, gfn); |
Marcelo Tosatti | 612819c | 2010-10-22 14:18:18 -0200 | [diff] [blame] | 1110 | return hva_to_pfn(kvm, addr, false, NULL, true, NULL); |
Marcelo Tosatti | 506f0d6 | 2009-12-23 14:35:19 -0200 | [diff] [blame] | 1111 | } |
| 1112 | |
Xiao Guangrong | 4898778 | 2010-08-22 19:11:43 +0800 | [diff] [blame] | 1113 | int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages, |
| 1114 | int nr_pages) |
| 1115 | { |
| 1116 | unsigned long addr; |
| 1117 | gfn_t entry; |
| 1118 | |
Gleb Natapov | 49c7754 | 2010-10-18 15:22:23 +0200 | [diff] [blame] | 1119 | addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry); |
Xiao Guangrong | 4898778 | 2010-08-22 19:11:43 +0800 | [diff] [blame] | 1120 | if (kvm_is_error_hva(addr)) |
| 1121 | return -1; |
| 1122 | |
| 1123 | if (entry < nr_pages) |
| 1124 | return 0; |
| 1125 | |
| 1126 | return __get_user_pages_fast(addr, nr_pages, 1, pages); |
| 1127 | } |
| 1128 | EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic); |
| 1129 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1130 | struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn) |
| 1131 | { |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 1132 | pfn_t pfn; |
| 1133 | |
| 1134 | pfn = gfn_to_pfn(kvm, gfn); |
Xiantao Zhang | c77fb9d | 2008-09-27 10:55:40 +0800 | [diff] [blame] | 1135 | if (!kvm_is_mmio_pfn(pfn)) |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 1136 | return pfn_to_page(pfn); |
| 1137 | |
Xiantao Zhang | c77fb9d | 2008-09-27 10:55:40 +0800 | [diff] [blame] | 1138 | WARN_ON(kvm_is_mmio_pfn(pfn)); |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 1139 | |
| 1140 | get_page(bad_page); |
| 1141 | return bad_page; |
Avi Kivity | 954bbbc | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 1142 | } |
Anthony Liguori | aab61cc | 2007-10-29 15:15:20 -0500 | [diff] [blame] | 1143 | |
Avi Kivity | 954bbbc | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 1144 | EXPORT_SYMBOL_GPL(gfn_to_page); |
| 1145 | |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 1146 | void kvm_release_page_clean(struct page *page) |
| 1147 | { |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1148 | kvm_release_pfn_clean(page_to_pfn(page)); |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 1149 | } |
| 1150 | EXPORT_SYMBOL_GPL(kvm_release_page_clean); |
| 1151 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1152 | void kvm_release_pfn_clean(pfn_t pfn) |
| 1153 | { |
Xiantao Zhang | c77fb9d | 2008-09-27 10:55:40 +0800 | [diff] [blame] | 1154 | if (!kvm_is_mmio_pfn(pfn)) |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 1155 | put_page(pfn_to_page(pfn)); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1156 | } |
| 1157 | EXPORT_SYMBOL_GPL(kvm_release_pfn_clean); |
| 1158 | |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 1159 | void kvm_release_page_dirty(struct page *page) |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 1160 | { |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1161 | kvm_release_pfn_dirty(page_to_pfn(page)); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 1162 | } |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 1163 | EXPORT_SYMBOL_GPL(kvm_release_page_dirty); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 1164 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1165 | void kvm_release_pfn_dirty(pfn_t pfn) |
| 1166 | { |
| 1167 | kvm_set_pfn_dirty(pfn); |
| 1168 | kvm_release_pfn_clean(pfn); |
| 1169 | } |
| 1170 | EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty); |
| 1171 | |
| 1172 | void kvm_set_page_dirty(struct page *page) |
| 1173 | { |
| 1174 | kvm_set_pfn_dirty(page_to_pfn(page)); |
| 1175 | } |
| 1176 | EXPORT_SYMBOL_GPL(kvm_set_page_dirty); |
| 1177 | |
| 1178 | void kvm_set_pfn_dirty(pfn_t pfn) |
| 1179 | { |
Xiantao Zhang | c77fb9d | 2008-09-27 10:55:40 +0800 | [diff] [blame] | 1180 | if (!kvm_is_mmio_pfn(pfn)) { |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 1181 | struct page *page = pfn_to_page(pfn); |
| 1182 | if (!PageReserved(page)) |
| 1183 | SetPageDirty(page); |
| 1184 | } |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1185 | } |
| 1186 | EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty); |
| 1187 | |
| 1188 | void kvm_set_pfn_accessed(pfn_t pfn) |
| 1189 | { |
Xiantao Zhang | c77fb9d | 2008-09-27 10:55:40 +0800 | [diff] [blame] | 1190 | if (!kvm_is_mmio_pfn(pfn)) |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 1191 | mark_page_accessed(pfn_to_page(pfn)); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1192 | } |
| 1193 | EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed); |
| 1194 | |
| 1195 | void kvm_get_pfn(pfn_t pfn) |
| 1196 | { |
Xiantao Zhang | c77fb9d | 2008-09-27 10:55:40 +0800 | [diff] [blame] | 1197 | if (!kvm_is_mmio_pfn(pfn)) |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 1198 | get_page(pfn_to_page(pfn)); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1199 | } |
| 1200 | EXPORT_SYMBOL_GPL(kvm_get_pfn); |
| 1201 | |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1202 | static int next_segment(unsigned long len, int offset) |
| 1203 | { |
| 1204 | if (len > PAGE_SIZE - offset) |
| 1205 | return PAGE_SIZE - offset; |
| 1206 | else |
| 1207 | return len; |
| 1208 | } |
| 1209 | |
| 1210 | int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, |
| 1211 | int len) |
| 1212 | { |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 1213 | int r; |
| 1214 | unsigned long addr; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1215 | |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 1216 | addr = gfn_to_hva(kvm, gfn); |
| 1217 | if (kvm_is_error_hva(addr)) |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1218 | return -EFAULT; |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 1219 | r = copy_from_user(data, (void __user *)addr + offset, len); |
| 1220 | if (r) |
| 1221 | return -EFAULT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1222 | return 0; |
| 1223 | } |
| 1224 | EXPORT_SYMBOL_GPL(kvm_read_guest_page); |
| 1225 | |
| 1226 | int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len) |
| 1227 | { |
| 1228 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 1229 | int seg; |
| 1230 | int offset = offset_in_page(gpa); |
| 1231 | int ret; |
| 1232 | |
| 1233 | while ((seg = next_segment(len, offset)) != 0) { |
| 1234 | ret = kvm_read_guest_page(kvm, gfn, data, offset, seg); |
| 1235 | if (ret < 0) |
| 1236 | return ret; |
| 1237 | offset = 0; |
| 1238 | len -= seg; |
| 1239 | data += seg; |
| 1240 | ++gfn; |
| 1241 | } |
| 1242 | return 0; |
| 1243 | } |
| 1244 | EXPORT_SYMBOL_GPL(kvm_read_guest); |
| 1245 | |
Marcelo Tosatti | 7ec5458 | 2007-12-20 19:18:23 -0500 | [diff] [blame] | 1246 | int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data, |
| 1247 | unsigned long len) |
| 1248 | { |
| 1249 | int r; |
| 1250 | unsigned long addr; |
| 1251 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 1252 | int offset = offset_in_page(gpa); |
| 1253 | |
| 1254 | addr = gfn_to_hva(kvm, gfn); |
| 1255 | if (kvm_is_error_hva(addr)) |
| 1256 | return -EFAULT; |
Andrea Arcangeli | 0aac03f | 2008-01-30 19:57:35 +0100 | [diff] [blame] | 1257 | pagefault_disable(); |
Marcelo Tosatti | 7ec5458 | 2007-12-20 19:18:23 -0500 | [diff] [blame] | 1258 | r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len); |
Andrea Arcangeli | 0aac03f | 2008-01-30 19:57:35 +0100 | [diff] [blame] | 1259 | pagefault_enable(); |
Marcelo Tosatti | 7ec5458 | 2007-12-20 19:18:23 -0500 | [diff] [blame] | 1260 | if (r) |
| 1261 | return -EFAULT; |
| 1262 | return 0; |
| 1263 | } |
| 1264 | EXPORT_SYMBOL(kvm_read_guest_atomic); |
| 1265 | |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1266 | int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data, |
| 1267 | int offset, int len) |
| 1268 | { |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 1269 | int r; |
| 1270 | unsigned long addr; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1271 | |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 1272 | addr = gfn_to_hva(kvm, gfn); |
| 1273 | if (kvm_is_error_hva(addr)) |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1274 | return -EFAULT; |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 1275 | r = copy_to_user((void __user *)addr + offset, data, len); |
| 1276 | if (r) |
| 1277 | return -EFAULT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1278 | mark_page_dirty(kvm, gfn); |
| 1279 | return 0; |
| 1280 | } |
| 1281 | EXPORT_SYMBOL_GPL(kvm_write_guest_page); |
| 1282 | |
| 1283 | int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data, |
| 1284 | unsigned long len) |
| 1285 | { |
| 1286 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 1287 | int seg; |
| 1288 | int offset = offset_in_page(gpa); |
| 1289 | int ret; |
| 1290 | |
| 1291 | while ((seg = next_segment(len, offset)) != 0) { |
| 1292 | ret = kvm_write_guest_page(kvm, gfn, data, offset, seg); |
| 1293 | if (ret < 0) |
| 1294 | return ret; |
| 1295 | offset = 0; |
| 1296 | len -= seg; |
| 1297 | data += seg; |
| 1298 | ++gfn; |
| 1299 | } |
| 1300 | return 0; |
| 1301 | } |
| 1302 | |
Gleb Natapov | 49c7754 | 2010-10-18 15:22:23 +0200 | [diff] [blame] | 1303 | int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc, |
| 1304 | gpa_t gpa) |
| 1305 | { |
| 1306 | struct kvm_memslots *slots = kvm_memslots(kvm); |
| 1307 | int offset = offset_in_page(gpa); |
| 1308 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 1309 | |
| 1310 | ghc->gpa = gpa; |
| 1311 | ghc->generation = slots->generation; |
| 1312 | ghc->memslot = __gfn_to_memslot(slots, gfn); |
| 1313 | ghc->hva = gfn_to_hva_many(ghc->memslot, gfn, NULL); |
| 1314 | if (!kvm_is_error_hva(ghc->hva)) |
| 1315 | ghc->hva += offset; |
| 1316 | else |
| 1317 | return -EFAULT; |
| 1318 | |
| 1319 | return 0; |
| 1320 | } |
| 1321 | EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init); |
| 1322 | |
| 1323 | int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, |
| 1324 | void *data, unsigned long len) |
| 1325 | { |
| 1326 | struct kvm_memslots *slots = kvm_memslots(kvm); |
| 1327 | int r; |
| 1328 | |
| 1329 | if (slots->generation != ghc->generation) |
| 1330 | kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa); |
| 1331 | |
| 1332 | if (kvm_is_error_hva(ghc->hva)) |
| 1333 | return -EFAULT; |
| 1334 | |
| 1335 | r = copy_to_user((void __user *)ghc->hva, data, len); |
| 1336 | if (r) |
| 1337 | return -EFAULT; |
| 1338 | mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT); |
| 1339 | |
| 1340 | return 0; |
| 1341 | } |
| 1342 | EXPORT_SYMBOL_GPL(kvm_write_guest_cached); |
| 1343 | |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1344 | int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len) |
| 1345 | { |
Izik Eidus | 3e021bf | 2007-11-19 11:16:57 +0200 | [diff] [blame] | 1346 | return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1347 | } |
| 1348 | EXPORT_SYMBOL_GPL(kvm_clear_guest_page); |
| 1349 | |
| 1350 | int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len) |
| 1351 | { |
| 1352 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 1353 | int seg; |
| 1354 | int offset = offset_in_page(gpa); |
| 1355 | int ret; |
| 1356 | |
| 1357 | while ((seg = next_segment(len, offset)) != 0) { |
| 1358 | ret = kvm_clear_guest_page(kvm, gfn, offset, seg); |
| 1359 | if (ret < 0) |
| 1360 | return ret; |
| 1361 | offset = 0; |
| 1362 | len -= seg; |
| 1363 | ++gfn; |
| 1364 | } |
| 1365 | return 0; |
| 1366 | } |
| 1367 | EXPORT_SYMBOL_GPL(kvm_clear_guest); |
| 1368 | |
Gleb Natapov | 49c7754 | 2010-10-18 15:22:23 +0200 | [diff] [blame] | 1369 | void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot, |
| 1370 | gfn_t gfn) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1371 | { |
Rusty Russell | 7e9d619 | 2007-07-31 20:41:14 +1000 | [diff] [blame] | 1372 | if (memslot && memslot->dirty_bitmap) { |
| 1373 | unsigned long rel_gfn = gfn - memslot->base_gfn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1374 | |
Takuya Yoshikawa | d147693 | 2010-04-23 17:48:35 +0900 | [diff] [blame] | 1375 | generic___set_le_bit(rel_gfn, memslot->dirty_bitmap); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1376 | } |
| 1377 | } |
| 1378 | |
Gleb Natapov | 49c7754 | 2010-10-18 15:22:23 +0200 | [diff] [blame] | 1379 | void mark_page_dirty(struct kvm *kvm, gfn_t gfn) |
| 1380 | { |
| 1381 | struct kvm_memory_slot *memslot; |
| 1382 | |
| 1383 | memslot = gfn_to_memslot(kvm, gfn); |
| 1384 | mark_page_dirty_in_slot(kvm, memslot, gfn); |
| 1385 | } |
| 1386 | |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 1387 | /* |
| 1388 | * The vCPU has executed a HLT instruction with in-kernel mode enabled. |
| 1389 | */ |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 1390 | void kvm_vcpu_block(struct kvm_vcpu *vcpu) |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 1391 | { |
Marcelo Tosatti | e5c239c | 2008-05-08 19:47:01 -0300 | [diff] [blame] | 1392 | DEFINE_WAIT(wait); |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 1393 | |
Marcelo Tosatti | e5c239c | 2008-05-08 19:47:01 -0300 | [diff] [blame] | 1394 | for (;;) { |
| 1395 | prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE); |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 1396 | |
Gleb Natapov | a1b3710 | 2009-07-09 15:33:52 +0300 | [diff] [blame] | 1397 | if (kvm_arch_vcpu_runnable(vcpu)) { |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 1398 | kvm_make_request(KVM_REQ_UNHALT, vcpu); |
Marcelo Tosatti | e5c239c | 2008-05-08 19:47:01 -0300 | [diff] [blame] | 1399 | break; |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 1400 | } |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 1401 | if (kvm_cpu_has_pending_timer(vcpu)) |
| 1402 | break; |
Marcelo Tosatti | e5c239c | 2008-05-08 19:47:01 -0300 | [diff] [blame] | 1403 | if (signal_pending(current)) |
| 1404 | break; |
| 1405 | |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 1406 | schedule(); |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 1407 | } |
| 1408 | |
Marcelo Tosatti | e5c239c | 2008-05-08 19:47:01 -0300 | [diff] [blame] | 1409 | finish_wait(&vcpu->wq, &wait); |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 1410 | } |
| 1411 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1412 | void kvm_resched(struct kvm_vcpu *vcpu) |
| 1413 | { |
Yaozu Dong | 3fca036 | 2007-04-25 16:49:19 +0300 | [diff] [blame] | 1414 | if (!need_resched()) |
| 1415 | return; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1416 | cond_resched(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1417 | } |
| 1418 | EXPORT_SYMBOL_GPL(kvm_resched); |
| 1419 | |
Zhai, Edwin | d255f4f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 1420 | void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu) |
| 1421 | { |
| 1422 | ktime_t expires; |
| 1423 | DEFINE_WAIT(wait); |
| 1424 | |
| 1425 | prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE); |
| 1426 | |
| 1427 | /* Sleep for 100 us, and hope lock-holder got scheduled */ |
| 1428 | expires = ktime_add_ns(ktime_get(), 100000UL); |
| 1429 | schedule_hrtimeout(&expires, HRTIMER_MODE_ABS); |
| 1430 | |
| 1431 | finish_wait(&vcpu->wq, &wait); |
| 1432 | } |
| 1433 | EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin); |
| 1434 | |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1435 | static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 1436 | { |
| 1437 | struct kvm_vcpu *vcpu = vma->vm_file->private_data; |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 1438 | struct page *page; |
| 1439 | |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1440 | if (vmf->pgoff == 0) |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1441 | page = virt_to_page(vcpu->run); |
Avi Kivity | 0956676 | 2008-01-23 18:14:23 +0200 | [diff] [blame] | 1442 | #ifdef CONFIG_X86 |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1443 | else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1444 | page = virt_to_page(vcpu->arch.pio_data); |
Avi Kivity | 0956676 | 2008-01-23 18:14:23 +0200 | [diff] [blame] | 1445 | #endif |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 1446 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
| 1447 | else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET) |
| 1448 | page = virt_to_page(vcpu->kvm->coalesced_mmio_ring); |
| 1449 | #endif |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1450 | else |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1451 | return VM_FAULT_SIGBUS; |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 1452 | get_page(page); |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1453 | vmf->page = page; |
| 1454 | return 0; |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 1455 | } |
| 1456 | |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 1457 | static const struct vm_operations_struct kvm_vcpu_vm_ops = { |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1458 | .fault = kvm_vcpu_fault, |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 1459 | }; |
| 1460 | |
| 1461 | static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma) |
| 1462 | { |
| 1463 | vma->vm_ops = &kvm_vcpu_vm_ops; |
| 1464 | return 0; |
| 1465 | } |
| 1466 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1467 | static int kvm_vcpu_release(struct inode *inode, struct file *filp) |
| 1468 | { |
| 1469 | struct kvm_vcpu *vcpu = filp->private_data; |
| 1470 | |
Al Viro | 66c0b39 | 2008-04-19 20:33:56 +0100 | [diff] [blame] | 1471 | kvm_put_kvm(vcpu->kvm); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1472 | return 0; |
| 1473 | } |
| 1474 | |
Christian Borntraeger | 3d3aab1 | 2008-12-02 11:17:32 +0100 | [diff] [blame] | 1475 | static struct file_operations kvm_vcpu_fops = { |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1476 | .release = kvm_vcpu_release, |
| 1477 | .unlocked_ioctl = kvm_vcpu_ioctl, |
| 1478 | .compat_ioctl = kvm_vcpu_ioctl, |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 1479 | .mmap = kvm_vcpu_mmap, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1480 | .llseek = noop_llseek, |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1481 | }; |
| 1482 | |
| 1483 | /* |
| 1484 | * Allocates an inode for the vcpu. |
| 1485 | */ |
| 1486 | static int create_vcpu_fd(struct kvm_vcpu *vcpu) |
| 1487 | { |
Roland Dreier | 628ff7c | 2009-12-18 09:41:24 -0800 | [diff] [blame] | 1488 | return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1489 | } |
| 1490 | |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 1491 | /* |
| 1492 | * Creates some virtual cpus. Good luck creating more than one. |
| 1493 | */ |
Gleb Natapov | 73880c8 | 2009-06-09 15:56:28 +0300 | [diff] [blame] | 1494 | static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id) |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 1495 | { |
| 1496 | int r; |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 1497 | struct kvm_vcpu *vcpu, *v; |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 1498 | |
Gleb Natapov | 73880c8 | 2009-06-09 15:56:28 +0300 | [diff] [blame] | 1499 | vcpu = kvm_arch_vcpu_create(kvm, id); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1500 | if (IS_ERR(vcpu)) |
| 1501 | return PTR_ERR(vcpu); |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 1502 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1503 | preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops); |
| 1504 | |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 1505 | r = kvm_arch_vcpu_setup(vcpu); |
| 1506 | if (r) |
Glauber Costa | 7d8fece | 2008-09-17 23:16:59 -0300 | [diff] [blame] | 1507 | return r; |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 1508 | |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 1509 | mutex_lock(&kvm->lock); |
Gleb Natapov | 73880c8 | 2009-06-09 15:56:28 +0300 | [diff] [blame] | 1510 | if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) { |
| 1511 | r = -EINVAL; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1512 | goto vcpu_destroy; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1513 | } |
Gleb Natapov | 73880c8 | 2009-06-09 15:56:28 +0300 | [diff] [blame] | 1514 | |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 1515 | kvm_for_each_vcpu(r, v, kvm) |
| 1516 | if (v->vcpu_id == id) { |
Gleb Natapov | 73880c8 | 2009-06-09 15:56:28 +0300 | [diff] [blame] | 1517 | r = -EEXIST; |
| 1518 | goto vcpu_destroy; |
| 1519 | } |
| 1520 | |
| 1521 | BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1522 | |
| 1523 | /* Now it's all set up, let userspace reach it */ |
Al Viro | 66c0b39 | 2008-04-19 20:33:56 +0100 | [diff] [blame] | 1524 | kvm_get_kvm(kvm); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1525 | r = create_vcpu_fd(vcpu); |
Gleb Natapov | 73880c8 | 2009-06-09 15:56:28 +0300 | [diff] [blame] | 1526 | if (r < 0) { |
| 1527 | kvm_put_kvm(kvm); |
| 1528 | goto vcpu_destroy; |
| 1529 | } |
| 1530 | |
| 1531 | kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu; |
| 1532 | smp_wmb(); |
| 1533 | atomic_inc(&kvm->online_vcpus); |
| 1534 | |
| 1535 | #ifdef CONFIG_KVM_APIC_ARCHITECTURE |
| 1536 | if (kvm->bsp_vcpu_id == id) |
| 1537 | kvm->bsp_vcpu = vcpu; |
| 1538 | #endif |
| 1539 | mutex_unlock(&kvm->lock); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1540 | return r; |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 1541 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1542 | vcpu_destroy: |
Glauber Costa | 7d8fece | 2008-09-17 23:16:59 -0300 | [diff] [blame] | 1543 | mutex_unlock(&kvm->lock); |
Hollis Blanchard | d40ccc6 | 2007-11-19 14:04:43 -0600 | [diff] [blame] | 1544 | kvm_arch_vcpu_destroy(vcpu); |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 1545 | return r; |
| 1546 | } |
| 1547 | |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 1548 | static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) |
| 1549 | { |
| 1550 | if (sigset) { |
| 1551 | sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP)); |
| 1552 | vcpu->sigset_active = 1; |
| 1553 | vcpu->sigset = *sigset; |
| 1554 | } else |
| 1555 | vcpu->sigset_active = 0; |
| 1556 | return 0; |
| 1557 | } |
| 1558 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1559 | static long kvm_vcpu_ioctl(struct file *filp, |
| 1560 | unsigned int ioctl, unsigned long arg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1561 | { |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1562 | struct kvm_vcpu *vcpu = filp->private_data; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 1563 | void __user *argp = (void __user *)arg; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1564 | int r; |
Dave Hansen | fa3795a | 2008-08-11 10:01:46 -0700 | [diff] [blame] | 1565 | struct kvm_fpu *fpu = NULL; |
| 1566 | struct kvm_sregs *kvm_sregs = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1567 | |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 1568 | if (vcpu->kvm->mm != current->mm) |
| 1569 | return -EIO; |
Avi Kivity | 2122ff5 | 2010-05-13 11:25:04 +0300 | [diff] [blame] | 1570 | |
| 1571 | #if defined(CONFIG_S390) || defined(CONFIG_PPC) |
| 1572 | /* |
| 1573 | * Special cases: vcpu ioctls that are asynchronous to vcpu execution, |
| 1574 | * so vcpu_load() would break it. |
| 1575 | */ |
| 1576 | if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT) |
| 1577 | return kvm_arch_vcpu_ioctl(filp, ioctl, arg); |
| 1578 | #endif |
| 1579 | |
| 1580 | |
| 1581 | vcpu_load(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1582 | switch (ioctl) { |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 1583 | case KVM_RUN: |
Avi Kivity | f0fe510 | 2007-03-07 13:11:17 +0200 | [diff] [blame] | 1584 | r = -EINVAL; |
| 1585 | if (arg) |
| 1586 | goto out; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 1587 | r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run); |
Gleb Natapov | 64be500 | 2010-10-24 16:49:08 +0200 | [diff] [blame] | 1588 | trace_kvm_userspace_exit(vcpu->run->exit_reason, r); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1589 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1590 | case KVM_GET_REGS: { |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1591 | struct kvm_regs *kvm_regs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1592 | |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1593 | r = -ENOMEM; |
| 1594 | kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL); |
| 1595 | if (!kvm_regs) |
| 1596 | goto out; |
| 1597 | r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1598 | if (r) |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1599 | goto out_free1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1600 | r = -EFAULT; |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1601 | if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs))) |
| 1602 | goto out_free1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1603 | r = 0; |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1604 | out_free1: |
| 1605 | kfree(kvm_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1606 | break; |
| 1607 | } |
| 1608 | case KVM_SET_REGS: { |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1609 | struct kvm_regs *kvm_regs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1610 | |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1611 | r = -ENOMEM; |
| 1612 | kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL); |
| 1613 | if (!kvm_regs) |
| 1614 | goto out; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1615 | r = -EFAULT; |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1616 | if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs))) |
| 1617 | goto out_free2; |
| 1618 | r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1619 | if (r) |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1620 | goto out_free2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1621 | r = 0; |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1622 | out_free2: |
| 1623 | kfree(kvm_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1624 | break; |
| 1625 | } |
| 1626 | case KVM_GET_SREGS: { |
Dave Hansen | fa3795a | 2008-08-11 10:01:46 -0700 | [diff] [blame] | 1627 | kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL); |
| 1628 | r = -ENOMEM; |
| 1629 | if (!kvm_sregs) |
| 1630 | goto out; |
| 1631 | r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1632 | if (r) |
| 1633 | goto out; |
| 1634 | r = -EFAULT; |
Dave Hansen | fa3795a | 2008-08-11 10:01:46 -0700 | [diff] [blame] | 1635 | if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs))) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1636 | goto out; |
| 1637 | r = 0; |
| 1638 | break; |
| 1639 | } |
| 1640 | case KVM_SET_SREGS: { |
Dave Hansen | fa3795a | 2008-08-11 10:01:46 -0700 | [diff] [blame] | 1641 | kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL); |
| 1642 | r = -ENOMEM; |
| 1643 | if (!kvm_sregs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1644 | goto out; |
Dave Hansen | fa3795a | 2008-08-11 10:01:46 -0700 | [diff] [blame] | 1645 | r = -EFAULT; |
| 1646 | if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs))) |
| 1647 | goto out; |
| 1648 | r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1649 | if (r) |
| 1650 | goto out; |
| 1651 | r = 0; |
| 1652 | break; |
| 1653 | } |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 1654 | case KVM_GET_MP_STATE: { |
| 1655 | struct kvm_mp_state mp_state; |
| 1656 | |
| 1657 | r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state); |
| 1658 | if (r) |
| 1659 | goto out; |
| 1660 | r = -EFAULT; |
| 1661 | if (copy_to_user(argp, &mp_state, sizeof mp_state)) |
| 1662 | goto out; |
| 1663 | r = 0; |
| 1664 | break; |
| 1665 | } |
| 1666 | case KVM_SET_MP_STATE: { |
| 1667 | struct kvm_mp_state mp_state; |
| 1668 | |
| 1669 | r = -EFAULT; |
| 1670 | if (copy_from_user(&mp_state, argp, sizeof mp_state)) |
| 1671 | goto out; |
| 1672 | r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state); |
| 1673 | if (r) |
| 1674 | goto out; |
| 1675 | r = 0; |
| 1676 | break; |
| 1677 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1678 | case KVM_TRANSLATE: { |
| 1679 | struct kvm_translation tr; |
| 1680 | |
| 1681 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 1682 | if (copy_from_user(&tr, argp, sizeof tr)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1683 | goto out; |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 1684 | r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1685 | if (r) |
| 1686 | goto out; |
| 1687 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 1688 | if (copy_to_user(argp, &tr, sizeof tr)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1689 | goto out; |
| 1690 | r = 0; |
| 1691 | break; |
| 1692 | } |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1693 | case KVM_SET_GUEST_DEBUG: { |
| 1694 | struct kvm_guest_debug dbg; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1695 | |
| 1696 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 1697 | if (copy_from_user(&dbg, argp, sizeof dbg)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1698 | goto out; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1699 | r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1700 | if (r) |
| 1701 | goto out; |
| 1702 | r = 0; |
| 1703 | break; |
| 1704 | } |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 1705 | case KVM_SET_SIGNAL_MASK: { |
| 1706 | struct kvm_signal_mask __user *sigmask_arg = argp; |
| 1707 | struct kvm_signal_mask kvm_sigmask; |
| 1708 | sigset_t sigset, *p; |
| 1709 | |
| 1710 | p = NULL; |
| 1711 | if (argp) { |
| 1712 | r = -EFAULT; |
| 1713 | if (copy_from_user(&kvm_sigmask, argp, |
| 1714 | sizeof kvm_sigmask)) |
| 1715 | goto out; |
| 1716 | r = -EINVAL; |
| 1717 | if (kvm_sigmask.len != sizeof sigset) |
| 1718 | goto out; |
| 1719 | r = -EFAULT; |
| 1720 | if (copy_from_user(&sigset, sigmask_arg->sigset, |
| 1721 | sizeof sigset)) |
| 1722 | goto out; |
| 1723 | p = &sigset; |
| 1724 | } |
Andi Kleen | 376d41f | 2010-06-10 13:10:47 +0200 | [diff] [blame] | 1725 | r = kvm_vcpu_ioctl_set_sigmask(vcpu, p); |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 1726 | break; |
| 1727 | } |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 1728 | case KVM_GET_FPU: { |
Dave Hansen | fa3795a | 2008-08-11 10:01:46 -0700 | [diff] [blame] | 1729 | fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL); |
| 1730 | r = -ENOMEM; |
| 1731 | if (!fpu) |
| 1732 | goto out; |
| 1733 | r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu); |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 1734 | if (r) |
| 1735 | goto out; |
| 1736 | r = -EFAULT; |
Dave Hansen | fa3795a | 2008-08-11 10:01:46 -0700 | [diff] [blame] | 1737 | if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu))) |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 1738 | goto out; |
| 1739 | r = 0; |
| 1740 | break; |
| 1741 | } |
| 1742 | case KVM_SET_FPU: { |
Dave Hansen | fa3795a | 2008-08-11 10:01:46 -0700 | [diff] [blame] | 1743 | fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL); |
| 1744 | r = -ENOMEM; |
| 1745 | if (!fpu) |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 1746 | goto out; |
Dave Hansen | fa3795a | 2008-08-11 10:01:46 -0700 | [diff] [blame] | 1747 | r = -EFAULT; |
| 1748 | if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu))) |
| 1749 | goto out; |
| 1750 | r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu); |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 1751 | if (r) |
| 1752 | goto out; |
| 1753 | r = 0; |
| 1754 | break; |
| 1755 | } |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1756 | default: |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1757 | r = kvm_arch_vcpu_ioctl(filp, ioctl, arg); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1758 | } |
| 1759 | out: |
Avi Kivity | 2122ff5 | 2010-05-13 11:25:04 +0300 | [diff] [blame] | 1760 | vcpu_put(vcpu); |
Dave Hansen | fa3795a | 2008-08-11 10:01:46 -0700 | [diff] [blame] | 1761 | kfree(fpu); |
| 1762 | kfree(kvm_sregs); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1763 | return r; |
| 1764 | } |
| 1765 | |
| 1766 | static long kvm_vm_ioctl(struct file *filp, |
| 1767 | unsigned int ioctl, unsigned long arg) |
| 1768 | { |
| 1769 | struct kvm *kvm = filp->private_data; |
| 1770 | void __user *argp = (void __user *)arg; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1771 | int r; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1772 | |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 1773 | if (kvm->mm != current->mm) |
| 1774 | return -EIO; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1775 | switch (ioctl) { |
| 1776 | case KVM_CREATE_VCPU: |
| 1777 | r = kvm_vm_ioctl_create_vcpu(kvm, arg); |
| 1778 | if (r < 0) |
| 1779 | goto out; |
| 1780 | break; |
Izik Eidus | 6fc138d | 2007-10-09 19:20:39 +0200 | [diff] [blame] | 1781 | case KVM_SET_USER_MEMORY_REGION: { |
| 1782 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 1783 | |
| 1784 | r = -EFAULT; |
| 1785 | if (copy_from_user(&kvm_userspace_mem, argp, |
| 1786 | sizeof kvm_userspace_mem)) |
| 1787 | goto out; |
| 1788 | |
| 1789 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1790 | if (r) |
| 1791 | goto out; |
| 1792 | break; |
| 1793 | } |
| 1794 | case KVM_GET_DIRTY_LOG: { |
| 1795 | struct kvm_dirty_log log; |
| 1796 | |
| 1797 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 1798 | if (copy_from_user(&log, argp, sizeof log)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1799 | goto out; |
Avi Kivity | 2c6f5df | 2007-02-20 18:27:58 +0200 | [diff] [blame] | 1800 | r = kvm_vm_ioctl_get_dirty_log(kvm, &log); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1801 | if (r) |
| 1802 | goto out; |
| 1803 | break; |
| 1804 | } |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 1805 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
| 1806 | case KVM_REGISTER_COALESCED_MMIO: { |
| 1807 | struct kvm_coalesced_mmio_zone zone; |
| 1808 | r = -EFAULT; |
| 1809 | if (copy_from_user(&zone, argp, sizeof zone)) |
| 1810 | goto out; |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 1811 | r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone); |
| 1812 | if (r) |
| 1813 | goto out; |
| 1814 | r = 0; |
| 1815 | break; |
| 1816 | } |
| 1817 | case KVM_UNREGISTER_COALESCED_MMIO: { |
| 1818 | struct kvm_coalesced_mmio_zone zone; |
| 1819 | r = -EFAULT; |
| 1820 | if (copy_from_user(&zone, argp, sizeof zone)) |
| 1821 | goto out; |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 1822 | r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone); |
| 1823 | if (r) |
| 1824 | goto out; |
| 1825 | r = 0; |
| 1826 | break; |
| 1827 | } |
| 1828 | #endif |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 1829 | case KVM_IRQFD: { |
| 1830 | struct kvm_irqfd data; |
| 1831 | |
| 1832 | r = -EFAULT; |
| 1833 | if (copy_from_user(&data, argp, sizeof data)) |
| 1834 | goto out; |
| 1835 | r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags); |
| 1836 | break; |
| 1837 | } |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 1838 | case KVM_IOEVENTFD: { |
| 1839 | struct kvm_ioeventfd data; |
| 1840 | |
| 1841 | r = -EFAULT; |
| 1842 | if (copy_from_user(&data, argp, sizeof data)) |
| 1843 | goto out; |
| 1844 | r = kvm_ioeventfd(kvm, &data); |
| 1845 | break; |
| 1846 | } |
Gleb Natapov | 73880c8 | 2009-06-09 15:56:28 +0300 | [diff] [blame] | 1847 | #ifdef CONFIG_KVM_APIC_ARCHITECTURE |
| 1848 | case KVM_SET_BOOT_CPU_ID: |
| 1849 | r = 0; |
Marcelo Tosatti | 894a9c5 | 2009-06-23 15:05:14 -0300 | [diff] [blame] | 1850 | mutex_lock(&kvm->lock); |
Gleb Natapov | 73880c8 | 2009-06-09 15:56:28 +0300 | [diff] [blame] | 1851 | if (atomic_read(&kvm->online_vcpus) != 0) |
| 1852 | r = -EBUSY; |
| 1853 | else |
| 1854 | kvm->bsp_vcpu_id = arg; |
Marcelo Tosatti | 894a9c5 | 2009-06-23 15:05:14 -0300 | [diff] [blame] | 1855 | mutex_unlock(&kvm->lock); |
Gleb Natapov | 73880c8 | 2009-06-09 15:56:28 +0300 | [diff] [blame] | 1856 | break; |
| 1857 | #endif |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1858 | default: |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1859 | r = kvm_arch_vm_ioctl(filp, ioctl, arg); |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 1860 | if (r == -ENOTTY) |
| 1861 | r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1862 | } |
| 1863 | out: |
| 1864 | return r; |
| 1865 | } |
| 1866 | |
Arnd Bergmann | 6ff5894 | 2009-10-22 14:19:27 +0200 | [diff] [blame] | 1867 | #ifdef CONFIG_COMPAT |
| 1868 | struct compat_kvm_dirty_log { |
| 1869 | __u32 slot; |
| 1870 | __u32 padding1; |
| 1871 | union { |
| 1872 | compat_uptr_t dirty_bitmap; /* one bit per page */ |
| 1873 | __u64 padding2; |
| 1874 | }; |
| 1875 | }; |
| 1876 | |
| 1877 | static long kvm_vm_compat_ioctl(struct file *filp, |
| 1878 | unsigned int ioctl, unsigned long arg) |
| 1879 | { |
| 1880 | struct kvm *kvm = filp->private_data; |
| 1881 | int r; |
| 1882 | |
| 1883 | if (kvm->mm != current->mm) |
| 1884 | return -EIO; |
| 1885 | switch (ioctl) { |
| 1886 | case KVM_GET_DIRTY_LOG: { |
| 1887 | struct compat_kvm_dirty_log compat_log; |
| 1888 | struct kvm_dirty_log log; |
| 1889 | |
| 1890 | r = -EFAULT; |
| 1891 | if (copy_from_user(&compat_log, (void __user *)arg, |
| 1892 | sizeof(compat_log))) |
| 1893 | goto out; |
| 1894 | log.slot = compat_log.slot; |
| 1895 | log.padding1 = compat_log.padding1; |
| 1896 | log.padding2 = compat_log.padding2; |
| 1897 | log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap); |
| 1898 | |
| 1899 | r = kvm_vm_ioctl_get_dirty_log(kvm, &log); |
| 1900 | if (r) |
| 1901 | goto out; |
| 1902 | break; |
| 1903 | } |
| 1904 | default: |
| 1905 | r = kvm_vm_ioctl(filp, ioctl, arg); |
| 1906 | } |
| 1907 | |
| 1908 | out: |
| 1909 | return r; |
| 1910 | } |
| 1911 | #endif |
| 1912 | |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1913 | static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1914 | { |
Marcelo Tosatti | 777b3f4 | 2008-09-16 20:54:46 -0300 | [diff] [blame] | 1915 | struct page *page[1]; |
| 1916 | unsigned long addr; |
| 1917 | int npages; |
| 1918 | gfn_t gfn = vmf->pgoff; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1919 | struct kvm *kvm = vma->vm_file->private_data; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1920 | |
Marcelo Tosatti | 777b3f4 | 2008-09-16 20:54:46 -0300 | [diff] [blame] | 1921 | addr = gfn_to_hva(kvm, gfn); |
| 1922 | if (kvm_is_error_hva(addr)) |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1923 | return VM_FAULT_SIGBUS; |
Marcelo Tosatti | 777b3f4 | 2008-09-16 20:54:46 -0300 | [diff] [blame] | 1924 | |
| 1925 | npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page, |
| 1926 | NULL); |
| 1927 | if (unlikely(npages != 1)) |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1928 | return VM_FAULT_SIGBUS; |
Marcelo Tosatti | 777b3f4 | 2008-09-16 20:54:46 -0300 | [diff] [blame] | 1929 | |
| 1930 | vmf->page = page[0]; |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1931 | return 0; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1932 | } |
| 1933 | |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 1934 | static const struct vm_operations_struct kvm_vm_vm_ops = { |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1935 | .fault = kvm_vm_fault, |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1936 | }; |
| 1937 | |
| 1938 | static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma) |
| 1939 | { |
| 1940 | vma->vm_ops = &kvm_vm_vm_ops; |
| 1941 | return 0; |
| 1942 | } |
| 1943 | |
Christian Borntraeger | 3d3aab1 | 2008-12-02 11:17:32 +0100 | [diff] [blame] | 1944 | static struct file_operations kvm_vm_fops = { |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1945 | .release = kvm_vm_release, |
| 1946 | .unlocked_ioctl = kvm_vm_ioctl, |
Arnd Bergmann | 6ff5894 | 2009-10-22 14:19:27 +0200 | [diff] [blame] | 1947 | #ifdef CONFIG_COMPAT |
| 1948 | .compat_ioctl = kvm_vm_compat_ioctl, |
| 1949 | #endif |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1950 | .mmap = kvm_vm_mmap, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1951 | .llseek = noop_llseek, |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1952 | }; |
| 1953 | |
| 1954 | static int kvm_dev_ioctl_create_vm(void) |
| 1955 | { |
Takuya Yoshikawa | 6ce5a09 | 2010-03-15 22:13:30 +0900 | [diff] [blame] | 1956 | int fd, r; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1957 | struct kvm *kvm; |
| 1958 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1959 | kvm = kvm_create_vm(); |
Avi Kivity | d6d2816 | 2007-06-28 08:38:16 -0400 | [diff] [blame] | 1960 | if (IS_ERR(kvm)) |
| 1961 | return PTR_ERR(kvm); |
Takuya Yoshikawa | 6ce5a09 | 2010-03-15 22:13:30 +0900 | [diff] [blame] | 1962 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
| 1963 | r = kvm_coalesced_mmio_init(kvm); |
| 1964 | if (r < 0) { |
| 1965 | kvm_put_kvm(kvm); |
| 1966 | return r; |
| 1967 | } |
| 1968 | #endif |
Roland Dreier | 628ff7c | 2009-12-18 09:41:24 -0800 | [diff] [blame] | 1969 | fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR); |
Al Viro | 2030a42 | 2008-02-23 06:46:49 -0500 | [diff] [blame] | 1970 | if (fd < 0) |
Al Viro | 66c0b39 | 2008-04-19 20:33:56 +0100 | [diff] [blame] | 1971 | kvm_put_kvm(kvm); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1972 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1973 | return fd; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1974 | } |
| 1975 | |
Avi Kivity | 1a811b6 | 2008-12-08 18:25:27 +0200 | [diff] [blame] | 1976 | static long kvm_dev_ioctl_check_extension_generic(long arg) |
| 1977 | { |
| 1978 | switch (arg) { |
Avi Kivity | ca9edae | 2008-12-08 18:29:29 +0200 | [diff] [blame] | 1979 | case KVM_CAP_USER_MEMORY: |
Avi Kivity | 1a811b6 | 2008-12-08 18:25:27 +0200 | [diff] [blame] | 1980 | case KVM_CAP_DESTROY_MEMORY_REGION_WORKS: |
Jan Kiszka | 4cd481f | 2009-04-13 11:59:32 +0200 | [diff] [blame] | 1981 | case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS: |
Gleb Natapov | 73880c8 | 2009-06-09 15:56:28 +0300 | [diff] [blame] | 1982 | #ifdef CONFIG_KVM_APIC_ARCHITECTURE |
| 1983 | case KVM_CAP_SET_BOOT_CPU_ID: |
| 1984 | #endif |
Avi Kivity | a9c7399 | 2009-11-04 11:54:59 +0200 | [diff] [blame] | 1985 | case KVM_CAP_INTERNAL_ERROR_DATA: |
Avi Kivity | 1a811b6 | 2008-12-08 18:25:27 +0200 | [diff] [blame] | 1986 | return 1; |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 1987 | #ifdef CONFIG_HAVE_KVM_IRQCHIP |
| 1988 | case KVM_CAP_IRQ_ROUTING: |
Sheng Yang | 3646314 | 2009-03-16 16:33:43 +0800 | [diff] [blame] | 1989 | return KVM_MAX_IRQ_ROUTES; |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 1990 | #endif |
Avi Kivity | 1a811b6 | 2008-12-08 18:25:27 +0200 | [diff] [blame] | 1991 | default: |
| 1992 | break; |
| 1993 | } |
| 1994 | return kvm_dev_ioctl_check_extension(arg); |
| 1995 | } |
| 1996 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1997 | static long kvm_dev_ioctl(struct file *filp, |
| 1998 | unsigned int ioctl, unsigned long arg) |
| 1999 | { |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 2000 | long r = -EINVAL; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2001 | |
| 2002 | switch (ioctl) { |
| 2003 | case KVM_GET_API_VERSION: |
Avi Kivity | f0fe510 | 2007-03-07 13:11:17 +0200 | [diff] [blame] | 2004 | r = -EINVAL; |
| 2005 | if (arg) |
| 2006 | goto out; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2007 | r = KVM_API_VERSION; |
| 2008 | break; |
| 2009 | case KVM_CREATE_VM: |
Avi Kivity | f0fe510 | 2007-03-07 13:11:17 +0200 | [diff] [blame] | 2010 | r = -EINVAL; |
| 2011 | if (arg) |
| 2012 | goto out; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2013 | r = kvm_dev_ioctl_create_vm(); |
| 2014 | break; |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 2015 | case KVM_CHECK_EXTENSION: |
Avi Kivity | 1a811b6 | 2008-12-08 18:25:27 +0200 | [diff] [blame] | 2016 | r = kvm_dev_ioctl_check_extension_generic(arg); |
Avi Kivity | 5d308f4 | 2007-03-01 17:56:20 +0200 | [diff] [blame] | 2017 | break; |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 2018 | case KVM_GET_VCPU_MMAP_SIZE: |
| 2019 | r = -EINVAL; |
| 2020 | if (arg) |
| 2021 | goto out; |
Avi Kivity | adb1ff4 | 2008-01-24 15:13:08 +0200 | [diff] [blame] | 2022 | r = PAGE_SIZE; /* struct kvm_run */ |
| 2023 | #ifdef CONFIG_X86 |
| 2024 | r += PAGE_SIZE; /* pio data page */ |
| 2025 | #endif |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 2026 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
| 2027 | r += PAGE_SIZE; /* coalesced mmio ring page */ |
| 2028 | #endif |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 2029 | break; |
Feng(Eric) Liu | d4c9ff2 | 2008-04-10 08:47:53 -0400 | [diff] [blame] | 2030 | case KVM_TRACE_ENABLE: |
| 2031 | case KVM_TRACE_PAUSE: |
| 2032 | case KVM_TRACE_DISABLE: |
Marcelo Tosatti | 2023a29 | 2009-06-18 11:47:28 -0300 | [diff] [blame] | 2033 | r = -EOPNOTSUPP; |
Feng(Eric) Liu | d4c9ff2 | 2008-04-10 08:47:53 -0400 | [diff] [blame] | 2034 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2035 | default: |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 2036 | return kvm_arch_dev_ioctl(filp, ioctl, arg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2037 | } |
| 2038 | out: |
| 2039 | return r; |
| 2040 | } |
| 2041 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2042 | static struct file_operations kvm_chardev_ops = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2043 | .unlocked_ioctl = kvm_dev_ioctl, |
| 2044 | .compat_ioctl = kvm_dev_ioctl, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 2045 | .llseek = noop_llseek, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2046 | }; |
| 2047 | |
| 2048 | static struct miscdevice kvm_dev = { |
Avi Kivity | bbe4432 | 2007-03-04 13:27:36 +0200 | [diff] [blame] | 2049 | KVM_MINOR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2050 | "kvm", |
| 2051 | &kvm_chardev_ops, |
| 2052 | }; |
| 2053 | |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 2054 | static void hardware_enable(void *junk) |
| 2055 | { |
| 2056 | int cpu = raw_smp_processor_id(); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2057 | int r; |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 2058 | |
Rusty Russell | 7f59f49 | 2008-12-07 21:25:45 +1030 | [diff] [blame] | 2059 | if (cpumask_test_cpu(cpu, cpus_hardware_enabled)) |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 2060 | return; |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2061 | |
Rusty Russell | 7f59f49 | 2008-12-07 21:25:45 +1030 | [diff] [blame] | 2062 | cpumask_set_cpu(cpu, cpus_hardware_enabled); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2063 | |
| 2064 | r = kvm_arch_hardware_enable(NULL); |
| 2065 | |
| 2066 | if (r) { |
| 2067 | cpumask_clear_cpu(cpu, cpus_hardware_enabled); |
| 2068 | atomic_inc(&hardware_enable_failed); |
| 2069 | printk(KERN_INFO "kvm: enabling virtualization on " |
| 2070 | "CPU%d failed\n", cpu); |
| 2071 | } |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 2072 | } |
| 2073 | |
| 2074 | static void hardware_disable(void *junk) |
| 2075 | { |
| 2076 | int cpu = raw_smp_processor_id(); |
| 2077 | |
Rusty Russell | 7f59f49 | 2008-12-07 21:25:45 +1030 | [diff] [blame] | 2078 | if (!cpumask_test_cpu(cpu, cpus_hardware_enabled)) |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 2079 | return; |
Rusty Russell | 7f59f49 | 2008-12-07 21:25:45 +1030 | [diff] [blame] | 2080 | cpumask_clear_cpu(cpu, cpus_hardware_enabled); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 2081 | kvm_arch_hardware_disable(NULL); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 2082 | } |
| 2083 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2084 | static void hardware_disable_all_nolock(void) |
| 2085 | { |
| 2086 | BUG_ON(!kvm_usage_count); |
| 2087 | |
| 2088 | kvm_usage_count--; |
| 2089 | if (!kvm_usage_count) |
| 2090 | on_each_cpu(hardware_disable, NULL, 1); |
| 2091 | } |
| 2092 | |
| 2093 | static void hardware_disable_all(void) |
| 2094 | { |
| 2095 | spin_lock(&kvm_lock); |
| 2096 | hardware_disable_all_nolock(); |
| 2097 | spin_unlock(&kvm_lock); |
| 2098 | } |
| 2099 | |
| 2100 | static int hardware_enable_all(void) |
| 2101 | { |
| 2102 | int r = 0; |
| 2103 | |
| 2104 | spin_lock(&kvm_lock); |
| 2105 | |
| 2106 | kvm_usage_count++; |
| 2107 | if (kvm_usage_count == 1) { |
| 2108 | atomic_set(&hardware_enable_failed, 0); |
| 2109 | on_each_cpu(hardware_enable, NULL, 1); |
| 2110 | |
| 2111 | if (atomic_read(&hardware_enable_failed)) { |
| 2112 | hardware_disable_all_nolock(); |
| 2113 | r = -EBUSY; |
| 2114 | } |
| 2115 | } |
| 2116 | |
| 2117 | spin_unlock(&kvm_lock); |
| 2118 | |
| 2119 | return r; |
| 2120 | } |
| 2121 | |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2122 | static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val, |
| 2123 | void *v) |
| 2124 | { |
| 2125 | int cpu = (long)v; |
| 2126 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2127 | if (!kvm_usage_count) |
| 2128 | return NOTIFY_OK; |
| 2129 | |
Avi Kivity | 1a6f4d7 | 2007-11-11 18:37:32 +0200 | [diff] [blame] | 2130 | val &= ~CPU_TASKS_FROZEN; |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2131 | switch (val) { |
Avi Kivity | cec9ad2 | 2007-05-24 13:11:41 +0300 | [diff] [blame] | 2132 | case CPU_DYING: |
Avi Kivity | 6ec8a85 | 2007-08-19 15:57:26 +0300 | [diff] [blame] | 2133 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
| 2134 | cpu); |
| 2135 | hardware_disable(NULL); |
| 2136 | break; |
Zachary Amsden | da908f2 | 2010-08-19 22:07:27 -1000 | [diff] [blame] | 2137 | case CPU_STARTING: |
Jeremy Katz | 43934a3 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 2138 | printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n", |
| 2139 | cpu); |
Zachary Amsden | ca84d1a | 2010-08-19 22:07:28 -1000 | [diff] [blame] | 2140 | spin_lock(&kvm_lock); |
Zachary Amsden | da908f2 | 2010-08-19 22:07:27 -1000 | [diff] [blame] | 2141 | hardware_enable(NULL); |
Zachary Amsden | ca84d1a | 2010-08-19 22:07:28 -1000 | [diff] [blame] | 2142 | spin_unlock(&kvm_lock); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2143 | break; |
| 2144 | } |
| 2145 | return NOTIFY_OK; |
| 2146 | } |
| 2147 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 2148 | |
| 2149 | asmlinkage void kvm_handle_fault_on_reboot(void) |
| 2150 | { |
Avi Kivity | ca242ac | 2010-09-21 19:59:43 +0200 | [diff] [blame] | 2151 | if (kvm_rebooting) { |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 2152 | /* spin while reset goes on */ |
Avi Kivity | ca242ac | 2010-09-21 19:59:43 +0200 | [diff] [blame] | 2153 | local_irq_enable(); |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 2154 | while (true) |
Avi Kivity | 624d84c | 2010-09-21 19:59:44 +0200 | [diff] [blame] | 2155 | cpu_relax(); |
Avi Kivity | ca242ac | 2010-09-21 19:59:43 +0200 | [diff] [blame] | 2156 | } |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 2157 | /* Fault while not rebooting. We want the trace. */ |
| 2158 | BUG(); |
| 2159 | } |
| 2160 | EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot); |
| 2161 | |
Rusty Russell | 9a2b85c | 2007-07-17 23:17:55 +1000 | [diff] [blame] | 2162 | static int kvm_reboot(struct notifier_block *notifier, unsigned long val, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2163 | void *v) |
Rusty Russell | 9a2b85c | 2007-07-17 23:17:55 +1000 | [diff] [blame] | 2164 | { |
Sheng Yang | 8e1c181 | 2009-04-29 11:09:04 +0800 | [diff] [blame] | 2165 | /* |
| 2166 | * Some (well, at least mine) BIOSes hang on reboot if |
| 2167 | * in vmx root mode. |
| 2168 | * |
| 2169 | * And Intel TXT required VMX off for all cpu when system shutdown. |
| 2170 | */ |
| 2171 | printk(KERN_INFO "kvm: exiting hardware virtualization\n"); |
| 2172 | kvm_rebooting = true; |
| 2173 | on_each_cpu(hardware_disable, NULL, 1); |
Rusty Russell | 9a2b85c | 2007-07-17 23:17:55 +1000 | [diff] [blame] | 2174 | return NOTIFY_OK; |
| 2175 | } |
| 2176 | |
| 2177 | static struct notifier_block kvm_reboot_notifier = { |
| 2178 | .notifier_call = kvm_reboot, |
| 2179 | .priority = 0, |
| 2180 | }; |
| 2181 | |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 2182 | static void kvm_io_bus_destroy(struct kvm_io_bus *bus) |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 2183 | { |
| 2184 | int i; |
| 2185 | |
| 2186 | for (i = 0; i < bus->dev_count; i++) { |
| 2187 | struct kvm_io_device *pos = bus->devs[i]; |
| 2188 | |
| 2189 | kvm_iodevice_destructor(pos); |
| 2190 | } |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 2191 | kfree(bus); |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 2192 | } |
| 2193 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 2194 | /* kvm_io_bus_write - called under kvm->slots_lock */ |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 2195 | int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 2196 | int len, const void *val) |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 2197 | { |
| 2198 | int i; |
Lai Jiangshan | 90d83dc | 2010-04-19 17:41:23 +0800 | [diff] [blame] | 2199 | struct kvm_io_bus *bus; |
| 2200 | |
| 2201 | bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu); |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 2202 | for (i = 0; i < bus->dev_count; i++) |
| 2203 | if (!kvm_iodevice_write(bus->devs[i], addr, len, val)) |
| 2204 | return 0; |
| 2205 | return -EOPNOTSUPP; |
| 2206 | } |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 2207 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 2208 | /* kvm_io_bus_read - called under kvm->slots_lock */ |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 2209 | int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, |
| 2210 | int len, void *val) |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 2211 | { |
| 2212 | int i; |
Lai Jiangshan | 90d83dc | 2010-04-19 17:41:23 +0800 | [diff] [blame] | 2213 | struct kvm_io_bus *bus; |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 2214 | |
Lai Jiangshan | 90d83dc | 2010-04-19 17:41:23 +0800 | [diff] [blame] | 2215 | bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu); |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 2216 | for (i = 0; i < bus->dev_count; i++) |
| 2217 | if (!kvm_iodevice_read(bus->devs[i], addr, len, val)) |
| 2218 | return 0; |
| 2219 | return -EOPNOTSUPP; |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 2220 | } |
| 2221 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2222 | /* Caller must hold slots_lock. */ |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 2223 | int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, |
| 2224 | struct kvm_io_device *dev) |
Michael S. Tsirkin | 6c47469 | 2009-06-29 22:24:26 +0300 | [diff] [blame] | 2225 | { |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 2226 | struct kvm_io_bus *new_bus, *bus; |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 2227 | |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 2228 | bus = kvm->buses[bus_idx]; |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 2229 | if (bus->dev_count > NR_IOBUS_DEVS-1) |
| 2230 | return -ENOSPC; |
| 2231 | |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 2232 | new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL); |
| 2233 | if (!new_bus) |
| 2234 | return -ENOMEM; |
| 2235 | memcpy(new_bus, bus, sizeof(struct kvm_io_bus)); |
| 2236 | new_bus->devs[new_bus->dev_count++] = dev; |
| 2237 | rcu_assign_pointer(kvm->buses[bus_idx], new_bus); |
| 2238 | synchronize_srcu_expedited(&kvm->srcu); |
| 2239 | kfree(bus); |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 2240 | |
| 2241 | return 0; |
| 2242 | } |
| 2243 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2244 | /* Caller must hold slots_lock. */ |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 2245 | int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx, |
| 2246 | struct kvm_io_device *dev) |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 2247 | { |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 2248 | int i, r; |
| 2249 | struct kvm_io_bus *new_bus, *bus; |
Michael S. Tsirkin | 6c47469 | 2009-06-29 22:24:26 +0300 | [diff] [blame] | 2250 | |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 2251 | new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL); |
| 2252 | if (!new_bus) |
| 2253 | return -ENOMEM; |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 2254 | |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 2255 | bus = kvm->buses[bus_idx]; |
| 2256 | memcpy(new_bus, bus, sizeof(struct kvm_io_bus)); |
| 2257 | |
| 2258 | r = -ENOENT; |
| 2259 | for (i = 0; i < new_bus->dev_count; i++) |
| 2260 | if (new_bus->devs[i] == dev) { |
| 2261 | r = 0; |
| 2262 | new_bus->devs[i] = new_bus->devs[--new_bus->dev_count]; |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 2263 | break; |
| 2264 | } |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 2265 | |
| 2266 | if (r) { |
| 2267 | kfree(new_bus); |
| 2268 | return r; |
| 2269 | } |
| 2270 | |
| 2271 | rcu_assign_pointer(kvm->buses[bus_idx], new_bus); |
| 2272 | synchronize_srcu_expedited(&kvm->srcu); |
| 2273 | kfree(bus); |
| 2274 | return r; |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 2275 | } |
| 2276 | |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2277 | static struct notifier_block kvm_cpu_notifier = { |
| 2278 | .notifier_call = kvm_cpu_hotplug, |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2279 | }; |
| 2280 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 2281 | static int vm_stat_get(void *_offset, u64 *val) |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 2282 | { |
| 2283 | unsigned offset = (long)_offset; |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 2284 | struct kvm *kvm; |
| 2285 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 2286 | *val = 0; |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 2287 | spin_lock(&kvm_lock); |
| 2288 | list_for_each_entry(kvm, &vm_list, vm_list) |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 2289 | *val += *(u32 *)((void *)kvm + offset); |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 2290 | spin_unlock(&kvm_lock); |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 2291 | return 0; |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 2292 | } |
| 2293 | |
| 2294 | DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n"); |
| 2295 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 2296 | static int vcpu_stat_get(void *_offset, u64 *val) |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2297 | { |
| 2298 | unsigned offset = (long)_offset; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2299 | struct kvm *kvm; |
| 2300 | struct kvm_vcpu *vcpu; |
| 2301 | int i; |
| 2302 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 2303 | *val = 0; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2304 | spin_lock(&kvm_lock); |
| 2305 | list_for_each_entry(kvm, &vm_list, vm_list) |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 2306 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 2307 | *val += *(u32 *)((void *)vcpu + offset); |
| 2308 | |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2309 | spin_unlock(&kvm_lock); |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 2310 | return 0; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2311 | } |
| 2312 | |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 2313 | DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n"); |
| 2314 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2315 | static const struct file_operations *stat_fops[] = { |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 2316 | [KVM_STAT_VCPU] = &vcpu_stat_fops, |
| 2317 | [KVM_STAT_VM] = &vm_stat_fops, |
| 2318 | }; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2319 | |
Zhang Xiantao | a16b043 | 2007-11-16 14:38:21 +0800 | [diff] [blame] | 2320 | static void kvm_init_debug(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2321 | { |
| 2322 | struct kvm_stats_debugfs_item *p; |
| 2323 | |
Hollis Blanchard | 76f7c87 | 2008-04-15 16:05:42 -0500 | [diff] [blame] | 2324 | kvm_debugfs_dir = debugfs_create_dir("kvm", NULL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2325 | for (p = debugfs_entries; p->name; ++p) |
Hollis Blanchard | 76f7c87 | 2008-04-15 16:05:42 -0500 | [diff] [blame] | 2326 | p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir, |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2327 | (void *)(long)p->offset, |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 2328 | stat_fops[p->kind]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2329 | } |
| 2330 | |
| 2331 | static void kvm_exit_debug(void) |
| 2332 | { |
| 2333 | struct kvm_stats_debugfs_item *p; |
| 2334 | |
| 2335 | for (p = debugfs_entries; p->name; ++p) |
| 2336 | debugfs_remove(p->dentry); |
Hollis Blanchard | 76f7c87 | 2008-04-15 16:05:42 -0500 | [diff] [blame] | 2337 | debugfs_remove(kvm_debugfs_dir); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2338 | } |
| 2339 | |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 2340 | static int kvm_suspend(struct sys_device *dev, pm_message_t state) |
| 2341 | { |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2342 | if (kvm_usage_count) |
| 2343 | hardware_disable(NULL); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 2344 | return 0; |
| 2345 | } |
| 2346 | |
| 2347 | static int kvm_resume(struct sys_device *dev) |
| 2348 | { |
Zachary Amsden | ca84d1a | 2010-08-19 22:07:28 -1000 | [diff] [blame] | 2349 | if (kvm_usage_count) { |
| 2350 | WARN_ON(spin_is_locked(&kvm_lock)); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2351 | hardware_enable(NULL); |
Zachary Amsden | ca84d1a | 2010-08-19 22:07:28 -1000 | [diff] [blame] | 2352 | } |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 2353 | return 0; |
| 2354 | } |
| 2355 | |
| 2356 | static struct sysdev_class kvm_sysdev_class = { |
Kay Sievers | af5ca3f4 | 2007-12-20 02:09:39 +0100 | [diff] [blame] | 2357 | .name = "kvm", |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 2358 | .suspend = kvm_suspend, |
| 2359 | .resume = kvm_resume, |
| 2360 | }; |
| 2361 | |
| 2362 | static struct sys_device kvm_sysdev = { |
| 2363 | .id = 0, |
| 2364 | .cls = &kvm_sysdev_class, |
| 2365 | }; |
| 2366 | |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 2367 | struct page *bad_page; |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2368 | pfn_t bad_pfn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2369 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2370 | static inline |
| 2371 | struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn) |
| 2372 | { |
| 2373 | return container_of(pn, struct kvm_vcpu, preempt_notifier); |
| 2374 | } |
| 2375 | |
| 2376 | static void kvm_sched_in(struct preempt_notifier *pn, int cpu) |
| 2377 | { |
| 2378 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); |
| 2379 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 2380 | kvm_arch_vcpu_load(vcpu, cpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2381 | } |
| 2382 | |
| 2383 | static void kvm_sched_out(struct preempt_notifier *pn, |
| 2384 | struct task_struct *next) |
| 2385 | { |
| 2386 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); |
| 2387 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 2388 | kvm_arch_vcpu_put(vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2389 | } |
| 2390 | |
Avi Kivity | 0ee75be | 2010-04-28 15:39:01 +0300 | [diff] [blame] | 2391 | int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 2392 | struct module *module) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2393 | { |
| 2394 | int r; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2395 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2396 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 2397 | r = kvm_arch_init(opaque); |
| 2398 | if (r) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 2399 | goto out_fail; |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 2400 | |
| 2401 | bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 2402 | |
| 2403 | if (bad_page == NULL) { |
| 2404 | r = -ENOMEM; |
| 2405 | goto out; |
| 2406 | } |
| 2407 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2408 | bad_pfn = page_to_pfn(bad_page); |
| 2409 | |
Huang Ying | bf99815 | 2010-05-31 14:28:19 +0800 | [diff] [blame] | 2410 | hwpoison_page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 2411 | |
| 2412 | if (hwpoison_page == NULL) { |
| 2413 | r = -ENOMEM; |
| 2414 | goto out_free_0; |
| 2415 | } |
| 2416 | |
| 2417 | hwpoison_pfn = page_to_pfn(hwpoison_page); |
| 2418 | |
Gleb Natapov | edba23e | 2010-07-07 20:16:45 +0300 | [diff] [blame] | 2419 | fault_page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 2420 | |
| 2421 | if (fault_page == NULL) { |
| 2422 | r = -ENOMEM; |
| 2423 | goto out_free_0; |
| 2424 | } |
| 2425 | |
| 2426 | fault_pfn = page_to_pfn(fault_page); |
| 2427 | |
Avi Kivity | 8437a617 | 2009-06-06 14:52:35 -0700 | [diff] [blame] | 2428 | if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) { |
Rusty Russell | 7f59f49 | 2008-12-07 21:25:45 +1030 | [diff] [blame] | 2429 | r = -ENOMEM; |
| 2430 | goto out_free_0; |
| 2431 | } |
| 2432 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 2433 | r = kvm_arch_hardware_setup(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2434 | if (r < 0) |
Rusty Russell | 7f59f49 | 2008-12-07 21:25:45 +1030 | [diff] [blame] | 2435 | goto out_free_0a; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2436 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2437 | for_each_online_cpu(cpu) { |
| 2438 | smp_call_function_single(cpu, |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 2439 | kvm_arch_check_processor_compat, |
Jens Axboe | 8691e5a | 2008-06-06 11:18:06 +0200 | [diff] [blame] | 2440 | &r, 1); |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2441 | if (r < 0) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 2442 | goto out_free_1; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2443 | } |
| 2444 | |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2445 | r = register_cpu_notifier(&kvm_cpu_notifier); |
| 2446 | if (r) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 2447 | goto out_free_2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2448 | register_reboot_notifier(&kvm_reboot_notifier); |
| 2449 | |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 2450 | r = sysdev_class_register(&kvm_sysdev_class); |
| 2451 | if (r) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 2452 | goto out_free_3; |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 2453 | |
| 2454 | r = sysdev_register(&kvm_sysdev); |
| 2455 | if (r) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 2456 | goto out_free_4; |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 2457 | |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 2458 | /* A kmem cache lets us meet the alignment requirements of fx_save. */ |
Avi Kivity | 0ee75be | 2010-04-28 15:39:01 +0300 | [diff] [blame] | 2459 | if (!vcpu_align) |
| 2460 | vcpu_align = __alignof__(struct kvm_vcpu); |
| 2461 | kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align, |
Joe Perches | 56919c5 | 2007-11-12 20:06:51 -0800 | [diff] [blame] | 2462 | 0, NULL); |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 2463 | if (!kvm_vcpu_cache) { |
| 2464 | r = -ENOMEM; |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 2465 | goto out_free_5; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 2466 | } |
| 2467 | |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 2468 | r = kvm_async_pf_init(); |
| 2469 | if (r) |
| 2470 | goto out_free; |
| 2471 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2472 | kvm_chardev_ops.owner = module; |
Christian Borntraeger | 3d3aab1 | 2008-12-02 11:17:32 +0100 | [diff] [blame] | 2473 | kvm_vm_fops.owner = module; |
| 2474 | kvm_vcpu_fops.owner = module; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2475 | |
| 2476 | r = misc_register(&kvm_dev); |
| 2477 | if (r) { |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2478 | printk(KERN_ERR "kvm: misc device register failed\n"); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 2479 | goto out_unreg; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2480 | } |
| 2481 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2482 | kvm_preempt_ops.sched_in = kvm_sched_in; |
| 2483 | kvm_preempt_ops.sched_out = kvm_sched_out; |
| 2484 | |
Darrick J. Wong | 0ea4ed8 | 2009-10-14 16:21:00 -0700 | [diff] [blame] | 2485 | kvm_init_debug(); |
| 2486 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 2487 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2488 | |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 2489 | out_unreg: |
| 2490 | kvm_async_pf_deinit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2491 | out_free: |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 2492 | kmem_cache_destroy(kvm_vcpu_cache); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 2493 | out_free_5: |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 2494 | sysdev_unregister(&kvm_sysdev); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 2495 | out_free_4: |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 2496 | sysdev_class_unregister(&kvm_sysdev_class); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 2497 | out_free_3: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2498 | unregister_reboot_notifier(&kvm_reboot_notifier); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2499 | unregister_cpu_notifier(&kvm_cpu_notifier); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 2500 | out_free_2: |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 2501 | out_free_1: |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 2502 | kvm_arch_hardware_unsetup(); |
Rusty Russell | 7f59f49 | 2008-12-07 21:25:45 +1030 | [diff] [blame] | 2503 | out_free_0a: |
| 2504 | free_cpumask_var(cpus_hardware_enabled); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 2505 | out_free_0: |
Gleb Natapov | edba23e | 2010-07-07 20:16:45 +0300 | [diff] [blame] | 2506 | if (fault_page) |
| 2507 | __free_page(fault_page); |
Huang Ying | bf99815 | 2010-05-31 14:28:19 +0800 | [diff] [blame] | 2508 | if (hwpoison_page) |
| 2509 | __free_page(hwpoison_page); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 2510 | __free_page(bad_page); |
Avi Kivity | ca45aaa | 2007-03-01 19:21:03 +0200 | [diff] [blame] | 2511 | out: |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 2512 | kvm_arch_exit(); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 2513 | out_fail: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2514 | return r; |
| 2515 | } |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 2516 | EXPORT_SYMBOL_GPL(kvm_init); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2517 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 2518 | void kvm_exit(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2519 | { |
Darrick J. Wong | 0ea4ed8 | 2009-10-14 16:21:00 -0700 | [diff] [blame] | 2520 | kvm_exit_debug(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2521 | misc_deregister(&kvm_dev); |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 2522 | kmem_cache_destroy(kvm_vcpu_cache); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 2523 | kvm_async_pf_deinit(); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 2524 | sysdev_unregister(&kvm_sysdev); |
| 2525 | sysdev_class_unregister(&kvm_sysdev_class); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2526 | unregister_reboot_notifier(&kvm_reboot_notifier); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 2527 | unregister_cpu_notifier(&kvm_cpu_notifier); |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 2528 | on_each_cpu(hardware_disable, NULL, 1); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 2529 | kvm_arch_hardware_unsetup(); |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 2530 | kvm_arch_exit(); |
Rusty Russell | 7f59f49 | 2008-12-07 21:25:45 +1030 | [diff] [blame] | 2531 | free_cpumask_var(cpus_hardware_enabled); |
Huang Ying | bf99815 | 2010-05-31 14:28:19 +0800 | [diff] [blame] | 2532 | __free_page(hwpoison_page); |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 2533 | __free_page(bad_page); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2534 | } |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 2535 | EXPORT_SYMBOL_GPL(kvm_exit); |