blob: a6e3165043d174d3d91b5c5cd135ed666b212cdf [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>
Anthony Liguori8d4e1282007-10-18 09:59:34 -050045#include <linux/mman.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080046
Avi Kivitye4956062007-06-28 14:15:57 -040047#include <asm/processor.h>
48#include <asm/msr.h>
49#include <asm/io.h>
50#include <asm/uaccess.h>
51#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080052
53MODULE_AUTHOR("Qumranet");
54MODULE_LICENSE("GPL");
55
Avi Kivity133de902007-02-12 00:54:44 -080056static DEFINE_SPINLOCK(kvm_lock);
57static LIST_HEAD(vm_list);
58
Avi Kivity1b6c0162007-05-24 13:03:52 +030059static cpumask_t cpus_hardware_enabled;
60
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +030061struct kvm_x86_ops *kvm_x86_ops;
Rusty Russellc16f8622007-07-30 21:12:19 +100062struct kmem_cache *kvm_vcpu_cache;
63EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030064
Avi Kivity15ad7142007-07-11 18:17:21 +030065static __read_mostly struct preempt_ops kvm_preempt_ops;
66
Avi Kivity1165f5f2007-04-19 17:27:43 +030067#define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
Avi Kivity6aa8b732006-12-10 02:21:36 -080068
69static struct kvm_stats_debugfs_item {
70 const char *name;
Avi Kivity1165f5f2007-04-19 17:27:43 +030071 int offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -080072 struct dentry *dentry;
73} debugfs_entries[] = {
Avi Kivity1165f5f2007-04-19 17:27:43 +030074 { "pf_fixed", STAT_OFFSET(pf_fixed) },
75 { "pf_guest", STAT_OFFSET(pf_guest) },
76 { "tlb_flush", STAT_OFFSET(tlb_flush) },
77 { "invlpg", STAT_OFFSET(invlpg) },
78 { "exits", STAT_OFFSET(exits) },
79 { "io_exits", STAT_OFFSET(io_exits) },
80 { "mmio_exits", STAT_OFFSET(mmio_exits) },
81 { "signal_exits", STAT_OFFSET(signal_exits) },
82 { "irq_window", STAT_OFFSET(irq_window_exits) },
83 { "halt_exits", STAT_OFFSET(halt_exits) },
Eddie Dongb6958ce2007-07-18 12:15:21 +030084 { "halt_wakeup", STAT_OFFSET(halt_wakeup) },
Avi Kivity1165f5f2007-04-19 17:27:43 +030085 { "request_irq", STAT_OFFSET(request_irq_exits) },
86 { "irq_exits", STAT_OFFSET(irq_exits) },
Avi Kivitye6adf282007-04-30 16:07:54 +030087 { "light_exits", STAT_OFFSET(light_exits) },
Eddie Dong2cc51562007-05-21 07:28:09 +030088 { "efer_reload", STAT_OFFSET(efer_reload) },
Avi Kivity1165f5f2007-04-19 17:27:43 +030089 { NULL }
Avi Kivity6aa8b732006-12-10 02:21:36 -080090};
91
92static struct dentry *debugfs_dir;
93
Rusty Russell707d92f2007-07-17 23:19:08 +100094#define CR0_RESERVED_BITS \
95 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
96 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
97 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
Rusty Russell66aee912007-07-17 23:34:16 +100098#define CR4_RESERVED_BITS \
99 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
100 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
101 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
102 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
103
Rusty Russell7075bc82007-07-17 23:37:17 +1000104#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800105#define EFER_RESERVED_BITS 0xfffffffffffff2fe
106
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800107#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400108/* LDT or TSS descriptor in the GDT. 16 bytes. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800109struct segment_descriptor_64 {
110 struct segment_descriptor s;
111 u32 base_higher;
112 u32 pad_zero;
113};
114
115#endif
116
Avi Kivitybccf2152007-02-21 18:04:26 +0200117static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
118 unsigned long arg);
119
Avi Kivity6aa8b732006-12-10 02:21:36 -0800120unsigned long segment_base(u16 selector)
121{
122 struct descriptor_table gdt;
123 struct segment_descriptor *d;
124 unsigned long table_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800125 unsigned long v;
126
127 if (selector == 0)
128 return 0;
129
Mike Dayd77c26f2007-10-08 09:02:08 -0400130 asm("sgdt %0" : "=m"(gdt));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131 table_base = gdt.base;
132
133 if (selector & 4) { /* from ldt */
134 u16 ldt_selector;
135
Mike Dayd77c26f2007-10-08 09:02:08 -0400136 asm("sldt %0" : "=g"(ldt_selector));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800137 table_base = segment_base(ldt_selector);
138 }
139 d = (struct segment_descriptor *)(table_base + (selector & ~7));
Mike Dayd77c26f2007-10-08 09:02:08 -0400140 v = d->base_low | ((unsigned long)d->base_mid << 16) |
141 ((unsigned long)d->base_high << 24);
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800142#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400143 if (d->system == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
144 v |= ((unsigned long) \
145 ((struct segment_descriptor_64 *)d)->base_higher) << 32;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800146#endif
147 return v;
148}
149EXPORT_SYMBOL_GPL(segment_base);
150
James Morris5aacf0c2006-12-22 01:04:55 -0800151static inline int valid_vcpu(int n)
152{
153 return likely(n >= 0 && n < KVM_MAX_VCPUS);
154}
155
Avi Kivity7702fd12007-06-14 16:27:40 +0300156void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
157{
158 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
159 return;
160
161 vcpu->guest_fpu_loaded = 1;
Rusty Russellb114b082007-07-30 21:13:43 +1000162 fx_save(&vcpu->host_fx_image);
163 fx_restore(&vcpu->guest_fx_image);
Avi Kivity7702fd12007-06-14 16:27:40 +0300164}
165EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
166
167void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
168{
169 if (!vcpu->guest_fpu_loaded)
170 return;
171
172 vcpu->guest_fpu_loaded = 0;
Rusty Russellb114b082007-07-30 21:13:43 +1000173 fx_save(&vcpu->guest_fx_image);
174 fx_restore(&vcpu->host_fx_image);
Avi Kivity7702fd12007-06-14 16:27:40 +0300175}
176EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
177
Avi Kivity6aa8b732006-12-10 02:21:36 -0800178/*
179 * Switches to specified vcpu, until a matching vcpu_put()
180 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200181void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800182{
Avi Kivity15ad7142007-07-11 18:17:21 +0300183 int cpu;
184
Avi Kivitybccf2152007-02-21 18:04:26 +0200185 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300186 cpu = get_cpu();
187 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200188 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300189 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200190}
191
Carsten Otte313a3dc2007-10-11 19:16:52 +0200192void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800193{
Avi Kivity15ad7142007-07-11 18:17:21 +0300194 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200195 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300196 preempt_notifier_unregister(&vcpu->preempt_notifier);
197 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800198 mutex_unlock(&vcpu->mutex);
199}
200
Avi Kivityd9e368d2007-06-07 19:18:30 +0300201static void ack_flush(void *_completed)
202{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300203}
204
205void kvm_flush_remote_tlbs(struct kvm *kvm)
206{
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200207 int i, cpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300208 cpumask_t cpus;
209 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300210
Avi Kivityd9e368d2007-06-07 19:18:30 +0300211 cpus_clear(cpus);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000212 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
213 vcpu = kvm->vcpus[i];
214 if (!vcpu)
215 continue;
Avi Kivity3176bc32007-10-16 17:22:08 +0200216 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300217 continue;
218 cpu = vcpu->cpu;
219 if (cpu != -1 && cpu != raw_smp_processor_id())
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200220 cpu_set(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300221 }
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200222 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300223}
224
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000225int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
226{
227 struct page *page;
228 int r;
229
230 mutex_init(&vcpu->mutex);
231 vcpu->cpu = -1;
232 vcpu->mmu.root_hpa = INVALID_PAGE;
233 vcpu->kvm = kvm;
234 vcpu->vcpu_id = id;
He, Qingc5ec1532007-09-03 17:07:41 +0300235 if (!irqchip_in_kernel(kvm) || id == 0)
236 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
237 else
238 vcpu->mp_state = VCPU_MP_STATE_UNINITIALIZED;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300239 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000240
241 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
242 if (!page) {
243 r = -ENOMEM;
244 goto fail;
245 }
246 vcpu->run = page_address(page);
247
248 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
249 if (!page) {
250 r = -ENOMEM;
251 goto fail_free_run;
252 }
253 vcpu->pio_data = page_address(page);
254
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000255 r = kvm_mmu_create(vcpu);
256 if (r < 0)
257 goto fail_free_pio_data;
258
Rusty Russell76fafa52007-10-08 10:50:48 +1000259 if (irqchip_in_kernel(kvm)) {
260 r = kvm_create_lapic(vcpu);
261 if (r < 0)
262 goto fail_mmu_destroy;
263 }
264
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000265 return 0;
266
Rusty Russell76fafa52007-10-08 10:50:48 +1000267fail_mmu_destroy:
268 kvm_mmu_destroy(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000269fail_free_pio_data:
270 free_page((unsigned long)vcpu->pio_data);
271fail_free_run:
272 free_page((unsigned long)vcpu->run);
273fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000274 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000275}
276EXPORT_SYMBOL_GPL(kvm_vcpu_init);
277
278void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
279{
Rusty Russelld5894442007-10-08 10:48:30 +1000280 kvm_free_lapic(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000281 kvm_mmu_destroy(vcpu);
282 free_page((unsigned long)vcpu->pio_data);
283 free_page((unsigned long)vcpu->run);
284}
285EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
286
Avi Kivityf17abe92007-02-21 19:28:04 +0200287static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800288{
289 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800290
291 if (!kvm)
Avi Kivityf17abe92007-02-21 19:28:04 +0200292 return ERR_PTR(-ENOMEM);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800293
Eddie Dong74906342007-06-19 18:05:03 +0300294 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800295 mutex_init(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800296 INIT_LIST_HEAD(&kvm->active_mmu_pages);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400297 kvm_io_bus_init(&kvm->mmio_bus);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000298 spin_lock(&kvm_lock);
299 list_add(&kvm->vm_list, &vm_list);
300 spin_unlock(&kvm_lock);
Avi Kivityf17abe92007-02-21 19:28:04 +0200301 return kvm;
302}
303
Avi Kivity6aa8b732006-12-10 02:21:36 -0800304/*
305 * Free any memory in @free but not in @dont.
306 */
307static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
308 struct kvm_memory_slot *dont)
309{
Izik Eidus290fc382007-09-27 14:11:22 +0200310 if (!dont || free->rmap != dont->rmap)
311 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800312
313 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
314 vfree(free->dirty_bitmap);
315
Avi Kivity6aa8b732006-12-10 02:21:36 -0800316 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000317 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500318 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800319}
320
321static void kvm_free_physmem(struct kvm *kvm)
322{
323 int i;
324
325 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000326 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800327}
328
Avi Kivity039576c2007-03-20 12:46:50 +0200329static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
330{
331 int i;
332
Rusty Russell3077c4512007-07-30 16:41:57 +1000333 for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i)
Avi Kivity039576c2007-03-20 12:46:50 +0200334 if (vcpu->pio.guest_pages[i]) {
Izik Eidus8a7ae052007-10-18 11:09:33 +0200335 kvm_release_page(vcpu->pio.guest_pages[i]);
Avi Kivity039576c2007-03-20 12:46:50 +0200336 vcpu->pio.guest_pages[i] = NULL;
337 }
338}
339
Avi Kivity7b53aa52007-06-05 12:17:03 +0300340static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
341{
Avi Kivity7b53aa52007-06-05 12:17:03 +0300342 vcpu_load(vcpu);
343 kvm_mmu_unload(vcpu);
344 vcpu_put(vcpu);
345}
346
Avi Kivity6aa8b732006-12-10 02:21:36 -0800347static void kvm_free_vcpus(struct kvm *kvm)
348{
349 unsigned int i;
350
Avi Kivity7b53aa52007-06-05 12:17:03 +0300351 /*
352 * Unpin any mmu pages first.
353 */
354 for (i = 0; i < KVM_MAX_VCPUS; ++i)
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000355 if (kvm->vcpus[i])
356 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
357 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
358 if (kvm->vcpus[i]) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300359 kvm_x86_ops->vcpu_free(kvm->vcpus[i]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000360 kvm->vcpus[i] = NULL;
361 }
362 }
363
Avi Kivity6aa8b732006-12-10 02:21:36 -0800364}
365
Avi Kivityf17abe92007-02-21 19:28:04 +0200366static void kvm_destroy_vm(struct kvm *kvm)
367{
Avi Kivity133de902007-02-12 00:54:44 -0800368 spin_lock(&kvm_lock);
369 list_del(&kvm->vm_list);
370 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300371 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400372 kvm_io_bus_destroy(&kvm->mmio_bus);
Eddie Dong85f455f2007-07-06 12:20:49 +0300373 kfree(kvm->vpic);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300374 kfree(kvm->vioapic);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800375 kvm_free_vcpus(kvm);
376 kvm_free_physmem(kvm);
377 kfree(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200378}
379
380static int kvm_vm_release(struct inode *inode, struct file *filp)
381{
382 struct kvm *kvm = filp->private_data;
383
384 kvm_destroy_vm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800385 return 0;
386}
387
388static void inject_gp(struct kvm_vcpu *vcpu)
389{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300390 kvm_x86_ops->inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800391}
392
Avi Kivity1342d352007-01-05 16:36:39 -0800393/*
394 * Load the pae pdptrs. Return true is they are all valid.
395 */
396static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800397{
398 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
Avi Kivity1342d352007-01-05 16:36:39 -0800399 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800400 int i;
Avi Kivity1342d352007-01-05 16:36:39 -0800401 int ret;
Rusty Russellc820c2a2007-07-25 13:29:51 +1000402 u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800403
Shaohua Li11ec2802007-07-23 14:51:37 +0800404 mutex_lock(&vcpu->kvm->lock);
Izik Eidus195aefd2007-10-01 22:14:18 +0200405 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
406 offset * sizeof(u64), sizeof(pdpte));
407 if (ret < 0) {
Rusty Russellc820c2a2007-07-25 13:29:51 +1000408 ret = 0;
409 goto out;
410 }
Rusty Russellc820c2a2007-07-25 13:29:51 +1000411 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
412 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
Avi Kivity1342d352007-01-05 16:36:39 -0800413 ret = 0;
414 goto out;
415 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800416 }
Rusty Russellc820c2a2007-07-25 13:29:51 +1000417 ret = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800418
Rusty Russellc820c2a2007-07-25 13:29:51 +1000419 memcpy(vcpu->pdptrs, pdpte, sizeof(vcpu->pdptrs));
Avi Kivity1342d352007-01-05 16:36:39 -0800420out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800421 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800422
Avi Kivity1342d352007-01-05 16:36:39 -0800423 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424}
425
426void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
427{
Rusty Russell707d92f2007-07-17 23:19:08 +1000428 if (cr0 & CR0_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800429 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
430 cr0, vcpu->cr0);
431 inject_gp(vcpu);
432 return;
433 }
434
Rusty Russell707d92f2007-07-17 23:19:08 +1000435 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800436 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
437 inject_gp(vcpu);
438 return;
439 }
440
Rusty Russell707d92f2007-07-17 23:19:08 +1000441 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800442 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
443 "and a clear PE flag\n");
444 inject_gp(vcpu);
445 return;
446 }
447
Rusty Russell707d92f2007-07-17 23:19:08 +1000448 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800449#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800450 if ((vcpu->shadow_efer & EFER_LME)) {
451 int cs_db, cs_l;
452
453 if (!is_pae(vcpu)) {
454 printk(KERN_DEBUG "set_cr0: #GP, start paging "
455 "in long mode while PAE is disabled\n");
456 inject_gp(vcpu);
457 return;
458 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300459 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800460 if (cs_l) {
461 printk(KERN_DEBUG "set_cr0: #GP, start paging "
462 "in long mode while CS.L == 1\n");
463 inject_gp(vcpu);
464 return;
465
466 }
467 } else
468#endif
Avi Kivity1342d352007-01-05 16:36:39 -0800469 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800470 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
471 "reserved bits\n");
472 inject_gp(vcpu);
473 return;
474 }
475
476 }
477
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300478 kvm_x86_ops->set_cr0(vcpu, cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800479 vcpu->cr0 = cr0;
480
Shaohua Li11ec2802007-07-23 14:51:37 +0800481 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800482 kvm_mmu_reset_context(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800483 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800484 return;
485}
486EXPORT_SYMBOL_GPL(set_cr0);
487
488void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
489{
490 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
491}
492EXPORT_SYMBOL_GPL(lmsw);
493
494void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
495{
Rusty Russell66aee912007-07-17 23:34:16 +1000496 if (cr4 & CR4_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800497 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
498 inject_gp(vcpu);
499 return;
500 }
501
Avi Kivitya9058ec2006-12-29 16:49:37 -0800502 if (is_long_mode(vcpu)) {
Rusty Russell66aee912007-07-17 23:34:16 +1000503 if (!(cr4 & X86_CR4_PAE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800504 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
505 "in long mode\n");
506 inject_gp(vcpu);
507 return;
508 }
Rusty Russell66aee912007-07-17 23:34:16 +1000509 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
Avi Kivity1342d352007-01-05 16:36:39 -0800510 && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800511 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
512 inject_gp(vcpu);
Rusty Russell310bc762007-07-23 17:11:02 +1000513 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800514 }
515
Rusty Russell66aee912007-07-17 23:34:16 +1000516 if (cr4 & X86_CR4_VMXE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800517 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
518 inject_gp(vcpu);
519 return;
520 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300521 kvm_x86_ops->set_cr4(vcpu, cr4);
Rusty Russell81f50e32007-09-06 01:20:38 +1000522 vcpu->cr4 = cr4;
Shaohua Li11ec2802007-07-23 14:51:37 +0800523 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800524 kvm_mmu_reset_context(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800525 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800526}
527EXPORT_SYMBOL_GPL(set_cr4);
528
529void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
530{
Avi Kivitya9058ec2006-12-29 16:49:37 -0800531 if (is_long_mode(vcpu)) {
Rusty Russellf802a302007-07-17 23:32:55 +1000532 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800533 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
534 inject_gp(vcpu);
535 return;
536 }
537 } else {
Rusty Russellf802a302007-07-17 23:32:55 +1000538 if (is_pae(vcpu)) {
539 if (cr3 & CR3_PAE_RESERVED_BITS) {
540 printk(KERN_DEBUG
541 "set_cr3: #GP, reserved bits\n");
542 inject_gp(vcpu);
543 return;
544 }
545 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
546 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
547 "reserved bits\n");
548 inject_gp(vcpu);
549 return;
550 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800551 }
Ryan Harper21764862007-09-18 14:05:16 -0500552 /*
553 * We don't check reserved bits in nonpae mode, because
554 * this isn't enforced, and VMware depends on this.
555 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800556 }
557
Shaohua Li11ec2802007-07-23 14:51:37 +0800558 mutex_lock(&vcpu->kvm->lock);
Ingo Molnard21225e2007-01-05 16:36:59 -0800559 /*
560 * Does the new cr3 value map to physical memory? (Note, we
561 * catch an invalid cr3 even in real-mode, because it would
562 * cause trouble later on when we turn on paging anyway.)
563 *
564 * A real CPU would silently accept an invalid cr3 and would
565 * attempt to use it - with largely undefined (and often hard
566 * to debug) behavior on the guest side.
567 */
568 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
569 inject_gp(vcpu);
Rusty Russellfb764412007-07-31 20:45:03 +1000570 else {
571 vcpu->cr3 = cr3;
Ingo Molnard21225e2007-01-05 16:36:59 -0800572 vcpu->mmu.new_cr3(vcpu);
Rusty Russellfb764412007-07-31 20:45:03 +1000573 }
Shaohua Li11ec2802007-07-23 14:51:37 +0800574 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800575}
576EXPORT_SYMBOL_GPL(set_cr3);
577
578void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
579{
Rusty Russell7075bc82007-07-17 23:37:17 +1000580 if (cr8 & CR8_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800581 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
582 inject_gp(vcpu);
583 return;
584 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300585 if (irqchip_in_kernel(vcpu->kvm))
586 kvm_lapic_set_tpr(vcpu, cr8);
587 else
588 vcpu->cr8 = cr8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800589}
590EXPORT_SYMBOL_GPL(set_cr8);
591
Eddie Dong7017fc32007-07-18 11:34:57 +0300592unsigned long get_cr8(struct kvm_vcpu *vcpu)
593{
Eddie Dong97222cc2007-09-12 10:58:04 +0300594 if (irqchip_in_kernel(vcpu->kvm))
595 return kvm_lapic_get_cr8(vcpu);
596 else
597 return vcpu->cr8;
Eddie Dong7017fc32007-07-18 11:34:57 +0300598}
599EXPORT_SYMBOL_GPL(get_cr8);
600
601u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
602{
Eddie Dong97222cc2007-09-12 10:58:04 +0300603 if (irqchip_in_kernel(vcpu->kvm))
604 return vcpu->apic_base;
605 else
606 return vcpu->apic_base;
Eddie Dong7017fc32007-07-18 11:34:57 +0300607}
608EXPORT_SYMBOL_GPL(kvm_get_apic_base);
609
610void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
611{
Eddie Dong97222cc2007-09-12 10:58:04 +0300612 /* TODO: reserve bits check */
613 if (irqchip_in_kernel(vcpu->kvm))
614 kvm_lapic_set_base(vcpu, data);
615 else
616 vcpu->apic_base = data;
Eddie Dong7017fc32007-07-18 11:34:57 +0300617}
618EXPORT_SYMBOL_GPL(kvm_set_apic_base);
619
Avi Kivity6aa8b732006-12-10 02:21:36 -0800620void fx_init(struct kvm_vcpu *vcpu)
621{
Rusty Russellb114b082007-07-30 21:13:43 +1000622 unsigned after_mxcsr_mask;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800623
Rusty Russell9bd01502007-07-30 16:29:56 +1000624 /* Initialize guest FPU by resetting ours and saving into guest's */
625 preempt_disable();
Rusty Russellb114b082007-07-30 21:13:43 +1000626 fx_save(&vcpu->host_fx_image);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800627 fpu_init();
Rusty Russellb114b082007-07-30 21:13:43 +1000628 fx_save(&vcpu->guest_fx_image);
629 fx_restore(&vcpu->host_fx_image);
Rusty Russell9bd01502007-07-30 16:29:56 +1000630 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800631
Amit Shah380102c2007-08-25 11:35:52 +0300632 vcpu->cr0 |= X86_CR0_ET;
Rusty Russellb114b082007-07-30 21:13:43 +1000633 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
634 vcpu->guest_fx_image.mxcsr = 0x1f80;
635 memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask,
636 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800637}
638EXPORT_SYMBOL_GPL(fx_init);
639
640/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800641 * Allocate some memory and give it an address in the guest physical address
642 * space.
643 *
644 * Discontiguous memory is allowed, mostly for framebuffers.
645 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200646static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
Izik Eidus6fc138d2007-10-09 19:20:39 +0200647 struct
648 kvm_userspace_memory_region *mem,
649 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800650{
651 int r;
652 gfn_t base_gfn;
653 unsigned long npages;
654 unsigned long i;
655 struct kvm_memory_slot *memslot;
656 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800657
658 r = -EINVAL;
659 /* General sanity checks */
660 if (mem->memory_size & (PAGE_SIZE - 1))
661 goto out;
662 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
663 goto out;
664 if (mem->slot >= KVM_MEMORY_SLOTS)
665 goto out;
666 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
667 goto out;
668
669 memslot = &kvm->memslots[mem->slot];
670 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
671 npages = mem->memory_size >> PAGE_SHIFT;
672
673 if (!npages)
674 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
675
Shaohua Li11ec2802007-07-23 14:51:37 +0800676 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800677
Avi Kivity6aa8b732006-12-10 02:21:36 -0800678 new = old = *memslot;
679
680 new.base_gfn = base_gfn;
681 new.npages = npages;
682 new.flags = mem->flags;
683
684 /* Disallow changing a memory slot's size. */
685 r = -EINVAL;
686 if (npages && old.npages && npages != old.npages)
687 goto out_unlock;
688
689 /* Check for overlaps */
690 r = -EEXIST;
691 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
692 struct kvm_memory_slot *s = &kvm->memslots[i];
693
694 if (s == memslot)
695 continue;
696 if (!((base_gfn + npages <= s->base_gfn) ||
697 (base_gfn >= s->base_gfn + s->npages)))
698 goto out_unlock;
699 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800700
Avi Kivity6aa8b732006-12-10 02:21:36 -0800701 /* Free page dirty bitmap if unneeded */
702 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000703 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800704
705 r = -ENOMEM;
706
707 /* Allocate if a slot is being created */
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500708 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400709 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200710
711 if (!new.rmap)
712 goto out_unlock;
713
Izik Eidus290fc382007-09-27 14:11:22 +0200714 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500715
716 if (user_alloc)
Izik Eidus8a7ae052007-10-18 11:09:33 +0200717 new.userspace_addr = mem->userspace_addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500718 else {
719 down_write(&current->mm->mmap_sem);
720 new.userspace_addr = do_mmap(NULL, 0,
721 npages * PAGE_SIZE,
722 PROT_READ | PROT_WRITE,
723 MAP_SHARED | MAP_ANONYMOUS,
724 0);
725 up_write(&current->mm->mmap_sem);
726
727 if (IS_ERR((void *)new.userspace_addr))
728 goto out_unlock;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800729 }
730 }
731
732 /* Allocate page dirty bitmap if needed */
733 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
734 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
735
736 new.dirty_bitmap = vmalloc(dirty_bytes);
737 if (!new.dirty_bitmap)
Laurent Vivier0d8d2bd2007-08-30 14:56:21 +0200738 goto out_unlock;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800739 memset(new.dirty_bitmap, 0, dirty_bytes);
740 }
741
Avi Kivity6aa8b732006-12-10 02:21:36 -0800742 if (mem->slot >= kvm->nmemslots)
743 kvm->nmemslots = mem->slot + 1;
744
Izik Eidus82ce2c92007-10-02 18:52:55 +0200745 if (!kvm->n_requested_mmu_pages) {
746 unsigned int n_pages;
747
748 if (npages) {
749 n_pages = npages * KVM_PERMILLE_MMU_PAGES / 1000;
750 kvm_mmu_change_mmu_pages(kvm, kvm->n_alloc_mmu_pages +
751 n_pages);
752 } else {
753 unsigned int nr_mmu_pages;
754
755 n_pages = old.npages * KVM_PERMILLE_MMU_PAGES / 1000;
756 nr_mmu_pages = kvm->n_alloc_mmu_pages - n_pages;
757 nr_mmu_pages = max(nr_mmu_pages,
758 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
759 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
760 }
761 }
762
Avi Kivity6aa8b732006-12-10 02:21:36 -0800763 *memslot = new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800764
Avi Kivity90cb0522007-07-17 13:04:56 +0300765 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
766 kvm_flush_remote_tlbs(kvm);
767
Shaohua Li11ec2802007-07-23 14:51:37 +0800768 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800769
Avi Kivity6aa8b732006-12-10 02:21:36 -0800770 kvm_free_physmem_slot(&old, &new);
771 return 0;
772
773out_unlock:
Shaohua Li11ec2802007-07-23 14:51:37 +0800774 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800775 kvm_free_physmem_slot(&new, &old);
776out:
777 return r;
778}
779
Izik Eidus82ce2c92007-10-02 18:52:55 +0200780static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
781 u32 kvm_nr_mmu_pages)
782{
783 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
784 return -EINVAL;
785
786 mutex_lock(&kvm->lock);
787
788 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
789 kvm->n_requested_mmu_pages = kvm_nr_mmu_pages;
790
791 mutex_unlock(&kvm->lock);
792 return 0;
793}
794
795static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
796{
797 return kvm->n_alloc_mmu_pages;
798}
799
Avi Kivity6aa8b732006-12-10 02:21:36 -0800800/*
801 * Get (and clear) the dirty memory log for a memory slot.
802 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200803static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
804 struct kvm_dirty_log *log)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800805{
806 struct kvm_memory_slot *memslot;
807 int r, i;
808 int n;
809 unsigned long any = 0;
810
Shaohua Li11ec2802007-07-23 14:51:37 +0800811 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800812
Avi Kivity6aa8b732006-12-10 02:21:36 -0800813 r = -EINVAL;
814 if (log->slot >= KVM_MEMORY_SLOTS)
815 goto out;
816
817 memslot = &kvm->memslots[log->slot];
818 r = -ENOENT;
819 if (!memslot->dirty_bitmap)
820 goto out;
821
Uri Lublincd1a4a92007-02-22 16:43:09 +0200822 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800823
Uri Lublincd1a4a92007-02-22 16:43:09 +0200824 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825 any = memslot->dirty_bitmap[i];
826
827 r = -EFAULT;
828 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
829 goto out;
830
Rusty Russell39214912007-07-31 19:57:47 +1000831 /* If nothing is dirty, don't bother messing with page tables. */
832 if (any) {
Rusty Russell39214912007-07-31 19:57:47 +1000833 kvm_mmu_slot_remove_write_access(kvm, log->slot);
834 kvm_flush_remote_tlbs(kvm);
835 memset(memslot->dirty_bitmap, 0, n);
Rusty Russell39214912007-07-31 19:57:47 +1000836 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800837
838 r = 0;
839
840out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800841 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800842 return r;
843}
844
Avi Kivitye8207542007-03-30 16:54:30 +0300845/*
846 * Set a new alias region. Aliases map a portion of physical memory into
847 * another portion. This is useful for memory windows, for example the PC
848 * VGA region.
849 */
850static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
851 struct kvm_memory_alias *alias)
852{
853 int r, n;
854 struct kvm_mem_alias *p;
855
856 r = -EINVAL;
857 /* General sanity checks */
858 if (alias->memory_size & (PAGE_SIZE - 1))
859 goto out;
860 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
861 goto out;
862 if (alias->slot >= KVM_ALIAS_SLOTS)
863 goto out;
864 if (alias->guest_phys_addr + alias->memory_size
865 < alias->guest_phys_addr)
866 goto out;
867 if (alias->target_phys_addr + alias->memory_size
868 < alias->target_phys_addr)
869 goto out;
870
Shaohua Li11ec2802007-07-23 14:51:37 +0800871 mutex_lock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300872
873 p = &kvm->aliases[alias->slot];
874 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
875 p->npages = alias->memory_size >> PAGE_SHIFT;
876 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
877
878 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
879 if (kvm->aliases[n - 1].npages)
880 break;
881 kvm->naliases = n;
882
Avi Kivity90cb0522007-07-17 13:04:56 +0300883 kvm_mmu_zap_all(kvm);
Avi Kivitye8207542007-03-30 16:54:30 +0300884
Shaohua Li11ec2802007-07-23 14:51:37 +0800885 mutex_unlock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300886
887 return 0;
888
889out:
890 return r;
891}
892
He, Qing6ceb9d72007-07-26 11:05:18 +0300893static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
894{
895 int r;
896
897 r = 0;
898 switch (chip->chip_id) {
899 case KVM_IRQCHIP_PIC_MASTER:
Mike Dayd77c26f2007-10-08 09:02:08 -0400900 memcpy(&chip->chip.pic,
He, Qing6ceb9d72007-07-26 11:05:18 +0300901 &pic_irqchip(kvm)->pics[0],
902 sizeof(struct kvm_pic_state));
903 break;
904 case KVM_IRQCHIP_PIC_SLAVE:
Mike Dayd77c26f2007-10-08 09:02:08 -0400905 memcpy(&chip->chip.pic,
He, Qing6ceb9d72007-07-26 11:05:18 +0300906 &pic_irqchip(kvm)->pics[1],
907 sizeof(struct kvm_pic_state));
908 break;
He, Qing6bf9e962007-08-05 10:49:16 +0300909 case KVM_IRQCHIP_IOAPIC:
Mike Dayd77c26f2007-10-08 09:02:08 -0400910 memcpy(&chip->chip.ioapic,
He, Qing6bf9e962007-08-05 10:49:16 +0300911 ioapic_irqchip(kvm),
912 sizeof(struct kvm_ioapic_state));
913 break;
He, Qing6ceb9d72007-07-26 11:05:18 +0300914 default:
915 r = -EINVAL;
916 break;
917 }
918 return r;
919}
920
921static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
922{
923 int r;
924
925 r = 0;
926 switch (chip->chip_id) {
927 case KVM_IRQCHIP_PIC_MASTER:
Mike Dayd77c26f2007-10-08 09:02:08 -0400928 memcpy(&pic_irqchip(kvm)->pics[0],
He, Qing6ceb9d72007-07-26 11:05:18 +0300929 &chip->chip.pic,
930 sizeof(struct kvm_pic_state));
931 break;
932 case KVM_IRQCHIP_PIC_SLAVE:
Mike Dayd77c26f2007-10-08 09:02:08 -0400933 memcpy(&pic_irqchip(kvm)->pics[1],
He, Qing6ceb9d72007-07-26 11:05:18 +0300934 &chip->chip.pic,
935 sizeof(struct kvm_pic_state));
936 break;
He, Qing6bf9e962007-08-05 10:49:16 +0300937 case KVM_IRQCHIP_IOAPIC:
Mike Dayd77c26f2007-10-08 09:02:08 -0400938 memcpy(ioapic_irqchip(kvm),
He, Qing6bf9e962007-08-05 10:49:16 +0300939 &chip->chip.ioapic,
940 sizeof(struct kvm_ioapic_state));
941 break;
He, Qing6ceb9d72007-07-26 11:05:18 +0300942 default:
943 r = -EINVAL;
944 break;
945 }
946 kvm_pic_update_irq(pic_irqchip(kvm));
947 return r;
948}
949
Izik Eiduscea7bb22007-10-17 19:17:48 +0200950int is_error_page(struct page *page)
951{
952 return page == bad_page;
953}
954EXPORT_SYMBOL_GPL(is_error_page);
955
Izik Eidus290fc382007-09-27 14:11:22 +0200956gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
Avi Kivitye8207542007-03-30 16:54:30 +0300957{
958 int i;
959 struct kvm_mem_alias *alias;
960
961 for (i = 0; i < kvm->naliases; ++i) {
962 alias = &kvm->aliases[i];
963 if (gfn >= alias->base_gfn
964 && gfn < alias->base_gfn + alias->npages)
965 return alias->target_gfn + gfn - alias->base_gfn;
966 }
967 return gfn;
968}
969
970static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971{
972 int i;
973
974 for (i = 0; i < kvm->nmemslots; ++i) {
975 struct kvm_memory_slot *memslot = &kvm->memslots[i];
976
977 if (gfn >= memslot->base_gfn
978 && gfn < memslot->base_gfn + memslot->npages)
979 return memslot;
980 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000981 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982}
Avi Kivitye8207542007-03-30 16:54:30 +0300983
984struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
985{
986 gfn = unalias_gfn(kvm, gfn);
987 return __gfn_to_memslot(kvm, gfn);
988}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800989
Avi Kivity954bbbc2007-03-30 14:02:32 +0300990struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
991{
992 struct kvm_memory_slot *slot;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500993 struct page *page[1];
994 int npages;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300995
Avi Kivitye8207542007-03-30 16:54:30 +0300996 gfn = unalias_gfn(kvm, gfn);
997 slot = __gfn_to_memslot(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200998 if (!slot) {
999 get_page(bad_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +02001000 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001001 }
Izik Eidus8a7ae052007-10-18 11:09:33 +02001002
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001003 down_read(&current->mm->mmap_sem);
1004 npages = get_user_pages(current, current->mm,
1005 slot->userspace_addr
1006 + (gfn - slot->base_gfn) * PAGE_SIZE, 1,
1007 1, 1, page, NULL);
1008 up_read(&current->mm->mmap_sem);
1009 if (npages != 1) {
1010 get_page(bad_page);
1011 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001012 }
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001013
1014 return page[0];
Avi Kivity954bbbc2007-03-30 14:02:32 +03001015}
1016EXPORT_SYMBOL_GPL(gfn_to_page);
1017
Izik Eidus8a7ae052007-10-18 11:09:33 +02001018void kvm_release_page(struct page *page)
1019{
1020 if (!PageReserved(page))
1021 SetPageDirty(page);
1022 put_page(page);
1023}
1024EXPORT_SYMBOL_GPL(kvm_release_page);
1025
Izik Eidus195aefd2007-10-01 22:14:18 +02001026static int next_segment(unsigned long len, int offset)
1027{
1028 if (len > PAGE_SIZE - offset)
1029 return PAGE_SIZE - offset;
1030 else
1031 return len;
1032}
1033
1034int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1035 int len)
1036{
1037 void *page_virt;
1038 struct page *page;
1039
1040 page = gfn_to_page(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001041 if (is_error_page(page)) {
1042 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001043 return -EFAULT;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001044 }
Izik Eidus195aefd2007-10-01 22:14:18 +02001045 page_virt = kmap_atomic(page, KM_USER0);
1046
1047 memcpy(data, page_virt + offset, len);
1048
1049 kunmap_atomic(page_virt, KM_USER0);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001050 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001051 return 0;
1052}
1053EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1054
1055int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1056{
1057 gfn_t gfn = gpa >> PAGE_SHIFT;
1058 int seg;
1059 int offset = offset_in_page(gpa);
1060 int ret;
1061
1062 while ((seg = next_segment(len, offset)) != 0) {
1063 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1064 if (ret < 0)
1065 return ret;
1066 offset = 0;
1067 len -= seg;
1068 data += seg;
1069 ++gfn;
1070 }
1071 return 0;
1072}
1073EXPORT_SYMBOL_GPL(kvm_read_guest);
1074
1075int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1076 int offset, int len)
1077{
1078 void *page_virt;
1079 struct page *page;
1080
1081 page = gfn_to_page(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001082 if (is_error_page(page)) {
1083 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001084 return -EFAULT;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001085 }
Izik Eidus195aefd2007-10-01 22:14:18 +02001086 page_virt = kmap_atomic(page, KM_USER0);
1087
1088 memcpy(page_virt + offset, data, len);
1089
1090 kunmap_atomic(page_virt, KM_USER0);
1091 mark_page_dirty(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001092 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001093 return 0;
1094}
1095EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1096
1097int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1098 unsigned long len)
1099{
1100 gfn_t gfn = gpa >> PAGE_SHIFT;
1101 int seg;
1102 int offset = offset_in_page(gpa);
1103 int ret;
1104
1105 while ((seg = next_segment(len, offset)) != 0) {
1106 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1107 if (ret < 0)
1108 return ret;
1109 offset = 0;
1110 len -= seg;
1111 data += seg;
1112 ++gfn;
1113 }
1114 return 0;
1115}
1116
1117int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1118{
1119 void *page_virt;
1120 struct page *page;
1121
1122 page = gfn_to_page(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001123 if (is_error_page(page)) {
1124 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001125 return -EFAULT;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001126 }
Izik Eidus195aefd2007-10-01 22:14:18 +02001127 page_virt = kmap_atomic(page, KM_USER0);
1128
1129 memset(page_virt + offset, 0, len);
1130
1131 kunmap_atomic(page_virt, KM_USER0);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001132 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001133 return 0;
1134}
1135EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1136
1137int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1138{
1139 gfn_t gfn = gpa >> PAGE_SHIFT;
1140 int seg;
1141 int offset = offset_in_page(gpa);
1142 int ret;
1143
1144 while ((seg = next_segment(len, offset)) != 0) {
1145 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1146 if (ret < 0)
1147 return ret;
1148 offset = 0;
1149 len -= seg;
1150 ++gfn;
1151 }
1152 return 0;
1153}
1154EXPORT_SYMBOL_GPL(kvm_clear_guest);
1155
Rusty Russell7e9d6192007-07-31 20:41:14 +10001156/* WARNING: Does not work on aliased pages. */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001157void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1158{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001159 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001160
Rusty Russell7e9d6192007-07-31 20:41:14 +10001161 memslot = __gfn_to_memslot(kvm, gfn);
1162 if (memslot && memslot->dirty_bitmap) {
1163 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001164
Rusty Russell7e9d6192007-07-31 20:41:14 +10001165 /* avoid RMW */
1166 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1167 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001168 }
1169}
1170
Laurent Viviere7d5d762007-07-30 13:41:19 +03001171int emulator_read_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001172 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001173 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001174 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001175{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001176 void *data = val;
1177
1178 while (bytes) {
1179 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1180 unsigned offset = addr & (PAGE_SIZE-1);
1181 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
Izik Eidus195aefd2007-10-01 22:14:18 +02001182 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001183
1184 if (gpa == UNMAPPED_GVA)
1185 return X86EMUL_PROPAGATE_FAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001186 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
1187 if (ret < 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001188 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001189
1190 bytes -= tocopy;
1191 data += tocopy;
1192 addr += tocopy;
1193 }
1194
1195 return X86EMUL_CONTINUE;
1196}
Laurent Viviere7d5d762007-07-30 13:41:19 +03001197EXPORT_SYMBOL_GPL(emulator_read_std);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001198
1199static int emulator_write_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001200 const void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001201 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001202 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001203{
Rusty Russellf0242472007-08-01 10:48:02 +10001204 pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001205 return X86EMUL_UNHANDLEABLE;
1206}
1207
Eddie Dong97222cc2007-09-12 10:58:04 +03001208/*
1209 * Only apic need an MMIO device hook, so shortcut now..
1210 */
1211static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
1212 gpa_t addr)
1213{
1214 struct kvm_io_device *dev;
1215
1216 if (vcpu->apic) {
1217 dev = &vcpu->apic->dev;
1218 if (dev->in_range(dev, addr))
1219 return dev;
1220 }
1221 return NULL;
1222}
1223
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001224static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1225 gpa_t addr)
1226{
Eddie Dong97222cc2007-09-12 10:58:04 +03001227 struct kvm_io_device *dev;
1228
1229 dev = vcpu_find_pervcpu_dev(vcpu, addr);
1230 if (dev == NULL)
1231 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1232 return dev;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001233}
1234
Eddie Dong74906342007-06-19 18:05:03 +03001235static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
1236 gpa_t addr)
1237{
1238 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
1239}
1240
Avi Kivity6aa8b732006-12-10 02:21:36 -08001241static int emulator_read_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001242 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001243 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001244 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001245{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001246 struct kvm_io_device *mmio_dev;
1247 gpa_t gpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001248
1249 if (vcpu->mmio_read_completed) {
1250 memcpy(val, vcpu->mmio_data, bytes);
1251 vcpu->mmio_read_completed = 0;
1252 return X86EMUL_CONTINUE;
Laurent Viviercebff022007-07-30 13:35:24 +03001253 } else if (emulator_read_std(addr, val, bytes, vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001254 == X86EMUL_CONTINUE)
1255 return X86EMUL_CONTINUE;
Avi Kivityd27d4ac2007-02-19 14:37:46 +02001256
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001257 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1258 if (gpa == UNMAPPED_GVA)
1259 return X86EMUL_PROPAGATE_FAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001260
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001261 /*
1262 * Is this MMIO handled locally?
1263 */
1264 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1265 if (mmio_dev) {
1266 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1267 return X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001268 }
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001269
1270 vcpu->mmio_needed = 1;
1271 vcpu->mmio_phys_addr = gpa;
1272 vcpu->mmio_size = bytes;
1273 vcpu->mmio_is_write = 0;
1274
1275 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001276}
1277
Avi Kivityda4a00f2007-01-05 16:36:44 -08001278static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity4c690a12007-04-22 15:28:19 +03001279 const void *val, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001280{
Izik Eidus195aefd2007-10-01 22:14:18 +02001281 int ret;
Avi Kivityda4a00f2007-01-05 16:36:44 -08001282
Izik Eidus195aefd2007-10-01 22:14:18 +02001283 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
1284 if (ret < 0)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001285 return 0;
Shaohua Life551882007-07-23 14:51:39 +08001286 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001287 return 1;
1288}
1289
Avi Kivityb0fcd902007-07-22 18:48:54 +03001290static int emulator_write_emulated_onepage(unsigned long addr,
1291 const void *val,
1292 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001293 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001294{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001295 struct kvm_io_device *mmio_dev;
1296 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001297
Avi Kivityc9047f52007-04-17 10:53:22 +03001298 if (gpa == UNMAPPED_GVA) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001299 kvm_x86_ops->inject_page_fault(vcpu, addr, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001300 return X86EMUL_PROPAGATE_FAULT;
Avi Kivityc9047f52007-04-17 10:53:22 +03001301 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001302
Avi Kivityda4a00f2007-01-05 16:36:44 -08001303 if (emulator_write_phys(vcpu, gpa, val, bytes))
1304 return X86EMUL_CONTINUE;
1305
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001306 /*
1307 * Is this MMIO handled locally?
1308 */
1309 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1310 if (mmio_dev) {
1311 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1312 return X86EMUL_CONTINUE;
1313 }
1314
Avi Kivity6aa8b732006-12-10 02:21:36 -08001315 vcpu->mmio_needed = 1;
1316 vcpu->mmio_phys_addr = gpa;
1317 vcpu->mmio_size = bytes;
1318 vcpu->mmio_is_write = 1;
Avi Kivity4c690a12007-04-22 15:28:19 +03001319 memcpy(vcpu->mmio_data, val, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001320
1321 return X86EMUL_CONTINUE;
1322}
1323
Laurent Viviere7d5d762007-07-30 13:41:19 +03001324int emulator_write_emulated(unsigned long addr,
Avi Kivityb0fcd902007-07-22 18:48:54 +03001325 const void *val,
1326 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001327 struct kvm_vcpu *vcpu)
Avi Kivityb0fcd902007-07-22 18:48:54 +03001328{
1329 /* Crossing a page boundary? */
1330 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1331 int rc, now;
1332
1333 now = -addr & ~PAGE_MASK;
Laurent Viviercebff022007-07-30 13:35:24 +03001334 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001335 if (rc != X86EMUL_CONTINUE)
1336 return rc;
1337 addr += now;
1338 val += now;
1339 bytes -= now;
1340 }
Laurent Viviercebff022007-07-30 13:35:24 +03001341 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001342}
Laurent Viviere7d5d762007-07-30 13:41:19 +03001343EXPORT_SYMBOL_GPL(emulator_write_emulated);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001344
Avi Kivity6aa8b732006-12-10 02:21:36 -08001345static int emulator_cmpxchg_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001346 const void *old,
1347 const void *new,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001348 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001349 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001350{
1351 static int reported;
1352
1353 if (!reported) {
1354 reported = 1;
1355 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1356 }
Laurent Viviercebff022007-07-30 13:35:24 +03001357 return emulator_write_emulated(addr, new, bytes, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001358}
1359
1360static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1361{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001362 return kvm_x86_ops->get_segment_base(vcpu, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001363}
1364
1365int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1366{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001367 return X86EMUL_CONTINUE;
1368}
1369
1370int emulate_clts(struct kvm_vcpu *vcpu)
1371{
Amit Shah404fb882007-11-19 17:57:35 +02001372 kvm_x86_ops->set_cr0(vcpu, vcpu->cr0 & ~X86_CR0_TS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001373 return X86EMUL_CONTINUE;
1374}
1375
Mike Dayd77c26f2007-10-08 09:02:08 -04001376int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001377{
1378 struct kvm_vcpu *vcpu = ctxt->vcpu;
1379
1380 switch (dr) {
1381 case 0 ... 3:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001382 *dest = kvm_x86_ops->get_dr(vcpu, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001383 return X86EMUL_CONTINUE;
1384 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001385 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001386 return X86EMUL_UNHANDLEABLE;
1387 }
1388}
1389
1390int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1391{
1392 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1393 int exception;
1394
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001395 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001396 if (exception) {
1397 /* FIXME: better handling */
1398 return X86EMUL_UNHANDLEABLE;
1399 }
1400 return X86EMUL_CONTINUE;
1401}
1402
Avi Kivity054b1362007-09-12 13:21:09 +03001403void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001404{
1405 static int reported;
1406 u8 opcodes[4];
Avi Kivity054b1362007-09-12 13:21:09 +03001407 unsigned long rip = vcpu->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001408 unsigned long rip_linear;
1409
Avi Kivity054b1362007-09-12 13:21:09 +03001410 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001411
1412 if (reported)
1413 return;
1414
Avi Kivity054b1362007-09-12 13:21:09 +03001415 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001416
Avi Kivity054b1362007-09-12 13:21:09 +03001417 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1418 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001419 reported = 1;
1420}
Avi Kivity054b1362007-09-12 13:21:09 +03001421EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001422
1423struct x86_emulate_ops emulate_ops = {
1424 .read_std = emulator_read_std,
1425 .write_std = emulator_write_std,
1426 .read_emulated = emulator_read_emulated,
1427 .write_emulated = emulator_write_emulated,
1428 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1429};
1430
1431int emulate_instruction(struct kvm_vcpu *vcpu,
1432 struct kvm_run *run,
1433 unsigned long cr2,
Laurent Vivier34273182007-09-18 11:27:37 +02001434 u16 error_code,
1435 int no_decode)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001436{
Laurent Viviera22436b2007-09-24 17:00:58 +02001437 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001438
Avi Kivitye7df56e2007-03-14 15:54:54 +02001439 vcpu->mmio_fault_cr2 = cr2;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001440 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001441
Avi Kivity6aa8b732006-12-10 02:21:36 -08001442 vcpu->mmio_is_write = 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03001443 vcpu->pio.string = 0;
Laurent Vivier34273182007-09-18 11:27:37 +02001444
1445 if (!no_decode) {
1446 int cs_db, cs_l;
1447 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1448
1449 vcpu->emulate_ctxt.vcpu = vcpu;
1450 vcpu->emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
1451 vcpu->emulate_ctxt.cr2 = cr2;
1452 vcpu->emulate_ctxt.mode =
1453 (vcpu->emulate_ctxt.eflags & X86_EFLAGS_VM)
1454 ? X86EMUL_MODE_REAL : cs_l
1455 ? X86EMUL_MODE_PROT64 : cs_db
1456 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1457
1458 if (vcpu->emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1459 vcpu->emulate_ctxt.cs_base = 0;
1460 vcpu->emulate_ctxt.ds_base = 0;
1461 vcpu->emulate_ctxt.es_base = 0;
1462 vcpu->emulate_ctxt.ss_base = 0;
1463 } else {
1464 vcpu->emulate_ctxt.cs_base =
1465 get_segment_base(vcpu, VCPU_SREG_CS);
1466 vcpu->emulate_ctxt.ds_base =
1467 get_segment_base(vcpu, VCPU_SREG_DS);
1468 vcpu->emulate_ctxt.es_base =
1469 get_segment_base(vcpu, VCPU_SREG_ES);
1470 vcpu->emulate_ctxt.ss_base =
1471 get_segment_base(vcpu, VCPU_SREG_SS);
1472 }
1473
1474 vcpu->emulate_ctxt.gs_base =
1475 get_segment_base(vcpu, VCPU_SREG_GS);
1476 vcpu->emulate_ctxt.fs_base =
1477 get_segment_base(vcpu, VCPU_SREG_FS);
1478
1479 r = x86_decode_insn(&vcpu->emulate_ctxt, &emulate_ops);
Laurent Viviera22436b2007-09-24 17:00:58 +02001480 if (r) {
1481 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1482 return EMULATE_DONE;
1483 return EMULATE_FAIL;
1484 }
Laurent Vivier34273182007-09-18 11:27:37 +02001485 }
1486
Laurent Viviera22436b2007-09-24 17:00:58 +02001487 r = x86_emulate_insn(&vcpu->emulate_ctxt, &emulate_ops);
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001488
Laurent Viviere70669a2007-08-05 10:36:40 +03001489 if (vcpu->pio.string)
1490 return EMULATE_DO_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001491
1492 if ((r || vcpu->mmio_is_write) && run) {
Jeff Dike8fc0d082007-07-17 12:26:59 -04001493 run->exit_reason = KVM_EXIT_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001494 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1495 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1496 run->mmio.len = vcpu->mmio_size;
1497 run->mmio.is_write = vcpu->mmio_is_write;
1498 }
1499
1500 if (r) {
Avi Kivitya4360362007-01-05 16:36:45 -08001501 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1502 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001503 if (!vcpu->mmio_needed) {
Avi Kivity054b1362007-09-12 13:21:09 +03001504 kvm_report_emulation_failure(vcpu, "mmio");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001505 return EMULATE_FAIL;
1506 }
1507 return EMULATE_DO_MMIO;
1508 }
1509
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001510 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier34273182007-09-18 11:27:37 +02001511 kvm_x86_ops->set_rflags(vcpu, vcpu->emulate_ctxt.eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001512
Avi Kivity02c83202007-04-29 15:02:17 +03001513 if (vcpu->mmio_is_write) {
1514 vcpu->mmio_needed = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001515 return EMULATE_DO_MMIO;
Avi Kivity02c83202007-04-29 15:02:17 +03001516 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001517
1518 return EMULATE_DONE;
1519}
1520EXPORT_SYMBOL_GPL(emulate_instruction);
1521
Eddie Dongb6958ce2007-07-18 12:15:21 +03001522/*
1523 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1524 */
He, Qingc5ec1532007-09-03 17:07:41 +03001525static void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001526{
1527 DECLARE_WAITQUEUE(wait, current);
1528
1529 add_wait_queue(&vcpu->wq, &wait);
1530
1531 /*
1532 * We will block until either an interrupt or a signal wakes us up
1533 */
He, Qingc5ec1532007-09-03 17:07:41 +03001534 while (!kvm_cpu_has_interrupt(vcpu)
1535 && !signal_pending(current)
1536 && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE
1537 && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) {
Eddie Dongb6958ce2007-07-18 12:15:21 +03001538 set_current_state(TASK_INTERRUPTIBLE);
1539 vcpu_put(vcpu);
1540 schedule();
1541 vcpu_load(vcpu);
1542 }
1543
He, Qingc5ec1532007-09-03 17:07:41 +03001544 __set_current_state(TASK_RUNNING);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001545 remove_wait_queue(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001546}
1547
Avi Kivityd3bef152007-06-05 15:53:05 +03001548int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1549{
Avi Kivityd3bef152007-06-05 15:53:05 +03001550 ++vcpu->stat.halt_exits;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001551 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc5ec1532007-09-03 17:07:41 +03001552 vcpu->mp_state = VCPU_MP_STATE_HALTED;
1553 kvm_vcpu_block(vcpu);
1554 if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE)
1555 return -EINTR;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001556 return 1;
1557 } else {
1558 vcpu->run->exit_reason = KVM_EXIT_HLT;
1559 return 0;
1560 }
Avi Kivityd3bef152007-06-05 15:53:05 +03001561}
1562EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1563
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001564int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
Avi Kivity270fd9b2007-02-19 14:37:47 +02001565{
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001566 unsigned long nr, a0, a1, a2, a3, ret;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001567
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001568 kvm_x86_ops->cache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001569
1570 nr = vcpu->regs[VCPU_REGS_RAX];
1571 a0 = vcpu->regs[VCPU_REGS_RBX];
1572 a1 = vcpu->regs[VCPU_REGS_RCX];
1573 a2 = vcpu->regs[VCPU_REGS_RDX];
1574 a3 = vcpu->regs[VCPU_REGS_RSI];
1575
1576 if (!is_long_mode(vcpu)) {
1577 nr &= 0xFFFFFFFF;
1578 a0 &= 0xFFFFFFFF;
1579 a1 &= 0xFFFFFFFF;
1580 a2 &= 0xFFFFFFFF;
1581 a3 &= 0xFFFFFFFF;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001582 }
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001583
Avi Kivity270fd9b2007-02-19 14:37:47 +02001584 switch (nr) {
1585 default:
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001586 ret = -KVM_ENOSYS;
1587 break;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001588 }
1589 vcpu->regs[VCPU_REGS_RAX] = ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001590 kvm_x86_ops->decache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001591 return 0;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001592}
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001593EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
1594
1595int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
1596{
1597 char instruction[3];
1598 int ret = 0;
1599
1600 mutex_lock(&vcpu->kvm->lock);
1601
1602 /*
1603 * Blow out the MMU to ensure that no other VCPU has an active mapping
1604 * to ensure that the updated hypercall appears atomically across all
1605 * VCPUs.
1606 */
1607 kvm_mmu_zap_all(vcpu->kvm);
1608
1609 kvm_x86_ops->cache_regs(vcpu);
1610 kvm_x86_ops->patch_hypercall(vcpu, instruction);
1611 if (emulator_write_emulated(vcpu->rip, instruction, 3, vcpu)
1612 != X86EMUL_CONTINUE)
1613 ret = -EFAULT;
1614
1615 mutex_unlock(&vcpu->kvm->lock);
1616
1617 return ret;
1618}
Avi Kivity270fd9b2007-02-19 14:37:47 +02001619
Avi Kivity6aa8b732006-12-10 02:21:36 -08001620static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1621{
1622 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1623}
1624
1625void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1626{
1627 struct descriptor_table dt = { limit, base };
1628
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001629 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001630}
1631
1632void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1633{
1634 struct descriptor_table dt = { limit, base };
1635
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001636 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001637}
1638
1639void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1640 unsigned long *rflags)
1641{
1642 lmsw(vcpu, msw);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001643 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001644}
1645
1646unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1647{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001648 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001649 switch (cr) {
1650 case 0:
1651 return vcpu->cr0;
1652 case 2:
1653 return vcpu->cr2;
1654 case 3:
1655 return vcpu->cr3;
1656 case 4:
1657 return vcpu->cr4;
1658 default:
1659 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1660 return 0;
1661 }
1662}
1663
1664void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1665 unsigned long *rflags)
1666{
1667 switch (cr) {
1668 case 0:
1669 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001670 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001671 break;
1672 case 2:
1673 vcpu->cr2 = val;
1674 break;
1675 case 3:
1676 set_cr3(vcpu, val);
1677 break;
1678 case 4:
1679 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1680 break;
1681 default:
1682 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1683 }
1684}
1685
Avi Kivity3bab1f52006-12-29 16:49:48 -08001686int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1687{
1688 u64 data;
1689
1690 switch (msr) {
1691 case 0xc0010010: /* SYSCFG */
1692 case 0xc0010015: /* HWCR */
1693 case MSR_IA32_PLATFORM_ID:
1694 case MSR_IA32_P5_MC_ADDR:
1695 case MSR_IA32_P5_MC_TYPE:
1696 case MSR_IA32_MC0_CTL:
1697 case MSR_IA32_MCG_STATUS:
1698 case MSR_IA32_MCG_CAP:
1699 case MSR_IA32_MC0_MISC:
1700 case MSR_IA32_MC0_MISC+4:
1701 case MSR_IA32_MC0_MISC+8:
1702 case MSR_IA32_MC0_MISC+12:
1703 case MSR_IA32_MC0_MISC+16:
1704 case MSR_IA32_UCODE_REV:
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001705 case MSR_IA32_PERF_STATUS:
Matthew Gregan2dc70942007-05-06 10:59:46 +03001706 case MSR_IA32_EBL_CR_POWERON:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001707 /* MTRR registers */
1708 case 0xfe:
1709 case 0x200 ... 0x2ff:
1710 data = 0;
1711 break;
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001712 case 0xcd: /* fsb frequency */
1713 data = 3;
1714 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001715 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001716 data = kvm_get_apic_base(vcpu);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001717 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001718 case MSR_IA32_MISC_ENABLE:
1719 data = vcpu->ia32_misc_enable_msr;
1720 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001721#ifdef CONFIG_X86_64
1722 case MSR_EFER:
1723 data = vcpu->shadow_efer;
1724 break;
1725#endif
1726 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001727 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001728 return 1;
1729 }
1730 *pdata = data;
1731 return 0;
1732}
1733EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1734
Avi Kivity6aa8b732006-12-10 02:21:36 -08001735/*
1736 * Reads an msr value (of 'msr_index') into 'pdata'.
1737 * Returns 0 on success, non-0 otherwise.
1738 * Assumes vcpu_load() was already called.
1739 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001740int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001741{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001742 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001743}
1744
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001745#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001746
Avi Kivity3bab1f52006-12-29 16:49:48 -08001747static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001748{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001749 if (efer & EFER_RESERVED_BITS) {
1750 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1751 efer);
1752 inject_gp(vcpu);
1753 return;
1754 }
1755
1756 if (is_paging(vcpu)
1757 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1758 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1759 inject_gp(vcpu);
1760 return;
1761 }
1762
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001763 kvm_x86_ops->set_efer(vcpu, efer);
Avi Kivity7725f0b2006-12-13 00:34:01 -08001764
Avi Kivity6aa8b732006-12-10 02:21:36 -08001765 efer &= ~EFER_LMA;
1766 efer |= vcpu->shadow_efer & EFER_LMA;
1767
1768 vcpu->shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001769}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001770
1771#endif
1772
Avi Kivity3bab1f52006-12-29 16:49:48 -08001773int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1774{
1775 switch (msr) {
1776#ifdef CONFIG_X86_64
1777 case MSR_EFER:
1778 set_efer(vcpu, data);
1779 break;
1780#endif
1781 case MSR_IA32_MC0_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001782 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
Avi Kivity3bab1f52006-12-29 16:49:48 -08001783 __FUNCTION__, data);
1784 break;
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001785 case MSR_IA32_MCG_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001786 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001787 __FUNCTION__, data);
1788 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001789 case MSR_IA32_UCODE_REV:
1790 case MSR_IA32_UCODE_WRITE:
1791 case 0x200 ... 0x2ff: /* MTRRs */
1792 break;
1793 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001794 kvm_set_apic_base(vcpu, data);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001795 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001796 case MSR_IA32_MISC_ENABLE:
1797 vcpu->ia32_misc_enable_msr = data;
1798 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001799 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001800 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001801 return 1;
1802 }
1803 return 0;
1804}
1805EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1806
Avi Kivity6aa8b732006-12-10 02:21:36 -08001807/*
1808 * Writes msr value into into the appropriate "register".
1809 * Returns 0 on success, non-0 otherwise.
1810 * Assumes vcpu_load() was already called.
1811 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001812int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001813{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001814 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001815}
1816
1817void kvm_resched(struct kvm_vcpu *vcpu)
1818{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001819 if (!need_resched())
1820 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001821 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001822}
1823EXPORT_SYMBOL_GPL(kvm_resched);
1824
Avi Kivity06465c52007-02-28 20:46:53 +02001825void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1826{
1827 int i;
1828 u32 function;
1829 struct kvm_cpuid_entry *e, *best;
1830
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001831 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001832 function = vcpu->regs[VCPU_REGS_RAX];
1833 vcpu->regs[VCPU_REGS_RAX] = 0;
1834 vcpu->regs[VCPU_REGS_RBX] = 0;
1835 vcpu->regs[VCPU_REGS_RCX] = 0;
1836 vcpu->regs[VCPU_REGS_RDX] = 0;
1837 best = NULL;
1838 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1839 e = &vcpu->cpuid_entries[i];
1840 if (e->function == function) {
1841 best = e;
1842 break;
1843 }
1844 /*
1845 * Both basic or both extended?
1846 */
1847 if (((e->function ^ function) & 0x80000000) == 0)
1848 if (!best || e->function > best->function)
1849 best = e;
1850 }
1851 if (best) {
1852 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1853 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1854 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1855 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1856 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001857 kvm_x86_ops->decache_regs(vcpu);
1858 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001859}
1860EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1861
Avi Kivity039576c2007-03-20 12:46:50 +02001862static int pio_copy_data(struct kvm_vcpu *vcpu)
Avi Kivity46fc1472007-02-22 19:39:30 +02001863{
Avi Kivity039576c2007-03-20 12:46:50 +02001864 void *p = vcpu->pio_data;
1865 void *q;
1866 unsigned bytes;
1867 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1868
Avi Kivity039576c2007-03-20 12:46:50 +02001869 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1870 PAGE_KERNEL);
1871 if (!q) {
Avi Kivity039576c2007-03-20 12:46:50 +02001872 free_pio_guest_pages(vcpu);
1873 return -ENOMEM;
1874 }
1875 q += vcpu->pio.guest_page_offset;
1876 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1877 if (vcpu->pio.in)
1878 memcpy(q, p, bytes);
1879 else
1880 memcpy(p, q, bytes);
1881 q -= vcpu->pio.guest_page_offset;
1882 vunmap(q);
Avi Kivity039576c2007-03-20 12:46:50 +02001883 free_pio_guest_pages(vcpu);
1884 return 0;
1885}
1886
1887static int complete_pio(struct kvm_vcpu *vcpu)
1888{
1889 struct kvm_pio_request *io = &vcpu->pio;
Avi Kivity46fc1472007-02-22 19:39:30 +02001890 long delta;
Avi Kivity039576c2007-03-20 12:46:50 +02001891 int r;
Avi Kivity46fc1472007-02-22 19:39:30 +02001892
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001893 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001894
1895 if (!io->string) {
Avi Kivity039576c2007-03-20 12:46:50 +02001896 if (io->in)
1897 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
Avi Kivity46fc1472007-02-22 19:39:30 +02001898 io->size);
1899 } else {
Avi Kivity039576c2007-03-20 12:46:50 +02001900 if (io->in) {
1901 r = pio_copy_data(vcpu);
1902 if (r) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001903 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02001904 return r;
1905 }
1906 }
1907
Avi Kivity46fc1472007-02-22 19:39:30 +02001908 delta = 1;
1909 if (io->rep) {
Avi Kivity039576c2007-03-20 12:46:50 +02001910 delta *= io->cur_count;
Avi Kivity46fc1472007-02-22 19:39:30 +02001911 /*
1912 * The size of the register should really depend on
1913 * current address size.
1914 */
1915 vcpu->regs[VCPU_REGS_RCX] -= delta;
1916 }
Avi Kivity039576c2007-03-20 12:46:50 +02001917 if (io->down)
Avi Kivity46fc1472007-02-22 19:39:30 +02001918 delta = -delta;
1919 delta *= io->size;
Avi Kivity039576c2007-03-20 12:46:50 +02001920 if (io->in)
Avi Kivity46fc1472007-02-22 19:39:30 +02001921 vcpu->regs[VCPU_REGS_RDI] += delta;
1922 else
1923 vcpu->regs[VCPU_REGS_RSI] += delta;
1924 }
1925
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001926 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001927
Avi Kivity039576c2007-03-20 12:46:50 +02001928 io->count -= io->cur_count;
1929 io->cur_count = 0;
1930
Avi Kivity039576c2007-03-20 12:46:50 +02001931 return 0;
Avi Kivity46fc1472007-02-22 19:39:30 +02001932}
1933
Eddie Dong65619eb2007-07-17 11:52:33 +03001934static void kernel_pio(struct kvm_io_device *pio_dev,
1935 struct kvm_vcpu *vcpu,
1936 void *pd)
Eddie Dong74906342007-06-19 18:05:03 +03001937{
1938 /* TODO: String I/O for in kernel device */
1939
Eddie Dong9cf98822007-07-22 10:36:31 +03001940 mutex_lock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001941 if (vcpu->pio.in)
1942 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1943 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001944 pd);
Eddie Dong74906342007-06-19 18:05:03 +03001945 else
1946 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1947 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001948 pd);
Eddie Dong9cf98822007-07-22 10:36:31 +03001949 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001950}
1951
1952static void pio_string_write(struct kvm_io_device *pio_dev,
1953 struct kvm_vcpu *vcpu)
1954{
1955 struct kvm_pio_request *io = &vcpu->pio;
1956 void *pd = vcpu->pio_data;
1957 int i;
1958
Eddie Dong9cf98822007-07-22 10:36:31 +03001959 mutex_lock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001960 for (i = 0; i < io->cur_count; i++) {
1961 kvm_iodevice_write(pio_dev, io->port,
1962 io->size,
1963 pd);
1964 pd += io->size;
1965 }
Eddie Dong9cf98822007-07-22 10:36:31 +03001966 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001967}
1968
Mike Dayd77c26f2007-10-08 09:02:08 -04001969int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
Laurent Vivier3090dd72007-08-05 10:43:32 +03001970 int size, unsigned port)
1971{
1972 struct kvm_io_device *pio_dev;
1973
1974 vcpu->run->exit_reason = KVM_EXIT_IO;
1975 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1976 vcpu->run->io.size = vcpu->pio.size = size;
1977 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1978 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1;
1979 vcpu->run->io.port = vcpu->pio.port = port;
1980 vcpu->pio.in = in;
1981 vcpu->pio.string = 0;
1982 vcpu->pio.down = 0;
1983 vcpu->pio.guest_page_offset = 0;
1984 vcpu->pio.rep = 0;
1985
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001986 kvm_x86_ops->cache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03001987 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001988 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03001989
Avi Kivity0967b7b2007-09-15 17:34:36 +03001990 kvm_x86_ops->skip_emulated_instruction(vcpu);
1991
Laurent Vivier3090dd72007-08-05 10:43:32 +03001992 pio_dev = vcpu_find_pio_dev(vcpu, port);
1993 if (pio_dev) {
1994 kernel_pio(pio_dev, vcpu, vcpu->pio_data);
1995 complete_pio(vcpu);
1996 return 1;
1997 }
1998 return 0;
1999}
2000EXPORT_SYMBOL_GPL(kvm_emulate_pio);
2001
2002int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2003 int size, unsigned long count, int down,
Avi Kivity039576c2007-03-20 12:46:50 +02002004 gva_t address, int rep, unsigned port)
2005{
2006 unsigned now, in_page;
Eddie Dong65619eb2007-07-17 11:52:33 +03002007 int i, ret = 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002008 int nr_pages = 1;
2009 struct page *page;
Eddie Dong74906342007-06-19 18:05:03 +03002010 struct kvm_io_device *pio_dev;
Avi Kivity039576c2007-03-20 12:46:50 +02002011
2012 vcpu->run->exit_reason = KVM_EXIT_IO;
2013 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Laurent Vivier3090dd72007-08-05 10:43:32 +03002014 vcpu->run->io.size = vcpu->pio.size = size;
Avi Kivity039576c2007-03-20 12:46:50 +02002015 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Laurent Vivier3090dd72007-08-05 10:43:32 +03002016 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count;
2017 vcpu->run->io.port = vcpu->pio.port = port;
Avi Kivity039576c2007-03-20 12:46:50 +02002018 vcpu->pio.in = in;
Laurent Vivier3090dd72007-08-05 10:43:32 +03002019 vcpu->pio.string = 1;
Avi Kivity039576c2007-03-20 12:46:50 +02002020 vcpu->pio.down = down;
2021 vcpu->pio.guest_page_offset = offset_in_page(address);
2022 vcpu->pio.rep = rep;
2023
Avi Kivity039576c2007-03-20 12:46:50 +02002024 if (!count) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002025 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02002026 return 1;
2027 }
2028
Avi Kivity039576c2007-03-20 12:46:50 +02002029 if (!down)
2030 in_page = PAGE_SIZE - offset_in_page(address);
2031 else
2032 in_page = offset_in_page(address) + size;
2033 now = min(count, (unsigned long)in_page / size);
2034 if (!now) {
2035 /*
2036 * String I/O straddles page boundary. Pin two guest pages
2037 * so that we satisfy atomicity constraints. Do just one
2038 * transaction to avoid complexity.
2039 */
2040 nr_pages = 2;
2041 now = 1;
2042 }
2043 if (down) {
2044 /*
2045 * String I/O in reverse. Yuck. Kill the guest, fix later.
2046 */
Rusty Russellf0242472007-08-01 10:48:02 +10002047 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivity039576c2007-03-20 12:46:50 +02002048 inject_gp(vcpu);
2049 return 1;
2050 }
2051 vcpu->run->io.count = now;
2052 vcpu->pio.cur_count = now;
2053
Avi Kivity0967b7b2007-09-15 17:34:36 +03002054 if (vcpu->pio.cur_count == vcpu->pio.count)
2055 kvm_x86_ops->skip_emulated_instruction(vcpu);
2056
Avi Kivity039576c2007-03-20 12:46:50 +02002057 for (i = 0; i < nr_pages; ++i) {
Shaohua Li11ec2802007-07-23 14:51:37 +08002058 mutex_lock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02002059 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
Avi Kivity039576c2007-03-20 12:46:50 +02002060 vcpu->pio.guest_pages[i] = page;
Shaohua Li11ec2802007-07-23 14:51:37 +08002061 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02002062 if (!page) {
2063 inject_gp(vcpu);
2064 free_pio_guest_pages(vcpu);
2065 return 1;
2066 }
2067 }
2068
Laurent Vivier3090dd72007-08-05 10:43:32 +03002069 pio_dev = vcpu_find_pio_dev(vcpu, port);
Eddie Dong65619eb2007-07-17 11:52:33 +03002070 if (!vcpu->pio.in) {
2071 /* string PIO write */
2072 ret = pio_copy_data(vcpu);
2073 if (ret >= 0 && pio_dev) {
2074 pio_string_write(pio_dev, vcpu);
2075 complete_pio(vcpu);
2076 if (vcpu->pio.count == 0)
2077 ret = 1;
2078 }
2079 } else if (pio_dev)
Rusty Russellf0242472007-08-01 10:48:02 +10002080 pr_unimpl(vcpu, "no string pio read support yet, "
Eddie Dong65619eb2007-07-17 11:52:33 +03002081 "port %x size %d count %ld\n",
2082 port, size, count);
2083
2084 return ret;
Avi Kivity039576c2007-03-20 12:46:50 +02002085}
Laurent Vivier3090dd72007-08-05 10:43:32 +03002086EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
Avi Kivity039576c2007-03-20 12:46:50 +02002087
Avi Kivity04d2cc72007-09-10 18:10:54 +03002088/*
2089 * Check if userspace requested an interrupt window, and that the
2090 * interrupt window is open.
2091 *
2092 * No need to exit to userspace if we already have an interrupt queued.
2093 */
2094static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2095 struct kvm_run *kvm_run)
2096{
2097 return (!vcpu->irq_summary &&
2098 kvm_run->request_interrupt_window &&
2099 vcpu->interrupt_window_open &&
2100 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
2101}
2102
2103static void post_kvm_run_save(struct kvm_vcpu *vcpu,
2104 struct kvm_run *kvm_run)
2105{
2106 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
2107 kvm_run->cr8 = get_cr8(vcpu);
2108 kvm_run->apic_base = kvm_get_apic_base(vcpu);
2109 if (irqchip_in_kernel(vcpu->kvm))
2110 kvm_run->ready_for_interrupt_injection = 1;
2111 else
2112 kvm_run->ready_for_interrupt_injection =
2113 (vcpu->interrupt_window_open &&
2114 vcpu->irq_summary == 0);
2115}
2116
2117static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2118{
2119 int r;
2120
2121 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002122 pr_debug("vcpu %d received sipi with vector # %x\n",
Avi Kivity04d2cc72007-09-10 18:10:54 +03002123 vcpu->vcpu_id, vcpu->sipi_vector);
2124 kvm_lapic_reset(vcpu);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002125 r = kvm_x86_ops->vcpu_reset(vcpu);
2126 if (r)
2127 return r;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002128 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
2129 }
2130
2131preempted:
2132 if (vcpu->guest_debug.enabled)
2133 kvm_x86_ops->guest_debug_pre(vcpu);
2134
2135again:
2136 r = kvm_mmu_reload(vcpu);
2137 if (unlikely(r))
2138 goto out;
2139
Avi Kivityab6ef342007-10-16 16:23:22 +02002140 kvm_inject_pending_timer_irqs(vcpu);
2141
Avi Kivity04d2cc72007-09-10 18:10:54 +03002142 preempt_disable();
2143
2144 kvm_x86_ops->prepare_guest_switch(vcpu);
2145 kvm_load_guest_fpu(vcpu);
2146
2147 local_irq_disable();
2148
2149 if (signal_pending(current)) {
2150 local_irq_enable();
2151 preempt_enable();
2152 r = -EINTR;
2153 kvm_run->exit_reason = KVM_EXIT_INTR;
2154 ++vcpu->stat.signal_exits;
2155 goto out;
2156 }
2157
2158 if (irqchip_in_kernel(vcpu->kvm))
2159 kvm_x86_ops->inject_pending_irq(vcpu);
2160 else if (!vcpu->mmio_read_completed)
2161 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2162
2163 vcpu->guest_mode = 1;
Laurent Vivierd172fcd2007-10-15 17:00:19 +02002164 kvm_guest_enter();
Avi Kivity04d2cc72007-09-10 18:10:54 +03002165
2166 if (vcpu->requests)
Avi Kivity3176bc32007-10-16 17:22:08 +02002167 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivity04d2cc72007-09-10 18:10:54 +03002168 kvm_x86_ops->tlb_flush(vcpu);
2169
2170 kvm_x86_ops->run(vcpu, kvm_run);
2171
2172 vcpu->guest_mode = 0;
2173 local_irq_enable();
2174
2175 ++vcpu->stat.exits;
2176
Laurent Vivier0552f732007-10-18 15:19:01 +02002177 /*
2178 * We must have an instruction between local_irq_enable() and
2179 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2180 * the interrupt shadow. The stat.exits increment will do nicely.
2181 * But we need to prevent reordering, hence this barrier():
2182 */
2183 barrier();
2184
2185 kvm_guest_exit();
2186
Avi Kivity04d2cc72007-09-10 18:10:54 +03002187 preempt_enable();
2188
2189 /*
2190 * Profile KVM exit RIPs:
2191 */
2192 if (unlikely(prof_on == KVM_PROFILING)) {
2193 kvm_x86_ops->cache_regs(vcpu);
2194 profile_hit(KVM_PROFILING, (void *)vcpu->rip);
2195 }
2196
2197 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2198
2199 if (r > 0) {
2200 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2201 r = -EINTR;
2202 kvm_run->exit_reason = KVM_EXIT_INTR;
2203 ++vcpu->stat.request_irq_exits;
2204 goto out;
2205 }
2206 if (!need_resched()) {
2207 ++vcpu->stat.light_exits;
2208 goto again;
2209 }
2210 }
2211
2212out:
2213 if (r > 0) {
2214 kvm_resched(vcpu);
2215 goto preempted;
2216 }
2217
2218 post_kvm_run_save(vcpu, kvm_run);
2219
2220 return r;
2221}
2222
2223
Avi Kivitybccf2152007-02-21 18:04:26 +02002224static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002225{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002226 int r;
Avi Kivity1961d272007-03-05 19:46:05 +02002227 sigset_t sigsaved;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002228
Avi Kivitybccf2152007-02-21 18:04:26 +02002229 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002230
He, Qingc5ec1532007-09-03 17:07:41 +03002231 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
2232 kvm_vcpu_block(vcpu);
2233 vcpu_put(vcpu);
2234 return -EAGAIN;
2235 }
2236
Avi Kivity1961d272007-03-05 19:46:05 +02002237 if (vcpu->sigset_active)
2238 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2239
Dor Laor54810342007-02-12 00:54:39 -08002240 /* re-sync apic's tpr */
He, Qing5cd4f6f2007-08-30 17:04:26 +08002241 if (!irqchip_in_kernel(vcpu->kvm))
2242 set_cr8(vcpu, kvm_run->cr8);
Dor Laor54810342007-02-12 00:54:39 -08002243
Avi Kivity02c83202007-04-29 15:02:17 +03002244 if (vcpu->pio.cur_count) {
2245 r = complete_pio(vcpu);
2246 if (r)
2247 goto out;
2248 }
Zhang Xiantao34c16ee2007-10-20 15:34:38 +08002249#if CONFIG_HAS_IOMEM
Avi Kivity02c83202007-04-29 15:02:17 +03002250 if (vcpu->mmio_needed) {
2251 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
2252 vcpu->mmio_read_completed = 1;
2253 vcpu->mmio_needed = 0;
2254 r = emulate_instruction(vcpu, kvm_run,
Laurent Vivier34273182007-09-18 11:27:37 +02002255 vcpu->mmio_fault_cr2, 0, 1);
Avi Kivity02c83202007-04-29 15:02:17 +03002256 if (r == EMULATE_DO_MMIO) {
2257 /*
2258 * Read-modify-write. Back to userspace.
2259 */
Avi Kivity02c83202007-04-29 15:02:17 +03002260 r = 0;
2261 goto out;
Avi Kivity46fc1472007-02-22 19:39:30 +02002262 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002263 }
Zhang Xiantao34c16ee2007-10-20 15:34:38 +08002264#endif
Avi Kivity8eb7d332007-03-04 14:17:08 +02002265 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002266 kvm_x86_ops->cache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002267 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002268 kvm_x86_ops->decache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002269 }
2270
Avi Kivity04d2cc72007-09-10 18:10:54 +03002271 r = __vcpu_run(vcpu, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002272
Avi Kivity039576c2007-03-20 12:46:50 +02002273out:
Avi Kivity1961d272007-03-05 19:46:05 +02002274 if (vcpu->sigset_active)
2275 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2276
Avi Kivity6aa8b732006-12-10 02:21:36 -08002277 vcpu_put(vcpu);
2278 return r;
2279}
2280
Avi Kivitybccf2152007-02-21 18:04:26 +02002281static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
2282 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002283{
Avi Kivitybccf2152007-02-21 18:04:26 +02002284 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002285
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002286 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002287
2288 regs->rax = vcpu->regs[VCPU_REGS_RAX];
2289 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
2290 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
2291 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
2292 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
2293 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
2294 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
2295 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002296#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002297 regs->r8 = vcpu->regs[VCPU_REGS_R8];
2298 regs->r9 = vcpu->regs[VCPU_REGS_R9];
2299 regs->r10 = vcpu->regs[VCPU_REGS_R10];
2300 regs->r11 = vcpu->regs[VCPU_REGS_R11];
2301 regs->r12 = vcpu->regs[VCPU_REGS_R12];
2302 regs->r13 = vcpu->regs[VCPU_REGS_R13];
2303 regs->r14 = vcpu->regs[VCPU_REGS_R14];
2304 regs->r15 = vcpu->regs[VCPU_REGS_R15];
2305#endif
2306
2307 regs->rip = vcpu->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002308 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002309
2310 /*
2311 * Don't leak debug flags in case they were set for guest debugging
2312 */
2313 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2314 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2315
2316 vcpu_put(vcpu);
2317
2318 return 0;
2319}
2320
Avi Kivitybccf2152007-02-21 18:04:26 +02002321static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
2322 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002323{
Avi Kivitybccf2152007-02-21 18:04:26 +02002324 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002325
2326 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
2327 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
2328 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
2329 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
2330 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
2331 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
2332 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
2333 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002334#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002335 vcpu->regs[VCPU_REGS_R8] = regs->r8;
2336 vcpu->regs[VCPU_REGS_R9] = regs->r9;
2337 vcpu->regs[VCPU_REGS_R10] = regs->r10;
2338 vcpu->regs[VCPU_REGS_R11] = regs->r11;
2339 vcpu->regs[VCPU_REGS_R12] = regs->r12;
2340 vcpu->regs[VCPU_REGS_R13] = regs->r13;
2341 vcpu->regs[VCPU_REGS_R14] = regs->r14;
2342 vcpu->regs[VCPU_REGS_R15] = regs->r15;
2343#endif
2344
2345 vcpu->rip = regs->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002346 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002347
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002348 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002349
2350 vcpu_put(vcpu);
2351
2352 return 0;
2353}
2354
2355static void get_segment(struct kvm_vcpu *vcpu,
2356 struct kvm_segment *var, int seg)
2357{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002358 return kvm_x86_ops->get_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002359}
2360
Avi Kivitybccf2152007-02-21 18:04:26 +02002361static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2362 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002363{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002364 struct descriptor_table dt;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002365 int pending_vec;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002366
Avi Kivitybccf2152007-02-21 18:04:26 +02002367 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002368
2369 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2370 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2371 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2372 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2373 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2374 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2375
2376 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2377 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2378
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002379 kvm_x86_ops->get_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002380 sregs->idt.limit = dt.limit;
2381 sregs->idt.base = dt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002382 kvm_x86_ops->get_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002383 sregs->gdt.limit = dt.limit;
2384 sregs->gdt.base = dt.base;
2385
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002386 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002387 sregs->cr0 = vcpu->cr0;
2388 sregs->cr2 = vcpu->cr2;
2389 sregs->cr3 = vcpu->cr3;
2390 sregs->cr4 = vcpu->cr4;
Eddie Dong7017fc32007-07-18 11:34:57 +03002391 sregs->cr8 = get_cr8(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002392 sregs->efer = vcpu->shadow_efer;
Eddie Dong7017fc32007-07-18 11:34:57 +03002393 sregs->apic_base = kvm_get_apic_base(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002394
Eddie Dong2a8067f2007-08-06 16:29:07 +03002395 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc52fb352007-08-02 14:03:07 +03002396 memset(sregs->interrupt_bitmap, 0,
2397 sizeof sregs->interrupt_bitmap);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002398 pending_vec = kvm_x86_ops->get_irq(vcpu);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002399 if (pending_vec >= 0)
Mike Dayd77c26f2007-10-08 09:02:08 -04002400 set_bit(pending_vec,
2401 (unsigned long *)sregs->interrupt_bitmap);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002402 } else
He, Qingc52fb352007-08-02 14:03:07 +03002403 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2404 sizeof sregs->interrupt_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002405
2406 vcpu_put(vcpu);
2407
2408 return 0;
2409}
2410
2411static void set_segment(struct kvm_vcpu *vcpu,
2412 struct kvm_segment *var, int seg)
2413{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002414 return kvm_x86_ops->set_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002415}
2416
Avi Kivitybccf2152007-02-21 18:04:26 +02002417static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2418 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002419{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002420 int mmu_reset_needed = 0;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002421 int i, pending_vec, max_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002422 struct descriptor_table dt;
2423
Avi Kivitybccf2152007-02-21 18:04:26 +02002424 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002425
Avi Kivity6aa8b732006-12-10 02:21:36 -08002426 dt.limit = sregs->idt.limit;
2427 dt.base = sregs->idt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002428 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002429 dt.limit = sregs->gdt.limit;
2430 dt.base = sregs->gdt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002431 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002432
2433 vcpu->cr2 = sregs->cr2;
2434 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2435 vcpu->cr3 = sregs->cr3;
2436
Eddie Dong7017fc32007-07-18 11:34:57 +03002437 set_cr8(vcpu, sregs->cr8);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002438
2439 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002440#ifdef CONFIG_X86_64
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002441 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002442#endif
Eddie Dong7017fc32007-07-18 11:34:57 +03002443 kvm_set_apic_base(vcpu, sregs->apic_base);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002444
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002445 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity399badf2007-01-05 16:36:38 -08002446
Avi Kivity6aa8b732006-12-10 02:21:36 -08002447 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
Rusty Russell81f50e32007-09-06 01:20:38 +10002448 vcpu->cr0 = sregs->cr0;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002449 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002450
2451 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002452 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Avi Kivity1b0973b2007-01-05 16:36:41 -08002453 if (!is_long_mode(vcpu) && is_pae(vcpu))
2454 load_pdptrs(vcpu, vcpu->cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002455
2456 if (mmu_reset_needed)
2457 kvm_mmu_reset_context(vcpu);
2458
He, Qingc52fb352007-08-02 14:03:07 +03002459 if (!irqchip_in_kernel(vcpu->kvm)) {
2460 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2461 sizeof vcpu->irq_pending);
2462 vcpu->irq_summary = 0;
2463 for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
2464 if (vcpu->irq_pending[i])
2465 __set_bit(i, &vcpu->irq_summary);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002466 } else {
2467 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
2468 pending_vec = find_first_bit(
2469 (const unsigned long *)sregs->interrupt_bitmap,
2470 max_bits);
2471 /* Only pending external irq is handled here */
2472 if (pending_vec < max_bits) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002473 kvm_x86_ops->set_irq(vcpu, pending_vec);
Mike Dayd77c26f2007-10-08 09:02:08 -04002474 pr_debug("Set back pending irq %d\n",
2475 pending_vec);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002476 }
He, Qingc52fb352007-08-02 14:03:07 +03002477 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002478
Avi Kivity024aa1c2007-03-21 13:44:58 +02002479 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2480 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2481 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2482 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2483 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2484 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2485
2486 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2487 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2488
Avi Kivity6aa8b732006-12-10 02:21:36 -08002489 vcpu_put(vcpu);
2490
2491 return 0;
2492}
2493
Rusty Russell1747fb72007-09-06 01:21:32 +10002494void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2495{
2496 struct kvm_segment cs;
2497
2498 get_segment(vcpu, &cs, VCPU_SREG_CS);
2499 *db = cs.db;
2500 *l = cs.l;
2501}
2502EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2503
Avi Kivity6aa8b732006-12-10 02:21:36 -08002504/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002505 * Translate a guest virtual address to a guest physical address.
2506 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002507static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2508 struct kvm_translation *tr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002509{
2510 unsigned long vaddr = tr->linear_address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002511 gpa_t gpa;
2512
Avi Kivitybccf2152007-02-21 18:04:26 +02002513 vcpu_load(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +08002514 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002515 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2516 tr->physical_address = gpa;
2517 tr->valid = gpa != UNMAPPED_GVA;
2518 tr->writeable = 1;
2519 tr->usermode = 0;
Shaohua Li11ec2802007-07-23 14:51:37 +08002520 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002521 vcpu_put(vcpu);
2522
2523 return 0;
2524}
2525
Avi Kivitybccf2152007-02-21 18:04:26 +02002526static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2527 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002528{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002529 if (irq->irq < 0 || irq->irq >= 256)
2530 return -EINVAL;
Eddie Dong97222cc2007-09-12 10:58:04 +03002531 if (irqchip_in_kernel(vcpu->kvm))
2532 return -ENXIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02002533 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002534
2535 set_bit(irq->irq, vcpu->irq_pending);
2536 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2537
2538 vcpu_put(vcpu);
2539
2540 return 0;
2541}
2542
Avi Kivitybccf2152007-02-21 18:04:26 +02002543static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2544 struct kvm_debug_guest *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002545{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002546 int r;
2547
Avi Kivitybccf2152007-02-21 18:04:26 +02002548 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002549
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002550 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002551
2552 vcpu_put(vcpu);
2553
2554 return r;
2555}
2556
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002557static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2558 unsigned long address,
2559 int *type)
2560{
2561 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2562 unsigned long pgoff;
2563 struct page *page;
2564
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002565 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +02002566 if (pgoff == 0)
2567 page = virt_to_page(vcpu->run);
2568 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2569 page = virt_to_page(vcpu->pio_data);
2570 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002571 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002572 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002573 if (type != NULL)
2574 *type = VM_FAULT_MINOR;
2575
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002576 return page;
2577}
2578
2579static struct vm_operations_struct kvm_vcpu_vm_ops = {
2580 .nopage = kvm_vcpu_nopage,
2581};
2582
2583static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2584{
2585 vma->vm_ops = &kvm_vcpu_vm_ops;
2586 return 0;
2587}
2588
Avi Kivitybccf2152007-02-21 18:04:26 +02002589static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2590{
2591 struct kvm_vcpu *vcpu = filp->private_data;
2592
2593 fput(vcpu->kvm->filp);
2594 return 0;
2595}
2596
2597static struct file_operations kvm_vcpu_fops = {
2598 .release = kvm_vcpu_release,
2599 .unlocked_ioctl = kvm_vcpu_ioctl,
2600 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002601 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02002602};
2603
2604/*
2605 * Allocates an inode for the vcpu.
2606 */
2607static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2608{
2609 int fd, r;
2610 struct inode *inode;
2611 struct file *file;
2612
Avi Kivityd6d28162007-06-28 08:38:16 -04002613 r = anon_inode_getfd(&fd, &inode, &file,
2614 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2615 if (r)
2616 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002617 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +02002618 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02002619}
2620
Avi Kivityc5ea7662007-02-20 18:41:05 +02002621/*
2622 * Creates some virtual cpus. Good luck creating more than one.
2623 */
2624static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2625{
2626 int r;
2627 struct kvm_vcpu *vcpu;
2628
Avi Kivityc5ea7662007-02-20 18:41:05 +02002629 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002630 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002631
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002632 vcpu = kvm_x86_ops->vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002633 if (IS_ERR(vcpu))
2634 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002635
Avi Kivity15ad7142007-07-11 18:17:21 +03002636 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
2637
Rusty Russellb114b082007-07-30 21:13:43 +10002638 /* We do fxsave: this must be aligned. */
2639 BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF);
2640
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002641 vcpu_load(vcpu);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002642 r = kvm_x86_ops->vcpu_reset(vcpu);
2643 if (r == 0)
2644 r = kvm_mmu_setup(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002645 vcpu_put(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002646 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002647 goto free_vcpu;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002648
Shaohua Li11ec2802007-07-23 14:51:37 +08002649 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002650 if (kvm->vcpus[n]) {
2651 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +08002652 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002653 goto mmu_unload;
2654 }
2655 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08002656 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002657
2658 /* Now it's all set up, let userspace reach it */
Avi Kivitybccf2152007-02-21 18:04:26 +02002659 r = create_vcpu_fd(vcpu);
2660 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002661 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02002662 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002663
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002664unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08002665 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002666 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +08002667 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002668
2669mmu_unload:
2670 vcpu_load(vcpu);
2671 kvm_mmu_unload(vcpu);
2672 vcpu_put(vcpu);
2673
2674free_vcpu:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002675 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002676 return r;
2677}
2678
Avi Kivity1961d272007-03-05 19:46:05 +02002679static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2680{
2681 if (sigset) {
2682 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2683 vcpu->sigset_active = 1;
2684 vcpu->sigset = *sigset;
2685 } else
2686 vcpu->sigset_active = 0;
2687 return 0;
2688}
2689
Avi Kivityb8836732007-04-01 16:34:31 +03002690/*
2691 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2692 * we have asm/x86/processor.h
2693 */
2694struct fxsave {
2695 u16 cwd;
2696 u16 swd;
2697 u16 twd;
2698 u16 fop;
2699 u64 rip;
2700 u64 rdp;
2701 u32 mxcsr;
2702 u32 mxcsr_mask;
2703 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2704#ifdef CONFIG_X86_64
2705 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2706#else
2707 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2708#endif
2709};
2710
2711static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2712{
Rusty Russellb114b082007-07-30 21:13:43 +10002713 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002714
2715 vcpu_load(vcpu);
2716
2717 memcpy(fpu->fpr, fxsave->st_space, 128);
2718 fpu->fcw = fxsave->cwd;
2719 fpu->fsw = fxsave->swd;
2720 fpu->ftwx = fxsave->twd;
2721 fpu->last_opcode = fxsave->fop;
2722 fpu->last_ip = fxsave->rip;
2723 fpu->last_dp = fxsave->rdp;
2724 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2725
2726 vcpu_put(vcpu);
2727
2728 return 0;
2729}
2730
2731static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2732{
Rusty Russellb114b082007-07-30 21:13:43 +10002733 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002734
2735 vcpu_load(vcpu);
2736
2737 memcpy(fxsave->st_space, fpu->fpr, 128);
2738 fxsave->cwd = fpu->fcw;
2739 fxsave->swd = fpu->fsw;
2740 fxsave->twd = fpu->ftwx;
2741 fxsave->fop = fpu->last_opcode;
2742 fxsave->rip = fpu->last_ip;
2743 fxsave->rdp = fpu->last_dp;
2744 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2745
2746 vcpu_put(vcpu);
2747
2748 return 0;
2749}
2750
Avi Kivitybccf2152007-02-21 18:04:26 +02002751static long kvm_vcpu_ioctl(struct file *filp,
2752 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002753{
Avi Kivitybccf2152007-02-21 18:04:26 +02002754 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00002755 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02002756 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002757
2758 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002759 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002760 r = -EINVAL;
2761 if (arg)
2762 goto out;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002763 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002764 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002765 case KVM_GET_REGS: {
2766 struct kvm_regs kvm_regs;
2767
Avi Kivitybccf2152007-02-21 18:04:26 +02002768 memset(&kvm_regs, 0, sizeof kvm_regs);
2769 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002770 if (r)
2771 goto out;
2772 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002773 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002774 goto out;
2775 r = 0;
2776 break;
2777 }
2778 case KVM_SET_REGS: {
2779 struct kvm_regs kvm_regs;
2780
2781 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002782 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002783 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002784 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002785 if (r)
2786 goto out;
2787 r = 0;
2788 break;
2789 }
2790 case KVM_GET_SREGS: {
2791 struct kvm_sregs kvm_sregs;
2792
Avi Kivitybccf2152007-02-21 18:04:26 +02002793 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2794 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002795 if (r)
2796 goto out;
2797 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002798 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002799 goto out;
2800 r = 0;
2801 break;
2802 }
2803 case KVM_SET_SREGS: {
2804 struct kvm_sregs kvm_sregs;
2805
2806 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002807 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002808 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002809 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002810 if (r)
2811 goto out;
2812 r = 0;
2813 break;
2814 }
2815 case KVM_TRANSLATE: {
2816 struct kvm_translation tr;
2817
2818 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002819 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002820 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002821 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002822 if (r)
2823 goto out;
2824 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002825 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002826 goto out;
2827 r = 0;
2828 break;
2829 }
2830 case KVM_INTERRUPT: {
2831 struct kvm_interrupt irq;
2832
2833 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002834 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002835 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002836 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002837 if (r)
2838 goto out;
2839 r = 0;
2840 break;
2841 }
2842 case KVM_DEBUG_GUEST: {
2843 struct kvm_debug_guest dbg;
2844
2845 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002846 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002847 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002848 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002849 if (r)
2850 goto out;
2851 r = 0;
2852 break;
2853 }
Avi Kivity1961d272007-03-05 19:46:05 +02002854 case KVM_SET_SIGNAL_MASK: {
2855 struct kvm_signal_mask __user *sigmask_arg = argp;
2856 struct kvm_signal_mask kvm_sigmask;
2857 sigset_t sigset, *p;
2858
2859 p = NULL;
2860 if (argp) {
2861 r = -EFAULT;
2862 if (copy_from_user(&kvm_sigmask, argp,
2863 sizeof kvm_sigmask))
2864 goto out;
2865 r = -EINVAL;
2866 if (kvm_sigmask.len != sizeof sigset)
2867 goto out;
2868 r = -EFAULT;
2869 if (copy_from_user(&sigset, sigmask_arg->sigset,
2870 sizeof sigset))
2871 goto out;
2872 p = &sigset;
2873 }
2874 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2875 break;
2876 }
Avi Kivityb8836732007-04-01 16:34:31 +03002877 case KVM_GET_FPU: {
2878 struct kvm_fpu fpu;
2879
2880 memset(&fpu, 0, sizeof fpu);
2881 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2882 if (r)
2883 goto out;
2884 r = -EFAULT;
2885 if (copy_to_user(argp, &fpu, sizeof fpu))
2886 goto out;
2887 r = 0;
2888 break;
2889 }
2890 case KVM_SET_FPU: {
2891 struct kvm_fpu fpu;
2892
2893 r = -EFAULT;
2894 if (copy_from_user(&fpu, argp, sizeof fpu))
2895 goto out;
2896 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2897 if (r)
2898 goto out;
2899 r = 0;
2900 break;
2901 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002902 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02002903 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02002904 }
2905out:
2906 return r;
2907}
2908
2909static long kvm_vm_ioctl(struct file *filp,
2910 unsigned int ioctl, unsigned long arg)
2911{
2912 struct kvm *kvm = filp->private_data;
2913 void __user *argp = (void __user *)arg;
2914 int r = -EINVAL;
2915
2916 switch (ioctl) {
2917 case KVM_CREATE_VCPU:
2918 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2919 if (r < 0)
2920 goto out;
2921 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002922 case KVM_SET_MEMORY_REGION: {
2923 struct kvm_memory_region kvm_mem;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002924 struct kvm_userspace_memory_region kvm_userspace_mem;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002925
2926 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002927 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002928 goto out;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002929 kvm_userspace_mem.slot = kvm_mem.slot;
2930 kvm_userspace_mem.flags = kvm_mem.flags;
2931 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
2932 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
2933 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
2934 if (r)
2935 goto out;
2936 break;
2937 }
2938 case KVM_SET_USER_MEMORY_REGION: {
2939 struct kvm_userspace_memory_region kvm_userspace_mem;
2940
2941 r = -EFAULT;
2942 if (copy_from_user(&kvm_userspace_mem, argp,
2943 sizeof kvm_userspace_mem))
2944 goto out;
2945
2946 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002947 if (r)
2948 goto out;
2949 break;
2950 }
Izik Eidus82ce2c92007-10-02 18:52:55 +02002951 case KVM_SET_NR_MMU_PAGES:
2952 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
2953 if (r)
2954 goto out;
2955 break;
2956 case KVM_GET_NR_MMU_PAGES:
2957 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
2958 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002959 case KVM_GET_DIRTY_LOG: {
2960 struct kvm_dirty_log log;
2961
2962 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002963 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002964 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002965 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002966 if (r)
2967 goto out;
2968 break;
2969 }
Avi Kivitye8207542007-03-30 16:54:30 +03002970 case KVM_SET_MEMORY_ALIAS: {
2971 struct kvm_memory_alias alias;
2972
2973 r = -EFAULT;
2974 if (copy_from_user(&alias, argp, sizeof alias))
2975 goto out;
2976 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
2977 if (r)
2978 goto out;
2979 break;
2980 }
Eddie Dong85f455f2007-07-06 12:20:49 +03002981 case KVM_CREATE_IRQCHIP:
2982 r = -ENOMEM;
2983 kvm->vpic = kvm_create_pic(kvm);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03002984 if (kvm->vpic) {
2985 r = kvm_ioapic_init(kvm);
2986 if (r) {
2987 kfree(kvm->vpic);
2988 kvm->vpic = NULL;
2989 goto out;
2990 }
Mike Dayd77c26f2007-10-08 09:02:08 -04002991 } else
Eddie Dong85f455f2007-07-06 12:20:49 +03002992 goto out;
2993 break;
2994 case KVM_IRQ_LINE: {
2995 struct kvm_irq_level irq_event;
2996
2997 r = -EFAULT;
2998 if (copy_from_user(&irq_event, argp, sizeof irq_event))
2999 goto out;
3000 if (irqchip_in_kernel(kvm)) {
Eddie Dong9cf98822007-07-22 10:36:31 +03003001 mutex_lock(&kvm->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +03003002 if (irq_event.irq < 16)
3003 kvm_pic_set_irq(pic_irqchip(kvm),
3004 irq_event.irq,
3005 irq_event.level);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03003006 kvm_ioapic_set_irq(kvm->vioapic,
3007 irq_event.irq,
3008 irq_event.level);
Eddie Dong9cf98822007-07-22 10:36:31 +03003009 mutex_unlock(&kvm->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +03003010 r = 0;
3011 }
3012 break;
3013 }
He, Qing6ceb9d72007-07-26 11:05:18 +03003014 case KVM_GET_IRQCHIP: {
3015 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3016 struct kvm_irqchip chip;
3017
3018 r = -EFAULT;
3019 if (copy_from_user(&chip, argp, sizeof chip))
3020 goto out;
3021 r = -ENXIO;
3022 if (!irqchip_in_kernel(kvm))
3023 goto out;
3024 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
3025 if (r)
3026 goto out;
3027 r = -EFAULT;
3028 if (copy_to_user(argp, &chip, sizeof chip))
3029 goto out;
3030 r = 0;
3031 break;
3032 }
3033 case KVM_SET_IRQCHIP: {
3034 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3035 struct kvm_irqchip chip;
3036
3037 r = -EFAULT;
3038 if (copy_from_user(&chip, argp, sizeof chip))
3039 goto out;
3040 r = -ENXIO;
3041 if (!irqchip_in_kernel(kvm))
3042 goto out;
3043 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
3044 if (r)
3045 goto out;
3046 r = 0;
3047 break;
3048 }
Avi Kivityf17abe92007-02-21 19:28:04 +02003049 default:
3050 ;
3051 }
3052out:
3053 return r;
3054}
3055
3056static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
3057 unsigned long address,
3058 int *type)
3059{
3060 struct kvm *kvm = vma->vm_file->private_data;
3061 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02003062 struct page *page;
3063
Avi Kivityf17abe92007-02-21 19:28:04 +02003064 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity954bbbc2007-03-30 14:02:32 +03003065 page = gfn_to_page(kvm, pgoff);
Izik Eidus8a7ae052007-10-18 11:09:33 +02003066 if (is_error_page(page)) {
3067 kvm_release_page(page);
Avi Kivityf17abe92007-02-21 19:28:04 +02003068 return NOPAGE_SIGBUS;
Izik Eidus8a7ae052007-10-18 11:09:33 +02003069 }
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03003070 if (type != NULL)
3071 *type = VM_FAULT_MINOR;
3072
Avi Kivityf17abe92007-02-21 19:28:04 +02003073 return page;
3074}
3075
3076static struct vm_operations_struct kvm_vm_vm_ops = {
3077 .nopage = kvm_vm_nopage,
3078};
3079
3080static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
3081{
3082 vma->vm_ops = &kvm_vm_vm_ops;
3083 return 0;
3084}
3085
3086static struct file_operations kvm_vm_fops = {
3087 .release = kvm_vm_release,
3088 .unlocked_ioctl = kvm_vm_ioctl,
3089 .compat_ioctl = kvm_vm_ioctl,
3090 .mmap = kvm_vm_mmap,
3091};
3092
3093static int kvm_dev_ioctl_create_vm(void)
3094{
3095 int fd, r;
3096 struct inode *inode;
3097 struct file *file;
3098 struct kvm *kvm;
3099
Avi Kivityf17abe92007-02-21 19:28:04 +02003100 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04003101 if (IS_ERR(kvm))
3102 return PTR_ERR(kvm);
3103 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
3104 if (r) {
3105 kvm_destroy_vm(kvm);
3106 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02003107 }
3108
Avi Kivitybccf2152007-02-21 18:04:26 +02003109 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02003110
Avi Kivityf17abe92007-02-21 19:28:04 +02003111 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02003112}
3113
3114static long kvm_dev_ioctl(struct file *filp,
3115 unsigned int ioctl, unsigned long arg)
3116{
3117 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02003118 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02003119
3120 switch (ioctl) {
3121 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02003122 r = -EINVAL;
3123 if (arg)
3124 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02003125 r = KVM_API_VERSION;
3126 break;
3127 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02003128 r = -EINVAL;
3129 if (arg)
3130 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02003131 r = kvm_dev_ioctl_create_vm();
3132 break;
Eddie Dong85f455f2007-07-06 12:20:49 +03003133 case KVM_CHECK_EXTENSION: {
3134 int ext = (long)argp;
3135
3136 switch (ext) {
3137 case KVM_CAP_IRQCHIP:
Eddie Dongb6958ce2007-07-18 12:15:21 +03003138 case KVM_CAP_HLT:
Izik Eidus82ce2c92007-10-02 18:52:55 +02003139 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Izik Eidus6fc138d2007-10-09 19:20:39 +02003140 case KVM_CAP_USER_MEMORY:
Eddie Dong85f455f2007-07-06 12:20:49 +03003141 r = 1;
3142 break;
3143 default:
3144 r = 0;
3145 break;
3146 }
Avi Kivity5d308f42007-03-01 17:56:20 +02003147 break;
Eddie Dong85f455f2007-07-06 12:20:49 +03003148 }
Avi Kivity07c45a32007-03-07 13:05:38 +02003149 case KVM_GET_VCPU_MMAP_SIZE:
3150 r = -EINVAL;
3151 if (arg)
3152 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02003153 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02003154 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003155 default:
Carsten Otte043405e2007-10-10 17:16:19 +02003156 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003157 }
3158out:
3159 return r;
3160}
3161
Avi Kivity6aa8b732006-12-10 02:21:36 -08003162static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003163 .unlocked_ioctl = kvm_dev_ioctl,
3164 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003165};
3166
3167static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02003168 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003169 "kvm",
3170 &kvm_chardev_ops,
3171};
3172
Avi Kivity774c47f2007-02-12 00:54:47 -08003173/*
3174 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
3175 * cached on it.
3176 */
3177static void decache_vcpus_on_cpu(int cpu)
3178{
3179 struct kvm *vm;
3180 struct kvm_vcpu *vcpu;
3181 int i;
3182
3183 spin_lock(&kvm_lock);
Shaohua Li11ec2802007-07-23 14:51:37 +08003184 list_for_each_entry(vm, &vm_list, vm_list)
Avi Kivity774c47f2007-02-12 00:54:47 -08003185 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003186 vcpu = vm->vcpus[i];
3187 if (!vcpu)
3188 continue;
Avi Kivity774c47f2007-02-12 00:54:47 -08003189 /*
3190 * If the vcpu is locked, then it is running on some
3191 * other cpu and therefore it is not cached on the
3192 * cpu in question.
3193 *
3194 * If it's not locked, check the last cpu it executed
3195 * on.
3196 */
3197 if (mutex_trylock(&vcpu->mutex)) {
3198 if (vcpu->cpu == cpu) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003199 kvm_x86_ops->vcpu_decache(vcpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08003200 vcpu->cpu = -1;
3201 }
3202 mutex_unlock(&vcpu->mutex);
3203 }
3204 }
3205 spin_unlock(&kvm_lock);
3206}
3207
Avi Kivity1b6c0162007-05-24 13:03:52 +03003208static void hardware_enable(void *junk)
3209{
3210 int cpu = raw_smp_processor_id();
3211
3212 if (cpu_isset(cpu, cpus_hardware_enabled))
3213 return;
3214 cpu_set(cpu, cpus_hardware_enabled);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003215 kvm_x86_ops->hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003216}
3217
3218static void hardware_disable(void *junk)
3219{
3220 int cpu = raw_smp_processor_id();
3221
3222 if (!cpu_isset(cpu, cpus_hardware_enabled))
3223 return;
3224 cpu_clear(cpu, cpus_hardware_enabled);
3225 decache_vcpus_on_cpu(cpu);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003226 kvm_x86_ops->hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003227}
3228
Avi Kivity774c47f2007-02-12 00:54:47 -08003229static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
3230 void *v)
3231{
3232 int cpu = (long)v;
3233
3234 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03003235 case CPU_DYING:
3236 case CPU_DYING_FROZEN:
Avi Kivity6ec8a852007-08-19 15:57:26 +03003237 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3238 cpu);
3239 hardware_disable(NULL);
3240 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08003241 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003242 case CPU_UP_CANCELED_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003243 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3244 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003245 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003246 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02003247 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003248 case CPU_ONLINE_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003249 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
3250 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003251 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003252 break;
3253 }
3254 return NOTIFY_OK;
3255}
3256
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003257static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04003258 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003259{
3260 if (val == SYS_RESTART) {
3261 /*
3262 * Some (well, at least mine) BIOSes hang on reboot if
3263 * in vmx root mode.
3264 */
3265 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
3266 on_each_cpu(hardware_disable, NULL, 0, 1);
3267 }
3268 return NOTIFY_OK;
3269}
3270
3271static struct notifier_block kvm_reboot_notifier = {
3272 .notifier_call = kvm_reboot,
3273 .priority = 0,
3274};
3275
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04003276void kvm_io_bus_init(struct kvm_io_bus *bus)
3277{
3278 memset(bus, 0, sizeof(*bus));
3279}
3280
3281void kvm_io_bus_destroy(struct kvm_io_bus *bus)
3282{
3283 int i;
3284
3285 for (i = 0; i < bus->dev_count; i++) {
3286 struct kvm_io_device *pos = bus->devs[i];
3287
3288 kvm_iodevice_destructor(pos);
3289 }
3290}
3291
3292struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
3293{
3294 int i;
3295
3296 for (i = 0; i < bus->dev_count; i++) {
3297 struct kvm_io_device *pos = bus->devs[i];
3298
3299 if (pos->in_range(pos, addr))
3300 return pos;
3301 }
3302
3303 return NULL;
3304}
3305
3306void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
3307{
3308 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
3309
3310 bus->devs[bus->dev_count++] = dev;
3311}
3312
Avi Kivity774c47f2007-02-12 00:54:47 -08003313static struct notifier_block kvm_cpu_notifier = {
3314 .notifier_call = kvm_cpu_hotplug,
3315 .priority = 20, /* must be > scheduler priority */
3316};
3317
Avi Kivity1165f5f2007-04-19 17:27:43 +03003318static u64 stat_get(void *_offset)
3319{
3320 unsigned offset = (long)_offset;
3321 u64 total = 0;
3322 struct kvm *kvm;
3323 struct kvm_vcpu *vcpu;
3324 int i;
3325
3326 spin_lock(&kvm_lock);
3327 list_for_each_entry(kvm, &vm_list, vm_list)
3328 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003329 vcpu = kvm->vcpus[i];
3330 if (vcpu)
3331 total += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03003332 }
3333 spin_unlock(&kvm_lock);
3334 return total;
3335}
3336
Rusty Russell3dea7ca2007-08-01 10:12:22 +10003337DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n");
Avi Kivity1165f5f2007-04-19 17:27:43 +03003338
Avi Kivity6aa8b732006-12-10 02:21:36 -08003339static __init void kvm_init_debug(void)
3340{
3341 struct kvm_stats_debugfs_item *p;
3342
Al Viro8b6d44c2007-02-09 16:38:40 +00003343 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003344 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03003345 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
3346 (void *)(long)p->offset,
3347 &stat_fops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003348}
3349
3350static void kvm_exit_debug(void)
3351{
3352 struct kvm_stats_debugfs_item *p;
3353
3354 for (p = debugfs_entries; p->name; ++p)
3355 debugfs_remove(p->dentry);
3356 debugfs_remove(debugfs_dir);
3357}
3358
Avi Kivity59ae6c62007-02-12 00:54:48 -08003359static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3360{
Avi Kivity4267c412007-05-24 13:09:41 +03003361 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003362 return 0;
3363}
3364
3365static int kvm_resume(struct sys_device *dev)
3366{
Avi Kivity4267c412007-05-24 13:09:41 +03003367 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003368 return 0;
3369}
3370
3371static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01003372 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08003373 .suspend = kvm_suspend,
3374 .resume = kvm_resume,
3375};
3376
3377static struct sys_device kvm_sysdev = {
3378 .id = 0,
3379 .cls = &kvm_sysdev_class,
3380};
3381
Izik Eiduscea7bb22007-10-17 19:17:48 +02003382struct page *bad_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003383
Avi Kivity15ad7142007-07-11 18:17:21 +03003384static inline
3385struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
3386{
3387 return container_of(pn, struct kvm_vcpu, preempt_notifier);
3388}
3389
3390static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
3391{
3392 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3393
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003394 kvm_x86_ops->vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003395}
3396
3397static void kvm_sched_out(struct preempt_notifier *pn,
3398 struct task_struct *next)
3399{
3400 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3401
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003402 kvm_x86_ops->vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003403}
3404
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003405int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10003406 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003407{
3408 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003409 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003410
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003411 if (kvm_x86_ops) {
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08003412 printk(KERN_ERR "kvm: already loaded the other module\n");
3413 return -EEXIST;
3414 }
3415
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003416 if (!ops->cpu_has_kvm_support()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003417 printk(KERN_ERR "kvm: no hardware support\n");
3418 return -EOPNOTSUPP;
3419 }
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003420 if (ops->disabled_by_bios()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003421 printk(KERN_ERR "kvm: disabled by bios\n");
3422 return -EOPNOTSUPP;
3423 }
3424
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003425 kvm_x86_ops = ops;
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003426
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003427 r = kvm_x86_ops->hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003428 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02003429 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003430
Yang, Sheng002c7f72007-07-31 14:23:01 +03003431 for_each_online_cpu(cpu) {
3432 smp_call_function_single(cpu,
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003433 kvm_x86_ops->check_processor_compatibility,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003434 &r, 0, 1);
3435 if (r < 0)
3436 goto out_free_0;
3437 }
3438
Avi Kivity1b6c0162007-05-24 13:03:52 +03003439 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003440 r = register_cpu_notifier(&kvm_cpu_notifier);
3441 if (r)
3442 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003443 register_reboot_notifier(&kvm_reboot_notifier);
3444
Avi Kivity59ae6c62007-02-12 00:54:48 -08003445 r = sysdev_class_register(&kvm_sysdev_class);
3446 if (r)
3447 goto out_free_2;
3448
3449 r = sysdev_register(&kvm_sysdev);
3450 if (r)
3451 goto out_free_3;
3452
Rusty Russellc16f8622007-07-30 21:12:19 +10003453 /* A kmem cache lets us meet the alignment requirements of fx_save. */
3454 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
3455 __alignof__(struct kvm_vcpu), 0, 0);
3456 if (!kvm_vcpu_cache) {
3457 r = -ENOMEM;
3458 goto out_free_4;
3459 }
3460
Avi Kivity6aa8b732006-12-10 02:21:36 -08003461 kvm_chardev_ops.owner = module;
3462
3463 r = misc_register(&kvm_dev);
3464 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04003465 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08003466 goto out_free;
3467 }
3468
Avi Kivity15ad7142007-07-11 18:17:21 +03003469 kvm_preempt_ops.sched_in = kvm_sched_in;
3470 kvm_preempt_ops.sched_out = kvm_sched_out;
3471
Avi Kivityc7addb92007-09-16 18:58:32 +02003472 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
3473
3474 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003475
3476out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10003477 kmem_cache_destroy(kvm_vcpu_cache);
3478out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08003479 sysdev_unregister(&kvm_sysdev);
3480out_free_3:
3481 sysdev_class_unregister(&kvm_sysdev_class);
3482out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003483 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08003484 unregister_cpu_notifier(&kvm_cpu_notifier);
3485out_free_1:
Avi Kivity1b6c0162007-05-24 13:03:52 +03003486 on_each_cpu(hardware_disable, NULL, 0, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03003487out_free_0:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003488 kvm_x86_ops->hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02003489out:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003490 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003491 return r;
3492}
Mike Dayd77c26f2007-10-08 09:02:08 -04003493EXPORT_SYMBOL_GPL(kvm_init_x86);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003494
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003495void kvm_exit_x86(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003496{
3497 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10003498 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003499 sysdev_unregister(&kvm_sysdev);
3500 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003501 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003502 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003503 on_each_cpu(hardware_disable, NULL, 0, 1);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003504 kvm_x86_ops->hardware_unsetup();
3505 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003506}
Mike Dayd77c26f2007-10-08 09:02:08 -04003507EXPORT_SYMBOL_GPL(kvm_exit_x86);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003508
3509static __init int kvm_init(void)
3510{
Avi Kivity37e29d92007-02-20 14:07:37 +02003511 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003512
Avi Kivityb5a33a72007-04-15 16:31:09 +03003513 r = kvm_mmu_module_init();
3514 if (r)
3515 goto out4;
3516
Avi Kivity6aa8b732006-12-10 02:21:36 -08003517 kvm_init_debug();
3518
Carsten Otte043405e2007-10-10 17:16:19 +02003519 kvm_arch_init();
Michael Riepebf591b22006-12-22 01:05:36 -08003520
Izik Eiduscea7bb22007-10-17 19:17:48 +02003521 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
Mike Dayd77c26f2007-10-08 09:02:08 -04003522
3523 if (bad_page == NULL) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003524 r = -ENOMEM;
3525 goto out;
3526 }
3527
Avi Kivity58e690e2007-02-26 16:29:43 +02003528 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003529
3530out:
3531 kvm_exit_debug();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003532 kvm_mmu_module_exit();
3533out4:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003534 return r;
3535}
3536
3537static __exit void kvm_exit(void)
3538{
3539 kvm_exit_debug();
Izik Eiduscea7bb22007-10-17 19:17:48 +02003540 __free_page(bad_page);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003541 kvm_mmu_module_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003542}
3543
3544module_init(kvm_init)
3545module_exit(kvm_exit)