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. |
| 8 | * |
| 9 | * Authors: |
| 10 | * Avi Kivity <avi@qumranet.com> |
| 11 | * Yaniv Kamay <yaniv@qumranet.com> |
| 12 | * |
| 13 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 14 | * the COPYING file in the top-level directory. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | #include "kvm.h" |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 19 | #include "x86.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 20 | #include "x86_emulate.h" |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 21 | #include "irq.h" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 22 | |
| 23 | #include <linux/kvm.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/errno.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 26 | #include <linux/percpu.h> |
| 27 | #include <linux/gfp.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 28 | #include <linux/mm.h> |
| 29 | #include <linux/miscdevice.h> |
| 30 | #include <linux/vmalloc.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 31 | #include <linux/reboot.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 32 | #include <linux/debugfs.h> |
| 33 | #include <linux/highmem.h> |
| 34 | #include <linux/file.h> |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 35 | #include <linux/sysdev.h> |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 36 | #include <linux/cpu.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 37 | #include <linux/sched.h> |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 38 | #include <linux/cpumask.h> |
| 39 | #include <linux/smp.h> |
Avi Kivity | d6d2816 | 2007-06-28 08:38:16 -0400 | [diff] [blame] | 40 | #include <linux/anon_inodes.h> |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 41 | #include <linux/profile.h> |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 42 | #include <linux/kvm_para.h> |
Izik Eidus | 6fc138d | 2007-10-09 19:20:39 +0200 | [diff] [blame] | 43 | #include <linux/pagemap.h> |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 44 | #include <linux/mman.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 45 | |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 46 | #include <asm/processor.h> |
| 47 | #include <asm/msr.h> |
| 48 | #include <asm/io.h> |
| 49 | #include <asm/uaccess.h> |
| 50 | #include <asm/desc.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 51 | |
| 52 | MODULE_AUTHOR("Qumranet"); |
| 53 | MODULE_LICENSE("GPL"); |
| 54 | |
Avi Kivity | 133de90 | 2007-02-12 00:54:44 -0800 | [diff] [blame] | 55 | static DEFINE_SPINLOCK(kvm_lock); |
| 56 | static LIST_HEAD(vm_list); |
| 57 | |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 58 | static cpumask_t cpus_hardware_enabled; |
| 59 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 60 | struct kvm_x86_ops *kvm_x86_ops; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 61 | struct kmem_cache *kvm_vcpu_cache; |
| 62 | EXPORT_SYMBOL_GPL(kvm_vcpu_cache); |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 63 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 64 | static __read_mostly struct preempt_ops kvm_preempt_ops; |
| 65 | |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 66 | #define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 67 | |
| 68 | static struct kvm_stats_debugfs_item { |
| 69 | const char *name; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 70 | int offset; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 71 | struct dentry *dentry; |
| 72 | } debugfs_entries[] = { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 73 | { "pf_fixed", STAT_OFFSET(pf_fixed) }, |
| 74 | { "pf_guest", STAT_OFFSET(pf_guest) }, |
| 75 | { "tlb_flush", STAT_OFFSET(tlb_flush) }, |
| 76 | { "invlpg", STAT_OFFSET(invlpg) }, |
| 77 | { "exits", STAT_OFFSET(exits) }, |
| 78 | { "io_exits", STAT_OFFSET(io_exits) }, |
| 79 | { "mmio_exits", STAT_OFFSET(mmio_exits) }, |
| 80 | { "signal_exits", STAT_OFFSET(signal_exits) }, |
| 81 | { "irq_window", STAT_OFFSET(irq_window_exits) }, |
| 82 | { "halt_exits", STAT_OFFSET(halt_exits) }, |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 83 | { "halt_wakeup", STAT_OFFSET(halt_wakeup) }, |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 84 | { "request_irq", STAT_OFFSET(request_irq_exits) }, |
| 85 | { "irq_exits", STAT_OFFSET(irq_exits) }, |
Avi Kivity | e6adf28 | 2007-04-30 16:07:54 +0300 | [diff] [blame] | 86 | { "light_exits", STAT_OFFSET(light_exits) }, |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 87 | { "efer_reload", STAT_OFFSET(efer_reload) }, |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 88 | { NULL } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | static struct dentry *debugfs_dir; |
| 92 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 93 | #define EFER_RESERVED_BITS 0xfffffffffffff2fe |
| 94 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 95 | static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl, |
| 96 | unsigned long arg); |
| 97 | |
James Morris | 5aacf0c | 2006-12-22 01:04:55 -0800 | [diff] [blame] | 98 | static inline int valid_vcpu(int n) |
| 99 | { |
| 100 | return likely(n >= 0 && n < KVM_MAX_VCPUS); |
| 101 | } |
| 102 | |
Avi Kivity | 7702fd1 | 2007-06-14 16:27:40 +0300 | [diff] [blame] | 103 | void kvm_load_guest_fpu(struct kvm_vcpu *vcpu) |
| 104 | { |
| 105 | if (!vcpu->fpu_active || vcpu->guest_fpu_loaded) |
| 106 | return; |
| 107 | |
| 108 | vcpu->guest_fpu_loaded = 1; |
Rusty Russell | b114b08 | 2007-07-30 21:13:43 +1000 | [diff] [blame] | 109 | fx_save(&vcpu->host_fx_image); |
| 110 | fx_restore(&vcpu->guest_fx_image); |
Avi Kivity | 7702fd1 | 2007-06-14 16:27:40 +0300 | [diff] [blame] | 111 | } |
| 112 | EXPORT_SYMBOL_GPL(kvm_load_guest_fpu); |
| 113 | |
| 114 | void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) |
| 115 | { |
| 116 | if (!vcpu->guest_fpu_loaded) |
| 117 | return; |
| 118 | |
| 119 | vcpu->guest_fpu_loaded = 0; |
Rusty Russell | b114b08 | 2007-07-30 21:13:43 +1000 | [diff] [blame] | 120 | fx_save(&vcpu->guest_fx_image); |
| 121 | fx_restore(&vcpu->host_fx_image); |
Avi Kivity | 7702fd1 | 2007-06-14 16:27:40 +0300 | [diff] [blame] | 122 | } |
| 123 | EXPORT_SYMBOL_GPL(kvm_put_guest_fpu); |
| 124 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 125 | /* |
| 126 | * Switches to specified vcpu, until a matching vcpu_put() |
| 127 | */ |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 128 | void vcpu_load(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 129 | { |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 130 | int cpu; |
| 131 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 132 | mutex_lock(&vcpu->mutex); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 133 | cpu = get_cpu(); |
| 134 | preempt_notifier_register(&vcpu->preempt_notifier); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 135 | kvm_arch_vcpu_load(vcpu, cpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 136 | put_cpu(); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 137 | } |
| 138 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 139 | void vcpu_put(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 140 | { |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 141 | preempt_disable(); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 142 | kvm_arch_vcpu_put(vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 143 | preempt_notifier_unregister(&vcpu->preempt_notifier); |
| 144 | preempt_enable(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 145 | mutex_unlock(&vcpu->mutex); |
| 146 | } |
| 147 | |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 148 | static void ack_flush(void *_completed) |
| 149 | { |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void kvm_flush_remote_tlbs(struct kvm *kvm) |
| 153 | { |
Laurent Vivier | 49d3bd7 | 2007-10-22 16:33:07 +0200 | [diff] [blame] | 154 | int i, cpu; |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 155 | cpumask_t cpus; |
| 156 | struct kvm_vcpu *vcpu; |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 157 | |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 158 | cpus_clear(cpus); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 159 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
| 160 | vcpu = kvm->vcpus[i]; |
| 161 | if (!vcpu) |
| 162 | continue; |
Avi Kivity | 3176bc3 | 2007-10-16 17:22:08 +0200 | [diff] [blame] | 163 | if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests)) |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 164 | continue; |
| 165 | cpu = vcpu->cpu; |
| 166 | if (cpu != -1 && cpu != raw_smp_processor_id()) |
Laurent Vivier | 49d3bd7 | 2007-10-22 16:33:07 +0200 | [diff] [blame] | 167 | cpu_set(cpu, cpus); |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 168 | } |
Laurent Vivier | 49d3bd7 | 2007-10-22 16:33:07 +0200 | [diff] [blame] | 169 | smp_call_function_mask(cpus, ack_flush, NULL, 1); |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 170 | } |
| 171 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 172 | int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id) |
| 173 | { |
| 174 | struct page *page; |
| 175 | int r; |
| 176 | |
| 177 | mutex_init(&vcpu->mutex); |
| 178 | vcpu->cpu = -1; |
| 179 | vcpu->mmu.root_hpa = INVALID_PAGE; |
| 180 | vcpu->kvm = kvm; |
| 181 | vcpu->vcpu_id = id; |
He, Qing | c5ec153 | 2007-09-03 17:07:41 +0300 | [diff] [blame] | 182 | if (!irqchip_in_kernel(kvm) || id == 0) |
| 183 | vcpu->mp_state = VCPU_MP_STATE_RUNNABLE; |
| 184 | else |
| 185 | vcpu->mp_state = VCPU_MP_STATE_UNINITIALIZED; |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 186 | init_waitqueue_head(&vcpu->wq); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 187 | |
| 188 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 189 | if (!page) { |
| 190 | r = -ENOMEM; |
| 191 | goto fail; |
| 192 | } |
| 193 | vcpu->run = page_address(page); |
| 194 | |
| 195 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 196 | if (!page) { |
| 197 | r = -ENOMEM; |
| 198 | goto fail_free_run; |
| 199 | } |
| 200 | vcpu->pio_data = page_address(page); |
| 201 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 202 | r = kvm_mmu_create(vcpu); |
| 203 | if (r < 0) |
| 204 | goto fail_free_pio_data; |
| 205 | |
Rusty Russell | 76fafa5 | 2007-10-08 10:50:48 +1000 | [diff] [blame] | 206 | if (irqchip_in_kernel(kvm)) { |
| 207 | r = kvm_create_lapic(vcpu); |
| 208 | if (r < 0) |
| 209 | goto fail_mmu_destroy; |
| 210 | } |
| 211 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 212 | return 0; |
| 213 | |
Rusty Russell | 76fafa5 | 2007-10-08 10:50:48 +1000 | [diff] [blame] | 214 | fail_mmu_destroy: |
| 215 | kvm_mmu_destroy(vcpu); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 216 | fail_free_pio_data: |
| 217 | free_page((unsigned long)vcpu->pio_data); |
| 218 | fail_free_run: |
| 219 | free_page((unsigned long)vcpu->run); |
| 220 | fail: |
Rusty Russell | 76fafa5 | 2007-10-08 10:50:48 +1000 | [diff] [blame] | 221 | return r; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 222 | } |
| 223 | EXPORT_SYMBOL_GPL(kvm_vcpu_init); |
| 224 | |
| 225 | void kvm_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 226 | { |
Rusty Russell | d589444 | 2007-10-08 10:48:30 +1000 | [diff] [blame] | 227 | kvm_free_lapic(vcpu); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 228 | kvm_mmu_destroy(vcpu); |
| 229 | free_page((unsigned long)vcpu->pio_data); |
| 230 | free_page((unsigned long)vcpu->run); |
| 231 | } |
| 232 | EXPORT_SYMBOL_GPL(kvm_vcpu_uninit); |
| 233 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 234 | static struct kvm *kvm_create_vm(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 235 | { |
| 236 | struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 237 | |
| 238 | if (!kvm) |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 239 | return ERR_PTR(-ENOMEM); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 240 | |
Eddie Dong | 7490634 | 2007-06-19 18:05:03 +0300 | [diff] [blame] | 241 | kvm_io_bus_init(&kvm->pio_bus); |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 242 | mutex_init(&kvm->lock); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 243 | INIT_LIST_HEAD(&kvm->active_mmu_pages); |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 244 | kvm_io_bus_init(&kvm->mmio_bus); |
Rusty Russell | 5e58cfe | 2007-07-23 17:08:21 +1000 | [diff] [blame] | 245 | spin_lock(&kvm_lock); |
| 246 | list_add(&kvm->vm_list, &vm_list); |
| 247 | spin_unlock(&kvm_lock); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 248 | return kvm; |
| 249 | } |
| 250 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 251 | /* |
| 252 | * Free any memory in @free but not in @dont. |
| 253 | */ |
| 254 | static void kvm_free_physmem_slot(struct kvm_memory_slot *free, |
| 255 | struct kvm_memory_slot *dont) |
| 256 | { |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 257 | if (!dont || free->rmap != dont->rmap) |
| 258 | vfree(free->rmap); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 259 | |
| 260 | if (!dont || free->dirty_bitmap != dont->dirty_bitmap) |
| 261 | vfree(free->dirty_bitmap); |
| 262 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 263 | free->npages = 0; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 264 | free->dirty_bitmap = NULL; |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 265 | free->rmap = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | static void kvm_free_physmem(struct kvm *kvm) |
| 269 | { |
| 270 | int i; |
| 271 | |
| 272 | for (i = 0; i < kvm->nmemslots; ++i) |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 273 | kvm_free_physmem_slot(&kvm->memslots[i], NULL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 276 | static void free_pio_guest_pages(struct kvm_vcpu *vcpu) |
| 277 | { |
| 278 | int i; |
| 279 | |
Rusty Russell | 3077c451 | 2007-07-30 16:41:57 +1000 | [diff] [blame] | 280 | for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i) |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 281 | if (vcpu->pio.guest_pages[i]) { |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 282 | kvm_release_page(vcpu->pio.guest_pages[i]); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 283 | vcpu->pio.guest_pages[i] = NULL; |
| 284 | } |
| 285 | } |
| 286 | |
Avi Kivity | 7b53aa5 | 2007-06-05 12:17:03 +0300 | [diff] [blame] | 287 | static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu) |
| 288 | { |
Avi Kivity | 7b53aa5 | 2007-06-05 12:17:03 +0300 | [diff] [blame] | 289 | vcpu_load(vcpu); |
| 290 | kvm_mmu_unload(vcpu); |
| 291 | vcpu_put(vcpu); |
| 292 | } |
| 293 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 294 | static void kvm_free_vcpus(struct kvm *kvm) |
| 295 | { |
| 296 | unsigned int i; |
| 297 | |
Avi Kivity | 7b53aa5 | 2007-06-05 12:17:03 +0300 | [diff] [blame] | 298 | /* |
| 299 | * Unpin any mmu pages first. |
| 300 | */ |
| 301 | for (i = 0; i < KVM_MAX_VCPUS; ++i) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 302 | if (kvm->vcpus[i]) |
| 303 | kvm_unload_vcpu_mmu(kvm->vcpus[i]); |
| 304 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
| 305 | if (kvm->vcpus[i]) { |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 306 | kvm_x86_ops->vcpu_free(kvm->vcpus[i]); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 307 | kvm->vcpus[i] = NULL; |
| 308 | } |
| 309 | } |
| 310 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 313 | static void kvm_destroy_vm(struct kvm *kvm) |
| 314 | { |
Avi Kivity | 133de90 | 2007-02-12 00:54:44 -0800 | [diff] [blame] | 315 | spin_lock(&kvm_lock); |
| 316 | list_del(&kvm->vm_list); |
| 317 | spin_unlock(&kvm_lock); |
Eddie Dong | 7490634 | 2007-06-19 18:05:03 +0300 | [diff] [blame] | 318 | kvm_io_bus_destroy(&kvm->pio_bus); |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 319 | kvm_io_bus_destroy(&kvm->mmio_bus); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 320 | kfree(kvm->vpic); |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 321 | kfree(kvm->vioapic); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 322 | kvm_free_vcpus(kvm); |
| 323 | kvm_free_physmem(kvm); |
| 324 | kfree(kvm); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | static int kvm_vm_release(struct inode *inode, struct file *filp) |
| 328 | { |
| 329 | struct kvm *kvm = filp->private_data; |
| 330 | |
| 331 | kvm_destroy_vm(kvm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | static void inject_gp(struct kvm_vcpu *vcpu) |
| 336 | { |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 337 | kvm_x86_ops->inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 340 | void fx_init(struct kvm_vcpu *vcpu) |
| 341 | { |
Rusty Russell | b114b08 | 2007-07-30 21:13:43 +1000 | [diff] [blame] | 342 | unsigned after_mxcsr_mask; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 343 | |
Rusty Russell | 9bd0150 | 2007-07-30 16:29:56 +1000 | [diff] [blame] | 344 | /* Initialize guest FPU by resetting ours and saving into guest's */ |
| 345 | preempt_disable(); |
Rusty Russell | b114b08 | 2007-07-30 21:13:43 +1000 | [diff] [blame] | 346 | fx_save(&vcpu->host_fx_image); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 347 | fpu_init(); |
Rusty Russell | b114b08 | 2007-07-30 21:13:43 +1000 | [diff] [blame] | 348 | fx_save(&vcpu->guest_fx_image); |
| 349 | fx_restore(&vcpu->host_fx_image); |
Rusty Russell | 9bd0150 | 2007-07-30 16:29:56 +1000 | [diff] [blame] | 350 | preempt_enable(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 351 | |
Amit Shah | 380102c | 2007-08-25 11:35:52 +0300 | [diff] [blame] | 352 | vcpu->cr0 |= X86_CR0_ET; |
Rusty Russell | b114b08 | 2007-07-30 21:13:43 +1000 | [diff] [blame] | 353 | after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space); |
| 354 | vcpu->guest_fx_image.mxcsr = 0x1f80; |
| 355 | memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask, |
| 356 | 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 357 | } |
| 358 | EXPORT_SYMBOL_GPL(fx_init); |
| 359 | |
| 360 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 361 | * Allocate some memory and give it an address in the guest physical address |
| 362 | * space. |
| 363 | * |
| 364 | * Discontiguous memory is allowed, mostly for framebuffers. |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 365 | * |
| 366 | * Must be called holding kvm->lock. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 367 | */ |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 368 | int __kvm_set_memory_region(struct kvm *kvm, |
| 369 | struct kvm_userspace_memory_region *mem, |
| 370 | int user_alloc) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 371 | { |
| 372 | int r; |
| 373 | gfn_t base_gfn; |
| 374 | unsigned long npages; |
| 375 | unsigned long i; |
| 376 | struct kvm_memory_slot *memslot; |
| 377 | struct kvm_memory_slot old, new; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 378 | |
| 379 | r = -EINVAL; |
| 380 | /* General sanity checks */ |
| 381 | if (mem->memory_size & (PAGE_SIZE - 1)) |
| 382 | goto out; |
| 383 | if (mem->guest_phys_addr & (PAGE_SIZE - 1)) |
| 384 | goto out; |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 385 | if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 386 | goto out; |
| 387 | if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr) |
| 388 | goto out; |
| 389 | |
| 390 | memslot = &kvm->memslots[mem->slot]; |
| 391 | base_gfn = mem->guest_phys_addr >> PAGE_SHIFT; |
| 392 | npages = mem->memory_size >> PAGE_SHIFT; |
| 393 | |
| 394 | if (!npages) |
| 395 | mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES; |
| 396 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 397 | new = old = *memslot; |
| 398 | |
| 399 | new.base_gfn = base_gfn; |
| 400 | new.npages = npages; |
| 401 | new.flags = mem->flags; |
| 402 | |
| 403 | /* Disallow changing a memory slot's size. */ |
| 404 | r = -EINVAL; |
| 405 | if (npages && old.npages && npages != old.npages) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 406 | goto out_free; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 407 | |
| 408 | /* Check for overlaps */ |
| 409 | r = -EEXIST; |
| 410 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { |
| 411 | struct kvm_memory_slot *s = &kvm->memslots[i]; |
| 412 | |
| 413 | if (s == memslot) |
| 414 | continue; |
| 415 | if (!((base_gfn + npages <= s->base_gfn) || |
| 416 | (base_gfn >= s->base_gfn + s->npages))) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 417 | goto out_free; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 418 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 419 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 420 | /* Free page dirty bitmap if unneeded */ |
| 421 | if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES)) |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 422 | new.dirty_bitmap = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 423 | |
| 424 | r = -ENOMEM; |
| 425 | |
| 426 | /* Allocate if a slot is being created */ |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 427 | if (npages && !new.rmap) { |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 428 | new.rmap = vmalloc(npages * sizeof(struct page *)); |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 429 | |
| 430 | if (!new.rmap) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 431 | goto out_free; |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 432 | |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 433 | memset(new.rmap, 0, npages * sizeof(*new.rmap)); |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 434 | |
Izik Eidus | 80b14b5 | 2007-10-25 11:54:04 +0200 | [diff] [blame] | 435 | new.user_alloc = user_alloc; |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 436 | if (user_alloc) |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 437 | new.userspace_addr = mem->userspace_addr; |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 438 | else { |
| 439 | down_write(¤t->mm->mmap_sem); |
| 440 | new.userspace_addr = do_mmap(NULL, 0, |
| 441 | npages * PAGE_SIZE, |
| 442 | PROT_READ | PROT_WRITE, |
| 443 | MAP_SHARED | MAP_ANONYMOUS, |
| 444 | 0); |
| 445 | up_write(¤t->mm->mmap_sem); |
| 446 | |
| 447 | if (IS_ERR((void *)new.userspace_addr)) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 448 | goto out_free; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 449 | } |
Izik Eidus | 80b14b5 | 2007-10-25 11:54:04 +0200 | [diff] [blame] | 450 | } else { |
| 451 | if (!old.user_alloc && old.rmap) { |
| 452 | int ret; |
| 453 | |
| 454 | down_write(¤t->mm->mmap_sem); |
| 455 | ret = do_munmap(current->mm, old.userspace_addr, |
| 456 | old.npages * PAGE_SIZE); |
| 457 | up_write(¤t->mm->mmap_sem); |
| 458 | if (ret < 0) |
| 459 | printk(KERN_WARNING |
| 460 | "kvm_vm_ioctl_set_memory_region: " |
| 461 | "failed to munmap memory\n"); |
| 462 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /* Allocate page dirty bitmap if needed */ |
| 466 | if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) { |
| 467 | unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8; |
| 468 | |
| 469 | new.dirty_bitmap = vmalloc(dirty_bytes); |
| 470 | if (!new.dirty_bitmap) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 471 | goto out_free; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 472 | memset(new.dirty_bitmap, 0, dirty_bytes); |
| 473 | } |
| 474 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 475 | if (mem->slot >= kvm->nmemslots) |
| 476 | kvm->nmemslots = mem->slot + 1; |
| 477 | |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 478 | if (!kvm->n_requested_mmu_pages) { |
| 479 | unsigned int n_pages; |
| 480 | |
| 481 | if (npages) { |
| 482 | n_pages = npages * KVM_PERMILLE_MMU_PAGES / 1000; |
| 483 | kvm_mmu_change_mmu_pages(kvm, kvm->n_alloc_mmu_pages + |
| 484 | n_pages); |
| 485 | } else { |
| 486 | unsigned int nr_mmu_pages; |
| 487 | |
| 488 | n_pages = old.npages * KVM_PERMILLE_MMU_PAGES / 1000; |
| 489 | nr_mmu_pages = kvm->n_alloc_mmu_pages - n_pages; |
| 490 | nr_mmu_pages = max(nr_mmu_pages, |
| 491 | (unsigned int) KVM_MIN_ALLOC_MMU_PAGES); |
| 492 | kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages); |
| 493 | } |
| 494 | } |
| 495 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 496 | *memslot = new; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 497 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 498 | kvm_mmu_slot_remove_write_access(kvm, mem->slot); |
| 499 | kvm_flush_remote_tlbs(kvm); |
| 500 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 501 | kvm_free_physmem_slot(&old, &new); |
| 502 | return 0; |
| 503 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 504 | out_free: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 505 | kvm_free_physmem_slot(&new, &old); |
| 506 | out: |
| 507 | return r; |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 508 | |
| 509 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 510 | EXPORT_SYMBOL_GPL(__kvm_set_memory_region); |
| 511 | |
| 512 | int kvm_set_memory_region(struct kvm *kvm, |
| 513 | struct kvm_userspace_memory_region *mem, |
| 514 | int user_alloc) |
| 515 | { |
| 516 | int r; |
| 517 | |
| 518 | mutex_lock(&kvm->lock); |
| 519 | r = __kvm_set_memory_region(kvm, mem, user_alloc); |
| 520 | mutex_unlock(&kvm->lock); |
| 521 | return r; |
| 522 | } |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 523 | EXPORT_SYMBOL_GPL(kvm_set_memory_region); |
| 524 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 525 | int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, |
| 526 | struct |
| 527 | kvm_userspace_memory_region *mem, |
| 528 | int user_alloc) |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 529 | { |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 530 | if (mem->slot >= KVM_MEMORY_SLOTS) |
| 531 | return -EINVAL; |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 532 | return kvm_set_memory_region(kvm, mem, user_alloc); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | /* |
| 536 | * Get (and clear) the dirty memory log for a memory slot. |
| 537 | */ |
Avi Kivity | 2c6f5df | 2007-02-20 18:27:58 +0200 | [diff] [blame] | 538 | static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, |
| 539 | struct kvm_dirty_log *log) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 540 | { |
| 541 | struct kvm_memory_slot *memslot; |
| 542 | int r, i; |
| 543 | int n; |
| 544 | unsigned long any = 0; |
| 545 | |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 546 | mutex_lock(&kvm->lock); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 547 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 548 | r = -EINVAL; |
| 549 | if (log->slot >= KVM_MEMORY_SLOTS) |
| 550 | goto out; |
| 551 | |
| 552 | memslot = &kvm->memslots[log->slot]; |
| 553 | r = -ENOENT; |
| 554 | if (!memslot->dirty_bitmap) |
| 555 | goto out; |
| 556 | |
Uri Lublin | cd1a4a9 | 2007-02-22 16:43:09 +0200 | [diff] [blame] | 557 | n = ALIGN(memslot->npages, BITS_PER_LONG) / 8; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 558 | |
Uri Lublin | cd1a4a9 | 2007-02-22 16:43:09 +0200 | [diff] [blame] | 559 | for (i = 0; !any && i < n/sizeof(long); ++i) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 560 | any = memslot->dirty_bitmap[i]; |
| 561 | |
| 562 | r = -EFAULT; |
| 563 | if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n)) |
| 564 | goto out; |
| 565 | |
Rusty Russell | 3921491 | 2007-07-31 19:57:47 +1000 | [diff] [blame] | 566 | /* If nothing is dirty, don't bother messing with page tables. */ |
| 567 | if (any) { |
Rusty Russell | 3921491 | 2007-07-31 19:57:47 +1000 | [diff] [blame] | 568 | kvm_mmu_slot_remove_write_access(kvm, log->slot); |
| 569 | kvm_flush_remote_tlbs(kvm); |
| 570 | memset(memslot->dirty_bitmap, 0, n); |
Rusty Russell | 3921491 | 2007-07-31 19:57:47 +1000 | [diff] [blame] | 571 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 572 | |
| 573 | r = 0; |
| 574 | |
| 575 | out: |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 576 | mutex_unlock(&kvm->lock); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 577 | return r; |
| 578 | } |
| 579 | |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 580 | int is_error_page(struct page *page) |
| 581 | { |
| 582 | return page == bad_page; |
| 583 | } |
| 584 | EXPORT_SYMBOL_GPL(is_error_page); |
| 585 | |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 586 | gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn) |
Avi Kivity | e820754 | 2007-03-30 16:54:30 +0300 | [diff] [blame] | 587 | { |
| 588 | int i; |
| 589 | struct kvm_mem_alias *alias; |
| 590 | |
| 591 | for (i = 0; i < kvm->naliases; ++i) { |
| 592 | alias = &kvm->aliases[i]; |
| 593 | if (gfn >= alias->base_gfn |
| 594 | && gfn < alias->base_gfn + alias->npages) |
| 595 | return alias->target_gfn + gfn - alias->base_gfn; |
| 596 | } |
| 597 | return gfn; |
| 598 | } |
| 599 | |
| 600 | static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 601 | { |
| 602 | int i; |
| 603 | |
| 604 | for (i = 0; i < kvm->nmemslots; ++i) { |
| 605 | struct kvm_memory_slot *memslot = &kvm->memslots[i]; |
| 606 | |
| 607 | if (gfn >= memslot->base_gfn |
| 608 | && gfn < memslot->base_gfn + memslot->npages) |
| 609 | return memslot; |
| 610 | } |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 611 | return NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 612 | } |
Avi Kivity | e820754 | 2007-03-30 16:54:30 +0300 | [diff] [blame] | 613 | |
| 614 | struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn) |
| 615 | { |
| 616 | gfn = unalias_gfn(kvm, gfn); |
| 617 | return __gfn_to_memslot(kvm, gfn); |
| 618 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 619 | |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 620 | int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn) |
| 621 | { |
| 622 | int i; |
| 623 | |
| 624 | gfn = unalias_gfn(kvm, gfn); |
| 625 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { |
| 626 | struct kvm_memory_slot *memslot = &kvm->memslots[i]; |
| 627 | |
| 628 | if (gfn >= memslot->base_gfn |
| 629 | && gfn < memslot->base_gfn + memslot->npages) |
| 630 | return 1; |
| 631 | } |
| 632 | return 0; |
| 633 | } |
| 634 | EXPORT_SYMBOL_GPL(kvm_is_visible_gfn); |
| 635 | |
Anthony Liguori | aab61cc | 2007-10-29 15:15:20 -0500 | [diff] [blame^] | 636 | /* |
| 637 | * Requires current->mm->mmap_sem to be held |
| 638 | */ |
| 639 | static struct page *__gfn_to_page(struct kvm *kvm, gfn_t gfn) |
Avi Kivity | 954bbbc | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 640 | { |
| 641 | struct kvm_memory_slot *slot; |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 642 | struct page *page[1]; |
| 643 | int npages; |
Avi Kivity | 954bbbc | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 644 | |
Avi Kivity | 6039522 | 2007-10-21 11:03:36 +0200 | [diff] [blame] | 645 | might_sleep(); |
| 646 | |
Avi Kivity | e820754 | 2007-03-30 16:54:30 +0300 | [diff] [blame] | 647 | gfn = unalias_gfn(kvm, gfn); |
| 648 | slot = __gfn_to_memslot(kvm, gfn); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 649 | if (!slot) { |
| 650 | get_page(bad_page); |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 651 | return bad_page; |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 652 | } |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 653 | |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 654 | npages = get_user_pages(current, current->mm, |
| 655 | slot->userspace_addr |
| 656 | + (gfn - slot->base_gfn) * PAGE_SIZE, 1, |
| 657 | 1, 1, page, NULL); |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 658 | if (npages != 1) { |
| 659 | get_page(bad_page); |
| 660 | return bad_page; |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 661 | } |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 662 | |
| 663 | return page[0]; |
Avi Kivity | 954bbbc | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 664 | } |
Anthony Liguori | aab61cc | 2007-10-29 15:15:20 -0500 | [diff] [blame^] | 665 | |
| 666 | struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn) |
| 667 | { |
| 668 | struct page *page; |
| 669 | |
| 670 | down_read(¤t->mm->mmap_sem); |
| 671 | page = __gfn_to_page(kvm, gfn); |
| 672 | up_read(¤t->mm->mmap_sem); |
| 673 | |
| 674 | return page; |
| 675 | } |
| 676 | |
Avi Kivity | 954bbbc | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 677 | EXPORT_SYMBOL_GPL(gfn_to_page); |
| 678 | |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 679 | void kvm_release_page(struct page *page) |
| 680 | { |
| 681 | if (!PageReserved(page)) |
| 682 | SetPageDirty(page); |
| 683 | put_page(page); |
| 684 | } |
| 685 | EXPORT_SYMBOL_GPL(kvm_release_page); |
| 686 | |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 687 | static int next_segment(unsigned long len, int offset) |
| 688 | { |
| 689 | if (len > PAGE_SIZE - offset) |
| 690 | return PAGE_SIZE - offset; |
| 691 | else |
| 692 | return len; |
| 693 | } |
| 694 | |
| 695 | int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, |
| 696 | int len) |
| 697 | { |
| 698 | void *page_virt; |
| 699 | struct page *page; |
| 700 | |
| 701 | page = gfn_to_page(kvm, gfn); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 702 | if (is_error_page(page)) { |
| 703 | kvm_release_page(page); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 704 | return -EFAULT; |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 705 | } |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 706 | page_virt = kmap_atomic(page, KM_USER0); |
| 707 | |
| 708 | memcpy(data, page_virt + offset, len); |
| 709 | |
| 710 | kunmap_atomic(page_virt, KM_USER0); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 711 | kvm_release_page(page); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 712 | return 0; |
| 713 | } |
| 714 | EXPORT_SYMBOL_GPL(kvm_read_guest_page); |
| 715 | |
| 716 | int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len) |
| 717 | { |
| 718 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 719 | int seg; |
| 720 | int offset = offset_in_page(gpa); |
| 721 | int ret; |
| 722 | |
| 723 | while ((seg = next_segment(len, offset)) != 0) { |
| 724 | ret = kvm_read_guest_page(kvm, gfn, data, offset, seg); |
| 725 | if (ret < 0) |
| 726 | return ret; |
| 727 | offset = 0; |
| 728 | len -= seg; |
| 729 | data += seg; |
| 730 | ++gfn; |
| 731 | } |
| 732 | return 0; |
| 733 | } |
| 734 | EXPORT_SYMBOL_GPL(kvm_read_guest); |
| 735 | |
| 736 | int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data, |
| 737 | int offset, int len) |
| 738 | { |
| 739 | void *page_virt; |
| 740 | struct page *page; |
| 741 | |
| 742 | page = gfn_to_page(kvm, gfn); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 743 | if (is_error_page(page)) { |
| 744 | kvm_release_page(page); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 745 | return -EFAULT; |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 746 | } |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 747 | page_virt = kmap_atomic(page, KM_USER0); |
| 748 | |
| 749 | memcpy(page_virt + offset, data, len); |
| 750 | |
| 751 | kunmap_atomic(page_virt, KM_USER0); |
| 752 | mark_page_dirty(kvm, gfn); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 753 | kvm_release_page(page); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 754 | return 0; |
| 755 | } |
| 756 | EXPORT_SYMBOL_GPL(kvm_write_guest_page); |
| 757 | |
| 758 | int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data, |
| 759 | unsigned long len) |
| 760 | { |
| 761 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 762 | int seg; |
| 763 | int offset = offset_in_page(gpa); |
| 764 | int ret; |
| 765 | |
| 766 | while ((seg = next_segment(len, offset)) != 0) { |
| 767 | ret = kvm_write_guest_page(kvm, gfn, data, offset, seg); |
| 768 | if (ret < 0) |
| 769 | return ret; |
| 770 | offset = 0; |
| 771 | len -= seg; |
| 772 | data += seg; |
| 773 | ++gfn; |
| 774 | } |
| 775 | return 0; |
| 776 | } |
| 777 | |
| 778 | int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len) |
| 779 | { |
| 780 | void *page_virt; |
| 781 | struct page *page; |
| 782 | |
| 783 | page = gfn_to_page(kvm, gfn); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 784 | if (is_error_page(page)) { |
| 785 | kvm_release_page(page); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 786 | return -EFAULT; |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 787 | } |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 788 | page_virt = kmap_atomic(page, KM_USER0); |
| 789 | |
| 790 | memset(page_virt + offset, 0, len); |
| 791 | |
| 792 | kunmap_atomic(page_virt, KM_USER0); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 793 | kvm_release_page(page); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 794 | return 0; |
| 795 | } |
| 796 | EXPORT_SYMBOL_GPL(kvm_clear_guest_page); |
| 797 | |
| 798 | int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len) |
| 799 | { |
| 800 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 801 | int seg; |
| 802 | int offset = offset_in_page(gpa); |
| 803 | int ret; |
| 804 | |
| 805 | while ((seg = next_segment(len, offset)) != 0) { |
| 806 | ret = kvm_clear_guest_page(kvm, gfn, offset, seg); |
| 807 | if (ret < 0) |
| 808 | return ret; |
| 809 | offset = 0; |
| 810 | len -= seg; |
| 811 | ++gfn; |
| 812 | } |
| 813 | return 0; |
| 814 | } |
| 815 | EXPORT_SYMBOL_GPL(kvm_clear_guest); |
| 816 | |
Rusty Russell | 7e9d619 | 2007-07-31 20:41:14 +1000 | [diff] [blame] | 817 | /* WARNING: Does not work on aliased pages. */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 818 | void mark_page_dirty(struct kvm *kvm, gfn_t gfn) |
| 819 | { |
Nguyen Anh Quynh | 3138994 | 2007-06-05 10:35:19 +0300 | [diff] [blame] | 820 | struct kvm_memory_slot *memslot; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 821 | |
Rusty Russell | 7e9d619 | 2007-07-31 20:41:14 +1000 | [diff] [blame] | 822 | memslot = __gfn_to_memslot(kvm, gfn); |
| 823 | if (memslot && memslot->dirty_bitmap) { |
| 824 | unsigned long rel_gfn = gfn - memslot->base_gfn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 825 | |
Rusty Russell | 7e9d619 | 2007-07-31 20:41:14 +1000 | [diff] [blame] | 826 | /* avoid RMW */ |
| 827 | if (!test_bit(rel_gfn, memslot->dirty_bitmap)) |
| 828 | set_bit(rel_gfn, memslot->dirty_bitmap); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 829 | } |
| 830 | } |
| 831 | |
Laurent Vivier | e7d5d76 | 2007-07-30 13:41:19 +0300 | [diff] [blame] | 832 | int emulator_read_std(unsigned long addr, |
Avi Kivity | 4c690a1 | 2007-04-22 15:28:19 +0300 | [diff] [blame] | 833 | void *val, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 834 | unsigned int bytes, |
Laurent Vivier | cebff02 | 2007-07-30 13:35:24 +0300 | [diff] [blame] | 835 | struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 836 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 837 | void *data = val; |
| 838 | |
| 839 | while (bytes) { |
| 840 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); |
| 841 | unsigned offset = addr & (PAGE_SIZE-1); |
| 842 | unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 843 | int ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 844 | |
| 845 | if (gpa == UNMAPPED_GVA) |
| 846 | return X86EMUL_PROPAGATE_FAULT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 847 | ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy); |
| 848 | if (ret < 0) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 849 | return X86EMUL_UNHANDLEABLE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 850 | |
| 851 | bytes -= tocopy; |
| 852 | data += tocopy; |
| 853 | addr += tocopy; |
| 854 | } |
| 855 | |
| 856 | return X86EMUL_CONTINUE; |
| 857 | } |
Laurent Vivier | e7d5d76 | 2007-07-30 13:41:19 +0300 | [diff] [blame] | 858 | EXPORT_SYMBOL_GPL(emulator_read_std); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 859 | |
| 860 | static int emulator_write_std(unsigned long addr, |
Avi Kivity | 4c690a1 | 2007-04-22 15:28:19 +0300 | [diff] [blame] | 861 | const void *val, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 862 | unsigned int bytes, |
Laurent Vivier | cebff02 | 2007-07-30 13:35:24 +0300 | [diff] [blame] | 863 | struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 864 | { |
Rusty Russell | f024247 | 2007-08-01 10:48:02 +1000 | [diff] [blame] | 865 | pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 866 | return X86EMUL_UNHANDLEABLE; |
| 867 | } |
| 868 | |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 869 | /* |
| 870 | * Only apic need an MMIO device hook, so shortcut now.. |
| 871 | */ |
| 872 | static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu, |
| 873 | gpa_t addr) |
| 874 | { |
| 875 | struct kvm_io_device *dev; |
| 876 | |
| 877 | if (vcpu->apic) { |
| 878 | dev = &vcpu->apic->dev; |
| 879 | if (dev->in_range(dev, addr)) |
| 880 | return dev; |
| 881 | } |
| 882 | return NULL; |
| 883 | } |
| 884 | |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 885 | static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu, |
| 886 | gpa_t addr) |
| 887 | { |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 888 | struct kvm_io_device *dev; |
| 889 | |
| 890 | dev = vcpu_find_pervcpu_dev(vcpu, addr); |
| 891 | if (dev == NULL) |
| 892 | dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr); |
| 893 | return dev; |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 894 | } |
| 895 | |
Eddie Dong | 7490634 | 2007-06-19 18:05:03 +0300 | [diff] [blame] | 896 | static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu, |
| 897 | gpa_t addr) |
| 898 | { |
| 899 | return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr); |
| 900 | } |
| 901 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 902 | static int emulator_read_emulated(unsigned long addr, |
Avi Kivity | 4c690a1 | 2007-04-22 15:28:19 +0300 | [diff] [blame] | 903 | void *val, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 904 | unsigned int bytes, |
Laurent Vivier | cebff02 | 2007-07-30 13:35:24 +0300 | [diff] [blame] | 905 | struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 906 | { |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 907 | struct kvm_io_device *mmio_dev; |
| 908 | gpa_t gpa; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 909 | |
| 910 | if (vcpu->mmio_read_completed) { |
| 911 | memcpy(val, vcpu->mmio_data, bytes); |
| 912 | vcpu->mmio_read_completed = 0; |
| 913 | return X86EMUL_CONTINUE; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 914 | } |
Avi Kivity | d27d4ac | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 915 | |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 916 | gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 917 | |
| 918 | /* For APIC access vmexit */ |
| 919 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) |
| 920 | goto mmio; |
| 921 | |
| 922 | if (emulator_read_std(addr, val, bytes, vcpu) |
| 923 | == X86EMUL_CONTINUE) |
| 924 | return X86EMUL_CONTINUE; |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 925 | if (gpa == UNMAPPED_GVA) |
| 926 | return X86EMUL_PROPAGATE_FAULT; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 927 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 928 | mmio: |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 929 | /* |
| 930 | * Is this MMIO handled locally? |
| 931 | */ |
| 932 | mmio_dev = vcpu_find_mmio_dev(vcpu, gpa); |
| 933 | if (mmio_dev) { |
| 934 | kvm_iodevice_read(mmio_dev, gpa, bytes, val); |
| 935 | return X86EMUL_CONTINUE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 936 | } |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 937 | |
| 938 | vcpu->mmio_needed = 1; |
| 939 | vcpu->mmio_phys_addr = gpa; |
| 940 | vcpu->mmio_size = bytes; |
| 941 | vcpu->mmio_is_write = 0; |
| 942 | |
| 943 | return X86EMUL_UNHANDLEABLE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 944 | } |
| 945 | |
Avi Kivity | da4a00f | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 946 | static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, |
Avi Kivity | 4c690a1 | 2007-04-22 15:28:19 +0300 | [diff] [blame] | 947 | const void *val, int bytes) |
Avi Kivity | da4a00f | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 948 | { |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 949 | int ret; |
Avi Kivity | da4a00f | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 950 | |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 951 | ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes); |
| 952 | if (ret < 0) |
Avi Kivity | da4a00f | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 953 | return 0; |
Shaohua Li | fe551881 | 2007-07-23 14:51:39 +0800 | [diff] [blame] | 954 | kvm_mmu_pte_write(vcpu, gpa, val, bytes); |
Avi Kivity | da4a00f | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 955 | return 1; |
| 956 | } |
| 957 | |
Avi Kivity | b0fcd90 | 2007-07-22 18:48:54 +0300 | [diff] [blame] | 958 | static int emulator_write_emulated_onepage(unsigned long addr, |
| 959 | const void *val, |
| 960 | unsigned int bytes, |
Laurent Vivier | cebff02 | 2007-07-30 13:35:24 +0300 | [diff] [blame] | 961 | struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 962 | { |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 963 | struct kvm_io_device *mmio_dev; |
| 964 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 965 | |
Avi Kivity | c9047f5 | 2007-04-17 10:53:22 +0300 | [diff] [blame] | 966 | if (gpa == UNMAPPED_GVA) { |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 967 | kvm_x86_ops->inject_page_fault(vcpu, addr, 2); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 968 | return X86EMUL_PROPAGATE_FAULT; |
Avi Kivity | c9047f5 | 2007-04-17 10:53:22 +0300 | [diff] [blame] | 969 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 970 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 971 | /* For APIC access vmexit */ |
| 972 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) |
| 973 | goto mmio; |
| 974 | |
Avi Kivity | da4a00f | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 975 | if (emulator_write_phys(vcpu, gpa, val, bytes)) |
| 976 | return X86EMUL_CONTINUE; |
| 977 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 978 | mmio: |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 979 | /* |
| 980 | * Is this MMIO handled locally? |
| 981 | */ |
| 982 | mmio_dev = vcpu_find_mmio_dev(vcpu, gpa); |
| 983 | if (mmio_dev) { |
| 984 | kvm_iodevice_write(mmio_dev, gpa, bytes, val); |
| 985 | return X86EMUL_CONTINUE; |
| 986 | } |
| 987 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 988 | vcpu->mmio_needed = 1; |
| 989 | vcpu->mmio_phys_addr = gpa; |
| 990 | vcpu->mmio_size = bytes; |
| 991 | vcpu->mmio_is_write = 1; |
Avi Kivity | 4c690a1 | 2007-04-22 15:28:19 +0300 | [diff] [blame] | 992 | memcpy(vcpu->mmio_data, val, bytes); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 993 | |
| 994 | return X86EMUL_CONTINUE; |
| 995 | } |
| 996 | |
Laurent Vivier | e7d5d76 | 2007-07-30 13:41:19 +0300 | [diff] [blame] | 997 | int emulator_write_emulated(unsigned long addr, |
Avi Kivity | b0fcd90 | 2007-07-22 18:48:54 +0300 | [diff] [blame] | 998 | const void *val, |
| 999 | unsigned int bytes, |
Laurent Vivier | cebff02 | 2007-07-30 13:35:24 +0300 | [diff] [blame] | 1000 | struct kvm_vcpu *vcpu) |
Avi Kivity | b0fcd90 | 2007-07-22 18:48:54 +0300 | [diff] [blame] | 1001 | { |
| 1002 | /* Crossing a page boundary? */ |
| 1003 | if (((addr + bytes - 1) ^ addr) & PAGE_MASK) { |
| 1004 | int rc, now; |
| 1005 | |
| 1006 | now = -addr & ~PAGE_MASK; |
Laurent Vivier | cebff02 | 2007-07-30 13:35:24 +0300 | [diff] [blame] | 1007 | rc = emulator_write_emulated_onepage(addr, val, now, vcpu); |
Avi Kivity | b0fcd90 | 2007-07-22 18:48:54 +0300 | [diff] [blame] | 1008 | if (rc != X86EMUL_CONTINUE) |
| 1009 | return rc; |
| 1010 | addr += now; |
| 1011 | val += now; |
| 1012 | bytes -= now; |
| 1013 | } |
Laurent Vivier | cebff02 | 2007-07-30 13:35:24 +0300 | [diff] [blame] | 1014 | return emulator_write_emulated_onepage(addr, val, bytes, vcpu); |
Avi Kivity | b0fcd90 | 2007-07-22 18:48:54 +0300 | [diff] [blame] | 1015 | } |
Laurent Vivier | e7d5d76 | 2007-07-30 13:41:19 +0300 | [diff] [blame] | 1016 | EXPORT_SYMBOL_GPL(emulator_write_emulated); |
Avi Kivity | b0fcd90 | 2007-07-22 18:48:54 +0300 | [diff] [blame] | 1017 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1018 | static int emulator_cmpxchg_emulated(unsigned long addr, |
Avi Kivity | 4c690a1 | 2007-04-22 15:28:19 +0300 | [diff] [blame] | 1019 | const void *old, |
| 1020 | const void *new, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1021 | unsigned int bytes, |
Laurent Vivier | cebff02 | 2007-07-30 13:35:24 +0300 | [diff] [blame] | 1022 | struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1023 | { |
| 1024 | static int reported; |
| 1025 | |
| 1026 | if (!reported) { |
| 1027 | reported = 1; |
| 1028 | printk(KERN_WARNING "kvm: emulating exchange as write\n"); |
| 1029 | } |
Laurent Vivier | cebff02 | 2007-07-30 13:35:24 +0300 | [diff] [blame] | 1030 | return emulator_write_emulated(addr, new, bytes, vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 1034 | { |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1035 | return kvm_x86_ops->get_segment_base(vcpu, seg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address) |
| 1039 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1040 | return X86EMUL_CONTINUE; |
| 1041 | } |
| 1042 | |
| 1043 | int emulate_clts(struct kvm_vcpu *vcpu) |
| 1044 | { |
Amit Shah | 404fb88 | 2007-11-19 17:57:35 +0200 | [diff] [blame] | 1045 | kvm_x86_ops->set_cr0(vcpu, vcpu->cr0 & ~X86_CR0_TS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1046 | return X86EMUL_CONTINUE; |
| 1047 | } |
| 1048 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1049 | int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1050 | { |
| 1051 | struct kvm_vcpu *vcpu = ctxt->vcpu; |
| 1052 | |
| 1053 | switch (dr) { |
| 1054 | case 0 ... 3: |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1055 | *dest = kvm_x86_ops->get_dr(vcpu, dr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1056 | return X86EMUL_CONTINUE; |
| 1057 | default: |
Rusty Russell | f024247 | 2007-08-01 10:48:02 +1000 | [diff] [blame] | 1058 | pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1059 | return X86EMUL_UNHANDLEABLE; |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value) |
| 1064 | { |
| 1065 | unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U; |
| 1066 | int exception; |
| 1067 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1068 | kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1069 | if (exception) { |
| 1070 | /* FIXME: better handling */ |
| 1071 | return X86EMUL_UNHANDLEABLE; |
| 1072 | } |
| 1073 | return X86EMUL_CONTINUE; |
| 1074 | } |
| 1075 | |
Avi Kivity | 054b136 | 2007-09-12 13:21:09 +0300 | [diff] [blame] | 1076 | void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1077 | { |
| 1078 | static int reported; |
| 1079 | u8 opcodes[4]; |
Avi Kivity | 054b136 | 2007-09-12 13:21:09 +0300 | [diff] [blame] | 1080 | unsigned long rip = vcpu->rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1081 | unsigned long rip_linear; |
| 1082 | |
Avi Kivity | 054b136 | 2007-09-12 13:21:09 +0300 | [diff] [blame] | 1083 | rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1084 | |
| 1085 | if (reported) |
| 1086 | return; |
| 1087 | |
Avi Kivity | 054b136 | 2007-09-12 13:21:09 +0300 | [diff] [blame] | 1088 | emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1089 | |
Avi Kivity | 054b136 | 2007-09-12 13:21:09 +0300 | [diff] [blame] | 1090 | printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n", |
| 1091 | context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1092 | reported = 1; |
| 1093 | } |
Avi Kivity | 054b136 | 2007-09-12 13:21:09 +0300 | [diff] [blame] | 1094 | EXPORT_SYMBOL_GPL(kvm_report_emulation_failure); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1095 | |
| 1096 | struct x86_emulate_ops emulate_ops = { |
| 1097 | .read_std = emulator_read_std, |
| 1098 | .write_std = emulator_write_std, |
| 1099 | .read_emulated = emulator_read_emulated, |
| 1100 | .write_emulated = emulator_write_emulated, |
| 1101 | .cmpxchg_emulated = emulator_cmpxchg_emulated, |
| 1102 | }; |
| 1103 | |
| 1104 | int emulate_instruction(struct kvm_vcpu *vcpu, |
| 1105 | struct kvm_run *run, |
| 1106 | unsigned long cr2, |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 1107 | u16 error_code, |
| 1108 | int no_decode) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1109 | { |
Laurent Vivier | a22436b | 2007-09-24 17:00:58 +0200 | [diff] [blame] | 1110 | int r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1111 | |
Avi Kivity | e7df56e | 2007-03-14 15:54:54 +0200 | [diff] [blame] | 1112 | vcpu->mmio_fault_cr2 = cr2; |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1113 | kvm_x86_ops->cache_regs(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1114 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1115 | vcpu->mmio_is_write = 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 1116 | vcpu->pio.string = 0; |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 1117 | |
| 1118 | if (!no_decode) { |
| 1119 | int cs_db, cs_l; |
| 1120 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
| 1121 | |
| 1122 | vcpu->emulate_ctxt.vcpu = vcpu; |
| 1123 | vcpu->emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu); |
| 1124 | vcpu->emulate_ctxt.cr2 = cr2; |
| 1125 | vcpu->emulate_ctxt.mode = |
| 1126 | (vcpu->emulate_ctxt.eflags & X86_EFLAGS_VM) |
| 1127 | ? X86EMUL_MODE_REAL : cs_l |
| 1128 | ? X86EMUL_MODE_PROT64 : cs_db |
| 1129 | ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16; |
| 1130 | |
| 1131 | if (vcpu->emulate_ctxt.mode == X86EMUL_MODE_PROT64) { |
| 1132 | vcpu->emulate_ctxt.cs_base = 0; |
| 1133 | vcpu->emulate_ctxt.ds_base = 0; |
| 1134 | vcpu->emulate_ctxt.es_base = 0; |
| 1135 | vcpu->emulate_ctxt.ss_base = 0; |
| 1136 | } else { |
| 1137 | vcpu->emulate_ctxt.cs_base = |
| 1138 | get_segment_base(vcpu, VCPU_SREG_CS); |
| 1139 | vcpu->emulate_ctxt.ds_base = |
| 1140 | get_segment_base(vcpu, VCPU_SREG_DS); |
| 1141 | vcpu->emulate_ctxt.es_base = |
| 1142 | get_segment_base(vcpu, VCPU_SREG_ES); |
| 1143 | vcpu->emulate_ctxt.ss_base = |
| 1144 | get_segment_base(vcpu, VCPU_SREG_SS); |
| 1145 | } |
| 1146 | |
| 1147 | vcpu->emulate_ctxt.gs_base = |
| 1148 | get_segment_base(vcpu, VCPU_SREG_GS); |
| 1149 | vcpu->emulate_ctxt.fs_base = |
| 1150 | get_segment_base(vcpu, VCPU_SREG_FS); |
| 1151 | |
| 1152 | r = x86_decode_insn(&vcpu->emulate_ctxt, &emulate_ops); |
Laurent Vivier | a22436b | 2007-09-24 17:00:58 +0200 | [diff] [blame] | 1153 | if (r) { |
| 1154 | if (kvm_mmu_unprotect_page_virt(vcpu, cr2)) |
| 1155 | return EMULATE_DONE; |
| 1156 | return EMULATE_FAIL; |
| 1157 | } |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 1158 | } |
| 1159 | |
Laurent Vivier | a22436b | 2007-09-24 17:00:58 +0200 | [diff] [blame] | 1160 | r = x86_emulate_insn(&vcpu->emulate_ctxt, &emulate_ops); |
Laurent Vivier | 1be3aa4 | 2007-09-18 11:27:27 +0200 | [diff] [blame] | 1161 | |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 1162 | if (vcpu->pio.string) |
| 1163 | return EMULATE_DO_MMIO; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1164 | |
| 1165 | if ((r || vcpu->mmio_is_write) && run) { |
Jeff Dike | 8fc0d08 | 2007-07-17 12:26:59 -0400 | [diff] [blame] | 1166 | run->exit_reason = KVM_EXIT_MMIO; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1167 | run->mmio.phys_addr = vcpu->mmio_phys_addr; |
| 1168 | memcpy(run->mmio.data, vcpu->mmio_data, 8); |
| 1169 | run->mmio.len = vcpu->mmio_size; |
| 1170 | run->mmio.is_write = vcpu->mmio_is_write; |
| 1171 | } |
| 1172 | |
| 1173 | if (r) { |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1174 | if (kvm_mmu_unprotect_page_virt(vcpu, cr2)) |
| 1175 | return EMULATE_DONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1176 | if (!vcpu->mmio_needed) { |
Avi Kivity | 054b136 | 2007-09-12 13:21:09 +0300 | [diff] [blame] | 1177 | kvm_report_emulation_failure(vcpu, "mmio"); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1178 | return EMULATE_FAIL; |
| 1179 | } |
| 1180 | return EMULATE_DO_MMIO; |
| 1181 | } |
| 1182 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1183 | kvm_x86_ops->decache_regs(vcpu); |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 1184 | kvm_x86_ops->set_rflags(vcpu, vcpu->emulate_ctxt.eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1185 | |
Avi Kivity | 02c8320 | 2007-04-29 15:02:17 +0300 | [diff] [blame] | 1186 | if (vcpu->mmio_is_write) { |
| 1187 | vcpu->mmio_needed = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1188 | return EMULATE_DO_MMIO; |
Avi Kivity | 02c8320 | 2007-04-29 15:02:17 +0300 | [diff] [blame] | 1189 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1190 | |
| 1191 | return EMULATE_DONE; |
| 1192 | } |
| 1193 | EXPORT_SYMBOL_GPL(emulate_instruction); |
| 1194 | |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 1195 | /* |
| 1196 | * The vCPU has executed a HLT instruction with in-kernel mode enabled. |
| 1197 | */ |
He, Qing | c5ec153 | 2007-09-03 17:07:41 +0300 | [diff] [blame] | 1198 | static void kvm_vcpu_block(struct kvm_vcpu *vcpu) |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 1199 | { |
| 1200 | DECLARE_WAITQUEUE(wait, current); |
| 1201 | |
| 1202 | add_wait_queue(&vcpu->wq, &wait); |
| 1203 | |
| 1204 | /* |
| 1205 | * We will block until either an interrupt or a signal wakes us up |
| 1206 | */ |
He, Qing | c5ec153 | 2007-09-03 17:07:41 +0300 | [diff] [blame] | 1207 | while (!kvm_cpu_has_interrupt(vcpu) |
| 1208 | && !signal_pending(current) |
| 1209 | && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE |
| 1210 | && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) { |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 1211 | set_current_state(TASK_INTERRUPTIBLE); |
| 1212 | vcpu_put(vcpu); |
| 1213 | schedule(); |
| 1214 | vcpu_load(vcpu); |
| 1215 | } |
| 1216 | |
He, Qing | c5ec153 | 2007-09-03 17:07:41 +0300 | [diff] [blame] | 1217 | __set_current_state(TASK_RUNNING); |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 1218 | remove_wait_queue(&vcpu->wq, &wait); |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 1219 | } |
| 1220 | |
Avi Kivity | d3bef15 | 2007-06-05 15:53:05 +0300 | [diff] [blame] | 1221 | int kvm_emulate_halt(struct kvm_vcpu *vcpu) |
| 1222 | { |
Avi Kivity | d3bef15 | 2007-06-05 15:53:05 +0300 | [diff] [blame] | 1223 | ++vcpu->stat.halt_exits; |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 1224 | if (irqchip_in_kernel(vcpu->kvm)) { |
He, Qing | c5ec153 | 2007-09-03 17:07:41 +0300 | [diff] [blame] | 1225 | vcpu->mp_state = VCPU_MP_STATE_HALTED; |
| 1226 | kvm_vcpu_block(vcpu); |
| 1227 | if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE) |
| 1228 | return -EINTR; |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 1229 | return 1; |
| 1230 | } else { |
| 1231 | vcpu->run->exit_reason = KVM_EXIT_HLT; |
| 1232 | return 0; |
| 1233 | } |
Avi Kivity | d3bef15 | 2007-06-05 15:53:05 +0300 | [diff] [blame] | 1234 | } |
| 1235 | EXPORT_SYMBOL_GPL(kvm_emulate_halt); |
| 1236 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1237 | int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) |
Avi Kivity | 270fd9b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1238 | { |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1239 | unsigned long nr, a0, a1, a2, a3, ret; |
Avi Kivity | 270fd9b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1240 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1241 | kvm_x86_ops->cache_regs(vcpu); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1242 | |
| 1243 | nr = vcpu->regs[VCPU_REGS_RAX]; |
| 1244 | a0 = vcpu->regs[VCPU_REGS_RBX]; |
| 1245 | a1 = vcpu->regs[VCPU_REGS_RCX]; |
| 1246 | a2 = vcpu->regs[VCPU_REGS_RDX]; |
| 1247 | a3 = vcpu->regs[VCPU_REGS_RSI]; |
| 1248 | |
| 1249 | if (!is_long_mode(vcpu)) { |
| 1250 | nr &= 0xFFFFFFFF; |
| 1251 | a0 &= 0xFFFFFFFF; |
| 1252 | a1 &= 0xFFFFFFFF; |
| 1253 | a2 &= 0xFFFFFFFF; |
| 1254 | a3 &= 0xFFFFFFFF; |
Avi Kivity | 270fd9b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1255 | } |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1256 | |
Avi Kivity | 270fd9b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1257 | switch (nr) { |
| 1258 | default: |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1259 | ret = -KVM_ENOSYS; |
| 1260 | break; |
Avi Kivity | 270fd9b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1261 | } |
| 1262 | vcpu->regs[VCPU_REGS_RAX] = ret; |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1263 | kvm_x86_ops->decache_regs(vcpu); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1264 | return 0; |
Avi Kivity | 270fd9b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1265 | } |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1266 | EXPORT_SYMBOL_GPL(kvm_emulate_hypercall); |
| 1267 | |
| 1268 | int kvm_fix_hypercall(struct kvm_vcpu *vcpu) |
| 1269 | { |
| 1270 | char instruction[3]; |
| 1271 | int ret = 0; |
| 1272 | |
| 1273 | mutex_lock(&vcpu->kvm->lock); |
| 1274 | |
| 1275 | /* |
| 1276 | * Blow out the MMU to ensure that no other VCPU has an active mapping |
| 1277 | * to ensure that the updated hypercall appears atomically across all |
| 1278 | * VCPUs. |
| 1279 | */ |
| 1280 | kvm_mmu_zap_all(vcpu->kvm); |
| 1281 | |
| 1282 | kvm_x86_ops->cache_regs(vcpu); |
| 1283 | kvm_x86_ops->patch_hypercall(vcpu, instruction); |
| 1284 | if (emulator_write_emulated(vcpu->rip, instruction, 3, vcpu) |
| 1285 | != X86EMUL_CONTINUE) |
| 1286 | ret = -EFAULT; |
| 1287 | |
| 1288 | mutex_unlock(&vcpu->kvm->lock); |
| 1289 | |
| 1290 | return ret; |
| 1291 | } |
Avi Kivity | 270fd9b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1292 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1293 | static u64 mk_cr_64(u64 curr_cr, u32 new_val) |
| 1294 | { |
| 1295 | return (curr_cr & ~((1ULL << 32) - 1)) | new_val; |
| 1296 | } |
| 1297 | |
| 1298 | void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) |
| 1299 | { |
| 1300 | struct descriptor_table dt = { limit, base }; |
| 1301 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1302 | kvm_x86_ops->set_gdt(vcpu, &dt); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) |
| 1306 | { |
| 1307 | struct descriptor_table dt = { limit, base }; |
| 1308 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1309 | kvm_x86_ops->set_idt(vcpu, &dt); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw, |
| 1313 | unsigned long *rflags) |
| 1314 | { |
| 1315 | lmsw(vcpu, msw); |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1316 | *rflags = kvm_x86_ops->get_rflags(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr) |
| 1320 | { |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1321 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1322 | switch (cr) { |
| 1323 | case 0: |
| 1324 | return vcpu->cr0; |
| 1325 | case 2: |
| 1326 | return vcpu->cr2; |
| 1327 | case 3: |
| 1328 | return vcpu->cr3; |
| 1329 | case 4: |
| 1330 | return vcpu->cr4; |
| 1331 | default: |
| 1332 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr); |
| 1333 | return 0; |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val, |
| 1338 | unsigned long *rflags) |
| 1339 | { |
| 1340 | switch (cr) { |
| 1341 | case 0: |
| 1342 | set_cr0(vcpu, mk_cr_64(vcpu->cr0, val)); |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1343 | *rflags = kvm_x86_ops->get_rflags(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1344 | break; |
| 1345 | case 2: |
| 1346 | vcpu->cr2 = val; |
| 1347 | break; |
| 1348 | case 3: |
| 1349 | set_cr3(vcpu, val); |
| 1350 | break; |
| 1351 | case 4: |
| 1352 | set_cr4(vcpu, mk_cr_64(vcpu->cr4, val)); |
| 1353 | break; |
| 1354 | default: |
| 1355 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr); |
| 1356 | } |
| 1357 | } |
| 1358 | |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1359 | int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 1360 | { |
| 1361 | u64 data; |
| 1362 | |
| 1363 | switch (msr) { |
| 1364 | case 0xc0010010: /* SYSCFG */ |
| 1365 | case 0xc0010015: /* HWCR */ |
| 1366 | case MSR_IA32_PLATFORM_ID: |
| 1367 | case MSR_IA32_P5_MC_ADDR: |
| 1368 | case MSR_IA32_P5_MC_TYPE: |
| 1369 | case MSR_IA32_MC0_CTL: |
| 1370 | case MSR_IA32_MCG_STATUS: |
| 1371 | case MSR_IA32_MCG_CAP: |
| 1372 | case MSR_IA32_MC0_MISC: |
| 1373 | case MSR_IA32_MC0_MISC+4: |
| 1374 | case MSR_IA32_MC0_MISC+8: |
| 1375 | case MSR_IA32_MC0_MISC+12: |
| 1376 | case MSR_IA32_MC0_MISC+16: |
| 1377 | case MSR_IA32_UCODE_REV: |
Avi Kivity | a8d13ea | 2006-12-29 16:49:51 -0800 | [diff] [blame] | 1378 | case MSR_IA32_PERF_STATUS: |
Matthew Gregan | 2dc7094 | 2007-05-06 10:59:46 +0300 | [diff] [blame] | 1379 | case MSR_IA32_EBL_CR_POWERON: |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1380 | /* MTRR registers */ |
| 1381 | case 0xfe: |
| 1382 | case 0x200 ... 0x2ff: |
| 1383 | data = 0; |
| 1384 | break; |
Avi Kivity | a8d13ea | 2006-12-29 16:49:51 -0800 | [diff] [blame] | 1385 | case 0xcd: /* fsb frequency */ |
| 1386 | data = 3; |
| 1387 | break; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1388 | case MSR_IA32_APICBASE: |
Eddie Dong | 7017fc3 | 2007-07-18 11:34:57 +0300 | [diff] [blame] | 1389 | data = kvm_get_apic_base(vcpu); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1390 | break; |
Avi Kivity | 6f00e68 | 2007-01-26 00:56:40 -0800 | [diff] [blame] | 1391 | case MSR_IA32_MISC_ENABLE: |
| 1392 | data = vcpu->ia32_misc_enable_msr; |
| 1393 | break; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1394 | #ifdef CONFIG_X86_64 |
| 1395 | case MSR_EFER: |
| 1396 | data = vcpu->shadow_efer; |
| 1397 | break; |
| 1398 | #endif |
| 1399 | default: |
Rusty Russell | f024247 | 2007-08-01 10:48:02 +1000 | [diff] [blame] | 1400 | pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1401 | return 1; |
| 1402 | } |
| 1403 | *pdata = data; |
| 1404 | return 0; |
| 1405 | } |
| 1406 | EXPORT_SYMBOL_GPL(kvm_get_msr_common); |
| 1407 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1408 | /* |
| 1409 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 1410 | * Returns 0 on success, non-0 otherwise. |
| 1411 | * Assumes vcpu_load() was already called. |
| 1412 | */ |
Avi Kivity | 35f3f28 | 2007-07-17 14:20:30 +0300 | [diff] [blame] | 1413 | int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1414 | { |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1415 | return kvm_x86_ops->get_msr(vcpu, msr_index, pdata); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1416 | } |
| 1417 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1418 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1419 | |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1420 | static void set_efer(struct kvm_vcpu *vcpu, u64 efer) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1421 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1422 | if (efer & EFER_RESERVED_BITS) { |
| 1423 | printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n", |
| 1424 | efer); |
| 1425 | inject_gp(vcpu); |
| 1426 | return; |
| 1427 | } |
| 1428 | |
| 1429 | if (is_paging(vcpu) |
| 1430 | && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) { |
| 1431 | printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n"); |
| 1432 | inject_gp(vcpu); |
| 1433 | return; |
| 1434 | } |
| 1435 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1436 | kvm_x86_ops->set_efer(vcpu, efer); |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 1437 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1438 | efer &= ~EFER_LMA; |
| 1439 | efer |= vcpu->shadow_efer & EFER_LMA; |
| 1440 | |
| 1441 | vcpu->shadow_efer = efer; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1442 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1443 | |
| 1444 | #endif |
| 1445 | |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1446 | int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 1447 | { |
| 1448 | switch (msr) { |
| 1449 | #ifdef CONFIG_X86_64 |
| 1450 | case MSR_EFER: |
| 1451 | set_efer(vcpu, data); |
| 1452 | break; |
| 1453 | #endif |
| 1454 | case MSR_IA32_MC0_STATUS: |
Rusty Russell | f024247 | 2007-08-01 10:48:02 +1000 | [diff] [blame] | 1455 | pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n", |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1456 | __FUNCTION__, data); |
| 1457 | break; |
Sergey Kiselev | 0e5bf0d | 2007-03-22 14:06:18 +0200 | [diff] [blame] | 1458 | case MSR_IA32_MCG_STATUS: |
Rusty Russell | f024247 | 2007-08-01 10:48:02 +1000 | [diff] [blame] | 1459 | pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n", |
Sergey Kiselev | 0e5bf0d | 2007-03-22 14:06:18 +0200 | [diff] [blame] | 1460 | __FUNCTION__, data); |
| 1461 | break; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1462 | case MSR_IA32_UCODE_REV: |
| 1463 | case MSR_IA32_UCODE_WRITE: |
| 1464 | case 0x200 ... 0x2ff: /* MTRRs */ |
| 1465 | break; |
| 1466 | case MSR_IA32_APICBASE: |
Eddie Dong | 7017fc3 | 2007-07-18 11:34:57 +0300 | [diff] [blame] | 1467 | kvm_set_apic_base(vcpu, data); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1468 | break; |
Avi Kivity | 6f00e68 | 2007-01-26 00:56:40 -0800 | [diff] [blame] | 1469 | case MSR_IA32_MISC_ENABLE: |
| 1470 | vcpu->ia32_misc_enable_msr = data; |
| 1471 | break; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1472 | default: |
Rusty Russell | f024247 | 2007-08-01 10:48:02 +1000 | [diff] [blame] | 1473 | pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1474 | return 1; |
| 1475 | } |
| 1476 | return 0; |
| 1477 | } |
| 1478 | EXPORT_SYMBOL_GPL(kvm_set_msr_common); |
| 1479 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1480 | /* |
| 1481 | * Writes msr value into into the appropriate "register". |
| 1482 | * Returns 0 on success, non-0 otherwise. |
| 1483 | * Assumes vcpu_load() was already called. |
| 1484 | */ |
Avi Kivity | 35f3f28 | 2007-07-17 14:20:30 +0300 | [diff] [blame] | 1485 | int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1486 | { |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1487 | return kvm_x86_ops->set_msr(vcpu, msr_index, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | void kvm_resched(struct kvm_vcpu *vcpu) |
| 1491 | { |
Yaozu Dong | 3fca036 | 2007-04-25 16:49:19 +0300 | [diff] [blame] | 1492 | if (!need_resched()) |
| 1493 | return; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1494 | cond_resched(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1495 | } |
| 1496 | EXPORT_SYMBOL_GPL(kvm_resched); |
| 1497 | |
Avi Kivity | 06465c5 | 2007-02-28 20:46:53 +0200 | [diff] [blame] | 1498 | void kvm_emulate_cpuid(struct kvm_vcpu *vcpu) |
| 1499 | { |
| 1500 | int i; |
| 1501 | u32 function; |
| 1502 | struct kvm_cpuid_entry *e, *best; |
| 1503 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1504 | kvm_x86_ops->cache_regs(vcpu); |
Avi Kivity | 06465c5 | 2007-02-28 20:46:53 +0200 | [diff] [blame] | 1505 | function = vcpu->regs[VCPU_REGS_RAX]; |
| 1506 | vcpu->regs[VCPU_REGS_RAX] = 0; |
| 1507 | vcpu->regs[VCPU_REGS_RBX] = 0; |
| 1508 | vcpu->regs[VCPU_REGS_RCX] = 0; |
| 1509 | vcpu->regs[VCPU_REGS_RDX] = 0; |
| 1510 | best = NULL; |
| 1511 | for (i = 0; i < vcpu->cpuid_nent; ++i) { |
| 1512 | e = &vcpu->cpuid_entries[i]; |
| 1513 | if (e->function == function) { |
| 1514 | best = e; |
| 1515 | break; |
| 1516 | } |
| 1517 | /* |
| 1518 | * Both basic or both extended? |
| 1519 | */ |
| 1520 | if (((e->function ^ function) & 0x80000000) == 0) |
| 1521 | if (!best || e->function > best->function) |
| 1522 | best = e; |
| 1523 | } |
| 1524 | if (best) { |
| 1525 | vcpu->regs[VCPU_REGS_RAX] = best->eax; |
| 1526 | vcpu->regs[VCPU_REGS_RBX] = best->ebx; |
| 1527 | vcpu->regs[VCPU_REGS_RCX] = best->ecx; |
| 1528 | vcpu->regs[VCPU_REGS_RDX] = best->edx; |
| 1529 | } |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1530 | kvm_x86_ops->decache_regs(vcpu); |
| 1531 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
Avi Kivity | 06465c5 | 2007-02-28 20:46:53 +0200 | [diff] [blame] | 1532 | } |
| 1533 | EXPORT_SYMBOL_GPL(kvm_emulate_cpuid); |
| 1534 | |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1535 | static int pio_copy_data(struct kvm_vcpu *vcpu) |
Avi Kivity | 46fc147 | 2007-02-22 19:39:30 +0200 | [diff] [blame] | 1536 | { |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1537 | void *p = vcpu->pio_data; |
| 1538 | void *q; |
| 1539 | unsigned bytes; |
| 1540 | int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1; |
| 1541 | |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1542 | q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE, |
| 1543 | PAGE_KERNEL); |
| 1544 | if (!q) { |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1545 | free_pio_guest_pages(vcpu); |
| 1546 | return -ENOMEM; |
| 1547 | } |
| 1548 | q += vcpu->pio.guest_page_offset; |
| 1549 | bytes = vcpu->pio.size * vcpu->pio.cur_count; |
| 1550 | if (vcpu->pio.in) |
| 1551 | memcpy(q, p, bytes); |
| 1552 | else |
| 1553 | memcpy(p, q, bytes); |
| 1554 | q -= vcpu->pio.guest_page_offset; |
| 1555 | vunmap(q); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1556 | free_pio_guest_pages(vcpu); |
| 1557 | return 0; |
| 1558 | } |
| 1559 | |
| 1560 | static int complete_pio(struct kvm_vcpu *vcpu) |
| 1561 | { |
| 1562 | struct kvm_pio_request *io = &vcpu->pio; |
Avi Kivity | 46fc147 | 2007-02-22 19:39:30 +0200 | [diff] [blame] | 1563 | long delta; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1564 | int r; |
Avi Kivity | 46fc147 | 2007-02-22 19:39:30 +0200 | [diff] [blame] | 1565 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1566 | kvm_x86_ops->cache_regs(vcpu); |
Avi Kivity | 46fc147 | 2007-02-22 19:39:30 +0200 | [diff] [blame] | 1567 | |
| 1568 | if (!io->string) { |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1569 | if (io->in) |
| 1570 | memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data, |
Avi Kivity | 46fc147 | 2007-02-22 19:39:30 +0200 | [diff] [blame] | 1571 | io->size); |
| 1572 | } else { |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1573 | if (io->in) { |
| 1574 | r = pio_copy_data(vcpu); |
| 1575 | if (r) { |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1576 | kvm_x86_ops->cache_regs(vcpu); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1577 | return r; |
| 1578 | } |
| 1579 | } |
| 1580 | |
Avi Kivity | 46fc147 | 2007-02-22 19:39:30 +0200 | [diff] [blame] | 1581 | delta = 1; |
| 1582 | if (io->rep) { |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1583 | delta *= io->cur_count; |
Avi Kivity | 46fc147 | 2007-02-22 19:39:30 +0200 | [diff] [blame] | 1584 | /* |
| 1585 | * The size of the register should really depend on |
| 1586 | * current address size. |
| 1587 | */ |
| 1588 | vcpu->regs[VCPU_REGS_RCX] -= delta; |
| 1589 | } |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1590 | if (io->down) |
Avi Kivity | 46fc147 | 2007-02-22 19:39:30 +0200 | [diff] [blame] | 1591 | delta = -delta; |
| 1592 | delta *= io->size; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1593 | if (io->in) |
Avi Kivity | 46fc147 | 2007-02-22 19:39:30 +0200 | [diff] [blame] | 1594 | vcpu->regs[VCPU_REGS_RDI] += delta; |
| 1595 | else |
| 1596 | vcpu->regs[VCPU_REGS_RSI] += delta; |
| 1597 | } |
| 1598 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1599 | kvm_x86_ops->decache_regs(vcpu); |
Avi Kivity | 46fc147 | 2007-02-22 19:39:30 +0200 | [diff] [blame] | 1600 | |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1601 | io->count -= io->cur_count; |
| 1602 | io->cur_count = 0; |
| 1603 | |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1604 | return 0; |
Avi Kivity | 46fc147 | 2007-02-22 19:39:30 +0200 | [diff] [blame] | 1605 | } |
| 1606 | |
Eddie Dong | 65619eb | 2007-07-17 11:52:33 +0300 | [diff] [blame] | 1607 | static void kernel_pio(struct kvm_io_device *pio_dev, |
| 1608 | struct kvm_vcpu *vcpu, |
| 1609 | void *pd) |
Eddie Dong | 7490634 | 2007-06-19 18:05:03 +0300 | [diff] [blame] | 1610 | { |
| 1611 | /* TODO: String I/O for in kernel device */ |
| 1612 | |
Eddie Dong | 9cf9882 | 2007-07-22 10:36:31 +0300 | [diff] [blame] | 1613 | mutex_lock(&vcpu->kvm->lock); |
Eddie Dong | 7490634 | 2007-06-19 18:05:03 +0300 | [diff] [blame] | 1614 | if (vcpu->pio.in) |
| 1615 | kvm_iodevice_read(pio_dev, vcpu->pio.port, |
| 1616 | vcpu->pio.size, |
Eddie Dong | 65619eb | 2007-07-17 11:52:33 +0300 | [diff] [blame] | 1617 | pd); |
Eddie Dong | 7490634 | 2007-06-19 18:05:03 +0300 | [diff] [blame] | 1618 | else |
| 1619 | kvm_iodevice_write(pio_dev, vcpu->pio.port, |
| 1620 | vcpu->pio.size, |
Eddie Dong | 65619eb | 2007-07-17 11:52:33 +0300 | [diff] [blame] | 1621 | pd); |
Eddie Dong | 9cf9882 | 2007-07-22 10:36:31 +0300 | [diff] [blame] | 1622 | mutex_unlock(&vcpu->kvm->lock); |
Eddie Dong | 65619eb | 2007-07-17 11:52:33 +0300 | [diff] [blame] | 1623 | } |
| 1624 | |
| 1625 | static void pio_string_write(struct kvm_io_device *pio_dev, |
| 1626 | struct kvm_vcpu *vcpu) |
| 1627 | { |
| 1628 | struct kvm_pio_request *io = &vcpu->pio; |
| 1629 | void *pd = vcpu->pio_data; |
| 1630 | int i; |
| 1631 | |
Eddie Dong | 9cf9882 | 2007-07-22 10:36:31 +0300 | [diff] [blame] | 1632 | mutex_lock(&vcpu->kvm->lock); |
Eddie Dong | 65619eb | 2007-07-17 11:52:33 +0300 | [diff] [blame] | 1633 | for (i = 0; i < io->cur_count; i++) { |
| 1634 | kvm_iodevice_write(pio_dev, io->port, |
| 1635 | io->size, |
| 1636 | pd); |
| 1637 | pd += io->size; |
| 1638 | } |
Eddie Dong | 9cf9882 | 2007-07-22 10:36:31 +0300 | [diff] [blame] | 1639 | mutex_unlock(&vcpu->kvm->lock); |
Eddie Dong | 7490634 | 2007-06-19 18:05:03 +0300 | [diff] [blame] | 1640 | } |
| 1641 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1642 | int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in, |
Laurent Vivier | 3090dd7 | 2007-08-05 10:43:32 +0300 | [diff] [blame] | 1643 | int size, unsigned port) |
| 1644 | { |
| 1645 | struct kvm_io_device *pio_dev; |
| 1646 | |
| 1647 | vcpu->run->exit_reason = KVM_EXIT_IO; |
| 1648 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; |
| 1649 | vcpu->run->io.size = vcpu->pio.size = size; |
| 1650 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; |
| 1651 | vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1; |
| 1652 | vcpu->run->io.port = vcpu->pio.port = port; |
| 1653 | vcpu->pio.in = in; |
| 1654 | vcpu->pio.string = 0; |
| 1655 | vcpu->pio.down = 0; |
| 1656 | vcpu->pio.guest_page_offset = 0; |
| 1657 | vcpu->pio.rep = 0; |
| 1658 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1659 | kvm_x86_ops->cache_regs(vcpu); |
Laurent Vivier | 3090dd7 | 2007-08-05 10:43:32 +0300 | [diff] [blame] | 1660 | memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4); |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1661 | kvm_x86_ops->decache_regs(vcpu); |
Laurent Vivier | 3090dd7 | 2007-08-05 10:43:32 +0300 | [diff] [blame] | 1662 | |
Avi Kivity | 0967b7b | 2007-09-15 17:34:36 +0300 | [diff] [blame] | 1663 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
| 1664 | |
Laurent Vivier | 3090dd7 | 2007-08-05 10:43:32 +0300 | [diff] [blame] | 1665 | pio_dev = vcpu_find_pio_dev(vcpu, port); |
| 1666 | if (pio_dev) { |
| 1667 | kernel_pio(pio_dev, vcpu, vcpu->pio_data); |
| 1668 | complete_pio(vcpu); |
| 1669 | return 1; |
| 1670 | } |
| 1671 | return 0; |
| 1672 | } |
| 1673 | EXPORT_SYMBOL_GPL(kvm_emulate_pio); |
| 1674 | |
| 1675 | int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in, |
| 1676 | int size, unsigned long count, int down, |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1677 | gva_t address, int rep, unsigned port) |
| 1678 | { |
| 1679 | unsigned now, in_page; |
Eddie Dong | 65619eb | 2007-07-17 11:52:33 +0300 | [diff] [blame] | 1680 | int i, ret = 0; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1681 | int nr_pages = 1; |
| 1682 | struct page *page; |
Eddie Dong | 7490634 | 2007-06-19 18:05:03 +0300 | [diff] [blame] | 1683 | struct kvm_io_device *pio_dev; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1684 | |
| 1685 | vcpu->run->exit_reason = KVM_EXIT_IO; |
| 1686 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; |
Laurent Vivier | 3090dd7 | 2007-08-05 10:43:32 +0300 | [diff] [blame] | 1687 | vcpu->run->io.size = vcpu->pio.size = size; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1688 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; |
Laurent Vivier | 3090dd7 | 2007-08-05 10:43:32 +0300 | [diff] [blame] | 1689 | vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count; |
| 1690 | vcpu->run->io.port = vcpu->pio.port = port; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1691 | vcpu->pio.in = in; |
Laurent Vivier | 3090dd7 | 2007-08-05 10:43:32 +0300 | [diff] [blame] | 1692 | vcpu->pio.string = 1; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1693 | vcpu->pio.down = down; |
| 1694 | vcpu->pio.guest_page_offset = offset_in_page(address); |
| 1695 | vcpu->pio.rep = rep; |
| 1696 | |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1697 | if (!count) { |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1698 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1699 | return 1; |
| 1700 | } |
| 1701 | |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1702 | if (!down) |
| 1703 | in_page = PAGE_SIZE - offset_in_page(address); |
| 1704 | else |
| 1705 | in_page = offset_in_page(address) + size; |
| 1706 | now = min(count, (unsigned long)in_page / size); |
| 1707 | if (!now) { |
| 1708 | /* |
| 1709 | * String I/O straddles page boundary. Pin two guest pages |
| 1710 | * so that we satisfy atomicity constraints. Do just one |
| 1711 | * transaction to avoid complexity. |
| 1712 | */ |
| 1713 | nr_pages = 2; |
| 1714 | now = 1; |
| 1715 | } |
| 1716 | if (down) { |
| 1717 | /* |
| 1718 | * String I/O in reverse. Yuck. Kill the guest, fix later. |
| 1719 | */ |
Rusty Russell | f024247 | 2007-08-01 10:48:02 +1000 | [diff] [blame] | 1720 | pr_unimpl(vcpu, "guest string pio down\n"); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1721 | inject_gp(vcpu); |
| 1722 | return 1; |
| 1723 | } |
| 1724 | vcpu->run->io.count = now; |
| 1725 | vcpu->pio.cur_count = now; |
| 1726 | |
Avi Kivity | 0967b7b | 2007-09-15 17:34:36 +0300 | [diff] [blame] | 1727 | if (vcpu->pio.cur_count == vcpu->pio.count) |
| 1728 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
| 1729 | |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1730 | for (i = 0; i < nr_pages; ++i) { |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 1731 | mutex_lock(&vcpu->kvm->lock); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1732 | page = gva_to_page(vcpu, address + i * PAGE_SIZE); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1733 | vcpu->pio.guest_pages[i] = page; |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 1734 | mutex_unlock(&vcpu->kvm->lock); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1735 | if (!page) { |
| 1736 | inject_gp(vcpu); |
| 1737 | free_pio_guest_pages(vcpu); |
| 1738 | return 1; |
| 1739 | } |
| 1740 | } |
| 1741 | |
Laurent Vivier | 3090dd7 | 2007-08-05 10:43:32 +0300 | [diff] [blame] | 1742 | pio_dev = vcpu_find_pio_dev(vcpu, port); |
Eddie Dong | 65619eb | 2007-07-17 11:52:33 +0300 | [diff] [blame] | 1743 | if (!vcpu->pio.in) { |
| 1744 | /* string PIO write */ |
| 1745 | ret = pio_copy_data(vcpu); |
| 1746 | if (ret >= 0 && pio_dev) { |
| 1747 | pio_string_write(pio_dev, vcpu); |
| 1748 | complete_pio(vcpu); |
| 1749 | if (vcpu->pio.count == 0) |
| 1750 | ret = 1; |
| 1751 | } |
| 1752 | } else if (pio_dev) |
Rusty Russell | f024247 | 2007-08-01 10:48:02 +1000 | [diff] [blame] | 1753 | pr_unimpl(vcpu, "no string pio read support yet, " |
Eddie Dong | 65619eb | 2007-07-17 11:52:33 +0300 | [diff] [blame] | 1754 | "port %x size %d count %ld\n", |
| 1755 | port, size, count); |
| 1756 | |
| 1757 | return ret; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1758 | } |
Laurent Vivier | 3090dd7 | 2007-08-05 10:43:32 +0300 | [diff] [blame] | 1759 | EXPORT_SYMBOL_GPL(kvm_emulate_pio_string); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1760 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1761 | /* |
| 1762 | * Check if userspace requested an interrupt window, and that the |
| 1763 | * interrupt window is open. |
| 1764 | * |
| 1765 | * No need to exit to userspace if we already have an interrupt queued. |
| 1766 | */ |
| 1767 | static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu, |
| 1768 | struct kvm_run *kvm_run) |
| 1769 | { |
| 1770 | return (!vcpu->irq_summary && |
| 1771 | kvm_run->request_interrupt_window && |
| 1772 | vcpu->interrupt_window_open && |
| 1773 | (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF)); |
| 1774 | } |
| 1775 | |
| 1776 | static void post_kvm_run_save(struct kvm_vcpu *vcpu, |
| 1777 | struct kvm_run *kvm_run) |
| 1778 | { |
| 1779 | kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0; |
| 1780 | kvm_run->cr8 = get_cr8(vcpu); |
| 1781 | kvm_run->apic_base = kvm_get_apic_base(vcpu); |
| 1782 | if (irqchip_in_kernel(vcpu->kvm)) |
| 1783 | kvm_run->ready_for_interrupt_injection = 1; |
| 1784 | else |
| 1785 | kvm_run->ready_for_interrupt_injection = |
| 1786 | (vcpu->interrupt_window_open && |
| 1787 | vcpu->irq_summary == 0); |
| 1788 | } |
| 1789 | |
| 1790 | static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 1791 | { |
| 1792 | int r; |
| 1793 | |
| 1794 | if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) { |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1795 | pr_debug("vcpu %d received sipi with vector # %x\n", |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1796 | vcpu->vcpu_id, vcpu->sipi_vector); |
| 1797 | kvm_lapic_reset(vcpu); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 1798 | r = kvm_x86_ops->vcpu_reset(vcpu); |
| 1799 | if (r) |
| 1800 | return r; |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1801 | vcpu->mp_state = VCPU_MP_STATE_RUNNABLE; |
| 1802 | } |
| 1803 | |
| 1804 | preempted: |
| 1805 | if (vcpu->guest_debug.enabled) |
| 1806 | kvm_x86_ops->guest_debug_pre(vcpu); |
| 1807 | |
| 1808 | again: |
| 1809 | r = kvm_mmu_reload(vcpu); |
| 1810 | if (unlikely(r)) |
| 1811 | goto out; |
| 1812 | |
Avi Kivity | ab6ef34 | 2007-10-16 16:23:22 +0200 | [diff] [blame] | 1813 | kvm_inject_pending_timer_irqs(vcpu); |
| 1814 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1815 | preempt_disable(); |
| 1816 | |
| 1817 | kvm_x86_ops->prepare_guest_switch(vcpu); |
| 1818 | kvm_load_guest_fpu(vcpu); |
| 1819 | |
| 1820 | local_irq_disable(); |
| 1821 | |
| 1822 | if (signal_pending(current)) { |
| 1823 | local_irq_enable(); |
| 1824 | preempt_enable(); |
| 1825 | r = -EINTR; |
| 1826 | kvm_run->exit_reason = KVM_EXIT_INTR; |
| 1827 | ++vcpu->stat.signal_exits; |
| 1828 | goto out; |
| 1829 | } |
| 1830 | |
| 1831 | if (irqchip_in_kernel(vcpu->kvm)) |
| 1832 | kvm_x86_ops->inject_pending_irq(vcpu); |
| 1833 | else if (!vcpu->mmio_read_completed) |
| 1834 | kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run); |
| 1835 | |
| 1836 | vcpu->guest_mode = 1; |
Laurent Vivier | d172fcd | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 1837 | kvm_guest_enter(); |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1838 | |
| 1839 | if (vcpu->requests) |
Avi Kivity | 3176bc3 | 2007-10-16 17:22:08 +0200 | [diff] [blame] | 1840 | if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests)) |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1841 | kvm_x86_ops->tlb_flush(vcpu); |
| 1842 | |
| 1843 | kvm_x86_ops->run(vcpu, kvm_run); |
| 1844 | |
| 1845 | vcpu->guest_mode = 0; |
| 1846 | local_irq_enable(); |
| 1847 | |
| 1848 | ++vcpu->stat.exits; |
| 1849 | |
Laurent Vivier | 0552f73 | 2007-10-18 15:19:01 +0200 | [diff] [blame] | 1850 | /* |
| 1851 | * We must have an instruction between local_irq_enable() and |
| 1852 | * kvm_guest_exit(), so the timer interrupt isn't delayed by |
| 1853 | * the interrupt shadow. The stat.exits increment will do nicely. |
| 1854 | * But we need to prevent reordering, hence this barrier(): |
| 1855 | */ |
| 1856 | barrier(); |
| 1857 | |
| 1858 | kvm_guest_exit(); |
| 1859 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1860 | preempt_enable(); |
| 1861 | |
| 1862 | /* |
| 1863 | * Profile KVM exit RIPs: |
| 1864 | */ |
| 1865 | if (unlikely(prof_on == KVM_PROFILING)) { |
| 1866 | kvm_x86_ops->cache_regs(vcpu); |
| 1867 | profile_hit(KVM_PROFILING, (void *)vcpu->rip); |
| 1868 | } |
| 1869 | |
| 1870 | r = kvm_x86_ops->handle_exit(kvm_run, vcpu); |
| 1871 | |
| 1872 | if (r > 0) { |
| 1873 | if (dm_request_for_irq_injection(vcpu, kvm_run)) { |
| 1874 | r = -EINTR; |
| 1875 | kvm_run->exit_reason = KVM_EXIT_INTR; |
| 1876 | ++vcpu->stat.request_irq_exits; |
| 1877 | goto out; |
| 1878 | } |
| 1879 | if (!need_resched()) { |
| 1880 | ++vcpu->stat.light_exits; |
| 1881 | goto again; |
| 1882 | } |
| 1883 | } |
| 1884 | |
| 1885 | out: |
| 1886 | if (r > 0) { |
| 1887 | kvm_resched(vcpu); |
| 1888 | goto preempted; |
| 1889 | } |
| 1890 | |
| 1891 | post_kvm_run_save(vcpu, kvm_run); |
| 1892 | |
| 1893 | return r; |
| 1894 | } |
| 1895 | |
| 1896 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1897 | static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1898 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1899 | int r; |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 1900 | sigset_t sigsaved; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1901 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1902 | vcpu_load(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1903 | |
He, Qing | c5ec153 | 2007-09-03 17:07:41 +0300 | [diff] [blame] | 1904 | if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) { |
| 1905 | kvm_vcpu_block(vcpu); |
| 1906 | vcpu_put(vcpu); |
| 1907 | return -EAGAIN; |
| 1908 | } |
| 1909 | |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 1910 | if (vcpu->sigset_active) |
| 1911 | sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); |
| 1912 | |
Dor Laor | 5481034 | 2007-02-12 00:54:39 -0800 | [diff] [blame] | 1913 | /* re-sync apic's tpr */ |
He, Qing | 5cd4f6f | 2007-08-30 17:04:26 +0800 | [diff] [blame] | 1914 | if (!irqchip_in_kernel(vcpu->kvm)) |
| 1915 | set_cr8(vcpu, kvm_run->cr8); |
Dor Laor | 5481034 | 2007-02-12 00:54:39 -0800 | [diff] [blame] | 1916 | |
Avi Kivity | 02c8320 | 2007-04-29 15:02:17 +0300 | [diff] [blame] | 1917 | if (vcpu->pio.cur_count) { |
| 1918 | r = complete_pio(vcpu); |
| 1919 | if (r) |
| 1920 | goto out; |
| 1921 | } |
Zhang Xiantao | 34c16ee | 2007-10-20 15:34:38 +0800 | [diff] [blame] | 1922 | #if CONFIG_HAS_IOMEM |
Avi Kivity | 02c8320 | 2007-04-29 15:02:17 +0300 | [diff] [blame] | 1923 | if (vcpu->mmio_needed) { |
| 1924 | memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8); |
| 1925 | vcpu->mmio_read_completed = 1; |
| 1926 | vcpu->mmio_needed = 0; |
| 1927 | r = emulate_instruction(vcpu, kvm_run, |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 1928 | vcpu->mmio_fault_cr2, 0, 1); |
Avi Kivity | 02c8320 | 2007-04-29 15:02:17 +0300 | [diff] [blame] | 1929 | if (r == EMULATE_DO_MMIO) { |
| 1930 | /* |
| 1931 | * Read-modify-write. Back to userspace. |
| 1932 | */ |
Avi Kivity | 02c8320 | 2007-04-29 15:02:17 +0300 | [diff] [blame] | 1933 | r = 0; |
| 1934 | goto out; |
Avi Kivity | 46fc147 | 2007-02-22 19:39:30 +0200 | [diff] [blame] | 1935 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1936 | } |
Zhang Xiantao | 34c16ee | 2007-10-20 15:34:38 +0800 | [diff] [blame] | 1937 | #endif |
Avi Kivity | 8eb7d33 | 2007-03-04 14:17:08 +0200 | [diff] [blame] | 1938 | if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) { |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1939 | kvm_x86_ops->cache_regs(vcpu); |
Avi Kivity | b4e63f5 | 2007-03-04 13:59:30 +0200 | [diff] [blame] | 1940 | vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret; |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1941 | kvm_x86_ops->decache_regs(vcpu); |
Avi Kivity | b4e63f5 | 2007-03-04 13:59:30 +0200 | [diff] [blame] | 1942 | } |
| 1943 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1944 | r = __vcpu_run(vcpu, kvm_run); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1945 | |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1946 | out: |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 1947 | if (vcpu->sigset_active) |
| 1948 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); |
| 1949 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1950 | vcpu_put(vcpu); |
| 1951 | return r; |
| 1952 | } |
| 1953 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1954 | static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, |
| 1955 | struct kvm_regs *regs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1956 | { |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1957 | vcpu_load(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1958 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1959 | kvm_x86_ops->cache_regs(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1960 | |
| 1961 | regs->rax = vcpu->regs[VCPU_REGS_RAX]; |
| 1962 | regs->rbx = vcpu->regs[VCPU_REGS_RBX]; |
| 1963 | regs->rcx = vcpu->regs[VCPU_REGS_RCX]; |
| 1964 | regs->rdx = vcpu->regs[VCPU_REGS_RDX]; |
| 1965 | regs->rsi = vcpu->regs[VCPU_REGS_RSI]; |
| 1966 | regs->rdi = vcpu->regs[VCPU_REGS_RDI]; |
| 1967 | regs->rsp = vcpu->regs[VCPU_REGS_RSP]; |
| 1968 | regs->rbp = vcpu->regs[VCPU_REGS_RBP]; |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1969 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1970 | regs->r8 = vcpu->regs[VCPU_REGS_R8]; |
| 1971 | regs->r9 = vcpu->regs[VCPU_REGS_R9]; |
| 1972 | regs->r10 = vcpu->regs[VCPU_REGS_R10]; |
| 1973 | regs->r11 = vcpu->regs[VCPU_REGS_R11]; |
| 1974 | regs->r12 = vcpu->regs[VCPU_REGS_R12]; |
| 1975 | regs->r13 = vcpu->regs[VCPU_REGS_R13]; |
| 1976 | regs->r14 = vcpu->regs[VCPU_REGS_R14]; |
| 1977 | regs->r15 = vcpu->regs[VCPU_REGS_R15]; |
| 1978 | #endif |
| 1979 | |
| 1980 | regs->rip = vcpu->rip; |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1981 | regs->rflags = kvm_x86_ops->get_rflags(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1982 | |
| 1983 | /* |
| 1984 | * Don't leak debug flags in case they were set for guest debugging |
| 1985 | */ |
| 1986 | if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep) |
| 1987 | regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF); |
| 1988 | |
| 1989 | vcpu_put(vcpu); |
| 1990 | |
| 1991 | return 0; |
| 1992 | } |
| 1993 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1994 | static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, |
| 1995 | struct kvm_regs *regs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1996 | { |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1997 | vcpu_load(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1998 | |
| 1999 | vcpu->regs[VCPU_REGS_RAX] = regs->rax; |
| 2000 | vcpu->regs[VCPU_REGS_RBX] = regs->rbx; |
| 2001 | vcpu->regs[VCPU_REGS_RCX] = regs->rcx; |
| 2002 | vcpu->regs[VCPU_REGS_RDX] = regs->rdx; |
| 2003 | vcpu->regs[VCPU_REGS_RSI] = regs->rsi; |
| 2004 | vcpu->regs[VCPU_REGS_RDI] = regs->rdi; |
| 2005 | vcpu->regs[VCPU_REGS_RSP] = regs->rsp; |
| 2006 | vcpu->regs[VCPU_REGS_RBP] = regs->rbp; |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2007 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2008 | vcpu->regs[VCPU_REGS_R8] = regs->r8; |
| 2009 | vcpu->regs[VCPU_REGS_R9] = regs->r9; |
| 2010 | vcpu->regs[VCPU_REGS_R10] = regs->r10; |
| 2011 | vcpu->regs[VCPU_REGS_R11] = regs->r11; |
| 2012 | vcpu->regs[VCPU_REGS_R12] = regs->r12; |
| 2013 | vcpu->regs[VCPU_REGS_R13] = regs->r13; |
| 2014 | vcpu->regs[VCPU_REGS_R14] = regs->r14; |
| 2015 | vcpu->regs[VCPU_REGS_R15] = regs->r15; |
| 2016 | #endif |
| 2017 | |
| 2018 | vcpu->rip = regs->rip; |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2019 | kvm_x86_ops->set_rflags(vcpu, regs->rflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2020 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2021 | kvm_x86_ops->decache_regs(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2022 | |
| 2023 | vcpu_put(vcpu); |
| 2024 | |
| 2025 | return 0; |
| 2026 | } |
| 2027 | |
| 2028 | static void get_segment(struct kvm_vcpu *vcpu, |
| 2029 | struct kvm_segment *var, int seg) |
| 2030 | { |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2031 | return kvm_x86_ops->get_segment(vcpu, var, seg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2032 | } |
| 2033 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2034 | static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
| 2035 | struct kvm_sregs *sregs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2036 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2037 | struct descriptor_table dt; |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 2038 | int pending_vec; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2039 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2040 | vcpu_load(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2041 | |
| 2042 | get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
| 2043 | get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); |
| 2044 | get_segment(vcpu, &sregs->es, VCPU_SREG_ES); |
| 2045 | get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); |
| 2046 | get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); |
| 2047 | get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); |
| 2048 | |
| 2049 | get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
| 2050 | get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); |
| 2051 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2052 | kvm_x86_ops->get_idt(vcpu, &dt); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2053 | sregs->idt.limit = dt.limit; |
| 2054 | sregs->idt.base = dt.base; |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2055 | kvm_x86_ops->get_gdt(vcpu, &dt); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2056 | sregs->gdt.limit = dt.limit; |
| 2057 | sregs->gdt.base = dt.base; |
| 2058 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2059 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2060 | sregs->cr0 = vcpu->cr0; |
| 2061 | sregs->cr2 = vcpu->cr2; |
| 2062 | sregs->cr3 = vcpu->cr3; |
| 2063 | sregs->cr4 = vcpu->cr4; |
Eddie Dong | 7017fc3 | 2007-07-18 11:34:57 +0300 | [diff] [blame] | 2064 | sregs->cr8 = get_cr8(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2065 | sregs->efer = vcpu->shadow_efer; |
Eddie Dong | 7017fc3 | 2007-07-18 11:34:57 +0300 | [diff] [blame] | 2066 | sregs->apic_base = kvm_get_apic_base(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2067 | |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 2068 | if (irqchip_in_kernel(vcpu->kvm)) { |
He, Qing | c52fb35 | 2007-08-02 14:03:07 +0300 | [diff] [blame] | 2069 | memset(sregs->interrupt_bitmap, 0, |
| 2070 | sizeof sregs->interrupt_bitmap); |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2071 | pending_vec = kvm_x86_ops->get_irq(vcpu); |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 2072 | if (pending_vec >= 0) |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2073 | set_bit(pending_vec, |
| 2074 | (unsigned long *)sregs->interrupt_bitmap); |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 2075 | } else |
He, Qing | c52fb35 | 2007-08-02 14:03:07 +0300 | [diff] [blame] | 2076 | memcpy(sregs->interrupt_bitmap, vcpu->irq_pending, |
| 2077 | sizeof sregs->interrupt_bitmap); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2078 | |
| 2079 | vcpu_put(vcpu); |
| 2080 | |
| 2081 | return 0; |
| 2082 | } |
| 2083 | |
| 2084 | static void set_segment(struct kvm_vcpu *vcpu, |
| 2085 | struct kvm_segment *var, int seg) |
| 2086 | { |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2087 | return kvm_x86_ops->set_segment(vcpu, var, seg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2088 | } |
| 2089 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2090 | static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, |
| 2091 | struct kvm_sregs *sregs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2092 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2093 | int mmu_reset_needed = 0; |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 2094 | int i, pending_vec, max_bits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2095 | struct descriptor_table dt; |
| 2096 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2097 | vcpu_load(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2098 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2099 | dt.limit = sregs->idt.limit; |
| 2100 | dt.base = sregs->idt.base; |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2101 | kvm_x86_ops->set_idt(vcpu, &dt); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2102 | dt.limit = sregs->gdt.limit; |
| 2103 | dt.base = sregs->gdt.base; |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2104 | kvm_x86_ops->set_gdt(vcpu, &dt); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2105 | |
| 2106 | vcpu->cr2 = sregs->cr2; |
| 2107 | mmu_reset_needed |= vcpu->cr3 != sregs->cr3; |
| 2108 | vcpu->cr3 = sregs->cr3; |
| 2109 | |
Eddie Dong | 7017fc3 | 2007-07-18 11:34:57 +0300 | [diff] [blame] | 2110 | set_cr8(vcpu, sregs->cr8); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2111 | |
| 2112 | mmu_reset_needed |= vcpu->shadow_efer != sregs->efer; |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2113 | #ifdef CONFIG_X86_64 |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2114 | kvm_x86_ops->set_efer(vcpu, sregs->efer); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2115 | #endif |
Eddie Dong | 7017fc3 | 2007-07-18 11:34:57 +0300 | [diff] [blame] | 2116 | kvm_set_apic_base(vcpu, sregs->apic_base); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2117 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2118 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 2119 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2120 | mmu_reset_needed |= vcpu->cr0 != sregs->cr0; |
Rusty Russell | 81f50e3 | 2007-09-06 01:20:38 +1000 | [diff] [blame] | 2121 | vcpu->cr0 = sregs->cr0; |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2122 | kvm_x86_ops->set_cr0(vcpu, sregs->cr0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2123 | |
| 2124 | mmu_reset_needed |= vcpu->cr4 != sregs->cr4; |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2125 | kvm_x86_ops->set_cr4(vcpu, sregs->cr4); |
Avi Kivity | 1b0973b | 2007-01-05 16:36:41 -0800 | [diff] [blame] | 2126 | if (!is_long_mode(vcpu) && is_pae(vcpu)) |
| 2127 | load_pdptrs(vcpu, vcpu->cr3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2128 | |
| 2129 | if (mmu_reset_needed) |
| 2130 | kvm_mmu_reset_context(vcpu); |
| 2131 | |
He, Qing | c52fb35 | 2007-08-02 14:03:07 +0300 | [diff] [blame] | 2132 | if (!irqchip_in_kernel(vcpu->kvm)) { |
| 2133 | memcpy(vcpu->irq_pending, sregs->interrupt_bitmap, |
| 2134 | sizeof vcpu->irq_pending); |
| 2135 | vcpu->irq_summary = 0; |
| 2136 | for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i) |
| 2137 | if (vcpu->irq_pending[i]) |
| 2138 | __set_bit(i, &vcpu->irq_summary); |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 2139 | } else { |
| 2140 | max_bits = (sizeof sregs->interrupt_bitmap) << 3; |
| 2141 | pending_vec = find_first_bit( |
| 2142 | (const unsigned long *)sregs->interrupt_bitmap, |
| 2143 | max_bits); |
| 2144 | /* Only pending external irq is handled here */ |
| 2145 | if (pending_vec < max_bits) { |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2146 | kvm_x86_ops->set_irq(vcpu, pending_vec); |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2147 | pr_debug("Set back pending irq %d\n", |
| 2148 | pending_vec); |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 2149 | } |
He, Qing | c52fb35 | 2007-08-02 14:03:07 +0300 | [diff] [blame] | 2150 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2151 | |
Avi Kivity | 024aa1c | 2007-03-21 13:44:58 +0200 | [diff] [blame] | 2152 | set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
| 2153 | set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); |
| 2154 | set_segment(vcpu, &sregs->es, VCPU_SREG_ES); |
| 2155 | set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); |
| 2156 | set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); |
| 2157 | set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); |
| 2158 | |
| 2159 | set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
| 2160 | set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); |
| 2161 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2162 | vcpu_put(vcpu); |
| 2163 | |
| 2164 | return 0; |
| 2165 | } |
| 2166 | |
Rusty Russell | 1747fb7 | 2007-09-06 01:21:32 +1000 | [diff] [blame] | 2167 | void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 2168 | { |
| 2169 | struct kvm_segment cs; |
| 2170 | |
| 2171 | get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 2172 | *db = cs.db; |
| 2173 | *l = cs.l; |
| 2174 | } |
| 2175 | EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits); |
| 2176 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2177 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2178 | * Translate a guest virtual address to a guest physical address. |
| 2179 | */ |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2180 | static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
| 2181 | struct kvm_translation *tr) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2182 | { |
| 2183 | unsigned long vaddr = tr->linear_address; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2184 | gpa_t gpa; |
| 2185 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2186 | vcpu_load(vcpu); |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 2187 | mutex_lock(&vcpu->kvm->lock); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2188 | gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr); |
| 2189 | tr->physical_address = gpa; |
| 2190 | tr->valid = gpa != UNMAPPED_GVA; |
| 2191 | tr->writeable = 1; |
| 2192 | tr->usermode = 0; |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 2193 | mutex_unlock(&vcpu->kvm->lock); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2194 | vcpu_put(vcpu); |
| 2195 | |
| 2196 | return 0; |
| 2197 | } |
| 2198 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2199 | static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, |
| 2200 | struct kvm_interrupt *irq) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2201 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2202 | if (irq->irq < 0 || irq->irq >= 256) |
| 2203 | return -EINVAL; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 2204 | if (irqchip_in_kernel(vcpu->kvm)) |
| 2205 | return -ENXIO; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2206 | vcpu_load(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2207 | |
| 2208 | set_bit(irq->irq, vcpu->irq_pending); |
| 2209 | set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary); |
| 2210 | |
| 2211 | vcpu_put(vcpu); |
| 2212 | |
| 2213 | return 0; |
| 2214 | } |
| 2215 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2216 | static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu, |
| 2217 | struct kvm_debug_guest *dbg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2218 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2219 | int r; |
| 2220 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2221 | vcpu_load(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2222 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2223 | r = kvm_x86_ops->set_guest_debug(vcpu, dbg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2224 | |
| 2225 | vcpu_put(vcpu); |
| 2226 | |
| 2227 | return r; |
| 2228 | } |
| 2229 | |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 2230 | static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma, |
| 2231 | unsigned long address, |
| 2232 | int *type) |
| 2233 | { |
| 2234 | struct kvm_vcpu *vcpu = vma->vm_file->private_data; |
| 2235 | unsigned long pgoff; |
| 2236 | struct page *page; |
| 2237 | |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 2238 | pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 2239 | if (pgoff == 0) |
| 2240 | page = virt_to_page(vcpu->run); |
| 2241 | else if (pgoff == KVM_PIO_PAGE_OFFSET) |
| 2242 | page = virt_to_page(vcpu->pio_data); |
| 2243 | else |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 2244 | return NOPAGE_SIGBUS; |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 2245 | get_page(page); |
Nguyen Anh Quynh | cd0d913 | 2007-07-11 14:30:54 +0300 | [diff] [blame] | 2246 | if (type != NULL) |
| 2247 | *type = VM_FAULT_MINOR; |
| 2248 | |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 2249 | return page; |
| 2250 | } |
| 2251 | |
| 2252 | static struct vm_operations_struct kvm_vcpu_vm_ops = { |
| 2253 | .nopage = kvm_vcpu_nopage, |
| 2254 | }; |
| 2255 | |
| 2256 | static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma) |
| 2257 | { |
| 2258 | vma->vm_ops = &kvm_vcpu_vm_ops; |
| 2259 | return 0; |
| 2260 | } |
| 2261 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2262 | static int kvm_vcpu_release(struct inode *inode, struct file *filp) |
| 2263 | { |
| 2264 | struct kvm_vcpu *vcpu = filp->private_data; |
| 2265 | |
| 2266 | fput(vcpu->kvm->filp); |
| 2267 | return 0; |
| 2268 | } |
| 2269 | |
| 2270 | static struct file_operations kvm_vcpu_fops = { |
| 2271 | .release = kvm_vcpu_release, |
| 2272 | .unlocked_ioctl = kvm_vcpu_ioctl, |
| 2273 | .compat_ioctl = kvm_vcpu_ioctl, |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 2274 | .mmap = kvm_vcpu_mmap, |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2275 | }; |
| 2276 | |
| 2277 | /* |
| 2278 | * Allocates an inode for the vcpu. |
| 2279 | */ |
| 2280 | static int create_vcpu_fd(struct kvm_vcpu *vcpu) |
| 2281 | { |
| 2282 | int fd, r; |
| 2283 | struct inode *inode; |
| 2284 | struct file *file; |
| 2285 | |
Avi Kivity | d6d2816 | 2007-06-28 08:38:16 -0400 | [diff] [blame] | 2286 | r = anon_inode_getfd(&fd, &inode, &file, |
| 2287 | "kvm-vcpu", &kvm_vcpu_fops, vcpu); |
| 2288 | if (r) |
| 2289 | return r; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2290 | atomic_inc(&vcpu->kvm->filp->f_count); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2291 | return fd; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2292 | } |
| 2293 | |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 2294 | /* |
| 2295 | * Creates some virtual cpus. Good luck creating more than one. |
| 2296 | */ |
| 2297 | static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n) |
| 2298 | { |
| 2299 | int r; |
| 2300 | struct kvm_vcpu *vcpu; |
| 2301 | |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 2302 | if (!valid_vcpu(n)) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2303 | return -EINVAL; |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 2304 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2305 | vcpu = kvm_x86_ops->vcpu_create(kvm, n); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2306 | if (IS_ERR(vcpu)) |
| 2307 | return PTR_ERR(vcpu); |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 2308 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2309 | preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops); |
| 2310 | |
Rusty Russell | b114b08 | 2007-07-30 21:13:43 +1000 | [diff] [blame] | 2311 | /* We do fxsave: this must be aligned. */ |
| 2312 | BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF); |
| 2313 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2314 | vcpu_load(vcpu); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2315 | r = kvm_x86_ops->vcpu_reset(vcpu); |
| 2316 | if (r == 0) |
| 2317 | r = kvm_mmu_setup(vcpu); |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 2318 | vcpu_put(vcpu); |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 2319 | if (r < 0) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2320 | goto free_vcpu; |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 2321 | |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 2322 | mutex_lock(&kvm->lock); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2323 | if (kvm->vcpus[n]) { |
| 2324 | r = -EEXIST; |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 2325 | mutex_unlock(&kvm->lock); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2326 | goto mmu_unload; |
| 2327 | } |
| 2328 | kvm->vcpus[n] = vcpu; |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 2329 | mutex_unlock(&kvm->lock); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2330 | |
| 2331 | /* Now it's all set up, let userspace reach it */ |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2332 | r = create_vcpu_fd(vcpu); |
| 2333 | if (r < 0) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2334 | goto unlink; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2335 | return r; |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 2336 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2337 | unlink: |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 2338 | mutex_lock(&kvm->lock); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2339 | kvm->vcpus[n] = NULL; |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 2340 | mutex_unlock(&kvm->lock); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2341 | |
| 2342 | mmu_unload: |
| 2343 | vcpu_load(vcpu); |
| 2344 | kvm_mmu_unload(vcpu); |
| 2345 | vcpu_put(vcpu); |
| 2346 | |
| 2347 | free_vcpu: |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2348 | kvm_x86_ops->vcpu_free(vcpu); |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 2349 | return r; |
| 2350 | } |
| 2351 | |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 2352 | static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) |
| 2353 | { |
| 2354 | if (sigset) { |
| 2355 | sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP)); |
| 2356 | vcpu->sigset_active = 1; |
| 2357 | vcpu->sigset = *sigset; |
| 2358 | } else |
| 2359 | vcpu->sigset_active = 0; |
| 2360 | return 0; |
| 2361 | } |
| 2362 | |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 2363 | /* |
| 2364 | * fxsave fpu state. Taken from x86_64/processor.h. To be killed when |
| 2365 | * we have asm/x86/processor.h |
| 2366 | */ |
| 2367 | struct fxsave { |
| 2368 | u16 cwd; |
| 2369 | u16 swd; |
| 2370 | u16 twd; |
| 2371 | u16 fop; |
| 2372 | u64 rip; |
| 2373 | u64 rdp; |
| 2374 | u32 mxcsr; |
| 2375 | u32 mxcsr_mask; |
| 2376 | u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */ |
| 2377 | #ifdef CONFIG_X86_64 |
| 2378 | u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */ |
| 2379 | #else |
| 2380 | u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */ |
| 2381 | #endif |
| 2382 | }; |
| 2383 | |
| 2384 | static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 2385 | { |
Rusty Russell | b114b08 | 2007-07-30 21:13:43 +1000 | [diff] [blame] | 2386 | struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image; |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 2387 | |
| 2388 | vcpu_load(vcpu); |
| 2389 | |
| 2390 | memcpy(fpu->fpr, fxsave->st_space, 128); |
| 2391 | fpu->fcw = fxsave->cwd; |
| 2392 | fpu->fsw = fxsave->swd; |
| 2393 | fpu->ftwx = fxsave->twd; |
| 2394 | fpu->last_opcode = fxsave->fop; |
| 2395 | fpu->last_ip = fxsave->rip; |
| 2396 | fpu->last_dp = fxsave->rdp; |
| 2397 | memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space); |
| 2398 | |
| 2399 | vcpu_put(vcpu); |
| 2400 | |
| 2401 | return 0; |
| 2402 | } |
| 2403 | |
| 2404 | static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 2405 | { |
Rusty Russell | b114b08 | 2007-07-30 21:13:43 +1000 | [diff] [blame] | 2406 | struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image; |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 2407 | |
| 2408 | vcpu_load(vcpu); |
| 2409 | |
| 2410 | memcpy(fxsave->st_space, fpu->fpr, 128); |
| 2411 | fxsave->cwd = fpu->fcw; |
| 2412 | fxsave->swd = fpu->fsw; |
| 2413 | fxsave->twd = fpu->ftwx; |
| 2414 | fxsave->fop = fpu->last_opcode; |
| 2415 | fxsave->rip = fpu->last_ip; |
| 2416 | fxsave->rdp = fpu->last_dp; |
| 2417 | memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space); |
| 2418 | |
| 2419 | vcpu_put(vcpu); |
| 2420 | |
| 2421 | return 0; |
| 2422 | } |
| 2423 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2424 | static long kvm_vcpu_ioctl(struct file *filp, |
| 2425 | unsigned int ioctl, unsigned long arg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2426 | { |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2427 | struct kvm_vcpu *vcpu = filp->private_data; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 2428 | void __user *argp = (void __user *)arg; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2429 | int r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2430 | |
| 2431 | switch (ioctl) { |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 2432 | case KVM_RUN: |
Avi Kivity | f0fe510 | 2007-03-07 13:11:17 +0200 | [diff] [blame] | 2433 | r = -EINVAL; |
| 2434 | if (arg) |
| 2435 | goto out; |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 2436 | r = kvm_vcpu_ioctl_run(vcpu, vcpu->run); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2437 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2438 | case KVM_GET_REGS: { |
| 2439 | struct kvm_regs kvm_regs; |
| 2440 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2441 | memset(&kvm_regs, 0, sizeof kvm_regs); |
| 2442 | r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2443 | if (r) |
| 2444 | goto out; |
| 2445 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 2446 | if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2447 | goto out; |
| 2448 | r = 0; |
| 2449 | break; |
| 2450 | } |
| 2451 | case KVM_SET_REGS: { |
| 2452 | struct kvm_regs kvm_regs; |
| 2453 | |
| 2454 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 2455 | if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2456 | goto out; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2457 | r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2458 | if (r) |
| 2459 | goto out; |
| 2460 | r = 0; |
| 2461 | break; |
| 2462 | } |
| 2463 | case KVM_GET_SREGS: { |
| 2464 | struct kvm_sregs kvm_sregs; |
| 2465 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2466 | memset(&kvm_sregs, 0, sizeof kvm_sregs); |
| 2467 | r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2468 | if (r) |
| 2469 | goto out; |
| 2470 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 2471 | if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2472 | goto out; |
| 2473 | r = 0; |
| 2474 | break; |
| 2475 | } |
| 2476 | case KVM_SET_SREGS: { |
| 2477 | struct kvm_sregs kvm_sregs; |
| 2478 | |
| 2479 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 2480 | if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2481 | goto out; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2482 | r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2483 | if (r) |
| 2484 | goto out; |
| 2485 | r = 0; |
| 2486 | break; |
| 2487 | } |
| 2488 | case KVM_TRANSLATE: { |
| 2489 | struct kvm_translation tr; |
| 2490 | |
| 2491 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 2492 | if (copy_from_user(&tr, argp, sizeof tr)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2493 | goto out; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2494 | r = kvm_vcpu_ioctl_translate(vcpu, &tr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2495 | if (r) |
| 2496 | goto out; |
| 2497 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 2498 | if (copy_to_user(argp, &tr, sizeof tr)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2499 | goto out; |
| 2500 | r = 0; |
| 2501 | break; |
| 2502 | } |
| 2503 | case KVM_INTERRUPT: { |
| 2504 | struct kvm_interrupt irq; |
| 2505 | |
| 2506 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 2507 | if (copy_from_user(&irq, argp, sizeof irq)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2508 | goto out; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2509 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2510 | if (r) |
| 2511 | goto out; |
| 2512 | r = 0; |
| 2513 | break; |
| 2514 | } |
| 2515 | case KVM_DEBUG_GUEST: { |
| 2516 | struct kvm_debug_guest dbg; |
| 2517 | |
| 2518 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 2519 | if (copy_from_user(&dbg, argp, sizeof dbg)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2520 | goto out; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2521 | r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2522 | if (r) |
| 2523 | goto out; |
| 2524 | r = 0; |
| 2525 | break; |
| 2526 | } |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 2527 | case KVM_SET_SIGNAL_MASK: { |
| 2528 | struct kvm_signal_mask __user *sigmask_arg = argp; |
| 2529 | struct kvm_signal_mask kvm_sigmask; |
| 2530 | sigset_t sigset, *p; |
| 2531 | |
| 2532 | p = NULL; |
| 2533 | if (argp) { |
| 2534 | r = -EFAULT; |
| 2535 | if (copy_from_user(&kvm_sigmask, argp, |
| 2536 | sizeof kvm_sigmask)) |
| 2537 | goto out; |
| 2538 | r = -EINVAL; |
| 2539 | if (kvm_sigmask.len != sizeof sigset) |
| 2540 | goto out; |
| 2541 | r = -EFAULT; |
| 2542 | if (copy_from_user(&sigset, sigmask_arg->sigset, |
| 2543 | sizeof sigset)) |
| 2544 | goto out; |
| 2545 | p = &sigset; |
| 2546 | } |
| 2547 | r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset); |
| 2548 | break; |
| 2549 | } |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 2550 | case KVM_GET_FPU: { |
| 2551 | struct kvm_fpu fpu; |
| 2552 | |
| 2553 | memset(&fpu, 0, sizeof fpu); |
| 2554 | r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu); |
| 2555 | if (r) |
| 2556 | goto out; |
| 2557 | r = -EFAULT; |
| 2558 | if (copy_to_user(argp, &fpu, sizeof fpu)) |
| 2559 | goto out; |
| 2560 | r = 0; |
| 2561 | break; |
| 2562 | } |
| 2563 | case KVM_SET_FPU: { |
| 2564 | struct kvm_fpu fpu; |
| 2565 | |
| 2566 | r = -EFAULT; |
| 2567 | if (copy_from_user(&fpu, argp, sizeof fpu)) |
| 2568 | goto out; |
| 2569 | r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu); |
| 2570 | if (r) |
| 2571 | goto out; |
| 2572 | r = 0; |
| 2573 | break; |
| 2574 | } |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2575 | default: |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2576 | r = kvm_arch_vcpu_ioctl(filp, ioctl, arg); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2577 | } |
| 2578 | out: |
| 2579 | return r; |
| 2580 | } |
| 2581 | |
| 2582 | static long kvm_vm_ioctl(struct file *filp, |
| 2583 | unsigned int ioctl, unsigned long arg) |
| 2584 | { |
| 2585 | struct kvm *kvm = filp->private_data; |
| 2586 | void __user *argp = (void __user *)arg; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2587 | int r; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2588 | |
| 2589 | switch (ioctl) { |
| 2590 | case KVM_CREATE_VCPU: |
| 2591 | r = kvm_vm_ioctl_create_vcpu(kvm, arg); |
| 2592 | if (r < 0) |
| 2593 | goto out; |
| 2594 | break; |
Izik Eidus | 6fc138d | 2007-10-09 19:20:39 +0200 | [diff] [blame] | 2595 | case KVM_SET_USER_MEMORY_REGION: { |
| 2596 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 2597 | |
| 2598 | r = -EFAULT; |
| 2599 | if (copy_from_user(&kvm_userspace_mem, argp, |
| 2600 | sizeof kvm_userspace_mem)) |
| 2601 | goto out; |
| 2602 | |
| 2603 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2604 | if (r) |
| 2605 | goto out; |
| 2606 | break; |
| 2607 | } |
| 2608 | case KVM_GET_DIRTY_LOG: { |
| 2609 | struct kvm_dirty_log log; |
| 2610 | |
| 2611 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 2612 | if (copy_from_user(&log, argp, sizeof log)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2613 | goto out; |
Avi Kivity | 2c6f5df | 2007-02-20 18:27:58 +0200 | [diff] [blame] | 2614 | r = kvm_vm_ioctl_get_dirty_log(kvm, &log); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2615 | if (r) |
| 2616 | goto out; |
| 2617 | break; |
| 2618 | } |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2619 | default: |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2620 | r = kvm_arch_vm_ioctl(filp, ioctl, arg); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2621 | } |
| 2622 | out: |
| 2623 | return r; |
| 2624 | } |
| 2625 | |
| 2626 | static struct page *kvm_vm_nopage(struct vm_area_struct *vma, |
| 2627 | unsigned long address, |
| 2628 | int *type) |
| 2629 | { |
| 2630 | struct kvm *kvm = vma->vm_file->private_data; |
| 2631 | unsigned long pgoff; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2632 | struct page *page; |
| 2633 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2634 | pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 2635 | if (!kvm_is_visible_gfn(kvm, pgoff)) |
| 2636 | return NOPAGE_SIGBUS; |
Anthony Liguori | aab61cc | 2007-10-29 15:15:20 -0500 | [diff] [blame^] | 2637 | /* current->mm->mmap_sem is already held so call lockless version */ |
| 2638 | page = __gfn_to_page(kvm, pgoff); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 2639 | if (is_error_page(page)) { |
| 2640 | kvm_release_page(page); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2641 | return NOPAGE_SIGBUS; |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 2642 | } |
Nguyen Anh Quynh | cd0d913 | 2007-07-11 14:30:54 +0300 | [diff] [blame] | 2643 | if (type != NULL) |
| 2644 | *type = VM_FAULT_MINOR; |
| 2645 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2646 | return page; |
| 2647 | } |
| 2648 | |
| 2649 | static struct vm_operations_struct kvm_vm_vm_ops = { |
| 2650 | .nopage = kvm_vm_nopage, |
| 2651 | }; |
| 2652 | |
| 2653 | static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma) |
| 2654 | { |
| 2655 | vma->vm_ops = &kvm_vm_vm_ops; |
| 2656 | return 0; |
| 2657 | } |
| 2658 | |
| 2659 | static struct file_operations kvm_vm_fops = { |
| 2660 | .release = kvm_vm_release, |
| 2661 | .unlocked_ioctl = kvm_vm_ioctl, |
| 2662 | .compat_ioctl = kvm_vm_ioctl, |
| 2663 | .mmap = kvm_vm_mmap, |
| 2664 | }; |
| 2665 | |
| 2666 | static int kvm_dev_ioctl_create_vm(void) |
| 2667 | { |
| 2668 | int fd, r; |
| 2669 | struct inode *inode; |
| 2670 | struct file *file; |
| 2671 | struct kvm *kvm; |
| 2672 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2673 | kvm = kvm_create_vm(); |
Avi Kivity | d6d2816 | 2007-06-28 08:38:16 -0400 | [diff] [blame] | 2674 | if (IS_ERR(kvm)) |
| 2675 | return PTR_ERR(kvm); |
| 2676 | r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm); |
| 2677 | if (r) { |
| 2678 | kvm_destroy_vm(kvm); |
| 2679 | return r; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2680 | } |
| 2681 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2682 | kvm->filp = file; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2683 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2684 | return fd; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2685 | } |
| 2686 | |
| 2687 | static long kvm_dev_ioctl(struct file *filp, |
| 2688 | unsigned int ioctl, unsigned long arg) |
| 2689 | { |
| 2690 | void __user *argp = (void __user *)arg; |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 2691 | long r = -EINVAL; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2692 | |
| 2693 | switch (ioctl) { |
| 2694 | case KVM_GET_API_VERSION: |
Avi Kivity | f0fe510 | 2007-03-07 13:11:17 +0200 | [diff] [blame] | 2695 | r = -EINVAL; |
| 2696 | if (arg) |
| 2697 | goto out; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2698 | r = KVM_API_VERSION; |
| 2699 | break; |
| 2700 | case KVM_CREATE_VM: |
Avi Kivity | f0fe510 | 2007-03-07 13:11:17 +0200 | [diff] [blame] | 2701 | r = -EINVAL; |
| 2702 | if (arg) |
| 2703 | goto out; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2704 | r = kvm_dev_ioctl_create_vm(); |
| 2705 | break; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2706 | case KVM_CHECK_EXTENSION: { |
| 2707 | int ext = (long)argp; |
| 2708 | |
| 2709 | switch (ext) { |
| 2710 | case KVM_CAP_IRQCHIP: |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 2711 | case KVM_CAP_HLT: |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 2712 | case KVM_CAP_MMU_SHADOW_CACHE_CONTROL: |
Izik Eidus | 6fc138d | 2007-10-09 19:20:39 +0200 | [diff] [blame] | 2713 | case KVM_CAP_USER_MEMORY: |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 2714 | case KVM_CAP_SET_TSS_ADDR: |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2715 | r = 1; |
| 2716 | break; |
| 2717 | default: |
| 2718 | r = 0; |
| 2719 | break; |
| 2720 | } |
Avi Kivity | 5d308f4 | 2007-03-01 17:56:20 +0200 | [diff] [blame] | 2721 | break; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2722 | } |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 2723 | case KVM_GET_VCPU_MMAP_SIZE: |
| 2724 | r = -EINVAL; |
| 2725 | if (arg) |
| 2726 | goto out; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 2727 | r = 2 * PAGE_SIZE; |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 2728 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2729 | default: |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 2730 | return kvm_arch_dev_ioctl(filp, ioctl, arg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2731 | } |
| 2732 | out: |
| 2733 | return r; |
| 2734 | } |
| 2735 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2736 | static struct file_operations kvm_chardev_ops = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2737 | .unlocked_ioctl = kvm_dev_ioctl, |
| 2738 | .compat_ioctl = kvm_dev_ioctl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2739 | }; |
| 2740 | |
| 2741 | static struct miscdevice kvm_dev = { |
Avi Kivity | bbe4432 | 2007-03-04 13:27:36 +0200 | [diff] [blame] | 2742 | KVM_MINOR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2743 | "kvm", |
| 2744 | &kvm_chardev_ops, |
| 2745 | }; |
| 2746 | |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2747 | /* |
| 2748 | * Make sure that a cpu that is being hot-unplugged does not have any vcpus |
| 2749 | * cached on it. |
| 2750 | */ |
| 2751 | static void decache_vcpus_on_cpu(int cpu) |
| 2752 | { |
| 2753 | struct kvm *vm; |
| 2754 | struct kvm_vcpu *vcpu; |
| 2755 | int i; |
| 2756 | |
| 2757 | spin_lock(&kvm_lock); |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 2758 | list_for_each_entry(vm, &vm_list, vm_list) |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2759 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2760 | vcpu = vm->vcpus[i]; |
| 2761 | if (!vcpu) |
| 2762 | continue; |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2763 | /* |
| 2764 | * If the vcpu is locked, then it is running on some |
| 2765 | * other cpu and therefore it is not cached on the |
| 2766 | * cpu in question. |
| 2767 | * |
| 2768 | * If it's not locked, check the last cpu it executed |
| 2769 | * on. |
| 2770 | */ |
| 2771 | if (mutex_trylock(&vcpu->mutex)) { |
| 2772 | if (vcpu->cpu == cpu) { |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2773 | kvm_x86_ops->vcpu_decache(vcpu); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2774 | vcpu->cpu = -1; |
| 2775 | } |
| 2776 | mutex_unlock(&vcpu->mutex); |
| 2777 | } |
| 2778 | } |
| 2779 | spin_unlock(&kvm_lock); |
| 2780 | } |
| 2781 | |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 2782 | static void hardware_enable(void *junk) |
| 2783 | { |
| 2784 | int cpu = raw_smp_processor_id(); |
| 2785 | |
| 2786 | if (cpu_isset(cpu, cpus_hardware_enabled)) |
| 2787 | return; |
| 2788 | cpu_set(cpu, cpus_hardware_enabled); |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2789 | kvm_x86_ops->hardware_enable(NULL); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 2790 | } |
| 2791 | |
| 2792 | static void hardware_disable(void *junk) |
| 2793 | { |
| 2794 | int cpu = raw_smp_processor_id(); |
| 2795 | |
| 2796 | if (!cpu_isset(cpu, cpus_hardware_enabled)) |
| 2797 | return; |
| 2798 | cpu_clear(cpu, cpus_hardware_enabled); |
| 2799 | decache_vcpus_on_cpu(cpu); |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2800 | kvm_x86_ops->hardware_disable(NULL); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 2801 | } |
| 2802 | |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2803 | static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val, |
| 2804 | void *v) |
| 2805 | { |
| 2806 | int cpu = (long)v; |
| 2807 | |
| 2808 | switch (val) { |
Avi Kivity | cec9ad2 | 2007-05-24 13:11:41 +0300 | [diff] [blame] | 2809 | case CPU_DYING: |
| 2810 | case CPU_DYING_FROZEN: |
Avi Kivity | 6ec8a85 | 2007-08-19 15:57:26 +0300 | [diff] [blame] | 2811 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
| 2812 | cpu); |
| 2813 | hardware_disable(NULL); |
| 2814 | break; |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2815 | case CPU_UP_CANCELED: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 2816 | case CPU_UP_CANCELED_FROZEN: |
Jeremy Katz | 43934a3 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 2817 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
| 2818 | cpu); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 2819 | smp_call_function_single(cpu, hardware_disable, NULL, 0, 1); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2820 | break; |
Jeremy Katz | 43934a3 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 2821 | case CPU_ONLINE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 2822 | case CPU_ONLINE_FROZEN: |
Jeremy Katz | 43934a3 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 2823 | printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n", |
| 2824 | cpu); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 2825 | smp_call_function_single(cpu, hardware_enable, NULL, 0, 1); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2826 | break; |
| 2827 | } |
| 2828 | return NOTIFY_OK; |
| 2829 | } |
| 2830 | |
Rusty Russell | 9a2b85c | 2007-07-17 23:17:55 +1000 | [diff] [blame] | 2831 | static int kvm_reboot(struct notifier_block *notifier, unsigned long val, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2832 | void *v) |
Rusty Russell | 9a2b85c | 2007-07-17 23:17:55 +1000 | [diff] [blame] | 2833 | { |
| 2834 | if (val == SYS_RESTART) { |
| 2835 | /* |
| 2836 | * Some (well, at least mine) BIOSes hang on reboot if |
| 2837 | * in vmx root mode. |
| 2838 | */ |
| 2839 | printk(KERN_INFO "kvm: exiting hardware virtualization\n"); |
| 2840 | on_each_cpu(hardware_disable, NULL, 0, 1); |
| 2841 | } |
| 2842 | return NOTIFY_OK; |
| 2843 | } |
| 2844 | |
| 2845 | static struct notifier_block kvm_reboot_notifier = { |
| 2846 | .notifier_call = kvm_reboot, |
| 2847 | .priority = 0, |
| 2848 | }; |
| 2849 | |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 2850 | void kvm_io_bus_init(struct kvm_io_bus *bus) |
| 2851 | { |
| 2852 | memset(bus, 0, sizeof(*bus)); |
| 2853 | } |
| 2854 | |
| 2855 | void kvm_io_bus_destroy(struct kvm_io_bus *bus) |
| 2856 | { |
| 2857 | int i; |
| 2858 | |
| 2859 | for (i = 0; i < bus->dev_count; i++) { |
| 2860 | struct kvm_io_device *pos = bus->devs[i]; |
| 2861 | |
| 2862 | kvm_iodevice_destructor(pos); |
| 2863 | } |
| 2864 | } |
| 2865 | |
| 2866 | struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr) |
| 2867 | { |
| 2868 | int i; |
| 2869 | |
| 2870 | for (i = 0; i < bus->dev_count; i++) { |
| 2871 | struct kvm_io_device *pos = bus->devs[i]; |
| 2872 | |
| 2873 | if (pos->in_range(pos, addr)) |
| 2874 | return pos; |
| 2875 | } |
| 2876 | |
| 2877 | return NULL; |
| 2878 | } |
| 2879 | |
| 2880 | void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev) |
| 2881 | { |
| 2882 | BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1)); |
| 2883 | |
| 2884 | bus->devs[bus->dev_count++] = dev; |
| 2885 | } |
| 2886 | |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2887 | static struct notifier_block kvm_cpu_notifier = { |
| 2888 | .notifier_call = kvm_cpu_hotplug, |
| 2889 | .priority = 20, /* must be > scheduler priority */ |
| 2890 | }; |
| 2891 | |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2892 | static u64 stat_get(void *_offset) |
| 2893 | { |
| 2894 | unsigned offset = (long)_offset; |
| 2895 | u64 total = 0; |
| 2896 | struct kvm *kvm; |
| 2897 | struct kvm_vcpu *vcpu; |
| 2898 | int i; |
| 2899 | |
| 2900 | spin_lock(&kvm_lock); |
| 2901 | list_for_each_entry(kvm, &vm_list, vm_list) |
| 2902 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2903 | vcpu = kvm->vcpus[i]; |
| 2904 | if (vcpu) |
| 2905 | total += *(u32 *)((void *)vcpu + offset); |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2906 | } |
| 2907 | spin_unlock(&kvm_lock); |
| 2908 | return total; |
| 2909 | } |
| 2910 | |
Rusty Russell | 3dea7ca | 2007-08-01 10:12:22 +1000 | [diff] [blame] | 2911 | DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n"); |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2912 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2913 | static __init void kvm_init_debug(void) |
| 2914 | { |
| 2915 | struct kvm_stats_debugfs_item *p; |
| 2916 | |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 2917 | debugfs_dir = debugfs_create_dir("kvm", NULL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2918 | for (p = debugfs_entries; p->name; ++p) |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2919 | p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir, |
| 2920 | (void *)(long)p->offset, |
| 2921 | &stat_fops); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2922 | } |
| 2923 | |
| 2924 | static void kvm_exit_debug(void) |
| 2925 | { |
| 2926 | struct kvm_stats_debugfs_item *p; |
| 2927 | |
| 2928 | for (p = debugfs_entries; p->name; ++p) |
| 2929 | debugfs_remove(p->dentry); |
| 2930 | debugfs_remove(debugfs_dir); |
| 2931 | } |
| 2932 | |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 2933 | static int kvm_suspend(struct sys_device *dev, pm_message_t state) |
| 2934 | { |
Avi Kivity | 4267c41 | 2007-05-24 13:09:41 +0300 | [diff] [blame] | 2935 | hardware_disable(NULL); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 2936 | return 0; |
| 2937 | } |
| 2938 | |
| 2939 | static int kvm_resume(struct sys_device *dev) |
| 2940 | { |
Avi Kivity | 4267c41 | 2007-05-24 13:09:41 +0300 | [diff] [blame] | 2941 | hardware_enable(NULL); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 2942 | return 0; |
| 2943 | } |
| 2944 | |
| 2945 | static struct sysdev_class kvm_sysdev_class = { |
Kay Sievers | af5ca3f4 | 2007-12-20 02:09:39 +0100 | [diff] [blame] | 2946 | .name = "kvm", |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 2947 | .suspend = kvm_suspend, |
| 2948 | .resume = kvm_resume, |
| 2949 | }; |
| 2950 | |
| 2951 | static struct sys_device kvm_sysdev = { |
| 2952 | .id = 0, |
| 2953 | .cls = &kvm_sysdev_class, |
| 2954 | }; |
| 2955 | |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 2956 | struct page *bad_page; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2957 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2958 | static inline |
| 2959 | struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn) |
| 2960 | { |
| 2961 | return container_of(pn, struct kvm_vcpu, preempt_notifier); |
| 2962 | } |
| 2963 | |
| 2964 | static void kvm_sched_in(struct preempt_notifier *pn, int cpu) |
| 2965 | { |
| 2966 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); |
| 2967 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2968 | kvm_x86_ops->vcpu_load(vcpu, cpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2969 | } |
| 2970 | |
| 2971 | static void kvm_sched_out(struct preempt_notifier *pn, |
| 2972 | struct task_struct *next) |
| 2973 | { |
| 2974 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); |
| 2975 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2976 | kvm_x86_ops->vcpu_put(vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2977 | } |
| 2978 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2979 | int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size, |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 2980 | struct module *module) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2981 | { |
| 2982 | int r; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2983 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2984 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2985 | if (kvm_x86_ops) { |
Yoshimi Ichiyanagi | 09db28b | 2006-12-29 16:49:41 -0800 | [diff] [blame] | 2986 | printk(KERN_ERR "kvm: already loaded the other module\n"); |
| 2987 | return -EEXIST; |
| 2988 | } |
| 2989 | |
Yoshimi Ichiyanagi | e097f35 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2990 | if (!ops->cpu_has_kvm_support()) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2991 | printk(KERN_ERR "kvm: no hardware support\n"); |
| 2992 | return -EOPNOTSUPP; |
| 2993 | } |
Yoshimi Ichiyanagi | e097f35 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2994 | if (ops->disabled_by_bios()) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2995 | printk(KERN_ERR "kvm: disabled by bios\n"); |
| 2996 | return -EOPNOTSUPP; |
| 2997 | } |
| 2998 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2999 | kvm_x86_ops = ops; |
Yoshimi Ichiyanagi | e097f35 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 3000 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 3001 | r = kvm_x86_ops->hardware_setup(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3002 | if (r < 0) |
Avi Kivity | ca45aaa | 2007-03-01 19:21:03 +0200 | [diff] [blame] | 3003 | goto out; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3004 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3005 | for_each_online_cpu(cpu) { |
| 3006 | smp_call_function_single(cpu, |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 3007 | kvm_x86_ops->check_processor_compatibility, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3008 | &r, 0, 1); |
| 3009 | if (r < 0) |
| 3010 | goto out_free_0; |
| 3011 | } |
| 3012 | |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 3013 | on_each_cpu(hardware_enable, NULL, 0, 1); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 3014 | r = register_cpu_notifier(&kvm_cpu_notifier); |
| 3015 | if (r) |
| 3016 | goto out_free_1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3017 | register_reboot_notifier(&kvm_reboot_notifier); |
| 3018 | |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 3019 | r = sysdev_class_register(&kvm_sysdev_class); |
| 3020 | if (r) |
| 3021 | goto out_free_2; |
| 3022 | |
| 3023 | r = sysdev_register(&kvm_sysdev); |
| 3024 | if (r) |
| 3025 | goto out_free_3; |
| 3026 | |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 3027 | /* A kmem cache lets us meet the alignment requirements of fx_save. */ |
| 3028 | kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, |
| 3029 | __alignof__(struct kvm_vcpu), 0, 0); |
| 3030 | if (!kvm_vcpu_cache) { |
| 3031 | r = -ENOMEM; |
| 3032 | goto out_free_4; |
| 3033 | } |
| 3034 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3035 | kvm_chardev_ops.owner = module; |
| 3036 | |
| 3037 | r = misc_register(&kvm_dev); |
| 3038 | if (r) { |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 3039 | printk(KERN_ERR "kvm: misc device register failed\n"); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3040 | goto out_free; |
| 3041 | } |
| 3042 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 3043 | kvm_preempt_ops.sched_in = kvm_sched_in; |
| 3044 | kvm_preempt_ops.sched_out = kvm_sched_out; |
| 3045 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 3046 | kvm_mmu_set_nonpresent_ptes(0ull, 0ull); |
| 3047 | |
| 3048 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3049 | |
| 3050 | out_free: |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 3051 | kmem_cache_destroy(kvm_vcpu_cache); |
| 3052 | out_free_4: |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 3053 | sysdev_unregister(&kvm_sysdev); |
| 3054 | out_free_3: |
| 3055 | sysdev_class_unregister(&kvm_sysdev_class); |
| 3056 | out_free_2: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3057 | unregister_reboot_notifier(&kvm_reboot_notifier); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 3058 | unregister_cpu_notifier(&kvm_cpu_notifier); |
| 3059 | out_free_1: |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 3060 | on_each_cpu(hardware_disable, NULL, 0, 1); |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3061 | out_free_0: |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 3062 | kvm_x86_ops->hardware_unsetup(); |
Avi Kivity | ca45aaa | 2007-03-01 19:21:03 +0200 | [diff] [blame] | 3063 | out: |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 3064 | kvm_x86_ops = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3065 | return r; |
| 3066 | } |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 3067 | EXPORT_SYMBOL_GPL(kvm_init_x86); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3068 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 3069 | void kvm_exit_x86(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3070 | { |
| 3071 | misc_deregister(&kvm_dev); |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 3072 | kmem_cache_destroy(kvm_vcpu_cache); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 3073 | sysdev_unregister(&kvm_sysdev); |
| 3074 | sysdev_class_unregister(&kvm_sysdev_class); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3075 | unregister_reboot_notifier(&kvm_reboot_notifier); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 3076 | unregister_cpu_notifier(&kvm_cpu_notifier); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 3077 | on_each_cpu(hardware_disable, NULL, 0, 1); |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 3078 | kvm_x86_ops->hardware_unsetup(); |
| 3079 | kvm_x86_ops = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3080 | } |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 3081 | EXPORT_SYMBOL_GPL(kvm_exit_x86); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3082 | |
| 3083 | static __init int kvm_init(void) |
| 3084 | { |
Avi Kivity | 37e29d9 | 2007-02-20 14:07:37 +0200 | [diff] [blame] | 3085 | int r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3086 | |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 3087 | r = kvm_mmu_module_init(); |
| 3088 | if (r) |
| 3089 | goto out4; |
| 3090 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3091 | kvm_init_debug(); |
| 3092 | |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 3093 | kvm_arch_init(); |
Michael Riepe | bf591b2 | 2006-12-22 01:05:36 -0800 | [diff] [blame] | 3094 | |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 3095 | bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 3096 | |
| 3097 | if (bad_page == NULL) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3098 | r = -ENOMEM; |
| 3099 | goto out; |
| 3100 | } |
| 3101 | |
Avi Kivity | 58e690e | 2007-02-26 16:29:43 +0200 | [diff] [blame] | 3102 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3103 | |
| 3104 | out: |
| 3105 | kvm_exit_debug(); |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 3106 | kvm_mmu_module_exit(); |
| 3107 | out4: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3108 | return r; |
| 3109 | } |
| 3110 | |
| 3111 | static __exit void kvm_exit(void) |
| 3112 | { |
| 3113 | kvm_exit_debug(); |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 3114 | __free_page(bad_page); |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 3115 | kvm_mmu_module_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3116 | } |
| 3117 | |
| 3118 | module_init(kvm_init) |
| 3119 | module_exit(kvm_exit) |