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