blob: 0c910c774a9bdbbff74fac4c7fd27ec200bc33df [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}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100216EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100217
Avi Kivityd835dfe2007-11-21 02:57:59 +0200218static bool pdptrs_changed(struct kvm_vcpu *vcpu)
219{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800220 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Avi Kivityd835dfe2007-11-21 02:57:59 +0200221 bool changed = true;
222 int r;
223
224 if (is_long_mode(vcpu) || !is_pae(vcpu))
225 return false;
226
Izik Eidus72dc67a2008-02-10 18:04:15 +0200227 down_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800228 r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
Avi Kivityd835dfe2007-11-21 02:57:59 +0200229 if (r < 0)
230 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800231 changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200232out:
Izik Eidus72dc67a2008-02-10 18:04:15 +0200233 up_read(&vcpu->kvm->slots_lock);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200234
235 return changed;
236}
237
Carsten Ottea03490e2007-10-29 16:09:35 +0100238void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
239{
240 if (cr0 & CR0_RESERVED_BITS) {
241 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800242 cr0, vcpu->arch.cr0);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200243 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100244 return;
245 }
246
247 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
248 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200249 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100250 return;
251 }
252
253 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
254 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
255 "and a clear PE flag\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200256 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100257 return;
258 }
259
260 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
261#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800262 if ((vcpu->arch.shadow_efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100263 int cs_db, cs_l;
264
265 if (!is_pae(vcpu)) {
266 printk(KERN_DEBUG "set_cr0: #GP, start paging "
267 "in long mode while PAE is disabled\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200268 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100269 return;
270 }
271 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
272 if (cs_l) {
273 printk(KERN_DEBUG "set_cr0: #GP, start paging "
274 "in long mode while CS.L == 1\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200275 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100276 return;
277
278 }
279 } else
280#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800281 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100282 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
283 "reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200284 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100285 return;
286 }
287
288 }
289
290 kvm_x86_ops->set_cr0(vcpu, cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800291 vcpu->arch.cr0 = cr0;
Carsten Ottea03490e2007-10-29 16:09:35 +0100292
Carsten Ottea03490e2007-10-29 16:09:35 +0100293 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100294 return;
295}
296EXPORT_SYMBOL_GPL(set_cr0);
297
298void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
299{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800300 set_cr0(vcpu, (vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100301}
302EXPORT_SYMBOL_GPL(lmsw);
303
304void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
305{
306 if (cr4 & CR4_RESERVED_BITS) {
307 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200308 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100309 return;
310 }
311
312 if (is_long_mode(vcpu)) {
313 if (!(cr4 & X86_CR4_PAE)) {
314 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
315 "in long mode\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200316 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100317 return;
318 }
319 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800320 && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100321 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200322 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100323 return;
324 }
325
326 if (cr4 & X86_CR4_VMXE) {
327 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200328 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100329 return;
330 }
331 kvm_x86_ops->set_cr4(vcpu, cr4);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800332 vcpu->arch.cr4 = cr4;
Carsten Ottea03490e2007-10-29 16:09:35 +0100333 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100334}
335EXPORT_SYMBOL_GPL(set_cr4);
336
337void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
338{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800339 if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
Avi Kivityd835dfe2007-11-21 02:57:59 +0200340 kvm_mmu_flush_tlb(vcpu);
341 return;
342 }
343
Carsten Ottea03490e2007-10-29 16:09:35 +0100344 if (is_long_mode(vcpu)) {
345 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
346 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200347 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100348 return;
349 }
350 } else {
351 if (is_pae(vcpu)) {
352 if (cr3 & CR3_PAE_RESERVED_BITS) {
353 printk(KERN_DEBUG
354 "set_cr3: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200355 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100356 return;
357 }
358 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
359 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
360 "reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200361 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100362 return;
363 }
364 }
365 /*
366 * We don't check reserved bits in nonpae mode, because
367 * this isn't enforced, and VMware depends on this.
368 */
369 }
370
Izik Eidus72dc67a2008-02-10 18:04:15 +0200371 down_read(&vcpu->kvm->slots_lock);
Carsten Ottea03490e2007-10-29 16:09:35 +0100372 /*
373 * Does the new cr3 value map to physical memory? (Note, we
374 * catch an invalid cr3 even in real-mode, because it would
375 * cause trouble later on when we turn on paging anyway.)
376 *
377 * A real CPU would silently accept an invalid cr3 and would
378 * attempt to use it - with largely undefined (and often hard
379 * to debug) behavior on the guest side.
380 */
381 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200382 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100383 else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800384 vcpu->arch.cr3 = cr3;
385 vcpu->arch.mmu.new_cr3(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100386 }
Izik Eidus72dc67a2008-02-10 18:04:15 +0200387 up_read(&vcpu->kvm->slots_lock);
Carsten Ottea03490e2007-10-29 16:09:35 +0100388}
389EXPORT_SYMBOL_GPL(set_cr3);
390
391void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
392{
393 if (cr8 & CR8_RESERVED_BITS) {
394 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200395 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100396 return;
397 }
398 if (irqchip_in_kernel(vcpu->kvm))
399 kvm_lapic_set_tpr(vcpu, cr8);
400 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800401 vcpu->arch.cr8 = cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100402}
403EXPORT_SYMBOL_GPL(set_cr8);
404
405unsigned long get_cr8(struct kvm_vcpu *vcpu)
406{
407 if (irqchip_in_kernel(vcpu->kvm))
408 return kvm_lapic_get_cr8(vcpu);
409 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800410 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100411}
412EXPORT_SYMBOL_GPL(get_cr8);
413
Carsten Otte043405e2007-10-10 17:16:19 +0200414/*
415 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
416 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
417 *
418 * This list is modified at module load time to reflect the
419 * capabilities of the host cpu.
420 */
421static u32 msrs_to_save[] = {
422 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
423 MSR_K6_STAR,
424#ifdef CONFIG_X86_64
425 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
426#endif
427 MSR_IA32_TIME_STAMP_COUNTER,
428};
429
430static unsigned num_msrs_to_save;
431
432static u32 emulated_msrs[] = {
433 MSR_IA32_MISC_ENABLE,
434};
435
Carsten Otte15c4a642007-10-30 18:44:17 +0100436static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
437{
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100438 if (efer & efer_reserved_bits) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100439 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
440 efer);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200441 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100442 return;
443 }
444
445 if (is_paging(vcpu)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800446 && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100447 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200448 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100449 return;
450 }
451
452 kvm_x86_ops->set_efer(vcpu, efer);
453
454 efer &= ~EFER_LMA;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800455 efer |= vcpu->arch.shadow_efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +0100456
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800457 vcpu->arch.shadow_efer = efer;
Carsten Otte15c4a642007-10-30 18:44:17 +0100458}
459
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100460void kvm_enable_efer_bits(u64 mask)
461{
462 efer_reserved_bits &= ~mask;
463}
464EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
465
466
Carsten Otte15c4a642007-10-30 18:44:17 +0100467/*
468 * Writes msr value into into the appropriate "register".
469 * Returns 0 on success, non-0 otherwise.
470 * Assumes vcpu_load() was already called.
471 */
472int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
473{
474 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
475}
476
Carsten Otte313a3dc2007-10-11 19:16:52 +0200477/*
478 * Adapt set_msr() to msr_io()'s calling convention
479 */
480static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
481{
482 return kvm_set_msr(vcpu, index, *data);
483}
484
Carsten Otte15c4a642007-10-30 18:44:17 +0100485
486int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
487{
488 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100489 case MSR_EFER:
490 set_efer(vcpu, data);
491 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100492 case MSR_IA32_MC0_STATUS:
493 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
494 __FUNCTION__, data);
495 break;
496 case MSR_IA32_MCG_STATUS:
497 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
498 __FUNCTION__, data);
499 break;
Joerg Roedelc7ac6792008-02-11 20:28:27 +0100500 case MSR_IA32_MCG_CTL:
501 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_CTL 0x%llx, nop\n",
502 __FUNCTION__, data);
503 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100504 case MSR_IA32_UCODE_REV:
505 case MSR_IA32_UCODE_WRITE:
506 case 0x200 ... 0x2ff: /* MTRRs */
507 break;
508 case MSR_IA32_APICBASE:
509 kvm_set_apic_base(vcpu, data);
510 break;
511 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800512 vcpu->arch.ia32_misc_enable_msr = data;
Carsten Otte15c4a642007-10-30 18:44:17 +0100513 break;
514 default:
Avi Kivity565f1fb2007-12-19 12:02:40 +0200515 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n", msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +0100516 return 1;
517 }
518 return 0;
519}
520EXPORT_SYMBOL_GPL(kvm_set_msr_common);
521
522
523/*
524 * Reads an msr value (of 'msr_index') into 'pdata'.
525 * Returns 0 on success, non-0 otherwise.
526 * Assumes vcpu_load() was already called.
527 */
528int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
529{
530 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
531}
532
533int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
534{
535 u64 data;
536
537 switch (msr) {
538 case 0xc0010010: /* SYSCFG */
539 case 0xc0010015: /* HWCR */
540 case MSR_IA32_PLATFORM_ID:
541 case MSR_IA32_P5_MC_ADDR:
542 case MSR_IA32_P5_MC_TYPE:
543 case MSR_IA32_MC0_CTL:
544 case MSR_IA32_MCG_STATUS:
545 case MSR_IA32_MCG_CAP:
Joerg Roedelc7ac6792008-02-11 20:28:27 +0100546 case MSR_IA32_MCG_CTL:
Carsten Otte15c4a642007-10-30 18:44:17 +0100547 case MSR_IA32_MC0_MISC:
548 case MSR_IA32_MC0_MISC+4:
549 case MSR_IA32_MC0_MISC+8:
550 case MSR_IA32_MC0_MISC+12:
551 case MSR_IA32_MC0_MISC+16:
552 case MSR_IA32_UCODE_REV:
553 case MSR_IA32_PERF_STATUS:
554 case MSR_IA32_EBL_CR_POWERON:
555 /* MTRR registers */
556 case 0xfe:
557 case 0x200 ... 0x2ff:
558 data = 0;
559 break;
560 case 0xcd: /* fsb frequency */
561 data = 3;
562 break;
563 case MSR_IA32_APICBASE:
564 data = kvm_get_apic_base(vcpu);
565 break;
566 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800567 data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +0100568 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100569 case MSR_EFER:
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800570 data = vcpu->arch.shadow_efer;
Carsten Otte15c4a642007-10-30 18:44:17 +0100571 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100572 default:
573 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
574 return 1;
575 }
576 *pdata = data;
577 return 0;
578}
579EXPORT_SYMBOL_GPL(kvm_get_msr_common);
580
Carsten Otte313a3dc2007-10-11 19:16:52 +0200581/*
582 * Read or write a bunch of msrs. All parameters are kernel addresses.
583 *
584 * @return number of msrs set successfully.
585 */
586static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
587 struct kvm_msr_entry *entries,
588 int (*do_msr)(struct kvm_vcpu *vcpu,
589 unsigned index, u64 *data))
590{
591 int i;
592
593 vcpu_load(vcpu);
594
595 for (i = 0; i < msrs->nmsrs; ++i)
596 if (do_msr(vcpu, entries[i].index, &entries[i].data))
597 break;
598
599 vcpu_put(vcpu);
600
601 return i;
602}
603
604/*
605 * Read or write a bunch of msrs. Parameters are user addresses.
606 *
607 * @return number of msrs set successfully.
608 */
609static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
610 int (*do_msr)(struct kvm_vcpu *vcpu,
611 unsigned index, u64 *data),
612 int writeback)
613{
614 struct kvm_msrs msrs;
615 struct kvm_msr_entry *entries;
616 int r, n;
617 unsigned size;
618
619 r = -EFAULT;
620 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
621 goto out;
622
623 r = -E2BIG;
624 if (msrs.nmsrs >= MAX_IO_MSRS)
625 goto out;
626
627 r = -ENOMEM;
628 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
629 entries = vmalloc(size);
630 if (!entries)
631 goto out;
632
633 r = -EFAULT;
634 if (copy_from_user(entries, user_msrs->entries, size))
635 goto out_free;
636
637 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
638 if (r < 0)
639 goto out_free;
640
641 r = -EFAULT;
642 if (writeback && copy_to_user(user_msrs->entries, entries, size))
643 goto out_free;
644
645 r = n;
646
647out_free:
648 vfree(entries);
649out:
650 return r;
651}
652
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800653/*
654 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
655 * cached on it.
656 */
657void decache_vcpus_on_cpu(int cpu)
658{
659 struct kvm *vm;
660 struct kvm_vcpu *vcpu;
661 int i;
662
663 spin_lock(&kvm_lock);
664 list_for_each_entry(vm, &vm_list, vm_list)
665 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
666 vcpu = vm->vcpus[i];
667 if (!vcpu)
668 continue;
669 /*
670 * If the vcpu is locked, then it is running on some
671 * other cpu and therefore it is not cached on the
672 * cpu in question.
673 *
674 * If it's not locked, check the last cpu it executed
675 * on.
676 */
677 if (mutex_trylock(&vcpu->mutex)) {
678 if (vcpu->cpu == cpu) {
679 kvm_x86_ops->vcpu_decache(vcpu);
680 vcpu->cpu = -1;
681 }
682 mutex_unlock(&vcpu->mutex);
683 }
684 }
685 spin_unlock(&kvm_lock);
686}
687
Zhang Xiantao018d00d2007-11-15 23:07:47 +0800688int kvm_dev_ioctl_check_extension(long ext)
689{
690 int r;
691
692 switch (ext) {
693 case KVM_CAP_IRQCHIP:
694 case KVM_CAP_HLT:
695 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
696 case KVM_CAP_USER_MEMORY:
697 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +0200698 case KVM_CAP_EXT_CPUID:
Zhang Xiantao018d00d2007-11-15 23:07:47 +0800699 r = 1;
700 break;
Avi Kivity774ead32007-12-26 13:57:04 +0200701 case KVM_CAP_VAPIC:
702 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
703 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +0800704 default:
705 r = 0;
706 break;
707 }
708 return r;
709
710}
711
Carsten Otte043405e2007-10-10 17:16:19 +0200712long kvm_arch_dev_ioctl(struct file *filp,
713 unsigned int ioctl, unsigned long arg)
714{
715 void __user *argp = (void __user *)arg;
716 long r;
717
718 switch (ioctl) {
719 case KVM_GET_MSR_INDEX_LIST: {
720 struct kvm_msr_list __user *user_msr_list = argp;
721 struct kvm_msr_list msr_list;
722 unsigned n;
723
724 r = -EFAULT;
725 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
726 goto out;
727 n = msr_list.nmsrs;
728 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
729 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
730 goto out;
731 r = -E2BIG;
732 if (n < num_msrs_to_save)
733 goto out;
734 r = -EFAULT;
735 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
736 num_msrs_to_save * sizeof(u32)))
737 goto out;
738 if (copy_to_user(user_msr_list->indices
739 + num_msrs_to_save * sizeof(u32),
740 &emulated_msrs,
741 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
742 goto out;
743 r = 0;
744 break;
745 }
Avi Kivity674eea02008-02-11 18:37:23 +0200746 case KVM_GET_SUPPORTED_CPUID: {
747 struct kvm_cpuid2 __user *cpuid_arg = argp;
748 struct kvm_cpuid2 cpuid;
749
750 r = -EFAULT;
751 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
752 goto out;
753 r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
754 cpuid_arg->entries);
755 if (r)
756 goto out;
757
758 r = -EFAULT;
759 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
760 goto out;
761 r = 0;
762 break;
763 }
Carsten Otte043405e2007-10-10 17:16:19 +0200764 default:
765 r = -EINVAL;
766 }
767out:
768 return r;
769}
770
Carsten Otte313a3dc2007-10-11 19:16:52 +0200771void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
772{
773 kvm_x86_ops->vcpu_load(vcpu, cpu);
774}
775
776void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
777{
778 kvm_x86_ops->vcpu_put(vcpu);
Amit Shah9327fd12007-11-15 18:38:46 +0200779 kvm_put_guest_fpu(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200780}
781
Dan Kenigsberg07716712007-11-21 17:10:04 +0200782static int is_efer_nx(void)
Carsten Otte313a3dc2007-10-11 19:16:52 +0200783{
784 u64 efer;
Carsten Otte313a3dc2007-10-11 19:16:52 +0200785
786 rdmsrl(MSR_EFER, efer);
Dan Kenigsberg07716712007-11-21 17:10:04 +0200787 return efer & EFER_NX;
788}
789
790static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
791{
792 int i;
793 struct kvm_cpuid_entry2 *e, *entry;
794
Carsten Otte313a3dc2007-10-11 19:16:52 +0200795 entry = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800796 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
797 e = &vcpu->arch.cpuid_entries[i];
Carsten Otte313a3dc2007-10-11 19:16:52 +0200798 if (e->function == 0x80000001) {
799 entry = e;
800 break;
801 }
802 }
Dan Kenigsberg07716712007-11-21 17:10:04 +0200803 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
Carsten Otte313a3dc2007-10-11 19:16:52 +0200804 entry->edx &= ~(1 << 20);
805 printk(KERN_INFO "kvm: guest NX capability removed\n");
806 }
807}
808
Dan Kenigsberg07716712007-11-21 17:10:04 +0200809/* when an old userspace process fills a new kernel module */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200810static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
811 struct kvm_cpuid *cpuid,
812 struct kvm_cpuid_entry __user *entries)
813{
Dan Kenigsberg07716712007-11-21 17:10:04 +0200814 int r, i;
815 struct kvm_cpuid_entry *cpuid_entries;
816
817 r = -E2BIG;
818 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
819 goto out;
820 r = -ENOMEM;
821 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
822 if (!cpuid_entries)
823 goto out;
824 r = -EFAULT;
825 if (copy_from_user(cpuid_entries, entries,
826 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
827 goto out_free;
828 for (i = 0; i < cpuid->nent; i++) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800829 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
830 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
831 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
832 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
833 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
834 vcpu->arch.cpuid_entries[i].index = 0;
835 vcpu->arch.cpuid_entries[i].flags = 0;
836 vcpu->arch.cpuid_entries[i].padding[0] = 0;
837 vcpu->arch.cpuid_entries[i].padding[1] = 0;
838 vcpu->arch.cpuid_entries[i].padding[2] = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +0200839 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800840 vcpu->arch.cpuid_nent = cpuid->nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +0200841 cpuid_fix_nx_cap(vcpu);
842 r = 0;
843
844out_free:
845 vfree(cpuid_entries);
846out:
847 return r;
848}
849
850static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
851 struct kvm_cpuid2 *cpuid,
852 struct kvm_cpuid_entry2 __user *entries)
853{
Carsten Otte313a3dc2007-10-11 19:16:52 +0200854 int r;
855
856 r = -E2BIG;
857 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
858 goto out;
859 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800860 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
Dan Kenigsberg07716712007-11-21 17:10:04 +0200861 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
Carsten Otte313a3dc2007-10-11 19:16:52 +0200862 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800863 vcpu->arch.cpuid_nent = cpuid->nent;
Carsten Otte313a3dc2007-10-11 19:16:52 +0200864 return 0;
865
866out:
867 return r;
868}
869
Dan Kenigsberg07716712007-11-21 17:10:04 +0200870static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
871 struct kvm_cpuid2 *cpuid,
872 struct kvm_cpuid_entry2 __user *entries)
873{
874 int r;
875
876 r = -E2BIG;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800877 if (cpuid->nent < vcpu->arch.cpuid_nent)
Dan Kenigsberg07716712007-11-21 17:10:04 +0200878 goto out;
879 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800880 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
881 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +0200882 goto out;
883 return 0;
884
885out:
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800886 cpuid->nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +0200887 return r;
888}
889
890static inline u32 bit(int bitno)
891{
892 return 1 << (bitno & 31);
893}
894
895static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
896 u32 index)
897{
898 entry->function = function;
899 entry->index = index;
900 cpuid_count(entry->function, entry->index,
901 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
902 entry->flags = 0;
903}
904
905static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
906 u32 index, int *nent, int maxnent)
907{
908 const u32 kvm_supported_word0_x86_features = bit(X86_FEATURE_FPU) |
909 bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
910 bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
911 bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
912 bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
913 bit(X86_FEATURE_SEP) | bit(X86_FEATURE_PGE) |
914 bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
915 bit(X86_FEATURE_CLFLSH) | bit(X86_FEATURE_MMX) |
916 bit(X86_FEATURE_FXSR) | bit(X86_FEATURE_XMM) |
917 bit(X86_FEATURE_XMM2) | bit(X86_FEATURE_SELFSNOOP);
918 const u32 kvm_supported_word1_x86_features = bit(X86_FEATURE_FPU) |
919 bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
920 bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
921 bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
922 bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
923 bit(X86_FEATURE_PGE) |
924 bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
925 bit(X86_FEATURE_MMX) | bit(X86_FEATURE_FXSR) |
926 bit(X86_FEATURE_SYSCALL) |
927 (bit(X86_FEATURE_NX) && is_efer_nx()) |
928#ifdef CONFIG_X86_64
929 bit(X86_FEATURE_LM) |
930#endif
931 bit(X86_FEATURE_MMXEXT) |
932 bit(X86_FEATURE_3DNOWEXT) |
933 bit(X86_FEATURE_3DNOW);
934 const u32 kvm_supported_word3_x86_features =
935 bit(X86_FEATURE_XMM3) | bit(X86_FEATURE_CX16);
936 const u32 kvm_supported_word6_x86_features =
937 bit(X86_FEATURE_LAHF_LM) | bit(X86_FEATURE_CMP_LEGACY);
938
939 /* all func 2 cpuid_count() should be called on the same cpu */
940 get_cpu();
941 do_cpuid_1_ent(entry, function, index);
942 ++*nent;
943
944 switch (function) {
945 case 0:
946 entry->eax = min(entry->eax, (u32)0xb);
947 break;
948 case 1:
949 entry->edx &= kvm_supported_word0_x86_features;
950 entry->ecx &= kvm_supported_word3_x86_features;
951 break;
952 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
953 * may return different values. This forces us to get_cpu() before
954 * issuing the first command, and also to emulate this annoying behavior
955 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
956 case 2: {
957 int t, times = entry->eax & 0xff;
958
959 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
960 for (t = 1; t < times && *nent < maxnent; ++t) {
961 do_cpuid_1_ent(&entry[t], function, 0);
962 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
963 ++*nent;
964 }
965 break;
966 }
967 /* function 4 and 0xb have additional index. */
968 case 4: {
969 int index, cache_type;
970
971 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
972 /* read more entries until cache_type is zero */
973 for (index = 1; *nent < maxnent; ++index) {
974 cache_type = entry[index - 1].eax & 0x1f;
975 if (!cache_type)
976 break;
977 do_cpuid_1_ent(&entry[index], function, index);
978 entry[index].flags |=
979 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
980 ++*nent;
981 }
982 break;
983 }
984 case 0xb: {
985 int index, level_type;
986
987 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
988 /* read more entries until level_type is zero */
989 for (index = 1; *nent < maxnent; ++index) {
990 level_type = entry[index - 1].ecx & 0xff;
991 if (!level_type)
992 break;
993 do_cpuid_1_ent(&entry[index], function, index);
994 entry[index].flags |=
995 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
996 ++*nent;
997 }
998 break;
999 }
1000 case 0x80000000:
1001 entry->eax = min(entry->eax, 0x8000001a);
1002 break;
1003 case 0x80000001:
1004 entry->edx &= kvm_supported_word1_x86_features;
1005 entry->ecx &= kvm_supported_word6_x86_features;
1006 break;
1007 }
1008 put_cpu();
1009}
1010
Avi Kivity674eea02008-02-11 18:37:23 +02001011static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
Dan Kenigsberg07716712007-11-21 17:10:04 +02001012 struct kvm_cpuid_entry2 __user *entries)
1013{
1014 struct kvm_cpuid_entry2 *cpuid_entries;
1015 int limit, nent = 0, r = -E2BIG;
1016 u32 func;
1017
1018 if (cpuid->nent < 1)
1019 goto out;
1020 r = -ENOMEM;
1021 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
1022 if (!cpuid_entries)
1023 goto out;
1024
1025 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
1026 limit = cpuid_entries[0].eax;
1027 for (func = 1; func <= limit && nent < cpuid->nent; ++func)
1028 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1029 &nent, cpuid->nent);
1030 r = -E2BIG;
1031 if (nent >= cpuid->nent)
1032 goto out_free;
1033
1034 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
1035 limit = cpuid_entries[nent - 1].eax;
1036 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
1037 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1038 &nent, cpuid->nent);
1039 r = -EFAULT;
1040 if (copy_to_user(entries, cpuid_entries,
1041 nent * sizeof(struct kvm_cpuid_entry2)))
1042 goto out_free;
1043 cpuid->nent = nent;
1044 r = 0;
1045
1046out_free:
1047 vfree(cpuid_entries);
1048out:
1049 return r;
1050}
1051
Carsten Otte313a3dc2007-10-11 19:16:52 +02001052static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
1053 struct kvm_lapic_state *s)
1054{
1055 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001056 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001057 vcpu_put(vcpu);
1058
1059 return 0;
1060}
1061
1062static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
1063 struct kvm_lapic_state *s)
1064{
1065 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001066 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001067 kvm_apic_post_state_restore(vcpu);
1068 vcpu_put(vcpu);
1069
1070 return 0;
1071}
1072
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001073static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
1074 struct kvm_interrupt *irq)
1075{
1076 if (irq->irq < 0 || irq->irq >= 256)
1077 return -EINVAL;
1078 if (irqchip_in_kernel(vcpu->kvm))
1079 return -ENXIO;
1080 vcpu_load(vcpu);
1081
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001082 set_bit(irq->irq, vcpu->arch.irq_pending);
1083 set_bit(irq->irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001084
1085 vcpu_put(vcpu);
1086
1087 return 0;
1088}
1089
Avi Kivityb209749f2007-10-22 16:50:39 +02001090static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
1091 struct kvm_tpr_access_ctl *tac)
1092{
1093 if (tac->flags)
1094 return -EINVAL;
1095 vcpu->arch.tpr_access_reporting = !!tac->enabled;
1096 return 0;
1097}
1098
Carsten Otte313a3dc2007-10-11 19:16:52 +02001099long kvm_arch_vcpu_ioctl(struct file *filp,
1100 unsigned int ioctl, unsigned long arg)
1101{
1102 struct kvm_vcpu *vcpu = filp->private_data;
1103 void __user *argp = (void __user *)arg;
1104 int r;
1105
1106 switch (ioctl) {
1107 case KVM_GET_LAPIC: {
1108 struct kvm_lapic_state lapic;
1109
1110 memset(&lapic, 0, sizeof lapic);
1111 r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic);
1112 if (r)
1113 goto out;
1114 r = -EFAULT;
1115 if (copy_to_user(argp, &lapic, sizeof lapic))
1116 goto out;
1117 r = 0;
1118 break;
1119 }
1120 case KVM_SET_LAPIC: {
1121 struct kvm_lapic_state lapic;
1122
1123 r = -EFAULT;
1124 if (copy_from_user(&lapic, argp, sizeof lapic))
1125 goto out;
1126 r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);;
1127 if (r)
1128 goto out;
1129 r = 0;
1130 break;
1131 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001132 case KVM_INTERRUPT: {
1133 struct kvm_interrupt irq;
1134
1135 r = -EFAULT;
1136 if (copy_from_user(&irq, argp, sizeof irq))
1137 goto out;
1138 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1139 if (r)
1140 goto out;
1141 r = 0;
1142 break;
1143 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001144 case KVM_SET_CPUID: {
1145 struct kvm_cpuid __user *cpuid_arg = argp;
1146 struct kvm_cpuid cpuid;
1147
1148 r = -EFAULT;
1149 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1150 goto out;
1151 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
1152 if (r)
1153 goto out;
1154 break;
1155 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02001156 case KVM_SET_CPUID2: {
1157 struct kvm_cpuid2 __user *cpuid_arg = argp;
1158 struct kvm_cpuid2 cpuid;
1159
1160 r = -EFAULT;
1161 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1162 goto out;
1163 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
1164 cpuid_arg->entries);
1165 if (r)
1166 goto out;
1167 break;
1168 }
1169 case KVM_GET_CPUID2: {
1170 struct kvm_cpuid2 __user *cpuid_arg = argp;
1171 struct kvm_cpuid2 cpuid;
1172
1173 r = -EFAULT;
1174 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1175 goto out;
1176 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
1177 cpuid_arg->entries);
1178 if (r)
1179 goto out;
1180 r = -EFAULT;
1181 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1182 goto out;
1183 r = 0;
1184 break;
1185 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001186 case KVM_GET_MSRS:
1187 r = msr_io(vcpu, argp, kvm_get_msr, 1);
1188 break;
1189 case KVM_SET_MSRS:
1190 r = msr_io(vcpu, argp, do_set_msr, 0);
1191 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02001192 case KVM_TPR_ACCESS_REPORTING: {
1193 struct kvm_tpr_access_ctl tac;
1194
1195 r = -EFAULT;
1196 if (copy_from_user(&tac, argp, sizeof tac))
1197 goto out;
1198 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
1199 if (r)
1200 goto out;
1201 r = -EFAULT;
1202 if (copy_to_user(argp, &tac, sizeof tac))
1203 goto out;
1204 r = 0;
1205 break;
1206 };
Avi Kivityb93463a2007-10-25 16:52:32 +02001207 case KVM_SET_VAPIC_ADDR: {
1208 struct kvm_vapic_addr va;
1209
1210 r = -EINVAL;
1211 if (!irqchip_in_kernel(vcpu->kvm))
1212 goto out;
1213 r = -EFAULT;
1214 if (copy_from_user(&va, argp, sizeof va))
1215 goto out;
1216 r = 0;
1217 kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
1218 break;
1219 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001220 default:
1221 r = -EINVAL;
1222 }
1223out:
1224 return r;
1225}
1226
Carsten Otte1fe779f2007-10-29 16:08:35 +01001227static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
1228{
1229 int ret;
1230
1231 if (addr > (unsigned int)(-3 * PAGE_SIZE))
1232 return -1;
1233 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
1234 return ret;
1235}
1236
1237static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
1238 u32 kvm_nr_mmu_pages)
1239{
1240 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
1241 return -EINVAL;
1242
Izik Eidus72dc67a2008-02-10 18:04:15 +02001243 down_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001244
1245 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001246 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001247
Izik Eidus72dc67a2008-02-10 18:04:15 +02001248 up_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001249 return 0;
1250}
1251
1252static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
1253{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001254 return kvm->arch.n_alloc_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001255}
1256
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08001257gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
1258{
1259 int i;
1260 struct kvm_mem_alias *alias;
1261
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001262 for (i = 0; i < kvm->arch.naliases; ++i) {
1263 alias = &kvm->arch.aliases[i];
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08001264 if (gfn >= alias->base_gfn
1265 && gfn < alias->base_gfn + alias->npages)
1266 return alias->target_gfn + gfn - alias->base_gfn;
1267 }
1268 return gfn;
1269}
1270
Carsten Otte1fe779f2007-10-29 16:08:35 +01001271/*
1272 * Set a new alias region. Aliases map a portion of physical memory into
1273 * another portion. This is useful for memory windows, for example the PC
1274 * VGA region.
1275 */
1276static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
1277 struct kvm_memory_alias *alias)
1278{
1279 int r, n;
1280 struct kvm_mem_alias *p;
1281
1282 r = -EINVAL;
1283 /* General sanity checks */
1284 if (alias->memory_size & (PAGE_SIZE - 1))
1285 goto out;
1286 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
1287 goto out;
1288 if (alias->slot >= KVM_ALIAS_SLOTS)
1289 goto out;
1290 if (alias->guest_phys_addr + alias->memory_size
1291 < alias->guest_phys_addr)
1292 goto out;
1293 if (alias->target_phys_addr + alias->memory_size
1294 < alias->target_phys_addr)
1295 goto out;
1296
Izik Eidus72dc67a2008-02-10 18:04:15 +02001297 down_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001298
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001299 p = &kvm->arch.aliases[alias->slot];
Carsten Otte1fe779f2007-10-29 16:08:35 +01001300 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
1301 p->npages = alias->memory_size >> PAGE_SHIFT;
1302 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
1303
1304 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001305 if (kvm->arch.aliases[n - 1].npages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01001306 break;
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001307 kvm->arch.naliases = n;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001308
1309 kvm_mmu_zap_all(kvm);
1310
Izik Eidus72dc67a2008-02-10 18:04:15 +02001311 up_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001312
1313 return 0;
1314
1315out:
1316 return r;
1317}
1318
1319static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1320{
1321 int r;
1322
1323 r = 0;
1324 switch (chip->chip_id) {
1325 case KVM_IRQCHIP_PIC_MASTER:
1326 memcpy(&chip->chip.pic,
1327 &pic_irqchip(kvm)->pics[0],
1328 sizeof(struct kvm_pic_state));
1329 break;
1330 case KVM_IRQCHIP_PIC_SLAVE:
1331 memcpy(&chip->chip.pic,
1332 &pic_irqchip(kvm)->pics[1],
1333 sizeof(struct kvm_pic_state));
1334 break;
1335 case KVM_IRQCHIP_IOAPIC:
1336 memcpy(&chip->chip.ioapic,
1337 ioapic_irqchip(kvm),
1338 sizeof(struct kvm_ioapic_state));
1339 break;
1340 default:
1341 r = -EINVAL;
1342 break;
1343 }
1344 return r;
1345}
1346
1347static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1348{
1349 int r;
1350
1351 r = 0;
1352 switch (chip->chip_id) {
1353 case KVM_IRQCHIP_PIC_MASTER:
1354 memcpy(&pic_irqchip(kvm)->pics[0],
1355 &chip->chip.pic,
1356 sizeof(struct kvm_pic_state));
1357 break;
1358 case KVM_IRQCHIP_PIC_SLAVE:
1359 memcpy(&pic_irqchip(kvm)->pics[1],
1360 &chip->chip.pic,
1361 sizeof(struct kvm_pic_state));
1362 break;
1363 case KVM_IRQCHIP_IOAPIC:
1364 memcpy(ioapic_irqchip(kvm),
1365 &chip->chip.ioapic,
1366 sizeof(struct kvm_ioapic_state));
1367 break;
1368 default:
1369 r = -EINVAL;
1370 break;
1371 }
1372 kvm_pic_update_irq(pic_irqchip(kvm));
1373 return r;
1374}
1375
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001376/*
1377 * Get (and clear) the dirty memory log for a memory slot.
1378 */
1379int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1380 struct kvm_dirty_log *log)
1381{
1382 int r;
1383 int n;
1384 struct kvm_memory_slot *memslot;
1385 int is_dirty = 0;
1386
Izik Eidus72dc67a2008-02-10 18:04:15 +02001387 down_write(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001388
1389 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1390 if (r)
1391 goto out;
1392
1393 /* If nothing is dirty, don't bother messing with page tables. */
1394 if (is_dirty) {
1395 kvm_mmu_slot_remove_write_access(kvm, log->slot);
1396 kvm_flush_remote_tlbs(kvm);
1397 memslot = &kvm->memslots[log->slot];
1398 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1399 memset(memslot->dirty_bitmap, 0, n);
1400 }
1401 r = 0;
1402out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02001403 up_write(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001404 return r;
1405}
1406
Carsten Otte1fe779f2007-10-29 16:08:35 +01001407long kvm_arch_vm_ioctl(struct file *filp,
1408 unsigned int ioctl, unsigned long arg)
1409{
1410 struct kvm *kvm = filp->private_data;
1411 void __user *argp = (void __user *)arg;
1412 int r = -EINVAL;
1413
1414 switch (ioctl) {
1415 case KVM_SET_TSS_ADDR:
1416 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
1417 if (r < 0)
1418 goto out;
1419 break;
1420 case KVM_SET_MEMORY_REGION: {
1421 struct kvm_memory_region kvm_mem;
1422 struct kvm_userspace_memory_region kvm_userspace_mem;
1423
1424 r = -EFAULT;
1425 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
1426 goto out;
1427 kvm_userspace_mem.slot = kvm_mem.slot;
1428 kvm_userspace_mem.flags = kvm_mem.flags;
1429 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
1430 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
1431 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
1432 if (r)
1433 goto out;
1434 break;
1435 }
1436 case KVM_SET_NR_MMU_PAGES:
1437 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
1438 if (r)
1439 goto out;
1440 break;
1441 case KVM_GET_NR_MMU_PAGES:
1442 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
1443 break;
1444 case KVM_SET_MEMORY_ALIAS: {
1445 struct kvm_memory_alias alias;
1446
1447 r = -EFAULT;
1448 if (copy_from_user(&alias, argp, sizeof alias))
1449 goto out;
1450 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
1451 if (r)
1452 goto out;
1453 break;
1454 }
1455 case KVM_CREATE_IRQCHIP:
1456 r = -ENOMEM;
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08001457 kvm->arch.vpic = kvm_create_pic(kvm);
1458 if (kvm->arch.vpic) {
Carsten Otte1fe779f2007-10-29 16:08:35 +01001459 r = kvm_ioapic_init(kvm);
1460 if (r) {
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08001461 kfree(kvm->arch.vpic);
1462 kvm->arch.vpic = NULL;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001463 goto out;
1464 }
1465 } else
1466 goto out;
1467 break;
1468 case KVM_IRQ_LINE: {
1469 struct kvm_irq_level irq_event;
1470
1471 r = -EFAULT;
1472 if (copy_from_user(&irq_event, argp, sizeof irq_event))
1473 goto out;
1474 if (irqchip_in_kernel(kvm)) {
1475 mutex_lock(&kvm->lock);
1476 if (irq_event.irq < 16)
1477 kvm_pic_set_irq(pic_irqchip(kvm),
1478 irq_event.irq,
1479 irq_event.level);
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08001480 kvm_ioapic_set_irq(kvm->arch.vioapic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01001481 irq_event.irq,
1482 irq_event.level);
1483 mutex_unlock(&kvm->lock);
1484 r = 0;
1485 }
1486 break;
1487 }
1488 case KVM_GET_IRQCHIP: {
1489 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1490 struct kvm_irqchip chip;
1491
1492 r = -EFAULT;
1493 if (copy_from_user(&chip, argp, sizeof chip))
1494 goto out;
1495 r = -ENXIO;
1496 if (!irqchip_in_kernel(kvm))
1497 goto out;
1498 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
1499 if (r)
1500 goto out;
1501 r = -EFAULT;
1502 if (copy_to_user(argp, &chip, sizeof chip))
1503 goto out;
1504 r = 0;
1505 break;
1506 }
1507 case KVM_SET_IRQCHIP: {
1508 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1509 struct kvm_irqchip chip;
1510
1511 r = -EFAULT;
1512 if (copy_from_user(&chip, argp, sizeof chip))
1513 goto out;
1514 r = -ENXIO;
1515 if (!irqchip_in_kernel(kvm))
1516 goto out;
1517 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
1518 if (r)
1519 goto out;
1520 r = 0;
1521 break;
1522 }
1523 default:
1524 ;
1525 }
1526out:
1527 return r;
1528}
1529
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08001530static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02001531{
1532 u32 dummy[2];
1533 unsigned i, j;
1534
1535 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
1536 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
1537 continue;
1538 if (j < i)
1539 msrs_to_save[j] = msrs_to_save[i];
1540 j++;
1541 }
1542 num_msrs_to_save = j;
1543}
1544
Carsten Ottebbd9b642007-10-30 18:44:21 +01001545/*
1546 * Only apic need an MMIO device hook, so shortcut now..
1547 */
1548static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
1549 gpa_t addr)
1550{
1551 struct kvm_io_device *dev;
1552
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001553 if (vcpu->arch.apic) {
1554 dev = &vcpu->arch.apic->dev;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001555 if (dev->in_range(dev, addr))
1556 return dev;
1557 }
1558 return NULL;
1559}
1560
1561
1562static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1563 gpa_t addr)
1564{
1565 struct kvm_io_device *dev;
1566
1567 dev = vcpu_find_pervcpu_dev(vcpu, addr);
1568 if (dev == NULL)
1569 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1570 return dev;
1571}
1572
1573int emulator_read_std(unsigned long addr,
1574 void *val,
1575 unsigned int bytes,
1576 struct kvm_vcpu *vcpu)
1577{
1578 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001579 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001580
Izik Eidus72dc67a2008-02-10 18:04:15 +02001581 down_read(&vcpu->kvm->slots_lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001582 while (bytes) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001583 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001584 unsigned offset = addr & (PAGE_SIZE-1);
1585 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
1586 int ret;
1587
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001588 if (gpa == UNMAPPED_GVA) {
1589 r = X86EMUL_PROPAGATE_FAULT;
1590 goto out;
1591 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01001592 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001593 if (ret < 0) {
1594 r = X86EMUL_UNHANDLEABLE;
1595 goto out;
1596 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01001597
1598 bytes -= tocopy;
1599 data += tocopy;
1600 addr += tocopy;
1601 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001602out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02001603 up_read(&vcpu->kvm->slots_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001604 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001605}
1606EXPORT_SYMBOL_GPL(emulator_read_std);
1607
Carsten Ottebbd9b642007-10-30 18:44:21 +01001608static int emulator_read_emulated(unsigned long addr,
1609 void *val,
1610 unsigned int bytes,
1611 struct kvm_vcpu *vcpu)
1612{
1613 struct kvm_io_device *mmio_dev;
1614 gpa_t gpa;
1615
1616 if (vcpu->mmio_read_completed) {
1617 memcpy(val, vcpu->mmio_data, bytes);
1618 vcpu->mmio_read_completed = 0;
1619 return X86EMUL_CONTINUE;
1620 }
1621
Izik Eidus72dc67a2008-02-10 18:04:15 +02001622 down_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001623 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001624 up_read(&vcpu->kvm->slots_lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001625
1626 /* For APIC access vmexit */
1627 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1628 goto mmio;
1629
1630 if (emulator_read_std(addr, val, bytes, vcpu)
1631 == X86EMUL_CONTINUE)
1632 return X86EMUL_CONTINUE;
1633 if (gpa == UNMAPPED_GVA)
1634 return X86EMUL_PROPAGATE_FAULT;
1635
1636mmio:
1637 /*
1638 * Is this MMIO handled locally?
1639 */
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001640 mutex_lock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001641 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1642 if (mmio_dev) {
1643 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001644 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001645 return X86EMUL_CONTINUE;
1646 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001647 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001648
1649 vcpu->mmio_needed = 1;
1650 vcpu->mmio_phys_addr = gpa;
1651 vcpu->mmio_size = bytes;
1652 vcpu->mmio_is_write = 0;
1653
1654 return X86EMUL_UNHANDLEABLE;
1655}
1656
1657static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
1658 const void *val, int bytes)
1659{
1660 int ret;
1661
Izik Eidus72dc67a2008-02-10 18:04:15 +02001662 down_read(&vcpu->kvm->slots_lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001663 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001664 if (ret < 0) {
Izik Eidus72dc67a2008-02-10 18:04:15 +02001665 up_read(&vcpu->kvm->slots_lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001666 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001667 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01001668 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001669 up_read(&vcpu->kvm->slots_lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001670 return 1;
1671}
1672
1673static int emulator_write_emulated_onepage(unsigned long addr,
1674 const void *val,
1675 unsigned int bytes,
1676 struct kvm_vcpu *vcpu)
1677{
1678 struct kvm_io_device *mmio_dev;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001679 gpa_t gpa;
1680
Izik Eidus72dc67a2008-02-10 18:04:15 +02001681 down_read(&vcpu->kvm->slots_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001682 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001683 up_read(&vcpu->kvm->slots_lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001684
1685 if (gpa == UNMAPPED_GVA) {
Avi Kivityc3c91fe2007-11-25 14:04:58 +02001686 kvm_inject_page_fault(vcpu, addr, 2);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001687 return X86EMUL_PROPAGATE_FAULT;
1688 }
1689
1690 /* For APIC access vmexit */
1691 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1692 goto mmio;
1693
1694 if (emulator_write_phys(vcpu, gpa, val, bytes))
1695 return X86EMUL_CONTINUE;
1696
1697mmio:
1698 /*
1699 * Is this MMIO handled locally?
1700 */
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001701 mutex_lock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001702 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1703 if (mmio_dev) {
1704 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001705 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001706 return X86EMUL_CONTINUE;
1707 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001708 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001709
1710 vcpu->mmio_needed = 1;
1711 vcpu->mmio_phys_addr = gpa;
1712 vcpu->mmio_size = bytes;
1713 vcpu->mmio_is_write = 1;
1714 memcpy(vcpu->mmio_data, val, bytes);
1715
1716 return X86EMUL_CONTINUE;
1717}
1718
1719int emulator_write_emulated(unsigned long addr,
1720 const void *val,
1721 unsigned int bytes,
1722 struct kvm_vcpu *vcpu)
1723{
1724 /* Crossing a page boundary? */
1725 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1726 int rc, now;
1727
1728 now = -addr & ~PAGE_MASK;
1729 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
1730 if (rc != X86EMUL_CONTINUE)
1731 return rc;
1732 addr += now;
1733 val += now;
1734 bytes -= now;
1735 }
1736 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
1737}
1738EXPORT_SYMBOL_GPL(emulator_write_emulated);
1739
1740static int emulator_cmpxchg_emulated(unsigned long addr,
1741 const void *old,
1742 const void *new,
1743 unsigned int bytes,
1744 struct kvm_vcpu *vcpu)
1745{
1746 static int reported;
1747
1748 if (!reported) {
1749 reported = 1;
1750 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1751 }
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001752#ifndef CONFIG_X86_64
1753 /* guests cmpxchg8b have to be emulated atomically */
1754 if (bytes == 8) {
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001755 gpa_t gpa;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001756 struct page *page;
Andrew Mortonc0b49b02008-02-04 22:27:18 -08001757 char *kaddr;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001758 u64 val;
1759
Izik Eidus72dc67a2008-02-10 18:04:15 +02001760 down_read(&vcpu->kvm->slots_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001761 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
1762
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001763 if (gpa == UNMAPPED_GVA ||
1764 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1765 goto emul_write;
1766
1767 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
1768 goto emul_write;
1769
1770 val = *(u64 *)new;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001771
1772 down_read(&current->mm->mmap_sem);
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001773 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001774 up_read(&current->mm->mmap_sem);
1775
Andrew Mortonc0b49b02008-02-04 22:27:18 -08001776 kaddr = kmap_atomic(page, KM_USER0);
1777 set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
1778 kunmap_atomic(kaddr, KM_USER0);
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001779 kvm_release_page_dirty(page);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001780 emul_write:
Izik Eidus72dc67a2008-02-10 18:04:15 +02001781 up_read(&vcpu->kvm->slots_lock);
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001782 }
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001783#endif
1784
Carsten Ottebbd9b642007-10-30 18:44:21 +01001785 return emulator_write_emulated(addr, new, bytes, vcpu);
1786}
1787
1788static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1789{
1790 return kvm_x86_ops->get_segment_base(vcpu, seg);
1791}
1792
1793int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1794{
1795 return X86EMUL_CONTINUE;
1796}
1797
1798int emulate_clts(struct kvm_vcpu *vcpu)
1799{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001800 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001801 return X86EMUL_CONTINUE;
1802}
1803
1804int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
1805{
1806 struct kvm_vcpu *vcpu = ctxt->vcpu;
1807
1808 switch (dr) {
1809 case 0 ... 3:
1810 *dest = kvm_x86_ops->get_dr(vcpu, dr);
1811 return X86EMUL_CONTINUE;
1812 default:
1813 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
1814 return X86EMUL_UNHANDLEABLE;
1815 }
1816}
1817
1818int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1819{
1820 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1821 int exception;
1822
1823 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1824 if (exception) {
1825 /* FIXME: better handling */
1826 return X86EMUL_UNHANDLEABLE;
1827 }
1828 return X86EMUL_CONTINUE;
1829}
1830
1831void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
1832{
1833 static int reported;
1834 u8 opcodes[4];
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001835 unsigned long rip = vcpu->arch.rip;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001836 unsigned long rip_linear;
1837
1838 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
1839
1840 if (reported)
1841 return;
1842
1843 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
1844
1845 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1846 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1847 reported = 1;
1848}
1849EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
1850
1851struct x86_emulate_ops emulate_ops = {
1852 .read_std = emulator_read_std,
Carsten Ottebbd9b642007-10-30 18:44:21 +01001853 .read_emulated = emulator_read_emulated,
1854 .write_emulated = emulator_write_emulated,
1855 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1856};
1857
1858int emulate_instruction(struct kvm_vcpu *vcpu,
1859 struct kvm_run *run,
1860 unsigned long cr2,
1861 u16 error_code,
Sheng Yang571008d2008-01-02 14:49:22 +08001862 int emulation_type)
Carsten Ottebbd9b642007-10-30 18:44:21 +01001863{
1864 int r;
Sheng Yang571008d2008-01-02 14:49:22 +08001865 struct decode_cache *c;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001866
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001867 vcpu->arch.mmio_fault_cr2 = cr2;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001868 kvm_x86_ops->cache_regs(vcpu);
1869
1870 vcpu->mmio_is_write = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001871 vcpu->arch.pio.string = 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001872
Sheng Yang571008d2008-01-02 14:49:22 +08001873 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Carsten Ottebbd9b642007-10-30 18:44:21 +01001874 int cs_db, cs_l;
1875 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1876
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001877 vcpu->arch.emulate_ctxt.vcpu = vcpu;
1878 vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
1879 vcpu->arch.emulate_ctxt.mode =
1880 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
Carsten Ottebbd9b642007-10-30 18:44:21 +01001881 ? X86EMUL_MODE_REAL : cs_l
1882 ? X86EMUL_MODE_PROT64 : cs_db
1883 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1884
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001885 if (vcpu->arch.emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1886 vcpu->arch.emulate_ctxt.cs_base = 0;
1887 vcpu->arch.emulate_ctxt.ds_base = 0;
1888 vcpu->arch.emulate_ctxt.es_base = 0;
1889 vcpu->arch.emulate_ctxt.ss_base = 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001890 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001891 vcpu->arch.emulate_ctxt.cs_base =
Carsten Ottebbd9b642007-10-30 18:44:21 +01001892 get_segment_base(vcpu, VCPU_SREG_CS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001893 vcpu->arch.emulate_ctxt.ds_base =
Carsten Ottebbd9b642007-10-30 18:44:21 +01001894 get_segment_base(vcpu, VCPU_SREG_DS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001895 vcpu->arch.emulate_ctxt.es_base =
Carsten Ottebbd9b642007-10-30 18:44:21 +01001896 get_segment_base(vcpu, VCPU_SREG_ES);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001897 vcpu->arch.emulate_ctxt.ss_base =
Carsten Ottebbd9b642007-10-30 18:44:21 +01001898 get_segment_base(vcpu, VCPU_SREG_SS);
1899 }
1900
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001901 vcpu->arch.emulate_ctxt.gs_base =
Carsten Ottebbd9b642007-10-30 18:44:21 +01001902 get_segment_base(vcpu, VCPU_SREG_GS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001903 vcpu->arch.emulate_ctxt.fs_base =
Carsten Ottebbd9b642007-10-30 18:44:21 +01001904 get_segment_base(vcpu, VCPU_SREG_FS);
1905
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001906 r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Sheng Yang571008d2008-01-02 14:49:22 +08001907
1908 /* Reject the instructions other than VMCALL/VMMCALL when
1909 * try to emulate invalid opcode */
1910 c = &vcpu->arch.emulate_ctxt.decode;
1911 if ((emulation_type & EMULTYPE_TRAP_UD) &&
1912 (!(c->twobyte && c->b == 0x01 &&
1913 (c->modrm_reg == 0 || c->modrm_reg == 3) &&
1914 c->modrm_mod == 3 && c->modrm_rm == 1)))
1915 return EMULATE_FAIL;
1916
Avi Kivityf2b57562007-11-18 15:17:51 +02001917 ++vcpu->stat.insn_emulation;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001918 if (r) {
Avi Kivityf2b57562007-11-18 15:17:51 +02001919 ++vcpu->stat.insn_emulation_fail;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001920 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1921 return EMULATE_DONE;
1922 return EMULATE_FAIL;
1923 }
1924 }
1925
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001926 r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001927
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001928 if (vcpu->arch.pio.string)
Carsten Ottebbd9b642007-10-30 18:44:21 +01001929 return EMULATE_DO_MMIO;
1930
1931 if ((r || vcpu->mmio_is_write) && run) {
1932 run->exit_reason = KVM_EXIT_MMIO;
1933 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1934 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1935 run->mmio.len = vcpu->mmio_size;
1936 run->mmio.is_write = vcpu->mmio_is_write;
1937 }
1938
1939 if (r) {
1940 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1941 return EMULATE_DONE;
1942 if (!vcpu->mmio_needed) {
1943 kvm_report_emulation_failure(vcpu, "mmio");
1944 return EMULATE_FAIL;
1945 }
1946 return EMULATE_DO_MMIO;
1947 }
1948
1949 kvm_x86_ops->decache_regs(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001950 kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001951
1952 if (vcpu->mmio_is_write) {
1953 vcpu->mmio_needed = 0;
1954 return EMULATE_DO_MMIO;
1955 }
1956
1957 return EMULATE_DONE;
1958}
1959EXPORT_SYMBOL_GPL(emulate_instruction);
1960
Carsten Ottede7d7892007-10-30 18:44:25 +01001961static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
1962{
1963 int i;
1964
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001965 for (i = 0; i < ARRAY_SIZE(vcpu->arch.pio.guest_pages); ++i)
1966 if (vcpu->arch.pio.guest_pages[i]) {
1967 kvm_release_page_dirty(vcpu->arch.pio.guest_pages[i]);
1968 vcpu->arch.pio.guest_pages[i] = NULL;
Carsten Ottede7d7892007-10-30 18:44:25 +01001969 }
1970}
1971
1972static int pio_copy_data(struct kvm_vcpu *vcpu)
1973{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001974 void *p = vcpu->arch.pio_data;
Carsten Ottede7d7892007-10-30 18:44:25 +01001975 void *q;
1976 unsigned bytes;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001977 int nr_pages = vcpu->arch.pio.guest_pages[1] ? 2 : 1;
Carsten Ottede7d7892007-10-30 18:44:25 +01001978
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001979 q = vmap(vcpu->arch.pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
Carsten Ottede7d7892007-10-30 18:44:25 +01001980 PAGE_KERNEL);
1981 if (!q) {
1982 free_pio_guest_pages(vcpu);
1983 return -ENOMEM;
1984 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001985 q += vcpu->arch.pio.guest_page_offset;
1986 bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
1987 if (vcpu->arch.pio.in)
Carsten Ottede7d7892007-10-30 18:44:25 +01001988 memcpy(q, p, bytes);
1989 else
1990 memcpy(p, q, bytes);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001991 q -= vcpu->arch.pio.guest_page_offset;
Carsten Ottede7d7892007-10-30 18:44:25 +01001992 vunmap(q);
1993 free_pio_guest_pages(vcpu);
1994 return 0;
1995}
1996
1997int complete_pio(struct kvm_vcpu *vcpu)
1998{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001999 struct kvm_pio_request *io = &vcpu->arch.pio;
Carsten Ottede7d7892007-10-30 18:44:25 +01002000 long delta;
2001 int r;
2002
2003 kvm_x86_ops->cache_regs(vcpu);
2004
2005 if (!io->string) {
2006 if (io->in)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002007 memcpy(&vcpu->arch.regs[VCPU_REGS_RAX], vcpu->arch.pio_data,
Carsten Ottede7d7892007-10-30 18:44:25 +01002008 io->size);
2009 } else {
2010 if (io->in) {
2011 r = pio_copy_data(vcpu);
2012 if (r) {
2013 kvm_x86_ops->cache_regs(vcpu);
2014 return r;
2015 }
2016 }
2017
2018 delta = 1;
2019 if (io->rep) {
2020 delta *= io->cur_count;
2021 /*
2022 * The size of the register should really depend on
2023 * current address size.
2024 */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002025 vcpu->arch.regs[VCPU_REGS_RCX] -= delta;
Carsten Ottede7d7892007-10-30 18:44:25 +01002026 }
2027 if (io->down)
2028 delta = -delta;
2029 delta *= io->size;
2030 if (io->in)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002031 vcpu->arch.regs[VCPU_REGS_RDI] += delta;
Carsten Ottede7d7892007-10-30 18:44:25 +01002032 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002033 vcpu->arch.regs[VCPU_REGS_RSI] += delta;
Carsten Ottede7d7892007-10-30 18:44:25 +01002034 }
2035
2036 kvm_x86_ops->decache_regs(vcpu);
2037
2038 io->count -= io->cur_count;
2039 io->cur_count = 0;
2040
2041 return 0;
2042}
2043
2044static void kernel_pio(struct kvm_io_device *pio_dev,
2045 struct kvm_vcpu *vcpu,
2046 void *pd)
2047{
2048 /* TODO: String I/O for in kernel device */
2049
2050 mutex_lock(&vcpu->kvm->lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002051 if (vcpu->arch.pio.in)
2052 kvm_iodevice_read(pio_dev, vcpu->arch.pio.port,
2053 vcpu->arch.pio.size,
Carsten Ottede7d7892007-10-30 18:44:25 +01002054 pd);
2055 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002056 kvm_iodevice_write(pio_dev, vcpu->arch.pio.port,
2057 vcpu->arch.pio.size,
Carsten Ottede7d7892007-10-30 18:44:25 +01002058 pd);
2059 mutex_unlock(&vcpu->kvm->lock);
2060}
2061
2062static void pio_string_write(struct kvm_io_device *pio_dev,
2063 struct kvm_vcpu *vcpu)
2064{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002065 struct kvm_pio_request *io = &vcpu->arch.pio;
2066 void *pd = vcpu->arch.pio_data;
Carsten Ottede7d7892007-10-30 18:44:25 +01002067 int i;
2068
2069 mutex_lock(&vcpu->kvm->lock);
2070 for (i = 0; i < io->cur_count; i++) {
2071 kvm_iodevice_write(pio_dev, io->port,
2072 io->size,
2073 pd);
2074 pd += io->size;
2075 }
2076 mutex_unlock(&vcpu->kvm->lock);
2077}
2078
2079static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
2080 gpa_t addr)
2081{
2082 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
2083}
2084
2085int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2086 int size, unsigned port)
2087{
2088 struct kvm_io_device *pio_dev;
2089
2090 vcpu->run->exit_reason = KVM_EXIT_IO;
2091 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002092 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01002093 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002094 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
2095 vcpu->run->io.port = vcpu->arch.pio.port = port;
2096 vcpu->arch.pio.in = in;
2097 vcpu->arch.pio.string = 0;
2098 vcpu->arch.pio.down = 0;
2099 vcpu->arch.pio.guest_page_offset = 0;
2100 vcpu->arch.pio.rep = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01002101
2102 kvm_x86_ops->cache_regs(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002103 memcpy(vcpu->arch.pio_data, &vcpu->arch.regs[VCPU_REGS_RAX], 4);
Carsten Ottede7d7892007-10-30 18:44:25 +01002104 kvm_x86_ops->decache_regs(vcpu);
2105
2106 kvm_x86_ops->skip_emulated_instruction(vcpu);
2107
2108 pio_dev = vcpu_find_pio_dev(vcpu, port);
2109 if (pio_dev) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002110 kernel_pio(pio_dev, vcpu, vcpu->arch.pio_data);
Carsten Ottede7d7892007-10-30 18:44:25 +01002111 complete_pio(vcpu);
2112 return 1;
2113 }
2114 return 0;
2115}
2116EXPORT_SYMBOL_GPL(kvm_emulate_pio);
2117
2118int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2119 int size, unsigned long count, int down,
2120 gva_t address, int rep, unsigned port)
2121{
2122 unsigned now, in_page;
2123 int i, ret = 0;
2124 int nr_pages = 1;
2125 struct page *page;
2126 struct kvm_io_device *pio_dev;
2127
2128 vcpu->run->exit_reason = KVM_EXIT_IO;
2129 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002130 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01002131 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002132 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
2133 vcpu->run->io.port = vcpu->arch.pio.port = port;
2134 vcpu->arch.pio.in = in;
2135 vcpu->arch.pio.string = 1;
2136 vcpu->arch.pio.down = down;
2137 vcpu->arch.pio.guest_page_offset = offset_in_page(address);
2138 vcpu->arch.pio.rep = rep;
Carsten Ottede7d7892007-10-30 18:44:25 +01002139
2140 if (!count) {
2141 kvm_x86_ops->skip_emulated_instruction(vcpu);
2142 return 1;
2143 }
2144
2145 if (!down)
2146 in_page = PAGE_SIZE - offset_in_page(address);
2147 else
2148 in_page = offset_in_page(address) + size;
2149 now = min(count, (unsigned long)in_page / size);
2150 if (!now) {
2151 /*
2152 * String I/O straddles page boundary. Pin two guest pages
2153 * so that we satisfy atomicity constraints. Do just one
2154 * transaction to avoid complexity.
2155 */
2156 nr_pages = 2;
2157 now = 1;
2158 }
2159 if (down) {
2160 /*
2161 * String I/O in reverse. Yuck. Kill the guest, fix later.
2162 */
2163 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002164 kvm_inject_gp(vcpu, 0);
Carsten Ottede7d7892007-10-30 18:44:25 +01002165 return 1;
2166 }
2167 vcpu->run->io.count = now;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002168 vcpu->arch.pio.cur_count = now;
Carsten Ottede7d7892007-10-30 18:44:25 +01002169
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002170 if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
Carsten Ottede7d7892007-10-30 18:44:25 +01002171 kvm_x86_ops->skip_emulated_instruction(vcpu);
2172
2173 for (i = 0; i < nr_pages; ++i) {
Izik Eidus72dc67a2008-02-10 18:04:15 +02002174 down_read(&vcpu->kvm->slots_lock);
Carsten Ottede7d7892007-10-30 18:44:25 +01002175 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002176 vcpu->arch.pio.guest_pages[i] = page;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002177 up_read(&vcpu->kvm->slots_lock);
Carsten Ottede7d7892007-10-30 18:44:25 +01002178 if (!page) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002179 kvm_inject_gp(vcpu, 0);
Carsten Ottede7d7892007-10-30 18:44:25 +01002180 free_pio_guest_pages(vcpu);
2181 return 1;
2182 }
2183 }
2184
2185 pio_dev = vcpu_find_pio_dev(vcpu, port);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002186 if (!vcpu->arch.pio.in) {
Carsten Ottede7d7892007-10-30 18:44:25 +01002187 /* string PIO write */
2188 ret = pio_copy_data(vcpu);
2189 if (ret >= 0 && pio_dev) {
2190 pio_string_write(pio_dev, vcpu);
2191 complete_pio(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002192 if (vcpu->arch.pio.count == 0)
Carsten Ottede7d7892007-10-30 18:44:25 +01002193 ret = 1;
2194 }
2195 } else if (pio_dev)
2196 pr_unimpl(vcpu, "no string pio read support yet, "
2197 "port %x size %d count %ld\n",
2198 port, size, count);
2199
2200 return ret;
2201}
2202EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
2203
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002204int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02002205{
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002206 int r;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002207 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
2208
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002209 if (kvm_x86_ops) {
2210 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002211 r = -EEXIST;
2212 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002213 }
2214
2215 if (!ops->cpu_has_kvm_support()) {
2216 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002217 r = -EOPNOTSUPP;
2218 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002219 }
2220 if (ops->disabled_by_bios()) {
2221 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002222 r = -EOPNOTSUPP;
2223 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002224 }
2225
Avi Kivity97db56c2008-01-13 13:23:56 +02002226 r = kvm_mmu_module_init();
2227 if (r)
2228 goto out;
2229
2230 kvm_init_msr_list();
2231
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002232 kvm_x86_ops = ops;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002233 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002234 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002235
2236out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002237 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02002238}
Hollis Blanchard8776e512007-10-31 17:24:24 -05002239
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002240void kvm_arch_exit(void)
2241{
2242 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002243 kvm_mmu_module_exit();
2244}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002245
Hollis Blanchard8776e512007-10-31 17:24:24 -05002246int kvm_emulate_halt(struct kvm_vcpu *vcpu)
2247{
2248 ++vcpu->stat.halt_exits;
2249 if (irqchip_in_kernel(vcpu->kvm)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002250 vcpu->arch.mp_state = VCPU_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002251 kvm_vcpu_block(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002252 if (vcpu->arch.mp_state != VCPU_MP_STATE_RUNNABLE)
Hollis Blanchard8776e512007-10-31 17:24:24 -05002253 return -EINTR;
2254 return 1;
2255 } else {
2256 vcpu->run->exit_reason = KVM_EXIT_HLT;
2257 return 0;
2258 }
2259}
2260EXPORT_SYMBOL_GPL(kvm_emulate_halt);
2261
2262int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
2263{
2264 unsigned long nr, a0, a1, a2, a3, ret;
2265
2266 kvm_x86_ops->cache_regs(vcpu);
2267
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002268 nr = vcpu->arch.regs[VCPU_REGS_RAX];
2269 a0 = vcpu->arch.regs[VCPU_REGS_RBX];
2270 a1 = vcpu->arch.regs[VCPU_REGS_RCX];
2271 a2 = vcpu->arch.regs[VCPU_REGS_RDX];
2272 a3 = vcpu->arch.regs[VCPU_REGS_RSI];
Hollis Blanchard8776e512007-10-31 17:24:24 -05002273
2274 if (!is_long_mode(vcpu)) {
2275 nr &= 0xFFFFFFFF;
2276 a0 &= 0xFFFFFFFF;
2277 a1 &= 0xFFFFFFFF;
2278 a2 &= 0xFFFFFFFF;
2279 a3 &= 0xFFFFFFFF;
2280 }
2281
2282 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02002283 case KVM_HC_VAPIC_POLL_IRQ:
2284 ret = 0;
2285 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002286 default:
2287 ret = -KVM_ENOSYS;
2288 break;
2289 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002290 vcpu->arch.regs[VCPU_REGS_RAX] = ret;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002291 kvm_x86_ops->decache_regs(vcpu);
2292 return 0;
2293}
2294EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
2295
2296int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
2297{
2298 char instruction[3];
2299 int ret = 0;
2300
Hollis Blanchard8776e512007-10-31 17:24:24 -05002301
2302 /*
2303 * Blow out the MMU to ensure that no other VCPU has an active mapping
2304 * to ensure that the updated hypercall appears atomically across all
2305 * VCPUs.
2306 */
2307 kvm_mmu_zap_all(vcpu->kvm);
2308
2309 kvm_x86_ops->cache_regs(vcpu);
2310 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002311 if (emulator_write_emulated(vcpu->arch.rip, instruction, 3, vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05002312 != X86EMUL_CONTINUE)
2313 ret = -EFAULT;
2314
Hollis Blanchard8776e512007-10-31 17:24:24 -05002315 return ret;
2316}
2317
2318static u64 mk_cr_64(u64 curr_cr, u32 new_val)
2319{
2320 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
2321}
2322
2323void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2324{
2325 struct descriptor_table dt = { limit, base };
2326
2327 kvm_x86_ops->set_gdt(vcpu, &dt);
2328}
2329
2330void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2331{
2332 struct descriptor_table dt = { limit, base };
2333
2334 kvm_x86_ops->set_idt(vcpu, &dt);
2335}
2336
2337void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
2338 unsigned long *rflags)
2339{
2340 lmsw(vcpu, msw);
2341 *rflags = kvm_x86_ops->get_rflags(vcpu);
2342}
2343
2344unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
2345{
2346 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2347 switch (cr) {
2348 case 0:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002349 return vcpu->arch.cr0;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002350 case 2:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002351 return vcpu->arch.cr2;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002352 case 3:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002353 return vcpu->arch.cr3;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002354 case 4:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002355 return vcpu->arch.cr4;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01002356 case 8:
2357 return get_cr8(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002358 default:
2359 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
2360 return 0;
2361 }
2362}
2363
2364void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
2365 unsigned long *rflags)
2366{
2367 switch (cr) {
2368 case 0:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002369 set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val));
Hollis Blanchard8776e512007-10-31 17:24:24 -05002370 *rflags = kvm_x86_ops->get_rflags(vcpu);
2371 break;
2372 case 2:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002373 vcpu->arch.cr2 = val;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002374 break;
2375 case 3:
2376 set_cr3(vcpu, val);
2377 break;
2378 case 4:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002379 set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val));
Hollis Blanchard8776e512007-10-31 17:24:24 -05002380 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01002381 case 8:
2382 set_cr8(vcpu, val & 0xfUL);
2383 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002384 default:
2385 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
2386 }
2387}
2388
Dan Kenigsberg07716712007-11-21 17:10:04 +02002389static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
2390{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002391 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
2392 int j, nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02002393
2394 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
2395 /* when no next entry is found, the current entry[i] is reselected */
2396 for (j = i + 1; j == i; j = (j + 1) % nent) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002397 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
Dan Kenigsberg07716712007-11-21 17:10:04 +02002398 if (ej->function == e->function) {
2399 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
2400 return j;
2401 }
2402 }
2403 return 0; /* silence gcc, even though control never reaches here */
2404}
2405
2406/* find an entry with matching function, matching index (if needed), and that
2407 * should be read next (if it's stateful) */
2408static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
2409 u32 function, u32 index)
2410{
2411 if (e->function != function)
2412 return 0;
2413 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
2414 return 0;
2415 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
2416 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
2417 return 0;
2418 return 1;
2419}
2420
Hollis Blanchard8776e512007-10-31 17:24:24 -05002421void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
2422{
2423 int i;
Dan Kenigsberg07716712007-11-21 17:10:04 +02002424 u32 function, index;
2425 struct kvm_cpuid_entry2 *e, *best;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002426
2427 kvm_x86_ops->cache_regs(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002428 function = vcpu->arch.regs[VCPU_REGS_RAX];
2429 index = vcpu->arch.regs[VCPU_REGS_RCX];
2430 vcpu->arch.regs[VCPU_REGS_RAX] = 0;
2431 vcpu->arch.regs[VCPU_REGS_RBX] = 0;
2432 vcpu->arch.regs[VCPU_REGS_RCX] = 0;
2433 vcpu->arch.regs[VCPU_REGS_RDX] = 0;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002434 best = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002435 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
2436 e = &vcpu->arch.cpuid_entries[i];
Dan Kenigsberg07716712007-11-21 17:10:04 +02002437 if (is_matching_cpuid_entry(e, function, index)) {
2438 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
2439 move_to_next_stateful_cpuid_entry(vcpu, i);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002440 best = e;
2441 break;
2442 }
2443 /*
2444 * Both basic or both extended?
2445 */
2446 if (((e->function ^ function) & 0x80000000) == 0)
2447 if (!best || e->function > best->function)
2448 best = e;
2449 }
2450 if (best) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002451 vcpu->arch.regs[VCPU_REGS_RAX] = best->eax;
2452 vcpu->arch.regs[VCPU_REGS_RBX] = best->ebx;
2453 vcpu->arch.regs[VCPU_REGS_RCX] = best->ecx;
2454 vcpu->arch.regs[VCPU_REGS_RDX] = best->edx;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002455 }
2456 kvm_x86_ops->decache_regs(vcpu);
2457 kvm_x86_ops->skip_emulated_instruction(vcpu);
2458}
2459EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
Hollis Blanchardd0752062007-10-31 17:24:25 -05002460
2461/*
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002462 * Check if userspace requested an interrupt window, and that the
2463 * interrupt window is open.
2464 *
2465 * No need to exit to userspace if we already have an interrupt queued.
2466 */
2467static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2468 struct kvm_run *kvm_run)
2469{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002470 return (!vcpu->arch.irq_summary &&
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002471 kvm_run->request_interrupt_window &&
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002472 vcpu->arch.interrupt_window_open &&
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002473 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
2474}
2475
2476static void post_kvm_run_save(struct kvm_vcpu *vcpu,
2477 struct kvm_run *kvm_run)
2478{
2479 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
2480 kvm_run->cr8 = get_cr8(vcpu);
2481 kvm_run->apic_base = kvm_get_apic_base(vcpu);
2482 if (irqchip_in_kernel(vcpu->kvm))
2483 kvm_run->ready_for_interrupt_injection = 1;
2484 else
2485 kvm_run->ready_for_interrupt_injection =
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002486 (vcpu->arch.interrupt_window_open &&
2487 vcpu->arch.irq_summary == 0);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002488}
2489
Avi Kivityb93463a2007-10-25 16:52:32 +02002490static void vapic_enter(struct kvm_vcpu *vcpu)
2491{
2492 struct kvm_lapic *apic = vcpu->arch.apic;
2493 struct page *page;
2494
2495 if (!apic || !apic->vapic_addr)
2496 return;
2497
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002498 down_read(&current->mm->mmap_sem);
Avi Kivityb93463a2007-10-25 16:52:32 +02002499 page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002500 up_read(&current->mm->mmap_sem);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002501
2502 vcpu->arch.apic->vapic_page = page;
Avi Kivityb93463a2007-10-25 16:52:32 +02002503}
2504
2505static void vapic_exit(struct kvm_vcpu *vcpu)
2506{
2507 struct kvm_lapic *apic = vcpu->arch.apic;
2508
2509 if (!apic || !apic->vapic_addr)
2510 return;
2511
2512 kvm_release_page_dirty(apic->vapic_page);
2513 mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
2514}
2515
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002516static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2517{
2518 int r;
2519
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002520 if (unlikely(vcpu->arch.mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002521 pr_debug("vcpu %d received sipi with vector # %x\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002522 vcpu->vcpu_id, vcpu->arch.sipi_vector);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002523 kvm_lapic_reset(vcpu);
2524 r = kvm_x86_ops->vcpu_reset(vcpu);
2525 if (r)
2526 return r;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002527 vcpu->arch.mp_state = VCPU_MP_STATE_RUNNABLE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002528 }
2529
Avi Kivityb93463a2007-10-25 16:52:32 +02002530 vapic_enter(vcpu);
2531
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002532preempted:
2533 if (vcpu->guest_debug.enabled)
2534 kvm_x86_ops->guest_debug_pre(vcpu);
2535
2536again:
2537 r = kvm_mmu_reload(vcpu);
2538 if (unlikely(r))
2539 goto out;
2540
Avi Kivity2f52d582008-01-16 12:49:30 +02002541 if (vcpu->requests) {
2542 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
2543 __kvm_migrate_apic_timer(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02002544 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
2545 &vcpu->requests)) {
2546 kvm_run->exit_reason = KVM_EXIT_TPR_ACCESS;
2547 r = 0;
2548 goto out;
2549 }
Avi Kivity2f52d582008-01-16 12:49:30 +02002550 }
Avi Kivityb93463a2007-10-25 16:52:32 +02002551
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002552 kvm_inject_pending_timer_irqs(vcpu);
2553
2554 preempt_disable();
2555
2556 kvm_x86_ops->prepare_guest_switch(vcpu);
2557 kvm_load_guest_fpu(vcpu);
2558
2559 local_irq_disable();
2560
Avi Kivity6c142802008-01-15 18:27:32 +02002561 if (need_resched()) {
2562 local_irq_enable();
2563 preempt_enable();
2564 r = 1;
2565 goto out;
2566 }
2567
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002568 if (signal_pending(current)) {
2569 local_irq_enable();
2570 preempt_enable();
2571 r = -EINTR;
2572 kvm_run->exit_reason = KVM_EXIT_INTR;
2573 ++vcpu->stat.signal_exits;
2574 goto out;
2575 }
2576
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002577 if (vcpu->arch.exception.pending)
Avi Kivity298101d2007-11-25 13:41:11 +02002578 __queue_exception(vcpu);
2579 else if (irqchip_in_kernel(vcpu->kvm))
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002580 kvm_x86_ops->inject_pending_irq(vcpu);
Avi Kivityeb9774f2007-11-25 17:45:31 +02002581 else
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002582 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2583
Avi Kivityb93463a2007-10-25 16:52:32 +02002584 kvm_lapic_sync_to_vapic(vcpu);
2585
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002586 vcpu->guest_mode = 1;
2587 kvm_guest_enter();
2588
2589 if (vcpu->requests)
2590 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
2591 kvm_x86_ops->tlb_flush(vcpu);
2592
2593 kvm_x86_ops->run(vcpu, kvm_run);
2594
2595 vcpu->guest_mode = 0;
2596 local_irq_enable();
2597
2598 ++vcpu->stat.exits;
2599
2600 /*
2601 * We must have an instruction between local_irq_enable() and
2602 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2603 * the interrupt shadow. The stat.exits increment will do nicely.
2604 * But we need to prevent reordering, hence this barrier():
2605 */
2606 barrier();
2607
2608 kvm_guest_exit();
2609
2610 preempt_enable();
2611
2612 /*
2613 * Profile KVM exit RIPs:
2614 */
2615 if (unlikely(prof_on == KVM_PROFILING)) {
2616 kvm_x86_ops->cache_regs(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002617 profile_hit(KVM_PROFILING, (void *)vcpu->arch.rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002618 }
2619
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002620 if (vcpu->arch.exception.pending && kvm_x86_ops->exception_injected(vcpu))
2621 vcpu->arch.exception.pending = false;
Avi Kivity298101d2007-11-25 13:41:11 +02002622
Avi Kivityb93463a2007-10-25 16:52:32 +02002623 kvm_lapic_sync_from_vapic(vcpu);
2624
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002625 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2626
2627 if (r > 0) {
2628 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2629 r = -EINTR;
2630 kvm_run->exit_reason = KVM_EXIT_INTR;
2631 ++vcpu->stat.request_irq_exits;
2632 goto out;
2633 }
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002634 if (!need_resched())
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002635 goto again;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002636 }
2637
2638out:
2639 if (r > 0) {
2640 kvm_resched(vcpu);
2641 goto preempted;
2642 }
2643
2644 post_kvm_run_save(vcpu, kvm_run);
2645
Avi Kivityb93463a2007-10-25 16:52:32 +02002646 vapic_exit(vcpu);
2647
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002648 return r;
2649}
2650
2651int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2652{
2653 int r;
2654 sigset_t sigsaved;
2655
2656 vcpu_load(vcpu);
2657
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002658 if (unlikely(vcpu->arch.mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002659 kvm_vcpu_block(vcpu);
2660 vcpu_put(vcpu);
2661 return -EAGAIN;
2662 }
2663
2664 if (vcpu->sigset_active)
2665 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2666
2667 /* re-sync apic's tpr */
2668 if (!irqchip_in_kernel(vcpu->kvm))
2669 set_cr8(vcpu, kvm_run->cr8);
2670
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002671 if (vcpu->arch.pio.cur_count) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002672 r = complete_pio(vcpu);
2673 if (r)
2674 goto out;
2675 }
2676#if CONFIG_HAS_IOMEM
2677 if (vcpu->mmio_needed) {
2678 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
2679 vcpu->mmio_read_completed = 1;
2680 vcpu->mmio_needed = 0;
2681 r = emulate_instruction(vcpu, kvm_run,
Sheng Yang571008d2008-01-02 14:49:22 +08002682 vcpu->arch.mmio_fault_cr2, 0,
2683 EMULTYPE_NO_DECODE);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002684 if (r == EMULATE_DO_MMIO) {
2685 /*
2686 * Read-modify-write. Back to userspace.
2687 */
2688 r = 0;
2689 goto out;
2690 }
2691 }
2692#endif
2693 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
2694 kvm_x86_ops->cache_regs(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002695 vcpu->arch.regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002696 kvm_x86_ops->decache_regs(vcpu);
2697 }
2698
2699 r = __vcpu_run(vcpu, kvm_run);
2700
2701out:
2702 if (vcpu->sigset_active)
2703 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2704
2705 vcpu_put(vcpu);
2706 return r;
2707}
2708
2709int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2710{
2711 vcpu_load(vcpu);
2712
2713 kvm_x86_ops->cache_regs(vcpu);
2714
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002715 regs->rax = vcpu->arch.regs[VCPU_REGS_RAX];
2716 regs->rbx = vcpu->arch.regs[VCPU_REGS_RBX];
2717 regs->rcx = vcpu->arch.regs[VCPU_REGS_RCX];
2718 regs->rdx = vcpu->arch.regs[VCPU_REGS_RDX];
2719 regs->rsi = vcpu->arch.regs[VCPU_REGS_RSI];
2720 regs->rdi = vcpu->arch.regs[VCPU_REGS_RDI];
2721 regs->rsp = vcpu->arch.regs[VCPU_REGS_RSP];
2722 regs->rbp = vcpu->arch.regs[VCPU_REGS_RBP];
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002723#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002724 regs->r8 = vcpu->arch.regs[VCPU_REGS_R8];
2725 regs->r9 = vcpu->arch.regs[VCPU_REGS_R9];
2726 regs->r10 = vcpu->arch.regs[VCPU_REGS_R10];
2727 regs->r11 = vcpu->arch.regs[VCPU_REGS_R11];
2728 regs->r12 = vcpu->arch.regs[VCPU_REGS_R12];
2729 regs->r13 = vcpu->arch.regs[VCPU_REGS_R13];
2730 regs->r14 = vcpu->arch.regs[VCPU_REGS_R14];
2731 regs->r15 = vcpu->arch.regs[VCPU_REGS_R15];
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002732#endif
2733
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002734 regs->rip = vcpu->arch.rip;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002735 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
2736
2737 /*
2738 * Don't leak debug flags in case they were set for guest debugging
2739 */
2740 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2741 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2742
2743 vcpu_put(vcpu);
2744
2745 return 0;
2746}
2747
2748int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2749{
2750 vcpu_load(vcpu);
2751
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002752 vcpu->arch.regs[VCPU_REGS_RAX] = regs->rax;
2753 vcpu->arch.regs[VCPU_REGS_RBX] = regs->rbx;
2754 vcpu->arch.regs[VCPU_REGS_RCX] = regs->rcx;
2755 vcpu->arch.regs[VCPU_REGS_RDX] = regs->rdx;
2756 vcpu->arch.regs[VCPU_REGS_RSI] = regs->rsi;
2757 vcpu->arch.regs[VCPU_REGS_RDI] = regs->rdi;
2758 vcpu->arch.regs[VCPU_REGS_RSP] = regs->rsp;
2759 vcpu->arch.regs[VCPU_REGS_RBP] = regs->rbp;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002760#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002761 vcpu->arch.regs[VCPU_REGS_R8] = regs->r8;
2762 vcpu->arch.regs[VCPU_REGS_R9] = regs->r9;
2763 vcpu->arch.regs[VCPU_REGS_R10] = regs->r10;
2764 vcpu->arch.regs[VCPU_REGS_R11] = regs->r11;
2765 vcpu->arch.regs[VCPU_REGS_R12] = regs->r12;
2766 vcpu->arch.regs[VCPU_REGS_R13] = regs->r13;
2767 vcpu->arch.regs[VCPU_REGS_R14] = regs->r14;
2768 vcpu->arch.regs[VCPU_REGS_R15] = regs->r15;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002769#endif
2770
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002771 vcpu->arch.rip = regs->rip;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002772 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
2773
2774 kvm_x86_ops->decache_regs(vcpu);
2775
2776 vcpu_put(vcpu);
2777
2778 return 0;
2779}
2780
2781static void get_segment(struct kvm_vcpu *vcpu,
2782 struct kvm_segment *var, int seg)
2783{
2784 return kvm_x86_ops->get_segment(vcpu, var, seg);
2785}
2786
2787void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2788{
2789 struct kvm_segment cs;
2790
2791 get_segment(vcpu, &cs, VCPU_SREG_CS);
2792 *db = cs.db;
2793 *l = cs.l;
2794}
2795EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2796
2797int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2798 struct kvm_sregs *sregs)
2799{
2800 struct descriptor_table dt;
2801 int pending_vec;
2802
2803 vcpu_load(vcpu);
2804
2805 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2806 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2807 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2808 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2809 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2810 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2811
2812 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2813 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2814
2815 kvm_x86_ops->get_idt(vcpu, &dt);
2816 sregs->idt.limit = dt.limit;
2817 sregs->idt.base = dt.base;
2818 kvm_x86_ops->get_gdt(vcpu, &dt);
2819 sregs->gdt.limit = dt.limit;
2820 sregs->gdt.base = dt.base;
2821
2822 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002823 sregs->cr0 = vcpu->arch.cr0;
2824 sregs->cr2 = vcpu->arch.cr2;
2825 sregs->cr3 = vcpu->arch.cr3;
2826 sregs->cr4 = vcpu->arch.cr4;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002827 sregs->cr8 = get_cr8(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002828 sregs->efer = vcpu->arch.shadow_efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002829 sregs->apic_base = kvm_get_apic_base(vcpu);
2830
2831 if (irqchip_in_kernel(vcpu->kvm)) {
2832 memset(sregs->interrupt_bitmap, 0,
2833 sizeof sregs->interrupt_bitmap);
2834 pending_vec = kvm_x86_ops->get_irq(vcpu);
2835 if (pending_vec >= 0)
2836 set_bit(pending_vec,
2837 (unsigned long *)sregs->interrupt_bitmap);
2838 } else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002839 memcpy(sregs->interrupt_bitmap, vcpu->arch.irq_pending,
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002840 sizeof sregs->interrupt_bitmap);
2841
2842 vcpu_put(vcpu);
2843
2844 return 0;
2845}
2846
2847static void set_segment(struct kvm_vcpu *vcpu,
2848 struct kvm_segment *var, int seg)
2849{
2850 return kvm_x86_ops->set_segment(vcpu, var, seg);
2851}
2852
2853int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2854 struct kvm_sregs *sregs)
2855{
2856 int mmu_reset_needed = 0;
2857 int i, pending_vec, max_bits;
2858 struct descriptor_table dt;
2859
2860 vcpu_load(vcpu);
2861
2862 dt.limit = sregs->idt.limit;
2863 dt.base = sregs->idt.base;
2864 kvm_x86_ops->set_idt(vcpu, &dt);
2865 dt.limit = sregs->gdt.limit;
2866 dt.base = sregs->gdt.base;
2867 kvm_x86_ops->set_gdt(vcpu, &dt);
2868
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002869 vcpu->arch.cr2 = sregs->cr2;
2870 mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
2871 vcpu->arch.cr3 = sregs->cr3;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002872
2873 set_cr8(vcpu, sregs->cr8);
2874
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002875 mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002876 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002877 kvm_set_apic_base(vcpu, sregs->apic_base);
2878
2879 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2880
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002881 mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002882 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00002883 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002884
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002885 mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002886 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
2887 if (!is_long_mode(vcpu) && is_pae(vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002888 load_pdptrs(vcpu, vcpu->arch.cr3);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002889
2890 if (mmu_reset_needed)
2891 kvm_mmu_reset_context(vcpu);
2892
2893 if (!irqchip_in_kernel(vcpu->kvm)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002894 memcpy(vcpu->arch.irq_pending, sregs->interrupt_bitmap,
2895 sizeof vcpu->arch.irq_pending);
2896 vcpu->arch.irq_summary = 0;
2897 for (i = 0; i < ARRAY_SIZE(vcpu->arch.irq_pending); ++i)
2898 if (vcpu->arch.irq_pending[i])
2899 __set_bit(i, &vcpu->arch.irq_summary);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002900 } else {
2901 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
2902 pending_vec = find_first_bit(
2903 (const unsigned long *)sregs->interrupt_bitmap,
2904 max_bits);
2905 /* Only pending external irq is handled here */
2906 if (pending_vec < max_bits) {
2907 kvm_x86_ops->set_irq(vcpu, pending_vec);
2908 pr_debug("Set back pending irq %d\n",
2909 pending_vec);
2910 }
2911 }
2912
2913 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2914 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2915 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2916 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2917 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2918 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2919
2920 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2921 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2922
2923 vcpu_put(vcpu);
2924
2925 return 0;
2926}
2927
2928int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2929 struct kvm_debug_guest *dbg)
2930{
2931 int r;
2932
2933 vcpu_load(vcpu);
2934
2935 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
2936
2937 vcpu_put(vcpu);
2938
2939 return r;
2940}
2941
2942/*
Hollis Blanchardd0752062007-10-31 17:24:25 -05002943 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2944 * we have asm/x86/processor.h
2945 */
2946struct fxsave {
2947 u16 cwd;
2948 u16 swd;
2949 u16 twd;
2950 u16 fop;
2951 u64 rip;
2952 u64 rdp;
2953 u32 mxcsr;
2954 u32 mxcsr_mask;
2955 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2956#ifdef CONFIG_X86_64
2957 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2958#else
2959 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2960#endif
2961};
2962
Zhang Xiantao8b006792007-11-16 13:05:55 +08002963/*
2964 * Translate a guest virtual address to a guest physical address.
2965 */
2966int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2967 struct kvm_translation *tr)
2968{
2969 unsigned long vaddr = tr->linear_address;
2970 gpa_t gpa;
2971
2972 vcpu_load(vcpu);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002973 down_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002974 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002975 up_read(&vcpu->kvm->slots_lock);
Zhang Xiantao8b006792007-11-16 13:05:55 +08002976 tr->physical_address = gpa;
2977 tr->valid = gpa != UNMAPPED_GVA;
2978 tr->writeable = 1;
2979 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08002980 vcpu_put(vcpu);
2981
2982 return 0;
2983}
2984
Hollis Blanchardd0752062007-10-31 17:24:25 -05002985int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2986{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002987 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05002988
2989 vcpu_load(vcpu);
2990
2991 memcpy(fpu->fpr, fxsave->st_space, 128);
2992 fpu->fcw = fxsave->cwd;
2993 fpu->fsw = fxsave->swd;
2994 fpu->ftwx = fxsave->twd;
2995 fpu->last_opcode = fxsave->fop;
2996 fpu->last_ip = fxsave->rip;
2997 fpu->last_dp = fxsave->rdp;
2998 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2999
3000 vcpu_put(vcpu);
3001
3002 return 0;
3003}
3004
3005int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3006{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003007 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05003008
3009 vcpu_load(vcpu);
3010
3011 memcpy(fxsave->st_space, fpu->fpr, 128);
3012 fxsave->cwd = fpu->fcw;
3013 fxsave->swd = fpu->fsw;
3014 fxsave->twd = fpu->ftwx;
3015 fxsave->fop = fpu->last_opcode;
3016 fxsave->rip = fpu->last_ip;
3017 fxsave->rdp = fpu->last_dp;
3018 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
3019
3020 vcpu_put(vcpu);
3021
3022 return 0;
3023}
3024
3025void fx_init(struct kvm_vcpu *vcpu)
3026{
3027 unsigned after_mxcsr_mask;
3028
3029 /* Initialize guest FPU by resetting ours and saving into guest's */
3030 preempt_disable();
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003031 fx_save(&vcpu->arch.host_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05003032 fpu_init();
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003033 fx_save(&vcpu->arch.guest_fx_image);
3034 fx_restore(&vcpu->arch.host_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05003035 preempt_enable();
3036
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003037 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05003038 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003039 vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
3040 memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
Hollis Blanchardd0752062007-10-31 17:24:25 -05003041 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
3042}
3043EXPORT_SYMBOL_GPL(fx_init);
3044
3045void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
3046{
3047 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
3048 return;
3049
3050 vcpu->guest_fpu_loaded = 1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003051 fx_save(&vcpu->arch.host_fx_image);
3052 fx_restore(&vcpu->arch.guest_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05003053}
3054EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
3055
3056void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
3057{
3058 if (!vcpu->guest_fpu_loaded)
3059 return;
3060
3061 vcpu->guest_fpu_loaded = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003062 fx_save(&vcpu->arch.guest_fx_image);
3063 fx_restore(&vcpu->arch.host_fx_image);
Avi Kivityf096ed82007-11-18 13:54:33 +02003064 ++vcpu->stat.fpu_reload;
Hollis Blanchardd0752062007-10-31 17:24:25 -05003065}
3066EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003067
3068void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
3069{
3070 kvm_x86_ops->vcpu_free(vcpu);
3071}
3072
3073struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
3074 unsigned int id)
3075{
Avi Kivity26e52152007-11-20 15:30:24 +02003076 return kvm_x86_ops->vcpu_create(kvm, id);
3077}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003078
Avi Kivity26e52152007-11-20 15:30:24 +02003079int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
3080{
3081 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003082
3083 /* We do fxsave: this must be aligned. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003084 BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003085
3086 vcpu_load(vcpu);
3087 r = kvm_arch_vcpu_reset(vcpu);
3088 if (r == 0)
3089 r = kvm_mmu_setup(vcpu);
3090 vcpu_put(vcpu);
3091 if (r < 0)
3092 goto free_vcpu;
3093
Avi Kivity26e52152007-11-20 15:30:24 +02003094 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003095free_vcpu:
3096 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivity26e52152007-11-20 15:30:24 +02003097 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003098}
3099
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06003100void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003101{
3102 vcpu_load(vcpu);
3103 kvm_mmu_unload(vcpu);
3104 vcpu_put(vcpu);
3105
3106 kvm_x86_ops->vcpu_free(vcpu);
3107}
3108
3109int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
3110{
3111 return kvm_x86_ops->vcpu_reset(vcpu);
3112}
3113
3114void kvm_arch_hardware_enable(void *garbage)
3115{
3116 kvm_x86_ops->hardware_enable(garbage);
3117}
3118
3119void kvm_arch_hardware_disable(void *garbage)
3120{
3121 kvm_x86_ops->hardware_disable(garbage);
3122}
3123
3124int kvm_arch_hardware_setup(void)
3125{
3126 return kvm_x86_ops->hardware_setup();
3127}
3128
3129void kvm_arch_hardware_unsetup(void)
3130{
3131 kvm_x86_ops->hardware_unsetup();
3132}
3133
3134void kvm_arch_check_processor_compat(void *rtn)
3135{
3136 kvm_x86_ops->check_processor_compatibility(rtn);
3137}
3138
3139int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
3140{
3141 struct page *page;
3142 struct kvm *kvm;
3143 int r;
3144
3145 BUG_ON(vcpu->kvm == NULL);
3146 kvm = vcpu->kvm;
3147
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003148 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003149 if (!irqchip_in_kernel(kvm) || vcpu->vcpu_id == 0)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003150 vcpu->arch.mp_state = VCPU_MP_STATE_RUNNABLE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003151 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003152 vcpu->arch.mp_state = VCPU_MP_STATE_UNINITIALIZED;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003153
3154 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
3155 if (!page) {
3156 r = -ENOMEM;
3157 goto fail;
3158 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003159 vcpu->arch.pio_data = page_address(page);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003160
3161 r = kvm_mmu_create(vcpu);
3162 if (r < 0)
3163 goto fail_free_pio_data;
3164
3165 if (irqchip_in_kernel(kvm)) {
3166 r = kvm_create_lapic(vcpu);
3167 if (r < 0)
3168 goto fail_mmu_destroy;
3169 }
3170
3171 return 0;
3172
3173fail_mmu_destroy:
3174 kvm_mmu_destroy(vcpu);
3175fail_free_pio_data:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003176 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003177fail:
3178 return r;
3179}
3180
3181void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
3182{
3183 kvm_free_lapic(vcpu);
3184 kvm_mmu_destroy(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003185 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003186}
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08003187
3188struct kvm *kvm_arch_create_vm(void)
3189{
3190 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
3191
3192 if (!kvm)
3193 return ERR_PTR(-ENOMEM);
3194
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003195 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08003196
3197 return kvm;
3198}
3199
3200static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
3201{
3202 vcpu_load(vcpu);
3203 kvm_mmu_unload(vcpu);
3204 vcpu_put(vcpu);
3205}
3206
3207static void kvm_free_vcpus(struct kvm *kvm)
3208{
3209 unsigned int i;
3210
3211 /*
3212 * Unpin any mmu pages first.
3213 */
3214 for (i = 0; i < KVM_MAX_VCPUS; ++i)
3215 if (kvm->vcpus[i])
3216 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
3217 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
3218 if (kvm->vcpus[i]) {
3219 kvm_arch_vcpu_free(kvm->vcpus[i]);
3220 kvm->vcpus[i] = NULL;
3221 }
3222 }
3223
3224}
3225
3226void kvm_arch_destroy_vm(struct kvm *kvm)
3227{
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08003228 kfree(kvm->arch.vpic);
3229 kfree(kvm->arch.vioapic);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08003230 kvm_free_vcpus(kvm);
3231 kvm_free_physmem(kvm);
3232 kfree(kvm);
3233}
Zhang Xiantao0de10342007-11-20 16:25:04 +08003234
3235int kvm_arch_set_memory_region(struct kvm *kvm,
3236 struct kvm_userspace_memory_region *mem,
3237 struct kvm_memory_slot old,
3238 int user_alloc)
3239{
3240 int npages = mem->memory_size >> PAGE_SHIFT;
3241 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
3242
3243 /*To keep backward compatibility with older userspace,
3244 *x86 needs to hanlde !user_alloc case.
3245 */
3246 if (!user_alloc) {
3247 if (npages && !old.rmap) {
Izik Eidus72dc67a2008-02-10 18:04:15 +02003248 down_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08003249 memslot->userspace_addr = do_mmap(NULL, 0,
3250 npages * PAGE_SIZE,
3251 PROT_READ | PROT_WRITE,
3252 MAP_SHARED | MAP_ANONYMOUS,
3253 0);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003254 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08003255
3256 if (IS_ERR((void *)memslot->userspace_addr))
3257 return PTR_ERR((void *)memslot->userspace_addr);
3258 } else {
3259 if (!old.user_alloc && old.rmap) {
3260 int ret;
3261
Izik Eidus72dc67a2008-02-10 18:04:15 +02003262 down_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08003263 ret = do_munmap(current->mm, old.userspace_addr,
3264 old.npages * PAGE_SIZE);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003265 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08003266 if (ret < 0)
3267 printk(KERN_WARNING
3268 "kvm_vm_ioctl_set_memory_region: "
3269 "failed to munmap memory\n");
3270 }
3271 }
3272 }
3273
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003274 if (!kvm->arch.n_requested_mmu_pages) {
Zhang Xiantao0de10342007-11-20 16:25:04 +08003275 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
3276 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
3277 }
3278
3279 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
3280 kvm_flush_remote_tlbs(kvm);
3281
3282 return 0;
3283}
Zhang Xiantao1d737c82007-12-14 09:35:10 +08003284
3285int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
3286{
3287 return vcpu->arch.mp_state == VCPU_MP_STATE_RUNNABLE
3288 || vcpu->arch.mp_state == VCPU_MP_STATE_SIPI_RECEIVED;
3289}
Zhang Xiantao57361992007-12-17 14:21:40 +08003290
3291static void vcpu_kick_intr(void *info)
3292{
3293#ifdef DEBUG
3294 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info;
3295 printk(KERN_DEBUG "vcpu_kick_intr %p \n", vcpu);
3296#endif
3297}
3298
3299void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
3300{
3301 int ipi_pcpu = vcpu->cpu;
3302
3303 if (waitqueue_active(&vcpu->wq)) {
3304 wake_up_interruptible(&vcpu->wq);
3305 ++vcpu->stat.halt_wakeup;
3306 }
3307 if (vcpu->guest_mode)
3308 smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0, 0);
3309}