blob: 47000be25479f4030245ceea5b08d8a51765db0f [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_userspace_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 if (!PageReserved(free->phys_mem[i]))
310 SetPageDirty(free->phys_mem[i]);
311 page_cache_release(free->phys_mem[i]);
312 }
313 }
314}
315
316static void kvm_free_kernel_physmem(struct kvm_memory_slot *free)
317{
318 int i;
319
320 for (i = 0; i < free->npages; ++i)
321 if (free->phys_mem[i])
322 __free_page(free->phys_mem[i]);
323}
324
Avi Kivity6aa8b732006-12-10 02:21:36 -0800325/*
326 * Free any memory in @free but not in @dont.
327 */
328static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
329 struct kvm_memory_slot *dont)
330{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800331 if (!dont || free->phys_mem != dont->phys_mem)
332 if (free->phys_mem) {
Izik Eidus6fc138d2007-10-09 19:20:39 +0200333 if (free->user_alloc)
334 kvm_free_userspace_physmem(free);
335 else
336 kvm_free_kernel_physmem(free);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800337 vfree(free->phys_mem);
338 }
Izik Eidus290fc382007-09-27 14:11:22 +0200339 if (!dont || free->rmap != dont->rmap)
340 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800341
342 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
343 vfree(free->dirty_bitmap);
344
Al Viro8b6d44c2007-02-09 16:38:40 +0000345 free->phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800346 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000347 free->dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800348}
349
350static void kvm_free_physmem(struct kvm *kvm)
351{
352 int i;
353
354 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000355 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800356}
357
Avi Kivity039576c2007-03-20 12:46:50 +0200358static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
359{
360 int i;
361
Rusty Russell3077c4512007-07-30 16:41:57 +1000362 for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i)
Avi Kivity039576c2007-03-20 12:46:50 +0200363 if (vcpu->pio.guest_pages[i]) {
364 __free_page(vcpu->pio.guest_pages[i]);
365 vcpu->pio.guest_pages[i] = NULL;
366 }
367}
368
Avi Kivity7b53aa52007-06-05 12:17:03 +0300369static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
370{
Avi Kivity7b53aa52007-06-05 12:17:03 +0300371 vcpu_load(vcpu);
372 kvm_mmu_unload(vcpu);
373 vcpu_put(vcpu);
374}
375
Avi Kivity6aa8b732006-12-10 02:21:36 -0800376static void kvm_free_vcpus(struct kvm *kvm)
377{
378 unsigned int i;
379
Avi Kivity7b53aa52007-06-05 12:17:03 +0300380 /*
381 * Unpin any mmu pages first.
382 */
383 for (i = 0; i < KVM_MAX_VCPUS; ++i)
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000384 if (kvm->vcpus[i])
385 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
386 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
387 if (kvm->vcpus[i]) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300388 kvm_x86_ops->vcpu_free(kvm->vcpus[i]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000389 kvm->vcpus[i] = NULL;
390 }
391 }
392
Avi Kivity6aa8b732006-12-10 02:21:36 -0800393}
394
Avi Kivityf17abe92007-02-21 19:28:04 +0200395static void kvm_destroy_vm(struct kvm *kvm)
396{
Avi Kivity133de902007-02-12 00:54:44 -0800397 spin_lock(&kvm_lock);
398 list_del(&kvm->vm_list);
399 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300400 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400401 kvm_io_bus_destroy(&kvm->mmio_bus);
Eddie Dong85f455f2007-07-06 12:20:49 +0300402 kfree(kvm->vpic);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300403 kfree(kvm->vioapic);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800404 kvm_free_vcpus(kvm);
405 kvm_free_physmem(kvm);
406 kfree(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200407}
408
409static int kvm_vm_release(struct inode *inode, struct file *filp)
410{
411 struct kvm *kvm = filp->private_data;
412
413 kvm_destroy_vm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800414 return 0;
415}
416
417static void inject_gp(struct kvm_vcpu *vcpu)
418{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300419 kvm_x86_ops->inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800420}
421
Avi Kivity1342d352007-01-05 16:36:39 -0800422/*
423 * Load the pae pdptrs. Return true is they are all valid.
424 */
425static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800426{
427 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
Avi Kivity1342d352007-01-05 16:36:39 -0800428 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800429 int i;
Avi Kivity1342d352007-01-05 16:36:39 -0800430 int ret;
Rusty Russellc820c2a2007-07-25 13:29:51 +1000431 u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800432
Shaohua Li11ec2802007-07-23 14:51:37 +0800433 mutex_lock(&vcpu->kvm->lock);
Izik Eidus195aefd2007-10-01 22:14:18 +0200434 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
435 offset * sizeof(u64), sizeof(pdpte));
436 if (ret < 0) {
Rusty Russellc820c2a2007-07-25 13:29:51 +1000437 ret = 0;
438 goto out;
439 }
Rusty Russellc820c2a2007-07-25 13:29:51 +1000440 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
441 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
Avi Kivity1342d352007-01-05 16:36:39 -0800442 ret = 0;
443 goto out;
444 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800445 }
Rusty Russellc820c2a2007-07-25 13:29:51 +1000446 ret = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800447
Rusty Russellc820c2a2007-07-25 13:29:51 +1000448 memcpy(vcpu->pdptrs, pdpte, sizeof(vcpu->pdptrs));
Avi Kivity1342d352007-01-05 16:36:39 -0800449out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800450 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800451
Avi Kivity1342d352007-01-05 16:36:39 -0800452 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800453}
454
455void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
456{
Rusty Russell707d92f2007-07-17 23:19:08 +1000457 if (cr0 & CR0_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800458 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
459 cr0, vcpu->cr0);
460 inject_gp(vcpu);
461 return;
462 }
463
Rusty Russell707d92f2007-07-17 23:19:08 +1000464 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800465 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
466 inject_gp(vcpu);
467 return;
468 }
469
Rusty Russell707d92f2007-07-17 23:19:08 +1000470 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800471 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
472 "and a clear PE flag\n");
473 inject_gp(vcpu);
474 return;
475 }
476
Rusty Russell707d92f2007-07-17 23:19:08 +1000477 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800478#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800479 if ((vcpu->shadow_efer & EFER_LME)) {
480 int cs_db, cs_l;
481
482 if (!is_pae(vcpu)) {
483 printk(KERN_DEBUG "set_cr0: #GP, start paging "
484 "in long mode while PAE is disabled\n");
485 inject_gp(vcpu);
486 return;
487 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300488 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800489 if (cs_l) {
490 printk(KERN_DEBUG "set_cr0: #GP, start paging "
491 "in long mode while CS.L == 1\n");
492 inject_gp(vcpu);
493 return;
494
495 }
496 } else
497#endif
Avi Kivity1342d352007-01-05 16:36:39 -0800498 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800499 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
500 "reserved bits\n");
501 inject_gp(vcpu);
502 return;
503 }
504
505 }
506
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300507 kvm_x86_ops->set_cr0(vcpu, cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800508 vcpu->cr0 = cr0;
509
Shaohua Li11ec2802007-07-23 14:51:37 +0800510 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800511 kvm_mmu_reset_context(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800512 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800513 return;
514}
515EXPORT_SYMBOL_GPL(set_cr0);
516
517void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
518{
519 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
520}
521EXPORT_SYMBOL_GPL(lmsw);
522
523void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
524{
Rusty Russell66aee912007-07-17 23:34:16 +1000525 if (cr4 & CR4_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800526 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
527 inject_gp(vcpu);
528 return;
529 }
530
Avi Kivitya9058ec2006-12-29 16:49:37 -0800531 if (is_long_mode(vcpu)) {
Rusty Russell66aee912007-07-17 23:34:16 +1000532 if (!(cr4 & X86_CR4_PAE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800533 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
534 "in long mode\n");
535 inject_gp(vcpu);
536 return;
537 }
Rusty Russell66aee912007-07-17 23:34:16 +1000538 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
Avi Kivity1342d352007-01-05 16:36:39 -0800539 && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800540 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
541 inject_gp(vcpu);
Rusty Russell310bc762007-07-23 17:11:02 +1000542 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800543 }
544
Rusty Russell66aee912007-07-17 23:34:16 +1000545 if (cr4 & X86_CR4_VMXE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800546 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
547 inject_gp(vcpu);
548 return;
549 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300550 kvm_x86_ops->set_cr4(vcpu, cr4);
Rusty Russell81f50e32007-09-06 01:20:38 +1000551 vcpu->cr4 = cr4;
Shaohua Li11ec2802007-07-23 14:51:37 +0800552 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800553 kvm_mmu_reset_context(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800554 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800555}
556EXPORT_SYMBOL_GPL(set_cr4);
557
558void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
559{
Avi Kivitya9058ec2006-12-29 16:49:37 -0800560 if (is_long_mode(vcpu)) {
Rusty Russellf802a302007-07-17 23:32:55 +1000561 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800562 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
563 inject_gp(vcpu);
564 return;
565 }
566 } else {
Rusty Russellf802a302007-07-17 23:32:55 +1000567 if (is_pae(vcpu)) {
568 if (cr3 & CR3_PAE_RESERVED_BITS) {
569 printk(KERN_DEBUG
570 "set_cr3: #GP, reserved bits\n");
571 inject_gp(vcpu);
572 return;
573 }
574 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
575 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
576 "reserved bits\n");
577 inject_gp(vcpu);
578 return;
579 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800580 }
Ryan Harper21764862007-09-18 14:05:16 -0500581 /*
582 * We don't check reserved bits in nonpae mode, because
583 * this isn't enforced, and VMware depends on this.
584 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800585 }
586
Shaohua Li11ec2802007-07-23 14:51:37 +0800587 mutex_lock(&vcpu->kvm->lock);
Ingo Molnard21225e2007-01-05 16:36:59 -0800588 /*
589 * Does the new cr3 value map to physical memory? (Note, we
590 * catch an invalid cr3 even in real-mode, because it would
591 * cause trouble later on when we turn on paging anyway.)
592 *
593 * A real CPU would silently accept an invalid cr3 and would
594 * attempt to use it - with largely undefined (and often hard
595 * to debug) behavior on the guest side.
596 */
597 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
598 inject_gp(vcpu);
Rusty Russellfb764412007-07-31 20:45:03 +1000599 else {
600 vcpu->cr3 = cr3;
Ingo Molnard21225e2007-01-05 16:36:59 -0800601 vcpu->mmu.new_cr3(vcpu);
Rusty Russellfb764412007-07-31 20:45:03 +1000602 }
Shaohua Li11ec2802007-07-23 14:51:37 +0800603 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800604}
605EXPORT_SYMBOL_GPL(set_cr3);
606
607void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
608{
Rusty Russell7075bc82007-07-17 23:37:17 +1000609 if (cr8 & CR8_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800610 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
611 inject_gp(vcpu);
612 return;
613 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300614 if (irqchip_in_kernel(vcpu->kvm))
615 kvm_lapic_set_tpr(vcpu, cr8);
616 else
617 vcpu->cr8 = cr8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800618}
619EXPORT_SYMBOL_GPL(set_cr8);
620
Eddie Dong7017fc32007-07-18 11:34:57 +0300621unsigned long get_cr8(struct kvm_vcpu *vcpu)
622{
Eddie Dong97222cc2007-09-12 10:58:04 +0300623 if (irqchip_in_kernel(vcpu->kvm))
624 return kvm_lapic_get_cr8(vcpu);
625 else
626 return vcpu->cr8;
Eddie Dong7017fc32007-07-18 11:34:57 +0300627}
628EXPORT_SYMBOL_GPL(get_cr8);
629
630u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
631{
Eddie Dong97222cc2007-09-12 10:58:04 +0300632 if (irqchip_in_kernel(vcpu->kvm))
633 return vcpu->apic_base;
634 else
635 return vcpu->apic_base;
Eddie Dong7017fc32007-07-18 11:34:57 +0300636}
637EXPORT_SYMBOL_GPL(kvm_get_apic_base);
638
639void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
640{
Eddie Dong97222cc2007-09-12 10:58:04 +0300641 /* TODO: reserve bits check */
642 if (irqchip_in_kernel(vcpu->kvm))
643 kvm_lapic_set_base(vcpu, data);
644 else
645 vcpu->apic_base = data;
Eddie Dong7017fc32007-07-18 11:34:57 +0300646}
647EXPORT_SYMBOL_GPL(kvm_set_apic_base);
648
Avi Kivity6aa8b732006-12-10 02:21:36 -0800649void fx_init(struct kvm_vcpu *vcpu)
650{
Rusty Russellb114b082007-07-30 21:13:43 +1000651 unsigned after_mxcsr_mask;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800652
Rusty Russell9bd01502007-07-30 16:29:56 +1000653 /* Initialize guest FPU by resetting ours and saving into guest's */
654 preempt_disable();
Rusty Russellb114b082007-07-30 21:13:43 +1000655 fx_save(&vcpu->host_fx_image);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800656 fpu_init();
Rusty Russellb114b082007-07-30 21:13:43 +1000657 fx_save(&vcpu->guest_fx_image);
658 fx_restore(&vcpu->host_fx_image);
Rusty Russell9bd01502007-07-30 16:29:56 +1000659 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800660
Amit Shah380102c2007-08-25 11:35:52 +0300661 vcpu->cr0 |= X86_CR0_ET;
Rusty Russellb114b082007-07-30 21:13:43 +1000662 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
663 vcpu->guest_fx_image.mxcsr = 0x1f80;
664 memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask,
665 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800666}
667EXPORT_SYMBOL_GPL(fx_init);
668
669/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800670 * Allocate some memory and give it an address in the guest physical address
671 * space.
672 *
673 * Discontiguous memory is allowed, mostly for framebuffers.
674 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200675static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
Izik Eidus6fc138d2007-10-09 19:20:39 +0200676 struct
677 kvm_userspace_memory_region *mem,
678 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800679{
680 int r;
681 gfn_t base_gfn;
682 unsigned long npages;
683 unsigned long i;
684 struct kvm_memory_slot *memslot;
685 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800686
687 r = -EINVAL;
688 /* General sanity checks */
689 if (mem->memory_size & (PAGE_SIZE - 1))
690 goto out;
691 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
692 goto out;
693 if (mem->slot >= KVM_MEMORY_SLOTS)
694 goto out;
695 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
696 goto out;
697
698 memslot = &kvm->memslots[mem->slot];
699 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
700 npages = mem->memory_size >> PAGE_SHIFT;
701
702 if (!npages)
703 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
704
Shaohua Li11ec2802007-07-23 14:51:37 +0800705 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800706
Avi Kivity6aa8b732006-12-10 02:21:36 -0800707 new = old = *memslot;
708
709 new.base_gfn = base_gfn;
710 new.npages = npages;
711 new.flags = mem->flags;
712
713 /* Disallow changing a memory slot's size. */
714 r = -EINVAL;
715 if (npages && old.npages && npages != old.npages)
716 goto out_unlock;
717
718 /* Check for overlaps */
719 r = -EEXIST;
720 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
721 struct kvm_memory_slot *s = &kvm->memslots[i];
722
723 if (s == memslot)
724 continue;
725 if (!((base_gfn + npages <= s->base_gfn) ||
726 (base_gfn >= s->base_gfn + s->npages)))
727 goto out_unlock;
728 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800729
730 /* Deallocate if slot is being removed */
731 if (!npages)
Al Viro8b6d44c2007-02-09 16:38:40 +0000732 new.phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800733
734 /* Free page dirty bitmap if unneeded */
735 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000736 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800737
738 r = -ENOMEM;
739
740 /* Allocate if a slot is being created */
741 if (npages && !new.phys_mem) {
742 new.phys_mem = vmalloc(npages * sizeof(struct page *));
743
744 if (!new.phys_mem)
Laurent Vivier0d8d2bd2007-08-30 14:56:21 +0200745 goto out_unlock;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800746
Mike Dayd77c26f2007-10-08 09:02:08 -0400747 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200748
749 if (!new.rmap)
750 goto out_unlock;
751
Avi Kivity6aa8b732006-12-10 02:21:36 -0800752 memset(new.phys_mem, 0, npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200753 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Izik Eidus6fc138d2007-10-09 19:20:39 +0200754 if (user_alloc) {
755 unsigned long pages_num;
756
757 new.user_alloc = 1;
758 down_read(&current->mm->mmap_sem);
759
760 pages_num = get_user_pages(current, current->mm,
761 mem->userspace_addr,
762 npages, 1, 1, new.phys_mem,
763 NULL);
764
765 up_read(&current->mm->mmap_sem);
766 if (pages_num != npages)
Laurent Vivier0d8d2bd2007-08-30 14:56:21 +0200767 goto out_unlock;
Izik Eidus6fc138d2007-10-09 19:20:39 +0200768 } else {
769 for (i = 0; i < npages; ++i) {
770 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
771 | __GFP_ZERO);
772 if (!new.phys_mem[i])
773 goto out_unlock;
774 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800775 }
776 }
777
778 /* Allocate page dirty bitmap if needed */
779 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
780 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
781
782 new.dirty_bitmap = vmalloc(dirty_bytes);
783 if (!new.dirty_bitmap)
Laurent Vivier0d8d2bd2007-08-30 14:56:21 +0200784 goto out_unlock;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800785 memset(new.dirty_bitmap, 0, dirty_bytes);
786 }
787
Avi Kivity6aa8b732006-12-10 02:21:36 -0800788 if (mem->slot >= kvm->nmemslots)
789 kvm->nmemslots = mem->slot + 1;
790
Izik Eidus82ce2c92007-10-02 18:52:55 +0200791 if (!kvm->n_requested_mmu_pages) {
792 unsigned int n_pages;
793
794 if (npages) {
795 n_pages = npages * KVM_PERMILLE_MMU_PAGES / 1000;
796 kvm_mmu_change_mmu_pages(kvm, kvm->n_alloc_mmu_pages +
797 n_pages);
798 } else {
799 unsigned int nr_mmu_pages;
800
801 n_pages = old.npages * KVM_PERMILLE_MMU_PAGES / 1000;
802 nr_mmu_pages = kvm->n_alloc_mmu_pages - n_pages;
803 nr_mmu_pages = max(nr_mmu_pages,
804 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
805 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
806 }
807 }
808
Avi Kivity6aa8b732006-12-10 02:21:36 -0800809 *memslot = new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800810
Avi Kivity90cb0522007-07-17 13:04:56 +0300811 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
812 kvm_flush_remote_tlbs(kvm);
813
Shaohua Li11ec2802007-07-23 14:51:37 +0800814 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800815
Avi Kivity6aa8b732006-12-10 02:21:36 -0800816 kvm_free_physmem_slot(&old, &new);
817 return 0;
818
819out_unlock:
Shaohua Li11ec2802007-07-23 14:51:37 +0800820 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800821 kvm_free_physmem_slot(&new, &old);
822out:
823 return r;
824}
825
Izik Eidus82ce2c92007-10-02 18:52:55 +0200826static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
827 u32 kvm_nr_mmu_pages)
828{
829 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
830 return -EINVAL;
831
832 mutex_lock(&kvm->lock);
833
834 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
835 kvm->n_requested_mmu_pages = kvm_nr_mmu_pages;
836
837 mutex_unlock(&kvm->lock);
838 return 0;
839}
840
841static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
842{
843 return kvm->n_alloc_mmu_pages;
844}
845
Avi Kivity6aa8b732006-12-10 02:21:36 -0800846/*
847 * Get (and clear) the dirty memory log for a memory slot.
848 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200849static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
850 struct kvm_dirty_log *log)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800851{
852 struct kvm_memory_slot *memslot;
853 int r, i;
854 int n;
855 unsigned long any = 0;
856
Shaohua Li11ec2802007-07-23 14:51:37 +0800857 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858
Avi Kivity6aa8b732006-12-10 02:21:36 -0800859 r = -EINVAL;
860 if (log->slot >= KVM_MEMORY_SLOTS)
861 goto out;
862
863 memslot = &kvm->memslots[log->slot];
864 r = -ENOENT;
865 if (!memslot->dirty_bitmap)
866 goto out;
867
Uri Lublincd1a4a92007-02-22 16:43:09 +0200868 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800869
Uri Lublincd1a4a92007-02-22 16:43:09 +0200870 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800871 any = memslot->dirty_bitmap[i];
872
873 r = -EFAULT;
874 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
875 goto out;
876
Rusty Russell39214912007-07-31 19:57:47 +1000877 /* If nothing is dirty, don't bother messing with page tables. */
878 if (any) {
Rusty Russell39214912007-07-31 19:57:47 +1000879 kvm_mmu_slot_remove_write_access(kvm, log->slot);
880 kvm_flush_remote_tlbs(kvm);
881 memset(memslot->dirty_bitmap, 0, n);
Rusty Russell39214912007-07-31 19:57:47 +1000882 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800883
884 r = 0;
885
886out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800887 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800888 return r;
889}
890
Avi Kivitye8207542007-03-30 16:54:30 +0300891/*
892 * Set a new alias region. Aliases map a portion of physical memory into
893 * another portion. This is useful for memory windows, for example the PC
894 * VGA region.
895 */
896static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
897 struct kvm_memory_alias *alias)
898{
899 int r, n;
900 struct kvm_mem_alias *p;
901
902 r = -EINVAL;
903 /* General sanity checks */
904 if (alias->memory_size & (PAGE_SIZE - 1))
905 goto out;
906 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
907 goto out;
908 if (alias->slot >= KVM_ALIAS_SLOTS)
909 goto out;
910 if (alias->guest_phys_addr + alias->memory_size
911 < alias->guest_phys_addr)
912 goto out;
913 if (alias->target_phys_addr + alias->memory_size
914 < alias->target_phys_addr)
915 goto out;
916
Shaohua Li11ec2802007-07-23 14:51:37 +0800917 mutex_lock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300918
919 p = &kvm->aliases[alias->slot];
920 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
921 p->npages = alias->memory_size >> PAGE_SHIFT;
922 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
923
924 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
925 if (kvm->aliases[n - 1].npages)
926 break;
927 kvm->naliases = n;
928
Avi Kivity90cb0522007-07-17 13:04:56 +0300929 kvm_mmu_zap_all(kvm);
Avi Kivitye8207542007-03-30 16:54:30 +0300930
Shaohua Li11ec2802007-07-23 14:51:37 +0800931 mutex_unlock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300932
933 return 0;
934
935out:
936 return r;
937}
938
He, Qing6ceb9d72007-07-26 11:05:18 +0300939static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
940{
941 int r;
942
943 r = 0;
944 switch (chip->chip_id) {
945 case KVM_IRQCHIP_PIC_MASTER:
Mike Dayd77c26f2007-10-08 09:02:08 -0400946 memcpy(&chip->chip.pic,
He, Qing6ceb9d72007-07-26 11:05:18 +0300947 &pic_irqchip(kvm)->pics[0],
948 sizeof(struct kvm_pic_state));
949 break;
950 case KVM_IRQCHIP_PIC_SLAVE:
Mike Dayd77c26f2007-10-08 09:02:08 -0400951 memcpy(&chip->chip.pic,
He, Qing6ceb9d72007-07-26 11:05:18 +0300952 &pic_irqchip(kvm)->pics[1],
953 sizeof(struct kvm_pic_state));
954 break;
He, Qing6bf9e962007-08-05 10:49:16 +0300955 case KVM_IRQCHIP_IOAPIC:
Mike Dayd77c26f2007-10-08 09:02:08 -0400956 memcpy(&chip->chip.ioapic,
He, Qing6bf9e962007-08-05 10:49:16 +0300957 ioapic_irqchip(kvm),
958 sizeof(struct kvm_ioapic_state));
959 break;
He, Qing6ceb9d72007-07-26 11:05:18 +0300960 default:
961 r = -EINVAL;
962 break;
963 }
964 return r;
965}
966
967static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
968{
969 int r;
970
971 r = 0;
972 switch (chip->chip_id) {
973 case KVM_IRQCHIP_PIC_MASTER:
Mike Dayd77c26f2007-10-08 09:02:08 -0400974 memcpy(&pic_irqchip(kvm)->pics[0],
He, Qing6ceb9d72007-07-26 11:05:18 +0300975 &chip->chip.pic,
976 sizeof(struct kvm_pic_state));
977 break;
978 case KVM_IRQCHIP_PIC_SLAVE:
Mike Dayd77c26f2007-10-08 09:02:08 -0400979 memcpy(&pic_irqchip(kvm)->pics[1],
He, Qing6ceb9d72007-07-26 11:05:18 +0300980 &chip->chip.pic,
981 sizeof(struct kvm_pic_state));
982 break;
He, Qing6bf9e962007-08-05 10:49:16 +0300983 case KVM_IRQCHIP_IOAPIC:
Mike Dayd77c26f2007-10-08 09:02:08 -0400984 memcpy(ioapic_irqchip(kvm),
He, Qing6bf9e962007-08-05 10:49:16 +0300985 &chip->chip.ioapic,
986 sizeof(struct kvm_ioapic_state));
987 break;
He, Qing6ceb9d72007-07-26 11:05:18 +0300988 default:
989 r = -EINVAL;
990 break;
991 }
992 kvm_pic_update_irq(pic_irqchip(kvm));
993 return r;
994}
995
Izik Eiduscea7bb22007-10-17 19:17:48 +0200996int is_error_page(struct page *page)
997{
998 return page == bad_page;
999}
1000EXPORT_SYMBOL_GPL(is_error_page);
1001
Izik Eidus290fc382007-09-27 14:11:22 +02001002gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
Avi Kivitye8207542007-03-30 16:54:30 +03001003{
1004 int i;
1005 struct kvm_mem_alias *alias;
1006
1007 for (i = 0; i < kvm->naliases; ++i) {
1008 alias = &kvm->aliases[i];
1009 if (gfn >= alias->base_gfn
1010 && gfn < alias->base_gfn + alias->npages)
1011 return alias->target_gfn + gfn - alias->base_gfn;
1012 }
1013 return gfn;
1014}
1015
1016static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001017{
1018 int i;
1019
1020 for (i = 0; i < kvm->nmemslots; ++i) {
1021 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1022
1023 if (gfn >= memslot->base_gfn
1024 && gfn < memslot->base_gfn + memslot->npages)
1025 return memslot;
1026 }
Al Viro8b6d44c2007-02-09 16:38:40 +00001027 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028}
Avi Kivitye8207542007-03-30 16:54:30 +03001029
1030struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1031{
1032 gfn = unalias_gfn(kvm, gfn);
1033 return __gfn_to_memslot(kvm, gfn);
1034}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035
Avi Kivity954bbbc22007-03-30 14:02:32 +03001036struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1037{
1038 struct kvm_memory_slot *slot;
1039
Avi Kivitye8207542007-03-30 16:54:30 +03001040 gfn = unalias_gfn(kvm, gfn);
1041 slot = __gfn_to_memslot(kvm, gfn);
Avi Kivity954bbbc22007-03-30 14:02:32 +03001042 if (!slot)
Izik Eiduscea7bb22007-10-17 19:17:48 +02001043 return bad_page;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001044 return slot->phys_mem[gfn - slot->base_gfn];
1045}
1046EXPORT_SYMBOL_GPL(gfn_to_page);
1047
Izik Eidus195aefd2007-10-01 22:14:18 +02001048static int next_segment(unsigned long len, int offset)
1049{
1050 if (len > PAGE_SIZE - offset)
1051 return PAGE_SIZE - offset;
1052 else
1053 return len;
1054}
1055
1056int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1057 int len)
1058{
1059 void *page_virt;
1060 struct page *page;
1061
1062 page = gfn_to_page(kvm, gfn);
Izik Eiduscea7bb22007-10-17 19:17:48 +02001063 if (is_error_page(page))
Izik Eidus195aefd2007-10-01 22:14:18 +02001064 return -EFAULT;
1065 page_virt = kmap_atomic(page, KM_USER0);
1066
1067 memcpy(data, page_virt + offset, len);
1068
1069 kunmap_atomic(page_virt, KM_USER0);
1070 return 0;
1071}
1072EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1073
1074int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1075{
1076 gfn_t gfn = gpa >> PAGE_SHIFT;
1077 int seg;
1078 int offset = offset_in_page(gpa);
1079 int ret;
1080
1081 while ((seg = next_segment(len, offset)) != 0) {
1082 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1083 if (ret < 0)
1084 return ret;
1085 offset = 0;
1086 len -= seg;
1087 data += seg;
1088 ++gfn;
1089 }
1090 return 0;
1091}
1092EXPORT_SYMBOL_GPL(kvm_read_guest);
1093
1094int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1095 int offset, int len)
1096{
1097 void *page_virt;
1098 struct page *page;
1099
1100 page = gfn_to_page(kvm, gfn);
Izik Eiduscea7bb22007-10-17 19:17:48 +02001101 if (is_error_page(page))
Izik Eidus195aefd2007-10-01 22:14:18 +02001102 return -EFAULT;
1103 page_virt = kmap_atomic(page, KM_USER0);
1104
1105 memcpy(page_virt + offset, data, len);
1106
1107 kunmap_atomic(page_virt, KM_USER0);
1108 mark_page_dirty(kvm, gfn);
1109 return 0;
1110}
1111EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1112
1113int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1114 unsigned long len)
1115{
1116 gfn_t gfn = gpa >> PAGE_SHIFT;
1117 int seg;
1118 int offset = offset_in_page(gpa);
1119 int ret;
1120
1121 while ((seg = next_segment(len, offset)) != 0) {
1122 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1123 if (ret < 0)
1124 return ret;
1125 offset = 0;
1126 len -= seg;
1127 data += seg;
1128 ++gfn;
1129 }
1130 return 0;
1131}
1132
1133int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1134{
1135 void *page_virt;
1136 struct page *page;
1137
1138 page = gfn_to_page(kvm, gfn);
Izik Eiduscea7bb22007-10-17 19:17:48 +02001139 if (is_error_page(page))
Izik Eidus195aefd2007-10-01 22:14:18 +02001140 return -EFAULT;
1141 page_virt = kmap_atomic(page, KM_USER0);
1142
1143 memset(page_virt + offset, 0, len);
1144
1145 kunmap_atomic(page_virt, KM_USER0);
1146 return 0;
1147}
1148EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1149
1150int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1151{
1152 gfn_t gfn = gpa >> PAGE_SHIFT;
1153 int seg;
1154 int offset = offset_in_page(gpa);
1155 int ret;
1156
1157 while ((seg = next_segment(len, offset)) != 0) {
1158 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1159 if (ret < 0)
1160 return ret;
1161 offset = 0;
1162 len -= seg;
1163 ++gfn;
1164 }
1165 return 0;
1166}
1167EXPORT_SYMBOL_GPL(kvm_clear_guest);
1168
Rusty Russell7e9d6192007-07-31 20:41:14 +10001169/* WARNING: Does not work on aliased pages. */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001170void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1171{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001172 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001173
Rusty Russell7e9d6192007-07-31 20:41:14 +10001174 memslot = __gfn_to_memslot(kvm, gfn);
1175 if (memslot && memslot->dirty_bitmap) {
1176 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001177
Rusty Russell7e9d6192007-07-31 20:41:14 +10001178 /* avoid RMW */
1179 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1180 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001181 }
1182}
1183
Laurent Viviere7d5d762007-07-30 13:41:19 +03001184int emulator_read_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001185 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001186 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001187 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001188{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001189 void *data = val;
1190
1191 while (bytes) {
1192 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1193 unsigned offset = addr & (PAGE_SIZE-1);
1194 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
Izik Eidus195aefd2007-10-01 22:14:18 +02001195 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001196
1197 if (gpa == UNMAPPED_GVA)
1198 return X86EMUL_PROPAGATE_FAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001199 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
1200 if (ret < 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001201 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001202
1203 bytes -= tocopy;
1204 data += tocopy;
1205 addr += tocopy;
1206 }
1207
1208 return X86EMUL_CONTINUE;
1209}
Laurent Viviere7d5d762007-07-30 13:41:19 +03001210EXPORT_SYMBOL_GPL(emulator_read_std);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001211
1212static int emulator_write_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001213 const void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001214 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001215 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001216{
Rusty Russellf0242472007-08-01 10:48:02 +10001217 pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001218 return X86EMUL_UNHANDLEABLE;
1219}
1220
Eddie Dong97222cc2007-09-12 10:58:04 +03001221/*
1222 * Only apic need an MMIO device hook, so shortcut now..
1223 */
1224static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
1225 gpa_t addr)
1226{
1227 struct kvm_io_device *dev;
1228
1229 if (vcpu->apic) {
1230 dev = &vcpu->apic->dev;
1231 if (dev->in_range(dev, addr))
1232 return dev;
1233 }
1234 return NULL;
1235}
1236
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001237static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1238 gpa_t addr)
1239{
Eddie Dong97222cc2007-09-12 10:58:04 +03001240 struct kvm_io_device *dev;
1241
1242 dev = vcpu_find_pervcpu_dev(vcpu, addr);
1243 if (dev == NULL)
1244 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1245 return dev;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001246}
1247
Eddie Dong74906342007-06-19 18:05:03 +03001248static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
1249 gpa_t addr)
1250{
1251 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
1252}
1253
Avi Kivity6aa8b732006-12-10 02:21:36 -08001254static int emulator_read_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001255 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001256 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001257 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001258{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001259 struct kvm_io_device *mmio_dev;
1260 gpa_t gpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001261
1262 if (vcpu->mmio_read_completed) {
1263 memcpy(val, vcpu->mmio_data, bytes);
1264 vcpu->mmio_read_completed = 0;
1265 return X86EMUL_CONTINUE;
Laurent Viviercebff022007-07-30 13:35:24 +03001266 } else if (emulator_read_std(addr, val, bytes, vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001267 == X86EMUL_CONTINUE)
1268 return X86EMUL_CONTINUE;
Avi Kivityd27d4ac2007-02-19 14:37:46 +02001269
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001270 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1271 if (gpa == UNMAPPED_GVA)
1272 return X86EMUL_PROPAGATE_FAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001273
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001274 /*
1275 * Is this MMIO handled locally?
1276 */
1277 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1278 if (mmio_dev) {
1279 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1280 return X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001281 }
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001282
1283 vcpu->mmio_needed = 1;
1284 vcpu->mmio_phys_addr = gpa;
1285 vcpu->mmio_size = bytes;
1286 vcpu->mmio_is_write = 0;
1287
1288 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001289}
1290
Avi Kivityda4a00f2007-01-05 16:36:44 -08001291static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity4c690a12007-04-22 15:28:19 +03001292 const void *val, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001293{
Izik Eidus195aefd2007-10-01 22:14:18 +02001294 int ret;
Avi Kivityda4a00f2007-01-05 16:36:44 -08001295
Izik Eidus195aefd2007-10-01 22:14:18 +02001296 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
1297 if (ret < 0)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001298 return 0;
Shaohua Life551882007-07-23 14:51:39 +08001299 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001300 return 1;
1301}
1302
Avi Kivityb0fcd902007-07-22 18:48:54 +03001303static int emulator_write_emulated_onepage(unsigned long addr,
1304 const void *val,
1305 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001306 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001307{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001308 struct kvm_io_device *mmio_dev;
1309 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001310
Avi Kivityc9047f52007-04-17 10:53:22 +03001311 if (gpa == UNMAPPED_GVA) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001312 kvm_x86_ops->inject_page_fault(vcpu, addr, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001313 return X86EMUL_PROPAGATE_FAULT;
Avi Kivityc9047f52007-04-17 10:53:22 +03001314 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001315
Avi Kivityda4a00f2007-01-05 16:36:44 -08001316 if (emulator_write_phys(vcpu, gpa, val, bytes))
1317 return X86EMUL_CONTINUE;
1318
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001319 /*
1320 * Is this MMIO handled locally?
1321 */
1322 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1323 if (mmio_dev) {
1324 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1325 return X86EMUL_CONTINUE;
1326 }
1327
Avi Kivity6aa8b732006-12-10 02:21:36 -08001328 vcpu->mmio_needed = 1;
1329 vcpu->mmio_phys_addr = gpa;
1330 vcpu->mmio_size = bytes;
1331 vcpu->mmio_is_write = 1;
Avi Kivity4c690a12007-04-22 15:28:19 +03001332 memcpy(vcpu->mmio_data, val, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001333
1334 return X86EMUL_CONTINUE;
1335}
1336
Laurent Viviere7d5d762007-07-30 13:41:19 +03001337int emulator_write_emulated(unsigned long addr,
Avi Kivityb0fcd902007-07-22 18:48:54 +03001338 const void *val,
1339 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001340 struct kvm_vcpu *vcpu)
Avi Kivityb0fcd902007-07-22 18:48:54 +03001341{
1342 /* Crossing a page boundary? */
1343 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1344 int rc, now;
1345
1346 now = -addr & ~PAGE_MASK;
Laurent Viviercebff022007-07-30 13:35:24 +03001347 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001348 if (rc != X86EMUL_CONTINUE)
1349 return rc;
1350 addr += now;
1351 val += now;
1352 bytes -= now;
1353 }
Laurent Viviercebff022007-07-30 13:35:24 +03001354 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001355}
Laurent Viviere7d5d762007-07-30 13:41:19 +03001356EXPORT_SYMBOL_GPL(emulator_write_emulated);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001357
Avi Kivity6aa8b732006-12-10 02:21:36 -08001358static int emulator_cmpxchg_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001359 const void *old,
1360 const void *new,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001361 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001362 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001363{
1364 static int reported;
1365
1366 if (!reported) {
1367 reported = 1;
1368 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1369 }
Laurent Viviercebff022007-07-30 13:35:24 +03001370 return emulator_write_emulated(addr, new, bytes, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001371}
1372
1373static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1374{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001375 return kvm_x86_ops->get_segment_base(vcpu, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001376}
1377
1378int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1379{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001380 return X86EMUL_CONTINUE;
1381}
1382
1383int emulate_clts(struct kvm_vcpu *vcpu)
1384{
Amit Shah404fb882007-11-19 17:57:35 +02001385 kvm_x86_ops->set_cr0(vcpu, vcpu->cr0 & ~X86_CR0_TS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001386 return X86EMUL_CONTINUE;
1387}
1388
Mike Dayd77c26f2007-10-08 09:02:08 -04001389int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001390{
1391 struct kvm_vcpu *vcpu = ctxt->vcpu;
1392
1393 switch (dr) {
1394 case 0 ... 3:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001395 *dest = kvm_x86_ops->get_dr(vcpu, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001396 return X86EMUL_CONTINUE;
1397 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001398 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001399 return X86EMUL_UNHANDLEABLE;
1400 }
1401}
1402
1403int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1404{
1405 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1406 int exception;
1407
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001408 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001409 if (exception) {
1410 /* FIXME: better handling */
1411 return X86EMUL_UNHANDLEABLE;
1412 }
1413 return X86EMUL_CONTINUE;
1414}
1415
Avi Kivity054b1362007-09-12 13:21:09 +03001416void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001417{
1418 static int reported;
1419 u8 opcodes[4];
Avi Kivity054b1362007-09-12 13:21:09 +03001420 unsigned long rip = vcpu->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001421 unsigned long rip_linear;
1422
Avi Kivity054b1362007-09-12 13:21:09 +03001423 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001424
1425 if (reported)
1426 return;
1427
Avi Kivity054b1362007-09-12 13:21:09 +03001428 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429
Avi Kivity054b1362007-09-12 13:21:09 +03001430 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1431 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001432 reported = 1;
1433}
Avi Kivity054b1362007-09-12 13:21:09 +03001434EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001435
1436struct x86_emulate_ops emulate_ops = {
1437 .read_std = emulator_read_std,
1438 .write_std = emulator_write_std,
1439 .read_emulated = emulator_read_emulated,
1440 .write_emulated = emulator_write_emulated,
1441 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1442};
1443
1444int emulate_instruction(struct kvm_vcpu *vcpu,
1445 struct kvm_run *run,
1446 unsigned long cr2,
Laurent Vivier34273182007-09-18 11:27:37 +02001447 u16 error_code,
1448 int no_decode)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001449{
Laurent Viviera22436b2007-09-24 17:00:58 +02001450 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001451
Avi Kivitye7df56e2007-03-14 15:54:54 +02001452 vcpu->mmio_fault_cr2 = cr2;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001453 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001454
Avi Kivity6aa8b732006-12-10 02:21:36 -08001455 vcpu->mmio_is_write = 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03001456 vcpu->pio.string = 0;
Laurent Vivier34273182007-09-18 11:27:37 +02001457
1458 if (!no_decode) {
1459 int cs_db, cs_l;
1460 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1461
1462 vcpu->emulate_ctxt.vcpu = vcpu;
1463 vcpu->emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
1464 vcpu->emulate_ctxt.cr2 = cr2;
1465 vcpu->emulate_ctxt.mode =
1466 (vcpu->emulate_ctxt.eflags & X86_EFLAGS_VM)
1467 ? X86EMUL_MODE_REAL : cs_l
1468 ? X86EMUL_MODE_PROT64 : cs_db
1469 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1470
1471 if (vcpu->emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1472 vcpu->emulate_ctxt.cs_base = 0;
1473 vcpu->emulate_ctxt.ds_base = 0;
1474 vcpu->emulate_ctxt.es_base = 0;
1475 vcpu->emulate_ctxt.ss_base = 0;
1476 } else {
1477 vcpu->emulate_ctxt.cs_base =
1478 get_segment_base(vcpu, VCPU_SREG_CS);
1479 vcpu->emulate_ctxt.ds_base =
1480 get_segment_base(vcpu, VCPU_SREG_DS);
1481 vcpu->emulate_ctxt.es_base =
1482 get_segment_base(vcpu, VCPU_SREG_ES);
1483 vcpu->emulate_ctxt.ss_base =
1484 get_segment_base(vcpu, VCPU_SREG_SS);
1485 }
1486
1487 vcpu->emulate_ctxt.gs_base =
1488 get_segment_base(vcpu, VCPU_SREG_GS);
1489 vcpu->emulate_ctxt.fs_base =
1490 get_segment_base(vcpu, VCPU_SREG_FS);
1491
1492 r = x86_decode_insn(&vcpu->emulate_ctxt, &emulate_ops);
Laurent Viviera22436b2007-09-24 17:00:58 +02001493 if (r) {
1494 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1495 return EMULATE_DONE;
1496 return EMULATE_FAIL;
1497 }
Laurent Vivier34273182007-09-18 11:27:37 +02001498 }
1499
Laurent Viviera22436b2007-09-24 17:00:58 +02001500 r = x86_emulate_insn(&vcpu->emulate_ctxt, &emulate_ops);
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001501
Laurent Viviere70669a2007-08-05 10:36:40 +03001502 if (vcpu->pio.string)
1503 return EMULATE_DO_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001504
1505 if ((r || vcpu->mmio_is_write) && run) {
Jeff Dike8fc0d082007-07-17 12:26:59 -04001506 run->exit_reason = KVM_EXIT_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1508 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1509 run->mmio.len = vcpu->mmio_size;
1510 run->mmio.is_write = vcpu->mmio_is_write;
1511 }
1512
1513 if (r) {
Avi Kivitya4360362007-01-05 16:36:45 -08001514 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1515 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001516 if (!vcpu->mmio_needed) {
Avi Kivity054b1362007-09-12 13:21:09 +03001517 kvm_report_emulation_failure(vcpu, "mmio");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001518 return EMULATE_FAIL;
1519 }
1520 return EMULATE_DO_MMIO;
1521 }
1522
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001523 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier34273182007-09-18 11:27:37 +02001524 kvm_x86_ops->set_rflags(vcpu, vcpu->emulate_ctxt.eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001525
Avi Kivity02c83202007-04-29 15:02:17 +03001526 if (vcpu->mmio_is_write) {
1527 vcpu->mmio_needed = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001528 return EMULATE_DO_MMIO;
Avi Kivity02c83202007-04-29 15:02:17 +03001529 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001530
1531 return EMULATE_DONE;
1532}
1533EXPORT_SYMBOL_GPL(emulate_instruction);
1534
Eddie Dongb6958ce2007-07-18 12:15:21 +03001535/*
1536 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1537 */
He, Qingc5ec1532007-09-03 17:07:41 +03001538static void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001539{
1540 DECLARE_WAITQUEUE(wait, current);
1541
1542 add_wait_queue(&vcpu->wq, &wait);
1543
1544 /*
1545 * We will block until either an interrupt or a signal wakes us up
1546 */
He, Qingc5ec1532007-09-03 17:07:41 +03001547 while (!kvm_cpu_has_interrupt(vcpu)
1548 && !signal_pending(current)
1549 && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE
1550 && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) {
Eddie Dongb6958ce2007-07-18 12:15:21 +03001551 set_current_state(TASK_INTERRUPTIBLE);
1552 vcpu_put(vcpu);
1553 schedule();
1554 vcpu_load(vcpu);
1555 }
1556
He, Qingc5ec1532007-09-03 17:07:41 +03001557 __set_current_state(TASK_RUNNING);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001558 remove_wait_queue(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001559}
1560
Avi Kivityd3bef152007-06-05 15:53:05 +03001561int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1562{
Avi Kivityd3bef152007-06-05 15:53:05 +03001563 ++vcpu->stat.halt_exits;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001564 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc5ec1532007-09-03 17:07:41 +03001565 vcpu->mp_state = VCPU_MP_STATE_HALTED;
1566 kvm_vcpu_block(vcpu);
1567 if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE)
1568 return -EINTR;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001569 return 1;
1570 } else {
1571 vcpu->run->exit_reason = KVM_EXIT_HLT;
1572 return 0;
1573 }
Avi Kivityd3bef152007-06-05 15:53:05 +03001574}
1575EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1576
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001577int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
Avi Kivity270fd9b2007-02-19 14:37:47 +02001578{
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001579 unsigned long nr, a0, a1, a2, a3, ret;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001580
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001581 kvm_x86_ops->cache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001582
1583 nr = vcpu->regs[VCPU_REGS_RAX];
1584 a0 = vcpu->regs[VCPU_REGS_RBX];
1585 a1 = vcpu->regs[VCPU_REGS_RCX];
1586 a2 = vcpu->regs[VCPU_REGS_RDX];
1587 a3 = vcpu->regs[VCPU_REGS_RSI];
1588
1589 if (!is_long_mode(vcpu)) {
1590 nr &= 0xFFFFFFFF;
1591 a0 &= 0xFFFFFFFF;
1592 a1 &= 0xFFFFFFFF;
1593 a2 &= 0xFFFFFFFF;
1594 a3 &= 0xFFFFFFFF;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001595 }
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001596
Avi Kivity270fd9b2007-02-19 14:37:47 +02001597 switch (nr) {
1598 default:
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001599 ret = -KVM_ENOSYS;
1600 break;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001601 }
1602 vcpu->regs[VCPU_REGS_RAX] = ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001603 kvm_x86_ops->decache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001604 return 0;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001605}
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001606EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
1607
1608int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
1609{
1610 char instruction[3];
1611 int ret = 0;
1612
1613 mutex_lock(&vcpu->kvm->lock);
1614
1615 /*
1616 * Blow out the MMU to ensure that no other VCPU has an active mapping
1617 * to ensure that the updated hypercall appears atomically across all
1618 * VCPUs.
1619 */
1620 kvm_mmu_zap_all(vcpu->kvm);
1621
1622 kvm_x86_ops->cache_regs(vcpu);
1623 kvm_x86_ops->patch_hypercall(vcpu, instruction);
1624 if (emulator_write_emulated(vcpu->rip, instruction, 3, vcpu)
1625 != X86EMUL_CONTINUE)
1626 ret = -EFAULT;
1627
1628 mutex_unlock(&vcpu->kvm->lock);
1629
1630 return ret;
1631}
Avi Kivity270fd9b2007-02-19 14:37:47 +02001632
Avi Kivity6aa8b732006-12-10 02:21:36 -08001633static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1634{
1635 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1636}
1637
1638void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1639{
1640 struct descriptor_table dt = { limit, base };
1641
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001642 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001643}
1644
1645void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1646{
1647 struct descriptor_table dt = { limit, base };
1648
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001649 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001650}
1651
1652void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1653 unsigned long *rflags)
1654{
1655 lmsw(vcpu, msw);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001656 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001657}
1658
1659unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1660{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001661 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001662 switch (cr) {
1663 case 0:
1664 return vcpu->cr0;
1665 case 2:
1666 return vcpu->cr2;
1667 case 3:
1668 return vcpu->cr3;
1669 case 4:
1670 return vcpu->cr4;
1671 default:
1672 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1673 return 0;
1674 }
1675}
1676
1677void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1678 unsigned long *rflags)
1679{
1680 switch (cr) {
1681 case 0:
1682 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001683 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001684 break;
1685 case 2:
1686 vcpu->cr2 = val;
1687 break;
1688 case 3:
1689 set_cr3(vcpu, val);
1690 break;
1691 case 4:
1692 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1693 break;
1694 default:
1695 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1696 }
1697}
1698
Avi Kivity3bab1f52006-12-29 16:49:48 -08001699int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1700{
1701 u64 data;
1702
1703 switch (msr) {
1704 case 0xc0010010: /* SYSCFG */
1705 case 0xc0010015: /* HWCR */
1706 case MSR_IA32_PLATFORM_ID:
1707 case MSR_IA32_P5_MC_ADDR:
1708 case MSR_IA32_P5_MC_TYPE:
1709 case MSR_IA32_MC0_CTL:
1710 case MSR_IA32_MCG_STATUS:
1711 case MSR_IA32_MCG_CAP:
1712 case MSR_IA32_MC0_MISC:
1713 case MSR_IA32_MC0_MISC+4:
1714 case MSR_IA32_MC0_MISC+8:
1715 case MSR_IA32_MC0_MISC+12:
1716 case MSR_IA32_MC0_MISC+16:
1717 case MSR_IA32_UCODE_REV:
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001718 case MSR_IA32_PERF_STATUS:
Matthew Gregan2dc70942007-05-06 10:59:46 +03001719 case MSR_IA32_EBL_CR_POWERON:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001720 /* MTRR registers */
1721 case 0xfe:
1722 case 0x200 ... 0x2ff:
1723 data = 0;
1724 break;
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001725 case 0xcd: /* fsb frequency */
1726 data = 3;
1727 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001728 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001729 data = kvm_get_apic_base(vcpu);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001730 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001731 case MSR_IA32_MISC_ENABLE:
1732 data = vcpu->ia32_misc_enable_msr;
1733 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001734#ifdef CONFIG_X86_64
1735 case MSR_EFER:
1736 data = vcpu->shadow_efer;
1737 break;
1738#endif
1739 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001740 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001741 return 1;
1742 }
1743 *pdata = data;
1744 return 0;
1745}
1746EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1747
Avi Kivity6aa8b732006-12-10 02:21:36 -08001748/*
1749 * Reads an msr value (of 'msr_index') into 'pdata'.
1750 * Returns 0 on success, non-0 otherwise.
1751 * Assumes vcpu_load() was already called.
1752 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001753int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001754{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001755 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001756}
1757
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001758#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001759
Avi Kivity3bab1f52006-12-29 16:49:48 -08001760static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001761{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001762 if (efer & EFER_RESERVED_BITS) {
1763 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1764 efer);
1765 inject_gp(vcpu);
1766 return;
1767 }
1768
1769 if (is_paging(vcpu)
1770 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1771 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1772 inject_gp(vcpu);
1773 return;
1774 }
1775
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001776 kvm_x86_ops->set_efer(vcpu, efer);
Avi Kivity7725f0b2006-12-13 00:34:01 -08001777
Avi Kivity6aa8b732006-12-10 02:21:36 -08001778 efer &= ~EFER_LMA;
1779 efer |= vcpu->shadow_efer & EFER_LMA;
1780
1781 vcpu->shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001782}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001783
1784#endif
1785
Avi Kivity3bab1f52006-12-29 16:49:48 -08001786int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1787{
1788 switch (msr) {
1789#ifdef CONFIG_X86_64
1790 case MSR_EFER:
1791 set_efer(vcpu, data);
1792 break;
1793#endif
1794 case MSR_IA32_MC0_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001795 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
Avi Kivity3bab1f52006-12-29 16:49:48 -08001796 __FUNCTION__, data);
1797 break;
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001798 case MSR_IA32_MCG_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001799 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001800 __FUNCTION__, data);
1801 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001802 case MSR_IA32_UCODE_REV:
1803 case MSR_IA32_UCODE_WRITE:
1804 case 0x200 ... 0x2ff: /* MTRRs */
1805 break;
1806 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001807 kvm_set_apic_base(vcpu, data);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001808 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001809 case MSR_IA32_MISC_ENABLE:
1810 vcpu->ia32_misc_enable_msr = data;
1811 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001812 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001813 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001814 return 1;
1815 }
1816 return 0;
1817}
1818EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1819
Avi Kivity6aa8b732006-12-10 02:21:36 -08001820/*
1821 * Writes msr value into into the appropriate "register".
1822 * Returns 0 on success, non-0 otherwise.
1823 * Assumes vcpu_load() was already called.
1824 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001825int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001826{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001827 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001828}
1829
1830void kvm_resched(struct kvm_vcpu *vcpu)
1831{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001832 if (!need_resched())
1833 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001834 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001835}
1836EXPORT_SYMBOL_GPL(kvm_resched);
1837
Avi Kivity06465c52007-02-28 20:46:53 +02001838void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1839{
1840 int i;
1841 u32 function;
1842 struct kvm_cpuid_entry *e, *best;
1843
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001844 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001845 function = vcpu->regs[VCPU_REGS_RAX];
1846 vcpu->regs[VCPU_REGS_RAX] = 0;
1847 vcpu->regs[VCPU_REGS_RBX] = 0;
1848 vcpu->regs[VCPU_REGS_RCX] = 0;
1849 vcpu->regs[VCPU_REGS_RDX] = 0;
1850 best = NULL;
1851 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1852 e = &vcpu->cpuid_entries[i];
1853 if (e->function == function) {
1854 best = e;
1855 break;
1856 }
1857 /*
1858 * Both basic or both extended?
1859 */
1860 if (((e->function ^ function) & 0x80000000) == 0)
1861 if (!best || e->function > best->function)
1862 best = e;
1863 }
1864 if (best) {
1865 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1866 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1867 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1868 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1869 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001870 kvm_x86_ops->decache_regs(vcpu);
1871 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001872}
1873EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1874
Avi Kivity039576c2007-03-20 12:46:50 +02001875static int pio_copy_data(struct kvm_vcpu *vcpu)
Avi Kivity46fc1472007-02-22 19:39:30 +02001876{
Avi Kivity039576c2007-03-20 12:46:50 +02001877 void *p = vcpu->pio_data;
1878 void *q;
1879 unsigned bytes;
1880 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1881
Avi Kivity039576c2007-03-20 12:46:50 +02001882 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1883 PAGE_KERNEL);
1884 if (!q) {
Avi Kivity039576c2007-03-20 12:46:50 +02001885 free_pio_guest_pages(vcpu);
1886 return -ENOMEM;
1887 }
1888 q += vcpu->pio.guest_page_offset;
1889 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1890 if (vcpu->pio.in)
1891 memcpy(q, p, bytes);
1892 else
1893 memcpy(p, q, bytes);
1894 q -= vcpu->pio.guest_page_offset;
1895 vunmap(q);
Avi Kivity039576c2007-03-20 12:46:50 +02001896 free_pio_guest_pages(vcpu);
1897 return 0;
1898}
1899
1900static int complete_pio(struct kvm_vcpu *vcpu)
1901{
1902 struct kvm_pio_request *io = &vcpu->pio;
Avi Kivity46fc1472007-02-22 19:39:30 +02001903 long delta;
Avi Kivity039576c2007-03-20 12:46:50 +02001904 int r;
Avi Kivity46fc1472007-02-22 19:39:30 +02001905
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001906 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001907
1908 if (!io->string) {
Avi Kivity039576c2007-03-20 12:46:50 +02001909 if (io->in)
1910 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
Avi Kivity46fc1472007-02-22 19:39:30 +02001911 io->size);
1912 } else {
Avi Kivity039576c2007-03-20 12:46:50 +02001913 if (io->in) {
1914 r = pio_copy_data(vcpu);
1915 if (r) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001916 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02001917 return r;
1918 }
1919 }
1920
Avi Kivity46fc1472007-02-22 19:39:30 +02001921 delta = 1;
1922 if (io->rep) {
Avi Kivity039576c2007-03-20 12:46:50 +02001923 delta *= io->cur_count;
Avi Kivity46fc1472007-02-22 19:39:30 +02001924 /*
1925 * The size of the register should really depend on
1926 * current address size.
1927 */
1928 vcpu->regs[VCPU_REGS_RCX] -= delta;
1929 }
Avi Kivity039576c2007-03-20 12:46:50 +02001930 if (io->down)
Avi Kivity46fc1472007-02-22 19:39:30 +02001931 delta = -delta;
1932 delta *= io->size;
Avi Kivity039576c2007-03-20 12:46:50 +02001933 if (io->in)
Avi Kivity46fc1472007-02-22 19:39:30 +02001934 vcpu->regs[VCPU_REGS_RDI] += delta;
1935 else
1936 vcpu->regs[VCPU_REGS_RSI] += delta;
1937 }
1938
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001939 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001940
Avi Kivity039576c2007-03-20 12:46:50 +02001941 io->count -= io->cur_count;
1942 io->cur_count = 0;
1943
Avi Kivity039576c2007-03-20 12:46:50 +02001944 return 0;
Avi Kivity46fc1472007-02-22 19:39:30 +02001945}
1946
Eddie Dong65619eb2007-07-17 11:52:33 +03001947static void kernel_pio(struct kvm_io_device *pio_dev,
1948 struct kvm_vcpu *vcpu,
1949 void *pd)
Eddie Dong74906342007-06-19 18:05:03 +03001950{
1951 /* TODO: String I/O for in kernel device */
1952
Eddie Dong9cf98822007-07-22 10:36:31 +03001953 mutex_lock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001954 if (vcpu->pio.in)
1955 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1956 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001957 pd);
Eddie Dong74906342007-06-19 18:05:03 +03001958 else
1959 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1960 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001961 pd);
Eddie Dong9cf98822007-07-22 10:36:31 +03001962 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001963}
1964
1965static void pio_string_write(struct kvm_io_device *pio_dev,
1966 struct kvm_vcpu *vcpu)
1967{
1968 struct kvm_pio_request *io = &vcpu->pio;
1969 void *pd = vcpu->pio_data;
1970 int i;
1971
Eddie Dong9cf98822007-07-22 10:36:31 +03001972 mutex_lock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001973 for (i = 0; i < io->cur_count; i++) {
1974 kvm_iodevice_write(pio_dev, io->port,
1975 io->size,
1976 pd);
1977 pd += io->size;
1978 }
Eddie Dong9cf98822007-07-22 10:36:31 +03001979 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001980}
1981
Mike Dayd77c26f2007-10-08 09:02:08 -04001982int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
Laurent Vivier3090dd72007-08-05 10:43:32 +03001983 int size, unsigned port)
1984{
1985 struct kvm_io_device *pio_dev;
1986
1987 vcpu->run->exit_reason = KVM_EXIT_IO;
1988 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1989 vcpu->run->io.size = vcpu->pio.size = size;
1990 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1991 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1;
1992 vcpu->run->io.port = vcpu->pio.port = port;
1993 vcpu->pio.in = in;
1994 vcpu->pio.string = 0;
1995 vcpu->pio.down = 0;
1996 vcpu->pio.guest_page_offset = 0;
1997 vcpu->pio.rep = 0;
1998
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001999 kvm_x86_ops->cache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03002000 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002001 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03002002
Avi Kivity0967b7b2007-09-15 17:34:36 +03002003 kvm_x86_ops->skip_emulated_instruction(vcpu);
2004
Laurent Vivier3090dd72007-08-05 10:43:32 +03002005 pio_dev = vcpu_find_pio_dev(vcpu, port);
2006 if (pio_dev) {
2007 kernel_pio(pio_dev, vcpu, vcpu->pio_data);
2008 complete_pio(vcpu);
2009 return 1;
2010 }
2011 return 0;
2012}
2013EXPORT_SYMBOL_GPL(kvm_emulate_pio);
2014
2015int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2016 int size, unsigned long count, int down,
Avi Kivity039576c2007-03-20 12:46:50 +02002017 gva_t address, int rep, unsigned port)
2018{
2019 unsigned now, in_page;
Eddie Dong65619eb2007-07-17 11:52:33 +03002020 int i, ret = 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002021 int nr_pages = 1;
2022 struct page *page;
Eddie Dong74906342007-06-19 18:05:03 +03002023 struct kvm_io_device *pio_dev;
Avi Kivity039576c2007-03-20 12:46:50 +02002024
2025 vcpu->run->exit_reason = KVM_EXIT_IO;
2026 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Laurent Vivier3090dd72007-08-05 10:43:32 +03002027 vcpu->run->io.size = vcpu->pio.size = size;
Avi Kivity039576c2007-03-20 12:46:50 +02002028 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Laurent Vivier3090dd72007-08-05 10:43:32 +03002029 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count;
2030 vcpu->run->io.port = vcpu->pio.port = port;
Avi Kivity039576c2007-03-20 12:46:50 +02002031 vcpu->pio.in = in;
Laurent Vivier3090dd72007-08-05 10:43:32 +03002032 vcpu->pio.string = 1;
Avi Kivity039576c2007-03-20 12:46:50 +02002033 vcpu->pio.down = down;
2034 vcpu->pio.guest_page_offset = offset_in_page(address);
2035 vcpu->pio.rep = rep;
2036
Avi Kivity039576c2007-03-20 12:46:50 +02002037 if (!count) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002038 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02002039 return 1;
2040 }
2041
Avi Kivity039576c2007-03-20 12:46:50 +02002042 if (!down)
2043 in_page = PAGE_SIZE - offset_in_page(address);
2044 else
2045 in_page = offset_in_page(address) + size;
2046 now = min(count, (unsigned long)in_page / size);
2047 if (!now) {
2048 /*
2049 * String I/O straddles page boundary. Pin two guest pages
2050 * so that we satisfy atomicity constraints. Do just one
2051 * transaction to avoid complexity.
2052 */
2053 nr_pages = 2;
2054 now = 1;
2055 }
2056 if (down) {
2057 /*
2058 * String I/O in reverse. Yuck. Kill the guest, fix later.
2059 */
Rusty Russellf0242472007-08-01 10:48:02 +10002060 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivity039576c2007-03-20 12:46:50 +02002061 inject_gp(vcpu);
2062 return 1;
2063 }
2064 vcpu->run->io.count = now;
2065 vcpu->pio.cur_count = now;
2066
Avi Kivity0967b7b2007-09-15 17:34:36 +03002067 if (vcpu->pio.cur_count == vcpu->pio.count)
2068 kvm_x86_ops->skip_emulated_instruction(vcpu);
2069
Avi Kivity039576c2007-03-20 12:46:50 +02002070 for (i = 0; i < nr_pages; ++i) {
Shaohua Li11ec2802007-07-23 14:51:37 +08002071 mutex_lock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02002072 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
2073 if (page)
2074 get_page(page);
2075 vcpu->pio.guest_pages[i] = page;
Shaohua Li11ec2802007-07-23 14:51:37 +08002076 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02002077 if (!page) {
2078 inject_gp(vcpu);
2079 free_pio_guest_pages(vcpu);
2080 return 1;
2081 }
2082 }
2083
Laurent Vivier3090dd72007-08-05 10:43:32 +03002084 pio_dev = vcpu_find_pio_dev(vcpu, port);
Eddie Dong65619eb2007-07-17 11:52:33 +03002085 if (!vcpu->pio.in) {
2086 /* string PIO write */
2087 ret = pio_copy_data(vcpu);
2088 if (ret >= 0 && pio_dev) {
2089 pio_string_write(pio_dev, vcpu);
2090 complete_pio(vcpu);
2091 if (vcpu->pio.count == 0)
2092 ret = 1;
2093 }
2094 } else if (pio_dev)
Rusty Russellf0242472007-08-01 10:48:02 +10002095 pr_unimpl(vcpu, "no string pio read support yet, "
Eddie Dong65619eb2007-07-17 11:52:33 +03002096 "port %x size %d count %ld\n",
2097 port, size, count);
2098
2099 return ret;
Avi Kivity039576c2007-03-20 12:46:50 +02002100}
Laurent Vivier3090dd72007-08-05 10:43:32 +03002101EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
Avi Kivity039576c2007-03-20 12:46:50 +02002102
Avi Kivity04d2cc72007-09-10 18:10:54 +03002103/*
2104 * Check if userspace requested an interrupt window, and that the
2105 * interrupt window is open.
2106 *
2107 * No need to exit to userspace if we already have an interrupt queued.
2108 */
2109static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2110 struct kvm_run *kvm_run)
2111{
2112 return (!vcpu->irq_summary &&
2113 kvm_run->request_interrupt_window &&
2114 vcpu->interrupt_window_open &&
2115 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
2116}
2117
2118static void post_kvm_run_save(struct kvm_vcpu *vcpu,
2119 struct kvm_run *kvm_run)
2120{
2121 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
2122 kvm_run->cr8 = get_cr8(vcpu);
2123 kvm_run->apic_base = kvm_get_apic_base(vcpu);
2124 if (irqchip_in_kernel(vcpu->kvm))
2125 kvm_run->ready_for_interrupt_injection = 1;
2126 else
2127 kvm_run->ready_for_interrupt_injection =
2128 (vcpu->interrupt_window_open &&
2129 vcpu->irq_summary == 0);
2130}
2131
2132static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2133{
2134 int r;
2135
2136 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002137 pr_debug("vcpu %d received sipi with vector # %x\n",
Avi Kivity04d2cc72007-09-10 18:10:54 +03002138 vcpu->vcpu_id, vcpu->sipi_vector);
2139 kvm_lapic_reset(vcpu);
2140 kvm_x86_ops->vcpu_reset(vcpu);
2141 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
2142 }
2143
2144preempted:
2145 if (vcpu->guest_debug.enabled)
2146 kvm_x86_ops->guest_debug_pre(vcpu);
2147
2148again:
2149 r = kvm_mmu_reload(vcpu);
2150 if (unlikely(r))
2151 goto out;
2152
Avi Kivityab6ef342007-10-16 16:23:22 +02002153 kvm_inject_pending_timer_irqs(vcpu);
2154
Avi Kivity04d2cc72007-09-10 18:10:54 +03002155 preempt_disable();
2156
2157 kvm_x86_ops->prepare_guest_switch(vcpu);
2158 kvm_load_guest_fpu(vcpu);
2159
2160 local_irq_disable();
2161
2162 if (signal_pending(current)) {
2163 local_irq_enable();
2164 preempt_enable();
2165 r = -EINTR;
2166 kvm_run->exit_reason = KVM_EXIT_INTR;
2167 ++vcpu->stat.signal_exits;
2168 goto out;
2169 }
2170
2171 if (irqchip_in_kernel(vcpu->kvm))
2172 kvm_x86_ops->inject_pending_irq(vcpu);
2173 else if (!vcpu->mmio_read_completed)
2174 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2175
2176 vcpu->guest_mode = 1;
Laurent Vivierd172fcd2007-10-15 17:00:19 +02002177 kvm_guest_enter();
Avi Kivity04d2cc72007-09-10 18:10:54 +03002178
2179 if (vcpu->requests)
Avi Kivity3176bc32007-10-16 17:22:08 +02002180 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivity04d2cc72007-09-10 18:10:54 +03002181 kvm_x86_ops->tlb_flush(vcpu);
2182
2183 kvm_x86_ops->run(vcpu, kvm_run);
2184
2185 vcpu->guest_mode = 0;
2186 local_irq_enable();
2187
2188 ++vcpu->stat.exits;
2189
Laurent Vivier0552f732007-10-18 15:19:01 +02002190 /*
2191 * We must have an instruction between local_irq_enable() and
2192 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2193 * the interrupt shadow. The stat.exits increment will do nicely.
2194 * But we need to prevent reordering, hence this barrier():
2195 */
2196 barrier();
2197
2198 kvm_guest_exit();
2199
Avi Kivity04d2cc72007-09-10 18:10:54 +03002200 preempt_enable();
2201
2202 /*
2203 * Profile KVM exit RIPs:
2204 */
2205 if (unlikely(prof_on == KVM_PROFILING)) {
2206 kvm_x86_ops->cache_regs(vcpu);
2207 profile_hit(KVM_PROFILING, (void *)vcpu->rip);
2208 }
2209
2210 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2211
2212 if (r > 0) {
2213 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2214 r = -EINTR;
2215 kvm_run->exit_reason = KVM_EXIT_INTR;
2216 ++vcpu->stat.request_irq_exits;
2217 goto out;
2218 }
2219 if (!need_resched()) {
2220 ++vcpu->stat.light_exits;
2221 goto again;
2222 }
2223 }
2224
2225out:
2226 if (r > 0) {
2227 kvm_resched(vcpu);
2228 goto preempted;
2229 }
2230
2231 post_kvm_run_save(vcpu, kvm_run);
2232
2233 return r;
2234}
2235
2236
Avi Kivitybccf2152007-02-21 18:04:26 +02002237static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002238{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002239 int r;
Avi Kivity1961d272007-03-05 19:46:05 +02002240 sigset_t sigsaved;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002241
Avi Kivitybccf2152007-02-21 18:04:26 +02002242 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002243
He, Qingc5ec1532007-09-03 17:07:41 +03002244 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
2245 kvm_vcpu_block(vcpu);
2246 vcpu_put(vcpu);
2247 return -EAGAIN;
2248 }
2249
Avi Kivity1961d272007-03-05 19:46:05 +02002250 if (vcpu->sigset_active)
2251 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2252
Dor Laor54810342007-02-12 00:54:39 -08002253 /* re-sync apic's tpr */
He, Qing5cd4f6f2007-08-30 17:04:26 +08002254 if (!irqchip_in_kernel(vcpu->kvm))
2255 set_cr8(vcpu, kvm_run->cr8);
Dor Laor54810342007-02-12 00:54:39 -08002256
Avi Kivity02c83202007-04-29 15:02:17 +03002257 if (vcpu->pio.cur_count) {
2258 r = complete_pio(vcpu);
2259 if (r)
2260 goto out;
2261 }
2262
2263 if (vcpu->mmio_needed) {
2264 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
2265 vcpu->mmio_read_completed = 1;
2266 vcpu->mmio_needed = 0;
2267 r = emulate_instruction(vcpu, kvm_run,
Laurent Vivier34273182007-09-18 11:27:37 +02002268 vcpu->mmio_fault_cr2, 0, 1);
Avi Kivity02c83202007-04-29 15:02:17 +03002269 if (r == EMULATE_DO_MMIO) {
2270 /*
2271 * Read-modify-write. Back to userspace.
2272 */
Avi Kivity02c83202007-04-29 15:02:17 +03002273 r = 0;
2274 goto out;
Avi Kivity46fc1472007-02-22 19:39:30 +02002275 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002276 }
2277
Avi Kivity8eb7d332007-03-04 14:17:08 +02002278 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002279 kvm_x86_ops->cache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002280 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002281 kvm_x86_ops->decache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002282 }
2283
Avi Kivity04d2cc72007-09-10 18:10:54 +03002284 r = __vcpu_run(vcpu, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002285
Avi Kivity039576c2007-03-20 12:46:50 +02002286out:
Avi Kivity1961d272007-03-05 19:46:05 +02002287 if (vcpu->sigset_active)
2288 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2289
Avi Kivity6aa8b732006-12-10 02:21:36 -08002290 vcpu_put(vcpu);
2291 return r;
2292}
2293
Avi Kivitybccf2152007-02-21 18:04:26 +02002294static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
2295 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002296{
Avi Kivitybccf2152007-02-21 18:04:26 +02002297 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002298
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002299 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002300
2301 regs->rax = vcpu->regs[VCPU_REGS_RAX];
2302 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
2303 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
2304 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
2305 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
2306 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
2307 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
2308 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002309#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002310 regs->r8 = vcpu->regs[VCPU_REGS_R8];
2311 regs->r9 = vcpu->regs[VCPU_REGS_R9];
2312 regs->r10 = vcpu->regs[VCPU_REGS_R10];
2313 regs->r11 = vcpu->regs[VCPU_REGS_R11];
2314 regs->r12 = vcpu->regs[VCPU_REGS_R12];
2315 regs->r13 = vcpu->regs[VCPU_REGS_R13];
2316 regs->r14 = vcpu->regs[VCPU_REGS_R14];
2317 regs->r15 = vcpu->regs[VCPU_REGS_R15];
2318#endif
2319
2320 regs->rip = vcpu->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002321 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002322
2323 /*
2324 * Don't leak debug flags in case they were set for guest debugging
2325 */
2326 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2327 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2328
2329 vcpu_put(vcpu);
2330
2331 return 0;
2332}
2333
Avi Kivitybccf2152007-02-21 18:04:26 +02002334static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
2335 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002336{
Avi Kivitybccf2152007-02-21 18:04:26 +02002337 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002338
2339 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
2340 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
2341 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
2342 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
2343 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
2344 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
2345 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
2346 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002347#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002348 vcpu->regs[VCPU_REGS_R8] = regs->r8;
2349 vcpu->regs[VCPU_REGS_R9] = regs->r9;
2350 vcpu->regs[VCPU_REGS_R10] = regs->r10;
2351 vcpu->regs[VCPU_REGS_R11] = regs->r11;
2352 vcpu->regs[VCPU_REGS_R12] = regs->r12;
2353 vcpu->regs[VCPU_REGS_R13] = regs->r13;
2354 vcpu->regs[VCPU_REGS_R14] = regs->r14;
2355 vcpu->regs[VCPU_REGS_R15] = regs->r15;
2356#endif
2357
2358 vcpu->rip = regs->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002359 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002360
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002361 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002362
2363 vcpu_put(vcpu);
2364
2365 return 0;
2366}
2367
2368static void get_segment(struct kvm_vcpu *vcpu,
2369 struct kvm_segment *var, int seg)
2370{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002371 return kvm_x86_ops->get_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002372}
2373
Avi Kivitybccf2152007-02-21 18:04:26 +02002374static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2375 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002376{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002377 struct descriptor_table dt;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002378 int pending_vec;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002379
Avi Kivitybccf2152007-02-21 18:04:26 +02002380 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002381
2382 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2383 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2384 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2385 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2386 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2387 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2388
2389 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2390 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2391
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002392 kvm_x86_ops->get_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002393 sregs->idt.limit = dt.limit;
2394 sregs->idt.base = dt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002395 kvm_x86_ops->get_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002396 sregs->gdt.limit = dt.limit;
2397 sregs->gdt.base = dt.base;
2398
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002399 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002400 sregs->cr0 = vcpu->cr0;
2401 sregs->cr2 = vcpu->cr2;
2402 sregs->cr3 = vcpu->cr3;
2403 sregs->cr4 = vcpu->cr4;
Eddie Dong7017fc32007-07-18 11:34:57 +03002404 sregs->cr8 = get_cr8(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002405 sregs->efer = vcpu->shadow_efer;
Eddie Dong7017fc32007-07-18 11:34:57 +03002406 sregs->apic_base = kvm_get_apic_base(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002407
Eddie Dong2a8067f2007-08-06 16:29:07 +03002408 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc52fb352007-08-02 14:03:07 +03002409 memset(sregs->interrupt_bitmap, 0,
2410 sizeof sregs->interrupt_bitmap);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002411 pending_vec = kvm_x86_ops->get_irq(vcpu);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002412 if (pending_vec >= 0)
Mike Dayd77c26f2007-10-08 09:02:08 -04002413 set_bit(pending_vec,
2414 (unsigned long *)sregs->interrupt_bitmap);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002415 } else
He, Qingc52fb352007-08-02 14:03:07 +03002416 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2417 sizeof sregs->interrupt_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002418
2419 vcpu_put(vcpu);
2420
2421 return 0;
2422}
2423
2424static void set_segment(struct kvm_vcpu *vcpu,
2425 struct kvm_segment *var, int seg)
2426{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002427 return kvm_x86_ops->set_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002428}
2429
Avi Kivitybccf2152007-02-21 18:04:26 +02002430static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2431 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002432{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002433 int mmu_reset_needed = 0;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002434 int i, pending_vec, max_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002435 struct descriptor_table dt;
2436
Avi Kivitybccf2152007-02-21 18:04:26 +02002437 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002438
Avi Kivity6aa8b732006-12-10 02:21:36 -08002439 dt.limit = sregs->idt.limit;
2440 dt.base = sregs->idt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002441 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002442 dt.limit = sregs->gdt.limit;
2443 dt.base = sregs->gdt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002444 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002445
2446 vcpu->cr2 = sregs->cr2;
2447 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2448 vcpu->cr3 = sregs->cr3;
2449
Eddie Dong7017fc32007-07-18 11:34:57 +03002450 set_cr8(vcpu, sregs->cr8);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002451
2452 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002453#ifdef CONFIG_X86_64
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002454 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002455#endif
Eddie Dong7017fc32007-07-18 11:34:57 +03002456 kvm_set_apic_base(vcpu, sregs->apic_base);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002457
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002458 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity399badf2007-01-05 16:36:38 -08002459
Avi Kivity6aa8b732006-12-10 02:21:36 -08002460 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
Rusty Russell81f50e32007-09-06 01:20:38 +10002461 vcpu->cr0 = sregs->cr0;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002462 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002463
2464 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002465 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Avi Kivity1b0973b2007-01-05 16:36:41 -08002466 if (!is_long_mode(vcpu) && is_pae(vcpu))
2467 load_pdptrs(vcpu, vcpu->cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002468
2469 if (mmu_reset_needed)
2470 kvm_mmu_reset_context(vcpu);
2471
He, Qingc52fb352007-08-02 14:03:07 +03002472 if (!irqchip_in_kernel(vcpu->kvm)) {
2473 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2474 sizeof vcpu->irq_pending);
2475 vcpu->irq_summary = 0;
2476 for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
2477 if (vcpu->irq_pending[i])
2478 __set_bit(i, &vcpu->irq_summary);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002479 } else {
2480 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
2481 pending_vec = find_first_bit(
2482 (const unsigned long *)sregs->interrupt_bitmap,
2483 max_bits);
2484 /* Only pending external irq is handled here */
2485 if (pending_vec < max_bits) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002486 kvm_x86_ops->set_irq(vcpu, pending_vec);
Mike Dayd77c26f2007-10-08 09:02:08 -04002487 pr_debug("Set back pending irq %d\n",
2488 pending_vec);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002489 }
He, Qingc52fb352007-08-02 14:03:07 +03002490 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002491
Avi Kivity024aa1c2007-03-21 13:44:58 +02002492 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2493 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2494 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2495 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2496 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2497 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2498
2499 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2500 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2501
Avi Kivity6aa8b732006-12-10 02:21:36 -08002502 vcpu_put(vcpu);
2503
2504 return 0;
2505}
2506
Rusty Russell1747fb72007-09-06 01:21:32 +10002507void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2508{
2509 struct kvm_segment cs;
2510
2511 get_segment(vcpu, &cs, VCPU_SREG_CS);
2512 *db = cs.db;
2513 *l = cs.l;
2514}
2515EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2516
Avi Kivity6aa8b732006-12-10 02:21:36 -08002517/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002518 * Translate a guest virtual address to a guest physical address.
2519 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002520static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2521 struct kvm_translation *tr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002522{
2523 unsigned long vaddr = tr->linear_address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002524 gpa_t gpa;
2525
Avi Kivitybccf2152007-02-21 18:04:26 +02002526 vcpu_load(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +08002527 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002528 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2529 tr->physical_address = gpa;
2530 tr->valid = gpa != UNMAPPED_GVA;
2531 tr->writeable = 1;
2532 tr->usermode = 0;
Shaohua Li11ec2802007-07-23 14:51:37 +08002533 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002534 vcpu_put(vcpu);
2535
2536 return 0;
2537}
2538
Avi Kivitybccf2152007-02-21 18:04:26 +02002539static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2540 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002541{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002542 if (irq->irq < 0 || irq->irq >= 256)
2543 return -EINVAL;
Eddie Dong97222cc2007-09-12 10:58:04 +03002544 if (irqchip_in_kernel(vcpu->kvm))
2545 return -ENXIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02002546 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002547
2548 set_bit(irq->irq, vcpu->irq_pending);
2549 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2550
2551 vcpu_put(vcpu);
2552
2553 return 0;
2554}
2555
Avi Kivitybccf2152007-02-21 18:04:26 +02002556static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2557 struct kvm_debug_guest *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002558{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002559 int r;
2560
Avi Kivitybccf2152007-02-21 18:04:26 +02002561 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002562
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002563 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002564
2565 vcpu_put(vcpu);
2566
2567 return r;
2568}
2569
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002570static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2571 unsigned long address,
2572 int *type)
2573{
2574 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2575 unsigned long pgoff;
2576 struct page *page;
2577
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002578 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +02002579 if (pgoff == 0)
2580 page = virt_to_page(vcpu->run);
2581 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2582 page = virt_to_page(vcpu->pio_data);
2583 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002584 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002585 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002586 if (type != NULL)
2587 *type = VM_FAULT_MINOR;
2588
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002589 return page;
2590}
2591
2592static struct vm_operations_struct kvm_vcpu_vm_ops = {
2593 .nopage = kvm_vcpu_nopage,
2594};
2595
2596static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2597{
2598 vma->vm_ops = &kvm_vcpu_vm_ops;
2599 return 0;
2600}
2601
Avi Kivitybccf2152007-02-21 18:04:26 +02002602static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2603{
2604 struct kvm_vcpu *vcpu = filp->private_data;
2605
2606 fput(vcpu->kvm->filp);
2607 return 0;
2608}
2609
2610static struct file_operations kvm_vcpu_fops = {
2611 .release = kvm_vcpu_release,
2612 .unlocked_ioctl = kvm_vcpu_ioctl,
2613 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002614 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02002615};
2616
2617/*
2618 * Allocates an inode for the vcpu.
2619 */
2620static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2621{
2622 int fd, r;
2623 struct inode *inode;
2624 struct file *file;
2625
Avi Kivityd6d28162007-06-28 08:38:16 -04002626 r = anon_inode_getfd(&fd, &inode, &file,
2627 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2628 if (r)
2629 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002630 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +02002631 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02002632}
2633
Avi Kivityc5ea7662007-02-20 18:41:05 +02002634/*
2635 * Creates some virtual cpus. Good luck creating more than one.
2636 */
2637static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2638{
2639 int r;
2640 struct kvm_vcpu *vcpu;
2641
Avi Kivityc5ea7662007-02-20 18:41:05 +02002642 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002643 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002644
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002645 vcpu = kvm_x86_ops->vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002646 if (IS_ERR(vcpu))
2647 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002648
Avi Kivity15ad7142007-07-11 18:17:21 +03002649 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
2650
Rusty Russellb114b082007-07-30 21:13:43 +10002651 /* We do fxsave: this must be aligned. */
2652 BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF);
2653
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002654 vcpu_load(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002655 r = kvm_mmu_setup(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002656 vcpu_put(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002657 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002658 goto free_vcpu;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002659
Shaohua Li11ec2802007-07-23 14:51:37 +08002660 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002661 if (kvm->vcpus[n]) {
2662 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +08002663 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002664 goto mmu_unload;
2665 }
2666 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08002667 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002668
2669 /* Now it's all set up, let userspace reach it */
Avi Kivitybccf2152007-02-21 18:04:26 +02002670 r = create_vcpu_fd(vcpu);
2671 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002672 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02002673 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002674
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002675unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08002676 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002677 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +08002678 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002679
2680mmu_unload:
2681 vcpu_load(vcpu);
2682 kvm_mmu_unload(vcpu);
2683 vcpu_put(vcpu);
2684
2685free_vcpu:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002686 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002687 return r;
2688}
2689
Avi Kivity1961d272007-03-05 19:46:05 +02002690static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2691{
2692 if (sigset) {
2693 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2694 vcpu->sigset_active = 1;
2695 vcpu->sigset = *sigset;
2696 } else
2697 vcpu->sigset_active = 0;
2698 return 0;
2699}
2700
Avi Kivityb8836732007-04-01 16:34:31 +03002701/*
2702 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2703 * we have asm/x86/processor.h
2704 */
2705struct fxsave {
2706 u16 cwd;
2707 u16 swd;
2708 u16 twd;
2709 u16 fop;
2710 u64 rip;
2711 u64 rdp;
2712 u32 mxcsr;
2713 u32 mxcsr_mask;
2714 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2715#ifdef CONFIG_X86_64
2716 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2717#else
2718 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2719#endif
2720};
2721
2722static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2723{
Rusty Russellb114b082007-07-30 21:13:43 +10002724 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002725
2726 vcpu_load(vcpu);
2727
2728 memcpy(fpu->fpr, fxsave->st_space, 128);
2729 fpu->fcw = fxsave->cwd;
2730 fpu->fsw = fxsave->swd;
2731 fpu->ftwx = fxsave->twd;
2732 fpu->last_opcode = fxsave->fop;
2733 fpu->last_ip = fxsave->rip;
2734 fpu->last_dp = fxsave->rdp;
2735 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2736
2737 vcpu_put(vcpu);
2738
2739 return 0;
2740}
2741
2742static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2743{
Rusty Russellb114b082007-07-30 21:13:43 +10002744 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002745
2746 vcpu_load(vcpu);
2747
2748 memcpy(fxsave->st_space, fpu->fpr, 128);
2749 fxsave->cwd = fpu->fcw;
2750 fxsave->swd = fpu->fsw;
2751 fxsave->twd = fpu->ftwx;
2752 fxsave->fop = fpu->last_opcode;
2753 fxsave->rip = fpu->last_ip;
2754 fxsave->rdp = fpu->last_dp;
2755 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2756
2757 vcpu_put(vcpu);
2758
2759 return 0;
2760}
2761
Avi Kivitybccf2152007-02-21 18:04:26 +02002762static long kvm_vcpu_ioctl(struct file *filp,
2763 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002764{
Avi Kivitybccf2152007-02-21 18:04:26 +02002765 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +00002766 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02002767 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002768
2769 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002770 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002771 r = -EINVAL;
2772 if (arg)
2773 goto out;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002774 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002775 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002776 case KVM_GET_REGS: {
2777 struct kvm_regs kvm_regs;
2778
Avi Kivitybccf2152007-02-21 18:04:26 +02002779 memset(&kvm_regs, 0, sizeof kvm_regs);
2780 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002781 if (r)
2782 goto out;
2783 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002784 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002785 goto out;
2786 r = 0;
2787 break;
2788 }
2789 case KVM_SET_REGS: {
2790 struct kvm_regs kvm_regs;
2791
2792 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002793 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002794 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002795 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002796 if (r)
2797 goto out;
2798 r = 0;
2799 break;
2800 }
2801 case KVM_GET_SREGS: {
2802 struct kvm_sregs kvm_sregs;
2803
Avi Kivitybccf2152007-02-21 18:04:26 +02002804 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2805 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002806 if (r)
2807 goto out;
2808 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002809 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002810 goto out;
2811 r = 0;
2812 break;
2813 }
2814 case KVM_SET_SREGS: {
2815 struct kvm_sregs kvm_sregs;
2816
2817 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002818 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002819 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002820 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002821 if (r)
2822 goto out;
2823 r = 0;
2824 break;
2825 }
2826 case KVM_TRANSLATE: {
2827 struct kvm_translation tr;
2828
2829 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002830 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002831 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002832 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002833 if (r)
2834 goto out;
2835 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002836 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002837 goto out;
2838 r = 0;
2839 break;
2840 }
2841 case KVM_INTERRUPT: {
2842 struct kvm_interrupt irq;
2843
2844 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002845 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002846 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002847 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002848 if (r)
2849 goto out;
2850 r = 0;
2851 break;
2852 }
2853 case KVM_DEBUG_GUEST: {
2854 struct kvm_debug_guest dbg;
2855
2856 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002857 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002858 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002859 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002860 if (r)
2861 goto out;
2862 r = 0;
2863 break;
2864 }
Avi Kivity1961d272007-03-05 19:46:05 +02002865 case KVM_SET_SIGNAL_MASK: {
2866 struct kvm_signal_mask __user *sigmask_arg = argp;
2867 struct kvm_signal_mask kvm_sigmask;
2868 sigset_t sigset, *p;
2869
2870 p = NULL;
2871 if (argp) {
2872 r = -EFAULT;
2873 if (copy_from_user(&kvm_sigmask, argp,
2874 sizeof kvm_sigmask))
2875 goto out;
2876 r = -EINVAL;
2877 if (kvm_sigmask.len != sizeof sigset)
2878 goto out;
2879 r = -EFAULT;
2880 if (copy_from_user(&sigset, sigmask_arg->sigset,
2881 sizeof sigset))
2882 goto out;
2883 p = &sigset;
2884 }
2885 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2886 break;
2887 }
Avi Kivityb8836732007-04-01 16:34:31 +03002888 case KVM_GET_FPU: {
2889 struct kvm_fpu fpu;
2890
2891 memset(&fpu, 0, sizeof fpu);
2892 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2893 if (r)
2894 goto out;
2895 r = -EFAULT;
2896 if (copy_to_user(argp, &fpu, sizeof fpu))
2897 goto out;
2898 r = 0;
2899 break;
2900 }
2901 case KVM_SET_FPU: {
2902 struct kvm_fpu fpu;
2903
2904 r = -EFAULT;
2905 if (copy_from_user(&fpu, argp, sizeof fpu))
2906 goto out;
2907 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2908 if (r)
2909 goto out;
2910 r = 0;
2911 break;
2912 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002913 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02002914 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02002915 }
2916out:
2917 return r;
2918}
2919
2920static long kvm_vm_ioctl(struct file *filp,
2921 unsigned int ioctl, unsigned long arg)
2922{
2923 struct kvm *kvm = filp->private_data;
2924 void __user *argp = (void __user *)arg;
2925 int r = -EINVAL;
2926
2927 switch (ioctl) {
2928 case KVM_CREATE_VCPU:
2929 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2930 if (r < 0)
2931 goto out;
2932 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002933 case KVM_SET_MEMORY_REGION: {
2934 struct kvm_memory_region kvm_mem;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002935 struct kvm_userspace_memory_region kvm_userspace_mem;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002936
2937 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002938 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002939 goto out;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002940 kvm_userspace_mem.slot = kvm_mem.slot;
2941 kvm_userspace_mem.flags = kvm_mem.flags;
2942 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
2943 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
2944 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
2945 if (r)
2946 goto out;
2947 break;
2948 }
2949 case KVM_SET_USER_MEMORY_REGION: {
2950 struct kvm_userspace_memory_region kvm_userspace_mem;
2951
2952 r = -EFAULT;
2953 if (copy_from_user(&kvm_userspace_mem, argp,
2954 sizeof kvm_userspace_mem))
2955 goto out;
2956
2957 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002958 if (r)
2959 goto out;
2960 break;
2961 }
Izik Eidus82ce2c92007-10-02 18:52:55 +02002962 case KVM_SET_NR_MMU_PAGES:
2963 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
2964 if (r)
2965 goto out;
2966 break;
2967 case KVM_GET_NR_MMU_PAGES:
2968 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
2969 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002970 case KVM_GET_DIRTY_LOG: {
2971 struct kvm_dirty_log log;
2972
2973 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002974 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002975 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002976 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002977 if (r)
2978 goto out;
2979 break;
2980 }
Avi Kivitye8207542007-03-30 16:54:30 +03002981 case KVM_SET_MEMORY_ALIAS: {
2982 struct kvm_memory_alias alias;
2983
2984 r = -EFAULT;
2985 if (copy_from_user(&alias, argp, sizeof alias))
2986 goto out;
2987 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
2988 if (r)
2989 goto out;
2990 break;
2991 }
Eddie Dong85f455f2007-07-06 12:20:49 +03002992 case KVM_CREATE_IRQCHIP:
2993 r = -ENOMEM;
2994 kvm->vpic = kvm_create_pic(kvm);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03002995 if (kvm->vpic) {
2996 r = kvm_ioapic_init(kvm);
2997 if (r) {
2998 kfree(kvm->vpic);
2999 kvm->vpic = NULL;
3000 goto out;
3001 }
Mike Dayd77c26f2007-10-08 09:02:08 -04003002 } else
Eddie Dong85f455f2007-07-06 12:20:49 +03003003 goto out;
3004 break;
3005 case KVM_IRQ_LINE: {
3006 struct kvm_irq_level irq_event;
3007
3008 r = -EFAULT;
3009 if (copy_from_user(&irq_event, argp, sizeof irq_event))
3010 goto out;
3011 if (irqchip_in_kernel(kvm)) {
Eddie Dong9cf98822007-07-22 10:36:31 +03003012 mutex_lock(&kvm->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +03003013 if (irq_event.irq < 16)
3014 kvm_pic_set_irq(pic_irqchip(kvm),
3015 irq_event.irq,
3016 irq_event.level);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03003017 kvm_ioapic_set_irq(kvm->vioapic,
3018 irq_event.irq,
3019 irq_event.level);
Eddie Dong9cf98822007-07-22 10:36:31 +03003020 mutex_unlock(&kvm->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +03003021 r = 0;
3022 }
3023 break;
3024 }
He, Qing6ceb9d72007-07-26 11:05:18 +03003025 case KVM_GET_IRQCHIP: {
3026 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3027 struct kvm_irqchip chip;
3028
3029 r = -EFAULT;
3030 if (copy_from_user(&chip, argp, sizeof chip))
3031 goto out;
3032 r = -ENXIO;
3033 if (!irqchip_in_kernel(kvm))
3034 goto out;
3035 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
3036 if (r)
3037 goto out;
3038 r = -EFAULT;
3039 if (copy_to_user(argp, &chip, sizeof chip))
3040 goto out;
3041 r = 0;
3042 break;
3043 }
3044 case KVM_SET_IRQCHIP: {
3045 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3046 struct kvm_irqchip chip;
3047
3048 r = -EFAULT;
3049 if (copy_from_user(&chip, argp, sizeof chip))
3050 goto out;
3051 r = -ENXIO;
3052 if (!irqchip_in_kernel(kvm))
3053 goto out;
3054 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
3055 if (r)
3056 goto out;
3057 r = 0;
3058 break;
3059 }
Avi Kivityf17abe92007-02-21 19:28:04 +02003060 default:
3061 ;
3062 }
3063out:
3064 return r;
3065}
3066
3067static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
3068 unsigned long address,
3069 int *type)
3070{
3071 struct kvm *kvm = vma->vm_file->private_data;
3072 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02003073 struct page *page;
3074
Avi Kivityf17abe92007-02-21 19:28:04 +02003075 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity954bbbc22007-03-30 14:02:32 +03003076 page = gfn_to_page(kvm, pgoff);
Izik Eiduscea7bb22007-10-17 19:17:48 +02003077 if (is_error_page(page))
Avi Kivityf17abe92007-02-21 19:28:04 +02003078 return NOPAGE_SIGBUS;
3079 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03003080 if (type != NULL)
3081 *type = VM_FAULT_MINOR;
3082
Avi Kivityf17abe92007-02-21 19:28:04 +02003083 return page;
3084}
3085
3086static struct vm_operations_struct kvm_vm_vm_ops = {
3087 .nopage = kvm_vm_nopage,
3088};
3089
3090static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
3091{
3092 vma->vm_ops = &kvm_vm_vm_ops;
3093 return 0;
3094}
3095
3096static struct file_operations kvm_vm_fops = {
3097 .release = kvm_vm_release,
3098 .unlocked_ioctl = kvm_vm_ioctl,
3099 .compat_ioctl = kvm_vm_ioctl,
3100 .mmap = kvm_vm_mmap,
3101};
3102
3103static int kvm_dev_ioctl_create_vm(void)
3104{
3105 int fd, r;
3106 struct inode *inode;
3107 struct file *file;
3108 struct kvm *kvm;
3109
Avi Kivityf17abe92007-02-21 19:28:04 +02003110 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04003111 if (IS_ERR(kvm))
3112 return PTR_ERR(kvm);
3113 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
3114 if (r) {
3115 kvm_destroy_vm(kvm);
3116 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02003117 }
3118
Avi Kivitybccf2152007-02-21 18:04:26 +02003119 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02003120
Avi Kivityf17abe92007-02-21 19:28:04 +02003121 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02003122}
3123
3124static long kvm_dev_ioctl(struct file *filp,
3125 unsigned int ioctl, unsigned long arg)
3126{
3127 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02003128 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02003129
3130 switch (ioctl) {
3131 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02003132 r = -EINVAL;
3133 if (arg)
3134 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02003135 r = KVM_API_VERSION;
3136 break;
3137 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02003138 r = -EINVAL;
3139 if (arg)
3140 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02003141 r = kvm_dev_ioctl_create_vm();
3142 break;
Eddie Dong85f455f2007-07-06 12:20:49 +03003143 case KVM_CHECK_EXTENSION: {
3144 int ext = (long)argp;
3145
3146 switch (ext) {
3147 case KVM_CAP_IRQCHIP:
Eddie Dongb6958ce2007-07-18 12:15:21 +03003148 case KVM_CAP_HLT:
Izik Eidus82ce2c92007-10-02 18:52:55 +02003149 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Izik Eidus6fc138d2007-10-09 19:20:39 +02003150 case KVM_CAP_USER_MEMORY:
Eddie Dong85f455f2007-07-06 12:20:49 +03003151 r = 1;
3152 break;
3153 default:
3154 r = 0;
3155 break;
3156 }
Avi Kivity5d308f42007-03-01 17:56:20 +02003157 break;
Eddie Dong85f455f2007-07-06 12:20:49 +03003158 }
Avi Kivity07c45a32007-03-07 13:05:38 +02003159 case KVM_GET_VCPU_MMAP_SIZE:
3160 r = -EINVAL;
3161 if (arg)
3162 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02003163 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02003164 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003165 default:
Carsten Otte043405e2007-10-10 17:16:19 +02003166 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003167 }
3168out:
3169 return r;
3170}
3171
Avi Kivity6aa8b732006-12-10 02:21:36 -08003172static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003173 .unlocked_ioctl = kvm_dev_ioctl,
3174 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003175};
3176
3177static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02003178 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003179 "kvm",
3180 &kvm_chardev_ops,
3181};
3182
Avi Kivity774c47f2007-02-12 00:54:47 -08003183/*
3184 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
3185 * cached on it.
3186 */
3187static void decache_vcpus_on_cpu(int cpu)
3188{
3189 struct kvm *vm;
3190 struct kvm_vcpu *vcpu;
3191 int i;
3192
3193 spin_lock(&kvm_lock);
Shaohua Li11ec2802007-07-23 14:51:37 +08003194 list_for_each_entry(vm, &vm_list, vm_list)
Avi Kivity774c47f2007-02-12 00:54:47 -08003195 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003196 vcpu = vm->vcpus[i];
3197 if (!vcpu)
3198 continue;
Avi Kivity774c47f2007-02-12 00:54:47 -08003199 /*
3200 * If the vcpu is locked, then it is running on some
3201 * other cpu and therefore it is not cached on the
3202 * cpu in question.
3203 *
3204 * If it's not locked, check the last cpu it executed
3205 * on.
3206 */
3207 if (mutex_trylock(&vcpu->mutex)) {
3208 if (vcpu->cpu == cpu) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003209 kvm_x86_ops->vcpu_decache(vcpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08003210 vcpu->cpu = -1;
3211 }
3212 mutex_unlock(&vcpu->mutex);
3213 }
3214 }
3215 spin_unlock(&kvm_lock);
3216}
3217
Avi Kivity1b6c0162007-05-24 13:03:52 +03003218static void hardware_enable(void *junk)
3219{
3220 int cpu = raw_smp_processor_id();
3221
3222 if (cpu_isset(cpu, cpus_hardware_enabled))
3223 return;
3224 cpu_set(cpu, cpus_hardware_enabled);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003225 kvm_x86_ops->hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003226}
3227
3228static void hardware_disable(void *junk)
3229{
3230 int cpu = raw_smp_processor_id();
3231
3232 if (!cpu_isset(cpu, cpus_hardware_enabled))
3233 return;
3234 cpu_clear(cpu, cpus_hardware_enabled);
3235 decache_vcpus_on_cpu(cpu);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003236 kvm_x86_ops->hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003237}
3238
Avi Kivity774c47f2007-02-12 00:54:47 -08003239static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
3240 void *v)
3241{
3242 int cpu = (long)v;
3243
3244 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03003245 case CPU_DYING:
3246 case CPU_DYING_FROZEN:
Avi Kivity6ec8a852007-08-19 15:57:26 +03003247 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3248 cpu);
3249 hardware_disable(NULL);
3250 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08003251 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003252 case CPU_UP_CANCELED_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003253 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3254 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003255 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003256 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02003257 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003258 case CPU_ONLINE_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003259 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
3260 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003261 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003262 break;
3263 }
3264 return NOTIFY_OK;
3265}
3266
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003267static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04003268 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003269{
3270 if (val == SYS_RESTART) {
3271 /*
3272 * Some (well, at least mine) BIOSes hang on reboot if
3273 * in vmx root mode.
3274 */
3275 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
3276 on_each_cpu(hardware_disable, NULL, 0, 1);
3277 }
3278 return NOTIFY_OK;
3279}
3280
3281static struct notifier_block kvm_reboot_notifier = {
3282 .notifier_call = kvm_reboot,
3283 .priority = 0,
3284};
3285
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04003286void kvm_io_bus_init(struct kvm_io_bus *bus)
3287{
3288 memset(bus, 0, sizeof(*bus));
3289}
3290
3291void kvm_io_bus_destroy(struct kvm_io_bus *bus)
3292{
3293 int i;
3294
3295 for (i = 0; i < bus->dev_count; i++) {
3296 struct kvm_io_device *pos = bus->devs[i];
3297
3298 kvm_iodevice_destructor(pos);
3299 }
3300}
3301
3302struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
3303{
3304 int i;
3305
3306 for (i = 0; i < bus->dev_count; i++) {
3307 struct kvm_io_device *pos = bus->devs[i];
3308
3309 if (pos->in_range(pos, addr))
3310 return pos;
3311 }
3312
3313 return NULL;
3314}
3315
3316void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
3317{
3318 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
3319
3320 bus->devs[bus->dev_count++] = dev;
3321}
3322
Avi Kivity774c47f2007-02-12 00:54:47 -08003323static struct notifier_block kvm_cpu_notifier = {
3324 .notifier_call = kvm_cpu_hotplug,
3325 .priority = 20, /* must be > scheduler priority */
3326};
3327
Avi Kivity1165f5f2007-04-19 17:27:43 +03003328static u64 stat_get(void *_offset)
3329{
3330 unsigned offset = (long)_offset;
3331 u64 total = 0;
3332 struct kvm *kvm;
3333 struct kvm_vcpu *vcpu;
3334 int i;
3335
3336 spin_lock(&kvm_lock);
3337 list_for_each_entry(kvm, &vm_list, vm_list)
3338 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003339 vcpu = kvm->vcpus[i];
3340 if (vcpu)
3341 total += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03003342 }
3343 spin_unlock(&kvm_lock);
3344 return total;
3345}
3346
Rusty Russell3dea7ca2007-08-01 10:12:22 +10003347DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n");
Avi Kivity1165f5f2007-04-19 17:27:43 +03003348
Avi Kivity6aa8b732006-12-10 02:21:36 -08003349static __init void kvm_init_debug(void)
3350{
3351 struct kvm_stats_debugfs_item *p;
3352
Al Viro8b6d44c2007-02-09 16:38:40 +00003353 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003354 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03003355 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
3356 (void *)(long)p->offset,
3357 &stat_fops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003358}
3359
3360static void kvm_exit_debug(void)
3361{
3362 struct kvm_stats_debugfs_item *p;
3363
3364 for (p = debugfs_entries; p->name; ++p)
3365 debugfs_remove(p->dentry);
3366 debugfs_remove(debugfs_dir);
3367}
3368
Avi Kivity59ae6c62007-02-12 00:54:48 -08003369static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3370{
Avi Kivity4267c412007-05-24 13:09:41 +03003371 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003372 return 0;
3373}
3374
3375static int kvm_resume(struct sys_device *dev)
3376{
Avi Kivity4267c412007-05-24 13:09:41 +03003377 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003378 return 0;
3379}
3380
3381static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01003382 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08003383 .suspend = kvm_suspend,
3384 .resume = kvm_resume,
3385};
3386
3387static struct sys_device kvm_sysdev = {
3388 .id = 0,
3389 .cls = &kvm_sysdev_class,
3390};
3391
Izik Eiduscea7bb22007-10-17 19:17:48 +02003392struct page *bad_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003393
Avi Kivity15ad7142007-07-11 18:17:21 +03003394static inline
3395struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
3396{
3397 return container_of(pn, struct kvm_vcpu, preempt_notifier);
3398}
3399
3400static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
3401{
3402 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3403
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003404 kvm_x86_ops->vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003405}
3406
3407static void kvm_sched_out(struct preempt_notifier *pn,
3408 struct task_struct *next)
3409{
3410 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3411
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003412 kvm_x86_ops->vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003413}
3414
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003415int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10003416 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003417{
3418 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003419 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003420
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003421 if (kvm_x86_ops) {
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08003422 printk(KERN_ERR "kvm: already loaded the other module\n");
3423 return -EEXIST;
3424 }
3425
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003426 if (!ops->cpu_has_kvm_support()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003427 printk(KERN_ERR "kvm: no hardware support\n");
3428 return -EOPNOTSUPP;
3429 }
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003430 if (ops->disabled_by_bios()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003431 printk(KERN_ERR "kvm: disabled by bios\n");
3432 return -EOPNOTSUPP;
3433 }
3434
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003435 kvm_x86_ops = ops;
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003436
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003437 r = kvm_x86_ops->hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003438 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02003439 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003440
Yang, Sheng002c7f72007-07-31 14:23:01 +03003441 for_each_online_cpu(cpu) {
3442 smp_call_function_single(cpu,
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003443 kvm_x86_ops->check_processor_compatibility,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003444 &r, 0, 1);
3445 if (r < 0)
3446 goto out_free_0;
3447 }
3448
Avi Kivity1b6c0162007-05-24 13:03:52 +03003449 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003450 r = register_cpu_notifier(&kvm_cpu_notifier);
3451 if (r)
3452 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003453 register_reboot_notifier(&kvm_reboot_notifier);
3454
Avi Kivity59ae6c62007-02-12 00:54:48 -08003455 r = sysdev_class_register(&kvm_sysdev_class);
3456 if (r)
3457 goto out_free_2;
3458
3459 r = sysdev_register(&kvm_sysdev);
3460 if (r)
3461 goto out_free_3;
3462
Rusty Russellc16f8622007-07-30 21:12:19 +10003463 /* A kmem cache lets us meet the alignment requirements of fx_save. */
3464 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
3465 __alignof__(struct kvm_vcpu), 0, 0);
3466 if (!kvm_vcpu_cache) {
3467 r = -ENOMEM;
3468 goto out_free_4;
3469 }
3470
Avi Kivity6aa8b732006-12-10 02:21:36 -08003471 kvm_chardev_ops.owner = module;
3472
3473 r = misc_register(&kvm_dev);
3474 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04003475 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08003476 goto out_free;
3477 }
3478
Avi Kivity15ad7142007-07-11 18:17:21 +03003479 kvm_preempt_ops.sched_in = kvm_sched_in;
3480 kvm_preempt_ops.sched_out = kvm_sched_out;
3481
Avi Kivityc7addb92007-09-16 18:58:32 +02003482 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
3483
3484 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003485
3486out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10003487 kmem_cache_destroy(kvm_vcpu_cache);
3488out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08003489 sysdev_unregister(&kvm_sysdev);
3490out_free_3:
3491 sysdev_class_unregister(&kvm_sysdev_class);
3492out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003493 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08003494 unregister_cpu_notifier(&kvm_cpu_notifier);
3495out_free_1:
Avi Kivity1b6c0162007-05-24 13:03:52 +03003496 on_each_cpu(hardware_disable, NULL, 0, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03003497out_free_0:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003498 kvm_x86_ops->hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02003499out:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003500 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003501 return r;
3502}
Mike Dayd77c26f2007-10-08 09:02:08 -04003503EXPORT_SYMBOL_GPL(kvm_init_x86);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003504
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003505void kvm_exit_x86(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003506{
3507 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10003508 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003509 sysdev_unregister(&kvm_sysdev);
3510 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003511 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003512 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003513 on_each_cpu(hardware_disable, NULL, 0, 1);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003514 kvm_x86_ops->hardware_unsetup();
3515 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003516}
Mike Dayd77c26f2007-10-08 09:02:08 -04003517EXPORT_SYMBOL_GPL(kvm_exit_x86);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003518
3519static __init int kvm_init(void)
3520{
Avi Kivity37e29d92007-02-20 14:07:37 +02003521 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003522
Avi Kivityb5a33a72007-04-15 16:31:09 +03003523 r = kvm_mmu_module_init();
3524 if (r)
3525 goto out4;
3526
Avi Kivity6aa8b732006-12-10 02:21:36 -08003527 kvm_init_debug();
3528
Carsten Otte043405e2007-10-10 17:16:19 +02003529 kvm_arch_init();
Michael Riepebf591b22006-12-22 01:05:36 -08003530
Izik Eiduscea7bb22007-10-17 19:17:48 +02003531 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
Mike Dayd77c26f2007-10-08 09:02:08 -04003532
3533 if (bad_page == NULL) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003534 r = -ENOMEM;
3535 goto out;
3536 }
3537
Avi Kivity58e690e2007-02-26 16:29:43 +02003538 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003539
3540out:
3541 kvm_exit_debug();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003542 kvm_mmu_module_exit();
3543out4:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003544 return r;
3545}
3546
3547static __exit void kvm_exit(void)
3548{
3549 kvm_exit_debug();
Izik Eiduscea7bb22007-10-17 19:17:48 +02003550 __free_page(bad_page);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003551 kvm_mmu_module_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003552}
3553
3554module_init(kvm_init)
3555module_exit(kvm_exit)