blob: 8f6c21d026567e3b96756c5b284d56ac33f98d3f [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 Russell707d92fa2007-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 Russell707d92fa2007-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 Russell707d92fa2007-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 Russell707d92fa2007-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 Russell707d92fa2007-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 Life5518812007-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);
2125 kvm_x86_ops->vcpu_reset(vcpu);
2126 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
2127 }
2128
2129preempted:
2130 if (vcpu->guest_debug.enabled)
2131 kvm_x86_ops->guest_debug_pre(vcpu);
2132
2133again:
2134 r = kvm_mmu_reload(vcpu);
2135 if (unlikely(r))
2136 goto out;
2137
Avi Kivityab6ef342007-10-16 16:23:22 +02002138 kvm_inject_pending_timer_irqs(vcpu);
2139
Avi Kivity04d2cc72007-09-10 18:10:54 +03002140 preempt_disable();
2141
2142 kvm_x86_ops->prepare_guest_switch(vcpu);
2143 kvm_load_guest_fpu(vcpu);
2144
2145 local_irq_disable();
2146
2147 if (signal_pending(current)) {
2148 local_irq_enable();
2149 preempt_enable();
2150 r = -EINTR;
2151 kvm_run->exit_reason = KVM_EXIT_INTR;
2152 ++vcpu->stat.signal_exits;
2153 goto out;
2154 }
2155
2156 if (irqchip_in_kernel(vcpu->kvm))
2157 kvm_x86_ops->inject_pending_irq(vcpu);
2158 else if (!vcpu->mmio_read_completed)
2159 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2160
2161 vcpu->guest_mode = 1;
Laurent Vivierd172fcd2007-10-15 17:00:19 +02002162 kvm_guest_enter();
Avi Kivity04d2cc72007-09-10 18:10:54 +03002163
2164 if (vcpu->requests)
Avi Kivity3176bc32007-10-16 17:22:08 +02002165 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivity04d2cc72007-09-10 18:10:54 +03002166 kvm_x86_ops->tlb_flush(vcpu);
2167
2168 kvm_x86_ops->run(vcpu, kvm_run);
2169
2170 vcpu->guest_mode = 0;
2171 local_irq_enable();
2172
2173 ++vcpu->stat.exits;
2174
Laurent Vivier0552f732007-10-18 15:19:01 +02002175 /*
2176 * We must have an instruction between local_irq_enable() and
2177 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2178 * the interrupt shadow. The stat.exits increment will do nicely.
2179 * But we need to prevent reordering, hence this barrier():
2180 */
2181 barrier();
2182
2183 kvm_guest_exit();
2184
Avi Kivity04d2cc72007-09-10 18:10:54 +03002185 preempt_enable();
2186
2187 /*
2188 * Profile KVM exit RIPs:
2189 */
2190 if (unlikely(prof_on == KVM_PROFILING)) {
2191 kvm_x86_ops->cache_regs(vcpu);
2192 profile_hit(KVM_PROFILING, (void *)vcpu->rip);
2193 }
2194
2195 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2196
2197 if (r > 0) {
2198 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2199 r = -EINTR;
2200 kvm_run->exit_reason = KVM_EXIT_INTR;
2201 ++vcpu->stat.request_irq_exits;
2202 goto out;
2203 }
2204 if (!need_resched()) {
2205 ++vcpu->stat.light_exits;
2206 goto again;
2207 }
2208 }
2209
2210out:
2211 if (r > 0) {
2212 kvm_resched(vcpu);
2213 goto preempted;
2214 }
2215
2216 post_kvm_run_save(vcpu, kvm_run);
2217
2218 return r;
2219}
2220
2221
Avi Kivitybccf2152007-02-21 18:04:26 +02002222static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002223{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002224 int r;
Avi Kivity1961d272007-03-05 19:46:05 +02002225 sigset_t sigsaved;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002226
Avi Kivitybccf2152007-02-21 18:04:26 +02002227 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002228
He, Qingc5ec1532007-09-03 17:07:41 +03002229 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
2230 kvm_vcpu_block(vcpu);
2231 vcpu_put(vcpu);
2232 return -EAGAIN;
2233 }
2234
Avi Kivity1961d272007-03-05 19:46:05 +02002235 if (vcpu->sigset_active)
2236 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2237
Dor Laor54810342007-02-12 00:54:39 -08002238 /* re-sync apic's tpr */
He, Qing5cd4f6f2007-08-30 17:04:26 +08002239 if (!irqchip_in_kernel(vcpu->kvm))
2240 set_cr8(vcpu, kvm_run->cr8);
Dor Laor54810342007-02-12 00:54:39 -08002241
Avi Kivity02c83202007-04-29 15:02:17 +03002242 if (vcpu->pio.cur_count) {
2243 r = complete_pio(vcpu);
2244 if (r)
2245 goto out;
2246 }
2247
2248 if (vcpu->mmio_needed) {
2249 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
2250 vcpu->mmio_read_completed = 1;
2251 vcpu->mmio_needed = 0;
2252 r = emulate_instruction(vcpu, kvm_run,
Laurent Vivier34273182007-09-18 11:27:37 +02002253 vcpu->mmio_fault_cr2, 0, 1);
Avi Kivity02c83202007-04-29 15:02:17 +03002254 if (r == EMULATE_DO_MMIO) {
2255 /*
2256 * Read-modify-write. Back to userspace.
2257 */
Avi Kivity02c83202007-04-29 15:02:17 +03002258 r = 0;
2259 goto out;
Avi Kivity46fc1472007-02-22 19:39:30 +02002260 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002261 }
2262
Avi Kivity8eb7d332007-03-04 14:17:08 +02002263 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002264 kvm_x86_ops->cache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002265 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002266 kvm_x86_ops->decache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002267 }
2268
Avi Kivity04d2cc72007-09-10 18:10:54 +03002269 r = __vcpu_run(vcpu, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002270
Avi Kivity039576c2007-03-20 12:46:50 +02002271out:
Avi Kivity1961d272007-03-05 19:46:05 +02002272 if (vcpu->sigset_active)
2273 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2274
Avi Kivity6aa8b732006-12-10 02:21:36 -08002275 vcpu_put(vcpu);
2276 return r;
2277}
2278
Avi Kivitybccf2152007-02-21 18:04:26 +02002279static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
2280 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002281{
Avi Kivitybccf2152007-02-21 18:04:26 +02002282 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002283
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002284 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002285
2286 regs->rax = vcpu->regs[VCPU_REGS_RAX];
2287 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
2288 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
2289 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
2290 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
2291 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
2292 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
2293 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002294#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002295 regs->r8 = vcpu->regs[VCPU_REGS_R8];
2296 regs->r9 = vcpu->regs[VCPU_REGS_R9];
2297 regs->r10 = vcpu->regs[VCPU_REGS_R10];
2298 regs->r11 = vcpu->regs[VCPU_REGS_R11];
2299 regs->r12 = vcpu->regs[VCPU_REGS_R12];
2300 regs->r13 = vcpu->regs[VCPU_REGS_R13];
2301 regs->r14 = vcpu->regs[VCPU_REGS_R14];
2302 regs->r15 = vcpu->regs[VCPU_REGS_R15];
2303#endif
2304
2305 regs->rip = vcpu->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002306 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002307
2308 /*
2309 * Don't leak debug flags in case they were set for guest debugging
2310 */
2311 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2312 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2313
2314 vcpu_put(vcpu);
2315
2316 return 0;
2317}
2318
Avi Kivitybccf2152007-02-21 18:04:26 +02002319static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
2320 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321{
Avi Kivitybccf2152007-02-21 18:04:26 +02002322 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002323
2324 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
2325 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
2326 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
2327 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
2328 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
2329 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
2330 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
2331 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002332#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333 vcpu->regs[VCPU_REGS_R8] = regs->r8;
2334 vcpu->regs[VCPU_REGS_R9] = regs->r9;
2335 vcpu->regs[VCPU_REGS_R10] = regs->r10;
2336 vcpu->regs[VCPU_REGS_R11] = regs->r11;
2337 vcpu->regs[VCPU_REGS_R12] = regs->r12;
2338 vcpu->regs[VCPU_REGS_R13] = regs->r13;
2339 vcpu->regs[VCPU_REGS_R14] = regs->r14;
2340 vcpu->regs[VCPU_REGS_R15] = regs->r15;
2341#endif
2342
2343 vcpu->rip = regs->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002344 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002345
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002346 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002347
2348 vcpu_put(vcpu);
2349
2350 return 0;
2351}
2352
2353static void get_segment(struct kvm_vcpu *vcpu,
2354 struct kvm_segment *var, int seg)
2355{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002356 return kvm_x86_ops->get_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002357}
2358
Avi Kivitybccf2152007-02-21 18:04:26 +02002359static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2360 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002361{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002362 struct descriptor_table dt;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002363 int pending_vec;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002364
Avi Kivitybccf2152007-02-21 18:04:26 +02002365 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002366
2367 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2368 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2369 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2370 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2371 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2372 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2373
2374 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2375 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2376
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002377 kvm_x86_ops->get_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002378 sregs->idt.limit = dt.limit;
2379 sregs->idt.base = dt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002380 kvm_x86_ops->get_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002381 sregs->gdt.limit = dt.limit;
2382 sregs->gdt.base = dt.base;
2383
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002384 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002385 sregs->cr0 = vcpu->cr0;
2386 sregs->cr2 = vcpu->cr2;
2387 sregs->cr3 = vcpu->cr3;
2388 sregs->cr4 = vcpu->cr4;
Eddie Dong7017fc32007-07-18 11:34:57 +03002389 sregs->cr8 = get_cr8(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002390 sregs->efer = vcpu->shadow_efer;
Eddie Dong7017fc32007-07-18 11:34:57 +03002391 sregs->apic_base = kvm_get_apic_base(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002392
Eddie Dong2a8067f2007-08-06 16:29:07 +03002393 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc52fb352007-08-02 14:03:07 +03002394 memset(sregs->interrupt_bitmap, 0,
2395 sizeof sregs->interrupt_bitmap);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002396 pending_vec = kvm_x86_ops->get_irq(vcpu);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002397 if (pending_vec >= 0)
Mike Dayd77c26f2007-10-08 09:02:08 -04002398 set_bit(pending_vec,
2399 (unsigned long *)sregs->interrupt_bitmap);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002400 } else
He, Qingc52fb352007-08-02 14:03:07 +03002401 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2402 sizeof sregs->interrupt_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002403
2404 vcpu_put(vcpu);
2405
2406 return 0;
2407}
2408
2409static void set_segment(struct kvm_vcpu *vcpu,
2410 struct kvm_segment *var, int seg)
2411{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002412 return kvm_x86_ops->set_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002413}
2414
Avi Kivitybccf2152007-02-21 18:04:26 +02002415static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2416 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002417{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002418 int mmu_reset_needed = 0;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002419 int i, pending_vec, max_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002420 struct descriptor_table dt;
2421
Avi Kivitybccf2152007-02-21 18:04:26 +02002422 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002423
Avi Kivity6aa8b732006-12-10 02:21:36 -08002424 dt.limit = sregs->idt.limit;
2425 dt.base = sregs->idt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002426 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002427 dt.limit = sregs->gdt.limit;
2428 dt.base = sregs->gdt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002429 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002430
2431 vcpu->cr2 = sregs->cr2;
2432 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2433 vcpu->cr3 = sregs->cr3;
2434
Eddie Dong7017fc32007-07-18 11:34:57 +03002435 set_cr8(vcpu, sregs->cr8);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002436
2437 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002438#ifdef CONFIG_X86_64
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002439 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002440#endif
Eddie Dong7017fc32007-07-18 11:34:57 +03002441 kvm_set_apic_base(vcpu, sregs->apic_base);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002442
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002443 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity399badf2007-01-05 16:36:38 -08002444
Avi Kivity6aa8b732006-12-10 02:21:36 -08002445 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
Rusty Russell81f50e32007-09-06 01:20:38 +10002446 vcpu->cr0 = sregs->cr0;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002447 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002448
2449 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002450 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Avi Kivity1b0973b2007-01-05 16:36:41 -08002451 if (!is_long_mode(vcpu) && is_pae(vcpu))
2452 load_pdptrs(vcpu, vcpu->cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002453
2454 if (mmu_reset_needed)
2455 kvm_mmu_reset_context(vcpu);
2456
He, Qingc52fb352007-08-02 14:03:07 +03002457 if (!irqchip_in_kernel(vcpu->kvm)) {
2458 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2459 sizeof vcpu->irq_pending);
2460 vcpu->irq_summary = 0;
2461 for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
2462 if (vcpu->irq_pending[i])
2463 __set_bit(i, &vcpu->irq_summary);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002464 } else {
2465 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
2466 pending_vec = find_first_bit(
2467 (const unsigned long *)sregs->interrupt_bitmap,
2468 max_bits);
2469 /* Only pending external irq is handled here */
2470 if (pending_vec < max_bits) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002471 kvm_x86_ops->set_irq(vcpu, pending_vec);
Mike Dayd77c26f2007-10-08 09:02:08 -04002472 pr_debug("Set back pending irq %d\n",
2473 pending_vec);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002474 }
He, Qingc52fb352007-08-02 14:03:07 +03002475 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002476
Avi Kivity024aa1c2007-03-21 13:44:58 +02002477 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2478 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2479 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2480 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2481 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2482 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2483
2484 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2485 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2486
Avi Kivity6aa8b732006-12-10 02:21:36 -08002487 vcpu_put(vcpu);
2488
2489 return 0;
2490}
2491
Rusty Russell1747fb72007-09-06 01:21:32 +10002492void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2493{
2494 struct kvm_segment cs;
2495
2496 get_segment(vcpu, &cs, VCPU_SREG_CS);
2497 *db = cs.db;
2498 *l = cs.l;
2499}
2500EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2501
Avi Kivity6aa8b732006-12-10 02:21:36 -08002502/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002503 * Translate a guest virtual address to a guest physical address.
2504 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002505static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2506 struct kvm_translation *tr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002507{
2508 unsigned long vaddr = tr->linear_address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002509 gpa_t gpa;
2510
Avi Kivitybccf2152007-02-21 18:04:26 +02002511 vcpu_load(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +08002512 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002513 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2514 tr->physical_address = gpa;
2515 tr->valid = gpa != UNMAPPED_GVA;
2516 tr->writeable = 1;
2517 tr->usermode = 0;
Shaohua Li11ec2802007-07-23 14:51:37 +08002518 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002519 vcpu_put(vcpu);
2520
2521 return 0;
2522}
2523
Avi Kivitybccf2152007-02-21 18:04:26 +02002524static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2525 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002526{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002527 if (irq->irq < 0 || irq->irq >= 256)
2528 return -EINVAL;
Eddie Dong97222cc2007-09-12 10:58:04 +03002529 if (irqchip_in_kernel(vcpu->kvm))
2530 return -ENXIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02002531 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002532
2533 set_bit(irq->irq, vcpu->irq_pending);
2534 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2535
2536 vcpu_put(vcpu);
2537
2538 return 0;
2539}
2540
Avi Kivitybccf2152007-02-21 18:04:26 +02002541static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2542 struct kvm_debug_guest *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002543{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002544 int r;
2545
Avi Kivitybccf2152007-02-21 18:04:26 +02002546 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002547
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002548 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002549
2550 vcpu_put(vcpu);
2551
2552 return r;
2553}
2554
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002555static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2556 unsigned long address,
2557 int *type)
2558{
2559 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2560 unsigned long pgoff;
2561 struct page *page;
2562
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002563 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +02002564 if (pgoff == 0)
2565 page = virt_to_page(vcpu->run);
2566 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2567 page = virt_to_page(vcpu->pio_data);
2568 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002569 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002570 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002571 if (type != NULL)
2572 *type = VM_FAULT_MINOR;
2573
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002574 return page;
2575}
2576
2577static struct vm_operations_struct kvm_vcpu_vm_ops = {
2578 .nopage = kvm_vcpu_nopage,
2579};
2580
2581static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2582{
2583 vma->vm_ops = &kvm_vcpu_vm_ops;
2584 return 0;
2585}
2586
Avi Kivitybccf2152007-02-21 18:04:26 +02002587static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2588{
2589 struct kvm_vcpu *vcpu = filp->private_data;
2590
2591 fput(vcpu->kvm->filp);
2592 return 0;
2593}
2594
2595static struct file_operations kvm_vcpu_fops = {
2596 .release = kvm_vcpu_release,
2597 .unlocked_ioctl = kvm_vcpu_ioctl,
2598 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002599 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02002600};
2601
2602/*
2603 * Allocates an inode for the vcpu.
2604 */
2605static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2606{
2607 int fd, r;
2608 struct inode *inode;
2609 struct file *file;
2610
Avi Kivityd6d28162007-06-28 08:38:16 -04002611 r = anon_inode_getfd(&fd, &inode, &file,
2612 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2613 if (r)
2614 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002615 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +02002616 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02002617}
2618
Avi Kivityc5ea7662007-02-20 18:41:05 +02002619/*
2620 * Creates some virtual cpus. Good luck creating more than one.
2621 */
2622static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2623{
2624 int r;
2625 struct kvm_vcpu *vcpu;
2626
Avi Kivityc5ea7662007-02-20 18:41:05 +02002627 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002628 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002629
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002630 vcpu = kvm_x86_ops->vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002631 if (IS_ERR(vcpu))
2632 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002633
Avi Kivity15ad7142007-07-11 18:17:21 +03002634 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
2635
Rusty Russellb114b082007-07-30 21:13:43 +10002636 /* We do fxsave: this must be aligned. */
2637 BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF);
2638
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002639 vcpu_load(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002640 r = kvm_mmu_setup(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002641 vcpu_put(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002642 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002643 goto free_vcpu;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002644
Shaohua Li11ec2802007-07-23 14:51:37 +08002645 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002646 if (kvm->vcpus[n]) {
2647 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +08002648 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002649 goto mmu_unload;
2650 }
2651 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08002652 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002653
2654 /* Now it's all set up, let userspace reach it */
Avi Kivitybccf2152007-02-21 18:04:26 +02002655 r = create_vcpu_fd(vcpu);
2656 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002657 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02002658 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002659
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002660unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08002661 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002662 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +08002663 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002664
2665mmu_unload:
2666 vcpu_load(vcpu);
2667 kvm_mmu_unload(vcpu);
2668 vcpu_put(vcpu);
2669
2670free_vcpu:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002671 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002672 return r;
2673}
2674
Avi Kivity1961d272007-03-05 19:46:05 +02002675static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2676{
2677 if (sigset) {
2678 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2679 vcpu->sigset_active = 1;
2680 vcpu->sigset = *sigset;
2681 } else
2682 vcpu->sigset_active = 0;
2683 return 0;
2684}
2685
Avi Kivityb8836732007-04-01 16:34:31 +03002686/*
2687 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2688 * we have asm/x86/processor.h
2689 */
2690struct fxsave {
2691 u16 cwd;
2692 u16 swd;
2693 u16 twd;
2694 u16 fop;
2695 u64 rip;
2696 u64 rdp;
2697 u32 mxcsr;
2698 u32 mxcsr_mask;
2699 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2700#ifdef CONFIG_X86_64
2701 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2702#else
2703 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2704#endif
2705};
2706
2707static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2708{
Rusty Russellb114b082007-07-30 21:13:43 +10002709 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002710
2711 vcpu_load(vcpu);
2712
2713 memcpy(fpu->fpr, fxsave->st_space, 128);
2714 fpu->fcw = fxsave->cwd;
2715 fpu->fsw = fxsave->swd;
2716 fpu->ftwx = fxsave->twd;
2717 fpu->last_opcode = fxsave->fop;
2718 fpu->last_ip = fxsave->rip;
2719 fpu->last_dp = fxsave->rdp;
2720 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2721
2722 vcpu_put(vcpu);
2723
2724 return 0;
2725}
2726
2727static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2728{
Rusty Russellb114b082007-07-30 21:13:43 +10002729 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002730
2731 vcpu_load(vcpu);
2732
2733 memcpy(fxsave->st_space, fpu->fpr, 128);
2734 fxsave->cwd = fpu->fcw;
2735 fxsave->swd = fpu->fsw;
2736 fxsave->twd = fpu->ftwx;
2737 fxsave->fop = fpu->last_opcode;
2738 fxsave->rip = fpu->last_ip;
2739 fxsave->rdp = fpu->last_dp;
2740 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2741
2742 vcpu_put(vcpu);
2743
2744 return 0;
2745}
2746
Avi Kivitybccf2152007-02-21 18:04:26 +02002747static long kvm_vcpu_ioctl(struct file *filp,
2748 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002749{
Avi Kivitybccf2152007-02-21 18:04:26 +02002750 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00002751 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02002752 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002753
2754 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002755 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002756 r = -EINVAL;
2757 if (arg)
2758 goto out;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002759 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002760 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002761 case KVM_GET_REGS: {
2762 struct kvm_regs kvm_regs;
2763
Avi Kivitybccf2152007-02-21 18:04:26 +02002764 memset(&kvm_regs, 0, sizeof kvm_regs);
2765 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002766 if (r)
2767 goto out;
2768 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002769 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002770 goto out;
2771 r = 0;
2772 break;
2773 }
2774 case KVM_SET_REGS: {
2775 struct kvm_regs kvm_regs;
2776
2777 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002778 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002779 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002780 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002781 if (r)
2782 goto out;
2783 r = 0;
2784 break;
2785 }
2786 case KVM_GET_SREGS: {
2787 struct kvm_sregs kvm_sregs;
2788
Avi Kivitybccf2152007-02-21 18:04:26 +02002789 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2790 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002791 if (r)
2792 goto out;
2793 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002794 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002795 goto out;
2796 r = 0;
2797 break;
2798 }
2799 case KVM_SET_SREGS: {
2800 struct kvm_sregs kvm_sregs;
2801
2802 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002803 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002804 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002805 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002806 if (r)
2807 goto out;
2808 r = 0;
2809 break;
2810 }
2811 case KVM_TRANSLATE: {
2812 struct kvm_translation tr;
2813
2814 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002815 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002816 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002817 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002818 if (r)
2819 goto out;
2820 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002821 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002822 goto out;
2823 r = 0;
2824 break;
2825 }
2826 case KVM_INTERRUPT: {
2827 struct kvm_interrupt irq;
2828
2829 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002830 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002831 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002832 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002833 if (r)
2834 goto out;
2835 r = 0;
2836 break;
2837 }
2838 case KVM_DEBUG_GUEST: {
2839 struct kvm_debug_guest dbg;
2840
2841 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002842 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002843 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002844 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002845 if (r)
2846 goto out;
2847 r = 0;
2848 break;
2849 }
Avi Kivity1961d272007-03-05 19:46:05 +02002850 case KVM_SET_SIGNAL_MASK: {
2851 struct kvm_signal_mask __user *sigmask_arg = argp;
2852 struct kvm_signal_mask kvm_sigmask;
2853 sigset_t sigset, *p;
2854
2855 p = NULL;
2856 if (argp) {
2857 r = -EFAULT;
2858 if (copy_from_user(&kvm_sigmask, argp,
2859 sizeof kvm_sigmask))
2860 goto out;
2861 r = -EINVAL;
2862 if (kvm_sigmask.len != sizeof sigset)
2863 goto out;
2864 r = -EFAULT;
2865 if (copy_from_user(&sigset, sigmask_arg->sigset,
2866 sizeof sigset))
2867 goto out;
2868 p = &sigset;
2869 }
2870 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2871 break;
2872 }
Avi Kivityb8836732007-04-01 16:34:31 +03002873 case KVM_GET_FPU: {
2874 struct kvm_fpu fpu;
2875
2876 memset(&fpu, 0, sizeof fpu);
2877 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2878 if (r)
2879 goto out;
2880 r = -EFAULT;
2881 if (copy_to_user(argp, &fpu, sizeof fpu))
2882 goto out;
2883 r = 0;
2884 break;
2885 }
2886 case KVM_SET_FPU: {
2887 struct kvm_fpu fpu;
2888
2889 r = -EFAULT;
2890 if (copy_from_user(&fpu, argp, sizeof fpu))
2891 goto out;
2892 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2893 if (r)
2894 goto out;
2895 r = 0;
2896 break;
2897 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002898 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02002899 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02002900 }
2901out:
2902 return r;
2903}
2904
2905static long kvm_vm_ioctl(struct file *filp,
2906 unsigned int ioctl, unsigned long arg)
2907{
2908 struct kvm *kvm = filp->private_data;
2909 void __user *argp = (void __user *)arg;
2910 int r = -EINVAL;
2911
2912 switch (ioctl) {
2913 case KVM_CREATE_VCPU:
2914 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2915 if (r < 0)
2916 goto out;
2917 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002918 case KVM_SET_MEMORY_REGION: {
2919 struct kvm_memory_region kvm_mem;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002920 struct kvm_userspace_memory_region kvm_userspace_mem;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002921
2922 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002923 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002924 goto out;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002925 kvm_userspace_mem.slot = kvm_mem.slot;
2926 kvm_userspace_mem.flags = kvm_mem.flags;
2927 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
2928 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
2929 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
2930 if (r)
2931 goto out;
2932 break;
2933 }
2934 case KVM_SET_USER_MEMORY_REGION: {
2935 struct kvm_userspace_memory_region kvm_userspace_mem;
2936
2937 r = -EFAULT;
2938 if (copy_from_user(&kvm_userspace_mem, argp,
2939 sizeof kvm_userspace_mem))
2940 goto out;
2941
2942 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002943 if (r)
2944 goto out;
2945 break;
2946 }
Izik Eidus82ce2c92007-10-02 18:52:55 +02002947 case KVM_SET_NR_MMU_PAGES:
2948 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
2949 if (r)
2950 goto out;
2951 break;
2952 case KVM_GET_NR_MMU_PAGES:
2953 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
2954 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002955 case KVM_GET_DIRTY_LOG: {
2956 struct kvm_dirty_log log;
2957
2958 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002959 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002960 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002961 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002962 if (r)
2963 goto out;
2964 break;
2965 }
Avi Kivitye8207542007-03-30 16:54:30 +03002966 case KVM_SET_MEMORY_ALIAS: {
2967 struct kvm_memory_alias alias;
2968
2969 r = -EFAULT;
2970 if (copy_from_user(&alias, argp, sizeof alias))
2971 goto out;
2972 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
2973 if (r)
2974 goto out;
2975 break;
2976 }
Eddie Dong85f455f2007-07-06 12:20:49 +03002977 case KVM_CREATE_IRQCHIP:
2978 r = -ENOMEM;
2979 kvm->vpic = kvm_create_pic(kvm);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03002980 if (kvm->vpic) {
2981 r = kvm_ioapic_init(kvm);
2982 if (r) {
2983 kfree(kvm->vpic);
2984 kvm->vpic = NULL;
2985 goto out;
2986 }
Mike Dayd77c26f2007-10-08 09:02:08 -04002987 } else
Eddie Dong85f455f2007-07-06 12:20:49 +03002988 goto out;
2989 break;
2990 case KVM_IRQ_LINE: {
2991 struct kvm_irq_level irq_event;
2992
2993 r = -EFAULT;
2994 if (copy_from_user(&irq_event, argp, sizeof irq_event))
2995 goto out;
2996 if (irqchip_in_kernel(kvm)) {
Eddie Dong9cf98822007-07-22 10:36:31 +03002997 mutex_lock(&kvm->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +03002998 if (irq_event.irq < 16)
2999 kvm_pic_set_irq(pic_irqchip(kvm),
3000 irq_event.irq,
3001 irq_event.level);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03003002 kvm_ioapic_set_irq(kvm->vioapic,
3003 irq_event.irq,
3004 irq_event.level);
Eddie Dong9cf98822007-07-22 10:36:31 +03003005 mutex_unlock(&kvm->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +03003006 r = 0;
3007 }
3008 break;
3009 }
He, Qing6ceb9d72007-07-26 11:05:18 +03003010 case KVM_GET_IRQCHIP: {
3011 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3012 struct kvm_irqchip chip;
3013
3014 r = -EFAULT;
3015 if (copy_from_user(&chip, argp, sizeof chip))
3016 goto out;
3017 r = -ENXIO;
3018 if (!irqchip_in_kernel(kvm))
3019 goto out;
3020 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
3021 if (r)
3022 goto out;
3023 r = -EFAULT;
3024 if (copy_to_user(argp, &chip, sizeof chip))
3025 goto out;
3026 r = 0;
3027 break;
3028 }
3029 case KVM_SET_IRQCHIP: {
3030 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3031 struct kvm_irqchip chip;
3032
3033 r = -EFAULT;
3034 if (copy_from_user(&chip, argp, sizeof chip))
3035 goto out;
3036 r = -ENXIO;
3037 if (!irqchip_in_kernel(kvm))
3038 goto out;
3039 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
3040 if (r)
3041 goto out;
3042 r = 0;
3043 break;
3044 }
Avi Kivityf17abe92007-02-21 19:28:04 +02003045 default:
3046 ;
3047 }
3048out:
3049 return r;
3050}
3051
3052static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
3053 unsigned long address,
3054 int *type)
3055{
3056 struct kvm *kvm = vma->vm_file->private_data;
3057 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02003058 struct page *page;
3059
Avi Kivityf17abe92007-02-21 19:28:04 +02003060 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity954bbbc2007-03-30 14:02:32 +03003061 page = gfn_to_page(kvm, pgoff);
Izik Eidus8a7ae052007-10-18 11:09:33 +02003062 if (is_error_page(page)) {
3063 kvm_release_page(page);
Avi Kivityf17abe92007-02-21 19:28:04 +02003064 return NOPAGE_SIGBUS;
Izik Eidus8a7ae052007-10-18 11:09:33 +02003065 }
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03003066 if (type != NULL)
3067 *type = VM_FAULT_MINOR;
3068
Avi Kivityf17abe92007-02-21 19:28:04 +02003069 return page;
3070}
3071
3072static struct vm_operations_struct kvm_vm_vm_ops = {
3073 .nopage = kvm_vm_nopage,
3074};
3075
3076static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
3077{
3078 vma->vm_ops = &kvm_vm_vm_ops;
3079 return 0;
3080}
3081
3082static struct file_operations kvm_vm_fops = {
3083 .release = kvm_vm_release,
3084 .unlocked_ioctl = kvm_vm_ioctl,
3085 .compat_ioctl = kvm_vm_ioctl,
3086 .mmap = kvm_vm_mmap,
3087};
3088
3089static int kvm_dev_ioctl_create_vm(void)
3090{
3091 int fd, r;
3092 struct inode *inode;
3093 struct file *file;
3094 struct kvm *kvm;
3095
Avi Kivityf17abe92007-02-21 19:28:04 +02003096 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04003097 if (IS_ERR(kvm))
3098 return PTR_ERR(kvm);
3099 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
3100 if (r) {
3101 kvm_destroy_vm(kvm);
3102 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02003103 }
3104
Avi Kivitybccf2152007-02-21 18:04:26 +02003105 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02003106
Avi Kivityf17abe92007-02-21 19:28:04 +02003107 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02003108}
3109
3110static long kvm_dev_ioctl(struct file *filp,
3111 unsigned int ioctl, unsigned long arg)
3112{
3113 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02003114 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02003115
3116 switch (ioctl) {
3117 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02003118 r = -EINVAL;
3119 if (arg)
3120 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02003121 r = KVM_API_VERSION;
3122 break;
3123 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02003124 r = -EINVAL;
3125 if (arg)
3126 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02003127 r = kvm_dev_ioctl_create_vm();
3128 break;
Eddie Dong85f455f2007-07-06 12:20:49 +03003129 case KVM_CHECK_EXTENSION: {
3130 int ext = (long)argp;
3131
3132 switch (ext) {
3133 case KVM_CAP_IRQCHIP:
Eddie Dongb6958ce2007-07-18 12:15:21 +03003134 case KVM_CAP_HLT:
Izik Eidus82ce2c92007-10-02 18:52:55 +02003135 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Izik Eidus6fc138d2007-10-09 19:20:39 +02003136 case KVM_CAP_USER_MEMORY:
Eddie Dong85f455f2007-07-06 12:20:49 +03003137 r = 1;
3138 break;
3139 default:
3140 r = 0;
3141 break;
3142 }
Avi Kivity5d308f42007-03-01 17:56:20 +02003143 break;
Eddie Dong85f455f2007-07-06 12:20:49 +03003144 }
Avi Kivity07c45a32007-03-07 13:05:38 +02003145 case KVM_GET_VCPU_MMAP_SIZE:
3146 r = -EINVAL;
3147 if (arg)
3148 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02003149 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02003150 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003151 default:
Carsten Otte043405e2007-10-10 17:16:19 +02003152 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003153 }
3154out:
3155 return r;
3156}
3157
Avi Kivity6aa8b732006-12-10 02:21:36 -08003158static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003159 .unlocked_ioctl = kvm_dev_ioctl,
3160 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003161};
3162
3163static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02003164 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003165 "kvm",
3166 &kvm_chardev_ops,
3167};
3168
Avi Kivity774c47f2007-02-12 00:54:47 -08003169/*
3170 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
3171 * cached on it.
3172 */
3173static void decache_vcpus_on_cpu(int cpu)
3174{
3175 struct kvm *vm;
3176 struct kvm_vcpu *vcpu;
3177 int i;
3178
3179 spin_lock(&kvm_lock);
Shaohua Li11ec2802007-07-23 14:51:37 +08003180 list_for_each_entry(vm, &vm_list, vm_list)
Avi Kivity774c47f2007-02-12 00:54:47 -08003181 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003182 vcpu = vm->vcpus[i];
3183 if (!vcpu)
3184 continue;
Avi Kivity774c47f2007-02-12 00:54:47 -08003185 /*
3186 * If the vcpu is locked, then it is running on some
3187 * other cpu and therefore it is not cached on the
3188 * cpu in question.
3189 *
3190 * If it's not locked, check the last cpu it executed
3191 * on.
3192 */
3193 if (mutex_trylock(&vcpu->mutex)) {
3194 if (vcpu->cpu == cpu) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003195 kvm_x86_ops->vcpu_decache(vcpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08003196 vcpu->cpu = -1;
3197 }
3198 mutex_unlock(&vcpu->mutex);
3199 }
3200 }
3201 spin_unlock(&kvm_lock);
3202}
3203
Avi Kivity1b6c0162007-05-24 13:03:52 +03003204static void hardware_enable(void *junk)
3205{
3206 int cpu = raw_smp_processor_id();
3207
3208 if (cpu_isset(cpu, cpus_hardware_enabled))
3209 return;
3210 cpu_set(cpu, cpus_hardware_enabled);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003211 kvm_x86_ops->hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003212}
3213
3214static void hardware_disable(void *junk)
3215{
3216 int cpu = raw_smp_processor_id();
3217
3218 if (!cpu_isset(cpu, cpus_hardware_enabled))
3219 return;
3220 cpu_clear(cpu, cpus_hardware_enabled);
3221 decache_vcpus_on_cpu(cpu);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003222 kvm_x86_ops->hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003223}
3224
Avi Kivity774c47f2007-02-12 00:54:47 -08003225static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
3226 void *v)
3227{
3228 int cpu = (long)v;
3229
3230 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03003231 case CPU_DYING:
3232 case CPU_DYING_FROZEN:
Avi Kivity6ec8a852007-08-19 15:57:26 +03003233 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3234 cpu);
3235 hardware_disable(NULL);
3236 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08003237 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003238 case CPU_UP_CANCELED_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003239 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3240 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003241 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003242 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02003243 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003244 case CPU_ONLINE_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003245 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
3246 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003247 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003248 break;
3249 }
3250 return NOTIFY_OK;
3251}
3252
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003253static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04003254 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003255{
3256 if (val == SYS_RESTART) {
3257 /*
3258 * Some (well, at least mine) BIOSes hang on reboot if
3259 * in vmx root mode.
3260 */
3261 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
3262 on_each_cpu(hardware_disable, NULL, 0, 1);
3263 }
3264 return NOTIFY_OK;
3265}
3266
3267static struct notifier_block kvm_reboot_notifier = {
3268 .notifier_call = kvm_reboot,
3269 .priority = 0,
3270};
3271
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04003272void kvm_io_bus_init(struct kvm_io_bus *bus)
3273{
3274 memset(bus, 0, sizeof(*bus));
3275}
3276
3277void kvm_io_bus_destroy(struct kvm_io_bus *bus)
3278{
3279 int i;
3280
3281 for (i = 0; i < bus->dev_count; i++) {
3282 struct kvm_io_device *pos = bus->devs[i];
3283
3284 kvm_iodevice_destructor(pos);
3285 }
3286}
3287
3288struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
3289{
3290 int i;
3291
3292 for (i = 0; i < bus->dev_count; i++) {
3293 struct kvm_io_device *pos = bus->devs[i];
3294
3295 if (pos->in_range(pos, addr))
3296 return pos;
3297 }
3298
3299 return NULL;
3300}
3301
3302void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
3303{
3304 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
3305
3306 bus->devs[bus->dev_count++] = dev;
3307}
3308
Avi Kivity774c47f2007-02-12 00:54:47 -08003309static struct notifier_block kvm_cpu_notifier = {
3310 .notifier_call = kvm_cpu_hotplug,
3311 .priority = 20, /* must be > scheduler priority */
3312};
3313
Avi Kivity1165f5f2007-04-19 17:27:43 +03003314static u64 stat_get(void *_offset)
3315{
3316 unsigned offset = (long)_offset;
3317 u64 total = 0;
3318 struct kvm *kvm;
3319 struct kvm_vcpu *vcpu;
3320 int i;
3321
3322 spin_lock(&kvm_lock);
3323 list_for_each_entry(kvm, &vm_list, vm_list)
3324 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003325 vcpu = kvm->vcpus[i];
3326 if (vcpu)
3327 total += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03003328 }
3329 spin_unlock(&kvm_lock);
3330 return total;
3331}
3332
Rusty Russell3dea7ca2007-08-01 10:12:22 +10003333DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n");
Avi Kivity1165f5f2007-04-19 17:27:43 +03003334
Avi Kivity6aa8b732006-12-10 02:21:36 -08003335static __init void kvm_init_debug(void)
3336{
3337 struct kvm_stats_debugfs_item *p;
3338
Al Viro8b6d44c2007-02-09 16:38:40 +00003339 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003340 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03003341 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
3342 (void *)(long)p->offset,
3343 &stat_fops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003344}
3345
3346static void kvm_exit_debug(void)
3347{
3348 struct kvm_stats_debugfs_item *p;
3349
3350 for (p = debugfs_entries; p->name; ++p)
3351 debugfs_remove(p->dentry);
3352 debugfs_remove(debugfs_dir);
3353}
3354
Avi Kivity59ae6c62007-02-12 00:54:48 -08003355static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3356{
Avi Kivity4267c412007-05-24 13:09:41 +03003357 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003358 return 0;
3359}
3360
3361static int kvm_resume(struct sys_device *dev)
3362{
Avi Kivity4267c412007-05-24 13:09:41 +03003363 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003364 return 0;
3365}
3366
3367static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01003368 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08003369 .suspend = kvm_suspend,
3370 .resume = kvm_resume,
3371};
3372
3373static struct sys_device kvm_sysdev = {
3374 .id = 0,
3375 .cls = &kvm_sysdev_class,
3376};
3377
Izik Eiduscea7bb22007-10-17 19:17:48 +02003378struct page *bad_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003379
Avi Kivity15ad7142007-07-11 18:17:21 +03003380static inline
3381struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
3382{
3383 return container_of(pn, struct kvm_vcpu, preempt_notifier);
3384}
3385
3386static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
3387{
3388 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3389
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003390 kvm_x86_ops->vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003391}
3392
3393static void kvm_sched_out(struct preempt_notifier *pn,
3394 struct task_struct *next)
3395{
3396 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3397
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003398 kvm_x86_ops->vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003399}
3400
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003401int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10003402 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003403{
3404 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003405 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003406
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003407 if (kvm_x86_ops) {
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08003408 printk(KERN_ERR "kvm: already loaded the other module\n");
3409 return -EEXIST;
3410 }
3411
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003412 if (!ops->cpu_has_kvm_support()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003413 printk(KERN_ERR "kvm: no hardware support\n");
3414 return -EOPNOTSUPP;
3415 }
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003416 if (ops->disabled_by_bios()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003417 printk(KERN_ERR "kvm: disabled by bios\n");
3418 return -EOPNOTSUPP;
3419 }
3420
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003421 kvm_x86_ops = ops;
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003422
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003423 r = kvm_x86_ops->hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003424 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02003425 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003426
Yang, Sheng002c7f72007-07-31 14:23:01 +03003427 for_each_online_cpu(cpu) {
3428 smp_call_function_single(cpu,
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003429 kvm_x86_ops->check_processor_compatibility,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003430 &r, 0, 1);
3431 if (r < 0)
3432 goto out_free_0;
3433 }
3434
Avi Kivity1b6c0162007-05-24 13:03:52 +03003435 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003436 r = register_cpu_notifier(&kvm_cpu_notifier);
3437 if (r)
3438 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003439 register_reboot_notifier(&kvm_reboot_notifier);
3440
Avi Kivity59ae6c62007-02-12 00:54:48 -08003441 r = sysdev_class_register(&kvm_sysdev_class);
3442 if (r)
3443 goto out_free_2;
3444
3445 r = sysdev_register(&kvm_sysdev);
3446 if (r)
3447 goto out_free_3;
3448
Rusty Russellc16f8622007-07-30 21:12:19 +10003449 /* A kmem cache lets us meet the alignment requirements of fx_save. */
3450 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
3451 __alignof__(struct kvm_vcpu), 0, 0);
3452 if (!kvm_vcpu_cache) {
3453 r = -ENOMEM;
3454 goto out_free_4;
3455 }
3456
Avi Kivity6aa8b732006-12-10 02:21:36 -08003457 kvm_chardev_ops.owner = module;
3458
3459 r = misc_register(&kvm_dev);
3460 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04003461 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08003462 goto out_free;
3463 }
3464
Avi Kivity15ad7142007-07-11 18:17:21 +03003465 kvm_preempt_ops.sched_in = kvm_sched_in;
3466 kvm_preempt_ops.sched_out = kvm_sched_out;
3467
Avi Kivityc7addb92007-09-16 18:58:32 +02003468 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
3469
3470 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003471
3472out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10003473 kmem_cache_destroy(kvm_vcpu_cache);
3474out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08003475 sysdev_unregister(&kvm_sysdev);
3476out_free_3:
3477 sysdev_class_unregister(&kvm_sysdev_class);
3478out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003479 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08003480 unregister_cpu_notifier(&kvm_cpu_notifier);
3481out_free_1:
Avi Kivity1b6c0162007-05-24 13:03:52 +03003482 on_each_cpu(hardware_disable, NULL, 0, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03003483out_free_0:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003484 kvm_x86_ops->hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02003485out:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003486 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003487 return r;
3488}
Mike Dayd77c26f2007-10-08 09:02:08 -04003489EXPORT_SYMBOL_GPL(kvm_init_x86);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003490
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003491void kvm_exit_x86(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003492{
3493 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10003494 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003495 sysdev_unregister(&kvm_sysdev);
3496 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003497 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003498 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003499 on_each_cpu(hardware_disable, NULL, 0, 1);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003500 kvm_x86_ops->hardware_unsetup();
3501 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003502}
Mike Dayd77c26f2007-10-08 09:02:08 -04003503EXPORT_SYMBOL_GPL(kvm_exit_x86);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003504
3505static __init int kvm_init(void)
3506{
Avi Kivity37e29d92007-02-20 14:07:37 +02003507 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003508
Avi Kivityb5a33a72007-04-15 16:31:09 +03003509 r = kvm_mmu_module_init();
3510 if (r)
3511 goto out4;
3512
Avi Kivity6aa8b732006-12-10 02:21:36 -08003513 kvm_init_debug();
3514
Carsten Otte043405e2007-10-10 17:16:19 +02003515 kvm_arch_init();
Michael Riepebf591b22006-12-22 01:05:36 -08003516
Izik Eiduscea7bb22007-10-17 19:17:48 +02003517 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
Mike Dayd77c26f2007-10-08 09:02:08 -04003518
3519 if (bad_page == NULL) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003520 r = -ENOMEM;
3521 goto out;
3522 }
3523
Avi Kivity58e690e2007-02-26 16:29:43 +02003524 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003525
3526out:
3527 kvm_exit_debug();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003528 kvm_mmu_module_exit();
3529out4:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003530 return r;
3531}
3532
3533static __exit void kvm_exit(void)
3534{
3535 kvm_exit_debug();
Izik Eiduscea7bb22007-10-17 19:17:48 +02003536 __free_page(bad_page);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003537 kvm_mmu_module_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003538}
3539
3540module_init(kvm_init)
3541module_exit(kvm_exit)