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