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