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