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