blob: 0dd038e7392b9390aa5029c70645c2196651517c [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 Otte313a3dc2007-10-11 19:16:52 +020018#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080019#include "mmu.h"
Carsten Otte313a3dc2007-10-11 19:16:52 +020020
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020021#include <linux/clocksource.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020022#include <linux/kvm.h>
23#include <linux/fs.h>
24#include <linux/vmalloc.h>
Carsten Otte5fb76f92007-10-29 16:08:51 +010025#include <linux/module.h>
Zhang Xiantao0de10342007-11-20 16:25:04 +080026#include <linux/mman.h>
Marcelo Tosatti2bacc552007-12-12 10:46:12 -050027#include <linux/highmem.h>
Carsten Otte043405e2007-10-10 17:16:19 +020028
29#include <asm/uaccess.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080030#include <asm/msr.h>
Avi Kivitya5f61302008-02-20 17:57:21 +020031#include <asm/desc.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) },
Amit Shahf11c3a82008-02-21 01:00:30 +053075 { "hypercalls", VCPU_STAT(hypercalls) },
Avi Kivityba1389b2007-11-18 16:24:12 +020076 { "request_irq", VCPU_STAT(request_irq_exits) },
77 { "irq_exits", VCPU_STAT(irq_exits) },
78 { "host_state_reload", VCPU_STAT(host_state_reload) },
79 { "efer_reload", VCPU_STAT(efer_reload) },
80 { "fpu_reload", VCPU_STAT(fpu_reload) },
81 { "insn_emulation", VCPU_STAT(insn_emulation) },
82 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
Avi Kivity4cee5762007-11-18 16:37:07 +020083 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
84 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
85 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
86 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
87 { "mmu_flooded", VM_STAT(mmu_flooded) },
88 { "mmu_recycled", VM_STAT(mmu_recycled) },
Avi Kivitydfc5aa02007-12-18 19:47:18 +020089 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
Avi Kivity0f74a242007-11-20 23:01:14 +020090 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Hollis Blanchard417bc302007-10-31 17:24:23 -050091 { NULL }
92};
93
94
Carsten Otte5fb76f92007-10-29 16:08:51 +010095unsigned long segment_base(u16 selector)
96{
97 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +020098 struct desc_struct *d;
Carsten Otte5fb76f92007-10-29 16:08:51 +010099 unsigned long table_base;
100 unsigned long v;
101
102 if (selector == 0)
103 return 0;
104
105 asm("sgdt %0" : "=m"(gdt));
106 table_base = gdt.base;
107
108 if (selector & 4) { /* from ldt */
109 u16 ldt_selector;
110
111 asm("sldt %0" : "=g"(ldt_selector));
112 table_base = segment_base(ldt_selector);
113 }
Avi Kivitya5f61302008-02-20 17:57:21 +0200114 d = (struct desc_struct *)(table_base + (selector & ~7));
115 v = d->base0 | ((unsigned long)d->base1 << 16) |
116 ((unsigned long)d->base2 << 24);
Carsten Otte5fb76f92007-10-29 16:08:51 +0100117#ifdef CONFIG_X86_64
Avi Kivitya5f61302008-02-20 17:57:21 +0200118 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
119 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
Carsten Otte5fb76f92007-10-29 16:08:51 +0100120#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,
Alexander Graf847f0ad2008-02-21 12:11:01 +0100429 MSR_IA32_PERF_STATUS,
Carsten Otte043405e2007-10-10 17:16:19 +0200430};
431
432static unsigned num_msrs_to_save;
433
434static u32 emulated_msrs[] = {
435 MSR_IA32_MISC_ENABLE,
436};
437
Carsten Otte15c4a642007-10-30 18:44:17 +0100438static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
439{
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100440 if (efer & efer_reserved_bits) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100441 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
442 efer);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200443 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100444 return;
445 }
446
447 if (is_paging(vcpu)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800448 && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100449 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200450 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100451 return;
452 }
453
454 kvm_x86_ops->set_efer(vcpu, efer);
455
456 efer &= ~EFER_LMA;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800457 efer |= vcpu->arch.shadow_efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +0100458
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800459 vcpu->arch.shadow_efer = efer;
Carsten Otte15c4a642007-10-30 18:44:17 +0100460}
461
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100462void kvm_enable_efer_bits(u64 mask)
463{
464 efer_reserved_bits &= ~mask;
465}
466EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
467
468
Carsten Otte15c4a642007-10-30 18:44:17 +0100469/*
470 * Writes msr value into into the appropriate "register".
471 * Returns 0 on success, non-0 otherwise.
472 * Assumes vcpu_load() was already called.
473 */
474int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
475{
476 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
477}
478
Carsten Otte313a3dc2007-10-11 19:16:52 +0200479/*
480 * Adapt set_msr() to msr_io()'s calling convention
481 */
482static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
483{
484 return kvm_set_msr(vcpu, index, *data);
485}
486
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200487static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
488{
489 static int version;
490 struct kvm_wall_clock wc;
491 struct timespec wc_ts;
492
493 if (!wall_clock)
494 return;
495
496 version++;
497
498 down_read(&kvm->slots_lock);
499 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
500
501 wc_ts = current_kernel_time();
502 wc.wc_sec = wc_ts.tv_sec;
503 wc.wc_nsec = wc_ts.tv_nsec;
504 wc.wc_version = version;
505
506 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
507
508 version++;
509 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
510 up_read(&kvm->slots_lock);
511}
512
513static void kvm_write_guest_time(struct kvm_vcpu *v)
514{
515 struct timespec ts;
516 unsigned long flags;
517 struct kvm_vcpu_arch *vcpu = &v->arch;
518 void *shared_kaddr;
519
520 if ((!vcpu->time_page))
521 return;
522
523 /* Keep irq disabled to prevent changes to the clock */
524 local_irq_save(flags);
525 kvm_get_msr(v, MSR_IA32_TIME_STAMP_COUNTER,
526 &vcpu->hv_clock.tsc_timestamp);
527 ktime_get_ts(&ts);
528 local_irq_restore(flags);
529
530 /* With all the info we got, fill in the values */
531
532 vcpu->hv_clock.system_time = ts.tv_nsec +
533 (NSEC_PER_SEC * (u64)ts.tv_sec);
534 /*
535 * The interface expects us to write an even number signaling that the
536 * update is finished. Since the guest won't see the intermediate
537 * state, we just write "2" at the end
538 */
539 vcpu->hv_clock.version = 2;
540
541 shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
542
543 memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
544 sizeof(vcpu->hv_clock));
545
546 kunmap_atomic(shared_kaddr, KM_USER0);
547
548 mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
549}
550
Carsten Otte15c4a642007-10-30 18:44:17 +0100551
552int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
553{
554 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100555 case MSR_EFER:
556 set_efer(vcpu, data);
557 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100558 case MSR_IA32_MC0_STATUS:
559 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
560 __FUNCTION__, data);
561 break;
562 case MSR_IA32_MCG_STATUS:
563 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
564 __FUNCTION__, data);
565 break;
Joerg Roedelc7ac6792008-02-11 20:28:27 +0100566 case MSR_IA32_MCG_CTL:
567 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_CTL 0x%llx, nop\n",
568 __FUNCTION__, data);
569 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100570 case MSR_IA32_UCODE_REV:
571 case MSR_IA32_UCODE_WRITE:
572 case 0x200 ... 0x2ff: /* MTRRs */
573 break;
574 case MSR_IA32_APICBASE:
575 kvm_set_apic_base(vcpu, data);
576 break;
577 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800578 vcpu->arch.ia32_misc_enable_msr = data;
Carsten Otte15c4a642007-10-30 18:44:17 +0100579 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200580 case MSR_KVM_WALL_CLOCK:
581 vcpu->kvm->arch.wall_clock = data;
582 kvm_write_wall_clock(vcpu->kvm, data);
583 break;
584 case MSR_KVM_SYSTEM_TIME: {
585 if (vcpu->arch.time_page) {
586 kvm_release_page_dirty(vcpu->arch.time_page);
587 vcpu->arch.time_page = NULL;
588 }
589
590 vcpu->arch.time = data;
591
592 /* we verify if the enable bit is set... */
593 if (!(data & 1))
594 break;
595
596 /* ...but clean it before doing the actual write */
597 vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
598
599 vcpu->arch.hv_clock.tsc_to_system_mul =
600 clocksource_khz2mult(tsc_khz, 22);
601 vcpu->arch.hv_clock.tsc_shift = 22;
602
603 down_read(&current->mm->mmap_sem);
604 down_read(&vcpu->kvm->slots_lock);
605 vcpu->arch.time_page =
606 gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
607 up_read(&vcpu->kvm->slots_lock);
608 up_read(&current->mm->mmap_sem);
609
610 if (is_error_page(vcpu->arch.time_page)) {
611 kvm_release_page_clean(vcpu->arch.time_page);
612 vcpu->arch.time_page = NULL;
613 }
614
615 kvm_write_guest_time(vcpu);
616 break;
617 }
Carsten Otte15c4a642007-10-30 18:44:17 +0100618 default:
Avi Kivity565f1fb2007-12-19 12:02:40 +0200619 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n", msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +0100620 return 1;
621 }
622 return 0;
623}
624EXPORT_SYMBOL_GPL(kvm_set_msr_common);
625
626
627/*
628 * Reads an msr value (of 'msr_index') into 'pdata'.
629 * Returns 0 on success, non-0 otherwise.
630 * Assumes vcpu_load() was already called.
631 */
632int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
633{
634 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
635}
636
637int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
638{
639 u64 data;
640
641 switch (msr) {
642 case 0xc0010010: /* SYSCFG */
643 case 0xc0010015: /* HWCR */
644 case MSR_IA32_PLATFORM_ID:
645 case MSR_IA32_P5_MC_ADDR:
646 case MSR_IA32_P5_MC_TYPE:
647 case MSR_IA32_MC0_CTL:
648 case MSR_IA32_MCG_STATUS:
649 case MSR_IA32_MCG_CAP:
Joerg Roedelc7ac6792008-02-11 20:28:27 +0100650 case MSR_IA32_MCG_CTL:
Carsten Otte15c4a642007-10-30 18:44:17 +0100651 case MSR_IA32_MC0_MISC:
652 case MSR_IA32_MC0_MISC+4:
653 case MSR_IA32_MC0_MISC+8:
654 case MSR_IA32_MC0_MISC+12:
655 case MSR_IA32_MC0_MISC+16:
656 case MSR_IA32_UCODE_REV:
Carsten Otte15c4a642007-10-30 18:44:17 +0100657 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;
Alexander Graf847f0ad2008-02-21 12:11:01 +0100672 case MSR_IA32_PERF_STATUS:
673 /* TSC increment by tick */
674 data = 1000ULL;
675 /* CPU multiplier */
676 data |= (((uint64_t)4ULL) << 40);
677 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100678 case MSR_EFER:
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800679 data = vcpu->arch.shadow_efer;
Carsten Otte15c4a642007-10-30 18:44:17 +0100680 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200681 case MSR_KVM_WALL_CLOCK:
682 data = vcpu->kvm->arch.wall_clock;
683 break;
684 case MSR_KVM_SYSTEM_TIME:
685 data = vcpu->arch.time;
686 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100687 default:
688 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
689 return 1;
690 }
691 *pdata = data;
692 return 0;
693}
694EXPORT_SYMBOL_GPL(kvm_get_msr_common);
695
Carsten Otte313a3dc2007-10-11 19:16:52 +0200696/*
697 * Read or write a bunch of msrs. All parameters are kernel addresses.
698 *
699 * @return number of msrs set successfully.
700 */
701static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
702 struct kvm_msr_entry *entries,
703 int (*do_msr)(struct kvm_vcpu *vcpu,
704 unsigned index, u64 *data))
705{
706 int i;
707
708 vcpu_load(vcpu);
709
710 for (i = 0; i < msrs->nmsrs; ++i)
711 if (do_msr(vcpu, entries[i].index, &entries[i].data))
712 break;
713
714 vcpu_put(vcpu);
715
716 return i;
717}
718
719/*
720 * Read or write a bunch of msrs. Parameters are user addresses.
721 *
722 * @return number of msrs set successfully.
723 */
724static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
725 int (*do_msr)(struct kvm_vcpu *vcpu,
726 unsigned index, u64 *data),
727 int writeback)
728{
729 struct kvm_msrs msrs;
730 struct kvm_msr_entry *entries;
731 int r, n;
732 unsigned size;
733
734 r = -EFAULT;
735 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
736 goto out;
737
738 r = -E2BIG;
739 if (msrs.nmsrs >= MAX_IO_MSRS)
740 goto out;
741
742 r = -ENOMEM;
743 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
744 entries = vmalloc(size);
745 if (!entries)
746 goto out;
747
748 r = -EFAULT;
749 if (copy_from_user(entries, user_msrs->entries, size))
750 goto out_free;
751
752 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
753 if (r < 0)
754 goto out_free;
755
756 r = -EFAULT;
757 if (writeback && copy_to_user(user_msrs->entries, entries, size))
758 goto out_free;
759
760 r = n;
761
762out_free:
763 vfree(entries);
764out:
765 return r;
766}
767
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800768/*
769 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
770 * cached on it.
771 */
772void decache_vcpus_on_cpu(int cpu)
773{
774 struct kvm *vm;
775 struct kvm_vcpu *vcpu;
776 int i;
777
778 spin_lock(&kvm_lock);
779 list_for_each_entry(vm, &vm_list, vm_list)
780 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
781 vcpu = vm->vcpus[i];
782 if (!vcpu)
783 continue;
784 /*
785 * If the vcpu is locked, then it is running on some
786 * other cpu and therefore it is not cached on the
787 * cpu in question.
788 *
789 * If it's not locked, check the last cpu it executed
790 * on.
791 */
792 if (mutex_trylock(&vcpu->mutex)) {
793 if (vcpu->cpu == cpu) {
794 kvm_x86_ops->vcpu_decache(vcpu);
795 vcpu->cpu = -1;
796 }
797 mutex_unlock(&vcpu->mutex);
798 }
799 }
800 spin_unlock(&kvm_lock);
801}
802
Zhang Xiantao018d00d2007-11-15 23:07:47 +0800803int kvm_dev_ioctl_check_extension(long ext)
804{
805 int r;
806
807 switch (ext) {
808 case KVM_CAP_IRQCHIP:
809 case KVM_CAP_HLT:
810 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
811 case KVM_CAP_USER_MEMORY:
812 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +0200813 case KVM_CAP_EXT_CPUID:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200814 case KVM_CAP_CLOCKSOURCE:
Zhang Xiantao018d00d2007-11-15 23:07:47 +0800815 r = 1;
816 break;
Avi Kivity774ead32007-12-26 13:57:04 +0200817 case KVM_CAP_VAPIC:
818 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
819 break;
Avi Kivityf7252302008-02-20 11:53:16 +0200820 case KVM_CAP_NR_VCPUS:
821 r = KVM_MAX_VCPUS;
822 break;
Avi Kivitya988b912008-02-20 11:59:20 +0200823 case KVM_CAP_NR_MEMSLOTS:
824 r = KVM_MEMORY_SLOTS;
825 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +0800826 default:
827 r = 0;
828 break;
829 }
830 return r;
831
832}
833
Carsten Otte043405e2007-10-10 17:16:19 +0200834long kvm_arch_dev_ioctl(struct file *filp,
835 unsigned int ioctl, unsigned long arg)
836{
837 void __user *argp = (void __user *)arg;
838 long r;
839
840 switch (ioctl) {
841 case KVM_GET_MSR_INDEX_LIST: {
842 struct kvm_msr_list __user *user_msr_list = argp;
843 struct kvm_msr_list msr_list;
844 unsigned n;
845
846 r = -EFAULT;
847 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
848 goto out;
849 n = msr_list.nmsrs;
850 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
851 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
852 goto out;
853 r = -E2BIG;
854 if (n < num_msrs_to_save)
855 goto out;
856 r = -EFAULT;
857 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
858 num_msrs_to_save * sizeof(u32)))
859 goto out;
860 if (copy_to_user(user_msr_list->indices
861 + num_msrs_to_save * sizeof(u32),
862 &emulated_msrs,
863 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
864 goto out;
865 r = 0;
866 break;
867 }
Avi Kivity674eea02008-02-11 18:37:23 +0200868 case KVM_GET_SUPPORTED_CPUID: {
869 struct kvm_cpuid2 __user *cpuid_arg = argp;
870 struct kvm_cpuid2 cpuid;
871
872 r = -EFAULT;
873 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
874 goto out;
875 r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
876 cpuid_arg->entries);
877 if (r)
878 goto out;
879
880 r = -EFAULT;
881 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
882 goto out;
883 r = 0;
884 break;
885 }
Carsten Otte043405e2007-10-10 17:16:19 +0200886 default:
887 r = -EINVAL;
888 }
889out:
890 return r;
891}
892
Carsten Otte313a3dc2007-10-11 19:16:52 +0200893void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
894{
895 kvm_x86_ops->vcpu_load(vcpu, cpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200896 kvm_write_guest_time(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200897}
898
899void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
900{
901 kvm_x86_ops->vcpu_put(vcpu);
Amit Shah9327fd12007-11-15 18:38:46 +0200902 kvm_put_guest_fpu(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200903}
904
Dan Kenigsberg07716712007-11-21 17:10:04 +0200905static int is_efer_nx(void)
Carsten Otte313a3dc2007-10-11 19:16:52 +0200906{
907 u64 efer;
Carsten Otte313a3dc2007-10-11 19:16:52 +0200908
909 rdmsrl(MSR_EFER, efer);
Dan Kenigsberg07716712007-11-21 17:10:04 +0200910 return efer & EFER_NX;
911}
912
913static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
914{
915 int i;
916 struct kvm_cpuid_entry2 *e, *entry;
917
Carsten Otte313a3dc2007-10-11 19:16:52 +0200918 entry = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800919 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
920 e = &vcpu->arch.cpuid_entries[i];
Carsten Otte313a3dc2007-10-11 19:16:52 +0200921 if (e->function == 0x80000001) {
922 entry = e;
923 break;
924 }
925 }
Dan Kenigsberg07716712007-11-21 17:10:04 +0200926 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
Carsten Otte313a3dc2007-10-11 19:16:52 +0200927 entry->edx &= ~(1 << 20);
928 printk(KERN_INFO "kvm: guest NX capability removed\n");
929 }
930}
931
Dan Kenigsberg07716712007-11-21 17:10:04 +0200932/* when an old userspace process fills a new kernel module */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200933static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
934 struct kvm_cpuid *cpuid,
935 struct kvm_cpuid_entry __user *entries)
936{
Dan Kenigsberg07716712007-11-21 17:10:04 +0200937 int r, i;
938 struct kvm_cpuid_entry *cpuid_entries;
939
940 r = -E2BIG;
941 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
942 goto out;
943 r = -ENOMEM;
944 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
945 if (!cpuid_entries)
946 goto out;
947 r = -EFAULT;
948 if (copy_from_user(cpuid_entries, entries,
949 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
950 goto out_free;
951 for (i = 0; i < cpuid->nent; i++) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800952 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
953 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
954 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
955 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
956 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
957 vcpu->arch.cpuid_entries[i].index = 0;
958 vcpu->arch.cpuid_entries[i].flags = 0;
959 vcpu->arch.cpuid_entries[i].padding[0] = 0;
960 vcpu->arch.cpuid_entries[i].padding[1] = 0;
961 vcpu->arch.cpuid_entries[i].padding[2] = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +0200962 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800963 vcpu->arch.cpuid_nent = cpuid->nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +0200964 cpuid_fix_nx_cap(vcpu);
965 r = 0;
966
967out_free:
968 vfree(cpuid_entries);
969out:
970 return r;
971}
972
973static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
974 struct kvm_cpuid2 *cpuid,
975 struct kvm_cpuid_entry2 __user *entries)
976{
Carsten Otte313a3dc2007-10-11 19:16:52 +0200977 int r;
978
979 r = -E2BIG;
980 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
981 goto out;
982 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800983 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
Dan Kenigsberg07716712007-11-21 17:10:04 +0200984 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
Carsten Otte313a3dc2007-10-11 19:16:52 +0200985 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800986 vcpu->arch.cpuid_nent = cpuid->nent;
Carsten Otte313a3dc2007-10-11 19:16:52 +0200987 return 0;
988
989out:
990 return r;
991}
992
Dan Kenigsberg07716712007-11-21 17:10:04 +0200993static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
994 struct kvm_cpuid2 *cpuid,
995 struct kvm_cpuid_entry2 __user *entries)
996{
997 int r;
998
999 r = -E2BIG;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001000 if (cpuid->nent < vcpu->arch.cpuid_nent)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001001 goto out;
1002 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001003 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
1004 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02001005 goto out;
1006 return 0;
1007
1008out:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001009 cpuid->nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001010 return r;
1011}
1012
1013static inline u32 bit(int bitno)
1014{
1015 return 1 << (bitno & 31);
1016}
1017
1018static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1019 u32 index)
1020{
1021 entry->function = function;
1022 entry->index = index;
1023 cpuid_count(entry->function, entry->index,
1024 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
1025 entry->flags = 0;
1026}
1027
1028static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1029 u32 index, int *nent, int maxnent)
1030{
1031 const u32 kvm_supported_word0_x86_features = bit(X86_FEATURE_FPU) |
1032 bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
1033 bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
1034 bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
1035 bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
1036 bit(X86_FEATURE_SEP) | bit(X86_FEATURE_PGE) |
1037 bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
1038 bit(X86_FEATURE_CLFLSH) | bit(X86_FEATURE_MMX) |
1039 bit(X86_FEATURE_FXSR) | bit(X86_FEATURE_XMM) |
1040 bit(X86_FEATURE_XMM2) | bit(X86_FEATURE_SELFSNOOP);
1041 const u32 kvm_supported_word1_x86_features = bit(X86_FEATURE_FPU) |
1042 bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
1043 bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
1044 bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
1045 bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
1046 bit(X86_FEATURE_PGE) |
1047 bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
1048 bit(X86_FEATURE_MMX) | bit(X86_FEATURE_FXSR) |
1049 bit(X86_FEATURE_SYSCALL) |
1050 (bit(X86_FEATURE_NX) && is_efer_nx()) |
1051#ifdef CONFIG_X86_64
1052 bit(X86_FEATURE_LM) |
1053#endif
1054 bit(X86_FEATURE_MMXEXT) |
1055 bit(X86_FEATURE_3DNOWEXT) |
1056 bit(X86_FEATURE_3DNOW);
1057 const u32 kvm_supported_word3_x86_features =
1058 bit(X86_FEATURE_XMM3) | bit(X86_FEATURE_CX16);
1059 const u32 kvm_supported_word6_x86_features =
1060 bit(X86_FEATURE_LAHF_LM) | bit(X86_FEATURE_CMP_LEGACY);
1061
1062 /* all func 2 cpuid_count() should be called on the same cpu */
1063 get_cpu();
1064 do_cpuid_1_ent(entry, function, index);
1065 ++*nent;
1066
1067 switch (function) {
1068 case 0:
1069 entry->eax = min(entry->eax, (u32)0xb);
1070 break;
1071 case 1:
1072 entry->edx &= kvm_supported_word0_x86_features;
1073 entry->ecx &= kvm_supported_word3_x86_features;
1074 break;
1075 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1076 * may return different values. This forces us to get_cpu() before
1077 * issuing the first command, and also to emulate this annoying behavior
1078 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1079 case 2: {
1080 int t, times = entry->eax & 0xff;
1081
1082 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1083 for (t = 1; t < times && *nent < maxnent; ++t) {
1084 do_cpuid_1_ent(&entry[t], function, 0);
1085 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1086 ++*nent;
1087 }
1088 break;
1089 }
1090 /* function 4 and 0xb have additional index. */
1091 case 4: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001092 int i, cache_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001093
1094 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1095 /* read more entries until cache_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001096 for (i = 1; *nent < maxnent; ++i) {
1097 cache_type = entry[i - 1].eax & 0x1f;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001098 if (!cache_type)
1099 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001100 do_cpuid_1_ent(&entry[i], function, i);
1101 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001102 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1103 ++*nent;
1104 }
1105 break;
1106 }
1107 case 0xb: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001108 int i, level_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001109
1110 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1111 /* read more entries until level_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001112 for (i = 1; *nent < maxnent; ++i) {
1113 level_type = entry[i - 1].ecx & 0xff;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001114 if (!level_type)
1115 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001116 do_cpuid_1_ent(&entry[i], function, i);
1117 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001118 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1119 ++*nent;
1120 }
1121 break;
1122 }
1123 case 0x80000000:
1124 entry->eax = min(entry->eax, 0x8000001a);
1125 break;
1126 case 0x80000001:
1127 entry->edx &= kvm_supported_word1_x86_features;
1128 entry->ecx &= kvm_supported_word6_x86_features;
1129 break;
1130 }
1131 put_cpu();
1132}
1133
Avi Kivity674eea02008-02-11 18:37:23 +02001134static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
Dan Kenigsberg07716712007-11-21 17:10:04 +02001135 struct kvm_cpuid_entry2 __user *entries)
1136{
1137 struct kvm_cpuid_entry2 *cpuid_entries;
1138 int limit, nent = 0, r = -E2BIG;
1139 u32 func;
1140
1141 if (cpuid->nent < 1)
1142 goto out;
1143 r = -ENOMEM;
1144 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
1145 if (!cpuid_entries)
1146 goto out;
1147
1148 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
1149 limit = cpuid_entries[0].eax;
1150 for (func = 1; func <= limit && nent < cpuid->nent; ++func)
1151 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1152 &nent, cpuid->nent);
1153 r = -E2BIG;
1154 if (nent >= cpuid->nent)
1155 goto out_free;
1156
1157 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
1158 limit = cpuid_entries[nent - 1].eax;
1159 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
1160 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1161 &nent, cpuid->nent);
1162 r = -EFAULT;
1163 if (copy_to_user(entries, cpuid_entries,
1164 nent * sizeof(struct kvm_cpuid_entry2)))
1165 goto out_free;
1166 cpuid->nent = nent;
1167 r = 0;
1168
1169out_free:
1170 vfree(cpuid_entries);
1171out:
1172 return r;
1173}
1174
Carsten Otte313a3dc2007-10-11 19:16:52 +02001175static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
1176 struct kvm_lapic_state *s)
1177{
1178 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001179 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001180 vcpu_put(vcpu);
1181
1182 return 0;
1183}
1184
1185static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
1186 struct kvm_lapic_state *s)
1187{
1188 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001189 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001190 kvm_apic_post_state_restore(vcpu);
1191 vcpu_put(vcpu);
1192
1193 return 0;
1194}
1195
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001196static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
1197 struct kvm_interrupt *irq)
1198{
1199 if (irq->irq < 0 || irq->irq >= 256)
1200 return -EINVAL;
1201 if (irqchip_in_kernel(vcpu->kvm))
1202 return -ENXIO;
1203 vcpu_load(vcpu);
1204
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001205 set_bit(irq->irq, vcpu->arch.irq_pending);
1206 set_bit(irq->irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001207
1208 vcpu_put(vcpu);
1209
1210 return 0;
1211}
1212
Avi Kivityb209749f2007-10-22 16:50:39 +02001213static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
1214 struct kvm_tpr_access_ctl *tac)
1215{
1216 if (tac->flags)
1217 return -EINVAL;
1218 vcpu->arch.tpr_access_reporting = !!tac->enabled;
1219 return 0;
1220}
1221
Carsten Otte313a3dc2007-10-11 19:16:52 +02001222long kvm_arch_vcpu_ioctl(struct file *filp,
1223 unsigned int ioctl, unsigned long arg)
1224{
1225 struct kvm_vcpu *vcpu = filp->private_data;
1226 void __user *argp = (void __user *)arg;
1227 int r;
1228
1229 switch (ioctl) {
1230 case KVM_GET_LAPIC: {
1231 struct kvm_lapic_state lapic;
1232
1233 memset(&lapic, 0, sizeof lapic);
1234 r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic);
1235 if (r)
1236 goto out;
1237 r = -EFAULT;
1238 if (copy_to_user(argp, &lapic, sizeof lapic))
1239 goto out;
1240 r = 0;
1241 break;
1242 }
1243 case KVM_SET_LAPIC: {
1244 struct kvm_lapic_state lapic;
1245
1246 r = -EFAULT;
1247 if (copy_from_user(&lapic, argp, sizeof lapic))
1248 goto out;
1249 r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);;
1250 if (r)
1251 goto out;
1252 r = 0;
1253 break;
1254 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001255 case KVM_INTERRUPT: {
1256 struct kvm_interrupt irq;
1257
1258 r = -EFAULT;
1259 if (copy_from_user(&irq, argp, sizeof irq))
1260 goto out;
1261 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1262 if (r)
1263 goto out;
1264 r = 0;
1265 break;
1266 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001267 case KVM_SET_CPUID: {
1268 struct kvm_cpuid __user *cpuid_arg = argp;
1269 struct kvm_cpuid cpuid;
1270
1271 r = -EFAULT;
1272 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1273 goto out;
1274 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
1275 if (r)
1276 goto out;
1277 break;
1278 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02001279 case KVM_SET_CPUID2: {
1280 struct kvm_cpuid2 __user *cpuid_arg = argp;
1281 struct kvm_cpuid2 cpuid;
1282
1283 r = -EFAULT;
1284 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1285 goto out;
1286 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
1287 cpuid_arg->entries);
1288 if (r)
1289 goto out;
1290 break;
1291 }
1292 case KVM_GET_CPUID2: {
1293 struct kvm_cpuid2 __user *cpuid_arg = argp;
1294 struct kvm_cpuid2 cpuid;
1295
1296 r = -EFAULT;
1297 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1298 goto out;
1299 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
1300 cpuid_arg->entries);
1301 if (r)
1302 goto out;
1303 r = -EFAULT;
1304 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1305 goto out;
1306 r = 0;
1307 break;
1308 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001309 case KVM_GET_MSRS:
1310 r = msr_io(vcpu, argp, kvm_get_msr, 1);
1311 break;
1312 case KVM_SET_MSRS:
1313 r = msr_io(vcpu, argp, do_set_msr, 0);
1314 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02001315 case KVM_TPR_ACCESS_REPORTING: {
1316 struct kvm_tpr_access_ctl tac;
1317
1318 r = -EFAULT;
1319 if (copy_from_user(&tac, argp, sizeof tac))
1320 goto out;
1321 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
1322 if (r)
1323 goto out;
1324 r = -EFAULT;
1325 if (copy_to_user(argp, &tac, sizeof tac))
1326 goto out;
1327 r = 0;
1328 break;
1329 };
Avi Kivityb93463a2007-10-25 16:52:32 +02001330 case KVM_SET_VAPIC_ADDR: {
1331 struct kvm_vapic_addr va;
1332
1333 r = -EINVAL;
1334 if (!irqchip_in_kernel(vcpu->kvm))
1335 goto out;
1336 r = -EFAULT;
1337 if (copy_from_user(&va, argp, sizeof va))
1338 goto out;
1339 r = 0;
1340 kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
1341 break;
1342 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001343 default:
1344 r = -EINVAL;
1345 }
1346out:
1347 return r;
1348}
1349
Carsten Otte1fe779f2007-10-29 16:08:35 +01001350static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
1351{
1352 int ret;
1353
1354 if (addr > (unsigned int)(-3 * PAGE_SIZE))
1355 return -1;
1356 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
1357 return ret;
1358}
1359
1360static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
1361 u32 kvm_nr_mmu_pages)
1362{
1363 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
1364 return -EINVAL;
1365
Izik Eidus72dc67a2008-02-10 18:04:15 +02001366 down_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001367
1368 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001369 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001370
Izik Eidus72dc67a2008-02-10 18:04:15 +02001371 up_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001372 return 0;
1373}
1374
1375static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
1376{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001377 return kvm->arch.n_alloc_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001378}
1379
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08001380gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
1381{
1382 int i;
1383 struct kvm_mem_alias *alias;
1384
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001385 for (i = 0; i < kvm->arch.naliases; ++i) {
1386 alias = &kvm->arch.aliases[i];
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08001387 if (gfn >= alias->base_gfn
1388 && gfn < alias->base_gfn + alias->npages)
1389 return alias->target_gfn + gfn - alias->base_gfn;
1390 }
1391 return gfn;
1392}
1393
Carsten Otte1fe779f2007-10-29 16:08:35 +01001394/*
1395 * Set a new alias region. Aliases map a portion of physical memory into
1396 * another portion. This is useful for memory windows, for example the PC
1397 * VGA region.
1398 */
1399static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
1400 struct kvm_memory_alias *alias)
1401{
1402 int r, n;
1403 struct kvm_mem_alias *p;
1404
1405 r = -EINVAL;
1406 /* General sanity checks */
1407 if (alias->memory_size & (PAGE_SIZE - 1))
1408 goto out;
1409 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
1410 goto out;
1411 if (alias->slot >= KVM_ALIAS_SLOTS)
1412 goto out;
1413 if (alias->guest_phys_addr + alias->memory_size
1414 < alias->guest_phys_addr)
1415 goto out;
1416 if (alias->target_phys_addr + alias->memory_size
1417 < alias->target_phys_addr)
1418 goto out;
1419
Izik Eidus72dc67a2008-02-10 18:04:15 +02001420 down_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001421
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001422 p = &kvm->arch.aliases[alias->slot];
Carsten Otte1fe779f2007-10-29 16:08:35 +01001423 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
1424 p->npages = alias->memory_size >> PAGE_SHIFT;
1425 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
1426
1427 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001428 if (kvm->arch.aliases[n - 1].npages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01001429 break;
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001430 kvm->arch.naliases = n;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001431
1432 kvm_mmu_zap_all(kvm);
1433
Izik Eidus72dc67a2008-02-10 18:04:15 +02001434 up_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001435
1436 return 0;
1437
1438out:
1439 return r;
1440}
1441
1442static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1443{
1444 int r;
1445
1446 r = 0;
1447 switch (chip->chip_id) {
1448 case KVM_IRQCHIP_PIC_MASTER:
1449 memcpy(&chip->chip.pic,
1450 &pic_irqchip(kvm)->pics[0],
1451 sizeof(struct kvm_pic_state));
1452 break;
1453 case KVM_IRQCHIP_PIC_SLAVE:
1454 memcpy(&chip->chip.pic,
1455 &pic_irqchip(kvm)->pics[1],
1456 sizeof(struct kvm_pic_state));
1457 break;
1458 case KVM_IRQCHIP_IOAPIC:
1459 memcpy(&chip->chip.ioapic,
1460 ioapic_irqchip(kvm),
1461 sizeof(struct kvm_ioapic_state));
1462 break;
1463 default:
1464 r = -EINVAL;
1465 break;
1466 }
1467 return r;
1468}
1469
1470static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1471{
1472 int r;
1473
1474 r = 0;
1475 switch (chip->chip_id) {
1476 case KVM_IRQCHIP_PIC_MASTER:
1477 memcpy(&pic_irqchip(kvm)->pics[0],
1478 &chip->chip.pic,
1479 sizeof(struct kvm_pic_state));
1480 break;
1481 case KVM_IRQCHIP_PIC_SLAVE:
1482 memcpy(&pic_irqchip(kvm)->pics[1],
1483 &chip->chip.pic,
1484 sizeof(struct kvm_pic_state));
1485 break;
1486 case KVM_IRQCHIP_IOAPIC:
1487 memcpy(ioapic_irqchip(kvm),
1488 &chip->chip.ioapic,
1489 sizeof(struct kvm_ioapic_state));
1490 break;
1491 default:
1492 r = -EINVAL;
1493 break;
1494 }
1495 kvm_pic_update_irq(pic_irqchip(kvm));
1496 return r;
1497}
1498
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001499/*
1500 * Get (and clear) the dirty memory log for a memory slot.
1501 */
1502int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1503 struct kvm_dirty_log *log)
1504{
1505 int r;
1506 int n;
1507 struct kvm_memory_slot *memslot;
1508 int is_dirty = 0;
1509
Izik Eidus72dc67a2008-02-10 18:04:15 +02001510 down_write(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001511
1512 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1513 if (r)
1514 goto out;
1515
1516 /* If nothing is dirty, don't bother messing with page tables. */
1517 if (is_dirty) {
1518 kvm_mmu_slot_remove_write_access(kvm, log->slot);
1519 kvm_flush_remote_tlbs(kvm);
1520 memslot = &kvm->memslots[log->slot];
1521 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1522 memset(memslot->dirty_bitmap, 0, n);
1523 }
1524 r = 0;
1525out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02001526 up_write(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001527 return r;
1528}
1529
Carsten Otte1fe779f2007-10-29 16:08:35 +01001530long kvm_arch_vm_ioctl(struct file *filp,
1531 unsigned int ioctl, unsigned long arg)
1532{
1533 struct kvm *kvm = filp->private_data;
1534 void __user *argp = (void __user *)arg;
1535 int r = -EINVAL;
1536
1537 switch (ioctl) {
1538 case KVM_SET_TSS_ADDR:
1539 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
1540 if (r < 0)
1541 goto out;
1542 break;
1543 case KVM_SET_MEMORY_REGION: {
1544 struct kvm_memory_region kvm_mem;
1545 struct kvm_userspace_memory_region kvm_userspace_mem;
1546
1547 r = -EFAULT;
1548 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
1549 goto out;
1550 kvm_userspace_mem.slot = kvm_mem.slot;
1551 kvm_userspace_mem.flags = kvm_mem.flags;
1552 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
1553 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
1554 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
1555 if (r)
1556 goto out;
1557 break;
1558 }
1559 case KVM_SET_NR_MMU_PAGES:
1560 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
1561 if (r)
1562 goto out;
1563 break;
1564 case KVM_GET_NR_MMU_PAGES:
1565 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
1566 break;
1567 case KVM_SET_MEMORY_ALIAS: {
1568 struct kvm_memory_alias alias;
1569
1570 r = -EFAULT;
1571 if (copy_from_user(&alias, argp, sizeof alias))
1572 goto out;
1573 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
1574 if (r)
1575 goto out;
1576 break;
1577 }
1578 case KVM_CREATE_IRQCHIP:
1579 r = -ENOMEM;
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08001580 kvm->arch.vpic = kvm_create_pic(kvm);
1581 if (kvm->arch.vpic) {
Carsten Otte1fe779f2007-10-29 16:08:35 +01001582 r = kvm_ioapic_init(kvm);
1583 if (r) {
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08001584 kfree(kvm->arch.vpic);
1585 kvm->arch.vpic = NULL;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001586 goto out;
1587 }
1588 } else
1589 goto out;
1590 break;
1591 case KVM_IRQ_LINE: {
1592 struct kvm_irq_level irq_event;
1593
1594 r = -EFAULT;
1595 if (copy_from_user(&irq_event, argp, sizeof irq_event))
1596 goto out;
1597 if (irqchip_in_kernel(kvm)) {
1598 mutex_lock(&kvm->lock);
1599 if (irq_event.irq < 16)
1600 kvm_pic_set_irq(pic_irqchip(kvm),
1601 irq_event.irq,
1602 irq_event.level);
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08001603 kvm_ioapic_set_irq(kvm->arch.vioapic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01001604 irq_event.irq,
1605 irq_event.level);
1606 mutex_unlock(&kvm->lock);
1607 r = 0;
1608 }
1609 break;
1610 }
1611 case KVM_GET_IRQCHIP: {
1612 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1613 struct kvm_irqchip chip;
1614
1615 r = -EFAULT;
1616 if (copy_from_user(&chip, argp, sizeof chip))
1617 goto out;
1618 r = -ENXIO;
1619 if (!irqchip_in_kernel(kvm))
1620 goto out;
1621 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
1622 if (r)
1623 goto out;
1624 r = -EFAULT;
1625 if (copy_to_user(argp, &chip, sizeof chip))
1626 goto out;
1627 r = 0;
1628 break;
1629 }
1630 case KVM_SET_IRQCHIP: {
1631 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1632 struct kvm_irqchip chip;
1633
1634 r = -EFAULT;
1635 if (copy_from_user(&chip, argp, sizeof chip))
1636 goto out;
1637 r = -ENXIO;
1638 if (!irqchip_in_kernel(kvm))
1639 goto out;
1640 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
1641 if (r)
1642 goto out;
1643 r = 0;
1644 break;
1645 }
1646 default:
1647 ;
1648 }
1649out:
1650 return r;
1651}
1652
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08001653static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02001654{
1655 u32 dummy[2];
1656 unsigned i, j;
1657
1658 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
1659 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
1660 continue;
1661 if (j < i)
1662 msrs_to_save[j] = msrs_to_save[i];
1663 j++;
1664 }
1665 num_msrs_to_save = j;
1666}
1667
Carsten Ottebbd9b642007-10-30 18:44:21 +01001668/*
1669 * Only apic need an MMIO device hook, so shortcut now..
1670 */
1671static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
1672 gpa_t addr)
1673{
1674 struct kvm_io_device *dev;
1675
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001676 if (vcpu->arch.apic) {
1677 dev = &vcpu->arch.apic->dev;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001678 if (dev->in_range(dev, addr))
1679 return dev;
1680 }
1681 return NULL;
1682}
1683
1684
1685static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1686 gpa_t addr)
1687{
1688 struct kvm_io_device *dev;
1689
1690 dev = vcpu_find_pervcpu_dev(vcpu, addr);
1691 if (dev == NULL)
1692 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1693 return dev;
1694}
1695
1696int emulator_read_std(unsigned long addr,
1697 void *val,
1698 unsigned int bytes,
1699 struct kvm_vcpu *vcpu)
1700{
1701 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001702 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001703
Izik Eidus72dc67a2008-02-10 18:04:15 +02001704 down_read(&vcpu->kvm->slots_lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001705 while (bytes) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001706 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001707 unsigned offset = addr & (PAGE_SIZE-1);
1708 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
1709 int ret;
1710
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001711 if (gpa == UNMAPPED_GVA) {
1712 r = X86EMUL_PROPAGATE_FAULT;
1713 goto out;
1714 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01001715 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001716 if (ret < 0) {
1717 r = X86EMUL_UNHANDLEABLE;
1718 goto out;
1719 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01001720
1721 bytes -= tocopy;
1722 data += tocopy;
1723 addr += tocopy;
1724 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001725out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02001726 up_read(&vcpu->kvm->slots_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001727 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001728}
1729EXPORT_SYMBOL_GPL(emulator_read_std);
1730
Carsten Ottebbd9b642007-10-30 18:44:21 +01001731static int emulator_read_emulated(unsigned long addr,
1732 void *val,
1733 unsigned int bytes,
1734 struct kvm_vcpu *vcpu)
1735{
1736 struct kvm_io_device *mmio_dev;
1737 gpa_t gpa;
1738
1739 if (vcpu->mmio_read_completed) {
1740 memcpy(val, vcpu->mmio_data, bytes);
1741 vcpu->mmio_read_completed = 0;
1742 return X86EMUL_CONTINUE;
1743 }
1744
Izik Eidus72dc67a2008-02-10 18:04:15 +02001745 down_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001746 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001747 up_read(&vcpu->kvm->slots_lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001748
1749 /* For APIC access vmexit */
1750 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1751 goto mmio;
1752
1753 if (emulator_read_std(addr, val, bytes, vcpu)
1754 == X86EMUL_CONTINUE)
1755 return X86EMUL_CONTINUE;
1756 if (gpa == UNMAPPED_GVA)
1757 return X86EMUL_PROPAGATE_FAULT;
1758
1759mmio:
1760 /*
1761 * Is this MMIO handled locally?
1762 */
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001763 mutex_lock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001764 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1765 if (mmio_dev) {
1766 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001767 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001768 return X86EMUL_CONTINUE;
1769 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001770 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001771
1772 vcpu->mmio_needed = 1;
1773 vcpu->mmio_phys_addr = gpa;
1774 vcpu->mmio_size = bytes;
1775 vcpu->mmio_is_write = 0;
1776
1777 return X86EMUL_UNHANDLEABLE;
1778}
1779
1780static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
1781 const void *val, int bytes)
1782{
1783 int ret;
1784
Izik Eidus72dc67a2008-02-10 18:04:15 +02001785 down_read(&vcpu->kvm->slots_lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001786 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001787 if (ret < 0) {
Izik Eidus72dc67a2008-02-10 18:04:15 +02001788 up_read(&vcpu->kvm->slots_lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001789 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001790 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01001791 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001792 up_read(&vcpu->kvm->slots_lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001793 return 1;
1794}
1795
1796static int emulator_write_emulated_onepage(unsigned long addr,
1797 const void *val,
1798 unsigned int bytes,
1799 struct kvm_vcpu *vcpu)
1800{
1801 struct kvm_io_device *mmio_dev;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001802 gpa_t gpa;
1803
Izik Eidus72dc67a2008-02-10 18:04:15 +02001804 down_read(&vcpu->kvm->slots_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001805 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001806 up_read(&vcpu->kvm->slots_lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001807
1808 if (gpa == UNMAPPED_GVA) {
Avi Kivityc3c91fe2007-11-25 14:04:58 +02001809 kvm_inject_page_fault(vcpu, addr, 2);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001810 return X86EMUL_PROPAGATE_FAULT;
1811 }
1812
1813 /* For APIC access vmexit */
1814 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1815 goto mmio;
1816
1817 if (emulator_write_phys(vcpu, gpa, val, bytes))
1818 return X86EMUL_CONTINUE;
1819
1820mmio:
1821 /*
1822 * Is this MMIO handled locally?
1823 */
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001824 mutex_lock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001825 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1826 if (mmio_dev) {
1827 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001828 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001829 return X86EMUL_CONTINUE;
1830 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001831 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001832
1833 vcpu->mmio_needed = 1;
1834 vcpu->mmio_phys_addr = gpa;
1835 vcpu->mmio_size = bytes;
1836 vcpu->mmio_is_write = 1;
1837 memcpy(vcpu->mmio_data, val, bytes);
1838
1839 return X86EMUL_CONTINUE;
1840}
1841
1842int emulator_write_emulated(unsigned long addr,
1843 const void *val,
1844 unsigned int bytes,
1845 struct kvm_vcpu *vcpu)
1846{
1847 /* Crossing a page boundary? */
1848 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1849 int rc, now;
1850
1851 now = -addr & ~PAGE_MASK;
1852 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
1853 if (rc != X86EMUL_CONTINUE)
1854 return rc;
1855 addr += now;
1856 val += now;
1857 bytes -= now;
1858 }
1859 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
1860}
1861EXPORT_SYMBOL_GPL(emulator_write_emulated);
1862
1863static int emulator_cmpxchg_emulated(unsigned long addr,
1864 const void *old,
1865 const void *new,
1866 unsigned int bytes,
1867 struct kvm_vcpu *vcpu)
1868{
1869 static int reported;
1870
1871 if (!reported) {
1872 reported = 1;
1873 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1874 }
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001875#ifndef CONFIG_X86_64
1876 /* guests cmpxchg8b have to be emulated atomically */
1877 if (bytes == 8) {
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001878 gpa_t gpa;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001879 struct page *page;
Andrew Mortonc0b49b02008-02-04 22:27:18 -08001880 char *kaddr;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001881 u64 val;
1882
Izik Eidus72dc67a2008-02-10 18:04:15 +02001883 down_read(&vcpu->kvm->slots_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001884 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
1885
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001886 if (gpa == UNMAPPED_GVA ||
1887 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1888 goto emul_write;
1889
1890 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
1891 goto emul_write;
1892
1893 val = *(u64 *)new;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001894
1895 down_read(&current->mm->mmap_sem);
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001896 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001897 up_read(&current->mm->mmap_sem);
1898
Andrew Mortonc0b49b02008-02-04 22:27:18 -08001899 kaddr = kmap_atomic(page, KM_USER0);
1900 set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
1901 kunmap_atomic(kaddr, KM_USER0);
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001902 kvm_release_page_dirty(page);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001903 emul_write:
Izik Eidus72dc67a2008-02-10 18:04:15 +02001904 up_read(&vcpu->kvm->slots_lock);
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001905 }
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05001906#endif
1907
Carsten Ottebbd9b642007-10-30 18:44:21 +01001908 return emulator_write_emulated(addr, new, bytes, vcpu);
1909}
1910
1911static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1912{
1913 return kvm_x86_ops->get_segment_base(vcpu, seg);
1914}
1915
1916int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1917{
1918 return X86EMUL_CONTINUE;
1919}
1920
1921int emulate_clts(struct kvm_vcpu *vcpu)
1922{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001923 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001924 return X86EMUL_CONTINUE;
1925}
1926
1927int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
1928{
1929 struct kvm_vcpu *vcpu = ctxt->vcpu;
1930
1931 switch (dr) {
1932 case 0 ... 3:
1933 *dest = kvm_x86_ops->get_dr(vcpu, dr);
1934 return X86EMUL_CONTINUE;
1935 default:
1936 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
1937 return X86EMUL_UNHANDLEABLE;
1938 }
1939}
1940
1941int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1942{
1943 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1944 int exception;
1945
1946 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1947 if (exception) {
1948 /* FIXME: better handling */
1949 return X86EMUL_UNHANDLEABLE;
1950 }
1951 return X86EMUL_CONTINUE;
1952}
1953
1954void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
1955{
1956 static int reported;
1957 u8 opcodes[4];
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001958 unsigned long rip = vcpu->arch.rip;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001959 unsigned long rip_linear;
1960
1961 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
1962
1963 if (reported)
1964 return;
1965
1966 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
1967
1968 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1969 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1970 reported = 1;
1971}
1972EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
1973
Harvey Harrison14af3f32008-02-19 10:25:50 -08001974static struct x86_emulate_ops emulate_ops = {
Carsten Ottebbd9b642007-10-30 18:44:21 +01001975 .read_std = emulator_read_std,
Carsten Ottebbd9b642007-10-30 18:44:21 +01001976 .read_emulated = emulator_read_emulated,
1977 .write_emulated = emulator_write_emulated,
1978 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1979};
1980
1981int emulate_instruction(struct kvm_vcpu *vcpu,
1982 struct kvm_run *run,
1983 unsigned long cr2,
1984 u16 error_code,
Sheng Yang571008d2008-01-02 14:49:22 +08001985 int emulation_type)
Carsten Ottebbd9b642007-10-30 18:44:21 +01001986{
1987 int r;
Sheng Yang571008d2008-01-02 14:49:22 +08001988 struct decode_cache *c;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001989
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001990 vcpu->arch.mmio_fault_cr2 = cr2;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001991 kvm_x86_ops->cache_regs(vcpu);
1992
1993 vcpu->mmio_is_write = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001994 vcpu->arch.pio.string = 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001995
Sheng Yang571008d2008-01-02 14:49:22 +08001996 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Carsten Ottebbd9b642007-10-30 18:44:21 +01001997 int cs_db, cs_l;
1998 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1999
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002000 vcpu->arch.emulate_ctxt.vcpu = vcpu;
2001 vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
2002 vcpu->arch.emulate_ctxt.mode =
2003 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002004 ? X86EMUL_MODE_REAL : cs_l
2005 ? X86EMUL_MODE_PROT64 : cs_db
2006 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
2007
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002008 if (vcpu->arch.emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
2009 vcpu->arch.emulate_ctxt.cs_base = 0;
2010 vcpu->arch.emulate_ctxt.ds_base = 0;
2011 vcpu->arch.emulate_ctxt.es_base = 0;
2012 vcpu->arch.emulate_ctxt.ss_base = 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002013 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002014 vcpu->arch.emulate_ctxt.cs_base =
Carsten Ottebbd9b642007-10-30 18:44:21 +01002015 get_segment_base(vcpu, VCPU_SREG_CS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002016 vcpu->arch.emulate_ctxt.ds_base =
Carsten Ottebbd9b642007-10-30 18:44:21 +01002017 get_segment_base(vcpu, VCPU_SREG_DS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002018 vcpu->arch.emulate_ctxt.es_base =
Carsten Ottebbd9b642007-10-30 18:44:21 +01002019 get_segment_base(vcpu, VCPU_SREG_ES);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002020 vcpu->arch.emulate_ctxt.ss_base =
Carsten Ottebbd9b642007-10-30 18:44:21 +01002021 get_segment_base(vcpu, VCPU_SREG_SS);
2022 }
2023
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002024 vcpu->arch.emulate_ctxt.gs_base =
Carsten Ottebbd9b642007-10-30 18:44:21 +01002025 get_segment_base(vcpu, VCPU_SREG_GS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002026 vcpu->arch.emulate_ctxt.fs_base =
Carsten Ottebbd9b642007-10-30 18:44:21 +01002027 get_segment_base(vcpu, VCPU_SREG_FS);
2028
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002029 r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Sheng Yang571008d2008-01-02 14:49:22 +08002030
2031 /* Reject the instructions other than VMCALL/VMMCALL when
2032 * try to emulate invalid opcode */
2033 c = &vcpu->arch.emulate_ctxt.decode;
2034 if ((emulation_type & EMULTYPE_TRAP_UD) &&
2035 (!(c->twobyte && c->b == 0x01 &&
2036 (c->modrm_reg == 0 || c->modrm_reg == 3) &&
2037 c->modrm_mod == 3 && c->modrm_rm == 1)))
2038 return EMULATE_FAIL;
2039
Avi Kivityf2b57562007-11-18 15:17:51 +02002040 ++vcpu->stat.insn_emulation;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002041 if (r) {
Avi Kivityf2b57562007-11-18 15:17:51 +02002042 ++vcpu->stat.insn_emulation_fail;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002043 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
2044 return EMULATE_DONE;
2045 return EMULATE_FAIL;
2046 }
2047 }
2048
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002049 r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002050
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002051 if (vcpu->arch.pio.string)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002052 return EMULATE_DO_MMIO;
2053
2054 if ((r || vcpu->mmio_is_write) && run) {
2055 run->exit_reason = KVM_EXIT_MMIO;
2056 run->mmio.phys_addr = vcpu->mmio_phys_addr;
2057 memcpy(run->mmio.data, vcpu->mmio_data, 8);
2058 run->mmio.len = vcpu->mmio_size;
2059 run->mmio.is_write = vcpu->mmio_is_write;
2060 }
2061
2062 if (r) {
2063 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
2064 return EMULATE_DONE;
2065 if (!vcpu->mmio_needed) {
2066 kvm_report_emulation_failure(vcpu, "mmio");
2067 return EMULATE_FAIL;
2068 }
2069 return EMULATE_DO_MMIO;
2070 }
2071
2072 kvm_x86_ops->decache_regs(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002073 kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002074
2075 if (vcpu->mmio_is_write) {
2076 vcpu->mmio_needed = 0;
2077 return EMULATE_DO_MMIO;
2078 }
2079
2080 return EMULATE_DONE;
2081}
2082EXPORT_SYMBOL_GPL(emulate_instruction);
2083
Carsten Ottede7d7892007-10-30 18:44:25 +01002084static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
2085{
2086 int i;
2087
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002088 for (i = 0; i < ARRAY_SIZE(vcpu->arch.pio.guest_pages); ++i)
2089 if (vcpu->arch.pio.guest_pages[i]) {
2090 kvm_release_page_dirty(vcpu->arch.pio.guest_pages[i]);
2091 vcpu->arch.pio.guest_pages[i] = NULL;
Carsten Ottede7d7892007-10-30 18:44:25 +01002092 }
2093}
2094
2095static int pio_copy_data(struct kvm_vcpu *vcpu)
2096{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002097 void *p = vcpu->arch.pio_data;
Carsten Ottede7d7892007-10-30 18:44:25 +01002098 void *q;
2099 unsigned bytes;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002100 int nr_pages = vcpu->arch.pio.guest_pages[1] ? 2 : 1;
Carsten Ottede7d7892007-10-30 18:44:25 +01002101
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002102 q = vmap(vcpu->arch.pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
Carsten Ottede7d7892007-10-30 18:44:25 +01002103 PAGE_KERNEL);
2104 if (!q) {
2105 free_pio_guest_pages(vcpu);
2106 return -ENOMEM;
2107 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002108 q += vcpu->arch.pio.guest_page_offset;
2109 bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
2110 if (vcpu->arch.pio.in)
Carsten Ottede7d7892007-10-30 18:44:25 +01002111 memcpy(q, p, bytes);
2112 else
2113 memcpy(p, q, bytes);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002114 q -= vcpu->arch.pio.guest_page_offset;
Carsten Ottede7d7892007-10-30 18:44:25 +01002115 vunmap(q);
2116 free_pio_guest_pages(vcpu);
2117 return 0;
2118}
2119
2120int complete_pio(struct kvm_vcpu *vcpu)
2121{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002122 struct kvm_pio_request *io = &vcpu->arch.pio;
Carsten Ottede7d7892007-10-30 18:44:25 +01002123 long delta;
2124 int r;
2125
2126 kvm_x86_ops->cache_regs(vcpu);
2127
2128 if (!io->string) {
2129 if (io->in)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002130 memcpy(&vcpu->arch.regs[VCPU_REGS_RAX], vcpu->arch.pio_data,
Carsten Ottede7d7892007-10-30 18:44:25 +01002131 io->size);
2132 } else {
2133 if (io->in) {
2134 r = pio_copy_data(vcpu);
2135 if (r) {
2136 kvm_x86_ops->cache_regs(vcpu);
2137 return r;
2138 }
2139 }
2140
2141 delta = 1;
2142 if (io->rep) {
2143 delta *= io->cur_count;
2144 /*
2145 * The size of the register should really depend on
2146 * current address size.
2147 */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002148 vcpu->arch.regs[VCPU_REGS_RCX] -= delta;
Carsten Ottede7d7892007-10-30 18:44:25 +01002149 }
2150 if (io->down)
2151 delta = -delta;
2152 delta *= io->size;
2153 if (io->in)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002154 vcpu->arch.regs[VCPU_REGS_RDI] += delta;
Carsten Ottede7d7892007-10-30 18:44:25 +01002155 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002156 vcpu->arch.regs[VCPU_REGS_RSI] += delta;
Carsten Ottede7d7892007-10-30 18:44:25 +01002157 }
2158
2159 kvm_x86_ops->decache_regs(vcpu);
2160
2161 io->count -= io->cur_count;
2162 io->cur_count = 0;
2163
2164 return 0;
2165}
2166
2167static void kernel_pio(struct kvm_io_device *pio_dev,
2168 struct kvm_vcpu *vcpu,
2169 void *pd)
2170{
2171 /* TODO: String I/O for in kernel device */
2172
2173 mutex_lock(&vcpu->kvm->lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002174 if (vcpu->arch.pio.in)
2175 kvm_iodevice_read(pio_dev, vcpu->arch.pio.port,
2176 vcpu->arch.pio.size,
Carsten Ottede7d7892007-10-30 18:44:25 +01002177 pd);
2178 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002179 kvm_iodevice_write(pio_dev, vcpu->arch.pio.port,
2180 vcpu->arch.pio.size,
Carsten Ottede7d7892007-10-30 18:44:25 +01002181 pd);
2182 mutex_unlock(&vcpu->kvm->lock);
2183}
2184
2185static void pio_string_write(struct kvm_io_device *pio_dev,
2186 struct kvm_vcpu *vcpu)
2187{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002188 struct kvm_pio_request *io = &vcpu->arch.pio;
2189 void *pd = vcpu->arch.pio_data;
Carsten Ottede7d7892007-10-30 18:44:25 +01002190 int i;
2191
2192 mutex_lock(&vcpu->kvm->lock);
2193 for (i = 0; i < io->cur_count; i++) {
2194 kvm_iodevice_write(pio_dev, io->port,
2195 io->size,
2196 pd);
2197 pd += io->size;
2198 }
2199 mutex_unlock(&vcpu->kvm->lock);
2200}
2201
2202static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
2203 gpa_t addr)
2204{
2205 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
2206}
2207
2208int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2209 int size, unsigned port)
2210{
2211 struct kvm_io_device *pio_dev;
2212
2213 vcpu->run->exit_reason = KVM_EXIT_IO;
2214 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002215 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01002216 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002217 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
2218 vcpu->run->io.port = vcpu->arch.pio.port = port;
2219 vcpu->arch.pio.in = in;
2220 vcpu->arch.pio.string = 0;
2221 vcpu->arch.pio.down = 0;
2222 vcpu->arch.pio.guest_page_offset = 0;
2223 vcpu->arch.pio.rep = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01002224
2225 kvm_x86_ops->cache_regs(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002226 memcpy(vcpu->arch.pio_data, &vcpu->arch.regs[VCPU_REGS_RAX], 4);
Carsten Ottede7d7892007-10-30 18:44:25 +01002227 kvm_x86_ops->decache_regs(vcpu);
2228
2229 kvm_x86_ops->skip_emulated_instruction(vcpu);
2230
2231 pio_dev = vcpu_find_pio_dev(vcpu, port);
2232 if (pio_dev) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002233 kernel_pio(pio_dev, vcpu, vcpu->arch.pio_data);
Carsten Ottede7d7892007-10-30 18:44:25 +01002234 complete_pio(vcpu);
2235 return 1;
2236 }
2237 return 0;
2238}
2239EXPORT_SYMBOL_GPL(kvm_emulate_pio);
2240
2241int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2242 int size, unsigned long count, int down,
2243 gva_t address, int rep, unsigned port)
2244{
2245 unsigned now, in_page;
2246 int i, ret = 0;
2247 int nr_pages = 1;
2248 struct page *page;
2249 struct kvm_io_device *pio_dev;
2250
2251 vcpu->run->exit_reason = KVM_EXIT_IO;
2252 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002253 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01002254 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002255 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
2256 vcpu->run->io.port = vcpu->arch.pio.port = port;
2257 vcpu->arch.pio.in = in;
2258 vcpu->arch.pio.string = 1;
2259 vcpu->arch.pio.down = down;
2260 vcpu->arch.pio.guest_page_offset = offset_in_page(address);
2261 vcpu->arch.pio.rep = rep;
Carsten Ottede7d7892007-10-30 18:44:25 +01002262
2263 if (!count) {
2264 kvm_x86_ops->skip_emulated_instruction(vcpu);
2265 return 1;
2266 }
2267
2268 if (!down)
2269 in_page = PAGE_SIZE - offset_in_page(address);
2270 else
2271 in_page = offset_in_page(address) + size;
2272 now = min(count, (unsigned long)in_page / size);
2273 if (!now) {
2274 /*
2275 * String I/O straddles page boundary. Pin two guest pages
2276 * so that we satisfy atomicity constraints. Do just one
2277 * transaction to avoid complexity.
2278 */
2279 nr_pages = 2;
2280 now = 1;
2281 }
2282 if (down) {
2283 /*
2284 * String I/O in reverse. Yuck. Kill the guest, fix later.
2285 */
2286 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002287 kvm_inject_gp(vcpu, 0);
Carsten Ottede7d7892007-10-30 18:44:25 +01002288 return 1;
2289 }
2290 vcpu->run->io.count = now;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002291 vcpu->arch.pio.cur_count = now;
Carsten Ottede7d7892007-10-30 18:44:25 +01002292
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002293 if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
Carsten Ottede7d7892007-10-30 18:44:25 +01002294 kvm_x86_ops->skip_emulated_instruction(vcpu);
2295
2296 for (i = 0; i < nr_pages; ++i) {
Izik Eidus72dc67a2008-02-10 18:04:15 +02002297 down_read(&vcpu->kvm->slots_lock);
Carsten Ottede7d7892007-10-30 18:44:25 +01002298 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002299 vcpu->arch.pio.guest_pages[i] = page;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002300 up_read(&vcpu->kvm->slots_lock);
Carsten Ottede7d7892007-10-30 18:44:25 +01002301 if (!page) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002302 kvm_inject_gp(vcpu, 0);
Carsten Ottede7d7892007-10-30 18:44:25 +01002303 free_pio_guest_pages(vcpu);
2304 return 1;
2305 }
2306 }
2307
2308 pio_dev = vcpu_find_pio_dev(vcpu, port);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002309 if (!vcpu->arch.pio.in) {
Carsten Ottede7d7892007-10-30 18:44:25 +01002310 /* string PIO write */
2311 ret = pio_copy_data(vcpu);
2312 if (ret >= 0 && pio_dev) {
2313 pio_string_write(pio_dev, vcpu);
2314 complete_pio(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002315 if (vcpu->arch.pio.count == 0)
Carsten Ottede7d7892007-10-30 18:44:25 +01002316 ret = 1;
2317 }
2318 } else if (pio_dev)
2319 pr_unimpl(vcpu, "no string pio read support yet, "
2320 "port %x size %d count %ld\n",
2321 port, size, count);
2322
2323 return ret;
2324}
2325EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
2326
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002327int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02002328{
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002329 int r;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002330 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
2331
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002332 if (kvm_x86_ops) {
2333 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002334 r = -EEXIST;
2335 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002336 }
2337
2338 if (!ops->cpu_has_kvm_support()) {
2339 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002340 r = -EOPNOTSUPP;
2341 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002342 }
2343 if (ops->disabled_by_bios()) {
2344 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002345 r = -EOPNOTSUPP;
2346 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002347 }
2348
Avi Kivity97db56c2008-01-13 13:23:56 +02002349 r = kvm_mmu_module_init();
2350 if (r)
2351 goto out;
2352
2353 kvm_init_msr_list();
2354
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002355 kvm_x86_ops = ops;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002356 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002357 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002358
2359out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002360 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02002361}
Hollis Blanchard8776e512007-10-31 17:24:24 -05002362
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002363void kvm_arch_exit(void)
2364{
2365 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002366 kvm_mmu_module_exit();
2367}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002368
Hollis Blanchard8776e512007-10-31 17:24:24 -05002369int kvm_emulate_halt(struct kvm_vcpu *vcpu)
2370{
2371 ++vcpu->stat.halt_exits;
2372 if (irqchip_in_kernel(vcpu->kvm)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002373 vcpu->arch.mp_state = VCPU_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002374 kvm_vcpu_block(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002375 if (vcpu->arch.mp_state != VCPU_MP_STATE_RUNNABLE)
Hollis Blanchard8776e512007-10-31 17:24:24 -05002376 return -EINTR;
2377 return 1;
2378 } else {
2379 vcpu->run->exit_reason = KVM_EXIT_HLT;
2380 return 0;
2381 }
2382}
2383EXPORT_SYMBOL_GPL(kvm_emulate_halt);
2384
2385int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
2386{
2387 unsigned long nr, a0, a1, a2, a3, ret;
2388
2389 kvm_x86_ops->cache_regs(vcpu);
2390
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002391 nr = vcpu->arch.regs[VCPU_REGS_RAX];
2392 a0 = vcpu->arch.regs[VCPU_REGS_RBX];
2393 a1 = vcpu->arch.regs[VCPU_REGS_RCX];
2394 a2 = vcpu->arch.regs[VCPU_REGS_RDX];
2395 a3 = vcpu->arch.regs[VCPU_REGS_RSI];
Hollis Blanchard8776e512007-10-31 17:24:24 -05002396
2397 if (!is_long_mode(vcpu)) {
2398 nr &= 0xFFFFFFFF;
2399 a0 &= 0xFFFFFFFF;
2400 a1 &= 0xFFFFFFFF;
2401 a2 &= 0xFFFFFFFF;
2402 a3 &= 0xFFFFFFFF;
2403 }
2404
2405 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02002406 case KVM_HC_VAPIC_POLL_IRQ:
2407 ret = 0;
2408 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002409 default:
2410 ret = -KVM_ENOSYS;
2411 break;
2412 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002413 vcpu->arch.regs[VCPU_REGS_RAX] = ret;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002414 kvm_x86_ops->decache_regs(vcpu);
Amit Shahf11c3a82008-02-21 01:00:30 +05302415 ++vcpu->stat.hypercalls;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002416 return 0;
2417}
2418EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
2419
2420int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
2421{
2422 char instruction[3];
2423 int ret = 0;
2424
Hollis Blanchard8776e512007-10-31 17:24:24 -05002425
2426 /*
2427 * Blow out the MMU to ensure that no other VCPU has an active mapping
2428 * to ensure that the updated hypercall appears atomically across all
2429 * VCPUs.
2430 */
2431 kvm_mmu_zap_all(vcpu->kvm);
2432
2433 kvm_x86_ops->cache_regs(vcpu);
2434 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002435 if (emulator_write_emulated(vcpu->arch.rip, instruction, 3, vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05002436 != X86EMUL_CONTINUE)
2437 ret = -EFAULT;
2438
Hollis Blanchard8776e512007-10-31 17:24:24 -05002439 return ret;
2440}
2441
2442static u64 mk_cr_64(u64 curr_cr, u32 new_val)
2443{
2444 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
2445}
2446
2447void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2448{
2449 struct descriptor_table dt = { limit, base };
2450
2451 kvm_x86_ops->set_gdt(vcpu, &dt);
2452}
2453
2454void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2455{
2456 struct descriptor_table dt = { limit, base };
2457
2458 kvm_x86_ops->set_idt(vcpu, &dt);
2459}
2460
2461void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
2462 unsigned long *rflags)
2463{
2464 lmsw(vcpu, msw);
2465 *rflags = kvm_x86_ops->get_rflags(vcpu);
2466}
2467
2468unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
2469{
2470 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2471 switch (cr) {
2472 case 0:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002473 return vcpu->arch.cr0;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002474 case 2:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002475 return vcpu->arch.cr2;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002476 case 3:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002477 return vcpu->arch.cr3;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002478 case 4:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002479 return vcpu->arch.cr4;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01002480 case 8:
2481 return get_cr8(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002482 default:
2483 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
2484 return 0;
2485 }
2486}
2487
2488void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
2489 unsigned long *rflags)
2490{
2491 switch (cr) {
2492 case 0:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002493 set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val));
Hollis Blanchard8776e512007-10-31 17:24:24 -05002494 *rflags = kvm_x86_ops->get_rflags(vcpu);
2495 break;
2496 case 2:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002497 vcpu->arch.cr2 = val;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002498 break;
2499 case 3:
2500 set_cr3(vcpu, val);
2501 break;
2502 case 4:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002503 set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val));
Hollis Blanchard8776e512007-10-31 17:24:24 -05002504 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01002505 case 8:
2506 set_cr8(vcpu, val & 0xfUL);
2507 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002508 default:
2509 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
2510 }
2511}
2512
Dan Kenigsberg07716712007-11-21 17:10:04 +02002513static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
2514{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002515 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
2516 int j, nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02002517
2518 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
2519 /* when no next entry is found, the current entry[i] is reselected */
2520 for (j = i + 1; j == i; j = (j + 1) % nent) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002521 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
Dan Kenigsberg07716712007-11-21 17:10:04 +02002522 if (ej->function == e->function) {
2523 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
2524 return j;
2525 }
2526 }
2527 return 0; /* silence gcc, even though control never reaches here */
2528}
2529
2530/* find an entry with matching function, matching index (if needed), and that
2531 * should be read next (if it's stateful) */
2532static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
2533 u32 function, u32 index)
2534{
2535 if (e->function != function)
2536 return 0;
2537 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
2538 return 0;
2539 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
2540 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
2541 return 0;
2542 return 1;
2543}
2544
Hollis Blanchard8776e512007-10-31 17:24:24 -05002545void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
2546{
2547 int i;
Dan Kenigsberg07716712007-11-21 17:10:04 +02002548 u32 function, index;
2549 struct kvm_cpuid_entry2 *e, *best;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002550
2551 kvm_x86_ops->cache_regs(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002552 function = vcpu->arch.regs[VCPU_REGS_RAX];
2553 index = vcpu->arch.regs[VCPU_REGS_RCX];
2554 vcpu->arch.regs[VCPU_REGS_RAX] = 0;
2555 vcpu->arch.regs[VCPU_REGS_RBX] = 0;
2556 vcpu->arch.regs[VCPU_REGS_RCX] = 0;
2557 vcpu->arch.regs[VCPU_REGS_RDX] = 0;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002558 best = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002559 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
2560 e = &vcpu->arch.cpuid_entries[i];
Dan Kenigsberg07716712007-11-21 17:10:04 +02002561 if (is_matching_cpuid_entry(e, function, index)) {
2562 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
2563 move_to_next_stateful_cpuid_entry(vcpu, i);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002564 best = e;
2565 break;
2566 }
2567 /*
2568 * Both basic or both extended?
2569 */
2570 if (((e->function ^ function) & 0x80000000) == 0)
2571 if (!best || e->function > best->function)
2572 best = e;
2573 }
2574 if (best) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002575 vcpu->arch.regs[VCPU_REGS_RAX] = best->eax;
2576 vcpu->arch.regs[VCPU_REGS_RBX] = best->ebx;
2577 vcpu->arch.regs[VCPU_REGS_RCX] = best->ecx;
2578 vcpu->arch.regs[VCPU_REGS_RDX] = best->edx;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002579 }
2580 kvm_x86_ops->decache_regs(vcpu);
2581 kvm_x86_ops->skip_emulated_instruction(vcpu);
2582}
2583EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
Hollis Blanchardd0752062007-10-31 17:24:25 -05002584
2585/*
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002586 * Check if userspace requested an interrupt window, and that the
2587 * interrupt window is open.
2588 *
2589 * No need to exit to userspace if we already have an interrupt queued.
2590 */
2591static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2592 struct kvm_run *kvm_run)
2593{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002594 return (!vcpu->arch.irq_summary &&
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002595 kvm_run->request_interrupt_window &&
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002596 vcpu->arch.interrupt_window_open &&
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002597 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
2598}
2599
2600static void post_kvm_run_save(struct kvm_vcpu *vcpu,
2601 struct kvm_run *kvm_run)
2602{
2603 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
2604 kvm_run->cr8 = get_cr8(vcpu);
2605 kvm_run->apic_base = kvm_get_apic_base(vcpu);
2606 if (irqchip_in_kernel(vcpu->kvm))
2607 kvm_run->ready_for_interrupt_injection = 1;
2608 else
2609 kvm_run->ready_for_interrupt_injection =
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002610 (vcpu->arch.interrupt_window_open &&
2611 vcpu->arch.irq_summary == 0);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002612}
2613
Avi Kivityb93463a2007-10-25 16:52:32 +02002614static void vapic_enter(struct kvm_vcpu *vcpu)
2615{
2616 struct kvm_lapic *apic = vcpu->arch.apic;
2617 struct page *page;
2618
2619 if (!apic || !apic->vapic_addr)
2620 return;
2621
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002622 down_read(&current->mm->mmap_sem);
Avi Kivityb93463a2007-10-25 16:52:32 +02002623 page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002624 up_read(&current->mm->mmap_sem);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002625
2626 vcpu->arch.apic->vapic_page = page;
Avi Kivityb93463a2007-10-25 16:52:32 +02002627}
2628
2629static void vapic_exit(struct kvm_vcpu *vcpu)
2630{
2631 struct kvm_lapic *apic = vcpu->arch.apic;
2632
2633 if (!apic || !apic->vapic_addr)
2634 return;
2635
2636 kvm_release_page_dirty(apic->vapic_page);
2637 mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
2638}
2639
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002640static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2641{
2642 int r;
2643
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002644 if (unlikely(vcpu->arch.mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002645 pr_debug("vcpu %d received sipi with vector # %x\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002646 vcpu->vcpu_id, vcpu->arch.sipi_vector);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002647 kvm_lapic_reset(vcpu);
2648 r = kvm_x86_ops->vcpu_reset(vcpu);
2649 if (r)
2650 return r;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002651 vcpu->arch.mp_state = VCPU_MP_STATE_RUNNABLE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002652 }
2653
Avi Kivityb93463a2007-10-25 16:52:32 +02002654 vapic_enter(vcpu);
2655
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002656preempted:
2657 if (vcpu->guest_debug.enabled)
2658 kvm_x86_ops->guest_debug_pre(vcpu);
2659
2660again:
2661 r = kvm_mmu_reload(vcpu);
2662 if (unlikely(r))
2663 goto out;
2664
Avi Kivity2f52d582008-01-16 12:49:30 +02002665 if (vcpu->requests) {
2666 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
2667 __kvm_migrate_apic_timer(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02002668 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
2669 &vcpu->requests)) {
2670 kvm_run->exit_reason = KVM_EXIT_TPR_ACCESS;
2671 r = 0;
2672 goto out;
2673 }
Avi Kivity2f52d582008-01-16 12:49:30 +02002674 }
Avi Kivityb93463a2007-10-25 16:52:32 +02002675
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002676 kvm_inject_pending_timer_irqs(vcpu);
2677
2678 preempt_disable();
2679
2680 kvm_x86_ops->prepare_guest_switch(vcpu);
2681 kvm_load_guest_fpu(vcpu);
2682
2683 local_irq_disable();
2684
Avi Kivity6c1428012008-01-15 18:27:32 +02002685 if (need_resched()) {
2686 local_irq_enable();
2687 preempt_enable();
2688 r = 1;
2689 goto out;
2690 }
2691
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002692 if (signal_pending(current)) {
2693 local_irq_enable();
2694 preempt_enable();
2695 r = -EINTR;
2696 kvm_run->exit_reason = KVM_EXIT_INTR;
2697 ++vcpu->stat.signal_exits;
2698 goto out;
2699 }
2700
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002701 if (vcpu->arch.exception.pending)
Avi Kivity298101d2007-11-25 13:41:11 +02002702 __queue_exception(vcpu);
2703 else if (irqchip_in_kernel(vcpu->kvm))
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002704 kvm_x86_ops->inject_pending_irq(vcpu);
Avi Kivityeb9774f2007-11-25 17:45:31 +02002705 else
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002706 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2707
Avi Kivityb93463a2007-10-25 16:52:32 +02002708 kvm_lapic_sync_to_vapic(vcpu);
2709
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002710 vcpu->guest_mode = 1;
2711 kvm_guest_enter();
2712
2713 if (vcpu->requests)
2714 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
2715 kvm_x86_ops->tlb_flush(vcpu);
2716
2717 kvm_x86_ops->run(vcpu, kvm_run);
2718
2719 vcpu->guest_mode = 0;
2720 local_irq_enable();
2721
2722 ++vcpu->stat.exits;
2723
2724 /*
2725 * We must have an instruction between local_irq_enable() and
2726 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2727 * the interrupt shadow. The stat.exits increment will do nicely.
2728 * But we need to prevent reordering, hence this barrier():
2729 */
2730 barrier();
2731
2732 kvm_guest_exit();
2733
2734 preempt_enable();
2735
2736 /*
2737 * Profile KVM exit RIPs:
2738 */
2739 if (unlikely(prof_on == KVM_PROFILING)) {
2740 kvm_x86_ops->cache_regs(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002741 profile_hit(KVM_PROFILING, (void *)vcpu->arch.rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002742 }
2743
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002744 if (vcpu->arch.exception.pending && kvm_x86_ops->exception_injected(vcpu))
2745 vcpu->arch.exception.pending = false;
Avi Kivity298101d2007-11-25 13:41:11 +02002746
Avi Kivityb93463a2007-10-25 16:52:32 +02002747 kvm_lapic_sync_from_vapic(vcpu);
2748
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002749 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2750
2751 if (r > 0) {
2752 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2753 r = -EINTR;
2754 kvm_run->exit_reason = KVM_EXIT_INTR;
2755 ++vcpu->stat.request_irq_exits;
2756 goto out;
2757 }
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002758 if (!need_resched())
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002759 goto again;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002760 }
2761
2762out:
2763 if (r > 0) {
2764 kvm_resched(vcpu);
2765 goto preempted;
2766 }
2767
2768 post_kvm_run_save(vcpu, kvm_run);
2769
Avi Kivityb93463a2007-10-25 16:52:32 +02002770 vapic_exit(vcpu);
2771
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002772 return r;
2773}
2774
2775int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2776{
2777 int r;
2778 sigset_t sigsaved;
2779
2780 vcpu_load(vcpu);
2781
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002782 if (unlikely(vcpu->arch.mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002783 kvm_vcpu_block(vcpu);
2784 vcpu_put(vcpu);
2785 return -EAGAIN;
2786 }
2787
2788 if (vcpu->sigset_active)
2789 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2790
2791 /* re-sync apic's tpr */
2792 if (!irqchip_in_kernel(vcpu->kvm))
2793 set_cr8(vcpu, kvm_run->cr8);
2794
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002795 if (vcpu->arch.pio.cur_count) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002796 r = complete_pio(vcpu);
2797 if (r)
2798 goto out;
2799 }
2800#if CONFIG_HAS_IOMEM
2801 if (vcpu->mmio_needed) {
2802 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
2803 vcpu->mmio_read_completed = 1;
2804 vcpu->mmio_needed = 0;
2805 r = emulate_instruction(vcpu, kvm_run,
Sheng Yang571008d2008-01-02 14:49:22 +08002806 vcpu->arch.mmio_fault_cr2, 0,
2807 EMULTYPE_NO_DECODE);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002808 if (r == EMULATE_DO_MMIO) {
2809 /*
2810 * Read-modify-write. Back to userspace.
2811 */
2812 r = 0;
2813 goto out;
2814 }
2815 }
2816#endif
2817 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
2818 kvm_x86_ops->cache_regs(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002819 vcpu->arch.regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002820 kvm_x86_ops->decache_regs(vcpu);
2821 }
2822
2823 r = __vcpu_run(vcpu, kvm_run);
2824
2825out:
2826 if (vcpu->sigset_active)
2827 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2828
2829 vcpu_put(vcpu);
2830 return r;
2831}
2832
2833int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2834{
2835 vcpu_load(vcpu);
2836
2837 kvm_x86_ops->cache_regs(vcpu);
2838
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002839 regs->rax = vcpu->arch.regs[VCPU_REGS_RAX];
2840 regs->rbx = vcpu->arch.regs[VCPU_REGS_RBX];
2841 regs->rcx = vcpu->arch.regs[VCPU_REGS_RCX];
2842 regs->rdx = vcpu->arch.regs[VCPU_REGS_RDX];
2843 regs->rsi = vcpu->arch.regs[VCPU_REGS_RSI];
2844 regs->rdi = vcpu->arch.regs[VCPU_REGS_RDI];
2845 regs->rsp = vcpu->arch.regs[VCPU_REGS_RSP];
2846 regs->rbp = vcpu->arch.regs[VCPU_REGS_RBP];
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002847#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002848 regs->r8 = vcpu->arch.regs[VCPU_REGS_R8];
2849 regs->r9 = vcpu->arch.regs[VCPU_REGS_R9];
2850 regs->r10 = vcpu->arch.regs[VCPU_REGS_R10];
2851 regs->r11 = vcpu->arch.regs[VCPU_REGS_R11];
2852 regs->r12 = vcpu->arch.regs[VCPU_REGS_R12];
2853 regs->r13 = vcpu->arch.regs[VCPU_REGS_R13];
2854 regs->r14 = vcpu->arch.regs[VCPU_REGS_R14];
2855 regs->r15 = vcpu->arch.regs[VCPU_REGS_R15];
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002856#endif
2857
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002858 regs->rip = vcpu->arch.rip;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002859 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
2860
2861 /*
2862 * Don't leak debug flags in case they were set for guest debugging
2863 */
2864 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2865 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2866
2867 vcpu_put(vcpu);
2868
2869 return 0;
2870}
2871
2872int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2873{
2874 vcpu_load(vcpu);
2875
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002876 vcpu->arch.regs[VCPU_REGS_RAX] = regs->rax;
2877 vcpu->arch.regs[VCPU_REGS_RBX] = regs->rbx;
2878 vcpu->arch.regs[VCPU_REGS_RCX] = regs->rcx;
2879 vcpu->arch.regs[VCPU_REGS_RDX] = regs->rdx;
2880 vcpu->arch.regs[VCPU_REGS_RSI] = regs->rsi;
2881 vcpu->arch.regs[VCPU_REGS_RDI] = regs->rdi;
2882 vcpu->arch.regs[VCPU_REGS_RSP] = regs->rsp;
2883 vcpu->arch.regs[VCPU_REGS_RBP] = regs->rbp;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002884#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002885 vcpu->arch.regs[VCPU_REGS_R8] = regs->r8;
2886 vcpu->arch.regs[VCPU_REGS_R9] = regs->r9;
2887 vcpu->arch.regs[VCPU_REGS_R10] = regs->r10;
2888 vcpu->arch.regs[VCPU_REGS_R11] = regs->r11;
2889 vcpu->arch.regs[VCPU_REGS_R12] = regs->r12;
2890 vcpu->arch.regs[VCPU_REGS_R13] = regs->r13;
2891 vcpu->arch.regs[VCPU_REGS_R14] = regs->r14;
2892 vcpu->arch.regs[VCPU_REGS_R15] = regs->r15;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002893#endif
2894
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002895 vcpu->arch.rip = regs->rip;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002896 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
2897
2898 kvm_x86_ops->decache_regs(vcpu);
2899
2900 vcpu_put(vcpu);
2901
2902 return 0;
2903}
2904
2905static void get_segment(struct kvm_vcpu *vcpu,
2906 struct kvm_segment *var, int seg)
2907{
Harvey Harrison14af3f32008-02-19 10:25:50 -08002908 kvm_x86_ops->get_segment(vcpu, var, seg);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002909}
2910
2911void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2912{
2913 struct kvm_segment cs;
2914
2915 get_segment(vcpu, &cs, VCPU_SREG_CS);
2916 *db = cs.db;
2917 *l = cs.l;
2918}
2919EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2920
2921int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2922 struct kvm_sregs *sregs)
2923{
2924 struct descriptor_table dt;
2925 int pending_vec;
2926
2927 vcpu_load(vcpu);
2928
2929 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2930 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2931 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2932 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2933 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2934 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2935
2936 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2937 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2938
2939 kvm_x86_ops->get_idt(vcpu, &dt);
2940 sregs->idt.limit = dt.limit;
2941 sregs->idt.base = dt.base;
2942 kvm_x86_ops->get_gdt(vcpu, &dt);
2943 sregs->gdt.limit = dt.limit;
2944 sregs->gdt.base = dt.base;
2945
2946 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002947 sregs->cr0 = vcpu->arch.cr0;
2948 sregs->cr2 = vcpu->arch.cr2;
2949 sregs->cr3 = vcpu->arch.cr3;
2950 sregs->cr4 = vcpu->arch.cr4;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002951 sregs->cr8 = get_cr8(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002952 sregs->efer = vcpu->arch.shadow_efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002953 sregs->apic_base = kvm_get_apic_base(vcpu);
2954
2955 if (irqchip_in_kernel(vcpu->kvm)) {
2956 memset(sregs->interrupt_bitmap, 0,
2957 sizeof sregs->interrupt_bitmap);
2958 pending_vec = kvm_x86_ops->get_irq(vcpu);
2959 if (pending_vec >= 0)
2960 set_bit(pending_vec,
2961 (unsigned long *)sregs->interrupt_bitmap);
2962 } else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002963 memcpy(sregs->interrupt_bitmap, vcpu->arch.irq_pending,
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002964 sizeof sregs->interrupt_bitmap);
2965
2966 vcpu_put(vcpu);
2967
2968 return 0;
2969}
2970
2971static void set_segment(struct kvm_vcpu *vcpu,
2972 struct kvm_segment *var, int seg)
2973{
Harvey Harrison14af3f32008-02-19 10:25:50 -08002974 kvm_x86_ops->set_segment(vcpu, var, seg);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002975}
2976
2977int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2978 struct kvm_sregs *sregs)
2979{
2980 int mmu_reset_needed = 0;
2981 int i, pending_vec, max_bits;
2982 struct descriptor_table dt;
2983
2984 vcpu_load(vcpu);
2985
2986 dt.limit = sregs->idt.limit;
2987 dt.base = sregs->idt.base;
2988 kvm_x86_ops->set_idt(vcpu, &dt);
2989 dt.limit = sregs->gdt.limit;
2990 dt.base = sregs->gdt.base;
2991 kvm_x86_ops->set_gdt(vcpu, &dt);
2992
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002993 vcpu->arch.cr2 = sregs->cr2;
2994 mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
2995 vcpu->arch.cr3 = sregs->cr3;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002996
2997 set_cr8(vcpu, sregs->cr8);
2998
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002999 mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003000 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003001 kvm_set_apic_base(vcpu, sregs->apic_base);
3002
3003 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
3004
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003005 mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003006 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00003007 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003008
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003009 mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003010 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
3011 if (!is_long_mode(vcpu) && is_pae(vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003012 load_pdptrs(vcpu, vcpu->arch.cr3);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003013
3014 if (mmu_reset_needed)
3015 kvm_mmu_reset_context(vcpu);
3016
3017 if (!irqchip_in_kernel(vcpu->kvm)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003018 memcpy(vcpu->arch.irq_pending, sregs->interrupt_bitmap,
3019 sizeof vcpu->arch.irq_pending);
3020 vcpu->arch.irq_summary = 0;
3021 for (i = 0; i < ARRAY_SIZE(vcpu->arch.irq_pending); ++i)
3022 if (vcpu->arch.irq_pending[i])
3023 __set_bit(i, &vcpu->arch.irq_summary);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003024 } else {
3025 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
3026 pending_vec = find_first_bit(
3027 (const unsigned long *)sregs->interrupt_bitmap,
3028 max_bits);
3029 /* Only pending external irq is handled here */
3030 if (pending_vec < max_bits) {
3031 kvm_x86_ops->set_irq(vcpu, pending_vec);
3032 pr_debug("Set back pending irq %d\n",
3033 pending_vec);
3034 }
3035 }
3036
3037 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
3038 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
3039 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
3040 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
3041 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
3042 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
3043
3044 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
3045 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
3046
3047 vcpu_put(vcpu);
3048
3049 return 0;
3050}
3051
3052int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
3053 struct kvm_debug_guest *dbg)
3054{
3055 int r;
3056
3057 vcpu_load(vcpu);
3058
3059 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
3060
3061 vcpu_put(vcpu);
3062
3063 return r;
3064}
3065
3066/*
Hollis Blanchardd0752062007-10-31 17:24:25 -05003067 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
3068 * we have asm/x86/processor.h
3069 */
3070struct fxsave {
3071 u16 cwd;
3072 u16 swd;
3073 u16 twd;
3074 u16 fop;
3075 u64 rip;
3076 u64 rdp;
3077 u32 mxcsr;
3078 u32 mxcsr_mask;
3079 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
3080#ifdef CONFIG_X86_64
3081 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
3082#else
3083 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
3084#endif
3085};
3086
Zhang Xiantao8b006792007-11-16 13:05:55 +08003087/*
3088 * Translate a guest virtual address to a guest physical address.
3089 */
3090int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
3091 struct kvm_translation *tr)
3092{
3093 unsigned long vaddr = tr->linear_address;
3094 gpa_t gpa;
3095
3096 vcpu_load(vcpu);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003097 down_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003098 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003099 up_read(&vcpu->kvm->slots_lock);
Zhang Xiantao8b006792007-11-16 13:05:55 +08003100 tr->physical_address = gpa;
3101 tr->valid = gpa != UNMAPPED_GVA;
3102 tr->writeable = 1;
3103 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08003104 vcpu_put(vcpu);
3105
3106 return 0;
3107}
3108
Hollis Blanchardd0752062007-10-31 17:24:25 -05003109int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3110{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003111 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05003112
3113 vcpu_load(vcpu);
3114
3115 memcpy(fpu->fpr, fxsave->st_space, 128);
3116 fpu->fcw = fxsave->cwd;
3117 fpu->fsw = fxsave->swd;
3118 fpu->ftwx = fxsave->twd;
3119 fpu->last_opcode = fxsave->fop;
3120 fpu->last_ip = fxsave->rip;
3121 fpu->last_dp = fxsave->rdp;
3122 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
3123
3124 vcpu_put(vcpu);
3125
3126 return 0;
3127}
3128
3129int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3130{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003131 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05003132
3133 vcpu_load(vcpu);
3134
3135 memcpy(fxsave->st_space, fpu->fpr, 128);
3136 fxsave->cwd = fpu->fcw;
3137 fxsave->swd = fpu->fsw;
3138 fxsave->twd = fpu->ftwx;
3139 fxsave->fop = fpu->last_opcode;
3140 fxsave->rip = fpu->last_ip;
3141 fxsave->rdp = fpu->last_dp;
3142 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
3143
3144 vcpu_put(vcpu);
3145
3146 return 0;
3147}
3148
3149void fx_init(struct kvm_vcpu *vcpu)
3150{
3151 unsigned after_mxcsr_mask;
3152
3153 /* Initialize guest FPU by resetting ours and saving into guest's */
3154 preempt_disable();
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003155 fx_save(&vcpu->arch.host_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05003156 fpu_init();
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003157 fx_save(&vcpu->arch.guest_fx_image);
3158 fx_restore(&vcpu->arch.host_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05003159 preempt_enable();
3160
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003161 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05003162 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003163 vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
3164 memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
Hollis Blanchardd0752062007-10-31 17:24:25 -05003165 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
3166}
3167EXPORT_SYMBOL_GPL(fx_init);
3168
3169void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
3170{
3171 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
3172 return;
3173
3174 vcpu->guest_fpu_loaded = 1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003175 fx_save(&vcpu->arch.host_fx_image);
3176 fx_restore(&vcpu->arch.guest_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05003177}
3178EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
3179
3180void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
3181{
3182 if (!vcpu->guest_fpu_loaded)
3183 return;
3184
3185 vcpu->guest_fpu_loaded = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003186 fx_save(&vcpu->arch.guest_fx_image);
3187 fx_restore(&vcpu->arch.host_fx_image);
Avi Kivityf096ed82007-11-18 13:54:33 +02003188 ++vcpu->stat.fpu_reload;
Hollis Blanchardd0752062007-10-31 17:24:25 -05003189}
3190EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003191
3192void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
3193{
3194 kvm_x86_ops->vcpu_free(vcpu);
3195}
3196
3197struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
3198 unsigned int id)
3199{
Avi Kivity26e52152007-11-20 15:30:24 +02003200 return kvm_x86_ops->vcpu_create(kvm, id);
3201}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003202
Avi Kivity26e52152007-11-20 15:30:24 +02003203int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
3204{
3205 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003206
3207 /* We do fxsave: this must be aligned. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003208 BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003209
3210 vcpu_load(vcpu);
3211 r = kvm_arch_vcpu_reset(vcpu);
3212 if (r == 0)
3213 r = kvm_mmu_setup(vcpu);
3214 vcpu_put(vcpu);
3215 if (r < 0)
3216 goto free_vcpu;
3217
Avi Kivity26e52152007-11-20 15:30:24 +02003218 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003219free_vcpu:
3220 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivity26e52152007-11-20 15:30:24 +02003221 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003222}
3223
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06003224void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003225{
3226 vcpu_load(vcpu);
3227 kvm_mmu_unload(vcpu);
3228 vcpu_put(vcpu);
3229
3230 kvm_x86_ops->vcpu_free(vcpu);
3231}
3232
3233int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
3234{
3235 return kvm_x86_ops->vcpu_reset(vcpu);
3236}
3237
3238void kvm_arch_hardware_enable(void *garbage)
3239{
3240 kvm_x86_ops->hardware_enable(garbage);
3241}
3242
3243void kvm_arch_hardware_disable(void *garbage)
3244{
3245 kvm_x86_ops->hardware_disable(garbage);
3246}
3247
3248int kvm_arch_hardware_setup(void)
3249{
3250 return kvm_x86_ops->hardware_setup();
3251}
3252
3253void kvm_arch_hardware_unsetup(void)
3254{
3255 kvm_x86_ops->hardware_unsetup();
3256}
3257
3258void kvm_arch_check_processor_compat(void *rtn)
3259{
3260 kvm_x86_ops->check_processor_compatibility(rtn);
3261}
3262
3263int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
3264{
3265 struct page *page;
3266 struct kvm *kvm;
3267 int r;
3268
3269 BUG_ON(vcpu->kvm == NULL);
3270 kvm = vcpu->kvm;
3271
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003272 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003273 if (!irqchip_in_kernel(kvm) || vcpu->vcpu_id == 0)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003274 vcpu->arch.mp_state = VCPU_MP_STATE_RUNNABLE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003275 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003276 vcpu->arch.mp_state = VCPU_MP_STATE_UNINITIALIZED;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003277
3278 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
3279 if (!page) {
3280 r = -ENOMEM;
3281 goto fail;
3282 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003283 vcpu->arch.pio_data = page_address(page);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003284
3285 r = kvm_mmu_create(vcpu);
3286 if (r < 0)
3287 goto fail_free_pio_data;
3288
3289 if (irqchip_in_kernel(kvm)) {
3290 r = kvm_create_lapic(vcpu);
3291 if (r < 0)
3292 goto fail_mmu_destroy;
3293 }
3294
3295 return 0;
3296
3297fail_mmu_destroy:
3298 kvm_mmu_destroy(vcpu);
3299fail_free_pio_data:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003300 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003301fail:
3302 return r;
3303}
3304
3305void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
3306{
3307 kvm_free_lapic(vcpu);
3308 kvm_mmu_destroy(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003309 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003310}
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08003311
3312struct kvm *kvm_arch_create_vm(void)
3313{
3314 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
3315
3316 if (!kvm)
3317 return ERR_PTR(-ENOMEM);
3318
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003319 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08003320
3321 return kvm;
3322}
3323
3324static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
3325{
3326 vcpu_load(vcpu);
3327 kvm_mmu_unload(vcpu);
3328 vcpu_put(vcpu);
3329}
3330
3331static void kvm_free_vcpus(struct kvm *kvm)
3332{
3333 unsigned int i;
3334
3335 /*
3336 * Unpin any mmu pages first.
3337 */
3338 for (i = 0; i < KVM_MAX_VCPUS; ++i)
3339 if (kvm->vcpus[i])
3340 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
3341 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
3342 if (kvm->vcpus[i]) {
3343 kvm_arch_vcpu_free(kvm->vcpus[i]);
3344 kvm->vcpus[i] = NULL;
3345 }
3346 }
3347
3348}
3349
3350void kvm_arch_destroy_vm(struct kvm *kvm)
3351{
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08003352 kfree(kvm->arch.vpic);
3353 kfree(kvm->arch.vioapic);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08003354 kvm_free_vcpus(kvm);
3355 kvm_free_physmem(kvm);
3356 kfree(kvm);
3357}
Zhang Xiantao0de10342007-11-20 16:25:04 +08003358
3359int kvm_arch_set_memory_region(struct kvm *kvm,
3360 struct kvm_userspace_memory_region *mem,
3361 struct kvm_memory_slot old,
3362 int user_alloc)
3363{
3364 int npages = mem->memory_size >> PAGE_SHIFT;
3365 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
3366
3367 /*To keep backward compatibility with older userspace,
3368 *x86 needs to hanlde !user_alloc case.
3369 */
3370 if (!user_alloc) {
3371 if (npages && !old.rmap) {
Izik Eidus72dc67a2008-02-10 18:04:15 +02003372 down_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08003373 memslot->userspace_addr = do_mmap(NULL, 0,
3374 npages * PAGE_SIZE,
3375 PROT_READ | PROT_WRITE,
3376 MAP_SHARED | MAP_ANONYMOUS,
3377 0);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003378 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08003379
3380 if (IS_ERR((void *)memslot->userspace_addr))
3381 return PTR_ERR((void *)memslot->userspace_addr);
3382 } else {
3383 if (!old.user_alloc && old.rmap) {
3384 int ret;
3385
Izik Eidus72dc67a2008-02-10 18:04:15 +02003386 down_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08003387 ret = do_munmap(current->mm, old.userspace_addr,
3388 old.npages * PAGE_SIZE);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003389 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08003390 if (ret < 0)
3391 printk(KERN_WARNING
3392 "kvm_vm_ioctl_set_memory_region: "
3393 "failed to munmap memory\n");
3394 }
3395 }
3396 }
3397
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003398 if (!kvm->arch.n_requested_mmu_pages) {
Zhang Xiantao0de10342007-11-20 16:25:04 +08003399 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
3400 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
3401 }
3402
3403 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
3404 kvm_flush_remote_tlbs(kvm);
3405
3406 return 0;
3407}
Zhang Xiantao1d737c82007-12-14 09:35:10 +08003408
3409int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
3410{
3411 return vcpu->arch.mp_state == VCPU_MP_STATE_RUNNABLE
3412 || vcpu->arch.mp_state == VCPU_MP_STATE_SIPI_RECEIVED;
3413}
Zhang Xiantao57361992007-12-17 14:21:40 +08003414
3415static void vcpu_kick_intr(void *info)
3416{
3417#ifdef DEBUG
3418 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info;
3419 printk(KERN_DEBUG "vcpu_kick_intr %p \n", vcpu);
3420#endif
3421}
3422
3423void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
3424{
3425 int ipi_pcpu = vcpu->cpu;
3426
3427 if (waitqueue_active(&vcpu->wq)) {
3428 wake_up_interruptible(&vcpu->wq);
3429 ++vcpu->stat.halt_wakeup;
3430 }
3431 if (vcpu->guest_mode)
3432 smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0, 0);
3433}