]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/kvm/kvm_main.c
KVM: Hoist kvm_mmu_reload() out of the critical section
[mirror_ubuntu-bionic-kernel.git] / drivers / kvm / kvm_main.c
CommitLineData
6aa8b732
AK
1/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 *
9 * Authors:
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
17
18#include "kvm.h"
e495606d
AK
19#include "x86_emulate.h"
20#include "segment_descriptor.h"
6aa8b732
AK
21
22#include <linux/kvm.h>
23#include <linux/module.h>
24#include <linux/errno.h>
6aa8b732
AK
25#include <linux/percpu.h>
26#include <linux/gfp.h>
6aa8b732
AK
27#include <linux/mm.h>
28#include <linux/miscdevice.h>
29#include <linux/vmalloc.h>
6aa8b732 30#include <linux/reboot.h>
6aa8b732
AK
31#include <linux/debugfs.h>
32#include <linux/highmem.h>
33#include <linux/file.h>
59ae6c6b 34#include <linux/sysdev.h>
774c47f1 35#include <linux/cpu.h>
e8edc6e0 36#include <linux/sched.h>
d9e368d6
AK
37#include <linux/cpumask.h>
38#include <linux/smp.h>
d6d28168 39#include <linux/anon_inodes.h>
6aa8b732 40
e495606d
AK
41#include <asm/processor.h>
42#include <asm/msr.h>
43#include <asm/io.h>
44#include <asm/uaccess.h>
45#include <asm/desc.h>
6aa8b732
AK
46
47MODULE_AUTHOR("Qumranet");
48MODULE_LICENSE("GPL");
49
133de902
AK
50static DEFINE_SPINLOCK(kvm_lock);
51static LIST_HEAD(vm_list);
52
1b6c0168
AK
53static cpumask_t cpus_hardware_enabled;
54
6aa8b732 55struct kvm_arch_ops *kvm_arch_ops;
1165f5fe
AK
56
57#define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
6aa8b732
AK
58
59static struct kvm_stats_debugfs_item {
60 const char *name;
1165f5fe 61 int offset;
6aa8b732
AK
62 struct dentry *dentry;
63} debugfs_entries[] = {
1165f5fe
AK
64 { "pf_fixed", STAT_OFFSET(pf_fixed) },
65 { "pf_guest", STAT_OFFSET(pf_guest) },
66 { "tlb_flush", STAT_OFFSET(tlb_flush) },
67 { "invlpg", STAT_OFFSET(invlpg) },
68 { "exits", STAT_OFFSET(exits) },
69 { "io_exits", STAT_OFFSET(io_exits) },
70 { "mmio_exits", STAT_OFFSET(mmio_exits) },
71 { "signal_exits", STAT_OFFSET(signal_exits) },
72 { "irq_window", STAT_OFFSET(irq_window_exits) },
73 { "halt_exits", STAT_OFFSET(halt_exits) },
74 { "request_irq", STAT_OFFSET(request_irq_exits) },
75 { "irq_exits", STAT_OFFSET(irq_exits) },
e6adf283 76 { "light_exits", STAT_OFFSET(light_exits) },
2cc51560 77 { "efer_reload", STAT_OFFSET(efer_reload) },
1165f5fe 78 { NULL }
6aa8b732
AK
79};
80
81static struct dentry *debugfs_dir;
82
83#define MAX_IO_MSRS 256
84
707d92fa
RR
85#define CR0_RESERVED_BITS \
86 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
87 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
88 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
66aee91a
RR
89#define CR4_RESERVED_BITS \
90 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
91 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
92 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
93 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
94
7075bc81 95#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
6aa8b732
AK
96#define EFER_RESERVED_BITS 0xfffffffffffff2fe
97
05b3e0c2 98#ifdef CONFIG_X86_64
6aa8b732
AK
99// LDT or TSS descriptor in the GDT. 16 bytes.
100struct segment_descriptor_64 {
101 struct segment_descriptor s;
102 u32 base_higher;
103 u32 pad_zero;
104};
105
106#endif
107
bccf2150
AK
108static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
109 unsigned long arg);
110
6aa8b732
AK
111unsigned long segment_base(u16 selector)
112{
113 struct descriptor_table gdt;
114 struct segment_descriptor *d;
115 unsigned long table_base;
116 typedef unsigned long ul;
117 unsigned long v;
118
119 if (selector == 0)
120 return 0;
121
122 asm ("sgdt %0" : "=m"(gdt));
123 table_base = gdt.base;
124
125 if (selector & 4) { /* from ldt */
126 u16 ldt_selector;
127
128 asm ("sldt %0" : "=g"(ldt_selector));
129 table_base = segment_base(ldt_selector);
130 }
131 d = (struct segment_descriptor *)(table_base + (selector & ~7));
132 v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
05b3e0c2 133#ifdef CONFIG_X86_64
6aa8b732
AK
134 if (d->system == 0
135 && (d->type == 2 || d->type == 9 || d->type == 11))
136 v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
137#endif
138 return v;
139}
140EXPORT_SYMBOL_GPL(segment_base);
141
5aacf0ca
JM
142static inline int valid_vcpu(int n)
143{
144 return likely(n >= 0 && n < KVM_MAX_VCPUS);
145}
146
d27d4aca
AK
147int kvm_read_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
148 void *dest)
6aa8b732
AK
149{
150 unsigned char *host_buf = dest;
151 unsigned long req_size = size;
152
153 while (size) {
154 hpa_t paddr;
155 unsigned now;
156 unsigned offset;
157 hva_t guest_buf;
158
159 paddr = gva_to_hpa(vcpu, addr);
160
161 if (is_error_hpa(paddr))
162 break;
163
164 guest_buf = (hva_t)kmap_atomic(
165 pfn_to_page(paddr >> PAGE_SHIFT),
166 KM_USER0);
167 offset = addr & ~PAGE_MASK;
168 guest_buf |= offset;
169 now = min(size, PAGE_SIZE - offset);
170 memcpy(host_buf, (void*)guest_buf, now);
171 host_buf += now;
172 addr += now;
173 size -= now;
174 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
175 }
176 return req_size - size;
177}
178EXPORT_SYMBOL_GPL(kvm_read_guest);
179
d27d4aca
AK
180int kvm_write_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
181 void *data)
6aa8b732
AK
182{
183 unsigned char *host_buf = data;
184 unsigned long req_size = size;
185
186 while (size) {
187 hpa_t paddr;
188 unsigned now;
189 unsigned offset;
190 hva_t guest_buf;
ab51a434 191 gfn_t gfn;
6aa8b732
AK
192
193 paddr = gva_to_hpa(vcpu, addr);
194
195 if (is_error_hpa(paddr))
196 break;
197
ab51a434
UL
198 gfn = vcpu->mmu.gva_to_gpa(vcpu, addr) >> PAGE_SHIFT;
199 mark_page_dirty(vcpu->kvm, gfn);
6aa8b732
AK
200 guest_buf = (hva_t)kmap_atomic(
201 pfn_to_page(paddr >> PAGE_SHIFT), KM_USER0);
202 offset = addr & ~PAGE_MASK;
203 guest_buf |= offset;
204 now = min(size, PAGE_SIZE - offset);
205 memcpy((void*)guest_buf, host_buf, now);
206 host_buf += now;
207 addr += now;
208 size -= now;
209 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
210 }
211 return req_size - size;
212}
213EXPORT_SYMBOL_GPL(kvm_write_guest);
214
7702fd1f
AK
215void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
216{
217 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
218 return;
219
220 vcpu->guest_fpu_loaded = 1;
221 fx_save(vcpu->host_fx_image);
222 fx_restore(vcpu->guest_fx_image);
223}
224EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
225
226void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
227{
228 if (!vcpu->guest_fpu_loaded)
229 return;
230
231 vcpu->guest_fpu_loaded = 0;
232 fx_save(vcpu->guest_fx_image);
233 fx_restore(vcpu->host_fx_image);
234}
235EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
236
bccf2150
AK
237/*
238 * Switches to specified vcpu, until a matching vcpu_put()
239 */
240static void vcpu_load(struct kvm_vcpu *vcpu)
6aa8b732 241{
bccf2150
AK
242 mutex_lock(&vcpu->mutex);
243 kvm_arch_ops->vcpu_load(vcpu);
6aa8b732
AK
244}
245
6aa8b732
AK
246static void vcpu_put(struct kvm_vcpu *vcpu)
247{
248 kvm_arch_ops->vcpu_put(vcpu);
6aa8b732
AK
249 mutex_unlock(&vcpu->mutex);
250}
251
d9e368d6
AK
252static void ack_flush(void *_completed)
253{
254 atomic_t *completed = _completed;
255
256 atomic_inc(completed);
257}
258
259void kvm_flush_remote_tlbs(struct kvm *kvm)
260{
261 int i, cpu, needed;
262 cpumask_t cpus;
263 struct kvm_vcpu *vcpu;
264 atomic_t completed;
265
266 atomic_set(&completed, 0);
267 cpus_clear(cpus);
268 needed = 0;
269 for (i = 0; i < kvm->nvcpus; ++i) {
270 vcpu = &kvm->vcpus[i];
271 if (test_and_set_bit(KVM_TLB_FLUSH, &vcpu->requests))
272 continue;
273 cpu = vcpu->cpu;
274 if (cpu != -1 && cpu != raw_smp_processor_id())
275 if (!cpu_isset(cpu, cpus)) {
276 cpu_set(cpu, cpus);
277 ++needed;
278 }
279 }
280
281 /*
282 * We really want smp_call_function_mask() here. But that's not
283 * available, so ipi all cpus in parallel and wait for them
284 * to complete.
285 */
286 for (cpu = first_cpu(cpus); cpu != NR_CPUS; cpu = next_cpu(cpu, cpus))
287 smp_call_function_single(cpu, ack_flush, &completed, 1, 0);
288 while (atomic_read(&completed) != needed) {
289 cpu_relax();
290 barrier();
291 }
292}
293
f17abe9a 294static struct kvm *kvm_create_vm(void)
6aa8b732
AK
295{
296 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
297 int i;
298
299 if (!kvm)
f17abe9a 300 return ERR_PTR(-ENOMEM);
6aa8b732 301
74906345 302 kvm_io_bus_init(&kvm->pio_bus);
6aa8b732
AK
303 spin_lock_init(&kvm->lock);
304 INIT_LIST_HEAD(&kvm->active_mmu_pages);
2eeb2e94 305 kvm_io_bus_init(&kvm->mmio_bus);
6aa8b732
AK
306 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
307 struct kvm_vcpu *vcpu = &kvm->vcpus[i];
308
309 mutex_init(&vcpu->mutex);
133de902 310 vcpu->cpu = -1;
86a2b42e 311 vcpu->kvm = kvm;
6aa8b732 312 vcpu->mmu.root_hpa = INVALID_PAGE;
6aa8b732 313 }
5e58cfe4
RR
314 spin_lock(&kvm_lock);
315 list_add(&kvm->vm_list, &vm_list);
316 spin_unlock(&kvm_lock);
f17abe9a
AK
317 return kvm;
318}
319
320static int kvm_dev_open(struct inode *inode, struct file *filp)
321{
6aa8b732
AK
322 return 0;
323}
324
325/*
326 * Free any memory in @free but not in @dont.
327 */
328static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
329 struct kvm_memory_slot *dont)
330{
331 int i;
332
333 if (!dont || free->phys_mem != dont->phys_mem)
334 if (free->phys_mem) {
335 for (i = 0; i < free->npages; ++i)
55a54f79
AK
336 if (free->phys_mem[i])
337 __free_page(free->phys_mem[i]);
6aa8b732
AK
338 vfree(free->phys_mem);
339 }
340
341 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
342 vfree(free->dirty_bitmap);
343
8b6d44c7 344 free->phys_mem = NULL;
6aa8b732 345 free->npages = 0;
8b6d44c7 346 free->dirty_bitmap = NULL;
6aa8b732
AK
347}
348
349static void kvm_free_physmem(struct kvm *kvm)
350{
351 int i;
352
353 for (i = 0; i < kvm->nmemslots; ++i)
8b6d44c7 354 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
6aa8b732
AK
355}
356
039576c0
AK
357static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
358{
359 int i;
360
361 for (i = 0; i < 2; ++i)
362 if (vcpu->pio.guest_pages[i]) {
363 __free_page(vcpu->pio.guest_pages[i]);
364 vcpu->pio.guest_pages[i] = NULL;
365 }
366}
367
7b53aa56
AK
368static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
369{
370 if (!vcpu->vmcs)
371 return;
372
373 vcpu_load(vcpu);
374 kvm_mmu_unload(vcpu);
375 vcpu_put(vcpu);
376}
377
6aa8b732
AK
378static void kvm_free_vcpu(struct kvm_vcpu *vcpu)
379{
bccf2150 380 if (!vcpu->vmcs)
1e8ba6fb
IM
381 return;
382
bccf2150 383 vcpu_load(vcpu);
6aa8b732 384 kvm_mmu_destroy(vcpu);
08438475 385 vcpu_put(vcpu);
9ede74e0 386 kvm_arch_ops->vcpu_free(vcpu);
9a2bb7f4
AK
387 free_page((unsigned long)vcpu->run);
388 vcpu->run = NULL;
039576c0
AK
389 free_page((unsigned long)vcpu->pio_data);
390 vcpu->pio_data = NULL;
391 free_pio_guest_pages(vcpu);
6aa8b732
AK
392}
393
394static void kvm_free_vcpus(struct kvm *kvm)
395{
396 unsigned int i;
397
7b53aa56
AK
398 /*
399 * Unpin any mmu pages first.
400 */
401 for (i = 0; i < KVM_MAX_VCPUS; ++i)
402 kvm_unload_vcpu_mmu(&kvm->vcpus[i]);
6aa8b732
AK
403 for (i = 0; i < KVM_MAX_VCPUS; ++i)
404 kvm_free_vcpu(&kvm->vcpus[i]);
405}
406
407static int kvm_dev_release(struct inode *inode, struct file *filp)
408{
f17abe9a
AK
409 return 0;
410}
6aa8b732 411
f17abe9a
AK
412static void kvm_destroy_vm(struct kvm *kvm)
413{
133de902
AK
414 spin_lock(&kvm_lock);
415 list_del(&kvm->vm_list);
416 spin_unlock(&kvm_lock);
74906345 417 kvm_io_bus_destroy(&kvm->pio_bus);
2eeb2e94 418 kvm_io_bus_destroy(&kvm->mmio_bus);
6aa8b732
AK
419 kvm_free_vcpus(kvm);
420 kvm_free_physmem(kvm);
421 kfree(kvm);
f17abe9a
AK
422}
423
424static int kvm_vm_release(struct inode *inode, struct file *filp)
425{
426 struct kvm *kvm = filp->private_data;
427
428 kvm_destroy_vm(kvm);
6aa8b732
AK
429 return 0;
430}
431
432static void inject_gp(struct kvm_vcpu *vcpu)
433{
434 kvm_arch_ops->inject_gp(vcpu, 0);
435}
436
1342d353
AK
437/*
438 * Load the pae pdptrs. Return true is they are all valid.
439 */
440static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
6aa8b732
AK
441{
442 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
1342d353 443 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
6aa8b732
AK
444 int i;
445 u64 pdpte;
446 u64 *pdpt;
1342d353 447 int ret;
954bbbc2 448 struct page *page;
6aa8b732
AK
449
450 spin_lock(&vcpu->kvm->lock);
954bbbc2
AK
451 page = gfn_to_page(vcpu->kvm, pdpt_gfn);
452 /* FIXME: !page - emulate? 0xff? */
453 pdpt = kmap_atomic(page, KM_USER0);
6aa8b732 454
1342d353 455 ret = 1;
6aa8b732
AK
456 for (i = 0; i < 4; ++i) {
457 pdpte = pdpt[offset + i];
1342d353
AK
458 if ((pdpte & 1) && (pdpte & 0xfffffff0000001e6ull)) {
459 ret = 0;
460 goto out;
461 }
6aa8b732
AK
462 }
463
1342d353
AK
464 for (i = 0; i < 4; ++i)
465 vcpu->pdptrs[i] = pdpt[offset + i];
466
467out:
6aa8b732
AK
468 kunmap_atomic(pdpt, KM_USER0);
469 spin_unlock(&vcpu->kvm->lock);
470
1342d353 471 return ret;
6aa8b732
AK
472}
473
474void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
475{
707d92fa 476 if (cr0 & CR0_RESERVED_BITS) {
6aa8b732
AK
477 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
478 cr0, vcpu->cr0);
479 inject_gp(vcpu);
480 return;
481 }
482
707d92fa 483 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
6aa8b732
AK
484 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
485 inject_gp(vcpu);
486 return;
487 }
488
707d92fa 489 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
6aa8b732
AK
490 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
491 "and a clear PE flag\n");
492 inject_gp(vcpu);
493 return;
494 }
495
707d92fa 496 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
05b3e0c2 497#ifdef CONFIG_X86_64
6aa8b732
AK
498 if ((vcpu->shadow_efer & EFER_LME)) {
499 int cs_db, cs_l;
500
501 if (!is_pae(vcpu)) {
502 printk(KERN_DEBUG "set_cr0: #GP, start paging "
503 "in long mode while PAE is disabled\n");
504 inject_gp(vcpu);
505 return;
506 }
507 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
508 if (cs_l) {
509 printk(KERN_DEBUG "set_cr0: #GP, start paging "
510 "in long mode while CS.L == 1\n");
511 inject_gp(vcpu);
512 return;
513
514 }
515 } else
516#endif
1342d353 517 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
6aa8b732
AK
518 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
519 "reserved bits\n");
520 inject_gp(vcpu);
521 return;
522 }
523
524 }
525
526 kvm_arch_ops->set_cr0(vcpu, cr0);
527 vcpu->cr0 = cr0;
528
529 spin_lock(&vcpu->kvm->lock);
530 kvm_mmu_reset_context(vcpu);
531 spin_unlock(&vcpu->kvm->lock);
532 return;
533}
534EXPORT_SYMBOL_GPL(set_cr0);
535
536void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
537{
538 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
539}
540EXPORT_SYMBOL_GPL(lmsw);
541
542void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
543{
66aee91a 544 if (cr4 & CR4_RESERVED_BITS) {
6aa8b732
AK
545 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
546 inject_gp(vcpu);
547 return;
548 }
549
a9058ecd 550 if (is_long_mode(vcpu)) {
66aee91a 551 if (!(cr4 & X86_CR4_PAE)) {
6aa8b732
AK
552 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
553 "in long mode\n");
554 inject_gp(vcpu);
555 return;
556 }
66aee91a 557 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
1342d353 558 && !load_pdptrs(vcpu, vcpu->cr3)) {
6aa8b732
AK
559 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
560 inject_gp(vcpu);
310bc76c 561 return;
6aa8b732
AK
562 }
563
66aee91a 564 if (cr4 & X86_CR4_VMXE) {
6aa8b732
AK
565 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
566 inject_gp(vcpu);
567 return;
568 }
569 kvm_arch_ops->set_cr4(vcpu, cr4);
570 spin_lock(&vcpu->kvm->lock);
571 kvm_mmu_reset_context(vcpu);
572 spin_unlock(&vcpu->kvm->lock);
573}
574EXPORT_SYMBOL_GPL(set_cr4);
575
576void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
577{
a9058ecd 578 if (is_long_mode(vcpu)) {
f802a307 579 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
6aa8b732
AK
580 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
581 inject_gp(vcpu);
582 return;
583 }
584 } else {
f802a307
RR
585 if (is_pae(vcpu)) {
586 if (cr3 & CR3_PAE_RESERVED_BITS) {
587 printk(KERN_DEBUG
588 "set_cr3: #GP, reserved bits\n");
589 inject_gp(vcpu);
590 return;
591 }
592 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
593 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
594 "reserved bits\n");
595 inject_gp(vcpu);
596 return;
597 }
598 } else {
599 if (cr3 & CR3_NONPAE_RESERVED_BITS) {
600 printk(KERN_DEBUG
601 "set_cr3: #GP, reserved bits\n");
602 inject_gp(vcpu);
603 return;
604 }
6aa8b732
AK
605 }
606 }
607
608 vcpu->cr3 = cr3;
609 spin_lock(&vcpu->kvm->lock);
d21225ee
IM
610 /*
611 * Does the new cr3 value map to physical memory? (Note, we
612 * catch an invalid cr3 even in real-mode, because it would
613 * cause trouble later on when we turn on paging anyway.)
614 *
615 * A real CPU would silently accept an invalid cr3 and would
616 * attempt to use it - with largely undefined (and often hard
617 * to debug) behavior on the guest side.
618 */
619 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
620 inject_gp(vcpu);
621 else
622 vcpu->mmu.new_cr3(vcpu);
6aa8b732
AK
623 spin_unlock(&vcpu->kvm->lock);
624}
625EXPORT_SYMBOL_GPL(set_cr3);
626
627void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
628{
7075bc81 629 if (cr8 & CR8_RESERVED_BITS) {
6aa8b732
AK
630 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
631 inject_gp(vcpu);
632 return;
633 }
634 vcpu->cr8 = cr8;
635}
636EXPORT_SYMBOL_GPL(set_cr8);
637
638void fx_init(struct kvm_vcpu *vcpu)
639{
640 struct __attribute__ ((__packed__)) fx_image_s {
641 u16 control; //fcw
642 u16 status; //fsw
643 u16 tag; // ftw
644 u16 opcode; //fop
645 u64 ip; // fpu ip
646 u64 operand;// fpu dp
647 u32 mxcsr;
648 u32 mxcsr_mask;
649
650 } *fx_image;
651
652 fx_save(vcpu->host_fx_image);
653 fpu_init();
654 fx_save(vcpu->guest_fx_image);
655 fx_restore(vcpu->host_fx_image);
656
657 fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
658 fx_image->mxcsr = 0x1f80;
659 memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
660 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
661}
662EXPORT_SYMBOL_GPL(fx_init);
663
6aa8b732
AK
664/*
665 * Allocate some memory and give it an address in the guest physical address
666 * space.
667 *
668 * Discontiguous memory is allowed, mostly for framebuffers.
669 */
2c6f5df9
AK
670static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
671 struct kvm_memory_region *mem)
6aa8b732
AK
672{
673 int r;
674 gfn_t base_gfn;
675 unsigned long npages;
676 unsigned long i;
677 struct kvm_memory_slot *memslot;
678 struct kvm_memory_slot old, new;
679 int memory_config_version;
680
681 r = -EINVAL;
682 /* General sanity checks */
683 if (mem->memory_size & (PAGE_SIZE - 1))
684 goto out;
685 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
686 goto out;
687 if (mem->slot >= KVM_MEMORY_SLOTS)
688 goto out;
689 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
690 goto out;
691
692 memslot = &kvm->memslots[mem->slot];
693 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
694 npages = mem->memory_size >> PAGE_SHIFT;
695
696 if (!npages)
697 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
698
699raced:
700 spin_lock(&kvm->lock);
701
702 memory_config_version = kvm->memory_config_version;
703 new = old = *memslot;
704
705 new.base_gfn = base_gfn;
706 new.npages = npages;
707 new.flags = mem->flags;
708
709 /* Disallow changing a memory slot's size. */
710 r = -EINVAL;
711 if (npages && old.npages && npages != old.npages)
712 goto out_unlock;
713
714 /* Check for overlaps */
715 r = -EEXIST;
716 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
717 struct kvm_memory_slot *s = &kvm->memslots[i];
718
719 if (s == memslot)
720 continue;
721 if (!((base_gfn + npages <= s->base_gfn) ||
722 (base_gfn >= s->base_gfn + s->npages)))
723 goto out_unlock;
724 }
725 /*
726 * Do memory allocations outside lock. memory_config_version will
727 * detect any races.
728 */
729 spin_unlock(&kvm->lock);
730
731 /* Deallocate if slot is being removed */
732 if (!npages)
8b6d44c7 733 new.phys_mem = NULL;
6aa8b732
AK
734
735 /* Free page dirty bitmap if unneeded */
736 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
8b6d44c7 737 new.dirty_bitmap = NULL;
6aa8b732
AK
738
739 r = -ENOMEM;
740
741 /* Allocate if a slot is being created */
742 if (npages && !new.phys_mem) {
743 new.phys_mem = vmalloc(npages * sizeof(struct page *));
744
745 if (!new.phys_mem)
746 goto out_free;
747
748 memset(new.phys_mem, 0, npages * sizeof(struct page *));
749 for (i = 0; i < npages; ++i) {
750 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
751 | __GFP_ZERO);
752 if (!new.phys_mem[i])
753 goto out_free;
5972e953 754 set_page_private(new.phys_mem[i],0);
6aa8b732
AK
755 }
756 }
757
758 /* Allocate page dirty bitmap if needed */
759 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
760 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
761
762 new.dirty_bitmap = vmalloc(dirty_bytes);
763 if (!new.dirty_bitmap)
764 goto out_free;
765 memset(new.dirty_bitmap, 0, dirty_bytes);
766 }
767
768 spin_lock(&kvm->lock);
769
770 if (memory_config_version != kvm->memory_config_version) {
771 spin_unlock(&kvm->lock);
772 kvm_free_physmem_slot(&new, &old);
773 goto raced;
774 }
775
776 r = -EAGAIN;
777 if (kvm->busy)
778 goto out_unlock;
779
780 if (mem->slot >= kvm->nmemslots)
781 kvm->nmemslots = mem->slot + 1;
782
783 *memslot = new;
784 ++kvm->memory_config_version;
785
90cb0529
AK
786 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
787 kvm_flush_remote_tlbs(kvm);
6aa8b732 788
90cb0529 789 spin_unlock(&kvm->lock);
6aa8b732
AK
790
791 kvm_free_physmem_slot(&old, &new);
792 return 0;
793
794out_unlock:
795 spin_unlock(&kvm->lock);
796out_free:
797 kvm_free_physmem_slot(&new, &old);
798out:
799 return r;
800}
801
802/*
803 * Get (and clear) the dirty memory log for a memory slot.
804 */
2c6f5df9
AK
805static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
806 struct kvm_dirty_log *log)
6aa8b732
AK
807{
808 struct kvm_memory_slot *memslot;
809 int r, i;
810 int n;
811 unsigned long any = 0;
812
813 spin_lock(&kvm->lock);
814
815 /*
816 * Prevent changes to guest memory configuration even while the lock
817 * is not taken.
818 */
819 ++kvm->busy;
820 spin_unlock(&kvm->lock);
821 r = -EINVAL;
822 if (log->slot >= KVM_MEMORY_SLOTS)
823 goto out;
824
825 memslot = &kvm->memslots[log->slot];
826 r = -ENOENT;
827 if (!memslot->dirty_bitmap)
828 goto out;
829
cd1a4a98 830 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
6aa8b732 831
cd1a4a98 832 for (i = 0; !any && i < n/sizeof(long); ++i)
6aa8b732
AK
833 any = memslot->dirty_bitmap[i];
834
835 r = -EFAULT;
836 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
837 goto out;
838
90cb0529
AK
839 spin_lock(&kvm->lock);
840 kvm_mmu_slot_remove_write_access(kvm, log->slot);
841 kvm_flush_remote_tlbs(kvm);
842 memset(memslot->dirty_bitmap, 0, n);
843 spin_unlock(&kvm->lock);
6aa8b732
AK
844
845 r = 0;
846
847out:
848 spin_lock(&kvm->lock);
849 --kvm->busy;
850 spin_unlock(&kvm->lock);
851 return r;
852}
853
e8207547
AK
854/*
855 * Set a new alias region. Aliases map a portion of physical memory into
856 * another portion. This is useful for memory windows, for example the PC
857 * VGA region.
858 */
859static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
860 struct kvm_memory_alias *alias)
861{
862 int r, n;
863 struct kvm_mem_alias *p;
864
865 r = -EINVAL;
866 /* General sanity checks */
867 if (alias->memory_size & (PAGE_SIZE - 1))
868 goto out;
869 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
870 goto out;
871 if (alias->slot >= KVM_ALIAS_SLOTS)
872 goto out;
873 if (alias->guest_phys_addr + alias->memory_size
874 < alias->guest_phys_addr)
875 goto out;
876 if (alias->target_phys_addr + alias->memory_size
877 < alias->target_phys_addr)
878 goto out;
879
880 spin_lock(&kvm->lock);
881
882 p = &kvm->aliases[alias->slot];
883 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
884 p->npages = alias->memory_size >> PAGE_SHIFT;
885 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
886
887 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
888 if (kvm->aliases[n - 1].npages)
889 break;
890 kvm->naliases = n;
891
90cb0529 892 kvm_mmu_zap_all(kvm);
e8207547 893
e8207547 894 spin_unlock(&kvm->lock);
e8207547
AK
895
896 return 0;
897
898out:
899 return r;
900}
901
902static gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
903{
904 int i;
905 struct kvm_mem_alias *alias;
906
907 for (i = 0; i < kvm->naliases; ++i) {
908 alias = &kvm->aliases[i];
909 if (gfn >= alias->base_gfn
910 && gfn < alias->base_gfn + alias->npages)
911 return alias->target_gfn + gfn - alias->base_gfn;
912 }
913 return gfn;
914}
915
916static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
6aa8b732
AK
917{
918 int i;
919
920 for (i = 0; i < kvm->nmemslots; ++i) {
921 struct kvm_memory_slot *memslot = &kvm->memslots[i];
922
923 if (gfn >= memslot->base_gfn
924 && gfn < memslot->base_gfn + memslot->npages)
925 return memslot;
926 }
8b6d44c7 927 return NULL;
6aa8b732 928}
e8207547
AK
929
930struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
931{
932 gfn = unalias_gfn(kvm, gfn);
933 return __gfn_to_memslot(kvm, gfn);
934}
6aa8b732 935
954bbbc2
AK
936struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
937{
938 struct kvm_memory_slot *slot;
939
e8207547
AK
940 gfn = unalias_gfn(kvm, gfn);
941 slot = __gfn_to_memslot(kvm, gfn);
954bbbc2
AK
942 if (!slot)
943 return NULL;
944 return slot->phys_mem[gfn - slot->base_gfn];
945}
946EXPORT_SYMBOL_GPL(gfn_to_page);
947
6aa8b732
AK
948void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
949{
950 int i;
31389947 951 struct kvm_memory_slot *memslot;
6aa8b732
AK
952 unsigned long rel_gfn;
953
954 for (i = 0; i < kvm->nmemslots; ++i) {
955 memslot = &kvm->memslots[i];
956
957 if (gfn >= memslot->base_gfn
958 && gfn < memslot->base_gfn + memslot->npages) {
959
31389947 960 if (!memslot->dirty_bitmap)
6aa8b732
AK
961 return;
962
963 rel_gfn = gfn - memslot->base_gfn;
964
965 /* avoid RMW */
966 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
967 set_bit(rel_gfn, memslot->dirty_bitmap);
968 return;
969 }
970 }
971}
972
973static int emulator_read_std(unsigned long addr,
4c690a1e 974 void *val,
6aa8b732
AK
975 unsigned int bytes,
976 struct x86_emulate_ctxt *ctxt)
977{
978 struct kvm_vcpu *vcpu = ctxt->vcpu;
979 void *data = val;
980
981 while (bytes) {
982 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
983 unsigned offset = addr & (PAGE_SIZE-1);
984 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
985 unsigned long pfn;
954bbbc2
AK
986 struct page *page;
987 void *page_virt;
6aa8b732
AK
988
989 if (gpa == UNMAPPED_GVA)
990 return X86EMUL_PROPAGATE_FAULT;
991 pfn = gpa >> PAGE_SHIFT;
954bbbc2
AK
992 page = gfn_to_page(vcpu->kvm, pfn);
993 if (!page)
6aa8b732 994 return X86EMUL_UNHANDLEABLE;
954bbbc2 995 page_virt = kmap_atomic(page, KM_USER0);
6aa8b732 996
954bbbc2 997 memcpy(data, page_virt + offset, tocopy);
6aa8b732 998
954bbbc2 999 kunmap_atomic(page_virt, KM_USER0);
6aa8b732
AK
1000
1001 bytes -= tocopy;
1002 data += tocopy;
1003 addr += tocopy;
1004 }
1005
1006 return X86EMUL_CONTINUE;
1007}
1008
1009static int emulator_write_std(unsigned long addr,
4c690a1e 1010 const void *val,
6aa8b732
AK
1011 unsigned int bytes,
1012 struct x86_emulate_ctxt *ctxt)
1013{
1014 printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
1015 addr, bytes);
1016 return X86EMUL_UNHANDLEABLE;
1017}
1018
2eeb2e94
GH
1019static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1020 gpa_t addr)
1021{
1022 /*
1023 * Note that its important to have this wrapper function because
1024 * in the very near future we will be checking for MMIOs against
1025 * the LAPIC as well as the general MMIO bus
1026 */
1027 return kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1028}
1029
74906345
ED
1030static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
1031 gpa_t addr)
1032{
1033 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
1034}
1035
6aa8b732 1036static int emulator_read_emulated(unsigned long addr,
4c690a1e 1037 void *val,
6aa8b732
AK
1038 unsigned int bytes,
1039 struct x86_emulate_ctxt *ctxt)
1040{
2eeb2e94
GH
1041 struct kvm_vcpu *vcpu = ctxt->vcpu;
1042 struct kvm_io_device *mmio_dev;
1043 gpa_t gpa;
6aa8b732
AK
1044
1045 if (vcpu->mmio_read_completed) {
1046 memcpy(val, vcpu->mmio_data, bytes);
1047 vcpu->mmio_read_completed = 0;
1048 return X86EMUL_CONTINUE;
1049 } else if (emulator_read_std(addr, val, bytes, ctxt)
1050 == X86EMUL_CONTINUE)
1051 return X86EMUL_CONTINUE;
d27d4aca 1052
2eeb2e94
GH
1053 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1054 if (gpa == UNMAPPED_GVA)
1055 return X86EMUL_PROPAGATE_FAULT;
6aa8b732 1056
2eeb2e94
GH
1057 /*
1058 * Is this MMIO handled locally?
1059 */
1060 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1061 if (mmio_dev) {
1062 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1063 return X86EMUL_CONTINUE;
6aa8b732 1064 }
2eeb2e94
GH
1065
1066 vcpu->mmio_needed = 1;
1067 vcpu->mmio_phys_addr = gpa;
1068 vcpu->mmio_size = bytes;
1069 vcpu->mmio_is_write = 0;
1070
1071 return X86EMUL_UNHANDLEABLE;
6aa8b732
AK
1072}
1073
da4a00f0 1074static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
4c690a1e 1075 const void *val, int bytes)
da4a00f0 1076{
da4a00f0
AK
1077 struct page *page;
1078 void *virt;
09072daf 1079 unsigned offset = offset_in_page(gpa);
da4a00f0
AK
1080
1081 if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
1082 return 0;
954bbbc2
AK
1083 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1084 if (!page)
da4a00f0 1085 return 0;
ab51a434 1086 mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
da4a00f0 1087 virt = kmap_atomic(page, KM_USER0);
7cfa4b0a
AK
1088 kvm_mmu_pte_write(vcpu, gpa, virt + offset, val, bytes);
1089 memcpy(virt + offset_in_page(gpa), val, bytes);
da4a00f0 1090 kunmap_atomic(virt, KM_USER0);
da4a00f0
AK
1091 return 1;
1092}
1093
b0fcd903
AK
1094static int emulator_write_emulated_onepage(unsigned long addr,
1095 const void *val,
1096 unsigned int bytes,
1097 struct x86_emulate_ctxt *ctxt)
6aa8b732 1098{
2eeb2e94
GH
1099 struct kvm_vcpu *vcpu = ctxt->vcpu;
1100 struct kvm_io_device *mmio_dev;
1101 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
6aa8b732 1102
c9047f53
AK
1103 if (gpa == UNMAPPED_GVA) {
1104 kvm_arch_ops->inject_page_fault(vcpu, addr, 2);
6aa8b732 1105 return X86EMUL_PROPAGATE_FAULT;
c9047f53 1106 }
6aa8b732 1107
da4a00f0
AK
1108 if (emulator_write_phys(vcpu, gpa, val, bytes))
1109 return X86EMUL_CONTINUE;
1110
2eeb2e94
GH
1111 /*
1112 * Is this MMIO handled locally?
1113 */
1114 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1115 if (mmio_dev) {
1116 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1117 return X86EMUL_CONTINUE;
1118 }
1119
6aa8b732
AK
1120 vcpu->mmio_needed = 1;
1121 vcpu->mmio_phys_addr = gpa;
1122 vcpu->mmio_size = bytes;
1123 vcpu->mmio_is_write = 1;
4c690a1e 1124 memcpy(vcpu->mmio_data, val, bytes);
6aa8b732
AK
1125
1126 return X86EMUL_CONTINUE;
1127}
1128
b0fcd903
AK
1129static int emulator_write_emulated(unsigned long addr,
1130 const void *val,
1131 unsigned int bytes,
1132 struct x86_emulate_ctxt *ctxt)
1133{
1134 /* Crossing a page boundary? */
1135 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1136 int rc, now;
1137
1138 now = -addr & ~PAGE_MASK;
1139 rc = emulator_write_emulated_onepage(addr, val, now, ctxt);
1140 if (rc != X86EMUL_CONTINUE)
1141 return rc;
1142 addr += now;
1143 val += now;
1144 bytes -= now;
1145 }
1146 return emulator_write_emulated_onepage(addr, val, bytes, ctxt);
1147}
1148
6aa8b732 1149static int emulator_cmpxchg_emulated(unsigned long addr,
4c690a1e
AK
1150 const void *old,
1151 const void *new,
6aa8b732
AK
1152 unsigned int bytes,
1153 struct x86_emulate_ctxt *ctxt)
1154{
1155 static int reported;
1156
1157 if (!reported) {
1158 reported = 1;
1159 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1160 }
1161 return emulator_write_emulated(addr, new, bytes, ctxt);
1162}
1163
1164static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1165{
1166 return kvm_arch_ops->get_segment_base(vcpu, seg);
1167}
1168
1169int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1170{
6aa8b732
AK
1171 return X86EMUL_CONTINUE;
1172}
1173
1174int emulate_clts(struct kvm_vcpu *vcpu)
1175{
399badf3 1176 unsigned long cr0;
6aa8b732 1177
707d92fa 1178 cr0 = vcpu->cr0 & ~X86_CR0_TS;
6aa8b732
AK
1179 kvm_arch_ops->set_cr0(vcpu, cr0);
1180 return X86EMUL_CONTINUE;
1181}
1182
1183int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1184{
1185 struct kvm_vcpu *vcpu = ctxt->vcpu;
1186
1187 switch (dr) {
1188 case 0 ... 3:
1189 *dest = kvm_arch_ops->get_dr(vcpu, dr);
1190 return X86EMUL_CONTINUE;
1191 default:
1192 printk(KERN_DEBUG "%s: unexpected dr %u\n",
1193 __FUNCTION__, dr);
1194 return X86EMUL_UNHANDLEABLE;
1195 }
1196}
1197
1198int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1199{
1200 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1201 int exception;
1202
1203 kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1204 if (exception) {
1205 /* FIXME: better handling */
1206 return X86EMUL_UNHANDLEABLE;
1207 }
1208 return X86EMUL_CONTINUE;
1209}
1210
1211static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
1212{
1213 static int reported;
1214 u8 opcodes[4];
1215 unsigned long rip = ctxt->vcpu->rip;
1216 unsigned long rip_linear;
1217
1218 rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
1219
1220 if (reported)
1221 return;
1222
1223 emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
1224
1225 printk(KERN_ERR "emulation failed but !mmio_needed?"
1226 " rip %lx %02x %02x %02x %02x\n",
1227 rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1228 reported = 1;
1229}
1230
1231struct x86_emulate_ops emulate_ops = {
1232 .read_std = emulator_read_std,
1233 .write_std = emulator_write_std,
1234 .read_emulated = emulator_read_emulated,
1235 .write_emulated = emulator_write_emulated,
1236 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1237};
1238
1239int emulate_instruction(struct kvm_vcpu *vcpu,
1240 struct kvm_run *run,
1241 unsigned long cr2,
1242 u16 error_code)
1243{
1244 struct x86_emulate_ctxt emulate_ctxt;
1245 int r;
1246 int cs_db, cs_l;
1247
e7df56e4 1248 vcpu->mmio_fault_cr2 = cr2;
6aa8b732
AK
1249 kvm_arch_ops->cache_regs(vcpu);
1250
1251 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1252
1253 emulate_ctxt.vcpu = vcpu;
1254 emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
1255 emulate_ctxt.cr2 = cr2;
1256 emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
1257 ? X86EMUL_MODE_REAL : cs_l
1258 ? X86EMUL_MODE_PROT64 : cs_db
1259 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1260
1261 if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1262 emulate_ctxt.cs_base = 0;
1263 emulate_ctxt.ds_base = 0;
1264 emulate_ctxt.es_base = 0;
1265 emulate_ctxt.ss_base = 0;
1266 } else {
1267 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1268 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1269 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1270 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1271 }
1272
1273 emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1274 emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1275
1276 vcpu->mmio_is_write = 0;
1277 r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
1278
1279 if ((r || vcpu->mmio_is_write) && run) {
8fc0d085 1280 run->exit_reason = KVM_EXIT_MMIO;
6aa8b732
AK
1281 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1282 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1283 run->mmio.len = vcpu->mmio_size;
1284 run->mmio.is_write = vcpu->mmio_is_write;
1285 }
1286
1287 if (r) {
a436036b
AK
1288 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1289 return EMULATE_DONE;
6aa8b732
AK
1290 if (!vcpu->mmio_needed) {
1291 report_emulation_failure(&emulate_ctxt);
1292 return EMULATE_FAIL;
1293 }
1294 return EMULATE_DO_MMIO;
1295 }
1296
1297 kvm_arch_ops->decache_regs(vcpu);
1298 kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
1299
02c83209
AK
1300 if (vcpu->mmio_is_write) {
1301 vcpu->mmio_needed = 0;
6aa8b732 1302 return EMULATE_DO_MMIO;
02c83209 1303 }
6aa8b732
AK
1304
1305 return EMULATE_DONE;
1306}
1307EXPORT_SYMBOL_GPL(emulate_instruction);
1308
d3bef15f
AK
1309int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1310{
1311 if (vcpu->irq_summary)
1312 return 1;
1313
1314 vcpu->run->exit_reason = KVM_EXIT_HLT;
1315 ++vcpu->stat.halt_exits;
1316 return 0;
1317}
1318EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1319
270fd9b9
AK
1320int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
1321{
1322 unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
1323
9b22bf57 1324 kvm_arch_ops->cache_regs(vcpu);
270fd9b9
AK
1325 ret = -KVM_EINVAL;
1326#ifdef CONFIG_X86_64
1327 if (is_long_mode(vcpu)) {
1328 nr = vcpu->regs[VCPU_REGS_RAX];
1329 a0 = vcpu->regs[VCPU_REGS_RDI];
1330 a1 = vcpu->regs[VCPU_REGS_RSI];
1331 a2 = vcpu->regs[VCPU_REGS_RDX];
1332 a3 = vcpu->regs[VCPU_REGS_RCX];
1333 a4 = vcpu->regs[VCPU_REGS_R8];
1334 a5 = vcpu->regs[VCPU_REGS_R9];
1335 } else
1336#endif
1337 {
1338 nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
1339 a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
1340 a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
1341 a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
1342 a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
1343 a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
1344 a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
1345 }
1346 switch (nr) {
1347 default:
b4e63f56
AK
1348 run->hypercall.args[0] = a0;
1349 run->hypercall.args[1] = a1;
1350 run->hypercall.args[2] = a2;
1351 run->hypercall.args[3] = a3;
1352 run->hypercall.args[4] = a4;
1353 run->hypercall.args[5] = a5;
1354 run->hypercall.ret = ret;
1355 run->hypercall.longmode = is_long_mode(vcpu);
1356 kvm_arch_ops->decache_regs(vcpu);
1357 return 0;
270fd9b9
AK
1358 }
1359 vcpu->regs[VCPU_REGS_RAX] = ret;
9b22bf57 1360 kvm_arch_ops->decache_regs(vcpu);
270fd9b9
AK
1361 return 1;
1362}
1363EXPORT_SYMBOL_GPL(kvm_hypercall);
1364
6aa8b732
AK
1365static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1366{
1367 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1368}
1369
1370void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1371{
1372 struct descriptor_table dt = { limit, base };
1373
1374 kvm_arch_ops->set_gdt(vcpu, &dt);
1375}
1376
1377void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1378{
1379 struct descriptor_table dt = { limit, base };
1380
1381 kvm_arch_ops->set_idt(vcpu, &dt);
1382}
1383
1384void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1385 unsigned long *rflags)
1386{
1387 lmsw(vcpu, msw);
1388 *rflags = kvm_arch_ops->get_rflags(vcpu);
1389}
1390
1391unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1392{
25c4c276 1393 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
6aa8b732
AK
1394 switch (cr) {
1395 case 0:
1396 return vcpu->cr0;
1397 case 2:
1398 return vcpu->cr2;
1399 case 3:
1400 return vcpu->cr3;
1401 case 4:
1402 return vcpu->cr4;
1403 default:
1404 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1405 return 0;
1406 }
1407}
1408
1409void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1410 unsigned long *rflags)
1411{
1412 switch (cr) {
1413 case 0:
1414 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
1415 *rflags = kvm_arch_ops->get_rflags(vcpu);
1416 break;
1417 case 2:
1418 vcpu->cr2 = val;
1419 break;
1420 case 3:
1421 set_cr3(vcpu, val);
1422 break;
1423 case 4:
1424 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1425 break;
1426 default:
1427 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1428 }
1429}
1430
102d8325
IM
1431/*
1432 * Register the para guest with the host:
1433 */
1434static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
1435{
1436 struct kvm_vcpu_para_state *para_state;
1437 hpa_t para_state_hpa, hypercall_hpa;
1438 struct page *para_state_page;
1439 unsigned char *hypercall;
1440 gpa_t hypercall_gpa;
1441
1442 printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
1443 printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
1444
1445 /*
1446 * Needs to be page aligned:
1447 */
1448 if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
1449 goto err_gp;
1450
1451 para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
1452 printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
1453 if (is_error_hpa(para_state_hpa))
1454 goto err_gp;
1455
ab51a434 1456 mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
102d8325
IM
1457 para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
1458 para_state = kmap_atomic(para_state_page, KM_USER0);
1459
1460 printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
1461 printk(KERN_DEBUG ".... size: %d\n", para_state->size);
1462
1463 para_state->host_version = KVM_PARA_API_VERSION;
1464 /*
1465 * We cannot support guests that try to register themselves
1466 * with a newer API version than the host supports:
1467 */
1468 if (para_state->guest_version > KVM_PARA_API_VERSION) {
1469 para_state->ret = -KVM_EINVAL;
1470 goto err_kunmap_skip;
1471 }
1472
1473 hypercall_gpa = para_state->hypercall_gpa;
1474 hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
1475 printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
1476 if (is_error_hpa(hypercall_hpa)) {
1477 para_state->ret = -KVM_EINVAL;
1478 goto err_kunmap_skip;
1479 }
1480
1481 printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
1482 vcpu->para_state_page = para_state_page;
1483 vcpu->para_state_gpa = para_state_gpa;
1484 vcpu->hypercall_gpa = hypercall_gpa;
1485
ab51a434 1486 mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
102d8325
IM
1487 hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
1488 KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
1489 kvm_arch_ops->patch_hypercall(vcpu, hypercall);
1490 kunmap_atomic(hypercall, KM_USER1);
1491
1492 para_state->ret = 0;
1493err_kunmap_skip:
1494 kunmap_atomic(para_state, KM_USER0);
1495 return 0;
1496err_gp:
1497 return 1;
1498}
1499
3bab1f5d
AK
1500int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1501{
1502 u64 data;
1503
1504 switch (msr) {
1505 case 0xc0010010: /* SYSCFG */
1506 case 0xc0010015: /* HWCR */
1507 case MSR_IA32_PLATFORM_ID:
1508 case MSR_IA32_P5_MC_ADDR:
1509 case MSR_IA32_P5_MC_TYPE:
1510 case MSR_IA32_MC0_CTL:
1511 case MSR_IA32_MCG_STATUS:
1512 case MSR_IA32_MCG_CAP:
1513 case MSR_IA32_MC0_MISC:
1514 case MSR_IA32_MC0_MISC+4:
1515 case MSR_IA32_MC0_MISC+8:
1516 case MSR_IA32_MC0_MISC+12:
1517 case MSR_IA32_MC0_MISC+16:
1518 case MSR_IA32_UCODE_REV:
a8d13ea2 1519 case MSR_IA32_PERF_STATUS:
2dc7094b 1520 case MSR_IA32_EBL_CR_POWERON:
3bab1f5d
AK
1521 /* MTRR registers */
1522 case 0xfe:
1523 case 0x200 ... 0x2ff:
1524 data = 0;
1525 break;
a8d13ea2
AK
1526 case 0xcd: /* fsb frequency */
1527 data = 3;
1528 break;
3bab1f5d
AK
1529 case MSR_IA32_APICBASE:
1530 data = vcpu->apic_base;
1531 break;
6f00e68f
AK
1532 case MSR_IA32_MISC_ENABLE:
1533 data = vcpu->ia32_misc_enable_msr;
1534 break;
3bab1f5d
AK
1535#ifdef CONFIG_X86_64
1536 case MSR_EFER:
1537 data = vcpu->shadow_efer;
1538 break;
1539#endif
1540 default:
1541 printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
1542 return 1;
1543 }
1544 *pdata = data;
1545 return 0;
1546}
1547EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1548
6aa8b732
AK
1549/*
1550 * Reads an msr value (of 'msr_index') into 'pdata'.
1551 * Returns 0 on success, non-0 otherwise.
1552 * Assumes vcpu_load() was already called.
1553 */
35f3f286 1554int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
6aa8b732
AK
1555{
1556 return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
1557}
1558
05b3e0c2 1559#ifdef CONFIG_X86_64
6aa8b732 1560
3bab1f5d 1561static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
6aa8b732 1562{
6aa8b732
AK
1563 if (efer & EFER_RESERVED_BITS) {
1564 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1565 efer);
1566 inject_gp(vcpu);
1567 return;
1568 }
1569
1570 if (is_paging(vcpu)
1571 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1572 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1573 inject_gp(vcpu);
1574 return;
1575 }
1576
7725f0ba
AK
1577 kvm_arch_ops->set_efer(vcpu, efer);
1578
6aa8b732
AK
1579 efer &= ~EFER_LMA;
1580 efer |= vcpu->shadow_efer & EFER_LMA;
1581
1582 vcpu->shadow_efer = efer;
6aa8b732 1583}
6aa8b732
AK
1584
1585#endif
1586
3bab1f5d
AK
1587int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1588{
1589 switch (msr) {
1590#ifdef CONFIG_X86_64
1591 case MSR_EFER:
1592 set_efer(vcpu, data);
1593 break;
1594#endif
1595 case MSR_IA32_MC0_STATUS:
1596 printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1597 __FUNCTION__, data);
1598 break;
0e5bf0d0
SK
1599 case MSR_IA32_MCG_STATUS:
1600 printk(KERN_WARNING "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
1601 __FUNCTION__, data);
1602 break;
3bab1f5d
AK
1603 case MSR_IA32_UCODE_REV:
1604 case MSR_IA32_UCODE_WRITE:
1605 case 0x200 ... 0x2ff: /* MTRRs */
1606 break;
1607 case MSR_IA32_APICBASE:
1608 vcpu->apic_base = data;
1609 break;
6f00e68f
AK
1610 case MSR_IA32_MISC_ENABLE:
1611 vcpu->ia32_misc_enable_msr = data;
1612 break;
102d8325
IM
1613 /*
1614 * This is the 'probe whether the host is KVM' logic:
1615 */
1616 case MSR_KVM_API_MAGIC:
1617 return vcpu_register_para(vcpu, data);
1618
3bab1f5d
AK
1619 default:
1620 printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
1621 return 1;
1622 }
1623 return 0;
1624}
1625EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1626
6aa8b732
AK
1627/*
1628 * Writes msr value into into the appropriate "register".
1629 * Returns 0 on success, non-0 otherwise.
1630 * Assumes vcpu_load() was already called.
1631 */
35f3f286 1632int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
6aa8b732
AK
1633{
1634 return kvm_arch_ops->set_msr(vcpu, msr_index, data);
1635}
1636
1637void kvm_resched(struct kvm_vcpu *vcpu)
1638{
3fca0365
YD
1639 if (!need_resched())
1640 return;
6aa8b732
AK
1641 vcpu_put(vcpu);
1642 cond_resched();
bccf2150 1643 vcpu_load(vcpu);
6aa8b732
AK
1644}
1645EXPORT_SYMBOL_GPL(kvm_resched);
1646
1647void load_msrs(struct vmx_msr_entry *e, int n)
1648{
1649 int i;
1650
1651 for (i = 0; i < n; ++i)
1652 wrmsrl(e[i].index, e[i].data);
1653}
1654EXPORT_SYMBOL_GPL(load_msrs);
1655
1656void save_msrs(struct vmx_msr_entry *e, int n)
1657{
1658 int i;
1659
1660 for (i = 0; i < n; ++i)
1661 rdmsrl(e[i].index, e[i].data);
1662}
1663EXPORT_SYMBOL_GPL(save_msrs);
1664
06465c5a
AK
1665void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1666{
1667 int i;
1668 u32 function;
1669 struct kvm_cpuid_entry *e, *best;
1670
1671 kvm_arch_ops->cache_regs(vcpu);
1672 function = vcpu->regs[VCPU_REGS_RAX];
1673 vcpu->regs[VCPU_REGS_RAX] = 0;
1674 vcpu->regs[VCPU_REGS_RBX] = 0;
1675 vcpu->regs[VCPU_REGS_RCX] = 0;
1676 vcpu->regs[VCPU_REGS_RDX] = 0;
1677 best = NULL;
1678 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1679 e = &vcpu->cpuid_entries[i];
1680 if (e->function == function) {
1681 best = e;
1682 break;
1683 }
1684 /*
1685 * Both basic or both extended?
1686 */
1687 if (((e->function ^ function) & 0x80000000) == 0)
1688 if (!best || e->function > best->function)
1689 best = e;
1690 }
1691 if (best) {
1692 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1693 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1694 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1695 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1696 }
1697 kvm_arch_ops->decache_regs(vcpu);
1698 kvm_arch_ops->skip_emulated_instruction(vcpu);
1699}
1700EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1701
039576c0 1702static int pio_copy_data(struct kvm_vcpu *vcpu)
46fc1477 1703{
039576c0
AK
1704 void *p = vcpu->pio_data;
1705 void *q;
1706 unsigned bytes;
1707 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1708
1709 kvm_arch_ops->vcpu_put(vcpu);
1710 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1711 PAGE_KERNEL);
1712 if (!q) {
1713 kvm_arch_ops->vcpu_load(vcpu);
1714 free_pio_guest_pages(vcpu);
1715 return -ENOMEM;
1716 }
1717 q += vcpu->pio.guest_page_offset;
1718 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1719 if (vcpu->pio.in)
1720 memcpy(q, p, bytes);
1721 else
1722 memcpy(p, q, bytes);
1723 q -= vcpu->pio.guest_page_offset;
1724 vunmap(q);
1725 kvm_arch_ops->vcpu_load(vcpu);
1726 free_pio_guest_pages(vcpu);
1727 return 0;
1728}
1729
1730static int complete_pio(struct kvm_vcpu *vcpu)
1731{
1732 struct kvm_pio_request *io = &vcpu->pio;
46fc1477 1733 long delta;
039576c0 1734 int r;
46fc1477
AK
1735
1736 kvm_arch_ops->cache_regs(vcpu);
1737
1738 if (!io->string) {
039576c0
AK
1739 if (io->in)
1740 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
46fc1477
AK
1741 io->size);
1742 } else {
039576c0
AK
1743 if (io->in) {
1744 r = pio_copy_data(vcpu);
1745 if (r) {
1746 kvm_arch_ops->cache_regs(vcpu);
1747 return r;
1748 }
1749 }
1750
46fc1477
AK
1751 delta = 1;
1752 if (io->rep) {
039576c0 1753 delta *= io->cur_count;
46fc1477
AK
1754 /*
1755 * The size of the register should really depend on
1756 * current address size.
1757 */
1758 vcpu->regs[VCPU_REGS_RCX] -= delta;
1759 }
039576c0 1760 if (io->down)
46fc1477
AK
1761 delta = -delta;
1762 delta *= io->size;
039576c0 1763 if (io->in)
46fc1477
AK
1764 vcpu->regs[VCPU_REGS_RDI] += delta;
1765 else
1766 vcpu->regs[VCPU_REGS_RSI] += delta;
1767 }
1768
46fc1477
AK
1769 kvm_arch_ops->decache_regs(vcpu);
1770
039576c0
AK
1771 io->count -= io->cur_count;
1772 io->cur_count = 0;
1773
1774 if (!io->count)
1775 kvm_arch_ops->skip_emulated_instruction(vcpu);
1776 return 0;
46fc1477
AK
1777}
1778
65619eb5
ED
1779static void kernel_pio(struct kvm_io_device *pio_dev,
1780 struct kvm_vcpu *vcpu,
1781 void *pd)
74906345
ED
1782{
1783 /* TODO: String I/O for in kernel device */
1784
1785 if (vcpu->pio.in)
1786 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1787 vcpu->pio.size,
65619eb5 1788 pd);
74906345
ED
1789 else
1790 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1791 vcpu->pio.size,
65619eb5
ED
1792 pd);
1793}
1794
1795static void pio_string_write(struct kvm_io_device *pio_dev,
1796 struct kvm_vcpu *vcpu)
1797{
1798 struct kvm_pio_request *io = &vcpu->pio;
1799 void *pd = vcpu->pio_data;
1800 int i;
1801
1802 for (i = 0; i < io->cur_count; i++) {
1803 kvm_iodevice_write(pio_dev, io->port,
1804 io->size,
1805 pd);
1806 pd += io->size;
1807 }
74906345
ED
1808}
1809
039576c0
AK
1810int kvm_setup_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1811 int size, unsigned long count, int string, int down,
1812 gva_t address, int rep, unsigned port)
1813{
1814 unsigned now, in_page;
65619eb5 1815 int i, ret = 0;
039576c0
AK
1816 int nr_pages = 1;
1817 struct page *page;
74906345 1818 struct kvm_io_device *pio_dev;
039576c0
AK
1819
1820 vcpu->run->exit_reason = KVM_EXIT_IO;
1821 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1822 vcpu->run->io.size = size;
1823 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1824 vcpu->run->io.count = count;
1825 vcpu->run->io.port = port;
1826 vcpu->pio.count = count;
1827 vcpu->pio.cur_count = count;
1828 vcpu->pio.size = size;
1829 vcpu->pio.in = in;
74906345 1830 vcpu->pio.port = port;
039576c0
AK
1831 vcpu->pio.string = string;
1832 vcpu->pio.down = down;
1833 vcpu->pio.guest_page_offset = offset_in_page(address);
1834 vcpu->pio.rep = rep;
1835
74906345 1836 pio_dev = vcpu_find_pio_dev(vcpu, port);
039576c0
AK
1837 if (!string) {
1838 kvm_arch_ops->cache_regs(vcpu);
1839 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
1840 kvm_arch_ops->decache_regs(vcpu);
74906345 1841 if (pio_dev) {
65619eb5 1842 kernel_pio(pio_dev, vcpu, vcpu->pio_data);
74906345
ED
1843 complete_pio(vcpu);
1844 return 1;
1845 }
039576c0
AK
1846 return 0;
1847 }
1848
1849 if (!count) {
1850 kvm_arch_ops->skip_emulated_instruction(vcpu);
1851 return 1;
1852 }
1853
1854 now = min(count, PAGE_SIZE / size);
1855
1856 if (!down)
1857 in_page = PAGE_SIZE - offset_in_page(address);
1858 else
1859 in_page = offset_in_page(address) + size;
1860 now = min(count, (unsigned long)in_page / size);
1861 if (!now) {
1862 /*
1863 * String I/O straddles page boundary. Pin two guest pages
1864 * so that we satisfy atomicity constraints. Do just one
1865 * transaction to avoid complexity.
1866 */
1867 nr_pages = 2;
1868 now = 1;
1869 }
1870 if (down) {
1871 /*
1872 * String I/O in reverse. Yuck. Kill the guest, fix later.
1873 */
1874 printk(KERN_ERR "kvm: guest string pio down\n");
1875 inject_gp(vcpu);
1876 return 1;
1877 }
1878 vcpu->run->io.count = now;
1879 vcpu->pio.cur_count = now;
1880
1881 for (i = 0; i < nr_pages; ++i) {
1882 spin_lock(&vcpu->kvm->lock);
1883 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
1884 if (page)
1885 get_page(page);
1886 vcpu->pio.guest_pages[i] = page;
1887 spin_unlock(&vcpu->kvm->lock);
1888 if (!page) {
1889 inject_gp(vcpu);
1890 free_pio_guest_pages(vcpu);
1891 return 1;
1892 }
1893 }
1894
65619eb5
ED
1895 if (!vcpu->pio.in) {
1896 /* string PIO write */
1897 ret = pio_copy_data(vcpu);
1898 if (ret >= 0 && pio_dev) {
1899 pio_string_write(pio_dev, vcpu);
1900 complete_pio(vcpu);
1901 if (vcpu->pio.count == 0)
1902 ret = 1;
1903 }
1904 } else if (pio_dev)
1905 printk(KERN_ERR "no string pio read support yet, "
1906 "port %x size %d count %ld\n",
1907 port, size, count);
1908
1909 return ret;
039576c0
AK
1910}
1911EXPORT_SYMBOL_GPL(kvm_setup_pio);
1912
bccf2150 1913static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
6aa8b732 1914{
6aa8b732 1915 int r;
1961d276 1916 sigset_t sigsaved;
6aa8b732 1917
bccf2150 1918 vcpu_load(vcpu);
6aa8b732 1919
1961d276
AK
1920 if (vcpu->sigset_active)
1921 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
1922
54810342
DL
1923 /* re-sync apic's tpr */
1924 vcpu->cr8 = kvm_run->cr8;
1925
02c83209
AK
1926 if (vcpu->pio.cur_count) {
1927 r = complete_pio(vcpu);
1928 if (r)
1929 goto out;
1930 }
1931
1932 if (vcpu->mmio_needed) {
1933 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
1934 vcpu->mmio_read_completed = 1;
1935 vcpu->mmio_needed = 0;
1936 r = emulate_instruction(vcpu, kvm_run,
1937 vcpu->mmio_fault_cr2, 0);
1938 if (r == EMULATE_DO_MMIO) {
1939 /*
1940 * Read-modify-write. Back to userspace.
1941 */
02c83209
AK
1942 r = 0;
1943 goto out;
46fc1477 1944 }
6aa8b732
AK
1945 }
1946
8eb7d334 1947 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
b4e63f56
AK
1948 kvm_arch_ops->cache_regs(vcpu);
1949 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
1950 kvm_arch_ops->decache_regs(vcpu);
1951 }
1952
6aa8b732
AK
1953 r = kvm_arch_ops->run(vcpu, kvm_run);
1954
039576c0 1955out:
1961d276
AK
1956 if (vcpu->sigset_active)
1957 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1958
6aa8b732
AK
1959 vcpu_put(vcpu);
1960 return r;
1961}
1962
bccf2150
AK
1963static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
1964 struct kvm_regs *regs)
6aa8b732 1965{
bccf2150 1966 vcpu_load(vcpu);
6aa8b732
AK
1967
1968 kvm_arch_ops->cache_regs(vcpu);
1969
1970 regs->rax = vcpu->regs[VCPU_REGS_RAX];
1971 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
1972 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
1973 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
1974 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
1975 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
1976 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
1977 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
05b3e0c2 1978#ifdef CONFIG_X86_64
6aa8b732
AK
1979 regs->r8 = vcpu->regs[VCPU_REGS_R8];
1980 regs->r9 = vcpu->regs[VCPU_REGS_R9];
1981 regs->r10 = vcpu->regs[VCPU_REGS_R10];
1982 regs->r11 = vcpu->regs[VCPU_REGS_R11];
1983 regs->r12 = vcpu->regs[VCPU_REGS_R12];
1984 regs->r13 = vcpu->regs[VCPU_REGS_R13];
1985 regs->r14 = vcpu->regs[VCPU_REGS_R14];
1986 regs->r15 = vcpu->regs[VCPU_REGS_R15];
1987#endif
1988
1989 regs->rip = vcpu->rip;
1990 regs->rflags = kvm_arch_ops->get_rflags(vcpu);
1991
1992 /*
1993 * Don't leak debug flags in case they were set for guest debugging
1994 */
1995 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
1996 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1997
1998 vcpu_put(vcpu);
1999
2000 return 0;
2001}
2002
bccf2150
AK
2003static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
2004 struct kvm_regs *regs)
6aa8b732 2005{
bccf2150 2006 vcpu_load(vcpu);
6aa8b732
AK
2007
2008 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
2009 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
2010 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
2011 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
2012 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
2013 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
2014 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
2015 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
05b3e0c2 2016#ifdef CONFIG_X86_64
6aa8b732
AK
2017 vcpu->regs[VCPU_REGS_R8] = regs->r8;
2018 vcpu->regs[VCPU_REGS_R9] = regs->r9;
2019 vcpu->regs[VCPU_REGS_R10] = regs->r10;
2020 vcpu->regs[VCPU_REGS_R11] = regs->r11;
2021 vcpu->regs[VCPU_REGS_R12] = regs->r12;
2022 vcpu->regs[VCPU_REGS_R13] = regs->r13;
2023 vcpu->regs[VCPU_REGS_R14] = regs->r14;
2024 vcpu->regs[VCPU_REGS_R15] = regs->r15;
2025#endif
2026
2027 vcpu->rip = regs->rip;
2028 kvm_arch_ops->set_rflags(vcpu, regs->rflags);
2029
2030 kvm_arch_ops->decache_regs(vcpu);
2031
2032 vcpu_put(vcpu);
2033
2034 return 0;
2035}
2036
2037static void get_segment(struct kvm_vcpu *vcpu,
2038 struct kvm_segment *var, int seg)
2039{
2040 return kvm_arch_ops->get_segment(vcpu, var, seg);
2041}
2042
bccf2150
AK
2043static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2044 struct kvm_sregs *sregs)
6aa8b732 2045{
6aa8b732
AK
2046 struct descriptor_table dt;
2047
bccf2150 2048 vcpu_load(vcpu);
6aa8b732
AK
2049
2050 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2051 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2052 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2053 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2054 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2055 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2056
2057 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2058 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2059
2060 kvm_arch_ops->get_idt(vcpu, &dt);
2061 sregs->idt.limit = dt.limit;
2062 sregs->idt.base = dt.base;
2063 kvm_arch_ops->get_gdt(vcpu, &dt);
2064 sregs->gdt.limit = dt.limit;
2065 sregs->gdt.base = dt.base;
2066
25c4c276 2067 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
6aa8b732
AK
2068 sregs->cr0 = vcpu->cr0;
2069 sregs->cr2 = vcpu->cr2;
2070 sregs->cr3 = vcpu->cr3;
2071 sregs->cr4 = vcpu->cr4;
2072 sregs->cr8 = vcpu->cr8;
2073 sregs->efer = vcpu->shadow_efer;
2074 sregs->apic_base = vcpu->apic_base;
2075
2076 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2077 sizeof sregs->interrupt_bitmap);
2078
2079 vcpu_put(vcpu);
2080
2081 return 0;
2082}
2083
2084static void set_segment(struct kvm_vcpu *vcpu,
2085 struct kvm_segment *var, int seg)
2086{
2087 return kvm_arch_ops->set_segment(vcpu, var, seg);
2088}
2089
bccf2150
AK
2090static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2091 struct kvm_sregs *sregs)
6aa8b732 2092{
6aa8b732
AK
2093 int mmu_reset_needed = 0;
2094 int i;
2095 struct descriptor_table dt;
2096
bccf2150 2097 vcpu_load(vcpu);
6aa8b732 2098
6aa8b732
AK
2099 dt.limit = sregs->idt.limit;
2100 dt.base = sregs->idt.base;
2101 kvm_arch_ops->set_idt(vcpu, &dt);
2102 dt.limit = sregs->gdt.limit;
2103 dt.base = sregs->gdt.base;
2104 kvm_arch_ops->set_gdt(vcpu, &dt);
2105
2106 vcpu->cr2 = sregs->cr2;
2107 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2108 vcpu->cr3 = sregs->cr3;
2109
2110 vcpu->cr8 = sregs->cr8;
2111
2112 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
05b3e0c2 2113#ifdef CONFIG_X86_64
6aa8b732
AK
2114 kvm_arch_ops->set_efer(vcpu, sregs->efer);
2115#endif
2116 vcpu->apic_base = sregs->apic_base;
2117
25c4c276 2118 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
399badf3 2119
6aa8b732 2120 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
f6528b03 2121 kvm_arch_ops->set_cr0(vcpu, sregs->cr0);
6aa8b732
AK
2122
2123 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
2124 kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
1b0973bd
AK
2125 if (!is_long_mode(vcpu) && is_pae(vcpu))
2126 load_pdptrs(vcpu, vcpu->cr3);
6aa8b732
AK
2127
2128 if (mmu_reset_needed)
2129 kvm_mmu_reset_context(vcpu);
2130
2131 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2132 sizeof vcpu->irq_pending);
2133 vcpu->irq_summary = 0;
9eb829ce 2134 for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
6aa8b732
AK
2135 if (vcpu->irq_pending[i])
2136 __set_bit(i, &vcpu->irq_summary);
2137
024aa1c0
AK
2138 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2139 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2140 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2141 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2142 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2143 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2144
2145 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2146 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2147
6aa8b732
AK
2148 vcpu_put(vcpu);
2149
2150 return 0;
2151}
2152
2153/*
2154 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2155 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
bf591b24
MR
2156 *
2157 * This list is modified at module load time to reflect the
2158 * capabilities of the host cpu.
6aa8b732
AK
2159 */
2160static u32 msrs_to_save[] = {
2161 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
2162 MSR_K6_STAR,
05b3e0c2 2163#ifdef CONFIG_X86_64
6aa8b732
AK
2164 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
2165#endif
2166 MSR_IA32_TIME_STAMP_COUNTER,
2167};
2168
bf591b24
MR
2169static unsigned num_msrs_to_save;
2170
6f00e68f
AK
2171static u32 emulated_msrs[] = {
2172 MSR_IA32_MISC_ENABLE,
2173};
2174
bf591b24
MR
2175static __init void kvm_init_msr_list(void)
2176{
2177 u32 dummy[2];
2178 unsigned i, j;
2179
2180 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2181 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2182 continue;
2183 if (j < i)
2184 msrs_to_save[j] = msrs_to_save[i];
2185 j++;
2186 }
2187 num_msrs_to_save = j;
2188}
6aa8b732
AK
2189
2190/*
2191 * Adapt set_msr() to msr_io()'s calling convention
2192 */
2193static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2194{
35f3f286 2195 return kvm_set_msr(vcpu, index, *data);
6aa8b732
AK
2196}
2197
2198/*
2199 * Read or write a bunch of msrs. All parameters are kernel addresses.
2200 *
2201 * @return number of msrs set successfully.
2202 */
bccf2150 2203static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
6aa8b732
AK
2204 struct kvm_msr_entry *entries,
2205 int (*do_msr)(struct kvm_vcpu *vcpu,
2206 unsigned index, u64 *data))
2207{
6aa8b732
AK
2208 int i;
2209
bccf2150 2210 vcpu_load(vcpu);
6aa8b732
AK
2211
2212 for (i = 0; i < msrs->nmsrs; ++i)
2213 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2214 break;
2215
2216 vcpu_put(vcpu);
2217
2218 return i;
2219}
2220
2221/*
2222 * Read or write a bunch of msrs. Parameters are user addresses.
2223 *
2224 * @return number of msrs set successfully.
2225 */
bccf2150 2226static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
6aa8b732
AK
2227 int (*do_msr)(struct kvm_vcpu *vcpu,
2228 unsigned index, u64 *data),
2229 int writeback)
2230{
2231 struct kvm_msrs msrs;
2232 struct kvm_msr_entry *entries;
2233 int r, n;
2234 unsigned size;
2235
2236 r = -EFAULT;
2237 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2238 goto out;
2239
2240 r = -E2BIG;
2241 if (msrs.nmsrs >= MAX_IO_MSRS)
2242 goto out;
2243
2244 r = -ENOMEM;
2245 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
2246 entries = vmalloc(size);
2247 if (!entries)
2248 goto out;
2249
2250 r = -EFAULT;
2251 if (copy_from_user(entries, user_msrs->entries, size))
2252 goto out_free;
2253
bccf2150 2254 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
6aa8b732
AK
2255 if (r < 0)
2256 goto out_free;
2257
2258 r = -EFAULT;
2259 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2260 goto out_free;
2261
2262 r = n;
2263
2264out_free:
2265 vfree(entries);
2266out:
2267 return r;
2268}
2269
2270/*
2271 * Translate a guest virtual address to a guest physical address.
2272 */
bccf2150
AK
2273static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2274 struct kvm_translation *tr)
6aa8b732
AK
2275{
2276 unsigned long vaddr = tr->linear_address;
6aa8b732
AK
2277 gpa_t gpa;
2278
bccf2150
AK
2279 vcpu_load(vcpu);
2280 spin_lock(&vcpu->kvm->lock);
6aa8b732
AK
2281 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2282 tr->physical_address = gpa;
2283 tr->valid = gpa != UNMAPPED_GVA;
2284 tr->writeable = 1;
2285 tr->usermode = 0;
bccf2150 2286 spin_unlock(&vcpu->kvm->lock);
6aa8b732
AK
2287 vcpu_put(vcpu);
2288
2289 return 0;
2290}
2291
bccf2150
AK
2292static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2293 struct kvm_interrupt *irq)
6aa8b732 2294{
6aa8b732
AK
2295 if (irq->irq < 0 || irq->irq >= 256)
2296 return -EINVAL;
bccf2150 2297 vcpu_load(vcpu);
6aa8b732
AK
2298
2299 set_bit(irq->irq, vcpu->irq_pending);
2300 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2301
2302 vcpu_put(vcpu);
2303
2304 return 0;
2305}
2306
bccf2150
AK
2307static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2308 struct kvm_debug_guest *dbg)
6aa8b732 2309{
6aa8b732
AK
2310 int r;
2311
bccf2150 2312 vcpu_load(vcpu);
6aa8b732
AK
2313
2314 r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
2315
2316 vcpu_put(vcpu);
2317
2318 return r;
2319}
2320
9a2bb7f4
AK
2321static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2322 unsigned long address,
2323 int *type)
2324{
2325 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2326 unsigned long pgoff;
2327 struct page *page;
2328
9a2bb7f4 2329 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
039576c0
AK
2330 if (pgoff == 0)
2331 page = virt_to_page(vcpu->run);
2332 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2333 page = virt_to_page(vcpu->pio_data);
2334 else
9a2bb7f4 2335 return NOPAGE_SIGBUS;
9a2bb7f4 2336 get_page(page);
cd0d9137
NAQ
2337 if (type != NULL)
2338 *type = VM_FAULT_MINOR;
2339
9a2bb7f4
AK
2340 return page;
2341}
2342
2343static struct vm_operations_struct kvm_vcpu_vm_ops = {
2344 .nopage = kvm_vcpu_nopage,
2345};
2346
2347static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2348{
2349 vma->vm_ops = &kvm_vcpu_vm_ops;
2350 return 0;
2351}
2352
bccf2150
AK
2353static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2354{
2355 struct kvm_vcpu *vcpu = filp->private_data;
2356
2357 fput(vcpu->kvm->filp);
2358 return 0;
2359}
2360
2361static struct file_operations kvm_vcpu_fops = {
2362 .release = kvm_vcpu_release,
2363 .unlocked_ioctl = kvm_vcpu_ioctl,
2364 .compat_ioctl = kvm_vcpu_ioctl,
9a2bb7f4 2365 .mmap = kvm_vcpu_mmap,
bccf2150
AK
2366};
2367
2368/*
2369 * Allocates an inode for the vcpu.
2370 */
2371static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2372{
2373 int fd, r;
2374 struct inode *inode;
2375 struct file *file;
2376
d6d28168
AK
2377 r = anon_inode_getfd(&fd, &inode, &file,
2378 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2379 if (r)
2380 return r;
bccf2150 2381 atomic_inc(&vcpu->kvm->filp->f_count);
bccf2150 2382 return fd;
bccf2150
AK
2383}
2384
c5ea7660
AK
2385/*
2386 * Creates some virtual cpus. Good luck creating more than one.
2387 */
2388static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2389{
2390 int r;
2391 struct kvm_vcpu *vcpu;
9a2bb7f4 2392 struct page *page;
c5ea7660
AK
2393
2394 r = -EINVAL;
2395 if (!valid_vcpu(n))
2396 goto out;
2397
2398 vcpu = &kvm->vcpus[n];
dad3795d 2399 vcpu->vcpu_id = n;
c5ea7660
AK
2400
2401 mutex_lock(&vcpu->mutex);
2402
2403 if (vcpu->vmcs) {
2404 mutex_unlock(&vcpu->mutex);
2405 return -EEXIST;
2406 }
2407
9a2bb7f4
AK
2408 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2409 r = -ENOMEM;
2410 if (!page)
2411 goto out_unlock;
2412 vcpu->run = page_address(page);
2413
039576c0
AK
2414 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2415 r = -ENOMEM;
2416 if (!page)
2417 goto out_free_run;
2418 vcpu->pio_data = page_address(page);
2419
c5ea7660
AK
2420 vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
2421 FX_IMAGE_ALIGN);
2422 vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
d917a6b9 2423 vcpu->cr0 = 0x10;
c5ea7660
AK
2424
2425 r = kvm_arch_ops->vcpu_create(vcpu);
2426 if (r < 0)
2427 goto out_free_vcpus;
2428
2429 r = kvm_mmu_create(vcpu);
2430 if (r < 0)
2431 goto out_free_vcpus;
2432
2433 kvm_arch_ops->vcpu_load(vcpu);
2434 r = kvm_mmu_setup(vcpu);
2435 if (r >= 0)
2436 r = kvm_arch_ops->vcpu_setup(vcpu);
2437 vcpu_put(vcpu);
2438
2439 if (r < 0)
2440 goto out_free_vcpus;
2441
bccf2150
AK
2442 r = create_vcpu_fd(vcpu);
2443 if (r < 0)
2444 goto out_free_vcpus;
2445
39c3b86e
AK
2446 spin_lock(&kvm_lock);
2447 if (n >= kvm->nvcpus)
2448 kvm->nvcpus = n + 1;
2449 spin_unlock(&kvm_lock);
2450
bccf2150 2451 return r;
c5ea7660
AK
2452
2453out_free_vcpus:
2454 kvm_free_vcpu(vcpu);
039576c0
AK
2455out_free_run:
2456 free_page((unsigned long)vcpu->run);
2457 vcpu->run = NULL;
9a2bb7f4 2458out_unlock:
c5ea7660
AK
2459 mutex_unlock(&vcpu->mutex);
2460out:
2461 return r;
2462}
2463
2cc51560
ED
2464static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
2465{
2466 u64 efer;
2467 int i;
2468 struct kvm_cpuid_entry *e, *entry;
2469
2470 rdmsrl(MSR_EFER, efer);
2471 entry = NULL;
2472 for (i = 0; i < vcpu->cpuid_nent; ++i) {
2473 e = &vcpu->cpuid_entries[i];
2474 if (e->function == 0x80000001) {
2475 entry = e;
2476 break;
2477 }
2478 }
4c981b43 2479 if (entry && (entry->edx & (1 << 20)) && !(efer & EFER_NX)) {
2cc51560 2480 entry->edx &= ~(1 << 20);
4c981b43 2481 printk(KERN_INFO "kvm: guest NX capability removed\n");
2cc51560
ED
2482 }
2483}
2484
06465c5a
AK
2485static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
2486 struct kvm_cpuid *cpuid,
2487 struct kvm_cpuid_entry __user *entries)
2488{
2489 int r;
2490
2491 r = -E2BIG;
2492 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2493 goto out;
2494 r = -EFAULT;
2495 if (copy_from_user(&vcpu->cpuid_entries, entries,
2496 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
2497 goto out;
2498 vcpu->cpuid_nent = cpuid->nent;
2cc51560 2499 cpuid_fix_nx_cap(vcpu);
06465c5a
AK
2500 return 0;
2501
2502out:
2503 return r;
2504}
2505
1961d276
AK
2506static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2507{
2508 if (sigset) {
2509 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2510 vcpu->sigset_active = 1;
2511 vcpu->sigset = *sigset;
2512 } else
2513 vcpu->sigset_active = 0;
2514 return 0;
2515}
2516
b8836737
AK
2517/*
2518 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2519 * we have asm/x86/processor.h
2520 */
2521struct fxsave {
2522 u16 cwd;
2523 u16 swd;
2524 u16 twd;
2525 u16 fop;
2526 u64 rip;
2527 u64 rdp;
2528 u32 mxcsr;
2529 u32 mxcsr_mask;
2530 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2531#ifdef CONFIG_X86_64
2532 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2533#else
2534 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2535#endif
2536};
2537
2538static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2539{
2540 struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
2541
2542 vcpu_load(vcpu);
2543
2544 memcpy(fpu->fpr, fxsave->st_space, 128);
2545 fpu->fcw = fxsave->cwd;
2546 fpu->fsw = fxsave->swd;
2547 fpu->ftwx = fxsave->twd;
2548 fpu->last_opcode = fxsave->fop;
2549 fpu->last_ip = fxsave->rip;
2550 fpu->last_dp = fxsave->rdp;
2551 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2552
2553 vcpu_put(vcpu);
2554
2555 return 0;
2556}
2557
2558static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2559{
2560 struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
2561
2562 vcpu_load(vcpu);
2563
2564 memcpy(fxsave->st_space, fpu->fpr, 128);
2565 fxsave->cwd = fpu->fcw;
2566 fxsave->swd = fpu->fsw;
2567 fxsave->twd = fpu->ftwx;
2568 fxsave->fop = fpu->last_opcode;
2569 fxsave->rip = fpu->last_ip;
2570 fxsave->rdp = fpu->last_dp;
2571 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2572
2573 vcpu_put(vcpu);
2574
2575 return 0;
2576}
2577
bccf2150
AK
2578static long kvm_vcpu_ioctl(struct file *filp,
2579 unsigned int ioctl, unsigned long arg)
6aa8b732 2580{
bccf2150 2581 struct kvm_vcpu *vcpu = filp->private_data;
2f366987 2582 void __user *argp = (void __user *)arg;
6aa8b732
AK
2583 int r = -EINVAL;
2584
2585 switch (ioctl) {
9a2bb7f4 2586 case KVM_RUN:
f0fe5108
AK
2587 r = -EINVAL;
2588 if (arg)
2589 goto out;
9a2bb7f4 2590 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
6aa8b732 2591 break;
6aa8b732
AK
2592 case KVM_GET_REGS: {
2593 struct kvm_regs kvm_regs;
2594
bccf2150
AK
2595 memset(&kvm_regs, 0, sizeof kvm_regs);
2596 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
6aa8b732
AK
2597 if (r)
2598 goto out;
2599 r = -EFAULT;
2f366987 2600 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
6aa8b732
AK
2601 goto out;
2602 r = 0;
2603 break;
2604 }
2605 case KVM_SET_REGS: {
2606 struct kvm_regs kvm_regs;
2607
2608 r = -EFAULT;
2f366987 2609 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
6aa8b732 2610 goto out;
bccf2150 2611 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
6aa8b732
AK
2612 if (r)
2613 goto out;
2614 r = 0;
2615 break;
2616 }
2617 case KVM_GET_SREGS: {
2618 struct kvm_sregs kvm_sregs;
2619
bccf2150
AK
2620 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2621 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
6aa8b732
AK
2622 if (r)
2623 goto out;
2624 r = -EFAULT;
2f366987 2625 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
6aa8b732
AK
2626 goto out;
2627 r = 0;
2628 break;
2629 }
2630 case KVM_SET_SREGS: {
2631 struct kvm_sregs kvm_sregs;
2632
2633 r = -EFAULT;
2f366987 2634 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
6aa8b732 2635 goto out;
bccf2150 2636 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
6aa8b732
AK
2637 if (r)
2638 goto out;
2639 r = 0;
2640 break;
2641 }
2642 case KVM_TRANSLATE: {
2643 struct kvm_translation tr;
2644
2645 r = -EFAULT;
2f366987 2646 if (copy_from_user(&tr, argp, sizeof tr))
6aa8b732 2647 goto out;
bccf2150 2648 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
6aa8b732
AK
2649 if (r)
2650 goto out;
2651 r = -EFAULT;
2f366987 2652 if (copy_to_user(argp, &tr, sizeof tr))
6aa8b732
AK
2653 goto out;
2654 r = 0;
2655 break;
2656 }
2657 case KVM_INTERRUPT: {
2658 struct kvm_interrupt irq;
2659
2660 r = -EFAULT;
2f366987 2661 if (copy_from_user(&irq, argp, sizeof irq))
6aa8b732 2662 goto out;
bccf2150 2663 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
6aa8b732
AK
2664 if (r)
2665 goto out;
2666 r = 0;
2667 break;
2668 }
2669 case KVM_DEBUG_GUEST: {
2670 struct kvm_debug_guest dbg;
2671
2672 r = -EFAULT;
2f366987 2673 if (copy_from_user(&dbg, argp, sizeof dbg))
6aa8b732 2674 goto out;
bccf2150 2675 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
6aa8b732
AK
2676 if (r)
2677 goto out;
2678 r = 0;
2679 break;
2680 }
bccf2150 2681 case KVM_GET_MSRS:
35f3f286 2682 r = msr_io(vcpu, argp, kvm_get_msr, 1);
bccf2150
AK
2683 break;
2684 case KVM_SET_MSRS:
2685 r = msr_io(vcpu, argp, do_set_msr, 0);
2686 break;
06465c5a
AK
2687 case KVM_SET_CPUID: {
2688 struct kvm_cpuid __user *cpuid_arg = argp;
2689 struct kvm_cpuid cpuid;
2690
2691 r = -EFAULT;
2692 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2693 goto out;
2694 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2695 if (r)
2696 goto out;
2697 break;
2698 }
1961d276
AK
2699 case KVM_SET_SIGNAL_MASK: {
2700 struct kvm_signal_mask __user *sigmask_arg = argp;
2701 struct kvm_signal_mask kvm_sigmask;
2702 sigset_t sigset, *p;
2703
2704 p = NULL;
2705 if (argp) {
2706 r = -EFAULT;
2707 if (copy_from_user(&kvm_sigmask, argp,
2708 sizeof kvm_sigmask))
2709 goto out;
2710 r = -EINVAL;
2711 if (kvm_sigmask.len != sizeof sigset)
2712 goto out;
2713 r = -EFAULT;
2714 if (copy_from_user(&sigset, sigmask_arg->sigset,
2715 sizeof sigset))
2716 goto out;
2717 p = &sigset;
2718 }
2719 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2720 break;
2721 }
b8836737
AK
2722 case KVM_GET_FPU: {
2723 struct kvm_fpu fpu;
2724
2725 memset(&fpu, 0, sizeof fpu);
2726 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2727 if (r)
2728 goto out;
2729 r = -EFAULT;
2730 if (copy_to_user(argp, &fpu, sizeof fpu))
2731 goto out;
2732 r = 0;
2733 break;
2734 }
2735 case KVM_SET_FPU: {
2736 struct kvm_fpu fpu;
2737
2738 r = -EFAULT;
2739 if (copy_from_user(&fpu, argp, sizeof fpu))
2740 goto out;
2741 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2742 if (r)
2743 goto out;
2744 r = 0;
2745 break;
2746 }
bccf2150
AK
2747 default:
2748 ;
2749 }
2750out:
2751 return r;
2752}
2753
2754static long kvm_vm_ioctl(struct file *filp,
2755 unsigned int ioctl, unsigned long arg)
2756{
2757 struct kvm *kvm = filp->private_data;
2758 void __user *argp = (void __user *)arg;
2759 int r = -EINVAL;
2760
2761 switch (ioctl) {
2762 case KVM_CREATE_VCPU:
2763 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2764 if (r < 0)
2765 goto out;
2766 break;
6aa8b732
AK
2767 case KVM_SET_MEMORY_REGION: {
2768 struct kvm_memory_region kvm_mem;
2769
2770 r = -EFAULT;
2f366987 2771 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
6aa8b732 2772 goto out;
2c6f5df9 2773 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
6aa8b732
AK
2774 if (r)
2775 goto out;
2776 break;
2777 }
2778 case KVM_GET_DIRTY_LOG: {
2779 struct kvm_dirty_log log;
2780
2781 r = -EFAULT;
2f366987 2782 if (copy_from_user(&log, argp, sizeof log))
6aa8b732 2783 goto out;
2c6f5df9 2784 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
6aa8b732
AK
2785 if (r)
2786 goto out;
2787 break;
2788 }
e8207547
AK
2789 case KVM_SET_MEMORY_ALIAS: {
2790 struct kvm_memory_alias alias;
2791
2792 r = -EFAULT;
2793 if (copy_from_user(&alias, argp, sizeof alias))
2794 goto out;
2795 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
2796 if (r)
2797 goto out;
2798 break;
2799 }
f17abe9a
AK
2800 default:
2801 ;
2802 }
2803out:
2804 return r;
2805}
2806
2807static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
2808 unsigned long address,
2809 int *type)
2810{
2811 struct kvm *kvm = vma->vm_file->private_data;
2812 unsigned long pgoff;
f17abe9a
AK
2813 struct page *page;
2814
f17abe9a 2815 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
954bbbc2 2816 page = gfn_to_page(kvm, pgoff);
f17abe9a
AK
2817 if (!page)
2818 return NOPAGE_SIGBUS;
2819 get_page(page);
cd0d9137
NAQ
2820 if (type != NULL)
2821 *type = VM_FAULT_MINOR;
2822
f17abe9a
AK
2823 return page;
2824}
2825
2826static struct vm_operations_struct kvm_vm_vm_ops = {
2827 .nopage = kvm_vm_nopage,
2828};
2829
2830static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2831{
2832 vma->vm_ops = &kvm_vm_vm_ops;
2833 return 0;
2834}
2835
2836static struct file_operations kvm_vm_fops = {
2837 .release = kvm_vm_release,
2838 .unlocked_ioctl = kvm_vm_ioctl,
2839 .compat_ioctl = kvm_vm_ioctl,
2840 .mmap = kvm_vm_mmap,
2841};
2842
2843static int kvm_dev_ioctl_create_vm(void)
2844{
2845 int fd, r;
2846 struct inode *inode;
2847 struct file *file;
2848 struct kvm *kvm;
2849
f17abe9a 2850 kvm = kvm_create_vm();
d6d28168
AK
2851 if (IS_ERR(kvm))
2852 return PTR_ERR(kvm);
2853 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
2854 if (r) {
2855 kvm_destroy_vm(kvm);
2856 return r;
f17abe9a
AK
2857 }
2858
bccf2150 2859 kvm->filp = file;
f17abe9a 2860
f17abe9a 2861 return fd;
f17abe9a
AK
2862}
2863
2864static long kvm_dev_ioctl(struct file *filp,
2865 unsigned int ioctl, unsigned long arg)
2866{
2867 void __user *argp = (void __user *)arg;
07c45a36 2868 long r = -EINVAL;
f17abe9a
AK
2869
2870 switch (ioctl) {
2871 case KVM_GET_API_VERSION:
f0fe5108
AK
2872 r = -EINVAL;
2873 if (arg)
2874 goto out;
f17abe9a
AK
2875 r = KVM_API_VERSION;
2876 break;
2877 case KVM_CREATE_VM:
f0fe5108
AK
2878 r = -EINVAL;
2879 if (arg)
2880 goto out;
f17abe9a
AK
2881 r = kvm_dev_ioctl_create_vm();
2882 break;
6aa8b732 2883 case KVM_GET_MSR_INDEX_LIST: {
2f366987 2884 struct kvm_msr_list __user *user_msr_list = argp;
6aa8b732
AK
2885 struct kvm_msr_list msr_list;
2886 unsigned n;
2887
2888 r = -EFAULT;
2889 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
2890 goto out;
2891 n = msr_list.nmsrs;
6f00e68f 2892 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
6aa8b732
AK
2893 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
2894 goto out;
2895 r = -E2BIG;
bf591b24 2896 if (n < num_msrs_to_save)
6aa8b732
AK
2897 goto out;
2898 r = -EFAULT;
2899 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
bf591b24 2900 num_msrs_to_save * sizeof(u32)))
6aa8b732 2901 goto out;
6f00e68f
AK
2902 if (copy_to_user(user_msr_list->indices
2903 + num_msrs_to_save * sizeof(u32),
2904 &emulated_msrs,
2905 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
2906 goto out;
6aa8b732 2907 r = 0;
cc1d8955 2908 break;
6aa8b732 2909 }
5d308f45
AK
2910 case KVM_CHECK_EXTENSION:
2911 /*
2912 * No extensions defined at present.
2913 */
2914 r = 0;
2915 break;
07c45a36
AK
2916 case KVM_GET_VCPU_MMAP_SIZE:
2917 r = -EINVAL;
2918 if (arg)
2919 goto out;
039576c0 2920 r = 2 * PAGE_SIZE;
07c45a36 2921 break;
6aa8b732
AK
2922 default:
2923 ;
2924 }
2925out:
2926 return r;
2927}
2928
6aa8b732
AK
2929static struct file_operations kvm_chardev_ops = {
2930 .open = kvm_dev_open,
2931 .release = kvm_dev_release,
2932 .unlocked_ioctl = kvm_dev_ioctl,
2933 .compat_ioctl = kvm_dev_ioctl,
6aa8b732
AK
2934};
2935
2936static struct miscdevice kvm_dev = {
bbe4432e 2937 KVM_MINOR,
6aa8b732
AK
2938 "kvm",
2939 &kvm_chardev_ops,
2940};
2941
774c47f1
AK
2942/*
2943 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2944 * cached on it.
2945 */
2946static void decache_vcpus_on_cpu(int cpu)
2947{
2948 struct kvm *vm;
2949 struct kvm_vcpu *vcpu;
2950 int i;
2951
2952 spin_lock(&kvm_lock);
2953 list_for_each_entry(vm, &vm_list, vm_list)
2954 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2955 vcpu = &vm->vcpus[i];
2956 /*
2957 * If the vcpu is locked, then it is running on some
2958 * other cpu and therefore it is not cached on the
2959 * cpu in question.
2960 *
2961 * If it's not locked, check the last cpu it executed
2962 * on.
2963 */
2964 if (mutex_trylock(&vcpu->mutex)) {
2965 if (vcpu->cpu == cpu) {
2966 kvm_arch_ops->vcpu_decache(vcpu);
2967 vcpu->cpu = -1;
2968 }
2969 mutex_unlock(&vcpu->mutex);
2970 }
2971 }
2972 spin_unlock(&kvm_lock);
2973}
2974
1b6c0168
AK
2975static void hardware_enable(void *junk)
2976{
2977 int cpu = raw_smp_processor_id();
2978
2979 if (cpu_isset(cpu, cpus_hardware_enabled))
2980 return;
2981 cpu_set(cpu, cpus_hardware_enabled);
2982 kvm_arch_ops->hardware_enable(NULL);
2983}
2984
2985static void hardware_disable(void *junk)
2986{
2987 int cpu = raw_smp_processor_id();
2988
2989 if (!cpu_isset(cpu, cpus_hardware_enabled))
2990 return;
2991 cpu_clear(cpu, cpus_hardware_enabled);
2992 decache_vcpus_on_cpu(cpu);
2993 kvm_arch_ops->hardware_disable(NULL);
2994}
2995
774c47f1
AK
2996static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2997 void *v)
2998{
2999 int cpu = (long)v;
3000
3001 switch (val) {
cec9ad27
AK
3002 case CPU_DYING:
3003 case CPU_DYING_FROZEN:
6ec8a856
AK
3004 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3005 cpu);
3006 hardware_disable(NULL);
3007 break;
774c47f1 3008 case CPU_UP_CANCELED:
8bb78442 3009 case CPU_UP_CANCELED_FROZEN:
43934a38
JK
3010 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3011 cpu);
1b6c0168 3012 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
774c47f1 3013 break;
43934a38 3014 case CPU_ONLINE:
8bb78442 3015 case CPU_ONLINE_FROZEN:
43934a38
JK
3016 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
3017 cpu);
1b6c0168 3018 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
774c47f1
AK
3019 break;
3020 }
3021 return NOTIFY_OK;
3022}
3023
9a2b85c6
RR
3024static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
3025 void *v)
3026{
3027 if (val == SYS_RESTART) {
3028 /*
3029 * Some (well, at least mine) BIOSes hang on reboot if
3030 * in vmx root mode.
3031 */
3032 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
3033 on_each_cpu(hardware_disable, NULL, 0, 1);
3034 }
3035 return NOTIFY_OK;
3036}
3037
3038static struct notifier_block kvm_reboot_notifier = {
3039 .notifier_call = kvm_reboot,
3040 .priority = 0,
3041};
3042
2eeb2e94
GH
3043void kvm_io_bus_init(struct kvm_io_bus *bus)
3044{
3045 memset(bus, 0, sizeof(*bus));
3046}
3047
3048void kvm_io_bus_destroy(struct kvm_io_bus *bus)
3049{
3050 int i;
3051
3052 for (i = 0; i < bus->dev_count; i++) {
3053 struct kvm_io_device *pos = bus->devs[i];
3054
3055 kvm_iodevice_destructor(pos);
3056 }
3057}
3058
3059struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
3060{
3061 int i;
3062
3063 for (i = 0; i < bus->dev_count; i++) {
3064 struct kvm_io_device *pos = bus->devs[i];
3065
3066 if (pos->in_range(pos, addr))
3067 return pos;
3068 }
3069
3070 return NULL;
3071}
3072
3073void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
3074{
3075 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
3076
3077 bus->devs[bus->dev_count++] = dev;
3078}
3079
774c47f1
AK
3080static struct notifier_block kvm_cpu_notifier = {
3081 .notifier_call = kvm_cpu_hotplug,
3082 .priority = 20, /* must be > scheduler priority */
3083};
3084
1165f5fe
AK
3085static u64 stat_get(void *_offset)
3086{
3087 unsigned offset = (long)_offset;
3088 u64 total = 0;
3089 struct kvm *kvm;
3090 struct kvm_vcpu *vcpu;
3091 int i;
3092
3093 spin_lock(&kvm_lock);
3094 list_for_each_entry(kvm, &vm_list, vm_list)
3095 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
3096 vcpu = &kvm->vcpus[i];
3097 total += *(u32 *)((void *)vcpu + offset);
3098 }
3099 spin_unlock(&kvm_lock);
3100 return total;
3101}
3102
3103static void stat_set(void *offset, u64 val)
3104{
3105}
3106
3107DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, stat_set, "%llu\n");
3108
6aa8b732
AK
3109static __init void kvm_init_debug(void)
3110{
3111 struct kvm_stats_debugfs_item *p;
3112
8b6d44c7 3113 debugfs_dir = debugfs_create_dir("kvm", NULL);
6aa8b732 3114 for (p = debugfs_entries; p->name; ++p)
1165f5fe
AK
3115 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
3116 (void *)(long)p->offset,
3117 &stat_fops);
6aa8b732
AK
3118}
3119
3120static void kvm_exit_debug(void)
3121{
3122 struct kvm_stats_debugfs_item *p;
3123
3124 for (p = debugfs_entries; p->name; ++p)
3125 debugfs_remove(p->dentry);
3126 debugfs_remove(debugfs_dir);
3127}
3128
59ae6c6b
AK
3129static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3130{
4267c41a 3131 hardware_disable(NULL);
59ae6c6b
AK
3132 return 0;
3133}
3134
3135static int kvm_resume(struct sys_device *dev)
3136{
4267c41a 3137 hardware_enable(NULL);
59ae6c6b
AK
3138 return 0;
3139}
3140
3141static struct sysdev_class kvm_sysdev_class = {
3142 set_kset_name("kvm"),
3143 .suspend = kvm_suspend,
3144 .resume = kvm_resume,
3145};
3146
3147static struct sys_device kvm_sysdev = {
3148 .id = 0,
3149 .cls = &kvm_sysdev_class,
3150};
3151
6aa8b732
AK
3152hpa_t bad_page_address;
3153
3154int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
3155{
3156 int r;
3157
09db28b8
YI
3158 if (kvm_arch_ops) {
3159 printk(KERN_ERR "kvm: already loaded the other module\n");
3160 return -EEXIST;
3161 }
3162
e097f35c 3163 if (!ops->cpu_has_kvm_support()) {
6aa8b732
AK
3164 printk(KERN_ERR "kvm: no hardware support\n");
3165 return -EOPNOTSUPP;
3166 }
e097f35c 3167 if (ops->disabled_by_bios()) {
6aa8b732
AK
3168 printk(KERN_ERR "kvm: disabled by bios\n");
3169 return -EOPNOTSUPP;
3170 }
3171
e097f35c
YI
3172 kvm_arch_ops = ops;
3173
6aa8b732
AK
3174 r = kvm_arch_ops->hardware_setup();
3175 if (r < 0)
ca45aaae 3176 goto out;
6aa8b732 3177
1b6c0168 3178 on_each_cpu(hardware_enable, NULL, 0, 1);
774c47f1
AK
3179 r = register_cpu_notifier(&kvm_cpu_notifier);
3180 if (r)
3181 goto out_free_1;
6aa8b732
AK
3182 register_reboot_notifier(&kvm_reboot_notifier);
3183
59ae6c6b
AK
3184 r = sysdev_class_register(&kvm_sysdev_class);
3185 if (r)
3186 goto out_free_2;
3187
3188 r = sysdev_register(&kvm_sysdev);
3189 if (r)
3190 goto out_free_3;
3191
6aa8b732
AK
3192 kvm_chardev_ops.owner = module;
3193
3194 r = misc_register(&kvm_dev);
3195 if (r) {
3196 printk (KERN_ERR "kvm: misc device register failed\n");
3197 goto out_free;
3198 }
3199
3200 return r;
3201
3202out_free:
59ae6c6b
AK
3203 sysdev_unregister(&kvm_sysdev);
3204out_free_3:
3205 sysdev_class_unregister(&kvm_sysdev_class);
3206out_free_2:
6aa8b732 3207 unregister_reboot_notifier(&kvm_reboot_notifier);
774c47f1
AK
3208 unregister_cpu_notifier(&kvm_cpu_notifier);
3209out_free_1:
1b6c0168 3210 on_each_cpu(hardware_disable, NULL, 0, 1);
6aa8b732 3211 kvm_arch_ops->hardware_unsetup();
ca45aaae
AK
3212out:
3213 kvm_arch_ops = NULL;
6aa8b732
AK
3214 return r;
3215}
3216
3217void kvm_exit_arch(void)
3218{
3219 misc_deregister(&kvm_dev);
59ae6c6b
AK
3220 sysdev_unregister(&kvm_sysdev);
3221 sysdev_class_unregister(&kvm_sysdev_class);
6aa8b732 3222 unregister_reboot_notifier(&kvm_reboot_notifier);
59ae6c6b 3223 unregister_cpu_notifier(&kvm_cpu_notifier);
1b6c0168 3224 on_each_cpu(hardware_disable, NULL, 0, 1);
6aa8b732 3225 kvm_arch_ops->hardware_unsetup();
09db28b8 3226 kvm_arch_ops = NULL;
6aa8b732
AK
3227}
3228
3229static __init int kvm_init(void)
3230{
3231 static struct page *bad_page;
37e29d90
AK
3232 int r;
3233
b5a33a75
AK
3234 r = kvm_mmu_module_init();
3235 if (r)
3236 goto out4;
3237
6aa8b732
AK
3238 kvm_init_debug();
3239
bf591b24
MR
3240 kvm_init_msr_list();
3241
6aa8b732
AK
3242 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
3243 r = -ENOMEM;
3244 goto out;
3245 }
3246
3247 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
3248 memset(__va(bad_page_address), 0, PAGE_SIZE);
3249
58e690e6 3250 return 0;
6aa8b732
AK
3251
3252out:
3253 kvm_exit_debug();
b5a33a75
AK
3254 kvm_mmu_module_exit();
3255out4:
6aa8b732
AK
3256 return r;
3257}
3258
3259static __exit void kvm_exit(void)
3260{
3261 kvm_exit_debug();
3262 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
b5a33a75 3263 kvm_mmu_module_exit();
6aa8b732
AK
3264}
3265
3266module_init(kvm_init)
3267module_exit(kvm_exit)
3268
3269EXPORT_SYMBOL_GPL(kvm_init_arch);
3270EXPORT_SYMBOL_GPL(kvm_exit_arch);