blob: db16f2353e4bbdad5d1da5a13db5717ffb12b190 [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.
7 *
8 * Authors:
9 * Avi Kivity <avi@qumranet.com>
10 * Yaniv Kamay <yaniv@qumranet.com>
11 *
12 * This work is licensed under the terms of the GNU GPL, version 2. See
13 * the COPYING file in the top-level directory.
14 *
15 */
16
Avi Kivityedf88412007-12-16 11:02:48 +020017#include <linux/kvm_host.h>
Carsten Otte5fb76f92007-10-29 16:08:51 +010018#include "segment_descriptor.h"
Carsten Otte313a3dc2007-10-11 19:16:52 +020019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Carsten Otte313a3dc2007-10-11 19:16:52 +020021
22#include <linux/kvm.h>
23#include <linux/fs.h>
24#include <linux/vmalloc.h>
Carsten Otte5fb76f92007-10-29 16:08:51 +010025#include <linux/module.h>
Zhang Xiantao0de10342007-11-20 16:25:04 +080026#include <linux/mman.h>
Marcelo Tosatti2bacc552007-12-12 10:46:12 -050027#include <linux/highmem.h>
Carsten Otte043405e2007-10-10 17:16:19 +020028
29#include <asm/uaccess.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080030#include <asm/msr.h>
Carsten Otte043405e2007-10-10 17:16:19 +020031
Carsten Otte313a3dc2007-10-11 19:16:52 +020032#define MAX_IO_MSRS 256
Carsten Ottea03490e2007-10-29 16:09:35 +010033#define CR0_RESERVED_BITS \
34 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
35 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
36 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
37#define CR4_RESERVED_BITS \
38 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
39 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
40 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
41 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
42
43#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
Joerg Roedel50a37eb2008-01-31 14:57:38 +010044/* EFER defaults:
45 * - enable syscall per default because its emulated by KVM
46 * - enable LME and LMA per default on 64 bit KVM
47 */
48#ifdef CONFIG_X86_64
49static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL;
50#else
51static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL;
52#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +020053
Avi Kivityba1389b2007-11-18 16:24:12 +020054#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
55#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
Hollis Blanchard417bc302007-10-31 17:24:23 -050056
Avi Kivity674eea02008-02-11 18:37:23 +020057static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
58 struct kvm_cpuid_entry2 __user *entries);
59
Zhang Xiantao97896d02007-11-14 20:09:30 +080060struct kvm_x86_ops *kvm_x86_ops;
61
Hollis Blanchard417bc302007-10-31 17:24:23 -050062struct kvm_stats_debugfs_item debugfs_entries[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +020063 { "pf_fixed", VCPU_STAT(pf_fixed) },
64 { "pf_guest", VCPU_STAT(pf_guest) },
65 { "tlb_flush", VCPU_STAT(tlb_flush) },
66 { "invlpg", VCPU_STAT(invlpg) },
67 { "exits", VCPU_STAT(exits) },
68 { "io_exits", VCPU_STAT(io_exits) },
69 { "mmio_exits", VCPU_STAT(mmio_exits) },
70 { "signal_exits", VCPU_STAT(signal_exits) },
71 { "irq_window", VCPU_STAT(irq_window_exits) },
72 { "halt_exits", VCPU_STAT(halt_exits) },
73 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
74 { "request_irq", VCPU_STAT(request_irq_exits) },
75 { "irq_exits", VCPU_STAT(irq_exits) },
76 { "host_state_reload", VCPU_STAT(host_state_reload) },
77 { "efer_reload", VCPU_STAT(efer_reload) },
78 { "fpu_reload", VCPU_STAT(fpu_reload) },
79 { "insn_emulation", VCPU_STAT(insn_emulation) },
80 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
Avi Kivity4cee5762007-11-18 16:37:07 +020081 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
82 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
83 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
84 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
85 { "mmu_flooded", VM_STAT(mmu_flooded) },
86 { "mmu_recycled", VM_STAT(mmu_recycled) },
Avi Kivitydfc5aa02007-12-18 19:47:18 +020087 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
Avi Kivity0f74a242007-11-20 23:01:14 +020088 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Hollis Blanchard417bc302007-10-31 17:24:23 -050089 { NULL }
90};
91
92
Carsten Otte5fb76f92007-10-29 16:08:51 +010093unsigned long segment_base(u16 selector)
94{
95 struct descriptor_table gdt;
96 struct segment_descriptor *d;
97 unsigned long table_base;
98 unsigned long v;
99
100 if (selector == 0)
101 return 0;
102
103 asm("sgdt %0" : "=m"(gdt));
104 table_base = gdt.base;
105
106 if (selector & 4) { /* from ldt */
107 u16 ldt_selector;
108
109 asm("sldt %0" : "=g"(ldt_selector));
110 table_base = segment_base(ldt_selector);
111 }
112 d = (struct segment_descriptor *)(table_base + (selector & ~7));
113 v = d->base_low | ((unsigned long)d->base_mid << 16) |
114 ((unsigned long)d->base_high << 24);
115#ifdef CONFIG_X86_64
116 if (d->system == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
117 v |= ((unsigned long) \
118 ((struct segment_descriptor_64 *)d)->base_higher) << 32;
119#endif
120 return v;
121}
122EXPORT_SYMBOL_GPL(segment_base);
123
Carsten Otte6866b832007-10-29 16:09:10 +0100124u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
125{
126 if (irqchip_in_kernel(vcpu->kvm))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800127 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100128 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800129 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100130}
131EXPORT_SYMBOL_GPL(kvm_get_apic_base);
132
133void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
134{
135 /* TODO: reserve bits check */
136 if (irqchip_in_kernel(vcpu->kvm))
137 kvm_lapic_set_base(vcpu, data);
138 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800139 vcpu->arch.apic_base = data;
Carsten Otte6866b832007-10-29 16:09:10 +0100140}
141EXPORT_SYMBOL_GPL(kvm_set_apic_base);
142
Avi Kivity298101d2007-11-25 13:41:11 +0200143void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
144{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800145 WARN_ON(vcpu->arch.exception.pending);
146 vcpu->arch.exception.pending = true;
147 vcpu->arch.exception.has_error_code = false;
148 vcpu->arch.exception.nr = nr;
Avi Kivity298101d2007-11-25 13:41:11 +0200149}
150EXPORT_SYMBOL_GPL(kvm_queue_exception);
151
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200152void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
153 u32 error_code)
154{
155 ++vcpu->stat.pf_guest;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800156 if (vcpu->arch.exception.pending && vcpu->arch.exception.nr == PF_VECTOR) {
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200157 printk(KERN_DEBUG "kvm: inject_page_fault:"
158 " double fault 0x%lx\n", addr);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800159 vcpu->arch.exception.nr = DF_VECTOR;
160 vcpu->arch.exception.error_code = 0;
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200161 return;
162 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800163 vcpu->arch.cr2 = addr;
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200164 kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
165}
166
Avi Kivity298101d2007-11-25 13:41:11 +0200167void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
168{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800169 WARN_ON(vcpu->arch.exception.pending);
170 vcpu->arch.exception.pending = true;
171 vcpu->arch.exception.has_error_code = true;
172 vcpu->arch.exception.nr = nr;
173 vcpu->arch.exception.error_code = error_code;
Avi Kivity298101d2007-11-25 13:41:11 +0200174}
175EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
176
177static void __queue_exception(struct kvm_vcpu *vcpu)
178{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800179 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
180 vcpu->arch.exception.has_error_code,
181 vcpu->arch.exception.error_code);
Avi Kivity298101d2007-11-25 13:41:11 +0200182}
183
Carsten Ottea03490e2007-10-29 16:09:35 +0100184/*
185 * Load the pae pdptrs. Return true is they are all valid.
186 */
187int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
188{
189 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
190 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
191 int i;
192 int ret;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800193 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100194
Izik Eidus72dc67a2008-02-10 18:04:15 +0200195 down_read(&vcpu->kvm->slots_lock);
Carsten Ottea03490e2007-10-29 16:09:35 +0100196 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
197 offset * sizeof(u64), sizeof(pdpte));
198 if (ret < 0) {
199 ret = 0;
200 goto out;
201 }
202 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
203 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
204 ret = 0;
205 goto out;
206 }
207 }
208 ret = 1;
209
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800210 memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
Carsten Ottea03490e2007-10-29 16:09:35 +0100211out:
Izik Eidus72dc67a2008-02-10 18:04:15 +0200212 up_read(&vcpu->kvm->slots_lock);
Carsten Ottea03490e2007-10-29 16:09:35 +0100213
214 return ret;
215}
216
Avi Kivityd835dfe2007-11-21 02:57:59 +0200217static bool pdptrs_changed(struct kvm_vcpu *vcpu)
218{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800219 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Avi Kivityd835dfe2007-11-21 02:57:59 +0200220 bool changed = true;
221 int r;
222
223 if (is_long_mode(vcpu) || !is_pae(vcpu))
224 return false;
225
Izik Eidus72dc67a2008-02-10 18:04:15 +0200226 down_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800227 r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
Avi Kivityd835dfe2007-11-21 02:57:59 +0200228 if (r < 0)
229 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800230 changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200231out:
Izik Eidus72dc67a2008-02-10 18:04:15 +0200232 up_read(&vcpu->kvm->slots_lock);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200233
234 return changed;
235}
236
Carsten Ottea03490e2007-10-29 16:09:35 +0100237void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
238{
239 if (cr0 & CR0_RESERVED_BITS) {
240 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800241 cr0, vcpu->arch.cr0);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200242 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100243 return;
244 }
245
246 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
247 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200248 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100249 return;
250 }
251
252 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
253 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
254 "and a clear PE flag\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200255 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100256 return;
257 }
258
259 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
260#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800261 if ((vcpu->arch.shadow_efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100262 int cs_db, cs_l;
263
264 if (!is_pae(vcpu)) {
265 printk(KERN_DEBUG "set_cr0: #GP, start paging "
266 "in long mode while PAE is disabled\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200267 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100268 return;
269 }
270 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
271 if (cs_l) {
272 printk(KERN_DEBUG "set_cr0: #GP, start paging "
273 "in long mode while CS.L == 1\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200274 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100275 return;
276
277 }
278 } else
279#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800280 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100281 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
282 "reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200283 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100284 return;
285 }
286
287 }
288
289 kvm_x86_ops->set_cr0(vcpu, cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800290 vcpu->arch.cr0 = cr0;
Carsten Ottea03490e2007-10-29 16:09:35 +0100291
Carsten Ottea03490e2007-10-29 16:09:35 +0100292 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100293 return;
294}
295EXPORT_SYMBOL_GPL(set_cr0);
296
297void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
298{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800299 set_cr0(vcpu, (vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100300}
301EXPORT_SYMBOL_GPL(lmsw);
302
303void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
304{
305 if (cr4 & CR4_RESERVED_BITS) {
306 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200307 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100308 return;
309 }
310
311 if (is_long_mode(vcpu)) {
312 if (!(cr4 & X86_CR4_PAE)) {
313 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
314 "in long mode\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200315 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100316 return;
317 }
318 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800319 && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100320 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200321 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100322 return;
323 }
324
325 if (cr4 & X86_CR4_VMXE) {
326 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200327 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100328 return;
329 }
330 kvm_x86_ops->set_cr4(vcpu, cr4);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800331 vcpu->arch.cr4 = cr4;
Carsten Ottea03490e2007-10-29 16:09:35 +0100332 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100333}
334EXPORT_SYMBOL_GPL(set_cr4);
335
336void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
337{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800338 if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
Avi Kivityd835dfe2007-11-21 02:57:59 +0200339 kvm_mmu_flush_tlb(vcpu);
340 return;
341 }
342
Carsten Ottea03490e2007-10-29 16:09:35 +0100343 if (is_long_mode(vcpu)) {
344 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
345 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200346 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100347 return;
348 }
349 } else {
350 if (is_pae(vcpu)) {
351 if (cr3 & CR3_PAE_RESERVED_BITS) {
352 printk(KERN_DEBUG
353 "set_cr3: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200354 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100355 return;
356 }
357 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
358 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
359 "reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200360 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100361 return;
362 }
363 }
364 /*
365 * We don't check reserved bits in nonpae mode, because
366 * this isn't enforced, and VMware depends on this.
367 */
368 }
369
Izik Eidus72dc67a2008-02-10 18:04:15 +0200370 down_read(&vcpu->kvm->slots_lock);
Carsten Ottea03490e2007-10-29 16:09:35 +0100371 /*
372 * Does the new cr3 value map to physical memory? (Note, we
373 * catch an invalid cr3 even in real-mode, because it would
374 * cause trouble later on when we turn on paging anyway.)
375 *
376 * A real CPU would silently accept an invalid cr3 and would
377 * attempt to use it - with largely undefined (and often hard
378 * to debug) behavior on the guest side.
379 */
380 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200381 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100382 else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800383 vcpu->arch.cr3 = cr3;
384 vcpu->arch.mmu.new_cr3(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100385 }
Izik Eidus72dc67a2008-02-10 18:04:15 +0200386 up_read(&vcpu->kvm->slots_lock);
Carsten Ottea03490e2007-10-29 16:09:35 +0100387}
388EXPORT_SYMBOL_GPL(set_cr3);
389
390void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
391{
392 if (cr8 & CR8_RESERVED_BITS) {
393 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200394 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100395 return;
396 }
397 if (irqchip_in_kernel(vcpu->kvm))
398 kvm_lapic_set_tpr(vcpu, cr8);
399 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800400 vcpu->arch.cr8 = cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100401}
402EXPORT_SYMBOL_GPL(set_cr8);
403
404unsigned long get_cr8(struct kvm_vcpu *vcpu)
405{
406 if (irqchip_in_kernel(vcpu->kvm))
407 return kvm_lapic_get_cr8(vcpu);
408 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800409 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100410}
411EXPORT_SYMBOL_GPL(get_cr8);
412
Carsten Otte043405e2007-10-10 17:16:19 +0200413/*
414 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
415 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
416 *
417 * This list is modified at module load time to reflect the
418 * capabilities of the host cpu.
419 */
420static u32 msrs_to_save[] = {
421 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
422 MSR_K6_STAR,
423#ifdef CONFIG_X86_64
424 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
425#endif
426 MSR_IA32_TIME_STAMP_COUNTER,
427};
428
429static unsigned num_msrs_to_save;
430
431static u32 emulated_msrs[] = {
432 MSR_IA32_MISC_ENABLE,
433};
434
Carsten Otte15c4a642007-10-30 18:44:17 +0100435#ifdef CONFIG_X86_64
436
437static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
438{
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100439 if (efer & efer_reserved_bits) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100440 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
441 efer);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200442 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100443 return;
444 }
445
446 if (is_paging(vcpu)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800447 && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100448 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200449 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100450 return;
451 }
452
453 kvm_x86_ops->set_efer(vcpu, efer);
454
455 efer &= ~EFER_LMA;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800456 efer |= vcpu->arch.shadow_efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +0100457
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800458 vcpu->arch.shadow_efer = efer;
Carsten Otte15c4a642007-10-30 18:44:17 +0100459}
460
461#endif
462
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100463void kvm_enable_efer_bits(u64 mask)
464{
465 efer_reserved_bits &= ~mask;
466}
467EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
468
469
Carsten Otte15c4a642007-10-30 18:44:17 +0100470/*
471 * Writes msr value into into the appropriate "register".
472 * Returns 0 on success, non-0 otherwise.
473 * Assumes vcpu_load() was already called.
474 */
475int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
476{
477 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
478}
479
Carsten Otte313a3dc2007-10-11 19:16:52 +0200480/*
481 * Adapt set_msr() to msr_io()'s calling convention
482 */
483static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
484{
485 return kvm_set_msr(vcpu, index, *data);
486}
487
Carsten Otte15c4a642007-10-30 18:44:17 +0100488
489int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
490{
491 switch (msr) {
492#ifdef CONFIG_X86_64
493 case MSR_EFER:
494 set_efer(vcpu, data);
495 break;
496#endif
497 case MSR_IA32_MC0_STATUS:
498 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
499 __FUNCTION__, data);
500 break;
501 case MSR_IA32_MCG_STATUS:
502 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
503 __FUNCTION__, data);
504 break;
Joerg Roedelc7ac6792008-02-11 20:28:27 +0100505 case MSR_IA32_MCG_CTL:
506 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_CTL 0x%llx, nop\n",
507 __FUNCTION__, data);
508 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100509 case MSR_IA32_UCODE_REV:
510 case MSR_IA32_UCODE_WRITE:
511 case 0x200 ... 0x2ff: /* MTRRs */
512 break;
513 case MSR_IA32_APICBASE:
514 kvm_set_apic_base(vcpu, data);
515 break;
516 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800517 vcpu->arch.ia32_misc_enable_msr = data;
Carsten Otte15c4a642007-10-30 18:44:17 +0100518 break;
519 default:
Avi Kivity565f1fb2007-12-19 12:02:40 +0200520 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n", msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +0100521 return 1;
522 }
523 return 0;
524}
525EXPORT_SYMBOL_GPL(kvm_set_msr_common);
526
527
528/*
529 * Reads an msr value (of 'msr_index') into 'pdata'.
530 * Returns 0 on success, non-0 otherwise.
531 * Assumes vcpu_load() was already called.
532 */
533int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
534{
535 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
536}
537
538int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
539{
540 u64 data;
541
542 switch (msr) {
543 case 0xc0010010: /* SYSCFG */
544 case 0xc0010015: /* HWCR */
545 case MSR_IA32_PLATFORM_ID:
546 case MSR_IA32_P5_MC_ADDR:
547 case MSR_IA32_P5_MC_TYPE:
548 case MSR_IA32_MC0_CTL:
549 case MSR_IA32_MCG_STATUS:
550 case MSR_IA32_MCG_CAP:
Joerg Roedelc7ac6792008-02-11 20:28:27 +0100551 case MSR_IA32_MCG_CTL:
Carsten Otte15c4a642007-10-30 18:44:17 +0100552 case MSR_IA32_MC0_MISC:
553 case MSR_IA32_MC0_MISC+4:
554 case MSR_IA32_MC0_MISC+8:
555 case MSR_IA32_MC0_MISC+12:
556 case MSR_IA32_MC0_MISC+16:
557 case MSR_IA32_UCODE_REV:
558 case MSR_IA32_PERF_STATUS:
559 case MSR_IA32_EBL_CR_POWERON:
560 /* MTRR registers */
561 case 0xfe:
562 case 0x200 ... 0x2ff:
563 data = 0;
564 break;
565 case 0xcd: /* fsb frequency */
566 data = 3;
567 break;
568 case MSR_IA32_APICBASE:
569 data = kvm_get_apic_base(vcpu);
570 break;
571 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800572 data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +0100573 break;
574#ifdef CONFIG_X86_64
575 case MSR_EFER:
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800576 data = vcpu->arch.shadow_efer;
Carsten Otte15c4a642007-10-30 18:44:17 +0100577 break;
578#endif
579 default:
580 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
581 return 1;
582 }
583 *pdata = data;
584 return 0;
585}
586EXPORT_SYMBOL_GPL(kvm_get_msr_common);
587
Carsten Otte313a3dc2007-10-11 19:16:52 +0200588/*
589 * Read or write a bunch of msrs. All parameters are kernel addresses.
590 *
591 * @return number of msrs set successfully.
592 */
593static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
594 struct kvm_msr_entry *entries,
595 int (*do_msr)(struct kvm_vcpu *vcpu,
596 unsigned index, u64 *data))
597{
598 int i;
599
600 vcpu_load(vcpu);
601
602 for (i = 0; i < msrs->nmsrs; ++i)
603 if (do_msr(vcpu, entries[i].index, &entries[i].data))
604 break;
605
606 vcpu_put(vcpu);
607
608 return i;
609}
610
611/*
612 * Read or write a bunch of msrs. Parameters are user addresses.
613 *
614 * @return number of msrs set successfully.
615 */
616static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
617 int (*do_msr)(struct kvm_vcpu *vcpu,
618 unsigned index, u64 *data),
619 int writeback)
620{
621 struct kvm_msrs msrs;
622 struct kvm_msr_entry *entries;
623 int r, n;
624 unsigned size;
625
626 r = -EFAULT;
627 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
628 goto out;
629
630 r = -E2BIG;
631 if (msrs.nmsrs >= MAX_IO_MSRS)
632 goto out;
633
634 r = -ENOMEM;
635 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
636 entries = vmalloc(size);
637 if (!entries)
638 goto out;
639
640 r = -EFAULT;
641 if (copy_from_user(entries, user_msrs->entries, size))
642 goto out_free;
643
644 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
645 if (r < 0)
646 goto out_free;
647
648 r = -EFAULT;
649 if (writeback && copy_to_user(user_msrs->entries, entries, size))
650 goto out_free;
651
652 r = n;
653
654out_free:
655 vfree(entries);
656out:
657 return r;
658}
659
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800660/*
661 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
662 * cached on it.
663 */
664void decache_vcpus_on_cpu(int cpu)
665{
666 struct kvm *vm;
667 struct kvm_vcpu *vcpu;
668 int i;
669
670 spin_lock(&kvm_lock);
671 list_for_each_entry(vm, &vm_list, vm_list)
672 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
673 vcpu = vm->vcpus[i];
674 if (!vcpu)
675 continue;
676 /*
677 * If the vcpu is locked, then it is running on some
678 * other cpu and therefore it is not cached on the
679 * cpu in question.
680 *
681 * If it's not locked, check the last cpu it executed
682 * on.
683 */
684 if (mutex_trylock(&vcpu->mutex)) {
685 if (vcpu->cpu == cpu) {
686 kvm_x86_ops->vcpu_decache(vcpu);
687 vcpu->cpu = -1;
688 }
689 mutex_unlock(&vcpu->mutex);
690 }
691 }
692 spin_unlock(&kvm_lock);
693}
694
Zhang Xiantao018d00d2007-11-15 23:07:47 +0800695int kvm_dev_ioctl_check_extension(long ext)
696{
697 int r;
698
699 switch (ext) {
700 case KVM_CAP_IRQCHIP:
701 case KVM_CAP_HLT:
702 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
703 case KVM_CAP_USER_MEMORY:
704 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +0200705 case KVM_CAP_EXT_CPUID:
Zhang Xiantao018d00d2007-11-15 23:07:47 +0800706 r = 1;
707 break;
Avi Kivity774ead32007-12-26 13:57:04 +0200708 case KVM_CAP_VAPIC:
709 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
710 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +0800711 default:
712 r = 0;
713 break;
714 }
715 return r;
716
717}
718
Carsten Otte043405e2007-10-10 17:16:19 +0200719long kvm_arch_dev_ioctl(struct file *filp,
720 unsigned int ioctl, unsigned long arg)
721{
722 void __user *argp = (void __user *)arg;
723 long r;
724
725 switch (ioctl) {
726 case KVM_GET_MSR_INDEX_LIST: {
727 struct kvm_msr_list __user *user_msr_list = argp;
728 struct kvm_msr_list msr_list;
729 unsigned n;
730
731 r = -EFAULT;
732 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
733 goto out;
734 n = msr_list.nmsrs;
735 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
736 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
737 goto out;
738 r = -E2BIG;
739 if (n < num_msrs_to_save)
740 goto out;
741 r = -EFAULT;
742 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
743 num_msrs_to_save * sizeof(u32)))
744 goto out;
745 if (copy_to_user(user_msr_list->indices
746 + num_msrs_to_save * sizeof(u32),
747 &emulated_msrs,
748 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
749 goto out;
750 r = 0;
751 break;
752 }
Avi Kivity674eea02008-02-11 18:37:23 +0200753 case KVM_GET_SUPPORTED_CPUID: {
754 struct kvm_cpuid2 __user *cpuid_arg = argp;
755 struct kvm_cpuid2 cpuid;
756
757 r = -EFAULT;
758 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
759 goto out;
760 r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
761 cpuid_arg->entries);
762 if (r)
763 goto out;
764
765 r = -EFAULT;
766 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
767 goto out;
768 r = 0;
769 break;
770 }
Carsten Otte043405e2007-10-10 17:16:19 +0200771 default:
772 r = -EINVAL;
773 }
774out:
775 return r;
776}
777
Carsten Otte313a3dc2007-10-11 19:16:52 +0200778void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
779{
780 kvm_x86_ops->vcpu_load(vcpu, cpu);
781}
782
783void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
784{
785 kvm_x86_ops->vcpu_put(vcpu);
Amit Shah9327fd12007-11-15 18:38:46 +0200786 kvm_put_guest_fpu(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200787}
788
Dan Kenigsberg07716712007-11-21 17:10:04 +0200789static int is_efer_nx(void)
Carsten Otte313a3dc2007-10-11 19:16:52 +0200790{
791 u64 efer;
Carsten Otte313a3dc2007-10-11 19:16:52 +0200792
793 rdmsrl(MSR_EFER, efer);
Dan Kenigsberg07716712007-11-21 17:10:04 +0200794 return efer & EFER_NX;
795}
796
797static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
798{
799 int i;
800 struct kvm_cpuid_entry2 *e, *entry;
801
Carsten Otte313a3dc2007-10-11 19:16:52 +0200802 entry = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800803 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
804 e = &vcpu->arch.cpuid_entries[i];
Carsten Otte313a3dc2007-10-11 19:16:52 +0200805 if (e->function == 0x80000001) {
806 entry = e;
807 break;
808 }
809 }
Dan Kenigsberg07716712007-11-21 17:10:04 +0200810 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
Carsten Otte313a3dc2007-10-11 19:16:52 +0200811 entry->edx &= ~(1 << 20);
812 printk(KERN_INFO "kvm: guest NX capability removed\n");
813 }
814}
815
Dan Kenigsberg07716712007-11-21 17:10:04 +0200816/* when an old userspace process fills a new kernel module */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200817static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
818 struct kvm_cpuid *cpuid,
819 struct kvm_cpuid_entry __user *entries)
820{
Dan Kenigsberg07716712007-11-21 17:10:04 +0200821 int r, i;
822 struct kvm_cpuid_entry *cpuid_entries;
823
824 r = -E2BIG;
825 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
826 goto out;
827 r = -ENOMEM;
828 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
829 if (!cpuid_entries)
830 goto out;
831 r = -EFAULT;
832 if (copy_from_user(cpuid_entries, entries,
833 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
834 goto out_free;
835 for (i = 0; i < cpuid->nent; i++) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800836 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
837 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
838 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
839 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
840 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
841 vcpu->arch.cpuid_entries[i].index = 0;
842 vcpu->arch.cpuid_entries[i].flags = 0;
843 vcpu->arch.cpuid_entries[i].padding[0] = 0;
844 vcpu->arch.cpuid_entries[i].padding[1] = 0;
845 vcpu->arch.cpuid_entries[i].padding[2] = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +0200846 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800847 vcpu->arch.cpuid_nent = cpuid->nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +0200848 cpuid_fix_nx_cap(vcpu);
849 r = 0;
850
851out_free:
852 vfree(cpuid_entries);
853out:
854 return r;
855}
856
857static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
858 struct kvm_cpuid2 *cpuid,
859 struct kvm_cpuid_entry2 __user *entries)
860{
Carsten Otte313a3dc2007-10-11 19:16:52 +0200861 int r;
862
863 r = -E2BIG;
864 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
865 goto out;
866 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800867 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
Dan Kenigsberg07716712007-11-21 17:10:04 +0200868 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
Carsten Otte313a3dc2007-10-11 19:16:52 +0200869 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800870 vcpu->arch.cpuid_nent = cpuid->nent;
Carsten Otte313a3dc2007-10-11 19:16:52 +0200871 return 0;
872
873out:
874 return r;
875}
876
Dan Kenigsberg07716712007-11-21 17:10:04 +0200877static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
878 struct kvm_cpuid2 *cpuid,
879 struct kvm_cpuid_entry2 __user *entries)
880{
881 int r;
882
883 r = -E2BIG;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800884 if (cpuid->nent < vcpu->arch.cpuid_nent)
Dan Kenigsberg07716712007-11-21 17:10:04 +0200885 goto out;
886 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800887 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
888 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +0200889 goto out;
890 return 0;
891
892out:
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800893 cpuid->nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +0200894 return r;
895}
896
897static inline u32 bit(int bitno)
898{
899 return 1 << (bitno & 31);
900}
901
902static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
903 u32 index)
904{
905 entry->function = function;
906 entry->index = index;
907 cpuid_count(entry->function, entry->index,
908 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
909 entry->flags = 0;
910}
911
912static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
913 u32 index, int *nent, int maxnent)
914{
915 const u32 kvm_supported_word0_x86_features = bit(X86_FEATURE_FPU) |
916 bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
917 bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
918 bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
919 bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
920 bit(X86_FEATURE_SEP) | bit(X86_FEATURE_PGE) |
921 bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
922 bit(X86_FEATURE_CLFLSH) | bit(X86_FEATURE_MMX) |
923 bit(X86_FEATURE_FXSR) | bit(X86_FEATURE_XMM) |
924 bit(X86_FEATURE_XMM2) | bit(X86_FEATURE_SELFSNOOP);
925 const u32 kvm_supported_word1_x86_features = bit(X86_FEATURE_FPU) |
926 bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
927 bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
928 bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
929 bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
930 bit(X86_FEATURE_PGE) |
931 bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
932 bit(X86_FEATURE_MMX) | bit(X86_FEATURE_FXSR) |
933 bit(X86_FEATURE_SYSCALL) |
934 (bit(X86_FEATURE_NX) && is_efer_nx()) |
935#ifdef CONFIG_X86_64
936 bit(X86_FEATURE_LM) |
937#endif
938 bit(X86_FEATURE_MMXEXT) |
939 bit(X86_FEATURE_3DNOWEXT) |
940 bit(X86_FEATURE_3DNOW);
941 const u32 kvm_supported_word3_x86_features =
942 bit(X86_FEATURE_XMM3) | bit(X86_FEATURE_CX16);
943 const u32 kvm_supported_word6_x86_features =
944 bit(X86_FEATURE_LAHF_LM) | bit(X86_FEATURE_CMP_LEGACY);
945
946 /* all func 2 cpuid_count() should be called on the same cpu */
947 get_cpu();
948 do_cpuid_1_ent(entry, function, index);
949 ++*nent;
950
951 switch (function) {
952 case 0:
953 entry->eax = min(entry->eax, (u32)0xb);
954 break;
955 case 1:
956 entry->edx &= kvm_supported_word0_x86_features;
957 entry->ecx &= kvm_supported_word3_x86_features;
958 break;
959 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
960 * may return different values. This forces us to get_cpu() before
961 * issuing the first command, and also to emulate this annoying behavior
962 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
963 case 2: {
964 int t, times = entry->eax & 0xff;
965
966 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
967 for (t = 1; t < times && *nent < maxnent; ++t) {
968 do_cpuid_1_ent(&entry[t], function, 0);
969 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
970 ++*nent;
971 }
972 break;
973 }
974 /* function 4 and 0xb have additional index. */
975 case 4: {
976 int index, cache_type;
977
978 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
979 /* read more entries until cache_type is zero */
980 for (index = 1; *nent < maxnent; ++index) {
981 cache_type = entry[index - 1].eax & 0x1f;
982 if (!cache_type)
983 break;
984 do_cpuid_1_ent(&entry[index], function, index);
985 entry[index].flags |=
986 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
987 ++*nent;
988 }
989 break;
990 }
991 case 0xb: {
992 int index, level_type;
993
994 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
995 /* read more entries until level_type is zero */
996 for (index = 1; *nent < maxnent; ++index) {
997 level_type = entry[index - 1].ecx & 0xff;
998 if (!level_type)
999 break;
1000 do_cpuid_1_ent(&entry[index], function, index);
1001 entry[index].flags |=
1002 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1003 ++*nent;
1004 }
1005 break;
1006 }
1007 case 0x80000000:
1008 entry->eax = min(entry->eax, 0x8000001a);
1009 break;
1010 case 0x80000001:
1011 entry->edx &= kvm_supported_word1_x86_features;
1012 entry->ecx &= kvm_supported_word6_x86_features;
1013 break;
1014 }
1015 put_cpu();
1016}
1017
Avi Kivity674eea02008-02-11 18:37:23 +02001018static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
Dan Kenigsberg07716712007-11-21 17:10:04 +02001019 struct kvm_cpuid_entry2 __user *entries)
1020{
1021 struct kvm_cpuid_entry2 *cpuid_entries;
1022 int limit, nent = 0, r = -E2BIG;
1023 u32 func;
1024
1025 if (cpuid->nent < 1)
1026 goto out;
1027 r = -ENOMEM;
1028 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
1029 if (!cpuid_entries)
1030 goto out;
1031
1032 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
1033 limit = cpuid_entries[0].eax;
1034 for (func = 1; func <= limit && nent < cpuid->nent; ++func)
1035 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1036 &nent, cpuid->nent);
1037 r = -E2BIG;
1038 if (nent >= cpuid->nent)
1039 goto out_free;
1040
1041 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
1042 limit = cpuid_entries[nent - 1].eax;
1043 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
1044 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1045 &nent, cpuid->nent);
1046 r = -EFAULT;
1047 if (copy_to_user(entries, cpuid_entries,
1048 nent * sizeof(struct kvm_cpuid_entry2)))
1049 goto out_free;
1050 cpuid->nent = nent;
1051 r = 0;
1052
1053out_free:
1054 vfree(cpuid_entries);
1055out:
1056 return r;
1057}
1058
Carsten Otte313a3dc2007-10-11 19:16:52 +02001059static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
1060 struct kvm_lapic_state *s)
1061{
1062 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001063 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001064 vcpu_put(vcpu);
1065
1066 return 0;
1067}
1068
1069static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
1070 struct kvm_lapic_state *s)
1071{
1072 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001073 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001074 kvm_apic_post_state_restore(vcpu);
1075 vcpu_put(vcpu);
1076
1077 return 0;
1078}
1079
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001080static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
1081 struct kvm_interrupt *irq)
1082{
1083 if (irq->irq < 0 || irq->irq >= 256)
1084 return -EINVAL;
1085 if (irqchip_in_kernel(vcpu->kvm))
1086 return -ENXIO;
1087 vcpu_load(vcpu);
1088
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001089 set_bit(irq->irq, vcpu->arch.irq_pending);
1090 set_bit(irq->irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001091
1092 vcpu_put(vcpu);
1093
1094 return 0;
1095}
1096
Avi Kivityb209749f2007-10-22 16:50:39 +02001097static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
1098 struct kvm_tpr_access_ctl *tac)
1099{
1100 if (tac->flags)
1101 return -EINVAL;
1102 vcpu->arch.tpr_access_reporting = !!tac->enabled;
1103 return 0;
1104}
1105
Carsten Otte313a3dc2007-10-11 19:16:52 +02001106long kvm_arch_vcpu_ioctl(struct file *filp,
1107 unsigned int ioctl, unsigned long arg)
1108{
1109 struct kvm_vcpu *vcpu = filp->private_data;
1110 void __user *argp = (void __user *)arg;
1111 int r;
1112
1113 switch (ioctl) {
1114 case KVM_GET_LAPIC: {
1115 struct kvm_lapic_state lapic;
1116
1117 memset(&lapic, 0, sizeof lapic);
1118 r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic);
1119 if (r)
1120 goto out;
1121 r = -EFAULT;
1122 if (copy_to_user(argp, &lapic, sizeof lapic))
1123 goto out;
1124 r = 0;
1125 break;
1126 }
1127 case KVM_SET_LAPIC: {
1128 struct kvm_lapic_state lapic;
1129
1130 r = -EFAULT;
1131 if (copy_from_user(&lapic, argp, sizeof lapic))
1132 goto out;
1133 r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);;
1134 if (r)
1135 goto out;
1136 r = 0;
1137 break;
1138 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001139 case KVM_INTERRUPT: {
1140 struct kvm_interrupt irq;
1141
1142 r = -EFAULT;
1143 if (copy_from_user(&irq, argp, sizeof irq))
1144 goto out;
1145 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1146 if (r)
1147 goto out;
1148 r = 0;
1149 break;
1150 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001151 case KVM_SET_CPUID: {
1152 struct kvm_cpuid __user *cpuid_arg = argp;
1153 struct kvm_cpuid cpuid;
1154
1155 r = -EFAULT;
1156 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1157 goto out;
1158 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
1159 if (r)
1160 goto out;
1161 break;
1162 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02001163 case KVM_SET_CPUID2: {
1164 struct kvm_cpuid2 __user *cpuid_arg = argp;
1165 struct kvm_cpuid2 cpuid;
1166
1167 r = -EFAULT;
1168 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1169 goto out;
1170 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
1171 cpuid_arg->entries);
1172 if (r)
1173 goto out;
1174 break;
1175 }
1176 case KVM_GET_CPUID2: {
1177 struct kvm_cpuid2 __user *cpuid_arg = argp;
1178 struct kvm_cpuid2 cpuid;
1179
1180 r = -EFAULT;
1181 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1182 goto out;
1183 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
1184 cpuid_arg->entries);
1185 if (r)
1186 goto out;
1187 r = -EFAULT;
1188 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1189 goto out;
1190 r = 0;
1191 break;
1192 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001193 case KVM_GET_MSRS:
1194 r = msr_io(vcpu, argp, kvm_get_msr, 1);
1195 break;
1196 case KVM_SET_MSRS:
1197 r = msr_io(vcpu, argp, do_set_msr, 0);
1198 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02001199 case KVM_TPR_ACCESS_REPORTING: {
1200 struct kvm_tpr_access_ctl tac;
1201
1202 r = -EFAULT;
1203 if (copy_from_user(&tac, argp, sizeof tac))
1204 goto out;
1205 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
1206 if (r)
1207 goto out;
1208 r = -EFAULT;
1209 if (copy_to_user(argp, &tac, sizeof tac))
1210 goto out;
1211 r = 0;
1212 break;
1213 };
Avi Kivityb93463a2007-10-25 16:52:32 +02001214 case KVM_SET_VAPIC_ADDR: {
1215 struct kvm_vapic_addr va;
1216
1217 r = -EINVAL;
1218 if (!irqchip_in_kernel(vcpu->kvm))
1219 goto out;
1220 r = -EFAULT;
1221 if (copy_from_user(&va, argp, sizeof va))
1222 goto out;
1223 r = 0;
1224 kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
1225 break;
1226 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001227 default:
1228 r = -EINVAL;
1229 }
1230out:
1231 return r;
1232}
1233
Carsten Otte1fe779f2007-10-29 16:08:35 +01001234static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
1235{
1236 int ret;
1237
1238 if (addr > (unsigned int)(-3 * PAGE_SIZE))
1239 return -1;
1240 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
1241 return ret;
1242}
1243
1244static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
1245 u32 kvm_nr_mmu_pages)
1246{
1247 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
1248 return -EINVAL;
1249
Izik Eidus72dc67a2008-02-10 18:04:15 +02001250 down_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001251
1252 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001253 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001254
Izik Eidus72dc67a2008-02-10 18:04:15 +02001255 up_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001256 return 0;
1257}
1258
1259static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
1260{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001261 return kvm->arch.n_alloc_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001262}
1263
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08001264gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
1265{
1266 int i;
1267 struct kvm_mem_alias *alias;
1268
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001269 for (i = 0; i < kvm->arch.naliases; ++i) {
1270 alias = &kvm->arch.aliases[i];
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08001271 if (gfn >= alias->base_gfn
1272 && gfn < alias->base_gfn + alias->npages)
1273 return alias->target_gfn + gfn - alias->base_gfn;
1274 }
1275 return gfn;
1276}
1277
Carsten Otte1fe779f2007-10-29 16:08:35 +01001278/*
1279 * Set a new alias region. Aliases map a portion of physical memory into
1280 * another portion. This is useful for memory windows, for example the PC
1281 * VGA region.
1282 */
1283static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
1284 struct kvm_memory_alias *alias)
1285{
1286 int r, n;
1287 struct kvm_mem_alias *p;
1288
1289 r = -EINVAL;
1290 /* General sanity checks */
1291 if (alias->memory_size & (PAGE_SIZE - 1))
1292 goto out;
1293 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
1294 goto out;
1295 if (alias->slot >= KVM_ALIAS_SLOTS)
1296 goto out;
1297 if (alias->guest_phys_addr + alias->memory_size
1298 < alias->guest_phys_addr)
1299 goto out;
1300 if (alias->target_phys_addr + alias->memory_size
1301 < alias->target_phys_addr)
1302 goto out;
1303
Izik Eidus72dc67a2008-02-10 18:04:15 +02001304 down_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001305
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001306 p = &kvm->arch.aliases[alias->slot];
Carsten Otte1fe779f2007-10-29 16:08:35 +01001307 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
1308 p->npages = alias->memory_size >> PAGE_SHIFT;
1309 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
1310
1311 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001312 if (kvm->arch.aliases[n - 1].npages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01001313 break;
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001314 kvm->arch.naliases = n;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001315
1316 kvm_mmu_zap_all(kvm);
1317
Izik Eidus72dc67a2008-02-10 18:04:15 +02001318 up_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001319
1320 return 0;
1321
1322out:
1323 return r;
1324}
1325
1326static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1327{
1328 int r;
1329
1330 r = 0;
1331 switch (chip->chip_id) {
1332 case KVM_IRQCHIP_PIC_MASTER:
1333 memcpy(&chip->chip.pic,
1334 &pic_irqchip(kvm)->pics[0],
1335 sizeof(struct kvm_pic_state));
1336 break;
1337 case KVM_IRQCHIP_PIC_SLAVE:
1338 memcpy(&chip->chip.pic,
1339 &pic_irqchip(kvm)->pics[1],
1340 sizeof(struct kvm_pic_state));
1341 break;
1342 case KVM_IRQCHIP_IOAPIC:
1343 memcpy(&chip->chip.ioapic,
1344 ioapic_irqchip(kvm),
1345 sizeof(struct kvm_ioapic_state));
1346 break;
1347 default:
1348 r = -EINVAL;
1349 break;
1350 }
1351 return r;
1352}
1353
1354static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1355{
1356 int r;
1357
1358 r = 0;
1359 switch (chip->chip_id) {
1360 case KVM_IRQCHIP_PIC_MASTER:
1361 memcpy(&pic_irqchip(kvm)->pics[0],
1362 &chip->chip.pic,
1363 sizeof(struct kvm_pic_state));
1364 break;
1365 case KVM_IRQCHIP_PIC_SLAVE:
1366 memcpy(&pic_irqchip(kvm)->pics[1],
1367 &chip->chip.pic,
1368 sizeof(struct kvm_pic_state));
1369 break;
1370 case KVM_IRQCHIP_IOAPIC:
1371 memcpy(ioapic_irqchip(kvm),
1372 &chip->chip.ioapic,
1373 sizeof(struct kvm_ioapic_state));
1374 break;
1375 default:
1376 r = -EINVAL;
1377 break;
1378 }
1379 kvm_pic_update_irq(pic_irqchip(kvm));
1380 return r;
1381}
1382
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001383/*
1384 * Get (and clear) the dirty memory log for a memory slot.
1385 */
1386int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1387 struct kvm_dirty_log *log)
1388{
1389 int r;
1390 int n;
1391 struct kvm_memory_slot *memslot;
1392 int is_dirty = 0;
1393
Izik Eidus72dc67a2008-02-10 18:04:15 +02001394 down_write(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001395
1396 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1397 if (r)
1398 goto out;
1399
1400 /* If nothing is dirty, don't bother messing with page tables. */
1401 if (is_dirty) {
1402 kvm_mmu_slot_remove_write_access(kvm, log->slot);
1403 kvm_flush_remote_tlbs(kvm);
1404 memslot = &kvm->memslots[log->slot];
1405 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1406 memset(memslot->dirty_bitmap, 0, n);
1407 }
1408 r = 0;
1409out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02001410 up_write(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001411 return r;
1412}
1413
Carsten Otte1fe779f2007-10-29 16:08:35 +01001414long kvm_arch_vm_ioctl(struct file *filp,
1415 unsigned int ioctl, unsigned long arg)
1416{
1417 struct kvm *kvm = filp->private_data;
1418 void __user *argp = (void __user *)arg;
1419 int r = -EINVAL;
1420
1421 switch (ioctl) {
1422 case KVM_SET_TSS_ADDR:
1423 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
1424 if (r < 0)
1425 goto out;
1426 break;
1427 case KVM_SET_MEMORY_REGION: {
1428 struct kvm_memory_region kvm_mem;
1429 struct kvm_userspace_memory_region kvm_userspace_mem;
1430
1431 r = -EFAULT;
1432 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
1433 goto out;
1434 kvm_userspace_mem.slot = kvm_mem.slot;
1435 kvm_userspace_mem.flags = kvm_mem.flags;
1436 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
1437 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
1438 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
1439 if (r)
1440 goto out;
1441 break;
1442 }
1443 case KVM_SET_NR_MMU_PAGES:
1444 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
1445 if (r)
1446 goto out;
1447 break;
1448 case KVM_GET_NR_MMU_PAGES:
1449 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
1450 break;
1451 case KVM_SET_MEMORY_ALIAS: {
1452 struct kvm_memory_alias alias;
1453
1454 r = -EFAULT;
1455 if (copy_from_user(&alias, argp, sizeof alias))
1456 goto out;
1457 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
1458 if (r)
1459 goto out;
1460 break;
1461 }
1462 case KVM_CREATE_IRQCHIP:
1463 r = -ENOMEM;
Zhang Xiantaod7deeeb2007-12-14 10:17:34 +08001464 kvm->arch.vpic = kvm_create_pic(kvm);
1465 if (kvm->arch.vpic) {
Carsten Otte1fe779f2007-10-29 16:08:35 +01001466 r = kvm_ioapic_init(kvm);
1467 if (r) {
Zhang Xiantaod7deeeb2007-12-14 10:17:34 +08001468 kfree(kvm->arch.vpic);
1469 kvm->arch.vpic = NULL;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001470 goto out;
1471 }
1472 } else
1473 goto out;
1474 break;
1475 case KVM_IRQ_LINE: {
1476 struct kvm_irq_level irq_event;
1477
1478 r = -EFAULT;
1479 if (copy_from_user(&irq_event, argp, sizeof irq_event))
1480 goto out;
1481 if (irqchip_in_kernel(kvm)) {
1482 mutex_lock(&kvm->lock);
1483 if (irq_event.irq < 16)
1484 kvm_pic_set_irq(pic_irqchip(kvm),
1485 irq_event.irq,
1486 irq_event.level);
Zhang Xiantaod7deeeb2007-12-14 10:17:34 +08001487 kvm_ioapic_set_irq(kvm->arch.vioapic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01001488 irq_event.irq,
1489 irq_event.level);
1490 mutex_unlock(&kvm->lock);
1491 r = 0;
1492 }
1493 break;
1494 }
1495 case KVM_GET_IRQCHIP: {
1496 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1497 struct kvm_irqchip chip;
1498
1499 r = -EFAULT;
1500 if (copy_from_user(&chip, argp, sizeof chip))
1501 goto out;
1502 r = -ENXIO;
1503 if (!irqchip_in_kernel(kvm))
1504 goto out;
1505 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
1506 if (r)
1507 goto out;
1508 r = -EFAULT;
1509 if (copy_to_user(argp, &chip, sizeof chip))
1510 goto out;
1511 r = 0;
1512 break;
1513 }
1514 case KVM_SET_IRQCHIP: {
1515 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1516 struct kvm_irqchip chip;
1517
1518 r = -EFAULT;
1519 if (copy_from_user(&chip, argp, sizeof chip))
1520 goto out;
1521 r = -ENXIO;
1522 if (!irqchip_in_kernel(kvm))
1523 goto out;
1524 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
1525 if (r)
1526 goto out;
1527 r = 0;
1528 break;
1529 }
1530 default:
1531 ;
1532 }
1533out:
1534 return r;
1535}
1536
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08001537static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02001538{
1539 u32 dummy[2];
1540 unsigned i, j;
1541
1542 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
1543 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
1544 continue;
1545 if (j < i)
1546 msrs_to_save[j] = msrs_to_save[i];
1547 j++;
1548 }
1549 num_msrs_to_save = j;
1550}
1551
Carsten Ottebbd9b642007-10-30 18:44:21 +01001552/*
1553 * Only apic need an MMIO device hook, so shortcut now..
1554 */
1555static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
1556 gpa_t addr)
1557{
1558 struct kvm_io_device *dev;
1559
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001560 if (vcpu->arch.apic) {
1561 dev = &vcpu->arch.apic->dev;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001562 if (dev->in_range(dev, addr))
1563 return dev;
1564 }
1565 return NULL;
1566}
1567
1568
1569static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1570 gpa_t addr)
1571{
1572 struct kvm_io_device *dev;
1573
1574 dev = vcpu_find_pervcpu_dev(vcpu, addr);
1575 if (dev == NULL)
1576 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1577 return dev;
1578}
1579
1580int emulator_read_std(unsigned long addr,
1581 void *val,
1582 unsigned int bytes,
1583 struct kvm_vcpu *vcpu)
1584{
1585 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001586 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001587
Izik Eidus72dc67a2008-02-10 18:04:15 +02001588 down_read(&vcpu->kvm->slots_lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001589 while (bytes) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001590 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001591 unsigned offset = addr & (PAGE_SIZE-1);
1592 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
1593 int ret;
1594
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001595 if (gpa == UNMAPPED_GVA) {
1596 r = X86EMUL_PROPAGATE_FAULT;
1597 goto out;
1598 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01001599 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001600 if (ret < 0) {
1601 r = X86EMUL_UNHANDLEABLE;
1602 goto out;
1603 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01001604
1605 bytes -= tocopy;
1606 data += tocopy;
1607 addr += tocopy;
1608 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001609out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02001610 up_read(&vcpu->kvm->slots_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001611 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001612}
1613EXPORT_SYMBOL_GPL(emulator_read_std);
1614
Carsten Ottebbd9b642007-10-30 18:44:21 +01001615static int emulator_read_emulated(unsigned long addr,
1616 void *val,
1617 unsigned int bytes,
1618 struct kvm_vcpu *vcpu)
1619{
1620 struct kvm_io_device *mmio_dev;
1621 gpa_t gpa;
1622
1623 if (vcpu->mmio_read_completed) {
1624 memcpy(val, vcpu->mmio_data, bytes);
1625 vcpu->mmio_read_completed = 0;
1626 return X86EMUL_CONTINUE;
1627 }
1628
Izik Eidus72dc67a2008-02-10 18:04:15 +02001629 down_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001630 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001631 up_read(&vcpu->kvm->slots_lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001632
1633 /* For APIC access vmexit */
1634 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1635 goto mmio;
1636
1637 if (emulator_read_std(addr, val, bytes, vcpu)
1638 == X86EMUL_CONTINUE)
1639 return X86EMUL_CONTINUE;
1640 if (gpa == UNMAPPED_GVA)
1641 return X86EMUL_PROPAGATE_FAULT;
1642
1643mmio:
1644 /*
1645 * Is this MMIO handled locally?
1646 */
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001647 mutex_lock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001648 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1649 if (mmio_dev) {
1650 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001651 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001652 return X86EMUL_CONTINUE;
1653 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001654 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001655
1656 vcpu->mmio_needed = 1;
1657 vcpu->mmio_phys_addr = gpa;
1658 vcpu->mmio_size = bytes;
1659 vcpu->mmio_is_write = 0;
1660
1661 return X86EMUL_UNHANDLEABLE;
1662}
1663
1664static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
1665 const void *val, int bytes)
1666{
1667 int ret;
1668
Izik Eidus72dc67a2008-02-10 18:04:15 +02001669 down_read(&vcpu->kvm->slots_lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001670 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001671 if (ret < 0) {
Izik Eidus72dc67a2008-02-10 18:04:15 +02001672 up_read(&vcpu->kvm->slots_lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001673 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001674 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01001675 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001676 up_read(&vcpu->kvm->slots_lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001677 return 1;
1678}
1679
1680static int emulator_write_emulated_onepage(unsigned long addr,
1681 const void *val,
1682 unsigned int bytes,
1683 struct kvm_vcpu *vcpu)
1684{
1685 struct kvm_io_device *mmio_dev;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001686 gpa_t gpa;
1687
Izik Eidus72dc67a2008-02-10 18:04:15 +02001688 down_read(&vcpu->kvm->slots_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001689 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001690 up_read(&vcpu->kvm->slots_lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001691
1692 if (gpa == UNMAPPED_GVA) {
Avi Kivityc3c91fe2007-11-25 14:04:58 +02001693 kvm_inject_page_fault(vcpu, addr, 2);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001694 return X86EMUL_PROPAGATE_FAULT;
1695 }
1696
1697 /* For APIC access vmexit */
1698 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1699 goto mmio;
1700
1701 if (emulator_write_phys(vcpu, gpa, val, bytes))
1702 return X86EMUL_CONTINUE;
1703
1704mmio:
1705 /*
1706 * Is this MMIO handled locally?
1707 */
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001708 mutex_lock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001709 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1710 if (mmio_dev) {
1711 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001712 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001713 return X86EMUL_CONTINUE;
1714 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001715 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001716
1717 vcpu->mmio_needed = 1;
1718 vcpu->mmio_phys_addr = gpa;
1719 vcpu->mmio_size = bytes;
1720 vcpu->mmio_is_write = 1;
1721 memcpy(vcpu->mmio_data, val, bytes);
1722
1723 return X86EMUL_CONTINUE;
1724}
1725
1726int emulator_write_emulated(unsigned long addr,
1727 const void *val,
1728 unsigned int bytes,
1729 struct kvm_vcpu *vcpu)
1730{
1731 /* Crossing a page boundary? */
1732 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1733 int rc, now;
1734
1735 now = -addr & ~PAGE_MASK;
1736 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
1737 if (rc != X86EMUL_CONTINUE)
1738 return rc;
1739 addr += now;
1740 val += now;
1741 bytes -= now;
1742 }
1743 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
1744}
1745EXPORT_SYMBOL_GPL(emulator_write_emulated);
1746
1747static int emulator_cmpxchg_emulated(unsigned long addr,
1748 const void *old,
1749 const void *new,
1750 unsigned int bytes,
1751 struct kvm_vcpu *vcpu)
1752{
1753 static int reported;
1754
1755 if (!reported) {
1756 reported = 1;
1757 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1758 }
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001759#ifndef CONFIG_X86_64
1760 /* guests cmpxchg8b have to be emulated atomically */
1761 if (bytes == 8) {
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001762 gpa_t gpa;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001763 struct page *page;
Andrew Mortonc0b49b02008-02-04 22:27:18 -08001764 char *kaddr;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001765 u64 val;
1766
Izik Eidus72dc67a2008-02-10 18:04:15 +02001767 down_read(&vcpu->kvm->slots_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001768 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
1769
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001770 if (gpa == UNMAPPED_GVA ||
1771 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1772 goto emul_write;
1773
1774 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
1775 goto emul_write;
1776
1777 val = *(u64 *)new;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001778
1779 down_read(&current->mm->mmap_sem);
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001780 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001781 up_read(&current->mm->mmap_sem);
1782
Andrew Mortonc0b49b02008-02-04 22:27:18 -08001783 kaddr = kmap_atomic(page, KM_USER0);
1784 set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
1785 kunmap_atomic(kaddr, KM_USER0);
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001786 kvm_release_page_dirty(page);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001787 emul_write:
Izik Eidus72dc67a2008-02-10 18:04:15 +02001788 up_read(&vcpu->kvm->slots_lock);
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001789 }
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001790#endif
1791
Carsten Ottebbd9b642007-10-30 18:44:21 +01001792 return emulator_write_emulated(addr, new, bytes, vcpu);
1793}
1794
1795static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1796{
1797 return kvm_x86_ops->get_segment_base(vcpu, seg);
1798}
1799
1800int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1801{
1802 return X86EMUL_CONTINUE;
1803}
1804
1805int emulate_clts(struct kvm_vcpu *vcpu)
1806{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001807 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001808 return X86EMUL_CONTINUE;
1809}
1810
1811int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
1812{
1813 struct kvm_vcpu *vcpu = ctxt->vcpu;
1814
1815 switch (dr) {
1816 case 0 ... 3:
1817 *dest = kvm_x86_ops->get_dr(vcpu, dr);
1818 return X86EMUL_CONTINUE;
1819 default:
1820 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
1821 return X86EMUL_UNHANDLEABLE;
1822 }
1823}
1824
1825int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1826{
1827 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1828 int exception;
1829
1830 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1831 if (exception) {
1832 /* FIXME: better handling */
1833 return X86EMUL_UNHANDLEABLE;
1834 }
1835 return X86EMUL_CONTINUE;
1836}
1837
1838void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
1839{
1840 static int reported;
1841 u8 opcodes[4];
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001842 unsigned long rip = vcpu->arch.rip;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001843 unsigned long rip_linear;
1844
1845 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
1846
1847 if (reported)
1848 return;
1849
1850 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
1851
1852 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1853 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1854 reported = 1;
1855}
1856EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
1857
1858struct x86_emulate_ops emulate_ops = {
1859 .read_std = emulator_read_std,
Carsten Ottebbd9b642007-10-30 18:44:21 +01001860 .read_emulated = emulator_read_emulated,
1861 .write_emulated = emulator_write_emulated,
1862 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1863};
1864
1865int emulate_instruction(struct kvm_vcpu *vcpu,
1866 struct kvm_run *run,
1867 unsigned long cr2,
1868 u16 error_code,
Sheng Yang571008d2008-01-02 14:49:22 +08001869 int emulation_type)
Carsten Ottebbd9b642007-10-30 18:44:21 +01001870{
1871 int r;
Sheng Yang571008d2008-01-02 14:49:22 +08001872 struct decode_cache *c;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001873
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001874 vcpu->arch.mmio_fault_cr2 = cr2;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001875 kvm_x86_ops->cache_regs(vcpu);
1876
1877 vcpu->mmio_is_write = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001878 vcpu->arch.pio.string = 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001879
Sheng Yang571008d2008-01-02 14:49:22 +08001880 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Carsten Ottebbd9b642007-10-30 18:44:21 +01001881 int cs_db, cs_l;
1882 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1883
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001884 vcpu->arch.emulate_ctxt.vcpu = vcpu;
1885 vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
1886 vcpu->arch.emulate_ctxt.mode =
1887 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
Carsten Ottebbd9b642007-10-30 18:44:21 +01001888 ? X86EMUL_MODE_REAL : cs_l
1889 ? X86EMUL_MODE_PROT64 : cs_db
1890 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1891
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001892 if (vcpu->arch.emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1893 vcpu->arch.emulate_ctxt.cs_base = 0;
1894 vcpu->arch.emulate_ctxt.ds_base = 0;
1895 vcpu->arch.emulate_ctxt.es_base = 0;
1896 vcpu->arch.emulate_ctxt.ss_base = 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001897 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001898 vcpu->arch.emulate_ctxt.cs_base =
Carsten Ottebbd9b642007-10-30 18:44:21 +01001899 get_segment_base(vcpu, VCPU_SREG_CS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001900 vcpu->arch.emulate_ctxt.ds_base =
Carsten Ottebbd9b642007-10-30 18:44:21 +01001901 get_segment_base(vcpu, VCPU_SREG_DS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001902 vcpu->arch.emulate_ctxt.es_base =
Carsten Ottebbd9b642007-10-30 18:44:21 +01001903 get_segment_base(vcpu, VCPU_SREG_ES);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001904 vcpu->arch.emulate_ctxt.ss_base =
Carsten Ottebbd9b642007-10-30 18:44:21 +01001905 get_segment_base(vcpu, VCPU_SREG_SS);
1906 }
1907
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001908 vcpu->arch.emulate_ctxt.gs_base =
Carsten Ottebbd9b642007-10-30 18:44:21 +01001909 get_segment_base(vcpu, VCPU_SREG_GS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001910 vcpu->arch.emulate_ctxt.fs_base =
Carsten Ottebbd9b642007-10-30 18:44:21 +01001911 get_segment_base(vcpu, VCPU_SREG_FS);
1912
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001913 r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Sheng Yang571008d2008-01-02 14:49:22 +08001914
1915 /* Reject the instructions other than VMCALL/VMMCALL when
1916 * try to emulate invalid opcode */
1917 c = &vcpu->arch.emulate_ctxt.decode;
1918 if ((emulation_type & EMULTYPE_TRAP_UD) &&
1919 (!(c->twobyte && c->b == 0x01 &&
1920 (c->modrm_reg == 0 || c->modrm_reg == 3) &&
1921 c->modrm_mod == 3 && c->modrm_rm == 1)))
1922 return EMULATE_FAIL;
1923
Avi Kivityf2b57562007-11-18 15:17:51 +02001924 ++vcpu->stat.insn_emulation;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001925 if (r) {
Avi Kivityf2b57562007-11-18 15:17:51 +02001926 ++vcpu->stat.insn_emulation_fail;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001927 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1928 return EMULATE_DONE;
1929 return EMULATE_FAIL;
1930 }
1931 }
1932
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001933 r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001934
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001935 if (vcpu->arch.pio.string)
Carsten Ottebbd9b642007-10-30 18:44:21 +01001936 return EMULATE_DO_MMIO;
1937
1938 if ((r || vcpu->mmio_is_write) && run) {
1939 run->exit_reason = KVM_EXIT_MMIO;
1940 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1941 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1942 run->mmio.len = vcpu->mmio_size;
1943 run->mmio.is_write = vcpu->mmio_is_write;
1944 }
1945
1946 if (r) {
1947 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1948 return EMULATE_DONE;
1949 if (!vcpu->mmio_needed) {
1950 kvm_report_emulation_failure(vcpu, "mmio");
1951 return EMULATE_FAIL;
1952 }
1953 return EMULATE_DO_MMIO;
1954 }
1955
1956 kvm_x86_ops->decache_regs(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001957 kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001958
1959 if (vcpu->mmio_is_write) {
1960 vcpu->mmio_needed = 0;
1961 return EMULATE_DO_MMIO;
1962 }
1963
1964 return EMULATE_DONE;
1965}
1966EXPORT_SYMBOL_GPL(emulate_instruction);
1967
Carsten Ottede7d7892007-10-30 18:44:25 +01001968static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
1969{
1970 int i;
1971
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001972 for (i = 0; i < ARRAY_SIZE(vcpu->arch.pio.guest_pages); ++i)
1973 if (vcpu->arch.pio.guest_pages[i]) {
1974 kvm_release_page_dirty(vcpu->arch.pio.guest_pages[i]);
1975 vcpu->arch.pio.guest_pages[i] = NULL;
Carsten Ottede7d7892007-10-30 18:44:25 +01001976 }
1977}
1978
1979static int pio_copy_data(struct kvm_vcpu *vcpu)
1980{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001981 void *p = vcpu->arch.pio_data;
Carsten Ottede7d7892007-10-30 18:44:25 +01001982 void *q;
1983 unsigned bytes;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001984 int nr_pages = vcpu->arch.pio.guest_pages[1] ? 2 : 1;
Carsten Ottede7d7892007-10-30 18:44:25 +01001985
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001986 q = vmap(vcpu->arch.pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
Carsten Ottede7d7892007-10-30 18:44:25 +01001987 PAGE_KERNEL);
1988 if (!q) {
1989 free_pio_guest_pages(vcpu);
1990 return -ENOMEM;
1991 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001992 q += vcpu->arch.pio.guest_page_offset;
1993 bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
1994 if (vcpu->arch.pio.in)
Carsten Ottede7d7892007-10-30 18:44:25 +01001995 memcpy(q, p, bytes);
1996 else
1997 memcpy(p, q, bytes);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001998 q -= vcpu->arch.pio.guest_page_offset;
Carsten Ottede7d7892007-10-30 18:44:25 +01001999 vunmap(q);
2000 free_pio_guest_pages(vcpu);
2001 return 0;
2002}
2003
2004int complete_pio(struct kvm_vcpu *vcpu)
2005{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002006 struct kvm_pio_request *io = &vcpu->arch.pio;
Carsten Ottede7d7892007-10-30 18:44:25 +01002007 long delta;
2008 int r;
2009
2010 kvm_x86_ops->cache_regs(vcpu);
2011
2012 if (!io->string) {
2013 if (io->in)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002014 memcpy(&vcpu->arch.regs[VCPU_REGS_RAX], vcpu->arch.pio_data,
Carsten Ottede7d7892007-10-30 18:44:25 +01002015 io->size);
2016 } else {
2017 if (io->in) {
2018 r = pio_copy_data(vcpu);
2019 if (r) {
2020 kvm_x86_ops->cache_regs(vcpu);
2021 return r;
2022 }
2023 }
2024
2025 delta = 1;
2026 if (io->rep) {
2027 delta *= io->cur_count;
2028 /*
2029 * The size of the register should really depend on
2030 * current address size.
2031 */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002032 vcpu->arch.regs[VCPU_REGS_RCX] -= delta;
Carsten Ottede7d7892007-10-30 18:44:25 +01002033 }
2034 if (io->down)
2035 delta = -delta;
2036 delta *= io->size;
2037 if (io->in)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002038 vcpu->arch.regs[VCPU_REGS_RDI] += delta;
Carsten Ottede7d7892007-10-30 18:44:25 +01002039 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002040 vcpu->arch.regs[VCPU_REGS_RSI] += delta;
Carsten Ottede7d7892007-10-30 18:44:25 +01002041 }
2042
2043 kvm_x86_ops->decache_regs(vcpu);
2044
2045 io->count -= io->cur_count;
2046 io->cur_count = 0;
2047
2048 return 0;
2049}
2050
2051static void kernel_pio(struct kvm_io_device *pio_dev,
2052 struct kvm_vcpu *vcpu,
2053 void *pd)
2054{
2055 /* TODO: String I/O for in kernel device */
2056
2057 mutex_lock(&vcpu->kvm->lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002058 if (vcpu->arch.pio.in)
2059 kvm_iodevice_read(pio_dev, vcpu->arch.pio.port,
2060 vcpu->arch.pio.size,
Carsten Ottede7d7892007-10-30 18:44:25 +01002061 pd);
2062 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002063 kvm_iodevice_write(pio_dev, vcpu->arch.pio.port,
2064 vcpu->arch.pio.size,
Carsten Ottede7d7892007-10-30 18:44:25 +01002065 pd);
2066 mutex_unlock(&vcpu->kvm->lock);
2067}
2068
2069static void pio_string_write(struct kvm_io_device *pio_dev,
2070 struct kvm_vcpu *vcpu)
2071{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002072 struct kvm_pio_request *io = &vcpu->arch.pio;
2073 void *pd = vcpu->arch.pio_data;
Carsten Ottede7d7892007-10-30 18:44:25 +01002074 int i;
2075
2076 mutex_lock(&vcpu->kvm->lock);
2077 for (i = 0; i < io->cur_count; i++) {
2078 kvm_iodevice_write(pio_dev, io->port,
2079 io->size,
2080 pd);
2081 pd += io->size;
2082 }
2083 mutex_unlock(&vcpu->kvm->lock);
2084}
2085
2086static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
2087 gpa_t addr)
2088{
2089 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
2090}
2091
2092int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2093 int size, unsigned port)
2094{
2095 struct kvm_io_device *pio_dev;
2096
2097 vcpu->run->exit_reason = KVM_EXIT_IO;
2098 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002099 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01002100 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002101 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
2102 vcpu->run->io.port = vcpu->arch.pio.port = port;
2103 vcpu->arch.pio.in = in;
2104 vcpu->arch.pio.string = 0;
2105 vcpu->arch.pio.down = 0;
2106 vcpu->arch.pio.guest_page_offset = 0;
2107 vcpu->arch.pio.rep = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01002108
2109 kvm_x86_ops->cache_regs(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002110 memcpy(vcpu->arch.pio_data, &vcpu->arch.regs[VCPU_REGS_RAX], 4);
Carsten Ottede7d7892007-10-30 18:44:25 +01002111 kvm_x86_ops->decache_regs(vcpu);
2112
2113 kvm_x86_ops->skip_emulated_instruction(vcpu);
2114
2115 pio_dev = vcpu_find_pio_dev(vcpu, port);
2116 if (pio_dev) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002117 kernel_pio(pio_dev, vcpu, vcpu->arch.pio_data);
Carsten Ottede7d7892007-10-30 18:44:25 +01002118 complete_pio(vcpu);
2119 return 1;
2120 }
2121 return 0;
2122}
2123EXPORT_SYMBOL_GPL(kvm_emulate_pio);
2124
2125int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2126 int size, unsigned long count, int down,
2127 gva_t address, int rep, unsigned port)
2128{
2129 unsigned now, in_page;
2130 int i, ret = 0;
2131 int nr_pages = 1;
2132 struct page *page;
2133 struct kvm_io_device *pio_dev;
2134
2135 vcpu->run->exit_reason = KVM_EXIT_IO;
2136 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002137 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01002138 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002139 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
2140 vcpu->run->io.port = vcpu->arch.pio.port = port;
2141 vcpu->arch.pio.in = in;
2142 vcpu->arch.pio.string = 1;
2143 vcpu->arch.pio.down = down;
2144 vcpu->arch.pio.guest_page_offset = offset_in_page(address);
2145 vcpu->arch.pio.rep = rep;
Carsten Ottede7d7892007-10-30 18:44:25 +01002146
2147 if (!count) {
2148 kvm_x86_ops->skip_emulated_instruction(vcpu);
2149 return 1;
2150 }
2151
2152 if (!down)
2153 in_page = PAGE_SIZE - offset_in_page(address);
2154 else
2155 in_page = offset_in_page(address) + size;
2156 now = min(count, (unsigned long)in_page / size);
2157 if (!now) {
2158 /*
2159 * String I/O straddles page boundary. Pin two guest pages
2160 * so that we satisfy atomicity constraints. Do just one
2161 * transaction to avoid complexity.
2162 */
2163 nr_pages = 2;
2164 now = 1;
2165 }
2166 if (down) {
2167 /*
2168 * String I/O in reverse. Yuck. Kill the guest, fix later.
2169 */
2170 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002171 kvm_inject_gp(vcpu, 0);
Carsten Ottede7d7892007-10-30 18:44:25 +01002172 return 1;
2173 }
2174 vcpu->run->io.count = now;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002175 vcpu->arch.pio.cur_count = now;
Carsten Ottede7d7892007-10-30 18:44:25 +01002176
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002177 if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
Carsten Ottede7d7892007-10-30 18:44:25 +01002178 kvm_x86_ops->skip_emulated_instruction(vcpu);
2179
2180 for (i = 0; i < nr_pages; ++i) {
Izik Eidus72dc67a2008-02-10 18:04:15 +02002181 down_read(&vcpu->kvm->slots_lock);
Carsten Ottede7d7892007-10-30 18:44:25 +01002182 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002183 vcpu->arch.pio.guest_pages[i] = page;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002184 up_read(&vcpu->kvm->slots_lock);
Carsten Ottede7d7892007-10-30 18:44:25 +01002185 if (!page) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002186 kvm_inject_gp(vcpu, 0);
Carsten Ottede7d7892007-10-30 18:44:25 +01002187 free_pio_guest_pages(vcpu);
2188 return 1;
2189 }
2190 }
2191
2192 pio_dev = vcpu_find_pio_dev(vcpu, port);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002193 if (!vcpu->arch.pio.in) {
Carsten Ottede7d7892007-10-30 18:44:25 +01002194 /* string PIO write */
2195 ret = pio_copy_data(vcpu);
2196 if (ret >= 0 && pio_dev) {
2197 pio_string_write(pio_dev, vcpu);
2198 complete_pio(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002199 if (vcpu->arch.pio.count == 0)
Carsten Ottede7d7892007-10-30 18:44:25 +01002200 ret = 1;
2201 }
2202 } else if (pio_dev)
2203 pr_unimpl(vcpu, "no string pio read support yet, "
2204 "port %x size %d count %ld\n",
2205 port, size, count);
2206
2207 return ret;
2208}
2209EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
2210
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002211int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02002212{
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002213 int r;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002214 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
2215
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002216 if (kvm_x86_ops) {
2217 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002218 r = -EEXIST;
2219 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002220 }
2221
2222 if (!ops->cpu_has_kvm_support()) {
2223 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002224 r = -EOPNOTSUPP;
2225 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002226 }
2227 if (ops->disabled_by_bios()) {
2228 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002229 r = -EOPNOTSUPP;
2230 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002231 }
2232
Avi Kivity97db56c2008-01-13 13:23:56 +02002233 r = kvm_mmu_module_init();
2234 if (r)
2235 goto out;
2236
2237 kvm_init_msr_list();
2238
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002239 kvm_x86_ops = ops;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002240 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002241 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002242
2243out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002244 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02002245}
Hollis Blanchard8776e512007-10-31 17:24:24 -05002246
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002247void kvm_arch_exit(void)
2248{
2249 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002250 kvm_mmu_module_exit();
2251}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002252
Hollis Blanchard8776e512007-10-31 17:24:24 -05002253int kvm_emulate_halt(struct kvm_vcpu *vcpu)
2254{
2255 ++vcpu->stat.halt_exits;
2256 if (irqchip_in_kernel(vcpu->kvm)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002257 vcpu->arch.mp_state = VCPU_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002258 kvm_vcpu_block(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002259 if (vcpu->arch.mp_state != VCPU_MP_STATE_RUNNABLE)
Hollis Blanchard8776e512007-10-31 17:24:24 -05002260 return -EINTR;
2261 return 1;
2262 } else {
2263 vcpu->run->exit_reason = KVM_EXIT_HLT;
2264 return 0;
2265 }
2266}
2267EXPORT_SYMBOL_GPL(kvm_emulate_halt);
2268
2269int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
2270{
2271 unsigned long nr, a0, a1, a2, a3, ret;
2272
2273 kvm_x86_ops->cache_regs(vcpu);
2274
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002275 nr = vcpu->arch.regs[VCPU_REGS_RAX];
2276 a0 = vcpu->arch.regs[VCPU_REGS_RBX];
2277 a1 = vcpu->arch.regs[VCPU_REGS_RCX];
2278 a2 = vcpu->arch.regs[VCPU_REGS_RDX];
2279 a3 = vcpu->arch.regs[VCPU_REGS_RSI];
Hollis Blanchard8776e512007-10-31 17:24:24 -05002280
2281 if (!is_long_mode(vcpu)) {
2282 nr &= 0xFFFFFFFF;
2283 a0 &= 0xFFFFFFFF;
2284 a1 &= 0xFFFFFFFF;
2285 a2 &= 0xFFFFFFFF;
2286 a3 &= 0xFFFFFFFF;
2287 }
2288
2289 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02002290 case KVM_HC_VAPIC_POLL_IRQ:
2291 ret = 0;
2292 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002293 default:
2294 ret = -KVM_ENOSYS;
2295 break;
2296 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002297 vcpu->arch.regs[VCPU_REGS_RAX] = ret;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002298 kvm_x86_ops->decache_regs(vcpu);
2299 return 0;
2300}
2301EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
2302
2303int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
2304{
2305 char instruction[3];
2306 int ret = 0;
2307
Hollis Blanchard8776e512007-10-31 17:24:24 -05002308
2309 /*
2310 * Blow out the MMU to ensure that no other VCPU has an active mapping
2311 * to ensure that the updated hypercall appears atomically across all
2312 * VCPUs.
2313 */
2314 kvm_mmu_zap_all(vcpu->kvm);
2315
2316 kvm_x86_ops->cache_regs(vcpu);
2317 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002318 if (emulator_write_emulated(vcpu->arch.rip, instruction, 3, vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05002319 != X86EMUL_CONTINUE)
2320 ret = -EFAULT;
2321
Hollis Blanchard8776e512007-10-31 17:24:24 -05002322 return ret;
2323}
2324
2325static u64 mk_cr_64(u64 curr_cr, u32 new_val)
2326{
2327 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
2328}
2329
2330void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2331{
2332 struct descriptor_table dt = { limit, base };
2333
2334 kvm_x86_ops->set_gdt(vcpu, &dt);
2335}
2336
2337void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2338{
2339 struct descriptor_table dt = { limit, base };
2340
2341 kvm_x86_ops->set_idt(vcpu, &dt);
2342}
2343
2344void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
2345 unsigned long *rflags)
2346{
2347 lmsw(vcpu, msw);
2348 *rflags = kvm_x86_ops->get_rflags(vcpu);
2349}
2350
2351unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
2352{
2353 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2354 switch (cr) {
2355 case 0:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002356 return vcpu->arch.cr0;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002357 case 2:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002358 return vcpu->arch.cr2;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002359 case 3:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002360 return vcpu->arch.cr3;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002361 case 4:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002362 return vcpu->arch.cr4;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01002363 case 8:
2364 return get_cr8(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002365 default:
2366 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
2367 return 0;
2368 }
2369}
2370
2371void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
2372 unsigned long *rflags)
2373{
2374 switch (cr) {
2375 case 0:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002376 set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val));
Hollis Blanchard8776e512007-10-31 17:24:24 -05002377 *rflags = kvm_x86_ops->get_rflags(vcpu);
2378 break;
2379 case 2:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002380 vcpu->arch.cr2 = val;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002381 break;
2382 case 3:
2383 set_cr3(vcpu, val);
2384 break;
2385 case 4:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002386 set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val));
Hollis Blanchard8776e512007-10-31 17:24:24 -05002387 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01002388 case 8:
2389 set_cr8(vcpu, val & 0xfUL);
2390 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002391 default:
2392 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
2393 }
2394}
2395
Dan Kenigsberg07716712007-11-21 17:10:04 +02002396static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
2397{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002398 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
2399 int j, nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02002400
2401 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
2402 /* when no next entry is found, the current entry[i] is reselected */
2403 for (j = i + 1; j == i; j = (j + 1) % nent) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002404 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
Dan Kenigsberg07716712007-11-21 17:10:04 +02002405 if (ej->function == e->function) {
2406 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
2407 return j;
2408 }
2409 }
2410 return 0; /* silence gcc, even though control never reaches here */
2411}
2412
2413/* find an entry with matching function, matching index (if needed), and that
2414 * should be read next (if it's stateful) */
2415static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
2416 u32 function, u32 index)
2417{
2418 if (e->function != function)
2419 return 0;
2420 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
2421 return 0;
2422 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
2423 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
2424 return 0;
2425 return 1;
2426}
2427
Hollis Blanchard8776e512007-10-31 17:24:24 -05002428void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
2429{
2430 int i;
Dan Kenigsberg07716712007-11-21 17:10:04 +02002431 u32 function, index;
2432 struct kvm_cpuid_entry2 *e, *best;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002433
2434 kvm_x86_ops->cache_regs(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002435 function = vcpu->arch.regs[VCPU_REGS_RAX];
2436 index = vcpu->arch.regs[VCPU_REGS_RCX];
2437 vcpu->arch.regs[VCPU_REGS_RAX] = 0;
2438 vcpu->arch.regs[VCPU_REGS_RBX] = 0;
2439 vcpu->arch.regs[VCPU_REGS_RCX] = 0;
2440 vcpu->arch.regs[VCPU_REGS_RDX] = 0;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002441 best = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002442 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
2443 e = &vcpu->arch.cpuid_entries[i];
Dan Kenigsberg07716712007-11-21 17:10:04 +02002444 if (is_matching_cpuid_entry(e, function, index)) {
2445 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
2446 move_to_next_stateful_cpuid_entry(vcpu, i);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002447 best = e;
2448 break;
2449 }
2450 /*
2451 * Both basic or both extended?
2452 */
2453 if (((e->function ^ function) & 0x80000000) == 0)
2454 if (!best || e->function > best->function)
2455 best = e;
2456 }
2457 if (best) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002458 vcpu->arch.regs[VCPU_REGS_RAX] = best->eax;
2459 vcpu->arch.regs[VCPU_REGS_RBX] = best->ebx;
2460 vcpu->arch.regs[VCPU_REGS_RCX] = best->ecx;
2461 vcpu->arch.regs[VCPU_REGS_RDX] = best->edx;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002462 }
2463 kvm_x86_ops->decache_regs(vcpu);
2464 kvm_x86_ops->skip_emulated_instruction(vcpu);
2465}
2466EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
Hollis Blanchardd0752062007-10-31 17:24:25 -05002467
2468/*
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002469 * Check if userspace requested an interrupt window, and that the
2470 * interrupt window is open.
2471 *
2472 * No need to exit to userspace if we already have an interrupt queued.
2473 */
2474static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2475 struct kvm_run *kvm_run)
2476{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002477 return (!vcpu->arch.irq_summary &&
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002478 kvm_run->request_interrupt_window &&
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002479 vcpu->arch.interrupt_window_open &&
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002480 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
2481}
2482
2483static void post_kvm_run_save(struct kvm_vcpu *vcpu,
2484 struct kvm_run *kvm_run)
2485{
2486 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
2487 kvm_run->cr8 = get_cr8(vcpu);
2488 kvm_run->apic_base = kvm_get_apic_base(vcpu);
2489 if (irqchip_in_kernel(vcpu->kvm))
2490 kvm_run->ready_for_interrupt_injection = 1;
2491 else
2492 kvm_run->ready_for_interrupt_injection =
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002493 (vcpu->arch.interrupt_window_open &&
2494 vcpu->arch.irq_summary == 0);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002495}
2496
Avi Kivityb93463a2007-10-25 16:52:32 +02002497static void vapic_enter(struct kvm_vcpu *vcpu)
2498{
2499 struct kvm_lapic *apic = vcpu->arch.apic;
2500 struct page *page;
2501
2502 if (!apic || !apic->vapic_addr)
2503 return;
2504
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002505 down_read(&current->mm->mmap_sem);
Avi Kivityb93463a2007-10-25 16:52:32 +02002506 page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002507 up_read(&current->mm->mmap_sem);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002508
2509 vcpu->arch.apic->vapic_page = page;
Avi Kivityb93463a2007-10-25 16:52:32 +02002510}
2511
2512static void vapic_exit(struct kvm_vcpu *vcpu)
2513{
2514 struct kvm_lapic *apic = vcpu->arch.apic;
2515
2516 if (!apic || !apic->vapic_addr)
2517 return;
2518
2519 kvm_release_page_dirty(apic->vapic_page);
2520 mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
2521}
2522
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002523static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2524{
2525 int r;
2526
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002527 if (unlikely(vcpu->arch.mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002528 pr_debug("vcpu %d received sipi with vector # %x\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002529 vcpu->vcpu_id, vcpu->arch.sipi_vector);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002530 kvm_lapic_reset(vcpu);
2531 r = kvm_x86_ops->vcpu_reset(vcpu);
2532 if (r)
2533 return r;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002534 vcpu->arch.mp_state = VCPU_MP_STATE_RUNNABLE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002535 }
2536
Avi Kivityb93463a2007-10-25 16:52:32 +02002537 vapic_enter(vcpu);
2538
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002539preempted:
2540 if (vcpu->guest_debug.enabled)
2541 kvm_x86_ops->guest_debug_pre(vcpu);
2542
2543again:
2544 r = kvm_mmu_reload(vcpu);
2545 if (unlikely(r))
2546 goto out;
2547
Avi Kivity2f52d582008-01-16 12:49:30 +02002548 if (vcpu->requests) {
2549 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
2550 __kvm_migrate_apic_timer(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02002551 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
2552 &vcpu->requests)) {
2553 kvm_run->exit_reason = KVM_EXIT_TPR_ACCESS;
2554 r = 0;
2555 goto out;
2556 }
Avi Kivity2f52d582008-01-16 12:49:30 +02002557 }
Avi Kivityb93463a2007-10-25 16:52:32 +02002558
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002559 kvm_inject_pending_timer_irqs(vcpu);
2560
2561 preempt_disable();
2562
2563 kvm_x86_ops->prepare_guest_switch(vcpu);
2564 kvm_load_guest_fpu(vcpu);
2565
2566 local_irq_disable();
2567
Avi Kivity6c142802008-01-15 18:27:32 +02002568 if (need_resched()) {
2569 local_irq_enable();
2570 preempt_enable();
2571 r = 1;
2572 goto out;
2573 }
2574
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002575 if (signal_pending(current)) {
2576 local_irq_enable();
2577 preempt_enable();
2578 r = -EINTR;
2579 kvm_run->exit_reason = KVM_EXIT_INTR;
2580 ++vcpu->stat.signal_exits;
2581 goto out;
2582 }
2583
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002584 if (vcpu->arch.exception.pending)
Avi Kivity298101d2007-11-25 13:41:11 +02002585 __queue_exception(vcpu);
2586 else if (irqchip_in_kernel(vcpu->kvm))
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002587 kvm_x86_ops->inject_pending_irq(vcpu);
Avi Kivityeb9774f2007-11-25 17:45:31 +02002588 else
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002589 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2590
Avi Kivityb93463a2007-10-25 16:52:32 +02002591 kvm_lapic_sync_to_vapic(vcpu);
2592
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002593 vcpu->guest_mode = 1;
2594 kvm_guest_enter();
2595
2596 if (vcpu->requests)
2597 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
2598 kvm_x86_ops->tlb_flush(vcpu);
2599
2600 kvm_x86_ops->run(vcpu, kvm_run);
2601
2602 vcpu->guest_mode = 0;
2603 local_irq_enable();
2604
2605 ++vcpu->stat.exits;
2606
2607 /*
2608 * We must have an instruction between local_irq_enable() and
2609 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2610 * the interrupt shadow. The stat.exits increment will do nicely.
2611 * But we need to prevent reordering, hence this barrier():
2612 */
2613 barrier();
2614
2615 kvm_guest_exit();
2616
2617 preempt_enable();
2618
2619 /*
2620 * Profile KVM exit RIPs:
2621 */
2622 if (unlikely(prof_on == KVM_PROFILING)) {
2623 kvm_x86_ops->cache_regs(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002624 profile_hit(KVM_PROFILING, (void *)vcpu->arch.rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002625 }
2626
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002627 if (vcpu->arch.exception.pending && kvm_x86_ops->exception_injected(vcpu))
2628 vcpu->arch.exception.pending = false;
Avi Kivity298101d2007-11-25 13:41:11 +02002629
Avi Kivityb93463a2007-10-25 16:52:32 +02002630 kvm_lapic_sync_from_vapic(vcpu);
2631
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002632 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2633
2634 if (r > 0) {
2635 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2636 r = -EINTR;
2637 kvm_run->exit_reason = KVM_EXIT_INTR;
2638 ++vcpu->stat.request_irq_exits;
2639 goto out;
2640 }
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002641 if (!need_resched())
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002642 goto again;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002643 }
2644
2645out:
2646 if (r > 0) {
2647 kvm_resched(vcpu);
2648 goto preempted;
2649 }
2650
2651 post_kvm_run_save(vcpu, kvm_run);
2652
Avi Kivityb93463a2007-10-25 16:52:32 +02002653 vapic_exit(vcpu);
2654
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002655 return r;
2656}
2657
2658int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2659{
2660 int r;
2661 sigset_t sigsaved;
2662
2663 vcpu_load(vcpu);
2664
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002665 if (unlikely(vcpu->arch.mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002666 kvm_vcpu_block(vcpu);
2667 vcpu_put(vcpu);
2668 return -EAGAIN;
2669 }
2670
2671 if (vcpu->sigset_active)
2672 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2673
2674 /* re-sync apic's tpr */
2675 if (!irqchip_in_kernel(vcpu->kvm))
2676 set_cr8(vcpu, kvm_run->cr8);
2677
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002678 if (vcpu->arch.pio.cur_count) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002679 r = complete_pio(vcpu);
2680 if (r)
2681 goto out;
2682 }
2683#if CONFIG_HAS_IOMEM
2684 if (vcpu->mmio_needed) {
2685 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
2686 vcpu->mmio_read_completed = 1;
2687 vcpu->mmio_needed = 0;
2688 r = emulate_instruction(vcpu, kvm_run,
Sheng Yang571008d2008-01-02 14:49:22 +08002689 vcpu->arch.mmio_fault_cr2, 0,
2690 EMULTYPE_NO_DECODE);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002691 if (r == EMULATE_DO_MMIO) {
2692 /*
2693 * Read-modify-write. Back to userspace.
2694 */
2695 r = 0;
2696 goto out;
2697 }
2698 }
2699#endif
2700 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
2701 kvm_x86_ops->cache_regs(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002702 vcpu->arch.regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002703 kvm_x86_ops->decache_regs(vcpu);
2704 }
2705
2706 r = __vcpu_run(vcpu, kvm_run);
2707
2708out:
2709 if (vcpu->sigset_active)
2710 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2711
2712 vcpu_put(vcpu);
2713 return r;
2714}
2715
2716int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2717{
2718 vcpu_load(vcpu);
2719
2720 kvm_x86_ops->cache_regs(vcpu);
2721
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002722 regs->rax = vcpu->arch.regs[VCPU_REGS_RAX];
2723 regs->rbx = vcpu->arch.regs[VCPU_REGS_RBX];
2724 regs->rcx = vcpu->arch.regs[VCPU_REGS_RCX];
2725 regs->rdx = vcpu->arch.regs[VCPU_REGS_RDX];
2726 regs->rsi = vcpu->arch.regs[VCPU_REGS_RSI];
2727 regs->rdi = vcpu->arch.regs[VCPU_REGS_RDI];
2728 regs->rsp = vcpu->arch.regs[VCPU_REGS_RSP];
2729 regs->rbp = vcpu->arch.regs[VCPU_REGS_RBP];
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002730#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002731 regs->r8 = vcpu->arch.regs[VCPU_REGS_R8];
2732 regs->r9 = vcpu->arch.regs[VCPU_REGS_R9];
2733 regs->r10 = vcpu->arch.regs[VCPU_REGS_R10];
2734 regs->r11 = vcpu->arch.regs[VCPU_REGS_R11];
2735 regs->r12 = vcpu->arch.regs[VCPU_REGS_R12];
2736 regs->r13 = vcpu->arch.regs[VCPU_REGS_R13];
2737 regs->r14 = vcpu->arch.regs[VCPU_REGS_R14];
2738 regs->r15 = vcpu->arch.regs[VCPU_REGS_R15];
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002739#endif
2740
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002741 regs->rip = vcpu->arch.rip;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002742 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
2743
2744 /*
2745 * Don't leak debug flags in case they were set for guest debugging
2746 */
2747 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2748 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2749
2750 vcpu_put(vcpu);
2751
2752 return 0;
2753}
2754
2755int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2756{
2757 vcpu_load(vcpu);
2758
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002759 vcpu->arch.regs[VCPU_REGS_RAX] = regs->rax;
2760 vcpu->arch.regs[VCPU_REGS_RBX] = regs->rbx;
2761 vcpu->arch.regs[VCPU_REGS_RCX] = regs->rcx;
2762 vcpu->arch.regs[VCPU_REGS_RDX] = regs->rdx;
2763 vcpu->arch.regs[VCPU_REGS_RSI] = regs->rsi;
2764 vcpu->arch.regs[VCPU_REGS_RDI] = regs->rdi;
2765 vcpu->arch.regs[VCPU_REGS_RSP] = regs->rsp;
2766 vcpu->arch.regs[VCPU_REGS_RBP] = regs->rbp;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002767#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002768 vcpu->arch.regs[VCPU_REGS_R8] = regs->r8;
2769 vcpu->arch.regs[VCPU_REGS_R9] = regs->r9;
2770 vcpu->arch.regs[VCPU_REGS_R10] = regs->r10;
2771 vcpu->arch.regs[VCPU_REGS_R11] = regs->r11;
2772 vcpu->arch.regs[VCPU_REGS_R12] = regs->r12;
2773 vcpu->arch.regs[VCPU_REGS_R13] = regs->r13;
2774 vcpu->arch.regs[VCPU_REGS_R14] = regs->r14;
2775 vcpu->arch.regs[VCPU_REGS_R15] = regs->r15;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002776#endif
2777
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002778 vcpu->arch.rip = regs->rip;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002779 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
2780
2781 kvm_x86_ops->decache_regs(vcpu);
2782
2783 vcpu_put(vcpu);
2784
2785 return 0;
2786}
2787
2788static void get_segment(struct kvm_vcpu *vcpu,
2789 struct kvm_segment *var, int seg)
2790{
2791 return kvm_x86_ops->get_segment(vcpu, var, seg);
2792}
2793
2794void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2795{
2796 struct kvm_segment cs;
2797
2798 get_segment(vcpu, &cs, VCPU_SREG_CS);
2799 *db = cs.db;
2800 *l = cs.l;
2801}
2802EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2803
2804int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2805 struct kvm_sregs *sregs)
2806{
2807 struct descriptor_table dt;
2808 int pending_vec;
2809
2810 vcpu_load(vcpu);
2811
2812 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2813 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2814 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2815 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2816 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2817 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2818
2819 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2820 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2821
2822 kvm_x86_ops->get_idt(vcpu, &dt);
2823 sregs->idt.limit = dt.limit;
2824 sregs->idt.base = dt.base;
2825 kvm_x86_ops->get_gdt(vcpu, &dt);
2826 sregs->gdt.limit = dt.limit;
2827 sregs->gdt.base = dt.base;
2828
2829 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002830 sregs->cr0 = vcpu->arch.cr0;
2831 sregs->cr2 = vcpu->arch.cr2;
2832 sregs->cr3 = vcpu->arch.cr3;
2833 sregs->cr4 = vcpu->arch.cr4;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002834 sregs->cr8 = get_cr8(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002835 sregs->efer = vcpu->arch.shadow_efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002836 sregs->apic_base = kvm_get_apic_base(vcpu);
2837
2838 if (irqchip_in_kernel(vcpu->kvm)) {
2839 memset(sregs->interrupt_bitmap, 0,
2840 sizeof sregs->interrupt_bitmap);
2841 pending_vec = kvm_x86_ops->get_irq(vcpu);
2842 if (pending_vec >= 0)
2843 set_bit(pending_vec,
2844 (unsigned long *)sregs->interrupt_bitmap);
2845 } else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002846 memcpy(sregs->interrupt_bitmap, vcpu->arch.irq_pending,
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002847 sizeof sregs->interrupt_bitmap);
2848
2849 vcpu_put(vcpu);
2850
2851 return 0;
2852}
2853
2854static void set_segment(struct kvm_vcpu *vcpu,
2855 struct kvm_segment *var, int seg)
2856{
2857 return kvm_x86_ops->set_segment(vcpu, var, seg);
2858}
2859
2860int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2861 struct kvm_sregs *sregs)
2862{
2863 int mmu_reset_needed = 0;
2864 int i, pending_vec, max_bits;
2865 struct descriptor_table dt;
2866
2867 vcpu_load(vcpu);
2868
2869 dt.limit = sregs->idt.limit;
2870 dt.base = sregs->idt.base;
2871 kvm_x86_ops->set_idt(vcpu, &dt);
2872 dt.limit = sregs->gdt.limit;
2873 dt.base = sregs->gdt.base;
2874 kvm_x86_ops->set_gdt(vcpu, &dt);
2875
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002876 vcpu->arch.cr2 = sregs->cr2;
2877 mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
2878 vcpu->arch.cr3 = sregs->cr3;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002879
2880 set_cr8(vcpu, sregs->cr8);
2881
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002882 mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002883#ifdef CONFIG_X86_64
2884 kvm_x86_ops->set_efer(vcpu, sregs->efer);
2885#endif
2886 kvm_set_apic_base(vcpu, sregs->apic_base);
2887
2888 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2889
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002890 mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002891 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00002892 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002893
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002894 mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002895 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
2896 if (!is_long_mode(vcpu) && is_pae(vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002897 load_pdptrs(vcpu, vcpu->arch.cr3);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002898
2899 if (mmu_reset_needed)
2900 kvm_mmu_reset_context(vcpu);
2901
2902 if (!irqchip_in_kernel(vcpu->kvm)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002903 memcpy(vcpu->arch.irq_pending, sregs->interrupt_bitmap,
2904 sizeof vcpu->arch.irq_pending);
2905 vcpu->arch.irq_summary = 0;
2906 for (i = 0; i < ARRAY_SIZE(vcpu->arch.irq_pending); ++i)
2907 if (vcpu->arch.irq_pending[i])
2908 __set_bit(i, &vcpu->arch.irq_summary);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002909 } else {
2910 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
2911 pending_vec = find_first_bit(
2912 (const unsigned long *)sregs->interrupt_bitmap,
2913 max_bits);
2914 /* Only pending external irq is handled here */
2915 if (pending_vec < max_bits) {
2916 kvm_x86_ops->set_irq(vcpu, pending_vec);
2917 pr_debug("Set back pending irq %d\n",
2918 pending_vec);
2919 }
2920 }
2921
2922 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2923 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2924 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2925 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2926 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2927 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2928
2929 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2930 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2931
2932 vcpu_put(vcpu);
2933
2934 return 0;
2935}
2936
2937int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2938 struct kvm_debug_guest *dbg)
2939{
2940 int r;
2941
2942 vcpu_load(vcpu);
2943
2944 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
2945
2946 vcpu_put(vcpu);
2947
2948 return r;
2949}
2950
2951/*
Hollis Blanchardd0752062007-10-31 17:24:25 -05002952 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2953 * we have asm/x86/processor.h
2954 */
2955struct fxsave {
2956 u16 cwd;
2957 u16 swd;
2958 u16 twd;
2959 u16 fop;
2960 u64 rip;
2961 u64 rdp;
2962 u32 mxcsr;
2963 u32 mxcsr_mask;
2964 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2965#ifdef CONFIG_X86_64
2966 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2967#else
2968 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2969#endif
2970};
2971
Zhang Xiantao8b006792007-11-16 13:05:55 +08002972/*
2973 * Translate a guest virtual address to a guest physical address.
2974 */
2975int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2976 struct kvm_translation *tr)
2977{
2978 unsigned long vaddr = tr->linear_address;
2979 gpa_t gpa;
2980
2981 vcpu_load(vcpu);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002982 down_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002983 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002984 up_read(&vcpu->kvm->slots_lock);
Zhang Xiantao8b006792007-11-16 13:05:55 +08002985 tr->physical_address = gpa;
2986 tr->valid = gpa != UNMAPPED_GVA;
2987 tr->writeable = 1;
2988 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08002989 vcpu_put(vcpu);
2990
2991 return 0;
2992}
2993
Hollis Blanchardd0752062007-10-31 17:24:25 -05002994int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2995{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002996 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05002997
2998 vcpu_load(vcpu);
2999
3000 memcpy(fpu->fpr, fxsave->st_space, 128);
3001 fpu->fcw = fxsave->cwd;
3002 fpu->fsw = fxsave->swd;
3003 fpu->ftwx = fxsave->twd;
3004 fpu->last_opcode = fxsave->fop;
3005 fpu->last_ip = fxsave->rip;
3006 fpu->last_dp = fxsave->rdp;
3007 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
3008
3009 vcpu_put(vcpu);
3010
3011 return 0;
3012}
3013
3014int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3015{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003016 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05003017
3018 vcpu_load(vcpu);
3019
3020 memcpy(fxsave->st_space, fpu->fpr, 128);
3021 fxsave->cwd = fpu->fcw;
3022 fxsave->swd = fpu->fsw;
3023 fxsave->twd = fpu->ftwx;
3024 fxsave->fop = fpu->last_opcode;
3025 fxsave->rip = fpu->last_ip;
3026 fxsave->rdp = fpu->last_dp;
3027 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
3028
3029 vcpu_put(vcpu);
3030
3031 return 0;
3032}
3033
3034void fx_init(struct kvm_vcpu *vcpu)
3035{
3036 unsigned after_mxcsr_mask;
3037
3038 /* Initialize guest FPU by resetting ours and saving into guest's */
3039 preempt_disable();
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003040 fx_save(&vcpu->arch.host_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05003041 fpu_init();
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003042 fx_save(&vcpu->arch.guest_fx_image);
3043 fx_restore(&vcpu->arch.host_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05003044 preempt_enable();
3045
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003046 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05003047 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003048 vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
3049 memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
Hollis Blanchardd0752062007-10-31 17:24:25 -05003050 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
3051}
3052EXPORT_SYMBOL_GPL(fx_init);
3053
3054void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
3055{
3056 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
3057 return;
3058
3059 vcpu->guest_fpu_loaded = 1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003060 fx_save(&vcpu->arch.host_fx_image);
3061 fx_restore(&vcpu->arch.guest_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05003062}
3063EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
3064
3065void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
3066{
3067 if (!vcpu->guest_fpu_loaded)
3068 return;
3069
3070 vcpu->guest_fpu_loaded = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003071 fx_save(&vcpu->arch.guest_fx_image);
3072 fx_restore(&vcpu->arch.host_fx_image);
Avi Kivityf096ed82007-11-18 13:54:33 +02003073 ++vcpu->stat.fpu_reload;
Hollis Blanchardd0752062007-10-31 17:24:25 -05003074}
3075EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003076
3077void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
3078{
3079 kvm_x86_ops->vcpu_free(vcpu);
3080}
3081
3082struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
3083 unsigned int id)
3084{
Avi Kivity26e52152007-11-20 15:30:24 +02003085 return kvm_x86_ops->vcpu_create(kvm, id);
3086}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003087
Avi Kivity26e52152007-11-20 15:30:24 +02003088int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
3089{
3090 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003091
3092 /* We do fxsave: this must be aligned. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003093 BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003094
3095 vcpu_load(vcpu);
3096 r = kvm_arch_vcpu_reset(vcpu);
3097 if (r == 0)
3098 r = kvm_mmu_setup(vcpu);
3099 vcpu_put(vcpu);
3100 if (r < 0)
3101 goto free_vcpu;
3102
Avi Kivity26e52152007-11-20 15:30:24 +02003103 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003104free_vcpu:
3105 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivity26e52152007-11-20 15:30:24 +02003106 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003107}
3108
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06003109void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003110{
3111 vcpu_load(vcpu);
3112 kvm_mmu_unload(vcpu);
3113 vcpu_put(vcpu);
3114
3115 kvm_x86_ops->vcpu_free(vcpu);
3116}
3117
3118int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
3119{
3120 return kvm_x86_ops->vcpu_reset(vcpu);
3121}
3122
3123void kvm_arch_hardware_enable(void *garbage)
3124{
3125 kvm_x86_ops->hardware_enable(garbage);
3126}
3127
3128void kvm_arch_hardware_disable(void *garbage)
3129{
3130 kvm_x86_ops->hardware_disable(garbage);
3131}
3132
3133int kvm_arch_hardware_setup(void)
3134{
3135 return kvm_x86_ops->hardware_setup();
3136}
3137
3138void kvm_arch_hardware_unsetup(void)
3139{
3140 kvm_x86_ops->hardware_unsetup();
3141}
3142
3143void kvm_arch_check_processor_compat(void *rtn)
3144{
3145 kvm_x86_ops->check_processor_compatibility(rtn);
3146}
3147
3148int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
3149{
3150 struct page *page;
3151 struct kvm *kvm;
3152 int r;
3153
3154 BUG_ON(vcpu->kvm == NULL);
3155 kvm = vcpu->kvm;
3156
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003157 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003158 if (!irqchip_in_kernel(kvm) || vcpu->vcpu_id == 0)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003159 vcpu->arch.mp_state = VCPU_MP_STATE_RUNNABLE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003160 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003161 vcpu->arch.mp_state = VCPU_MP_STATE_UNINITIALIZED;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003162
3163 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
3164 if (!page) {
3165 r = -ENOMEM;
3166 goto fail;
3167 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003168 vcpu->arch.pio_data = page_address(page);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003169
3170 r = kvm_mmu_create(vcpu);
3171 if (r < 0)
3172 goto fail_free_pio_data;
3173
3174 if (irqchip_in_kernel(kvm)) {
3175 r = kvm_create_lapic(vcpu);
3176 if (r < 0)
3177 goto fail_mmu_destroy;
3178 }
3179
3180 return 0;
3181
3182fail_mmu_destroy:
3183 kvm_mmu_destroy(vcpu);
3184fail_free_pio_data:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003185 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003186fail:
3187 return r;
3188}
3189
3190void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
3191{
3192 kvm_free_lapic(vcpu);
3193 kvm_mmu_destroy(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003194 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003195}
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08003196
3197struct kvm *kvm_arch_create_vm(void)
3198{
3199 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
3200
3201 if (!kvm)
3202 return ERR_PTR(-ENOMEM);
3203
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003204 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08003205
3206 return kvm;
3207}
3208
3209static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
3210{
3211 vcpu_load(vcpu);
3212 kvm_mmu_unload(vcpu);
3213 vcpu_put(vcpu);
3214}
3215
3216static void kvm_free_vcpus(struct kvm *kvm)
3217{
3218 unsigned int i;
3219
3220 /*
3221 * Unpin any mmu pages first.
3222 */
3223 for (i = 0; i < KVM_MAX_VCPUS; ++i)
3224 if (kvm->vcpus[i])
3225 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
3226 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
3227 if (kvm->vcpus[i]) {
3228 kvm_arch_vcpu_free(kvm->vcpus[i]);
3229 kvm->vcpus[i] = NULL;
3230 }
3231 }
3232
3233}
3234
3235void kvm_arch_destroy_vm(struct kvm *kvm)
3236{
Zhang Xiantaod7deeeb2007-12-14 10:17:34 +08003237 kfree(kvm->arch.vpic);
3238 kfree(kvm->arch.vioapic);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08003239 kvm_free_vcpus(kvm);
3240 kvm_free_physmem(kvm);
3241 kfree(kvm);
3242}
Zhang Xiantao0de10342007-11-20 16:25:04 +08003243
3244int kvm_arch_set_memory_region(struct kvm *kvm,
3245 struct kvm_userspace_memory_region *mem,
3246 struct kvm_memory_slot old,
3247 int user_alloc)
3248{
3249 int npages = mem->memory_size >> PAGE_SHIFT;
3250 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
3251
3252 /*To keep backward compatibility with older userspace,
3253 *x86 needs to hanlde !user_alloc case.
3254 */
3255 if (!user_alloc) {
3256 if (npages && !old.rmap) {
Izik Eidus72dc67a2008-02-10 18:04:15 +02003257 down_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08003258 memslot->userspace_addr = do_mmap(NULL, 0,
3259 npages * PAGE_SIZE,
3260 PROT_READ | PROT_WRITE,
3261 MAP_SHARED | MAP_ANONYMOUS,
3262 0);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003263 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08003264
3265 if (IS_ERR((void *)memslot->userspace_addr))
3266 return PTR_ERR((void *)memslot->userspace_addr);
3267 } else {
3268 if (!old.user_alloc && old.rmap) {
3269 int ret;
3270
Izik Eidus72dc67a2008-02-10 18:04:15 +02003271 down_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08003272 ret = do_munmap(current->mm, old.userspace_addr,
3273 old.npages * PAGE_SIZE);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003274 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08003275 if (ret < 0)
3276 printk(KERN_WARNING
3277 "kvm_vm_ioctl_set_memory_region: "
3278 "failed to munmap memory\n");
3279 }
3280 }
3281 }
3282
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003283 if (!kvm->arch.n_requested_mmu_pages) {
Zhang Xiantao0de10342007-11-20 16:25:04 +08003284 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
3285 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
3286 }
3287
3288 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
3289 kvm_flush_remote_tlbs(kvm);
3290
3291 return 0;
3292}
Zhang Xiantao1d737c82007-12-14 09:35:10 +08003293
3294int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
3295{
3296 return vcpu->arch.mp_state == VCPU_MP_STATE_RUNNABLE
3297 || vcpu->arch.mp_state == VCPU_MP_STATE_SIPI_RECEIVED;
3298}
Zhang Xiantao57361992007-12-17 14:21:40 +08003299
3300static void vcpu_kick_intr(void *info)
3301{
3302#ifdef DEBUG
3303 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info;
3304 printk(KERN_DEBUG "vcpu_kick_intr %p \n", vcpu);
3305#endif
3306}
3307
3308void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
3309{
3310 int ipi_pcpu = vcpu->cpu;
3311
3312 if (waitqueue_active(&vcpu->wq)) {
3313 wake_up_interruptible(&vcpu->wq);
3314 ++vcpu->stat.halt_wakeup;
3315 }
3316 if (vcpu->guest_mode)
3317 smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0, 0);
3318}