Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Kernel-based Virtual Machine driver for Linux |
| 3 | * |
| 4 | * derived from drivers/kvm/kvm_main.c |
| 5 | * |
| 6 | * Copyright (C) 2006 Qumranet, Inc. |
Ben-Ami Yassour | 4d5c5d0 | 2008-07-28 19:26:26 +0300 | [diff] [blame] | 7 | * Copyright (C) 2008 Qumranet, Inc. |
| 8 | * Copyright IBM Corporation, 2008 |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 9 | * |
| 10 | * Authors: |
| 11 | * Avi Kivity <avi@qumranet.com> |
| 12 | * Yaniv Kamay <yaniv@qumranet.com> |
Ben-Ami Yassour | 4d5c5d0 | 2008-07-28 19:26:26 +0300 | [diff] [blame] | 13 | * Amit Shah <amit.shah@qumranet.com> |
| 14 | * Ben-Ami Yassour <benami@il.ibm.com> |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 15 | * |
| 16 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 17 | * the COPYING file in the top-level directory. |
| 18 | * |
| 19 | */ |
| 20 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 21 | #include <linux/kvm_host.h> |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 22 | #include "irq.h" |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 23 | #include "mmu.h" |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 24 | #include "i8254.h" |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 25 | #include "tss.h" |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 26 | #include "kvm_cache_regs.h" |
Avi Kivity | 26eef70 | 2008-07-03 14:59:22 +0300 | [diff] [blame] | 27 | #include "x86.h" |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 28 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 29 | #include <linux/clocksource.h> |
Ben-Ami Yassour | 4d5c5d0 | 2008-07-28 19:26:26 +0300 | [diff] [blame] | 30 | #include <linux/interrupt.h> |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 31 | #include <linux/kvm.h> |
| 32 | #include <linux/fs.h> |
Ben-Ami Yassour | 4d5c5d0 | 2008-07-28 19:26:26 +0300 | [diff] [blame] | 33 | #include <linux/pci.h> |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 34 | #include <linux/vmalloc.h> |
Carsten Otte | 5fb76f9 | 2007-10-29 16:08:51 +0100 | [diff] [blame] | 35 | #include <linux/module.h> |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 36 | #include <linux/mman.h> |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 37 | #include <linux/highmem.h> |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 38 | |
| 39 | #include <asm/uaccess.h> |
Zhang Xiantao | d825ed0 | 2007-11-14 20:08:51 +0800 | [diff] [blame] | 40 | #include <asm/msr.h> |
Avi Kivity | a5f6130 | 2008-02-20 17:57:21 +0200 | [diff] [blame] | 41 | #include <asm/desc.h> |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 42 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 43 | #define MAX_IO_MSRS 256 |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 44 | #define CR0_RESERVED_BITS \ |
| 45 | (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \ |
| 46 | | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \ |
| 47 | | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG)) |
| 48 | #define CR4_RESERVED_BITS \ |
| 49 | (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\ |
| 50 | | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \ |
| 51 | | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \ |
| 52 | | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE)) |
| 53 | |
| 54 | #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR) |
Joerg Roedel | 50a37eb | 2008-01-31 14:57:38 +0100 | [diff] [blame] | 55 | /* EFER defaults: |
| 56 | * - enable syscall per default because its emulated by KVM |
| 57 | * - enable LME and LMA per default on 64 bit KVM |
| 58 | */ |
| 59 | #ifdef CONFIG_X86_64 |
| 60 | static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL; |
| 61 | #else |
| 62 | static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL; |
| 63 | #endif |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 64 | |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 65 | #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM |
| 66 | #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU |
Hollis Blanchard | 417bc30 | 2007-10-31 17:24:23 -0500 | [diff] [blame] | 67 | |
Avi Kivity | 674eea0 | 2008-02-11 18:37:23 +0200 | [diff] [blame] | 68 | static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid, |
| 69 | struct kvm_cpuid_entry2 __user *entries); |
| 70 | |
Zhang Xiantao | 97896d0 | 2007-11-14 20:09:30 +0800 | [diff] [blame] | 71 | struct kvm_x86_ops *kvm_x86_ops; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 72 | EXPORT_SYMBOL_GPL(kvm_x86_ops); |
Zhang Xiantao | 97896d0 | 2007-11-14 20:09:30 +0800 | [diff] [blame] | 73 | |
Hollis Blanchard | 417bc30 | 2007-10-31 17:24:23 -0500 | [diff] [blame] | 74 | struct kvm_stats_debugfs_item debugfs_entries[] = { |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 75 | { "pf_fixed", VCPU_STAT(pf_fixed) }, |
| 76 | { "pf_guest", VCPU_STAT(pf_guest) }, |
| 77 | { "tlb_flush", VCPU_STAT(tlb_flush) }, |
| 78 | { "invlpg", VCPU_STAT(invlpg) }, |
| 79 | { "exits", VCPU_STAT(exits) }, |
| 80 | { "io_exits", VCPU_STAT(io_exits) }, |
| 81 | { "mmio_exits", VCPU_STAT(mmio_exits) }, |
| 82 | { "signal_exits", VCPU_STAT(signal_exits) }, |
| 83 | { "irq_window", VCPU_STAT(irq_window_exits) }, |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 84 | { "nmi_window", VCPU_STAT(nmi_window_exits) }, |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 85 | { "halt_exits", VCPU_STAT(halt_exits) }, |
| 86 | { "halt_wakeup", VCPU_STAT(halt_wakeup) }, |
Amit Shah | f11c3a8 | 2008-02-21 01:00:30 +0530 | [diff] [blame] | 87 | { "hypercalls", VCPU_STAT(hypercalls) }, |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 88 | { "request_irq", VCPU_STAT(request_irq_exits) }, |
| 89 | { "irq_exits", VCPU_STAT(irq_exits) }, |
| 90 | { "host_state_reload", VCPU_STAT(host_state_reload) }, |
| 91 | { "efer_reload", VCPU_STAT(efer_reload) }, |
| 92 | { "fpu_reload", VCPU_STAT(fpu_reload) }, |
| 93 | { "insn_emulation", VCPU_STAT(insn_emulation) }, |
| 94 | { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) }, |
Avi Kivity | 4cee576 | 2007-11-18 16:37:07 +0200 | [diff] [blame] | 95 | { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) }, |
| 96 | { "mmu_pte_write", VM_STAT(mmu_pte_write) }, |
| 97 | { "mmu_pte_updated", VM_STAT(mmu_pte_updated) }, |
| 98 | { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) }, |
| 99 | { "mmu_flooded", VM_STAT(mmu_flooded) }, |
| 100 | { "mmu_recycled", VM_STAT(mmu_recycled) }, |
Avi Kivity | dfc5aa0 | 2007-12-18 19:47:18 +0200 | [diff] [blame] | 101 | { "mmu_cache_miss", VM_STAT(mmu_cache_miss) }, |
Avi Kivity | 0f74a24 | 2007-11-20 23:01:14 +0200 | [diff] [blame] | 102 | { "remote_tlb_flush", VM_STAT(remote_tlb_flush) }, |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 103 | { "largepages", VM_STAT(lpages) }, |
Hollis Blanchard | 417bc30 | 2007-10-31 17:24:23 -0500 | [diff] [blame] | 104 | { NULL } |
| 105 | }; |
| 106 | |
Harvey Harrison | ee032c9 | 2008-08-11 16:54:20 -0700 | [diff] [blame] | 107 | static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head, |
Ben-Ami Yassour | 4d5c5d0 | 2008-07-28 19:26:26 +0300 | [diff] [blame] | 108 | int assigned_dev_id) |
| 109 | { |
| 110 | struct list_head *ptr; |
| 111 | struct kvm_assigned_dev_kernel *match; |
| 112 | |
| 113 | list_for_each(ptr, head) { |
| 114 | match = list_entry(ptr, struct kvm_assigned_dev_kernel, list); |
| 115 | if (match->assigned_dev_id == assigned_dev_id) |
| 116 | return match; |
| 117 | } |
| 118 | return NULL; |
| 119 | } |
| 120 | |
| 121 | static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work) |
| 122 | { |
| 123 | struct kvm_assigned_dev_kernel *assigned_dev; |
| 124 | |
| 125 | assigned_dev = container_of(work, struct kvm_assigned_dev_kernel, |
| 126 | interrupt_work); |
| 127 | |
| 128 | /* This is taken to safely inject irq inside the guest. When |
| 129 | * the interrupt injection (or the ioapic code) uses a |
| 130 | * finer-grained lock, update this |
| 131 | */ |
| 132 | mutex_lock(&assigned_dev->kvm->lock); |
| 133 | kvm_set_irq(assigned_dev->kvm, |
| 134 | assigned_dev->guest_irq, 1); |
| 135 | mutex_unlock(&assigned_dev->kvm->lock); |
| 136 | kvm_put_kvm(assigned_dev->kvm); |
| 137 | } |
| 138 | |
| 139 | /* FIXME: Implement the OR logic needed to make shared interrupts on |
| 140 | * this line behave properly |
| 141 | */ |
| 142 | static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id) |
| 143 | { |
| 144 | struct kvm_assigned_dev_kernel *assigned_dev = |
| 145 | (struct kvm_assigned_dev_kernel *) dev_id; |
| 146 | |
| 147 | kvm_get_kvm(assigned_dev->kvm); |
| 148 | schedule_work(&assigned_dev->interrupt_work); |
| 149 | disable_irq_nosync(irq); |
| 150 | return IRQ_HANDLED; |
| 151 | } |
| 152 | |
| 153 | /* Ack the irq line for an assigned device */ |
| 154 | static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian) |
| 155 | { |
| 156 | struct kvm_assigned_dev_kernel *dev; |
| 157 | |
| 158 | if (kian->gsi == -1) |
| 159 | return; |
| 160 | |
| 161 | dev = container_of(kian, struct kvm_assigned_dev_kernel, |
| 162 | ack_notifier); |
| 163 | kvm_set_irq(dev->kvm, dev->guest_irq, 0); |
| 164 | enable_irq(dev->host_irq); |
| 165 | } |
| 166 | |
| 167 | static int kvm_vm_ioctl_assign_irq(struct kvm *kvm, |
| 168 | struct kvm_assigned_irq |
| 169 | *assigned_irq) |
| 170 | { |
| 171 | int r = 0; |
| 172 | struct kvm_assigned_dev_kernel *match; |
| 173 | |
| 174 | mutex_lock(&kvm->lock); |
| 175 | |
| 176 | match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, |
| 177 | assigned_irq->assigned_dev_id); |
| 178 | if (!match) { |
| 179 | mutex_unlock(&kvm->lock); |
| 180 | return -EINVAL; |
| 181 | } |
| 182 | |
| 183 | if (match->irq_requested) { |
| 184 | match->guest_irq = assigned_irq->guest_irq; |
| 185 | match->ack_notifier.gsi = assigned_irq->guest_irq; |
| 186 | mutex_unlock(&kvm->lock); |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | INIT_WORK(&match->interrupt_work, |
| 191 | kvm_assigned_dev_interrupt_work_handler); |
| 192 | |
| 193 | if (irqchip_in_kernel(kvm)) { |
Amit Shah | 6762b72 | 2008-08-13 16:22:37 +0300 | [diff] [blame] | 194 | if (!capable(CAP_SYS_RAWIO)) { |
| 195 | return -EPERM; |
| 196 | goto out; |
| 197 | } |
| 198 | |
Ben-Ami Yassour | 4d5c5d0 | 2008-07-28 19:26:26 +0300 | [diff] [blame] | 199 | if (assigned_irq->host_irq) |
| 200 | match->host_irq = assigned_irq->host_irq; |
| 201 | else |
| 202 | match->host_irq = match->dev->irq; |
| 203 | match->guest_irq = assigned_irq->guest_irq; |
| 204 | match->ack_notifier.gsi = assigned_irq->guest_irq; |
| 205 | match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq; |
| 206 | kvm_register_irq_ack_notifier(kvm, &match->ack_notifier); |
| 207 | |
| 208 | /* Even though this is PCI, we don't want to use shared |
| 209 | * interrupts. Sharing host devices with guest-assigned devices |
| 210 | * on the same interrupt line is not a happy situation: there |
| 211 | * are going to be long delays in accepting, acking, etc. |
| 212 | */ |
| 213 | if (request_irq(match->host_irq, kvm_assigned_dev_intr, 0, |
| 214 | "kvm_assigned_device", (void *)match)) { |
| 215 | printk(KERN_INFO "%s: couldn't allocate irq for pv " |
| 216 | "device\n", __func__); |
| 217 | r = -EIO; |
| 218 | goto out; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | match->irq_requested = true; |
| 223 | out: |
| 224 | mutex_unlock(&kvm->lock); |
| 225 | return r; |
| 226 | } |
| 227 | |
| 228 | static int kvm_vm_ioctl_assign_device(struct kvm *kvm, |
| 229 | struct kvm_assigned_pci_dev *assigned_dev) |
| 230 | { |
| 231 | int r = 0; |
| 232 | struct kvm_assigned_dev_kernel *match; |
| 233 | struct pci_dev *dev; |
| 234 | |
| 235 | mutex_lock(&kvm->lock); |
| 236 | |
| 237 | match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, |
| 238 | assigned_dev->assigned_dev_id); |
| 239 | if (match) { |
| 240 | /* device already assigned */ |
| 241 | r = -EINVAL; |
| 242 | goto out; |
| 243 | } |
| 244 | |
| 245 | match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL); |
| 246 | if (match == NULL) { |
| 247 | printk(KERN_INFO "%s: Couldn't allocate memory\n", |
| 248 | __func__); |
| 249 | r = -ENOMEM; |
| 250 | goto out; |
| 251 | } |
| 252 | dev = pci_get_bus_and_slot(assigned_dev->busnr, |
| 253 | assigned_dev->devfn); |
| 254 | if (!dev) { |
| 255 | printk(KERN_INFO "%s: host device not found\n", __func__); |
| 256 | r = -EINVAL; |
| 257 | goto out_free; |
| 258 | } |
| 259 | if (pci_enable_device(dev)) { |
| 260 | printk(KERN_INFO "%s: Could not enable PCI device\n", __func__); |
| 261 | r = -EBUSY; |
| 262 | goto out_put; |
| 263 | } |
| 264 | r = pci_request_regions(dev, "kvm_assigned_device"); |
| 265 | if (r) { |
| 266 | printk(KERN_INFO "%s: Could not get access to device regions\n", |
| 267 | __func__); |
| 268 | goto out_disable; |
| 269 | } |
| 270 | match->assigned_dev_id = assigned_dev->assigned_dev_id; |
| 271 | match->host_busnr = assigned_dev->busnr; |
| 272 | match->host_devfn = assigned_dev->devfn; |
| 273 | match->dev = dev; |
| 274 | |
| 275 | match->kvm = kvm; |
| 276 | |
| 277 | list_add(&match->list, &kvm->arch.assigned_dev_head); |
| 278 | |
| 279 | out: |
| 280 | mutex_unlock(&kvm->lock); |
| 281 | return r; |
| 282 | out_disable: |
| 283 | pci_disable_device(dev); |
| 284 | out_put: |
| 285 | pci_dev_put(dev); |
| 286 | out_free: |
| 287 | kfree(match); |
| 288 | mutex_unlock(&kvm->lock); |
| 289 | return r; |
| 290 | } |
| 291 | |
| 292 | static void kvm_free_assigned_devices(struct kvm *kvm) |
| 293 | { |
| 294 | struct list_head *ptr, *ptr2; |
| 295 | struct kvm_assigned_dev_kernel *assigned_dev; |
| 296 | |
| 297 | list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) { |
| 298 | assigned_dev = list_entry(ptr, |
| 299 | struct kvm_assigned_dev_kernel, |
| 300 | list); |
| 301 | |
| 302 | if (irqchip_in_kernel(kvm) && assigned_dev->irq_requested) { |
| 303 | free_irq(assigned_dev->host_irq, |
| 304 | (void *)assigned_dev); |
| 305 | |
| 306 | kvm_unregister_irq_ack_notifier(kvm, |
| 307 | &assigned_dev-> |
| 308 | ack_notifier); |
| 309 | } |
| 310 | |
| 311 | if (cancel_work_sync(&assigned_dev->interrupt_work)) |
| 312 | /* We had pending work. That means we will have to take |
| 313 | * care of kvm_put_kvm. |
| 314 | */ |
| 315 | kvm_put_kvm(kvm); |
| 316 | |
| 317 | pci_release_regions(assigned_dev->dev); |
| 318 | pci_disable_device(assigned_dev->dev); |
| 319 | pci_dev_put(assigned_dev->dev); |
| 320 | |
| 321 | list_del(&assigned_dev->list); |
| 322 | kfree(assigned_dev); |
| 323 | } |
| 324 | } |
Hollis Blanchard | 417bc30 | 2007-10-31 17:24:23 -0500 | [diff] [blame] | 325 | |
Carsten Otte | 5fb76f9 | 2007-10-29 16:08:51 +0100 | [diff] [blame] | 326 | unsigned long segment_base(u16 selector) |
| 327 | { |
| 328 | struct descriptor_table gdt; |
Avi Kivity | a5f6130 | 2008-02-20 17:57:21 +0200 | [diff] [blame] | 329 | struct desc_struct *d; |
Carsten Otte | 5fb76f9 | 2007-10-29 16:08:51 +0100 | [diff] [blame] | 330 | unsigned long table_base; |
| 331 | unsigned long v; |
| 332 | |
| 333 | if (selector == 0) |
| 334 | return 0; |
| 335 | |
| 336 | asm("sgdt %0" : "=m"(gdt)); |
| 337 | table_base = gdt.base; |
| 338 | |
| 339 | if (selector & 4) { /* from ldt */ |
| 340 | u16 ldt_selector; |
| 341 | |
| 342 | asm("sldt %0" : "=g"(ldt_selector)); |
| 343 | table_base = segment_base(ldt_selector); |
| 344 | } |
Avi Kivity | a5f6130 | 2008-02-20 17:57:21 +0200 | [diff] [blame] | 345 | d = (struct desc_struct *)(table_base + (selector & ~7)); |
| 346 | v = d->base0 | ((unsigned long)d->base1 << 16) | |
| 347 | ((unsigned long)d->base2 << 24); |
Carsten Otte | 5fb76f9 | 2007-10-29 16:08:51 +0100 | [diff] [blame] | 348 | #ifdef CONFIG_X86_64 |
Avi Kivity | a5f6130 | 2008-02-20 17:57:21 +0200 | [diff] [blame] | 349 | if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11)) |
| 350 | v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32; |
Carsten Otte | 5fb76f9 | 2007-10-29 16:08:51 +0100 | [diff] [blame] | 351 | #endif |
| 352 | return v; |
| 353 | } |
| 354 | EXPORT_SYMBOL_GPL(segment_base); |
| 355 | |
Carsten Otte | 6866b83 | 2007-10-29 16:09:10 +0100 | [diff] [blame] | 356 | u64 kvm_get_apic_base(struct kvm_vcpu *vcpu) |
| 357 | { |
| 358 | if (irqchip_in_kernel(vcpu->kvm)) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 359 | return vcpu->arch.apic_base; |
Carsten Otte | 6866b83 | 2007-10-29 16:09:10 +0100 | [diff] [blame] | 360 | else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 361 | return vcpu->arch.apic_base; |
Carsten Otte | 6866b83 | 2007-10-29 16:09:10 +0100 | [diff] [blame] | 362 | } |
| 363 | EXPORT_SYMBOL_GPL(kvm_get_apic_base); |
| 364 | |
| 365 | void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data) |
| 366 | { |
| 367 | /* TODO: reserve bits check */ |
| 368 | if (irqchip_in_kernel(vcpu->kvm)) |
| 369 | kvm_lapic_set_base(vcpu, data); |
| 370 | else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 371 | vcpu->arch.apic_base = data; |
Carsten Otte | 6866b83 | 2007-10-29 16:09:10 +0100 | [diff] [blame] | 372 | } |
| 373 | EXPORT_SYMBOL_GPL(kvm_set_apic_base); |
| 374 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 375 | void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr) |
| 376 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 377 | WARN_ON(vcpu->arch.exception.pending); |
| 378 | vcpu->arch.exception.pending = true; |
| 379 | vcpu->arch.exception.has_error_code = false; |
| 380 | vcpu->arch.exception.nr = nr; |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 381 | } |
| 382 | EXPORT_SYMBOL_GPL(kvm_queue_exception); |
| 383 | |
Avi Kivity | c3c91fe | 2007-11-25 14:04:58 +0200 | [diff] [blame] | 384 | void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr, |
| 385 | u32 error_code) |
| 386 | { |
| 387 | ++vcpu->stat.pf_guest; |
Joerg Roedel | 71c4dfa | 2008-02-26 16:49:16 +0100 | [diff] [blame] | 388 | if (vcpu->arch.exception.pending) { |
| 389 | if (vcpu->arch.exception.nr == PF_VECTOR) { |
| 390 | printk(KERN_DEBUG "kvm: inject_page_fault:" |
| 391 | " double fault 0x%lx\n", addr); |
| 392 | vcpu->arch.exception.nr = DF_VECTOR; |
| 393 | vcpu->arch.exception.error_code = 0; |
| 394 | } else if (vcpu->arch.exception.nr == DF_VECTOR) { |
| 395 | /* triple fault -> shutdown */ |
| 396 | set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests); |
| 397 | } |
Avi Kivity | c3c91fe | 2007-11-25 14:04:58 +0200 | [diff] [blame] | 398 | return; |
| 399 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 400 | vcpu->arch.cr2 = addr; |
Avi Kivity | c3c91fe | 2007-11-25 14:04:58 +0200 | [diff] [blame] | 401 | kvm_queue_exception_e(vcpu, PF_VECTOR, error_code); |
| 402 | } |
| 403 | |
Sheng Yang | 3419ffc | 2008-05-15 09:52:48 +0800 | [diff] [blame] | 404 | void kvm_inject_nmi(struct kvm_vcpu *vcpu) |
| 405 | { |
| 406 | vcpu->arch.nmi_pending = 1; |
| 407 | } |
| 408 | EXPORT_SYMBOL_GPL(kvm_inject_nmi); |
| 409 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 410 | void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code) |
| 411 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 412 | WARN_ON(vcpu->arch.exception.pending); |
| 413 | vcpu->arch.exception.pending = true; |
| 414 | vcpu->arch.exception.has_error_code = true; |
| 415 | vcpu->arch.exception.nr = nr; |
| 416 | vcpu->arch.exception.error_code = error_code; |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 417 | } |
| 418 | EXPORT_SYMBOL_GPL(kvm_queue_exception_e); |
| 419 | |
| 420 | static void __queue_exception(struct kvm_vcpu *vcpu) |
| 421 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 422 | kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr, |
| 423 | vcpu->arch.exception.has_error_code, |
| 424 | vcpu->arch.exception.error_code); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 425 | } |
| 426 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 427 | /* |
| 428 | * Load the pae pdptrs. Return true is they are all valid. |
| 429 | */ |
| 430 | int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 431 | { |
| 432 | gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT; |
| 433 | unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2; |
| 434 | int i; |
| 435 | int ret; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 436 | u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)]; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 437 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 438 | ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte, |
| 439 | offset * sizeof(u64), sizeof(pdpte)); |
| 440 | if (ret < 0) { |
| 441 | ret = 0; |
| 442 | goto out; |
| 443 | } |
| 444 | for (i = 0; i < ARRAY_SIZE(pdpte); ++i) { |
| 445 | if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) { |
| 446 | ret = 0; |
| 447 | goto out; |
| 448 | } |
| 449 | } |
| 450 | ret = 1; |
| 451 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 452 | memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs)); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 453 | out: |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 454 | |
| 455 | return ret; |
| 456 | } |
Joerg Roedel | cc4b687 | 2008-02-07 13:47:43 +0100 | [diff] [blame] | 457 | EXPORT_SYMBOL_GPL(load_pdptrs); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 458 | |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 459 | static bool pdptrs_changed(struct kvm_vcpu *vcpu) |
| 460 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 461 | u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)]; |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 462 | bool changed = true; |
| 463 | int r; |
| 464 | |
| 465 | if (is_long_mode(vcpu) || !is_pae(vcpu)) |
| 466 | return false; |
| 467 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 468 | r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte)); |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 469 | if (r < 0) |
| 470 | goto out; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 471 | changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0; |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 472 | out: |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 473 | |
| 474 | return changed; |
| 475 | } |
| 476 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 477 | void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 478 | { |
| 479 | if (cr0 & CR0_RESERVED_BITS) { |
| 480 | printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n", |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 481 | cr0, vcpu->arch.cr0); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 482 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 483 | return; |
| 484 | } |
| 485 | |
| 486 | if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) { |
| 487 | printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 488 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 489 | return; |
| 490 | } |
| 491 | |
| 492 | if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) { |
| 493 | printk(KERN_DEBUG "set_cr0: #GP, set PG flag " |
| 494 | "and a clear PE flag\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 495 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 496 | return; |
| 497 | } |
| 498 | |
| 499 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) { |
| 500 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 501 | if ((vcpu->arch.shadow_efer & EFER_LME)) { |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 502 | int cs_db, cs_l; |
| 503 | |
| 504 | if (!is_pae(vcpu)) { |
| 505 | printk(KERN_DEBUG "set_cr0: #GP, start paging " |
| 506 | "in long mode while PAE is disabled\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 507 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 508 | return; |
| 509 | } |
| 510 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
| 511 | if (cs_l) { |
| 512 | printk(KERN_DEBUG "set_cr0: #GP, start paging " |
| 513 | "in long mode while CS.L == 1\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 514 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 515 | return; |
| 516 | |
| 517 | } |
| 518 | } else |
| 519 | #endif |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 520 | if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) { |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 521 | printk(KERN_DEBUG "set_cr0: #GP, pdptrs " |
| 522 | "reserved bits\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 523 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 524 | return; |
| 525 | } |
| 526 | |
| 527 | } |
| 528 | |
| 529 | kvm_x86_ops->set_cr0(vcpu, cr0); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 530 | vcpu->arch.cr0 = cr0; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 531 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 532 | kvm_mmu_reset_context(vcpu); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 533 | return; |
| 534 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 535 | EXPORT_SYMBOL_GPL(kvm_set_cr0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 536 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 537 | void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 538 | { |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 539 | kvm_set_cr0(vcpu, (vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f)); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 540 | KVMTRACE_1D(LMSW, vcpu, |
| 541 | (u32)((vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f)), |
| 542 | handler); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 543 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 544 | EXPORT_SYMBOL_GPL(kvm_lmsw); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 545 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 546 | void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 547 | { |
| 548 | if (cr4 & CR4_RESERVED_BITS) { |
| 549 | printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 550 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 551 | return; |
| 552 | } |
| 553 | |
| 554 | if (is_long_mode(vcpu)) { |
| 555 | if (!(cr4 & X86_CR4_PAE)) { |
| 556 | printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while " |
| 557 | "in long mode\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 558 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 559 | return; |
| 560 | } |
| 561 | } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 562 | && !load_pdptrs(vcpu, vcpu->arch.cr3)) { |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 563 | printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 564 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 565 | return; |
| 566 | } |
| 567 | |
| 568 | if (cr4 & X86_CR4_VMXE) { |
| 569 | printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 570 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 571 | return; |
| 572 | } |
| 573 | kvm_x86_ops->set_cr4(vcpu, cr4); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 574 | vcpu->arch.cr4 = cr4; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 575 | kvm_mmu_reset_context(vcpu); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 576 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 577 | EXPORT_SYMBOL_GPL(kvm_set_cr4); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 578 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 579 | void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 580 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 581 | if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) { |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 582 | kvm_mmu_flush_tlb(vcpu); |
| 583 | return; |
| 584 | } |
| 585 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 586 | if (is_long_mode(vcpu)) { |
| 587 | if (cr3 & CR3_L_MODE_RESERVED_BITS) { |
| 588 | printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 589 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 590 | return; |
| 591 | } |
| 592 | } else { |
| 593 | if (is_pae(vcpu)) { |
| 594 | if (cr3 & CR3_PAE_RESERVED_BITS) { |
| 595 | printk(KERN_DEBUG |
| 596 | "set_cr3: #GP, reserved bits\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 597 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 598 | return; |
| 599 | } |
| 600 | if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) { |
| 601 | printk(KERN_DEBUG "set_cr3: #GP, pdptrs " |
| 602 | "reserved bits\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 603 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 604 | return; |
| 605 | } |
| 606 | } |
| 607 | /* |
| 608 | * We don't check reserved bits in nonpae mode, because |
| 609 | * this isn't enforced, and VMware depends on this. |
| 610 | */ |
| 611 | } |
| 612 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 613 | /* |
| 614 | * Does the new cr3 value map to physical memory? (Note, we |
| 615 | * catch an invalid cr3 even in real-mode, because it would |
| 616 | * cause trouble later on when we turn on paging anyway.) |
| 617 | * |
| 618 | * A real CPU would silently accept an invalid cr3 and would |
| 619 | * attempt to use it - with largely undefined (and often hard |
| 620 | * to debug) behavior on the guest side. |
| 621 | */ |
| 622 | if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT))) |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 623 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 624 | else { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 625 | vcpu->arch.cr3 = cr3; |
| 626 | vcpu->arch.mmu.new_cr3(vcpu); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 627 | } |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 628 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 629 | EXPORT_SYMBOL_GPL(kvm_set_cr3); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 630 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 631 | void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 632 | { |
| 633 | if (cr8 & CR8_RESERVED_BITS) { |
| 634 | printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 635 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 636 | return; |
| 637 | } |
| 638 | if (irqchip_in_kernel(vcpu->kvm)) |
| 639 | kvm_lapic_set_tpr(vcpu, cr8); |
| 640 | else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 641 | vcpu->arch.cr8 = cr8; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 642 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 643 | EXPORT_SYMBOL_GPL(kvm_set_cr8); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 644 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 645 | unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 646 | { |
| 647 | if (irqchip_in_kernel(vcpu->kvm)) |
| 648 | return kvm_lapic_get_cr8(vcpu); |
| 649 | else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 650 | return vcpu->arch.cr8; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 651 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 652 | EXPORT_SYMBOL_GPL(kvm_get_cr8); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 653 | |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 654 | /* |
| 655 | * List of msr numbers which we expose to userspace through KVM_GET_MSRS |
| 656 | * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST. |
| 657 | * |
| 658 | * This list is modified at module load time to reflect the |
| 659 | * capabilities of the host cpu. |
| 660 | */ |
| 661 | static u32 msrs_to_save[] = { |
| 662 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, |
| 663 | MSR_K6_STAR, |
| 664 | #ifdef CONFIG_X86_64 |
| 665 | MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR, |
| 666 | #endif |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 667 | MSR_IA32_TIME_STAMP_COUNTER, MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK, |
Alexander Graf | 847f0ad | 2008-02-21 12:11:01 +0100 | [diff] [blame] | 668 | MSR_IA32_PERF_STATUS, |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 669 | }; |
| 670 | |
| 671 | static unsigned num_msrs_to_save; |
| 672 | |
| 673 | static u32 emulated_msrs[] = { |
| 674 | MSR_IA32_MISC_ENABLE, |
| 675 | }; |
| 676 | |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 677 | static void set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 678 | { |
Joerg Roedel | f2b4b7d | 2008-01-31 14:57:37 +0100 | [diff] [blame] | 679 | if (efer & efer_reserved_bits) { |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 680 | printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n", |
| 681 | efer); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 682 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 683 | return; |
| 684 | } |
| 685 | |
| 686 | if (is_paging(vcpu) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 687 | && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) { |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 688 | printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 689 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 690 | return; |
| 691 | } |
| 692 | |
| 693 | kvm_x86_ops->set_efer(vcpu, efer); |
| 694 | |
| 695 | efer &= ~EFER_LMA; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 696 | efer |= vcpu->arch.shadow_efer & EFER_LMA; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 697 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 698 | vcpu->arch.shadow_efer = efer; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 699 | } |
| 700 | |
Joerg Roedel | f2b4b7d | 2008-01-31 14:57:37 +0100 | [diff] [blame] | 701 | void kvm_enable_efer_bits(u64 mask) |
| 702 | { |
| 703 | efer_reserved_bits &= ~mask; |
| 704 | } |
| 705 | EXPORT_SYMBOL_GPL(kvm_enable_efer_bits); |
| 706 | |
| 707 | |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 708 | /* |
| 709 | * Writes msr value into into the appropriate "register". |
| 710 | * Returns 0 on success, non-0 otherwise. |
| 711 | * Assumes vcpu_load() was already called. |
| 712 | */ |
| 713 | int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 714 | { |
| 715 | return kvm_x86_ops->set_msr(vcpu, msr_index, data); |
| 716 | } |
| 717 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 718 | /* |
| 719 | * Adapt set_msr() to msr_io()'s calling convention |
| 720 | */ |
| 721 | static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) |
| 722 | { |
| 723 | return kvm_set_msr(vcpu, index, *data); |
| 724 | } |
| 725 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 726 | static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock) |
| 727 | { |
| 728 | static int version; |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 729 | struct pvclock_wall_clock wc; |
| 730 | struct timespec now, sys, boot; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 731 | |
| 732 | if (!wall_clock) |
| 733 | return; |
| 734 | |
| 735 | version++; |
| 736 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 737 | kvm_write_guest(kvm, wall_clock, &version, sizeof(version)); |
| 738 | |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 739 | /* |
| 740 | * The guest calculates current wall clock time by adding |
| 741 | * system time (updated by kvm_write_guest_time below) to the |
| 742 | * wall clock specified here. guest system time equals host |
| 743 | * system time for us, thus we must fill in host boot time here. |
| 744 | */ |
| 745 | now = current_kernel_time(); |
| 746 | ktime_get_ts(&sys); |
| 747 | boot = ns_to_timespec(timespec_to_ns(&now) - timespec_to_ns(&sys)); |
| 748 | |
| 749 | wc.sec = boot.tv_sec; |
| 750 | wc.nsec = boot.tv_nsec; |
| 751 | wc.version = version; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 752 | |
| 753 | kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc)); |
| 754 | |
| 755 | version++; |
| 756 | kvm_write_guest(kvm, wall_clock, &version, sizeof(version)); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 757 | } |
| 758 | |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 759 | static uint32_t div_frac(uint32_t dividend, uint32_t divisor) |
| 760 | { |
| 761 | uint32_t quotient, remainder; |
| 762 | |
| 763 | /* Don't try to replace with do_div(), this one calculates |
| 764 | * "(dividend << 32) / divisor" */ |
| 765 | __asm__ ( "divl %4" |
| 766 | : "=a" (quotient), "=d" (remainder) |
| 767 | : "0" (0), "1" (dividend), "r" (divisor) ); |
| 768 | return quotient; |
| 769 | } |
| 770 | |
| 771 | static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock) |
| 772 | { |
| 773 | uint64_t nsecs = 1000000000LL; |
| 774 | int32_t shift = 0; |
| 775 | uint64_t tps64; |
| 776 | uint32_t tps32; |
| 777 | |
| 778 | tps64 = tsc_khz * 1000LL; |
| 779 | while (tps64 > nsecs*2) { |
| 780 | tps64 >>= 1; |
| 781 | shift--; |
| 782 | } |
| 783 | |
| 784 | tps32 = (uint32_t)tps64; |
| 785 | while (tps32 <= (uint32_t)nsecs) { |
| 786 | tps32 <<= 1; |
| 787 | shift++; |
| 788 | } |
| 789 | |
| 790 | hv_clock->tsc_shift = shift; |
| 791 | hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32); |
| 792 | |
| 793 | pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n", |
| 794 | __FUNCTION__, tsc_khz, hv_clock->tsc_shift, |
| 795 | hv_clock->tsc_to_system_mul); |
| 796 | } |
| 797 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 798 | static void kvm_write_guest_time(struct kvm_vcpu *v) |
| 799 | { |
| 800 | struct timespec ts; |
| 801 | unsigned long flags; |
| 802 | struct kvm_vcpu_arch *vcpu = &v->arch; |
| 803 | void *shared_kaddr; |
| 804 | |
| 805 | if ((!vcpu->time_page)) |
| 806 | return; |
| 807 | |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 808 | if (unlikely(vcpu->hv_clock_tsc_khz != tsc_khz)) { |
| 809 | kvm_set_time_scale(tsc_khz, &vcpu->hv_clock); |
| 810 | vcpu->hv_clock_tsc_khz = tsc_khz; |
| 811 | } |
| 812 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 813 | /* Keep irq disabled to prevent changes to the clock */ |
| 814 | local_irq_save(flags); |
| 815 | kvm_get_msr(v, MSR_IA32_TIME_STAMP_COUNTER, |
| 816 | &vcpu->hv_clock.tsc_timestamp); |
| 817 | ktime_get_ts(&ts); |
| 818 | local_irq_restore(flags); |
| 819 | |
| 820 | /* With all the info we got, fill in the values */ |
| 821 | |
| 822 | vcpu->hv_clock.system_time = ts.tv_nsec + |
| 823 | (NSEC_PER_SEC * (u64)ts.tv_sec); |
| 824 | /* |
| 825 | * The interface expects us to write an even number signaling that the |
| 826 | * update is finished. Since the guest won't see the intermediate |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 827 | * state, we just increase by 2 at the end. |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 828 | */ |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 829 | vcpu->hv_clock.version += 2; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 830 | |
| 831 | shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0); |
| 832 | |
| 833 | memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock, |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 834 | sizeof(vcpu->hv_clock)); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 835 | |
| 836 | kunmap_atomic(shared_kaddr, KM_USER0); |
| 837 | |
| 838 | mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT); |
| 839 | } |
| 840 | |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 841 | static bool msr_mtrr_valid(unsigned msr) |
| 842 | { |
| 843 | switch (msr) { |
| 844 | case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1: |
| 845 | case MSR_MTRRfix64K_00000: |
| 846 | case MSR_MTRRfix16K_80000: |
| 847 | case MSR_MTRRfix16K_A0000: |
| 848 | case MSR_MTRRfix4K_C0000: |
| 849 | case MSR_MTRRfix4K_C8000: |
| 850 | case MSR_MTRRfix4K_D0000: |
| 851 | case MSR_MTRRfix4K_D8000: |
| 852 | case MSR_MTRRfix4K_E0000: |
| 853 | case MSR_MTRRfix4K_E8000: |
| 854 | case MSR_MTRRfix4K_F0000: |
| 855 | case MSR_MTRRfix4K_F8000: |
| 856 | case MSR_MTRRdefType: |
| 857 | case MSR_IA32_CR_PAT: |
| 858 | return true; |
| 859 | case 0x2f8: |
| 860 | return true; |
| 861 | } |
| 862 | return false; |
| 863 | } |
| 864 | |
| 865 | static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 866 | { |
| 867 | if (!msr_mtrr_valid(msr)) |
| 868 | return 1; |
| 869 | |
| 870 | vcpu->arch.mtrr[msr - 0x200] = data; |
| 871 | return 0; |
| 872 | } |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 873 | |
| 874 | int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 875 | { |
| 876 | switch (msr) { |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 877 | case MSR_EFER: |
| 878 | set_efer(vcpu, data); |
| 879 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 880 | case MSR_IA32_MC0_STATUS: |
| 881 | pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 882 | __func__, data); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 883 | break; |
| 884 | case MSR_IA32_MCG_STATUS: |
| 885 | pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 886 | __func__, data); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 887 | break; |
Joerg Roedel | c7ac679 | 2008-02-11 20:28:27 +0100 | [diff] [blame] | 888 | case MSR_IA32_MCG_CTL: |
| 889 | pr_unimpl(vcpu, "%s: MSR_IA32_MCG_CTL 0x%llx, nop\n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 890 | __func__, data); |
Joerg Roedel | c7ac679 | 2008-02-11 20:28:27 +0100 | [diff] [blame] | 891 | break; |
Alexander Graf | b5e2fec | 2008-07-22 08:00:45 +0200 | [diff] [blame] | 892 | case MSR_IA32_DEBUGCTLMSR: |
| 893 | if (!data) { |
| 894 | /* We support the non-activated case already */ |
| 895 | break; |
| 896 | } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) { |
| 897 | /* Values other than LBR and BTF are vendor-specific, |
| 898 | thus reserved and should throw a #GP */ |
| 899 | return 1; |
| 900 | } |
| 901 | pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n", |
| 902 | __func__, data); |
| 903 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 904 | case MSR_IA32_UCODE_REV: |
| 905 | case MSR_IA32_UCODE_WRITE: |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 906 | break; |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 907 | case 0x200 ... 0x2ff: |
| 908 | return set_msr_mtrr(vcpu, msr, data); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 909 | case MSR_IA32_APICBASE: |
| 910 | kvm_set_apic_base(vcpu, data); |
| 911 | break; |
| 912 | case MSR_IA32_MISC_ENABLE: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 913 | vcpu->arch.ia32_misc_enable_msr = data; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 914 | break; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 915 | case MSR_KVM_WALL_CLOCK: |
| 916 | vcpu->kvm->arch.wall_clock = data; |
| 917 | kvm_write_wall_clock(vcpu->kvm, data); |
| 918 | break; |
| 919 | case MSR_KVM_SYSTEM_TIME: { |
| 920 | if (vcpu->arch.time_page) { |
| 921 | kvm_release_page_dirty(vcpu->arch.time_page); |
| 922 | vcpu->arch.time_page = NULL; |
| 923 | } |
| 924 | |
| 925 | vcpu->arch.time = data; |
| 926 | |
| 927 | /* we verify if the enable bit is set... */ |
| 928 | if (!(data & 1)) |
| 929 | break; |
| 930 | |
| 931 | /* ...but clean it before doing the actual write */ |
| 932 | vcpu->arch.time_offset = data & ~(PAGE_MASK | 1); |
| 933 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 934 | down_read(¤t->mm->mmap_sem); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 935 | vcpu->arch.time_page = |
| 936 | gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 937 | up_read(¤t->mm->mmap_sem); |
| 938 | |
| 939 | if (is_error_page(vcpu->arch.time_page)) { |
| 940 | kvm_release_page_clean(vcpu->arch.time_page); |
| 941 | vcpu->arch.time_page = NULL; |
| 942 | } |
| 943 | |
| 944 | kvm_write_guest_time(vcpu); |
| 945 | break; |
| 946 | } |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 947 | default: |
Avi Kivity | 565f1fb | 2007-12-19 12:02:40 +0200 | [diff] [blame] | 948 | pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n", msr, data); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 949 | return 1; |
| 950 | } |
| 951 | return 0; |
| 952 | } |
| 953 | EXPORT_SYMBOL_GPL(kvm_set_msr_common); |
| 954 | |
| 955 | |
| 956 | /* |
| 957 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 958 | * Returns 0 on success, non-0 otherwise. |
| 959 | * Assumes vcpu_load() was already called. |
| 960 | */ |
| 961 | int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
| 962 | { |
| 963 | return kvm_x86_ops->get_msr(vcpu, msr_index, pdata); |
| 964 | } |
| 965 | |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 966 | static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 967 | { |
| 968 | if (!msr_mtrr_valid(msr)) |
| 969 | return 1; |
| 970 | |
| 971 | *pdata = vcpu->arch.mtrr[msr - 0x200]; |
| 972 | return 0; |
| 973 | } |
| 974 | |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 975 | int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 976 | { |
| 977 | u64 data; |
| 978 | |
| 979 | switch (msr) { |
| 980 | case 0xc0010010: /* SYSCFG */ |
| 981 | case 0xc0010015: /* HWCR */ |
| 982 | case MSR_IA32_PLATFORM_ID: |
| 983 | case MSR_IA32_P5_MC_ADDR: |
| 984 | case MSR_IA32_P5_MC_TYPE: |
| 985 | case MSR_IA32_MC0_CTL: |
| 986 | case MSR_IA32_MCG_STATUS: |
| 987 | case MSR_IA32_MCG_CAP: |
Joerg Roedel | c7ac679 | 2008-02-11 20:28:27 +0100 | [diff] [blame] | 988 | case MSR_IA32_MCG_CTL: |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 989 | case MSR_IA32_MC0_MISC: |
| 990 | case MSR_IA32_MC0_MISC+4: |
| 991 | case MSR_IA32_MC0_MISC+8: |
| 992 | case MSR_IA32_MC0_MISC+12: |
| 993 | case MSR_IA32_MC0_MISC+16: |
| 994 | case MSR_IA32_UCODE_REV: |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 995 | case MSR_IA32_EBL_CR_POWERON: |
Alexander Graf | b5e2fec | 2008-07-22 08:00:45 +0200 | [diff] [blame] | 996 | case MSR_IA32_DEBUGCTLMSR: |
| 997 | case MSR_IA32_LASTBRANCHFROMIP: |
| 998 | case MSR_IA32_LASTBRANCHTOIP: |
| 999 | case MSR_IA32_LASTINTFROMIP: |
| 1000 | case MSR_IA32_LASTINTTOIP: |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1001 | data = 0; |
| 1002 | break; |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 1003 | case MSR_MTRRcap: |
| 1004 | data = 0x500 | KVM_NR_VAR_MTRR; |
| 1005 | break; |
| 1006 | case 0x200 ... 0x2ff: |
| 1007 | return get_msr_mtrr(vcpu, msr, pdata); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1008 | case 0xcd: /* fsb frequency */ |
| 1009 | data = 3; |
| 1010 | break; |
| 1011 | case MSR_IA32_APICBASE: |
| 1012 | data = kvm_get_apic_base(vcpu); |
| 1013 | break; |
| 1014 | case MSR_IA32_MISC_ENABLE: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1015 | data = vcpu->arch.ia32_misc_enable_msr; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1016 | break; |
Alexander Graf | 847f0ad | 2008-02-21 12:11:01 +0100 | [diff] [blame] | 1017 | case MSR_IA32_PERF_STATUS: |
| 1018 | /* TSC increment by tick */ |
| 1019 | data = 1000ULL; |
| 1020 | /* CPU multiplier */ |
| 1021 | data |= (((uint64_t)4ULL) << 40); |
| 1022 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1023 | case MSR_EFER: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1024 | data = vcpu->arch.shadow_efer; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1025 | break; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1026 | case MSR_KVM_WALL_CLOCK: |
| 1027 | data = vcpu->kvm->arch.wall_clock; |
| 1028 | break; |
| 1029 | case MSR_KVM_SYSTEM_TIME: |
| 1030 | data = vcpu->arch.time; |
| 1031 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1032 | default: |
| 1033 | pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr); |
| 1034 | return 1; |
| 1035 | } |
| 1036 | *pdata = data; |
| 1037 | return 0; |
| 1038 | } |
| 1039 | EXPORT_SYMBOL_GPL(kvm_get_msr_common); |
| 1040 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1041 | /* |
| 1042 | * Read or write a bunch of msrs. All parameters are kernel addresses. |
| 1043 | * |
| 1044 | * @return number of msrs set successfully. |
| 1045 | */ |
| 1046 | static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs, |
| 1047 | struct kvm_msr_entry *entries, |
| 1048 | int (*do_msr)(struct kvm_vcpu *vcpu, |
| 1049 | unsigned index, u64 *data)) |
| 1050 | { |
| 1051 | int i; |
| 1052 | |
| 1053 | vcpu_load(vcpu); |
| 1054 | |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 1055 | down_read(&vcpu->kvm->slots_lock); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1056 | for (i = 0; i < msrs->nmsrs; ++i) |
| 1057 | if (do_msr(vcpu, entries[i].index, &entries[i].data)) |
| 1058 | break; |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 1059 | up_read(&vcpu->kvm->slots_lock); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1060 | |
| 1061 | vcpu_put(vcpu); |
| 1062 | |
| 1063 | return i; |
| 1064 | } |
| 1065 | |
| 1066 | /* |
| 1067 | * Read or write a bunch of msrs. Parameters are user addresses. |
| 1068 | * |
| 1069 | * @return number of msrs set successfully. |
| 1070 | */ |
| 1071 | static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs, |
| 1072 | int (*do_msr)(struct kvm_vcpu *vcpu, |
| 1073 | unsigned index, u64 *data), |
| 1074 | int writeback) |
| 1075 | { |
| 1076 | struct kvm_msrs msrs; |
| 1077 | struct kvm_msr_entry *entries; |
| 1078 | int r, n; |
| 1079 | unsigned size; |
| 1080 | |
| 1081 | r = -EFAULT; |
| 1082 | if (copy_from_user(&msrs, user_msrs, sizeof msrs)) |
| 1083 | goto out; |
| 1084 | |
| 1085 | r = -E2BIG; |
| 1086 | if (msrs.nmsrs >= MAX_IO_MSRS) |
| 1087 | goto out; |
| 1088 | |
| 1089 | r = -ENOMEM; |
| 1090 | size = sizeof(struct kvm_msr_entry) * msrs.nmsrs; |
| 1091 | entries = vmalloc(size); |
| 1092 | if (!entries) |
| 1093 | goto out; |
| 1094 | |
| 1095 | r = -EFAULT; |
| 1096 | if (copy_from_user(entries, user_msrs->entries, size)) |
| 1097 | goto out_free; |
| 1098 | |
| 1099 | r = n = __msr_io(vcpu, &msrs, entries, do_msr); |
| 1100 | if (r < 0) |
| 1101 | goto out_free; |
| 1102 | |
| 1103 | r = -EFAULT; |
| 1104 | if (writeback && copy_to_user(user_msrs->entries, entries, size)) |
| 1105 | goto out_free; |
| 1106 | |
| 1107 | r = n; |
| 1108 | |
| 1109 | out_free: |
| 1110 | vfree(entries); |
| 1111 | out: |
| 1112 | return r; |
| 1113 | } |
| 1114 | |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 1115 | int kvm_dev_ioctl_check_extension(long ext) |
| 1116 | { |
| 1117 | int r; |
| 1118 | |
| 1119 | switch (ext) { |
| 1120 | case KVM_CAP_IRQCHIP: |
| 1121 | case KVM_CAP_HLT: |
| 1122 | case KVM_CAP_MMU_SHADOW_CACHE_CONTROL: |
| 1123 | case KVM_CAP_USER_MEMORY: |
| 1124 | case KVM_CAP_SET_TSS_ADDR: |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1125 | case KVM_CAP_EXT_CPUID: |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1126 | case KVM_CAP_CLOCKSOURCE: |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 1127 | case KVM_CAP_PIT: |
Marcelo Tosatti | a28e4f5 | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 1128 | case KVM_CAP_NOP_IO_DELAY: |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 1129 | case KVM_CAP_MP_STATE: |
Avi Kivity | ed84862 | 2008-07-29 11:30:57 +0300 | [diff] [blame] | 1130 | case KVM_CAP_SYNC_MMU: |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 1131 | r = 1; |
| 1132 | break; |
Laurent Vivier | 542472b | 2008-05-30 16:05:55 +0200 | [diff] [blame] | 1133 | case KVM_CAP_COALESCED_MMIO: |
| 1134 | r = KVM_COALESCED_MMIO_PAGE_OFFSET; |
| 1135 | break; |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 1136 | case KVM_CAP_VAPIC: |
| 1137 | r = !kvm_x86_ops->cpu_has_accelerated_tpr(); |
| 1138 | break; |
Avi Kivity | f725230 | 2008-02-20 11:53:16 +0200 | [diff] [blame] | 1139 | case KVM_CAP_NR_VCPUS: |
| 1140 | r = KVM_MAX_VCPUS; |
| 1141 | break; |
Avi Kivity | a988b91 | 2008-02-20 11:59:20 +0200 | [diff] [blame] | 1142 | case KVM_CAP_NR_MEMSLOTS: |
| 1143 | r = KVM_MEMORY_SLOTS; |
| 1144 | break; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 1145 | case KVM_CAP_PV_MMU: |
| 1146 | r = !tdp_enabled; |
| 1147 | break; |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 1148 | default: |
| 1149 | r = 0; |
| 1150 | break; |
| 1151 | } |
| 1152 | return r; |
| 1153 | |
| 1154 | } |
| 1155 | |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1156 | long kvm_arch_dev_ioctl(struct file *filp, |
| 1157 | unsigned int ioctl, unsigned long arg) |
| 1158 | { |
| 1159 | void __user *argp = (void __user *)arg; |
| 1160 | long r; |
| 1161 | |
| 1162 | switch (ioctl) { |
| 1163 | case KVM_GET_MSR_INDEX_LIST: { |
| 1164 | struct kvm_msr_list __user *user_msr_list = argp; |
| 1165 | struct kvm_msr_list msr_list; |
| 1166 | unsigned n; |
| 1167 | |
| 1168 | r = -EFAULT; |
| 1169 | if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list)) |
| 1170 | goto out; |
| 1171 | n = msr_list.nmsrs; |
| 1172 | msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs); |
| 1173 | if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list)) |
| 1174 | goto out; |
| 1175 | r = -E2BIG; |
| 1176 | if (n < num_msrs_to_save) |
| 1177 | goto out; |
| 1178 | r = -EFAULT; |
| 1179 | if (copy_to_user(user_msr_list->indices, &msrs_to_save, |
| 1180 | num_msrs_to_save * sizeof(u32))) |
| 1181 | goto out; |
| 1182 | if (copy_to_user(user_msr_list->indices |
| 1183 | + num_msrs_to_save * sizeof(u32), |
| 1184 | &emulated_msrs, |
| 1185 | ARRAY_SIZE(emulated_msrs) * sizeof(u32))) |
| 1186 | goto out; |
| 1187 | r = 0; |
| 1188 | break; |
| 1189 | } |
Avi Kivity | 674eea0 | 2008-02-11 18:37:23 +0200 | [diff] [blame] | 1190 | case KVM_GET_SUPPORTED_CPUID: { |
| 1191 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
| 1192 | struct kvm_cpuid2 cpuid; |
| 1193 | |
| 1194 | r = -EFAULT; |
| 1195 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) |
| 1196 | goto out; |
| 1197 | r = kvm_dev_ioctl_get_supported_cpuid(&cpuid, |
| 1198 | cpuid_arg->entries); |
| 1199 | if (r) |
| 1200 | goto out; |
| 1201 | |
| 1202 | r = -EFAULT; |
| 1203 | if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid)) |
| 1204 | goto out; |
| 1205 | r = 0; |
| 1206 | break; |
| 1207 | } |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1208 | default: |
| 1209 | r = -EINVAL; |
| 1210 | } |
| 1211 | out: |
| 1212 | return r; |
| 1213 | } |
| 1214 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1215 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 1216 | { |
| 1217 | kvm_x86_ops->vcpu_load(vcpu, cpu); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1218 | kvm_write_guest_time(vcpu); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) |
| 1222 | { |
| 1223 | kvm_x86_ops->vcpu_put(vcpu); |
Amit Shah | 9327fd1 | 2007-11-15 18:38:46 +0200 | [diff] [blame] | 1224 | kvm_put_guest_fpu(vcpu); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1225 | } |
| 1226 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1227 | static int is_efer_nx(void) |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1228 | { |
| 1229 | u64 efer; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1230 | |
| 1231 | rdmsrl(MSR_EFER, efer); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1232 | return efer & EFER_NX; |
| 1233 | } |
| 1234 | |
| 1235 | static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu) |
| 1236 | { |
| 1237 | int i; |
| 1238 | struct kvm_cpuid_entry2 *e, *entry; |
| 1239 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1240 | entry = NULL; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1241 | for (i = 0; i < vcpu->arch.cpuid_nent; ++i) { |
| 1242 | e = &vcpu->arch.cpuid_entries[i]; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1243 | if (e->function == 0x80000001) { |
| 1244 | entry = e; |
| 1245 | break; |
| 1246 | } |
| 1247 | } |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1248 | if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) { |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1249 | entry->edx &= ~(1 << 20); |
| 1250 | printk(KERN_INFO "kvm: guest NX capability removed\n"); |
| 1251 | } |
| 1252 | } |
| 1253 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1254 | /* when an old userspace process fills a new kernel module */ |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1255 | static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu, |
| 1256 | struct kvm_cpuid *cpuid, |
| 1257 | struct kvm_cpuid_entry __user *entries) |
| 1258 | { |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1259 | int r, i; |
| 1260 | struct kvm_cpuid_entry *cpuid_entries; |
| 1261 | |
| 1262 | r = -E2BIG; |
| 1263 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) |
| 1264 | goto out; |
| 1265 | r = -ENOMEM; |
| 1266 | cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent); |
| 1267 | if (!cpuid_entries) |
| 1268 | goto out; |
| 1269 | r = -EFAULT; |
| 1270 | if (copy_from_user(cpuid_entries, entries, |
| 1271 | cpuid->nent * sizeof(struct kvm_cpuid_entry))) |
| 1272 | goto out_free; |
| 1273 | for (i = 0; i < cpuid->nent; i++) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1274 | vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function; |
| 1275 | vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax; |
| 1276 | vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx; |
| 1277 | vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx; |
| 1278 | vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx; |
| 1279 | vcpu->arch.cpuid_entries[i].index = 0; |
| 1280 | vcpu->arch.cpuid_entries[i].flags = 0; |
| 1281 | vcpu->arch.cpuid_entries[i].padding[0] = 0; |
| 1282 | vcpu->arch.cpuid_entries[i].padding[1] = 0; |
| 1283 | vcpu->arch.cpuid_entries[i].padding[2] = 0; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1284 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1285 | vcpu->arch.cpuid_nent = cpuid->nent; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1286 | cpuid_fix_nx_cap(vcpu); |
| 1287 | r = 0; |
| 1288 | |
| 1289 | out_free: |
| 1290 | vfree(cpuid_entries); |
| 1291 | out: |
| 1292 | return r; |
| 1293 | } |
| 1294 | |
| 1295 | static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu, |
| 1296 | struct kvm_cpuid2 *cpuid, |
| 1297 | struct kvm_cpuid_entry2 __user *entries) |
| 1298 | { |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1299 | int r; |
| 1300 | |
| 1301 | r = -E2BIG; |
| 1302 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) |
| 1303 | goto out; |
| 1304 | r = -EFAULT; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1305 | if (copy_from_user(&vcpu->arch.cpuid_entries, entries, |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1306 | cpuid->nent * sizeof(struct kvm_cpuid_entry2))) |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1307 | goto out; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1308 | vcpu->arch.cpuid_nent = cpuid->nent; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1309 | return 0; |
| 1310 | |
| 1311 | out: |
| 1312 | return r; |
| 1313 | } |
| 1314 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1315 | static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu, |
| 1316 | struct kvm_cpuid2 *cpuid, |
| 1317 | struct kvm_cpuid_entry2 __user *entries) |
| 1318 | { |
| 1319 | int r; |
| 1320 | |
| 1321 | r = -E2BIG; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1322 | if (cpuid->nent < vcpu->arch.cpuid_nent) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1323 | goto out; |
| 1324 | r = -EFAULT; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1325 | if (copy_to_user(entries, &vcpu->arch.cpuid_entries, |
| 1326 | vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2))) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1327 | goto out; |
| 1328 | return 0; |
| 1329 | |
| 1330 | out: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1331 | cpuid->nent = vcpu->arch.cpuid_nent; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1332 | return r; |
| 1333 | } |
| 1334 | |
| 1335 | static inline u32 bit(int bitno) |
| 1336 | { |
| 1337 | return 1 << (bitno & 31); |
| 1338 | } |
| 1339 | |
| 1340 | static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function, |
| 1341 | u32 index) |
| 1342 | { |
| 1343 | entry->function = function; |
| 1344 | entry->index = index; |
| 1345 | cpuid_count(entry->function, entry->index, |
| 1346 | &entry->eax, &entry->ebx, &entry->ecx, &entry->edx); |
| 1347 | entry->flags = 0; |
| 1348 | } |
| 1349 | |
| 1350 | static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function, |
| 1351 | u32 index, int *nent, int maxnent) |
| 1352 | { |
| 1353 | const u32 kvm_supported_word0_x86_features = bit(X86_FEATURE_FPU) | |
| 1354 | bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) | |
| 1355 | bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) | |
| 1356 | bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) | |
| 1357 | bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) | |
| 1358 | bit(X86_FEATURE_SEP) | bit(X86_FEATURE_PGE) | |
| 1359 | bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) | |
| 1360 | bit(X86_FEATURE_CLFLSH) | bit(X86_FEATURE_MMX) | |
| 1361 | bit(X86_FEATURE_FXSR) | bit(X86_FEATURE_XMM) | |
| 1362 | bit(X86_FEATURE_XMM2) | bit(X86_FEATURE_SELFSNOOP); |
| 1363 | const u32 kvm_supported_word1_x86_features = bit(X86_FEATURE_FPU) | |
| 1364 | bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) | |
| 1365 | bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) | |
| 1366 | bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) | |
| 1367 | bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) | |
| 1368 | bit(X86_FEATURE_PGE) | |
| 1369 | bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) | |
| 1370 | bit(X86_FEATURE_MMX) | bit(X86_FEATURE_FXSR) | |
| 1371 | bit(X86_FEATURE_SYSCALL) | |
| 1372 | (bit(X86_FEATURE_NX) && is_efer_nx()) | |
| 1373 | #ifdef CONFIG_X86_64 |
| 1374 | bit(X86_FEATURE_LM) | |
| 1375 | #endif |
| 1376 | bit(X86_FEATURE_MMXEXT) | |
| 1377 | bit(X86_FEATURE_3DNOWEXT) | |
| 1378 | bit(X86_FEATURE_3DNOW); |
| 1379 | const u32 kvm_supported_word3_x86_features = |
| 1380 | bit(X86_FEATURE_XMM3) | bit(X86_FEATURE_CX16); |
| 1381 | const u32 kvm_supported_word6_x86_features = |
| 1382 | bit(X86_FEATURE_LAHF_LM) | bit(X86_FEATURE_CMP_LEGACY); |
| 1383 | |
| 1384 | /* all func 2 cpuid_count() should be called on the same cpu */ |
| 1385 | get_cpu(); |
| 1386 | do_cpuid_1_ent(entry, function, index); |
| 1387 | ++*nent; |
| 1388 | |
| 1389 | switch (function) { |
| 1390 | case 0: |
| 1391 | entry->eax = min(entry->eax, (u32)0xb); |
| 1392 | break; |
| 1393 | case 1: |
| 1394 | entry->edx &= kvm_supported_word0_x86_features; |
| 1395 | entry->ecx &= kvm_supported_word3_x86_features; |
| 1396 | break; |
| 1397 | /* function 2 entries are STATEFUL. That is, repeated cpuid commands |
| 1398 | * may return different values. This forces us to get_cpu() before |
| 1399 | * issuing the first command, and also to emulate this annoying behavior |
| 1400 | * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */ |
| 1401 | case 2: { |
| 1402 | int t, times = entry->eax & 0xff; |
| 1403 | |
| 1404 | entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC; |
| 1405 | for (t = 1; t < times && *nent < maxnent; ++t) { |
| 1406 | do_cpuid_1_ent(&entry[t], function, 0); |
| 1407 | entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC; |
| 1408 | ++*nent; |
| 1409 | } |
| 1410 | break; |
| 1411 | } |
| 1412 | /* function 4 and 0xb have additional index. */ |
| 1413 | case 4: { |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1414 | int i, cache_type; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1415 | |
| 1416 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
| 1417 | /* read more entries until cache_type is zero */ |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1418 | for (i = 1; *nent < maxnent; ++i) { |
| 1419 | cache_type = entry[i - 1].eax & 0x1f; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1420 | if (!cache_type) |
| 1421 | break; |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1422 | do_cpuid_1_ent(&entry[i], function, i); |
| 1423 | entry[i].flags |= |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1424 | KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
| 1425 | ++*nent; |
| 1426 | } |
| 1427 | break; |
| 1428 | } |
| 1429 | case 0xb: { |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1430 | int i, level_type; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1431 | |
| 1432 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
| 1433 | /* read more entries until level_type is zero */ |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1434 | for (i = 1; *nent < maxnent; ++i) { |
| 1435 | level_type = entry[i - 1].ecx & 0xff; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1436 | if (!level_type) |
| 1437 | break; |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1438 | do_cpuid_1_ent(&entry[i], function, i); |
| 1439 | entry[i].flags |= |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1440 | KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
| 1441 | ++*nent; |
| 1442 | } |
| 1443 | break; |
| 1444 | } |
| 1445 | case 0x80000000: |
| 1446 | entry->eax = min(entry->eax, 0x8000001a); |
| 1447 | break; |
| 1448 | case 0x80000001: |
| 1449 | entry->edx &= kvm_supported_word1_x86_features; |
| 1450 | entry->ecx &= kvm_supported_word6_x86_features; |
| 1451 | break; |
| 1452 | } |
| 1453 | put_cpu(); |
| 1454 | } |
| 1455 | |
Avi Kivity | 674eea0 | 2008-02-11 18:37:23 +0200 | [diff] [blame] | 1456 | static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid, |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1457 | struct kvm_cpuid_entry2 __user *entries) |
| 1458 | { |
| 1459 | struct kvm_cpuid_entry2 *cpuid_entries; |
| 1460 | int limit, nent = 0, r = -E2BIG; |
| 1461 | u32 func; |
| 1462 | |
| 1463 | if (cpuid->nent < 1) |
| 1464 | goto out; |
| 1465 | r = -ENOMEM; |
| 1466 | cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent); |
| 1467 | if (!cpuid_entries) |
| 1468 | goto out; |
| 1469 | |
| 1470 | do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent); |
| 1471 | limit = cpuid_entries[0].eax; |
| 1472 | for (func = 1; func <= limit && nent < cpuid->nent; ++func) |
| 1473 | do_cpuid_ent(&cpuid_entries[nent], func, 0, |
| 1474 | &nent, cpuid->nent); |
| 1475 | r = -E2BIG; |
| 1476 | if (nent >= cpuid->nent) |
| 1477 | goto out_free; |
| 1478 | |
| 1479 | do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent); |
| 1480 | limit = cpuid_entries[nent - 1].eax; |
| 1481 | for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func) |
| 1482 | do_cpuid_ent(&cpuid_entries[nent], func, 0, |
| 1483 | &nent, cpuid->nent); |
| 1484 | r = -EFAULT; |
| 1485 | if (copy_to_user(entries, cpuid_entries, |
| 1486 | nent * sizeof(struct kvm_cpuid_entry2))) |
| 1487 | goto out_free; |
| 1488 | cpuid->nent = nent; |
| 1489 | r = 0; |
| 1490 | |
| 1491 | out_free: |
| 1492 | vfree(cpuid_entries); |
| 1493 | out: |
| 1494 | return r; |
| 1495 | } |
| 1496 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1497 | static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu, |
| 1498 | struct kvm_lapic_state *s) |
| 1499 | { |
| 1500 | vcpu_load(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1501 | memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1502 | vcpu_put(vcpu); |
| 1503 | |
| 1504 | return 0; |
| 1505 | } |
| 1506 | |
| 1507 | static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu, |
| 1508 | struct kvm_lapic_state *s) |
| 1509 | { |
| 1510 | vcpu_load(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1511 | memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1512 | kvm_apic_post_state_restore(vcpu); |
| 1513 | vcpu_put(vcpu); |
| 1514 | |
| 1515 | return 0; |
| 1516 | } |
| 1517 | |
Zhang Xiantao | f77bc6a | 2007-11-21 04:36:41 +0800 | [diff] [blame] | 1518 | static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, |
| 1519 | struct kvm_interrupt *irq) |
| 1520 | { |
| 1521 | if (irq->irq < 0 || irq->irq >= 256) |
| 1522 | return -EINVAL; |
| 1523 | if (irqchip_in_kernel(vcpu->kvm)) |
| 1524 | return -ENXIO; |
| 1525 | vcpu_load(vcpu); |
| 1526 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1527 | set_bit(irq->irq, vcpu->arch.irq_pending); |
| 1528 | set_bit(irq->irq / BITS_PER_LONG, &vcpu->arch.irq_summary); |
Zhang Xiantao | f77bc6a | 2007-11-21 04:36:41 +0800 | [diff] [blame] | 1529 | |
| 1530 | vcpu_put(vcpu); |
| 1531 | |
| 1532 | return 0; |
| 1533 | } |
| 1534 | |
Avi Kivity | b209749f | 2007-10-22 16:50:39 +0200 | [diff] [blame] | 1535 | static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu, |
| 1536 | struct kvm_tpr_access_ctl *tac) |
| 1537 | { |
| 1538 | if (tac->flags) |
| 1539 | return -EINVAL; |
| 1540 | vcpu->arch.tpr_access_reporting = !!tac->enabled; |
| 1541 | return 0; |
| 1542 | } |
| 1543 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1544 | long kvm_arch_vcpu_ioctl(struct file *filp, |
| 1545 | unsigned int ioctl, unsigned long arg) |
| 1546 | { |
| 1547 | struct kvm_vcpu *vcpu = filp->private_data; |
| 1548 | void __user *argp = (void __user *)arg; |
| 1549 | int r; |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 1550 | struct kvm_lapic_state *lapic = NULL; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1551 | |
| 1552 | switch (ioctl) { |
| 1553 | case KVM_GET_LAPIC: { |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 1554 | lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1555 | |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 1556 | r = -ENOMEM; |
| 1557 | if (!lapic) |
| 1558 | goto out; |
| 1559 | r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1560 | if (r) |
| 1561 | goto out; |
| 1562 | r = -EFAULT; |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 1563 | if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state))) |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1564 | goto out; |
| 1565 | r = 0; |
| 1566 | break; |
| 1567 | } |
| 1568 | case KVM_SET_LAPIC: { |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 1569 | lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL); |
| 1570 | r = -ENOMEM; |
| 1571 | if (!lapic) |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1572 | goto out; |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 1573 | r = -EFAULT; |
| 1574 | if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state))) |
| 1575 | goto out; |
| 1576 | r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1577 | if (r) |
| 1578 | goto out; |
| 1579 | r = 0; |
| 1580 | break; |
| 1581 | } |
Zhang Xiantao | f77bc6a | 2007-11-21 04:36:41 +0800 | [diff] [blame] | 1582 | case KVM_INTERRUPT: { |
| 1583 | struct kvm_interrupt irq; |
| 1584 | |
| 1585 | r = -EFAULT; |
| 1586 | if (copy_from_user(&irq, argp, sizeof irq)) |
| 1587 | goto out; |
| 1588 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); |
| 1589 | if (r) |
| 1590 | goto out; |
| 1591 | r = 0; |
| 1592 | break; |
| 1593 | } |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1594 | case KVM_SET_CPUID: { |
| 1595 | struct kvm_cpuid __user *cpuid_arg = argp; |
| 1596 | struct kvm_cpuid cpuid; |
| 1597 | |
| 1598 | r = -EFAULT; |
| 1599 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) |
| 1600 | goto out; |
| 1601 | r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries); |
| 1602 | if (r) |
| 1603 | goto out; |
| 1604 | break; |
| 1605 | } |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1606 | case KVM_SET_CPUID2: { |
| 1607 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
| 1608 | struct kvm_cpuid2 cpuid; |
| 1609 | |
| 1610 | r = -EFAULT; |
| 1611 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) |
| 1612 | goto out; |
| 1613 | r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid, |
| 1614 | cpuid_arg->entries); |
| 1615 | if (r) |
| 1616 | goto out; |
| 1617 | break; |
| 1618 | } |
| 1619 | case KVM_GET_CPUID2: { |
| 1620 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
| 1621 | struct kvm_cpuid2 cpuid; |
| 1622 | |
| 1623 | r = -EFAULT; |
| 1624 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) |
| 1625 | goto out; |
| 1626 | r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid, |
| 1627 | cpuid_arg->entries); |
| 1628 | if (r) |
| 1629 | goto out; |
| 1630 | r = -EFAULT; |
| 1631 | if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid)) |
| 1632 | goto out; |
| 1633 | r = 0; |
| 1634 | break; |
| 1635 | } |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1636 | case KVM_GET_MSRS: |
| 1637 | r = msr_io(vcpu, argp, kvm_get_msr, 1); |
| 1638 | break; |
| 1639 | case KVM_SET_MSRS: |
| 1640 | r = msr_io(vcpu, argp, do_set_msr, 0); |
| 1641 | break; |
Avi Kivity | b209749f | 2007-10-22 16:50:39 +0200 | [diff] [blame] | 1642 | case KVM_TPR_ACCESS_REPORTING: { |
| 1643 | struct kvm_tpr_access_ctl tac; |
| 1644 | |
| 1645 | r = -EFAULT; |
| 1646 | if (copy_from_user(&tac, argp, sizeof tac)) |
| 1647 | goto out; |
| 1648 | r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac); |
| 1649 | if (r) |
| 1650 | goto out; |
| 1651 | r = -EFAULT; |
| 1652 | if (copy_to_user(argp, &tac, sizeof tac)) |
| 1653 | goto out; |
| 1654 | r = 0; |
| 1655 | break; |
| 1656 | }; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 1657 | case KVM_SET_VAPIC_ADDR: { |
| 1658 | struct kvm_vapic_addr va; |
| 1659 | |
| 1660 | r = -EINVAL; |
| 1661 | if (!irqchip_in_kernel(vcpu->kvm)) |
| 1662 | goto out; |
| 1663 | r = -EFAULT; |
| 1664 | if (copy_from_user(&va, argp, sizeof va)) |
| 1665 | goto out; |
| 1666 | r = 0; |
| 1667 | kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr); |
| 1668 | break; |
| 1669 | } |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1670 | default: |
| 1671 | r = -EINVAL; |
| 1672 | } |
| 1673 | out: |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 1674 | if (lapic) |
| 1675 | kfree(lapic); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1676 | return r; |
| 1677 | } |
| 1678 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1679 | static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr) |
| 1680 | { |
| 1681 | int ret; |
| 1682 | |
| 1683 | if (addr > (unsigned int)(-3 * PAGE_SIZE)) |
| 1684 | return -1; |
| 1685 | ret = kvm_x86_ops->set_tss_addr(kvm, addr); |
| 1686 | return ret; |
| 1687 | } |
| 1688 | |
| 1689 | static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm, |
| 1690 | u32 kvm_nr_mmu_pages) |
| 1691 | { |
| 1692 | if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES) |
| 1693 | return -EINVAL; |
| 1694 | |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 1695 | down_write(&kvm->slots_lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1696 | |
| 1697 | kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages); |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 1698 | kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1699 | |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 1700 | up_write(&kvm->slots_lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1701 | return 0; |
| 1702 | } |
| 1703 | |
| 1704 | static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm) |
| 1705 | { |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 1706 | return kvm->arch.n_alloc_mmu_pages; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1707 | } |
| 1708 | |
Zhang Xiantao | e9f85cd | 2007-11-22 11:20:33 +0800 | [diff] [blame] | 1709 | gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn) |
| 1710 | { |
| 1711 | int i; |
| 1712 | struct kvm_mem_alias *alias; |
| 1713 | |
Zhang Xiantao | d69fb81 | 2007-12-14 09:54:20 +0800 | [diff] [blame] | 1714 | for (i = 0; i < kvm->arch.naliases; ++i) { |
| 1715 | alias = &kvm->arch.aliases[i]; |
Zhang Xiantao | e9f85cd | 2007-11-22 11:20:33 +0800 | [diff] [blame] | 1716 | if (gfn >= alias->base_gfn |
| 1717 | && gfn < alias->base_gfn + alias->npages) |
| 1718 | return alias->target_gfn + gfn - alias->base_gfn; |
| 1719 | } |
| 1720 | return gfn; |
| 1721 | } |
| 1722 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1723 | /* |
| 1724 | * Set a new alias region. Aliases map a portion of physical memory into |
| 1725 | * another portion. This is useful for memory windows, for example the PC |
| 1726 | * VGA region. |
| 1727 | */ |
| 1728 | static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm, |
| 1729 | struct kvm_memory_alias *alias) |
| 1730 | { |
| 1731 | int r, n; |
| 1732 | struct kvm_mem_alias *p; |
| 1733 | |
| 1734 | r = -EINVAL; |
| 1735 | /* General sanity checks */ |
| 1736 | if (alias->memory_size & (PAGE_SIZE - 1)) |
| 1737 | goto out; |
| 1738 | if (alias->guest_phys_addr & (PAGE_SIZE - 1)) |
| 1739 | goto out; |
| 1740 | if (alias->slot >= KVM_ALIAS_SLOTS) |
| 1741 | goto out; |
| 1742 | if (alias->guest_phys_addr + alias->memory_size |
| 1743 | < alias->guest_phys_addr) |
| 1744 | goto out; |
| 1745 | if (alias->target_phys_addr + alias->memory_size |
| 1746 | < alias->target_phys_addr) |
| 1747 | goto out; |
| 1748 | |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 1749 | down_write(&kvm->slots_lock); |
Andrea Arcangeli | a1708ce | 2008-07-25 16:26:39 +0200 | [diff] [blame] | 1750 | spin_lock(&kvm->mmu_lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1751 | |
Zhang Xiantao | d69fb81 | 2007-12-14 09:54:20 +0800 | [diff] [blame] | 1752 | p = &kvm->arch.aliases[alias->slot]; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1753 | p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT; |
| 1754 | p->npages = alias->memory_size >> PAGE_SHIFT; |
| 1755 | p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT; |
| 1756 | |
| 1757 | for (n = KVM_ALIAS_SLOTS; n > 0; --n) |
Zhang Xiantao | d69fb81 | 2007-12-14 09:54:20 +0800 | [diff] [blame] | 1758 | if (kvm->arch.aliases[n - 1].npages) |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1759 | break; |
Zhang Xiantao | d69fb81 | 2007-12-14 09:54:20 +0800 | [diff] [blame] | 1760 | kvm->arch.naliases = n; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1761 | |
Andrea Arcangeli | a1708ce | 2008-07-25 16:26:39 +0200 | [diff] [blame] | 1762 | spin_unlock(&kvm->mmu_lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1763 | kvm_mmu_zap_all(kvm); |
| 1764 | |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 1765 | up_write(&kvm->slots_lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1766 | |
| 1767 | return 0; |
| 1768 | |
| 1769 | out: |
| 1770 | return r; |
| 1771 | } |
| 1772 | |
| 1773 | static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) |
| 1774 | { |
| 1775 | int r; |
| 1776 | |
| 1777 | r = 0; |
| 1778 | switch (chip->chip_id) { |
| 1779 | case KVM_IRQCHIP_PIC_MASTER: |
| 1780 | memcpy(&chip->chip.pic, |
| 1781 | &pic_irqchip(kvm)->pics[0], |
| 1782 | sizeof(struct kvm_pic_state)); |
| 1783 | break; |
| 1784 | case KVM_IRQCHIP_PIC_SLAVE: |
| 1785 | memcpy(&chip->chip.pic, |
| 1786 | &pic_irqchip(kvm)->pics[1], |
| 1787 | sizeof(struct kvm_pic_state)); |
| 1788 | break; |
| 1789 | case KVM_IRQCHIP_IOAPIC: |
| 1790 | memcpy(&chip->chip.ioapic, |
| 1791 | ioapic_irqchip(kvm), |
| 1792 | sizeof(struct kvm_ioapic_state)); |
| 1793 | break; |
| 1794 | default: |
| 1795 | r = -EINVAL; |
| 1796 | break; |
| 1797 | } |
| 1798 | return r; |
| 1799 | } |
| 1800 | |
| 1801 | static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) |
| 1802 | { |
| 1803 | int r; |
| 1804 | |
| 1805 | r = 0; |
| 1806 | switch (chip->chip_id) { |
| 1807 | case KVM_IRQCHIP_PIC_MASTER: |
| 1808 | memcpy(&pic_irqchip(kvm)->pics[0], |
| 1809 | &chip->chip.pic, |
| 1810 | sizeof(struct kvm_pic_state)); |
| 1811 | break; |
| 1812 | case KVM_IRQCHIP_PIC_SLAVE: |
| 1813 | memcpy(&pic_irqchip(kvm)->pics[1], |
| 1814 | &chip->chip.pic, |
| 1815 | sizeof(struct kvm_pic_state)); |
| 1816 | break; |
| 1817 | case KVM_IRQCHIP_IOAPIC: |
| 1818 | memcpy(ioapic_irqchip(kvm), |
| 1819 | &chip->chip.ioapic, |
| 1820 | sizeof(struct kvm_ioapic_state)); |
| 1821 | break; |
| 1822 | default: |
| 1823 | r = -EINVAL; |
| 1824 | break; |
| 1825 | } |
| 1826 | kvm_pic_update_irq(pic_irqchip(kvm)); |
| 1827 | return r; |
| 1828 | } |
| 1829 | |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 1830 | static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps) |
| 1831 | { |
| 1832 | int r = 0; |
| 1833 | |
| 1834 | memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state)); |
| 1835 | return r; |
| 1836 | } |
| 1837 | |
| 1838 | static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps) |
| 1839 | { |
| 1840 | int r = 0; |
| 1841 | |
| 1842 | memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state)); |
| 1843 | kvm_pit_load_count(kvm, 0, ps->channels[0].count); |
| 1844 | return r; |
| 1845 | } |
| 1846 | |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 1847 | /* |
| 1848 | * Get (and clear) the dirty memory log for a memory slot. |
| 1849 | */ |
| 1850 | int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, |
| 1851 | struct kvm_dirty_log *log) |
| 1852 | { |
| 1853 | int r; |
| 1854 | int n; |
| 1855 | struct kvm_memory_slot *memslot; |
| 1856 | int is_dirty = 0; |
| 1857 | |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 1858 | down_write(&kvm->slots_lock); |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 1859 | |
| 1860 | r = kvm_get_dirty_log(kvm, log, &is_dirty); |
| 1861 | if (r) |
| 1862 | goto out; |
| 1863 | |
| 1864 | /* If nothing is dirty, don't bother messing with page tables. */ |
| 1865 | if (is_dirty) { |
| 1866 | kvm_mmu_slot_remove_write_access(kvm, log->slot); |
| 1867 | kvm_flush_remote_tlbs(kvm); |
| 1868 | memslot = &kvm->memslots[log->slot]; |
| 1869 | n = ALIGN(memslot->npages, BITS_PER_LONG) / 8; |
| 1870 | memset(memslot->dirty_bitmap, 0, n); |
| 1871 | } |
| 1872 | r = 0; |
| 1873 | out: |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 1874 | up_write(&kvm->slots_lock); |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 1875 | return r; |
| 1876 | } |
| 1877 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1878 | long kvm_arch_vm_ioctl(struct file *filp, |
| 1879 | unsigned int ioctl, unsigned long arg) |
| 1880 | { |
| 1881 | struct kvm *kvm = filp->private_data; |
| 1882 | void __user *argp = (void __user *)arg; |
| 1883 | int r = -EINVAL; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 1884 | /* |
| 1885 | * This union makes it completely explicit to gcc-3.x |
| 1886 | * that these two variables' stack usage should be |
| 1887 | * combined, not added together. |
| 1888 | */ |
| 1889 | union { |
| 1890 | struct kvm_pit_state ps; |
| 1891 | struct kvm_memory_alias alias; |
| 1892 | } u; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1893 | |
| 1894 | switch (ioctl) { |
| 1895 | case KVM_SET_TSS_ADDR: |
| 1896 | r = kvm_vm_ioctl_set_tss_addr(kvm, arg); |
| 1897 | if (r < 0) |
| 1898 | goto out; |
| 1899 | break; |
| 1900 | case KVM_SET_MEMORY_REGION: { |
| 1901 | struct kvm_memory_region kvm_mem; |
| 1902 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 1903 | |
| 1904 | r = -EFAULT; |
| 1905 | if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem)) |
| 1906 | goto out; |
| 1907 | kvm_userspace_mem.slot = kvm_mem.slot; |
| 1908 | kvm_userspace_mem.flags = kvm_mem.flags; |
| 1909 | kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr; |
| 1910 | kvm_userspace_mem.memory_size = kvm_mem.memory_size; |
| 1911 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0); |
| 1912 | if (r) |
| 1913 | goto out; |
| 1914 | break; |
| 1915 | } |
| 1916 | case KVM_SET_NR_MMU_PAGES: |
| 1917 | r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg); |
| 1918 | if (r) |
| 1919 | goto out; |
| 1920 | break; |
| 1921 | case KVM_GET_NR_MMU_PAGES: |
| 1922 | r = kvm_vm_ioctl_get_nr_mmu_pages(kvm); |
| 1923 | break; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 1924 | case KVM_SET_MEMORY_ALIAS: |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1925 | r = -EFAULT; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 1926 | if (copy_from_user(&u.alias, argp, sizeof(struct kvm_memory_alias))) |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1927 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 1928 | r = kvm_vm_ioctl_set_memory_alias(kvm, &u.alias); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1929 | if (r) |
| 1930 | goto out; |
| 1931 | break; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1932 | case KVM_CREATE_IRQCHIP: |
| 1933 | r = -ENOMEM; |
Zhang Xiantao | d7deeeb | 2007-12-14 10:17:34 +0800 | [diff] [blame] | 1934 | kvm->arch.vpic = kvm_create_pic(kvm); |
| 1935 | if (kvm->arch.vpic) { |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1936 | r = kvm_ioapic_init(kvm); |
| 1937 | if (r) { |
Zhang Xiantao | d7deeeb | 2007-12-14 10:17:34 +0800 | [diff] [blame] | 1938 | kfree(kvm->arch.vpic); |
| 1939 | kvm->arch.vpic = NULL; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1940 | goto out; |
| 1941 | } |
| 1942 | } else |
| 1943 | goto out; |
| 1944 | break; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 1945 | case KVM_CREATE_PIT: |
| 1946 | r = -ENOMEM; |
| 1947 | kvm->arch.vpit = kvm_create_pit(kvm); |
| 1948 | if (kvm->arch.vpit) |
| 1949 | r = 0; |
| 1950 | break; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1951 | case KVM_IRQ_LINE: { |
| 1952 | struct kvm_irq_level irq_event; |
| 1953 | |
| 1954 | r = -EFAULT; |
| 1955 | if (copy_from_user(&irq_event, argp, sizeof irq_event)) |
| 1956 | goto out; |
| 1957 | if (irqchip_in_kernel(kvm)) { |
| 1958 | mutex_lock(&kvm->lock); |
Amit Shah | 29c8fa3 | 2008-08-18 15:07:05 +0300 | [diff] [blame] | 1959 | kvm_set_irq(kvm, irq_event.irq, irq_event.level); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1960 | mutex_unlock(&kvm->lock); |
| 1961 | r = 0; |
| 1962 | } |
| 1963 | break; |
| 1964 | } |
| 1965 | case KVM_GET_IRQCHIP: { |
| 1966 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 1967 | struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1968 | |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 1969 | r = -ENOMEM; |
| 1970 | if (!chip) |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1971 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 1972 | r = -EFAULT; |
| 1973 | if (copy_from_user(chip, argp, sizeof *chip)) |
| 1974 | goto get_irqchip_out; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1975 | r = -ENXIO; |
| 1976 | if (!irqchip_in_kernel(kvm)) |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 1977 | goto get_irqchip_out; |
| 1978 | r = kvm_vm_ioctl_get_irqchip(kvm, chip); |
| 1979 | if (r) |
| 1980 | goto get_irqchip_out; |
| 1981 | r = -EFAULT; |
| 1982 | if (copy_to_user(argp, chip, sizeof *chip)) |
| 1983 | goto get_irqchip_out; |
| 1984 | r = 0; |
| 1985 | get_irqchip_out: |
| 1986 | kfree(chip); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1987 | if (r) |
| 1988 | goto out; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1989 | break; |
| 1990 | } |
| 1991 | case KVM_SET_IRQCHIP: { |
| 1992 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 1993 | struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1994 | |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 1995 | r = -ENOMEM; |
| 1996 | if (!chip) |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1997 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 1998 | r = -EFAULT; |
| 1999 | if (copy_from_user(chip, argp, sizeof *chip)) |
| 2000 | goto set_irqchip_out; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2001 | r = -ENXIO; |
| 2002 | if (!irqchip_in_kernel(kvm)) |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2003 | goto set_irqchip_out; |
| 2004 | r = kvm_vm_ioctl_set_irqchip(kvm, chip); |
| 2005 | if (r) |
| 2006 | goto set_irqchip_out; |
| 2007 | r = 0; |
| 2008 | set_irqchip_out: |
| 2009 | kfree(chip); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2010 | if (r) |
| 2011 | goto out; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2012 | break; |
| 2013 | } |
Ben-Ami Yassour | 4d5c5d0 | 2008-07-28 19:26:26 +0300 | [diff] [blame] | 2014 | case KVM_ASSIGN_PCI_DEVICE: { |
| 2015 | struct kvm_assigned_pci_dev assigned_dev; |
| 2016 | |
| 2017 | r = -EFAULT; |
| 2018 | if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev)) |
| 2019 | goto out; |
| 2020 | r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev); |
| 2021 | if (r) |
| 2022 | goto out; |
| 2023 | break; |
| 2024 | } |
| 2025 | case KVM_ASSIGN_IRQ: { |
| 2026 | struct kvm_assigned_irq assigned_irq; |
| 2027 | |
| 2028 | r = -EFAULT; |
| 2029 | if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq)) |
| 2030 | goto out; |
| 2031 | r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq); |
| 2032 | if (r) |
| 2033 | goto out; |
| 2034 | break; |
| 2035 | } |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2036 | case KVM_GET_PIT: { |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2037 | r = -EFAULT; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2038 | if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state))) |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2039 | goto out; |
| 2040 | r = -ENXIO; |
| 2041 | if (!kvm->arch.vpit) |
| 2042 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2043 | r = kvm_vm_ioctl_get_pit(kvm, &u.ps); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2044 | if (r) |
| 2045 | goto out; |
| 2046 | r = -EFAULT; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2047 | if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state))) |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2048 | goto out; |
| 2049 | r = 0; |
| 2050 | break; |
| 2051 | } |
| 2052 | case KVM_SET_PIT: { |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2053 | r = -EFAULT; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2054 | if (copy_from_user(&u.ps, argp, sizeof u.ps)) |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2055 | goto out; |
| 2056 | r = -ENXIO; |
| 2057 | if (!kvm->arch.vpit) |
| 2058 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2059 | r = kvm_vm_ioctl_set_pit(kvm, &u.ps); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2060 | if (r) |
| 2061 | goto out; |
| 2062 | r = 0; |
| 2063 | break; |
| 2064 | } |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2065 | default: |
| 2066 | ; |
| 2067 | } |
| 2068 | out: |
| 2069 | return r; |
| 2070 | } |
| 2071 | |
Zhang Xiantao | a16b043 | 2007-11-16 14:38:21 +0800 | [diff] [blame] | 2072 | static void kvm_init_msr_list(void) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 2073 | { |
| 2074 | u32 dummy[2]; |
| 2075 | unsigned i, j; |
| 2076 | |
| 2077 | for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) { |
| 2078 | if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0) |
| 2079 | continue; |
| 2080 | if (j < i) |
| 2081 | msrs_to_save[j] = msrs_to_save[i]; |
| 2082 | j++; |
| 2083 | } |
| 2084 | num_msrs_to_save = j; |
| 2085 | } |
| 2086 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2087 | /* |
| 2088 | * Only apic need an MMIO device hook, so shortcut now.. |
| 2089 | */ |
| 2090 | static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu, |
Laurent Vivier | 9276049 | 2008-05-30 16:05:53 +0200 | [diff] [blame] | 2091 | gpa_t addr, int len, |
| 2092 | int is_write) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2093 | { |
| 2094 | struct kvm_io_device *dev; |
| 2095 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2096 | if (vcpu->arch.apic) { |
| 2097 | dev = &vcpu->arch.apic->dev; |
Laurent Vivier | 9276049 | 2008-05-30 16:05:53 +0200 | [diff] [blame] | 2098 | if (dev->in_range(dev, addr, len, is_write)) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2099 | return dev; |
| 2100 | } |
| 2101 | return NULL; |
| 2102 | } |
| 2103 | |
| 2104 | |
| 2105 | static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu, |
Laurent Vivier | 9276049 | 2008-05-30 16:05:53 +0200 | [diff] [blame] | 2106 | gpa_t addr, int len, |
| 2107 | int is_write) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2108 | { |
| 2109 | struct kvm_io_device *dev; |
| 2110 | |
Laurent Vivier | 9276049 | 2008-05-30 16:05:53 +0200 | [diff] [blame] | 2111 | dev = vcpu_find_pervcpu_dev(vcpu, addr, len, is_write); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2112 | if (dev == NULL) |
Laurent Vivier | 9276049 | 2008-05-30 16:05:53 +0200 | [diff] [blame] | 2113 | dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr, len, |
| 2114 | is_write); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2115 | return dev; |
| 2116 | } |
| 2117 | |
| 2118 | int emulator_read_std(unsigned long addr, |
| 2119 | void *val, |
| 2120 | unsigned int bytes, |
| 2121 | struct kvm_vcpu *vcpu) |
| 2122 | { |
| 2123 | void *data = val; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2124 | int r = X86EMUL_CONTINUE; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2125 | |
| 2126 | while (bytes) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2127 | gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2128 | unsigned offset = addr & (PAGE_SIZE-1); |
| 2129 | unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset); |
| 2130 | int ret; |
| 2131 | |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2132 | if (gpa == UNMAPPED_GVA) { |
| 2133 | r = X86EMUL_PROPAGATE_FAULT; |
| 2134 | goto out; |
| 2135 | } |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2136 | ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2137 | if (ret < 0) { |
| 2138 | r = X86EMUL_UNHANDLEABLE; |
| 2139 | goto out; |
| 2140 | } |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2141 | |
| 2142 | bytes -= tocopy; |
| 2143 | data += tocopy; |
| 2144 | addr += tocopy; |
| 2145 | } |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2146 | out: |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2147 | return r; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2148 | } |
| 2149 | EXPORT_SYMBOL_GPL(emulator_read_std); |
| 2150 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2151 | static int emulator_read_emulated(unsigned long addr, |
| 2152 | void *val, |
| 2153 | unsigned int bytes, |
| 2154 | struct kvm_vcpu *vcpu) |
| 2155 | { |
| 2156 | struct kvm_io_device *mmio_dev; |
| 2157 | gpa_t gpa; |
| 2158 | |
| 2159 | if (vcpu->mmio_read_completed) { |
| 2160 | memcpy(val, vcpu->mmio_data, bytes); |
| 2161 | vcpu->mmio_read_completed = 0; |
| 2162 | return X86EMUL_CONTINUE; |
| 2163 | } |
| 2164 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2165 | gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2166 | |
| 2167 | /* For APIC access vmexit */ |
| 2168 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) |
| 2169 | goto mmio; |
| 2170 | |
| 2171 | if (emulator_read_std(addr, val, bytes, vcpu) |
| 2172 | == X86EMUL_CONTINUE) |
| 2173 | return X86EMUL_CONTINUE; |
| 2174 | if (gpa == UNMAPPED_GVA) |
| 2175 | return X86EMUL_PROPAGATE_FAULT; |
| 2176 | |
| 2177 | mmio: |
| 2178 | /* |
| 2179 | * Is this MMIO handled locally? |
| 2180 | */ |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2181 | mutex_lock(&vcpu->kvm->lock); |
Laurent Vivier | 9276049 | 2008-05-30 16:05:53 +0200 | [diff] [blame] | 2182 | mmio_dev = vcpu_find_mmio_dev(vcpu, gpa, bytes, 0); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2183 | if (mmio_dev) { |
| 2184 | kvm_iodevice_read(mmio_dev, gpa, bytes, val); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2185 | mutex_unlock(&vcpu->kvm->lock); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2186 | return X86EMUL_CONTINUE; |
| 2187 | } |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2188 | mutex_unlock(&vcpu->kvm->lock); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2189 | |
| 2190 | vcpu->mmio_needed = 1; |
| 2191 | vcpu->mmio_phys_addr = gpa; |
| 2192 | vcpu->mmio_size = bytes; |
| 2193 | vcpu->mmio_is_write = 0; |
| 2194 | |
| 2195 | return X86EMUL_UNHANDLEABLE; |
| 2196 | } |
| 2197 | |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 2198 | int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, |
Avi Kivity | 9f81128 | 2008-03-02 14:06:05 +0200 | [diff] [blame] | 2199 | const void *val, int bytes) |
| 2200 | { |
| 2201 | int ret; |
| 2202 | |
| 2203 | ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes); |
| 2204 | if (ret < 0) |
| 2205 | return 0; |
| 2206 | kvm_mmu_pte_write(vcpu, gpa, val, bytes); |
| 2207 | return 1; |
| 2208 | } |
| 2209 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2210 | static int emulator_write_emulated_onepage(unsigned long addr, |
| 2211 | const void *val, |
| 2212 | unsigned int bytes, |
| 2213 | struct kvm_vcpu *vcpu) |
| 2214 | { |
| 2215 | struct kvm_io_device *mmio_dev; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2216 | gpa_t gpa; |
| 2217 | |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2218 | gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2219 | |
| 2220 | if (gpa == UNMAPPED_GVA) { |
Avi Kivity | c3c91fe | 2007-11-25 14:04:58 +0200 | [diff] [blame] | 2221 | kvm_inject_page_fault(vcpu, addr, 2); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2222 | return X86EMUL_PROPAGATE_FAULT; |
| 2223 | } |
| 2224 | |
| 2225 | /* For APIC access vmexit */ |
| 2226 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) |
| 2227 | goto mmio; |
| 2228 | |
| 2229 | if (emulator_write_phys(vcpu, gpa, val, bytes)) |
| 2230 | return X86EMUL_CONTINUE; |
| 2231 | |
| 2232 | mmio: |
| 2233 | /* |
| 2234 | * Is this MMIO handled locally? |
| 2235 | */ |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2236 | mutex_lock(&vcpu->kvm->lock); |
Laurent Vivier | 9276049 | 2008-05-30 16:05:53 +0200 | [diff] [blame] | 2237 | mmio_dev = vcpu_find_mmio_dev(vcpu, gpa, bytes, 1); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2238 | if (mmio_dev) { |
| 2239 | kvm_iodevice_write(mmio_dev, gpa, bytes, val); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2240 | mutex_unlock(&vcpu->kvm->lock); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2241 | return X86EMUL_CONTINUE; |
| 2242 | } |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2243 | mutex_unlock(&vcpu->kvm->lock); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2244 | |
| 2245 | vcpu->mmio_needed = 1; |
| 2246 | vcpu->mmio_phys_addr = gpa; |
| 2247 | vcpu->mmio_size = bytes; |
| 2248 | vcpu->mmio_is_write = 1; |
| 2249 | memcpy(vcpu->mmio_data, val, bytes); |
| 2250 | |
| 2251 | return X86EMUL_CONTINUE; |
| 2252 | } |
| 2253 | |
| 2254 | int emulator_write_emulated(unsigned long addr, |
| 2255 | const void *val, |
| 2256 | unsigned int bytes, |
| 2257 | struct kvm_vcpu *vcpu) |
| 2258 | { |
| 2259 | /* Crossing a page boundary? */ |
| 2260 | if (((addr + bytes - 1) ^ addr) & PAGE_MASK) { |
| 2261 | int rc, now; |
| 2262 | |
| 2263 | now = -addr & ~PAGE_MASK; |
| 2264 | rc = emulator_write_emulated_onepage(addr, val, now, vcpu); |
| 2265 | if (rc != X86EMUL_CONTINUE) |
| 2266 | return rc; |
| 2267 | addr += now; |
| 2268 | val += now; |
| 2269 | bytes -= now; |
| 2270 | } |
| 2271 | return emulator_write_emulated_onepage(addr, val, bytes, vcpu); |
| 2272 | } |
| 2273 | EXPORT_SYMBOL_GPL(emulator_write_emulated); |
| 2274 | |
| 2275 | static int emulator_cmpxchg_emulated(unsigned long addr, |
| 2276 | const void *old, |
| 2277 | const void *new, |
| 2278 | unsigned int bytes, |
| 2279 | struct kvm_vcpu *vcpu) |
| 2280 | { |
| 2281 | static int reported; |
| 2282 | |
| 2283 | if (!reported) { |
| 2284 | reported = 1; |
| 2285 | printk(KERN_WARNING "kvm: emulating exchange as write\n"); |
| 2286 | } |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 2287 | #ifndef CONFIG_X86_64 |
| 2288 | /* guests cmpxchg8b have to be emulated atomically */ |
| 2289 | if (bytes == 8) { |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2290 | gpa_t gpa; |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 2291 | struct page *page; |
Andrew Morton | c0b49b0 | 2008-02-04 22:27:18 -0800 | [diff] [blame] | 2292 | char *kaddr; |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 2293 | u64 val; |
| 2294 | |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2295 | gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr); |
| 2296 | |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 2297 | if (gpa == UNMAPPED_GVA || |
| 2298 | (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) |
| 2299 | goto emul_write; |
| 2300 | |
| 2301 | if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK)) |
| 2302 | goto emul_write; |
| 2303 | |
| 2304 | val = *(u64 *)new; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 2305 | |
| 2306 | down_read(¤t->mm->mmap_sem); |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 2307 | page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 2308 | up_read(¤t->mm->mmap_sem); |
| 2309 | |
Andrew Morton | c0b49b0 | 2008-02-04 22:27:18 -0800 | [diff] [blame] | 2310 | kaddr = kmap_atomic(page, KM_USER0); |
| 2311 | set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val); |
| 2312 | kunmap_atomic(kaddr, KM_USER0); |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 2313 | kvm_release_page_dirty(page); |
| 2314 | } |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 2315 | emul_write: |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 2316 | #endif |
| 2317 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2318 | return emulator_write_emulated(addr, new, bytes, vcpu); |
| 2319 | } |
| 2320 | |
| 2321 | static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 2322 | { |
| 2323 | return kvm_x86_ops->get_segment_base(vcpu, seg); |
| 2324 | } |
| 2325 | |
| 2326 | int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address) |
| 2327 | { |
| 2328 | return X86EMUL_CONTINUE; |
| 2329 | } |
| 2330 | |
| 2331 | int emulate_clts(struct kvm_vcpu *vcpu) |
| 2332 | { |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 2333 | KVMTRACE_0D(CLTS, vcpu, handler); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2334 | kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2335 | return X86EMUL_CONTINUE; |
| 2336 | } |
| 2337 | |
| 2338 | int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest) |
| 2339 | { |
| 2340 | struct kvm_vcpu *vcpu = ctxt->vcpu; |
| 2341 | |
| 2342 | switch (dr) { |
| 2343 | case 0 ... 3: |
| 2344 | *dest = kvm_x86_ops->get_dr(vcpu, dr); |
| 2345 | return X86EMUL_CONTINUE; |
| 2346 | default: |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 2347 | pr_unimpl(vcpu, "%s: unexpected dr %u\n", __func__, dr); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2348 | return X86EMUL_UNHANDLEABLE; |
| 2349 | } |
| 2350 | } |
| 2351 | |
| 2352 | int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value) |
| 2353 | { |
| 2354 | unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U; |
| 2355 | int exception; |
| 2356 | |
| 2357 | kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception); |
| 2358 | if (exception) { |
| 2359 | /* FIXME: better handling */ |
| 2360 | return X86EMUL_UNHANDLEABLE; |
| 2361 | } |
| 2362 | return X86EMUL_CONTINUE; |
| 2363 | } |
| 2364 | |
| 2365 | void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context) |
| 2366 | { |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2367 | u8 opcodes[4]; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2368 | unsigned long rip = kvm_rip_read(vcpu); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2369 | unsigned long rip_linear; |
| 2370 | |
Avi Kivity | f76c710 | 2008-06-13 22:45:42 +0300 | [diff] [blame] | 2371 | if (!printk_ratelimit()) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2372 | return; |
| 2373 | |
Glauber Costa | 25be460 | 2008-06-10 10:46:53 -0300 | [diff] [blame] | 2374 | rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS); |
| 2375 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2376 | emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu); |
| 2377 | |
| 2378 | printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n", |
| 2379 | context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2380 | } |
| 2381 | EXPORT_SYMBOL_GPL(kvm_report_emulation_failure); |
| 2382 | |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 2383 | static struct x86_emulate_ops emulate_ops = { |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2384 | .read_std = emulator_read_std, |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2385 | .read_emulated = emulator_read_emulated, |
| 2386 | .write_emulated = emulator_write_emulated, |
| 2387 | .cmpxchg_emulated = emulator_cmpxchg_emulated, |
| 2388 | }; |
| 2389 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2390 | static void cache_all_regs(struct kvm_vcpu *vcpu) |
| 2391 | { |
| 2392 | kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 2393 | kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 2394 | kvm_register_read(vcpu, VCPU_REGS_RIP); |
| 2395 | vcpu->arch.regs_dirty = ~0; |
| 2396 | } |
| 2397 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2398 | int emulate_instruction(struct kvm_vcpu *vcpu, |
| 2399 | struct kvm_run *run, |
| 2400 | unsigned long cr2, |
| 2401 | u16 error_code, |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 2402 | int emulation_type) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2403 | { |
| 2404 | int r; |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 2405 | struct decode_cache *c; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2406 | |
Avi Kivity | 26eef70 | 2008-07-03 14:59:22 +0300 | [diff] [blame] | 2407 | kvm_clear_exception_queue(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2408 | vcpu->arch.mmio_fault_cr2 = cr2; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2409 | /* |
| 2410 | * TODO: fix x86_emulate.c to use guest_read/write_register |
| 2411 | * instead of direct ->regs accesses, can save hundred cycles |
| 2412 | * on Intel for instructions that don't read/change RSP, for |
| 2413 | * for example. |
| 2414 | */ |
| 2415 | cache_all_regs(vcpu); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2416 | |
| 2417 | vcpu->mmio_is_write = 0; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2418 | vcpu->arch.pio.string = 0; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2419 | |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 2420 | if (!(emulation_type & EMULTYPE_NO_DECODE)) { |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2421 | int cs_db, cs_l; |
| 2422 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
| 2423 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2424 | vcpu->arch.emulate_ctxt.vcpu = vcpu; |
| 2425 | vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu); |
| 2426 | vcpu->arch.emulate_ctxt.mode = |
| 2427 | (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2428 | ? X86EMUL_MODE_REAL : cs_l |
| 2429 | ? X86EMUL_MODE_PROT64 : cs_db |
| 2430 | ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16; |
| 2431 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2432 | r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops); |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 2433 | |
| 2434 | /* Reject the instructions other than VMCALL/VMMCALL when |
| 2435 | * try to emulate invalid opcode */ |
| 2436 | c = &vcpu->arch.emulate_ctxt.decode; |
| 2437 | if ((emulation_type & EMULTYPE_TRAP_UD) && |
| 2438 | (!(c->twobyte && c->b == 0x01 && |
| 2439 | (c->modrm_reg == 0 || c->modrm_reg == 3) && |
| 2440 | c->modrm_mod == 3 && c->modrm_rm == 1))) |
| 2441 | return EMULATE_FAIL; |
| 2442 | |
Avi Kivity | f2b5756 | 2007-11-18 15:17:51 +0200 | [diff] [blame] | 2443 | ++vcpu->stat.insn_emulation; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2444 | if (r) { |
Avi Kivity | f2b5756 | 2007-11-18 15:17:51 +0200 | [diff] [blame] | 2445 | ++vcpu->stat.insn_emulation_fail; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2446 | if (kvm_mmu_unprotect_page_virt(vcpu, cr2)) |
| 2447 | return EMULATE_DONE; |
| 2448 | return EMULATE_FAIL; |
| 2449 | } |
| 2450 | } |
| 2451 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2452 | r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2453 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2454 | if (vcpu->arch.pio.string) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2455 | return EMULATE_DO_MMIO; |
| 2456 | |
| 2457 | if ((r || vcpu->mmio_is_write) && run) { |
| 2458 | run->exit_reason = KVM_EXIT_MMIO; |
| 2459 | run->mmio.phys_addr = vcpu->mmio_phys_addr; |
| 2460 | memcpy(run->mmio.data, vcpu->mmio_data, 8); |
| 2461 | run->mmio.len = vcpu->mmio_size; |
| 2462 | run->mmio.is_write = vcpu->mmio_is_write; |
| 2463 | } |
| 2464 | |
| 2465 | if (r) { |
| 2466 | if (kvm_mmu_unprotect_page_virt(vcpu, cr2)) |
| 2467 | return EMULATE_DONE; |
| 2468 | if (!vcpu->mmio_needed) { |
| 2469 | kvm_report_emulation_failure(vcpu, "mmio"); |
| 2470 | return EMULATE_FAIL; |
| 2471 | } |
| 2472 | return EMULATE_DO_MMIO; |
| 2473 | } |
| 2474 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2475 | kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2476 | |
| 2477 | if (vcpu->mmio_is_write) { |
| 2478 | vcpu->mmio_needed = 0; |
| 2479 | return EMULATE_DO_MMIO; |
| 2480 | } |
| 2481 | |
| 2482 | return EMULATE_DONE; |
| 2483 | } |
| 2484 | EXPORT_SYMBOL_GPL(emulate_instruction); |
| 2485 | |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2486 | static void free_pio_guest_pages(struct kvm_vcpu *vcpu) |
| 2487 | { |
| 2488 | int i; |
| 2489 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2490 | for (i = 0; i < ARRAY_SIZE(vcpu->arch.pio.guest_pages); ++i) |
| 2491 | if (vcpu->arch.pio.guest_pages[i]) { |
| 2492 | kvm_release_page_dirty(vcpu->arch.pio.guest_pages[i]); |
| 2493 | vcpu->arch.pio.guest_pages[i] = NULL; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2494 | } |
| 2495 | } |
| 2496 | |
| 2497 | static int pio_copy_data(struct kvm_vcpu *vcpu) |
| 2498 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2499 | void *p = vcpu->arch.pio_data; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2500 | void *q; |
| 2501 | unsigned bytes; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2502 | int nr_pages = vcpu->arch.pio.guest_pages[1] ? 2 : 1; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2503 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2504 | q = vmap(vcpu->arch.pio.guest_pages, nr_pages, VM_READ|VM_WRITE, |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2505 | PAGE_KERNEL); |
| 2506 | if (!q) { |
| 2507 | free_pio_guest_pages(vcpu); |
| 2508 | return -ENOMEM; |
| 2509 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2510 | q += vcpu->arch.pio.guest_page_offset; |
| 2511 | bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count; |
| 2512 | if (vcpu->arch.pio.in) |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2513 | memcpy(q, p, bytes); |
| 2514 | else |
| 2515 | memcpy(p, q, bytes); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2516 | q -= vcpu->arch.pio.guest_page_offset; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2517 | vunmap(q); |
| 2518 | free_pio_guest_pages(vcpu); |
| 2519 | return 0; |
| 2520 | } |
| 2521 | |
| 2522 | int complete_pio(struct kvm_vcpu *vcpu) |
| 2523 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2524 | struct kvm_pio_request *io = &vcpu->arch.pio; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2525 | long delta; |
| 2526 | int r; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2527 | unsigned long val; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2528 | |
| 2529 | if (!io->string) { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2530 | if (io->in) { |
| 2531 | val = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 2532 | memcpy(&val, vcpu->arch.pio_data, io->size); |
| 2533 | kvm_register_write(vcpu, VCPU_REGS_RAX, val); |
| 2534 | } |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2535 | } else { |
| 2536 | if (io->in) { |
| 2537 | r = pio_copy_data(vcpu); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2538 | if (r) |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2539 | return r; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2540 | } |
| 2541 | |
| 2542 | delta = 1; |
| 2543 | if (io->rep) { |
| 2544 | delta *= io->cur_count; |
| 2545 | /* |
| 2546 | * The size of the register should really depend on |
| 2547 | * current address size. |
| 2548 | */ |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2549 | val = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 2550 | val -= delta; |
| 2551 | kvm_register_write(vcpu, VCPU_REGS_RCX, val); |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2552 | } |
| 2553 | if (io->down) |
| 2554 | delta = -delta; |
| 2555 | delta *= io->size; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2556 | if (io->in) { |
| 2557 | val = kvm_register_read(vcpu, VCPU_REGS_RDI); |
| 2558 | val += delta; |
| 2559 | kvm_register_write(vcpu, VCPU_REGS_RDI, val); |
| 2560 | } else { |
| 2561 | val = kvm_register_read(vcpu, VCPU_REGS_RSI); |
| 2562 | val += delta; |
| 2563 | kvm_register_write(vcpu, VCPU_REGS_RSI, val); |
| 2564 | } |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2565 | } |
| 2566 | |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2567 | io->count -= io->cur_count; |
| 2568 | io->cur_count = 0; |
| 2569 | |
| 2570 | return 0; |
| 2571 | } |
| 2572 | |
| 2573 | static void kernel_pio(struct kvm_io_device *pio_dev, |
| 2574 | struct kvm_vcpu *vcpu, |
| 2575 | void *pd) |
| 2576 | { |
| 2577 | /* TODO: String I/O for in kernel device */ |
| 2578 | |
| 2579 | mutex_lock(&vcpu->kvm->lock); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2580 | if (vcpu->arch.pio.in) |
| 2581 | kvm_iodevice_read(pio_dev, vcpu->arch.pio.port, |
| 2582 | vcpu->arch.pio.size, |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2583 | pd); |
| 2584 | else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2585 | kvm_iodevice_write(pio_dev, vcpu->arch.pio.port, |
| 2586 | vcpu->arch.pio.size, |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2587 | pd); |
| 2588 | mutex_unlock(&vcpu->kvm->lock); |
| 2589 | } |
| 2590 | |
| 2591 | static void pio_string_write(struct kvm_io_device *pio_dev, |
| 2592 | struct kvm_vcpu *vcpu) |
| 2593 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2594 | struct kvm_pio_request *io = &vcpu->arch.pio; |
| 2595 | void *pd = vcpu->arch.pio_data; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2596 | int i; |
| 2597 | |
| 2598 | mutex_lock(&vcpu->kvm->lock); |
| 2599 | for (i = 0; i < io->cur_count; i++) { |
| 2600 | kvm_iodevice_write(pio_dev, io->port, |
| 2601 | io->size, |
| 2602 | pd); |
| 2603 | pd += io->size; |
| 2604 | } |
| 2605 | mutex_unlock(&vcpu->kvm->lock); |
| 2606 | } |
| 2607 | |
| 2608 | static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu, |
Laurent Vivier | 9276049 | 2008-05-30 16:05:53 +0200 | [diff] [blame] | 2609 | gpa_t addr, int len, |
| 2610 | int is_write) |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2611 | { |
Laurent Vivier | 9276049 | 2008-05-30 16:05:53 +0200 | [diff] [blame] | 2612 | return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr, len, is_write); |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2613 | } |
| 2614 | |
| 2615 | int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in, |
| 2616 | int size, unsigned port) |
| 2617 | { |
| 2618 | struct kvm_io_device *pio_dev; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2619 | unsigned long val; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2620 | |
| 2621 | vcpu->run->exit_reason = KVM_EXIT_IO; |
| 2622 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2623 | vcpu->run->io.size = vcpu->arch.pio.size = size; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2624 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2625 | vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1; |
| 2626 | vcpu->run->io.port = vcpu->arch.pio.port = port; |
| 2627 | vcpu->arch.pio.in = in; |
| 2628 | vcpu->arch.pio.string = 0; |
| 2629 | vcpu->arch.pio.down = 0; |
| 2630 | vcpu->arch.pio.guest_page_offset = 0; |
| 2631 | vcpu->arch.pio.rep = 0; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2632 | |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2633 | if (vcpu->run->io.direction == KVM_EXIT_IO_IN) |
| 2634 | KVMTRACE_2D(IO_READ, vcpu, vcpu->run->io.port, (u32)size, |
| 2635 | handler); |
| 2636 | else |
| 2637 | KVMTRACE_2D(IO_WRITE, vcpu, vcpu->run->io.port, (u32)size, |
| 2638 | handler); |
| 2639 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2640 | val = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 2641 | memcpy(vcpu->arch.pio_data, &val, 4); |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2642 | |
| 2643 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
| 2644 | |
Laurent Vivier | 9276049 | 2008-05-30 16:05:53 +0200 | [diff] [blame] | 2645 | pio_dev = vcpu_find_pio_dev(vcpu, port, size, !in); |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2646 | if (pio_dev) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2647 | kernel_pio(pio_dev, vcpu, vcpu->arch.pio_data); |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2648 | complete_pio(vcpu); |
| 2649 | return 1; |
| 2650 | } |
| 2651 | return 0; |
| 2652 | } |
| 2653 | EXPORT_SYMBOL_GPL(kvm_emulate_pio); |
| 2654 | |
| 2655 | int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in, |
| 2656 | int size, unsigned long count, int down, |
| 2657 | gva_t address, int rep, unsigned port) |
| 2658 | { |
| 2659 | unsigned now, in_page; |
| 2660 | int i, ret = 0; |
| 2661 | int nr_pages = 1; |
| 2662 | struct page *page; |
| 2663 | struct kvm_io_device *pio_dev; |
| 2664 | |
| 2665 | vcpu->run->exit_reason = KVM_EXIT_IO; |
| 2666 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2667 | vcpu->run->io.size = vcpu->arch.pio.size = size; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2668 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2669 | vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count; |
| 2670 | vcpu->run->io.port = vcpu->arch.pio.port = port; |
| 2671 | vcpu->arch.pio.in = in; |
| 2672 | vcpu->arch.pio.string = 1; |
| 2673 | vcpu->arch.pio.down = down; |
| 2674 | vcpu->arch.pio.guest_page_offset = offset_in_page(address); |
| 2675 | vcpu->arch.pio.rep = rep; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2676 | |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2677 | if (vcpu->run->io.direction == KVM_EXIT_IO_IN) |
| 2678 | KVMTRACE_2D(IO_READ, vcpu, vcpu->run->io.port, (u32)size, |
| 2679 | handler); |
| 2680 | else |
| 2681 | KVMTRACE_2D(IO_WRITE, vcpu, vcpu->run->io.port, (u32)size, |
| 2682 | handler); |
| 2683 | |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2684 | if (!count) { |
| 2685 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
| 2686 | return 1; |
| 2687 | } |
| 2688 | |
| 2689 | if (!down) |
| 2690 | in_page = PAGE_SIZE - offset_in_page(address); |
| 2691 | else |
| 2692 | in_page = offset_in_page(address) + size; |
| 2693 | now = min(count, (unsigned long)in_page / size); |
| 2694 | if (!now) { |
| 2695 | /* |
| 2696 | * String I/O straddles page boundary. Pin two guest pages |
| 2697 | * so that we satisfy atomicity constraints. Do just one |
| 2698 | * transaction to avoid complexity. |
| 2699 | */ |
| 2700 | nr_pages = 2; |
| 2701 | now = 1; |
| 2702 | } |
| 2703 | if (down) { |
| 2704 | /* |
| 2705 | * String I/O in reverse. Yuck. Kill the guest, fix later. |
| 2706 | */ |
| 2707 | pr_unimpl(vcpu, "guest string pio down\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 2708 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2709 | return 1; |
| 2710 | } |
| 2711 | vcpu->run->io.count = now; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2712 | vcpu->arch.pio.cur_count = now; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2713 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2714 | if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count) |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2715 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
| 2716 | |
| 2717 | for (i = 0; i < nr_pages; ++i) { |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2718 | page = gva_to_page(vcpu, address + i * PAGE_SIZE); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2719 | vcpu->arch.pio.guest_pages[i] = page; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2720 | if (!page) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 2721 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2722 | free_pio_guest_pages(vcpu); |
| 2723 | return 1; |
| 2724 | } |
| 2725 | } |
| 2726 | |
Laurent Vivier | 9276049 | 2008-05-30 16:05:53 +0200 | [diff] [blame] | 2727 | pio_dev = vcpu_find_pio_dev(vcpu, port, |
| 2728 | vcpu->arch.pio.cur_count, |
| 2729 | !vcpu->arch.pio.in); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2730 | if (!vcpu->arch.pio.in) { |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2731 | /* string PIO write */ |
| 2732 | ret = pio_copy_data(vcpu); |
| 2733 | if (ret >= 0 && pio_dev) { |
| 2734 | pio_string_write(pio_dev, vcpu); |
| 2735 | complete_pio(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2736 | if (vcpu->arch.pio.count == 0) |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2737 | ret = 1; |
| 2738 | } |
| 2739 | } else if (pio_dev) |
| 2740 | pr_unimpl(vcpu, "no string pio read support yet, " |
| 2741 | "port %x size %d count %ld\n", |
| 2742 | port, size, count); |
| 2743 | |
| 2744 | return ret; |
| 2745 | } |
| 2746 | EXPORT_SYMBOL_GPL(kvm_emulate_pio_string); |
| 2747 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 2748 | int kvm_arch_init(void *opaque) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 2749 | { |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 2750 | int r; |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 2751 | struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque; |
| 2752 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 2753 | if (kvm_x86_ops) { |
| 2754 | printk(KERN_ERR "kvm: already loaded the other module\n"); |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 2755 | r = -EEXIST; |
| 2756 | goto out; |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 2757 | } |
| 2758 | |
| 2759 | if (!ops->cpu_has_kvm_support()) { |
| 2760 | printk(KERN_ERR "kvm: no hardware support\n"); |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 2761 | r = -EOPNOTSUPP; |
| 2762 | goto out; |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 2763 | } |
| 2764 | if (ops->disabled_by_bios()) { |
| 2765 | printk(KERN_ERR "kvm: disabled by bios\n"); |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 2766 | r = -EOPNOTSUPP; |
| 2767 | goto out; |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 2768 | } |
| 2769 | |
Avi Kivity | 97db56c | 2008-01-13 13:23:56 +0200 | [diff] [blame] | 2770 | r = kvm_mmu_module_init(); |
| 2771 | if (r) |
| 2772 | goto out; |
| 2773 | |
| 2774 | kvm_init_msr_list(); |
| 2775 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 2776 | kvm_x86_ops = ops; |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 2777 | kvm_mmu_set_nonpresent_ptes(0ull, 0ull); |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 2778 | kvm_mmu_set_base_ptes(PT_PRESENT_MASK); |
| 2779 | kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK, |
| 2780 | PT_DIRTY_MASK, PT64_NX_MASK, 0); |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 2781 | return 0; |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 2782 | |
| 2783 | out: |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 2784 | return r; |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 2785 | } |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2786 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 2787 | void kvm_arch_exit(void) |
| 2788 | { |
| 2789 | kvm_x86_ops = NULL; |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 2790 | kvm_mmu_module_exit(); |
| 2791 | } |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 2792 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2793 | int kvm_emulate_halt(struct kvm_vcpu *vcpu) |
| 2794 | { |
| 2795 | ++vcpu->stat.halt_exits; |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2796 | KVMTRACE_0D(HLT, vcpu, handler); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2797 | if (irqchip_in_kernel(vcpu->kvm)) { |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 2798 | vcpu->arch.mp_state = KVM_MP_STATE_HALTED; |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 2799 | up_read(&vcpu->kvm->slots_lock); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2800 | kvm_vcpu_block(vcpu); |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 2801 | down_read(&vcpu->kvm->slots_lock); |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 2802 | if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE) |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2803 | return -EINTR; |
| 2804 | return 1; |
| 2805 | } else { |
| 2806 | vcpu->run->exit_reason = KVM_EXIT_HLT; |
| 2807 | return 0; |
| 2808 | } |
| 2809 | } |
| 2810 | EXPORT_SYMBOL_GPL(kvm_emulate_halt); |
| 2811 | |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 2812 | static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0, |
| 2813 | unsigned long a1) |
| 2814 | { |
| 2815 | if (is_long_mode(vcpu)) |
| 2816 | return a0; |
| 2817 | else |
| 2818 | return a0 | ((gpa_t)a1 << 32); |
| 2819 | } |
| 2820 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2821 | int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) |
| 2822 | { |
| 2823 | unsigned long nr, a0, a1, a2, a3, ret; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 2824 | int r = 1; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2825 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2826 | nr = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 2827 | a0 = kvm_register_read(vcpu, VCPU_REGS_RBX); |
| 2828 | a1 = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 2829 | a2 = kvm_register_read(vcpu, VCPU_REGS_RDX); |
| 2830 | a3 = kvm_register_read(vcpu, VCPU_REGS_RSI); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2831 | |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2832 | KVMTRACE_1D(VMMCALL, vcpu, (u32)nr, handler); |
| 2833 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2834 | if (!is_long_mode(vcpu)) { |
| 2835 | nr &= 0xFFFFFFFF; |
| 2836 | a0 &= 0xFFFFFFFF; |
| 2837 | a1 &= 0xFFFFFFFF; |
| 2838 | a2 &= 0xFFFFFFFF; |
| 2839 | a3 &= 0xFFFFFFFF; |
| 2840 | } |
| 2841 | |
| 2842 | switch (nr) { |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2843 | case KVM_HC_VAPIC_POLL_IRQ: |
| 2844 | ret = 0; |
| 2845 | break; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 2846 | case KVM_HC_MMU_OP: |
| 2847 | r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret); |
| 2848 | break; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2849 | default: |
| 2850 | ret = -KVM_ENOSYS; |
| 2851 | break; |
| 2852 | } |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2853 | kvm_register_write(vcpu, VCPU_REGS_RAX, ret); |
Amit Shah | f11c3a8 | 2008-02-21 01:00:30 +0530 | [diff] [blame] | 2854 | ++vcpu->stat.hypercalls; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 2855 | return r; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2856 | } |
| 2857 | EXPORT_SYMBOL_GPL(kvm_emulate_hypercall); |
| 2858 | |
| 2859 | int kvm_fix_hypercall(struct kvm_vcpu *vcpu) |
| 2860 | { |
| 2861 | char instruction[3]; |
| 2862 | int ret = 0; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2863 | unsigned long rip = kvm_rip_read(vcpu); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2864 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2865 | |
| 2866 | /* |
| 2867 | * Blow out the MMU to ensure that no other VCPU has an active mapping |
| 2868 | * to ensure that the updated hypercall appears atomically across all |
| 2869 | * VCPUs. |
| 2870 | */ |
| 2871 | kvm_mmu_zap_all(vcpu->kvm); |
| 2872 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2873 | kvm_x86_ops->patch_hypercall(vcpu, instruction); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2874 | if (emulator_write_emulated(rip, instruction, 3, vcpu) |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2875 | != X86EMUL_CONTINUE) |
| 2876 | ret = -EFAULT; |
| 2877 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2878 | return ret; |
| 2879 | } |
| 2880 | |
| 2881 | static u64 mk_cr_64(u64 curr_cr, u32 new_val) |
| 2882 | { |
| 2883 | return (curr_cr & ~((1ULL << 32) - 1)) | new_val; |
| 2884 | } |
| 2885 | |
| 2886 | void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) |
| 2887 | { |
| 2888 | struct descriptor_table dt = { limit, base }; |
| 2889 | |
| 2890 | kvm_x86_ops->set_gdt(vcpu, &dt); |
| 2891 | } |
| 2892 | |
| 2893 | void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) |
| 2894 | { |
| 2895 | struct descriptor_table dt = { limit, base }; |
| 2896 | |
| 2897 | kvm_x86_ops->set_idt(vcpu, &dt); |
| 2898 | } |
| 2899 | |
| 2900 | void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw, |
| 2901 | unsigned long *rflags) |
| 2902 | { |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 2903 | kvm_lmsw(vcpu, msw); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2904 | *rflags = kvm_x86_ops->get_rflags(vcpu); |
| 2905 | } |
| 2906 | |
| 2907 | unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr) |
| 2908 | { |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 2909 | unsigned long value; |
| 2910 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2911 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); |
| 2912 | switch (cr) { |
| 2913 | case 0: |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 2914 | value = vcpu->arch.cr0; |
| 2915 | break; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2916 | case 2: |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 2917 | value = vcpu->arch.cr2; |
| 2918 | break; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2919 | case 3: |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 2920 | value = vcpu->arch.cr3; |
| 2921 | break; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2922 | case 4: |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 2923 | value = vcpu->arch.cr4; |
| 2924 | break; |
Joerg Roedel | 152ff9b | 2007-12-06 15:46:52 +0100 | [diff] [blame] | 2925 | case 8: |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 2926 | value = kvm_get_cr8(vcpu); |
| 2927 | break; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2928 | default: |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 2929 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2930 | return 0; |
| 2931 | } |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 2932 | KVMTRACE_3D(CR_READ, vcpu, (u32)cr, (u32)value, |
| 2933 | (u32)((u64)value >> 32), handler); |
| 2934 | |
| 2935 | return value; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2936 | } |
| 2937 | |
| 2938 | void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val, |
| 2939 | unsigned long *rflags) |
| 2940 | { |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 2941 | KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr, (u32)val, |
| 2942 | (u32)((u64)val >> 32), handler); |
| 2943 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2944 | switch (cr) { |
| 2945 | case 0: |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 2946 | kvm_set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val)); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2947 | *rflags = kvm_x86_ops->get_rflags(vcpu); |
| 2948 | break; |
| 2949 | case 2: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2950 | vcpu->arch.cr2 = val; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2951 | break; |
| 2952 | case 3: |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 2953 | kvm_set_cr3(vcpu, val); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2954 | break; |
| 2955 | case 4: |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 2956 | kvm_set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val)); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2957 | break; |
Joerg Roedel | 152ff9b | 2007-12-06 15:46:52 +0100 | [diff] [blame] | 2958 | case 8: |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 2959 | kvm_set_cr8(vcpu, val & 0xfUL); |
Joerg Roedel | 152ff9b | 2007-12-06 15:46:52 +0100 | [diff] [blame] | 2960 | break; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2961 | default: |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 2962 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2963 | } |
| 2964 | } |
| 2965 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2966 | static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i) |
| 2967 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2968 | struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i]; |
| 2969 | int j, nent = vcpu->arch.cpuid_nent; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2970 | |
| 2971 | e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT; |
| 2972 | /* when no next entry is found, the current entry[i] is reselected */ |
| 2973 | for (j = i + 1; j == i; j = (j + 1) % nent) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2974 | struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j]; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2975 | if (ej->function == e->function) { |
| 2976 | ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT; |
| 2977 | return j; |
| 2978 | } |
| 2979 | } |
| 2980 | return 0; /* silence gcc, even though control never reaches here */ |
| 2981 | } |
| 2982 | |
| 2983 | /* find an entry with matching function, matching index (if needed), and that |
| 2984 | * should be read next (if it's stateful) */ |
| 2985 | static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e, |
| 2986 | u32 function, u32 index) |
| 2987 | { |
| 2988 | if (e->function != function) |
| 2989 | return 0; |
| 2990 | if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index) |
| 2991 | return 0; |
| 2992 | if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) && |
| 2993 | !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT)) |
| 2994 | return 0; |
| 2995 | return 1; |
| 2996 | } |
| 2997 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2998 | void kvm_emulate_cpuid(struct kvm_vcpu *vcpu) |
| 2999 | { |
| 3000 | int i; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 3001 | u32 function, index; |
| 3002 | struct kvm_cpuid_entry2 *e, *best; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3003 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3004 | function = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 3005 | index = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 3006 | kvm_register_write(vcpu, VCPU_REGS_RAX, 0); |
| 3007 | kvm_register_write(vcpu, VCPU_REGS_RBX, 0); |
| 3008 | kvm_register_write(vcpu, VCPU_REGS_RCX, 0); |
| 3009 | kvm_register_write(vcpu, VCPU_REGS_RDX, 0); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3010 | best = NULL; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3011 | for (i = 0; i < vcpu->arch.cpuid_nent; ++i) { |
| 3012 | e = &vcpu->arch.cpuid_entries[i]; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 3013 | if (is_matching_cpuid_entry(e, function, index)) { |
| 3014 | if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) |
| 3015 | move_to_next_stateful_cpuid_entry(vcpu, i); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3016 | best = e; |
| 3017 | break; |
| 3018 | } |
| 3019 | /* |
| 3020 | * Both basic or both extended? |
| 3021 | */ |
| 3022 | if (((e->function ^ function) & 0x80000000) == 0) |
| 3023 | if (!best || e->function > best->function) |
| 3024 | best = e; |
| 3025 | } |
| 3026 | if (best) { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3027 | kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax); |
| 3028 | kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx); |
| 3029 | kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx); |
| 3030 | kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3031 | } |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3032 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 3033 | KVMTRACE_5D(CPUID, vcpu, function, |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3034 | (u32)kvm_register_read(vcpu, VCPU_REGS_RAX), |
| 3035 | (u32)kvm_register_read(vcpu, VCPU_REGS_RBX), |
| 3036 | (u32)kvm_register_read(vcpu, VCPU_REGS_RCX), |
| 3037 | (u32)kvm_register_read(vcpu, VCPU_REGS_RDX), handler); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3038 | } |
| 3039 | EXPORT_SYMBOL_GPL(kvm_emulate_cpuid); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 3040 | |
| 3041 | /* |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3042 | * Check if userspace requested an interrupt window, and that the |
| 3043 | * interrupt window is open. |
| 3044 | * |
| 3045 | * No need to exit to userspace if we already have an interrupt queued. |
| 3046 | */ |
| 3047 | static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu, |
| 3048 | struct kvm_run *kvm_run) |
| 3049 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3050 | return (!vcpu->arch.irq_summary && |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3051 | kvm_run->request_interrupt_window && |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3052 | vcpu->arch.interrupt_window_open && |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3053 | (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF)); |
| 3054 | } |
| 3055 | |
| 3056 | static void post_kvm_run_save(struct kvm_vcpu *vcpu, |
| 3057 | struct kvm_run *kvm_run) |
| 3058 | { |
| 3059 | kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0; |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 3060 | kvm_run->cr8 = kvm_get_cr8(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3061 | kvm_run->apic_base = kvm_get_apic_base(vcpu); |
| 3062 | if (irqchip_in_kernel(vcpu->kvm)) |
| 3063 | kvm_run->ready_for_interrupt_injection = 1; |
| 3064 | else |
| 3065 | kvm_run->ready_for_interrupt_injection = |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3066 | (vcpu->arch.interrupt_window_open && |
| 3067 | vcpu->arch.irq_summary == 0); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3068 | } |
| 3069 | |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 3070 | static void vapic_enter(struct kvm_vcpu *vcpu) |
| 3071 | { |
| 3072 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 3073 | struct page *page; |
| 3074 | |
| 3075 | if (!apic || !apic->vapic_addr) |
| 3076 | return; |
| 3077 | |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3078 | down_read(¤t->mm->mmap_sem); |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 3079 | page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3080 | up_read(¤t->mm->mmap_sem); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 3081 | |
| 3082 | vcpu->arch.apic->vapic_page = page; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 3083 | } |
| 3084 | |
| 3085 | static void vapic_exit(struct kvm_vcpu *vcpu) |
| 3086 | { |
| 3087 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 3088 | |
| 3089 | if (!apic || !apic->vapic_addr) |
| 3090 | return; |
| 3091 | |
Marcelo Tosatti | f8b78fa | 2008-06-23 12:04:25 -0300 | [diff] [blame] | 3092 | down_read(&vcpu->kvm->slots_lock); |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 3093 | kvm_release_page_dirty(apic->vapic_page); |
| 3094 | mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT); |
Marcelo Tosatti | f8b78fa | 2008-06-23 12:04:25 -0300 | [diff] [blame] | 3095 | up_read(&vcpu->kvm->slots_lock); |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 3096 | } |
| 3097 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3098 | static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 3099 | { |
| 3100 | int r; |
| 3101 | |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 3102 | if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) { |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3103 | pr_debug("vcpu %d received sipi with vector # %x\n", |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3104 | vcpu->vcpu_id, vcpu->arch.sipi_vector); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3105 | kvm_lapic_reset(vcpu); |
| 3106 | r = kvm_x86_ops->vcpu_reset(vcpu); |
| 3107 | if (r) |
| 3108 | return r; |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 3109 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3110 | } |
| 3111 | |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 3112 | down_read(&vcpu->kvm->slots_lock); |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 3113 | vapic_enter(vcpu); |
| 3114 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3115 | again: |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 3116 | if (vcpu->requests) |
| 3117 | if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests)) |
| 3118 | kvm_mmu_unload(vcpu); |
| 3119 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3120 | r = kvm_mmu_reload(vcpu); |
| 3121 | if (unlikely(r)) |
| 3122 | goto out; |
| 3123 | |
Avi Kivity | 2f52d58 | 2008-01-16 12:49:30 +0200 | [diff] [blame] | 3124 | if (vcpu->requests) { |
| 3125 | if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests)) |
Marcelo Tosatti | 2f59971 | 2008-05-27 12:10:20 -0300 | [diff] [blame] | 3126 | __kvm_migrate_timers(vcpu); |
Marcelo Tosatti | d4acf7e | 2008-06-06 16:37:35 -0300 | [diff] [blame] | 3127 | if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests)) |
| 3128 | kvm_x86_ops->tlb_flush(vcpu); |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 3129 | if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS, |
| 3130 | &vcpu->requests)) { |
| 3131 | kvm_run->exit_reason = KVM_EXIT_TPR_ACCESS; |
| 3132 | r = 0; |
| 3133 | goto out; |
| 3134 | } |
Joerg Roedel | 71c4dfa | 2008-02-26 16:49:16 +0100 | [diff] [blame] | 3135 | if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) { |
| 3136 | kvm_run->exit_reason = KVM_EXIT_SHUTDOWN; |
| 3137 | r = 0; |
| 3138 | goto out; |
| 3139 | } |
Avi Kivity | 2f52d58 | 2008-01-16 12:49:30 +0200 | [diff] [blame] | 3140 | } |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 3141 | |
Marcelo Tosatti | 06e0564 | 2008-06-06 16:37:36 -0300 | [diff] [blame] | 3142 | clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3143 | kvm_inject_pending_timer_irqs(vcpu); |
| 3144 | |
| 3145 | preempt_disable(); |
| 3146 | |
| 3147 | kvm_x86_ops->prepare_guest_switch(vcpu); |
| 3148 | kvm_load_guest_fpu(vcpu); |
| 3149 | |
| 3150 | local_irq_disable(); |
| 3151 | |
Marcelo Tosatti | d4acf7e | 2008-06-06 16:37:35 -0300 | [diff] [blame] | 3152 | if (vcpu->requests || need_resched()) { |
Avi Kivity | 6c14280 | 2008-01-15 18:27:32 +0200 | [diff] [blame] | 3153 | local_irq_enable(); |
| 3154 | preempt_enable(); |
| 3155 | r = 1; |
| 3156 | goto out; |
| 3157 | } |
| 3158 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3159 | if (signal_pending(current)) { |
| 3160 | local_irq_enable(); |
| 3161 | preempt_enable(); |
| 3162 | r = -EINTR; |
| 3163 | kvm_run->exit_reason = KVM_EXIT_INTR; |
| 3164 | ++vcpu->stat.signal_exits; |
| 3165 | goto out; |
| 3166 | } |
| 3167 | |
Marcelo Tosatti | 29415c3 | 2008-08-01 20:09:13 -0300 | [diff] [blame] | 3168 | if (vcpu->guest_debug.enabled) |
| 3169 | kvm_x86_ops->guest_debug_pre(vcpu); |
| 3170 | |
Marcelo Tosatti | e9571ed | 2008-04-11 15:01:22 -0300 | [diff] [blame] | 3171 | vcpu->guest_mode = 1; |
| 3172 | /* |
| 3173 | * Make sure that guest_mode assignment won't happen after |
| 3174 | * testing the pending IRQ vector bitmap. |
| 3175 | */ |
| 3176 | smp_wmb(); |
| 3177 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3178 | if (vcpu->arch.exception.pending) |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 3179 | __queue_exception(vcpu); |
| 3180 | else if (irqchip_in_kernel(vcpu->kvm)) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3181 | kvm_x86_ops->inject_pending_irq(vcpu); |
Avi Kivity | eb9774f | 2007-11-25 17:45:31 +0200 | [diff] [blame] | 3182 | else |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3183 | kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run); |
| 3184 | |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 3185 | kvm_lapic_sync_to_vapic(vcpu); |
| 3186 | |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 3187 | up_read(&vcpu->kvm->slots_lock); |
| 3188 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3189 | kvm_guest_enter(); |
| 3190 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3191 | |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 3192 | KVMTRACE_0D(VMENTRY, vcpu, entryexit); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3193 | kvm_x86_ops->run(vcpu, kvm_run); |
| 3194 | |
| 3195 | vcpu->guest_mode = 0; |
| 3196 | local_irq_enable(); |
| 3197 | |
| 3198 | ++vcpu->stat.exits; |
| 3199 | |
| 3200 | /* |
| 3201 | * We must have an instruction between local_irq_enable() and |
| 3202 | * kvm_guest_exit(), so the timer interrupt isn't delayed by |
| 3203 | * the interrupt shadow. The stat.exits increment will do nicely. |
| 3204 | * But we need to prevent reordering, hence this barrier(): |
| 3205 | */ |
| 3206 | barrier(); |
| 3207 | |
| 3208 | kvm_guest_exit(); |
| 3209 | |
| 3210 | preempt_enable(); |
| 3211 | |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 3212 | down_read(&vcpu->kvm->slots_lock); |
| 3213 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3214 | /* |
| 3215 | * Profile KVM exit RIPs: |
| 3216 | */ |
| 3217 | if (unlikely(prof_on == KVM_PROFILING)) { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3218 | unsigned long rip = kvm_rip_read(vcpu); |
| 3219 | profile_hit(KVM_PROFILING, (void *)rip); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3220 | } |
| 3221 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3222 | if (vcpu->arch.exception.pending && kvm_x86_ops->exception_injected(vcpu)) |
| 3223 | vcpu->arch.exception.pending = false; |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 3224 | |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 3225 | kvm_lapic_sync_from_vapic(vcpu); |
| 3226 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3227 | r = kvm_x86_ops->handle_exit(kvm_run, vcpu); |
| 3228 | |
| 3229 | if (r > 0) { |
| 3230 | if (dm_request_for_irq_injection(vcpu, kvm_run)) { |
| 3231 | r = -EINTR; |
| 3232 | kvm_run->exit_reason = KVM_EXIT_INTR; |
| 3233 | ++vcpu->stat.request_irq_exits; |
| 3234 | goto out; |
| 3235 | } |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 3236 | if (!need_resched()) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3237 | goto again; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3238 | } |
| 3239 | |
| 3240 | out: |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 3241 | up_read(&vcpu->kvm->slots_lock); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3242 | if (r > 0) { |
| 3243 | kvm_resched(vcpu); |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 3244 | down_read(&vcpu->kvm->slots_lock); |
Marcelo Tosatti | 29415c3 | 2008-08-01 20:09:13 -0300 | [diff] [blame] | 3245 | goto again; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3246 | } |
| 3247 | |
| 3248 | post_kvm_run_save(vcpu, kvm_run); |
| 3249 | |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 3250 | vapic_exit(vcpu); |
| 3251 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3252 | return r; |
| 3253 | } |
| 3254 | |
| 3255 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 3256 | { |
| 3257 | int r; |
| 3258 | sigset_t sigsaved; |
| 3259 | |
| 3260 | vcpu_load(vcpu); |
| 3261 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3262 | if (vcpu->sigset_active) |
| 3263 | sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); |
| 3264 | |
Avi Kivity | ac9f6dc | 2008-07-06 15:48:31 +0300 | [diff] [blame] | 3265 | if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) { |
| 3266 | kvm_vcpu_block(vcpu); |
| 3267 | r = -EAGAIN; |
| 3268 | goto out; |
| 3269 | } |
| 3270 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3271 | /* re-sync apic's tpr */ |
| 3272 | if (!irqchip_in_kernel(vcpu->kvm)) |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 3273 | kvm_set_cr8(vcpu, kvm_run->cr8); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3274 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3275 | if (vcpu->arch.pio.cur_count) { |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3276 | r = complete_pio(vcpu); |
| 3277 | if (r) |
| 3278 | goto out; |
| 3279 | } |
| 3280 | #if CONFIG_HAS_IOMEM |
| 3281 | if (vcpu->mmio_needed) { |
| 3282 | memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8); |
| 3283 | vcpu->mmio_read_completed = 1; |
| 3284 | vcpu->mmio_needed = 0; |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 3285 | |
| 3286 | down_read(&vcpu->kvm->slots_lock); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3287 | r = emulate_instruction(vcpu, kvm_run, |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 3288 | vcpu->arch.mmio_fault_cr2, 0, |
| 3289 | EMULTYPE_NO_DECODE); |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 3290 | up_read(&vcpu->kvm->slots_lock); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3291 | if (r == EMULATE_DO_MMIO) { |
| 3292 | /* |
| 3293 | * Read-modify-write. Back to userspace. |
| 3294 | */ |
| 3295 | r = 0; |
| 3296 | goto out; |
| 3297 | } |
| 3298 | } |
| 3299 | #endif |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3300 | if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) |
| 3301 | kvm_register_write(vcpu, VCPU_REGS_RAX, |
| 3302 | kvm_run->hypercall.ret); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3303 | |
| 3304 | r = __vcpu_run(vcpu, kvm_run); |
| 3305 | |
| 3306 | out: |
| 3307 | if (vcpu->sigset_active) |
| 3308 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); |
| 3309 | |
| 3310 | vcpu_put(vcpu); |
| 3311 | return r; |
| 3312 | } |
| 3313 | |
| 3314 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 3315 | { |
| 3316 | vcpu_load(vcpu); |
| 3317 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3318 | regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 3319 | regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX); |
| 3320 | regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 3321 | regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX); |
| 3322 | regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI); |
| 3323 | regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI); |
| 3324 | regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 3325 | regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3326 | #ifdef CONFIG_X86_64 |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3327 | regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8); |
| 3328 | regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9); |
| 3329 | regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10); |
| 3330 | regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11); |
| 3331 | regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12); |
| 3332 | regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13); |
| 3333 | regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14); |
| 3334 | regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3335 | #endif |
| 3336 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3337 | regs->rip = kvm_rip_read(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3338 | regs->rflags = kvm_x86_ops->get_rflags(vcpu); |
| 3339 | |
| 3340 | /* |
| 3341 | * Don't leak debug flags in case they were set for guest debugging |
| 3342 | */ |
| 3343 | if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep) |
| 3344 | regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF); |
| 3345 | |
| 3346 | vcpu_put(vcpu); |
| 3347 | |
| 3348 | return 0; |
| 3349 | } |
| 3350 | |
| 3351 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 3352 | { |
| 3353 | vcpu_load(vcpu); |
| 3354 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3355 | kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax); |
| 3356 | kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx); |
| 3357 | kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx); |
| 3358 | kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx); |
| 3359 | kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi); |
| 3360 | kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi); |
| 3361 | kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp); |
| 3362 | kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3363 | #ifdef CONFIG_X86_64 |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3364 | kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8); |
| 3365 | kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9); |
| 3366 | kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10); |
| 3367 | kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11); |
| 3368 | kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12); |
| 3369 | kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13); |
| 3370 | kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14); |
| 3371 | kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15); |
| 3372 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3373 | #endif |
| 3374 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3375 | kvm_rip_write(vcpu, regs->rip); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3376 | kvm_x86_ops->set_rflags(vcpu, regs->rflags); |
| 3377 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3378 | |
Jan Kiszka | b4f14ab | 2008-04-30 17:59:04 +0200 | [diff] [blame] | 3379 | vcpu->arch.exception.pending = false; |
| 3380 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3381 | vcpu_put(vcpu); |
| 3382 | |
| 3383 | return 0; |
| 3384 | } |
| 3385 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3386 | void kvm_get_segment(struct kvm_vcpu *vcpu, |
| 3387 | struct kvm_segment *var, int seg) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3388 | { |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 3389 | kvm_x86_ops->get_segment(vcpu, var, seg); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3390 | } |
| 3391 | |
| 3392 | void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 3393 | { |
| 3394 | struct kvm_segment cs; |
| 3395 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3396 | kvm_get_segment(vcpu, &cs, VCPU_SREG_CS); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3397 | *db = cs.db; |
| 3398 | *l = cs.l; |
| 3399 | } |
| 3400 | EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits); |
| 3401 | |
| 3402 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
| 3403 | struct kvm_sregs *sregs) |
| 3404 | { |
| 3405 | struct descriptor_table dt; |
| 3406 | int pending_vec; |
| 3407 | |
| 3408 | vcpu_load(vcpu); |
| 3409 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3410 | kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
| 3411 | kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); |
| 3412 | kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES); |
| 3413 | kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); |
| 3414 | kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); |
| 3415 | kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3416 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3417 | kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
| 3418 | kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3419 | |
| 3420 | kvm_x86_ops->get_idt(vcpu, &dt); |
| 3421 | sregs->idt.limit = dt.limit; |
| 3422 | sregs->idt.base = dt.base; |
| 3423 | kvm_x86_ops->get_gdt(vcpu, &dt); |
| 3424 | sregs->gdt.limit = dt.limit; |
| 3425 | sregs->gdt.base = dt.base; |
| 3426 | |
| 3427 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3428 | sregs->cr0 = vcpu->arch.cr0; |
| 3429 | sregs->cr2 = vcpu->arch.cr2; |
| 3430 | sregs->cr3 = vcpu->arch.cr3; |
| 3431 | sregs->cr4 = vcpu->arch.cr4; |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 3432 | sregs->cr8 = kvm_get_cr8(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3433 | sregs->efer = vcpu->arch.shadow_efer; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3434 | sregs->apic_base = kvm_get_apic_base(vcpu); |
| 3435 | |
| 3436 | if (irqchip_in_kernel(vcpu->kvm)) { |
| 3437 | memset(sregs->interrupt_bitmap, 0, |
| 3438 | sizeof sregs->interrupt_bitmap); |
| 3439 | pending_vec = kvm_x86_ops->get_irq(vcpu); |
| 3440 | if (pending_vec >= 0) |
| 3441 | set_bit(pending_vec, |
| 3442 | (unsigned long *)sregs->interrupt_bitmap); |
| 3443 | } else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3444 | memcpy(sregs->interrupt_bitmap, vcpu->arch.irq_pending, |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3445 | sizeof sregs->interrupt_bitmap); |
| 3446 | |
| 3447 | vcpu_put(vcpu); |
| 3448 | |
| 3449 | return 0; |
| 3450 | } |
| 3451 | |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 3452 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
| 3453 | struct kvm_mp_state *mp_state) |
| 3454 | { |
| 3455 | vcpu_load(vcpu); |
| 3456 | mp_state->mp_state = vcpu->arch.mp_state; |
| 3457 | vcpu_put(vcpu); |
| 3458 | return 0; |
| 3459 | } |
| 3460 | |
| 3461 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, |
| 3462 | struct kvm_mp_state *mp_state) |
| 3463 | { |
| 3464 | vcpu_load(vcpu); |
| 3465 | vcpu->arch.mp_state = mp_state->mp_state; |
| 3466 | vcpu_put(vcpu); |
| 3467 | return 0; |
| 3468 | } |
| 3469 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3470 | static void kvm_set_segment(struct kvm_vcpu *vcpu, |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3471 | struct kvm_segment *var, int seg) |
| 3472 | { |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 3473 | kvm_x86_ops->set_segment(vcpu, var, seg); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3474 | } |
| 3475 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3476 | static void seg_desct_to_kvm_desct(struct desc_struct *seg_desc, u16 selector, |
| 3477 | struct kvm_segment *kvm_desct) |
| 3478 | { |
| 3479 | kvm_desct->base = seg_desc->base0; |
| 3480 | kvm_desct->base |= seg_desc->base1 << 16; |
| 3481 | kvm_desct->base |= seg_desc->base2 << 24; |
| 3482 | kvm_desct->limit = seg_desc->limit0; |
| 3483 | kvm_desct->limit |= seg_desc->limit << 16; |
Marcelo Tosatti | c93cd3a | 2008-07-19 19:08:07 -0300 | [diff] [blame] | 3484 | if (seg_desc->g) { |
| 3485 | kvm_desct->limit <<= 12; |
| 3486 | kvm_desct->limit |= 0xfff; |
| 3487 | } |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3488 | kvm_desct->selector = selector; |
| 3489 | kvm_desct->type = seg_desc->type; |
| 3490 | kvm_desct->present = seg_desc->p; |
| 3491 | kvm_desct->dpl = seg_desc->dpl; |
| 3492 | kvm_desct->db = seg_desc->d; |
| 3493 | kvm_desct->s = seg_desc->s; |
| 3494 | kvm_desct->l = seg_desc->l; |
| 3495 | kvm_desct->g = seg_desc->g; |
| 3496 | kvm_desct->avl = seg_desc->avl; |
| 3497 | if (!selector) |
| 3498 | kvm_desct->unusable = 1; |
| 3499 | else |
| 3500 | kvm_desct->unusable = 0; |
| 3501 | kvm_desct->padding = 0; |
| 3502 | } |
| 3503 | |
| 3504 | static void get_segment_descritptor_dtable(struct kvm_vcpu *vcpu, |
| 3505 | u16 selector, |
| 3506 | struct descriptor_table *dtable) |
| 3507 | { |
| 3508 | if (selector & 1 << 2) { |
| 3509 | struct kvm_segment kvm_seg; |
| 3510 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3511 | kvm_get_segment(vcpu, &kvm_seg, VCPU_SREG_LDTR); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3512 | |
| 3513 | if (kvm_seg.unusable) |
| 3514 | dtable->limit = 0; |
| 3515 | else |
| 3516 | dtable->limit = kvm_seg.limit; |
| 3517 | dtable->base = kvm_seg.base; |
| 3518 | } |
| 3519 | else |
| 3520 | kvm_x86_ops->get_gdt(vcpu, dtable); |
| 3521 | } |
| 3522 | |
| 3523 | /* allowed just for 8 bytes segments */ |
| 3524 | static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector, |
| 3525 | struct desc_struct *seg_desc) |
| 3526 | { |
Marcelo Tosatti | 98899aa | 2008-07-16 19:07:10 -0300 | [diff] [blame] | 3527 | gpa_t gpa; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3528 | struct descriptor_table dtable; |
| 3529 | u16 index = selector >> 3; |
| 3530 | |
| 3531 | get_segment_descritptor_dtable(vcpu, selector, &dtable); |
| 3532 | |
| 3533 | if (dtable.limit < index * 8 + 7) { |
| 3534 | kvm_queue_exception_e(vcpu, GP_VECTOR, selector & 0xfffc); |
| 3535 | return 1; |
| 3536 | } |
Marcelo Tosatti | 98899aa | 2008-07-16 19:07:10 -0300 | [diff] [blame] | 3537 | gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, dtable.base); |
| 3538 | gpa += index * 8; |
| 3539 | return kvm_read_guest(vcpu->kvm, gpa, seg_desc, 8); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3540 | } |
| 3541 | |
| 3542 | /* allowed just for 8 bytes segments */ |
| 3543 | static int save_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector, |
| 3544 | struct desc_struct *seg_desc) |
| 3545 | { |
Marcelo Tosatti | 98899aa | 2008-07-16 19:07:10 -0300 | [diff] [blame] | 3546 | gpa_t gpa; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3547 | struct descriptor_table dtable; |
| 3548 | u16 index = selector >> 3; |
| 3549 | |
| 3550 | get_segment_descritptor_dtable(vcpu, selector, &dtable); |
| 3551 | |
| 3552 | if (dtable.limit < index * 8 + 7) |
| 3553 | return 1; |
Marcelo Tosatti | 98899aa | 2008-07-16 19:07:10 -0300 | [diff] [blame] | 3554 | gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, dtable.base); |
| 3555 | gpa += index * 8; |
| 3556 | return kvm_write_guest(vcpu->kvm, gpa, seg_desc, 8); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3557 | } |
| 3558 | |
| 3559 | static u32 get_tss_base_addr(struct kvm_vcpu *vcpu, |
| 3560 | struct desc_struct *seg_desc) |
| 3561 | { |
| 3562 | u32 base_addr; |
| 3563 | |
| 3564 | base_addr = seg_desc->base0; |
| 3565 | base_addr |= (seg_desc->base1 << 16); |
| 3566 | base_addr |= (seg_desc->base2 << 24); |
| 3567 | |
Marcelo Tosatti | 98899aa | 2008-07-16 19:07:10 -0300 | [diff] [blame] | 3568 | return vcpu->arch.mmu.gva_to_gpa(vcpu, base_addr); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3569 | } |
| 3570 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3571 | static u16 get_segment_selector(struct kvm_vcpu *vcpu, int seg) |
| 3572 | { |
| 3573 | struct kvm_segment kvm_seg; |
| 3574 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3575 | kvm_get_segment(vcpu, &kvm_seg, seg); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3576 | return kvm_seg.selector; |
| 3577 | } |
| 3578 | |
| 3579 | static int load_segment_descriptor_to_kvm_desct(struct kvm_vcpu *vcpu, |
| 3580 | u16 selector, |
| 3581 | struct kvm_segment *kvm_seg) |
| 3582 | { |
| 3583 | struct desc_struct seg_desc; |
| 3584 | |
| 3585 | if (load_guest_segment_descriptor(vcpu, selector, &seg_desc)) |
| 3586 | return 1; |
| 3587 | seg_desct_to_kvm_desct(&seg_desc, selector, kvm_seg); |
| 3588 | return 0; |
| 3589 | } |
| 3590 | |
Avi Kivity | f4bbd9a | 2008-08-20 15:51:42 +0300 | [diff] [blame^] | 3591 | int kvm_load_realmode_segment(struct kvm_vcpu *vcpu, u16 selector, int seg) |
| 3592 | { |
| 3593 | struct kvm_segment segvar = { |
| 3594 | .base = selector << 4, |
| 3595 | .limit = 0xffff, |
| 3596 | .selector = selector, |
| 3597 | .type = 3, |
| 3598 | .present = 1, |
| 3599 | .dpl = 3, |
| 3600 | .db = 0, |
| 3601 | .s = 1, |
| 3602 | .l = 0, |
| 3603 | .g = 0, |
| 3604 | .avl = 0, |
| 3605 | .unusable = 0, |
| 3606 | }; |
| 3607 | kvm_x86_ops->set_segment(vcpu, &segvar, seg); |
| 3608 | return 0; |
| 3609 | } |
| 3610 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3611 | int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector, |
| 3612 | int type_bits, int seg) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3613 | { |
| 3614 | struct kvm_segment kvm_seg; |
| 3615 | |
Avi Kivity | f4bbd9a | 2008-08-20 15:51:42 +0300 | [diff] [blame^] | 3616 | if (!(vcpu->arch.cr0 & X86_CR0_PE)) |
| 3617 | return kvm_load_realmode_segment(vcpu, selector, seg); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3618 | if (load_segment_descriptor_to_kvm_desct(vcpu, selector, &kvm_seg)) |
| 3619 | return 1; |
| 3620 | kvm_seg.type |= type_bits; |
| 3621 | |
| 3622 | if (seg != VCPU_SREG_SS && seg != VCPU_SREG_CS && |
| 3623 | seg != VCPU_SREG_LDTR) |
| 3624 | if (!kvm_seg.s) |
| 3625 | kvm_seg.unusable = 1; |
| 3626 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3627 | kvm_set_segment(vcpu, &kvm_seg, seg); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3628 | return 0; |
| 3629 | } |
| 3630 | |
| 3631 | static void save_state_to_tss32(struct kvm_vcpu *vcpu, |
| 3632 | struct tss_segment_32 *tss) |
| 3633 | { |
| 3634 | tss->cr3 = vcpu->arch.cr3; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3635 | tss->eip = kvm_rip_read(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3636 | tss->eflags = kvm_x86_ops->get_rflags(vcpu); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3637 | tss->eax = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 3638 | tss->ecx = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 3639 | tss->edx = kvm_register_read(vcpu, VCPU_REGS_RDX); |
| 3640 | tss->ebx = kvm_register_read(vcpu, VCPU_REGS_RBX); |
| 3641 | tss->esp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 3642 | tss->ebp = kvm_register_read(vcpu, VCPU_REGS_RBP); |
| 3643 | tss->esi = kvm_register_read(vcpu, VCPU_REGS_RSI); |
| 3644 | tss->edi = kvm_register_read(vcpu, VCPU_REGS_RDI); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3645 | tss->es = get_segment_selector(vcpu, VCPU_SREG_ES); |
| 3646 | tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS); |
| 3647 | tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS); |
| 3648 | tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS); |
| 3649 | tss->fs = get_segment_selector(vcpu, VCPU_SREG_FS); |
| 3650 | tss->gs = get_segment_selector(vcpu, VCPU_SREG_GS); |
| 3651 | tss->ldt_selector = get_segment_selector(vcpu, VCPU_SREG_LDTR); |
| 3652 | tss->prev_task_link = get_segment_selector(vcpu, VCPU_SREG_TR); |
| 3653 | } |
| 3654 | |
| 3655 | static int load_state_from_tss32(struct kvm_vcpu *vcpu, |
| 3656 | struct tss_segment_32 *tss) |
| 3657 | { |
| 3658 | kvm_set_cr3(vcpu, tss->cr3); |
| 3659 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3660 | kvm_rip_write(vcpu, tss->eip); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3661 | kvm_x86_ops->set_rflags(vcpu, tss->eflags | 2); |
| 3662 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3663 | kvm_register_write(vcpu, VCPU_REGS_RAX, tss->eax); |
| 3664 | kvm_register_write(vcpu, VCPU_REGS_RCX, tss->ecx); |
| 3665 | kvm_register_write(vcpu, VCPU_REGS_RDX, tss->edx); |
| 3666 | kvm_register_write(vcpu, VCPU_REGS_RBX, tss->ebx); |
| 3667 | kvm_register_write(vcpu, VCPU_REGS_RSP, tss->esp); |
| 3668 | kvm_register_write(vcpu, VCPU_REGS_RBP, tss->ebp); |
| 3669 | kvm_register_write(vcpu, VCPU_REGS_RSI, tss->esi); |
| 3670 | kvm_register_write(vcpu, VCPU_REGS_RDI, tss->edi); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3671 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3672 | if (kvm_load_segment_descriptor(vcpu, tss->ldt_selector, 0, VCPU_SREG_LDTR)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3673 | return 1; |
| 3674 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3675 | if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3676 | return 1; |
| 3677 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3678 | if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3679 | return 1; |
| 3680 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3681 | if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3682 | return 1; |
| 3683 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3684 | if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3685 | return 1; |
| 3686 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3687 | if (kvm_load_segment_descriptor(vcpu, tss->fs, 1, VCPU_SREG_FS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3688 | return 1; |
| 3689 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3690 | if (kvm_load_segment_descriptor(vcpu, tss->gs, 1, VCPU_SREG_GS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3691 | return 1; |
| 3692 | return 0; |
| 3693 | } |
| 3694 | |
| 3695 | static void save_state_to_tss16(struct kvm_vcpu *vcpu, |
| 3696 | struct tss_segment_16 *tss) |
| 3697 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3698 | tss->ip = kvm_rip_read(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3699 | tss->flag = kvm_x86_ops->get_rflags(vcpu); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3700 | tss->ax = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 3701 | tss->cx = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 3702 | tss->dx = kvm_register_read(vcpu, VCPU_REGS_RDX); |
| 3703 | tss->bx = kvm_register_read(vcpu, VCPU_REGS_RBX); |
| 3704 | tss->sp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 3705 | tss->bp = kvm_register_read(vcpu, VCPU_REGS_RBP); |
| 3706 | tss->si = kvm_register_read(vcpu, VCPU_REGS_RSI); |
| 3707 | tss->di = kvm_register_read(vcpu, VCPU_REGS_RDI); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3708 | |
| 3709 | tss->es = get_segment_selector(vcpu, VCPU_SREG_ES); |
| 3710 | tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS); |
| 3711 | tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS); |
| 3712 | tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS); |
| 3713 | tss->ldt = get_segment_selector(vcpu, VCPU_SREG_LDTR); |
| 3714 | tss->prev_task_link = get_segment_selector(vcpu, VCPU_SREG_TR); |
| 3715 | } |
| 3716 | |
| 3717 | static int load_state_from_tss16(struct kvm_vcpu *vcpu, |
| 3718 | struct tss_segment_16 *tss) |
| 3719 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3720 | kvm_rip_write(vcpu, tss->ip); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3721 | kvm_x86_ops->set_rflags(vcpu, tss->flag | 2); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3722 | kvm_register_write(vcpu, VCPU_REGS_RAX, tss->ax); |
| 3723 | kvm_register_write(vcpu, VCPU_REGS_RCX, tss->cx); |
| 3724 | kvm_register_write(vcpu, VCPU_REGS_RDX, tss->dx); |
| 3725 | kvm_register_write(vcpu, VCPU_REGS_RBX, tss->bx); |
| 3726 | kvm_register_write(vcpu, VCPU_REGS_RSP, tss->sp); |
| 3727 | kvm_register_write(vcpu, VCPU_REGS_RBP, tss->bp); |
| 3728 | kvm_register_write(vcpu, VCPU_REGS_RSI, tss->si); |
| 3729 | kvm_register_write(vcpu, VCPU_REGS_RDI, tss->di); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3730 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3731 | if (kvm_load_segment_descriptor(vcpu, tss->ldt, 0, VCPU_SREG_LDTR)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3732 | return 1; |
| 3733 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3734 | if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3735 | return 1; |
| 3736 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3737 | if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3738 | return 1; |
| 3739 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3740 | if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3741 | return 1; |
| 3742 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3743 | if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3744 | return 1; |
| 3745 | return 0; |
| 3746 | } |
| 3747 | |
Harvey Harrison | 8b2cf73 | 2008-04-27 12:14:13 -0700 | [diff] [blame] | 3748 | static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector, |
Marcelo Tosatti | 34198bf | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3749 | u32 old_tss_base, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3750 | struct desc_struct *nseg_desc) |
| 3751 | { |
| 3752 | struct tss_segment_16 tss_segment_16; |
| 3753 | int ret = 0; |
| 3754 | |
Marcelo Tosatti | 34198bf | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3755 | if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_16, |
| 3756 | sizeof tss_segment_16)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3757 | goto out; |
| 3758 | |
| 3759 | save_state_to_tss16(vcpu, &tss_segment_16); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3760 | |
Marcelo Tosatti | 34198bf | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3761 | if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_16, |
| 3762 | sizeof tss_segment_16)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3763 | goto out; |
Marcelo Tosatti | 34198bf | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3764 | |
| 3765 | if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc), |
| 3766 | &tss_segment_16, sizeof tss_segment_16)) |
| 3767 | goto out; |
| 3768 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3769 | if (load_state_from_tss16(vcpu, &tss_segment_16)) |
| 3770 | goto out; |
| 3771 | |
| 3772 | ret = 1; |
| 3773 | out: |
| 3774 | return ret; |
| 3775 | } |
| 3776 | |
Harvey Harrison | 8b2cf73 | 2008-04-27 12:14:13 -0700 | [diff] [blame] | 3777 | static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector, |
Marcelo Tosatti | 34198bf | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3778 | u32 old_tss_base, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3779 | struct desc_struct *nseg_desc) |
| 3780 | { |
| 3781 | struct tss_segment_32 tss_segment_32; |
| 3782 | int ret = 0; |
| 3783 | |
Marcelo Tosatti | 34198bf | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3784 | if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_32, |
| 3785 | sizeof tss_segment_32)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3786 | goto out; |
| 3787 | |
| 3788 | save_state_to_tss32(vcpu, &tss_segment_32); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3789 | |
Marcelo Tosatti | 34198bf | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3790 | if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_32, |
| 3791 | sizeof tss_segment_32)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3792 | goto out; |
Marcelo Tosatti | 34198bf | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3793 | |
| 3794 | if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc), |
| 3795 | &tss_segment_32, sizeof tss_segment_32)) |
| 3796 | goto out; |
| 3797 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3798 | if (load_state_from_tss32(vcpu, &tss_segment_32)) |
| 3799 | goto out; |
| 3800 | |
| 3801 | ret = 1; |
| 3802 | out: |
| 3803 | return ret; |
| 3804 | } |
| 3805 | |
| 3806 | int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason) |
| 3807 | { |
| 3808 | struct kvm_segment tr_seg; |
| 3809 | struct desc_struct cseg_desc; |
| 3810 | struct desc_struct nseg_desc; |
| 3811 | int ret = 0; |
Marcelo Tosatti | 34198bf | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3812 | u32 old_tss_base = get_segment_base(vcpu, VCPU_SREG_TR); |
| 3813 | u16 old_tss_sel = get_segment_selector(vcpu, VCPU_SREG_TR); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3814 | |
Marcelo Tosatti | 34198bf | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3815 | old_tss_base = vcpu->arch.mmu.gva_to_gpa(vcpu, old_tss_base); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3816 | |
Marcelo Tosatti | 34198bf | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3817 | /* FIXME: Handle errors. Failure to read either TSS or their |
| 3818 | * descriptors should generate a pagefault. |
| 3819 | */ |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3820 | if (load_guest_segment_descriptor(vcpu, tss_selector, &nseg_desc)) |
| 3821 | goto out; |
| 3822 | |
Marcelo Tosatti | 34198bf | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3823 | if (load_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3824 | goto out; |
| 3825 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3826 | if (reason != TASK_SWITCH_IRET) { |
| 3827 | int cpl; |
| 3828 | |
| 3829 | cpl = kvm_x86_ops->get_cpl(vcpu); |
| 3830 | if ((tss_selector & 3) > nseg_desc.dpl || cpl > nseg_desc.dpl) { |
| 3831 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 3832 | return 1; |
| 3833 | } |
| 3834 | } |
| 3835 | |
| 3836 | if (!nseg_desc.p || (nseg_desc.limit0 | nseg_desc.limit << 16) < 0x67) { |
| 3837 | kvm_queue_exception_e(vcpu, TS_VECTOR, tss_selector & 0xfffc); |
| 3838 | return 1; |
| 3839 | } |
| 3840 | |
| 3841 | if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) { |
Izik Eidus | 3fe913e | 2008-04-28 18:23:52 +0300 | [diff] [blame] | 3842 | cseg_desc.type &= ~(1 << 1); //clear the B flag |
Marcelo Tosatti | 34198bf | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3843 | save_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3844 | } |
| 3845 | |
| 3846 | if (reason == TASK_SWITCH_IRET) { |
| 3847 | u32 eflags = kvm_x86_ops->get_rflags(vcpu); |
| 3848 | kvm_x86_ops->set_rflags(vcpu, eflags & ~X86_EFLAGS_NT); |
| 3849 | } |
| 3850 | |
| 3851 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3852 | |
| 3853 | if (nseg_desc.type & 8) |
Marcelo Tosatti | 34198bf | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3854 | ret = kvm_task_switch_32(vcpu, tss_selector, old_tss_base, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3855 | &nseg_desc); |
| 3856 | else |
Marcelo Tosatti | 34198bf | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3857 | ret = kvm_task_switch_16(vcpu, tss_selector, old_tss_base, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3858 | &nseg_desc); |
| 3859 | |
| 3860 | if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) { |
| 3861 | u32 eflags = kvm_x86_ops->get_rflags(vcpu); |
| 3862 | kvm_x86_ops->set_rflags(vcpu, eflags | X86_EFLAGS_NT); |
| 3863 | } |
| 3864 | |
| 3865 | if (reason != TASK_SWITCH_IRET) { |
Izik Eidus | 3fe913e | 2008-04-28 18:23:52 +0300 | [diff] [blame] | 3866 | nseg_desc.type |= (1 << 1); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3867 | save_guest_segment_descriptor(vcpu, tss_selector, |
| 3868 | &nseg_desc); |
| 3869 | } |
| 3870 | |
| 3871 | kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 | X86_CR0_TS); |
| 3872 | seg_desct_to_kvm_desct(&nseg_desc, tss_selector, &tr_seg); |
| 3873 | tr_seg.type = 11; |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3874 | kvm_set_segment(vcpu, &tr_seg, VCPU_SREG_TR); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3875 | out: |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3876 | return ret; |
| 3877 | } |
| 3878 | EXPORT_SYMBOL_GPL(kvm_task_switch); |
| 3879 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3880 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, |
| 3881 | struct kvm_sregs *sregs) |
| 3882 | { |
| 3883 | int mmu_reset_needed = 0; |
| 3884 | int i, pending_vec, max_bits; |
| 3885 | struct descriptor_table dt; |
| 3886 | |
| 3887 | vcpu_load(vcpu); |
| 3888 | |
| 3889 | dt.limit = sregs->idt.limit; |
| 3890 | dt.base = sregs->idt.base; |
| 3891 | kvm_x86_ops->set_idt(vcpu, &dt); |
| 3892 | dt.limit = sregs->gdt.limit; |
| 3893 | dt.base = sregs->gdt.base; |
| 3894 | kvm_x86_ops->set_gdt(vcpu, &dt); |
| 3895 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3896 | vcpu->arch.cr2 = sregs->cr2; |
| 3897 | mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3; |
| 3898 | vcpu->arch.cr3 = sregs->cr3; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3899 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 3900 | kvm_set_cr8(vcpu, sregs->cr8); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3901 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3902 | mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3903 | kvm_x86_ops->set_efer(vcpu, sregs->efer); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3904 | kvm_set_apic_base(vcpu, sregs->apic_base); |
| 3905 | |
| 3906 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); |
| 3907 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3908 | mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3909 | kvm_x86_ops->set_cr0(vcpu, sregs->cr0); |
Paul Knowles | d730616 | 2008-02-06 11:02:35 +0000 | [diff] [blame] | 3910 | vcpu->arch.cr0 = sregs->cr0; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3911 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3912 | mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3913 | kvm_x86_ops->set_cr4(vcpu, sregs->cr4); |
| 3914 | if (!is_long_mode(vcpu) && is_pae(vcpu)) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3915 | load_pdptrs(vcpu, vcpu->arch.cr3); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3916 | |
| 3917 | if (mmu_reset_needed) |
| 3918 | kvm_mmu_reset_context(vcpu); |
| 3919 | |
| 3920 | if (!irqchip_in_kernel(vcpu->kvm)) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3921 | memcpy(vcpu->arch.irq_pending, sregs->interrupt_bitmap, |
| 3922 | sizeof vcpu->arch.irq_pending); |
| 3923 | vcpu->arch.irq_summary = 0; |
| 3924 | for (i = 0; i < ARRAY_SIZE(vcpu->arch.irq_pending); ++i) |
| 3925 | if (vcpu->arch.irq_pending[i]) |
| 3926 | __set_bit(i, &vcpu->arch.irq_summary); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3927 | } else { |
| 3928 | max_bits = (sizeof sregs->interrupt_bitmap) << 3; |
| 3929 | pending_vec = find_first_bit( |
| 3930 | (const unsigned long *)sregs->interrupt_bitmap, |
| 3931 | max_bits); |
| 3932 | /* Only pending external irq is handled here */ |
| 3933 | if (pending_vec < max_bits) { |
| 3934 | kvm_x86_ops->set_irq(vcpu, pending_vec); |
| 3935 | pr_debug("Set back pending irq %d\n", |
| 3936 | pending_vec); |
| 3937 | } |
| 3938 | } |
| 3939 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3940 | kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
| 3941 | kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); |
| 3942 | kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES); |
| 3943 | kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); |
| 3944 | kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); |
| 3945 | kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3946 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3947 | kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
| 3948 | kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3949 | |
| 3950 | vcpu_put(vcpu); |
| 3951 | |
| 3952 | return 0; |
| 3953 | } |
| 3954 | |
| 3955 | int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu, |
| 3956 | struct kvm_debug_guest *dbg) |
| 3957 | { |
| 3958 | int r; |
| 3959 | |
| 3960 | vcpu_load(vcpu); |
| 3961 | |
| 3962 | r = kvm_x86_ops->set_guest_debug(vcpu, dbg); |
| 3963 | |
| 3964 | vcpu_put(vcpu); |
| 3965 | |
| 3966 | return r; |
| 3967 | } |
| 3968 | |
| 3969 | /* |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 3970 | * fxsave fpu state. Taken from x86_64/processor.h. To be killed when |
| 3971 | * we have asm/x86/processor.h |
| 3972 | */ |
| 3973 | struct fxsave { |
| 3974 | u16 cwd; |
| 3975 | u16 swd; |
| 3976 | u16 twd; |
| 3977 | u16 fop; |
| 3978 | u64 rip; |
| 3979 | u64 rdp; |
| 3980 | u32 mxcsr; |
| 3981 | u32 mxcsr_mask; |
| 3982 | u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */ |
| 3983 | #ifdef CONFIG_X86_64 |
| 3984 | u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */ |
| 3985 | #else |
| 3986 | u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */ |
| 3987 | #endif |
| 3988 | }; |
| 3989 | |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 3990 | /* |
| 3991 | * Translate a guest virtual address to a guest physical address. |
| 3992 | */ |
| 3993 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
| 3994 | struct kvm_translation *tr) |
| 3995 | { |
| 3996 | unsigned long vaddr = tr->linear_address; |
| 3997 | gpa_t gpa; |
| 3998 | |
| 3999 | vcpu_load(vcpu); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 4000 | down_read(&vcpu->kvm->slots_lock); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4001 | gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 4002 | up_read(&vcpu->kvm->slots_lock); |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 4003 | tr->physical_address = gpa; |
| 4004 | tr->valid = gpa != UNMAPPED_GVA; |
| 4005 | tr->writeable = 1; |
| 4006 | tr->usermode = 0; |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 4007 | vcpu_put(vcpu); |
| 4008 | |
| 4009 | return 0; |
| 4010 | } |
| 4011 | |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 4012 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 4013 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4014 | struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 4015 | |
| 4016 | vcpu_load(vcpu); |
| 4017 | |
| 4018 | memcpy(fpu->fpr, fxsave->st_space, 128); |
| 4019 | fpu->fcw = fxsave->cwd; |
| 4020 | fpu->fsw = fxsave->swd; |
| 4021 | fpu->ftwx = fxsave->twd; |
| 4022 | fpu->last_opcode = fxsave->fop; |
| 4023 | fpu->last_ip = fxsave->rip; |
| 4024 | fpu->last_dp = fxsave->rdp; |
| 4025 | memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space); |
| 4026 | |
| 4027 | vcpu_put(vcpu); |
| 4028 | |
| 4029 | return 0; |
| 4030 | } |
| 4031 | |
| 4032 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 4033 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4034 | struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 4035 | |
| 4036 | vcpu_load(vcpu); |
| 4037 | |
| 4038 | memcpy(fxsave->st_space, fpu->fpr, 128); |
| 4039 | fxsave->cwd = fpu->fcw; |
| 4040 | fxsave->swd = fpu->fsw; |
| 4041 | fxsave->twd = fpu->ftwx; |
| 4042 | fxsave->fop = fpu->last_opcode; |
| 4043 | fxsave->rip = fpu->last_ip; |
| 4044 | fxsave->rdp = fpu->last_dp; |
| 4045 | memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space); |
| 4046 | |
| 4047 | vcpu_put(vcpu); |
| 4048 | |
| 4049 | return 0; |
| 4050 | } |
| 4051 | |
| 4052 | void fx_init(struct kvm_vcpu *vcpu) |
| 4053 | { |
| 4054 | unsigned after_mxcsr_mask; |
| 4055 | |
Andrea Arcangeli | bc1a34f | 2008-05-01 18:43:33 +0200 | [diff] [blame] | 4056 | /* |
| 4057 | * Touch the fpu the first time in non atomic context as if |
| 4058 | * this is the first fpu instruction the exception handler |
| 4059 | * will fire before the instruction returns and it'll have to |
| 4060 | * allocate ram with GFP_KERNEL. |
| 4061 | */ |
| 4062 | if (!used_math()) |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 4063 | kvm_fx_save(&vcpu->arch.host_fx_image); |
Andrea Arcangeli | bc1a34f | 2008-05-01 18:43:33 +0200 | [diff] [blame] | 4064 | |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 4065 | /* Initialize guest FPU by resetting ours and saving into guest's */ |
| 4066 | preempt_disable(); |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 4067 | kvm_fx_save(&vcpu->arch.host_fx_image); |
| 4068 | kvm_fx_finit(); |
| 4069 | kvm_fx_save(&vcpu->arch.guest_fx_image); |
| 4070 | kvm_fx_restore(&vcpu->arch.host_fx_image); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 4071 | preempt_enable(); |
| 4072 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4073 | vcpu->arch.cr0 |= X86_CR0_ET; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 4074 | after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4075 | vcpu->arch.guest_fx_image.mxcsr = 0x1f80; |
| 4076 | memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask, |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 4077 | 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask); |
| 4078 | } |
| 4079 | EXPORT_SYMBOL_GPL(fx_init); |
| 4080 | |
| 4081 | void kvm_load_guest_fpu(struct kvm_vcpu *vcpu) |
| 4082 | { |
| 4083 | if (!vcpu->fpu_active || vcpu->guest_fpu_loaded) |
| 4084 | return; |
| 4085 | |
| 4086 | vcpu->guest_fpu_loaded = 1; |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 4087 | kvm_fx_save(&vcpu->arch.host_fx_image); |
| 4088 | kvm_fx_restore(&vcpu->arch.guest_fx_image); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 4089 | } |
| 4090 | EXPORT_SYMBOL_GPL(kvm_load_guest_fpu); |
| 4091 | |
| 4092 | void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) |
| 4093 | { |
| 4094 | if (!vcpu->guest_fpu_loaded) |
| 4095 | return; |
| 4096 | |
| 4097 | vcpu->guest_fpu_loaded = 0; |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 4098 | kvm_fx_save(&vcpu->arch.guest_fx_image); |
| 4099 | kvm_fx_restore(&vcpu->arch.host_fx_image); |
Avi Kivity | f096ed8 | 2007-11-18 13:54:33 +0200 | [diff] [blame] | 4100 | ++vcpu->stat.fpu_reload; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 4101 | } |
| 4102 | EXPORT_SYMBOL_GPL(kvm_put_guest_fpu); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4103 | |
| 4104 | void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu) |
| 4105 | { |
| 4106 | kvm_x86_ops->vcpu_free(vcpu); |
| 4107 | } |
| 4108 | |
| 4109 | struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, |
| 4110 | unsigned int id) |
| 4111 | { |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 4112 | return kvm_x86_ops->vcpu_create(kvm, id); |
| 4113 | } |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4114 | |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 4115 | int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) |
| 4116 | { |
| 4117 | int r; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4118 | |
| 4119 | /* We do fxsave: this must be aligned. */ |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4120 | BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4121 | |
| 4122 | vcpu_load(vcpu); |
| 4123 | r = kvm_arch_vcpu_reset(vcpu); |
| 4124 | if (r == 0) |
| 4125 | r = kvm_mmu_setup(vcpu); |
| 4126 | vcpu_put(vcpu); |
| 4127 | if (r < 0) |
| 4128 | goto free_vcpu; |
| 4129 | |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 4130 | return 0; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4131 | free_vcpu: |
| 4132 | kvm_x86_ops->vcpu_free(vcpu); |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 4133 | return r; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4134 | } |
| 4135 | |
Hollis Blanchard | d40ccc6 | 2007-11-19 14:04:43 -0600 | [diff] [blame] | 4136 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4137 | { |
| 4138 | vcpu_load(vcpu); |
| 4139 | kvm_mmu_unload(vcpu); |
| 4140 | vcpu_put(vcpu); |
| 4141 | |
| 4142 | kvm_x86_ops->vcpu_free(vcpu); |
| 4143 | } |
| 4144 | |
| 4145 | int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu) |
| 4146 | { |
| 4147 | return kvm_x86_ops->vcpu_reset(vcpu); |
| 4148 | } |
| 4149 | |
| 4150 | void kvm_arch_hardware_enable(void *garbage) |
| 4151 | { |
| 4152 | kvm_x86_ops->hardware_enable(garbage); |
| 4153 | } |
| 4154 | |
| 4155 | void kvm_arch_hardware_disable(void *garbage) |
| 4156 | { |
| 4157 | kvm_x86_ops->hardware_disable(garbage); |
| 4158 | } |
| 4159 | |
| 4160 | int kvm_arch_hardware_setup(void) |
| 4161 | { |
| 4162 | return kvm_x86_ops->hardware_setup(); |
| 4163 | } |
| 4164 | |
| 4165 | void kvm_arch_hardware_unsetup(void) |
| 4166 | { |
| 4167 | kvm_x86_ops->hardware_unsetup(); |
| 4168 | } |
| 4169 | |
| 4170 | void kvm_arch_check_processor_compat(void *rtn) |
| 4171 | { |
| 4172 | kvm_x86_ops->check_processor_compatibility(rtn); |
| 4173 | } |
| 4174 | |
| 4175 | int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) |
| 4176 | { |
| 4177 | struct page *page; |
| 4178 | struct kvm *kvm; |
| 4179 | int r; |
| 4180 | |
| 4181 | BUG_ON(vcpu->kvm == NULL); |
| 4182 | kvm = vcpu->kvm; |
| 4183 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4184 | vcpu->arch.mmu.root_hpa = INVALID_PAGE; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4185 | if (!irqchip_in_kernel(kvm) || vcpu->vcpu_id == 0) |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 4186 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4187 | else |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 4188 | vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4189 | |
| 4190 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 4191 | if (!page) { |
| 4192 | r = -ENOMEM; |
| 4193 | goto fail; |
| 4194 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4195 | vcpu->arch.pio_data = page_address(page); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4196 | |
| 4197 | r = kvm_mmu_create(vcpu); |
| 4198 | if (r < 0) |
| 4199 | goto fail_free_pio_data; |
| 4200 | |
| 4201 | if (irqchip_in_kernel(kvm)) { |
| 4202 | r = kvm_create_lapic(vcpu); |
| 4203 | if (r < 0) |
| 4204 | goto fail_mmu_destroy; |
| 4205 | } |
| 4206 | |
| 4207 | return 0; |
| 4208 | |
| 4209 | fail_mmu_destroy: |
| 4210 | kvm_mmu_destroy(vcpu); |
| 4211 | fail_free_pio_data: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4212 | free_page((unsigned long)vcpu->arch.pio_data); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4213 | fail: |
| 4214 | return r; |
| 4215 | } |
| 4216 | |
| 4217 | void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 4218 | { |
| 4219 | kvm_free_lapic(vcpu); |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 4220 | down_read(&vcpu->kvm->slots_lock); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4221 | kvm_mmu_destroy(vcpu); |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 4222 | up_read(&vcpu->kvm->slots_lock); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4223 | free_page((unsigned long)vcpu->arch.pio_data); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4224 | } |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 4225 | |
| 4226 | struct kvm *kvm_arch_create_vm(void) |
| 4227 | { |
| 4228 | struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL); |
| 4229 | |
| 4230 | if (!kvm) |
| 4231 | return ERR_PTR(-ENOMEM); |
| 4232 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 4233 | INIT_LIST_HEAD(&kvm->arch.active_mmu_pages); |
Ben-Ami Yassour | 4d5c5d0 | 2008-07-28 19:26:26 +0300 | [diff] [blame] | 4234 | INIT_LIST_HEAD(&kvm->arch.assigned_dev_head); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 4235 | |
| 4236 | return kvm; |
| 4237 | } |
| 4238 | |
| 4239 | static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu) |
| 4240 | { |
| 4241 | vcpu_load(vcpu); |
| 4242 | kvm_mmu_unload(vcpu); |
| 4243 | vcpu_put(vcpu); |
| 4244 | } |
| 4245 | |
| 4246 | static void kvm_free_vcpus(struct kvm *kvm) |
| 4247 | { |
| 4248 | unsigned int i; |
| 4249 | |
| 4250 | /* |
| 4251 | * Unpin any mmu pages first. |
| 4252 | */ |
| 4253 | for (i = 0; i < KVM_MAX_VCPUS; ++i) |
| 4254 | if (kvm->vcpus[i]) |
| 4255 | kvm_unload_vcpu_mmu(kvm->vcpus[i]); |
| 4256 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
| 4257 | if (kvm->vcpus[i]) { |
| 4258 | kvm_arch_vcpu_free(kvm->vcpus[i]); |
| 4259 | kvm->vcpus[i] = NULL; |
| 4260 | } |
| 4261 | } |
| 4262 | |
| 4263 | } |
| 4264 | |
| 4265 | void kvm_arch_destroy_vm(struct kvm *kvm) |
| 4266 | { |
Ben-Ami Yassour | 4d5c5d0 | 2008-07-28 19:26:26 +0300 | [diff] [blame] | 4267 | kvm_free_assigned_devices(kvm); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 4268 | kvm_free_pit(kvm); |
Zhang Xiantao | d7deeeb | 2007-12-14 10:17:34 +0800 | [diff] [blame] | 4269 | kfree(kvm->arch.vpic); |
| 4270 | kfree(kvm->arch.vioapic); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 4271 | kvm_free_vcpus(kvm); |
| 4272 | kvm_free_physmem(kvm); |
Avi Kivity | 3d45830 | 2008-03-25 11:26:13 +0200 | [diff] [blame] | 4273 | if (kvm->arch.apic_access_page) |
| 4274 | put_page(kvm->arch.apic_access_page); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4275 | if (kvm->arch.ept_identity_pagetable) |
| 4276 | put_page(kvm->arch.ept_identity_pagetable); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 4277 | kfree(kvm); |
| 4278 | } |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 4279 | |
| 4280 | int kvm_arch_set_memory_region(struct kvm *kvm, |
| 4281 | struct kvm_userspace_memory_region *mem, |
| 4282 | struct kvm_memory_slot old, |
| 4283 | int user_alloc) |
| 4284 | { |
| 4285 | int npages = mem->memory_size >> PAGE_SHIFT; |
| 4286 | struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot]; |
| 4287 | |
| 4288 | /*To keep backward compatibility with older userspace, |
| 4289 | *x86 needs to hanlde !user_alloc case. |
| 4290 | */ |
| 4291 | if (!user_alloc) { |
| 4292 | if (npages && !old.rmap) { |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 4293 | unsigned long userspace_addr; |
| 4294 | |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 4295 | down_write(¤t->mm->mmap_sem); |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 4296 | userspace_addr = do_mmap(NULL, 0, |
| 4297 | npages * PAGE_SIZE, |
| 4298 | PROT_READ | PROT_WRITE, |
| 4299 | MAP_SHARED | MAP_ANONYMOUS, |
| 4300 | 0); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 4301 | up_write(¤t->mm->mmap_sem); |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 4302 | |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 4303 | if (IS_ERR((void *)userspace_addr)) |
| 4304 | return PTR_ERR((void *)userspace_addr); |
| 4305 | |
| 4306 | /* set userspace_addr atomically for kvm_hva_to_rmapp */ |
| 4307 | spin_lock(&kvm->mmu_lock); |
| 4308 | memslot->userspace_addr = userspace_addr; |
| 4309 | spin_unlock(&kvm->mmu_lock); |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 4310 | } else { |
| 4311 | if (!old.user_alloc && old.rmap) { |
| 4312 | int ret; |
| 4313 | |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 4314 | down_write(¤t->mm->mmap_sem); |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 4315 | ret = do_munmap(current->mm, old.userspace_addr, |
| 4316 | old.npages * PAGE_SIZE); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 4317 | up_write(¤t->mm->mmap_sem); |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 4318 | if (ret < 0) |
| 4319 | printk(KERN_WARNING |
| 4320 | "kvm_vm_ioctl_set_memory_region: " |
| 4321 | "failed to munmap memory\n"); |
| 4322 | } |
| 4323 | } |
| 4324 | } |
| 4325 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 4326 | if (!kvm->arch.n_requested_mmu_pages) { |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 4327 | unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm); |
| 4328 | kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages); |
| 4329 | } |
| 4330 | |
| 4331 | kvm_mmu_slot_remove_write_access(kvm, mem->slot); |
| 4332 | kvm_flush_remote_tlbs(kvm); |
| 4333 | |
| 4334 | return 0; |
| 4335 | } |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 4336 | |
Marcelo Tosatti | 34d4cb8 | 2008-07-10 20:49:31 -0300 | [diff] [blame] | 4337 | void kvm_arch_flush_shadow(struct kvm *kvm) |
| 4338 | { |
| 4339 | kvm_mmu_zap_all(kvm); |
| 4340 | } |
| 4341 | |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 4342 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) |
| 4343 | { |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 4344 | return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE |
| 4345 | || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED; |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 4346 | } |
Zhang Xiantao | 5736199 | 2007-12-17 14:21:40 +0800 | [diff] [blame] | 4347 | |
| 4348 | static void vcpu_kick_intr(void *info) |
| 4349 | { |
| 4350 | #ifdef DEBUG |
| 4351 | struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info; |
| 4352 | printk(KERN_DEBUG "vcpu_kick_intr %p \n", vcpu); |
| 4353 | #endif |
| 4354 | } |
| 4355 | |
| 4356 | void kvm_vcpu_kick(struct kvm_vcpu *vcpu) |
| 4357 | { |
| 4358 | int ipi_pcpu = vcpu->cpu; |
Marcelo Tosatti | e9571ed | 2008-04-11 15:01:22 -0300 | [diff] [blame] | 4359 | int cpu = get_cpu(); |
Zhang Xiantao | 5736199 | 2007-12-17 14:21:40 +0800 | [diff] [blame] | 4360 | |
| 4361 | if (waitqueue_active(&vcpu->wq)) { |
| 4362 | wake_up_interruptible(&vcpu->wq); |
| 4363 | ++vcpu->stat.halt_wakeup; |
| 4364 | } |
Marcelo Tosatti | e9571ed | 2008-04-11 15:01:22 -0300 | [diff] [blame] | 4365 | /* |
| 4366 | * We may be called synchronously with irqs disabled in guest mode, |
| 4367 | * So need not to call smp_call_function_single() in that case. |
| 4368 | */ |
| 4369 | if (vcpu->guest_mode && vcpu->cpu != cpu) |
Jens Axboe | 8691e5a | 2008-06-06 11:18:06 +0200 | [diff] [blame] | 4370 | smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0); |
Marcelo Tosatti | e9571ed | 2008-04-11 15:01:22 -0300 | [diff] [blame] | 4371 | put_cpu(); |
Zhang Xiantao | 5736199 | 2007-12-17 14:21:40 +0800 | [diff] [blame] | 4372 | } |