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