blob: 256c0fc92b67b858417fa00e3d3e1fe5ae0280bc [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
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020022#include <linux/clocksource.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020023#include <linux/kvm.h>
24#include <linux/fs.h>
25#include <linux/vmalloc.h>
Carsten Otte5fb76f92007-10-29 16:08:51 +010026#include <linux/module.h>
Zhang Xiantao0de10342007-11-20 16:25:04 +080027#include <linux/mman.h>
Marcelo Tosatti2bacc552007-12-12 10:46:12 -050028#include <linux/highmem.h>
Carsten Otte043405e2007-10-10 17:16:19 +020029
30#include <asm/uaccess.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080031#include <asm/msr.h>
Carsten Otte043405e2007-10-10 17:16:19 +020032
Carsten Otte313a3dc2007-10-11 19:16:52 +020033#define MAX_IO_MSRS 256
Carsten Ottea03490e2007-10-29 16:09:35 +010034#define CR0_RESERVED_BITS \
35 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
36 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
37 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
38#define CR4_RESERVED_BITS \
39 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
40 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
41 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
42 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
43
44#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
Joerg Roedel50a37eb2008-01-31 14:57:38 +010045/* EFER defaults:
46 * - enable syscall per default because its emulated by KVM
47 * - enable LME and LMA per default on 64 bit KVM
48 */
49#ifdef CONFIG_X86_64
50static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL;
51#else
52static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL;
53#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +020054
Avi Kivityba1389b2007-11-18 16:24:12 +020055#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
56#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
Hollis Blanchard417bc302007-10-31 17:24:23 -050057
Avi Kivity674eea02008-02-11 18:37:23 +020058static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
59 struct kvm_cpuid_entry2 __user *entries);
60
Zhang Xiantao97896d02007-11-14 20:09:30 +080061struct kvm_x86_ops *kvm_x86_ops;
62
Hollis Blanchard417bc302007-10-31 17:24:23 -050063struct kvm_stats_debugfs_item debugfs_entries[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +020064 { "pf_fixed", VCPU_STAT(pf_fixed) },
65 { "pf_guest", VCPU_STAT(pf_guest) },
66 { "tlb_flush", VCPU_STAT(tlb_flush) },
67 { "invlpg", VCPU_STAT(invlpg) },
68 { "exits", VCPU_STAT(exits) },
69 { "io_exits", VCPU_STAT(io_exits) },
70 { "mmio_exits", VCPU_STAT(mmio_exits) },
71 { "signal_exits", VCPU_STAT(signal_exits) },
72 { "irq_window", VCPU_STAT(irq_window_exits) },
73 { "halt_exits", VCPU_STAT(halt_exits) },
74 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
75 { "request_irq", VCPU_STAT(request_irq_exits) },
76 { "irq_exits", VCPU_STAT(irq_exits) },
77 { "host_state_reload", VCPU_STAT(host_state_reload) },
78 { "efer_reload", VCPU_STAT(efer_reload) },
79 { "fpu_reload", VCPU_STAT(fpu_reload) },
80 { "insn_emulation", VCPU_STAT(insn_emulation) },
81 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
Avi Kivity4cee5762007-11-18 16:37:07 +020082 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
83 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
84 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
85 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
86 { "mmu_flooded", VM_STAT(mmu_flooded) },
87 { "mmu_recycled", VM_STAT(mmu_recycled) },
Avi Kivitydfc5aa02007-12-18 19:47:18 +020088 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
Avi Kivity0f74a242007-11-20 23:01:14 +020089 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Hollis Blanchard417bc302007-10-31 17:24:23 -050090 { NULL }
91};
92
93
Carsten Otte5fb76f92007-10-29 16:08:51 +010094unsigned long segment_base(u16 selector)
95{
96 struct descriptor_table gdt;
97 struct segment_descriptor *d;
98 unsigned long table_base;
99 unsigned long v;
100
101 if (selector == 0)
102 return 0;
103
104 asm("sgdt %0" : "=m"(gdt));
105 table_base = gdt.base;
106
107 if (selector & 4) { /* from ldt */
108 u16 ldt_selector;
109
110 asm("sldt %0" : "=g"(ldt_selector));
111 table_base = segment_base(ldt_selector);
112 }
113 d = (struct segment_descriptor *)(table_base + (selector & ~7));
114 v = d->base_low | ((unsigned long)d->base_mid << 16) |
115 ((unsigned long)d->base_high << 24);
116#ifdef CONFIG_X86_64
117 if (d->system == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
118 v |= ((unsigned long) \
119 ((struct segment_descriptor_64 *)d)->base_higher) << 32;
120#endif
121 return v;
122}
123EXPORT_SYMBOL_GPL(segment_base);
124
Carsten Otte6866b832007-10-29 16:09:10 +0100125u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
126{
127 if (irqchip_in_kernel(vcpu->kvm))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800128 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100129 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800130 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100131}
132EXPORT_SYMBOL_GPL(kvm_get_apic_base);
133
134void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
135{
136 /* TODO: reserve bits check */
137 if (irqchip_in_kernel(vcpu->kvm))
138 kvm_lapic_set_base(vcpu, data);
139 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800140 vcpu->arch.apic_base = data;
Carsten Otte6866b832007-10-29 16:09:10 +0100141}
142EXPORT_SYMBOL_GPL(kvm_set_apic_base);
143
Avi Kivity298101d2007-11-25 13:41:11 +0200144void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
145{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800146 WARN_ON(vcpu->arch.exception.pending);
147 vcpu->arch.exception.pending = true;
148 vcpu->arch.exception.has_error_code = false;
149 vcpu->arch.exception.nr = nr;
Avi Kivity298101d2007-11-25 13:41:11 +0200150}
151EXPORT_SYMBOL_GPL(kvm_queue_exception);
152
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200153void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
154 u32 error_code)
155{
156 ++vcpu->stat.pf_guest;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800157 if (vcpu->arch.exception.pending && vcpu->arch.exception.nr == PF_VECTOR) {
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200158 printk(KERN_DEBUG "kvm: inject_page_fault:"
159 " double fault 0x%lx\n", addr);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800160 vcpu->arch.exception.nr = DF_VECTOR;
161 vcpu->arch.exception.error_code = 0;
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200162 return;
163 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800164 vcpu->arch.cr2 = addr;
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200165 kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
166}
167
Avi Kivity298101d2007-11-25 13:41:11 +0200168void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
169{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800170 WARN_ON(vcpu->arch.exception.pending);
171 vcpu->arch.exception.pending = true;
172 vcpu->arch.exception.has_error_code = true;
173 vcpu->arch.exception.nr = nr;
174 vcpu->arch.exception.error_code = error_code;
Avi Kivity298101d2007-11-25 13:41:11 +0200175}
176EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
177
178static void __queue_exception(struct kvm_vcpu *vcpu)
179{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800180 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
181 vcpu->arch.exception.has_error_code,
182 vcpu->arch.exception.error_code);
Avi Kivity298101d2007-11-25 13:41:11 +0200183}
184
Carsten Ottea03490e2007-10-29 16:09:35 +0100185/*
186 * Load the pae pdptrs. Return true is they are all valid.
187 */
188int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
189{
190 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
191 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
192 int i;
193 int ret;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800194 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100195
Izik Eidus72dc67a2008-02-10 18:04:15 +0200196 down_read(&vcpu->kvm->slots_lock);
Carsten Ottea03490e2007-10-29 16:09:35 +0100197 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
198 offset * sizeof(u64), sizeof(pdpte));
199 if (ret < 0) {
200 ret = 0;
201 goto out;
202 }
203 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
204 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
205 ret = 0;
206 goto out;
207 }
208 }
209 ret = 1;
210
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800211 memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
Carsten Ottea03490e2007-10-29 16:09:35 +0100212out:
Izik Eidus72dc67a2008-02-10 18:04:15 +0200213 up_read(&vcpu->kvm->slots_lock);
Carsten Ottea03490e2007-10-29 16:09:35 +0100214
215 return ret;
216}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100217EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100218
Avi Kivityd835dfe2007-11-21 02:57:59 +0200219static bool pdptrs_changed(struct kvm_vcpu *vcpu)
220{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800221 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Avi Kivityd835dfe2007-11-21 02:57:59 +0200222 bool changed = true;
223 int r;
224
225 if (is_long_mode(vcpu) || !is_pae(vcpu))
226 return false;
227
Izik Eidus72dc67a2008-02-10 18:04:15 +0200228 down_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800229 r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
Avi Kivityd835dfe2007-11-21 02:57:59 +0200230 if (r < 0)
231 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800232 changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200233out:
Izik Eidus72dc67a2008-02-10 18:04:15 +0200234 up_read(&vcpu->kvm->slots_lock);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200235
236 return changed;
237}
238
Carsten Ottea03490e2007-10-29 16:09:35 +0100239void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
240{
241 if (cr0 & CR0_RESERVED_BITS) {
242 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800243 cr0, vcpu->arch.cr0);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200244 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100245 return;
246 }
247
248 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
249 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200250 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100251 return;
252 }
253
254 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
255 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
256 "and a clear PE flag\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200257 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100258 return;
259 }
260
261 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
262#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800263 if ((vcpu->arch.shadow_efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100264 int cs_db, cs_l;
265
266 if (!is_pae(vcpu)) {
267 printk(KERN_DEBUG "set_cr0: #GP, start paging "
268 "in long mode while PAE is disabled\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200269 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100270 return;
271 }
272 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
273 if (cs_l) {
274 printk(KERN_DEBUG "set_cr0: #GP, start paging "
275 "in long mode while CS.L == 1\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200276 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100277 return;
278
279 }
280 } else
281#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800282 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100283 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
284 "reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200285 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100286 return;
287 }
288
289 }
290
291 kvm_x86_ops->set_cr0(vcpu, cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800292 vcpu->arch.cr0 = cr0;
Carsten Ottea03490e2007-10-29 16:09:35 +0100293
Carsten Ottea03490e2007-10-29 16:09:35 +0100294 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100295 return;
296}
297EXPORT_SYMBOL_GPL(set_cr0);
298
299void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
300{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800301 set_cr0(vcpu, (vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100302}
303EXPORT_SYMBOL_GPL(lmsw);
304
305void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
306{
307 if (cr4 & CR4_RESERVED_BITS) {
308 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200309 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100310 return;
311 }
312
313 if (is_long_mode(vcpu)) {
314 if (!(cr4 & X86_CR4_PAE)) {
315 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
316 "in long mode\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200317 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100318 return;
319 }
320 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800321 && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100322 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200323 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100324 return;
325 }
326
327 if (cr4 & X86_CR4_VMXE) {
328 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200329 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100330 return;
331 }
332 kvm_x86_ops->set_cr4(vcpu, cr4);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800333 vcpu->arch.cr4 = cr4;
Carsten Ottea03490e2007-10-29 16:09:35 +0100334 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100335}
336EXPORT_SYMBOL_GPL(set_cr4);
337
338void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
339{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800340 if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
Avi Kivityd835dfe2007-11-21 02:57:59 +0200341 kvm_mmu_flush_tlb(vcpu);
342 return;
343 }
344
Carsten Ottea03490e2007-10-29 16:09:35 +0100345 if (is_long_mode(vcpu)) {
346 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
347 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200348 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100349 return;
350 }
351 } else {
352 if (is_pae(vcpu)) {
353 if (cr3 & CR3_PAE_RESERVED_BITS) {
354 printk(KERN_DEBUG
355 "set_cr3: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200356 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100357 return;
358 }
359 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
360 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
361 "reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200362 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100363 return;
364 }
365 }
366 /*
367 * We don't check reserved bits in nonpae mode, because
368 * this isn't enforced, and VMware depends on this.
369 */
370 }
371
Izik Eidus72dc67a2008-02-10 18:04:15 +0200372 down_read(&vcpu->kvm->slots_lock);
Carsten Ottea03490e2007-10-29 16:09:35 +0100373 /*
374 * Does the new cr3 value map to physical memory? (Note, we
375 * catch an invalid cr3 even in real-mode, because it would
376 * cause trouble later on when we turn on paging anyway.)
377 *
378 * A real CPU would silently accept an invalid cr3 and would
379 * attempt to use it - with largely undefined (and often hard
380 * to debug) behavior on the guest side.
381 */
382 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200383 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100384 else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800385 vcpu->arch.cr3 = cr3;
386 vcpu->arch.mmu.new_cr3(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100387 }
Izik Eidus72dc67a2008-02-10 18:04:15 +0200388 up_read(&vcpu->kvm->slots_lock);
Carsten Ottea03490e2007-10-29 16:09:35 +0100389}
390EXPORT_SYMBOL_GPL(set_cr3);
391
392void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
393{
394 if (cr8 & CR8_RESERVED_BITS) {
395 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200396 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100397 return;
398 }
399 if (irqchip_in_kernel(vcpu->kvm))
400 kvm_lapic_set_tpr(vcpu, cr8);
401 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800402 vcpu->arch.cr8 = cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100403}
404EXPORT_SYMBOL_GPL(set_cr8);
405
406unsigned long get_cr8(struct kvm_vcpu *vcpu)
407{
408 if (irqchip_in_kernel(vcpu->kvm))
409 return kvm_lapic_get_cr8(vcpu);
410 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800411 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100412}
413EXPORT_SYMBOL_GPL(get_cr8);
414
Carsten Otte043405e2007-10-10 17:16:19 +0200415/*
416 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
417 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
418 *
419 * This list is modified at module load time to reflect the
420 * capabilities of the host cpu.
421 */
422static u32 msrs_to_save[] = {
423 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
424 MSR_K6_STAR,
425#ifdef CONFIG_X86_64
426 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
427#endif
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200428 MSR_IA32_TIME_STAMP_COUNTER, MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
Carsten Otte043405e2007-10-10 17:16:19 +0200429};
430
431static unsigned num_msrs_to_save;
432
433static u32 emulated_msrs[] = {
434 MSR_IA32_MISC_ENABLE,
435};
436
Carsten Otte15c4a642007-10-30 18:44:17 +0100437static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
438{
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100439 if (efer & efer_reserved_bits) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100440 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
441 efer);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200442 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100443 return;
444 }
445
446 if (is_paging(vcpu)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800447 && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100448 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200449 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100450 return;
451 }
452
453 kvm_x86_ops->set_efer(vcpu, efer);
454
455 efer &= ~EFER_LMA;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800456 efer |= vcpu->arch.shadow_efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +0100457
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800458 vcpu->arch.shadow_efer = efer;
Carsten Otte15c4a642007-10-30 18:44:17 +0100459}
460
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100461void kvm_enable_efer_bits(u64 mask)
462{
463 efer_reserved_bits &= ~mask;
464}
465EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
466
467
Carsten Otte15c4a642007-10-30 18:44:17 +0100468/*
469 * Writes msr value into into the appropriate "register".
470 * Returns 0 on success, non-0 otherwise.
471 * Assumes vcpu_load() was already called.
472 */
473int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
474{
475 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
476}
477
Carsten Otte313a3dc2007-10-11 19:16:52 +0200478/*
479 * Adapt set_msr() to msr_io()'s calling convention
480 */
481static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
482{
483 return kvm_set_msr(vcpu, index, *data);
484}
485
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200486static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
487{
488 static int version;
489 struct kvm_wall_clock wc;
490 struct timespec wc_ts;
491
492 if (!wall_clock)
493 return;
494
495 version++;
496
497 down_read(&kvm->slots_lock);
498 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
499
500 wc_ts = current_kernel_time();
501 wc.wc_sec = wc_ts.tv_sec;
502 wc.wc_nsec = wc_ts.tv_nsec;
503 wc.wc_version = version;
504
505 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
506
507 version++;
508 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
509 up_read(&kvm->slots_lock);
510}
511
512static void kvm_write_guest_time(struct kvm_vcpu *v)
513{
514 struct timespec ts;
515 unsigned long flags;
516 struct kvm_vcpu_arch *vcpu = &v->arch;
517 void *shared_kaddr;
518
519 if ((!vcpu->time_page))
520 return;
521
522 /* Keep irq disabled to prevent changes to the clock */
523 local_irq_save(flags);
524 kvm_get_msr(v, MSR_IA32_TIME_STAMP_COUNTER,
525 &vcpu->hv_clock.tsc_timestamp);
526 ktime_get_ts(&ts);
527 local_irq_restore(flags);
528
529 /* With all the info we got, fill in the values */
530
531 vcpu->hv_clock.system_time = ts.tv_nsec +
532 (NSEC_PER_SEC * (u64)ts.tv_sec);
533 /*
534 * The interface expects us to write an even number signaling that the
535 * update is finished. Since the guest won't see the intermediate
536 * state, we just write "2" at the end
537 */
538 vcpu->hv_clock.version = 2;
539
540 shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
541
542 memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
543 sizeof(vcpu->hv_clock));
544
545 kunmap_atomic(shared_kaddr, KM_USER0);
546
547 mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
548}
549
Carsten Otte15c4a642007-10-30 18:44:17 +0100550
551int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
552{
553 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100554 case MSR_EFER:
555 set_efer(vcpu, data);
556 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100557 case MSR_IA32_MC0_STATUS:
558 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
559 __FUNCTION__, data);
560 break;
561 case MSR_IA32_MCG_STATUS:
562 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
563 __FUNCTION__, data);
564 break;
Joerg Roedelc7ac6792008-02-11 20:28:27 +0100565 case MSR_IA32_MCG_CTL:
566 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_CTL 0x%llx, nop\n",
567 __FUNCTION__, data);
568 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100569 case MSR_IA32_UCODE_REV:
570 case MSR_IA32_UCODE_WRITE:
571 case 0x200 ... 0x2ff: /* MTRRs */
572 break;
573 case MSR_IA32_APICBASE:
574 kvm_set_apic_base(vcpu, data);
575 break;
576 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800577 vcpu->arch.ia32_misc_enable_msr = data;
Carsten Otte15c4a642007-10-30 18:44:17 +0100578 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200579 case MSR_KVM_WALL_CLOCK:
580 vcpu->kvm->arch.wall_clock = data;
581 kvm_write_wall_clock(vcpu->kvm, data);
582 break;
583 case MSR_KVM_SYSTEM_TIME: {
584 if (vcpu->arch.time_page) {
585 kvm_release_page_dirty(vcpu->arch.time_page);
586 vcpu->arch.time_page = NULL;
587 }
588
589 vcpu->arch.time = data;
590
591 /* we verify if the enable bit is set... */
592 if (!(data & 1))
593 break;
594
595 /* ...but clean it before doing the actual write */
596 vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
597
598 vcpu->arch.hv_clock.tsc_to_system_mul =
599 clocksource_khz2mult(tsc_khz, 22);
600 vcpu->arch.hv_clock.tsc_shift = 22;
601
602 down_read(&current->mm->mmap_sem);
603 down_read(&vcpu->kvm->slots_lock);
604 vcpu->arch.time_page =
605 gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
606 up_read(&vcpu->kvm->slots_lock);
607 up_read(&current->mm->mmap_sem);
608
609 if (is_error_page(vcpu->arch.time_page)) {
610 kvm_release_page_clean(vcpu->arch.time_page);
611 vcpu->arch.time_page = NULL;
612 }
613
614 kvm_write_guest_time(vcpu);
615 break;
616 }
Carsten Otte15c4a642007-10-30 18:44:17 +0100617 default:
Avi Kivity565f1fb2007-12-19 12:02:40 +0200618 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n", msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +0100619 return 1;
620 }
621 return 0;
622}
623EXPORT_SYMBOL_GPL(kvm_set_msr_common);
624
625
626/*
627 * Reads an msr value (of 'msr_index') into 'pdata'.
628 * Returns 0 on success, non-0 otherwise.
629 * Assumes vcpu_load() was already called.
630 */
631int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
632{
633 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
634}
635
636int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
637{
638 u64 data;
639
640 switch (msr) {
641 case 0xc0010010: /* SYSCFG */
642 case 0xc0010015: /* HWCR */
643 case MSR_IA32_PLATFORM_ID:
644 case MSR_IA32_P5_MC_ADDR:
645 case MSR_IA32_P5_MC_TYPE:
646 case MSR_IA32_MC0_CTL:
647 case MSR_IA32_MCG_STATUS:
648 case MSR_IA32_MCG_CAP:
Joerg Roedelc7ac6792008-02-11 20:28:27 +0100649 case MSR_IA32_MCG_CTL:
Carsten Otte15c4a642007-10-30 18:44:17 +0100650 case MSR_IA32_MC0_MISC:
651 case MSR_IA32_MC0_MISC+4:
652 case MSR_IA32_MC0_MISC+8:
653 case MSR_IA32_MC0_MISC+12:
654 case MSR_IA32_MC0_MISC+16:
655 case MSR_IA32_UCODE_REV:
656 case MSR_IA32_PERF_STATUS:
657 case MSR_IA32_EBL_CR_POWERON:
658 /* MTRR registers */
659 case 0xfe:
660 case 0x200 ... 0x2ff:
661 data = 0;
662 break;
663 case 0xcd: /* fsb frequency */
664 data = 3;
665 break;
666 case MSR_IA32_APICBASE:
667 data = kvm_get_apic_base(vcpu);
668 break;
669 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800670 data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +0100671 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100672 case MSR_EFER:
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800673 data = vcpu->arch.shadow_efer;
Carsten Otte15c4a642007-10-30 18:44:17 +0100674 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200675 case MSR_KVM_WALL_CLOCK:
676 data = vcpu->kvm->arch.wall_clock;
677 break;
678 case MSR_KVM_SYSTEM_TIME:
679 data = vcpu->arch.time;
680 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100681 default:
682 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
683 return 1;
684 }
685 *pdata = data;
686 return 0;
687}
688EXPORT_SYMBOL_GPL(kvm_get_msr_common);
689
Carsten Otte313a3dc2007-10-11 19:16:52 +0200690/*
691 * Read or write a bunch of msrs. All parameters are kernel addresses.
692 *
693 * @return number of msrs set successfully.
694 */
695static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
696 struct kvm_msr_entry *entries,
697 int (*do_msr)(struct kvm_vcpu *vcpu,
698 unsigned index, u64 *data))
699{
700 int i;
701
702 vcpu_load(vcpu);
703
704 for (i = 0; i < msrs->nmsrs; ++i)
705 if (do_msr(vcpu, entries[i].index, &entries[i].data))
706 break;
707
708 vcpu_put(vcpu);
709
710 return i;
711}
712
713/*
714 * Read or write a bunch of msrs. Parameters are user addresses.
715 *
716 * @return number of msrs set successfully.
717 */
718static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
719 int (*do_msr)(struct kvm_vcpu *vcpu,
720 unsigned index, u64 *data),
721 int writeback)
722{
723 struct kvm_msrs msrs;
724 struct kvm_msr_entry *entries;
725 int r, n;
726 unsigned size;
727
728 r = -EFAULT;
729 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
730 goto out;
731
732 r = -E2BIG;
733 if (msrs.nmsrs >= MAX_IO_MSRS)
734 goto out;
735
736 r = -ENOMEM;
737 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
738 entries = vmalloc(size);
739 if (!entries)
740 goto out;
741
742 r = -EFAULT;
743 if (copy_from_user(entries, user_msrs->entries, size))
744 goto out_free;
745
746 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
747 if (r < 0)
748 goto out_free;
749
750 r = -EFAULT;
751 if (writeback && copy_to_user(user_msrs->entries, entries, size))
752 goto out_free;
753
754 r = n;
755
756out_free:
757 vfree(entries);
758out:
759 return r;
760}
761
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800762/*
763 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
764 * cached on it.
765 */
766void decache_vcpus_on_cpu(int cpu)
767{
768 struct kvm *vm;
769 struct kvm_vcpu *vcpu;
770 int i;
771
772 spin_lock(&kvm_lock);
773 list_for_each_entry(vm, &vm_list, vm_list)
774 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
775 vcpu = vm->vcpus[i];
776 if (!vcpu)
777 continue;
778 /*
779 * If the vcpu is locked, then it is running on some
780 * other cpu and therefore it is not cached on the
781 * cpu in question.
782 *
783 * If it's not locked, check the last cpu it executed
784 * on.
785 */
786 if (mutex_trylock(&vcpu->mutex)) {
787 if (vcpu->cpu == cpu) {
788 kvm_x86_ops->vcpu_decache(vcpu);
789 vcpu->cpu = -1;
790 }
791 mutex_unlock(&vcpu->mutex);
792 }
793 }
794 spin_unlock(&kvm_lock);
795}
796
Zhang Xiantao018d00d2007-11-15 23:07:47 +0800797int kvm_dev_ioctl_check_extension(long ext)
798{
799 int r;
800
801 switch (ext) {
802 case KVM_CAP_IRQCHIP:
803 case KVM_CAP_HLT:
804 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
805 case KVM_CAP_USER_MEMORY:
806 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +0200807 case KVM_CAP_EXT_CPUID:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200808 case KVM_CAP_CLOCKSOURCE:
Zhang Xiantao018d00d2007-11-15 23:07:47 +0800809 r = 1;
810 break;
Avi Kivity774ead32007-12-26 13:57:04 +0200811 case KVM_CAP_VAPIC:
812 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
813 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +0800814 default:
815 r = 0;
816 break;
817 }
818 return r;
819
820}
821
Carsten Otte043405e2007-10-10 17:16:19 +0200822long kvm_arch_dev_ioctl(struct file *filp,
823 unsigned int ioctl, unsigned long arg)
824{
825 void __user *argp = (void __user *)arg;
826 long r;
827
828 switch (ioctl) {
829 case KVM_GET_MSR_INDEX_LIST: {
830 struct kvm_msr_list __user *user_msr_list = argp;
831 struct kvm_msr_list msr_list;
832 unsigned n;
833
834 r = -EFAULT;
835 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
836 goto out;
837 n = msr_list.nmsrs;
838 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
839 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
840 goto out;
841 r = -E2BIG;
842 if (n < num_msrs_to_save)
843 goto out;
844 r = -EFAULT;
845 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
846 num_msrs_to_save * sizeof(u32)))
847 goto out;
848 if (copy_to_user(user_msr_list->indices
849 + num_msrs_to_save * sizeof(u32),
850 &emulated_msrs,
851 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
852 goto out;
853 r = 0;
854 break;
855 }
Avi Kivity674eea02008-02-11 18:37:23 +0200856 case KVM_GET_SUPPORTED_CPUID: {
857 struct kvm_cpuid2 __user *cpuid_arg = argp;
858 struct kvm_cpuid2 cpuid;
859
860 r = -EFAULT;
861 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
862 goto out;
863 r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
864 cpuid_arg->entries);
865 if (r)
866 goto out;
867
868 r = -EFAULT;
869 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
870 goto out;
871 r = 0;
872 break;
873 }
Carsten Otte043405e2007-10-10 17:16:19 +0200874 default:
875 r = -EINVAL;
876 }
877out:
878 return r;
879}
880
Carsten Otte313a3dc2007-10-11 19:16:52 +0200881void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
882{
883 kvm_x86_ops->vcpu_load(vcpu, cpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200884 kvm_write_guest_time(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200885}
886
887void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
888{
889 kvm_x86_ops->vcpu_put(vcpu);
Amit Shah9327fd12007-11-15 18:38:46 +0200890 kvm_put_guest_fpu(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200891}
892
Dan Kenigsberg07716712007-11-21 17:10:04 +0200893static int is_efer_nx(void)
Carsten Otte313a3dc2007-10-11 19:16:52 +0200894{
895 u64 efer;
Carsten Otte313a3dc2007-10-11 19:16:52 +0200896
897 rdmsrl(MSR_EFER, efer);
Dan Kenigsberg07716712007-11-21 17:10:04 +0200898 return efer & EFER_NX;
899}
900
901static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
902{
903 int i;
904 struct kvm_cpuid_entry2 *e, *entry;
905
Carsten Otte313a3dc2007-10-11 19:16:52 +0200906 entry = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800907 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
908 e = &vcpu->arch.cpuid_entries[i];
Carsten Otte313a3dc2007-10-11 19:16:52 +0200909 if (e->function == 0x80000001) {
910 entry = e;
911 break;
912 }
913 }
Dan Kenigsberg07716712007-11-21 17:10:04 +0200914 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
Carsten Otte313a3dc2007-10-11 19:16:52 +0200915 entry->edx &= ~(1 << 20);
916 printk(KERN_INFO "kvm: guest NX capability removed\n");
917 }
918}
919
Dan Kenigsberg07716712007-11-21 17:10:04 +0200920/* when an old userspace process fills a new kernel module */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200921static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
922 struct kvm_cpuid *cpuid,
923 struct kvm_cpuid_entry __user *entries)
924{
Dan Kenigsberg07716712007-11-21 17:10:04 +0200925 int r, i;
926 struct kvm_cpuid_entry *cpuid_entries;
927
928 r = -E2BIG;
929 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
930 goto out;
931 r = -ENOMEM;
932 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
933 if (!cpuid_entries)
934 goto out;
935 r = -EFAULT;
936 if (copy_from_user(cpuid_entries, entries,
937 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
938 goto out_free;
939 for (i = 0; i < cpuid->nent; i++) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800940 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
941 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
942 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
943 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
944 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
945 vcpu->arch.cpuid_entries[i].index = 0;
946 vcpu->arch.cpuid_entries[i].flags = 0;
947 vcpu->arch.cpuid_entries[i].padding[0] = 0;
948 vcpu->arch.cpuid_entries[i].padding[1] = 0;
949 vcpu->arch.cpuid_entries[i].padding[2] = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +0200950 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800951 vcpu->arch.cpuid_nent = cpuid->nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +0200952 cpuid_fix_nx_cap(vcpu);
953 r = 0;
954
955out_free:
956 vfree(cpuid_entries);
957out:
958 return r;
959}
960
961static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
962 struct kvm_cpuid2 *cpuid,
963 struct kvm_cpuid_entry2 __user *entries)
964{
Carsten Otte313a3dc2007-10-11 19:16:52 +0200965 int r;
966
967 r = -E2BIG;
968 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
969 goto out;
970 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800971 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
Dan Kenigsberg07716712007-11-21 17:10:04 +0200972 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
Carsten Otte313a3dc2007-10-11 19:16:52 +0200973 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800974 vcpu->arch.cpuid_nent = cpuid->nent;
Carsten Otte313a3dc2007-10-11 19:16:52 +0200975 return 0;
976
977out:
978 return r;
979}
980
Dan Kenigsberg07716712007-11-21 17:10:04 +0200981static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
982 struct kvm_cpuid2 *cpuid,
983 struct kvm_cpuid_entry2 __user *entries)
984{
985 int r;
986
987 r = -E2BIG;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800988 if (cpuid->nent < vcpu->arch.cpuid_nent)
Dan Kenigsberg07716712007-11-21 17:10:04 +0200989 goto out;
990 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800991 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
992 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +0200993 goto out;
994 return 0;
995
996out:
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800997 cpuid->nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +0200998 return r;
999}
1000
1001static inline u32 bit(int bitno)
1002{
1003 return 1 << (bitno & 31);
1004}
1005
1006static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1007 u32 index)
1008{
1009 entry->function = function;
1010 entry->index = index;
1011 cpuid_count(entry->function, entry->index,
1012 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
1013 entry->flags = 0;
1014}
1015
1016static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1017 u32 index, int *nent, int maxnent)
1018{
1019 const u32 kvm_supported_word0_x86_features = bit(X86_FEATURE_FPU) |
1020 bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
1021 bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
1022 bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
1023 bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
1024 bit(X86_FEATURE_SEP) | bit(X86_FEATURE_PGE) |
1025 bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
1026 bit(X86_FEATURE_CLFLSH) | bit(X86_FEATURE_MMX) |
1027 bit(X86_FEATURE_FXSR) | bit(X86_FEATURE_XMM) |
1028 bit(X86_FEATURE_XMM2) | bit(X86_FEATURE_SELFSNOOP);
1029 const u32 kvm_supported_word1_x86_features = bit(X86_FEATURE_FPU) |
1030 bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
1031 bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
1032 bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
1033 bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
1034 bit(X86_FEATURE_PGE) |
1035 bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
1036 bit(X86_FEATURE_MMX) | bit(X86_FEATURE_FXSR) |
1037 bit(X86_FEATURE_SYSCALL) |
1038 (bit(X86_FEATURE_NX) && is_efer_nx()) |
1039#ifdef CONFIG_X86_64
1040 bit(X86_FEATURE_LM) |
1041#endif
1042 bit(X86_FEATURE_MMXEXT) |
1043 bit(X86_FEATURE_3DNOWEXT) |
1044 bit(X86_FEATURE_3DNOW);
1045 const u32 kvm_supported_word3_x86_features =
1046 bit(X86_FEATURE_XMM3) | bit(X86_FEATURE_CX16);
1047 const u32 kvm_supported_word6_x86_features =
1048 bit(X86_FEATURE_LAHF_LM) | bit(X86_FEATURE_CMP_LEGACY);
1049
1050 /* all func 2 cpuid_count() should be called on the same cpu */
1051 get_cpu();
1052 do_cpuid_1_ent(entry, function, index);
1053 ++*nent;
1054
1055 switch (function) {
1056 case 0:
1057 entry->eax = min(entry->eax, (u32)0xb);
1058 break;
1059 case 1:
1060 entry->edx &= kvm_supported_word0_x86_features;
1061 entry->ecx &= kvm_supported_word3_x86_features;
1062 break;
1063 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1064 * may return different values. This forces us to get_cpu() before
1065 * issuing the first command, and also to emulate this annoying behavior
1066 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1067 case 2: {
1068 int t, times = entry->eax & 0xff;
1069
1070 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1071 for (t = 1; t < times && *nent < maxnent; ++t) {
1072 do_cpuid_1_ent(&entry[t], function, 0);
1073 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1074 ++*nent;
1075 }
1076 break;
1077 }
1078 /* function 4 and 0xb have additional index. */
1079 case 4: {
1080 int index, cache_type;
1081
1082 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1083 /* read more entries until cache_type is zero */
1084 for (index = 1; *nent < maxnent; ++index) {
1085 cache_type = entry[index - 1].eax & 0x1f;
1086 if (!cache_type)
1087 break;
1088 do_cpuid_1_ent(&entry[index], function, index);
1089 entry[index].flags |=
1090 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1091 ++*nent;
1092 }
1093 break;
1094 }
1095 case 0xb: {
1096 int index, level_type;
1097
1098 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1099 /* read more entries until level_type is zero */
1100 for (index = 1; *nent < maxnent; ++index) {
1101 level_type = entry[index - 1].ecx & 0xff;
1102 if (!level_type)
1103 break;
1104 do_cpuid_1_ent(&entry[index], function, index);
1105 entry[index].flags |=
1106 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1107 ++*nent;
1108 }
1109 break;
1110 }
1111 case 0x80000000:
1112 entry->eax = min(entry->eax, 0x8000001a);
1113 break;
1114 case 0x80000001:
1115 entry->edx &= kvm_supported_word1_x86_features;
1116 entry->ecx &= kvm_supported_word6_x86_features;
1117 break;
1118 }
1119 put_cpu();
1120}
1121
Avi Kivity674eea02008-02-11 18:37:23 +02001122static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
Dan Kenigsberg07716712007-11-21 17:10:04 +02001123 struct kvm_cpuid_entry2 __user *entries)
1124{
1125 struct kvm_cpuid_entry2 *cpuid_entries;
1126 int limit, nent = 0, r = -E2BIG;
1127 u32 func;
1128
1129 if (cpuid->nent < 1)
1130 goto out;
1131 r = -ENOMEM;
1132 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
1133 if (!cpuid_entries)
1134 goto out;
1135
1136 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
1137 limit = cpuid_entries[0].eax;
1138 for (func = 1; func <= limit && nent < cpuid->nent; ++func)
1139 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1140 &nent, cpuid->nent);
1141 r = -E2BIG;
1142 if (nent >= cpuid->nent)
1143 goto out_free;
1144
1145 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
1146 limit = cpuid_entries[nent - 1].eax;
1147 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
1148 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1149 &nent, cpuid->nent);
1150 r = -EFAULT;
1151 if (copy_to_user(entries, cpuid_entries,
1152 nent * sizeof(struct kvm_cpuid_entry2)))
1153 goto out_free;
1154 cpuid->nent = nent;
1155 r = 0;
1156
1157out_free:
1158 vfree(cpuid_entries);
1159out:
1160 return r;
1161}
1162
Carsten Otte313a3dc2007-10-11 19:16:52 +02001163static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
1164 struct kvm_lapic_state *s)
1165{
1166 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001167 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001168 vcpu_put(vcpu);
1169
1170 return 0;
1171}
1172
1173static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
1174 struct kvm_lapic_state *s)
1175{
1176 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001177 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001178 kvm_apic_post_state_restore(vcpu);
1179 vcpu_put(vcpu);
1180
1181 return 0;
1182}
1183
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001184static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
1185 struct kvm_interrupt *irq)
1186{
1187 if (irq->irq < 0 || irq->irq >= 256)
1188 return -EINVAL;
1189 if (irqchip_in_kernel(vcpu->kvm))
1190 return -ENXIO;
1191 vcpu_load(vcpu);
1192
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001193 set_bit(irq->irq, vcpu->arch.irq_pending);
1194 set_bit(irq->irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001195
1196 vcpu_put(vcpu);
1197
1198 return 0;
1199}
1200
Avi Kivityb209749f2007-10-22 16:50:39 +02001201static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
1202 struct kvm_tpr_access_ctl *tac)
1203{
1204 if (tac->flags)
1205 return -EINVAL;
1206 vcpu->arch.tpr_access_reporting = !!tac->enabled;
1207 return 0;
1208}
1209
Carsten Otte313a3dc2007-10-11 19:16:52 +02001210long kvm_arch_vcpu_ioctl(struct file *filp,
1211 unsigned int ioctl, unsigned long arg)
1212{
1213 struct kvm_vcpu *vcpu = filp->private_data;
1214 void __user *argp = (void __user *)arg;
1215 int r;
1216
1217 switch (ioctl) {
1218 case KVM_GET_LAPIC: {
1219 struct kvm_lapic_state lapic;
1220
1221 memset(&lapic, 0, sizeof lapic);
1222 r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic);
1223 if (r)
1224 goto out;
1225 r = -EFAULT;
1226 if (copy_to_user(argp, &lapic, sizeof lapic))
1227 goto out;
1228 r = 0;
1229 break;
1230 }
1231 case KVM_SET_LAPIC: {
1232 struct kvm_lapic_state lapic;
1233
1234 r = -EFAULT;
1235 if (copy_from_user(&lapic, argp, sizeof lapic))
1236 goto out;
1237 r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);;
1238 if (r)
1239 goto out;
1240 r = 0;
1241 break;
1242 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001243 case KVM_INTERRUPT: {
1244 struct kvm_interrupt irq;
1245
1246 r = -EFAULT;
1247 if (copy_from_user(&irq, argp, sizeof irq))
1248 goto out;
1249 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1250 if (r)
1251 goto out;
1252 r = 0;
1253 break;
1254 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001255 case KVM_SET_CPUID: {
1256 struct kvm_cpuid __user *cpuid_arg = argp;
1257 struct kvm_cpuid cpuid;
1258
1259 r = -EFAULT;
1260 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1261 goto out;
1262 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
1263 if (r)
1264 goto out;
1265 break;
1266 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02001267 case KVM_SET_CPUID2: {
1268 struct kvm_cpuid2 __user *cpuid_arg = argp;
1269 struct kvm_cpuid2 cpuid;
1270
1271 r = -EFAULT;
1272 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1273 goto out;
1274 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
1275 cpuid_arg->entries);
1276 if (r)
1277 goto out;
1278 break;
1279 }
1280 case KVM_GET_CPUID2: {
1281 struct kvm_cpuid2 __user *cpuid_arg = argp;
1282 struct kvm_cpuid2 cpuid;
1283
1284 r = -EFAULT;
1285 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1286 goto out;
1287 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
1288 cpuid_arg->entries);
1289 if (r)
1290 goto out;
1291 r = -EFAULT;
1292 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1293 goto out;
1294 r = 0;
1295 break;
1296 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001297 case KVM_GET_MSRS:
1298 r = msr_io(vcpu, argp, kvm_get_msr, 1);
1299 break;
1300 case KVM_SET_MSRS:
1301 r = msr_io(vcpu, argp, do_set_msr, 0);
1302 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02001303 case KVM_TPR_ACCESS_REPORTING: {
1304 struct kvm_tpr_access_ctl tac;
1305
1306 r = -EFAULT;
1307 if (copy_from_user(&tac, argp, sizeof tac))
1308 goto out;
1309 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
1310 if (r)
1311 goto out;
1312 r = -EFAULT;
1313 if (copy_to_user(argp, &tac, sizeof tac))
1314 goto out;
1315 r = 0;
1316 break;
1317 };
Avi Kivityb93463a2007-10-25 16:52:32 +02001318 case KVM_SET_VAPIC_ADDR: {
1319 struct kvm_vapic_addr va;
1320
1321 r = -EINVAL;
1322 if (!irqchip_in_kernel(vcpu->kvm))
1323 goto out;
1324 r = -EFAULT;
1325 if (copy_from_user(&va, argp, sizeof va))
1326 goto out;
1327 r = 0;
1328 kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
1329 break;
1330 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001331 default:
1332 r = -EINVAL;
1333 }
1334out:
1335 return r;
1336}
1337
Carsten Otte1fe779f2007-10-29 16:08:35 +01001338static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
1339{
1340 int ret;
1341
1342 if (addr > (unsigned int)(-3 * PAGE_SIZE))
1343 return -1;
1344 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
1345 return ret;
1346}
1347
1348static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
1349 u32 kvm_nr_mmu_pages)
1350{
1351 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
1352 return -EINVAL;
1353
Izik Eidus72dc67a2008-02-10 18:04:15 +02001354 down_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001355
1356 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001357 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001358
Izik Eidus72dc67a2008-02-10 18:04:15 +02001359 up_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001360 return 0;
1361}
1362
1363static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
1364{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001365 return kvm->arch.n_alloc_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001366}
1367
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08001368gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
1369{
1370 int i;
1371 struct kvm_mem_alias *alias;
1372
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001373 for (i = 0; i < kvm->arch.naliases; ++i) {
1374 alias = &kvm->arch.aliases[i];
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08001375 if (gfn >= alias->base_gfn
1376 && gfn < alias->base_gfn + alias->npages)
1377 return alias->target_gfn + gfn - alias->base_gfn;
1378 }
1379 return gfn;
1380}
1381
Carsten Otte1fe779f2007-10-29 16:08:35 +01001382/*
1383 * Set a new alias region. Aliases map a portion of physical memory into
1384 * another portion. This is useful for memory windows, for example the PC
1385 * VGA region.
1386 */
1387static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
1388 struct kvm_memory_alias *alias)
1389{
1390 int r, n;
1391 struct kvm_mem_alias *p;
1392
1393 r = -EINVAL;
1394 /* General sanity checks */
1395 if (alias->memory_size & (PAGE_SIZE - 1))
1396 goto out;
1397 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
1398 goto out;
1399 if (alias->slot >= KVM_ALIAS_SLOTS)
1400 goto out;
1401 if (alias->guest_phys_addr + alias->memory_size
1402 < alias->guest_phys_addr)
1403 goto out;
1404 if (alias->target_phys_addr + alias->memory_size
1405 < alias->target_phys_addr)
1406 goto out;
1407
Izik Eidus72dc67a2008-02-10 18:04:15 +02001408 down_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001409
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001410 p = &kvm->arch.aliases[alias->slot];
Carsten Otte1fe779f2007-10-29 16:08:35 +01001411 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
1412 p->npages = alias->memory_size >> PAGE_SHIFT;
1413 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
1414
1415 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001416 if (kvm->arch.aliases[n - 1].npages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01001417 break;
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001418 kvm->arch.naliases = n;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001419
1420 kvm_mmu_zap_all(kvm);
1421
Izik Eidus72dc67a2008-02-10 18:04:15 +02001422 up_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001423
1424 return 0;
1425
1426out:
1427 return r;
1428}
1429
1430static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1431{
1432 int r;
1433
1434 r = 0;
1435 switch (chip->chip_id) {
1436 case KVM_IRQCHIP_PIC_MASTER:
1437 memcpy(&chip->chip.pic,
1438 &pic_irqchip(kvm)->pics[0],
1439 sizeof(struct kvm_pic_state));
1440 break;
1441 case KVM_IRQCHIP_PIC_SLAVE:
1442 memcpy(&chip->chip.pic,
1443 &pic_irqchip(kvm)->pics[1],
1444 sizeof(struct kvm_pic_state));
1445 break;
1446 case KVM_IRQCHIP_IOAPIC:
1447 memcpy(&chip->chip.ioapic,
1448 ioapic_irqchip(kvm),
1449 sizeof(struct kvm_ioapic_state));
1450 break;
1451 default:
1452 r = -EINVAL;
1453 break;
1454 }
1455 return r;
1456}
1457
1458static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1459{
1460 int r;
1461
1462 r = 0;
1463 switch (chip->chip_id) {
1464 case KVM_IRQCHIP_PIC_MASTER:
1465 memcpy(&pic_irqchip(kvm)->pics[0],
1466 &chip->chip.pic,
1467 sizeof(struct kvm_pic_state));
1468 break;
1469 case KVM_IRQCHIP_PIC_SLAVE:
1470 memcpy(&pic_irqchip(kvm)->pics[1],
1471 &chip->chip.pic,
1472 sizeof(struct kvm_pic_state));
1473 break;
1474 case KVM_IRQCHIP_IOAPIC:
1475 memcpy(ioapic_irqchip(kvm),
1476 &chip->chip.ioapic,
1477 sizeof(struct kvm_ioapic_state));
1478 break;
1479 default:
1480 r = -EINVAL;
1481 break;
1482 }
1483 kvm_pic_update_irq(pic_irqchip(kvm));
1484 return r;
1485}
1486
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001487/*
1488 * Get (and clear) the dirty memory log for a memory slot.
1489 */
1490int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1491 struct kvm_dirty_log *log)
1492{
1493 int r;
1494 int n;
1495 struct kvm_memory_slot *memslot;
1496 int is_dirty = 0;
1497
Izik Eidus72dc67a2008-02-10 18:04:15 +02001498 down_write(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001499
1500 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1501 if (r)
1502 goto out;
1503
1504 /* If nothing is dirty, don't bother messing with page tables. */
1505 if (is_dirty) {
1506 kvm_mmu_slot_remove_write_access(kvm, log->slot);
1507 kvm_flush_remote_tlbs(kvm);
1508 memslot = &kvm->memslots[log->slot];
1509 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1510 memset(memslot->dirty_bitmap, 0, n);
1511 }
1512 r = 0;
1513out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02001514 up_write(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001515 return r;
1516}
1517
Carsten Otte1fe779f2007-10-29 16:08:35 +01001518long kvm_arch_vm_ioctl(struct file *filp,
1519 unsigned int ioctl, unsigned long arg)
1520{
1521 struct kvm *kvm = filp->private_data;
1522 void __user *argp = (void __user *)arg;
1523 int r = -EINVAL;
1524
1525 switch (ioctl) {
1526 case KVM_SET_TSS_ADDR:
1527 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
1528 if (r < 0)
1529 goto out;
1530 break;
1531 case KVM_SET_MEMORY_REGION: {
1532 struct kvm_memory_region kvm_mem;
1533 struct kvm_userspace_memory_region kvm_userspace_mem;
1534
1535 r = -EFAULT;
1536 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
1537 goto out;
1538 kvm_userspace_mem.slot = kvm_mem.slot;
1539 kvm_userspace_mem.flags = kvm_mem.flags;
1540 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
1541 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
1542 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
1543 if (r)
1544 goto out;
1545 break;
1546 }
1547 case KVM_SET_NR_MMU_PAGES:
1548 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
1549 if (r)
1550 goto out;
1551 break;
1552 case KVM_GET_NR_MMU_PAGES:
1553 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
1554 break;
1555 case KVM_SET_MEMORY_ALIAS: {
1556 struct kvm_memory_alias alias;
1557
1558 r = -EFAULT;
1559 if (copy_from_user(&alias, argp, sizeof alias))
1560 goto out;
1561 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
1562 if (r)
1563 goto out;
1564 break;
1565 }
1566 case KVM_CREATE_IRQCHIP:
1567 r = -ENOMEM;
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08001568 kvm->arch.vpic = kvm_create_pic(kvm);
1569 if (kvm->arch.vpic) {
Carsten Otte1fe779f2007-10-29 16:08:35 +01001570 r = kvm_ioapic_init(kvm);
1571 if (r) {
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08001572 kfree(kvm->arch.vpic);
1573 kvm->arch.vpic = NULL;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001574 goto out;
1575 }
1576 } else
1577 goto out;
1578 break;
1579 case KVM_IRQ_LINE: {
1580 struct kvm_irq_level irq_event;
1581
1582 r = -EFAULT;
1583 if (copy_from_user(&irq_event, argp, sizeof irq_event))
1584 goto out;
1585 if (irqchip_in_kernel(kvm)) {
1586 mutex_lock(&kvm->lock);
1587 if (irq_event.irq < 16)
1588 kvm_pic_set_irq(pic_irqchip(kvm),
1589 irq_event.irq,
1590 irq_event.level);
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08001591 kvm_ioapic_set_irq(kvm->arch.vioapic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01001592 irq_event.irq,
1593 irq_event.level);
1594 mutex_unlock(&kvm->lock);
1595 r = 0;
1596 }
1597 break;
1598 }
1599 case KVM_GET_IRQCHIP: {
1600 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1601 struct kvm_irqchip chip;
1602
1603 r = -EFAULT;
1604 if (copy_from_user(&chip, argp, sizeof chip))
1605 goto out;
1606 r = -ENXIO;
1607 if (!irqchip_in_kernel(kvm))
1608 goto out;
1609 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
1610 if (r)
1611 goto out;
1612 r = -EFAULT;
1613 if (copy_to_user(argp, &chip, sizeof chip))
1614 goto out;
1615 r = 0;
1616 break;
1617 }
1618 case KVM_SET_IRQCHIP: {
1619 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1620 struct kvm_irqchip chip;
1621
1622 r = -EFAULT;
1623 if (copy_from_user(&chip, argp, sizeof chip))
1624 goto out;
1625 r = -ENXIO;
1626 if (!irqchip_in_kernel(kvm))
1627 goto out;
1628 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
1629 if (r)
1630 goto out;
1631 r = 0;
1632 break;
1633 }
1634 default:
1635 ;
1636 }
1637out:
1638 return r;
1639}
1640
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08001641static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02001642{
1643 u32 dummy[2];
1644 unsigned i, j;
1645
1646 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
1647 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
1648 continue;
1649 if (j < i)
1650 msrs_to_save[j] = msrs_to_save[i];
1651 j++;
1652 }
1653 num_msrs_to_save = j;
1654}
1655
Carsten Ottebbd9b642007-10-30 18:44:21 +01001656/*
1657 * Only apic need an MMIO device hook, so shortcut now..
1658 */
1659static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
1660 gpa_t addr)
1661{
1662 struct kvm_io_device *dev;
1663
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001664 if (vcpu->arch.apic) {
1665 dev = &vcpu->arch.apic->dev;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001666 if (dev->in_range(dev, addr))
1667 return dev;
1668 }
1669 return NULL;
1670}
1671
1672
1673static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1674 gpa_t addr)
1675{
1676 struct kvm_io_device *dev;
1677
1678 dev = vcpu_find_pervcpu_dev(vcpu, addr);
1679 if (dev == NULL)
1680 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1681 return dev;
1682}
1683
1684int emulator_read_std(unsigned long addr,
1685 void *val,
1686 unsigned int bytes,
1687 struct kvm_vcpu *vcpu)
1688{
1689 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001690 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001691
Izik Eidus72dc67a2008-02-10 18:04:15 +02001692 down_read(&vcpu->kvm->slots_lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001693 while (bytes) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001694 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001695 unsigned offset = addr & (PAGE_SIZE-1);
1696 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
1697 int ret;
1698
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001699 if (gpa == UNMAPPED_GVA) {
1700 r = X86EMUL_PROPAGATE_FAULT;
1701 goto out;
1702 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01001703 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001704 if (ret < 0) {
1705 r = X86EMUL_UNHANDLEABLE;
1706 goto out;
1707 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01001708
1709 bytes -= tocopy;
1710 data += tocopy;
1711 addr += tocopy;
1712 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001713out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02001714 up_read(&vcpu->kvm->slots_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001715 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001716}
1717EXPORT_SYMBOL_GPL(emulator_read_std);
1718
Carsten Ottebbd9b642007-10-30 18:44:21 +01001719static int emulator_read_emulated(unsigned long addr,
1720 void *val,
1721 unsigned int bytes,
1722 struct kvm_vcpu *vcpu)
1723{
1724 struct kvm_io_device *mmio_dev;
1725 gpa_t gpa;
1726
1727 if (vcpu->mmio_read_completed) {
1728 memcpy(val, vcpu->mmio_data, bytes);
1729 vcpu->mmio_read_completed = 0;
1730 return X86EMUL_CONTINUE;
1731 }
1732
Izik Eidus72dc67a2008-02-10 18:04:15 +02001733 down_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001734 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001735 up_read(&vcpu->kvm->slots_lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001736
1737 /* For APIC access vmexit */
1738 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1739 goto mmio;
1740
1741 if (emulator_read_std(addr, val, bytes, vcpu)
1742 == X86EMUL_CONTINUE)
1743 return X86EMUL_CONTINUE;
1744 if (gpa == UNMAPPED_GVA)
1745 return X86EMUL_PROPAGATE_FAULT;
1746
1747mmio:
1748 /*
1749 * Is this MMIO handled locally?
1750 */
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001751 mutex_lock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001752 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1753 if (mmio_dev) {
1754 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001755 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001756 return X86EMUL_CONTINUE;
1757 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001758 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001759
1760 vcpu->mmio_needed = 1;
1761 vcpu->mmio_phys_addr = gpa;
1762 vcpu->mmio_size = bytes;
1763 vcpu->mmio_is_write = 0;
1764
1765 return X86EMUL_UNHANDLEABLE;
1766}
1767
1768static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
1769 const void *val, int bytes)
1770{
1771 int ret;
1772
Izik Eidus72dc67a2008-02-10 18:04:15 +02001773 down_read(&vcpu->kvm->slots_lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001774 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001775 if (ret < 0) {
Izik Eidus72dc67a2008-02-10 18:04:15 +02001776 up_read(&vcpu->kvm->slots_lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001777 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001778 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01001779 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001780 up_read(&vcpu->kvm->slots_lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001781 return 1;
1782}
1783
1784static int emulator_write_emulated_onepage(unsigned long addr,
1785 const void *val,
1786 unsigned int bytes,
1787 struct kvm_vcpu *vcpu)
1788{
1789 struct kvm_io_device *mmio_dev;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001790 gpa_t gpa;
1791
Izik Eidus72dc67a2008-02-10 18:04:15 +02001792 down_read(&vcpu->kvm->slots_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001793 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001794 up_read(&vcpu->kvm->slots_lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001795
1796 if (gpa == UNMAPPED_GVA) {
Avi Kivityc3c91fe2007-11-25 14:04:58 +02001797 kvm_inject_page_fault(vcpu, addr, 2);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001798 return X86EMUL_PROPAGATE_FAULT;
1799 }
1800
1801 /* For APIC access vmexit */
1802 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1803 goto mmio;
1804
1805 if (emulator_write_phys(vcpu, gpa, val, bytes))
1806 return X86EMUL_CONTINUE;
1807
1808mmio:
1809 /*
1810 * Is this MMIO handled locally?
1811 */
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001812 mutex_lock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001813 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1814 if (mmio_dev) {
1815 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001816 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001817 return X86EMUL_CONTINUE;
1818 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001819 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001820
1821 vcpu->mmio_needed = 1;
1822 vcpu->mmio_phys_addr = gpa;
1823 vcpu->mmio_size = bytes;
1824 vcpu->mmio_is_write = 1;
1825 memcpy(vcpu->mmio_data, val, bytes);
1826
1827 return X86EMUL_CONTINUE;
1828}
1829
1830int emulator_write_emulated(unsigned long addr,
1831 const void *val,
1832 unsigned int bytes,
1833 struct kvm_vcpu *vcpu)
1834{
1835 /* Crossing a page boundary? */
1836 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1837 int rc, now;
1838
1839 now = -addr & ~PAGE_MASK;
1840 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
1841 if (rc != X86EMUL_CONTINUE)
1842 return rc;
1843 addr += now;
1844 val += now;
1845 bytes -= now;
1846 }
1847 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
1848}
1849EXPORT_SYMBOL_GPL(emulator_write_emulated);
1850
1851static int emulator_cmpxchg_emulated(unsigned long addr,
1852 const void *old,
1853 const void *new,
1854 unsigned int bytes,
1855 struct kvm_vcpu *vcpu)
1856{
1857 static int reported;
1858
1859 if (!reported) {
1860 reported = 1;
1861 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1862 }
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001863#ifndef CONFIG_X86_64
1864 /* guests cmpxchg8b have to be emulated atomically */
1865 if (bytes == 8) {
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001866 gpa_t gpa;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001867 struct page *page;
Andrew Mortonc0b49b02008-02-04 22:27:18 -08001868 char *kaddr;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001869 u64 val;
1870
Izik Eidus72dc67a2008-02-10 18:04:15 +02001871 down_read(&vcpu->kvm->slots_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001872 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
1873
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001874 if (gpa == UNMAPPED_GVA ||
1875 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1876 goto emul_write;
1877
1878 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
1879 goto emul_write;
1880
1881 val = *(u64 *)new;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001882
1883 down_read(&current->mm->mmap_sem);
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001884 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001885 up_read(&current->mm->mmap_sem);
1886
Andrew Mortonc0b49b02008-02-04 22:27:18 -08001887 kaddr = kmap_atomic(page, KM_USER0);
1888 set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
1889 kunmap_atomic(kaddr, KM_USER0);
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001890 kvm_release_page_dirty(page);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001891 emul_write:
Izik Eidus72dc67a2008-02-10 18:04:15 +02001892 up_read(&vcpu->kvm->slots_lock);
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001893 }
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001894#endif
1895
Carsten Ottebbd9b642007-10-30 18:44:21 +01001896 return emulator_write_emulated(addr, new, bytes, vcpu);
1897}
1898
1899static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1900{
1901 return kvm_x86_ops->get_segment_base(vcpu, seg);
1902}
1903
1904int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1905{
1906 return X86EMUL_CONTINUE;
1907}
1908
1909int emulate_clts(struct kvm_vcpu *vcpu)
1910{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001911 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001912 return X86EMUL_CONTINUE;
1913}
1914
1915int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
1916{
1917 struct kvm_vcpu *vcpu = ctxt->vcpu;
1918
1919 switch (dr) {
1920 case 0 ... 3:
1921 *dest = kvm_x86_ops->get_dr(vcpu, dr);
1922 return X86EMUL_CONTINUE;
1923 default:
1924 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
1925 return X86EMUL_UNHANDLEABLE;
1926 }
1927}
1928
1929int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1930{
1931 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1932 int exception;
1933
1934 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1935 if (exception) {
1936 /* FIXME: better handling */
1937 return X86EMUL_UNHANDLEABLE;
1938 }
1939 return X86EMUL_CONTINUE;
1940}
1941
1942void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
1943{
1944 static int reported;
1945 u8 opcodes[4];
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001946 unsigned long rip = vcpu->arch.rip;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001947 unsigned long rip_linear;
1948
1949 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
1950
1951 if (reported)
1952 return;
1953
1954 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
1955
1956 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1957 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1958 reported = 1;
1959}
1960EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
1961
1962struct x86_emulate_ops emulate_ops = {
1963 .read_std = emulator_read_std,
Carsten Ottebbd9b642007-10-30 18:44:21 +01001964 .read_emulated = emulator_read_emulated,
1965 .write_emulated = emulator_write_emulated,
1966 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1967};
1968
1969int emulate_instruction(struct kvm_vcpu *vcpu,
1970 struct kvm_run *run,
1971 unsigned long cr2,
1972 u16 error_code,
Sheng Yang571008d2008-01-02 14:49:22 +08001973 int emulation_type)
Carsten Ottebbd9b642007-10-30 18:44:21 +01001974{
1975 int r;
Sheng Yang571008d2008-01-02 14:49:22 +08001976 struct decode_cache *c;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001977
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001978 vcpu->arch.mmio_fault_cr2 = cr2;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001979 kvm_x86_ops->cache_regs(vcpu);
1980
1981 vcpu->mmio_is_write = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001982 vcpu->arch.pio.string = 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001983
Sheng Yang571008d2008-01-02 14:49:22 +08001984 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Carsten Ottebbd9b642007-10-30 18:44:21 +01001985 int cs_db, cs_l;
1986 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1987
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001988 vcpu->arch.emulate_ctxt.vcpu = vcpu;
1989 vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
1990 vcpu->arch.emulate_ctxt.mode =
1991 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
Carsten Ottebbd9b642007-10-30 18:44:21 +01001992 ? X86EMUL_MODE_REAL : cs_l
1993 ? X86EMUL_MODE_PROT64 : cs_db
1994 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1995
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001996 if (vcpu->arch.emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1997 vcpu->arch.emulate_ctxt.cs_base = 0;
1998 vcpu->arch.emulate_ctxt.ds_base = 0;
1999 vcpu->arch.emulate_ctxt.es_base = 0;
2000 vcpu->arch.emulate_ctxt.ss_base = 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002001 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002002 vcpu->arch.emulate_ctxt.cs_base =
Carsten Ottebbd9b642007-10-30 18:44:21 +01002003 get_segment_base(vcpu, VCPU_SREG_CS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002004 vcpu->arch.emulate_ctxt.ds_base =
Carsten Ottebbd9b642007-10-30 18:44:21 +01002005 get_segment_base(vcpu, VCPU_SREG_DS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002006 vcpu->arch.emulate_ctxt.es_base =
Carsten Ottebbd9b642007-10-30 18:44:21 +01002007 get_segment_base(vcpu, VCPU_SREG_ES);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002008 vcpu->arch.emulate_ctxt.ss_base =
Carsten Ottebbd9b642007-10-30 18:44:21 +01002009 get_segment_base(vcpu, VCPU_SREG_SS);
2010 }
2011
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002012 vcpu->arch.emulate_ctxt.gs_base =
Carsten Ottebbd9b642007-10-30 18:44:21 +01002013 get_segment_base(vcpu, VCPU_SREG_GS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002014 vcpu->arch.emulate_ctxt.fs_base =
Carsten Ottebbd9b642007-10-30 18:44:21 +01002015 get_segment_base(vcpu, VCPU_SREG_FS);
2016
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002017 r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Sheng Yang571008d2008-01-02 14:49:22 +08002018
2019 /* Reject the instructions other than VMCALL/VMMCALL when
2020 * try to emulate invalid opcode */
2021 c = &vcpu->arch.emulate_ctxt.decode;
2022 if ((emulation_type & EMULTYPE_TRAP_UD) &&
2023 (!(c->twobyte && c->b == 0x01 &&
2024 (c->modrm_reg == 0 || c->modrm_reg == 3) &&
2025 c->modrm_mod == 3 && c->modrm_rm == 1)))
2026 return EMULATE_FAIL;
2027
Avi Kivityf2b57562007-11-18 15:17:51 +02002028 ++vcpu->stat.insn_emulation;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002029 if (r) {
Avi Kivityf2b57562007-11-18 15:17:51 +02002030 ++vcpu->stat.insn_emulation_fail;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002031 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
2032 return EMULATE_DONE;
2033 return EMULATE_FAIL;
2034 }
2035 }
2036
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002037 r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002038
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002039 if (vcpu->arch.pio.string)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002040 return EMULATE_DO_MMIO;
2041
2042 if ((r || vcpu->mmio_is_write) && run) {
2043 run->exit_reason = KVM_EXIT_MMIO;
2044 run->mmio.phys_addr = vcpu->mmio_phys_addr;
2045 memcpy(run->mmio.data, vcpu->mmio_data, 8);
2046 run->mmio.len = vcpu->mmio_size;
2047 run->mmio.is_write = vcpu->mmio_is_write;
2048 }
2049
2050 if (r) {
2051 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
2052 return EMULATE_DONE;
2053 if (!vcpu->mmio_needed) {
2054 kvm_report_emulation_failure(vcpu, "mmio");
2055 return EMULATE_FAIL;
2056 }
2057 return EMULATE_DO_MMIO;
2058 }
2059
2060 kvm_x86_ops->decache_regs(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002061 kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002062
2063 if (vcpu->mmio_is_write) {
2064 vcpu->mmio_needed = 0;
2065 return EMULATE_DO_MMIO;
2066 }
2067
2068 return EMULATE_DONE;
2069}
2070EXPORT_SYMBOL_GPL(emulate_instruction);
2071
Carsten Ottede7d7892007-10-30 18:44:25 +01002072static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
2073{
2074 int i;
2075
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002076 for (i = 0; i < ARRAY_SIZE(vcpu->arch.pio.guest_pages); ++i)
2077 if (vcpu->arch.pio.guest_pages[i]) {
2078 kvm_release_page_dirty(vcpu->arch.pio.guest_pages[i]);
2079 vcpu->arch.pio.guest_pages[i] = NULL;
Carsten Ottede7d7892007-10-30 18:44:25 +01002080 }
2081}
2082
2083static int pio_copy_data(struct kvm_vcpu *vcpu)
2084{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002085 void *p = vcpu->arch.pio_data;
Carsten Ottede7d7892007-10-30 18:44:25 +01002086 void *q;
2087 unsigned bytes;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002088 int nr_pages = vcpu->arch.pio.guest_pages[1] ? 2 : 1;
Carsten Ottede7d7892007-10-30 18:44:25 +01002089
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002090 q = vmap(vcpu->arch.pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
Carsten Ottede7d7892007-10-30 18:44:25 +01002091 PAGE_KERNEL);
2092 if (!q) {
2093 free_pio_guest_pages(vcpu);
2094 return -ENOMEM;
2095 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002096 q += vcpu->arch.pio.guest_page_offset;
2097 bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
2098 if (vcpu->arch.pio.in)
Carsten Ottede7d7892007-10-30 18:44:25 +01002099 memcpy(q, p, bytes);
2100 else
2101 memcpy(p, q, bytes);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002102 q -= vcpu->arch.pio.guest_page_offset;
Carsten Ottede7d7892007-10-30 18:44:25 +01002103 vunmap(q);
2104 free_pio_guest_pages(vcpu);
2105 return 0;
2106}
2107
2108int complete_pio(struct kvm_vcpu *vcpu)
2109{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002110 struct kvm_pio_request *io = &vcpu->arch.pio;
Carsten Ottede7d7892007-10-30 18:44:25 +01002111 long delta;
2112 int r;
2113
2114 kvm_x86_ops->cache_regs(vcpu);
2115
2116 if (!io->string) {
2117 if (io->in)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002118 memcpy(&vcpu->arch.regs[VCPU_REGS_RAX], vcpu->arch.pio_data,
Carsten Ottede7d7892007-10-30 18:44:25 +01002119 io->size);
2120 } else {
2121 if (io->in) {
2122 r = pio_copy_data(vcpu);
2123 if (r) {
2124 kvm_x86_ops->cache_regs(vcpu);
2125 return r;
2126 }
2127 }
2128
2129 delta = 1;
2130 if (io->rep) {
2131 delta *= io->cur_count;
2132 /*
2133 * The size of the register should really depend on
2134 * current address size.
2135 */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002136 vcpu->arch.regs[VCPU_REGS_RCX] -= delta;
Carsten Ottede7d7892007-10-30 18:44:25 +01002137 }
2138 if (io->down)
2139 delta = -delta;
2140 delta *= io->size;
2141 if (io->in)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002142 vcpu->arch.regs[VCPU_REGS_RDI] += delta;
Carsten Ottede7d7892007-10-30 18:44:25 +01002143 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002144 vcpu->arch.regs[VCPU_REGS_RSI] += delta;
Carsten Ottede7d7892007-10-30 18:44:25 +01002145 }
2146
2147 kvm_x86_ops->decache_regs(vcpu);
2148
2149 io->count -= io->cur_count;
2150 io->cur_count = 0;
2151
2152 return 0;
2153}
2154
2155static void kernel_pio(struct kvm_io_device *pio_dev,
2156 struct kvm_vcpu *vcpu,
2157 void *pd)
2158{
2159 /* TODO: String I/O for in kernel device */
2160
2161 mutex_lock(&vcpu->kvm->lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002162 if (vcpu->arch.pio.in)
2163 kvm_iodevice_read(pio_dev, vcpu->arch.pio.port,
2164 vcpu->arch.pio.size,
Carsten Ottede7d7892007-10-30 18:44:25 +01002165 pd);
2166 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002167 kvm_iodevice_write(pio_dev, vcpu->arch.pio.port,
2168 vcpu->arch.pio.size,
Carsten Ottede7d7892007-10-30 18:44:25 +01002169 pd);
2170 mutex_unlock(&vcpu->kvm->lock);
2171}
2172
2173static void pio_string_write(struct kvm_io_device *pio_dev,
2174 struct kvm_vcpu *vcpu)
2175{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002176 struct kvm_pio_request *io = &vcpu->arch.pio;
2177 void *pd = vcpu->arch.pio_data;
Carsten Ottede7d7892007-10-30 18:44:25 +01002178 int i;
2179
2180 mutex_lock(&vcpu->kvm->lock);
2181 for (i = 0; i < io->cur_count; i++) {
2182 kvm_iodevice_write(pio_dev, io->port,
2183 io->size,
2184 pd);
2185 pd += io->size;
2186 }
2187 mutex_unlock(&vcpu->kvm->lock);
2188}
2189
2190static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
2191 gpa_t addr)
2192{
2193 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
2194}
2195
2196int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2197 int size, unsigned port)
2198{
2199 struct kvm_io_device *pio_dev;
2200
2201 vcpu->run->exit_reason = KVM_EXIT_IO;
2202 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002203 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01002204 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002205 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
2206 vcpu->run->io.port = vcpu->arch.pio.port = port;
2207 vcpu->arch.pio.in = in;
2208 vcpu->arch.pio.string = 0;
2209 vcpu->arch.pio.down = 0;
2210 vcpu->arch.pio.guest_page_offset = 0;
2211 vcpu->arch.pio.rep = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01002212
2213 kvm_x86_ops->cache_regs(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002214 memcpy(vcpu->arch.pio_data, &vcpu->arch.regs[VCPU_REGS_RAX], 4);
Carsten Ottede7d7892007-10-30 18:44:25 +01002215 kvm_x86_ops->decache_regs(vcpu);
2216
2217 kvm_x86_ops->skip_emulated_instruction(vcpu);
2218
2219 pio_dev = vcpu_find_pio_dev(vcpu, port);
2220 if (pio_dev) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002221 kernel_pio(pio_dev, vcpu, vcpu->arch.pio_data);
Carsten Ottede7d7892007-10-30 18:44:25 +01002222 complete_pio(vcpu);
2223 return 1;
2224 }
2225 return 0;
2226}
2227EXPORT_SYMBOL_GPL(kvm_emulate_pio);
2228
2229int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2230 int size, unsigned long count, int down,
2231 gva_t address, int rep, unsigned port)
2232{
2233 unsigned now, in_page;
2234 int i, ret = 0;
2235 int nr_pages = 1;
2236 struct page *page;
2237 struct kvm_io_device *pio_dev;
2238
2239 vcpu->run->exit_reason = KVM_EXIT_IO;
2240 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002241 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01002242 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002243 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
2244 vcpu->run->io.port = vcpu->arch.pio.port = port;
2245 vcpu->arch.pio.in = in;
2246 vcpu->arch.pio.string = 1;
2247 vcpu->arch.pio.down = down;
2248 vcpu->arch.pio.guest_page_offset = offset_in_page(address);
2249 vcpu->arch.pio.rep = rep;
Carsten Ottede7d7892007-10-30 18:44:25 +01002250
2251 if (!count) {
2252 kvm_x86_ops->skip_emulated_instruction(vcpu);
2253 return 1;
2254 }
2255
2256 if (!down)
2257 in_page = PAGE_SIZE - offset_in_page(address);
2258 else
2259 in_page = offset_in_page(address) + size;
2260 now = min(count, (unsigned long)in_page / size);
2261 if (!now) {
2262 /*
2263 * String I/O straddles page boundary. Pin two guest pages
2264 * so that we satisfy atomicity constraints. Do just one
2265 * transaction to avoid complexity.
2266 */
2267 nr_pages = 2;
2268 now = 1;
2269 }
2270 if (down) {
2271 /*
2272 * String I/O in reverse. Yuck. Kill the guest, fix later.
2273 */
2274 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002275 kvm_inject_gp(vcpu, 0);
Carsten Ottede7d7892007-10-30 18:44:25 +01002276 return 1;
2277 }
2278 vcpu->run->io.count = now;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002279 vcpu->arch.pio.cur_count = now;
Carsten Ottede7d7892007-10-30 18:44:25 +01002280
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002281 if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
Carsten Ottede7d7892007-10-30 18:44:25 +01002282 kvm_x86_ops->skip_emulated_instruction(vcpu);
2283
2284 for (i = 0; i < nr_pages; ++i) {
Izik Eidus72dc67a2008-02-10 18:04:15 +02002285 down_read(&vcpu->kvm->slots_lock);
Carsten Ottede7d7892007-10-30 18:44:25 +01002286 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002287 vcpu->arch.pio.guest_pages[i] = page;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002288 up_read(&vcpu->kvm->slots_lock);
Carsten Ottede7d7892007-10-30 18:44:25 +01002289 if (!page) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002290 kvm_inject_gp(vcpu, 0);
Carsten Ottede7d7892007-10-30 18:44:25 +01002291 free_pio_guest_pages(vcpu);
2292 return 1;
2293 }
2294 }
2295
2296 pio_dev = vcpu_find_pio_dev(vcpu, port);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002297 if (!vcpu->arch.pio.in) {
Carsten Ottede7d7892007-10-30 18:44:25 +01002298 /* string PIO write */
2299 ret = pio_copy_data(vcpu);
2300 if (ret >= 0 && pio_dev) {
2301 pio_string_write(pio_dev, vcpu);
2302 complete_pio(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002303 if (vcpu->arch.pio.count == 0)
Carsten Ottede7d7892007-10-30 18:44:25 +01002304 ret = 1;
2305 }
2306 } else if (pio_dev)
2307 pr_unimpl(vcpu, "no string pio read support yet, "
2308 "port %x size %d count %ld\n",
2309 port, size, count);
2310
2311 return ret;
2312}
2313EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
2314
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002315int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02002316{
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002317 int r;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002318 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
2319
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002320 if (kvm_x86_ops) {
2321 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002322 r = -EEXIST;
2323 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002324 }
2325
2326 if (!ops->cpu_has_kvm_support()) {
2327 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002328 r = -EOPNOTSUPP;
2329 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002330 }
2331 if (ops->disabled_by_bios()) {
2332 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002333 r = -EOPNOTSUPP;
2334 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002335 }
2336
Avi Kivity97db56c2008-01-13 13:23:56 +02002337 r = kvm_mmu_module_init();
2338 if (r)
2339 goto out;
2340
2341 kvm_init_msr_list();
2342
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002343 kvm_x86_ops = ops;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002344 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002345 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002346
2347out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002348 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02002349}
Hollis Blanchard8776e512007-10-31 17:24:24 -05002350
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002351void kvm_arch_exit(void)
2352{
2353 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002354 kvm_mmu_module_exit();
2355}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002356
Hollis Blanchard8776e512007-10-31 17:24:24 -05002357int kvm_emulate_halt(struct kvm_vcpu *vcpu)
2358{
2359 ++vcpu->stat.halt_exits;
2360 if (irqchip_in_kernel(vcpu->kvm)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002361 vcpu->arch.mp_state = VCPU_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002362 kvm_vcpu_block(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002363 if (vcpu->arch.mp_state != VCPU_MP_STATE_RUNNABLE)
Hollis Blanchard8776e512007-10-31 17:24:24 -05002364 return -EINTR;
2365 return 1;
2366 } else {
2367 vcpu->run->exit_reason = KVM_EXIT_HLT;
2368 return 0;
2369 }
2370}
2371EXPORT_SYMBOL_GPL(kvm_emulate_halt);
2372
2373int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
2374{
2375 unsigned long nr, a0, a1, a2, a3, ret;
2376
2377 kvm_x86_ops->cache_regs(vcpu);
2378
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002379 nr = vcpu->arch.regs[VCPU_REGS_RAX];
2380 a0 = vcpu->arch.regs[VCPU_REGS_RBX];
2381 a1 = vcpu->arch.regs[VCPU_REGS_RCX];
2382 a2 = vcpu->arch.regs[VCPU_REGS_RDX];
2383 a3 = vcpu->arch.regs[VCPU_REGS_RSI];
Hollis Blanchard8776e512007-10-31 17:24:24 -05002384
2385 if (!is_long_mode(vcpu)) {
2386 nr &= 0xFFFFFFFF;
2387 a0 &= 0xFFFFFFFF;
2388 a1 &= 0xFFFFFFFF;
2389 a2 &= 0xFFFFFFFF;
2390 a3 &= 0xFFFFFFFF;
2391 }
2392
2393 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02002394 case KVM_HC_VAPIC_POLL_IRQ:
2395 ret = 0;
2396 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002397 default:
2398 ret = -KVM_ENOSYS;
2399 break;
2400 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002401 vcpu->arch.regs[VCPU_REGS_RAX] = ret;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002402 kvm_x86_ops->decache_regs(vcpu);
2403 return 0;
2404}
2405EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
2406
2407int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
2408{
2409 char instruction[3];
2410 int ret = 0;
2411
Hollis Blanchard8776e512007-10-31 17:24:24 -05002412
2413 /*
2414 * Blow out the MMU to ensure that no other VCPU has an active mapping
2415 * to ensure that the updated hypercall appears atomically across all
2416 * VCPUs.
2417 */
2418 kvm_mmu_zap_all(vcpu->kvm);
2419
2420 kvm_x86_ops->cache_regs(vcpu);
2421 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002422 if (emulator_write_emulated(vcpu->arch.rip, instruction, 3, vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05002423 != X86EMUL_CONTINUE)
2424 ret = -EFAULT;
2425
Hollis Blanchard8776e512007-10-31 17:24:24 -05002426 return ret;
2427}
2428
2429static u64 mk_cr_64(u64 curr_cr, u32 new_val)
2430{
2431 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
2432}
2433
2434void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2435{
2436 struct descriptor_table dt = { limit, base };
2437
2438 kvm_x86_ops->set_gdt(vcpu, &dt);
2439}
2440
2441void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2442{
2443 struct descriptor_table dt = { limit, base };
2444
2445 kvm_x86_ops->set_idt(vcpu, &dt);
2446}
2447
2448void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
2449 unsigned long *rflags)
2450{
2451 lmsw(vcpu, msw);
2452 *rflags = kvm_x86_ops->get_rflags(vcpu);
2453}
2454
2455unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
2456{
2457 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2458 switch (cr) {
2459 case 0:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002460 return vcpu->arch.cr0;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002461 case 2:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002462 return vcpu->arch.cr2;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002463 case 3:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002464 return vcpu->arch.cr3;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002465 case 4:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002466 return vcpu->arch.cr4;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01002467 case 8:
2468 return get_cr8(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002469 default:
2470 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
2471 return 0;
2472 }
2473}
2474
2475void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
2476 unsigned long *rflags)
2477{
2478 switch (cr) {
2479 case 0:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002480 set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val));
Hollis Blanchard8776e512007-10-31 17:24:24 -05002481 *rflags = kvm_x86_ops->get_rflags(vcpu);
2482 break;
2483 case 2:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002484 vcpu->arch.cr2 = val;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002485 break;
2486 case 3:
2487 set_cr3(vcpu, val);
2488 break;
2489 case 4:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002490 set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val));
Hollis Blanchard8776e512007-10-31 17:24:24 -05002491 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01002492 case 8:
2493 set_cr8(vcpu, val & 0xfUL);
2494 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002495 default:
2496 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
2497 }
2498}
2499
Dan Kenigsberg07716712007-11-21 17:10:04 +02002500static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
2501{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002502 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
2503 int j, nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02002504
2505 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
2506 /* when no next entry is found, the current entry[i] is reselected */
2507 for (j = i + 1; j == i; j = (j + 1) % nent) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002508 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
Dan Kenigsberg07716712007-11-21 17:10:04 +02002509 if (ej->function == e->function) {
2510 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
2511 return j;
2512 }
2513 }
2514 return 0; /* silence gcc, even though control never reaches here */
2515}
2516
2517/* find an entry with matching function, matching index (if needed), and that
2518 * should be read next (if it's stateful) */
2519static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
2520 u32 function, u32 index)
2521{
2522 if (e->function != function)
2523 return 0;
2524 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
2525 return 0;
2526 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
2527 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
2528 return 0;
2529 return 1;
2530}
2531
Hollis Blanchard8776e512007-10-31 17:24:24 -05002532void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
2533{
2534 int i;
Dan Kenigsberg07716712007-11-21 17:10:04 +02002535 u32 function, index;
2536 struct kvm_cpuid_entry2 *e, *best;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002537
2538 kvm_x86_ops->cache_regs(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002539 function = vcpu->arch.regs[VCPU_REGS_RAX];
2540 index = vcpu->arch.regs[VCPU_REGS_RCX];
2541 vcpu->arch.regs[VCPU_REGS_RAX] = 0;
2542 vcpu->arch.regs[VCPU_REGS_RBX] = 0;
2543 vcpu->arch.regs[VCPU_REGS_RCX] = 0;
2544 vcpu->arch.regs[VCPU_REGS_RDX] = 0;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002545 best = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002546 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
2547 e = &vcpu->arch.cpuid_entries[i];
Dan Kenigsberg07716712007-11-21 17:10:04 +02002548 if (is_matching_cpuid_entry(e, function, index)) {
2549 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
2550 move_to_next_stateful_cpuid_entry(vcpu, i);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002551 best = e;
2552 break;
2553 }
2554 /*
2555 * Both basic or both extended?
2556 */
2557 if (((e->function ^ function) & 0x80000000) == 0)
2558 if (!best || e->function > best->function)
2559 best = e;
2560 }
2561 if (best) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002562 vcpu->arch.regs[VCPU_REGS_RAX] = best->eax;
2563 vcpu->arch.regs[VCPU_REGS_RBX] = best->ebx;
2564 vcpu->arch.regs[VCPU_REGS_RCX] = best->ecx;
2565 vcpu->arch.regs[VCPU_REGS_RDX] = best->edx;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002566 }
2567 kvm_x86_ops->decache_regs(vcpu);
2568 kvm_x86_ops->skip_emulated_instruction(vcpu);
2569}
2570EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
Hollis Blanchardd0752062007-10-31 17:24:25 -05002571
2572/*
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002573 * Check if userspace requested an interrupt window, and that the
2574 * interrupt window is open.
2575 *
2576 * No need to exit to userspace if we already have an interrupt queued.
2577 */
2578static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2579 struct kvm_run *kvm_run)
2580{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002581 return (!vcpu->arch.irq_summary &&
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002582 kvm_run->request_interrupt_window &&
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002583 vcpu->arch.interrupt_window_open &&
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002584 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
2585}
2586
2587static void post_kvm_run_save(struct kvm_vcpu *vcpu,
2588 struct kvm_run *kvm_run)
2589{
2590 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
2591 kvm_run->cr8 = get_cr8(vcpu);
2592 kvm_run->apic_base = kvm_get_apic_base(vcpu);
2593 if (irqchip_in_kernel(vcpu->kvm))
2594 kvm_run->ready_for_interrupt_injection = 1;
2595 else
2596 kvm_run->ready_for_interrupt_injection =
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002597 (vcpu->arch.interrupt_window_open &&
2598 vcpu->arch.irq_summary == 0);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002599}
2600
Avi Kivityb93463a2007-10-25 16:52:32 +02002601static void vapic_enter(struct kvm_vcpu *vcpu)
2602{
2603 struct kvm_lapic *apic = vcpu->arch.apic;
2604 struct page *page;
2605
2606 if (!apic || !apic->vapic_addr)
2607 return;
2608
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002609 down_read(&current->mm->mmap_sem);
Avi Kivityb93463a2007-10-25 16:52:32 +02002610 page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002611 up_read(&current->mm->mmap_sem);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002612
2613 vcpu->arch.apic->vapic_page = page;
Avi Kivityb93463a2007-10-25 16:52:32 +02002614}
2615
2616static void vapic_exit(struct kvm_vcpu *vcpu)
2617{
2618 struct kvm_lapic *apic = vcpu->arch.apic;
2619
2620 if (!apic || !apic->vapic_addr)
2621 return;
2622
2623 kvm_release_page_dirty(apic->vapic_page);
2624 mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
2625}
2626
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002627static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2628{
2629 int r;
2630
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002631 if (unlikely(vcpu->arch.mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002632 pr_debug("vcpu %d received sipi with vector # %x\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002633 vcpu->vcpu_id, vcpu->arch.sipi_vector);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002634 kvm_lapic_reset(vcpu);
2635 r = kvm_x86_ops->vcpu_reset(vcpu);
2636 if (r)
2637 return r;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002638 vcpu->arch.mp_state = VCPU_MP_STATE_RUNNABLE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002639 }
2640
Avi Kivityb93463a2007-10-25 16:52:32 +02002641 vapic_enter(vcpu);
2642
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002643preempted:
2644 if (vcpu->guest_debug.enabled)
2645 kvm_x86_ops->guest_debug_pre(vcpu);
2646
2647again:
2648 r = kvm_mmu_reload(vcpu);
2649 if (unlikely(r))
2650 goto out;
2651
Avi Kivity2f52d582008-01-16 12:49:30 +02002652 if (vcpu->requests) {
2653 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
2654 __kvm_migrate_apic_timer(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02002655 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
2656 &vcpu->requests)) {
2657 kvm_run->exit_reason = KVM_EXIT_TPR_ACCESS;
2658 r = 0;
2659 goto out;
2660 }
Avi Kivity2f52d582008-01-16 12:49:30 +02002661 }
Avi Kivityb93463a2007-10-25 16:52:32 +02002662
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002663 kvm_inject_pending_timer_irqs(vcpu);
2664
2665 preempt_disable();
2666
2667 kvm_x86_ops->prepare_guest_switch(vcpu);
2668 kvm_load_guest_fpu(vcpu);
2669
2670 local_irq_disable();
2671
Avi Kivity6c142802008-01-15 18:27:32 +02002672 if (need_resched()) {
2673 local_irq_enable();
2674 preempt_enable();
2675 r = 1;
2676 goto out;
2677 }
2678
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002679 if (signal_pending(current)) {
2680 local_irq_enable();
2681 preempt_enable();
2682 r = -EINTR;
2683 kvm_run->exit_reason = KVM_EXIT_INTR;
2684 ++vcpu->stat.signal_exits;
2685 goto out;
2686 }
2687
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002688 if (vcpu->arch.exception.pending)
Avi Kivity298101d2007-11-25 13:41:11 +02002689 __queue_exception(vcpu);
2690 else if (irqchip_in_kernel(vcpu->kvm))
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002691 kvm_x86_ops->inject_pending_irq(vcpu);
Avi Kivityeb9774f2007-11-25 17:45:31 +02002692 else
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002693 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2694
Avi Kivityb93463a2007-10-25 16:52:32 +02002695 kvm_lapic_sync_to_vapic(vcpu);
2696
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002697 vcpu->guest_mode = 1;
2698 kvm_guest_enter();
2699
2700 if (vcpu->requests)
2701 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
2702 kvm_x86_ops->tlb_flush(vcpu);
2703
2704 kvm_x86_ops->run(vcpu, kvm_run);
2705
2706 vcpu->guest_mode = 0;
2707 local_irq_enable();
2708
2709 ++vcpu->stat.exits;
2710
2711 /*
2712 * We must have an instruction between local_irq_enable() and
2713 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2714 * the interrupt shadow. The stat.exits increment will do nicely.
2715 * But we need to prevent reordering, hence this barrier():
2716 */
2717 barrier();
2718
2719 kvm_guest_exit();
2720
2721 preempt_enable();
2722
2723 /*
2724 * Profile KVM exit RIPs:
2725 */
2726 if (unlikely(prof_on == KVM_PROFILING)) {
2727 kvm_x86_ops->cache_regs(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002728 profile_hit(KVM_PROFILING, (void *)vcpu->arch.rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002729 }
2730
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002731 if (vcpu->arch.exception.pending && kvm_x86_ops->exception_injected(vcpu))
2732 vcpu->arch.exception.pending = false;
Avi Kivity298101d2007-11-25 13:41:11 +02002733
Avi Kivityb93463a2007-10-25 16:52:32 +02002734 kvm_lapic_sync_from_vapic(vcpu);
2735
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002736 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2737
2738 if (r > 0) {
2739 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2740 r = -EINTR;
2741 kvm_run->exit_reason = KVM_EXIT_INTR;
2742 ++vcpu->stat.request_irq_exits;
2743 goto out;
2744 }
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002745 if (!need_resched())
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002746 goto again;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002747 }
2748
2749out:
2750 if (r > 0) {
2751 kvm_resched(vcpu);
2752 goto preempted;
2753 }
2754
2755 post_kvm_run_save(vcpu, kvm_run);
2756
Avi Kivityb93463a2007-10-25 16:52:32 +02002757 vapic_exit(vcpu);
2758
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002759 return r;
2760}
2761
2762int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2763{
2764 int r;
2765 sigset_t sigsaved;
2766
2767 vcpu_load(vcpu);
2768
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002769 if (unlikely(vcpu->arch.mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002770 kvm_vcpu_block(vcpu);
2771 vcpu_put(vcpu);
2772 return -EAGAIN;
2773 }
2774
2775 if (vcpu->sigset_active)
2776 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2777
2778 /* re-sync apic's tpr */
2779 if (!irqchip_in_kernel(vcpu->kvm))
2780 set_cr8(vcpu, kvm_run->cr8);
2781
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002782 if (vcpu->arch.pio.cur_count) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002783 r = complete_pio(vcpu);
2784 if (r)
2785 goto out;
2786 }
2787#if CONFIG_HAS_IOMEM
2788 if (vcpu->mmio_needed) {
2789 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
2790 vcpu->mmio_read_completed = 1;
2791 vcpu->mmio_needed = 0;
2792 r = emulate_instruction(vcpu, kvm_run,
Sheng Yang571008d2008-01-02 14:49:22 +08002793 vcpu->arch.mmio_fault_cr2, 0,
2794 EMULTYPE_NO_DECODE);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002795 if (r == EMULATE_DO_MMIO) {
2796 /*
2797 * Read-modify-write. Back to userspace.
2798 */
2799 r = 0;
2800 goto out;
2801 }
2802 }
2803#endif
2804 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
2805 kvm_x86_ops->cache_regs(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002806 vcpu->arch.regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002807 kvm_x86_ops->decache_regs(vcpu);
2808 }
2809
2810 r = __vcpu_run(vcpu, kvm_run);
2811
2812out:
2813 if (vcpu->sigset_active)
2814 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2815
2816 vcpu_put(vcpu);
2817 return r;
2818}
2819
2820int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2821{
2822 vcpu_load(vcpu);
2823
2824 kvm_x86_ops->cache_regs(vcpu);
2825
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002826 regs->rax = vcpu->arch.regs[VCPU_REGS_RAX];
2827 regs->rbx = vcpu->arch.regs[VCPU_REGS_RBX];
2828 regs->rcx = vcpu->arch.regs[VCPU_REGS_RCX];
2829 regs->rdx = vcpu->arch.regs[VCPU_REGS_RDX];
2830 regs->rsi = vcpu->arch.regs[VCPU_REGS_RSI];
2831 regs->rdi = vcpu->arch.regs[VCPU_REGS_RDI];
2832 regs->rsp = vcpu->arch.regs[VCPU_REGS_RSP];
2833 regs->rbp = vcpu->arch.regs[VCPU_REGS_RBP];
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002834#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002835 regs->r8 = vcpu->arch.regs[VCPU_REGS_R8];
2836 regs->r9 = vcpu->arch.regs[VCPU_REGS_R9];
2837 regs->r10 = vcpu->arch.regs[VCPU_REGS_R10];
2838 regs->r11 = vcpu->arch.regs[VCPU_REGS_R11];
2839 regs->r12 = vcpu->arch.regs[VCPU_REGS_R12];
2840 regs->r13 = vcpu->arch.regs[VCPU_REGS_R13];
2841 regs->r14 = vcpu->arch.regs[VCPU_REGS_R14];
2842 regs->r15 = vcpu->arch.regs[VCPU_REGS_R15];
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002843#endif
2844
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002845 regs->rip = vcpu->arch.rip;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002846 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
2847
2848 /*
2849 * Don't leak debug flags in case they were set for guest debugging
2850 */
2851 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2852 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2853
2854 vcpu_put(vcpu);
2855
2856 return 0;
2857}
2858
2859int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2860{
2861 vcpu_load(vcpu);
2862
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002863 vcpu->arch.regs[VCPU_REGS_RAX] = regs->rax;
2864 vcpu->arch.regs[VCPU_REGS_RBX] = regs->rbx;
2865 vcpu->arch.regs[VCPU_REGS_RCX] = regs->rcx;
2866 vcpu->arch.regs[VCPU_REGS_RDX] = regs->rdx;
2867 vcpu->arch.regs[VCPU_REGS_RSI] = regs->rsi;
2868 vcpu->arch.regs[VCPU_REGS_RDI] = regs->rdi;
2869 vcpu->arch.regs[VCPU_REGS_RSP] = regs->rsp;
2870 vcpu->arch.regs[VCPU_REGS_RBP] = regs->rbp;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002871#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002872 vcpu->arch.regs[VCPU_REGS_R8] = regs->r8;
2873 vcpu->arch.regs[VCPU_REGS_R9] = regs->r9;
2874 vcpu->arch.regs[VCPU_REGS_R10] = regs->r10;
2875 vcpu->arch.regs[VCPU_REGS_R11] = regs->r11;
2876 vcpu->arch.regs[VCPU_REGS_R12] = regs->r12;
2877 vcpu->arch.regs[VCPU_REGS_R13] = regs->r13;
2878 vcpu->arch.regs[VCPU_REGS_R14] = regs->r14;
2879 vcpu->arch.regs[VCPU_REGS_R15] = regs->r15;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002880#endif
2881
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002882 vcpu->arch.rip = regs->rip;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002883 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
2884
2885 kvm_x86_ops->decache_regs(vcpu);
2886
2887 vcpu_put(vcpu);
2888
2889 return 0;
2890}
2891
2892static void get_segment(struct kvm_vcpu *vcpu,
2893 struct kvm_segment *var, int seg)
2894{
2895 return kvm_x86_ops->get_segment(vcpu, var, seg);
2896}
2897
2898void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2899{
2900 struct kvm_segment cs;
2901
2902 get_segment(vcpu, &cs, VCPU_SREG_CS);
2903 *db = cs.db;
2904 *l = cs.l;
2905}
2906EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2907
2908int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2909 struct kvm_sregs *sregs)
2910{
2911 struct descriptor_table dt;
2912 int pending_vec;
2913
2914 vcpu_load(vcpu);
2915
2916 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2917 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2918 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2919 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2920 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2921 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2922
2923 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2924 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2925
2926 kvm_x86_ops->get_idt(vcpu, &dt);
2927 sregs->idt.limit = dt.limit;
2928 sregs->idt.base = dt.base;
2929 kvm_x86_ops->get_gdt(vcpu, &dt);
2930 sregs->gdt.limit = dt.limit;
2931 sregs->gdt.base = dt.base;
2932
2933 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002934 sregs->cr0 = vcpu->arch.cr0;
2935 sregs->cr2 = vcpu->arch.cr2;
2936 sregs->cr3 = vcpu->arch.cr3;
2937 sregs->cr4 = vcpu->arch.cr4;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002938 sregs->cr8 = get_cr8(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002939 sregs->efer = vcpu->arch.shadow_efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002940 sregs->apic_base = kvm_get_apic_base(vcpu);
2941
2942 if (irqchip_in_kernel(vcpu->kvm)) {
2943 memset(sregs->interrupt_bitmap, 0,
2944 sizeof sregs->interrupt_bitmap);
2945 pending_vec = kvm_x86_ops->get_irq(vcpu);
2946 if (pending_vec >= 0)
2947 set_bit(pending_vec,
2948 (unsigned long *)sregs->interrupt_bitmap);
2949 } else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002950 memcpy(sregs->interrupt_bitmap, vcpu->arch.irq_pending,
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002951 sizeof sregs->interrupt_bitmap);
2952
2953 vcpu_put(vcpu);
2954
2955 return 0;
2956}
2957
2958static void set_segment(struct kvm_vcpu *vcpu,
2959 struct kvm_segment *var, int seg)
2960{
2961 return kvm_x86_ops->set_segment(vcpu, var, seg);
2962}
2963
2964int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2965 struct kvm_sregs *sregs)
2966{
2967 int mmu_reset_needed = 0;
2968 int i, pending_vec, max_bits;
2969 struct descriptor_table dt;
2970
2971 vcpu_load(vcpu);
2972
2973 dt.limit = sregs->idt.limit;
2974 dt.base = sregs->idt.base;
2975 kvm_x86_ops->set_idt(vcpu, &dt);
2976 dt.limit = sregs->gdt.limit;
2977 dt.base = sregs->gdt.base;
2978 kvm_x86_ops->set_gdt(vcpu, &dt);
2979
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002980 vcpu->arch.cr2 = sregs->cr2;
2981 mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
2982 vcpu->arch.cr3 = sregs->cr3;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002983
2984 set_cr8(vcpu, sregs->cr8);
2985
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002986 mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002987 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002988 kvm_set_apic_base(vcpu, sregs->apic_base);
2989
2990 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2991
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002992 mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002993 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00002994 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002995
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002996 mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002997 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
2998 if (!is_long_mode(vcpu) && is_pae(vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002999 load_pdptrs(vcpu, vcpu->arch.cr3);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003000
3001 if (mmu_reset_needed)
3002 kvm_mmu_reset_context(vcpu);
3003
3004 if (!irqchip_in_kernel(vcpu->kvm)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003005 memcpy(vcpu->arch.irq_pending, sregs->interrupt_bitmap,
3006 sizeof vcpu->arch.irq_pending);
3007 vcpu->arch.irq_summary = 0;
3008 for (i = 0; i < ARRAY_SIZE(vcpu->arch.irq_pending); ++i)
3009 if (vcpu->arch.irq_pending[i])
3010 __set_bit(i, &vcpu->arch.irq_summary);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003011 } else {
3012 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
3013 pending_vec = find_first_bit(
3014 (const unsigned long *)sregs->interrupt_bitmap,
3015 max_bits);
3016 /* Only pending external irq is handled here */
3017 if (pending_vec < max_bits) {
3018 kvm_x86_ops->set_irq(vcpu, pending_vec);
3019 pr_debug("Set back pending irq %d\n",
3020 pending_vec);
3021 }
3022 }
3023
3024 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
3025 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
3026 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
3027 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
3028 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
3029 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
3030
3031 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
3032 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
3033
3034 vcpu_put(vcpu);
3035
3036 return 0;
3037}
3038
3039int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
3040 struct kvm_debug_guest *dbg)
3041{
3042 int r;
3043
3044 vcpu_load(vcpu);
3045
3046 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
3047
3048 vcpu_put(vcpu);
3049
3050 return r;
3051}
3052
3053/*
Hollis Blanchardd0752062007-10-31 17:24:25 -05003054 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
3055 * we have asm/x86/processor.h
3056 */
3057struct fxsave {
3058 u16 cwd;
3059 u16 swd;
3060 u16 twd;
3061 u16 fop;
3062 u64 rip;
3063 u64 rdp;
3064 u32 mxcsr;
3065 u32 mxcsr_mask;
3066 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
3067#ifdef CONFIG_X86_64
3068 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
3069#else
3070 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
3071#endif
3072};
3073
Zhang Xiantao8b006792007-11-16 13:05:55 +08003074/*
3075 * Translate a guest virtual address to a guest physical address.
3076 */
3077int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
3078 struct kvm_translation *tr)
3079{
3080 unsigned long vaddr = tr->linear_address;
3081 gpa_t gpa;
3082
3083 vcpu_load(vcpu);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003084 down_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003085 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003086 up_read(&vcpu->kvm->slots_lock);
Zhang Xiantao8b006792007-11-16 13:05:55 +08003087 tr->physical_address = gpa;
3088 tr->valid = gpa != UNMAPPED_GVA;
3089 tr->writeable = 1;
3090 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08003091 vcpu_put(vcpu);
3092
3093 return 0;
3094}
3095
Hollis Blanchardd0752062007-10-31 17:24:25 -05003096int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3097{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003098 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05003099
3100 vcpu_load(vcpu);
3101
3102 memcpy(fpu->fpr, fxsave->st_space, 128);
3103 fpu->fcw = fxsave->cwd;
3104 fpu->fsw = fxsave->swd;
3105 fpu->ftwx = fxsave->twd;
3106 fpu->last_opcode = fxsave->fop;
3107 fpu->last_ip = fxsave->rip;
3108 fpu->last_dp = fxsave->rdp;
3109 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
3110
3111 vcpu_put(vcpu);
3112
3113 return 0;
3114}
3115
3116int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3117{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003118 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05003119
3120 vcpu_load(vcpu);
3121
3122 memcpy(fxsave->st_space, fpu->fpr, 128);
3123 fxsave->cwd = fpu->fcw;
3124 fxsave->swd = fpu->fsw;
3125 fxsave->twd = fpu->ftwx;
3126 fxsave->fop = fpu->last_opcode;
3127 fxsave->rip = fpu->last_ip;
3128 fxsave->rdp = fpu->last_dp;
3129 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
3130
3131 vcpu_put(vcpu);
3132
3133 return 0;
3134}
3135
3136void fx_init(struct kvm_vcpu *vcpu)
3137{
3138 unsigned after_mxcsr_mask;
3139
3140 /* Initialize guest FPU by resetting ours and saving into guest's */
3141 preempt_disable();
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003142 fx_save(&vcpu->arch.host_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05003143 fpu_init();
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003144 fx_save(&vcpu->arch.guest_fx_image);
3145 fx_restore(&vcpu->arch.host_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05003146 preempt_enable();
3147
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003148 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05003149 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003150 vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
3151 memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
Hollis Blanchardd0752062007-10-31 17:24:25 -05003152 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
3153}
3154EXPORT_SYMBOL_GPL(fx_init);
3155
3156void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
3157{
3158 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
3159 return;
3160
3161 vcpu->guest_fpu_loaded = 1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003162 fx_save(&vcpu->arch.host_fx_image);
3163 fx_restore(&vcpu->arch.guest_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05003164}
3165EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
3166
3167void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
3168{
3169 if (!vcpu->guest_fpu_loaded)
3170 return;
3171
3172 vcpu->guest_fpu_loaded = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003173 fx_save(&vcpu->arch.guest_fx_image);
3174 fx_restore(&vcpu->arch.host_fx_image);
Avi Kivityf096ed82007-11-18 13:54:33 +02003175 ++vcpu->stat.fpu_reload;
Hollis Blanchardd0752062007-10-31 17:24:25 -05003176}
3177EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003178
3179void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
3180{
3181 kvm_x86_ops->vcpu_free(vcpu);
3182}
3183
3184struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
3185 unsigned int id)
3186{
Avi Kivity26e52152007-11-20 15:30:24 +02003187 return kvm_x86_ops->vcpu_create(kvm, id);
3188}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003189
Avi Kivity26e52152007-11-20 15:30:24 +02003190int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
3191{
3192 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003193
3194 /* We do fxsave: this must be aligned. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003195 BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003196
3197 vcpu_load(vcpu);
3198 r = kvm_arch_vcpu_reset(vcpu);
3199 if (r == 0)
3200 r = kvm_mmu_setup(vcpu);
3201 vcpu_put(vcpu);
3202 if (r < 0)
3203 goto free_vcpu;
3204
Avi Kivity26e52152007-11-20 15:30:24 +02003205 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003206free_vcpu:
3207 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivity26e52152007-11-20 15:30:24 +02003208 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003209}
3210
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06003211void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003212{
3213 vcpu_load(vcpu);
3214 kvm_mmu_unload(vcpu);
3215 vcpu_put(vcpu);
3216
3217 kvm_x86_ops->vcpu_free(vcpu);
3218}
3219
3220int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
3221{
3222 return kvm_x86_ops->vcpu_reset(vcpu);
3223}
3224
3225void kvm_arch_hardware_enable(void *garbage)
3226{
3227 kvm_x86_ops->hardware_enable(garbage);
3228}
3229
3230void kvm_arch_hardware_disable(void *garbage)
3231{
3232 kvm_x86_ops->hardware_disable(garbage);
3233}
3234
3235int kvm_arch_hardware_setup(void)
3236{
3237 return kvm_x86_ops->hardware_setup();
3238}
3239
3240void kvm_arch_hardware_unsetup(void)
3241{
3242 kvm_x86_ops->hardware_unsetup();
3243}
3244
3245void kvm_arch_check_processor_compat(void *rtn)
3246{
3247 kvm_x86_ops->check_processor_compatibility(rtn);
3248}
3249
3250int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
3251{
3252 struct page *page;
3253 struct kvm *kvm;
3254 int r;
3255
3256 BUG_ON(vcpu->kvm == NULL);
3257 kvm = vcpu->kvm;
3258
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003259 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003260 if (!irqchip_in_kernel(kvm) || vcpu->vcpu_id == 0)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003261 vcpu->arch.mp_state = VCPU_MP_STATE_RUNNABLE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003262 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003263 vcpu->arch.mp_state = VCPU_MP_STATE_UNINITIALIZED;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003264
3265 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
3266 if (!page) {
3267 r = -ENOMEM;
3268 goto fail;
3269 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003270 vcpu->arch.pio_data = page_address(page);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003271
3272 r = kvm_mmu_create(vcpu);
3273 if (r < 0)
3274 goto fail_free_pio_data;
3275
3276 if (irqchip_in_kernel(kvm)) {
3277 r = kvm_create_lapic(vcpu);
3278 if (r < 0)
3279 goto fail_mmu_destroy;
3280 }
3281
3282 return 0;
3283
3284fail_mmu_destroy:
3285 kvm_mmu_destroy(vcpu);
3286fail_free_pio_data:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003287 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003288fail:
3289 return r;
3290}
3291
3292void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
3293{
3294 kvm_free_lapic(vcpu);
3295 kvm_mmu_destroy(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003296 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003297}
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08003298
3299struct kvm *kvm_arch_create_vm(void)
3300{
3301 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
3302
3303 if (!kvm)
3304 return ERR_PTR(-ENOMEM);
3305
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003306 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08003307
3308 return kvm;
3309}
3310
3311static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
3312{
3313 vcpu_load(vcpu);
3314 kvm_mmu_unload(vcpu);
3315 vcpu_put(vcpu);
3316}
3317
3318static void kvm_free_vcpus(struct kvm *kvm)
3319{
3320 unsigned int i;
3321
3322 /*
3323 * Unpin any mmu pages first.
3324 */
3325 for (i = 0; i < KVM_MAX_VCPUS; ++i)
3326 if (kvm->vcpus[i])
3327 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
3328 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
3329 if (kvm->vcpus[i]) {
3330 kvm_arch_vcpu_free(kvm->vcpus[i]);
3331 kvm->vcpus[i] = NULL;
3332 }
3333 }
3334
3335}
3336
3337void kvm_arch_destroy_vm(struct kvm *kvm)
3338{
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08003339 kfree(kvm->arch.vpic);
3340 kfree(kvm->arch.vioapic);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08003341 kvm_free_vcpus(kvm);
3342 kvm_free_physmem(kvm);
3343 kfree(kvm);
3344}
Zhang Xiantao0de10342007-11-20 16:25:04 +08003345
3346int kvm_arch_set_memory_region(struct kvm *kvm,
3347 struct kvm_userspace_memory_region *mem,
3348 struct kvm_memory_slot old,
3349 int user_alloc)
3350{
3351 int npages = mem->memory_size >> PAGE_SHIFT;
3352 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
3353
3354 /*To keep backward compatibility with older userspace,
3355 *x86 needs to hanlde !user_alloc case.
3356 */
3357 if (!user_alloc) {
3358 if (npages && !old.rmap) {
Izik Eidus72dc67a2008-02-10 18:04:15 +02003359 down_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08003360 memslot->userspace_addr = do_mmap(NULL, 0,
3361 npages * PAGE_SIZE,
3362 PROT_READ | PROT_WRITE,
3363 MAP_SHARED | MAP_ANONYMOUS,
3364 0);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003365 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08003366
3367 if (IS_ERR((void *)memslot->userspace_addr))
3368 return PTR_ERR((void *)memslot->userspace_addr);
3369 } else {
3370 if (!old.user_alloc && old.rmap) {
3371 int ret;
3372
Izik Eidus72dc67a2008-02-10 18:04:15 +02003373 down_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08003374 ret = do_munmap(current->mm, old.userspace_addr,
3375 old.npages * PAGE_SIZE);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003376 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08003377 if (ret < 0)
3378 printk(KERN_WARNING
3379 "kvm_vm_ioctl_set_memory_region: "
3380 "failed to munmap memory\n");
3381 }
3382 }
3383 }
3384
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003385 if (!kvm->arch.n_requested_mmu_pages) {
Zhang Xiantao0de10342007-11-20 16:25:04 +08003386 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
3387 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
3388 }
3389
3390 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
3391 kvm_flush_remote_tlbs(kvm);
3392
3393 return 0;
3394}
Zhang Xiantao1d737c82007-12-14 09:35:10 +08003395
3396int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
3397{
3398 return vcpu->arch.mp_state == VCPU_MP_STATE_RUNNABLE
3399 || vcpu->arch.mp_state == VCPU_MP_STATE_SIPI_RECEIVED;
3400}
Zhang Xiantao57361992007-12-17 14:21:40 +08003401
3402static void vcpu_kick_intr(void *info)
3403{
3404#ifdef DEBUG
3405 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info;
3406 printk(KERN_DEBUG "vcpu_kick_intr %p \n", vcpu);
3407#endif
3408}
3409
3410void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
3411{
3412 int ipi_pcpu = vcpu->cpu;
3413
3414 if (waitqueue_active(&vcpu->wq)) {
3415 wake_up_interruptible(&vcpu->wq);
3416 ++vcpu->stat.halt_wakeup;
3417 }
3418 if (vcpu->guest_mode)
3419 smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0, 0);
3420}