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