blob: f86a47c2f2553fbf5cb5a1ddfbfe35ade351cbd8 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
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"
Carsten Otte043405e2007-10-10 17:16:19 +020019#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040020#include "x86_emulate.h"
21#include "segment_descriptor.h"
Eddie Dong85f455f2007-07-06 12:20:49 +030022#include "irq.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080023
24#include <linux/kvm.h>
25#include <linux/module.h>
26#include <linux/errno.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/percpu.h>
28#include <linux/gfp.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080029#include <linux/mm.h>
30#include <linux/miscdevice.h>
31#include <linux/vmalloc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080032#include <linux/reboot.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080033#include <linux/debugfs.h>
34#include <linux/highmem.h>
35#include <linux/file.h>
Avi Kivity59ae6c62007-02-12 00:54:48 -080036#include <linux/sysdev.h>
Avi Kivity774c47f2007-02-12 00:54:47 -080037#include <linux/cpu.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040038#include <linux/sched.h>
Avi Kivityd9e368d2007-06-07 19:18:30 +030039#include <linux/cpumask.h>
40#include <linux/smp.h>
Avi Kivityd6d28162007-06-28 08:38:16 -040041#include <linux/anon_inodes.h>
Avi Kivity04d2cc72007-09-10 18:10:54 +030042#include <linux/profile.h>
Anthony Liguori7aa81cc2007-09-17 14:57:50 -050043#include <linux/kvm_para.h>
Izik Eidus6fc138d2007-10-09 19:20:39 +020044#include <linux/pagemap.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080045
Avi Kivitye4956062007-06-28 14:15:57 -040046#include <asm/processor.h>
47#include <asm/msr.h>
48#include <asm/io.h>
49#include <asm/uaccess.h>
50#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080051
52MODULE_AUTHOR("Qumranet");
53MODULE_LICENSE("GPL");
54
Avi Kivity133de902007-02-12 00:54:44 -080055static DEFINE_SPINLOCK(kvm_lock);
56static LIST_HEAD(vm_list);
57
Avi Kivity1b6c0162007-05-24 13:03:52 +030058static cpumask_t cpus_hardware_enabled;
59
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +030060struct kvm_x86_ops *kvm_x86_ops;
Rusty Russellc16f8622007-07-30 21:12:19 +100061struct kmem_cache *kvm_vcpu_cache;
62EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030063
Avi Kivity15ad7142007-07-11 18:17:21 +030064static __read_mostly struct preempt_ops kvm_preempt_ops;
65
Avi Kivity1165f5f2007-04-19 17:27:43 +030066#define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
Avi Kivity6aa8b732006-12-10 02:21:36 -080067
68static struct kvm_stats_debugfs_item {
69 const char *name;
Avi Kivity1165f5f2007-04-19 17:27:43 +030070 int offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -080071 struct dentry *dentry;
72} debugfs_entries[] = {
Avi Kivity1165f5f2007-04-19 17:27:43 +030073 { "pf_fixed", STAT_OFFSET(pf_fixed) },
74 { "pf_guest", STAT_OFFSET(pf_guest) },
75 { "tlb_flush", STAT_OFFSET(tlb_flush) },
76 { "invlpg", STAT_OFFSET(invlpg) },
77 { "exits", STAT_OFFSET(exits) },
78 { "io_exits", STAT_OFFSET(io_exits) },
79 { "mmio_exits", STAT_OFFSET(mmio_exits) },
80 { "signal_exits", STAT_OFFSET(signal_exits) },
81 { "irq_window", STAT_OFFSET(irq_window_exits) },
82 { "halt_exits", STAT_OFFSET(halt_exits) },
Eddie Dongb6958ce2007-07-18 12:15:21 +030083 { "halt_wakeup", STAT_OFFSET(halt_wakeup) },
Avi Kivity1165f5f2007-04-19 17:27:43 +030084 { "request_irq", STAT_OFFSET(request_irq_exits) },
85 { "irq_exits", STAT_OFFSET(irq_exits) },
Avi Kivitye6adf282007-04-30 16:07:54 +030086 { "light_exits", STAT_OFFSET(light_exits) },
Eddie Dong2cc51562007-05-21 07:28:09 +030087 { "efer_reload", STAT_OFFSET(efer_reload) },
Avi Kivity1165f5f2007-04-19 17:27:43 +030088 { NULL }
Avi Kivity6aa8b732006-12-10 02:21:36 -080089};
90
91static struct dentry *debugfs_dir;
92
Rusty Russell707d92f2007-07-17 23:19:08 +100093#define CR0_RESERVED_BITS \
94 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
95 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
96 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
Rusty Russell66aee912007-07-17 23:34:16 +100097#define CR4_RESERVED_BITS \
98 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
99 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
100 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
101 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
102
Rusty Russell7075bc82007-07-17 23:37:17 +1000103#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800104#define EFER_RESERVED_BITS 0xfffffffffffff2fe
105
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800106#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400107/* LDT or TSS descriptor in the GDT. 16 bytes. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800108struct segment_descriptor_64 {
109 struct segment_descriptor s;
110 u32 base_higher;
111 u32 pad_zero;
112};
113
114#endif
115
Avi Kivitybccf2152007-02-21 18:04:26 +0200116static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
117 unsigned long arg);
118
Avi Kivity6aa8b732006-12-10 02:21:36 -0800119unsigned long segment_base(u16 selector)
120{
121 struct descriptor_table gdt;
122 struct segment_descriptor *d;
123 unsigned long table_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800124 unsigned long v;
125
126 if (selector == 0)
127 return 0;
128
Mike Dayd77c26f2007-10-08 09:02:08 -0400129 asm("sgdt %0" : "=m"(gdt));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800130 table_base = gdt.base;
131
132 if (selector & 4) { /* from ldt */
133 u16 ldt_selector;
134
Mike Dayd77c26f2007-10-08 09:02:08 -0400135 asm("sldt %0" : "=g"(ldt_selector));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800136 table_base = segment_base(ldt_selector);
137 }
138 d = (struct segment_descriptor *)(table_base + (selector & ~7));
Mike Dayd77c26f2007-10-08 09:02:08 -0400139 v = d->base_low | ((unsigned long)d->base_mid << 16) |
140 ((unsigned long)d->base_high << 24);
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800141#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400142 if (d->system == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
143 v |= ((unsigned long) \
144 ((struct segment_descriptor_64 *)d)->base_higher) << 32;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800145#endif
146 return v;
147}
148EXPORT_SYMBOL_GPL(segment_base);
149
James Morris5aacf0c2006-12-22 01:04:55 -0800150static inline int valid_vcpu(int n)
151{
152 return likely(n >= 0 && n < KVM_MAX_VCPUS);
153}
154
Avi Kivity7702fd12007-06-14 16:27:40 +0300155void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
156{
157 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
158 return;
159
160 vcpu->guest_fpu_loaded = 1;
Rusty Russellb114b082007-07-30 21:13:43 +1000161 fx_save(&vcpu->host_fx_image);
162 fx_restore(&vcpu->guest_fx_image);
Avi Kivity7702fd12007-06-14 16:27:40 +0300163}
164EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
165
166void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
167{
168 if (!vcpu->guest_fpu_loaded)
169 return;
170
171 vcpu->guest_fpu_loaded = 0;
Rusty Russellb114b082007-07-30 21:13:43 +1000172 fx_save(&vcpu->guest_fx_image);
173 fx_restore(&vcpu->host_fx_image);
Avi Kivity7702fd12007-06-14 16:27:40 +0300174}
175EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
176
Avi Kivity6aa8b732006-12-10 02:21:36 -0800177/*
178 * Switches to specified vcpu, until a matching vcpu_put()
179 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200180void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800181{
Avi Kivity15ad7142007-07-11 18:17:21 +0300182 int cpu;
183
Avi Kivitybccf2152007-02-21 18:04:26 +0200184 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300185 cpu = get_cpu();
186 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200187 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300188 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200189}
190
Carsten Otte313a3dc2007-10-11 19:16:52 +0200191void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800192{
Avi Kivity15ad7142007-07-11 18:17:21 +0300193 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200194 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300195 preempt_notifier_unregister(&vcpu->preempt_notifier);
196 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800197 mutex_unlock(&vcpu->mutex);
198}
199
Avi Kivityd9e368d2007-06-07 19:18:30 +0300200static void ack_flush(void *_completed)
201{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300202}
203
204void kvm_flush_remote_tlbs(struct kvm *kvm)
205{
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200206 int i, cpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300207 cpumask_t cpus;
208 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300209
Avi Kivityd9e368d2007-06-07 19:18:30 +0300210 cpus_clear(cpus);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000211 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
212 vcpu = kvm->vcpus[i];
213 if (!vcpu)
214 continue;
Avi Kivity3176bc32007-10-16 17:22:08 +0200215 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300216 continue;
217 cpu = vcpu->cpu;
218 if (cpu != -1 && cpu != raw_smp_processor_id())
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200219 cpu_set(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300220 }
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200221 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300222}
223
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000224int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
225{
226 struct page *page;
227 int r;
228
229 mutex_init(&vcpu->mutex);
230 vcpu->cpu = -1;
231 vcpu->mmu.root_hpa = INVALID_PAGE;
232 vcpu->kvm = kvm;
233 vcpu->vcpu_id = id;
He, Qingc5ec1532007-09-03 17:07:41 +0300234 if (!irqchip_in_kernel(kvm) || id == 0)
235 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
236 else
237 vcpu->mp_state = VCPU_MP_STATE_UNINITIALIZED;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300238 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000239
240 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
241 if (!page) {
242 r = -ENOMEM;
243 goto fail;
244 }
245 vcpu->run = page_address(page);
246
247 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
248 if (!page) {
249 r = -ENOMEM;
250 goto fail_free_run;
251 }
252 vcpu->pio_data = page_address(page);
253
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000254 r = kvm_mmu_create(vcpu);
255 if (r < 0)
256 goto fail_free_pio_data;
257
Rusty Russell76fafa52007-10-08 10:50:48 +1000258 if (irqchip_in_kernel(kvm)) {
259 r = kvm_create_lapic(vcpu);
260 if (r < 0)
261 goto fail_mmu_destroy;
262 }
263
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000264 return 0;
265
Rusty Russell76fafa52007-10-08 10:50:48 +1000266fail_mmu_destroy:
267 kvm_mmu_destroy(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000268fail_free_pio_data:
269 free_page((unsigned long)vcpu->pio_data);
270fail_free_run:
271 free_page((unsigned long)vcpu->run);
272fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000273 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000274}
275EXPORT_SYMBOL_GPL(kvm_vcpu_init);
276
277void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
278{
Rusty Russelld5894442007-10-08 10:48:30 +1000279 kvm_free_lapic(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000280 kvm_mmu_destroy(vcpu);
281 free_page((unsigned long)vcpu->pio_data);
282 free_page((unsigned long)vcpu->run);
283}
284EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
285
Avi Kivityf17abe92007-02-21 19:28:04 +0200286static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800287{
288 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800289
290 if (!kvm)
Avi Kivityf17abe92007-02-21 19:28:04 +0200291 return ERR_PTR(-ENOMEM);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800292
Eddie Dong74906342007-06-19 18:05:03 +0300293 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800294 mutex_init(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800295 INIT_LIST_HEAD(&kvm->active_mmu_pages);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400296 kvm_io_bus_init(&kvm->mmio_bus);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000297 spin_lock(&kvm_lock);
298 list_add(&kvm->vm_list, &vm_list);
299 spin_unlock(&kvm_lock);
Avi Kivityf17abe92007-02-21 19:28:04 +0200300 return kvm;
301}
302
Izik Eidus6fc138d2007-10-09 19:20:39 +0200303static void kvm_free_kernel_physmem(struct kvm_memory_slot *free)
304{
305 int i;
306
307 for (i = 0; i < free->npages; ++i)
308 if (free->phys_mem[i])
309 __free_page(free->phys_mem[i]);
310}
311
Avi Kivity6aa8b732006-12-10 02:21:36 -0800312/*
313 * Free any memory in @free but not in @dont.
314 */
315static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
316 struct kvm_memory_slot *dont)
317{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800318 if (!dont || free->phys_mem != dont->phys_mem)
319 if (free->phys_mem) {
Izik Eidus8a7ae052007-10-18 11:09:33 +0200320 if (!free->user_alloc)
Izik Eidus6fc138d2007-10-09 19:20:39 +0200321 kvm_free_kernel_physmem(free);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800322 vfree(free->phys_mem);
323 }
Izik Eidus290fc382007-09-27 14:11:22 +0200324 if (!dont || free->rmap != dont->rmap)
325 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800326
327 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
328 vfree(free->dirty_bitmap);
329
Al Viro8b6d44c2007-02-09 16:38:40 +0000330 free->phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800331 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000332 free->dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800333}
334
335static void kvm_free_physmem(struct kvm *kvm)
336{
337 int i;
338
339 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000340 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800341}
342
Avi Kivity039576c2007-03-20 12:46:50 +0200343static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
344{
345 int i;
346
Rusty Russell3077c4512007-07-30 16:41:57 +1000347 for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i)
Avi Kivity039576c2007-03-20 12:46:50 +0200348 if (vcpu->pio.guest_pages[i]) {
Izik Eidus8a7ae052007-10-18 11:09:33 +0200349 kvm_release_page(vcpu->pio.guest_pages[i]);
Avi Kivity039576c2007-03-20 12:46:50 +0200350 vcpu->pio.guest_pages[i] = NULL;
351 }
352}
353
Avi Kivity7b53aa52007-06-05 12:17:03 +0300354static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
355{
Avi Kivity7b53aa52007-06-05 12:17:03 +0300356 vcpu_load(vcpu);
357 kvm_mmu_unload(vcpu);
358 vcpu_put(vcpu);
359}
360
Avi Kivity6aa8b732006-12-10 02:21:36 -0800361static void kvm_free_vcpus(struct kvm *kvm)
362{
363 unsigned int i;
364
Avi Kivity7b53aa52007-06-05 12:17:03 +0300365 /*
366 * Unpin any mmu pages first.
367 */
368 for (i = 0; i < KVM_MAX_VCPUS; ++i)
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000369 if (kvm->vcpus[i])
370 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
371 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
372 if (kvm->vcpus[i]) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300373 kvm_x86_ops->vcpu_free(kvm->vcpus[i]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000374 kvm->vcpus[i] = NULL;
375 }
376 }
377
Avi Kivity6aa8b732006-12-10 02:21:36 -0800378}
379
Avi Kivityf17abe92007-02-21 19:28:04 +0200380static void kvm_destroy_vm(struct kvm *kvm)
381{
Avi Kivity133de902007-02-12 00:54:44 -0800382 spin_lock(&kvm_lock);
383 list_del(&kvm->vm_list);
384 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300385 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400386 kvm_io_bus_destroy(&kvm->mmio_bus);
Eddie Dong85f455f2007-07-06 12:20:49 +0300387 kfree(kvm->vpic);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300388 kfree(kvm->vioapic);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800389 kvm_free_vcpus(kvm);
390 kvm_free_physmem(kvm);
391 kfree(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200392}
393
394static int kvm_vm_release(struct inode *inode, struct file *filp)
395{
396 struct kvm *kvm = filp->private_data;
397
398 kvm_destroy_vm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800399 return 0;
400}
401
402static void inject_gp(struct kvm_vcpu *vcpu)
403{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300404 kvm_x86_ops->inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800405}
406
Avi Kivity1342d352007-01-05 16:36:39 -0800407/*
408 * Load the pae pdptrs. Return true is they are all valid.
409 */
410static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800411{
412 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
Avi Kivity1342d352007-01-05 16:36:39 -0800413 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800414 int i;
Avi Kivity1342d352007-01-05 16:36:39 -0800415 int ret;
Rusty Russellc820c2a2007-07-25 13:29:51 +1000416 u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800417
Shaohua Li11ec2802007-07-23 14:51:37 +0800418 mutex_lock(&vcpu->kvm->lock);
Izik Eidus195aefd2007-10-01 22:14:18 +0200419 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
420 offset * sizeof(u64), sizeof(pdpte));
421 if (ret < 0) {
Rusty Russellc820c2a2007-07-25 13:29:51 +1000422 ret = 0;
423 goto out;
424 }
Rusty Russellc820c2a2007-07-25 13:29:51 +1000425 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
426 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
Avi Kivity1342d352007-01-05 16:36:39 -0800427 ret = 0;
428 goto out;
429 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800430 }
Rusty Russellc820c2a2007-07-25 13:29:51 +1000431 ret = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800432
Rusty Russellc820c2a2007-07-25 13:29:51 +1000433 memcpy(vcpu->pdptrs, pdpte, sizeof(vcpu->pdptrs));
Avi Kivity1342d352007-01-05 16:36:39 -0800434out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800435 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800436
Avi Kivity1342d352007-01-05 16:36:39 -0800437 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800438}
439
440void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
441{
Rusty Russell707d92f2007-07-17 23:19:08 +1000442 if (cr0 & CR0_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800443 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
444 cr0, vcpu->cr0);
445 inject_gp(vcpu);
446 return;
447 }
448
Rusty Russell707d92f2007-07-17 23:19:08 +1000449 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800450 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
451 inject_gp(vcpu);
452 return;
453 }
454
Rusty Russell707d92f2007-07-17 23:19:08 +1000455 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800456 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
457 "and a clear PE flag\n");
458 inject_gp(vcpu);
459 return;
460 }
461
Rusty Russell707d92f2007-07-17 23:19:08 +1000462 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800463#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800464 if ((vcpu->shadow_efer & EFER_LME)) {
465 int cs_db, cs_l;
466
467 if (!is_pae(vcpu)) {
468 printk(KERN_DEBUG "set_cr0: #GP, start paging "
469 "in long mode while PAE is disabled\n");
470 inject_gp(vcpu);
471 return;
472 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300473 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800474 if (cs_l) {
475 printk(KERN_DEBUG "set_cr0: #GP, start paging "
476 "in long mode while CS.L == 1\n");
477 inject_gp(vcpu);
478 return;
479
480 }
481 } else
482#endif
Avi Kivity1342d352007-01-05 16:36:39 -0800483 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800484 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
485 "reserved bits\n");
486 inject_gp(vcpu);
487 return;
488 }
489
490 }
491
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300492 kvm_x86_ops->set_cr0(vcpu, cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800493 vcpu->cr0 = cr0;
494
Shaohua Li11ec2802007-07-23 14:51:37 +0800495 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800496 kvm_mmu_reset_context(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800497 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800498 return;
499}
500EXPORT_SYMBOL_GPL(set_cr0);
501
502void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
503{
504 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
505}
506EXPORT_SYMBOL_GPL(lmsw);
507
508void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
509{
Rusty Russell66aee912007-07-17 23:34:16 +1000510 if (cr4 & CR4_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800511 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
512 inject_gp(vcpu);
513 return;
514 }
515
Avi Kivitya9058ec2006-12-29 16:49:37 -0800516 if (is_long_mode(vcpu)) {
Rusty Russell66aee912007-07-17 23:34:16 +1000517 if (!(cr4 & X86_CR4_PAE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800518 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
519 "in long mode\n");
520 inject_gp(vcpu);
521 return;
522 }
Rusty Russell66aee912007-07-17 23:34:16 +1000523 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
Avi Kivity1342d352007-01-05 16:36:39 -0800524 && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800525 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
526 inject_gp(vcpu);
Rusty Russell310bc762007-07-23 17:11:02 +1000527 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800528 }
529
Rusty Russell66aee912007-07-17 23:34:16 +1000530 if (cr4 & X86_CR4_VMXE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800531 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
532 inject_gp(vcpu);
533 return;
534 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300535 kvm_x86_ops->set_cr4(vcpu, cr4);
Rusty Russell81f50e32007-09-06 01:20:38 +1000536 vcpu->cr4 = cr4;
Shaohua Li11ec2802007-07-23 14:51:37 +0800537 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800538 kvm_mmu_reset_context(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800539 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800540}
541EXPORT_SYMBOL_GPL(set_cr4);
542
543void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
544{
Avi Kivitya9058ec2006-12-29 16:49:37 -0800545 if (is_long_mode(vcpu)) {
Rusty Russellf802a302007-07-17 23:32:55 +1000546 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800547 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
548 inject_gp(vcpu);
549 return;
550 }
551 } else {
Rusty Russellf802a302007-07-17 23:32:55 +1000552 if (is_pae(vcpu)) {
553 if (cr3 & CR3_PAE_RESERVED_BITS) {
554 printk(KERN_DEBUG
555 "set_cr3: #GP, reserved bits\n");
556 inject_gp(vcpu);
557 return;
558 }
559 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
560 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
561 "reserved bits\n");
562 inject_gp(vcpu);
563 return;
564 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800565 }
Ryan Harper21764862007-09-18 14:05:16 -0500566 /*
567 * We don't check reserved bits in nonpae mode, because
568 * this isn't enforced, and VMware depends on this.
569 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800570 }
571
Shaohua Li11ec2802007-07-23 14:51:37 +0800572 mutex_lock(&vcpu->kvm->lock);
Ingo Molnard21225e2007-01-05 16:36:59 -0800573 /*
574 * Does the new cr3 value map to physical memory? (Note, we
575 * catch an invalid cr3 even in real-mode, because it would
576 * cause trouble later on when we turn on paging anyway.)
577 *
578 * A real CPU would silently accept an invalid cr3 and would
579 * attempt to use it - with largely undefined (and often hard
580 * to debug) behavior on the guest side.
581 */
582 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
583 inject_gp(vcpu);
Rusty Russellfb764412007-07-31 20:45:03 +1000584 else {
585 vcpu->cr3 = cr3;
Ingo Molnard21225e2007-01-05 16:36:59 -0800586 vcpu->mmu.new_cr3(vcpu);
Rusty Russellfb764412007-07-31 20:45:03 +1000587 }
Shaohua Li11ec2802007-07-23 14:51:37 +0800588 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800589}
590EXPORT_SYMBOL_GPL(set_cr3);
591
592void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
593{
Rusty Russell7075bc82007-07-17 23:37:17 +1000594 if (cr8 & CR8_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800595 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
596 inject_gp(vcpu);
597 return;
598 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300599 if (irqchip_in_kernel(vcpu->kvm))
600 kvm_lapic_set_tpr(vcpu, cr8);
601 else
602 vcpu->cr8 = cr8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800603}
604EXPORT_SYMBOL_GPL(set_cr8);
605
Eddie Dong7017fc32007-07-18 11:34:57 +0300606unsigned long get_cr8(struct kvm_vcpu *vcpu)
607{
Eddie Dong97222cc2007-09-12 10:58:04 +0300608 if (irqchip_in_kernel(vcpu->kvm))
609 return kvm_lapic_get_cr8(vcpu);
610 else
611 return vcpu->cr8;
Eddie Dong7017fc32007-07-18 11:34:57 +0300612}
613EXPORT_SYMBOL_GPL(get_cr8);
614
615u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
616{
Eddie Dong97222cc2007-09-12 10:58:04 +0300617 if (irqchip_in_kernel(vcpu->kvm))
618 return vcpu->apic_base;
619 else
620 return vcpu->apic_base;
Eddie Dong7017fc32007-07-18 11:34:57 +0300621}
622EXPORT_SYMBOL_GPL(kvm_get_apic_base);
623
624void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
625{
Eddie Dong97222cc2007-09-12 10:58:04 +0300626 /* TODO: reserve bits check */
627 if (irqchip_in_kernel(vcpu->kvm))
628 kvm_lapic_set_base(vcpu, data);
629 else
630 vcpu->apic_base = data;
Eddie Dong7017fc32007-07-18 11:34:57 +0300631}
632EXPORT_SYMBOL_GPL(kvm_set_apic_base);
633
Avi Kivity6aa8b732006-12-10 02:21:36 -0800634void fx_init(struct kvm_vcpu *vcpu)
635{
Rusty Russellb114b082007-07-30 21:13:43 +1000636 unsigned after_mxcsr_mask;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800637
Rusty Russell9bd01502007-07-30 16:29:56 +1000638 /* Initialize guest FPU by resetting ours and saving into guest's */
639 preempt_disable();
Rusty Russellb114b082007-07-30 21:13:43 +1000640 fx_save(&vcpu->host_fx_image);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800641 fpu_init();
Rusty Russellb114b082007-07-30 21:13:43 +1000642 fx_save(&vcpu->guest_fx_image);
643 fx_restore(&vcpu->host_fx_image);
Rusty Russell9bd01502007-07-30 16:29:56 +1000644 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800645
Amit Shah380102c2007-08-25 11:35:52 +0300646 vcpu->cr0 |= X86_CR0_ET;
Rusty Russellb114b082007-07-30 21:13:43 +1000647 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
648 vcpu->guest_fx_image.mxcsr = 0x1f80;
649 memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask,
650 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800651}
652EXPORT_SYMBOL_GPL(fx_init);
653
654/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800655 * Allocate some memory and give it an address in the guest physical address
656 * space.
657 *
658 * Discontiguous memory is allowed, mostly for framebuffers.
659 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200660static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
Izik Eidus6fc138d2007-10-09 19:20:39 +0200661 struct
662 kvm_userspace_memory_region *mem,
663 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800664{
665 int r;
666 gfn_t base_gfn;
667 unsigned long npages;
668 unsigned long i;
669 struct kvm_memory_slot *memslot;
670 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800671
672 r = -EINVAL;
673 /* General sanity checks */
674 if (mem->memory_size & (PAGE_SIZE - 1))
675 goto out;
676 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
677 goto out;
678 if (mem->slot >= KVM_MEMORY_SLOTS)
679 goto out;
680 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
681 goto out;
682
683 memslot = &kvm->memslots[mem->slot];
684 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
685 npages = mem->memory_size >> PAGE_SHIFT;
686
687 if (!npages)
688 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
689
Shaohua Li11ec2802007-07-23 14:51:37 +0800690 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800691
Avi Kivity6aa8b732006-12-10 02:21:36 -0800692 new = old = *memslot;
693
694 new.base_gfn = base_gfn;
695 new.npages = npages;
696 new.flags = mem->flags;
697
698 /* Disallow changing a memory slot's size. */
699 r = -EINVAL;
700 if (npages && old.npages && npages != old.npages)
701 goto out_unlock;
702
703 /* Check for overlaps */
704 r = -EEXIST;
705 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
706 struct kvm_memory_slot *s = &kvm->memslots[i];
707
708 if (s == memslot)
709 continue;
710 if (!((base_gfn + npages <= s->base_gfn) ||
711 (base_gfn >= s->base_gfn + s->npages)))
712 goto out_unlock;
713 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800714
715 /* Deallocate if slot is being removed */
716 if (!npages)
Al Viro8b6d44c2007-02-09 16:38:40 +0000717 new.phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800718
719 /* Free page dirty bitmap if unneeded */
720 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000721 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800722
723 r = -ENOMEM;
724
725 /* Allocate if a slot is being created */
726 if (npages && !new.phys_mem) {
727 new.phys_mem = vmalloc(npages * sizeof(struct page *));
728
729 if (!new.phys_mem)
Laurent Vivier0d8d2bd2007-08-30 14:56:21 +0200730 goto out_unlock;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800731
Mike Dayd77c26f2007-10-08 09:02:08 -0400732 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200733
734 if (!new.rmap)
735 goto out_unlock;
736
Avi Kivity6aa8b732006-12-10 02:21:36 -0800737 memset(new.phys_mem, 0, npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200738 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Izik Eidus6fc138d2007-10-09 19:20:39 +0200739 if (user_alloc) {
Izik Eidus6fc138d2007-10-09 19:20:39 +0200740 new.user_alloc = 1;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200741 new.userspace_addr = mem->userspace_addr;
Izik Eidus6fc138d2007-10-09 19:20:39 +0200742 } else {
743 for (i = 0; i < npages; ++i) {
744 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
745 | __GFP_ZERO);
746 if (!new.phys_mem[i])
747 goto out_unlock;
748 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800749 }
750 }
751
752 /* Allocate page dirty bitmap if needed */
753 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
754 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
755
756 new.dirty_bitmap = vmalloc(dirty_bytes);
757 if (!new.dirty_bitmap)
Laurent Vivier0d8d2bd2007-08-30 14:56:21 +0200758 goto out_unlock;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800759 memset(new.dirty_bitmap, 0, dirty_bytes);
760 }
761
Avi Kivity6aa8b732006-12-10 02:21:36 -0800762 if (mem->slot >= kvm->nmemslots)
763 kvm->nmemslots = mem->slot + 1;
764
Izik Eidus82ce2c92007-10-02 18:52:55 +0200765 if (!kvm->n_requested_mmu_pages) {
766 unsigned int n_pages;
767
768 if (npages) {
769 n_pages = npages * KVM_PERMILLE_MMU_PAGES / 1000;
770 kvm_mmu_change_mmu_pages(kvm, kvm->n_alloc_mmu_pages +
771 n_pages);
772 } else {
773 unsigned int nr_mmu_pages;
774
775 n_pages = old.npages * KVM_PERMILLE_MMU_PAGES / 1000;
776 nr_mmu_pages = kvm->n_alloc_mmu_pages - n_pages;
777 nr_mmu_pages = max(nr_mmu_pages,
778 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
779 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
780 }
781 }
782
Avi Kivity6aa8b732006-12-10 02:21:36 -0800783 *memslot = new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800784
Avi Kivity90cb0522007-07-17 13:04:56 +0300785 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
786 kvm_flush_remote_tlbs(kvm);
787
Shaohua Li11ec2802007-07-23 14:51:37 +0800788 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790 kvm_free_physmem_slot(&old, &new);
791 return 0;
792
793out_unlock:
Shaohua Li11ec2802007-07-23 14:51:37 +0800794 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800795 kvm_free_physmem_slot(&new, &old);
796out:
797 return r;
798}
799
Izik Eidus82ce2c92007-10-02 18:52:55 +0200800static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
801 u32 kvm_nr_mmu_pages)
802{
803 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
804 return -EINVAL;
805
806 mutex_lock(&kvm->lock);
807
808 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
809 kvm->n_requested_mmu_pages = kvm_nr_mmu_pages;
810
811 mutex_unlock(&kvm->lock);
812 return 0;
813}
814
815static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
816{
817 return kvm->n_alloc_mmu_pages;
818}
819
Avi Kivity6aa8b732006-12-10 02:21:36 -0800820/*
821 * Get (and clear) the dirty memory log for a memory slot.
822 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200823static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
824 struct kvm_dirty_log *log)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825{
826 struct kvm_memory_slot *memslot;
827 int r, i;
828 int n;
829 unsigned long any = 0;
830
Shaohua Li11ec2802007-07-23 14:51:37 +0800831 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800832
Avi Kivity6aa8b732006-12-10 02:21:36 -0800833 r = -EINVAL;
834 if (log->slot >= KVM_MEMORY_SLOTS)
835 goto out;
836
837 memslot = &kvm->memslots[log->slot];
838 r = -ENOENT;
839 if (!memslot->dirty_bitmap)
840 goto out;
841
Uri Lublincd1a4a92007-02-22 16:43:09 +0200842 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800843
Uri Lublincd1a4a92007-02-22 16:43:09 +0200844 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800845 any = memslot->dirty_bitmap[i];
846
847 r = -EFAULT;
848 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
849 goto out;
850
Rusty Russell39214912007-07-31 19:57:47 +1000851 /* If nothing is dirty, don't bother messing with page tables. */
852 if (any) {
Rusty Russell39214912007-07-31 19:57:47 +1000853 kvm_mmu_slot_remove_write_access(kvm, log->slot);
854 kvm_flush_remote_tlbs(kvm);
855 memset(memslot->dirty_bitmap, 0, n);
Rusty Russell39214912007-07-31 19:57:47 +1000856 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800857
858 r = 0;
859
860out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800861 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800862 return r;
863}
864
Avi Kivitye8207542007-03-30 16:54:30 +0300865/*
866 * Set a new alias region. Aliases map a portion of physical memory into
867 * another portion. This is useful for memory windows, for example the PC
868 * VGA region.
869 */
870static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
871 struct kvm_memory_alias *alias)
872{
873 int r, n;
874 struct kvm_mem_alias *p;
875
876 r = -EINVAL;
877 /* General sanity checks */
878 if (alias->memory_size & (PAGE_SIZE - 1))
879 goto out;
880 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
881 goto out;
882 if (alias->slot >= KVM_ALIAS_SLOTS)
883 goto out;
884 if (alias->guest_phys_addr + alias->memory_size
885 < alias->guest_phys_addr)
886 goto out;
887 if (alias->target_phys_addr + alias->memory_size
888 < alias->target_phys_addr)
889 goto out;
890
Shaohua Li11ec2802007-07-23 14:51:37 +0800891 mutex_lock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300892
893 p = &kvm->aliases[alias->slot];
894 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
895 p->npages = alias->memory_size >> PAGE_SHIFT;
896 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
897
898 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
899 if (kvm->aliases[n - 1].npages)
900 break;
901 kvm->naliases = n;
902
Avi Kivity90cb0522007-07-17 13:04:56 +0300903 kvm_mmu_zap_all(kvm);
Avi Kivitye8207542007-03-30 16:54:30 +0300904
Shaohua Li11ec2802007-07-23 14:51:37 +0800905 mutex_unlock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300906
907 return 0;
908
909out:
910 return r;
911}
912
He, Qing6ceb9d72007-07-26 11:05:18 +0300913static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
914{
915 int r;
916
917 r = 0;
918 switch (chip->chip_id) {
919 case KVM_IRQCHIP_PIC_MASTER:
Mike Dayd77c26f2007-10-08 09:02:08 -0400920 memcpy(&chip->chip.pic,
He, Qing6ceb9d72007-07-26 11:05:18 +0300921 &pic_irqchip(kvm)->pics[0],
922 sizeof(struct kvm_pic_state));
923 break;
924 case KVM_IRQCHIP_PIC_SLAVE:
Mike Dayd77c26f2007-10-08 09:02:08 -0400925 memcpy(&chip->chip.pic,
He, Qing6ceb9d72007-07-26 11:05:18 +0300926 &pic_irqchip(kvm)->pics[1],
927 sizeof(struct kvm_pic_state));
928 break;
He, Qing6bf9e962007-08-05 10:49:16 +0300929 case KVM_IRQCHIP_IOAPIC:
Mike Dayd77c26f2007-10-08 09:02:08 -0400930 memcpy(&chip->chip.ioapic,
He, Qing6bf9e962007-08-05 10:49:16 +0300931 ioapic_irqchip(kvm),
932 sizeof(struct kvm_ioapic_state));
933 break;
He, Qing6ceb9d72007-07-26 11:05:18 +0300934 default:
935 r = -EINVAL;
936 break;
937 }
938 return r;
939}
940
941static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
942{
943 int r;
944
945 r = 0;
946 switch (chip->chip_id) {
947 case KVM_IRQCHIP_PIC_MASTER:
Mike Dayd77c26f2007-10-08 09:02:08 -0400948 memcpy(&pic_irqchip(kvm)->pics[0],
He, Qing6ceb9d72007-07-26 11:05:18 +0300949 &chip->chip.pic,
950 sizeof(struct kvm_pic_state));
951 break;
952 case KVM_IRQCHIP_PIC_SLAVE:
Mike Dayd77c26f2007-10-08 09:02:08 -0400953 memcpy(&pic_irqchip(kvm)->pics[1],
He, Qing6ceb9d72007-07-26 11:05:18 +0300954 &chip->chip.pic,
955 sizeof(struct kvm_pic_state));
956 break;
He, Qing6bf9e962007-08-05 10:49:16 +0300957 case KVM_IRQCHIP_IOAPIC:
Mike Dayd77c26f2007-10-08 09:02:08 -0400958 memcpy(ioapic_irqchip(kvm),
He, Qing6bf9e962007-08-05 10:49:16 +0300959 &chip->chip.ioapic,
960 sizeof(struct kvm_ioapic_state));
961 break;
He, Qing6ceb9d72007-07-26 11:05:18 +0300962 default:
963 r = -EINVAL;
964 break;
965 }
966 kvm_pic_update_irq(pic_irqchip(kvm));
967 return r;
968}
969
Izik Eiduscea7bb22007-10-17 19:17:48 +0200970int is_error_page(struct page *page)
971{
972 return page == bad_page;
973}
974EXPORT_SYMBOL_GPL(is_error_page);
975
Izik Eidus290fc382007-09-27 14:11:22 +0200976gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
Avi Kivitye8207542007-03-30 16:54:30 +0300977{
978 int i;
979 struct kvm_mem_alias *alias;
980
981 for (i = 0; i < kvm->naliases; ++i) {
982 alias = &kvm->aliases[i];
983 if (gfn >= alias->base_gfn
984 && gfn < alias->base_gfn + alias->npages)
985 return alias->target_gfn + gfn - alias->base_gfn;
986 }
987 return gfn;
988}
989
990static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991{
992 int i;
993
994 for (i = 0; i < kvm->nmemslots; ++i) {
995 struct kvm_memory_slot *memslot = &kvm->memslots[i];
996
997 if (gfn >= memslot->base_gfn
998 && gfn < memslot->base_gfn + memslot->npages)
999 return memslot;
1000 }
Al Viro8b6d44c2007-02-09 16:38:40 +00001001 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001002}
Avi Kivitye8207542007-03-30 16:54:30 +03001003
1004struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1005{
1006 gfn = unalias_gfn(kvm, gfn);
1007 return __gfn_to_memslot(kvm, gfn);
1008}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009
Avi Kivity954bbbc22007-03-30 14:02:32 +03001010struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1011{
1012 struct kvm_memory_slot *slot;
1013
Avi Kivitye8207542007-03-30 16:54:30 +03001014 gfn = unalias_gfn(kvm, gfn);
1015 slot = __gfn_to_memslot(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001016 if (!slot) {
1017 get_page(bad_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +02001018 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001019 }
1020 if (slot->user_alloc) {
1021 struct page *page[1];
1022 int npages;
1023
1024 down_read(&current->mm->mmap_sem);
1025 npages = get_user_pages(current, current->mm,
1026 slot->userspace_addr
1027 + (gfn - slot->base_gfn) * PAGE_SIZE, 1,
1028 1, 1, page, NULL);
1029 up_read(&current->mm->mmap_sem);
1030 if (npages != 1) {
1031 get_page(bad_page);
1032 return bad_page;
1033 }
1034 return page[0];
1035 }
1036 get_page(slot->phys_mem[gfn - slot->base_gfn]);
Avi Kivity954bbbc22007-03-30 14:02:32 +03001037 return slot->phys_mem[gfn - slot->base_gfn];
1038}
1039EXPORT_SYMBOL_GPL(gfn_to_page);
1040
Izik Eidus8a7ae052007-10-18 11:09:33 +02001041void kvm_release_page(struct page *page)
1042{
1043 if (!PageReserved(page))
1044 SetPageDirty(page);
1045 put_page(page);
1046}
1047EXPORT_SYMBOL_GPL(kvm_release_page);
1048
Izik Eidus195aefd2007-10-01 22:14:18 +02001049static int next_segment(unsigned long len, int offset)
1050{
1051 if (len > PAGE_SIZE - offset)
1052 return PAGE_SIZE - offset;
1053 else
1054 return len;
1055}
1056
1057int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1058 int len)
1059{
1060 void *page_virt;
1061 struct page *page;
1062
1063 page = gfn_to_page(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001064 if (is_error_page(page)) {
1065 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001066 return -EFAULT;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001067 }
Izik Eidus195aefd2007-10-01 22:14:18 +02001068 page_virt = kmap_atomic(page, KM_USER0);
1069
1070 memcpy(data, page_virt + offset, len);
1071
1072 kunmap_atomic(page_virt, KM_USER0);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001073 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001074 return 0;
1075}
1076EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1077
1078int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1079{
1080 gfn_t gfn = gpa >> PAGE_SHIFT;
1081 int seg;
1082 int offset = offset_in_page(gpa);
1083 int ret;
1084
1085 while ((seg = next_segment(len, offset)) != 0) {
1086 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1087 if (ret < 0)
1088 return ret;
1089 offset = 0;
1090 len -= seg;
1091 data += seg;
1092 ++gfn;
1093 }
1094 return 0;
1095}
1096EXPORT_SYMBOL_GPL(kvm_read_guest);
1097
1098int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1099 int offset, int len)
1100{
1101 void *page_virt;
1102 struct page *page;
1103
1104 page = gfn_to_page(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001105 if (is_error_page(page)) {
1106 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001107 return -EFAULT;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001108 }
Izik Eidus195aefd2007-10-01 22:14:18 +02001109 page_virt = kmap_atomic(page, KM_USER0);
1110
1111 memcpy(page_virt + offset, data, len);
1112
1113 kunmap_atomic(page_virt, KM_USER0);
1114 mark_page_dirty(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001115 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001116 return 0;
1117}
1118EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1119
1120int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1121 unsigned long len)
1122{
1123 gfn_t gfn = gpa >> PAGE_SHIFT;
1124 int seg;
1125 int offset = offset_in_page(gpa);
1126 int ret;
1127
1128 while ((seg = next_segment(len, offset)) != 0) {
1129 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1130 if (ret < 0)
1131 return ret;
1132 offset = 0;
1133 len -= seg;
1134 data += seg;
1135 ++gfn;
1136 }
1137 return 0;
1138}
1139
1140int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1141{
1142 void *page_virt;
1143 struct page *page;
1144
1145 page = gfn_to_page(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001146 if (is_error_page(page)) {
1147 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001148 return -EFAULT;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001149 }
Izik Eidus195aefd2007-10-01 22:14:18 +02001150 page_virt = kmap_atomic(page, KM_USER0);
1151
1152 memset(page_virt + offset, 0, len);
1153
1154 kunmap_atomic(page_virt, KM_USER0);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001155 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001156 return 0;
1157}
1158EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1159
1160int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1161{
1162 gfn_t gfn = gpa >> PAGE_SHIFT;
1163 int seg;
1164 int offset = offset_in_page(gpa);
1165 int ret;
1166
1167 while ((seg = next_segment(len, offset)) != 0) {
1168 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1169 if (ret < 0)
1170 return ret;
1171 offset = 0;
1172 len -= seg;
1173 ++gfn;
1174 }
1175 return 0;
1176}
1177EXPORT_SYMBOL_GPL(kvm_clear_guest);
1178
Rusty Russell7e9d6192007-07-31 20:41:14 +10001179/* WARNING: Does not work on aliased pages. */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001180void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1181{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001182 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001183
Rusty Russell7e9d6192007-07-31 20:41:14 +10001184 memslot = __gfn_to_memslot(kvm, gfn);
1185 if (memslot && memslot->dirty_bitmap) {
1186 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001187
Rusty Russell7e9d6192007-07-31 20:41:14 +10001188 /* avoid RMW */
1189 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1190 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001191 }
1192}
1193
Laurent Viviere7d5d762007-07-30 13:41:19 +03001194int emulator_read_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001195 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001196 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001197 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001198{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001199 void *data = val;
1200
1201 while (bytes) {
1202 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1203 unsigned offset = addr & (PAGE_SIZE-1);
1204 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
Izik Eidus195aefd2007-10-01 22:14:18 +02001205 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001206
1207 if (gpa == UNMAPPED_GVA)
1208 return X86EMUL_PROPAGATE_FAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001209 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
1210 if (ret < 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001211 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001212
1213 bytes -= tocopy;
1214 data += tocopy;
1215 addr += tocopy;
1216 }
1217
1218 return X86EMUL_CONTINUE;
1219}
Laurent Viviere7d5d762007-07-30 13:41:19 +03001220EXPORT_SYMBOL_GPL(emulator_read_std);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001221
1222static int emulator_write_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001223 const void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001224 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001225 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001226{
Rusty Russellf0242472007-08-01 10:48:02 +10001227 pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001228 return X86EMUL_UNHANDLEABLE;
1229}
1230
Eddie Dong97222cc2007-09-12 10:58:04 +03001231/*
1232 * Only apic need an MMIO device hook, so shortcut now..
1233 */
1234static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
1235 gpa_t addr)
1236{
1237 struct kvm_io_device *dev;
1238
1239 if (vcpu->apic) {
1240 dev = &vcpu->apic->dev;
1241 if (dev->in_range(dev, addr))
1242 return dev;
1243 }
1244 return NULL;
1245}
1246
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001247static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1248 gpa_t addr)
1249{
Eddie Dong97222cc2007-09-12 10:58:04 +03001250 struct kvm_io_device *dev;
1251
1252 dev = vcpu_find_pervcpu_dev(vcpu, addr);
1253 if (dev == NULL)
1254 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1255 return dev;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001256}
1257
Eddie Dong74906342007-06-19 18:05:03 +03001258static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
1259 gpa_t addr)
1260{
1261 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
1262}
1263
Avi Kivity6aa8b732006-12-10 02:21:36 -08001264static int emulator_read_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001265 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001266 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001267 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001268{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001269 struct kvm_io_device *mmio_dev;
1270 gpa_t gpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001271
1272 if (vcpu->mmio_read_completed) {
1273 memcpy(val, vcpu->mmio_data, bytes);
1274 vcpu->mmio_read_completed = 0;
1275 return X86EMUL_CONTINUE;
Laurent Viviercebff022007-07-30 13:35:24 +03001276 } else if (emulator_read_std(addr, val, bytes, vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001277 == X86EMUL_CONTINUE)
1278 return X86EMUL_CONTINUE;
Avi Kivityd27d4ac2007-02-19 14:37:46 +02001279
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001280 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1281 if (gpa == UNMAPPED_GVA)
1282 return X86EMUL_PROPAGATE_FAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001283
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001284 /*
1285 * Is this MMIO handled locally?
1286 */
1287 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1288 if (mmio_dev) {
1289 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1290 return X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001291 }
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001292
1293 vcpu->mmio_needed = 1;
1294 vcpu->mmio_phys_addr = gpa;
1295 vcpu->mmio_size = bytes;
1296 vcpu->mmio_is_write = 0;
1297
1298 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001299}
1300
Avi Kivityda4a00f2007-01-05 16:36:44 -08001301static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity4c690a12007-04-22 15:28:19 +03001302 const void *val, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001303{
Izik Eidus195aefd2007-10-01 22:14:18 +02001304 int ret;
Avi Kivityda4a00f2007-01-05 16:36:44 -08001305
Izik Eidus195aefd2007-10-01 22:14:18 +02001306 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
1307 if (ret < 0)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001308 return 0;
Shaohua Life551882007-07-23 14:51:39 +08001309 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001310 return 1;
1311}
1312
Avi Kivityb0fcd902007-07-22 18:48:54 +03001313static int emulator_write_emulated_onepage(unsigned long addr,
1314 const void *val,
1315 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001316 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001317{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001318 struct kvm_io_device *mmio_dev;
1319 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001320
Avi Kivityc9047f52007-04-17 10:53:22 +03001321 if (gpa == UNMAPPED_GVA) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001322 kvm_x86_ops->inject_page_fault(vcpu, addr, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001323 return X86EMUL_PROPAGATE_FAULT;
Avi Kivityc9047f52007-04-17 10:53:22 +03001324 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001325
Avi Kivityda4a00f2007-01-05 16:36:44 -08001326 if (emulator_write_phys(vcpu, gpa, val, bytes))
1327 return X86EMUL_CONTINUE;
1328
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001329 /*
1330 * Is this MMIO handled locally?
1331 */
1332 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1333 if (mmio_dev) {
1334 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1335 return X86EMUL_CONTINUE;
1336 }
1337
Avi Kivity6aa8b732006-12-10 02:21:36 -08001338 vcpu->mmio_needed = 1;
1339 vcpu->mmio_phys_addr = gpa;
1340 vcpu->mmio_size = bytes;
1341 vcpu->mmio_is_write = 1;
Avi Kivity4c690a12007-04-22 15:28:19 +03001342 memcpy(vcpu->mmio_data, val, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001343
1344 return X86EMUL_CONTINUE;
1345}
1346
Laurent Viviere7d5d762007-07-30 13:41:19 +03001347int emulator_write_emulated(unsigned long addr,
Avi Kivityb0fcd902007-07-22 18:48:54 +03001348 const void *val,
1349 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001350 struct kvm_vcpu *vcpu)
Avi Kivityb0fcd902007-07-22 18:48:54 +03001351{
1352 /* Crossing a page boundary? */
1353 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1354 int rc, now;
1355
1356 now = -addr & ~PAGE_MASK;
Laurent Viviercebff022007-07-30 13:35:24 +03001357 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001358 if (rc != X86EMUL_CONTINUE)
1359 return rc;
1360 addr += now;
1361 val += now;
1362 bytes -= now;
1363 }
Laurent Viviercebff022007-07-30 13:35:24 +03001364 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001365}
Laurent Viviere7d5d762007-07-30 13:41:19 +03001366EXPORT_SYMBOL_GPL(emulator_write_emulated);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001367
Avi Kivity6aa8b732006-12-10 02:21:36 -08001368static int emulator_cmpxchg_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001369 const void *old,
1370 const void *new,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001371 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001372 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001373{
1374 static int reported;
1375
1376 if (!reported) {
1377 reported = 1;
1378 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1379 }
Laurent Viviercebff022007-07-30 13:35:24 +03001380 return emulator_write_emulated(addr, new, bytes, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001381}
1382
1383static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1384{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001385 return kvm_x86_ops->get_segment_base(vcpu, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001386}
1387
1388int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1389{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001390 return X86EMUL_CONTINUE;
1391}
1392
1393int emulate_clts(struct kvm_vcpu *vcpu)
1394{
Amit Shah404fb882007-11-19 17:57:35 +02001395 kvm_x86_ops->set_cr0(vcpu, vcpu->cr0 & ~X86_CR0_TS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001396 return X86EMUL_CONTINUE;
1397}
1398
Mike Dayd77c26f2007-10-08 09:02:08 -04001399int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001400{
1401 struct kvm_vcpu *vcpu = ctxt->vcpu;
1402
1403 switch (dr) {
1404 case 0 ... 3:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001405 *dest = kvm_x86_ops->get_dr(vcpu, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001406 return X86EMUL_CONTINUE;
1407 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001408 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001409 return X86EMUL_UNHANDLEABLE;
1410 }
1411}
1412
1413int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1414{
1415 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1416 int exception;
1417
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001418 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001419 if (exception) {
1420 /* FIXME: better handling */
1421 return X86EMUL_UNHANDLEABLE;
1422 }
1423 return X86EMUL_CONTINUE;
1424}
1425
Avi Kivity054b1362007-09-12 13:21:09 +03001426void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001427{
1428 static int reported;
1429 u8 opcodes[4];
Avi Kivity054b1362007-09-12 13:21:09 +03001430 unsigned long rip = vcpu->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001431 unsigned long rip_linear;
1432
Avi Kivity054b1362007-09-12 13:21:09 +03001433 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001434
1435 if (reported)
1436 return;
1437
Avi Kivity054b1362007-09-12 13:21:09 +03001438 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001439
Avi Kivity054b1362007-09-12 13:21:09 +03001440 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1441 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001442 reported = 1;
1443}
Avi Kivity054b1362007-09-12 13:21:09 +03001444EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001445
1446struct x86_emulate_ops emulate_ops = {
1447 .read_std = emulator_read_std,
1448 .write_std = emulator_write_std,
1449 .read_emulated = emulator_read_emulated,
1450 .write_emulated = emulator_write_emulated,
1451 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1452};
1453
1454int emulate_instruction(struct kvm_vcpu *vcpu,
1455 struct kvm_run *run,
1456 unsigned long cr2,
Laurent Vivier34273182007-09-18 11:27:37 +02001457 u16 error_code,
1458 int no_decode)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001459{
Laurent Viviera22436b2007-09-24 17:00:58 +02001460 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001461
Avi Kivitye7df56e2007-03-14 15:54:54 +02001462 vcpu->mmio_fault_cr2 = cr2;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001463 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001464
Avi Kivity6aa8b732006-12-10 02:21:36 -08001465 vcpu->mmio_is_write = 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03001466 vcpu->pio.string = 0;
Laurent Vivier34273182007-09-18 11:27:37 +02001467
1468 if (!no_decode) {
1469 int cs_db, cs_l;
1470 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1471
1472 vcpu->emulate_ctxt.vcpu = vcpu;
1473 vcpu->emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
1474 vcpu->emulate_ctxt.cr2 = cr2;
1475 vcpu->emulate_ctxt.mode =
1476 (vcpu->emulate_ctxt.eflags & X86_EFLAGS_VM)
1477 ? X86EMUL_MODE_REAL : cs_l
1478 ? X86EMUL_MODE_PROT64 : cs_db
1479 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1480
1481 if (vcpu->emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1482 vcpu->emulate_ctxt.cs_base = 0;
1483 vcpu->emulate_ctxt.ds_base = 0;
1484 vcpu->emulate_ctxt.es_base = 0;
1485 vcpu->emulate_ctxt.ss_base = 0;
1486 } else {
1487 vcpu->emulate_ctxt.cs_base =
1488 get_segment_base(vcpu, VCPU_SREG_CS);
1489 vcpu->emulate_ctxt.ds_base =
1490 get_segment_base(vcpu, VCPU_SREG_DS);
1491 vcpu->emulate_ctxt.es_base =
1492 get_segment_base(vcpu, VCPU_SREG_ES);
1493 vcpu->emulate_ctxt.ss_base =
1494 get_segment_base(vcpu, VCPU_SREG_SS);
1495 }
1496
1497 vcpu->emulate_ctxt.gs_base =
1498 get_segment_base(vcpu, VCPU_SREG_GS);
1499 vcpu->emulate_ctxt.fs_base =
1500 get_segment_base(vcpu, VCPU_SREG_FS);
1501
1502 r = x86_decode_insn(&vcpu->emulate_ctxt, &emulate_ops);
Laurent Viviera22436b2007-09-24 17:00:58 +02001503 if (r) {
1504 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1505 return EMULATE_DONE;
1506 return EMULATE_FAIL;
1507 }
Laurent Vivier34273182007-09-18 11:27:37 +02001508 }
1509
Laurent Viviera22436b2007-09-24 17:00:58 +02001510 r = x86_emulate_insn(&vcpu->emulate_ctxt, &emulate_ops);
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001511
Laurent Viviere70669a2007-08-05 10:36:40 +03001512 if (vcpu->pio.string)
1513 return EMULATE_DO_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001514
1515 if ((r || vcpu->mmio_is_write) && run) {
Jeff Dike8fc0d082007-07-17 12:26:59 -04001516 run->exit_reason = KVM_EXIT_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001517 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1518 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1519 run->mmio.len = vcpu->mmio_size;
1520 run->mmio.is_write = vcpu->mmio_is_write;
1521 }
1522
1523 if (r) {
Avi Kivitya4360362007-01-05 16:36:45 -08001524 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1525 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001526 if (!vcpu->mmio_needed) {
Avi Kivity054b1362007-09-12 13:21:09 +03001527 kvm_report_emulation_failure(vcpu, "mmio");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001528 return EMULATE_FAIL;
1529 }
1530 return EMULATE_DO_MMIO;
1531 }
1532
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001533 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier34273182007-09-18 11:27:37 +02001534 kvm_x86_ops->set_rflags(vcpu, vcpu->emulate_ctxt.eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001535
Avi Kivity02c83202007-04-29 15:02:17 +03001536 if (vcpu->mmio_is_write) {
1537 vcpu->mmio_needed = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001538 return EMULATE_DO_MMIO;
Avi Kivity02c83202007-04-29 15:02:17 +03001539 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001540
1541 return EMULATE_DONE;
1542}
1543EXPORT_SYMBOL_GPL(emulate_instruction);
1544
Eddie Dongb6958ce2007-07-18 12:15:21 +03001545/*
1546 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1547 */
He, Qingc5ec1532007-09-03 17:07:41 +03001548static void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001549{
1550 DECLARE_WAITQUEUE(wait, current);
1551
1552 add_wait_queue(&vcpu->wq, &wait);
1553
1554 /*
1555 * We will block until either an interrupt or a signal wakes us up
1556 */
He, Qingc5ec1532007-09-03 17:07:41 +03001557 while (!kvm_cpu_has_interrupt(vcpu)
1558 && !signal_pending(current)
1559 && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE
1560 && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) {
Eddie Dongb6958ce2007-07-18 12:15:21 +03001561 set_current_state(TASK_INTERRUPTIBLE);
1562 vcpu_put(vcpu);
1563 schedule();
1564 vcpu_load(vcpu);
1565 }
1566
He, Qingc5ec1532007-09-03 17:07:41 +03001567 __set_current_state(TASK_RUNNING);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001568 remove_wait_queue(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001569}
1570
Avi Kivityd3bef152007-06-05 15:53:05 +03001571int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1572{
Avi Kivityd3bef152007-06-05 15:53:05 +03001573 ++vcpu->stat.halt_exits;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001574 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc5ec1532007-09-03 17:07:41 +03001575 vcpu->mp_state = VCPU_MP_STATE_HALTED;
1576 kvm_vcpu_block(vcpu);
1577 if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE)
1578 return -EINTR;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001579 return 1;
1580 } else {
1581 vcpu->run->exit_reason = KVM_EXIT_HLT;
1582 return 0;
1583 }
Avi Kivityd3bef152007-06-05 15:53:05 +03001584}
1585EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1586
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001587int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
Avi Kivity270fd9b2007-02-19 14:37:47 +02001588{
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001589 unsigned long nr, a0, a1, a2, a3, ret;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001590
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001591 kvm_x86_ops->cache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001592
1593 nr = vcpu->regs[VCPU_REGS_RAX];
1594 a0 = vcpu->regs[VCPU_REGS_RBX];
1595 a1 = vcpu->regs[VCPU_REGS_RCX];
1596 a2 = vcpu->regs[VCPU_REGS_RDX];
1597 a3 = vcpu->regs[VCPU_REGS_RSI];
1598
1599 if (!is_long_mode(vcpu)) {
1600 nr &= 0xFFFFFFFF;
1601 a0 &= 0xFFFFFFFF;
1602 a1 &= 0xFFFFFFFF;
1603 a2 &= 0xFFFFFFFF;
1604 a3 &= 0xFFFFFFFF;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001605 }
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001606
Avi Kivity270fd9b2007-02-19 14:37:47 +02001607 switch (nr) {
1608 default:
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001609 ret = -KVM_ENOSYS;
1610 break;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001611 }
1612 vcpu->regs[VCPU_REGS_RAX] = ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001613 kvm_x86_ops->decache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001614 return 0;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001615}
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001616EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
1617
1618int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
1619{
1620 char instruction[3];
1621 int ret = 0;
1622
1623 mutex_lock(&vcpu->kvm->lock);
1624
1625 /*
1626 * Blow out the MMU to ensure that no other VCPU has an active mapping
1627 * to ensure that the updated hypercall appears atomically across all
1628 * VCPUs.
1629 */
1630 kvm_mmu_zap_all(vcpu->kvm);
1631
1632 kvm_x86_ops->cache_regs(vcpu);
1633 kvm_x86_ops->patch_hypercall(vcpu, instruction);
1634 if (emulator_write_emulated(vcpu->rip, instruction, 3, vcpu)
1635 != X86EMUL_CONTINUE)
1636 ret = -EFAULT;
1637
1638 mutex_unlock(&vcpu->kvm->lock);
1639
1640 return ret;
1641}
Avi Kivity270fd9b2007-02-19 14:37:47 +02001642
Avi Kivity6aa8b732006-12-10 02:21:36 -08001643static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1644{
1645 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1646}
1647
1648void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1649{
1650 struct descriptor_table dt = { limit, base };
1651
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001652 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001653}
1654
1655void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1656{
1657 struct descriptor_table dt = { limit, base };
1658
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001659 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001660}
1661
1662void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1663 unsigned long *rflags)
1664{
1665 lmsw(vcpu, msw);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001666 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001667}
1668
1669unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1670{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001671 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001672 switch (cr) {
1673 case 0:
1674 return vcpu->cr0;
1675 case 2:
1676 return vcpu->cr2;
1677 case 3:
1678 return vcpu->cr3;
1679 case 4:
1680 return vcpu->cr4;
1681 default:
1682 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1683 return 0;
1684 }
1685}
1686
1687void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1688 unsigned long *rflags)
1689{
1690 switch (cr) {
1691 case 0:
1692 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001693 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001694 break;
1695 case 2:
1696 vcpu->cr2 = val;
1697 break;
1698 case 3:
1699 set_cr3(vcpu, val);
1700 break;
1701 case 4:
1702 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1703 break;
1704 default:
1705 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1706 }
1707}
1708
Avi Kivity3bab1f52006-12-29 16:49:48 -08001709int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1710{
1711 u64 data;
1712
1713 switch (msr) {
1714 case 0xc0010010: /* SYSCFG */
1715 case 0xc0010015: /* HWCR */
1716 case MSR_IA32_PLATFORM_ID:
1717 case MSR_IA32_P5_MC_ADDR:
1718 case MSR_IA32_P5_MC_TYPE:
1719 case MSR_IA32_MC0_CTL:
1720 case MSR_IA32_MCG_STATUS:
1721 case MSR_IA32_MCG_CAP:
1722 case MSR_IA32_MC0_MISC:
1723 case MSR_IA32_MC0_MISC+4:
1724 case MSR_IA32_MC0_MISC+8:
1725 case MSR_IA32_MC0_MISC+12:
1726 case MSR_IA32_MC0_MISC+16:
1727 case MSR_IA32_UCODE_REV:
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001728 case MSR_IA32_PERF_STATUS:
Matthew Gregan2dc70942007-05-06 10:59:46 +03001729 case MSR_IA32_EBL_CR_POWERON:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001730 /* MTRR registers */
1731 case 0xfe:
1732 case 0x200 ... 0x2ff:
1733 data = 0;
1734 break;
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001735 case 0xcd: /* fsb frequency */
1736 data = 3;
1737 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001738 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001739 data = kvm_get_apic_base(vcpu);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001740 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001741 case MSR_IA32_MISC_ENABLE:
1742 data = vcpu->ia32_misc_enable_msr;
1743 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001744#ifdef CONFIG_X86_64
1745 case MSR_EFER:
1746 data = vcpu->shadow_efer;
1747 break;
1748#endif
1749 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001750 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001751 return 1;
1752 }
1753 *pdata = data;
1754 return 0;
1755}
1756EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1757
Avi Kivity6aa8b732006-12-10 02:21:36 -08001758/*
1759 * Reads an msr value (of 'msr_index') into 'pdata'.
1760 * Returns 0 on success, non-0 otherwise.
1761 * Assumes vcpu_load() was already called.
1762 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001763int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001764{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001765 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001766}
1767
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001768#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001769
Avi Kivity3bab1f52006-12-29 16:49:48 -08001770static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001771{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001772 if (efer & EFER_RESERVED_BITS) {
1773 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1774 efer);
1775 inject_gp(vcpu);
1776 return;
1777 }
1778
1779 if (is_paging(vcpu)
1780 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1781 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1782 inject_gp(vcpu);
1783 return;
1784 }
1785
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001786 kvm_x86_ops->set_efer(vcpu, efer);
Avi Kivity7725f0b2006-12-13 00:34:01 -08001787
Avi Kivity6aa8b732006-12-10 02:21:36 -08001788 efer &= ~EFER_LMA;
1789 efer |= vcpu->shadow_efer & EFER_LMA;
1790
1791 vcpu->shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001792}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001793
1794#endif
1795
Avi Kivity3bab1f52006-12-29 16:49:48 -08001796int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1797{
1798 switch (msr) {
1799#ifdef CONFIG_X86_64
1800 case MSR_EFER:
1801 set_efer(vcpu, data);
1802 break;
1803#endif
1804 case MSR_IA32_MC0_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001805 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
Avi Kivity3bab1f52006-12-29 16:49:48 -08001806 __FUNCTION__, data);
1807 break;
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001808 case MSR_IA32_MCG_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001809 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001810 __FUNCTION__, data);
1811 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001812 case MSR_IA32_UCODE_REV:
1813 case MSR_IA32_UCODE_WRITE:
1814 case 0x200 ... 0x2ff: /* MTRRs */
1815 break;
1816 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001817 kvm_set_apic_base(vcpu, data);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001818 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001819 case MSR_IA32_MISC_ENABLE:
1820 vcpu->ia32_misc_enable_msr = data;
1821 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001822 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001823 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001824 return 1;
1825 }
1826 return 0;
1827}
1828EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1829
Avi Kivity6aa8b732006-12-10 02:21:36 -08001830/*
1831 * Writes msr value into into the appropriate "register".
1832 * Returns 0 on success, non-0 otherwise.
1833 * Assumes vcpu_load() was already called.
1834 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001835int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001836{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001837 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001838}
1839
1840void kvm_resched(struct kvm_vcpu *vcpu)
1841{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001842 if (!need_resched())
1843 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001844 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001845}
1846EXPORT_SYMBOL_GPL(kvm_resched);
1847
Avi Kivity06465c52007-02-28 20:46:53 +02001848void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1849{
1850 int i;
1851 u32 function;
1852 struct kvm_cpuid_entry *e, *best;
1853
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001854 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001855 function = vcpu->regs[VCPU_REGS_RAX];
1856 vcpu->regs[VCPU_REGS_RAX] = 0;
1857 vcpu->regs[VCPU_REGS_RBX] = 0;
1858 vcpu->regs[VCPU_REGS_RCX] = 0;
1859 vcpu->regs[VCPU_REGS_RDX] = 0;
1860 best = NULL;
1861 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1862 e = &vcpu->cpuid_entries[i];
1863 if (e->function == function) {
1864 best = e;
1865 break;
1866 }
1867 /*
1868 * Both basic or both extended?
1869 */
1870 if (((e->function ^ function) & 0x80000000) == 0)
1871 if (!best || e->function > best->function)
1872 best = e;
1873 }
1874 if (best) {
1875 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1876 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1877 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1878 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1879 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001880 kvm_x86_ops->decache_regs(vcpu);
1881 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001882}
1883EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1884
Avi Kivity039576c2007-03-20 12:46:50 +02001885static int pio_copy_data(struct kvm_vcpu *vcpu)
Avi Kivity46fc1472007-02-22 19:39:30 +02001886{
Avi Kivity039576c2007-03-20 12:46:50 +02001887 void *p = vcpu->pio_data;
1888 void *q;
1889 unsigned bytes;
1890 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1891
Avi Kivity039576c2007-03-20 12:46:50 +02001892 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1893 PAGE_KERNEL);
1894 if (!q) {
Avi Kivity039576c2007-03-20 12:46:50 +02001895 free_pio_guest_pages(vcpu);
1896 return -ENOMEM;
1897 }
1898 q += vcpu->pio.guest_page_offset;
1899 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1900 if (vcpu->pio.in)
1901 memcpy(q, p, bytes);
1902 else
1903 memcpy(p, q, bytes);
1904 q -= vcpu->pio.guest_page_offset;
1905 vunmap(q);
Avi Kivity039576c2007-03-20 12:46:50 +02001906 free_pio_guest_pages(vcpu);
1907 return 0;
1908}
1909
1910static int complete_pio(struct kvm_vcpu *vcpu)
1911{
1912 struct kvm_pio_request *io = &vcpu->pio;
Avi Kivity46fc1472007-02-22 19:39:30 +02001913 long delta;
Avi Kivity039576c2007-03-20 12:46:50 +02001914 int r;
Avi Kivity46fc1472007-02-22 19:39:30 +02001915
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001916 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001917
1918 if (!io->string) {
Avi Kivity039576c2007-03-20 12:46:50 +02001919 if (io->in)
1920 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
Avi Kivity46fc1472007-02-22 19:39:30 +02001921 io->size);
1922 } else {
Avi Kivity039576c2007-03-20 12:46:50 +02001923 if (io->in) {
1924 r = pio_copy_data(vcpu);
1925 if (r) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001926 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02001927 return r;
1928 }
1929 }
1930
Avi Kivity46fc1472007-02-22 19:39:30 +02001931 delta = 1;
1932 if (io->rep) {
Avi Kivity039576c2007-03-20 12:46:50 +02001933 delta *= io->cur_count;
Avi Kivity46fc1472007-02-22 19:39:30 +02001934 /*
1935 * The size of the register should really depend on
1936 * current address size.
1937 */
1938 vcpu->regs[VCPU_REGS_RCX] -= delta;
1939 }
Avi Kivity039576c2007-03-20 12:46:50 +02001940 if (io->down)
Avi Kivity46fc1472007-02-22 19:39:30 +02001941 delta = -delta;
1942 delta *= io->size;
Avi Kivity039576c2007-03-20 12:46:50 +02001943 if (io->in)
Avi Kivity46fc1472007-02-22 19:39:30 +02001944 vcpu->regs[VCPU_REGS_RDI] += delta;
1945 else
1946 vcpu->regs[VCPU_REGS_RSI] += delta;
1947 }
1948
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001949 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001950
Avi Kivity039576c2007-03-20 12:46:50 +02001951 io->count -= io->cur_count;
1952 io->cur_count = 0;
1953
Avi Kivity039576c2007-03-20 12:46:50 +02001954 return 0;
Avi Kivity46fc1472007-02-22 19:39:30 +02001955}
1956
Eddie Dong65619eb2007-07-17 11:52:33 +03001957static void kernel_pio(struct kvm_io_device *pio_dev,
1958 struct kvm_vcpu *vcpu,
1959 void *pd)
Eddie Dong74906342007-06-19 18:05:03 +03001960{
1961 /* TODO: String I/O for in kernel device */
1962
Eddie Dong9cf98822007-07-22 10:36:31 +03001963 mutex_lock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001964 if (vcpu->pio.in)
1965 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1966 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001967 pd);
Eddie Dong74906342007-06-19 18:05:03 +03001968 else
1969 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1970 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001971 pd);
Eddie Dong9cf98822007-07-22 10:36:31 +03001972 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001973}
1974
1975static void pio_string_write(struct kvm_io_device *pio_dev,
1976 struct kvm_vcpu *vcpu)
1977{
1978 struct kvm_pio_request *io = &vcpu->pio;
1979 void *pd = vcpu->pio_data;
1980 int i;
1981
Eddie Dong9cf98822007-07-22 10:36:31 +03001982 mutex_lock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001983 for (i = 0; i < io->cur_count; i++) {
1984 kvm_iodevice_write(pio_dev, io->port,
1985 io->size,
1986 pd);
1987 pd += io->size;
1988 }
Eddie Dong9cf98822007-07-22 10:36:31 +03001989 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001990}
1991
Mike Dayd77c26f2007-10-08 09:02:08 -04001992int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
Laurent Vivier3090dd72007-08-05 10:43:32 +03001993 int size, unsigned port)
1994{
1995 struct kvm_io_device *pio_dev;
1996
1997 vcpu->run->exit_reason = KVM_EXIT_IO;
1998 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1999 vcpu->run->io.size = vcpu->pio.size = size;
2000 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
2001 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1;
2002 vcpu->run->io.port = vcpu->pio.port = port;
2003 vcpu->pio.in = in;
2004 vcpu->pio.string = 0;
2005 vcpu->pio.down = 0;
2006 vcpu->pio.guest_page_offset = 0;
2007 vcpu->pio.rep = 0;
2008
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002009 kvm_x86_ops->cache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03002010 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002011 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03002012
Avi Kivity0967b7b2007-09-15 17:34:36 +03002013 kvm_x86_ops->skip_emulated_instruction(vcpu);
2014
Laurent Vivier3090dd72007-08-05 10:43:32 +03002015 pio_dev = vcpu_find_pio_dev(vcpu, port);
2016 if (pio_dev) {
2017 kernel_pio(pio_dev, vcpu, vcpu->pio_data);
2018 complete_pio(vcpu);
2019 return 1;
2020 }
2021 return 0;
2022}
2023EXPORT_SYMBOL_GPL(kvm_emulate_pio);
2024
2025int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2026 int size, unsigned long count, int down,
Avi Kivity039576c2007-03-20 12:46:50 +02002027 gva_t address, int rep, unsigned port)
2028{
2029 unsigned now, in_page;
Eddie Dong65619eb2007-07-17 11:52:33 +03002030 int i, ret = 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002031 int nr_pages = 1;
2032 struct page *page;
Eddie Dong74906342007-06-19 18:05:03 +03002033 struct kvm_io_device *pio_dev;
Avi Kivity039576c2007-03-20 12:46:50 +02002034
2035 vcpu->run->exit_reason = KVM_EXIT_IO;
2036 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Laurent Vivier3090dd72007-08-05 10:43:32 +03002037 vcpu->run->io.size = vcpu->pio.size = size;
Avi Kivity039576c2007-03-20 12:46:50 +02002038 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Laurent Vivier3090dd72007-08-05 10:43:32 +03002039 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count;
2040 vcpu->run->io.port = vcpu->pio.port = port;
Avi Kivity039576c2007-03-20 12:46:50 +02002041 vcpu->pio.in = in;
Laurent Vivier3090dd72007-08-05 10:43:32 +03002042 vcpu->pio.string = 1;
Avi Kivity039576c2007-03-20 12:46:50 +02002043 vcpu->pio.down = down;
2044 vcpu->pio.guest_page_offset = offset_in_page(address);
2045 vcpu->pio.rep = rep;
2046
Avi Kivity039576c2007-03-20 12:46:50 +02002047 if (!count) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002048 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02002049 return 1;
2050 }
2051
Avi Kivity039576c2007-03-20 12:46:50 +02002052 if (!down)
2053 in_page = PAGE_SIZE - offset_in_page(address);
2054 else
2055 in_page = offset_in_page(address) + size;
2056 now = min(count, (unsigned long)in_page / size);
2057 if (!now) {
2058 /*
2059 * String I/O straddles page boundary. Pin two guest pages
2060 * so that we satisfy atomicity constraints. Do just one
2061 * transaction to avoid complexity.
2062 */
2063 nr_pages = 2;
2064 now = 1;
2065 }
2066 if (down) {
2067 /*
2068 * String I/O in reverse. Yuck. Kill the guest, fix later.
2069 */
Rusty Russellf0242472007-08-01 10:48:02 +10002070 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivity039576c2007-03-20 12:46:50 +02002071 inject_gp(vcpu);
2072 return 1;
2073 }
2074 vcpu->run->io.count = now;
2075 vcpu->pio.cur_count = now;
2076
Avi Kivity0967b7b2007-09-15 17:34:36 +03002077 if (vcpu->pio.cur_count == vcpu->pio.count)
2078 kvm_x86_ops->skip_emulated_instruction(vcpu);
2079
Avi Kivity039576c2007-03-20 12:46:50 +02002080 for (i = 0; i < nr_pages; ++i) {
Shaohua Li11ec2802007-07-23 14:51:37 +08002081 mutex_lock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02002082 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
Avi Kivity039576c2007-03-20 12:46:50 +02002083 vcpu->pio.guest_pages[i] = page;
Shaohua Li11ec2802007-07-23 14:51:37 +08002084 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02002085 if (!page) {
2086 inject_gp(vcpu);
2087 free_pio_guest_pages(vcpu);
2088 return 1;
2089 }
2090 }
2091
Laurent Vivier3090dd72007-08-05 10:43:32 +03002092 pio_dev = vcpu_find_pio_dev(vcpu, port);
Eddie Dong65619eb2007-07-17 11:52:33 +03002093 if (!vcpu->pio.in) {
2094 /* string PIO write */
2095 ret = pio_copy_data(vcpu);
2096 if (ret >= 0 && pio_dev) {
2097 pio_string_write(pio_dev, vcpu);
2098 complete_pio(vcpu);
2099 if (vcpu->pio.count == 0)
2100 ret = 1;
2101 }
2102 } else if (pio_dev)
Rusty Russellf0242472007-08-01 10:48:02 +10002103 pr_unimpl(vcpu, "no string pio read support yet, "
Eddie Dong65619eb2007-07-17 11:52:33 +03002104 "port %x size %d count %ld\n",
2105 port, size, count);
2106
2107 return ret;
Avi Kivity039576c2007-03-20 12:46:50 +02002108}
Laurent Vivier3090dd72007-08-05 10:43:32 +03002109EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
Avi Kivity039576c2007-03-20 12:46:50 +02002110
Avi Kivity04d2cc72007-09-10 18:10:54 +03002111/*
2112 * Check if userspace requested an interrupt window, and that the
2113 * interrupt window is open.
2114 *
2115 * No need to exit to userspace if we already have an interrupt queued.
2116 */
2117static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2118 struct kvm_run *kvm_run)
2119{
2120 return (!vcpu->irq_summary &&
2121 kvm_run->request_interrupt_window &&
2122 vcpu->interrupt_window_open &&
2123 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
2124}
2125
2126static void post_kvm_run_save(struct kvm_vcpu *vcpu,
2127 struct kvm_run *kvm_run)
2128{
2129 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
2130 kvm_run->cr8 = get_cr8(vcpu);
2131 kvm_run->apic_base = kvm_get_apic_base(vcpu);
2132 if (irqchip_in_kernel(vcpu->kvm))
2133 kvm_run->ready_for_interrupt_injection = 1;
2134 else
2135 kvm_run->ready_for_interrupt_injection =
2136 (vcpu->interrupt_window_open &&
2137 vcpu->irq_summary == 0);
2138}
2139
2140static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2141{
2142 int r;
2143
2144 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002145 pr_debug("vcpu %d received sipi with vector # %x\n",
Avi Kivity04d2cc72007-09-10 18:10:54 +03002146 vcpu->vcpu_id, vcpu->sipi_vector);
2147 kvm_lapic_reset(vcpu);
2148 kvm_x86_ops->vcpu_reset(vcpu);
2149 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
2150 }
2151
2152preempted:
2153 if (vcpu->guest_debug.enabled)
2154 kvm_x86_ops->guest_debug_pre(vcpu);
2155
2156again:
2157 r = kvm_mmu_reload(vcpu);
2158 if (unlikely(r))
2159 goto out;
2160
Avi Kivityab6ef342007-10-16 16:23:22 +02002161 kvm_inject_pending_timer_irqs(vcpu);
2162
Avi Kivity04d2cc72007-09-10 18:10:54 +03002163 preempt_disable();
2164
2165 kvm_x86_ops->prepare_guest_switch(vcpu);
2166 kvm_load_guest_fpu(vcpu);
2167
2168 local_irq_disable();
2169
2170 if (signal_pending(current)) {
2171 local_irq_enable();
2172 preempt_enable();
2173 r = -EINTR;
2174 kvm_run->exit_reason = KVM_EXIT_INTR;
2175 ++vcpu->stat.signal_exits;
2176 goto out;
2177 }
2178
2179 if (irqchip_in_kernel(vcpu->kvm))
2180 kvm_x86_ops->inject_pending_irq(vcpu);
2181 else if (!vcpu->mmio_read_completed)
2182 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2183
2184 vcpu->guest_mode = 1;
Laurent Vivierd172fcd2007-10-15 17:00:19 +02002185 kvm_guest_enter();
Avi Kivity04d2cc72007-09-10 18:10:54 +03002186
2187 if (vcpu->requests)
Avi Kivity3176bc32007-10-16 17:22:08 +02002188 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivity04d2cc72007-09-10 18:10:54 +03002189 kvm_x86_ops->tlb_flush(vcpu);
2190
2191 kvm_x86_ops->run(vcpu, kvm_run);
2192
2193 vcpu->guest_mode = 0;
2194 local_irq_enable();
2195
2196 ++vcpu->stat.exits;
2197
Laurent Vivier0552f732007-10-18 15:19:01 +02002198 /*
2199 * We must have an instruction between local_irq_enable() and
2200 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2201 * the interrupt shadow. The stat.exits increment will do nicely.
2202 * But we need to prevent reordering, hence this barrier():
2203 */
2204 barrier();
2205
2206 kvm_guest_exit();
2207
Avi Kivity04d2cc72007-09-10 18:10:54 +03002208 preempt_enable();
2209
2210 /*
2211 * Profile KVM exit RIPs:
2212 */
2213 if (unlikely(prof_on == KVM_PROFILING)) {
2214 kvm_x86_ops->cache_regs(vcpu);
2215 profile_hit(KVM_PROFILING, (void *)vcpu->rip);
2216 }
2217
2218 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2219
2220 if (r > 0) {
2221 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2222 r = -EINTR;
2223 kvm_run->exit_reason = KVM_EXIT_INTR;
2224 ++vcpu->stat.request_irq_exits;
2225 goto out;
2226 }
2227 if (!need_resched()) {
2228 ++vcpu->stat.light_exits;
2229 goto again;
2230 }
2231 }
2232
2233out:
2234 if (r > 0) {
2235 kvm_resched(vcpu);
2236 goto preempted;
2237 }
2238
2239 post_kvm_run_save(vcpu, kvm_run);
2240
2241 return r;
2242}
2243
2244
Avi Kivitybccf2152007-02-21 18:04:26 +02002245static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002246{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002247 int r;
Avi Kivity1961d272007-03-05 19:46:05 +02002248 sigset_t sigsaved;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002249
Avi Kivitybccf2152007-02-21 18:04:26 +02002250 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002251
He, Qingc5ec1532007-09-03 17:07:41 +03002252 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
2253 kvm_vcpu_block(vcpu);
2254 vcpu_put(vcpu);
2255 return -EAGAIN;
2256 }
2257
Avi Kivity1961d272007-03-05 19:46:05 +02002258 if (vcpu->sigset_active)
2259 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2260
Dor Laor54810342007-02-12 00:54:39 -08002261 /* re-sync apic's tpr */
He, Qing5cd4f6f2007-08-30 17:04:26 +08002262 if (!irqchip_in_kernel(vcpu->kvm))
2263 set_cr8(vcpu, kvm_run->cr8);
Dor Laor54810342007-02-12 00:54:39 -08002264
Avi Kivity02c83202007-04-29 15:02:17 +03002265 if (vcpu->pio.cur_count) {
2266 r = complete_pio(vcpu);
2267 if (r)
2268 goto out;
2269 }
2270
2271 if (vcpu->mmio_needed) {
2272 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
2273 vcpu->mmio_read_completed = 1;
2274 vcpu->mmio_needed = 0;
2275 r = emulate_instruction(vcpu, kvm_run,
Laurent Vivier34273182007-09-18 11:27:37 +02002276 vcpu->mmio_fault_cr2, 0, 1);
Avi Kivity02c83202007-04-29 15:02:17 +03002277 if (r == EMULATE_DO_MMIO) {
2278 /*
2279 * Read-modify-write. Back to userspace.
2280 */
Avi Kivity02c83202007-04-29 15:02:17 +03002281 r = 0;
2282 goto out;
Avi Kivity46fc1472007-02-22 19:39:30 +02002283 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002284 }
2285
Avi Kivity8eb7d332007-03-04 14:17:08 +02002286 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002287 kvm_x86_ops->cache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002288 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002289 kvm_x86_ops->decache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002290 }
2291
Avi Kivity04d2cc72007-09-10 18:10:54 +03002292 r = __vcpu_run(vcpu, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002293
Avi Kivity039576c2007-03-20 12:46:50 +02002294out:
Avi Kivity1961d272007-03-05 19:46:05 +02002295 if (vcpu->sigset_active)
2296 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2297
Avi Kivity6aa8b732006-12-10 02:21:36 -08002298 vcpu_put(vcpu);
2299 return r;
2300}
2301
Avi Kivitybccf2152007-02-21 18:04:26 +02002302static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
2303 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002304{
Avi Kivitybccf2152007-02-21 18:04:26 +02002305 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002306
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002307 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002308
2309 regs->rax = vcpu->regs[VCPU_REGS_RAX];
2310 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
2311 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
2312 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
2313 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
2314 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
2315 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
2316 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002317#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002318 regs->r8 = vcpu->regs[VCPU_REGS_R8];
2319 regs->r9 = vcpu->regs[VCPU_REGS_R9];
2320 regs->r10 = vcpu->regs[VCPU_REGS_R10];
2321 regs->r11 = vcpu->regs[VCPU_REGS_R11];
2322 regs->r12 = vcpu->regs[VCPU_REGS_R12];
2323 regs->r13 = vcpu->regs[VCPU_REGS_R13];
2324 regs->r14 = vcpu->regs[VCPU_REGS_R14];
2325 regs->r15 = vcpu->regs[VCPU_REGS_R15];
2326#endif
2327
2328 regs->rip = vcpu->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002329 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002330
2331 /*
2332 * Don't leak debug flags in case they were set for guest debugging
2333 */
2334 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2335 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2336
2337 vcpu_put(vcpu);
2338
2339 return 0;
2340}
2341
Avi Kivitybccf2152007-02-21 18:04:26 +02002342static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
2343 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002344{
Avi Kivitybccf2152007-02-21 18:04:26 +02002345 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002346
2347 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
2348 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
2349 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
2350 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
2351 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
2352 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
2353 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
2354 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002355#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002356 vcpu->regs[VCPU_REGS_R8] = regs->r8;
2357 vcpu->regs[VCPU_REGS_R9] = regs->r9;
2358 vcpu->regs[VCPU_REGS_R10] = regs->r10;
2359 vcpu->regs[VCPU_REGS_R11] = regs->r11;
2360 vcpu->regs[VCPU_REGS_R12] = regs->r12;
2361 vcpu->regs[VCPU_REGS_R13] = regs->r13;
2362 vcpu->regs[VCPU_REGS_R14] = regs->r14;
2363 vcpu->regs[VCPU_REGS_R15] = regs->r15;
2364#endif
2365
2366 vcpu->rip = regs->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002367 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002368
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002369 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002370
2371 vcpu_put(vcpu);
2372
2373 return 0;
2374}
2375
2376static void get_segment(struct kvm_vcpu *vcpu,
2377 struct kvm_segment *var, int seg)
2378{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002379 return kvm_x86_ops->get_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002380}
2381
Avi Kivitybccf2152007-02-21 18:04:26 +02002382static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2383 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002384{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002385 struct descriptor_table dt;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002386 int pending_vec;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002387
Avi Kivitybccf2152007-02-21 18:04:26 +02002388 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002389
2390 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2391 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2392 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2393 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2394 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2395 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2396
2397 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2398 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2399
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002400 kvm_x86_ops->get_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002401 sregs->idt.limit = dt.limit;
2402 sregs->idt.base = dt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002403 kvm_x86_ops->get_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002404 sregs->gdt.limit = dt.limit;
2405 sregs->gdt.base = dt.base;
2406
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002407 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002408 sregs->cr0 = vcpu->cr0;
2409 sregs->cr2 = vcpu->cr2;
2410 sregs->cr3 = vcpu->cr3;
2411 sregs->cr4 = vcpu->cr4;
Eddie Dong7017fc32007-07-18 11:34:57 +03002412 sregs->cr8 = get_cr8(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002413 sregs->efer = vcpu->shadow_efer;
Eddie Dong7017fc32007-07-18 11:34:57 +03002414 sregs->apic_base = kvm_get_apic_base(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002415
Eddie Dong2a8067f2007-08-06 16:29:07 +03002416 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc52fb352007-08-02 14:03:07 +03002417 memset(sregs->interrupt_bitmap, 0,
2418 sizeof sregs->interrupt_bitmap);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002419 pending_vec = kvm_x86_ops->get_irq(vcpu);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002420 if (pending_vec >= 0)
Mike Dayd77c26f2007-10-08 09:02:08 -04002421 set_bit(pending_vec,
2422 (unsigned long *)sregs->interrupt_bitmap);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002423 } else
He, Qingc52fb352007-08-02 14:03:07 +03002424 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2425 sizeof sregs->interrupt_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002426
2427 vcpu_put(vcpu);
2428
2429 return 0;
2430}
2431
2432static void set_segment(struct kvm_vcpu *vcpu,
2433 struct kvm_segment *var, int seg)
2434{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002435 return kvm_x86_ops->set_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002436}
2437
Avi Kivitybccf2152007-02-21 18:04:26 +02002438static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2439 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002440{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002441 int mmu_reset_needed = 0;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002442 int i, pending_vec, max_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002443 struct descriptor_table dt;
2444
Avi Kivitybccf2152007-02-21 18:04:26 +02002445 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002446
Avi Kivity6aa8b732006-12-10 02:21:36 -08002447 dt.limit = sregs->idt.limit;
2448 dt.base = sregs->idt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002449 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002450 dt.limit = sregs->gdt.limit;
2451 dt.base = sregs->gdt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002452 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002453
2454 vcpu->cr2 = sregs->cr2;
2455 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2456 vcpu->cr3 = sregs->cr3;
2457
Eddie Dong7017fc32007-07-18 11:34:57 +03002458 set_cr8(vcpu, sregs->cr8);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002459
2460 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002461#ifdef CONFIG_X86_64
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002462 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002463#endif
Eddie Dong7017fc32007-07-18 11:34:57 +03002464 kvm_set_apic_base(vcpu, sregs->apic_base);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002465
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002466 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity399badf2007-01-05 16:36:38 -08002467
Avi Kivity6aa8b732006-12-10 02:21:36 -08002468 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
Rusty Russell81f50e32007-09-06 01:20:38 +10002469 vcpu->cr0 = sregs->cr0;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002470 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002471
2472 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002473 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Avi Kivity1b0973b2007-01-05 16:36:41 -08002474 if (!is_long_mode(vcpu) && is_pae(vcpu))
2475 load_pdptrs(vcpu, vcpu->cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002476
2477 if (mmu_reset_needed)
2478 kvm_mmu_reset_context(vcpu);
2479
He, Qingc52fb352007-08-02 14:03:07 +03002480 if (!irqchip_in_kernel(vcpu->kvm)) {
2481 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2482 sizeof vcpu->irq_pending);
2483 vcpu->irq_summary = 0;
2484 for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
2485 if (vcpu->irq_pending[i])
2486 __set_bit(i, &vcpu->irq_summary);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002487 } else {
2488 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
2489 pending_vec = find_first_bit(
2490 (const unsigned long *)sregs->interrupt_bitmap,
2491 max_bits);
2492 /* Only pending external irq is handled here */
2493 if (pending_vec < max_bits) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002494 kvm_x86_ops->set_irq(vcpu, pending_vec);
Mike Dayd77c26f2007-10-08 09:02:08 -04002495 pr_debug("Set back pending irq %d\n",
2496 pending_vec);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002497 }
He, Qingc52fb352007-08-02 14:03:07 +03002498 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002499
Avi Kivity024aa1c2007-03-21 13:44:58 +02002500 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2501 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2502 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2503 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2504 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2505 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2506
2507 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2508 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2509
Avi Kivity6aa8b732006-12-10 02:21:36 -08002510 vcpu_put(vcpu);
2511
2512 return 0;
2513}
2514
Rusty Russell1747fb72007-09-06 01:21:32 +10002515void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2516{
2517 struct kvm_segment cs;
2518
2519 get_segment(vcpu, &cs, VCPU_SREG_CS);
2520 *db = cs.db;
2521 *l = cs.l;
2522}
2523EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2524
Avi Kivity6aa8b732006-12-10 02:21:36 -08002525/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002526 * Translate a guest virtual address to a guest physical address.
2527 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002528static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2529 struct kvm_translation *tr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002530{
2531 unsigned long vaddr = tr->linear_address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002532 gpa_t gpa;
2533
Avi Kivitybccf2152007-02-21 18:04:26 +02002534 vcpu_load(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +08002535 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002536 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2537 tr->physical_address = gpa;
2538 tr->valid = gpa != UNMAPPED_GVA;
2539 tr->writeable = 1;
2540 tr->usermode = 0;
Shaohua Li11ec2802007-07-23 14:51:37 +08002541 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002542 vcpu_put(vcpu);
2543
2544 return 0;
2545}
2546
Avi Kivitybccf2152007-02-21 18:04:26 +02002547static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2548 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002549{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002550 if (irq->irq < 0 || irq->irq >= 256)
2551 return -EINVAL;
Eddie Dong97222cc2007-09-12 10:58:04 +03002552 if (irqchip_in_kernel(vcpu->kvm))
2553 return -ENXIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02002554 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002555
2556 set_bit(irq->irq, vcpu->irq_pending);
2557 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2558
2559 vcpu_put(vcpu);
2560
2561 return 0;
2562}
2563
Avi Kivitybccf2152007-02-21 18:04:26 +02002564static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2565 struct kvm_debug_guest *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002566{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002567 int r;
2568
Avi Kivitybccf2152007-02-21 18:04:26 +02002569 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002570
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002571 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002572
2573 vcpu_put(vcpu);
2574
2575 return r;
2576}
2577
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002578static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2579 unsigned long address,
2580 int *type)
2581{
2582 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2583 unsigned long pgoff;
2584 struct page *page;
2585
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002586 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +02002587 if (pgoff == 0)
2588 page = virt_to_page(vcpu->run);
2589 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2590 page = virt_to_page(vcpu->pio_data);
2591 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002592 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002593 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002594 if (type != NULL)
2595 *type = VM_FAULT_MINOR;
2596
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002597 return page;
2598}
2599
2600static struct vm_operations_struct kvm_vcpu_vm_ops = {
2601 .nopage = kvm_vcpu_nopage,
2602};
2603
2604static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2605{
2606 vma->vm_ops = &kvm_vcpu_vm_ops;
2607 return 0;
2608}
2609
Avi Kivitybccf2152007-02-21 18:04:26 +02002610static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2611{
2612 struct kvm_vcpu *vcpu = filp->private_data;
2613
2614 fput(vcpu->kvm->filp);
2615 return 0;
2616}
2617
2618static struct file_operations kvm_vcpu_fops = {
2619 .release = kvm_vcpu_release,
2620 .unlocked_ioctl = kvm_vcpu_ioctl,
2621 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002622 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02002623};
2624
2625/*
2626 * Allocates an inode for the vcpu.
2627 */
2628static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2629{
2630 int fd, r;
2631 struct inode *inode;
2632 struct file *file;
2633
Avi Kivityd6d28162007-06-28 08:38:16 -04002634 r = anon_inode_getfd(&fd, &inode, &file,
2635 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2636 if (r)
2637 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002638 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +02002639 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02002640}
2641
Avi Kivityc5ea7662007-02-20 18:41:05 +02002642/*
2643 * Creates some virtual cpus. Good luck creating more than one.
2644 */
2645static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2646{
2647 int r;
2648 struct kvm_vcpu *vcpu;
2649
Avi Kivityc5ea7662007-02-20 18:41:05 +02002650 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002651 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002652
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002653 vcpu = kvm_x86_ops->vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002654 if (IS_ERR(vcpu))
2655 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002656
Avi Kivity15ad7142007-07-11 18:17:21 +03002657 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
2658
Rusty Russellb114b082007-07-30 21:13:43 +10002659 /* We do fxsave: this must be aligned. */
2660 BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF);
2661
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002662 vcpu_load(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002663 r = kvm_mmu_setup(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002664 vcpu_put(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002665 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002666 goto free_vcpu;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002667
Shaohua Li11ec2802007-07-23 14:51:37 +08002668 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002669 if (kvm->vcpus[n]) {
2670 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +08002671 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002672 goto mmu_unload;
2673 }
2674 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08002675 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002676
2677 /* Now it's all set up, let userspace reach it */
Avi Kivitybccf2152007-02-21 18:04:26 +02002678 r = create_vcpu_fd(vcpu);
2679 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002680 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02002681 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002682
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002683unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08002684 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002685 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +08002686 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002687
2688mmu_unload:
2689 vcpu_load(vcpu);
2690 kvm_mmu_unload(vcpu);
2691 vcpu_put(vcpu);
2692
2693free_vcpu:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002694 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002695 return r;
2696}
2697
Avi Kivity1961d272007-03-05 19:46:05 +02002698static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2699{
2700 if (sigset) {
2701 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2702 vcpu->sigset_active = 1;
2703 vcpu->sigset = *sigset;
2704 } else
2705 vcpu->sigset_active = 0;
2706 return 0;
2707}
2708
Avi Kivityb8836732007-04-01 16:34:31 +03002709/*
2710 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2711 * we have asm/x86/processor.h
2712 */
2713struct fxsave {
2714 u16 cwd;
2715 u16 swd;
2716 u16 twd;
2717 u16 fop;
2718 u64 rip;
2719 u64 rdp;
2720 u32 mxcsr;
2721 u32 mxcsr_mask;
2722 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2723#ifdef CONFIG_X86_64
2724 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2725#else
2726 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2727#endif
2728};
2729
2730static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2731{
Rusty Russellb114b082007-07-30 21:13:43 +10002732 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002733
2734 vcpu_load(vcpu);
2735
2736 memcpy(fpu->fpr, fxsave->st_space, 128);
2737 fpu->fcw = fxsave->cwd;
2738 fpu->fsw = fxsave->swd;
2739 fpu->ftwx = fxsave->twd;
2740 fpu->last_opcode = fxsave->fop;
2741 fpu->last_ip = fxsave->rip;
2742 fpu->last_dp = fxsave->rdp;
2743 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2744
2745 vcpu_put(vcpu);
2746
2747 return 0;
2748}
2749
2750static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2751{
Rusty Russellb114b082007-07-30 21:13:43 +10002752 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002753
2754 vcpu_load(vcpu);
2755
2756 memcpy(fxsave->st_space, fpu->fpr, 128);
2757 fxsave->cwd = fpu->fcw;
2758 fxsave->swd = fpu->fsw;
2759 fxsave->twd = fpu->ftwx;
2760 fxsave->fop = fpu->last_opcode;
2761 fxsave->rip = fpu->last_ip;
2762 fxsave->rdp = fpu->last_dp;
2763 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2764
2765 vcpu_put(vcpu);
2766
2767 return 0;
2768}
2769
Avi Kivitybccf2152007-02-21 18:04:26 +02002770static long kvm_vcpu_ioctl(struct file *filp,
2771 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002772{
Avi Kivitybccf2152007-02-21 18:04:26 +02002773 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +00002774 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02002775 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002776
2777 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002778 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002779 r = -EINVAL;
2780 if (arg)
2781 goto out;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002782 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002783 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002784 case KVM_GET_REGS: {
2785 struct kvm_regs kvm_regs;
2786
Avi Kivitybccf2152007-02-21 18:04:26 +02002787 memset(&kvm_regs, 0, sizeof kvm_regs);
2788 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002789 if (r)
2790 goto out;
2791 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002792 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002793 goto out;
2794 r = 0;
2795 break;
2796 }
2797 case KVM_SET_REGS: {
2798 struct kvm_regs kvm_regs;
2799
2800 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002801 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002802 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002803 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002804 if (r)
2805 goto out;
2806 r = 0;
2807 break;
2808 }
2809 case KVM_GET_SREGS: {
2810 struct kvm_sregs kvm_sregs;
2811
Avi Kivitybccf2152007-02-21 18:04:26 +02002812 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2813 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002814 if (r)
2815 goto out;
2816 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002817 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002818 goto out;
2819 r = 0;
2820 break;
2821 }
2822 case KVM_SET_SREGS: {
2823 struct kvm_sregs kvm_sregs;
2824
2825 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002826 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002827 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002828 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002829 if (r)
2830 goto out;
2831 r = 0;
2832 break;
2833 }
2834 case KVM_TRANSLATE: {
2835 struct kvm_translation tr;
2836
2837 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002838 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002839 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002840 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002841 if (r)
2842 goto out;
2843 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002844 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002845 goto out;
2846 r = 0;
2847 break;
2848 }
2849 case KVM_INTERRUPT: {
2850 struct kvm_interrupt irq;
2851
2852 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002853 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002854 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002855 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002856 if (r)
2857 goto out;
2858 r = 0;
2859 break;
2860 }
2861 case KVM_DEBUG_GUEST: {
2862 struct kvm_debug_guest dbg;
2863
2864 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002865 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002866 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002867 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002868 if (r)
2869 goto out;
2870 r = 0;
2871 break;
2872 }
Avi Kivity1961d272007-03-05 19:46:05 +02002873 case KVM_SET_SIGNAL_MASK: {
2874 struct kvm_signal_mask __user *sigmask_arg = argp;
2875 struct kvm_signal_mask kvm_sigmask;
2876 sigset_t sigset, *p;
2877
2878 p = NULL;
2879 if (argp) {
2880 r = -EFAULT;
2881 if (copy_from_user(&kvm_sigmask, argp,
2882 sizeof kvm_sigmask))
2883 goto out;
2884 r = -EINVAL;
2885 if (kvm_sigmask.len != sizeof sigset)
2886 goto out;
2887 r = -EFAULT;
2888 if (copy_from_user(&sigset, sigmask_arg->sigset,
2889 sizeof sigset))
2890 goto out;
2891 p = &sigset;
2892 }
2893 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2894 break;
2895 }
Avi Kivityb8836732007-04-01 16:34:31 +03002896 case KVM_GET_FPU: {
2897 struct kvm_fpu fpu;
2898
2899 memset(&fpu, 0, sizeof fpu);
2900 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2901 if (r)
2902 goto out;
2903 r = -EFAULT;
2904 if (copy_to_user(argp, &fpu, sizeof fpu))
2905 goto out;
2906 r = 0;
2907 break;
2908 }
2909 case KVM_SET_FPU: {
2910 struct kvm_fpu fpu;
2911
2912 r = -EFAULT;
2913 if (copy_from_user(&fpu, argp, sizeof fpu))
2914 goto out;
2915 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2916 if (r)
2917 goto out;
2918 r = 0;
2919 break;
2920 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002921 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02002922 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02002923 }
2924out:
2925 return r;
2926}
2927
2928static long kvm_vm_ioctl(struct file *filp,
2929 unsigned int ioctl, unsigned long arg)
2930{
2931 struct kvm *kvm = filp->private_data;
2932 void __user *argp = (void __user *)arg;
2933 int r = -EINVAL;
2934
2935 switch (ioctl) {
2936 case KVM_CREATE_VCPU:
2937 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2938 if (r < 0)
2939 goto out;
2940 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002941 case KVM_SET_MEMORY_REGION: {
2942 struct kvm_memory_region kvm_mem;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002943 struct kvm_userspace_memory_region kvm_userspace_mem;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002944
2945 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002946 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002947 goto out;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002948 kvm_userspace_mem.slot = kvm_mem.slot;
2949 kvm_userspace_mem.flags = kvm_mem.flags;
2950 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
2951 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
2952 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
2953 if (r)
2954 goto out;
2955 break;
2956 }
2957 case KVM_SET_USER_MEMORY_REGION: {
2958 struct kvm_userspace_memory_region kvm_userspace_mem;
2959
2960 r = -EFAULT;
2961 if (copy_from_user(&kvm_userspace_mem, argp,
2962 sizeof kvm_userspace_mem))
2963 goto out;
2964
2965 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002966 if (r)
2967 goto out;
2968 break;
2969 }
Izik Eidus82ce2c92007-10-02 18:52:55 +02002970 case KVM_SET_NR_MMU_PAGES:
2971 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
2972 if (r)
2973 goto out;
2974 break;
2975 case KVM_GET_NR_MMU_PAGES:
2976 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
2977 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002978 case KVM_GET_DIRTY_LOG: {
2979 struct kvm_dirty_log log;
2980
2981 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002982 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002983 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002984 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002985 if (r)
2986 goto out;
2987 break;
2988 }
Avi Kivitye8207542007-03-30 16:54:30 +03002989 case KVM_SET_MEMORY_ALIAS: {
2990 struct kvm_memory_alias alias;
2991
2992 r = -EFAULT;
2993 if (copy_from_user(&alias, argp, sizeof alias))
2994 goto out;
2995 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
2996 if (r)
2997 goto out;
2998 break;
2999 }
Eddie Dong85f455f2007-07-06 12:20:49 +03003000 case KVM_CREATE_IRQCHIP:
3001 r = -ENOMEM;
3002 kvm->vpic = kvm_create_pic(kvm);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03003003 if (kvm->vpic) {
3004 r = kvm_ioapic_init(kvm);
3005 if (r) {
3006 kfree(kvm->vpic);
3007 kvm->vpic = NULL;
3008 goto out;
3009 }
Mike Dayd77c26f2007-10-08 09:02:08 -04003010 } else
Eddie Dong85f455f2007-07-06 12:20:49 +03003011 goto out;
3012 break;
3013 case KVM_IRQ_LINE: {
3014 struct kvm_irq_level irq_event;
3015
3016 r = -EFAULT;
3017 if (copy_from_user(&irq_event, argp, sizeof irq_event))
3018 goto out;
3019 if (irqchip_in_kernel(kvm)) {
Eddie Dong9cf98822007-07-22 10:36:31 +03003020 mutex_lock(&kvm->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +03003021 if (irq_event.irq < 16)
3022 kvm_pic_set_irq(pic_irqchip(kvm),
3023 irq_event.irq,
3024 irq_event.level);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03003025 kvm_ioapic_set_irq(kvm->vioapic,
3026 irq_event.irq,
3027 irq_event.level);
Eddie Dong9cf98822007-07-22 10:36:31 +03003028 mutex_unlock(&kvm->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +03003029 r = 0;
3030 }
3031 break;
3032 }
He, Qing6ceb9d72007-07-26 11:05:18 +03003033 case KVM_GET_IRQCHIP: {
3034 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3035 struct kvm_irqchip chip;
3036
3037 r = -EFAULT;
3038 if (copy_from_user(&chip, argp, sizeof chip))
3039 goto out;
3040 r = -ENXIO;
3041 if (!irqchip_in_kernel(kvm))
3042 goto out;
3043 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
3044 if (r)
3045 goto out;
3046 r = -EFAULT;
3047 if (copy_to_user(argp, &chip, sizeof chip))
3048 goto out;
3049 r = 0;
3050 break;
3051 }
3052 case KVM_SET_IRQCHIP: {
3053 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3054 struct kvm_irqchip chip;
3055
3056 r = -EFAULT;
3057 if (copy_from_user(&chip, argp, sizeof chip))
3058 goto out;
3059 r = -ENXIO;
3060 if (!irqchip_in_kernel(kvm))
3061 goto out;
3062 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
3063 if (r)
3064 goto out;
3065 r = 0;
3066 break;
3067 }
Avi Kivityf17abe92007-02-21 19:28:04 +02003068 default:
3069 ;
3070 }
3071out:
3072 return r;
3073}
3074
3075static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
3076 unsigned long address,
3077 int *type)
3078{
3079 struct kvm *kvm = vma->vm_file->private_data;
3080 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02003081 struct page *page;
3082
Avi Kivityf17abe92007-02-21 19:28:04 +02003083 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity954bbbc22007-03-30 14:02:32 +03003084 page = gfn_to_page(kvm, pgoff);
Izik Eidus8a7ae052007-10-18 11:09:33 +02003085 if (is_error_page(page)) {
3086 kvm_release_page(page);
Avi Kivityf17abe92007-02-21 19:28:04 +02003087 return NOPAGE_SIGBUS;
Izik Eidus8a7ae052007-10-18 11:09:33 +02003088 }
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03003089 if (type != NULL)
3090 *type = VM_FAULT_MINOR;
3091
Avi Kivityf17abe92007-02-21 19:28:04 +02003092 return page;
3093}
3094
3095static struct vm_operations_struct kvm_vm_vm_ops = {
3096 .nopage = kvm_vm_nopage,
3097};
3098
3099static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
3100{
3101 vma->vm_ops = &kvm_vm_vm_ops;
3102 return 0;
3103}
3104
3105static struct file_operations kvm_vm_fops = {
3106 .release = kvm_vm_release,
3107 .unlocked_ioctl = kvm_vm_ioctl,
3108 .compat_ioctl = kvm_vm_ioctl,
3109 .mmap = kvm_vm_mmap,
3110};
3111
3112static int kvm_dev_ioctl_create_vm(void)
3113{
3114 int fd, r;
3115 struct inode *inode;
3116 struct file *file;
3117 struct kvm *kvm;
3118
Avi Kivityf17abe92007-02-21 19:28:04 +02003119 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04003120 if (IS_ERR(kvm))
3121 return PTR_ERR(kvm);
3122 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
3123 if (r) {
3124 kvm_destroy_vm(kvm);
3125 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02003126 }
3127
Avi Kivitybccf2152007-02-21 18:04:26 +02003128 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02003129
Avi Kivityf17abe92007-02-21 19:28:04 +02003130 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02003131}
3132
3133static long kvm_dev_ioctl(struct file *filp,
3134 unsigned int ioctl, unsigned long arg)
3135{
3136 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02003137 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02003138
3139 switch (ioctl) {
3140 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02003141 r = -EINVAL;
3142 if (arg)
3143 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02003144 r = KVM_API_VERSION;
3145 break;
3146 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02003147 r = -EINVAL;
3148 if (arg)
3149 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02003150 r = kvm_dev_ioctl_create_vm();
3151 break;
Eddie Dong85f455f2007-07-06 12:20:49 +03003152 case KVM_CHECK_EXTENSION: {
3153 int ext = (long)argp;
3154
3155 switch (ext) {
3156 case KVM_CAP_IRQCHIP:
Eddie Dongb6958ce2007-07-18 12:15:21 +03003157 case KVM_CAP_HLT:
Izik Eidus82ce2c92007-10-02 18:52:55 +02003158 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Izik Eidus6fc138d2007-10-09 19:20:39 +02003159 case KVM_CAP_USER_MEMORY:
Eddie Dong85f455f2007-07-06 12:20:49 +03003160 r = 1;
3161 break;
3162 default:
3163 r = 0;
3164 break;
3165 }
Avi Kivity5d308f42007-03-01 17:56:20 +02003166 break;
Eddie Dong85f455f2007-07-06 12:20:49 +03003167 }
Avi Kivity07c45a32007-03-07 13:05:38 +02003168 case KVM_GET_VCPU_MMAP_SIZE:
3169 r = -EINVAL;
3170 if (arg)
3171 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02003172 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02003173 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003174 default:
Carsten Otte043405e2007-10-10 17:16:19 +02003175 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003176 }
3177out:
3178 return r;
3179}
3180
Avi Kivity6aa8b732006-12-10 02:21:36 -08003181static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003182 .unlocked_ioctl = kvm_dev_ioctl,
3183 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003184};
3185
3186static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02003187 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003188 "kvm",
3189 &kvm_chardev_ops,
3190};
3191
Avi Kivity774c47f2007-02-12 00:54:47 -08003192/*
3193 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
3194 * cached on it.
3195 */
3196static void decache_vcpus_on_cpu(int cpu)
3197{
3198 struct kvm *vm;
3199 struct kvm_vcpu *vcpu;
3200 int i;
3201
3202 spin_lock(&kvm_lock);
Shaohua Li11ec2802007-07-23 14:51:37 +08003203 list_for_each_entry(vm, &vm_list, vm_list)
Avi Kivity774c47f2007-02-12 00:54:47 -08003204 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003205 vcpu = vm->vcpus[i];
3206 if (!vcpu)
3207 continue;
Avi Kivity774c47f2007-02-12 00:54:47 -08003208 /*
3209 * If the vcpu is locked, then it is running on some
3210 * other cpu and therefore it is not cached on the
3211 * cpu in question.
3212 *
3213 * If it's not locked, check the last cpu it executed
3214 * on.
3215 */
3216 if (mutex_trylock(&vcpu->mutex)) {
3217 if (vcpu->cpu == cpu) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003218 kvm_x86_ops->vcpu_decache(vcpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08003219 vcpu->cpu = -1;
3220 }
3221 mutex_unlock(&vcpu->mutex);
3222 }
3223 }
3224 spin_unlock(&kvm_lock);
3225}
3226
Avi Kivity1b6c0162007-05-24 13:03:52 +03003227static void hardware_enable(void *junk)
3228{
3229 int cpu = raw_smp_processor_id();
3230
3231 if (cpu_isset(cpu, cpus_hardware_enabled))
3232 return;
3233 cpu_set(cpu, cpus_hardware_enabled);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003234 kvm_x86_ops->hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003235}
3236
3237static void hardware_disable(void *junk)
3238{
3239 int cpu = raw_smp_processor_id();
3240
3241 if (!cpu_isset(cpu, cpus_hardware_enabled))
3242 return;
3243 cpu_clear(cpu, cpus_hardware_enabled);
3244 decache_vcpus_on_cpu(cpu);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003245 kvm_x86_ops->hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003246}
3247
Avi Kivity774c47f2007-02-12 00:54:47 -08003248static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
3249 void *v)
3250{
3251 int cpu = (long)v;
3252
3253 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03003254 case CPU_DYING:
3255 case CPU_DYING_FROZEN:
Avi Kivity6ec8a852007-08-19 15:57:26 +03003256 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3257 cpu);
3258 hardware_disable(NULL);
3259 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08003260 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003261 case CPU_UP_CANCELED_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003262 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3263 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003264 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003265 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02003266 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003267 case CPU_ONLINE_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003268 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
3269 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003270 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003271 break;
3272 }
3273 return NOTIFY_OK;
3274}
3275
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003276static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04003277 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003278{
3279 if (val == SYS_RESTART) {
3280 /*
3281 * Some (well, at least mine) BIOSes hang on reboot if
3282 * in vmx root mode.
3283 */
3284 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
3285 on_each_cpu(hardware_disable, NULL, 0, 1);
3286 }
3287 return NOTIFY_OK;
3288}
3289
3290static struct notifier_block kvm_reboot_notifier = {
3291 .notifier_call = kvm_reboot,
3292 .priority = 0,
3293};
3294
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04003295void kvm_io_bus_init(struct kvm_io_bus *bus)
3296{
3297 memset(bus, 0, sizeof(*bus));
3298}
3299
3300void kvm_io_bus_destroy(struct kvm_io_bus *bus)
3301{
3302 int i;
3303
3304 for (i = 0; i < bus->dev_count; i++) {
3305 struct kvm_io_device *pos = bus->devs[i];
3306
3307 kvm_iodevice_destructor(pos);
3308 }
3309}
3310
3311struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
3312{
3313 int i;
3314
3315 for (i = 0; i < bus->dev_count; i++) {
3316 struct kvm_io_device *pos = bus->devs[i];
3317
3318 if (pos->in_range(pos, addr))
3319 return pos;
3320 }
3321
3322 return NULL;
3323}
3324
3325void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
3326{
3327 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
3328
3329 bus->devs[bus->dev_count++] = dev;
3330}
3331
Avi Kivity774c47f2007-02-12 00:54:47 -08003332static struct notifier_block kvm_cpu_notifier = {
3333 .notifier_call = kvm_cpu_hotplug,
3334 .priority = 20, /* must be > scheduler priority */
3335};
3336
Avi Kivity1165f5f2007-04-19 17:27:43 +03003337static u64 stat_get(void *_offset)
3338{
3339 unsigned offset = (long)_offset;
3340 u64 total = 0;
3341 struct kvm *kvm;
3342 struct kvm_vcpu *vcpu;
3343 int i;
3344
3345 spin_lock(&kvm_lock);
3346 list_for_each_entry(kvm, &vm_list, vm_list)
3347 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003348 vcpu = kvm->vcpus[i];
3349 if (vcpu)
3350 total += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03003351 }
3352 spin_unlock(&kvm_lock);
3353 return total;
3354}
3355
Rusty Russell3dea7ca2007-08-01 10:12:22 +10003356DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n");
Avi Kivity1165f5f2007-04-19 17:27:43 +03003357
Avi Kivity6aa8b732006-12-10 02:21:36 -08003358static __init void kvm_init_debug(void)
3359{
3360 struct kvm_stats_debugfs_item *p;
3361
Al Viro8b6d44c2007-02-09 16:38:40 +00003362 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003363 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03003364 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
3365 (void *)(long)p->offset,
3366 &stat_fops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003367}
3368
3369static void kvm_exit_debug(void)
3370{
3371 struct kvm_stats_debugfs_item *p;
3372
3373 for (p = debugfs_entries; p->name; ++p)
3374 debugfs_remove(p->dentry);
3375 debugfs_remove(debugfs_dir);
3376}
3377
Avi Kivity59ae6c62007-02-12 00:54:48 -08003378static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3379{
Avi Kivity4267c412007-05-24 13:09:41 +03003380 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003381 return 0;
3382}
3383
3384static int kvm_resume(struct sys_device *dev)
3385{
Avi Kivity4267c412007-05-24 13:09:41 +03003386 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003387 return 0;
3388}
3389
3390static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01003391 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08003392 .suspend = kvm_suspend,
3393 .resume = kvm_resume,
3394};
3395
3396static struct sys_device kvm_sysdev = {
3397 .id = 0,
3398 .cls = &kvm_sysdev_class,
3399};
3400
Izik Eiduscea7bb22007-10-17 19:17:48 +02003401struct page *bad_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003402
Avi Kivity15ad7142007-07-11 18:17:21 +03003403static inline
3404struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
3405{
3406 return container_of(pn, struct kvm_vcpu, preempt_notifier);
3407}
3408
3409static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
3410{
3411 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3412
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003413 kvm_x86_ops->vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003414}
3415
3416static void kvm_sched_out(struct preempt_notifier *pn,
3417 struct task_struct *next)
3418{
3419 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3420
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003421 kvm_x86_ops->vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003422}
3423
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003424int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10003425 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003426{
3427 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003428 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003429
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003430 if (kvm_x86_ops) {
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08003431 printk(KERN_ERR "kvm: already loaded the other module\n");
3432 return -EEXIST;
3433 }
3434
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003435 if (!ops->cpu_has_kvm_support()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003436 printk(KERN_ERR "kvm: no hardware support\n");
3437 return -EOPNOTSUPP;
3438 }
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003439 if (ops->disabled_by_bios()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003440 printk(KERN_ERR "kvm: disabled by bios\n");
3441 return -EOPNOTSUPP;
3442 }
3443
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003444 kvm_x86_ops = ops;
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003445
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003446 r = kvm_x86_ops->hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003447 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02003448 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003449
Yang, Sheng002c7f72007-07-31 14:23:01 +03003450 for_each_online_cpu(cpu) {
3451 smp_call_function_single(cpu,
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003452 kvm_x86_ops->check_processor_compatibility,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003453 &r, 0, 1);
3454 if (r < 0)
3455 goto out_free_0;
3456 }
3457
Avi Kivity1b6c0162007-05-24 13:03:52 +03003458 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003459 r = register_cpu_notifier(&kvm_cpu_notifier);
3460 if (r)
3461 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003462 register_reboot_notifier(&kvm_reboot_notifier);
3463
Avi Kivity59ae6c62007-02-12 00:54:48 -08003464 r = sysdev_class_register(&kvm_sysdev_class);
3465 if (r)
3466 goto out_free_2;
3467
3468 r = sysdev_register(&kvm_sysdev);
3469 if (r)
3470 goto out_free_3;
3471
Rusty Russellc16f8622007-07-30 21:12:19 +10003472 /* A kmem cache lets us meet the alignment requirements of fx_save. */
3473 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
3474 __alignof__(struct kvm_vcpu), 0, 0);
3475 if (!kvm_vcpu_cache) {
3476 r = -ENOMEM;
3477 goto out_free_4;
3478 }
3479
Avi Kivity6aa8b732006-12-10 02:21:36 -08003480 kvm_chardev_ops.owner = module;
3481
3482 r = misc_register(&kvm_dev);
3483 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04003484 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08003485 goto out_free;
3486 }
3487
Avi Kivity15ad7142007-07-11 18:17:21 +03003488 kvm_preempt_ops.sched_in = kvm_sched_in;
3489 kvm_preempt_ops.sched_out = kvm_sched_out;
3490
Avi Kivityc7addb92007-09-16 18:58:32 +02003491 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
3492
3493 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003494
3495out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10003496 kmem_cache_destroy(kvm_vcpu_cache);
3497out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08003498 sysdev_unregister(&kvm_sysdev);
3499out_free_3:
3500 sysdev_class_unregister(&kvm_sysdev_class);
3501out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003502 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08003503 unregister_cpu_notifier(&kvm_cpu_notifier);
3504out_free_1:
Avi Kivity1b6c0162007-05-24 13:03:52 +03003505 on_each_cpu(hardware_disable, NULL, 0, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03003506out_free_0:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003507 kvm_x86_ops->hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02003508out:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003509 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003510 return r;
3511}
Mike Dayd77c26f2007-10-08 09:02:08 -04003512EXPORT_SYMBOL_GPL(kvm_init_x86);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003513
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003514void kvm_exit_x86(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003515{
3516 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10003517 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003518 sysdev_unregister(&kvm_sysdev);
3519 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003520 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003521 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003522 on_each_cpu(hardware_disable, NULL, 0, 1);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003523 kvm_x86_ops->hardware_unsetup();
3524 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003525}
Mike Dayd77c26f2007-10-08 09:02:08 -04003526EXPORT_SYMBOL_GPL(kvm_exit_x86);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003527
3528static __init int kvm_init(void)
3529{
Avi Kivity37e29d92007-02-20 14:07:37 +02003530 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003531
Avi Kivityb5a33a72007-04-15 16:31:09 +03003532 r = kvm_mmu_module_init();
3533 if (r)
3534 goto out4;
3535
Avi Kivity6aa8b732006-12-10 02:21:36 -08003536 kvm_init_debug();
3537
Carsten Otte043405e2007-10-10 17:16:19 +02003538 kvm_arch_init();
Michael Riepebf591b22006-12-22 01:05:36 -08003539
Izik Eiduscea7bb22007-10-17 19:17:48 +02003540 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
Mike Dayd77c26f2007-10-08 09:02:08 -04003541
3542 if (bad_page == NULL) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003543 r = -ENOMEM;
3544 goto out;
3545 }
3546
Avi Kivity58e690e2007-02-26 16:29:43 +02003547 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003548
3549out:
3550 kvm_exit_debug();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003551 kvm_mmu_module_exit();
3552out4:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003553 return r;
3554}
3555
3556static __exit void kvm_exit(void)
3557{
3558 kvm_exit_debug();
Izik Eiduscea7bb22007-10-17 19:17:48 +02003559 __free_page(bad_page);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003560 kvm_mmu_module_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003561}
3562
3563module_init(kvm_init)
3564module_exit(kvm_exit)