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