blob: e3b89662cf6d6746adafd65b5e38117cef070c22 [file] [log] [blame]
Carsten Otte043405e2007-10-10 17:16:19 +02001/*
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 Yassour4d5c5d02008-07-28 19:26:26 +03007 * Copyright (C) 2008 Qumranet, Inc.
8 * Copyright IBM Corporation, 2008
Carsten Otte043405e2007-10-10 17:16:19 +02009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030013 * Amit Shah <amit.shah@qumranet.com>
14 * Ben-Ami Yassour <benami@il.ibm.com>
Carsten Otte043405e2007-10-10 17:16:19 +020015 *
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 Kivityedf88412007-12-16 11:02:48 +020021#include <linux/kvm_host.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020022#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080023#include "mmu.h"
Sheng Yang78376992008-01-28 05:10:22 +080024#include "i8254.h"
Izik Eidus37817f22008-03-24 23:14:53 +020025#include "tss.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030026#include "kvm_cache_regs.h"
Avi Kivity26eef702008-07-03 14:59:22 +030027#include "x86.h"
Carsten Otte313a3dc2007-10-11 19:16:52 +020028
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020029#include <linux/clocksource.h>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030030#include <linux/interrupt.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020031#include <linux/kvm.h>
32#include <linux/fs.h>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030033#include <linux/pci.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020034#include <linux/vmalloc.h>
Carsten Otte5fb76f92007-10-29 16:08:51 +010035#include <linux/module.h>
Zhang Xiantao0de10342007-11-20 16:25:04 +080036#include <linux/mman.h>
Marcelo Tosatti2bacc552007-12-12 10:46:12 -050037#include <linux/highmem.h>
Carsten Otte043405e2007-10-10 17:16:19 +020038
39#include <asm/uaccess.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080040#include <asm/msr.h>
Avi Kivitya5f61302008-02-20 17:57:21 +020041#include <asm/desc.h>
Carsten Otte043405e2007-10-10 17:16:19 +020042
Carsten Otte313a3dc2007-10-11 19:16:52 +020043#define MAX_IO_MSRS 256
Carsten Ottea03490e2007-10-29 16:09:35 +010044#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 Roedel50a37eb2008-01-31 14:57:38 +010055/* 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
60static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL;
61#else
62static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL;
63#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +020064
Avi Kivityba1389b2007-11-18 16:24:12 +020065#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 Blanchard417bc302007-10-31 17:24:23 -050067
Avi Kivity674eea02008-02-11 18:37:23 +020068static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
69 struct kvm_cpuid_entry2 __user *entries);
70
Zhang Xiantao97896d02007-11-14 20:09:30 +080071struct kvm_x86_ops *kvm_x86_ops;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030072EXPORT_SYMBOL_GPL(kvm_x86_ops);
Zhang Xiantao97896d02007-11-14 20:09:30 +080073
Hollis Blanchard417bc302007-10-31 17:24:23 -050074struct kvm_stats_debugfs_item debugfs_entries[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +020075 { "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 Yangf08864b2008-05-15 18:23:25 +080084 { "nmi_window", VCPU_STAT(nmi_window_exits) },
Avi Kivityba1389b2007-11-18 16:24:12 +020085 { "halt_exits", VCPU_STAT(halt_exits) },
86 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Amit Shahf11c3a82008-02-21 01:00:30 +053087 { "hypercalls", VCPU_STAT(hypercalls) },
Avi Kivityba1389b2007-11-18 16:24:12 +020088 { "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 Kivity4cee5762007-11-18 16:37:07 +020095 { "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 Kivitydfc5aa02007-12-18 19:47:18 +0200101 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
Avi Kivity0f74a242007-11-20 23:01:14 +0200102 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300103 { "largepages", VM_STAT(lpages) },
Hollis Blanchard417bc302007-10-31 17:24:23 -0500104 { NULL }
105};
106
Harvey Harrisonee032c92008-08-11 16:54:20 -0700107static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +0300108 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
121static 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 */
142static 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 */
154static 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
167static 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 Shah6762b722008-08-13 16:22:37 +0300194 if (!capable(CAP_SYS_RAWIO)) {
195 return -EPERM;
196 goto out;
197 }
198
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +0300199 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;
223out:
224 mutex_unlock(&kvm->lock);
225 return r;
226}
227
228static 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
279out:
280 mutex_unlock(&kvm->lock);
281 return r;
282out_disable:
283 pci_disable_device(dev);
284out_put:
285 pci_dev_put(dev);
286out_free:
287 kfree(match);
288 mutex_unlock(&kvm->lock);
289 return r;
290}
291
292static 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 Blanchard417bc302007-10-31 17:24:23 -0500325
Carsten Otte5fb76f92007-10-29 16:08:51 +0100326unsigned long segment_base(u16 selector)
327{
328 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200329 struct desc_struct *d;
Carsten Otte5fb76f92007-10-29 16:08:51 +0100330 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 Kivitya5f61302008-02-20 17:57:21 +0200345 d = (struct desc_struct *)(table_base + (selector & ~7));
346 v = d->base0 | ((unsigned long)d->base1 << 16) |
347 ((unsigned long)d->base2 << 24);
Carsten Otte5fb76f92007-10-29 16:08:51 +0100348#ifdef CONFIG_X86_64
Avi Kivitya5f61302008-02-20 17:57:21 +0200349 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
350 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
Carsten Otte5fb76f92007-10-29 16:08:51 +0100351#endif
352 return v;
353}
354EXPORT_SYMBOL_GPL(segment_base);
355
Carsten Otte6866b832007-10-29 16:09:10 +0100356u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
357{
358 if (irqchip_in_kernel(vcpu->kvm))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800359 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100360 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800361 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100362}
363EXPORT_SYMBOL_GPL(kvm_get_apic_base);
364
365void 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 Xiantaoad312c72007-12-13 23:50:52 +0800371 vcpu->arch.apic_base = data;
Carsten Otte6866b832007-10-29 16:09:10 +0100372}
373EXPORT_SYMBOL_GPL(kvm_set_apic_base);
374
Avi Kivity298101d2007-11-25 13:41:11 +0200375void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
376{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800377 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 Kivity298101d2007-11-25 13:41:11 +0200381}
382EXPORT_SYMBOL_GPL(kvm_queue_exception);
383
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200384void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
385 u32 error_code)
386{
387 ++vcpu->stat.pf_guest;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +0100388 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 Kivityc3c91fe2007-11-25 14:04:58 +0200398 return;
399 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800400 vcpu->arch.cr2 = addr;
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200401 kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
402}
403
Sheng Yang3419ffc2008-05-15 09:52:48 +0800404void kvm_inject_nmi(struct kvm_vcpu *vcpu)
405{
406 vcpu->arch.nmi_pending = 1;
407}
408EXPORT_SYMBOL_GPL(kvm_inject_nmi);
409
Avi Kivity298101d2007-11-25 13:41:11 +0200410void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
411{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800412 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 Kivity298101d2007-11-25 13:41:11 +0200417}
418EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
419
420static void __queue_exception(struct kvm_vcpu *vcpu)
421{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800422 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
423 vcpu->arch.exception.has_error_code,
424 vcpu->arch.exception.error_code);
Avi Kivity298101d2007-11-25 13:41:11 +0200425}
426
Carsten Ottea03490e2007-10-29 16:09:35 +0100427/*
428 * Load the pae pdptrs. Return true is they are all valid.
429 */
430int 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 Xiantaoad312c72007-12-13 23:50:52 +0800436 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100437
Carsten Ottea03490e2007-10-29 16:09:35 +0100438 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 Xiantaoad312c72007-12-13 23:50:52 +0800452 memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
Carsten Ottea03490e2007-10-29 16:09:35 +0100453out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100454
455 return ret;
456}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100457EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100458
Avi Kivityd835dfe2007-11-21 02:57:59 +0200459static bool pdptrs_changed(struct kvm_vcpu *vcpu)
460{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800461 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Avi Kivityd835dfe2007-11-21 02:57:59 +0200462 bool changed = true;
463 int r;
464
465 if (is_long_mode(vcpu) || !is_pae(vcpu))
466 return false;
467
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800468 r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
Avi Kivityd835dfe2007-11-21 02:57:59 +0200469 if (r < 0)
470 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800471 changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200472out:
Avi Kivityd835dfe2007-11-21 02:57:59 +0200473
474 return changed;
475}
476
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200477void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100478{
479 if (cr0 & CR0_RESERVED_BITS) {
480 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800481 cr0, vcpu->arch.cr0);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200482 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100483 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 Kivityc1a5d4f2007-11-25 14:12:03 +0200488 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100489 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 Kivityc1a5d4f2007-11-25 14:12:03 +0200495 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100496 return;
497 }
498
499 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
500#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800501 if ((vcpu->arch.shadow_efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100502 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 Kivityc1a5d4f2007-11-25 14:12:03 +0200507 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100508 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 Kivityc1a5d4f2007-11-25 14:12:03 +0200514 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100515 return;
516
517 }
518 } else
519#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800520 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100521 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
522 "reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200523 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100524 return;
525 }
526
527 }
528
529 kvm_x86_ops->set_cr0(vcpu, cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800530 vcpu->arch.cr0 = cr0;
Carsten Ottea03490e2007-10-29 16:09:35 +0100531
Carsten Ottea03490e2007-10-29 16:09:35 +0100532 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100533 return;
534}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200535EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100536
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200537void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100538{
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200539 kvm_set_cr0(vcpu, (vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f));
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -0400540 KVMTRACE_1D(LMSW, vcpu,
541 (u32)((vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f)),
542 handler);
Carsten Ottea03490e2007-10-29 16:09:35 +0100543}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200544EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100545
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200546void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100547{
548 if (cr4 & CR4_RESERVED_BITS) {
549 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200550 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100551 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 Kivityc1a5d4f2007-11-25 14:12:03 +0200558 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100559 return;
560 }
561 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800562 && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100563 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200564 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100565 return;
566 }
567
568 if (cr4 & X86_CR4_VMXE) {
569 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200570 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100571 return;
572 }
573 kvm_x86_ops->set_cr4(vcpu, cr4);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800574 vcpu->arch.cr4 = cr4;
Carsten Ottea03490e2007-10-29 16:09:35 +0100575 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100576}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200577EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +0100578
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200579void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100580{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800581 if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
Avi Kivityd835dfe2007-11-21 02:57:59 +0200582 kvm_mmu_flush_tlb(vcpu);
583 return;
584 }
585
Carsten Ottea03490e2007-10-29 16:09:35 +0100586 if (is_long_mode(vcpu)) {
587 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
588 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200589 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100590 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 Kivityc1a5d4f2007-11-25 14:12:03 +0200597 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100598 return;
599 }
600 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
601 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
602 "reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200603 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100604 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 Ottea03490e2007-10-29 16:09:35 +0100613 /*
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 Kivityc1a5d4f2007-11-25 14:12:03 +0200623 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100624 else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800625 vcpu->arch.cr3 = cr3;
626 vcpu->arch.mmu.new_cr3(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100627 }
Carsten Ottea03490e2007-10-29 16:09:35 +0100628}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200629EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +0100630
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200631void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +0100632{
633 if (cr8 & CR8_RESERVED_BITS) {
634 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200635 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100636 return;
637 }
638 if (irqchip_in_kernel(vcpu->kvm))
639 kvm_lapic_set_tpr(vcpu, cr8);
640 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800641 vcpu->arch.cr8 = cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100642}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200643EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100644
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200645unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +0100646{
647 if (irqchip_in_kernel(vcpu->kvm))
648 return kvm_lapic_get_cr8(vcpu);
649 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800650 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100651}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200652EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100653
Carsten Otte043405e2007-10-10 17:16:19 +0200654/*
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 */
661static 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 Costa18068522008-02-15 17:52:47 -0200667 MSR_IA32_TIME_STAMP_COUNTER, MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
Alexander Graf847f0ad2008-02-21 12:11:01 +0100668 MSR_IA32_PERF_STATUS,
Carsten Otte043405e2007-10-10 17:16:19 +0200669};
670
671static unsigned num_msrs_to_save;
672
673static u32 emulated_msrs[] = {
674 MSR_IA32_MISC_ENABLE,
675};
676
Carsten Otte15c4a642007-10-30 18:44:17 +0100677static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
678{
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100679 if (efer & efer_reserved_bits) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100680 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
681 efer);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200682 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100683 return;
684 }
685
686 if (is_paging(vcpu)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800687 && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100688 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200689 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100690 return;
691 }
692
693 kvm_x86_ops->set_efer(vcpu, efer);
694
695 efer &= ~EFER_LMA;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800696 efer |= vcpu->arch.shadow_efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +0100697
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800698 vcpu->arch.shadow_efer = efer;
Carsten Otte15c4a642007-10-30 18:44:17 +0100699}
700
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100701void kvm_enable_efer_bits(u64 mask)
702{
703 efer_reserved_bits &= ~mask;
704}
705EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
706
707
Carsten Otte15c4a642007-10-30 18:44:17 +0100708/*
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 */
713int 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 Otte313a3dc2007-10-11 19:16:52 +0200718/*
719 * Adapt set_msr() to msr_io()'s calling convention
720 */
721static 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 Costa18068522008-02-15 17:52:47 -0200726static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
727{
728 static int version;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200729 struct pvclock_wall_clock wc;
730 struct timespec now, sys, boot;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200731
732 if (!wall_clock)
733 return;
734
735 version++;
736
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200737 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
738
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200739 /*
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 Costa18068522008-02-15 17:52:47 -0200752
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 Costa18068522008-02-15 17:52:47 -0200757}
758
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200759static 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
771static 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 Costa18068522008-02-15 17:52:47 -0200798static 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 Hoffmann50d0a0f2008-06-03 16:17:31 +0200808 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 Costa18068522008-02-15 17:52:47 -0200813 /* 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 Hoffmann50d0a0f2008-06-03 16:17:31 +0200827 * state, we just increase by 2 at the end.
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200828 */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200829 vcpu->hv_clock.version += 2;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200830
831 shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
832
833 memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200834 sizeof(vcpu->hv_clock));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200835
836 kunmap_atomic(shared_kaddr, KM_USER0);
837
838 mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
839}
840
Avi Kivity9ba075a2008-05-26 20:06:35 +0300841static 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
865static 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 Otte15c4a642007-10-30 18:44:17 +0100873
874int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
875{
876 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100877 case MSR_EFER:
878 set_efer(vcpu, data);
879 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100880 case MSR_IA32_MC0_STATUS:
881 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800882 __func__, data);
Carsten Otte15c4a642007-10-30 18:44:17 +0100883 break;
884 case MSR_IA32_MCG_STATUS:
885 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800886 __func__, data);
Carsten Otte15c4a642007-10-30 18:44:17 +0100887 break;
Joerg Roedelc7ac6792008-02-11 20:28:27 +0100888 case MSR_IA32_MCG_CTL:
889 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_CTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800890 __func__, data);
Joerg Roedelc7ac6792008-02-11 20:28:27 +0100891 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +0200892 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 Otte15c4a642007-10-30 18:44:17 +0100904 case MSR_IA32_UCODE_REV:
905 case MSR_IA32_UCODE_WRITE:
Carsten Otte15c4a642007-10-30 18:44:17 +0100906 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +0300907 case 0x200 ... 0x2ff:
908 return set_msr_mtrr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +0100909 case MSR_IA32_APICBASE:
910 kvm_set_apic_base(vcpu, data);
911 break;
912 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800913 vcpu->arch.ia32_misc_enable_msr = data;
Carsten Otte15c4a642007-10-30 18:44:17 +0100914 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200915 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 Costa18068522008-02-15 17:52:47 -0200934 down_read(&current->mm->mmap_sem);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200935 vcpu->arch.time_page =
936 gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200937 up_read(&current->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 Otte15c4a642007-10-30 18:44:17 +0100947 default:
Avi Kivity565f1fb2007-12-19 12:02:40 +0200948 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n", msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +0100949 return 1;
950 }
951 return 0;
952}
953EXPORT_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 */
961int 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 Kivity9ba075a2008-05-26 20:06:35 +0300966static 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 Otte15c4a642007-10-30 18:44:17 +0100975int 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 Roedelc7ac6792008-02-11 20:28:27 +0100988 case MSR_IA32_MCG_CTL:
Carsten Otte15c4a642007-10-30 18:44:17 +0100989 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:
Joerg Roedela89c1ad2008-08-29 11:52:07 +0200994 case MSR_IA32_MC0_MISC+20:
Carsten Otte15c4a642007-10-30 18:44:17 +0100995 case MSR_IA32_UCODE_REV:
Carsten Otte15c4a642007-10-30 18:44:17 +0100996 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +0200997 case MSR_IA32_DEBUGCTLMSR:
998 case MSR_IA32_LASTBRANCHFROMIP:
999 case MSR_IA32_LASTBRANCHTOIP:
1000 case MSR_IA32_LASTINTFROMIP:
1001 case MSR_IA32_LASTINTTOIP:
Carsten Otte15c4a642007-10-30 18:44:17 +01001002 data = 0;
1003 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03001004 case MSR_MTRRcap:
1005 data = 0x500 | KVM_NR_VAR_MTRR;
1006 break;
1007 case 0x200 ... 0x2ff:
1008 return get_msr_mtrr(vcpu, msr, pdata);
Carsten Otte15c4a642007-10-30 18:44:17 +01001009 case 0xcd: /* fsb frequency */
1010 data = 3;
1011 break;
1012 case MSR_IA32_APICBASE:
1013 data = kvm_get_apic_base(vcpu);
1014 break;
1015 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001016 data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01001017 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01001018 case MSR_IA32_PERF_STATUS:
1019 /* TSC increment by tick */
1020 data = 1000ULL;
1021 /* CPU multiplier */
1022 data |= (((uint64_t)4ULL) << 40);
1023 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001024 case MSR_EFER:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001025 data = vcpu->arch.shadow_efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01001026 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001027 case MSR_KVM_WALL_CLOCK:
1028 data = vcpu->kvm->arch.wall_clock;
1029 break;
1030 case MSR_KVM_SYSTEM_TIME:
1031 data = vcpu->arch.time;
1032 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001033 default:
1034 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
1035 return 1;
1036 }
1037 *pdata = data;
1038 return 0;
1039}
1040EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1041
Carsten Otte313a3dc2007-10-11 19:16:52 +02001042/*
1043 * Read or write a bunch of msrs. All parameters are kernel addresses.
1044 *
1045 * @return number of msrs set successfully.
1046 */
1047static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
1048 struct kvm_msr_entry *entries,
1049 int (*do_msr)(struct kvm_vcpu *vcpu,
1050 unsigned index, u64 *data))
1051{
1052 int i;
1053
1054 vcpu_load(vcpu);
1055
Marcelo Tosatti3200f402008-03-29 20:17:59 -03001056 down_read(&vcpu->kvm->slots_lock);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001057 for (i = 0; i < msrs->nmsrs; ++i)
1058 if (do_msr(vcpu, entries[i].index, &entries[i].data))
1059 break;
Marcelo Tosatti3200f402008-03-29 20:17:59 -03001060 up_read(&vcpu->kvm->slots_lock);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001061
1062 vcpu_put(vcpu);
1063
1064 return i;
1065}
1066
1067/*
1068 * Read or write a bunch of msrs. Parameters are user addresses.
1069 *
1070 * @return number of msrs set successfully.
1071 */
1072static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
1073 int (*do_msr)(struct kvm_vcpu *vcpu,
1074 unsigned index, u64 *data),
1075 int writeback)
1076{
1077 struct kvm_msrs msrs;
1078 struct kvm_msr_entry *entries;
1079 int r, n;
1080 unsigned size;
1081
1082 r = -EFAULT;
1083 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
1084 goto out;
1085
1086 r = -E2BIG;
1087 if (msrs.nmsrs >= MAX_IO_MSRS)
1088 goto out;
1089
1090 r = -ENOMEM;
1091 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
1092 entries = vmalloc(size);
1093 if (!entries)
1094 goto out;
1095
1096 r = -EFAULT;
1097 if (copy_from_user(entries, user_msrs->entries, size))
1098 goto out_free;
1099
1100 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
1101 if (r < 0)
1102 goto out_free;
1103
1104 r = -EFAULT;
1105 if (writeback && copy_to_user(user_msrs->entries, entries, size))
1106 goto out_free;
1107
1108 r = n;
1109
1110out_free:
1111 vfree(entries);
1112out:
1113 return r;
1114}
1115
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001116int kvm_dev_ioctl_check_extension(long ext)
1117{
1118 int r;
1119
1120 switch (ext) {
1121 case KVM_CAP_IRQCHIP:
1122 case KVM_CAP_HLT:
1123 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
1124 case KVM_CAP_USER_MEMORY:
1125 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02001126 case KVM_CAP_EXT_CPUID:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001127 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08001128 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05001129 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001130 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03001131 case KVM_CAP_SYNC_MMU:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001132 r = 1;
1133 break;
Laurent Vivier542472b2008-05-30 16:05:55 +02001134 case KVM_CAP_COALESCED_MMIO:
1135 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1136 break;
Avi Kivity774ead32007-12-26 13:57:04 +02001137 case KVM_CAP_VAPIC:
1138 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
1139 break;
Avi Kivityf7252302008-02-20 11:53:16 +02001140 case KVM_CAP_NR_VCPUS:
1141 r = KVM_MAX_VCPUS;
1142 break;
Avi Kivitya988b912008-02-20 11:59:20 +02001143 case KVM_CAP_NR_MEMSLOTS:
1144 r = KVM_MEMORY_SLOTS;
1145 break;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05001146 case KVM_CAP_PV_MMU:
1147 r = !tdp_enabled;
1148 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001149 default:
1150 r = 0;
1151 break;
1152 }
1153 return r;
1154
1155}
1156
Carsten Otte043405e2007-10-10 17:16:19 +02001157long kvm_arch_dev_ioctl(struct file *filp,
1158 unsigned int ioctl, unsigned long arg)
1159{
1160 void __user *argp = (void __user *)arg;
1161 long r;
1162
1163 switch (ioctl) {
1164 case KVM_GET_MSR_INDEX_LIST: {
1165 struct kvm_msr_list __user *user_msr_list = argp;
1166 struct kvm_msr_list msr_list;
1167 unsigned n;
1168
1169 r = -EFAULT;
1170 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
1171 goto out;
1172 n = msr_list.nmsrs;
1173 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
1174 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
1175 goto out;
1176 r = -E2BIG;
1177 if (n < num_msrs_to_save)
1178 goto out;
1179 r = -EFAULT;
1180 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
1181 num_msrs_to_save * sizeof(u32)))
1182 goto out;
1183 if (copy_to_user(user_msr_list->indices
1184 + num_msrs_to_save * sizeof(u32),
1185 &emulated_msrs,
1186 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
1187 goto out;
1188 r = 0;
1189 break;
1190 }
Avi Kivity674eea02008-02-11 18:37:23 +02001191 case KVM_GET_SUPPORTED_CPUID: {
1192 struct kvm_cpuid2 __user *cpuid_arg = argp;
1193 struct kvm_cpuid2 cpuid;
1194
1195 r = -EFAULT;
1196 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1197 goto out;
1198 r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
1199 cpuid_arg->entries);
1200 if (r)
1201 goto out;
1202
1203 r = -EFAULT;
1204 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1205 goto out;
1206 r = 0;
1207 break;
1208 }
Carsten Otte043405e2007-10-10 17:16:19 +02001209 default:
1210 r = -EINVAL;
1211 }
1212out:
1213 return r;
1214}
1215
Carsten Otte313a3dc2007-10-11 19:16:52 +02001216void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1217{
1218 kvm_x86_ops->vcpu_load(vcpu, cpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001219 kvm_write_guest_time(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001220}
1221
1222void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1223{
1224 kvm_x86_ops->vcpu_put(vcpu);
Amit Shah9327fd12007-11-15 18:38:46 +02001225 kvm_put_guest_fpu(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001226}
1227
Dan Kenigsberg07716712007-11-21 17:10:04 +02001228static int is_efer_nx(void)
Carsten Otte313a3dc2007-10-11 19:16:52 +02001229{
1230 u64 efer;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001231
1232 rdmsrl(MSR_EFER, efer);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001233 return efer & EFER_NX;
1234}
1235
1236static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
1237{
1238 int i;
1239 struct kvm_cpuid_entry2 *e, *entry;
1240
Carsten Otte313a3dc2007-10-11 19:16:52 +02001241 entry = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001242 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
1243 e = &vcpu->arch.cpuid_entries[i];
Carsten Otte313a3dc2007-10-11 19:16:52 +02001244 if (e->function == 0x80000001) {
1245 entry = e;
1246 break;
1247 }
1248 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02001249 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
Carsten Otte313a3dc2007-10-11 19:16:52 +02001250 entry->edx &= ~(1 << 20);
1251 printk(KERN_INFO "kvm: guest NX capability removed\n");
1252 }
1253}
1254
Dan Kenigsberg07716712007-11-21 17:10:04 +02001255/* when an old userspace process fills a new kernel module */
Carsten Otte313a3dc2007-10-11 19:16:52 +02001256static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
1257 struct kvm_cpuid *cpuid,
1258 struct kvm_cpuid_entry __user *entries)
1259{
Dan Kenigsberg07716712007-11-21 17:10:04 +02001260 int r, i;
1261 struct kvm_cpuid_entry *cpuid_entries;
1262
1263 r = -E2BIG;
1264 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1265 goto out;
1266 r = -ENOMEM;
1267 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
1268 if (!cpuid_entries)
1269 goto out;
1270 r = -EFAULT;
1271 if (copy_from_user(cpuid_entries, entries,
1272 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
1273 goto out_free;
1274 for (i = 0; i < cpuid->nent; i++) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001275 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
1276 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
1277 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
1278 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
1279 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
1280 vcpu->arch.cpuid_entries[i].index = 0;
1281 vcpu->arch.cpuid_entries[i].flags = 0;
1282 vcpu->arch.cpuid_entries[i].padding[0] = 0;
1283 vcpu->arch.cpuid_entries[i].padding[1] = 0;
1284 vcpu->arch.cpuid_entries[i].padding[2] = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001285 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001286 vcpu->arch.cpuid_nent = cpuid->nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001287 cpuid_fix_nx_cap(vcpu);
1288 r = 0;
1289
1290out_free:
1291 vfree(cpuid_entries);
1292out:
1293 return r;
1294}
1295
1296static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
1297 struct kvm_cpuid2 *cpuid,
1298 struct kvm_cpuid_entry2 __user *entries)
1299{
Carsten Otte313a3dc2007-10-11 19:16:52 +02001300 int r;
1301
1302 r = -E2BIG;
1303 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1304 goto out;
1305 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001306 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
Dan Kenigsberg07716712007-11-21 17:10:04 +02001307 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02001308 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001309 vcpu->arch.cpuid_nent = cpuid->nent;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001310 return 0;
1311
1312out:
1313 return r;
1314}
1315
Dan Kenigsberg07716712007-11-21 17:10:04 +02001316static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
1317 struct kvm_cpuid2 *cpuid,
1318 struct kvm_cpuid_entry2 __user *entries)
1319{
1320 int r;
1321
1322 r = -E2BIG;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001323 if (cpuid->nent < vcpu->arch.cpuid_nent)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001324 goto out;
1325 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001326 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
1327 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02001328 goto out;
1329 return 0;
1330
1331out:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001332 cpuid->nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001333 return r;
1334}
1335
1336static inline u32 bit(int bitno)
1337{
1338 return 1 << (bitno & 31);
1339}
1340
1341static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1342 u32 index)
1343{
1344 entry->function = function;
1345 entry->index = index;
1346 cpuid_count(entry->function, entry->index,
1347 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
1348 entry->flags = 0;
1349}
1350
1351static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1352 u32 index, int *nent, int maxnent)
1353{
1354 const u32 kvm_supported_word0_x86_features = bit(X86_FEATURE_FPU) |
1355 bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
1356 bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
1357 bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
1358 bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
1359 bit(X86_FEATURE_SEP) | bit(X86_FEATURE_PGE) |
1360 bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
1361 bit(X86_FEATURE_CLFLSH) | bit(X86_FEATURE_MMX) |
1362 bit(X86_FEATURE_FXSR) | bit(X86_FEATURE_XMM) |
1363 bit(X86_FEATURE_XMM2) | bit(X86_FEATURE_SELFSNOOP);
1364 const u32 kvm_supported_word1_x86_features = bit(X86_FEATURE_FPU) |
1365 bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
1366 bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
1367 bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
1368 bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
1369 bit(X86_FEATURE_PGE) |
1370 bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
1371 bit(X86_FEATURE_MMX) | bit(X86_FEATURE_FXSR) |
1372 bit(X86_FEATURE_SYSCALL) |
1373 (bit(X86_FEATURE_NX) && is_efer_nx()) |
1374#ifdef CONFIG_X86_64
1375 bit(X86_FEATURE_LM) |
1376#endif
1377 bit(X86_FEATURE_MMXEXT) |
1378 bit(X86_FEATURE_3DNOWEXT) |
1379 bit(X86_FEATURE_3DNOW);
1380 const u32 kvm_supported_word3_x86_features =
1381 bit(X86_FEATURE_XMM3) | bit(X86_FEATURE_CX16);
1382 const u32 kvm_supported_word6_x86_features =
1383 bit(X86_FEATURE_LAHF_LM) | bit(X86_FEATURE_CMP_LEGACY);
1384
1385 /* all func 2 cpuid_count() should be called on the same cpu */
1386 get_cpu();
1387 do_cpuid_1_ent(entry, function, index);
1388 ++*nent;
1389
1390 switch (function) {
1391 case 0:
1392 entry->eax = min(entry->eax, (u32)0xb);
1393 break;
1394 case 1:
1395 entry->edx &= kvm_supported_word0_x86_features;
1396 entry->ecx &= kvm_supported_word3_x86_features;
1397 break;
1398 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1399 * may return different values. This forces us to get_cpu() before
1400 * issuing the first command, and also to emulate this annoying behavior
1401 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1402 case 2: {
1403 int t, times = entry->eax & 0xff;
1404
1405 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1406 for (t = 1; t < times && *nent < maxnent; ++t) {
1407 do_cpuid_1_ent(&entry[t], function, 0);
1408 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1409 ++*nent;
1410 }
1411 break;
1412 }
1413 /* function 4 and 0xb have additional index. */
1414 case 4: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001415 int i, cache_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001416
1417 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1418 /* read more entries until cache_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001419 for (i = 1; *nent < maxnent; ++i) {
1420 cache_type = entry[i - 1].eax & 0x1f;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001421 if (!cache_type)
1422 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001423 do_cpuid_1_ent(&entry[i], function, i);
1424 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001425 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1426 ++*nent;
1427 }
1428 break;
1429 }
1430 case 0xb: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001431 int i, level_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001432
1433 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1434 /* read more entries until level_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001435 for (i = 1; *nent < maxnent; ++i) {
1436 level_type = entry[i - 1].ecx & 0xff;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001437 if (!level_type)
1438 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001439 do_cpuid_1_ent(&entry[i], function, i);
1440 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001441 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1442 ++*nent;
1443 }
1444 break;
1445 }
1446 case 0x80000000:
1447 entry->eax = min(entry->eax, 0x8000001a);
1448 break;
1449 case 0x80000001:
1450 entry->edx &= kvm_supported_word1_x86_features;
1451 entry->ecx &= kvm_supported_word6_x86_features;
1452 break;
1453 }
1454 put_cpu();
1455}
1456
Avi Kivity674eea02008-02-11 18:37:23 +02001457static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
Dan Kenigsberg07716712007-11-21 17:10:04 +02001458 struct kvm_cpuid_entry2 __user *entries)
1459{
1460 struct kvm_cpuid_entry2 *cpuid_entries;
1461 int limit, nent = 0, r = -E2BIG;
1462 u32 func;
1463
1464 if (cpuid->nent < 1)
1465 goto out;
1466 r = -ENOMEM;
1467 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
1468 if (!cpuid_entries)
1469 goto out;
1470
1471 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
1472 limit = cpuid_entries[0].eax;
1473 for (func = 1; func <= limit && nent < cpuid->nent; ++func)
1474 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1475 &nent, cpuid->nent);
1476 r = -E2BIG;
1477 if (nent >= cpuid->nent)
1478 goto out_free;
1479
1480 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
1481 limit = cpuid_entries[nent - 1].eax;
1482 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
1483 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1484 &nent, cpuid->nent);
1485 r = -EFAULT;
1486 if (copy_to_user(entries, cpuid_entries,
1487 nent * sizeof(struct kvm_cpuid_entry2)))
1488 goto out_free;
1489 cpuid->nent = nent;
1490 r = 0;
1491
1492out_free:
1493 vfree(cpuid_entries);
1494out:
1495 return r;
1496}
1497
Carsten Otte313a3dc2007-10-11 19:16:52 +02001498static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
1499 struct kvm_lapic_state *s)
1500{
1501 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001502 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001503 vcpu_put(vcpu);
1504
1505 return 0;
1506}
1507
1508static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
1509 struct kvm_lapic_state *s)
1510{
1511 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001512 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001513 kvm_apic_post_state_restore(vcpu);
1514 vcpu_put(vcpu);
1515
1516 return 0;
1517}
1518
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001519static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
1520 struct kvm_interrupt *irq)
1521{
1522 if (irq->irq < 0 || irq->irq >= 256)
1523 return -EINVAL;
1524 if (irqchip_in_kernel(vcpu->kvm))
1525 return -ENXIO;
1526 vcpu_load(vcpu);
1527
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001528 set_bit(irq->irq, vcpu->arch.irq_pending);
1529 set_bit(irq->irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001530
1531 vcpu_put(vcpu);
1532
1533 return 0;
1534}
1535
Avi Kivityb209749f2007-10-22 16:50:39 +02001536static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
1537 struct kvm_tpr_access_ctl *tac)
1538{
1539 if (tac->flags)
1540 return -EINVAL;
1541 vcpu->arch.tpr_access_reporting = !!tac->enabled;
1542 return 0;
1543}
1544
Carsten Otte313a3dc2007-10-11 19:16:52 +02001545long kvm_arch_vcpu_ioctl(struct file *filp,
1546 unsigned int ioctl, unsigned long arg)
1547{
1548 struct kvm_vcpu *vcpu = filp->private_data;
1549 void __user *argp = (void __user *)arg;
1550 int r;
Dave Hansenb772ff32008-08-11 10:01:47 -07001551 struct kvm_lapic_state *lapic = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001552
1553 switch (ioctl) {
1554 case KVM_GET_LAPIC: {
Dave Hansenb772ff32008-08-11 10:01:47 -07001555 lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001556
Dave Hansenb772ff32008-08-11 10:01:47 -07001557 r = -ENOMEM;
1558 if (!lapic)
1559 goto out;
1560 r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001561 if (r)
1562 goto out;
1563 r = -EFAULT;
Dave Hansenb772ff32008-08-11 10:01:47 -07001564 if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02001565 goto out;
1566 r = 0;
1567 break;
1568 }
1569 case KVM_SET_LAPIC: {
Dave Hansenb772ff32008-08-11 10:01:47 -07001570 lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
1571 r = -ENOMEM;
1572 if (!lapic)
Carsten Otte313a3dc2007-10-11 19:16:52 +02001573 goto out;
Dave Hansenb772ff32008-08-11 10:01:47 -07001574 r = -EFAULT;
1575 if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state)))
1576 goto out;
1577 r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001578 if (r)
1579 goto out;
1580 r = 0;
1581 break;
1582 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001583 case KVM_INTERRUPT: {
1584 struct kvm_interrupt irq;
1585
1586 r = -EFAULT;
1587 if (copy_from_user(&irq, argp, sizeof irq))
1588 goto out;
1589 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1590 if (r)
1591 goto out;
1592 r = 0;
1593 break;
1594 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001595 case KVM_SET_CPUID: {
1596 struct kvm_cpuid __user *cpuid_arg = argp;
1597 struct kvm_cpuid cpuid;
1598
1599 r = -EFAULT;
1600 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1601 goto out;
1602 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
1603 if (r)
1604 goto out;
1605 break;
1606 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02001607 case KVM_SET_CPUID2: {
1608 struct kvm_cpuid2 __user *cpuid_arg = argp;
1609 struct kvm_cpuid2 cpuid;
1610
1611 r = -EFAULT;
1612 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1613 goto out;
1614 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
1615 cpuid_arg->entries);
1616 if (r)
1617 goto out;
1618 break;
1619 }
1620 case KVM_GET_CPUID2: {
1621 struct kvm_cpuid2 __user *cpuid_arg = argp;
1622 struct kvm_cpuid2 cpuid;
1623
1624 r = -EFAULT;
1625 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1626 goto out;
1627 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
1628 cpuid_arg->entries);
1629 if (r)
1630 goto out;
1631 r = -EFAULT;
1632 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1633 goto out;
1634 r = 0;
1635 break;
1636 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001637 case KVM_GET_MSRS:
1638 r = msr_io(vcpu, argp, kvm_get_msr, 1);
1639 break;
1640 case KVM_SET_MSRS:
1641 r = msr_io(vcpu, argp, do_set_msr, 0);
1642 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02001643 case KVM_TPR_ACCESS_REPORTING: {
1644 struct kvm_tpr_access_ctl tac;
1645
1646 r = -EFAULT;
1647 if (copy_from_user(&tac, argp, sizeof tac))
1648 goto out;
1649 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
1650 if (r)
1651 goto out;
1652 r = -EFAULT;
1653 if (copy_to_user(argp, &tac, sizeof tac))
1654 goto out;
1655 r = 0;
1656 break;
1657 };
Avi Kivityb93463a2007-10-25 16:52:32 +02001658 case KVM_SET_VAPIC_ADDR: {
1659 struct kvm_vapic_addr va;
1660
1661 r = -EINVAL;
1662 if (!irqchip_in_kernel(vcpu->kvm))
1663 goto out;
1664 r = -EFAULT;
1665 if (copy_from_user(&va, argp, sizeof va))
1666 goto out;
1667 r = 0;
1668 kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
1669 break;
1670 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001671 default:
1672 r = -EINVAL;
1673 }
1674out:
Dave Hansenb772ff32008-08-11 10:01:47 -07001675 if (lapic)
1676 kfree(lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001677 return r;
1678}
1679
Carsten Otte1fe779f2007-10-29 16:08:35 +01001680static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
1681{
1682 int ret;
1683
1684 if (addr > (unsigned int)(-3 * PAGE_SIZE))
1685 return -1;
1686 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
1687 return ret;
1688}
1689
1690static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
1691 u32 kvm_nr_mmu_pages)
1692{
1693 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
1694 return -EINVAL;
1695
Izik Eidus72dc67a2008-02-10 18:04:15 +02001696 down_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001697
1698 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001699 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001700
Izik Eidus72dc67a2008-02-10 18:04:15 +02001701 up_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001702 return 0;
1703}
1704
1705static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
1706{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001707 return kvm->arch.n_alloc_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001708}
1709
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08001710gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
1711{
1712 int i;
1713 struct kvm_mem_alias *alias;
1714
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001715 for (i = 0; i < kvm->arch.naliases; ++i) {
1716 alias = &kvm->arch.aliases[i];
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08001717 if (gfn >= alias->base_gfn
1718 && gfn < alias->base_gfn + alias->npages)
1719 return alias->target_gfn + gfn - alias->base_gfn;
1720 }
1721 return gfn;
1722}
1723
Carsten Otte1fe779f2007-10-29 16:08:35 +01001724/*
1725 * Set a new alias region. Aliases map a portion of physical memory into
1726 * another portion. This is useful for memory windows, for example the PC
1727 * VGA region.
1728 */
1729static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
1730 struct kvm_memory_alias *alias)
1731{
1732 int r, n;
1733 struct kvm_mem_alias *p;
1734
1735 r = -EINVAL;
1736 /* General sanity checks */
1737 if (alias->memory_size & (PAGE_SIZE - 1))
1738 goto out;
1739 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
1740 goto out;
1741 if (alias->slot >= KVM_ALIAS_SLOTS)
1742 goto out;
1743 if (alias->guest_phys_addr + alias->memory_size
1744 < alias->guest_phys_addr)
1745 goto out;
1746 if (alias->target_phys_addr + alias->memory_size
1747 < alias->target_phys_addr)
1748 goto out;
1749
Izik Eidus72dc67a2008-02-10 18:04:15 +02001750 down_write(&kvm->slots_lock);
Andrea Arcangelia1708ce2008-07-25 16:26:39 +02001751 spin_lock(&kvm->mmu_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001752
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001753 p = &kvm->arch.aliases[alias->slot];
Carsten Otte1fe779f2007-10-29 16:08:35 +01001754 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
1755 p->npages = alias->memory_size >> PAGE_SHIFT;
1756 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
1757
1758 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001759 if (kvm->arch.aliases[n - 1].npages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01001760 break;
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001761 kvm->arch.naliases = n;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001762
Andrea Arcangelia1708ce2008-07-25 16:26:39 +02001763 spin_unlock(&kvm->mmu_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001764 kvm_mmu_zap_all(kvm);
1765
Izik Eidus72dc67a2008-02-10 18:04:15 +02001766 up_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001767
1768 return 0;
1769
1770out:
1771 return r;
1772}
1773
1774static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1775{
1776 int r;
1777
1778 r = 0;
1779 switch (chip->chip_id) {
1780 case KVM_IRQCHIP_PIC_MASTER:
1781 memcpy(&chip->chip.pic,
1782 &pic_irqchip(kvm)->pics[0],
1783 sizeof(struct kvm_pic_state));
1784 break;
1785 case KVM_IRQCHIP_PIC_SLAVE:
1786 memcpy(&chip->chip.pic,
1787 &pic_irqchip(kvm)->pics[1],
1788 sizeof(struct kvm_pic_state));
1789 break;
1790 case KVM_IRQCHIP_IOAPIC:
1791 memcpy(&chip->chip.ioapic,
1792 ioapic_irqchip(kvm),
1793 sizeof(struct kvm_ioapic_state));
1794 break;
1795 default:
1796 r = -EINVAL;
1797 break;
1798 }
1799 return r;
1800}
1801
1802static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1803{
1804 int r;
1805
1806 r = 0;
1807 switch (chip->chip_id) {
1808 case KVM_IRQCHIP_PIC_MASTER:
1809 memcpy(&pic_irqchip(kvm)->pics[0],
1810 &chip->chip.pic,
1811 sizeof(struct kvm_pic_state));
1812 break;
1813 case KVM_IRQCHIP_PIC_SLAVE:
1814 memcpy(&pic_irqchip(kvm)->pics[1],
1815 &chip->chip.pic,
1816 sizeof(struct kvm_pic_state));
1817 break;
1818 case KVM_IRQCHIP_IOAPIC:
1819 memcpy(ioapic_irqchip(kvm),
1820 &chip->chip.ioapic,
1821 sizeof(struct kvm_ioapic_state));
1822 break;
1823 default:
1824 r = -EINVAL;
1825 break;
1826 }
1827 kvm_pic_update_irq(pic_irqchip(kvm));
1828 return r;
1829}
1830
Sheng Yange0f63cb2008-03-04 00:50:59 +08001831static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
1832{
1833 int r = 0;
1834
1835 memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
1836 return r;
1837}
1838
1839static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
1840{
1841 int r = 0;
1842
1843 memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
1844 kvm_pit_load_count(kvm, 0, ps->channels[0].count);
1845 return r;
1846}
1847
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001848/*
1849 * Get (and clear) the dirty memory log for a memory slot.
1850 */
1851int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1852 struct kvm_dirty_log *log)
1853{
1854 int r;
1855 int n;
1856 struct kvm_memory_slot *memslot;
1857 int is_dirty = 0;
1858
Izik Eidus72dc67a2008-02-10 18:04:15 +02001859 down_write(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001860
1861 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1862 if (r)
1863 goto out;
1864
1865 /* If nothing is dirty, don't bother messing with page tables. */
1866 if (is_dirty) {
1867 kvm_mmu_slot_remove_write_access(kvm, log->slot);
1868 kvm_flush_remote_tlbs(kvm);
1869 memslot = &kvm->memslots[log->slot];
1870 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1871 memset(memslot->dirty_bitmap, 0, n);
1872 }
1873 r = 0;
1874out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02001875 up_write(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001876 return r;
1877}
1878
Carsten Otte1fe779f2007-10-29 16:08:35 +01001879long kvm_arch_vm_ioctl(struct file *filp,
1880 unsigned int ioctl, unsigned long arg)
1881{
1882 struct kvm *kvm = filp->private_data;
1883 void __user *argp = (void __user *)arg;
1884 int r = -EINVAL;
Dave Hansenf0d66272008-08-11 10:01:45 -07001885 /*
1886 * This union makes it completely explicit to gcc-3.x
1887 * that these two variables' stack usage should be
1888 * combined, not added together.
1889 */
1890 union {
1891 struct kvm_pit_state ps;
1892 struct kvm_memory_alias alias;
1893 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001894
1895 switch (ioctl) {
1896 case KVM_SET_TSS_ADDR:
1897 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
1898 if (r < 0)
1899 goto out;
1900 break;
1901 case KVM_SET_MEMORY_REGION: {
1902 struct kvm_memory_region kvm_mem;
1903 struct kvm_userspace_memory_region kvm_userspace_mem;
1904
1905 r = -EFAULT;
1906 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
1907 goto out;
1908 kvm_userspace_mem.slot = kvm_mem.slot;
1909 kvm_userspace_mem.flags = kvm_mem.flags;
1910 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
1911 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
1912 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
1913 if (r)
1914 goto out;
1915 break;
1916 }
1917 case KVM_SET_NR_MMU_PAGES:
1918 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
1919 if (r)
1920 goto out;
1921 break;
1922 case KVM_GET_NR_MMU_PAGES:
1923 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
1924 break;
Dave Hansenf0d66272008-08-11 10:01:45 -07001925 case KVM_SET_MEMORY_ALIAS:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001926 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07001927 if (copy_from_user(&u.alias, argp, sizeof(struct kvm_memory_alias)))
Carsten Otte1fe779f2007-10-29 16:08:35 +01001928 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07001929 r = kvm_vm_ioctl_set_memory_alias(kvm, &u.alias);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001930 if (r)
1931 goto out;
1932 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001933 case KVM_CREATE_IRQCHIP:
1934 r = -ENOMEM;
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08001935 kvm->arch.vpic = kvm_create_pic(kvm);
1936 if (kvm->arch.vpic) {
Carsten Otte1fe779f2007-10-29 16:08:35 +01001937 r = kvm_ioapic_init(kvm);
1938 if (r) {
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08001939 kfree(kvm->arch.vpic);
1940 kvm->arch.vpic = NULL;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001941 goto out;
1942 }
1943 } else
1944 goto out;
1945 break;
Sheng Yang78376992008-01-28 05:10:22 +08001946 case KVM_CREATE_PIT:
1947 r = -ENOMEM;
1948 kvm->arch.vpit = kvm_create_pit(kvm);
1949 if (kvm->arch.vpit)
1950 r = 0;
1951 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001952 case KVM_IRQ_LINE: {
1953 struct kvm_irq_level irq_event;
1954
1955 r = -EFAULT;
1956 if (copy_from_user(&irq_event, argp, sizeof irq_event))
1957 goto out;
1958 if (irqchip_in_kernel(kvm)) {
1959 mutex_lock(&kvm->lock);
Amit Shah29c8fa32008-08-18 15:07:05 +03001960 kvm_set_irq(kvm, irq_event.irq, irq_event.level);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001961 mutex_unlock(&kvm->lock);
1962 r = 0;
1963 }
1964 break;
1965 }
1966 case KVM_GET_IRQCHIP: {
1967 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Dave Hansenf0d66272008-08-11 10:01:45 -07001968 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001969
Dave Hansenf0d66272008-08-11 10:01:45 -07001970 r = -ENOMEM;
1971 if (!chip)
Carsten Otte1fe779f2007-10-29 16:08:35 +01001972 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07001973 r = -EFAULT;
1974 if (copy_from_user(chip, argp, sizeof *chip))
1975 goto get_irqchip_out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001976 r = -ENXIO;
1977 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07001978 goto get_irqchip_out;
1979 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
1980 if (r)
1981 goto get_irqchip_out;
1982 r = -EFAULT;
1983 if (copy_to_user(argp, chip, sizeof *chip))
1984 goto get_irqchip_out;
1985 r = 0;
1986 get_irqchip_out:
1987 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001988 if (r)
1989 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001990 break;
1991 }
1992 case KVM_SET_IRQCHIP: {
1993 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Dave Hansenf0d66272008-08-11 10:01:45 -07001994 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001995
Dave Hansenf0d66272008-08-11 10:01:45 -07001996 r = -ENOMEM;
1997 if (!chip)
Carsten Otte1fe779f2007-10-29 16:08:35 +01001998 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07001999 r = -EFAULT;
2000 if (copy_from_user(chip, argp, sizeof *chip))
2001 goto set_irqchip_out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002002 r = -ENXIO;
2003 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07002004 goto set_irqchip_out;
2005 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
2006 if (r)
2007 goto set_irqchip_out;
2008 r = 0;
2009 set_irqchip_out:
2010 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002011 if (r)
2012 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002013 break;
2014 }
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03002015 case KVM_ASSIGN_PCI_DEVICE: {
2016 struct kvm_assigned_pci_dev assigned_dev;
2017
2018 r = -EFAULT;
2019 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
2020 goto out;
2021 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
2022 if (r)
2023 goto out;
2024 break;
2025 }
2026 case KVM_ASSIGN_IRQ: {
2027 struct kvm_assigned_irq assigned_irq;
2028
2029 r = -EFAULT;
2030 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
2031 goto out;
2032 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
2033 if (r)
2034 goto out;
2035 break;
2036 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08002037 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08002038 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002039 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002040 goto out;
2041 r = -ENXIO;
2042 if (!kvm->arch.vpit)
2043 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002044 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002045 if (r)
2046 goto out;
2047 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002048 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002049 goto out;
2050 r = 0;
2051 break;
2052 }
2053 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08002054 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002055 if (copy_from_user(&u.ps, argp, sizeof u.ps))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002056 goto out;
2057 r = -ENXIO;
2058 if (!kvm->arch.vpit)
2059 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002060 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002061 if (r)
2062 goto out;
2063 r = 0;
2064 break;
2065 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01002066 default:
2067 ;
2068 }
2069out:
2070 return r;
2071}
2072
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002073static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02002074{
2075 u32 dummy[2];
2076 unsigned i, j;
2077
2078 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2079 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2080 continue;
2081 if (j < i)
2082 msrs_to_save[j] = msrs_to_save[i];
2083 j++;
2084 }
2085 num_msrs_to_save = j;
2086}
2087
Carsten Ottebbd9b642007-10-30 18:44:21 +01002088/*
2089 * Only apic need an MMIO device hook, so shortcut now..
2090 */
2091static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
Laurent Vivier92760492008-05-30 16:05:53 +02002092 gpa_t addr, int len,
2093 int is_write)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002094{
2095 struct kvm_io_device *dev;
2096
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002097 if (vcpu->arch.apic) {
2098 dev = &vcpu->arch.apic->dev;
Laurent Vivier92760492008-05-30 16:05:53 +02002099 if (dev->in_range(dev, addr, len, is_write))
Carsten Ottebbd9b642007-10-30 18:44:21 +01002100 return dev;
2101 }
2102 return NULL;
2103}
2104
2105
2106static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
Laurent Vivier92760492008-05-30 16:05:53 +02002107 gpa_t addr, int len,
2108 int is_write)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002109{
2110 struct kvm_io_device *dev;
2111
Laurent Vivier92760492008-05-30 16:05:53 +02002112 dev = vcpu_find_pervcpu_dev(vcpu, addr, len, is_write);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002113 if (dev == NULL)
Laurent Vivier92760492008-05-30 16:05:53 +02002114 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr, len,
2115 is_write);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002116 return dev;
2117}
2118
2119int emulator_read_std(unsigned long addr,
2120 void *val,
2121 unsigned int bytes,
2122 struct kvm_vcpu *vcpu)
2123{
2124 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002125 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002126
2127 while (bytes) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002128 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002129 unsigned offset = addr & (PAGE_SIZE-1);
2130 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
2131 int ret;
2132
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002133 if (gpa == UNMAPPED_GVA) {
2134 r = X86EMUL_PROPAGATE_FAULT;
2135 goto out;
2136 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01002137 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002138 if (ret < 0) {
2139 r = X86EMUL_UNHANDLEABLE;
2140 goto out;
2141 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01002142
2143 bytes -= tocopy;
2144 data += tocopy;
2145 addr += tocopy;
2146 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002147out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002148 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002149}
2150EXPORT_SYMBOL_GPL(emulator_read_std);
2151
Carsten Ottebbd9b642007-10-30 18:44:21 +01002152static int emulator_read_emulated(unsigned long addr,
2153 void *val,
2154 unsigned int bytes,
2155 struct kvm_vcpu *vcpu)
2156{
2157 struct kvm_io_device *mmio_dev;
2158 gpa_t gpa;
2159
2160 if (vcpu->mmio_read_completed) {
2161 memcpy(val, vcpu->mmio_data, bytes);
2162 vcpu->mmio_read_completed = 0;
2163 return X86EMUL_CONTINUE;
2164 }
2165
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002166 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002167
2168 /* For APIC access vmexit */
2169 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
2170 goto mmio;
2171
2172 if (emulator_read_std(addr, val, bytes, vcpu)
2173 == X86EMUL_CONTINUE)
2174 return X86EMUL_CONTINUE;
2175 if (gpa == UNMAPPED_GVA)
2176 return X86EMUL_PROPAGATE_FAULT;
2177
2178mmio:
2179 /*
2180 * Is this MMIO handled locally?
2181 */
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002182 mutex_lock(&vcpu->kvm->lock);
Laurent Vivier92760492008-05-30 16:05:53 +02002183 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa, bytes, 0);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002184 if (mmio_dev) {
2185 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002186 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002187 return X86EMUL_CONTINUE;
2188 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002189 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002190
2191 vcpu->mmio_needed = 1;
2192 vcpu->mmio_phys_addr = gpa;
2193 vcpu->mmio_size = bytes;
2194 vcpu->mmio_is_write = 0;
2195
2196 return X86EMUL_UNHANDLEABLE;
2197}
2198
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002199int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity9f811282008-03-02 14:06:05 +02002200 const void *val, int bytes)
2201{
2202 int ret;
2203
2204 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
2205 if (ret < 0)
2206 return 0;
2207 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
2208 return 1;
2209}
2210
Carsten Ottebbd9b642007-10-30 18:44:21 +01002211static int emulator_write_emulated_onepage(unsigned long addr,
2212 const void *val,
2213 unsigned int bytes,
2214 struct kvm_vcpu *vcpu)
2215{
2216 struct kvm_io_device *mmio_dev;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002217 gpa_t gpa;
2218
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002219 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002220
2221 if (gpa == UNMAPPED_GVA) {
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002222 kvm_inject_page_fault(vcpu, addr, 2);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002223 return X86EMUL_PROPAGATE_FAULT;
2224 }
2225
2226 /* For APIC access vmexit */
2227 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
2228 goto mmio;
2229
2230 if (emulator_write_phys(vcpu, gpa, val, bytes))
2231 return X86EMUL_CONTINUE;
2232
2233mmio:
2234 /*
2235 * Is this MMIO handled locally?
2236 */
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002237 mutex_lock(&vcpu->kvm->lock);
Laurent Vivier92760492008-05-30 16:05:53 +02002238 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa, bytes, 1);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002239 if (mmio_dev) {
2240 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002241 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002242 return X86EMUL_CONTINUE;
2243 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002244 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002245
2246 vcpu->mmio_needed = 1;
2247 vcpu->mmio_phys_addr = gpa;
2248 vcpu->mmio_size = bytes;
2249 vcpu->mmio_is_write = 1;
2250 memcpy(vcpu->mmio_data, val, bytes);
2251
2252 return X86EMUL_CONTINUE;
2253}
2254
2255int emulator_write_emulated(unsigned long addr,
2256 const void *val,
2257 unsigned int bytes,
2258 struct kvm_vcpu *vcpu)
2259{
2260 /* Crossing a page boundary? */
2261 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
2262 int rc, now;
2263
2264 now = -addr & ~PAGE_MASK;
2265 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
2266 if (rc != X86EMUL_CONTINUE)
2267 return rc;
2268 addr += now;
2269 val += now;
2270 bytes -= now;
2271 }
2272 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
2273}
2274EXPORT_SYMBOL_GPL(emulator_write_emulated);
2275
2276static int emulator_cmpxchg_emulated(unsigned long addr,
2277 const void *old,
2278 const void *new,
2279 unsigned int bytes,
2280 struct kvm_vcpu *vcpu)
2281{
2282 static int reported;
2283
2284 if (!reported) {
2285 reported = 1;
2286 printk(KERN_WARNING "kvm: emulating exchange as write\n");
2287 }
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002288#ifndef CONFIG_X86_64
2289 /* guests cmpxchg8b have to be emulated atomically */
2290 if (bytes == 8) {
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002291 gpa_t gpa;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002292 struct page *page;
Andrew Mortonc0b49b02008-02-04 22:27:18 -08002293 char *kaddr;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002294 u64 val;
2295
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002296 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
2297
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002298 if (gpa == UNMAPPED_GVA ||
2299 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
2300 goto emul_write;
2301
2302 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
2303 goto emul_write;
2304
2305 val = *(u64 *)new;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002306
2307 down_read(&current->mm->mmap_sem);
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002308 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002309 up_read(&current->mm->mmap_sem);
2310
Andrew Mortonc0b49b02008-02-04 22:27:18 -08002311 kaddr = kmap_atomic(page, KM_USER0);
2312 set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
2313 kunmap_atomic(kaddr, KM_USER0);
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002314 kvm_release_page_dirty(page);
2315 }
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002316emul_write:
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002317#endif
2318
Carsten Ottebbd9b642007-10-30 18:44:21 +01002319 return emulator_write_emulated(addr, new, bytes, vcpu);
2320}
2321
2322static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
2323{
2324 return kvm_x86_ops->get_segment_base(vcpu, seg);
2325}
2326
2327int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
2328{
2329 return X86EMUL_CONTINUE;
2330}
2331
2332int emulate_clts(struct kvm_vcpu *vcpu)
2333{
Joerg Roedel54e445c2008-04-30 17:56:02 +02002334 KVMTRACE_0D(CLTS, vcpu, handler);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002335 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002336 return X86EMUL_CONTINUE;
2337}
2338
2339int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
2340{
2341 struct kvm_vcpu *vcpu = ctxt->vcpu;
2342
2343 switch (dr) {
2344 case 0 ... 3:
2345 *dest = kvm_x86_ops->get_dr(vcpu, dr);
2346 return X86EMUL_CONTINUE;
2347 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002348 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __func__, dr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002349 return X86EMUL_UNHANDLEABLE;
2350 }
2351}
2352
2353int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
2354{
2355 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
2356 int exception;
2357
2358 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
2359 if (exception) {
2360 /* FIXME: better handling */
2361 return X86EMUL_UNHANDLEABLE;
2362 }
2363 return X86EMUL_CONTINUE;
2364}
2365
2366void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
2367{
Carsten Ottebbd9b642007-10-30 18:44:21 +01002368 u8 opcodes[4];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002369 unsigned long rip = kvm_rip_read(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002370 unsigned long rip_linear;
2371
Avi Kivityf76c7102008-06-13 22:45:42 +03002372 if (!printk_ratelimit())
Carsten Ottebbd9b642007-10-30 18:44:21 +01002373 return;
2374
Glauber Costa25be4602008-06-10 10:46:53 -03002375 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
2376
Carsten Ottebbd9b642007-10-30 18:44:21 +01002377 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
2378
2379 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
2380 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002381}
2382EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
2383
Harvey Harrison14af3f32008-02-19 10:25:50 -08002384static struct x86_emulate_ops emulate_ops = {
Carsten Ottebbd9b642007-10-30 18:44:21 +01002385 .read_std = emulator_read_std,
Carsten Ottebbd9b642007-10-30 18:44:21 +01002386 .read_emulated = emulator_read_emulated,
2387 .write_emulated = emulator_write_emulated,
2388 .cmpxchg_emulated = emulator_cmpxchg_emulated,
2389};
2390
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002391static void cache_all_regs(struct kvm_vcpu *vcpu)
2392{
2393 kvm_register_read(vcpu, VCPU_REGS_RAX);
2394 kvm_register_read(vcpu, VCPU_REGS_RSP);
2395 kvm_register_read(vcpu, VCPU_REGS_RIP);
2396 vcpu->arch.regs_dirty = ~0;
2397}
2398
Carsten Ottebbd9b642007-10-30 18:44:21 +01002399int emulate_instruction(struct kvm_vcpu *vcpu,
2400 struct kvm_run *run,
2401 unsigned long cr2,
2402 u16 error_code,
Sheng Yang571008d2008-01-02 14:49:22 +08002403 int emulation_type)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002404{
2405 int r;
Sheng Yang571008d2008-01-02 14:49:22 +08002406 struct decode_cache *c;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002407
Avi Kivity26eef702008-07-03 14:59:22 +03002408 kvm_clear_exception_queue(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002409 vcpu->arch.mmio_fault_cr2 = cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002410 /*
2411 * TODO: fix x86_emulate.c to use guest_read/write_register
2412 * instead of direct ->regs accesses, can save hundred cycles
2413 * on Intel for instructions that don't read/change RSP, for
2414 * for example.
2415 */
2416 cache_all_regs(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002417
2418 vcpu->mmio_is_write = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002419 vcpu->arch.pio.string = 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002420
Sheng Yang571008d2008-01-02 14:49:22 +08002421 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Carsten Ottebbd9b642007-10-30 18:44:21 +01002422 int cs_db, cs_l;
2423 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
2424
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002425 vcpu->arch.emulate_ctxt.vcpu = vcpu;
2426 vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
2427 vcpu->arch.emulate_ctxt.mode =
2428 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002429 ? X86EMUL_MODE_REAL : cs_l
2430 ? X86EMUL_MODE_PROT64 : cs_db
2431 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
2432
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002433 r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Sheng Yang571008d2008-01-02 14:49:22 +08002434
2435 /* Reject the instructions other than VMCALL/VMMCALL when
2436 * try to emulate invalid opcode */
2437 c = &vcpu->arch.emulate_ctxt.decode;
2438 if ((emulation_type & EMULTYPE_TRAP_UD) &&
2439 (!(c->twobyte && c->b == 0x01 &&
2440 (c->modrm_reg == 0 || c->modrm_reg == 3) &&
2441 c->modrm_mod == 3 && c->modrm_rm == 1)))
2442 return EMULATE_FAIL;
2443
Avi Kivityf2b57562007-11-18 15:17:51 +02002444 ++vcpu->stat.insn_emulation;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002445 if (r) {
Avi Kivityf2b57562007-11-18 15:17:51 +02002446 ++vcpu->stat.insn_emulation_fail;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002447 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
2448 return EMULATE_DONE;
2449 return EMULATE_FAIL;
2450 }
2451 }
2452
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002453 r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002454
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002455 if (vcpu->arch.pio.string)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002456 return EMULATE_DO_MMIO;
2457
2458 if ((r || vcpu->mmio_is_write) && run) {
2459 run->exit_reason = KVM_EXIT_MMIO;
2460 run->mmio.phys_addr = vcpu->mmio_phys_addr;
2461 memcpy(run->mmio.data, vcpu->mmio_data, 8);
2462 run->mmio.len = vcpu->mmio_size;
2463 run->mmio.is_write = vcpu->mmio_is_write;
2464 }
2465
2466 if (r) {
2467 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
2468 return EMULATE_DONE;
2469 if (!vcpu->mmio_needed) {
2470 kvm_report_emulation_failure(vcpu, "mmio");
2471 return EMULATE_FAIL;
2472 }
2473 return EMULATE_DO_MMIO;
2474 }
2475
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002476 kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002477
2478 if (vcpu->mmio_is_write) {
2479 vcpu->mmio_needed = 0;
2480 return EMULATE_DO_MMIO;
2481 }
2482
2483 return EMULATE_DONE;
2484}
2485EXPORT_SYMBOL_GPL(emulate_instruction);
2486
Carsten Ottede7d7892007-10-30 18:44:25 +01002487static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
2488{
2489 int i;
2490
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002491 for (i = 0; i < ARRAY_SIZE(vcpu->arch.pio.guest_pages); ++i)
2492 if (vcpu->arch.pio.guest_pages[i]) {
2493 kvm_release_page_dirty(vcpu->arch.pio.guest_pages[i]);
2494 vcpu->arch.pio.guest_pages[i] = NULL;
Carsten Ottede7d7892007-10-30 18:44:25 +01002495 }
2496}
2497
2498static int pio_copy_data(struct kvm_vcpu *vcpu)
2499{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002500 void *p = vcpu->arch.pio_data;
Carsten Ottede7d7892007-10-30 18:44:25 +01002501 void *q;
2502 unsigned bytes;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002503 int nr_pages = vcpu->arch.pio.guest_pages[1] ? 2 : 1;
Carsten Ottede7d7892007-10-30 18:44:25 +01002504
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002505 q = vmap(vcpu->arch.pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
Carsten Ottede7d7892007-10-30 18:44:25 +01002506 PAGE_KERNEL);
2507 if (!q) {
2508 free_pio_guest_pages(vcpu);
2509 return -ENOMEM;
2510 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002511 q += vcpu->arch.pio.guest_page_offset;
2512 bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
2513 if (vcpu->arch.pio.in)
Carsten Ottede7d7892007-10-30 18:44:25 +01002514 memcpy(q, p, bytes);
2515 else
2516 memcpy(p, q, bytes);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002517 q -= vcpu->arch.pio.guest_page_offset;
Carsten Ottede7d7892007-10-30 18:44:25 +01002518 vunmap(q);
2519 free_pio_guest_pages(vcpu);
2520 return 0;
2521}
2522
2523int complete_pio(struct kvm_vcpu *vcpu)
2524{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002525 struct kvm_pio_request *io = &vcpu->arch.pio;
Carsten Ottede7d7892007-10-30 18:44:25 +01002526 long delta;
2527 int r;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002528 unsigned long val;
Carsten Ottede7d7892007-10-30 18:44:25 +01002529
2530 if (!io->string) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002531 if (io->in) {
2532 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
2533 memcpy(&val, vcpu->arch.pio_data, io->size);
2534 kvm_register_write(vcpu, VCPU_REGS_RAX, val);
2535 }
Carsten Ottede7d7892007-10-30 18:44:25 +01002536 } else {
2537 if (io->in) {
2538 r = pio_copy_data(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002539 if (r)
Carsten Ottede7d7892007-10-30 18:44:25 +01002540 return r;
Carsten Ottede7d7892007-10-30 18:44:25 +01002541 }
2542
2543 delta = 1;
2544 if (io->rep) {
2545 delta *= io->cur_count;
2546 /*
2547 * The size of the register should really depend on
2548 * current address size.
2549 */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002550 val = kvm_register_read(vcpu, VCPU_REGS_RCX);
2551 val -= delta;
2552 kvm_register_write(vcpu, VCPU_REGS_RCX, val);
Carsten Ottede7d7892007-10-30 18:44:25 +01002553 }
2554 if (io->down)
2555 delta = -delta;
2556 delta *= io->size;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002557 if (io->in) {
2558 val = kvm_register_read(vcpu, VCPU_REGS_RDI);
2559 val += delta;
2560 kvm_register_write(vcpu, VCPU_REGS_RDI, val);
2561 } else {
2562 val = kvm_register_read(vcpu, VCPU_REGS_RSI);
2563 val += delta;
2564 kvm_register_write(vcpu, VCPU_REGS_RSI, val);
2565 }
Carsten Ottede7d7892007-10-30 18:44:25 +01002566 }
2567
Carsten Ottede7d7892007-10-30 18:44:25 +01002568 io->count -= io->cur_count;
2569 io->cur_count = 0;
2570
2571 return 0;
2572}
2573
2574static void kernel_pio(struct kvm_io_device *pio_dev,
2575 struct kvm_vcpu *vcpu,
2576 void *pd)
2577{
2578 /* TODO: String I/O for in kernel device */
2579
2580 mutex_lock(&vcpu->kvm->lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002581 if (vcpu->arch.pio.in)
2582 kvm_iodevice_read(pio_dev, vcpu->arch.pio.port,
2583 vcpu->arch.pio.size,
Carsten Ottede7d7892007-10-30 18:44:25 +01002584 pd);
2585 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002586 kvm_iodevice_write(pio_dev, vcpu->arch.pio.port,
2587 vcpu->arch.pio.size,
Carsten Ottede7d7892007-10-30 18:44:25 +01002588 pd);
2589 mutex_unlock(&vcpu->kvm->lock);
2590}
2591
2592static void pio_string_write(struct kvm_io_device *pio_dev,
2593 struct kvm_vcpu *vcpu)
2594{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002595 struct kvm_pio_request *io = &vcpu->arch.pio;
2596 void *pd = vcpu->arch.pio_data;
Carsten Ottede7d7892007-10-30 18:44:25 +01002597 int i;
2598
2599 mutex_lock(&vcpu->kvm->lock);
2600 for (i = 0; i < io->cur_count; i++) {
2601 kvm_iodevice_write(pio_dev, io->port,
2602 io->size,
2603 pd);
2604 pd += io->size;
2605 }
2606 mutex_unlock(&vcpu->kvm->lock);
2607}
2608
2609static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
Laurent Vivier92760492008-05-30 16:05:53 +02002610 gpa_t addr, int len,
2611 int is_write)
Carsten Ottede7d7892007-10-30 18:44:25 +01002612{
Laurent Vivier92760492008-05-30 16:05:53 +02002613 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr, len, is_write);
Carsten Ottede7d7892007-10-30 18:44:25 +01002614}
2615
2616int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2617 int size, unsigned port)
2618{
2619 struct kvm_io_device *pio_dev;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002620 unsigned long val;
Carsten Ottede7d7892007-10-30 18:44:25 +01002621
2622 vcpu->run->exit_reason = KVM_EXIT_IO;
2623 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002624 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01002625 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002626 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
2627 vcpu->run->io.port = vcpu->arch.pio.port = port;
2628 vcpu->arch.pio.in = in;
2629 vcpu->arch.pio.string = 0;
2630 vcpu->arch.pio.down = 0;
2631 vcpu->arch.pio.guest_page_offset = 0;
2632 vcpu->arch.pio.rep = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01002633
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002634 if (vcpu->run->io.direction == KVM_EXIT_IO_IN)
2635 KVMTRACE_2D(IO_READ, vcpu, vcpu->run->io.port, (u32)size,
2636 handler);
2637 else
2638 KVMTRACE_2D(IO_WRITE, vcpu, vcpu->run->io.port, (u32)size,
2639 handler);
2640
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002641 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
2642 memcpy(vcpu->arch.pio_data, &val, 4);
Carsten Ottede7d7892007-10-30 18:44:25 +01002643
2644 kvm_x86_ops->skip_emulated_instruction(vcpu);
2645
Laurent Vivier92760492008-05-30 16:05:53 +02002646 pio_dev = vcpu_find_pio_dev(vcpu, port, size, !in);
Carsten Ottede7d7892007-10-30 18:44:25 +01002647 if (pio_dev) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002648 kernel_pio(pio_dev, vcpu, vcpu->arch.pio_data);
Carsten Ottede7d7892007-10-30 18:44:25 +01002649 complete_pio(vcpu);
2650 return 1;
2651 }
2652 return 0;
2653}
2654EXPORT_SYMBOL_GPL(kvm_emulate_pio);
2655
2656int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2657 int size, unsigned long count, int down,
2658 gva_t address, int rep, unsigned port)
2659{
2660 unsigned now, in_page;
2661 int i, ret = 0;
2662 int nr_pages = 1;
2663 struct page *page;
2664 struct kvm_io_device *pio_dev;
2665
2666 vcpu->run->exit_reason = KVM_EXIT_IO;
2667 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002668 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01002669 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002670 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
2671 vcpu->run->io.port = vcpu->arch.pio.port = port;
2672 vcpu->arch.pio.in = in;
2673 vcpu->arch.pio.string = 1;
2674 vcpu->arch.pio.down = down;
2675 vcpu->arch.pio.guest_page_offset = offset_in_page(address);
2676 vcpu->arch.pio.rep = rep;
Carsten Ottede7d7892007-10-30 18:44:25 +01002677
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002678 if (vcpu->run->io.direction == KVM_EXIT_IO_IN)
2679 KVMTRACE_2D(IO_READ, vcpu, vcpu->run->io.port, (u32)size,
2680 handler);
2681 else
2682 KVMTRACE_2D(IO_WRITE, vcpu, vcpu->run->io.port, (u32)size,
2683 handler);
2684
Carsten Ottede7d7892007-10-30 18:44:25 +01002685 if (!count) {
2686 kvm_x86_ops->skip_emulated_instruction(vcpu);
2687 return 1;
2688 }
2689
2690 if (!down)
2691 in_page = PAGE_SIZE - offset_in_page(address);
2692 else
2693 in_page = offset_in_page(address) + size;
2694 now = min(count, (unsigned long)in_page / size);
2695 if (!now) {
2696 /*
2697 * String I/O straddles page boundary. Pin two guest pages
2698 * so that we satisfy atomicity constraints. Do just one
2699 * transaction to avoid complexity.
2700 */
2701 nr_pages = 2;
2702 now = 1;
2703 }
2704 if (down) {
2705 /*
2706 * String I/O in reverse. Yuck. Kill the guest, fix later.
2707 */
2708 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002709 kvm_inject_gp(vcpu, 0);
Carsten Ottede7d7892007-10-30 18:44:25 +01002710 return 1;
2711 }
2712 vcpu->run->io.count = now;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002713 vcpu->arch.pio.cur_count = now;
Carsten Ottede7d7892007-10-30 18:44:25 +01002714
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002715 if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
Carsten Ottede7d7892007-10-30 18:44:25 +01002716 kvm_x86_ops->skip_emulated_instruction(vcpu);
2717
2718 for (i = 0; i < nr_pages; ++i) {
Carsten Ottede7d7892007-10-30 18:44:25 +01002719 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002720 vcpu->arch.pio.guest_pages[i] = page;
Carsten Ottede7d7892007-10-30 18:44:25 +01002721 if (!page) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002722 kvm_inject_gp(vcpu, 0);
Carsten Ottede7d7892007-10-30 18:44:25 +01002723 free_pio_guest_pages(vcpu);
2724 return 1;
2725 }
2726 }
2727
Laurent Vivier92760492008-05-30 16:05:53 +02002728 pio_dev = vcpu_find_pio_dev(vcpu, port,
2729 vcpu->arch.pio.cur_count,
2730 !vcpu->arch.pio.in);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002731 if (!vcpu->arch.pio.in) {
Carsten Ottede7d7892007-10-30 18:44:25 +01002732 /* string PIO write */
2733 ret = pio_copy_data(vcpu);
2734 if (ret >= 0 && pio_dev) {
2735 pio_string_write(pio_dev, vcpu);
2736 complete_pio(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002737 if (vcpu->arch.pio.count == 0)
Carsten Ottede7d7892007-10-30 18:44:25 +01002738 ret = 1;
2739 }
2740 } else if (pio_dev)
2741 pr_unimpl(vcpu, "no string pio read support yet, "
2742 "port %x size %d count %ld\n",
2743 port, size, count);
2744
2745 return ret;
2746}
2747EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
2748
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002749int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02002750{
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002751 int r;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002752 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
2753
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002754 if (kvm_x86_ops) {
2755 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002756 r = -EEXIST;
2757 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002758 }
2759
2760 if (!ops->cpu_has_kvm_support()) {
2761 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002762 r = -EOPNOTSUPP;
2763 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002764 }
2765 if (ops->disabled_by_bios()) {
2766 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002767 r = -EOPNOTSUPP;
2768 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002769 }
2770
Avi Kivity97db56c2008-01-13 13:23:56 +02002771 r = kvm_mmu_module_init();
2772 if (r)
2773 goto out;
2774
2775 kvm_init_msr_list();
2776
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002777 kvm_x86_ops = ops;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002778 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
Sheng Yang7b523452008-04-25 21:13:50 +08002779 kvm_mmu_set_base_ptes(PT_PRESENT_MASK);
2780 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
2781 PT_DIRTY_MASK, PT64_NX_MASK, 0);
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002782 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002783
2784out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002785 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02002786}
Hollis Blanchard8776e512007-10-31 17:24:24 -05002787
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002788void kvm_arch_exit(void)
2789{
2790 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002791 kvm_mmu_module_exit();
2792}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002793
Hollis Blanchard8776e512007-10-31 17:24:24 -05002794int kvm_emulate_halt(struct kvm_vcpu *vcpu)
2795{
2796 ++vcpu->stat.halt_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002797 KVMTRACE_0D(HLT, vcpu, handler);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002798 if (irqchip_in_kernel(vcpu->kvm)) {
Avi Kivitya4535292008-04-13 17:54:35 +03002799 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002800 up_read(&vcpu->kvm->slots_lock);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002801 kvm_vcpu_block(vcpu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002802 down_read(&vcpu->kvm->slots_lock);
Avi Kivitya4535292008-04-13 17:54:35 +03002803 if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE)
Hollis Blanchard8776e512007-10-31 17:24:24 -05002804 return -EINTR;
2805 return 1;
2806 } else {
2807 vcpu->run->exit_reason = KVM_EXIT_HLT;
2808 return 0;
2809 }
2810}
2811EXPORT_SYMBOL_GPL(kvm_emulate_halt);
2812
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002813static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0,
2814 unsigned long a1)
2815{
2816 if (is_long_mode(vcpu))
2817 return a0;
2818 else
2819 return a0 | ((gpa_t)a1 << 32);
2820}
2821
Hollis Blanchard8776e512007-10-31 17:24:24 -05002822int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
2823{
2824 unsigned long nr, a0, a1, a2, a3, ret;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002825 int r = 1;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002826
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002827 nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
2828 a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
2829 a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
2830 a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
2831 a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002832
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002833 KVMTRACE_1D(VMMCALL, vcpu, (u32)nr, handler);
2834
Hollis Blanchard8776e512007-10-31 17:24:24 -05002835 if (!is_long_mode(vcpu)) {
2836 nr &= 0xFFFFFFFF;
2837 a0 &= 0xFFFFFFFF;
2838 a1 &= 0xFFFFFFFF;
2839 a2 &= 0xFFFFFFFF;
2840 a3 &= 0xFFFFFFFF;
2841 }
2842
2843 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02002844 case KVM_HC_VAPIC_POLL_IRQ:
2845 ret = 0;
2846 break;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002847 case KVM_HC_MMU_OP:
2848 r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret);
2849 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002850 default:
2851 ret = -KVM_ENOSYS;
2852 break;
2853 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002854 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
Amit Shahf11c3a82008-02-21 01:00:30 +05302855 ++vcpu->stat.hypercalls;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002856 return r;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002857}
2858EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
2859
2860int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
2861{
2862 char instruction[3];
2863 int ret = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002864 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002865
Hollis Blanchard8776e512007-10-31 17:24:24 -05002866
2867 /*
2868 * Blow out the MMU to ensure that no other VCPU has an active mapping
2869 * to ensure that the updated hypercall appears atomically across all
2870 * VCPUs.
2871 */
2872 kvm_mmu_zap_all(vcpu->kvm);
2873
Hollis Blanchard8776e512007-10-31 17:24:24 -05002874 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002875 if (emulator_write_emulated(rip, instruction, 3, vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05002876 != X86EMUL_CONTINUE)
2877 ret = -EFAULT;
2878
Hollis Blanchard8776e512007-10-31 17:24:24 -05002879 return ret;
2880}
2881
2882static u64 mk_cr_64(u64 curr_cr, u32 new_val)
2883{
2884 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
2885}
2886
2887void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2888{
2889 struct descriptor_table dt = { limit, base };
2890
2891 kvm_x86_ops->set_gdt(vcpu, &dt);
2892}
2893
2894void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2895{
2896 struct descriptor_table dt = { limit, base };
2897
2898 kvm_x86_ops->set_idt(vcpu, &dt);
2899}
2900
2901void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
2902 unsigned long *rflags)
2903{
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002904 kvm_lmsw(vcpu, msw);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002905 *rflags = kvm_x86_ops->get_rflags(vcpu);
2906}
2907
2908unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
2909{
Joerg Roedel54e445c2008-04-30 17:56:02 +02002910 unsigned long value;
2911
Hollis Blanchard8776e512007-10-31 17:24:24 -05002912 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2913 switch (cr) {
2914 case 0:
Joerg Roedel54e445c2008-04-30 17:56:02 +02002915 value = vcpu->arch.cr0;
2916 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002917 case 2:
Joerg Roedel54e445c2008-04-30 17:56:02 +02002918 value = vcpu->arch.cr2;
2919 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002920 case 3:
Joerg Roedel54e445c2008-04-30 17:56:02 +02002921 value = vcpu->arch.cr3;
2922 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002923 case 4:
Joerg Roedel54e445c2008-04-30 17:56:02 +02002924 value = vcpu->arch.cr4;
2925 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01002926 case 8:
Joerg Roedel54e445c2008-04-30 17:56:02 +02002927 value = kvm_get_cr8(vcpu);
2928 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002929 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002930 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002931 return 0;
2932 }
Joerg Roedel54e445c2008-04-30 17:56:02 +02002933 KVMTRACE_3D(CR_READ, vcpu, (u32)cr, (u32)value,
2934 (u32)((u64)value >> 32), handler);
2935
2936 return value;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002937}
2938
2939void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
2940 unsigned long *rflags)
2941{
Joerg Roedel54e445c2008-04-30 17:56:02 +02002942 KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr, (u32)val,
2943 (u32)((u64)val >> 32), handler);
2944
Hollis Blanchard8776e512007-10-31 17:24:24 -05002945 switch (cr) {
2946 case 0:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002947 kvm_set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val));
Hollis Blanchard8776e512007-10-31 17:24:24 -05002948 *rflags = kvm_x86_ops->get_rflags(vcpu);
2949 break;
2950 case 2:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002951 vcpu->arch.cr2 = val;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002952 break;
2953 case 3:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002954 kvm_set_cr3(vcpu, val);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002955 break;
2956 case 4:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002957 kvm_set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val));
Hollis Blanchard8776e512007-10-31 17:24:24 -05002958 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01002959 case 8:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002960 kvm_set_cr8(vcpu, val & 0xfUL);
Joerg Roedel152ff9b2007-12-06 15:46:52 +01002961 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002962 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002963 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002964 }
2965}
2966
Dan Kenigsberg07716712007-11-21 17:10:04 +02002967static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
2968{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002969 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
2970 int j, nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02002971
2972 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
2973 /* when no next entry is found, the current entry[i] is reselected */
2974 for (j = i + 1; j == i; j = (j + 1) % nent) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002975 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
Dan Kenigsberg07716712007-11-21 17:10:04 +02002976 if (ej->function == e->function) {
2977 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
2978 return j;
2979 }
2980 }
2981 return 0; /* silence gcc, even though control never reaches here */
2982}
2983
2984/* find an entry with matching function, matching index (if needed), and that
2985 * should be read next (if it's stateful) */
2986static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
2987 u32 function, u32 index)
2988{
2989 if (e->function != function)
2990 return 0;
2991 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
2992 return 0;
2993 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
2994 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
2995 return 0;
2996 return 1;
2997}
2998
Hollis Blanchard8776e512007-10-31 17:24:24 -05002999void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
3000{
3001 int i;
Dan Kenigsberg07716712007-11-21 17:10:04 +02003002 u32 function, index;
3003 struct kvm_cpuid_entry2 *e, *best;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003004
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003005 function = kvm_register_read(vcpu, VCPU_REGS_RAX);
3006 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
3007 kvm_register_write(vcpu, VCPU_REGS_RAX, 0);
3008 kvm_register_write(vcpu, VCPU_REGS_RBX, 0);
3009 kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
3010 kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003011 best = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003012 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
3013 e = &vcpu->arch.cpuid_entries[i];
Dan Kenigsberg07716712007-11-21 17:10:04 +02003014 if (is_matching_cpuid_entry(e, function, index)) {
3015 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
3016 move_to_next_stateful_cpuid_entry(vcpu, i);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003017 best = e;
3018 break;
3019 }
3020 /*
3021 * Both basic or both extended?
3022 */
3023 if (((e->function ^ function) & 0x80000000) == 0)
3024 if (!best || e->function > best->function)
3025 best = e;
3026 }
3027 if (best) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003028 kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
3029 kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
3030 kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
3031 kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003032 }
Hollis Blanchard8776e512007-10-31 17:24:24 -05003033 kvm_x86_ops->skip_emulated_instruction(vcpu);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003034 KVMTRACE_5D(CPUID, vcpu, function,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003035 (u32)kvm_register_read(vcpu, VCPU_REGS_RAX),
3036 (u32)kvm_register_read(vcpu, VCPU_REGS_RBX),
3037 (u32)kvm_register_read(vcpu, VCPU_REGS_RCX),
3038 (u32)kvm_register_read(vcpu, VCPU_REGS_RDX), handler);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003039}
3040EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
Hollis Blanchardd0752062007-10-31 17:24:25 -05003041
3042/*
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003043 * Check if userspace requested an interrupt window, and that the
3044 * interrupt window is open.
3045 *
3046 * No need to exit to userspace if we already have an interrupt queued.
3047 */
3048static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
3049 struct kvm_run *kvm_run)
3050{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003051 return (!vcpu->arch.irq_summary &&
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003052 kvm_run->request_interrupt_window &&
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003053 vcpu->arch.interrupt_window_open &&
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003054 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
3055}
3056
3057static void post_kvm_run_save(struct kvm_vcpu *vcpu,
3058 struct kvm_run *kvm_run)
3059{
3060 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003061 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003062 kvm_run->apic_base = kvm_get_apic_base(vcpu);
3063 if (irqchip_in_kernel(vcpu->kvm))
3064 kvm_run->ready_for_interrupt_injection = 1;
3065 else
3066 kvm_run->ready_for_interrupt_injection =
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003067 (vcpu->arch.interrupt_window_open &&
3068 vcpu->arch.irq_summary == 0);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003069}
3070
Avi Kivityb93463a2007-10-25 16:52:32 +02003071static void vapic_enter(struct kvm_vcpu *vcpu)
3072{
3073 struct kvm_lapic *apic = vcpu->arch.apic;
3074 struct page *page;
3075
3076 if (!apic || !apic->vapic_addr)
3077 return;
3078
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003079 down_read(&current->mm->mmap_sem);
Avi Kivityb93463a2007-10-25 16:52:32 +02003080 page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003081 up_read(&current->mm->mmap_sem);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003082
3083 vcpu->arch.apic->vapic_page = page;
Avi Kivityb93463a2007-10-25 16:52:32 +02003084}
3085
3086static void vapic_exit(struct kvm_vcpu *vcpu)
3087{
3088 struct kvm_lapic *apic = vcpu->arch.apic;
3089
3090 if (!apic || !apic->vapic_addr)
3091 return;
3092
Marcelo Tosattif8b78fa2008-06-23 12:04:25 -03003093 down_read(&vcpu->kvm->slots_lock);
Avi Kivityb93463a2007-10-25 16:52:32 +02003094 kvm_release_page_dirty(apic->vapic_page);
3095 mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Marcelo Tosattif8b78fa2008-06-23 12:04:25 -03003096 up_read(&vcpu->kvm->slots_lock);
Avi Kivityb93463a2007-10-25 16:52:32 +02003097}
3098
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003099static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3100{
3101 int r;
3102
Avi Kivitya4535292008-04-13 17:54:35 +03003103 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003104 pr_debug("vcpu %d received sipi with vector # %x\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003105 vcpu->vcpu_id, vcpu->arch.sipi_vector);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003106 kvm_lapic_reset(vcpu);
3107 r = kvm_x86_ops->vcpu_reset(vcpu);
3108 if (r)
3109 return r;
Avi Kivitya4535292008-04-13 17:54:35 +03003110 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003111 }
3112
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003113 down_read(&vcpu->kvm->slots_lock);
Avi Kivityb93463a2007-10-25 16:52:32 +02003114 vapic_enter(vcpu);
3115
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003116again:
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05003117 if (vcpu->requests)
3118 if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
3119 kvm_mmu_unload(vcpu);
3120
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003121 r = kvm_mmu_reload(vcpu);
3122 if (unlikely(r))
3123 goto out;
3124
Avi Kivity2f52d582008-01-16 12:49:30 +02003125 if (vcpu->requests) {
3126 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03003127 __kvm_migrate_timers(vcpu);
Marcelo Tosattid4acf7e2008-06-06 16:37:35 -03003128 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
3129 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02003130 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
3131 &vcpu->requests)) {
3132 kvm_run->exit_reason = KVM_EXIT_TPR_ACCESS;
3133 r = 0;
3134 goto out;
3135 }
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01003136 if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) {
3137 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
3138 r = 0;
3139 goto out;
3140 }
Avi Kivity2f52d582008-01-16 12:49:30 +02003141 }
Avi Kivityb93463a2007-10-25 16:52:32 +02003142
Marcelo Tosatti06e05642008-06-06 16:37:36 -03003143 clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003144 kvm_inject_pending_timer_irqs(vcpu);
3145
3146 preempt_disable();
3147
3148 kvm_x86_ops->prepare_guest_switch(vcpu);
3149 kvm_load_guest_fpu(vcpu);
3150
3151 local_irq_disable();
3152
Marcelo Tosattid4acf7e2008-06-06 16:37:35 -03003153 if (vcpu->requests || need_resched()) {
Avi Kivity6c142802008-01-15 18:27:32 +02003154 local_irq_enable();
3155 preempt_enable();
3156 r = 1;
3157 goto out;
3158 }
3159
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003160 if (signal_pending(current)) {
3161 local_irq_enable();
3162 preempt_enable();
3163 r = -EINTR;
3164 kvm_run->exit_reason = KVM_EXIT_INTR;
3165 ++vcpu->stat.signal_exits;
3166 goto out;
3167 }
3168
Marcelo Tosatti29415c32008-08-01 20:09:13 -03003169 if (vcpu->guest_debug.enabled)
3170 kvm_x86_ops->guest_debug_pre(vcpu);
3171
Marcelo Tosattie9571ed2008-04-11 15:01:22 -03003172 vcpu->guest_mode = 1;
3173 /*
3174 * Make sure that guest_mode assignment won't happen after
3175 * testing the pending IRQ vector bitmap.
3176 */
3177 smp_wmb();
3178
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003179 if (vcpu->arch.exception.pending)
Avi Kivity298101d2007-11-25 13:41:11 +02003180 __queue_exception(vcpu);
3181 else if (irqchip_in_kernel(vcpu->kvm))
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003182 kvm_x86_ops->inject_pending_irq(vcpu);
Avi Kivityeb9774f2007-11-25 17:45:31 +02003183 else
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003184 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
3185
Avi Kivityb93463a2007-10-25 16:52:32 +02003186 kvm_lapic_sync_to_vapic(vcpu);
3187
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003188 up_read(&vcpu->kvm->slots_lock);
3189
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003190 kvm_guest_enter();
3191
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003192
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003193 KVMTRACE_0D(VMENTRY, vcpu, entryexit);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003194 kvm_x86_ops->run(vcpu, kvm_run);
3195
3196 vcpu->guest_mode = 0;
3197 local_irq_enable();
3198
3199 ++vcpu->stat.exits;
3200
3201 /*
3202 * We must have an instruction between local_irq_enable() and
3203 * kvm_guest_exit(), so the timer interrupt isn't delayed by
3204 * the interrupt shadow. The stat.exits increment will do nicely.
3205 * But we need to prevent reordering, hence this barrier():
3206 */
3207 barrier();
3208
3209 kvm_guest_exit();
3210
3211 preempt_enable();
3212
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003213 down_read(&vcpu->kvm->slots_lock);
3214
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003215 /*
3216 * Profile KVM exit RIPs:
3217 */
3218 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003219 unsigned long rip = kvm_rip_read(vcpu);
3220 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003221 }
3222
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003223 if (vcpu->arch.exception.pending && kvm_x86_ops->exception_injected(vcpu))
3224 vcpu->arch.exception.pending = false;
Avi Kivity298101d2007-11-25 13:41:11 +02003225
Avi Kivityb93463a2007-10-25 16:52:32 +02003226 kvm_lapic_sync_from_vapic(vcpu);
3227
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003228 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
3229
3230 if (r > 0) {
3231 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
3232 r = -EINTR;
3233 kvm_run->exit_reason = KVM_EXIT_INTR;
3234 ++vcpu->stat.request_irq_exits;
3235 goto out;
3236 }
Avi Kivitye1beb1d2007-11-18 13:50:24 +02003237 if (!need_resched())
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003238 goto again;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003239 }
3240
3241out:
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003242 up_read(&vcpu->kvm->slots_lock);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003243 if (r > 0) {
3244 kvm_resched(vcpu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003245 down_read(&vcpu->kvm->slots_lock);
Marcelo Tosatti29415c32008-08-01 20:09:13 -03003246 goto again;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003247 }
3248
3249 post_kvm_run_save(vcpu, kvm_run);
3250
Avi Kivityb93463a2007-10-25 16:52:32 +02003251 vapic_exit(vcpu);
3252
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003253 return r;
3254}
3255
3256int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3257{
3258 int r;
3259 sigset_t sigsaved;
3260
3261 vcpu_load(vcpu);
3262
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003263 if (vcpu->sigset_active)
3264 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
3265
Avi Kivityac9f6dc2008-07-06 15:48:31 +03003266 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
3267 kvm_vcpu_block(vcpu);
3268 r = -EAGAIN;
3269 goto out;
3270 }
3271
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003272 /* re-sync apic's tpr */
3273 if (!irqchip_in_kernel(vcpu->kvm))
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003274 kvm_set_cr8(vcpu, kvm_run->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003275
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003276 if (vcpu->arch.pio.cur_count) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003277 r = complete_pio(vcpu);
3278 if (r)
3279 goto out;
3280 }
3281#if CONFIG_HAS_IOMEM
3282 if (vcpu->mmio_needed) {
3283 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
3284 vcpu->mmio_read_completed = 1;
3285 vcpu->mmio_needed = 0;
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003286
3287 down_read(&vcpu->kvm->slots_lock);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003288 r = emulate_instruction(vcpu, kvm_run,
Sheng Yang571008d2008-01-02 14:49:22 +08003289 vcpu->arch.mmio_fault_cr2, 0,
3290 EMULTYPE_NO_DECODE);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003291 up_read(&vcpu->kvm->slots_lock);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003292 if (r == EMULATE_DO_MMIO) {
3293 /*
3294 * Read-modify-write. Back to userspace.
3295 */
3296 r = 0;
3297 goto out;
3298 }
3299 }
3300#endif
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003301 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL)
3302 kvm_register_write(vcpu, VCPU_REGS_RAX,
3303 kvm_run->hypercall.ret);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003304
3305 r = __vcpu_run(vcpu, kvm_run);
3306
3307out:
3308 if (vcpu->sigset_active)
3309 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
3310
3311 vcpu_put(vcpu);
3312 return r;
3313}
3314
3315int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3316{
3317 vcpu_load(vcpu);
3318
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003319 regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
3320 regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
3321 regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
3322 regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
3323 regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
3324 regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
3325 regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
3326 regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003327#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003328 regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
3329 regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
3330 regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
3331 regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
3332 regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
3333 regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
3334 regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
3335 regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003336#endif
3337
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003338 regs->rip = kvm_rip_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003339 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
3340
3341 /*
3342 * Don't leak debug flags in case they were set for guest debugging
3343 */
3344 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
3345 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
3346
3347 vcpu_put(vcpu);
3348
3349 return 0;
3350}
3351
3352int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3353{
3354 vcpu_load(vcpu);
3355
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003356 kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
3357 kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
3358 kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
3359 kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
3360 kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
3361 kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
3362 kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
3363 kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003364#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003365 kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
3366 kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
3367 kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
3368 kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
3369 kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
3370 kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
3371 kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
3372 kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
3373
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003374#endif
3375
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003376 kvm_rip_write(vcpu, regs->rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003377 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
3378
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003379
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02003380 vcpu->arch.exception.pending = false;
3381
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003382 vcpu_put(vcpu);
3383
3384 return 0;
3385}
3386
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003387void kvm_get_segment(struct kvm_vcpu *vcpu,
3388 struct kvm_segment *var, int seg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003389{
Harvey Harrison14af3f32008-02-19 10:25:50 -08003390 kvm_x86_ops->get_segment(vcpu, var, seg);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003391}
3392
3393void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3394{
3395 struct kvm_segment cs;
3396
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003397 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003398 *db = cs.db;
3399 *l = cs.l;
3400}
3401EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
3402
3403int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
3404 struct kvm_sregs *sregs)
3405{
3406 struct descriptor_table dt;
3407 int pending_vec;
3408
3409 vcpu_load(vcpu);
3410
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003411 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
3412 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
3413 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
3414 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
3415 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
3416 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003417
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003418 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
3419 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003420
3421 kvm_x86_ops->get_idt(vcpu, &dt);
3422 sregs->idt.limit = dt.limit;
3423 sregs->idt.base = dt.base;
3424 kvm_x86_ops->get_gdt(vcpu, &dt);
3425 sregs->gdt.limit = dt.limit;
3426 sregs->gdt.base = dt.base;
3427
3428 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003429 sregs->cr0 = vcpu->arch.cr0;
3430 sregs->cr2 = vcpu->arch.cr2;
3431 sregs->cr3 = vcpu->arch.cr3;
3432 sregs->cr4 = vcpu->arch.cr4;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003433 sregs->cr8 = kvm_get_cr8(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003434 sregs->efer = vcpu->arch.shadow_efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003435 sregs->apic_base = kvm_get_apic_base(vcpu);
3436
3437 if (irqchip_in_kernel(vcpu->kvm)) {
3438 memset(sregs->interrupt_bitmap, 0,
3439 sizeof sregs->interrupt_bitmap);
3440 pending_vec = kvm_x86_ops->get_irq(vcpu);
3441 if (pending_vec >= 0)
3442 set_bit(pending_vec,
3443 (unsigned long *)sregs->interrupt_bitmap);
3444 } else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003445 memcpy(sregs->interrupt_bitmap, vcpu->arch.irq_pending,
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003446 sizeof sregs->interrupt_bitmap);
3447
3448 vcpu_put(vcpu);
3449
3450 return 0;
3451}
3452
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003453int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
3454 struct kvm_mp_state *mp_state)
3455{
3456 vcpu_load(vcpu);
3457 mp_state->mp_state = vcpu->arch.mp_state;
3458 vcpu_put(vcpu);
3459 return 0;
3460}
3461
3462int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
3463 struct kvm_mp_state *mp_state)
3464{
3465 vcpu_load(vcpu);
3466 vcpu->arch.mp_state = mp_state->mp_state;
3467 vcpu_put(vcpu);
3468 return 0;
3469}
3470
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003471static void kvm_set_segment(struct kvm_vcpu *vcpu,
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003472 struct kvm_segment *var, int seg)
3473{
Harvey Harrison14af3f32008-02-19 10:25:50 -08003474 kvm_x86_ops->set_segment(vcpu, var, seg);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003475}
3476
Izik Eidus37817f22008-03-24 23:14:53 +02003477static void seg_desct_to_kvm_desct(struct desc_struct *seg_desc, u16 selector,
3478 struct kvm_segment *kvm_desct)
3479{
3480 kvm_desct->base = seg_desc->base0;
3481 kvm_desct->base |= seg_desc->base1 << 16;
3482 kvm_desct->base |= seg_desc->base2 << 24;
3483 kvm_desct->limit = seg_desc->limit0;
3484 kvm_desct->limit |= seg_desc->limit << 16;
Marcelo Tosattic93cd3a2008-07-19 19:08:07 -03003485 if (seg_desc->g) {
3486 kvm_desct->limit <<= 12;
3487 kvm_desct->limit |= 0xfff;
3488 }
Izik Eidus37817f22008-03-24 23:14:53 +02003489 kvm_desct->selector = selector;
3490 kvm_desct->type = seg_desc->type;
3491 kvm_desct->present = seg_desc->p;
3492 kvm_desct->dpl = seg_desc->dpl;
3493 kvm_desct->db = seg_desc->d;
3494 kvm_desct->s = seg_desc->s;
3495 kvm_desct->l = seg_desc->l;
3496 kvm_desct->g = seg_desc->g;
3497 kvm_desct->avl = seg_desc->avl;
3498 if (!selector)
3499 kvm_desct->unusable = 1;
3500 else
3501 kvm_desct->unusable = 0;
3502 kvm_desct->padding = 0;
3503}
3504
3505static void get_segment_descritptor_dtable(struct kvm_vcpu *vcpu,
3506 u16 selector,
3507 struct descriptor_table *dtable)
3508{
3509 if (selector & 1 << 2) {
3510 struct kvm_segment kvm_seg;
3511
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003512 kvm_get_segment(vcpu, &kvm_seg, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02003513
3514 if (kvm_seg.unusable)
3515 dtable->limit = 0;
3516 else
3517 dtable->limit = kvm_seg.limit;
3518 dtable->base = kvm_seg.base;
3519 }
3520 else
3521 kvm_x86_ops->get_gdt(vcpu, dtable);
3522}
3523
3524/* allowed just for 8 bytes segments */
3525static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
3526 struct desc_struct *seg_desc)
3527{
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03003528 gpa_t gpa;
Izik Eidus37817f22008-03-24 23:14:53 +02003529 struct descriptor_table dtable;
3530 u16 index = selector >> 3;
3531
3532 get_segment_descritptor_dtable(vcpu, selector, &dtable);
3533
3534 if (dtable.limit < index * 8 + 7) {
3535 kvm_queue_exception_e(vcpu, GP_VECTOR, selector & 0xfffc);
3536 return 1;
3537 }
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03003538 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, dtable.base);
3539 gpa += index * 8;
3540 return kvm_read_guest(vcpu->kvm, gpa, seg_desc, 8);
Izik Eidus37817f22008-03-24 23:14:53 +02003541}
3542
3543/* allowed just for 8 bytes segments */
3544static int save_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
3545 struct desc_struct *seg_desc)
3546{
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03003547 gpa_t gpa;
Izik Eidus37817f22008-03-24 23:14:53 +02003548 struct descriptor_table dtable;
3549 u16 index = selector >> 3;
3550
3551 get_segment_descritptor_dtable(vcpu, selector, &dtable);
3552
3553 if (dtable.limit < index * 8 + 7)
3554 return 1;
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03003555 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, dtable.base);
3556 gpa += index * 8;
3557 return kvm_write_guest(vcpu->kvm, gpa, seg_desc, 8);
Izik Eidus37817f22008-03-24 23:14:53 +02003558}
3559
3560static u32 get_tss_base_addr(struct kvm_vcpu *vcpu,
3561 struct desc_struct *seg_desc)
3562{
3563 u32 base_addr;
3564
3565 base_addr = seg_desc->base0;
3566 base_addr |= (seg_desc->base1 << 16);
3567 base_addr |= (seg_desc->base2 << 24);
3568
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03003569 return vcpu->arch.mmu.gva_to_gpa(vcpu, base_addr);
Izik Eidus37817f22008-03-24 23:14:53 +02003570}
3571
Izik Eidus37817f22008-03-24 23:14:53 +02003572static u16 get_segment_selector(struct kvm_vcpu *vcpu, int seg)
3573{
3574 struct kvm_segment kvm_seg;
3575
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003576 kvm_get_segment(vcpu, &kvm_seg, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02003577 return kvm_seg.selector;
3578}
3579
3580static int load_segment_descriptor_to_kvm_desct(struct kvm_vcpu *vcpu,
3581 u16 selector,
3582 struct kvm_segment *kvm_seg)
3583{
3584 struct desc_struct seg_desc;
3585
3586 if (load_guest_segment_descriptor(vcpu, selector, &seg_desc))
3587 return 1;
3588 seg_desct_to_kvm_desct(&seg_desc, selector, kvm_seg);
3589 return 0;
3590}
3591
Avi Kivityf4bbd9a2008-08-20 15:51:42 +03003592int kvm_load_realmode_segment(struct kvm_vcpu *vcpu, u16 selector, int seg)
3593{
3594 struct kvm_segment segvar = {
3595 .base = selector << 4,
3596 .limit = 0xffff,
3597 .selector = selector,
3598 .type = 3,
3599 .present = 1,
3600 .dpl = 3,
3601 .db = 0,
3602 .s = 1,
3603 .l = 0,
3604 .g = 0,
3605 .avl = 0,
3606 .unusable = 0,
3607 };
3608 kvm_x86_ops->set_segment(vcpu, &segvar, seg);
3609 return 0;
3610}
3611
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003612int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
3613 int type_bits, int seg)
Izik Eidus37817f22008-03-24 23:14:53 +02003614{
3615 struct kvm_segment kvm_seg;
3616
Avi Kivityf4bbd9a2008-08-20 15:51:42 +03003617 if (!(vcpu->arch.cr0 & X86_CR0_PE))
3618 return kvm_load_realmode_segment(vcpu, selector, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02003619 if (load_segment_descriptor_to_kvm_desct(vcpu, selector, &kvm_seg))
3620 return 1;
3621 kvm_seg.type |= type_bits;
3622
3623 if (seg != VCPU_SREG_SS && seg != VCPU_SREG_CS &&
3624 seg != VCPU_SREG_LDTR)
3625 if (!kvm_seg.s)
3626 kvm_seg.unusable = 1;
3627
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003628 kvm_set_segment(vcpu, &kvm_seg, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02003629 return 0;
3630}
3631
3632static void save_state_to_tss32(struct kvm_vcpu *vcpu,
3633 struct tss_segment_32 *tss)
3634{
3635 tss->cr3 = vcpu->arch.cr3;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003636 tss->eip = kvm_rip_read(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003637 tss->eflags = kvm_x86_ops->get_rflags(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003638 tss->eax = kvm_register_read(vcpu, VCPU_REGS_RAX);
3639 tss->ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
3640 tss->edx = kvm_register_read(vcpu, VCPU_REGS_RDX);
3641 tss->ebx = kvm_register_read(vcpu, VCPU_REGS_RBX);
3642 tss->esp = kvm_register_read(vcpu, VCPU_REGS_RSP);
3643 tss->ebp = kvm_register_read(vcpu, VCPU_REGS_RBP);
3644 tss->esi = kvm_register_read(vcpu, VCPU_REGS_RSI);
3645 tss->edi = kvm_register_read(vcpu, VCPU_REGS_RDI);
Izik Eidus37817f22008-03-24 23:14:53 +02003646 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
3647 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
3648 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
3649 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
3650 tss->fs = get_segment_selector(vcpu, VCPU_SREG_FS);
3651 tss->gs = get_segment_selector(vcpu, VCPU_SREG_GS);
3652 tss->ldt_selector = get_segment_selector(vcpu, VCPU_SREG_LDTR);
3653 tss->prev_task_link = get_segment_selector(vcpu, VCPU_SREG_TR);
3654}
3655
3656static int load_state_from_tss32(struct kvm_vcpu *vcpu,
3657 struct tss_segment_32 *tss)
3658{
3659 kvm_set_cr3(vcpu, tss->cr3);
3660
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003661 kvm_rip_write(vcpu, tss->eip);
Izik Eidus37817f22008-03-24 23:14:53 +02003662 kvm_x86_ops->set_rflags(vcpu, tss->eflags | 2);
3663
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003664 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->eax);
3665 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->ecx);
3666 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->edx);
3667 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->ebx);
3668 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->esp);
3669 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->ebp);
3670 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->esi);
3671 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->edi);
Izik Eidus37817f22008-03-24 23:14:53 +02003672
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003673 if (kvm_load_segment_descriptor(vcpu, tss->ldt_selector, 0, VCPU_SREG_LDTR))
Izik Eidus37817f22008-03-24 23:14:53 +02003674 return 1;
3675
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003676 if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
Izik Eidus37817f22008-03-24 23:14:53 +02003677 return 1;
3678
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003679 if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
Izik Eidus37817f22008-03-24 23:14:53 +02003680 return 1;
3681
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003682 if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
Izik Eidus37817f22008-03-24 23:14:53 +02003683 return 1;
3684
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003685 if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
Izik Eidus37817f22008-03-24 23:14:53 +02003686 return 1;
3687
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003688 if (kvm_load_segment_descriptor(vcpu, tss->fs, 1, VCPU_SREG_FS))
Izik Eidus37817f22008-03-24 23:14:53 +02003689 return 1;
3690
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003691 if (kvm_load_segment_descriptor(vcpu, tss->gs, 1, VCPU_SREG_GS))
Izik Eidus37817f22008-03-24 23:14:53 +02003692 return 1;
3693 return 0;
3694}
3695
3696static void save_state_to_tss16(struct kvm_vcpu *vcpu,
3697 struct tss_segment_16 *tss)
3698{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003699 tss->ip = kvm_rip_read(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003700 tss->flag = kvm_x86_ops->get_rflags(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003701 tss->ax = kvm_register_read(vcpu, VCPU_REGS_RAX);
3702 tss->cx = kvm_register_read(vcpu, VCPU_REGS_RCX);
3703 tss->dx = kvm_register_read(vcpu, VCPU_REGS_RDX);
3704 tss->bx = kvm_register_read(vcpu, VCPU_REGS_RBX);
3705 tss->sp = kvm_register_read(vcpu, VCPU_REGS_RSP);
3706 tss->bp = kvm_register_read(vcpu, VCPU_REGS_RBP);
3707 tss->si = kvm_register_read(vcpu, VCPU_REGS_RSI);
3708 tss->di = kvm_register_read(vcpu, VCPU_REGS_RDI);
Izik Eidus37817f22008-03-24 23:14:53 +02003709
3710 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
3711 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
3712 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
3713 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
3714 tss->ldt = get_segment_selector(vcpu, VCPU_SREG_LDTR);
3715 tss->prev_task_link = get_segment_selector(vcpu, VCPU_SREG_TR);
3716}
3717
3718static int load_state_from_tss16(struct kvm_vcpu *vcpu,
3719 struct tss_segment_16 *tss)
3720{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003721 kvm_rip_write(vcpu, tss->ip);
Izik Eidus37817f22008-03-24 23:14:53 +02003722 kvm_x86_ops->set_rflags(vcpu, tss->flag | 2);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003723 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->ax);
3724 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->cx);
3725 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->dx);
3726 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->bx);
3727 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->sp);
3728 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->bp);
3729 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->si);
3730 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->di);
Izik Eidus37817f22008-03-24 23:14:53 +02003731
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003732 if (kvm_load_segment_descriptor(vcpu, tss->ldt, 0, VCPU_SREG_LDTR))
Izik Eidus37817f22008-03-24 23:14:53 +02003733 return 1;
3734
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003735 if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
Izik Eidus37817f22008-03-24 23:14:53 +02003736 return 1;
3737
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003738 if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
Izik Eidus37817f22008-03-24 23:14:53 +02003739 return 1;
3740
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003741 if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
Izik Eidus37817f22008-03-24 23:14:53 +02003742 return 1;
3743
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003744 if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
Izik Eidus37817f22008-03-24 23:14:53 +02003745 return 1;
3746 return 0;
3747}
3748
Harvey Harrison8b2cf732008-04-27 12:14:13 -07003749static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector,
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003750 u32 old_tss_base,
Izik Eidus37817f22008-03-24 23:14:53 +02003751 struct desc_struct *nseg_desc)
3752{
3753 struct tss_segment_16 tss_segment_16;
3754 int ret = 0;
3755
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003756 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
3757 sizeof tss_segment_16))
Izik Eidus37817f22008-03-24 23:14:53 +02003758 goto out;
3759
3760 save_state_to_tss16(vcpu, &tss_segment_16);
Izik Eidus37817f22008-03-24 23:14:53 +02003761
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003762 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
3763 sizeof tss_segment_16))
Izik Eidus37817f22008-03-24 23:14:53 +02003764 goto out;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003765
3766 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
3767 &tss_segment_16, sizeof tss_segment_16))
3768 goto out;
3769
Izik Eidus37817f22008-03-24 23:14:53 +02003770 if (load_state_from_tss16(vcpu, &tss_segment_16))
3771 goto out;
3772
3773 ret = 1;
3774out:
3775 return ret;
3776}
3777
Harvey Harrison8b2cf732008-04-27 12:14:13 -07003778static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector,
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003779 u32 old_tss_base,
Izik Eidus37817f22008-03-24 23:14:53 +02003780 struct desc_struct *nseg_desc)
3781{
3782 struct tss_segment_32 tss_segment_32;
3783 int ret = 0;
3784
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003785 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
3786 sizeof tss_segment_32))
Izik Eidus37817f22008-03-24 23:14:53 +02003787 goto out;
3788
3789 save_state_to_tss32(vcpu, &tss_segment_32);
Izik Eidus37817f22008-03-24 23:14:53 +02003790
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003791 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
3792 sizeof tss_segment_32))
Izik Eidus37817f22008-03-24 23:14:53 +02003793 goto out;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003794
3795 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
3796 &tss_segment_32, sizeof tss_segment_32))
3797 goto out;
3798
Izik Eidus37817f22008-03-24 23:14:53 +02003799 if (load_state_from_tss32(vcpu, &tss_segment_32))
3800 goto out;
3801
3802 ret = 1;
3803out:
3804 return ret;
3805}
3806
3807int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason)
3808{
3809 struct kvm_segment tr_seg;
3810 struct desc_struct cseg_desc;
3811 struct desc_struct nseg_desc;
3812 int ret = 0;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003813 u32 old_tss_base = get_segment_base(vcpu, VCPU_SREG_TR);
3814 u16 old_tss_sel = get_segment_selector(vcpu, VCPU_SREG_TR);
Izik Eidus37817f22008-03-24 23:14:53 +02003815
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003816 old_tss_base = vcpu->arch.mmu.gva_to_gpa(vcpu, old_tss_base);
Izik Eidus37817f22008-03-24 23:14:53 +02003817
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003818 /* FIXME: Handle errors. Failure to read either TSS or their
3819 * descriptors should generate a pagefault.
3820 */
Izik Eidus37817f22008-03-24 23:14:53 +02003821 if (load_guest_segment_descriptor(vcpu, tss_selector, &nseg_desc))
3822 goto out;
3823
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003824 if (load_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc))
Izik Eidus37817f22008-03-24 23:14:53 +02003825 goto out;
3826
Izik Eidus37817f22008-03-24 23:14:53 +02003827 if (reason != TASK_SWITCH_IRET) {
3828 int cpl;
3829
3830 cpl = kvm_x86_ops->get_cpl(vcpu);
3831 if ((tss_selector & 3) > nseg_desc.dpl || cpl > nseg_desc.dpl) {
3832 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
3833 return 1;
3834 }
3835 }
3836
3837 if (!nseg_desc.p || (nseg_desc.limit0 | nseg_desc.limit << 16) < 0x67) {
3838 kvm_queue_exception_e(vcpu, TS_VECTOR, tss_selector & 0xfffc);
3839 return 1;
3840 }
3841
3842 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
Izik Eidus3fe913e2008-04-28 18:23:52 +03003843 cseg_desc.type &= ~(1 << 1); //clear the B flag
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003844 save_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02003845 }
3846
3847 if (reason == TASK_SWITCH_IRET) {
3848 u32 eflags = kvm_x86_ops->get_rflags(vcpu);
3849 kvm_x86_ops->set_rflags(vcpu, eflags & ~X86_EFLAGS_NT);
3850 }
3851
3852 kvm_x86_ops->skip_emulated_instruction(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003853
3854 if (nseg_desc.type & 8)
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003855 ret = kvm_task_switch_32(vcpu, tss_selector, old_tss_base,
Izik Eidus37817f22008-03-24 23:14:53 +02003856 &nseg_desc);
3857 else
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003858 ret = kvm_task_switch_16(vcpu, tss_selector, old_tss_base,
Izik Eidus37817f22008-03-24 23:14:53 +02003859 &nseg_desc);
3860
3861 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) {
3862 u32 eflags = kvm_x86_ops->get_rflags(vcpu);
3863 kvm_x86_ops->set_rflags(vcpu, eflags | X86_EFLAGS_NT);
3864 }
3865
3866 if (reason != TASK_SWITCH_IRET) {
Izik Eidus3fe913e2008-04-28 18:23:52 +03003867 nseg_desc.type |= (1 << 1);
Izik Eidus37817f22008-03-24 23:14:53 +02003868 save_guest_segment_descriptor(vcpu, tss_selector,
3869 &nseg_desc);
3870 }
3871
3872 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 | X86_CR0_TS);
3873 seg_desct_to_kvm_desct(&nseg_desc, tss_selector, &tr_seg);
3874 tr_seg.type = 11;
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003875 kvm_set_segment(vcpu, &tr_seg, VCPU_SREG_TR);
Izik Eidus37817f22008-03-24 23:14:53 +02003876out:
Izik Eidus37817f22008-03-24 23:14:53 +02003877 return ret;
3878}
3879EXPORT_SYMBOL_GPL(kvm_task_switch);
3880
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003881int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
3882 struct kvm_sregs *sregs)
3883{
3884 int mmu_reset_needed = 0;
3885 int i, pending_vec, max_bits;
3886 struct descriptor_table dt;
3887
3888 vcpu_load(vcpu);
3889
3890 dt.limit = sregs->idt.limit;
3891 dt.base = sregs->idt.base;
3892 kvm_x86_ops->set_idt(vcpu, &dt);
3893 dt.limit = sregs->gdt.limit;
3894 dt.base = sregs->gdt.base;
3895 kvm_x86_ops->set_gdt(vcpu, &dt);
3896
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003897 vcpu->arch.cr2 = sregs->cr2;
3898 mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
3899 vcpu->arch.cr3 = sregs->cr3;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003900
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003901 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003902
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003903 mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003904 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003905 kvm_set_apic_base(vcpu, sregs->apic_base);
3906
3907 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
3908
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003909 mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003910 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00003911 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003912
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003913 mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003914 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
3915 if (!is_long_mode(vcpu) && is_pae(vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003916 load_pdptrs(vcpu, vcpu->arch.cr3);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003917
3918 if (mmu_reset_needed)
3919 kvm_mmu_reset_context(vcpu);
3920
3921 if (!irqchip_in_kernel(vcpu->kvm)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003922 memcpy(vcpu->arch.irq_pending, sregs->interrupt_bitmap,
3923 sizeof vcpu->arch.irq_pending);
3924 vcpu->arch.irq_summary = 0;
3925 for (i = 0; i < ARRAY_SIZE(vcpu->arch.irq_pending); ++i)
3926 if (vcpu->arch.irq_pending[i])
3927 __set_bit(i, &vcpu->arch.irq_summary);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003928 } else {
3929 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
3930 pending_vec = find_first_bit(
3931 (const unsigned long *)sregs->interrupt_bitmap,
3932 max_bits);
3933 /* Only pending external irq is handled here */
3934 if (pending_vec < max_bits) {
3935 kvm_x86_ops->set_irq(vcpu, pending_vec);
3936 pr_debug("Set back pending irq %d\n",
3937 pending_vec);
3938 }
3939 }
3940
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003941 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
3942 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
3943 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
3944 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
3945 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
3946 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003947
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003948 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
3949 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003950
3951 vcpu_put(vcpu);
3952
3953 return 0;
3954}
3955
3956int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
3957 struct kvm_debug_guest *dbg)
3958{
3959 int r;
3960
3961 vcpu_load(vcpu);
3962
3963 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
3964
3965 vcpu_put(vcpu);
3966
3967 return r;
3968}
3969
3970/*
Hollis Blanchardd0752062007-10-31 17:24:25 -05003971 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
3972 * we have asm/x86/processor.h
3973 */
3974struct fxsave {
3975 u16 cwd;
3976 u16 swd;
3977 u16 twd;
3978 u16 fop;
3979 u64 rip;
3980 u64 rdp;
3981 u32 mxcsr;
3982 u32 mxcsr_mask;
3983 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
3984#ifdef CONFIG_X86_64
3985 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
3986#else
3987 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
3988#endif
3989};
3990
Zhang Xiantao8b006792007-11-16 13:05:55 +08003991/*
3992 * Translate a guest virtual address to a guest physical address.
3993 */
3994int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
3995 struct kvm_translation *tr)
3996{
3997 unsigned long vaddr = tr->linear_address;
3998 gpa_t gpa;
3999
4000 vcpu_load(vcpu);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004001 down_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004002 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004003 up_read(&vcpu->kvm->slots_lock);
Zhang Xiantao8b006792007-11-16 13:05:55 +08004004 tr->physical_address = gpa;
4005 tr->valid = gpa != UNMAPPED_GVA;
4006 tr->writeable = 1;
4007 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08004008 vcpu_put(vcpu);
4009
4010 return 0;
4011}
4012
Hollis Blanchardd0752062007-10-31 17:24:25 -05004013int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
4014{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004015 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05004016
4017 vcpu_load(vcpu);
4018
4019 memcpy(fpu->fpr, fxsave->st_space, 128);
4020 fpu->fcw = fxsave->cwd;
4021 fpu->fsw = fxsave->swd;
4022 fpu->ftwx = fxsave->twd;
4023 fpu->last_opcode = fxsave->fop;
4024 fpu->last_ip = fxsave->rip;
4025 fpu->last_dp = fxsave->rdp;
4026 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
4027
4028 vcpu_put(vcpu);
4029
4030 return 0;
4031}
4032
4033int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
4034{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004035 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05004036
4037 vcpu_load(vcpu);
4038
4039 memcpy(fxsave->st_space, fpu->fpr, 128);
4040 fxsave->cwd = fpu->fcw;
4041 fxsave->swd = fpu->fsw;
4042 fxsave->twd = fpu->ftwx;
4043 fxsave->fop = fpu->last_opcode;
4044 fxsave->rip = fpu->last_ip;
4045 fxsave->rdp = fpu->last_dp;
4046 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
4047
4048 vcpu_put(vcpu);
4049
4050 return 0;
4051}
4052
4053void fx_init(struct kvm_vcpu *vcpu)
4054{
4055 unsigned after_mxcsr_mask;
4056
Andrea Arcangelibc1a34f2008-05-01 18:43:33 +02004057 /*
4058 * Touch the fpu the first time in non atomic context as if
4059 * this is the first fpu instruction the exception handler
4060 * will fire before the instruction returns and it'll have to
4061 * allocate ram with GFP_KERNEL.
4062 */
4063 if (!used_math())
Avi Kivityd6e88ae2008-07-10 16:53:33 +03004064 kvm_fx_save(&vcpu->arch.host_fx_image);
Andrea Arcangelibc1a34f2008-05-01 18:43:33 +02004065
Hollis Blanchardd0752062007-10-31 17:24:25 -05004066 /* Initialize guest FPU by resetting ours and saving into guest's */
4067 preempt_disable();
Avi Kivityd6e88ae2008-07-10 16:53:33 +03004068 kvm_fx_save(&vcpu->arch.host_fx_image);
4069 kvm_fx_finit();
4070 kvm_fx_save(&vcpu->arch.guest_fx_image);
4071 kvm_fx_restore(&vcpu->arch.host_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05004072 preempt_enable();
4073
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004074 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05004075 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004076 vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
4077 memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
Hollis Blanchardd0752062007-10-31 17:24:25 -05004078 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
4079}
4080EXPORT_SYMBOL_GPL(fx_init);
4081
4082void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
4083{
4084 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
4085 return;
4086
4087 vcpu->guest_fpu_loaded = 1;
Avi Kivityd6e88ae2008-07-10 16:53:33 +03004088 kvm_fx_save(&vcpu->arch.host_fx_image);
4089 kvm_fx_restore(&vcpu->arch.guest_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05004090}
4091EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
4092
4093void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
4094{
4095 if (!vcpu->guest_fpu_loaded)
4096 return;
4097
4098 vcpu->guest_fpu_loaded = 0;
Avi Kivityd6e88ae2008-07-10 16:53:33 +03004099 kvm_fx_save(&vcpu->arch.guest_fx_image);
4100 kvm_fx_restore(&vcpu->arch.host_fx_image);
Avi Kivityf096ed82007-11-18 13:54:33 +02004101 ++vcpu->stat.fpu_reload;
Hollis Blanchardd0752062007-10-31 17:24:25 -05004102}
4103EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004104
4105void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
4106{
4107 kvm_x86_ops->vcpu_free(vcpu);
4108}
4109
4110struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
4111 unsigned int id)
4112{
Avi Kivity26e52152007-11-20 15:30:24 +02004113 return kvm_x86_ops->vcpu_create(kvm, id);
4114}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004115
Avi Kivity26e52152007-11-20 15:30:24 +02004116int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
4117{
4118 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004119
4120 /* We do fxsave: this must be aligned. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004121 BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004122
4123 vcpu_load(vcpu);
4124 r = kvm_arch_vcpu_reset(vcpu);
4125 if (r == 0)
4126 r = kvm_mmu_setup(vcpu);
4127 vcpu_put(vcpu);
4128 if (r < 0)
4129 goto free_vcpu;
4130
Avi Kivity26e52152007-11-20 15:30:24 +02004131 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004132free_vcpu:
4133 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivity26e52152007-11-20 15:30:24 +02004134 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004135}
4136
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06004137void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004138{
4139 vcpu_load(vcpu);
4140 kvm_mmu_unload(vcpu);
4141 vcpu_put(vcpu);
4142
4143 kvm_x86_ops->vcpu_free(vcpu);
4144}
4145
4146int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
4147{
4148 return kvm_x86_ops->vcpu_reset(vcpu);
4149}
4150
4151void kvm_arch_hardware_enable(void *garbage)
4152{
4153 kvm_x86_ops->hardware_enable(garbage);
4154}
4155
4156void kvm_arch_hardware_disable(void *garbage)
4157{
4158 kvm_x86_ops->hardware_disable(garbage);
4159}
4160
4161int kvm_arch_hardware_setup(void)
4162{
4163 return kvm_x86_ops->hardware_setup();
4164}
4165
4166void kvm_arch_hardware_unsetup(void)
4167{
4168 kvm_x86_ops->hardware_unsetup();
4169}
4170
4171void kvm_arch_check_processor_compat(void *rtn)
4172{
4173 kvm_x86_ops->check_processor_compatibility(rtn);
4174}
4175
4176int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
4177{
4178 struct page *page;
4179 struct kvm *kvm;
4180 int r;
4181
4182 BUG_ON(vcpu->kvm == NULL);
4183 kvm = vcpu->kvm;
4184
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004185 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004186 if (!irqchip_in_kernel(kvm) || vcpu->vcpu_id == 0)
Avi Kivitya4535292008-04-13 17:54:35 +03004187 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004188 else
Avi Kivitya4535292008-04-13 17:54:35 +03004189 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004190
4191 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
4192 if (!page) {
4193 r = -ENOMEM;
4194 goto fail;
4195 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004196 vcpu->arch.pio_data = page_address(page);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004197
4198 r = kvm_mmu_create(vcpu);
4199 if (r < 0)
4200 goto fail_free_pio_data;
4201
4202 if (irqchip_in_kernel(kvm)) {
4203 r = kvm_create_lapic(vcpu);
4204 if (r < 0)
4205 goto fail_mmu_destroy;
4206 }
4207
4208 return 0;
4209
4210fail_mmu_destroy:
4211 kvm_mmu_destroy(vcpu);
4212fail_free_pio_data:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004213 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004214fail:
4215 return r;
4216}
4217
4218void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
4219{
4220 kvm_free_lapic(vcpu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004221 down_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004222 kvm_mmu_destroy(vcpu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004223 up_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004224 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004225}
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004226
4227struct kvm *kvm_arch_create_vm(void)
4228{
4229 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
4230
4231 if (!kvm)
4232 return ERR_PTR(-ENOMEM);
4233
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08004234 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03004235 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004236
4237 return kvm;
4238}
4239
4240static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
4241{
4242 vcpu_load(vcpu);
4243 kvm_mmu_unload(vcpu);
4244 vcpu_put(vcpu);
4245}
4246
4247static void kvm_free_vcpus(struct kvm *kvm)
4248{
4249 unsigned int i;
4250
4251 /*
4252 * Unpin any mmu pages first.
4253 */
4254 for (i = 0; i < KVM_MAX_VCPUS; ++i)
4255 if (kvm->vcpus[i])
4256 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
4257 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
4258 if (kvm->vcpus[i]) {
4259 kvm_arch_vcpu_free(kvm->vcpus[i]);
4260 kvm->vcpus[i] = NULL;
4261 }
4262 }
4263
4264}
4265
4266void kvm_arch_destroy_vm(struct kvm *kvm)
4267{
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03004268 kvm_free_assigned_devices(kvm);
Sheng Yang78376992008-01-28 05:10:22 +08004269 kvm_free_pit(kvm);
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08004270 kfree(kvm->arch.vpic);
4271 kfree(kvm->arch.vioapic);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004272 kvm_free_vcpus(kvm);
4273 kvm_free_physmem(kvm);
Avi Kivity3d458302008-03-25 11:26:13 +02004274 if (kvm->arch.apic_access_page)
4275 put_page(kvm->arch.apic_access_page);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004276 if (kvm->arch.ept_identity_pagetable)
4277 put_page(kvm->arch.ept_identity_pagetable);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004278 kfree(kvm);
4279}
Zhang Xiantao0de10342007-11-20 16:25:04 +08004280
4281int kvm_arch_set_memory_region(struct kvm *kvm,
4282 struct kvm_userspace_memory_region *mem,
4283 struct kvm_memory_slot old,
4284 int user_alloc)
4285{
4286 int npages = mem->memory_size >> PAGE_SHIFT;
4287 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
4288
4289 /*To keep backward compatibility with older userspace,
4290 *x86 needs to hanlde !user_alloc case.
4291 */
4292 if (!user_alloc) {
4293 if (npages && !old.rmap) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02004294 unsigned long userspace_addr;
4295
Izik Eidus72dc67a2008-02-10 18:04:15 +02004296 down_write(&current->mm->mmap_sem);
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02004297 userspace_addr = do_mmap(NULL, 0,
4298 npages * PAGE_SIZE,
4299 PROT_READ | PROT_WRITE,
Avi Kivityacee3c02008-08-26 17:22:47 +03004300 MAP_PRIVATE | MAP_ANONYMOUS,
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02004301 0);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004302 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08004303
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02004304 if (IS_ERR((void *)userspace_addr))
4305 return PTR_ERR((void *)userspace_addr);
4306
4307 /* set userspace_addr atomically for kvm_hva_to_rmapp */
4308 spin_lock(&kvm->mmu_lock);
4309 memslot->userspace_addr = userspace_addr;
4310 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08004311 } else {
4312 if (!old.user_alloc && old.rmap) {
4313 int ret;
4314
Izik Eidus72dc67a2008-02-10 18:04:15 +02004315 down_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08004316 ret = do_munmap(current->mm, old.userspace_addr,
4317 old.npages * PAGE_SIZE);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004318 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08004319 if (ret < 0)
4320 printk(KERN_WARNING
4321 "kvm_vm_ioctl_set_memory_region: "
4322 "failed to munmap memory\n");
4323 }
4324 }
4325 }
4326
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08004327 if (!kvm->arch.n_requested_mmu_pages) {
Zhang Xiantao0de10342007-11-20 16:25:04 +08004328 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
4329 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
4330 }
4331
4332 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
4333 kvm_flush_remote_tlbs(kvm);
4334
4335 return 0;
4336}
Zhang Xiantao1d737c82007-12-14 09:35:10 +08004337
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03004338void kvm_arch_flush_shadow(struct kvm *kvm)
4339{
4340 kvm_mmu_zap_all(kvm);
4341}
4342
Zhang Xiantao1d737c82007-12-14 09:35:10 +08004343int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
4344{
Avi Kivitya4535292008-04-13 17:54:35 +03004345 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE
4346 || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED;
Zhang Xiantao1d737c82007-12-14 09:35:10 +08004347}
Zhang Xiantao57361992007-12-17 14:21:40 +08004348
4349static void vcpu_kick_intr(void *info)
4350{
4351#ifdef DEBUG
4352 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info;
4353 printk(KERN_DEBUG "vcpu_kick_intr %p \n", vcpu);
4354#endif
4355}
4356
4357void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
4358{
4359 int ipi_pcpu = vcpu->cpu;
Marcelo Tosattie9571ed2008-04-11 15:01:22 -03004360 int cpu = get_cpu();
Zhang Xiantao57361992007-12-17 14:21:40 +08004361
4362 if (waitqueue_active(&vcpu->wq)) {
4363 wake_up_interruptible(&vcpu->wq);
4364 ++vcpu->stat.halt_wakeup;
4365 }
Marcelo Tosattie9571ed2008-04-11 15:01:22 -03004366 /*
4367 * We may be called synchronously with irqs disabled in guest mode,
4368 * So need not to call smp_call_function_single() in that case.
4369 */
4370 if (vcpu->guest_mode && vcpu->cpu != cpu)
Jens Axboe8691e5a2008-06-06 11:18:06 +02004371 smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0);
Marcelo Tosattie9571ed2008-04-11 15:01:22 -03004372 put_cpu();
Zhang Xiantao57361992007-12-17 14:21:40 +08004373}