blob: 5113cbf7567453e4d2ba331bb31e1e8fa7ffad43 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 *
9 * Authors:
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
17
18#include "kvm.h"
Carsten Otte043405e2007-10-10 17:16:19 +020019#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040020#include "x86_emulate.h"
21#include "segment_descriptor.h"
Eddie Dong85f455f2007-07-06 12:20:49 +030022#include "irq.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080023
24#include <linux/kvm.h>
25#include <linux/module.h>
26#include <linux/errno.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/percpu.h>
28#include <linux/gfp.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080029#include <linux/mm.h>
30#include <linux/miscdevice.h>
31#include <linux/vmalloc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080032#include <linux/reboot.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080033#include <linux/debugfs.h>
34#include <linux/highmem.h>
35#include <linux/file.h>
Avi Kivity59ae6c62007-02-12 00:54:48 -080036#include <linux/sysdev.h>
Avi Kivity774c47f2007-02-12 00:54:47 -080037#include <linux/cpu.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040038#include <linux/sched.h>
Avi Kivityd9e368d2007-06-07 19:18:30 +030039#include <linux/cpumask.h>
40#include <linux/smp.h>
Avi Kivityd6d28162007-06-28 08:38:16 -040041#include <linux/anon_inodes.h>
Avi Kivity04d2cc72007-09-10 18:10:54 +030042#include <linux/profile.h>
Anthony Liguori7aa81cc2007-09-17 14:57:50 -050043#include <linux/kvm_para.h>
Izik Eidus6fc138d2007-10-09 19:20:39 +020044#include <linux/pagemap.h>
Anthony Liguori8d4e1282007-10-18 09:59:34 -050045#include <linux/mman.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080046
Avi Kivitye4956062007-06-28 14:15:57 -040047#include <asm/processor.h>
48#include <asm/msr.h>
49#include <asm/io.h>
50#include <asm/uaccess.h>
51#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080052
53MODULE_AUTHOR("Qumranet");
54MODULE_LICENSE("GPL");
55
Avi Kivity133de902007-02-12 00:54:44 -080056static DEFINE_SPINLOCK(kvm_lock);
57static LIST_HEAD(vm_list);
58
Avi Kivity1b6c0162007-05-24 13:03:52 +030059static cpumask_t cpus_hardware_enabled;
60
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +030061struct kvm_x86_ops *kvm_x86_ops;
Rusty Russellc16f8622007-07-30 21:12:19 +100062struct kmem_cache *kvm_vcpu_cache;
63EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030064
Avi Kivity15ad7142007-07-11 18:17:21 +030065static __read_mostly struct preempt_ops kvm_preempt_ops;
66
Avi Kivity1165f5f2007-04-19 17:27:43 +030067#define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
Avi Kivity6aa8b732006-12-10 02:21:36 -080068
69static struct kvm_stats_debugfs_item {
70 const char *name;
Avi Kivity1165f5f2007-04-19 17:27:43 +030071 int offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -080072 struct dentry *dentry;
73} debugfs_entries[] = {
Avi Kivity1165f5f2007-04-19 17:27:43 +030074 { "pf_fixed", STAT_OFFSET(pf_fixed) },
75 { "pf_guest", STAT_OFFSET(pf_guest) },
76 { "tlb_flush", STAT_OFFSET(tlb_flush) },
77 { "invlpg", STAT_OFFSET(invlpg) },
78 { "exits", STAT_OFFSET(exits) },
79 { "io_exits", STAT_OFFSET(io_exits) },
80 { "mmio_exits", STAT_OFFSET(mmio_exits) },
81 { "signal_exits", STAT_OFFSET(signal_exits) },
82 { "irq_window", STAT_OFFSET(irq_window_exits) },
83 { "halt_exits", STAT_OFFSET(halt_exits) },
Eddie Dongb6958ce2007-07-18 12:15:21 +030084 { "halt_wakeup", STAT_OFFSET(halt_wakeup) },
Avi Kivity1165f5f2007-04-19 17:27:43 +030085 { "request_irq", STAT_OFFSET(request_irq_exits) },
86 { "irq_exits", STAT_OFFSET(irq_exits) },
Avi Kivitye6adf282007-04-30 16:07:54 +030087 { "light_exits", STAT_OFFSET(light_exits) },
Eddie Dong2cc51562007-05-21 07:28:09 +030088 { "efer_reload", STAT_OFFSET(efer_reload) },
Avi Kivity1165f5f2007-04-19 17:27:43 +030089 { NULL }
Avi Kivity6aa8b732006-12-10 02:21:36 -080090};
91
92static struct dentry *debugfs_dir;
93
Rusty Russell707d92f2007-07-17 23:19:08 +100094#define CR0_RESERVED_BITS \
95 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
96 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
97 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
Rusty Russell66aee912007-07-17 23:34:16 +100098#define CR4_RESERVED_BITS \
99 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
100 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
101 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
102 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
103
Rusty Russell7075bc82007-07-17 23:37:17 +1000104#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800105#define EFER_RESERVED_BITS 0xfffffffffffff2fe
106
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800107#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400108/* LDT or TSS descriptor in the GDT. 16 bytes. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800109struct segment_descriptor_64 {
110 struct segment_descriptor s;
111 u32 base_higher;
112 u32 pad_zero;
113};
114
115#endif
116
Avi Kivitybccf2152007-02-21 18:04:26 +0200117static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
118 unsigned long arg);
119
Avi Kivity6aa8b732006-12-10 02:21:36 -0800120unsigned long segment_base(u16 selector)
121{
122 struct descriptor_table gdt;
123 struct segment_descriptor *d;
124 unsigned long table_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800125 unsigned long v;
126
127 if (selector == 0)
128 return 0;
129
Mike Dayd77c26f2007-10-08 09:02:08 -0400130 asm("sgdt %0" : "=m"(gdt));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131 table_base = gdt.base;
132
133 if (selector & 4) { /* from ldt */
134 u16 ldt_selector;
135
Mike Dayd77c26f2007-10-08 09:02:08 -0400136 asm("sldt %0" : "=g"(ldt_selector));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800137 table_base = segment_base(ldt_selector);
138 }
139 d = (struct segment_descriptor *)(table_base + (selector & ~7));
Mike Dayd77c26f2007-10-08 09:02:08 -0400140 v = d->base_low | ((unsigned long)d->base_mid << 16) |
141 ((unsigned long)d->base_high << 24);
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800142#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400143 if (d->system == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
144 v |= ((unsigned long) \
145 ((struct segment_descriptor_64 *)d)->base_higher) << 32;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800146#endif
147 return v;
148}
149EXPORT_SYMBOL_GPL(segment_base);
150
James Morris5aacf0c2006-12-22 01:04:55 -0800151static inline int valid_vcpu(int n)
152{
153 return likely(n >= 0 && n < KVM_MAX_VCPUS);
154}
155
Avi Kivity7702fd12007-06-14 16:27:40 +0300156void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
157{
158 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
159 return;
160
161 vcpu->guest_fpu_loaded = 1;
Rusty Russellb114b082007-07-30 21:13:43 +1000162 fx_save(&vcpu->host_fx_image);
163 fx_restore(&vcpu->guest_fx_image);
Avi Kivity7702fd12007-06-14 16:27:40 +0300164}
165EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
166
167void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
168{
169 if (!vcpu->guest_fpu_loaded)
170 return;
171
172 vcpu->guest_fpu_loaded = 0;
Rusty Russellb114b082007-07-30 21:13:43 +1000173 fx_save(&vcpu->guest_fx_image);
174 fx_restore(&vcpu->host_fx_image);
Avi Kivity7702fd12007-06-14 16:27:40 +0300175}
176EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
177
Avi Kivity6aa8b732006-12-10 02:21:36 -0800178/*
179 * Switches to specified vcpu, until a matching vcpu_put()
180 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200181void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800182{
Avi Kivity15ad7142007-07-11 18:17:21 +0300183 int cpu;
184
Avi Kivitybccf2152007-02-21 18:04:26 +0200185 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300186 cpu = get_cpu();
187 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200188 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300189 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200190}
191
Carsten Otte313a3dc2007-10-11 19:16:52 +0200192void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800193{
Avi Kivity15ad7142007-07-11 18:17:21 +0300194 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200195 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300196 preempt_notifier_unregister(&vcpu->preempt_notifier);
197 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800198 mutex_unlock(&vcpu->mutex);
199}
200
Avi Kivityd9e368d2007-06-07 19:18:30 +0300201static void ack_flush(void *_completed)
202{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300203}
204
205void kvm_flush_remote_tlbs(struct kvm *kvm)
206{
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200207 int i, cpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300208 cpumask_t cpus;
209 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300210
Avi Kivityd9e368d2007-06-07 19:18:30 +0300211 cpus_clear(cpus);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000212 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
213 vcpu = kvm->vcpus[i];
214 if (!vcpu)
215 continue;
Avi Kivity3176bc32007-10-16 17:22:08 +0200216 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300217 continue;
218 cpu = vcpu->cpu;
219 if (cpu != -1 && cpu != raw_smp_processor_id())
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200220 cpu_set(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300221 }
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200222 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300223}
224
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000225int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
226{
227 struct page *page;
228 int r;
229
230 mutex_init(&vcpu->mutex);
231 vcpu->cpu = -1;
232 vcpu->mmu.root_hpa = INVALID_PAGE;
233 vcpu->kvm = kvm;
234 vcpu->vcpu_id = id;
He, Qingc5ec1532007-09-03 17:07:41 +0300235 if (!irqchip_in_kernel(kvm) || id == 0)
236 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
237 else
238 vcpu->mp_state = VCPU_MP_STATE_UNINITIALIZED;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300239 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000240
241 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
242 if (!page) {
243 r = -ENOMEM;
244 goto fail;
245 }
246 vcpu->run = page_address(page);
247
248 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
249 if (!page) {
250 r = -ENOMEM;
251 goto fail_free_run;
252 }
253 vcpu->pio_data = page_address(page);
254
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000255 r = kvm_mmu_create(vcpu);
256 if (r < 0)
257 goto fail_free_pio_data;
258
Rusty Russell76fafa52007-10-08 10:50:48 +1000259 if (irqchip_in_kernel(kvm)) {
260 r = kvm_create_lapic(vcpu);
261 if (r < 0)
262 goto fail_mmu_destroy;
263 }
264
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000265 return 0;
266
Rusty Russell76fafa52007-10-08 10:50:48 +1000267fail_mmu_destroy:
268 kvm_mmu_destroy(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000269fail_free_pio_data:
270 free_page((unsigned long)vcpu->pio_data);
271fail_free_run:
272 free_page((unsigned long)vcpu->run);
273fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000274 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000275}
276EXPORT_SYMBOL_GPL(kvm_vcpu_init);
277
278void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
279{
Rusty Russelld5894442007-10-08 10:48:30 +1000280 kvm_free_lapic(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000281 kvm_mmu_destroy(vcpu);
282 free_page((unsigned long)vcpu->pio_data);
283 free_page((unsigned long)vcpu->run);
284}
285EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
286
Avi Kivityf17abe92007-02-21 19:28:04 +0200287static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800288{
289 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800290
291 if (!kvm)
Avi Kivityf17abe92007-02-21 19:28:04 +0200292 return ERR_PTR(-ENOMEM);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800293
Eddie Dong74906342007-06-19 18:05:03 +0300294 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800295 mutex_init(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800296 INIT_LIST_HEAD(&kvm->active_mmu_pages);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400297 kvm_io_bus_init(&kvm->mmio_bus);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000298 spin_lock(&kvm_lock);
299 list_add(&kvm->vm_list, &vm_list);
300 spin_unlock(&kvm_lock);
Avi Kivityf17abe92007-02-21 19:28:04 +0200301 return kvm;
302}
303
Avi Kivity6aa8b732006-12-10 02:21:36 -0800304/*
305 * Free any memory in @free but not in @dont.
306 */
307static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
308 struct kvm_memory_slot *dont)
309{
Izik Eidus290fc382007-09-27 14:11:22 +0200310 if (!dont || free->rmap != dont->rmap)
311 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800312
313 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
314 vfree(free->dirty_bitmap);
315
Avi Kivity6aa8b732006-12-10 02:21:36 -0800316 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000317 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500318 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800319}
320
321static void kvm_free_physmem(struct kvm *kvm)
322{
323 int i;
324
325 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000326 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800327}
328
Avi Kivity039576c2007-03-20 12:46:50 +0200329static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
330{
331 int i;
332
Rusty Russell3077c4512007-07-30 16:41:57 +1000333 for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i)
Avi Kivity039576c2007-03-20 12:46:50 +0200334 if (vcpu->pio.guest_pages[i]) {
Izik Eidus8a7ae052007-10-18 11:09:33 +0200335 kvm_release_page(vcpu->pio.guest_pages[i]);
Avi Kivity039576c2007-03-20 12:46:50 +0200336 vcpu->pio.guest_pages[i] = NULL;
337 }
338}
339
Avi Kivity7b53aa52007-06-05 12:17:03 +0300340static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
341{
Avi Kivity7b53aa52007-06-05 12:17:03 +0300342 vcpu_load(vcpu);
343 kvm_mmu_unload(vcpu);
344 vcpu_put(vcpu);
345}
346
Avi Kivity6aa8b732006-12-10 02:21:36 -0800347static void kvm_free_vcpus(struct kvm *kvm)
348{
349 unsigned int i;
350
Avi Kivity7b53aa52007-06-05 12:17:03 +0300351 /*
352 * Unpin any mmu pages first.
353 */
354 for (i = 0; i < KVM_MAX_VCPUS; ++i)
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000355 if (kvm->vcpus[i])
356 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
357 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
358 if (kvm->vcpus[i]) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300359 kvm_x86_ops->vcpu_free(kvm->vcpus[i]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000360 kvm->vcpus[i] = NULL;
361 }
362 }
363
Avi Kivity6aa8b732006-12-10 02:21:36 -0800364}
365
Avi Kivityf17abe92007-02-21 19:28:04 +0200366static void kvm_destroy_vm(struct kvm *kvm)
367{
Avi Kivity133de902007-02-12 00:54:44 -0800368 spin_lock(&kvm_lock);
369 list_del(&kvm->vm_list);
370 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300371 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400372 kvm_io_bus_destroy(&kvm->mmio_bus);
Eddie Dong85f455f2007-07-06 12:20:49 +0300373 kfree(kvm->vpic);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300374 kfree(kvm->vioapic);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800375 kvm_free_vcpus(kvm);
376 kvm_free_physmem(kvm);
377 kfree(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200378}
379
380static int kvm_vm_release(struct inode *inode, struct file *filp)
381{
382 struct kvm *kvm = filp->private_data;
383
384 kvm_destroy_vm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800385 return 0;
386}
387
388static void inject_gp(struct kvm_vcpu *vcpu)
389{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300390 kvm_x86_ops->inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800391}
392
Avi Kivity1342d352007-01-05 16:36:39 -0800393/*
394 * Load the pae pdptrs. Return true is they are all valid.
395 */
396static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800397{
398 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
Avi Kivity1342d352007-01-05 16:36:39 -0800399 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800400 int i;
Avi Kivity1342d352007-01-05 16:36:39 -0800401 int ret;
Rusty Russellc820c2a2007-07-25 13:29:51 +1000402 u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800403
Shaohua Li11ec2802007-07-23 14:51:37 +0800404 mutex_lock(&vcpu->kvm->lock);
Izik Eidus195aefd2007-10-01 22:14:18 +0200405 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
406 offset * sizeof(u64), sizeof(pdpte));
407 if (ret < 0) {
Rusty Russellc820c2a2007-07-25 13:29:51 +1000408 ret = 0;
409 goto out;
410 }
Rusty Russellc820c2a2007-07-25 13:29:51 +1000411 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
412 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
Avi Kivity1342d352007-01-05 16:36:39 -0800413 ret = 0;
414 goto out;
415 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800416 }
Rusty Russellc820c2a2007-07-25 13:29:51 +1000417 ret = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800418
Rusty Russellc820c2a2007-07-25 13:29:51 +1000419 memcpy(vcpu->pdptrs, pdpte, sizeof(vcpu->pdptrs));
Avi Kivity1342d352007-01-05 16:36:39 -0800420out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800421 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800422
Avi Kivity1342d352007-01-05 16:36:39 -0800423 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424}
425
426void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
427{
Rusty Russell707d92f2007-07-17 23:19:08 +1000428 if (cr0 & CR0_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800429 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
430 cr0, vcpu->cr0);
431 inject_gp(vcpu);
432 return;
433 }
434
Rusty Russell707d92f2007-07-17 23:19:08 +1000435 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800436 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
437 inject_gp(vcpu);
438 return;
439 }
440
Rusty Russell707d92f2007-07-17 23:19:08 +1000441 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800442 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
443 "and a clear PE flag\n");
444 inject_gp(vcpu);
445 return;
446 }
447
Rusty Russell707d92f2007-07-17 23:19:08 +1000448 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800449#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800450 if ((vcpu->shadow_efer & EFER_LME)) {
451 int cs_db, cs_l;
452
453 if (!is_pae(vcpu)) {
454 printk(KERN_DEBUG "set_cr0: #GP, start paging "
455 "in long mode while PAE is disabled\n");
456 inject_gp(vcpu);
457 return;
458 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300459 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800460 if (cs_l) {
461 printk(KERN_DEBUG "set_cr0: #GP, start paging "
462 "in long mode while CS.L == 1\n");
463 inject_gp(vcpu);
464 return;
465
466 }
467 } else
468#endif
Avi Kivity1342d352007-01-05 16:36:39 -0800469 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800470 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
471 "reserved bits\n");
472 inject_gp(vcpu);
473 return;
474 }
475
476 }
477
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300478 kvm_x86_ops->set_cr0(vcpu, cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800479 vcpu->cr0 = cr0;
480
Shaohua Li11ec2802007-07-23 14:51:37 +0800481 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800482 kvm_mmu_reset_context(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800483 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800484 return;
485}
486EXPORT_SYMBOL_GPL(set_cr0);
487
488void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
489{
490 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
491}
492EXPORT_SYMBOL_GPL(lmsw);
493
494void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
495{
Rusty Russell66aee912007-07-17 23:34:16 +1000496 if (cr4 & CR4_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800497 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
498 inject_gp(vcpu);
499 return;
500 }
501
Avi Kivitya9058ec2006-12-29 16:49:37 -0800502 if (is_long_mode(vcpu)) {
Rusty Russell66aee912007-07-17 23:34:16 +1000503 if (!(cr4 & X86_CR4_PAE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800504 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
505 "in long mode\n");
506 inject_gp(vcpu);
507 return;
508 }
Rusty Russell66aee912007-07-17 23:34:16 +1000509 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
Avi Kivity1342d352007-01-05 16:36:39 -0800510 && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800511 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
512 inject_gp(vcpu);
Rusty Russell310bc762007-07-23 17:11:02 +1000513 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800514 }
515
Rusty Russell66aee912007-07-17 23:34:16 +1000516 if (cr4 & X86_CR4_VMXE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800517 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
518 inject_gp(vcpu);
519 return;
520 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300521 kvm_x86_ops->set_cr4(vcpu, cr4);
Rusty Russell81f50e32007-09-06 01:20:38 +1000522 vcpu->cr4 = cr4;
Shaohua Li11ec2802007-07-23 14:51:37 +0800523 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800524 kvm_mmu_reset_context(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800525 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800526}
527EXPORT_SYMBOL_GPL(set_cr4);
528
529void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
530{
Avi Kivitya9058ec2006-12-29 16:49:37 -0800531 if (is_long_mode(vcpu)) {
Rusty Russellf802a302007-07-17 23:32:55 +1000532 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800533 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
534 inject_gp(vcpu);
535 return;
536 }
537 } else {
Rusty Russellf802a302007-07-17 23:32:55 +1000538 if (is_pae(vcpu)) {
539 if (cr3 & CR3_PAE_RESERVED_BITS) {
540 printk(KERN_DEBUG
541 "set_cr3: #GP, reserved bits\n");
542 inject_gp(vcpu);
543 return;
544 }
545 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
546 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
547 "reserved bits\n");
548 inject_gp(vcpu);
549 return;
550 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800551 }
Ryan Harper21764862007-09-18 14:05:16 -0500552 /*
553 * We don't check reserved bits in nonpae mode, because
554 * this isn't enforced, and VMware depends on this.
555 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800556 }
557
Shaohua Li11ec2802007-07-23 14:51:37 +0800558 mutex_lock(&vcpu->kvm->lock);
Ingo Molnard21225e2007-01-05 16:36:59 -0800559 /*
560 * Does the new cr3 value map to physical memory? (Note, we
561 * catch an invalid cr3 even in real-mode, because it would
562 * cause trouble later on when we turn on paging anyway.)
563 *
564 * A real CPU would silently accept an invalid cr3 and would
565 * attempt to use it - with largely undefined (and often hard
566 * to debug) behavior on the guest side.
567 */
568 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
569 inject_gp(vcpu);
Rusty Russellfb764412007-07-31 20:45:03 +1000570 else {
571 vcpu->cr3 = cr3;
Ingo Molnard21225e2007-01-05 16:36:59 -0800572 vcpu->mmu.new_cr3(vcpu);
Rusty Russellfb764412007-07-31 20:45:03 +1000573 }
Shaohua Li11ec2802007-07-23 14:51:37 +0800574 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800575}
576EXPORT_SYMBOL_GPL(set_cr3);
577
578void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
579{
Rusty Russell7075bc82007-07-17 23:37:17 +1000580 if (cr8 & CR8_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800581 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
582 inject_gp(vcpu);
583 return;
584 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300585 if (irqchip_in_kernel(vcpu->kvm))
586 kvm_lapic_set_tpr(vcpu, cr8);
587 else
588 vcpu->cr8 = cr8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800589}
590EXPORT_SYMBOL_GPL(set_cr8);
591
Eddie Dong7017fc32007-07-18 11:34:57 +0300592unsigned long get_cr8(struct kvm_vcpu *vcpu)
593{
Eddie Dong97222cc2007-09-12 10:58:04 +0300594 if (irqchip_in_kernel(vcpu->kvm))
595 return kvm_lapic_get_cr8(vcpu);
596 else
597 return vcpu->cr8;
Eddie Dong7017fc32007-07-18 11:34:57 +0300598}
599EXPORT_SYMBOL_GPL(get_cr8);
600
601u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
602{
Eddie Dong97222cc2007-09-12 10:58:04 +0300603 if (irqchip_in_kernel(vcpu->kvm))
604 return vcpu->apic_base;
605 else
606 return vcpu->apic_base;
Eddie Dong7017fc32007-07-18 11:34:57 +0300607}
608EXPORT_SYMBOL_GPL(kvm_get_apic_base);
609
610void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
611{
Eddie Dong97222cc2007-09-12 10:58:04 +0300612 /* TODO: reserve bits check */
613 if (irqchip_in_kernel(vcpu->kvm))
614 kvm_lapic_set_base(vcpu, data);
615 else
616 vcpu->apic_base = data;
Eddie Dong7017fc32007-07-18 11:34:57 +0300617}
618EXPORT_SYMBOL_GPL(kvm_set_apic_base);
619
Avi Kivity6aa8b732006-12-10 02:21:36 -0800620void fx_init(struct kvm_vcpu *vcpu)
621{
Rusty Russellb114b082007-07-30 21:13:43 +1000622 unsigned after_mxcsr_mask;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800623
Rusty Russell9bd01502007-07-30 16:29:56 +1000624 /* Initialize guest FPU by resetting ours and saving into guest's */
625 preempt_disable();
Rusty Russellb114b082007-07-30 21:13:43 +1000626 fx_save(&vcpu->host_fx_image);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800627 fpu_init();
Rusty Russellb114b082007-07-30 21:13:43 +1000628 fx_save(&vcpu->guest_fx_image);
629 fx_restore(&vcpu->host_fx_image);
Rusty Russell9bd01502007-07-30 16:29:56 +1000630 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800631
Amit Shah380102c2007-08-25 11:35:52 +0300632 vcpu->cr0 |= X86_CR0_ET;
Rusty Russellb114b082007-07-30 21:13:43 +1000633 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
634 vcpu->guest_fx_image.mxcsr = 0x1f80;
635 memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask,
636 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800637}
638EXPORT_SYMBOL_GPL(fx_init);
639
640/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800641 * Allocate some memory and give it an address in the guest physical address
642 * space.
643 *
644 * Discontiguous memory is allowed, mostly for framebuffers.
645 */
Izik Eidus210c7c42007-10-24 23:52:57 +0200646int kvm_set_memory_region(struct kvm *kvm,
647 struct kvm_userspace_memory_region *mem,
648 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800649{
650 int r;
651 gfn_t base_gfn;
652 unsigned long npages;
653 unsigned long i;
654 struct kvm_memory_slot *memslot;
655 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800656
657 r = -EINVAL;
658 /* General sanity checks */
659 if (mem->memory_size & (PAGE_SIZE - 1))
660 goto out;
661 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
662 goto out;
663 if (mem->slot >= KVM_MEMORY_SLOTS)
664 goto out;
665 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
666 goto out;
667
668 memslot = &kvm->memslots[mem->slot];
669 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
670 npages = mem->memory_size >> PAGE_SHIFT;
671
672 if (!npages)
673 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
674
Shaohua Li11ec2802007-07-23 14:51:37 +0800675 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800676
Avi Kivity6aa8b732006-12-10 02:21:36 -0800677 new = old = *memslot;
678
679 new.base_gfn = base_gfn;
680 new.npages = npages;
681 new.flags = mem->flags;
682
683 /* Disallow changing a memory slot's size. */
684 r = -EINVAL;
685 if (npages && old.npages && npages != old.npages)
686 goto out_unlock;
687
688 /* Check for overlaps */
689 r = -EEXIST;
690 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
691 struct kvm_memory_slot *s = &kvm->memslots[i];
692
693 if (s == memslot)
694 continue;
695 if (!((base_gfn + npages <= s->base_gfn) ||
696 (base_gfn >= s->base_gfn + s->npages)))
697 goto out_unlock;
698 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800699
Avi Kivity6aa8b732006-12-10 02:21:36 -0800700 /* Free page dirty bitmap if unneeded */
701 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000702 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800703
704 r = -ENOMEM;
705
706 /* Allocate if a slot is being created */
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500707 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400708 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200709
710 if (!new.rmap)
711 goto out_unlock;
712
Izik Eidus290fc382007-09-27 14:11:22 +0200713 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500714
Izik Eidus80b14b52007-10-25 11:54:04 +0200715 new.user_alloc = user_alloc;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500716 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 }
Izik Eidus80b14b52007-10-25 11:54:04 +0200730 } else {
731 if (!old.user_alloc && old.rmap) {
732 int ret;
733
734 down_write(&current->mm->mmap_sem);
735 ret = do_munmap(current->mm, old.userspace_addr,
736 old.npages * PAGE_SIZE);
737 up_write(&current->mm->mmap_sem);
738 if (ret < 0)
739 printk(KERN_WARNING
740 "kvm_vm_ioctl_set_memory_region: "
741 "failed to munmap memory\n");
742 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800743 }
744
745 /* Allocate page dirty bitmap if needed */
746 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
747 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
748
749 new.dirty_bitmap = vmalloc(dirty_bytes);
750 if (!new.dirty_bitmap)
Laurent Vivier0d8d2bd2007-08-30 14:56:21 +0200751 goto out_unlock;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800752 memset(new.dirty_bitmap, 0, dirty_bytes);
753 }
754
Avi Kivity6aa8b732006-12-10 02:21:36 -0800755 if (mem->slot >= kvm->nmemslots)
756 kvm->nmemslots = mem->slot + 1;
757
Izik Eidus82ce2c92007-10-02 18:52:55 +0200758 if (!kvm->n_requested_mmu_pages) {
759 unsigned int n_pages;
760
761 if (npages) {
762 n_pages = npages * KVM_PERMILLE_MMU_PAGES / 1000;
763 kvm_mmu_change_mmu_pages(kvm, kvm->n_alloc_mmu_pages +
764 n_pages);
765 } else {
766 unsigned int nr_mmu_pages;
767
768 n_pages = old.npages * KVM_PERMILLE_MMU_PAGES / 1000;
769 nr_mmu_pages = kvm->n_alloc_mmu_pages - n_pages;
770 nr_mmu_pages = max(nr_mmu_pages,
771 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
772 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
773 }
774 }
775
Avi Kivity6aa8b732006-12-10 02:21:36 -0800776 *memslot = new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777
Avi Kivity90cb0522007-07-17 13:04:56 +0300778 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
779 kvm_flush_remote_tlbs(kvm);
780
Shaohua Li11ec2802007-07-23 14:51:37 +0800781 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800782
Avi Kivity6aa8b732006-12-10 02:21:36 -0800783 kvm_free_physmem_slot(&old, &new);
784 return 0;
785
786out_unlock:
Shaohua Li11ec2802007-07-23 14:51:37 +0800787 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800788 kvm_free_physmem_slot(&new, &old);
789out:
790 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200791
792}
793EXPORT_SYMBOL_GPL(kvm_set_memory_region);
794
795static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
796 struct
797 kvm_userspace_memory_region *mem,
798 int user_alloc)
799{
800 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800801}
802
Izik Eidus82ce2c92007-10-02 18:52:55 +0200803static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
804 u32 kvm_nr_mmu_pages)
805{
806 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
807 return -EINVAL;
808
809 mutex_lock(&kvm->lock);
810
811 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
812 kvm->n_requested_mmu_pages = kvm_nr_mmu_pages;
813
814 mutex_unlock(&kvm->lock);
815 return 0;
816}
817
818static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
819{
820 return kvm->n_alloc_mmu_pages;
821}
822
Avi Kivity6aa8b732006-12-10 02:21:36 -0800823/*
824 * Get (and clear) the dirty memory log for a memory slot.
825 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200826static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
827 struct kvm_dirty_log *log)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800828{
829 struct kvm_memory_slot *memslot;
830 int r, i;
831 int n;
832 unsigned long any = 0;
833
Shaohua Li11ec2802007-07-23 14:51:37 +0800834 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800835
Avi Kivity6aa8b732006-12-10 02:21:36 -0800836 r = -EINVAL;
837 if (log->slot >= KVM_MEMORY_SLOTS)
838 goto out;
839
840 memslot = &kvm->memslots[log->slot];
841 r = -ENOENT;
842 if (!memslot->dirty_bitmap)
843 goto out;
844
Uri Lublincd1a4a92007-02-22 16:43:09 +0200845 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800846
Uri Lublincd1a4a92007-02-22 16:43:09 +0200847 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800848 any = memslot->dirty_bitmap[i];
849
850 r = -EFAULT;
851 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
852 goto out;
853
Rusty Russell39214912007-07-31 19:57:47 +1000854 /* If nothing is dirty, don't bother messing with page tables. */
855 if (any) {
Rusty Russell39214912007-07-31 19:57:47 +1000856 kvm_mmu_slot_remove_write_access(kvm, log->slot);
857 kvm_flush_remote_tlbs(kvm);
858 memset(memslot->dirty_bitmap, 0, n);
Rusty Russell39214912007-07-31 19:57:47 +1000859 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800860
861 r = 0;
862
863out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800864 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800865 return r;
866}
867
Avi Kivitye8207542007-03-30 16:54:30 +0300868/*
869 * Set a new alias region. Aliases map a portion of physical memory into
870 * another portion. This is useful for memory windows, for example the PC
871 * VGA region.
872 */
873static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
874 struct kvm_memory_alias *alias)
875{
876 int r, n;
877 struct kvm_mem_alias *p;
878
879 r = -EINVAL;
880 /* General sanity checks */
881 if (alias->memory_size & (PAGE_SIZE - 1))
882 goto out;
883 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
884 goto out;
885 if (alias->slot >= KVM_ALIAS_SLOTS)
886 goto out;
887 if (alias->guest_phys_addr + alias->memory_size
888 < alias->guest_phys_addr)
889 goto out;
890 if (alias->target_phys_addr + alias->memory_size
891 < alias->target_phys_addr)
892 goto out;
893
Shaohua Li11ec2802007-07-23 14:51:37 +0800894 mutex_lock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300895
896 p = &kvm->aliases[alias->slot];
897 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
898 p->npages = alias->memory_size >> PAGE_SHIFT;
899 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
900
901 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
902 if (kvm->aliases[n - 1].npages)
903 break;
904 kvm->naliases = n;
905
Avi Kivity90cb0522007-07-17 13:04:56 +0300906 kvm_mmu_zap_all(kvm);
Avi Kivitye8207542007-03-30 16:54:30 +0300907
Shaohua Li11ec2802007-07-23 14:51:37 +0800908 mutex_unlock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300909
910 return 0;
911
912out:
913 return r;
914}
915
He, Qing6ceb9d72007-07-26 11:05:18 +0300916static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
917{
918 int r;
919
920 r = 0;
921 switch (chip->chip_id) {
922 case KVM_IRQCHIP_PIC_MASTER:
Mike Dayd77c26f2007-10-08 09:02:08 -0400923 memcpy(&chip->chip.pic,
He, Qing6ceb9d72007-07-26 11:05:18 +0300924 &pic_irqchip(kvm)->pics[0],
925 sizeof(struct kvm_pic_state));
926 break;
927 case KVM_IRQCHIP_PIC_SLAVE:
Mike Dayd77c26f2007-10-08 09:02:08 -0400928 memcpy(&chip->chip.pic,
He, Qing6ceb9d72007-07-26 11:05:18 +0300929 &pic_irqchip(kvm)->pics[1],
930 sizeof(struct kvm_pic_state));
931 break;
He, Qing6bf9e962007-08-05 10:49:16 +0300932 case KVM_IRQCHIP_IOAPIC:
Mike Dayd77c26f2007-10-08 09:02:08 -0400933 memcpy(&chip->chip.ioapic,
He, Qing6bf9e962007-08-05 10:49:16 +0300934 ioapic_irqchip(kvm),
935 sizeof(struct kvm_ioapic_state));
936 break;
He, Qing6ceb9d72007-07-26 11:05:18 +0300937 default:
938 r = -EINVAL;
939 break;
940 }
941 return r;
942}
943
944static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
945{
946 int r;
947
948 r = 0;
949 switch (chip->chip_id) {
950 case KVM_IRQCHIP_PIC_MASTER:
Mike Dayd77c26f2007-10-08 09:02:08 -0400951 memcpy(&pic_irqchip(kvm)->pics[0],
He, Qing6ceb9d72007-07-26 11:05:18 +0300952 &chip->chip.pic,
953 sizeof(struct kvm_pic_state));
954 break;
955 case KVM_IRQCHIP_PIC_SLAVE:
Mike Dayd77c26f2007-10-08 09:02:08 -0400956 memcpy(&pic_irqchip(kvm)->pics[1],
He, Qing6ceb9d72007-07-26 11:05:18 +0300957 &chip->chip.pic,
958 sizeof(struct kvm_pic_state));
959 break;
He, Qing6bf9e962007-08-05 10:49:16 +0300960 case KVM_IRQCHIP_IOAPIC:
Mike Dayd77c26f2007-10-08 09:02:08 -0400961 memcpy(ioapic_irqchip(kvm),
He, Qing6bf9e962007-08-05 10:49:16 +0300962 &chip->chip.ioapic,
963 sizeof(struct kvm_ioapic_state));
964 break;
He, Qing6ceb9d72007-07-26 11:05:18 +0300965 default:
966 r = -EINVAL;
967 break;
968 }
969 kvm_pic_update_irq(pic_irqchip(kvm));
970 return r;
971}
972
Izik Eiduscea7bb22007-10-17 19:17:48 +0200973int is_error_page(struct page *page)
974{
975 return page == bad_page;
976}
977EXPORT_SYMBOL_GPL(is_error_page);
978
Izik Eidus290fc382007-09-27 14:11:22 +0200979gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
Avi Kivitye8207542007-03-30 16:54:30 +0300980{
981 int i;
982 struct kvm_mem_alias *alias;
983
984 for (i = 0; i < kvm->naliases; ++i) {
985 alias = &kvm->aliases[i];
986 if (gfn >= alias->base_gfn
987 && gfn < alias->base_gfn + alias->npages)
988 return alias->target_gfn + gfn - alias->base_gfn;
989 }
990 return gfn;
991}
992
993static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994{
995 int i;
996
997 for (i = 0; i < kvm->nmemslots; ++i) {
998 struct kvm_memory_slot *memslot = &kvm->memslots[i];
999
1000 if (gfn >= memslot->base_gfn
1001 && gfn < memslot->base_gfn + memslot->npages)
1002 return memslot;
1003 }
Al Viro8b6d44c2007-02-09 16:38:40 +00001004 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001005}
Avi Kivitye8207542007-03-30 16:54:30 +03001006
1007struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1008{
1009 gfn = unalias_gfn(kvm, gfn);
1010 return __gfn_to_memslot(kvm, gfn);
1011}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012
Avi Kivity954bbbc22007-03-30 14:02:32 +03001013struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1014{
1015 struct kvm_memory_slot *slot;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001016 struct page *page[1];
1017 int npages;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001018
Avi Kivity60395222007-10-21 11:03:36 +02001019 might_sleep();
1020
Avi Kivitye8207542007-03-30 16:54:30 +03001021 gfn = unalias_gfn(kvm, gfn);
1022 slot = __gfn_to_memslot(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001023 if (!slot) {
1024 get_page(bad_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +02001025 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001026 }
Izik Eidus8a7ae052007-10-18 11:09:33 +02001027
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001028 down_read(&current->mm->mmap_sem);
1029 npages = get_user_pages(current, current->mm,
1030 slot->userspace_addr
1031 + (gfn - slot->base_gfn) * PAGE_SIZE, 1,
1032 1, 1, page, NULL);
1033 up_read(&current->mm->mmap_sem);
1034 if (npages != 1) {
1035 get_page(bad_page);
1036 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001037 }
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001038
1039 return page[0];
Avi Kivity954bbbc22007-03-30 14:02:32 +03001040}
1041EXPORT_SYMBOL_GPL(gfn_to_page);
1042
Izik Eidus8a7ae052007-10-18 11:09:33 +02001043void kvm_release_page(struct page *page)
1044{
1045 if (!PageReserved(page))
1046 SetPageDirty(page);
1047 put_page(page);
1048}
1049EXPORT_SYMBOL_GPL(kvm_release_page);
1050
Izik Eidus195aefd2007-10-01 22:14:18 +02001051static int next_segment(unsigned long len, int offset)
1052{
1053 if (len > PAGE_SIZE - offset)
1054 return PAGE_SIZE - offset;
1055 else
1056 return len;
1057}
1058
1059int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1060 int len)
1061{
1062 void *page_virt;
1063 struct page *page;
1064
1065 page = gfn_to_page(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001066 if (is_error_page(page)) {
1067 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001068 return -EFAULT;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001069 }
Izik Eidus195aefd2007-10-01 22:14:18 +02001070 page_virt = kmap_atomic(page, KM_USER0);
1071
1072 memcpy(data, page_virt + offset, len);
1073
1074 kunmap_atomic(page_virt, KM_USER0);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001075 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001076 return 0;
1077}
1078EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1079
1080int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1081{
1082 gfn_t gfn = gpa >> PAGE_SHIFT;
1083 int seg;
1084 int offset = offset_in_page(gpa);
1085 int ret;
1086
1087 while ((seg = next_segment(len, offset)) != 0) {
1088 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1089 if (ret < 0)
1090 return ret;
1091 offset = 0;
1092 len -= seg;
1093 data += seg;
1094 ++gfn;
1095 }
1096 return 0;
1097}
1098EXPORT_SYMBOL_GPL(kvm_read_guest);
1099
1100int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1101 int offset, int len)
1102{
1103 void *page_virt;
1104 struct page *page;
1105
1106 page = gfn_to_page(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001107 if (is_error_page(page)) {
1108 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001109 return -EFAULT;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001110 }
Izik Eidus195aefd2007-10-01 22:14:18 +02001111 page_virt = kmap_atomic(page, KM_USER0);
1112
1113 memcpy(page_virt + offset, data, len);
1114
1115 kunmap_atomic(page_virt, KM_USER0);
1116 mark_page_dirty(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001117 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001118 return 0;
1119}
1120EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1121
1122int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1123 unsigned long len)
1124{
1125 gfn_t gfn = gpa >> PAGE_SHIFT;
1126 int seg;
1127 int offset = offset_in_page(gpa);
1128 int ret;
1129
1130 while ((seg = next_segment(len, offset)) != 0) {
1131 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1132 if (ret < 0)
1133 return ret;
1134 offset = 0;
1135 len -= seg;
1136 data += seg;
1137 ++gfn;
1138 }
1139 return 0;
1140}
1141
1142int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1143{
1144 void *page_virt;
1145 struct page *page;
1146
1147 page = gfn_to_page(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001148 if (is_error_page(page)) {
1149 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001150 return -EFAULT;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001151 }
Izik Eidus195aefd2007-10-01 22:14:18 +02001152 page_virt = kmap_atomic(page, KM_USER0);
1153
1154 memset(page_virt + offset, 0, len);
1155
1156 kunmap_atomic(page_virt, KM_USER0);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001157 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001158 return 0;
1159}
1160EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1161
1162int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1163{
1164 gfn_t gfn = gpa >> PAGE_SHIFT;
1165 int seg;
1166 int offset = offset_in_page(gpa);
1167 int ret;
1168
1169 while ((seg = next_segment(len, offset)) != 0) {
1170 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1171 if (ret < 0)
1172 return ret;
1173 offset = 0;
1174 len -= seg;
1175 ++gfn;
1176 }
1177 return 0;
1178}
1179EXPORT_SYMBOL_GPL(kvm_clear_guest);
1180
Rusty Russell7e9d6192007-07-31 20:41:14 +10001181/* WARNING: Does not work on aliased pages. */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001182void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1183{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001184 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001185
Rusty Russell7e9d6192007-07-31 20:41:14 +10001186 memslot = __gfn_to_memslot(kvm, gfn);
1187 if (memslot && memslot->dirty_bitmap) {
1188 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001189
Rusty Russell7e9d6192007-07-31 20:41:14 +10001190 /* avoid RMW */
1191 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1192 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001193 }
1194}
1195
Laurent Viviere7d5d762007-07-30 13:41:19 +03001196int emulator_read_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001197 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001198 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001199 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001200{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001201 void *data = val;
1202
1203 while (bytes) {
1204 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1205 unsigned offset = addr & (PAGE_SIZE-1);
1206 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
Izik Eidus195aefd2007-10-01 22:14:18 +02001207 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001208
1209 if (gpa == UNMAPPED_GVA)
1210 return X86EMUL_PROPAGATE_FAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001211 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
1212 if (ret < 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001213 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001214
1215 bytes -= tocopy;
1216 data += tocopy;
1217 addr += tocopy;
1218 }
1219
1220 return X86EMUL_CONTINUE;
1221}
Laurent Viviere7d5d762007-07-30 13:41:19 +03001222EXPORT_SYMBOL_GPL(emulator_read_std);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001223
1224static int emulator_write_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001225 const void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001226 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001227 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001228{
Rusty Russellf0242472007-08-01 10:48:02 +10001229 pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001230 return X86EMUL_UNHANDLEABLE;
1231}
1232
Eddie Dong97222cc2007-09-12 10:58:04 +03001233/*
1234 * Only apic need an MMIO device hook, so shortcut now..
1235 */
1236static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
1237 gpa_t addr)
1238{
1239 struct kvm_io_device *dev;
1240
1241 if (vcpu->apic) {
1242 dev = &vcpu->apic->dev;
1243 if (dev->in_range(dev, addr))
1244 return dev;
1245 }
1246 return NULL;
1247}
1248
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001249static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1250 gpa_t addr)
1251{
Eddie Dong97222cc2007-09-12 10:58:04 +03001252 struct kvm_io_device *dev;
1253
1254 dev = vcpu_find_pervcpu_dev(vcpu, addr);
1255 if (dev == NULL)
1256 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1257 return dev;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001258}
1259
Eddie Dong74906342007-06-19 18:05:03 +03001260static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
1261 gpa_t addr)
1262{
1263 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
1264}
1265
Avi Kivity6aa8b732006-12-10 02:21:36 -08001266static int emulator_read_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001267 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001268 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001269 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001270{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001271 struct kvm_io_device *mmio_dev;
1272 gpa_t gpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001273
1274 if (vcpu->mmio_read_completed) {
1275 memcpy(val, vcpu->mmio_data, bytes);
1276 vcpu->mmio_read_completed = 0;
1277 return X86EMUL_CONTINUE;
Laurent Viviercebff022007-07-30 13:35:24 +03001278 } else if (emulator_read_std(addr, val, bytes, vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001279 == X86EMUL_CONTINUE)
1280 return X86EMUL_CONTINUE;
Avi Kivityd27d4ac2007-02-19 14:37:46 +02001281
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001282 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1283 if (gpa == UNMAPPED_GVA)
1284 return X86EMUL_PROPAGATE_FAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001285
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001286 /*
1287 * Is this MMIO handled locally?
1288 */
1289 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1290 if (mmio_dev) {
1291 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1292 return X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001293 }
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001294
1295 vcpu->mmio_needed = 1;
1296 vcpu->mmio_phys_addr = gpa;
1297 vcpu->mmio_size = bytes;
1298 vcpu->mmio_is_write = 0;
1299
1300 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001301}
1302
Avi Kivityda4a00f2007-01-05 16:36:44 -08001303static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity4c690a12007-04-22 15:28:19 +03001304 const void *val, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001305{
Izik Eidus195aefd2007-10-01 22:14:18 +02001306 int ret;
Avi Kivityda4a00f2007-01-05 16:36:44 -08001307
Izik Eidus195aefd2007-10-01 22:14:18 +02001308 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
1309 if (ret < 0)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001310 return 0;
Shaohua Life551882007-07-23 14:51:39 +08001311 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001312 return 1;
1313}
1314
Avi Kivityb0fcd902007-07-22 18:48:54 +03001315static int emulator_write_emulated_onepage(unsigned long addr,
1316 const void *val,
1317 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001318 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001319{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001320 struct kvm_io_device *mmio_dev;
1321 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001322
Avi Kivityc9047f52007-04-17 10:53:22 +03001323 if (gpa == UNMAPPED_GVA) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001324 kvm_x86_ops->inject_page_fault(vcpu, addr, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001325 return X86EMUL_PROPAGATE_FAULT;
Avi Kivityc9047f52007-04-17 10:53:22 +03001326 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001327
Avi Kivityda4a00f2007-01-05 16:36:44 -08001328 if (emulator_write_phys(vcpu, gpa, val, bytes))
1329 return X86EMUL_CONTINUE;
1330
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001331 /*
1332 * Is this MMIO handled locally?
1333 */
1334 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1335 if (mmio_dev) {
1336 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1337 return X86EMUL_CONTINUE;
1338 }
1339
Avi Kivity6aa8b732006-12-10 02:21:36 -08001340 vcpu->mmio_needed = 1;
1341 vcpu->mmio_phys_addr = gpa;
1342 vcpu->mmio_size = bytes;
1343 vcpu->mmio_is_write = 1;
Avi Kivity4c690a12007-04-22 15:28:19 +03001344 memcpy(vcpu->mmio_data, val, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001345
1346 return X86EMUL_CONTINUE;
1347}
1348
Laurent Viviere7d5d762007-07-30 13:41:19 +03001349int emulator_write_emulated(unsigned long addr,
Avi Kivityb0fcd902007-07-22 18:48:54 +03001350 const void *val,
1351 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001352 struct kvm_vcpu *vcpu)
Avi Kivityb0fcd902007-07-22 18:48:54 +03001353{
1354 /* Crossing a page boundary? */
1355 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1356 int rc, now;
1357
1358 now = -addr & ~PAGE_MASK;
Laurent Viviercebff022007-07-30 13:35:24 +03001359 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001360 if (rc != X86EMUL_CONTINUE)
1361 return rc;
1362 addr += now;
1363 val += now;
1364 bytes -= now;
1365 }
Laurent Viviercebff022007-07-30 13:35:24 +03001366 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001367}
Laurent Viviere7d5d762007-07-30 13:41:19 +03001368EXPORT_SYMBOL_GPL(emulator_write_emulated);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001369
Avi Kivity6aa8b732006-12-10 02:21:36 -08001370static int emulator_cmpxchg_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001371 const void *old,
1372 const void *new,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001373 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001374 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001375{
1376 static int reported;
1377
1378 if (!reported) {
1379 reported = 1;
1380 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1381 }
Laurent Viviercebff022007-07-30 13:35:24 +03001382 return emulator_write_emulated(addr, new, bytes, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001383}
1384
1385static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1386{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001387 return kvm_x86_ops->get_segment_base(vcpu, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001388}
1389
1390int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1391{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001392 return X86EMUL_CONTINUE;
1393}
1394
1395int emulate_clts(struct kvm_vcpu *vcpu)
1396{
Amit Shah404fb882007-11-19 17:57:35 +02001397 kvm_x86_ops->set_cr0(vcpu, vcpu->cr0 & ~X86_CR0_TS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001398 return X86EMUL_CONTINUE;
1399}
1400
Mike Dayd77c26f2007-10-08 09:02:08 -04001401int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001402{
1403 struct kvm_vcpu *vcpu = ctxt->vcpu;
1404
1405 switch (dr) {
1406 case 0 ... 3:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001407 *dest = kvm_x86_ops->get_dr(vcpu, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001408 return X86EMUL_CONTINUE;
1409 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001410 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001411 return X86EMUL_UNHANDLEABLE;
1412 }
1413}
1414
1415int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1416{
1417 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1418 int exception;
1419
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001420 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001421 if (exception) {
1422 /* FIXME: better handling */
1423 return X86EMUL_UNHANDLEABLE;
1424 }
1425 return X86EMUL_CONTINUE;
1426}
1427
Avi Kivity054b1362007-09-12 13:21:09 +03001428void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429{
1430 static int reported;
1431 u8 opcodes[4];
Avi Kivity054b1362007-09-12 13:21:09 +03001432 unsigned long rip = vcpu->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001433 unsigned long rip_linear;
1434
Avi Kivity054b1362007-09-12 13:21:09 +03001435 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001436
1437 if (reported)
1438 return;
1439
Avi Kivity054b1362007-09-12 13:21:09 +03001440 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001441
Avi Kivity054b1362007-09-12 13:21:09 +03001442 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1443 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001444 reported = 1;
1445}
Avi Kivity054b1362007-09-12 13:21:09 +03001446EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001447
1448struct x86_emulate_ops emulate_ops = {
1449 .read_std = emulator_read_std,
1450 .write_std = emulator_write_std,
1451 .read_emulated = emulator_read_emulated,
1452 .write_emulated = emulator_write_emulated,
1453 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1454};
1455
1456int emulate_instruction(struct kvm_vcpu *vcpu,
1457 struct kvm_run *run,
1458 unsigned long cr2,
Laurent Vivier34273182007-09-18 11:27:37 +02001459 u16 error_code,
1460 int no_decode)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001461{
Laurent Viviera22436b2007-09-24 17:00:58 +02001462 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001463
Avi Kivitye7df56e2007-03-14 15:54:54 +02001464 vcpu->mmio_fault_cr2 = cr2;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001465 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001466
Avi Kivity6aa8b732006-12-10 02:21:36 -08001467 vcpu->mmio_is_write = 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03001468 vcpu->pio.string = 0;
Laurent Vivier34273182007-09-18 11:27:37 +02001469
1470 if (!no_decode) {
1471 int cs_db, cs_l;
1472 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1473
1474 vcpu->emulate_ctxt.vcpu = vcpu;
1475 vcpu->emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
1476 vcpu->emulate_ctxt.cr2 = cr2;
1477 vcpu->emulate_ctxt.mode =
1478 (vcpu->emulate_ctxt.eflags & X86_EFLAGS_VM)
1479 ? X86EMUL_MODE_REAL : cs_l
1480 ? X86EMUL_MODE_PROT64 : cs_db
1481 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1482
1483 if (vcpu->emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1484 vcpu->emulate_ctxt.cs_base = 0;
1485 vcpu->emulate_ctxt.ds_base = 0;
1486 vcpu->emulate_ctxt.es_base = 0;
1487 vcpu->emulate_ctxt.ss_base = 0;
1488 } else {
1489 vcpu->emulate_ctxt.cs_base =
1490 get_segment_base(vcpu, VCPU_SREG_CS);
1491 vcpu->emulate_ctxt.ds_base =
1492 get_segment_base(vcpu, VCPU_SREG_DS);
1493 vcpu->emulate_ctxt.es_base =
1494 get_segment_base(vcpu, VCPU_SREG_ES);
1495 vcpu->emulate_ctxt.ss_base =
1496 get_segment_base(vcpu, VCPU_SREG_SS);
1497 }
1498
1499 vcpu->emulate_ctxt.gs_base =
1500 get_segment_base(vcpu, VCPU_SREG_GS);
1501 vcpu->emulate_ctxt.fs_base =
1502 get_segment_base(vcpu, VCPU_SREG_FS);
1503
1504 r = x86_decode_insn(&vcpu->emulate_ctxt, &emulate_ops);
Laurent Viviera22436b2007-09-24 17:00:58 +02001505 if (r) {
1506 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1507 return EMULATE_DONE;
1508 return EMULATE_FAIL;
1509 }
Laurent Vivier34273182007-09-18 11:27:37 +02001510 }
1511
Laurent Viviera22436b2007-09-24 17:00:58 +02001512 r = x86_emulate_insn(&vcpu->emulate_ctxt, &emulate_ops);
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001513
Laurent Viviere70669a2007-08-05 10:36:40 +03001514 if (vcpu->pio.string)
1515 return EMULATE_DO_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001516
1517 if ((r || vcpu->mmio_is_write) && run) {
Jeff Dike8fc0d082007-07-17 12:26:59 -04001518 run->exit_reason = KVM_EXIT_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001519 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1520 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1521 run->mmio.len = vcpu->mmio_size;
1522 run->mmio.is_write = vcpu->mmio_is_write;
1523 }
1524
1525 if (r) {
Avi Kivitya4360362007-01-05 16:36:45 -08001526 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1527 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001528 if (!vcpu->mmio_needed) {
Avi Kivity054b1362007-09-12 13:21:09 +03001529 kvm_report_emulation_failure(vcpu, "mmio");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001530 return EMULATE_FAIL;
1531 }
1532 return EMULATE_DO_MMIO;
1533 }
1534
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001535 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier34273182007-09-18 11:27:37 +02001536 kvm_x86_ops->set_rflags(vcpu, vcpu->emulate_ctxt.eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001537
Avi Kivity02c83202007-04-29 15:02:17 +03001538 if (vcpu->mmio_is_write) {
1539 vcpu->mmio_needed = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001540 return EMULATE_DO_MMIO;
Avi Kivity02c83202007-04-29 15:02:17 +03001541 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001542
1543 return EMULATE_DONE;
1544}
1545EXPORT_SYMBOL_GPL(emulate_instruction);
1546
Eddie Dongb6958ce2007-07-18 12:15:21 +03001547/*
1548 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1549 */
He, Qingc5ec1532007-09-03 17:07:41 +03001550static void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001551{
1552 DECLARE_WAITQUEUE(wait, current);
1553
1554 add_wait_queue(&vcpu->wq, &wait);
1555
1556 /*
1557 * We will block until either an interrupt or a signal wakes us up
1558 */
He, Qingc5ec1532007-09-03 17:07:41 +03001559 while (!kvm_cpu_has_interrupt(vcpu)
1560 && !signal_pending(current)
1561 && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE
1562 && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) {
Eddie Dongb6958ce2007-07-18 12:15:21 +03001563 set_current_state(TASK_INTERRUPTIBLE);
1564 vcpu_put(vcpu);
1565 schedule();
1566 vcpu_load(vcpu);
1567 }
1568
He, Qingc5ec1532007-09-03 17:07:41 +03001569 __set_current_state(TASK_RUNNING);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001570 remove_wait_queue(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001571}
1572
Avi Kivityd3bef152007-06-05 15:53:05 +03001573int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1574{
Avi Kivityd3bef152007-06-05 15:53:05 +03001575 ++vcpu->stat.halt_exits;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001576 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc5ec1532007-09-03 17:07:41 +03001577 vcpu->mp_state = VCPU_MP_STATE_HALTED;
1578 kvm_vcpu_block(vcpu);
1579 if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE)
1580 return -EINTR;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001581 return 1;
1582 } else {
1583 vcpu->run->exit_reason = KVM_EXIT_HLT;
1584 return 0;
1585 }
Avi Kivityd3bef152007-06-05 15:53:05 +03001586}
1587EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1588
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001589int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
Avi Kivity270fd9b2007-02-19 14:37:47 +02001590{
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001591 unsigned long nr, a0, a1, a2, a3, ret;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001592
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001593 kvm_x86_ops->cache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001594
1595 nr = vcpu->regs[VCPU_REGS_RAX];
1596 a0 = vcpu->regs[VCPU_REGS_RBX];
1597 a1 = vcpu->regs[VCPU_REGS_RCX];
1598 a2 = vcpu->regs[VCPU_REGS_RDX];
1599 a3 = vcpu->regs[VCPU_REGS_RSI];
1600
1601 if (!is_long_mode(vcpu)) {
1602 nr &= 0xFFFFFFFF;
1603 a0 &= 0xFFFFFFFF;
1604 a1 &= 0xFFFFFFFF;
1605 a2 &= 0xFFFFFFFF;
1606 a3 &= 0xFFFFFFFF;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001607 }
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001608
Avi Kivity270fd9b2007-02-19 14:37:47 +02001609 switch (nr) {
1610 default:
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001611 ret = -KVM_ENOSYS;
1612 break;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001613 }
1614 vcpu->regs[VCPU_REGS_RAX] = ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001615 kvm_x86_ops->decache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001616 return 0;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001617}
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001618EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
1619
1620int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
1621{
1622 char instruction[3];
1623 int ret = 0;
1624
1625 mutex_lock(&vcpu->kvm->lock);
1626
1627 /*
1628 * Blow out the MMU to ensure that no other VCPU has an active mapping
1629 * to ensure that the updated hypercall appears atomically across all
1630 * VCPUs.
1631 */
1632 kvm_mmu_zap_all(vcpu->kvm);
1633
1634 kvm_x86_ops->cache_regs(vcpu);
1635 kvm_x86_ops->patch_hypercall(vcpu, instruction);
1636 if (emulator_write_emulated(vcpu->rip, instruction, 3, vcpu)
1637 != X86EMUL_CONTINUE)
1638 ret = -EFAULT;
1639
1640 mutex_unlock(&vcpu->kvm->lock);
1641
1642 return ret;
1643}
Avi Kivity270fd9b2007-02-19 14:37:47 +02001644
Avi Kivity6aa8b732006-12-10 02:21:36 -08001645static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1646{
1647 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1648}
1649
1650void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1651{
1652 struct descriptor_table dt = { limit, base };
1653
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001654 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001655}
1656
1657void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1658{
1659 struct descriptor_table dt = { limit, base };
1660
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001661 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001662}
1663
1664void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1665 unsigned long *rflags)
1666{
1667 lmsw(vcpu, msw);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001668 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001669}
1670
1671unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1672{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001673 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001674 switch (cr) {
1675 case 0:
1676 return vcpu->cr0;
1677 case 2:
1678 return vcpu->cr2;
1679 case 3:
1680 return vcpu->cr3;
1681 case 4:
1682 return vcpu->cr4;
1683 default:
1684 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1685 return 0;
1686 }
1687}
1688
1689void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1690 unsigned long *rflags)
1691{
1692 switch (cr) {
1693 case 0:
1694 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001695 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001696 break;
1697 case 2:
1698 vcpu->cr2 = val;
1699 break;
1700 case 3:
1701 set_cr3(vcpu, val);
1702 break;
1703 case 4:
1704 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1705 break;
1706 default:
1707 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1708 }
1709}
1710
Avi Kivity3bab1f52006-12-29 16:49:48 -08001711int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1712{
1713 u64 data;
1714
1715 switch (msr) {
1716 case 0xc0010010: /* SYSCFG */
1717 case 0xc0010015: /* HWCR */
1718 case MSR_IA32_PLATFORM_ID:
1719 case MSR_IA32_P5_MC_ADDR:
1720 case MSR_IA32_P5_MC_TYPE:
1721 case MSR_IA32_MC0_CTL:
1722 case MSR_IA32_MCG_STATUS:
1723 case MSR_IA32_MCG_CAP:
1724 case MSR_IA32_MC0_MISC:
1725 case MSR_IA32_MC0_MISC+4:
1726 case MSR_IA32_MC0_MISC+8:
1727 case MSR_IA32_MC0_MISC+12:
1728 case MSR_IA32_MC0_MISC+16:
1729 case MSR_IA32_UCODE_REV:
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001730 case MSR_IA32_PERF_STATUS:
Matthew Gregan2dc70942007-05-06 10:59:46 +03001731 case MSR_IA32_EBL_CR_POWERON:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001732 /* MTRR registers */
1733 case 0xfe:
1734 case 0x200 ... 0x2ff:
1735 data = 0;
1736 break;
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001737 case 0xcd: /* fsb frequency */
1738 data = 3;
1739 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001740 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001741 data = kvm_get_apic_base(vcpu);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001742 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001743 case MSR_IA32_MISC_ENABLE:
1744 data = vcpu->ia32_misc_enable_msr;
1745 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001746#ifdef CONFIG_X86_64
1747 case MSR_EFER:
1748 data = vcpu->shadow_efer;
1749 break;
1750#endif
1751 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001752 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001753 return 1;
1754 }
1755 *pdata = data;
1756 return 0;
1757}
1758EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1759
Avi Kivity6aa8b732006-12-10 02:21:36 -08001760/*
1761 * Reads an msr value (of 'msr_index') into 'pdata'.
1762 * Returns 0 on success, non-0 otherwise.
1763 * Assumes vcpu_load() was already called.
1764 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001765int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001766{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001767 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001768}
1769
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001770#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001771
Avi Kivity3bab1f52006-12-29 16:49:48 -08001772static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001773{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001774 if (efer & EFER_RESERVED_BITS) {
1775 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1776 efer);
1777 inject_gp(vcpu);
1778 return;
1779 }
1780
1781 if (is_paging(vcpu)
1782 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1783 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1784 inject_gp(vcpu);
1785 return;
1786 }
1787
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001788 kvm_x86_ops->set_efer(vcpu, efer);
Avi Kivity7725f0b2006-12-13 00:34:01 -08001789
Avi Kivity6aa8b732006-12-10 02:21:36 -08001790 efer &= ~EFER_LMA;
1791 efer |= vcpu->shadow_efer & EFER_LMA;
1792
1793 vcpu->shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001794}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001795
1796#endif
1797
Avi Kivity3bab1f52006-12-29 16:49:48 -08001798int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1799{
1800 switch (msr) {
1801#ifdef CONFIG_X86_64
1802 case MSR_EFER:
1803 set_efer(vcpu, data);
1804 break;
1805#endif
1806 case MSR_IA32_MC0_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001807 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
Avi Kivity3bab1f52006-12-29 16:49:48 -08001808 __FUNCTION__, data);
1809 break;
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001810 case MSR_IA32_MCG_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001811 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001812 __FUNCTION__, data);
1813 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001814 case MSR_IA32_UCODE_REV:
1815 case MSR_IA32_UCODE_WRITE:
1816 case 0x200 ... 0x2ff: /* MTRRs */
1817 break;
1818 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001819 kvm_set_apic_base(vcpu, data);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001820 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001821 case MSR_IA32_MISC_ENABLE:
1822 vcpu->ia32_misc_enable_msr = data;
1823 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001824 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001825 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001826 return 1;
1827 }
1828 return 0;
1829}
1830EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1831
Avi Kivity6aa8b732006-12-10 02:21:36 -08001832/*
1833 * Writes msr value into into the appropriate "register".
1834 * Returns 0 on success, non-0 otherwise.
1835 * Assumes vcpu_load() was already called.
1836 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001837int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001838{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001839 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001840}
1841
1842void kvm_resched(struct kvm_vcpu *vcpu)
1843{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001844 if (!need_resched())
1845 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001846 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001847}
1848EXPORT_SYMBOL_GPL(kvm_resched);
1849
Avi Kivity06465c52007-02-28 20:46:53 +02001850void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1851{
1852 int i;
1853 u32 function;
1854 struct kvm_cpuid_entry *e, *best;
1855
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001856 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001857 function = vcpu->regs[VCPU_REGS_RAX];
1858 vcpu->regs[VCPU_REGS_RAX] = 0;
1859 vcpu->regs[VCPU_REGS_RBX] = 0;
1860 vcpu->regs[VCPU_REGS_RCX] = 0;
1861 vcpu->regs[VCPU_REGS_RDX] = 0;
1862 best = NULL;
1863 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1864 e = &vcpu->cpuid_entries[i];
1865 if (e->function == function) {
1866 best = e;
1867 break;
1868 }
1869 /*
1870 * Both basic or both extended?
1871 */
1872 if (((e->function ^ function) & 0x80000000) == 0)
1873 if (!best || e->function > best->function)
1874 best = e;
1875 }
1876 if (best) {
1877 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1878 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1879 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1880 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1881 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001882 kvm_x86_ops->decache_regs(vcpu);
1883 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001884}
1885EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1886
Avi Kivity039576c2007-03-20 12:46:50 +02001887static int pio_copy_data(struct kvm_vcpu *vcpu)
Avi Kivity46fc1472007-02-22 19:39:30 +02001888{
Avi Kivity039576c2007-03-20 12:46:50 +02001889 void *p = vcpu->pio_data;
1890 void *q;
1891 unsigned bytes;
1892 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1893
Avi Kivity039576c2007-03-20 12:46:50 +02001894 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1895 PAGE_KERNEL);
1896 if (!q) {
Avi Kivity039576c2007-03-20 12:46:50 +02001897 free_pio_guest_pages(vcpu);
1898 return -ENOMEM;
1899 }
1900 q += vcpu->pio.guest_page_offset;
1901 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1902 if (vcpu->pio.in)
1903 memcpy(q, p, bytes);
1904 else
1905 memcpy(p, q, bytes);
1906 q -= vcpu->pio.guest_page_offset;
1907 vunmap(q);
Avi Kivity039576c2007-03-20 12:46:50 +02001908 free_pio_guest_pages(vcpu);
1909 return 0;
1910}
1911
1912static int complete_pio(struct kvm_vcpu *vcpu)
1913{
1914 struct kvm_pio_request *io = &vcpu->pio;
Avi Kivity46fc1472007-02-22 19:39:30 +02001915 long delta;
Avi Kivity039576c2007-03-20 12:46:50 +02001916 int r;
Avi Kivity46fc1472007-02-22 19:39:30 +02001917
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001918 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001919
1920 if (!io->string) {
Avi Kivity039576c2007-03-20 12:46:50 +02001921 if (io->in)
1922 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
Avi Kivity46fc1472007-02-22 19:39:30 +02001923 io->size);
1924 } else {
Avi Kivity039576c2007-03-20 12:46:50 +02001925 if (io->in) {
1926 r = pio_copy_data(vcpu);
1927 if (r) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001928 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02001929 return r;
1930 }
1931 }
1932
Avi Kivity46fc1472007-02-22 19:39:30 +02001933 delta = 1;
1934 if (io->rep) {
Avi Kivity039576c2007-03-20 12:46:50 +02001935 delta *= io->cur_count;
Avi Kivity46fc1472007-02-22 19:39:30 +02001936 /*
1937 * The size of the register should really depend on
1938 * current address size.
1939 */
1940 vcpu->regs[VCPU_REGS_RCX] -= delta;
1941 }
Avi Kivity039576c2007-03-20 12:46:50 +02001942 if (io->down)
Avi Kivity46fc1472007-02-22 19:39:30 +02001943 delta = -delta;
1944 delta *= io->size;
Avi Kivity039576c2007-03-20 12:46:50 +02001945 if (io->in)
Avi Kivity46fc1472007-02-22 19:39:30 +02001946 vcpu->regs[VCPU_REGS_RDI] += delta;
1947 else
1948 vcpu->regs[VCPU_REGS_RSI] += delta;
1949 }
1950
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001951 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001952
Avi Kivity039576c2007-03-20 12:46:50 +02001953 io->count -= io->cur_count;
1954 io->cur_count = 0;
1955
Avi Kivity039576c2007-03-20 12:46:50 +02001956 return 0;
Avi Kivity46fc1472007-02-22 19:39:30 +02001957}
1958
Eddie Dong65619eb2007-07-17 11:52:33 +03001959static void kernel_pio(struct kvm_io_device *pio_dev,
1960 struct kvm_vcpu *vcpu,
1961 void *pd)
Eddie Dong74906342007-06-19 18:05:03 +03001962{
1963 /* TODO: String I/O for in kernel device */
1964
Eddie Dong9cf98822007-07-22 10:36:31 +03001965 mutex_lock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001966 if (vcpu->pio.in)
1967 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1968 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001969 pd);
Eddie Dong74906342007-06-19 18:05:03 +03001970 else
1971 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1972 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001973 pd);
Eddie Dong9cf98822007-07-22 10:36:31 +03001974 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001975}
1976
1977static void pio_string_write(struct kvm_io_device *pio_dev,
1978 struct kvm_vcpu *vcpu)
1979{
1980 struct kvm_pio_request *io = &vcpu->pio;
1981 void *pd = vcpu->pio_data;
1982 int i;
1983
Eddie Dong9cf98822007-07-22 10:36:31 +03001984 mutex_lock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001985 for (i = 0; i < io->cur_count; i++) {
1986 kvm_iodevice_write(pio_dev, io->port,
1987 io->size,
1988 pd);
1989 pd += io->size;
1990 }
Eddie Dong9cf98822007-07-22 10:36:31 +03001991 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001992}
1993
Mike Dayd77c26f2007-10-08 09:02:08 -04001994int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
Laurent Vivier3090dd72007-08-05 10:43:32 +03001995 int size, unsigned port)
1996{
1997 struct kvm_io_device *pio_dev;
1998
1999 vcpu->run->exit_reason = KVM_EXIT_IO;
2000 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
2001 vcpu->run->io.size = vcpu->pio.size = size;
2002 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
2003 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1;
2004 vcpu->run->io.port = vcpu->pio.port = port;
2005 vcpu->pio.in = in;
2006 vcpu->pio.string = 0;
2007 vcpu->pio.down = 0;
2008 vcpu->pio.guest_page_offset = 0;
2009 vcpu->pio.rep = 0;
2010
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002011 kvm_x86_ops->cache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03002012 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002013 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03002014
Avi Kivity0967b7b2007-09-15 17:34:36 +03002015 kvm_x86_ops->skip_emulated_instruction(vcpu);
2016
Laurent Vivier3090dd72007-08-05 10:43:32 +03002017 pio_dev = vcpu_find_pio_dev(vcpu, port);
2018 if (pio_dev) {
2019 kernel_pio(pio_dev, vcpu, vcpu->pio_data);
2020 complete_pio(vcpu);
2021 return 1;
2022 }
2023 return 0;
2024}
2025EXPORT_SYMBOL_GPL(kvm_emulate_pio);
2026
2027int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2028 int size, unsigned long count, int down,
Avi Kivity039576c2007-03-20 12:46:50 +02002029 gva_t address, int rep, unsigned port)
2030{
2031 unsigned now, in_page;
Eddie Dong65619eb2007-07-17 11:52:33 +03002032 int i, ret = 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002033 int nr_pages = 1;
2034 struct page *page;
Eddie Dong74906342007-06-19 18:05:03 +03002035 struct kvm_io_device *pio_dev;
Avi Kivity039576c2007-03-20 12:46:50 +02002036
2037 vcpu->run->exit_reason = KVM_EXIT_IO;
2038 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Laurent Vivier3090dd72007-08-05 10:43:32 +03002039 vcpu->run->io.size = vcpu->pio.size = size;
Avi Kivity039576c2007-03-20 12:46:50 +02002040 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Laurent Vivier3090dd72007-08-05 10:43:32 +03002041 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count;
2042 vcpu->run->io.port = vcpu->pio.port = port;
Avi Kivity039576c2007-03-20 12:46:50 +02002043 vcpu->pio.in = in;
Laurent Vivier3090dd72007-08-05 10:43:32 +03002044 vcpu->pio.string = 1;
Avi Kivity039576c2007-03-20 12:46:50 +02002045 vcpu->pio.down = down;
2046 vcpu->pio.guest_page_offset = offset_in_page(address);
2047 vcpu->pio.rep = rep;
2048
Avi Kivity039576c2007-03-20 12:46:50 +02002049 if (!count) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002050 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02002051 return 1;
2052 }
2053
Avi Kivity039576c2007-03-20 12:46:50 +02002054 if (!down)
2055 in_page = PAGE_SIZE - offset_in_page(address);
2056 else
2057 in_page = offset_in_page(address) + size;
2058 now = min(count, (unsigned long)in_page / size);
2059 if (!now) {
2060 /*
2061 * String I/O straddles page boundary. Pin two guest pages
2062 * so that we satisfy atomicity constraints. Do just one
2063 * transaction to avoid complexity.
2064 */
2065 nr_pages = 2;
2066 now = 1;
2067 }
2068 if (down) {
2069 /*
2070 * String I/O in reverse. Yuck. Kill the guest, fix later.
2071 */
Rusty Russellf0242472007-08-01 10:48:02 +10002072 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivity039576c2007-03-20 12:46:50 +02002073 inject_gp(vcpu);
2074 return 1;
2075 }
2076 vcpu->run->io.count = now;
2077 vcpu->pio.cur_count = now;
2078
Avi Kivity0967b7b2007-09-15 17:34:36 +03002079 if (vcpu->pio.cur_count == vcpu->pio.count)
2080 kvm_x86_ops->skip_emulated_instruction(vcpu);
2081
Avi Kivity039576c2007-03-20 12:46:50 +02002082 for (i = 0; i < nr_pages; ++i) {
Shaohua Li11ec2802007-07-23 14:51:37 +08002083 mutex_lock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02002084 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
Avi Kivity039576c2007-03-20 12:46:50 +02002085 vcpu->pio.guest_pages[i] = page;
Shaohua Li11ec2802007-07-23 14:51:37 +08002086 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02002087 if (!page) {
2088 inject_gp(vcpu);
2089 free_pio_guest_pages(vcpu);
2090 return 1;
2091 }
2092 }
2093
Laurent Vivier3090dd72007-08-05 10:43:32 +03002094 pio_dev = vcpu_find_pio_dev(vcpu, port);
Eddie Dong65619eb2007-07-17 11:52:33 +03002095 if (!vcpu->pio.in) {
2096 /* string PIO write */
2097 ret = pio_copy_data(vcpu);
2098 if (ret >= 0 && pio_dev) {
2099 pio_string_write(pio_dev, vcpu);
2100 complete_pio(vcpu);
2101 if (vcpu->pio.count == 0)
2102 ret = 1;
2103 }
2104 } else if (pio_dev)
Rusty Russellf0242472007-08-01 10:48:02 +10002105 pr_unimpl(vcpu, "no string pio read support yet, "
Eddie Dong65619eb2007-07-17 11:52:33 +03002106 "port %x size %d count %ld\n",
2107 port, size, count);
2108
2109 return ret;
Avi Kivity039576c2007-03-20 12:46:50 +02002110}
Laurent Vivier3090dd72007-08-05 10:43:32 +03002111EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
Avi Kivity039576c2007-03-20 12:46:50 +02002112
Avi Kivity04d2cc72007-09-10 18:10:54 +03002113/*
2114 * Check if userspace requested an interrupt window, and that the
2115 * interrupt window is open.
2116 *
2117 * No need to exit to userspace if we already have an interrupt queued.
2118 */
2119static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2120 struct kvm_run *kvm_run)
2121{
2122 return (!vcpu->irq_summary &&
2123 kvm_run->request_interrupt_window &&
2124 vcpu->interrupt_window_open &&
2125 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
2126}
2127
2128static void post_kvm_run_save(struct kvm_vcpu *vcpu,
2129 struct kvm_run *kvm_run)
2130{
2131 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
2132 kvm_run->cr8 = get_cr8(vcpu);
2133 kvm_run->apic_base = kvm_get_apic_base(vcpu);
2134 if (irqchip_in_kernel(vcpu->kvm))
2135 kvm_run->ready_for_interrupt_injection = 1;
2136 else
2137 kvm_run->ready_for_interrupt_injection =
2138 (vcpu->interrupt_window_open &&
2139 vcpu->irq_summary == 0);
2140}
2141
2142static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2143{
2144 int r;
2145
2146 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002147 pr_debug("vcpu %d received sipi with vector # %x\n",
Avi Kivity04d2cc72007-09-10 18:10:54 +03002148 vcpu->vcpu_id, vcpu->sipi_vector);
2149 kvm_lapic_reset(vcpu);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002150 r = kvm_x86_ops->vcpu_reset(vcpu);
2151 if (r)
2152 return r;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002153 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
2154 }
2155
2156preempted:
2157 if (vcpu->guest_debug.enabled)
2158 kvm_x86_ops->guest_debug_pre(vcpu);
2159
2160again:
2161 r = kvm_mmu_reload(vcpu);
2162 if (unlikely(r))
2163 goto out;
2164
Avi Kivityab6ef342007-10-16 16:23:22 +02002165 kvm_inject_pending_timer_irqs(vcpu);
2166
Avi Kivity04d2cc72007-09-10 18:10:54 +03002167 preempt_disable();
2168
2169 kvm_x86_ops->prepare_guest_switch(vcpu);
2170 kvm_load_guest_fpu(vcpu);
2171
2172 local_irq_disable();
2173
2174 if (signal_pending(current)) {
2175 local_irq_enable();
2176 preempt_enable();
2177 r = -EINTR;
2178 kvm_run->exit_reason = KVM_EXIT_INTR;
2179 ++vcpu->stat.signal_exits;
2180 goto out;
2181 }
2182
2183 if (irqchip_in_kernel(vcpu->kvm))
2184 kvm_x86_ops->inject_pending_irq(vcpu);
2185 else if (!vcpu->mmio_read_completed)
2186 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2187
2188 vcpu->guest_mode = 1;
Laurent Vivierd172fcd2007-10-15 17:00:19 +02002189 kvm_guest_enter();
Avi Kivity04d2cc72007-09-10 18:10:54 +03002190
2191 if (vcpu->requests)
Avi Kivity3176bc32007-10-16 17:22:08 +02002192 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivity04d2cc72007-09-10 18:10:54 +03002193 kvm_x86_ops->tlb_flush(vcpu);
2194
2195 kvm_x86_ops->run(vcpu, kvm_run);
2196
2197 vcpu->guest_mode = 0;
2198 local_irq_enable();
2199
2200 ++vcpu->stat.exits;
2201
Laurent Vivier0552f732007-10-18 15:19:01 +02002202 /*
2203 * We must have an instruction between local_irq_enable() and
2204 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2205 * the interrupt shadow. The stat.exits increment will do nicely.
2206 * But we need to prevent reordering, hence this barrier():
2207 */
2208 barrier();
2209
2210 kvm_guest_exit();
2211
Avi Kivity04d2cc72007-09-10 18:10:54 +03002212 preempt_enable();
2213
2214 /*
2215 * Profile KVM exit RIPs:
2216 */
2217 if (unlikely(prof_on == KVM_PROFILING)) {
2218 kvm_x86_ops->cache_regs(vcpu);
2219 profile_hit(KVM_PROFILING, (void *)vcpu->rip);
2220 }
2221
2222 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2223
2224 if (r > 0) {
2225 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2226 r = -EINTR;
2227 kvm_run->exit_reason = KVM_EXIT_INTR;
2228 ++vcpu->stat.request_irq_exits;
2229 goto out;
2230 }
2231 if (!need_resched()) {
2232 ++vcpu->stat.light_exits;
2233 goto again;
2234 }
2235 }
2236
2237out:
2238 if (r > 0) {
2239 kvm_resched(vcpu);
2240 goto preempted;
2241 }
2242
2243 post_kvm_run_save(vcpu, kvm_run);
2244
2245 return r;
2246}
2247
2248
Avi Kivitybccf2152007-02-21 18:04:26 +02002249static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002250{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002251 int r;
Avi Kivity1961d272007-03-05 19:46:05 +02002252 sigset_t sigsaved;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002253
Avi Kivitybccf2152007-02-21 18:04:26 +02002254 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002255
He, Qingc5ec1532007-09-03 17:07:41 +03002256 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
2257 kvm_vcpu_block(vcpu);
2258 vcpu_put(vcpu);
2259 return -EAGAIN;
2260 }
2261
Avi Kivity1961d272007-03-05 19:46:05 +02002262 if (vcpu->sigset_active)
2263 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2264
Dor Laor54810342007-02-12 00:54:39 -08002265 /* re-sync apic's tpr */
He, Qing5cd4f6f2007-08-30 17:04:26 +08002266 if (!irqchip_in_kernel(vcpu->kvm))
2267 set_cr8(vcpu, kvm_run->cr8);
Dor Laor54810342007-02-12 00:54:39 -08002268
Avi Kivity02c83202007-04-29 15:02:17 +03002269 if (vcpu->pio.cur_count) {
2270 r = complete_pio(vcpu);
2271 if (r)
2272 goto out;
2273 }
Zhang Xiantao34c16ee2007-10-20 15:34:38 +08002274#if CONFIG_HAS_IOMEM
Avi Kivity02c83202007-04-29 15:02:17 +03002275 if (vcpu->mmio_needed) {
2276 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
2277 vcpu->mmio_read_completed = 1;
2278 vcpu->mmio_needed = 0;
2279 r = emulate_instruction(vcpu, kvm_run,
Laurent Vivier34273182007-09-18 11:27:37 +02002280 vcpu->mmio_fault_cr2, 0, 1);
Avi Kivity02c83202007-04-29 15:02:17 +03002281 if (r == EMULATE_DO_MMIO) {
2282 /*
2283 * Read-modify-write. Back to userspace.
2284 */
Avi Kivity02c83202007-04-29 15:02:17 +03002285 r = 0;
2286 goto out;
Avi Kivity46fc1472007-02-22 19:39:30 +02002287 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002288 }
Zhang Xiantao34c16ee2007-10-20 15:34:38 +08002289#endif
Avi Kivity8eb7d332007-03-04 14:17:08 +02002290 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002291 kvm_x86_ops->cache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002292 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002293 kvm_x86_ops->decache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002294 }
2295
Avi Kivity04d2cc72007-09-10 18:10:54 +03002296 r = __vcpu_run(vcpu, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002297
Avi Kivity039576c2007-03-20 12:46:50 +02002298out:
Avi Kivity1961d272007-03-05 19:46:05 +02002299 if (vcpu->sigset_active)
2300 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2301
Avi Kivity6aa8b732006-12-10 02:21:36 -08002302 vcpu_put(vcpu);
2303 return r;
2304}
2305
Avi Kivitybccf2152007-02-21 18:04:26 +02002306static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
2307 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002308{
Avi Kivitybccf2152007-02-21 18:04:26 +02002309 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002310
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002311 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002312
2313 regs->rax = vcpu->regs[VCPU_REGS_RAX];
2314 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
2315 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
2316 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
2317 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
2318 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
2319 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
2320 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002321#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002322 regs->r8 = vcpu->regs[VCPU_REGS_R8];
2323 regs->r9 = vcpu->regs[VCPU_REGS_R9];
2324 regs->r10 = vcpu->regs[VCPU_REGS_R10];
2325 regs->r11 = vcpu->regs[VCPU_REGS_R11];
2326 regs->r12 = vcpu->regs[VCPU_REGS_R12];
2327 regs->r13 = vcpu->regs[VCPU_REGS_R13];
2328 regs->r14 = vcpu->regs[VCPU_REGS_R14];
2329 regs->r15 = vcpu->regs[VCPU_REGS_R15];
2330#endif
2331
2332 regs->rip = vcpu->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002333 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002334
2335 /*
2336 * Don't leak debug flags in case they were set for guest debugging
2337 */
2338 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2339 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2340
2341 vcpu_put(vcpu);
2342
2343 return 0;
2344}
2345
Avi Kivitybccf2152007-02-21 18:04:26 +02002346static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
2347 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002348{
Avi Kivitybccf2152007-02-21 18:04:26 +02002349 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002350
2351 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
2352 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
2353 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
2354 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
2355 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
2356 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
2357 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
2358 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002359#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002360 vcpu->regs[VCPU_REGS_R8] = regs->r8;
2361 vcpu->regs[VCPU_REGS_R9] = regs->r9;
2362 vcpu->regs[VCPU_REGS_R10] = regs->r10;
2363 vcpu->regs[VCPU_REGS_R11] = regs->r11;
2364 vcpu->regs[VCPU_REGS_R12] = regs->r12;
2365 vcpu->regs[VCPU_REGS_R13] = regs->r13;
2366 vcpu->regs[VCPU_REGS_R14] = regs->r14;
2367 vcpu->regs[VCPU_REGS_R15] = regs->r15;
2368#endif
2369
2370 vcpu->rip = regs->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002371 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002372
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002373 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002374
2375 vcpu_put(vcpu);
2376
2377 return 0;
2378}
2379
2380static void get_segment(struct kvm_vcpu *vcpu,
2381 struct kvm_segment *var, int seg)
2382{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002383 return kvm_x86_ops->get_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002384}
2385
Avi Kivitybccf2152007-02-21 18:04:26 +02002386static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2387 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002388{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002389 struct descriptor_table dt;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002390 int pending_vec;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002391
Avi Kivitybccf2152007-02-21 18:04:26 +02002392 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002393
2394 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2395 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2396 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2397 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2398 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2399 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2400
2401 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2402 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2403
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002404 kvm_x86_ops->get_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002405 sregs->idt.limit = dt.limit;
2406 sregs->idt.base = dt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002407 kvm_x86_ops->get_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002408 sregs->gdt.limit = dt.limit;
2409 sregs->gdt.base = dt.base;
2410
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002411 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002412 sregs->cr0 = vcpu->cr0;
2413 sregs->cr2 = vcpu->cr2;
2414 sregs->cr3 = vcpu->cr3;
2415 sregs->cr4 = vcpu->cr4;
Eddie Dong7017fc32007-07-18 11:34:57 +03002416 sregs->cr8 = get_cr8(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002417 sregs->efer = vcpu->shadow_efer;
Eddie Dong7017fc32007-07-18 11:34:57 +03002418 sregs->apic_base = kvm_get_apic_base(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002419
Eddie Dong2a8067f2007-08-06 16:29:07 +03002420 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc52fb352007-08-02 14:03:07 +03002421 memset(sregs->interrupt_bitmap, 0,
2422 sizeof sregs->interrupt_bitmap);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002423 pending_vec = kvm_x86_ops->get_irq(vcpu);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002424 if (pending_vec >= 0)
Mike Dayd77c26f2007-10-08 09:02:08 -04002425 set_bit(pending_vec,
2426 (unsigned long *)sregs->interrupt_bitmap);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002427 } else
He, Qingc52fb352007-08-02 14:03:07 +03002428 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2429 sizeof sregs->interrupt_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002430
2431 vcpu_put(vcpu);
2432
2433 return 0;
2434}
2435
2436static void set_segment(struct kvm_vcpu *vcpu,
2437 struct kvm_segment *var, int seg)
2438{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002439 return kvm_x86_ops->set_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002440}
2441
Avi Kivitybccf2152007-02-21 18:04:26 +02002442static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2443 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002444{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002445 int mmu_reset_needed = 0;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002446 int i, pending_vec, max_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002447 struct descriptor_table dt;
2448
Avi Kivitybccf2152007-02-21 18:04:26 +02002449 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002450
Avi Kivity6aa8b732006-12-10 02:21:36 -08002451 dt.limit = sregs->idt.limit;
2452 dt.base = sregs->idt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002453 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002454 dt.limit = sregs->gdt.limit;
2455 dt.base = sregs->gdt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002456 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002457
2458 vcpu->cr2 = sregs->cr2;
2459 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2460 vcpu->cr3 = sregs->cr3;
2461
Eddie Dong7017fc32007-07-18 11:34:57 +03002462 set_cr8(vcpu, sregs->cr8);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002463
2464 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002465#ifdef CONFIG_X86_64
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002466 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002467#endif
Eddie Dong7017fc32007-07-18 11:34:57 +03002468 kvm_set_apic_base(vcpu, sregs->apic_base);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002469
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002470 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity399badf2007-01-05 16:36:38 -08002471
Avi Kivity6aa8b732006-12-10 02:21:36 -08002472 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
Rusty Russell81f50e32007-09-06 01:20:38 +10002473 vcpu->cr0 = sregs->cr0;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002474 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002475
2476 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002477 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Avi Kivity1b0973b2007-01-05 16:36:41 -08002478 if (!is_long_mode(vcpu) && is_pae(vcpu))
2479 load_pdptrs(vcpu, vcpu->cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002480
2481 if (mmu_reset_needed)
2482 kvm_mmu_reset_context(vcpu);
2483
He, Qingc52fb352007-08-02 14:03:07 +03002484 if (!irqchip_in_kernel(vcpu->kvm)) {
2485 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2486 sizeof vcpu->irq_pending);
2487 vcpu->irq_summary = 0;
2488 for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
2489 if (vcpu->irq_pending[i])
2490 __set_bit(i, &vcpu->irq_summary);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002491 } else {
2492 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
2493 pending_vec = find_first_bit(
2494 (const unsigned long *)sregs->interrupt_bitmap,
2495 max_bits);
2496 /* Only pending external irq is handled here */
2497 if (pending_vec < max_bits) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002498 kvm_x86_ops->set_irq(vcpu, pending_vec);
Mike Dayd77c26f2007-10-08 09:02:08 -04002499 pr_debug("Set back pending irq %d\n",
2500 pending_vec);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002501 }
He, Qingc52fb352007-08-02 14:03:07 +03002502 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002503
Avi Kivity024aa1c2007-03-21 13:44:58 +02002504 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2505 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2506 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2507 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2508 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2509 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2510
2511 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2512 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2513
Avi Kivity6aa8b732006-12-10 02:21:36 -08002514 vcpu_put(vcpu);
2515
2516 return 0;
2517}
2518
Rusty Russell1747fb72007-09-06 01:21:32 +10002519void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2520{
2521 struct kvm_segment cs;
2522
2523 get_segment(vcpu, &cs, VCPU_SREG_CS);
2524 *db = cs.db;
2525 *l = cs.l;
2526}
2527EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2528
Avi Kivity6aa8b732006-12-10 02:21:36 -08002529/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002530 * Translate a guest virtual address to a guest physical address.
2531 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002532static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2533 struct kvm_translation *tr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002534{
2535 unsigned long vaddr = tr->linear_address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002536 gpa_t gpa;
2537
Avi Kivitybccf2152007-02-21 18:04:26 +02002538 vcpu_load(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +08002539 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002540 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2541 tr->physical_address = gpa;
2542 tr->valid = gpa != UNMAPPED_GVA;
2543 tr->writeable = 1;
2544 tr->usermode = 0;
Shaohua Li11ec2802007-07-23 14:51:37 +08002545 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002546 vcpu_put(vcpu);
2547
2548 return 0;
2549}
2550
Avi Kivitybccf2152007-02-21 18:04:26 +02002551static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2552 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002553{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002554 if (irq->irq < 0 || irq->irq >= 256)
2555 return -EINVAL;
Eddie Dong97222cc2007-09-12 10:58:04 +03002556 if (irqchip_in_kernel(vcpu->kvm))
2557 return -ENXIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02002558 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002559
2560 set_bit(irq->irq, vcpu->irq_pending);
2561 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2562
2563 vcpu_put(vcpu);
2564
2565 return 0;
2566}
2567
Avi Kivitybccf2152007-02-21 18:04:26 +02002568static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2569 struct kvm_debug_guest *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002570{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002571 int r;
2572
Avi Kivitybccf2152007-02-21 18:04:26 +02002573 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002574
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002575 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002576
2577 vcpu_put(vcpu);
2578
2579 return r;
2580}
2581
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002582static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2583 unsigned long address,
2584 int *type)
2585{
2586 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2587 unsigned long pgoff;
2588 struct page *page;
2589
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002590 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +02002591 if (pgoff == 0)
2592 page = virt_to_page(vcpu->run);
2593 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2594 page = virt_to_page(vcpu->pio_data);
2595 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002596 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002597 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002598 if (type != NULL)
2599 *type = VM_FAULT_MINOR;
2600
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002601 return page;
2602}
2603
2604static struct vm_operations_struct kvm_vcpu_vm_ops = {
2605 .nopage = kvm_vcpu_nopage,
2606};
2607
2608static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2609{
2610 vma->vm_ops = &kvm_vcpu_vm_ops;
2611 return 0;
2612}
2613
Avi Kivitybccf2152007-02-21 18:04:26 +02002614static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2615{
2616 struct kvm_vcpu *vcpu = filp->private_data;
2617
2618 fput(vcpu->kvm->filp);
2619 return 0;
2620}
2621
2622static struct file_operations kvm_vcpu_fops = {
2623 .release = kvm_vcpu_release,
2624 .unlocked_ioctl = kvm_vcpu_ioctl,
2625 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002626 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02002627};
2628
2629/*
2630 * Allocates an inode for the vcpu.
2631 */
2632static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2633{
2634 int fd, r;
2635 struct inode *inode;
2636 struct file *file;
2637
Avi Kivityd6d28162007-06-28 08:38:16 -04002638 r = anon_inode_getfd(&fd, &inode, &file,
2639 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2640 if (r)
2641 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002642 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +02002643 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02002644}
2645
Avi Kivityc5ea7662007-02-20 18:41:05 +02002646/*
2647 * Creates some virtual cpus. Good luck creating more than one.
2648 */
2649static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2650{
2651 int r;
2652 struct kvm_vcpu *vcpu;
2653
Avi Kivityc5ea7662007-02-20 18:41:05 +02002654 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002655 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002656
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002657 vcpu = kvm_x86_ops->vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002658 if (IS_ERR(vcpu))
2659 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002660
Avi Kivity15ad7142007-07-11 18:17:21 +03002661 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
2662
Rusty Russellb114b082007-07-30 21:13:43 +10002663 /* We do fxsave: this must be aligned. */
2664 BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF);
2665
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002666 vcpu_load(vcpu);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002667 r = kvm_x86_ops->vcpu_reset(vcpu);
2668 if (r == 0)
2669 r = kvm_mmu_setup(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002670 vcpu_put(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002671 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002672 goto free_vcpu;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002673
Shaohua Li11ec2802007-07-23 14:51:37 +08002674 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002675 if (kvm->vcpus[n]) {
2676 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +08002677 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002678 goto mmu_unload;
2679 }
2680 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08002681 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002682
2683 /* Now it's all set up, let userspace reach it */
Avi Kivitybccf2152007-02-21 18:04:26 +02002684 r = create_vcpu_fd(vcpu);
2685 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002686 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02002687 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002688
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002689unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08002690 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002691 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +08002692 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002693
2694mmu_unload:
2695 vcpu_load(vcpu);
2696 kvm_mmu_unload(vcpu);
2697 vcpu_put(vcpu);
2698
2699free_vcpu:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002700 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002701 return r;
2702}
2703
Avi Kivity1961d272007-03-05 19:46:05 +02002704static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2705{
2706 if (sigset) {
2707 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2708 vcpu->sigset_active = 1;
2709 vcpu->sigset = *sigset;
2710 } else
2711 vcpu->sigset_active = 0;
2712 return 0;
2713}
2714
Avi Kivityb8836732007-04-01 16:34:31 +03002715/*
2716 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2717 * we have asm/x86/processor.h
2718 */
2719struct fxsave {
2720 u16 cwd;
2721 u16 swd;
2722 u16 twd;
2723 u16 fop;
2724 u64 rip;
2725 u64 rdp;
2726 u32 mxcsr;
2727 u32 mxcsr_mask;
2728 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2729#ifdef CONFIG_X86_64
2730 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2731#else
2732 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2733#endif
2734};
2735
2736static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2737{
Rusty Russellb114b082007-07-30 21:13:43 +10002738 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002739
2740 vcpu_load(vcpu);
2741
2742 memcpy(fpu->fpr, fxsave->st_space, 128);
2743 fpu->fcw = fxsave->cwd;
2744 fpu->fsw = fxsave->swd;
2745 fpu->ftwx = fxsave->twd;
2746 fpu->last_opcode = fxsave->fop;
2747 fpu->last_ip = fxsave->rip;
2748 fpu->last_dp = fxsave->rdp;
2749 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2750
2751 vcpu_put(vcpu);
2752
2753 return 0;
2754}
2755
2756static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2757{
Rusty Russellb114b082007-07-30 21:13:43 +10002758 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002759
2760 vcpu_load(vcpu);
2761
2762 memcpy(fxsave->st_space, fpu->fpr, 128);
2763 fxsave->cwd = fpu->fcw;
2764 fxsave->swd = fpu->fsw;
2765 fxsave->twd = fpu->ftwx;
2766 fxsave->fop = fpu->last_opcode;
2767 fxsave->rip = fpu->last_ip;
2768 fxsave->rdp = fpu->last_dp;
2769 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2770
2771 vcpu_put(vcpu);
2772
2773 return 0;
2774}
2775
Avi Kivitybccf2152007-02-21 18:04:26 +02002776static long kvm_vcpu_ioctl(struct file *filp,
2777 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002778{
Avi Kivitybccf2152007-02-21 18:04:26 +02002779 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +00002780 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02002781 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002782
2783 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002784 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002785 r = -EINVAL;
2786 if (arg)
2787 goto out;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002788 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002789 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002790 case KVM_GET_REGS: {
2791 struct kvm_regs kvm_regs;
2792
Avi Kivitybccf2152007-02-21 18:04:26 +02002793 memset(&kvm_regs, 0, sizeof kvm_regs);
2794 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002795 if (r)
2796 goto out;
2797 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002798 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002799 goto out;
2800 r = 0;
2801 break;
2802 }
2803 case KVM_SET_REGS: {
2804 struct kvm_regs kvm_regs;
2805
2806 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002807 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002808 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002809 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002810 if (r)
2811 goto out;
2812 r = 0;
2813 break;
2814 }
2815 case KVM_GET_SREGS: {
2816 struct kvm_sregs kvm_sregs;
2817
Avi Kivitybccf2152007-02-21 18:04:26 +02002818 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2819 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002820 if (r)
2821 goto out;
2822 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002823 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002824 goto out;
2825 r = 0;
2826 break;
2827 }
2828 case KVM_SET_SREGS: {
2829 struct kvm_sregs kvm_sregs;
2830
2831 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002832 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002833 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002834 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002835 if (r)
2836 goto out;
2837 r = 0;
2838 break;
2839 }
2840 case KVM_TRANSLATE: {
2841 struct kvm_translation tr;
2842
2843 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002844 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002845 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002846 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002847 if (r)
2848 goto out;
2849 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002850 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002851 goto out;
2852 r = 0;
2853 break;
2854 }
2855 case KVM_INTERRUPT: {
2856 struct kvm_interrupt irq;
2857
2858 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002859 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002860 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002861 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002862 if (r)
2863 goto out;
2864 r = 0;
2865 break;
2866 }
2867 case KVM_DEBUG_GUEST: {
2868 struct kvm_debug_guest dbg;
2869
2870 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002871 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002872 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002873 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002874 if (r)
2875 goto out;
2876 r = 0;
2877 break;
2878 }
Avi Kivity1961d272007-03-05 19:46:05 +02002879 case KVM_SET_SIGNAL_MASK: {
2880 struct kvm_signal_mask __user *sigmask_arg = argp;
2881 struct kvm_signal_mask kvm_sigmask;
2882 sigset_t sigset, *p;
2883
2884 p = NULL;
2885 if (argp) {
2886 r = -EFAULT;
2887 if (copy_from_user(&kvm_sigmask, argp,
2888 sizeof kvm_sigmask))
2889 goto out;
2890 r = -EINVAL;
2891 if (kvm_sigmask.len != sizeof sigset)
2892 goto out;
2893 r = -EFAULT;
2894 if (copy_from_user(&sigset, sigmask_arg->sigset,
2895 sizeof sigset))
2896 goto out;
2897 p = &sigset;
2898 }
2899 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2900 break;
2901 }
Avi Kivityb8836732007-04-01 16:34:31 +03002902 case KVM_GET_FPU: {
2903 struct kvm_fpu fpu;
2904
2905 memset(&fpu, 0, sizeof fpu);
2906 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2907 if (r)
2908 goto out;
2909 r = -EFAULT;
2910 if (copy_to_user(argp, &fpu, sizeof fpu))
2911 goto out;
2912 r = 0;
2913 break;
2914 }
2915 case KVM_SET_FPU: {
2916 struct kvm_fpu fpu;
2917
2918 r = -EFAULT;
2919 if (copy_from_user(&fpu, argp, sizeof fpu))
2920 goto out;
2921 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2922 if (r)
2923 goto out;
2924 r = 0;
2925 break;
2926 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002927 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02002928 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02002929 }
2930out:
2931 return r;
2932}
2933
2934static long kvm_vm_ioctl(struct file *filp,
2935 unsigned int ioctl, unsigned long arg)
2936{
2937 struct kvm *kvm = filp->private_data;
2938 void __user *argp = (void __user *)arg;
2939 int r = -EINVAL;
2940
2941 switch (ioctl) {
2942 case KVM_CREATE_VCPU:
2943 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2944 if (r < 0)
2945 goto out;
2946 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002947 case KVM_SET_MEMORY_REGION: {
2948 struct kvm_memory_region kvm_mem;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002949 struct kvm_userspace_memory_region kvm_userspace_mem;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002950
2951 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002952 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002953 goto out;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002954 kvm_userspace_mem.slot = kvm_mem.slot;
2955 kvm_userspace_mem.flags = kvm_mem.flags;
2956 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
2957 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
2958 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
2959 if (r)
2960 goto out;
2961 break;
2962 }
2963 case KVM_SET_USER_MEMORY_REGION: {
2964 struct kvm_userspace_memory_region kvm_userspace_mem;
2965
2966 r = -EFAULT;
2967 if (copy_from_user(&kvm_userspace_mem, argp,
2968 sizeof kvm_userspace_mem))
2969 goto out;
2970
2971 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002972 if (r)
2973 goto out;
2974 break;
2975 }
Izik Eidus82ce2c92007-10-02 18:52:55 +02002976 case KVM_SET_NR_MMU_PAGES:
2977 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
2978 if (r)
2979 goto out;
2980 break;
2981 case KVM_GET_NR_MMU_PAGES:
2982 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
2983 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002984 case KVM_GET_DIRTY_LOG: {
2985 struct kvm_dirty_log log;
2986
2987 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002988 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002989 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002990 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002991 if (r)
2992 goto out;
2993 break;
2994 }
Avi Kivitye8207542007-03-30 16:54:30 +03002995 case KVM_SET_MEMORY_ALIAS: {
2996 struct kvm_memory_alias alias;
2997
2998 r = -EFAULT;
2999 if (copy_from_user(&alias, argp, sizeof alias))
3000 goto out;
3001 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
3002 if (r)
3003 goto out;
3004 break;
3005 }
Eddie Dong85f455f2007-07-06 12:20:49 +03003006 case KVM_CREATE_IRQCHIP:
3007 r = -ENOMEM;
3008 kvm->vpic = kvm_create_pic(kvm);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03003009 if (kvm->vpic) {
3010 r = kvm_ioapic_init(kvm);
3011 if (r) {
3012 kfree(kvm->vpic);
3013 kvm->vpic = NULL;
3014 goto out;
3015 }
Mike Dayd77c26f2007-10-08 09:02:08 -04003016 } else
Eddie Dong85f455f2007-07-06 12:20:49 +03003017 goto out;
3018 break;
3019 case KVM_IRQ_LINE: {
3020 struct kvm_irq_level irq_event;
3021
3022 r = -EFAULT;
3023 if (copy_from_user(&irq_event, argp, sizeof irq_event))
3024 goto out;
3025 if (irqchip_in_kernel(kvm)) {
Eddie Dong9cf98822007-07-22 10:36:31 +03003026 mutex_lock(&kvm->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +03003027 if (irq_event.irq < 16)
3028 kvm_pic_set_irq(pic_irqchip(kvm),
3029 irq_event.irq,
3030 irq_event.level);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03003031 kvm_ioapic_set_irq(kvm->vioapic,
3032 irq_event.irq,
3033 irq_event.level);
Eddie Dong9cf98822007-07-22 10:36:31 +03003034 mutex_unlock(&kvm->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +03003035 r = 0;
3036 }
3037 break;
3038 }
He, Qing6ceb9d72007-07-26 11:05:18 +03003039 case KVM_GET_IRQCHIP: {
3040 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3041 struct kvm_irqchip chip;
3042
3043 r = -EFAULT;
3044 if (copy_from_user(&chip, argp, sizeof chip))
3045 goto out;
3046 r = -ENXIO;
3047 if (!irqchip_in_kernel(kvm))
3048 goto out;
3049 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
3050 if (r)
3051 goto out;
3052 r = -EFAULT;
3053 if (copy_to_user(argp, &chip, sizeof chip))
3054 goto out;
3055 r = 0;
3056 break;
3057 }
3058 case KVM_SET_IRQCHIP: {
3059 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3060 struct kvm_irqchip chip;
3061
3062 r = -EFAULT;
3063 if (copy_from_user(&chip, argp, sizeof chip))
3064 goto out;
3065 r = -ENXIO;
3066 if (!irqchip_in_kernel(kvm))
3067 goto out;
3068 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
3069 if (r)
3070 goto out;
3071 r = 0;
3072 break;
3073 }
Avi Kivityf17abe92007-02-21 19:28:04 +02003074 default:
3075 ;
3076 }
3077out:
3078 return r;
3079}
3080
3081static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
3082 unsigned long address,
3083 int *type)
3084{
3085 struct kvm *kvm = vma->vm_file->private_data;
3086 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02003087 struct page *page;
3088
Avi Kivityf17abe92007-02-21 19:28:04 +02003089 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity954bbbc22007-03-30 14:02:32 +03003090 page = gfn_to_page(kvm, pgoff);
Izik Eidus8a7ae052007-10-18 11:09:33 +02003091 if (is_error_page(page)) {
3092 kvm_release_page(page);
Avi Kivityf17abe92007-02-21 19:28:04 +02003093 return NOPAGE_SIGBUS;
Izik Eidus8a7ae052007-10-18 11:09:33 +02003094 }
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03003095 if (type != NULL)
3096 *type = VM_FAULT_MINOR;
3097
Avi Kivityf17abe92007-02-21 19:28:04 +02003098 return page;
3099}
3100
3101static struct vm_operations_struct kvm_vm_vm_ops = {
3102 .nopage = kvm_vm_nopage,
3103};
3104
3105static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
3106{
3107 vma->vm_ops = &kvm_vm_vm_ops;
3108 return 0;
3109}
3110
3111static struct file_operations kvm_vm_fops = {
3112 .release = kvm_vm_release,
3113 .unlocked_ioctl = kvm_vm_ioctl,
3114 .compat_ioctl = kvm_vm_ioctl,
3115 .mmap = kvm_vm_mmap,
3116};
3117
3118static int kvm_dev_ioctl_create_vm(void)
3119{
3120 int fd, r;
3121 struct inode *inode;
3122 struct file *file;
3123 struct kvm *kvm;
3124
Avi Kivityf17abe92007-02-21 19:28:04 +02003125 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04003126 if (IS_ERR(kvm))
3127 return PTR_ERR(kvm);
3128 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
3129 if (r) {
3130 kvm_destroy_vm(kvm);
3131 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02003132 }
3133
Avi Kivitybccf2152007-02-21 18:04:26 +02003134 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02003135
Avi Kivityf17abe92007-02-21 19:28:04 +02003136 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02003137}
3138
3139static long kvm_dev_ioctl(struct file *filp,
3140 unsigned int ioctl, unsigned long arg)
3141{
3142 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02003143 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02003144
3145 switch (ioctl) {
3146 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02003147 r = -EINVAL;
3148 if (arg)
3149 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02003150 r = KVM_API_VERSION;
3151 break;
3152 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02003153 r = -EINVAL;
3154 if (arg)
3155 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02003156 r = kvm_dev_ioctl_create_vm();
3157 break;
Eddie Dong85f455f2007-07-06 12:20:49 +03003158 case KVM_CHECK_EXTENSION: {
3159 int ext = (long)argp;
3160
3161 switch (ext) {
3162 case KVM_CAP_IRQCHIP:
Eddie Dongb6958ce2007-07-18 12:15:21 +03003163 case KVM_CAP_HLT:
Izik Eidus82ce2c92007-10-02 18:52:55 +02003164 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Izik Eidus6fc138d2007-10-09 19:20:39 +02003165 case KVM_CAP_USER_MEMORY:
Eddie Dong85f455f2007-07-06 12:20:49 +03003166 r = 1;
3167 break;
3168 default:
3169 r = 0;
3170 break;
3171 }
Avi Kivity5d308f42007-03-01 17:56:20 +02003172 break;
Eddie Dong85f455f2007-07-06 12:20:49 +03003173 }
Avi Kivity07c45a32007-03-07 13:05:38 +02003174 case KVM_GET_VCPU_MMAP_SIZE:
3175 r = -EINVAL;
3176 if (arg)
3177 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02003178 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02003179 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003180 default:
Carsten Otte043405e2007-10-10 17:16:19 +02003181 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003182 }
3183out:
3184 return r;
3185}
3186
Avi Kivity6aa8b732006-12-10 02:21:36 -08003187static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003188 .unlocked_ioctl = kvm_dev_ioctl,
3189 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003190};
3191
3192static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02003193 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003194 "kvm",
3195 &kvm_chardev_ops,
3196};
3197
Avi Kivity774c47f2007-02-12 00:54:47 -08003198/*
3199 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
3200 * cached on it.
3201 */
3202static void decache_vcpus_on_cpu(int cpu)
3203{
3204 struct kvm *vm;
3205 struct kvm_vcpu *vcpu;
3206 int i;
3207
3208 spin_lock(&kvm_lock);
Shaohua Li11ec2802007-07-23 14:51:37 +08003209 list_for_each_entry(vm, &vm_list, vm_list)
Avi Kivity774c47f2007-02-12 00:54:47 -08003210 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003211 vcpu = vm->vcpus[i];
3212 if (!vcpu)
3213 continue;
Avi Kivity774c47f2007-02-12 00:54:47 -08003214 /*
3215 * If the vcpu is locked, then it is running on some
3216 * other cpu and therefore it is not cached on the
3217 * cpu in question.
3218 *
3219 * If it's not locked, check the last cpu it executed
3220 * on.
3221 */
3222 if (mutex_trylock(&vcpu->mutex)) {
3223 if (vcpu->cpu == cpu) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003224 kvm_x86_ops->vcpu_decache(vcpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08003225 vcpu->cpu = -1;
3226 }
3227 mutex_unlock(&vcpu->mutex);
3228 }
3229 }
3230 spin_unlock(&kvm_lock);
3231}
3232
Avi Kivity1b6c0162007-05-24 13:03:52 +03003233static void hardware_enable(void *junk)
3234{
3235 int cpu = raw_smp_processor_id();
3236
3237 if (cpu_isset(cpu, cpus_hardware_enabled))
3238 return;
3239 cpu_set(cpu, cpus_hardware_enabled);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003240 kvm_x86_ops->hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003241}
3242
3243static void hardware_disable(void *junk)
3244{
3245 int cpu = raw_smp_processor_id();
3246
3247 if (!cpu_isset(cpu, cpus_hardware_enabled))
3248 return;
3249 cpu_clear(cpu, cpus_hardware_enabled);
3250 decache_vcpus_on_cpu(cpu);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003251 kvm_x86_ops->hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003252}
3253
Avi Kivity774c47f2007-02-12 00:54:47 -08003254static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
3255 void *v)
3256{
3257 int cpu = (long)v;
3258
3259 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03003260 case CPU_DYING:
3261 case CPU_DYING_FROZEN:
Avi Kivity6ec8a852007-08-19 15:57:26 +03003262 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3263 cpu);
3264 hardware_disable(NULL);
3265 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08003266 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003267 case CPU_UP_CANCELED_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003268 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3269 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003270 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003271 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02003272 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003273 case CPU_ONLINE_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003274 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
3275 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003276 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003277 break;
3278 }
3279 return NOTIFY_OK;
3280}
3281
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003282static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04003283 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003284{
3285 if (val == SYS_RESTART) {
3286 /*
3287 * Some (well, at least mine) BIOSes hang on reboot if
3288 * in vmx root mode.
3289 */
3290 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
3291 on_each_cpu(hardware_disable, NULL, 0, 1);
3292 }
3293 return NOTIFY_OK;
3294}
3295
3296static struct notifier_block kvm_reboot_notifier = {
3297 .notifier_call = kvm_reboot,
3298 .priority = 0,
3299};
3300
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04003301void kvm_io_bus_init(struct kvm_io_bus *bus)
3302{
3303 memset(bus, 0, sizeof(*bus));
3304}
3305
3306void kvm_io_bus_destroy(struct kvm_io_bus *bus)
3307{
3308 int i;
3309
3310 for (i = 0; i < bus->dev_count; i++) {
3311 struct kvm_io_device *pos = bus->devs[i];
3312
3313 kvm_iodevice_destructor(pos);
3314 }
3315}
3316
3317struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
3318{
3319 int i;
3320
3321 for (i = 0; i < bus->dev_count; i++) {
3322 struct kvm_io_device *pos = bus->devs[i];
3323
3324 if (pos->in_range(pos, addr))
3325 return pos;
3326 }
3327
3328 return NULL;
3329}
3330
3331void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
3332{
3333 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
3334
3335 bus->devs[bus->dev_count++] = dev;
3336}
3337
Avi Kivity774c47f2007-02-12 00:54:47 -08003338static struct notifier_block kvm_cpu_notifier = {
3339 .notifier_call = kvm_cpu_hotplug,
3340 .priority = 20, /* must be > scheduler priority */
3341};
3342
Avi Kivity1165f5f2007-04-19 17:27:43 +03003343static u64 stat_get(void *_offset)
3344{
3345 unsigned offset = (long)_offset;
3346 u64 total = 0;
3347 struct kvm *kvm;
3348 struct kvm_vcpu *vcpu;
3349 int i;
3350
3351 spin_lock(&kvm_lock);
3352 list_for_each_entry(kvm, &vm_list, vm_list)
3353 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003354 vcpu = kvm->vcpus[i];
3355 if (vcpu)
3356 total += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03003357 }
3358 spin_unlock(&kvm_lock);
3359 return total;
3360}
3361
Rusty Russell3dea7ca2007-08-01 10:12:22 +10003362DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n");
Avi Kivity1165f5f2007-04-19 17:27:43 +03003363
Avi Kivity6aa8b732006-12-10 02:21:36 -08003364static __init void kvm_init_debug(void)
3365{
3366 struct kvm_stats_debugfs_item *p;
3367
Al Viro8b6d44c2007-02-09 16:38:40 +00003368 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003369 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03003370 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
3371 (void *)(long)p->offset,
3372 &stat_fops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003373}
3374
3375static void kvm_exit_debug(void)
3376{
3377 struct kvm_stats_debugfs_item *p;
3378
3379 for (p = debugfs_entries; p->name; ++p)
3380 debugfs_remove(p->dentry);
3381 debugfs_remove(debugfs_dir);
3382}
3383
Avi Kivity59ae6c62007-02-12 00:54:48 -08003384static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3385{
Avi Kivity4267c412007-05-24 13:09:41 +03003386 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003387 return 0;
3388}
3389
3390static int kvm_resume(struct sys_device *dev)
3391{
Avi Kivity4267c412007-05-24 13:09:41 +03003392 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003393 return 0;
3394}
3395
3396static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01003397 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08003398 .suspend = kvm_suspend,
3399 .resume = kvm_resume,
3400};
3401
3402static struct sys_device kvm_sysdev = {
3403 .id = 0,
3404 .cls = &kvm_sysdev_class,
3405};
3406
Izik Eiduscea7bb22007-10-17 19:17:48 +02003407struct page *bad_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003408
Avi Kivity15ad7142007-07-11 18:17:21 +03003409static inline
3410struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
3411{
3412 return container_of(pn, struct kvm_vcpu, preempt_notifier);
3413}
3414
3415static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
3416{
3417 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3418
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003419 kvm_x86_ops->vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003420}
3421
3422static void kvm_sched_out(struct preempt_notifier *pn,
3423 struct task_struct *next)
3424{
3425 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3426
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003427 kvm_x86_ops->vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003428}
3429
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003430int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10003431 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003432{
3433 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003434 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003435
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003436 if (kvm_x86_ops) {
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08003437 printk(KERN_ERR "kvm: already loaded the other module\n");
3438 return -EEXIST;
3439 }
3440
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003441 if (!ops->cpu_has_kvm_support()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003442 printk(KERN_ERR "kvm: no hardware support\n");
3443 return -EOPNOTSUPP;
3444 }
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003445 if (ops->disabled_by_bios()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003446 printk(KERN_ERR "kvm: disabled by bios\n");
3447 return -EOPNOTSUPP;
3448 }
3449
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003450 kvm_x86_ops = ops;
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003451
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003452 r = kvm_x86_ops->hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003453 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02003454 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003455
Yang, Sheng002c7f72007-07-31 14:23:01 +03003456 for_each_online_cpu(cpu) {
3457 smp_call_function_single(cpu,
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003458 kvm_x86_ops->check_processor_compatibility,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003459 &r, 0, 1);
3460 if (r < 0)
3461 goto out_free_0;
3462 }
3463
Avi Kivity1b6c0162007-05-24 13:03:52 +03003464 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003465 r = register_cpu_notifier(&kvm_cpu_notifier);
3466 if (r)
3467 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003468 register_reboot_notifier(&kvm_reboot_notifier);
3469
Avi Kivity59ae6c62007-02-12 00:54:48 -08003470 r = sysdev_class_register(&kvm_sysdev_class);
3471 if (r)
3472 goto out_free_2;
3473
3474 r = sysdev_register(&kvm_sysdev);
3475 if (r)
3476 goto out_free_3;
3477
Rusty Russellc16f8622007-07-30 21:12:19 +10003478 /* A kmem cache lets us meet the alignment requirements of fx_save. */
3479 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
3480 __alignof__(struct kvm_vcpu), 0, 0);
3481 if (!kvm_vcpu_cache) {
3482 r = -ENOMEM;
3483 goto out_free_4;
3484 }
3485
Avi Kivity6aa8b732006-12-10 02:21:36 -08003486 kvm_chardev_ops.owner = module;
3487
3488 r = misc_register(&kvm_dev);
3489 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04003490 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08003491 goto out_free;
3492 }
3493
Avi Kivity15ad7142007-07-11 18:17:21 +03003494 kvm_preempt_ops.sched_in = kvm_sched_in;
3495 kvm_preempt_ops.sched_out = kvm_sched_out;
3496
Avi Kivityc7addb92007-09-16 18:58:32 +02003497 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
3498
3499 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003500
3501out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10003502 kmem_cache_destroy(kvm_vcpu_cache);
3503out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08003504 sysdev_unregister(&kvm_sysdev);
3505out_free_3:
3506 sysdev_class_unregister(&kvm_sysdev_class);
3507out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003508 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08003509 unregister_cpu_notifier(&kvm_cpu_notifier);
3510out_free_1:
Avi Kivity1b6c0162007-05-24 13:03:52 +03003511 on_each_cpu(hardware_disable, NULL, 0, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03003512out_free_0:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003513 kvm_x86_ops->hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02003514out:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003515 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003516 return r;
3517}
Mike Dayd77c26f2007-10-08 09:02:08 -04003518EXPORT_SYMBOL_GPL(kvm_init_x86);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003519
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003520void kvm_exit_x86(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003521{
3522 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10003523 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003524 sysdev_unregister(&kvm_sysdev);
3525 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003526 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003527 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003528 on_each_cpu(hardware_disable, NULL, 0, 1);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003529 kvm_x86_ops->hardware_unsetup();
3530 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003531}
Mike Dayd77c26f2007-10-08 09:02:08 -04003532EXPORT_SYMBOL_GPL(kvm_exit_x86);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003533
3534static __init int kvm_init(void)
3535{
Avi Kivity37e29d92007-02-20 14:07:37 +02003536 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003537
Avi Kivityb5a33a72007-04-15 16:31:09 +03003538 r = kvm_mmu_module_init();
3539 if (r)
3540 goto out4;
3541
Avi Kivity6aa8b732006-12-10 02:21:36 -08003542 kvm_init_debug();
3543
Carsten Otte043405e2007-10-10 17:16:19 +02003544 kvm_arch_init();
Michael Riepebf591b22006-12-22 01:05:36 -08003545
Izik Eiduscea7bb22007-10-17 19:17:48 +02003546 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
Mike Dayd77c26f2007-10-08 09:02:08 -04003547
3548 if (bad_page == NULL) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003549 r = -ENOMEM;
3550 goto out;
3551 }
3552
Avi Kivity58e690e2007-02-26 16:29:43 +02003553 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003554
3555out:
3556 kvm_exit_debug();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003557 kvm_mmu_module_exit();
3558out4:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003559 return r;
3560}
3561
3562static __exit void kvm_exit(void)
3563{
3564 kvm_exit_debug();
Izik Eiduscea7bb22007-10-17 19:17:48 +02003565 __free_page(bad_page);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003566 kvm_mmu_module_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003567}
3568
3569module_init(kvm_init)
3570module_exit(kvm_exit)