blob: 453e98e251dafd839d8c151cd21f922b98001a6d [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 Kivity60395222007-10-21 11:03:36 +0200996 might_sleep();
997
Avi Kivitye8207542007-03-30 16:54:30 +0300998 gfn = unalias_gfn(kvm, gfn);
999 slot = __gfn_to_memslot(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001000 if (!slot) {
1001 get_page(bad_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +02001002 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001003 }
Izik Eidus8a7ae052007-10-18 11:09:33 +02001004
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001005 down_read(&current->mm->mmap_sem);
1006 npages = get_user_pages(current, current->mm,
1007 slot->userspace_addr
1008 + (gfn - slot->base_gfn) * PAGE_SIZE, 1,
1009 1, 1, page, NULL);
1010 up_read(&current->mm->mmap_sem);
1011 if (npages != 1) {
1012 get_page(bad_page);
1013 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001014 }
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001015
1016 return page[0];
Avi Kivity954bbbc2007-03-30 14:02:32 +03001017}
1018EXPORT_SYMBOL_GPL(gfn_to_page);
1019
Izik Eidus8a7ae052007-10-18 11:09:33 +02001020void kvm_release_page(struct page *page)
1021{
1022 if (!PageReserved(page))
1023 SetPageDirty(page);
1024 put_page(page);
1025}
1026EXPORT_SYMBOL_GPL(kvm_release_page);
1027
Izik Eidus195aefd2007-10-01 22:14:18 +02001028static int next_segment(unsigned long len, int offset)
1029{
1030 if (len > PAGE_SIZE - offset)
1031 return PAGE_SIZE - offset;
1032 else
1033 return len;
1034}
1035
1036int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1037 int len)
1038{
1039 void *page_virt;
1040 struct page *page;
1041
1042 page = gfn_to_page(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001043 if (is_error_page(page)) {
1044 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001045 return -EFAULT;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001046 }
Izik Eidus195aefd2007-10-01 22:14:18 +02001047 page_virt = kmap_atomic(page, KM_USER0);
1048
1049 memcpy(data, page_virt + offset, len);
1050
1051 kunmap_atomic(page_virt, KM_USER0);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001052 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001053 return 0;
1054}
1055EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1056
1057int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1058{
1059 gfn_t gfn = gpa >> PAGE_SHIFT;
1060 int seg;
1061 int offset = offset_in_page(gpa);
1062 int ret;
1063
1064 while ((seg = next_segment(len, offset)) != 0) {
1065 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1066 if (ret < 0)
1067 return ret;
1068 offset = 0;
1069 len -= seg;
1070 data += seg;
1071 ++gfn;
1072 }
1073 return 0;
1074}
1075EXPORT_SYMBOL_GPL(kvm_read_guest);
1076
1077int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1078 int offset, int len)
1079{
1080 void *page_virt;
1081 struct page *page;
1082
1083 page = gfn_to_page(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001084 if (is_error_page(page)) {
1085 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001086 return -EFAULT;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001087 }
Izik Eidus195aefd2007-10-01 22:14:18 +02001088 page_virt = kmap_atomic(page, KM_USER0);
1089
1090 memcpy(page_virt + offset, data, len);
1091
1092 kunmap_atomic(page_virt, KM_USER0);
1093 mark_page_dirty(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001094 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001095 return 0;
1096}
1097EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1098
1099int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1100 unsigned long len)
1101{
1102 gfn_t gfn = gpa >> PAGE_SHIFT;
1103 int seg;
1104 int offset = offset_in_page(gpa);
1105 int ret;
1106
1107 while ((seg = next_segment(len, offset)) != 0) {
1108 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1109 if (ret < 0)
1110 return ret;
1111 offset = 0;
1112 len -= seg;
1113 data += seg;
1114 ++gfn;
1115 }
1116 return 0;
1117}
1118
1119int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1120{
1121 void *page_virt;
1122 struct page *page;
1123
1124 page = gfn_to_page(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001125 if (is_error_page(page)) {
1126 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001127 return -EFAULT;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001128 }
Izik Eidus195aefd2007-10-01 22:14:18 +02001129 page_virt = kmap_atomic(page, KM_USER0);
1130
1131 memset(page_virt + offset, 0, len);
1132
1133 kunmap_atomic(page_virt, KM_USER0);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001134 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001135 return 0;
1136}
1137EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1138
1139int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1140{
1141 gfn_t gfn = gpa >> PAGE_SHIFT;
1142 int seg;
1143 int offset = offset_in_page(gpa);
1144 int ret;
1145
1146 while ((seg = next_segment(len, offset)) != 0) {
1147 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1148 if (ret < 0)
1149 return ret;
1150 offset = 0;
1151 len -= seg;
1152 ++gfn;
1153 }
1154 return 0;
1155}
1156EXPORT_SYMBOL_GPL(kvm_clear_guest);
1157
Rusty Russell7e9d6192007-07-31 20:41:14 +10001158/* WARNING: Does not work on aliased pages. */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1160{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001161 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001162
Rusty Russell7e9d6192007-07-31 20:41:14 +10001163 memslot = __gfn_to_memslot(kvm, gfn);
1164 if (memslot && memslot->dirty_bitmap) {
1165 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001166
Rusty Russell7e9d6192007-07-31 20:41:14 +10001167 /* avoid RMW */
1168 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1169 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001170 }
1171}
1172
Laurent Viviere7d5d762007-07-30 13:41:19 +03001173int emulator_read_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001174 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001175 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001176 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001177{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001178 void *data = val;
1179
1180 while (bytes) {
1181 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1182 unsigned offset = addr & (PAGE_SIZE-1);
1183 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
Izik Eidus195aefd2007-10-01 22:14:18 +02001184 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001185
1186 if (gpa == UNMAPPED_GVA)
1187 return X86EMUL_PROPAGATE_FAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001188 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
1189 if (ret < 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001190 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001191
1192 bytes -= tocopy;
1193 data += tocopy;
1194 addr += tocopy;
1195 }
1196
1197 return X86EMUL_CONTINUE;
1198}
Laurent Viviere7d5d762007-07-30 13:41:19 +03001199EXPORT_SYMBOL_GPL(emulator_read_std);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001200
1201static int emulator_write_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001202 const void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001203 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001204 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001205{
Rusty Russellf0242472007-08-01 10:48:02 +10001206 pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001207 return X86EMUL_UNHANDLEABLE;
1208}
1209
Eddie Dong97222cc2007-09-12 10:58:04 +03001210/*
1211 * Only apic need an MMIO device hook, so shortcut now..
1212 */
1213static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
1214 gpa_t addr)
1215{
1216 struct kvm_io_device *dev;
1217
1218 if (vcpu->apic) {
1219 dev = &vcpu->apic->dev;
1220 if (dev->in_range(dev, addr))
1221 return dev;
1222 }
1223 return NULL;
1224}
1225
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001226static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1227 gpa_t addr)
1228{
Eddie Dong97222cc2007-09-12 10:58:04 +03001229 struct kvm_io_device *dev;
1230
1231 dev = vcpu_find_pervcpu_dev(vcpu, addr);
1232 if (dev == NULL)
1233 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1234 return dev;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001235}
1236
Eddie Dong74906342007-06-19 18:05:03 +03001237static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
1238 gpa_t addr)
1239{
1240 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
1241}
1242
Avi Kivity6aa8b732006-12-10 02:21:36 -08001243static int emulator_read_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001244 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001245 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001246 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001247{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001248 struct kvm_io_device *mmio_dev;
1249 gpa_t gpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001250
1251 if (vcpu->mmio_read_completed) {
1252 memcpy(val, vcpu->mmio_data, bytes);
1253 vcpu->mmio_read_completed = 0;
1254 return X86EMUL_CONTINUE;
Laurent Viviercebff022007-07-30 13:35:24 +03001255 } else if (emulator_read_std(addr, val, bytes, vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001256 == X86EMUL_CONTINUE)
1257 return X86EMUL_CONTINUE;
Avi Kivityd27d4ac2007-02-19 14:37:46 +02001258
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001259 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1260 if (gpa == UNMAPPED_GVA)
1261 return X86EMUL_PROPAGATE_FAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001262
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001263 /*
1264 * Is this MMIO handled locally?
1265 */
1266 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1267 if (mmio_dev) {
1268 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1269 return X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001270 }
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001271
1272 vcpu->mmio_needed = 1;
1273 vcpu->mmio_phys_addr = gpa;
1274 vcpu->mmio_size = bytes;
1275 vcpu->mmio_is_write = 0;
1276
1277 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001278}
1279
Avi Kivityda4a00f2007-01-05 16:36:44 -08001280static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity4c690a12007-04-22 15:28:19 +03001281 const void *val, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001282{
Izik Eidus195aefd2007-10-01 22:14:18 +02001283 int ret;
Avi Kivityda4a00f2007-01-05 16:36:44 -08001284
Izik Eidus195aefd2007-10-01 22:14:18 +02001285 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
1286 if (ret < 0)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001287 return 0;
Shaohua Life551882007-07-23 14:51:39 +08001288 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001289 return 1;
1290}
1291
Avi Kivityb0fcd902007-07-22 18:48:54 +03001292static int emulator_write_emulated_onepage(unsigned long addr,
1293 const void *val,
1294 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001295 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001296{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001297 struct kvm_io_device *mmio_dev;
1298 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001299
Avi Kivityc9047f52007-04-17 10:53:22 +03001300 if (gpa == UNMAPPED_GVA) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001301 kvm_x86_ops->inject_page_fault(vcpu, addr, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001302 return X86EMUL_PROPAGATE_FAULT;
Avi Kivityc9047f52007-04-17 10:53:22 +03001303 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001304
Avi Kivityda4a00f2007-01-05 16:36:44 -08001305 if (emulator_write_phys(vcpu, gpa, val, bytes))
1306 return X86EMUL_CONTINUE;
1307
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001308 /*
1309 * Is this MMIO handled locally?
1310 */
1311 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1312 if (mmio_dev) {
1313 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1314 return X86EMUL_CONTINUE;
1315 }
1316
Avi Kivity6aa8b732006-12-10 02:21:36 -08001317 vcpu->mmio_needed = 1;
1318 vcpu->mmio_phys_addr = gpa;
1319 vcpu->mmio_size = bytes;
1320 vcpu->mmio_is_write = 1;
Avi Kivity4c690a12007-04-22 15:28:19 +03001321 memcpy(vcpu->mmio_data, val, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001322
1323 return X86EMUL_CONTINUE;
1324}
1325
Laurent Viviere7d5d762007-07-30 13:41:19 +03001326int emulator_write_emulated(unsigned long addr,
Avi Kivityb0fcd902007-07-22 18:48:54 +03001327 const void *val,
1328 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001329 struct kvm_vcpu *vcpu)
Avi Kivityb0fcd902007-07-22 18:48:54 +03001330{
1331 /* Crossing a page boundary? */
1332 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1333 int rc, now;
1334
1335 now = -addr & ~PAGE_MASK;
Laurent Viviercebff022007-07-30 13:35:24 +03001336 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001337 if (rc != X86EMUL_CONTINUE)
1338 return rc;
1339 addr += now;
1340 val += now;
1341 bytes -= now;
1342 }
Laurent Viviercebff022007-07-30 13:35:24 +03001343 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001344}
Laurent Viviere7d5d762007-07-30 13:41:19 +03001345EXPORT_SYMBOL_GPL(emulator_write_emulated);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001346
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347static int emulator_cmpxchg_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001348 const void *old,
1349 const void *new,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001350 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001351 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001352{
1353 static int reported;
1354
1355 if (!reported) {
1356 reported = 1;
1357 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1358 }
Laurent Viviercebff022007-07-30 13:35:24 +03001359 return emulator_write_emulated(addr, new, bytes, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001360}
1361
1362static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1363{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001364 return kvm_x86_ops->get_segment_base(vcpu, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001365}
1366
1367int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1368{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001369 return X86EMUL_CONTINUE;
1370}
1371
1372int emulate_clts(struct kvm_vcpu *vcpu)
1373{
Amit Shah404fb882007-11-19 17:57:35 +02001374 kvm_x86_ops->set_cr0(vcpu, vcpu->cr0 & ~X86_CR0_TS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001375 return X86EMUL_CONTINUE;
1376}
1377
Mike Dayd77c26f2007-10-08 09:02:08 -04001378int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001379{
1380 struct kvm_vcpu *vcpu = ctxt->vcpu;
1381
1382 switch (dr) {
1383 case 0 ... 3:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001384 *dest = kvm_x86_ops->get_dr(vcpu, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385 return X86EMUL_CONTINUE;
1386 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001387 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001388 return X86EMUL_UNHANDLEABLE;
1389 }
1390}
1391
1392int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1393{
1394 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1395 int exception;
1396
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001397 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001398 if (exception) {
1399 /* FIXME: better handling */
1400 return X86EMUL_UNHANDLEABLE;
1401 }
1402 return X86EMUL_CONTINUE;
1403}
1404
Avi Kivity054b1362007-09-12 13:21:09 +03001405void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001406{
1407 static int reported;
1408 u8 opcodes[4];
Avi Kivity054b1362007-09-12 13:21:09 +03001409 unsigned long rip = vcpu->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001410 unsigned long rip_linear;
1411
Avi Kivity054b1362007-09-12 13:21:09 +03001412 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001413
1414 if (reported)
1415 return;
1416
Avi Kivity054b1362007-09-12 13:21:09 +03001417 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001418
Avi Kivity054b1362007-09-12 13:21:09 +03001419 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1420 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001421 reported = 1;
1422}
Avi Kivity054b1362007-09-12 13:21:09 +03001423EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001424
1425struct x86_emulate_ops emulate_ops = {
1426 .read_std = emulator_read_std,
1427 .write_std = emulator_write_std,
1428 .read_emulated = emulator_read_emulated,
1429 .write_emulated = emulator_write_emulated,
1430 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1431};
1432
1433int emulate_instruction(struct kvm_vcpu *vcpu,
1434 struct kvm_run *run,
1435 unsigned long cr2,
Laurent Vivier34273182007-09-18 11:27:37 +02001436 u16 error_code,
1437 int no_decode)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001438{
Laurent Viviera22436b2007-09-24 17:00:58 +02001439 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001440
Avi Kivitye7df56e2007-03-14 15:54:54 +02001441 vcpu->mmio_fault_cr2 = cr2;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001442 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001443
Avi Kivity6aa8b732006-12-10 02:21:36 -08001444 vcpu->mmio_is_write = 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03001445 vcpu->pio.string = 0;
Laurent Vivier34273182007-09-18 11:27:37 +02001446
1447 if (!no_decode) {
1448 int cs_db, cs_l;
1449 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1450
1451 vcpu->emulate_ctxt.vcpu = vcpu;
1452 vcpu->emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
1453 vcpu->emulate_ctxt.cr2 = cr2;
1454 vcpu->emulate_ctxt.mode =
1455 (vcpu->emulate_ctxt.eflags & X86_EFLAGS_VM)
1456 ? X86EMUL_MODE_REAL : cs_l
1457 ? X86EMUL_MODE_PROT64 : cs_db
1458 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1459
1460 if (vcpu->emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1461 vcpu->emulate_ctxt.cs_base = 0;
1462 vcpu->emulate_ctxt.ds_base = 0;
1463 vcpu->emulate_ctxt.es_base = 0;
1464 vcpu->emulate_ctxt.ss_base = 0;
1465 } else {
1466 vcpu->emulate_ctxt.cs_base =
1467 get_segment_base(vcpu, VCPU_SREG_CS);
1468 vcpu->emulate_ctxt.ds_base =
1469 get_segment_base(vcpu, VCPU_SREG_DS);
1470 vcpu->emulate_ctxt.es_base =
1471 get_segment_base(vcpu, VCPU_SREG_ES);
1472 vcpu->emulate_ctxt.ss_base =
1473 get_segment_base(vcpu, VCPU_SREG_SS);
1474 }
1475
1476 vcpu->emulate_ctxt.gs_base =
1477 get_segment_base(vcpu, VCPU_SREG_GS);
1478 vcpu->emulate_ctxt.fs_base =
1479 get_segment_base(vcpu, VCPU_SREG_FS);
1480
1481 r = x86_decode_insn(&vcpu->emulate_ctxt, &emulate_ops);
Laurent Viviera22436b2007-09-24 17:00:58 +02001482 if (r) {
1483 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1484 return EMULATE_DONE;
1485 return EMULATE_FAIL;
1486 }
Laurent Vivier34273182007-09-18 11:27:37 +02001487 }
1488
Laurent Viviera22436b2007-09-24 17:00:58 +02001489 r = x86_emulate_insn(&vcpu->emulate_ctxt, &emulate_ops);
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001490
Laurent Viviere70669a2007-08-05 10:36:40 +03001491 if (vcpu->pio.string)
1492 return EMULATE_DO_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001493
1494 if ((r || vcpu->mmio_is_write) && run) {
Jeff Dike8fc0d082007-07-17 12:26:59 -04001495 run->exit_reason = KVM_EXIT_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001496 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1497 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1498 run->mmio.len = vcpu->mmio_size;
1499 run->mmio.is_write = vcpu->mmio_is_write;
1500 }
1501
1502 if (r) {
Avi Kivitya4360362007-01-05 16:36:45 -08001503 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1504 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001505 if (!vcpu->mmio_needed) {
Avi Kivity054b1362007-09-12 13:21:09 +03001506 kvm_report_emulation_failure(vcpu, "mmio");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507 return EMULATE_FAIL;
1508 }
1509 return EMULATE_DO_MMIO;
1510 }
1511
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001512 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier34273182007-09-18 11:27:37 +02001513 kvm_x86_ops->set_rflags(vcpu, vcpu->emulate_ctxt.eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001514
Avi Kivity02c83202007-04-29 15:02:17 +03001515 if (vcpu->mmio_is_write) {
1516 vcpu->mmio_needed = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001517 return EMULATE_DO_MMIO;
Avi Kivity02c83202007-04-29 15:02:17 +03001518 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001519
1520 return EMULATE_DONE;
1521}
1522EXPORT_SYMBOL_GPL(emulate_instruction);
1523
Eddie Dongb6958ce2007-07-18 12:15:21 +03001524/*
1525 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1526 */
He, Qingc5ec1532007-09-03 17:07:41 +03001527static void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001528{
1529 DECLARE_WAITQUEUE(wait, current);
1530
1531 add_wait_queue(&vcpu->wq, &wait);
1532
1533 /*
1534 * We will block until either an interrupt or a signal wakes us up
1535 */
He, Qingc5ec1532007-09-03 17:07:41 +03001536 while (!kvm_cpu_has_interrupt(vcpu)
1537 && !signal_pending(current)
1538 && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE
1539 && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) {
Eddie Dongb6958ce2007-07-18 12:15:21 +03001540 set_current_state(TASK_INTERRUPTIBLE);
1541 vcpu_put(vcpu);
1542 schedule();
1543 vcpu_load(vcpu);
1544 }
1545
He, Qingc5ec1532007-09-03 17:07:41 +03001546 __set_current_state(TASK_RUNNING);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001547 remove_wait_queue(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001548}
1549
Avi Kivityd3bef152007-06-05 15:53:05 +03001550int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1551{
Avi Kivityd3bef152007-06-05 15:53:05 +03001552 ++vcpu->stat.halt_exits;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001553 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc5ec1532007-09-03 17:07:41 +03001554 vcpu->mp_state = VCPU_MP_STATE_HALTED;
1555 kvm_vcpu_block(vcpu);
1556 if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE)
1557 return -EINTR;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001558 return 1;
1559 } else {
1560 vcpu->run->exit_reason = KVM_EXIT_HLT;
1561 return 0;
1562 }
Avi Kivityd3bef152007-06-05 15:53:05 +03001563}
1564EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1565
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001566int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
Avi Kivity270fd9b2007-02-19 14:37:47 +02001567{
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001568 unsigned long nr, a0, a1, a2, a3, ret;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001569
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001570 kvm_x86_ops->cache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001571
1572 nr = vcpu->regs[VCPU_REGS_RAX];
1573 a0 = vcpu->regs[VCPU_REGS_RBX];
1574 a1 = vcpu->regs[VCPU_REGS_RCX];
1575 a2 = vcpu->regs[VCPU_REGS_RDX];
1576 a3 = vcpu->regs[VCPU_REGS_RSI];
1577
1578 if (!is_long_mode(vcpu)) {
1579 nr &= 0xFFFFFFFF;
1580 a0 &= 0xFFFFFFFF;
1581 a1 &= 0xFFFFFFFF;
1582 a2 &= 0xFFFFFFFF;
1583 a3 &= 0xFFFFFFFF;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001584 }
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001585
Avi Kivity270fd9b2007-02-19 14:37:47 +02001586 switch (nr) {
1587 default:
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001588 ret = -KVM_ENOSYS;
1589 break;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001590 }
1591 vcpu->regs[VCPU_REGS_RAX] = ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001592 kvm_x86_ops->decache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001593 return 0;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001594}
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001595EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
1596
1597int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
1598{
1599 char instruction[3];
1600 int ret = 0;
1601
1602 mutex_lock(&vcpu->kvm->lock);
1603
1604 /*
1605 * Blow out the MMU to ensure that no other VCPU has an active mapping
1606 * to ensure that the updated hypercall appears atomically across all
1607 * VCPUs.
1608 */
1609 kvm_mmu_zap_all(vcpu->kvm);
1610
1611 kvm_x86_ops->cache_regs(vcpu);
1612 kvm_x86_ops->patch_hypercall(vcpu, instruction);
1613 if (emulator_write_emulated(vcpu->rip, instruction, 3, vcpu)
1614 != X86EMUL_CONTINUE)
1615 ret = -EFAULT;
1616
1617 mutex_unlock(&vcpu->kvm->lock);
1618
1619 return ret;
1620}
Avi Kivity270fd9b2007-02-19 14:37:47 +02001621
Avi Kivity6aa8b732006-12-10 02:21:36 -08001622static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1623{
1624 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1625}
1626
1627void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1628{
1629 struct descriptor_table dt = { limit, base };
1630
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001631 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001632}
1633
1634void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1635{
1636 struct descriptor_table dt = { limit, base };
1637
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001638 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001639}
1640
1641void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1642 unsigned long *rflags)
1643{
1644 lmsw(vcpu, msw);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001645 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001646}
1647
1648unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1649{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001650 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001651 switch (cr) {
1652 case 0:
1653 return vcpu->cr0;
1654 case 2:
1655 return vcpu->cr2;
1656 case 3:
1657 return vcpu->cr3;
1658 case 4:
1659 return vcpu->cr4;
1660 default:
1661 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1662 return 0;
1663 }
1664}
1665
1666void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1667 unsigned long *rflags)
1668{
1669 switch (cr) {
1670 case 0:
1671 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001672 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001673 break;
1674 case 2:
1675 vcpu->cr2 = val;
1676 break;
1677 case 3:
1678 set_cr3(vcpu, val);
1679 break;
1680 case 4:
1681 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1682 break;
1683 default:
1684 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1685 }
1686}
1687
Avi Kivity3bab1f52006-12-29 16:49:48 -08001688int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1689{
1690 u64 data;
1691
1692 switch (msr) {
1693 case 0xc0010010: /* SYSCFG */
1694 case 0xc0010015: /* HWCR */
1695 case MSR_IA32_PLATFORM_ID:
1696 case MSR_IA32_P5_MC_ADDR:
1697 case MSR_IA32_P5_MC_TYPE:
1698 case MSR_IA32_MC0_CTL:
1699 case MSR_IA32_MCG_STATUS:
1700 case MSR_IA32_MCG_CAP:
1701 case MSR_IA32_MC0_MISC:
1702 case MSR_IA32_MC0_MISC+4:
1703 case MSR_IA32_MC0_MISC+8:
1704 case MSR_IA32_MC0_MISC+12:
1705 case MSR_IA32_MC0_MISC+16:
1706 case MSR_IA32_UCODE_REV:
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001707 case MSR_IA32_PERF_STATUS:
Matthew Gregan2dc70942007-05-06 10:59:46 +03001708 case MSR_IA32_EBL_CR_POWERON:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001709 /* MTRR registers */
1710 case 0xfe:
1711 case 0x200 ... 0x2ff:
1712 data = 0;
1713 break;
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001714 case 0xcd: /* fsb frequency */
1715 data = 3;
1716 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001717 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001718 data = kvm_get_apic_base(vcpu);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001719 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001720 case MSR_IA32_MISC_ENABLE:
1721 data = vcpu->ia32_misc_enable_msr;
1722 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001723#ifdef CONFIG_X86_64
1724 case MSR_EFER:
1725 data = vcpu->shadow_efer;
1726 break;
1727#endif
1728 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001729 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001730 return 1;
1731 }
1732 *pdata = data;
1733 return 0;
1734}
1735EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1736
Avi Kivity6aa8b732006-12-10 02:21:36 -08001737/*
1738 * Reads an msr value (of 'msr_index') into 'pdata'.
1739 * Returns 0 on success, non-0 otherwise.
1740 * Assumes vcpu_load() was already called.
1741 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001742int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001743{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001744 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001745}
1746
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001747#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001748
Avi Kivity3bab1f52006-12-29 16:49:48 -08001749static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001750{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001751 if (efer & EFER_RESERVED_BITS) {
1752 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1753 efer);
1754 inject_gp(vcpu);
1755 return;
1756 }
1757
1758 if (is_paging(vcpu)
1759 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1760 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1761 inject_gp(vcpu);
1762 return;
1763 }
1764
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001765 kvm_x86_ops->set_efer(vcpu, efer);
Avi Kivity7725f0b2006-12-13 00:34:01 -08001766
Avi Kivity6aa8b732006-12-10 02:21:36 -08001767 efer &= ~EFER_LMA;
1768 efer |= vcpu->shadow_efer & EFER_LMA;
1769
1770 vcpu->shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001771}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001772
1773#endif
1774
Avi Kivity3bab1f52006-12-29 16:49:48 -08001775int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1776{
1777 switch (msr) {
1778#ifdef CONFIG_X86_64
1779 case MSR_EFER:
1780 set_efer(vcpu, data);
1781 break;
1782#endif
1783 case MSR_IA32_MC0_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001784 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
Avi Kivity3bab1f52006-12-29 16:49:48 -08001785 __FUNCTION__, data);
1786 break;
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001787 case MSR_IA32_MCG_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001788 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001789 __FUNCTION__, data);
1790 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001791 case MSR_IA32_UCODE_REV:
1792 case MSR_IA32_UCODE_WRITE:
1793 case 0x200 ... 0x2ff: /* MTRRs */
1794 break;
1795 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001796 kvm_set_apic_base(vcpu, data);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001797 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001798 case MSR_IA32_MISC_ENABLE:
1799 vcpu->ia32_misc_enable_msr = data;
1800 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001801 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001802 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001803 return 1;
1804 }
1805 return 0;
1806}
1807EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1808
Avi Kivity6aa8b732006-12-10 02:21:36 -08001809/*
1810 * Writes msr value into into the appropriate "register".
1811 * Returns 0 on success, non-0 otherwise.
1812 * Assumes vcpu_load() was already called.
1813 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001814int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001815{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001816 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001817}
1818
1819void kvm_resched(struct kvm_vcpu *vcpu)
1820{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001821 if (!need_resched())
1822 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001823 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001824}
1825EXPORT_SYMBOL_GPL(kvm_resched);
1826
Avi Kivity06465c52007-02-28 20:46:53 +02001827void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1828{
1829 int i;
1830 u32 function;
1831 struct kvm_cpuid_entry *e, *best;
1832
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001833 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001834 function = vcpu->regs[VCPU_REGS_RAX];
1835 vcpu->regs[VCPU_REGS_RAX] = 0;
1836 vcpu->regs[VCPU_REGS_RBX] = 0;
1837 vcpu->regs[VCPU_REGS_RCX] = 0;
1838 vcpu->regs[VCPU_REGS_RDX] = 0;
1839 best = NULL;
1840 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1841 e = &vcpu->cpuid_entries[i];
1842 if (e->function == function) {
1843 best = e;
1844 break;
1845 }
1846 /*
1847 * Both basic or both extended?
1848 */
1849 if (((e->function ^ function) & 0x80000000) == 0)
1850 if (!best || e->function > best->function)
1851 best = e;
1852 }
1853 if (best) {
1854 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1855 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1856 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1857 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1858 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001859 kvm_x86_ops->decache_regs(vcpu);
1860 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001861}
1862EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1863
Avi Kivity039576c2007-03-20 12:46:50 +02001864static int pio_copy_data(struct kvm_vcpu *vcpu)
Avi Kivity46fc1472007-02-22 19:39:30 +02001865{
Avi Kivity039576c2007-03-20 12:46:50 +02001866 void *p = vcpu->pio_data;
1867 void *q;
1868 unsigned bytes;
1869 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1870
Avi Kivity039576c2007-03-20 12:46:50 +02001871 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1872 PAGE_KERNEL);
1873 if (!q) {
Avi Kivity039576c2007-03-20 12:46:50 +02001874 free_pio_guest_pages(vcpu);
1875 return -ENOMEM;
1876 }
1877 q += vcpu->pio.guest_page_offset;
1878 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1879 if (vcpu->pio.in)
1880 memcpy(q, p, bytes);
1881 else
1882 memcpy(p, q, bytes);
1883 q -= vcpu->pio.guest_page_offset;
1884 vunmap(q);
Avi Kivity039576c2007-03-20 12:46:50 +02001885 free_pio_guest_pages(vcpu);
1886 return 0;
1887}
1888
1889static int complete_pio(struct kvm_vcpu *vcpu)
1890{
1891 struct kvm_pio_request *io = &vcpu->pio;
Avi Kivity46fc1472007-02-22 19:39:30 +02001892 long delta;
Avi Kivity039576c2007-03-20 12:46:50 +02001893 int r;
Avi Kivity46fc1472007-02-22 19:39:30 +02001894
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001895 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001896
1897 if (!io->string) {
Avi Kivity039576c2007-03-20 12:46:50 +02001898 if (io->in)
1899 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
Avi Kivity46fc1472007-02-22 19:39:30 +02001900 io->size);
1901 } else {
Avi Kivity039576c2007-03-20 12:46:50 +02001902 if (io->in) {
1903 r = pio_copy_data(vcpu);
1904 if (r) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001905 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02001906 return r;
1907 }
1908 }
1909
Avi Kivity46fc1472007-02-22 19:39:30 +02001910 delta = 1;
1911 if (io->rep) {
Avi Kivity039576c2007-03-20 12:46:50 +02001912 delta *= io->cur_count;
Avi Kivity46fc1472007-02-22 19:39:30 +02001913 /*
1914 * The size of the register should really depend on
1915 * current address size.
1916 */
1917 vcpu->regs[VCPU_REGS_RCX] -= delta;
1918 }
Avi Kivity039576c2007-03-20 12:46:50 +02001919 if (io->down)
Avi Kivity46fc1472007-02-22 19:39:30 +02001920 delta = -delta;
1921 delta *= io->size;
Avi Kivity039576c2007-03-20 12:46:50 +02001922 if (io->in)
Avi Kivity46fc1472007-02-22 19:39:30 +02001923 vcpu->regs[VCPU_REGS_RDI] += delta;
1924 else
1925 vcpu->regs[VCPU_REGS_RSI] += delta;
1926 }
1927
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001928 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001929
Avi Kivity039576c2007-03-20 12:46:50 +02001930 io->count -= io->cur_count;
1931 io->cur_count = 0;
1932
Avi Kivity039576c2007-03-20 12:46:50 +02001933 return 0;
Avi Kivity46fc1472007-02-22 19:39:30 +02001934}
1935
Eddie Dong65619eb2007-07-17 11:52:33 +03001936static void kernel_pio(struct kvm_io_device *pio_dev,
1937 struct kvm_vcpu *vcpu,
1938 void *pd)
Eddie Dong74906342007-06-19 18:05:03 +03001939{
1940 /* TODO: String I/O for in kernel device */
1941
Eddie Dong9cf98822007-07-22 10:36:31 +03001942 mutex_lock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001943 if (vcpu->pio.in)
1944 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1945 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001946 pd);
Eddie Dong74906342007-06-19 18:05:03 +03001947 else
1948 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1949 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001950 pd);
Eddie Dong9cf98822007-07-22 10:36:31 +03001951 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001952}
1953
1954static void pio_string_write(struct kvm_io_device *pio_dev,
1955 struct kvm_vcpu *vcpu)
1956{
1957 struct kvm_pio_request *io = &vcpu->pio;
1958 void *pd = vcpu->pio_data;
1959 int i;
1960
Eddie Dong9cf98822007-07-22 10:36:31 +03001961 mutex_lock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001962 for (i = 0; i < io->cur_count; i++) {
1963 kvm_iodevice_write(pio_dev, io->port,
1964 io->size,
1965 pd);
1966 pd += io->size;
1967 }
Eddie Dong9cf98822007-07-22 10:36:31 +03001968 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001969}
1970
Mike Dayd77c26f2007-10-08 09:02:08 -04001971int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
Laurent Vivier3090dd72007-08-05 10:43:32 +03001972 int size, unsigned port)
1973{
1974 struct kvm_io_device *pio_dev;
1975
1976 vcpu->run->exit_reason = KVM_EXIT_IO;
1977 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1978 vcpu->run->io.size = vcpu->pio.size = size;
1979 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1980 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1;
1981 vcpu->run->io.port = vcpu->pio.port = port;
1982 vcpu->pio.in = in;
1983 vcpu->pio.string = 0;
1984 vcpu->pio.down = 0;
1985 vcpu->pio.guest_page_offset = 0;
1986 vcpu->pio.rep = 0;
1987
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001988 kvm_x86_ops->cache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03001989 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001990 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03001991
Avi Kivity0967b7b2007-09-15 17:34:36 +03001992 kvm_x86_ops->skip_emulated_instruction(vcpu);
1993
Laurent Vivier3090dd72007-08-05 10:43:32 +03001994 pio_dev = vcpu_find_pio_dev(vcpu, port);
1995 if (pio_dev) {
1996 kernel_pio(pio_dev, vcpu, vcpu->pio_data);
1997 complete_pio(vcpu);
1998 return 1;
1999 }
2000 return 0;
2001}
2002EXPORT_SYMBOL_GPL(kvm_emulate_pio);
2003
2004int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2005 int size, unsigned long count, int down,
Avi Kivity039576c2007-03-20 12:46:50 +02002006 gva_t address, int rep, unsigned port)
2007{
2008 unsigned now, in_page;
Eddie Dong65619eb2007-07-17 11:52:33 +03002009 int i, ret = 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002010 int nr_pages = 1;
2011 struct page *page;
Eddie Dong74906342007-06-19 18:05:03 +03002012 struct kvm_io_device *pio_dev;
Avi Kivity039576c2007-03-20 12:46:50 +02002013
2014 vcpu->run->exit_reason = KVM_EXIT_IO;
2015 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Laurent Vivier3090dd72007-08-05 10:43:32 +03002016 vcpu->run->io.size = vcpu->pio.size = size;
Avi Kivity039576c2007-03-20 12:46:50 +02002017 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Laurent Vivier3090dd72007-08-05 10:43:32 +03002018 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count;
2019 vcpu->run->io.port = vcpu->pio.port = port;
Avi Kivity039576c2007-03-20 12:46:50 +02002020 vcpu->pio.in = in;
Laurent Vivier3090dd72007-08-05 10:43:32 +03002021 vcpu->pio.string = 1;
Avi Kivity039576c2007-03-20 12:46:50 +02002022 vcpu->pio.down = down;
2023 vcpu->pio.guest_page_offset = offset_in_page(address);
2024 vcpu->pio.rep = rep;
2025
Avi Kivity039576c2007-03-20 12:46:50 +02002026 if (!count) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002027 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02002028 return 1;
2029 }
2030
Avi Kivity039576c2007-03-20 12:46:50 +02002031 if (!down)
2032 in_page = PAGE_SIZE - offset_in_page(address);
2033 else
2034 in_page = offset_in_page(address) + size;
2035 now = min(count, (unsigned long)in_page / size);
2036 if (!now) {
2037 /*
2038 * String I/O straddles page boundary. Pin two guest pages
2039 * so that we satisfy atomicity constraints. Do just one
2040 * transaction to avoid complexity.
2041 */
2042 nr_pages = 2;
2043 now = 1;
2044 }
2045 if (down) {
2046 /*
2047 * String I/O in reverse. Yuck. Kill the guest, fix later.
2048 */
Rusty Russellf0242472007-08-01 10:48:02 +10002049 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivity039576c2007-03-20 12:46:50 +02002050 inject_gp(vcpu);
2051 return 1;
2052 }
2053 vcpu->run->io.count = now;
2054 vcpu->pio.cur_count = now;
2055
Avi Kivity0967b7b2007-09-15 17:34:36 +03002056 if (vcpu->pio.cur_count == vcpu->pio.count)
2057 kvm_x86_ops->skip_emulated_instruction(vcpu);
2058
Avi Kivity039576c2007-03-20 12:46:50 +02002059 for (i = 0; i < nr_pages; ++i) {
Shaohua Li11ec2802007-07-23 14:51:37 +08002060 mutex_lock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02002061 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
Avi Kivity039576c2007-03-20 12:46:50 +02002062 vcpu->pio.guest_pages[i] = page;
Shaohua Li11ec2802007-07-23 14:51:37 +08002063 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02002064 if (!page) {
2065 inject_gp(vcpu);
2066 free_pio_guest_pages(vcpu);
2067 return 1;
2068 }
2069 }
2070
Laurent Vivier3090dd72007-08-05 10:43:32 +03002071 pio_dev = vcpu_find_pio_dev(vcpu, port);
Eddie Dong65619eb2007-07-17 11:52:33 +03002072 if (!vcpu->pio.in) {
2073 /* string PIO write */
2074 ret = pio_copy_data(vcpu);
2075 if (ret >= 0 && pio_dev) {
2076 pio_string_write(pio_dev, vcpu);
2077 complete_pio(vcpu);
2078 if (vcpu->pio.count == 0)
2079 ret = 1;
2080 }
2081 } else if (pio_dev)
Rusty Russellf0242472007-08-01 10:48:02 +10002082 pr_unimpl(vcpu, "no string pio read support yet, "
Eddie Dong65619eb2007-07-17 11:52:33 +03002083 "port %x size %d count %ld\n",
2084 port, size, count);
2085
2086 return ret;
Avi Kivity039576c2007-03-20 12:46:50 +02002087}
Laurent Vivier3090dd72007-08-05 10:43:32 +03002088EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
Avi Kivity039576c2007-03-20 12:46:50 +02002089
Avi Kivity04d2cc72007-09-10 18:10:54 +03002090/*
2091 * Check if userspace requested an interrupt window, and that the
2092 * interrupt window is open.
2093 *
2094 * No need to exit to userspace if we already have an interrupt queued.
2095 */
2096static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2097 struct kvm_run *kvm_run)
2098{
2099 return (!vcpu->irq_summary &&
2100 kvm_run->request_interrupt_window &&
2101 vcpu->interrupt_window_open &&
2102 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
2103}
2104
2105static void post_kvm_run_save(struct kvm_vcpu *vcpu,
2106 struct kvm_run *kvm_run)
2107{
2108 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
2109 kvm_run->cr8 = get_cr8(vcpu);
2110 kvm_run->apic_base = kvm_get_apic_base(vcpu);
2111 if (irqchip_in_kernel(vcpu->kvm))
2112 kvm_run->ready_for_interrupt_injection = 1;
2113 else
2114 kvm_run->ready_for_interrupt_injection =
2115 (vcpu->interrupt_window_open &&
2116 vcpu->irq_summary == 0);
2117}
2118
2119static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2120{
2121 int r;
2122
2123 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002124 pr_debug("vcpu %d received sipi with vector # %x\n",
Avi Kivity04d2cc72007-09-10 18:10:54 +03002125 vcpu->vcpu_id, vcpu->sipi_vector);
2126 kvm_lapic_reset(vcpu);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002127 r = kvm_x86_ops->vcpu_reset(vcpu);
2128 if (r)
2129 return r;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002130 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
2131 }
2132
2133preempted:
2134 if (vcpu->guest_debug.enabled)
2135 kvm_x86_ops->guest_debug_pre(vcpu);
2136
2137again:
2138 r = kvm_mmu_reload(vcpu);
2139 if (unlikely(r))
2140 goto out;
2141
Avi Kivityab6ef342007-10-16 16:23:22 +02002142 kvm_inject_pending_timer_irqs(vcpu);
2143
Avi Kivity04d2cc72007-09-10 18:10:54 +03002144 preempt_disable();
2145
2146 kvm_x86_ops->prepare_guest_switch(vcpu);
2147 kvm_load_guest_fpu(vcpu);
2148
2149 local_irq_disable();
2150
2151 if (signal_pending(current)) {
2152 local_irq_enable();
2153 preempt_enable();
2154 r = -EINTR;
2155 kvm_run->exit_reason = KVM_EXIT_INTR;
2156 ++vcpu->stat.signal_exits;
2157 goto out;
2158 }
2159
2160 if (irqchip_in_kernel(vcpu->kvm))
2161 kvm_x86_ops->inject_pending_irq(vcpu);
2162 else if (!vcpu->mmio_read_completed)
2163 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2164
2165 vcpu->guest_mode = 1;
Laurent Vivierd172fcd2007-10-15 17:00:19 +02002166 kvm_guest_enter();
Avi Kivity04d2cc72007-09-10 18:10:54 +03002167
2168 if (vcpu->requests)
Avi Kivity3176bc32007-10-16 17:22:08 +02002169 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivity04d2cc72007-09-10 18:10:54 +03002170 kvm_x86_ops->tlb_flush(vcpu);
2171
2172 kvm_x86_ops->run(vcpu, kvm_run);
2173
2174 vcpu->guest_mode = 0;
2175 local_irq_enable();
2176
2177 ++vcpu->stat.exits;
2178
Laurent Vivier0552f732007-10-18 15:19:01 +02002179 /*
2180 * We must have an instruction between local_irq_enable() and
2181 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2182 * the interrupt shadow. The stat.exits increment will do nicely.
2183 * But we need to prevent reordering, hence this barrier():
2184 */
2185 barrier();
2186
2187 kvm_guest_exit();
2188
Avi Kivity04d2cc72007-09-10 18:10:54 +03002189 preempt_enable();
2190
2191 /*
2192 * Profile KVM exit RIPs:
2193 */
2194 if (unlikely(prof_on == KVM_PROFILING)) {
2195 kvm_x86_ops->cache_regs(vcpu);
2196 profile_hit(KVM_PROFILING, (void *)vcpu->rip);
2197 }
2198
2199 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2200
2201 if (r > 0) {
2202 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2203 r = -EINTR;
2204 kvm_run->exit_reason = KVM_EXIT_INTR;
2205 ++vcpu->stat.request_irq_exits;
2206 goto out;
2207 }
2208 if (!need_resched()) {
2209 ++vcpu->stat.light_exits;
2210 goto again;
2211 }
2212 }
2213
2214out:
2215 if (r > 0) {
2216 kvm_resched(vcpu);
2217 goto preempted;
2218 }
2219
2220 post_kvm_run_save(vcpu, kvm_run);
2221
2222 return r;
2223}
2224
2225
Avi Kivitybccf2152007-02-21 18:04:26 +02002226static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002227{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002228 int r;
Avi Kivity1961d272007-03-05 19:46:05 +02002229 sigset_t sigsaved;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002230
Avi Kivitybccf2152007-02-21 18:04:26 +02002231 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002232
He, Qingc5ec1532007-09-03 17:07:41 +03002233 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
2234 kvm_vcpu_block(vcpu);
2235 vcpu_put(vcpu);
2236 return -EAGAIN;
2237 }
2238
Avi Kivity1961d272007-03-05 19:46:05 +02002239 if (vcpu->sigset_active)
2240 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2241
Dor Laor54810342007-02-12 00:54:39 -08002242 /* re-sync apic's tpr */
He, Qing5cd4f6f2007-08-30 17:04:26 +08002243 if (!irqchip_in_kernel(vcpu->kvm))
2244 set_cr8(vcpu, kvm_run->cr8);
Dor Laor54810342007-02-12 00:54:39 -08002245
Avi Kivity02c83202007-04-29 15:02:17 +03002246 if (vcpu->pio.cur_count) {
2247 r = complete_pio(vcpu);
2248 if (r)
2249 goto out;
2250 }
Zhang Xiantao34c16ee2007-10-20 15:34:38 +08002251#if CONFIG_HAS_IOMEM
Avi Kivity02c83202007-04-29 15:02:17 +03002252 if (vcpu->mmio_needed) {
2253 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
2254 vcpu->mmio_read_completed = 1;
2255 vcpu->mmio_needed = 0;
2256 r = emulate_instruction(vcpu, kvm_run,
Laurent Vivier34273182007-09-18 11:27:37 +02002257 vcpu->mmio_fault_cr2, 0, 1);
Avi Kivity02c83202007-04-29 15:02:17 +03002258 if (r == EMULATE_DO_MMIO) {
2259 /*
2260 * Read-modify-write. Back to userspace.
2261 */
Avi Kivity02c83202007-04-29 15:02:17 +03002262 r = 0;
2263 goto out;
Avi Kivity46fc1472007-02-22 19:39:30 +02002264 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002265 }
Zhang Xiantao34c16ee2007-10-20 15:34:38 +08002266#endif
Avi Kivity8eb7d332007-03-04 14:17:08 +02002267 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002268 kvm_x86_ops->cache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002269 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002270 kvm_x86_ops->decache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002271 }
2272
Avi Kivity04d2cc72007-09-10 18:10:54 +03002273 r = __vcpu_run(vcpu, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002274
Avi Kivity039576c2007-03-20 12:46:50 +02002275out:
Avi Kivity1961d272007-03-05 19:46:05 +02002276 if (vcpu->sigset_active)
2277 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2278
Avi Kivity6aa8b732006-12-10 02:21:36 -08002279 vcpu_put(vcpu);
2280 return r;
2281}
2282
Avi Kivitybccf2152007-02-21 18:04:26 +02002283static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
2284 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002285{
Avi Kivitybccf2152007-02-21 18:04:26 +02002286 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002287
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002288 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002289
2290 regs->rax = vcpu->regs[VCPU_REGS_RAX];
2291 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
2292 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
2293 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
2294 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
2295 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
2296 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
2297 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002298#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002299 regs->r8 = vcpu->regs[VCPU_REGS_R8];
2300 regs->r9 = vcpu->regs[VCPU_REGS_R9];
2301 regs->r10 = vcpu->regs[VCPU_REGS_R10];
2302 regs->r11 = vcpu->regs[VCPU_REGS_R11];
2303 regs->r12 = vcpu->regs[VCPU_REGS_R12];
2304 regs->r13 = vcpu->regs[VCPU_REGS_R13];
2305 regs->r14 = vcpu->regs[VCPU_REGS_R14];
2306 regs->r15 = vcpu->regs[VCPU_REGS_R15];
2307#endif
2308
2309 regs->rip = vcpu->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002310 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002311
2312 /*
2313 * Don't leak debug flags in case they were set for guest debugging
2314 */
2315 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2316 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2317
2318 vcpu_put(vcpu);
2319
2320 return 0;
2321}
2322
Avi Kivitybccf2152007-02-21 18:04:26 +02002323static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
2324 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002325{
Avi Kivitybccf2152007-02-21 18:04:26 +02002326 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002327
2328 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
2329 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
2330 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
2331 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
2332 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
2333 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
2334 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
2335 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002336#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002337 vcpu->regs[VCPU_REGS_R8] = regs->r8;
2338 vcpu->regs[VCPU_REGS_R9] = regs->r9;
2339 vcpu->regs[VCPU_REGS_R10] = regs->r10;
2340 vcpu->regs[VCPU_REGS_R11] = regs->r11;
2341 vcpu->regs[VCPU_REGS_R12] = regs->r12;
2342 vcpu->regs[VCPU_REGS_R13] = regs->r13;
2343 vcpu->regs[VCPU_REGS_R14] = regs->r14;
2344 vcpu->regs[VCPU_REGS_R15] = regs->r15;
2345#endif
2346
2347 vcpu->rip = regs->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002348 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002349
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002350 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002351
2352 vcpu_put(vcpu);
2353
2354 return 0;
2355}
2356
2357static void get_segment(struct kvm_vcpu *vcpu,
2358 struct kvm_segment *var, int seg)
2359{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002360 return kvm_x86_ops->get_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002361}
2362
Avi Kivitybccf2152007-02-21 18:04:26 +02002363static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2364 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002365{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002366 struct descriptor_table dt;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002367 int pending_vec;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002368
Avi Kivitybccf2152007-02-21 18:04:26 +02002369 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002370
2371 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2372 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2373 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2374 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2375 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2376 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2377
2378 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2379 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2380
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002381 kvm_x86_ops->get_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002382 sregs->idt.limit = dt.limit;
2383 sregs->idt.base = dt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002384 kvm_x86_ops->get_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002385 sregs->gdt.limit = dt.limit;
2386 sregs->gdt.base = dt.base;
2387
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002388 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002389 sregs->cr0 = vcpu->cr0;
2390 sregs->cr2 = vcpu->cr2;
2391 sregs->cr3 = vcpu->cr3;
2392 sregs->cr4 = vcpu->cr4;
Eddie Dong7017fc32007-07-18 11:34:57 +03002393 sregs->cr8 = get_cr8(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002394 sregs->efer = vcpu->shadow_efer;
Eddie Dong7017fc32007-07-18 11:34:57 +03002395 sregs->apic_base = kvm_get_apic_base(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002396
Eddie Dong2a8067f2007-08-06 16:29:07 +03002397 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc52fb352007-08-02 14:03:07 +03002398 memset(sregs->interrupt_bitmap, 0,
2399 sizeof sregs->interrupt_bitmap);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002400 pending_vec = kvm_x86_ops->get_irq(vcpu);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002401 if (pending_vec >= 0)
Mike Dayd77c26f2007-10-08 09:02:08 -04002402 set_bit(pending_vec,
2403 (unsigned long *)sregs->interrupt_bitmap);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002404 } else
He, Qingc52fb352007-08-02 14:03:07 +03002405 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2406 sizeof sregs->interrupt_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002407
2408 vcpu_put(vcpu);
2409
2410 return 0;
2411}
2412
2413static void set_segment(struct kvm_vcpu *vcpu,
2414 struct kvm_segment *var, int seg)
2415{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002416 return kvm_x86_ops->set_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002417}
2418
Avi Kivitybccf2152007-02-21 18:04:26 +02002419static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2420 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002421{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002422 int mmu_reset_needed = 0;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002423 int i, pending_vec, max_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002424 struct descriptor_table dt;
2425
Avi Kivitybccf2152007-02-21 18:04:26 +02002426 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002427
Avi Kivity6aa8b732006-12-10 02:21:36 -08002428 dt.limit = sregs->idt.limit;
2429 dt.base = sregs->idt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002430 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002431 dt.limit = sregs->gdt.limit;
2432 dt.base = sregs->gdt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002433 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002434
2435 vcpu->cr2 = sregs->cr2;
2436 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2437 vcpu->cr3 = sregs->cr3;
2438
Eddie Dong7017fc32007-07-18 11:34:57 +03002439 set_cr8(vcpu, sregs->cr8);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002440
2441 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002442#ifdef CONFIG_X86_64
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002443 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002444#endif
Eddie Dong7017fc32007-07-18 11:34:57 +03002445 kvm_set_apic_base(vcpu, sregs->apic_base);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002446
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002447 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity399badf2007-01-05 16:36:38 -08002448
Avi Kivity6aa8b732006-12-10 02:21:36 -08002449 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
Rusty Russell81f50e32007-09-06 01:20:38 +10002450 vcpu->cr0 = sregs->cr0;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002451 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002452
2453 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002454 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Avi Kivity1b0973b2007-01-05 16:36:41 -08002455 if (!is_long_mode(vcpu) && is_pae(vcpu))
2456 load_pdptrs(vcpu, vcpu->cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002457
2458 if (mmu_reset_needed)
2459 kvm_mmu_reset_context(vcpu);
2460
He, Qingc52fb352007-08-02 14:03:07 +03002461 if (!irqchip_in_kernel(vcpu->kvm)) {
2462 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2463 sizeof vcpu->irq_pending);
2464 vcpu->irq_summary = 0;
2465 for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
2466 if (vcpu->irq_pending[i])
2467 __set_bit(i, &vcpu->irq_summary);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002468 } else {
2469 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
2470 pending_vec = find_first_bit(
2471 (const unsigned long *)sregs->interrupt_bitmap,
2472 max_bits);
2473 /* Only pending external irq is handled here */
2474 if (pending_vec < max_bits) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002475 kvm_x86_ops->set_irq(vcpu, pending_vec);
Mike Dayd77c26f2007-10-08 09:02:08 -04002476 pr_debug("Set back pending irq %d\n",
2477 pending_vec);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002478 }
He, Qingc52fb352007-08-02 14:03:07 +03002479 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002480
Avi Kivity024aa1c2007-03-21 13:44:58 +02002481 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2482 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2483 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2484 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2485 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2486 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2487
2488 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2489 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2490
Avi Kivity6aa8b732006-12-10 02:21:36 -08002491 vcpu_put(vcpu);
2492
2493 return 0;
2494}
2495
Rusty Russell1747fb72007-09-06 01:21:32 +10002496void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2497{
2498 struct kvm_segment cs;
2499
2500 get_segment(vcpu, &cs, VCPU_SREG_CS);
2501 *db = cs.db;
2502 *l = cs.l;
2503}
2504EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2505
Avi Kivity6aa8b732006-12-10 02:21:36 -08002506/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002507 * Translate a guest virtual address to a guest physical address.
2508 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002509static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2510 struct kvm_translation *tr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002511{
2512 unsigned long vaddr = tr->linear_address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002513 gpa_t gpa;
2514
Avi Kivitybccf2152007-02-21 18:04:26 +02002515 vcpu_load(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +08002516 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002517 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2518 tr->physical_address = gpa;
2519 tr->valid = gpa != UNMAPPED_GVA;
2520 tr->writeable = 1;
2521 tr->usermode = 0;
Shaohua Li11ec2802007-07-23 14:51:37 +08002522 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002523 vcpu_put(vcpu);
2524
2525 return 0;
2526}
2527
Avi Kivitybccf2152007-02-21 18:04:26 +02002528static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2529 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002530{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002531 if (irq->irq < 0 || irq->irq >= 256)
2532 return -EINVAL;
Eddie Dong97222cc2007-09-12 10:58:04 +03002533 if (irqchip_in_kernel(vcpu->kvm))
2534 return -ENXIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02002535 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002536
2537 set_bit(irq->irq, vcpu->irq_pending);
2538 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2539
2540 vcpu_put(vcpu);
2541
2542 return 0;
2543}
2544
Avi Kivitybccf2152007-02-21 18:04:26 +02002545static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2546 struct kvm_debug_guest *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002547{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002548 int r;
2549
Avi Kivitybccf2152007-02-21 18:04:26 +02002550 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002551
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002552 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002553
2554 vcpu_put(vcpu);
2555
2556 return r;
2557}
2558
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002559static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2560 unsigned long address,
2561 int *type)
2562{
2563 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2564 unsigned long pgoff;
2565 struct page *page;
2566
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002567 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +02002568 if (pgoff == 0)
2569 page = virt_to_page(vcpu->run);
2570 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2571 page = virt_to_page(vcpu->pio_data);
2572 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002573 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002574 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002575 if (type != NULL)
2576 *type = VM_FAULT_MINOR;
2577
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002578 return page;
2579}
2580
2581static struct vm_operations_struct kvm_vcpu_vm_ops = {
2582 .nopage = kvm_vcpu_nopage,
2583};
2584
2585static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2586{
2587 vma->vm_ops = &kvm_vcpu_vm_ops;
2588 return 0;
2589}
2590
Avi Kivitybccf2152007-02-21 18:04:26 +02002591static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2592{
2593 struct kvm_vcpu *vcpu = filp->private_data;
2594
2595 fput(vcpu->kvm->filp);
2596 return 0;
2597}
2598
2599static struct file_operations kvm_vcpu_fops = {
2600 .release = kvm_vcpu_release,
2601 .unlocked_ioctl = kvm_vcpu_ioctl,
2602 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002603 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02002604};
2605
2606/*
2607 * Allocates an inode for the vcpu.
2608 */
2609static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2610{
2611 int fd, r;
2612 struct inode *inode;
2613 struct file *file;
2614
Avi Kivityd6d28162007-06-28 08:38:16 -04002615 r = anon_inode_getfd(&fd, &inode, &file,
2616 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2617 if (r)
2618 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002619 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +02002620 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02002621}
2622
Avi Kivityc5ea7662007-02-20 18:41:05 +02002623/*
2624 * Creates some virtual cpus. Good luck creating more than one.
2625 */
2626static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2627{
2628 int r;
2629 struct kvm_vcpu *vcpu;
2630
Avi Kivityc5ea7662007-02-20 18:41:05 +02002631 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002632 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002633
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002634 vcpu = kvm_x86_ops->vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002635 if (IS_ERR(vcpu))
2636 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002637
Avi Kivity15ad7142007-07-11 18:17:21 +03002638 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
2639
Rusty Russellb114b082007-07-30 21:13:43 +10002640 /* We do fxsave: this must be aligned. */
2641 BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF);
2642
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002643 vcpu_load(vcpu);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002644 r = kvm_x86_ops->vcpu_reset(vcpu);
2645 if (r == 0)
2646 r = kvm_mmu_setup(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002647 vcpu_put(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002648 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002649 goto free_vcpu;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002650
Shaohua Li11ec2802007-07-23 14:51:37 +08002651 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002652 if (kvm->vcpus[n]) {
2653 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +08002654 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002655 goto mmu_unload;
2656 }
2657 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08002658 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002659
2660 /* Now it's all set up, let userspace reach it */
Avi Kivitybccf2152007-02-21 18:04:26 +02002661 r = create_vcpu_fd(vcpu);
2662 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002663 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02002664 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002665
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002666unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08002667 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002668 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +08002669 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002670
2671mmu_unload:
2672 vcpu_load(vcpu);
2673 kvm_mmu_unload(vcpu);
2674 vcpu_put(vcpu);
2675
2676free_vcpu:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002677 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002678 return r;
2679}
2680
Avi Kivity1961d272007-03-05 19:46:05 +02002681static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2682{
2683 if (sigset) {
2684 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2685 vcpu->sigset_active = 1;
2686 vcpu->sigset = *sigset;
2687 } else
2688 vcpu->sigset_active = 0;
2689 return 0;
2690}
2691
Avi Kivityb8836732007-04-01 16:34:31 +03002692/*
2693 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2694 * we have asm/x86/processor.h
2695 */
2696struct fxsave {
2697 u16 cwd;
2698 u16 swd;
2699 u16 twd;
2700 u16 fop;
2701 u64 rip;
2702 u64 rdp;
2703 u32 mxcsr;
2704 u32 mxcsr_mask;
2705 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2706#ifdef CONFIG_X86_64
2707 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2708#else
2709 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2710#endif
2711};
2712
2713static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2714{
Rusty Russellb114b082007-07-30 21:13:43 +10002715 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002716
2717 vcpu_load(vcpu);
2718
2719 memcpy(fpu->fpr, fxsave->st_space, 128);
2720 fpu->fcw = fxsave->cwd;
2721 fpu->fsw = fxsave->swd;
2722 fpu->ftwx = fxsave->twd;
2723 fpu->last_opcode = fxsave->fop;
2724 fpu->last_ip = fxsave->rip;
2725 fpu->last_dp = fxsave->rdp;
2726 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2727
2728 vcpu_put(vcpu);
2729
2730 return 0;
2731}
2732
2733static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2734{
Rusty Russellb114b082007-07-30 21:13:43 +10002735 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002736
2737 vcpu_load(vcpu);
2738
2739 memcpy(fxsave->st_space, fpu->fpr, 128);
2740 fxsave->cwd = fpu->fcw;
2741 fxsave->swd = fpu->fsw;
2742 fxsave->twd = fpu->ftwx;
2743 fxsave->fop = fpu->last_opcode;
2744 fxsave->rip = fpu->last_ip;
2745 fxsave->rdp = fpu->last_dp;
2746 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2747
2748 vcpu_put(vcpu);
2749
2750 return 0;
2751}
2752
Avi Kivitybccf2152007-02-21 18:04:26 +02002753static long kvm_vcpu_ioctl(struct file *filp,
2754 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002755{
Avi Kivitybccf2152007-02-21 18:04:26 +02002756 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00002757 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02002758 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002759
2760 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002761 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002762 r = -EINVAL;
2763 if (arg)
2764 goto out;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002765 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002766 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002767 case KVM_GET_REGS: {
2768 struct kvm_regs kvm_regs;
2769
Avi Kivitybccf2152007-02-21 18:04:26 +02002770 memset(&kvm_regs, 0, sizeof kvm_regs);
2771 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002772 if (r)
2773 goto out;
2774 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002775 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002776 goto out;
2777 r = 0;
2778 break;
2779 }
2780 case KVM_SET_REGS: {
2781 struct kvm_regs kvm_regs;
2782
2783 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002784 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002785 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002786 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002787 if (r)
2788 goto out;
2789 r = 0;
2790 break;
2791 }
2792 case KVM_GET_SREGS: {
2793 struct kvm_sregs kvm_sregs;
2794
Avi Kivitybccf2152007-02-21 18:04:26 +02002795 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2796 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002797 if (r)
2798 goto out;
2799 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002800 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002801 goto out;
2802 r = 0;
2803 break;
2804 }
2805 case KVM_SET_SREGS: {
2806 struct kvm_sregs kvm_sregs;
2807
2808 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002809 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002810 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002811 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002812 if (r)
2813 goto out;
2814 r = 0;
2815 break;
2816 }
2817 case KVM_TRANSLATE: {
2818 struct kvm_translation tr;
2819
2820 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002821 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002822 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002823 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002824 if (r)
2825 goto out;
2826 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002827 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002828 goto out;
2829 r = 0;
2830 break;
2831 }
2832 case KVM_INTERRUPT: {
2833 struct kvm_interrupt irq;
2834
2835 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002836 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002837 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002838 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002839 if (r)
2840 goto out;
2841 r = 0;
2842 break;
2843 }
2844 case KVM_DEBUG_GUEST: {
2845 struct kvm_debug_guest dbg;
2846
2847 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002848 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002849 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002850 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002851 if (r)
2852 goto out;
2853 r = 0;
2854 break;
2855 }
Avi Kivity1961d272007-03-05 19:46:05 +02002856 case KVM_SET_SIGNAL_MASK: {
2857 struct kvm_signal_mask __user *sigmask_arg = argp;
2858 struct kvm_signal_mask kvm_sigmask;
2859 sigset_t sigset, *p;
2860
2861 p = NULL;
2862 if (argp) {
2863 r = -EFAULT;
2864 if (copy_from_user(&kvm_sigmask, argp,
2865 sizeof kvm_sigmask))
2866 goto out;
2867 r = -EINVAL;
2868 if (kvm_sigmask.len != sizeof sigset)
2869 goto out;
2870 r = -EFAULT;
2871 if (copy_from_user(&sigset, sigmask_arg->sigset,
2872 sizeof sigset))
2873 goto out;
2874 p = &sigset;
2875 }
2876 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2877 break;
2878 }
Avi Kivityb8836732007-04-01 16:34:31 +03002879 case KVM_GET_FPU: {
2880 struct kvm_fpu fpu;
2881
2882 memset(&fpu, 0, sizeof fpu);
2883 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2884 if (r)
2885 goto out;
2886 r = -EFAULT;
2887 if (copy_to_user(argp, &fpu, sizeof fpu))
2888 goto out;
2889 r = 0;
2890 break;
2891 }
2892 case KVM_SET_FPU: {
2893 struct kvm_fpu fpu;
2894
2895 r = -EFAULT;
2896 if (copy_from_user(&fpu, argp, sizeof fpu))
2897 goto out;
2898 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2899 if (r)
2900 goto out;
2901 r = 0;
2902 break;
2903 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002904 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02002905 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02002906 }
2907out:
2908 return r;
2909}
2910
2911static long kvm_vm_ioctl(struct file *filp,
2912 unsigned int ioctl, unsigned long arg)
2913{
2914 struct kvm *kvm = filp->private_data;
2915 void __user *argp = (void __user *)arg;
2916 int r = -EINVAL;
2917
2918 switch (ioctl) {
2919 case KVM_CREATE_VCPU:
2920 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2921 if (r < 0)
2922 goto out;
2923 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002924 case KVM_SET_MEMORY_REGION: {
2925 struct kvm_memory_region kvm_mem;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002926 struct kvm_userspace_memory_region kvm_userspace_mem;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002927
2928 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002929 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002930 goto out;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002931 kvm_userspace_mem.slot = kvm_mem.slot;
2932 kvm_userspace_mem.flags = kvm_mem.flags;
2933 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
2934 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
2935 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
2936 if (r)
2937 goto out;
2938 break;
2939 }
2940 case KVM_SET_USER_MEMORY_REGION: {
2941 struct kvm_userspace_memory_region kvm_userspace_mem;
2942
2943 r = -EFAULT;
2944 if (copy_from_user(&kvm_userspace_mem, argp,
2945 sizeof kvm_userspace_mem))
2946 goto out;
2947
2948 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002949 if (r)
2950 goto out;
2951 break;
2952 }
Izik Eidus82ce2c92007-10-02 18:52:55 +02002953 case KVM_SET_NR_MMU_PAGES:
2954 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
2955 if (r)
2956 goto out;
2957 break;
2958 case KVM_GET_NR_MMU_PAGES:
2959 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
2960 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002961 case KVM_GET_DIRTY_LOG: {
2962 struct kvm_dirty_log log;
2963
2964 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002965 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002966 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002967 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002968 if (r)
2969 goto out;
2970 break;
2971 }
Avi Kivitye8207542007-03-30 16:54:30 +03002972 case KVM_SET_MEMORY_ALIAS: {
2973 struct kvm_memory_alias alias;
2974
2975 r = -EFAULT;
2976 if (copy_from_user(&alias, argp, sizeof alias))
2977 goto out;
2978 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
2979 if (r)
2980 goto out;
2981 break;
2982 }
Eddie Dong85f455f2007-07-06 12:20:49 +03002983 case KVM_CREATE_IRQCHIP:
2984 r = -ENOMEM;
2985 kvm->vpic = kvm_create_pic(kvm);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03002986 if (kvm->vpic) {
2987 r = kvm_ioapic_init(kvm);
2988 if (r) {
2989 kfree(kvm->vpic);
2990 kvm->vpic = NULL;
2991 goto out;
2992 }
Mike Dayd77c26f2007-10-08 09:02:08 -04002993 } else
Eddie Dong85f455f2007-07-06 12:20:49 +03002994 goto out;
2995 break;
2996 case KVM_IRQ_LINE: {
2997 struct kvm_irq_level irq_event;
2998
2999 r = -EFAULT;
3000 if (copy_from_user(&irq_event, argp, sizeof irq_event))
3001 goto out;
3002 if (irqchip_in_kernel(kvm)) {
Eddie Dong9cf98822007-07-22 10:36:31 +03003003 mutex_lock(&kvm->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +03003004 if (irq_event.irq < 16)
3005 kvm_pic_set_irq(pic_irqchip(kvm),
3006 irq_event.irq,
3007 irq_event.level);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03003008 kvm_ioapic_set_irq(kvm->vioapic,
3009 irq_event.irq,
3010 irq_event.level);
Eddie Dong9cf98822007-07-22 10:36:31 +03003011 mutex_unlock(&kvm->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +03003012 r = 0;
3013 }
3014 break;
3015 }
He, Qing6ceb9d72007-07-26 11:05:18 +03003016 case KVM_GET_IRQCHIP: {
3017 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3018 struct kvm_irqchip chip;
3019
3020 r = -EFAULT;
3021 if (copy_from_user(&chip, argp, sizeof chip))
3022 goto out;
3023 r = -ENXIO;
3024 if (!irqchip_in_kernel(kvm))
3025 goto out;
3026 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
3027 if (r)
3028 goto out;
3029 r = -EFAULT;
3030 if (copy_to_user(argp, &chip, sizeof chip))
3031 goto out;
3032 r = 0;
3033 break;
3034 }
3035 case KVM_SET_IRQCHIP: {
3036 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3037 struct kvm_irqchip chip;
3038
3039 r = -EFAULT;
3040 if (copy_from_user(&chip, argp, sizeof chip))
3041 goto out;
3042 r = -ENXIO;
3043 if (!irqchip_in_kernel(kvm))
3044 goto out;
3045 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
3046 if (r)
3047 goto out;
3048 r = 0;
3049 break;
3050 }
Avi Kivityf17abe92007-02-21 19:28:04 +02003051 default:
3052 ;
3053 }
3054out:
3055 return r;
3056}
3057
3058static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
3059 unsigned long address,
3060 int *type)
3061{
3062 struct kvm *kvm = vma->vm_file->private_data;
3063 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02003064 struct page *page;
3065
Avi Kivityf17abe92007-02-21 19:28:04 +02003066 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity954bbbc2007-03-30 14:02:32 +03003067 page = gfn_to_page(kvm, pgoff);
Izik Eidus8a7ae052007-10-18 11:09:33 +02003068 if (is_error_page(page)) {
3069 kvm_release_page(page);
Avi Kivityf17abe92007-02-21 19:28:04 +02003070 return NOPAGE_SIGBUS;
Izik Eidus8a7ae052007-10-18 11:09:33 +02003071 }
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03003072 if (type != NULL)
3073 *type = VM_FAULT_MINOR;
3074
Avi Kivityf17abe92007-02-21 19:28:04 +02003075 return page;
3076}
3077
3078static struct vm_operations_struct kvm_vm_vm_ops = {
3079 .nopage = kvm_vm_nopage,
3080};
3081
3082static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
3083{
3084 vma->vm_ops = &kvm_vm_vm_ops;
3085 return 0;
3086}
3087
3088static struct file_operations kvm_vm_fops = {
3089 .release = kvm_vm_release,
3090 .unlocked_ioctl = kvm_vm_ioctl,
3091 .compat_ioctl = kvm_vm_ioctl,
3092 .mmap = kvm_vm_mmap,
3093};
3094
3095static int kvm_dev_ioctl_create_vm(void)
3096{
3097 int fd, r;
3098 struct inode *inode;
3099 struct file *file;
3100 struct kvm *kvm;
3101
Avi Kivityf17abe92007-02-21 19:28:04 +02003102 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04003103 if (IS_ERR(kvm))
3104 return PTR_ERR(kvm);
3105 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
3106 if (r) {
3107 kvm_destroy_vm(kvm);
3108 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02003109 }
3110
Avi Kivitybccf2152007-02-21 18:04:26 +02003111 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02003112
Avi Kivityf17abe92007-02-21 19:28:04 +02003113 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02003114}
3115
3116static long kvm_dev_ioctl(struct file *filp,
3117 unsigned int ioctl, unsigned long arg)
3118{
3119 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02003120 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02003121
3122 switch (ioctl) {
3123 case KVM_GET_API_VERSION:
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_API_VERSION;
3128 break;
3129 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02003130 r = -EINVAL;
3131 if (arg)
3132 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02003133 r = kvm_dev_ioctl_create_vm();
3134 break;
Eddie Dong85f455f2007-07-06 12:20:49 +03003135 case KVM_CHECK_EXTENSION: {
3136 int ext = (long)argp;
3137
3138 switch (ext) {
3139 case KVM_CAP_IRQCHIP:
Eddie Dongb6958ce2007-07-18 12:15:21 +03003140 case KVM_CAP_HLT:
Izik Eidus82ce2c92007-10-02 18:52:55 +02003141 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Izik Eidus6fc138d2007-10-09 19:20:39 +02003142 case KVM_CAP_USER_MEMORY:
Eddie Dong85f455f2007-07-06 12:20:49 +03003143 r = 1;
3144 break;
3145 default:
3146 r = 0;
3147 break;
3148 }
Avi Kivity5d308f42007-03-01 17:56:20 +02003149 break;
Eddie Dong85f455f2007-07-06 12:20:49 +03003150 }
Avi Kivity07c45a32007-03-07 13:05:38 +02003151 case KVM_GET_VCPU_MMAP_SIZE:
3152 r = -EINVAL;
3153 if (arg)
3154 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02003155 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02003156 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003157 default:
Carsten Otte043405e2007-10-10 17:16:19 +02003158 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003159 }
3160out:
3161 return r;
3162}
3163
Avi Kivity6aa8b732006-12-10 02:21:36 -08003164static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003165 .unlocked_ioctl = kvm_dev_ioctl,
3166 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003167};
3168
3169static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02003170 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003171 "kvm",
3172 &kvm_chardev_ops,
3173};
3174
Avi Kivity774c47f2007-02-12 00:54:47 -08003175/*
3176 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
3177 * cached on it.
3178 */
3179static void decache_vcpus_on_cpu(int cpu)
3180{
3181 struct kvm *vm;
3182 struct kvm_vcpu *vcpu;
3183 int i;
3184
3185 spin_lock(&kvm_lock);
Shaohua Li11ec2802007-07-23 14:51:37 +08003186 list_for_each_entry(vm, &vm_list, vm_list)
Avi Kivity774c47f2007-02-12 00:54:47 -08003187 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003188 vcpu = vm->vcpus[i];
3189 if (!vcpu)
3190 continue;
Avi Kivity774c47f2007-02-12 00:54:47 -08003191 /*
3192 * If the vcpu is locked, then it is running on some
3193 * other cpu and therefore it is not cached on the
3194 * cpu in question.
3195 *
3196 * If it's not locked, check the last cpu it executed
3197 * on.
3198 */
3199 if (mutex_trylock(&vcpu->mutex)) {
3200 if (vcpu->cpu == cpu) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003201 kvm_x86_ops->vcpu_decache(vcpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08003202 vcpu->cpu = -1;
3203 }
3204 mutex_unlock(&vcpu->mutex);
3205 }
3206 }
3207 spin_unlock(&kvm_lock);
3208}
3209
Avi Kivity1b6c0162007-05-24 13:03:52 +03003210static void hardware_enable(void *junk)
3211{
3212 int cpu = raw_smp_processor_id();
3213
3214 if (cpu_isset(cpu, cpus_hardware_enabled))
3215 return;
3216 cpu_set(cpu, cpus_hardware_enabled);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003217 kvm_x86_ops->hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003218}
3219
3220static void hardware_disable(void *junk)
3221{
3222 int cpu = raw_smp_processor_id();
3223
3224 if (!cpu_isset(cpu, cpus_hardware_enabled))
3225 return;
3226 cpu_clear(cpu, cpus_hardware_enabled);
3227 decache_vcpus_on_cpu(cpu);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003228 kvm_x86_ops->hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003229}
3230
Avi Kivity774c47f2007-02-12 00:54:47 -08003231static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
3232 void *v)
3233{
3234 int cpu = (long)v;
3235
3236 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03003237 case CPU_DYING:
3238 case CPU_DYING_FROZEN:
Avi Kivity6ec8a8562007-08-19 15:57:26 +03003239 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3240 cpu);
3241 hardware_disable(NULL);
3242 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08003243 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003244 case CPU_UP_CANCELED_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003245 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3246 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003247 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003248 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02003249 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003250 case CPU_ONLINE_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003251 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
3252 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003253 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003254 break;
3255 }
3256 return NOTIFY_OK;
3257}
3258
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003259static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04003260 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003261{
3262 if (val == SYS_RESTART) {
3263 /*
3264 * Some (well, at least mine) BIOSes hang on reboot if
3265 * in vmx root mode.
3266 */
3267 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
3268 on_each_cpu(hardware_disable, NULL, 0, 1);
3269 }
3270 return NOTIFY_OK;
3271}
3272
3273static struct notifier_block kvm_reboot_notifier = {
3274 .notifier_call = kvm_reboot,
3275 .priority = 0,
3276};
3277
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04003278void kvm_io_bus_init(struct kvm_io_bus *bus)
3279{
3280 memset(bus, 0, sizeof(*bus));
3281}
3282
3283void kvm_io_bus_destroy(struct kvm_io_bus *bus)
3284{
3285 int i;
3286
3287 for (i = 0; i < bus->dev_count; i++) {
3288 struct kvm_io_device *pos = bus->devs[i];
3289
3290 kvm_iodevice_destructor(pos);
3291 }
3292}
3293
3294struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
3295{
3296 int i;
3297
3298 for (i = 0; i < bus->dev_count; i++) {
3299 struct kvm_io_device *pos = bus->devs[i];
3300
3301 if (pos->in_range(pos, addr))
3302 return pos;
3303 }
3304
3305 return NULL;
3306}
3307
3308void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
3309{
3310 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
3311
3312 bus->devs[bus->dev_count++] = dev;
3313}
3314
Avi Kivity774c47f2007-02-12 00:54:47 -08003315static struct notifier_block kvm_cpu_notifier = {
3316 .notifier_call = kvm_cpu_hotplug,
3317 .priority = 20, /* must be > scheduler priority */
3318};
3319
Avi Kivity1165f5f2007-04-19 17:27:43 +03003320static u64 stat_get(void *_offset)
3321{
3322 unsigned offset = (long)_offset;
3323 u64 total = 0;
3324 struct kvm *kvm;
3325 struct kvm_vcpu *vcpu;
3326 int i;
3327
3328 spin_lock(&kvm_lock);
3329 list_for_each_entry(kvm, &vm_list, vm_list)
3330 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003331 vcpu = kvm->vcpus[i];
3332 if (vcpu)
3333 total += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03003334 }
3335 spin_unlock(&kvm_lock);
3336 return total;
3337}
3338
Rusty Russell3dea7ca2007-08-01 10:12:22 +10003339DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n");
Avi Kivity1165f5f2007-04-19 17:27:43 +03003340
Avi Kivity6aa8b732006-12-10 02:21:36 -08003341static __init void kvm_init_debug(void)
3342{
3343 struct kvm_stats_debugfs_item *p;
3344
Al Viro8b6d44c2007-02-09 16:38:40 +00003345 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003346 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03003347 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
3348 (void *)(long)p->offset,
3349 &stat_fops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003350}
3351
3352static void kvm_exit_debug(void)
3353{
3354 struct kvm_stats_debugfs_item *p;
3355
3356 for (p = debugfs_entries; p->name; ++p)
3357 debugfs_remove(p->dentry);
3358 debugfs_remove(debugfs_dir);
3359}
3360
Avi Kivity59ae6c62007-02-12 00:54:48 -08003361static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3362{
Avi Kivity4267c412007-05-24 13:09:41 +03003363 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003364 return 0;
3365}
3366
3367static int kvm_resume(struct sys_device *dev)
3368{
Avi Kivity4267c412007-05-24 13:09:41 +03003369 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003370 return 0;
3371}
3372
3373static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01003374 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08003375 .suspend = kvm_suspend,
3376 .resume = kvm_resume,
3377};
3378
3379static struct sys_device kvm_sysdev = {
3380 .id = 0,
3381 .cls = &kvm_sysdev_class,
3382};
3383
Izik Eiduscea7bb22007-10-17 19:17:48 +02003384struct page *bad_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003385
Avi Kivity15ad7142007-07-11 18:17:21 +03003386static inline
3387struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
3388{
3389 return container_of(pn, struct kvm_vcpu, preempt_notifier);
3390}
3391
3392static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
3393{
3394 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3395
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003396 kvm_x86_ops->vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003397}
3398
3399static void kvm_sched_out(struct preempt_notifier *pn,
3400 struct task_struct *next)
3401{
3402 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3403
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003404 kvm_x86_ops->vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003405}
3406
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003407int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10003408 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003409{
3410 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003411 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003412
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003413 if (kvm_x86_ops) {
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08003414 printk(KERN_ERR "kvm: already loaded the other module\n");
3415 return -EEXIST;
3416 }
3417
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003418 if (!ops->cpu_has_kvm_support()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003419 printk(KERN_ERR "kvm: no hardware support\n");
3420 return -EOPNOTSUPP;
3421 }
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003422 if (ops->disabled_by_bios()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003423 printk(KERN_ERR "kvm: disabled by bios\n");
3424 return -EOPNOTSUPP;
3425 }
3426
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003427 kvm_x86_ops = ops;
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003428
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003429 r = kvm_x86_ops->hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003430 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02003431 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003432
Yang, Sheng002c7f72007-07-31 14:23:01 +03003433 for_each_online_cpu(cpu) {
3434 smp_call_function_single(cpu,
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003435 kvm_x86_ops->check_processor_compatibility,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003436 &r, 0, 1);
3437 if (r < 0)
3438 goto out_free_0;
3439 }
3440
Avi Kivity1b6c0162007-05-24 13:03:52 +03003441 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003442 r = register_cpu_notifier(&kvm_cpu_notifier);
3443 if (r)
3444 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003445 register_reboot_notifier(&kvm_reboot_notifier);
3446
Avi Kivity59ae6c62007-02-12 00:54:48 -08003447 r = sysdev_class_register(&kvm_sysdev_class);
3448 if (r)
3449 goto out_free_2;
3450
3451 r = sysdev_register(&kvm_sysdev);
3452 if (r)
3453 goto out_free_3;
3454
Rusty Russellc16f8622007-07-30 21:12:19 +10003455 /* A kmem cache lets us meet the alignment requirements of fx_save. */
3456 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
3457 __alignof__(struct kvm_vcpu), 0, 0);
3458 if (!kvm_vcpu_cache) {
3459 r = -ENOMEM;
3460 goto out_free_4;
3461 }
3462
Avi Kivity6aa8b732006-12-10 02:21:36 -08003463 kvm_chardev_ops.owner = module;
3464
3465 r = misc_register(&kvm_dev);
3466 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04003467 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08003468 goto out_free;
3469 }
3470
Avi Kivity15ad7142007-07-11 18:17:21 +03003471 kvm_preempt_ops.sched_in = kvm_sched_in;
3472 kvm_preempt_ops.sched_out = kvm_sched_out;
3473
Avi Kivityc7addb92007-09-16 18:58:32 +02003474 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
3475
3476 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003477
3478out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10003479 kmem_cache_destroy(kvm_vcpu_cache);
3480out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08003481 sysdev_unregister(&kvm_sysdev);
3482out_free_3:
3483 sysdev_class_unregister(&kvm_sysdev_class);
3484out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003485 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08003486 unregister_cpu_notifier(&kvm_cpu_notifier);
3487out_free_1:
Avi Kivity1b6c0162007-05-24 13:03:52 +03003488 on_each_cpu(hardware_disable, NULL, 0, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03003489out_free_0:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003490 kvm_x86_ops->hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02003491out:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003492 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003493 return r;
3494}
Mike Dayd77c26f2007-10-08 09:02:08 -04003495EXPORT_SYMBOL_GPL(kvm_init_x86);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003496
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003497void kvm_exit_x86(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003498{
3499 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10003500 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003501 sysdev_unregister(&kvm_sysdev);
3502 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003503 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003504 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003505 on_each_cpu(hardware_disable, NULL, 0, 1);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003506 kvm_x86_ops->hardware_unsetup();
3507 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003508}
Mike Dayd77c26f2007-10-08 09:02:08 -04003509EXPORT_SYMBOL_GPL(kvm_exit_x86);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003510
3511static __init int kvm_init(void)
3512{
Avi Kivity37e29d92007-02-20 14:07:37 +02003513 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003514
Avi Kivityb5a33a72007-04-15 16:31:09 +03003515 r = kvm_mmu_module_init();
3516 if (r)
3517 goto out4;
3518
Avi Kivity6aa8b732006-12-10 02:21:36 -08003519 kvm_init_debug();
3520
Carsten Otte043405e2007-10-10 17:16:19 +02003521 kvm_arch_init();
Michael Riepebf591b22006-12-22 01:05:36 -08003522
Izik Eiduscea7bb22007-10-17 19:17:48 +02003523 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
Mike Dayd77c26f2007-10-08 09:02:08 -04003524
3525 if (bad_page == NULL) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003526 r = -ENOMEM;
3527 goto out;
3528 }
3529
Avi Kivity58e690e2007-02-26 16:29:43 +02003530 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003531
3532out:
3533 kvm_exit_debug();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003534 kvm_mmu_module_exit();
3535out4:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003536 return r;
3537}
3538
3539static __exit void kvm_exit(void)
3540{
3541 kvm_exit_debug();
Izik Eiduscea7bb22007-10-17 19:17:48 +02003542 __free_page(bad_page);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003543 kvm_mmu_module_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003544}
3545
3546module_init(kvm_init)
3547module_exit(kvm_exit)