blob: ab2c77c9ecef925c60e6eabe095241a5664a65fa [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 *
9 * Authors:
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
17
18#include "kvm.h"
Carsten Otte043405e2007-10-10 17:16:19 +020019#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040020#include "x86_emulate.h"
21#include "segment_descriptor.h"
Eddie Dong85f455f2007-07-06 12:20:49 +030022#include "irq.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080023
24#include <linux/kvm.h>
25#include <linux/module.h>
26#include <linux/errno.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/percpu.h>
28#include <linux/gfp.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080029#include <linux/mm.h>
30#include <linux/miscdevice.h>
31#include <linux/vmalloc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080032#include <linux/reboot.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080033#include <linux/debugfs.h>
34#include <linux/highmem.h>
35#include <linux/file.h>
Avi Kivity59ae6c62007-02-12 00:54:48 -080036#include <linux/sysdev.h>
Avi Kivity774c47f2007-02-12 00:54:47 -080037#include <linux/cpu.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040038#include <linux/sched.h>
Avi Kivityd9e368d2007-06-07 19:18:30 +030039#include <linux/cpumask.h>
40#include <linux/smp.h>
Avi Kivityd6d28162007-06-28 08:38:16 -040041#include <linux/anon_inodes.h>
Avi Kivity04d2cc72007-09-10 18:10:54 +030042#include <linux/profile.h>
Anthony Liguori7aa81cc2007-09-17 14:57:50 -050043#include <linux/kvm_para.h>
Izik Eidus6fc138d2007-10-09 19:20:39 +020044#include <linux/pagemap.h>
Anthony Liguori8d4e1282007-10-18 09:59:34 -050045#include <linux/mman.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080046
Avi Kivitye4956062007-06-28 14:15:57 -040047#include <asm/processor.h>
48#include <asm/msr.h>
49#include <asm/io.h>
50#include <asm/uaccess.h>
51#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080052
53MODULE_AUTHOR("Qumranet");
54MODULE_LICENSE("GPL");
55
Avi Kivity133de902007-02-12 00:54:44 -080056static DEFINE_SPINLOCK(kvm_lock);
57static LIST_HEAD(vm_list);
58
Avi Kivity1b6c0162007-05-24 13:03:52 +030059static cpumask_t cpus_hardware_enabled;
60
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +030061struct kvm_x86_ops *kvm_x86_ops;
Rusty Russellc16f8622007-07-30 21:12:19 +100062struct kmem_cache *kvm_vcpu_cache;
63EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030064
Avi Kivity15ad7142007-07-11 18:17:21 +030065static __read_mostly struct preempt_ops kvm_preempt_ops;
66
Avi Kivity1165f5f2007-04-19 17:27:43 +030067#define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
Avi Kivity6aa8b732006-12-10 02:21:36 -080068
69static struct kvm_stats_debugfs_item {
70 const char *name;
Avi Kivity1165f5f2007-04-19 17:27:43 +030071 int offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -080072 struct dentry *dentry;
73} debugfs_entries[] = {
Avi Kivity1165f5f2007-04-19 17:27:43 +030074 { "pf_fixed", STAT_OFFSET(pf_fixed) },
75 { "pf_guest", STAT_OFFSET(pf_guest) },
76 { "tlb_flush", STAT_OFFSET(tlb_flush) },
77 { "invlpg", STAT_OFFSET(invlpg) },
78 { "exits", STAT_OFFSET(exits) },
79 { "io_exits", STAT_OFFSET(io_exits) },
80 { "mmio_exits", STAT_OFFSET(mmio_exits) },
81 { "signal_exits", STAT_OFFSET(signal_exits) },
82 { "irq_window", STAT_OFFSET(irq_window_exits) },
83 { "halt_exits", STAT_OFFSET(halt_exits) },
Eddie Dongb6958ce2007-07-18 12:15:21 +030084 { "halt_wakeup", STAT_OFFSET(halt_wakeup) },
Avi Kivity1165f5f2007-04-19 17:27:43 +030085 { "request_irq", STAT_OFFSET(request_irq_exits) },
86 { "irq_exits", STAT_OFFSET(irq_exits) },
Avi Kivitye6adf282007-04-30 16:07:54 +030087 { "light_exits", STAT_OFFSET(light_exits) },
Eddie Dong2cc51562007-05-21 07:28:09 +030088 { "efer_reload", STAT_OFFSET(efer_reload) },
Avi Kivity1165f5f2007-04-19 17:27:43 +030089 { NULL }
Avi Kivity6aa8b732006-12-10 02:21:36 -080090};
91
92static struct dentry *debugfs_dir;
93
Rusty Russell707d92f2007-07-17 23:19:08 +100094#define CR0_RESERVED_BITS \
95 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
96 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
97 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
Rusty Russell66aee912007-07-17 23:34:16 +100098#define CR4_RESERVED_BITS \
99 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
100 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
101 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
102 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
103
Rusty Russell7075bc82007-07-17 23:37:17 +1000104#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800105#define EFER_RESERVED_BITS 0xfffffffffffff2fe
106
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800107#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400108/* LDT or TSS descriptor in the GDT. 16 bytes. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800109struct segment_descriptor_64 {
110 struct segment_descriptor s;
111 u32 base_higher;
112 u32 pad_zero;
113};
114
115#endif
116
Avi Kivitybccf2152007-02-21 18:04:26 +0200117static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
118 unsigned long arg);
119
Avi Kivity6aa8b732006-12-10 02:21:36 -0800120unsigned long segment_base(u16 selector)
121{
122 struct descriptor_table gdt;
123 struct segment_descriptor *d;
124 unsigned long table_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800125 unsigned long v;
126
127 if (selector == 0)
128 return 0;
129
Mike Dayd77c26f2007-10-08 09:02:08 -0400130 asm("sgdt %0" : "=m"(gdt));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131 table_base = gdt.base;
132
133 if (selector & 4) { /* from ldt */
134 u16 ldt_selector;
135
Mike Dayd77c26f2007-10-08 09:02:08 -0400136 asm("sldt %0" : "=g"(ldt_selector));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800137 table_base = segment_base(ldt_selector);
138 }
139 d = (struct segment_descriptor *)(table_base + (selector & ~7));
Mike Dayd77c26f2007-10-08 09:02:08 -0400140 v = d->base_low | ((unsigned long)d->base_mid << 16) |
141 ((unsigned long)d->base_high << 24);
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800142#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400143 if (d->system == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
144 v |= ((unsigned long) \
145 ((struct segment_descriptor_64 *)d)->base_higher) << 32;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800146#endif
147 return v;
148}
149EXPORT_SYMBOL_GPL(segment_base);
150
James Morris5aacf0c2006-12-22 01:04:55 -0800151static inline int valid_vcpu(int n)
152{
153 return likely(n >= 0 && n < KVM_MAX_VCPUS);
154}
155
Avi Kivity7702fd12007-06-14 16:27:40 +0300156void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
157{
158 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
159 return;
160
161 vcpu->guest_fpu_loaded = 1;
Rusty Russellb114b082007-07-30 21:13:43 +1000162 fx_save(&vcpu->host_fx_image);
163 fx_restore(&vcpu->guest_fx_image);
Avi Kivity7702fd12007-06-14 16:27:40 +0300164}
165EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
166
167void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
168{
169 if (!vcpu->guest_fpu_loaded)
170 return;
171
172 vcpu->guest_fpu_loaded = 0;
Rusty Russellb114b082007-07-30 21:13:43 +1000173 fx_save(&vcpu->guest_fx_image);
174 fx_restore(&vcpu->host_fx_image);
Avi Kivity7702fd12007-06-14 16:27:40 +0300175}
176EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
177
Avi Kivity6aa8b732006-12-10 02:21:36 -0800178/*
179 * Switches to specified vcpu, until a matching vcpu_put()
180 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200181void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800182{
Avi Kivity15ad7142007-07-11 18:17:21 +0300183 int cpu;
184
Avi Kivitybccf2152007-02-21 18:04:26 +0200185 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300186 cpu = get_cpu();
187 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200188 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300189 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200190}
191
Carsten Otte313a3dc2007-10-11 19:16:52 +0200192void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800193{
Avi Kivity15ad7142007-07-11 18:17:21 +0300194 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200195 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300196 preempt_notifier_unregister(&vcpu->preempt_notifier);
197 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800198 mutex_unlock(&vcpu->mutex);
199}
200
Avi Kivityd9e368d2007-06-07 19:18:30 +0300201static void ack_flush(void *_completed)
202{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300203}
204
205void kvm_flush_remote_tlbs(struct kvm *kvm)
206{
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200207 int i, cpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300208 cpumask_t cpus;
209 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300210
Avi Kivityd9e368d2007-06-07 19:18:30 +0300211 cpus_clear(cpus);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000212 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
213 vcpu = kvm->vcpus[i];
214 if (!vcpu)
215 continue;
Avi Kivity3176bc32007-10-16 17:22:08 +0200216 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300217 continue;
218 cpu = vcpu->cpu;
219 if (cpu != -1 && cpu != raw_smp_processor_id())
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200220 cpu_set(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300221 }
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200222 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300223}
224
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000225int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
226{
227 struct page *page;
228 int r;
229
230 mutex_init(&vcpu->mutex);
231 vcpu->cpu = -1;
232 vcpu->mmu.root_hpa = INVALID_PAGE;
233 vcpu->kvm = kvm;
234 vcpu->vcpu_id = id;
He, Qingc5ec1532007-09-03 17:07:41 +0300235 if (!irqchip_in_kernel(kvm) || id == 0)
236 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
237 else
238 vcpu->mp_state = VCPU_MP_STATE_UNINITIALIZED;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300239 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000240
241 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
242 if (!page) {
243 r = -ENOMEM;
244 goto fail;
245 }
246 vcpu->run = page_address(page);
247
248 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
249 if (!page) {
250 r = -ENOMEM;
251 goto fail_free_run;
252 }
253 vcpu->pio_data = page_address(page);
254
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000255 r = kvm_mmu_create(vcpu);
256 if (r < 0)
257 goto fail_free_pio_data;
258
Rusty Russell76fafa52007-10-08 10:50:48 +1000259 if (irqchip_in_kernel(kvm)) {
260 r = kvm_create_lapic(vcpu);
261 if (r < 0)
262 goto fail_mmu_destroy;
263 }
264
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000265 return 0;
266
Rusty Russell76fafa52007-10-08 10:50:48 +1000267fail_mmu_destroy:
268 kvm_mmu_destroy(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000269fail_free_pio_data:
270 free_page((unsigned long)vcpu->pio_data);
271fail_free_run:
272 free_page((unsigned long)vcpu->run);
273fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000274 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000275}
276EXPORT_SYMBOL_GPL(kvm_vcpu_init);
277
278void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
279{
Rusty Russelld5894442007-10-08 10:48:30 +1000280 kvm_free_lapic(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000281 kvm_mmu_destroy(vcpu);
282 free_page((unsigned long)vcpu->pio_data);
283 free_page((unsigned long)vcpu->run);
284}
285EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
286
Avi Kivityf17abe92007-02-21 19:28:04 +0200287static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800288{
289 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800290
291 if (!kvm)
Avi Kivityf17abe92007-02-21 19:28:04 +0200292 return ERR_PTR(-ENOMEM);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800293
Eddie Dong74906342007-06-19 18:05:03 +0300294 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800295 mutex_init(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800296 INIT_LIST_HEAD(&kvm->active_mmu_pages);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400297 kvm_io_bus_init(&kvm->mmio_bus);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000298 spin_lock(&kvm_lock);
299 list_add(&kvm->vm_list, &vm_list);
300 spin_unlock(&kvm_lock);
Avi Kivityf17abe92007-02-21 19:28:04 +0200301 return kvm;
302}
303
Avi Kivity6aa8b732006-12-10 02:21:36 -0800304/*
305 * Free any memory in @free but not in @dont.
306 */
307static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
308 struct kvm_memory_slot *dont)
309{
Izik Eidus290fc382007-09-27 14:11:22 +0200310 if (!dont || free->rmap != dont->rmap)
311 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800312
313 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
314 vfree(free->dirty_bitmap);
315
Avi Kivity6aa8b732006-12-10 02:21:36 -0800316 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000317 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500318 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800319}
320
321static void kvm_free_physmem(struct kvm *kvm)
322{
323 int i;
324
325 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000326 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800327}
328
Avi Kivity039576c2007-03-20 12:46:50 +0200329static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
330{
331 int i;
332
Rusty Russell3077c4512007-07-30 16:41:57 +1000333 for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i)
Avi Kivity039576c2007-03-20 12:46:50 +0200334 if (vcpu->pio.guest_pages[i]) {
Izik Eidus8a7ae052007-10-18 11:09:33 +0200335 kvm_release_page(vcpu->pio.guest_pages[i]);
Avi Kivity039576c2007-03-20 12:46:50 +0200336 vcpu->pio.guest_pages[i] = NULL;
337 }
338}
339
Avi Kivity7b53aa52007-06-05 12:17:03 +0300340static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
341{
Avi Kivity7b53aa52007-06-05 12:17:03 +0300342 vcpu_load(vcpu);
343 kvm_mmu_unload(vcpu);
344 vcpu_put(vcpu);
345}
346
Avi Kivity6aa8b732006-12-10 02:21:36 -0800347static void kvm_free_vcpus(struct kvm *kvm)
348{
349 unsigned int i;
350
Avi Kivity7b53aa52007-06-05 12:17:03 +0300351 /*
352 * Unpin any mmu pages first.
353 */
354 for (i = 0; i < KVM_MAX_VCPUS; ++i)
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000355 if (kvm->vcpus[i])
356 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
357 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
358 if (kvm->vcpus[i]) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300359 kvm_x86_ops->vcpu_free(kvm->vcpus[i]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000360 kvm->vcpus[i] = NULL;
361 }
362 }
363
Avi Kivity6aa8b732006-12-10 02:21:36 -0800364}
365
Avi Kivityf17abe92007-02-21 19:28:04 +0200366static void kvm_destroy_vm(struct kvm *kvm)
367{
Avi Kivity133de902007-02-12 00:54:44 -0800368 spin_lock(&kvm_lock);
369 list_del(&kvm->vm_list);
370 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300371 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400372 kvm_io_bus_destroy(&kvm->mmio_bus);
Eddie Dong85f455f2007-07-06 12:20:49 +0300373 kfree(kvm->vpic);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300374 kfree(kvm->vioapic);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800375 kvm_free_vcpus(kvm);
376 kvm_free_physmem(kvm);
377 kfree(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200378}
379
380static int kvm_vm_release(struct inode *inode, struct file *filp)
381{
382 struct kvm *kvm = filp->private_data;
383
384 kvm_destroy_vm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800385 return 0;
386}
387
388static void inject_gp(struct kvm_vcpu *vcpu)
389{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300390 kvm_x86_ops->inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800391}
392
Avi Kivity1342d352007-01-05 16:36:39 -0800393/*
394 * Load the pae pdptrs. Return true is they are all valid.
395 */
396static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800397{
398 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
Avi Kivity1342d352007-01-05 16:36:39 -0800399 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800400 int i;
Avi Kivity1342d352007-01-05 16:36:39 -0800401 int ret;
Rusty Russellc820c2a2007-07-25 13:29:51 +1000402 u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800403
Shaohua Li11ec2802007-07-23 14:51:37 +0800404 mutex_lock(&vcpu->kvm->lock);
Izik Eidus195aefd2007-10-01 22:14:18 +0200405 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
406 offset * sizeof(u64), sizeof(pdpte));
407 if (ret < 0) {
Rusty Russellc820c2a2007-07-25 13:29:51 +1000408 ret = 0;
409 goto out;
410 }
Rusty Russellc820c2a2007-07-25 13:29:51 +1000411 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
412 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
Avi Kivity1342d352007-01-05 16:36:39 -0800413 ret = 0;
414 goto out;
415 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800416 }
Rusty Russellc820c2a2007-07-25 13:29:51 +1000417 ret = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800418
Rusty Russellc820c2a2007-07-25 13:29:51 +1000419 memcpy(vcpu->pdptrs, pdpte, sizeof(vcpu->pdptrs));
Avi Kivity1342d352007-01-05 16:36:39 -0800420out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800421 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800422
Avi Kivity1342d352007-01-05 16:36:39 -0800423 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424}
425
426void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
427{
Rusty Russell707d92f2007-07-17 23:19:08 +1000428 if (cr0 & CR0_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800429 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
430 cr0, vcpu->cr0);
431 inject_gp(vcpu);
432 return;
433 }
434
Rusty Russell707d92f2007-07-17 23:19:08 +1000435 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800436 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
437 inject_gp(vcpu);
438 return;
439 }
440
Rusty Russell707d92f2007-07-17 23:19:08 +1000441 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800442 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
443 "and a clear PE flag\n");
444 inject_gp(vcpu);
445 return;
446 }
447
Rusty Russell707d92f2007-07-17 23:19:08 +1000448 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800449#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800450 if ((vcpu->shadow_efer & EFER_LME)) {
451 int cs_db, cs_l;
452
453 if (!is_pae(vcpu)) {
454 printk(KERN_DEBUG "set_cr0: #GP, start paging "
455 "in long mode while PAE is disabled\n");
456 inject_gp(vcpu);
457 return;
458 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300459 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800460 if (cs_l) {
461 printk(KERN_DEBUG "set_cr0: #GP, start paging "
462 "in long mode while CS.L == 1\n");
463 inject_gp(vcpu);
464 return;
465
466 }
467 } else
468#endif
Avi Kivity1342d352007-01-05 16:36:39 -0800469 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800470 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
471 "reserved bits\n");
472 inject_gp(vcpu);
473 return;
474 }
475
476 }
477
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300478 kvm_x86_ops->set_cr0(vcpu, cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800479 vcpu->cr0 = cr0;
480
Shaohua Li11ec2802007-07-23 14:51:37 +0800481 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800482 kvm_mmu_reset_context(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800483 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800484 return;
485}
486EXPORT_SYMBOL_GPL(set_cr0);
487
488void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
489{
490 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
491}
492EXPORT_SYMBOL_GPL(lmsw);
493
494void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
495{
Rusty Russell66aee912007-07-17 23:34:16 +1000496 if (cr4 & CR4_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800497 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
498 inject_gp(vcpu);
499 return;
500 }
501
Avi Kivitya9058ec2006-12-29 16:49:37 -0800502 if (is_long_mode(vcpu)) {
Rusty Russell66aee912007-07-17 23:34:16 +1000503 if (!(cr4 & X86_CR4_PAE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800504 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
505 "in long mode\n");
506 inject_gp(vcpu);
507 return;
508 }
Rusty Russell66aee912007-07-17 23:34:16 +1000509 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
Avi Kivity1342d352007-01-05 16:36:39 -0800510 && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800511 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
512 inject_gp(vcpu);
Rusty Russell310bc762007-07-23 17:11:02 +1000513 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800514 }
515
Rusty Russell66aee912007-07-17 23:34:16 +1000516 if (cr4 & X86_CR4_VMXE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800517 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
518 inject_gp(vcpu);
519 return;
520 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300521 kvm_x86_ops->set_cr4(vcpu, cr4);
Rusty Russell81f50e32007-09-06 01:20:38 +1000522 vcpu->cr4 = cr4;
Shaohua Li11ec2802007-07-23 14:51:37 +0800523 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800524 kvm_mmu_reset_context(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800525 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800526}
527EXPORT_SYMBOL_GPL(set_cr4);
528
529void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
530{
Avi Kivitya9058ec2006-12-29 16:49:37 -0800531 if (is_long_mode(vcpu)) {
Rusty Russellf802a302007-07-17 23:32:55 +1000532 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800533 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
534 inject_gp(vcpu);
535 return;
536 }
537 } else {
Rusty Russellf802a302007-07-17 23:32:55 +1000538 if (is_pae(vcpu)) {
539 if (cr3 & CR3_PAE_RESERVED_BITS) {
540 printk(KERN_DEBUG
541 "set_cr3: #GP, reserved bits\n");
542 inject_gp(vcpu);
543 return;
544 }
545 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
546 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
547 "reserved bits\n");
548 inject_gp(vcpu);
549 return;
550 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800551 }
Ryan Harper21764862007-09-18 14:05:16 -0500552 /*
553 * We don't check reserved bits in nonpae mode, because
554 * this isn't enforced, and VMware depends on this.
555 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800556 }
557
Shaohua Li11ec2802007-07-23 14:51:37 +0800558 mutex_lock(&vcpu->kvm->lock);
Ingo Molnard21225e2007-01-05 16:36:59 -0800559 /*
560 * Does the new cr3 value map to physical memory? (Note, we
561 * catch an invalid cr3 even in real-mode, because it would
562 * cause trouble later on when we turn on paging anyway.)
563 *
564 * A real CPU would silently accept an invalid cr3 and would
565 * attempt to use it - with largely undefined (and often hard
566 * to debug) behavior on the guest side.
567 */
568 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
569 inject_gp(vcpu);
Rusty Russellfb764412007-07-31 20:45:03 +1000570 else {
571 vcpu->cr3 = cr3;
Ingo Molnard21225e2007-01-05 16:36:59 -0800572 vcpu->mmu.new_cr3(vcpu);
Rusty Russellfb764412007-07-31 20:45:03 +1000573 }
Shaohua Li11ec2802007-07-23 14:51:37 +0800574 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800575}
576EXPORT_SYMBOL_GPL(set_cr3);
577
578void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
579{
Rusty Russell7075bc82007-07-17 23:37:17 +1000580 if (cr8 & CR8_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800581 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
582 inject_gp(vcpu);
583 return;
584 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300585 if (irqchip_in_kernel(vcpu->kvm))
586 kvm_lapic_set_tpr(vcpu, cr8);
587 else
588 vcpu->cr8 = cr8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800589}
590EXPORT_SYMBOL_GPL(set_cr8);
591
Eddie Dong7017fc32007-07-18 11:34:57 +0300592unsigned long get_cr8(struct kvm_vcpu *vcpu)
593{
Eddie Dong97222cc2007-09-12 10:58:04 +0300594 if (irqchip_in_kernel(vcpu->kvm))
595 return kvm_lapic_get_cr8(vcpu);
596 else
597 return vcpu->cr8;
Eddie Dong7017fc32007-07-18 11:34:57 +0300598}
599EXPORT_SYMBOL_GPL(get_cr8);
600
601u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
602{
Eddie Dong97222cc2007-09-12 10:58:04 +0300603 if (irqchip_in_kernel(vcpu->kvm))
604 return vcpu->apic_base;
605 else
606 return vcpu->apic_base;
Eddie Dong7017fc32007-07-18 11:34:57 +0300607}
608EXPORT_SYMBOL_GPL(kvm_get_apic_base);
609
610void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
611{
Eddie Dong97222cc2007-09-12 10:58:04 +0300612 /* TODO: reserve bits check */
613 if (irqchip_in_kernel(vcpu->kvm))
614 kvm_lapic_set_base(vcpu, data);
615 else
616 vcpu->apic_base = data;
Eddie Dong7017fc32007-07-18 11:34:57 +0300617}
618EXPORT_SYMBOL_GPL(kvm_set_apic_base);
619
Avi Kivity6aa8b732006-12-10 02:21:36 -0800620void fx_init(struct kvm_vcpu *vcpu)
621{
Rusty Russellb114b082007-07-30 21:13:43 +1000622 unsigned after_mxcsr_mask;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800623
Rusty Russell9bd01502007-07-30 16:29:56 +1000624 /* Initialize guest FPU by resetting ours and saving into guest's */
625 preempt_disable();
Rusty Russellb114b082007-07-30 21:13:43 +1000626 fx_save(&vcpu->host_fx_image);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800627 fpu_init();
Rusty Russellb114b082007-07-30 21:13:43 +1000628 fx_save(&vcpu->guest_fx_image);
629 fx_restore(&vcpu->host_fx_image);
Rusty Russell9bd01502007-07-30 16:29:56 +1000630 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800631
Amit Shah380102c2007-08-25 11:35:52 +0300632 vcpu->cr0 |= X86_CR0_ET;
Rusty Russellb114b082007-07-30 21:13:43 +1000633 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
634 vcpu->guest_fx_image.mxcsr = 0x1f80;
635 memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask,
636 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800637}
638EXPORT_SYMBOL_GPL(fx_init);
639
640/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800641 * Allocate some memory and give it an address in the guest physical address
642 * space.
643 *
644 * Discontiguous memory is allowed, mostly for framebuffers.
645 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200646static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
Izik Eidus6fc138d2007-10-09 19:20:39 +0200647 struct
648 kvm_userspace_memory_region *mem,
649 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800650{
651 int r;
652 gfn_t base_gfn;
653 unsigned long npages;
654 unsigned long i;
655 struct kvm_memory_slot *memslot;
656 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800657
658 r = -EINVAL;
659 /* General sanity checks */
660 if (mem->memory_size & (PAGE_SIZE - 1))
661 goto out;
662 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
663 goto out;
664 if (mem->slot >= KVM_MEMORY_SLOTS)
665 goto out;
666 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
667 goto out;
668
669 memslot = &kvm->memslots[mem->slot];
670 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
671 npages = mem->memory_size >> PAGE_SHIFT;
672
673 if (!npages)
674 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
675
Shaohua Li11ec2802007-07-23 14:51:37 +0800676 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800677
Avi Kivity6aa8b732006-12-10 02:21:36 -0800678 new = old = *memslot;
679
680 new.base_gfn = base_gfn;
681 new.npages = npages;
682 new.flags = mem->flags;
683
684 /* Disallow changing a memory slot's size. */
685 r = -EINVAL;
686 if (npages && old.npages && npages != old.npages)
687 goto out_unlock;
688
689 /* Check for overlaps */
690 r = -EEXIST;
691 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
692 struct kvm_memory_slot *s = &kvm->memslots[i];
693
694 if (s == memslot)
695 continue;
696 if (!((base_gfn + npages <= s->base_gfn) ||
697 (base_gfn >= s->base_gfn + s->npages)))
698 goto out_unlock;
699 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800700
Avi Kivity6aa8b732006-12-10 02:21:36 -0800701 /* Free page dirty bitmap if unneeded */
702 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000703 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800704
705 r = -ENOMEM;
706
707 /* Allocate if a slot is being created */
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500708 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400709 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200710
711 if (!new.rmap)
712 goto out_unlock;
713
Izik Eidus290fc382007-09-27 14:11:22 +0200714 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500715
Izik Eidus80b14b52007-10-25 11:54:04 +0200716 new.user_alloc = user_alloc;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500717 if (user_alloc)
Izik Eidus8a7ae052007-10-18 11:09:33 +0200718 new.userspace_addr = mem->userspace_addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500719 else {
720 down_write(&current->mm->mmap_sem);
721 new.userspace_addr = do_mmap(NULL, 0,
722 npages * PAGE_SIZE,
723 PROT_READ | PROT_WRITE,
724 MAP_SHARED | MAP_ANONYMOUS,
725 0);
726 up_write(&current->mm->mmap_sem);
727
728 if (IS_ERR((void *)new.userspace_addr))
729 goto out_unlock;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800730 }
Izik Eidus80b14b52007-10-25 11:54:04 +0200731 } else {
732 if (!old.user_alloc && old.rmap) {
733 int ret;
734
735 down_write(&current->mm->mmap_sem);
736 ret = do_munmap(current->mm, old.userspace_addr,
737 old.npages * PAGE_SIZE);
738 up_write(&current->mm->mmap_sem);
739 if (ret < 0)
740 printk(KERN_WARNING
741 "kvm_vm_ioctl_set_memory_region: "
742 "failed to munmap memory\n");
743 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800744 }
745
746 /* Allocate page dirty bitmap if needed */
747 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
748 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
749
750 new.dirty_bitmap = vmalloc(dirty_bytes);
751 if (!new.dirty_bitmap)
Laurent Vivier0d8d2bd2007-08-30 14:56:21 +0200752 goto out_unlock;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800753 memset(new.dirty_bitmap, 0, dirty_bytes);
754 }
755
Avi Kivity6aa8b732006-12-10 02:21:36 -0800756 if (mem->slot >= kvm->nmemslots)
757 kvm->nmemslots = mem->slot + 1;
758
Izik Eidus82ce2c92007-10-02 18:52:55 +0200759 if (!kvm->n_requested_mmu_pages) {
760 unsigned int n_pages;
761
762 if (npages) {
763 n_pages = npages * KVM_PERMILLE_MMU_PAGES / 1000;
764 kvm_mmu_change_mmu_pages(kvm, kvm->n_alloc_mmu_pages +
765 n_pages);
766 } else {
767 unsigned int nr_mmu_pages;
768
769 n_pages = old.npages * KVM_PERMILLE_MMU_PAGES / 1000;
770 nr_mmu_pages = kvm->n_alloc_mmu_pages - n_pages;
771 nr_mmu_pages = max(nr_mmu_pages,
772 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
773 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
774 }
775 }
776
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777 *memslot = new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800778
Avi Kivity90cb0522007-07-17 13:04:56 +0300779 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
780 kvm_flush_remote_tlbs(kvm);
781
Shaohua Li11ec2802007-07-23 14:51:37 +0800782 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800783
Avi Kivity6aa8b732006-12-10 02:21:36 -0800784 kvm_free_physmem_slot(&old, &new);
785 return 0;
786
787out_unlock:
Shaohua Li11ec2802007-07-23 14:51:37 +0800788 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789 kvm_free_physmem_slot(&new, &old);
790out:
791 return r;
792}
793
Izik Eidus82ce2c92007-10-02 18:52:55 +0200794static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
795 u32 kvm_nr_mmu_pages)
796{
797 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
798 return -EINVAL;
799
800 mutex_lock(&kvm->lock);
801
802 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
803 kvm->n_requested_mmu_pages = kvm_nr_mmu_pages;
804
805 mutex_unlock(&kvm->lock);
806 return 0;
807}
808
809static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
810{
811 return kvm->n_alloc_mmu_pages;
812}
813
Avi Kivity6aa8b732006-12-10 02:21:36 -0800814/*
815 * Get (and clear) the dirty memory log for a memory slot.
816 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200817static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
818 struct kvm_dirty_log *log)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800819{
820 struct kvm_memory_slot *memslot;
821 int r, i;
822 int n;
823 unsigned long any = 0;
824
Shaohua Li11ec2802007-07-23 14:51:37 +0800825 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800826
Avi Kivity6aa8b732006-12-10 02:21:36 -0800827 r = -EINVAL;
828 if (log->slot >= KVM_MEMORY_SLOTS)
829 goto out;
830
831 memslot = &kvm->memslots[log->slot];
832 r = -ENOENT;
833 if (!memslot->dirty_bitmap)
834 goto out;
835
Uri Lublincd1a4a92007-02-22 16:43:09 +0200836 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800837
Uri Lublincd1a4a92007-02-22 16:43:09 +0200838 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839 any = memslot->dirty_bitmap[i];
840
841 r = -EFAULT;
842 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
843 goto out;
844
Rusty Russell39214912007-07-31 19:57:47 +1000845 /* If nothing is dirty, don't bother messing with page tables. */
846 if (any) {
Rusty Russell39214912007-07-31 19:57:47 +1000847 kvm_mmu_slot_remove_write_access(kvm, log->slot);
848 kvm_flush_remote_tlbs(kvm);
849 memset(memslot->dirty_bitmap, 0, n);
Rusty Russell39214912007-07-31 19:57:47 +1000850 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800851
852 r = 0;
853
854out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800855 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800856 return r;
857}
858
Avi Kivitye8207542007-03-30 16:54:30 +0300859/*
860 * Set a new alias region. Aliases map a portion of physical memory into
861 * another portion. This is useful for memory windows, for example the PC
862 * VGA region.
863 */
864static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
865 struct kvm_memory_alias *alias)
866{
867 int r, n;
868 struct kvm_mem_alias *p;
869
870 r = -EINVAL;
871 /* General sanity checks */
872 if (alias->memory_size & (PAGE_SIZE - 1))
873 goto out;
874 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
875 goto out;
876 if (alias->slot >= KVM_ALIAS_SLOTS)
877 goto out;
878 if (alias->guest_phys_addr + alias->memory_size
879 < alias->guest_phys_addr)
880 goto out;
881 if (alias->target_phys_addr + alias->memory_size
882 < alias->target_phys_addr)
883 goto out;
884
Shaohua Li11ec2802007-07-23 14:51:37 +0800885 mutex_lock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300886
887 p = &kvm->aliases[alias->slot];
888 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
889 p->npages = alias->memory_size >> PAGE_SHIFT;
890 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
891
892 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
893 if (kvm->aliases[n - 1].npages)
894 break;
895 kvm->naliases = n;
896
Avi Kivity90cb0522007-07-17 13:04:56 +0300897 kvm_mmu_zap_all(kvm);
Avi Kivitye8207542007-03-30 16:54:30 +0300898
Shaohua Li11ec2802007-07-23 14:51:37 +0800899 mutex_unlock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300900
901 return 0;
902
903out:
904 return r;
905}
906
He, Qing6ceb9d72007-07-26 11:05:18 +0300907static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
908{
909 int r;
910
911 r = 0;
912 switch (chip->chip_id) {
913 case KVM_IRQCHIP_PIC_MASTER:
Mike Dayd77c26f2007-10-08 09:02:08 -0400914 memcpy(&chip->chip.pic,
He, Qing6ceb9d72007-07-26 11:05:18 +0300915 &pic_irqchip(kvm)->pics[0],
916 sizeof(struct kvm_pic_state));
917 break;
918 case KVM_IRQCHIP_PIC_SLAVE:
Mike Dayd77c26f2007-10-08 09:02:08 -0400919 memcpy(&chip->chip.pic,
He, Qing6ceb9d72007-07-26 11:05:18 +0300920 &pic_irqchip(kvm)->pics[1],
921 sizeof(struct kvm_pic_state));
922 break;
He, Qing6bf9e962007-08-05 10:49:16 +0300923 case KVM_IRQCHIP_IOAPIC:
Mike Dayd77c26f2007-10-08 09:02:08 -0400924 memcpy(&chip->chip.ioapic,
He, Qing6bf9e962007-08-05 10:49:16 +0300925 ioapic_irqchip(kvm),
926 sizeof(struct kvm_ioapic_state));
927 break;
He, Qing6ceb9d72007-07-26 11:05:18 +0300928 default:
929 r = -EINVAL;
930 break;
931 }
932 return r;
933}
934
935static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
936{
937 int r;
938
939 r = 0;
940 switch (chip->chip_id) {
941 case KVM_IRQCHIP_PIC_MASTER:
Mike Dayd77c26f2007-10-08 09:02:08 -0400942 memcpy(&pic_irqchip(kvm)->pics[0],
He, Qing6ceb9d72007-07-26 11:05:18 +0300943 &chip->chip.pic,
944 sizeof(struct kvm_pic_state));
945 break;
946 case KVM_IRQCHIP_PIC_SLAVE:
Mike Dayd77c26f2007-10-08 09:02:08 -0400947 memcpy(&pic_irqchip(kvm)->pics[1],
He, Qing6ceb9d72007-07-26 11:05:18 +0300948 &chip->chip.pic,
949 sizeof(struct kvm_pic_state));
950 break;
He, Qing6bf9e962007-08-05 10:49:16 +0300951 case KVM_IRQCHIP_IOAPIC:
Mike Dayd77c26f2007-10-08 09:02:08 -0400952 memcpy(ioapic_irqchip(kvm),
He, Qing6bf9e962007-08-05 10:49:16 +0300953 &chip->chip.ioapic,
954 sizeof(struct kvm_ioapic_state));
955 break;
He, Qing6ceb9d72007-07-26 11:05:18 +0300956 default:
957 r = -EINVAL;
958 break;
959 }
960 kvm_pic_update_irq(pic_irqchip(kvm));
961 return r;
962}
963
Izik Eiduscea7bb22007-10-17 19:17:48 +0200964int is_error_page(struct page *page)
965{
966 return page == bad_page;
967}
968EXPORT_SYMBOL_GPL(is_error_page);
969
Izik Eidus290fc382007-09-27 14:11:22 +0200970gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
Avi Kivitye8207542007-03-30 16:54:30 +0300971{
972 int i;
973 struct kvm_mem_alias *alias;
974
975 for (i = 0; i < kvm->naliases; ++i) {
976 alias = &kvm->aliases[i];
977 if (gfn >= alias->base_gfn
978 && gfn < alias->base_gfn + alias->npages)
979 return alias->target_gfn + gfn - alias->base_gfn;
980 }
981 return gfn;
982}
983
984static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985{
986 int i;
987
988 for (i = 0; i < kvm->nmemslots; ++i) {
989 struct kvm_memory_slot *memslot = &kvm->memslots[i];
990
991 if (gfn >= memslot->base_gfn
992 && gfn < memslot->base_gfn + memslot->npages)
993 return memslot;
994 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000995 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800996}
Avi Kivitye8207542007-03-30 16:54:30 +0300997
998struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
999{
1000 gfn = unalias_gfn(kvm, gfn);
1001 return __gfn_to_memslot(kvm, gfn);
1002}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003
Avi Kivity954bbbc22007-03-30 14:02:32 +03001004struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1005{
1006 struct kvm_memory_slot *slot;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001007 struct page *page[1];
1008 int npages;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001009
Avi Kivity60395222007-10-21 11:03:36 +02001010 might_sleep();
1011
Avi Kivitye8207542007-03-30 16:54:30 +03001012 gfn = unalias_gfn(kvm, gfn);
1013 slot = __gfn_to_memslot(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001014 if (!slot) {
1015 get_page(bad_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +02001016 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001017 }
Izik Eidus8a7ae052007-10-18 11:09:33 +02001018
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001019 down_read(&current->mm->mmap_sem);
1020 npages = get_user_pages(current, current->mm,
1021 slot->userspace_addr
1022 + (gfn - slot->base_gfn) * PAGE_SIZE, 1,
1023 1, 1, page, NULL);
1024 up_read(&current->mm->mmap_sem);
1025 if (npages != 1) {
1026 get_page(bad_page);
1027 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001028 }
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001029
1030 return page[0];
Avi Kivity954bbbc22007-03-30 14:02:32 +03001031}
1032EXPORT_SYMBOL_GPL(gfn_to_page);
1033
Izik Eidus8a7ae052007-10-18 11:09:33 +02001034void kvm_release_page(struct page *page)
1035{
1036 if (!PageReserved(page))
1037 SetPageDirty(page);
1038 put_page(page);
1039}
1040EXPORT_SYMBOL_GPL(kvm_release_page);
1041
Izik Eidus195aefd2007-10-01 22:14:18 +02001042static int next_segment(unsigned long len, int offset)
1043{
1044 if (len > PAGE_SIZE - offset)
1045 return PAGE_SIZE - offset;
1046 else
1047 return len;
1048}
1049
1050int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1051 int len)
1052{
1053 void *page_virt;
1054 struct page *page;
1055
1056 page = gfn_to_page(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001057 if (is_error_page(page)) {
1058 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001059 return -EFAULT;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001060 }
Izik Eidus195aefd2007-10-01 22:14:18 +02001061 page_virt = kmap_atomic(page, KM_USER0);
1062
1063 memcpy(data, page_virt + offset, len);
1064
1065 kunmap_atomic(page_virt, KM_USER0);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001066 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001067 return 0;
1068}
1069EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1070
1071int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1072{
1073 gfn_t gfn = gpa >> PAGE_SHIFT;
1074 int seg;
1075 int offset = offset_in_page(gpa);
1076 int ret;
1077
1078 while ((seg = next_segment(len, offset)) != 0) {
1079 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1080 if (ret < 0)
1081 return ret;
1082 offset = 0;
1083 len -= seg;
1084 data += seg;
1085 ++gfn;
1086 }
1087 return 0;
1088}
1089EXPORT_SYMBOL_GPL(kvm_read_guest);
1090
1091int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1092 int offset, int len)
1093{
1094 void *page_virt;
1095 struct page *page;
1096
1097 page = gfn_to_page(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001098 if (is_error_page(page)) {
1099 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001100 return -EFAULT;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001101 }
Izik Eidus195aefd2007-10-01 22:14:18 +02001102 page_virt = kmap_atomic(page, KM_USER0);
1103
1104 memcpy(page_virt + offset, data, len);
1105
1106 kunmap_atomic(page_virt, KM_USER0);
1107 mark_page_dirty(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001108 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001109 return 0;
1110}
1111EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1112
1113int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1114 unsigned long len)
1115{
1116 gfn_t gfn = gpa >> PAGE_SHIFT;
1117 int seg;
1118 int offset = offset_in_page(gpa);
1119 int ret;
1120
1121 while ((seg = next_segment(len, offset)) != 0) {
1122 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1123 if (ret < 0)
1124 return ret;
1125 offset = 0;
1126 len -= seg;
1127 data += seg;
1128 ++gfn;
1129 }
1130 return 0;
1131}
1132
1133int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1134{
1135 void *page_virt;
1136 struct page *page;
1137
1138 page = gfn_to_page(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001139 if (is_error_page(page)) {
1140 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001141 return -EFAULT;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001142 }
Izik Eidus195aefd2007-10-01 22:14:18 +02001143 page_virt = kmap_atomic(page, KM_USER0);
1144
1145 memset(page_virt + offset, 0, len);
1146
1147 kunmap_atomic(page_virt, KM_USER0);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001148 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001149 return 0;
1150}
1151EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1152
1153int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1154{
1155 gfn_t gfn = gpa >> PAGE_SHIFT;
1156 int seg;
1157 int offset = offset_in_page(gpa);
1158 int ret;
1159
1160 while ((seg = next_segment(len, offset)) != 0) {
1161 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1162 if (ret < 0)
1163 return ret;
1164 offset = 0;
1165 len -= seg;
1166 ++gfn;
1167 }
1168 return 0;
1169}
1170EXPORT_SYMBOL_GPL(kvm_clear_guest);
1171
Rusty Russell7e9d6192007-07-31 20:41:14 +10001172/* WARNING: Does not work on aliased pages. */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001173void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1174{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001175 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001176
Rusty Russell7e9d6192007-07-31 20:41:14 +10001177 memslot = __gfn_to_memslot(kvm, gfn);
1178 if (memslot && memslot->dirty_bitmap) {
1179 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001180
Rusty Russell7e9d6192007-07-31 20:41:14 +10001181 /* avoid RMW */
1182 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1183 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001184 }
1185}
1186
Laurent Viviere7d5d762007-07-30 13:41:19 +03001187int emulator_read_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001188 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001189 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001190 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001191{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001192 void *data = val;
1193
1194 while (bytes) {
1195 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1196 unsigned offset = addr & (PAGE_SIZE-1);
1197 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
Izik Eidus195aefd2007-10-01 22:14:18 +02001198 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001199
1200 if (gpa == UNMAPPED_GVA)
1201 return X86EMUL_PROPAGATE_FAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001202 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
1203 if (ret < 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001204 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001205
1206 bytes -= tocopy;
1207 data += tocopy;
1208 addr += tocopy;
1209 }
1210
1211 return X86EMUL_CONTINUE;
1212}
Laurent Viviere7d5d762007-07-30 13:41:19 +03001213EXPORT_SYMBOL_GPL(emulator_read_std);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001214
1215static int emulator_write_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001216 const void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001217 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001218 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001219{
Rusty Russellf0242472007-08-01 10:48:02 +10001220 pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001221 return X86EMUL_UNHANDLEABLE;
1222}
1223
Eddie Dong97222cc2007-09-12 10:58:04 +03001224/*
1225 * Only apic need an MMIO device hook, so shortcut now..
1226 */
1227static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
1228 gpa_t addr)
1229{
1230 struct kvm_io_device *dev;
1231
1232 if (vcpu->apic) {
1233 dev = &vcpu->apic->dev;
1234 if (dev->in_range(dev, addr))
1235 return dev;
1236 }
1237 return NULL;
1238}
1239
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001240static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1241 gpa_t addr)
1242{
Eddie Dong97222cc2007-09-12 10:58:04 +03001243 struct kvm_io_device *dev;
1244
1245 dev = vcpu_find_pervcpu_dev(vcpu, addr);
1246 if (dev == NULL)
1247 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1248 return dev;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001249}
1250
Eddie Dong74906342007-06-19 18:05:03 +03001251static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
1252 gpa_t addr)
1253{
1254 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
1255}
1256
Avi Kivity6aa8b732006-12-10 02:21:36 -08001257static int emulator_read_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001258 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001259 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001260 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001261{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001262 struct kvm_io_device *mmio_dev;
1263 gpa_t gpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001264
1265 if (vcpu->mmio_read_completed) {
1266 memcpy(val, vcpu->mmio_data, bytes);
1267 vcpu->mmio_read_completed = 0;
1268 return X86EMUL_CONTINUE;
Laurent Viviercebff022007-07-30 13:35:24 +03001269 } else if (emulator_read_std(addr, val, bytes, vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001270 == X86EMUL_CONTINUE)
1271 return X86EMUL_CONTINUE;
Avi Kivityd27d4ac2007-02-19 14:37:46 +02001272
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001273 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1274 if (gpa == UNMAPPED_GVA)
1275 return X86EMUL_PROPAGATE_FAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001276
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001277 /*
1278 * Is this MMIO handled locally?
1279 */
1280 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1281 if (mmio_dev) {
1282 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1283 return X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001284 }
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001285
1286 vcpu->mmio_needed = 1;
1287 vcpu->mmio_phys_addr = gpa;
1288 vcpu->mmio_size = bytes;
1289 vcpu->mmio_is_write = 0;
1290
1291 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001292}
1293
Avi Kivityda4a00f2007-01-05 16:36:44 -08001294static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity4c690a12007-04-22 15:28:19 +03001295 const void *val, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001296{
Izik Eidus195aefd2007-10-01 22:14:18 +02001297 int ret;
Avi Kivityda4a00f2007-01-05 16:36:44 -08001298
Izik Eidus195aefd2007-10-01 22:14:18 +02001299 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
1300 if (ret < 0)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001301 return 0;
Shaohua Life551882007-07-23 14:51:39 +08001302 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001303 return 1;
1304}
1305
Avi Kivityb0fcd902007-07-22 18:48:54 +03001306static int emulator_write_emulated_onepage(unsigned long addr,
1307 const void *val,
1308 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001309 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001310{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001311 struct kvm_io_device *mmio_dev;
1312 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001313
Avi Kivityc9047f52007-04-17 10:53:22 +03001314 if (gpa == UNMAPPED_GVA) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001315 kvm_x86_ops->inject_page_fault(vcpu, addr, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001316 return X86EMUL_PROPAGATE_FAULT;
Avi Kivityc9047f52007-04-17 10:53:22 +03001317 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001318
Avi Kivityda4a00f2007-01-05 16:36:44 -08001319 if (emulator_write_phys(vcpu, gpa, val, bytes))
1320 return X86EMUL_CONTINUE;
1321
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001322 /*
1323 * Is this MMIO handled locally?
1324 */
1325 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1326 if (mmio_dev) {
1327 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1328 return X86EMUL_CONTINUE;
1329 }
1330
Avi Kivity6aa8b732006-12-10 02:21:36 -08001331 vcpu->mmio_needed = 1;
1332 vcpu->mmio_phys_addr = gpa;
1333 vcpu->mmio_size = bytes;
1334 vcpu->mmio_is_write = 1;
Avi Kivity4c690a12007-04-22 15:28:19 +03001335 memcpy(vcpu->mmio_data, val, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336
1337 return X86EMUL_CONTINUE;
1338}
1339
Laurent Viviere7d5d762007-07-30 13:41:19 +03001340int emulator_write_emulated(unsigned long addr,
Avi Kivityb0fcd902007-07-22 18:48:54 +03001341 const void *val,
1342 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001343 struct kvm_vcpu *vcpu)
Avi Kivityb0fcd902007-07-22 18:48:54 +03001344{
1345 /* Crossing a page boundary? */
1346 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1347 int rc, now;
1348
1349 now = -addr & ~PAGE_MASK;
Laurent Viviercebff022007-07-30 13:35:24 +03001350 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001351 if (rc != X86EMUL_CONTINUE)
1352 return rc;
1353 addr += now;
1354 val += now;
1355 bytes -= now;
1356 }
Laurent Viviercebff022007-07-30 13:35:24 +03001357 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001358}
Laurent Viviere7d5d762007-07-30 13:41:19 +03001359EXPORT_SYMBOL_GPL(emulator_write_emulated);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001360
Avi Kivity6aa8b732006-12-10 02:21:36 -08001361static int emulator_cmpxchg_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001362 const void *old,
1363 const void *new,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001364 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001365 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366{
1367 static int reported;
1368
1369 if (!reported) {
1370 reported = 1;
1371 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1372 }
Laurent Viviercebff022007-07-30 13:35:24 +03001373 return emulator_write_emulated(addr, new, bytes, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001374}
1375
1376static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1377{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001378 return kvm_x86_ops->get_segment_base(vcpu, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001379}
1380
1381int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1382{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001383 return X86EMUL_CONTINUE;
1384}
1385
1386int emulate_clts(struct kvm_vcpu *vcpu)
1387{
Amit Shah404fb882007-11-19 17:57:35 +02001388 kvm_x86_ops->set_cr0(vcpu, vcpu->cr0 & ~X86_CR0_TS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001389 return X86EMUL_CONTINUE;
1390}
1391
Mike Dayd77c26f2007-10-08 09:02:08 -04001392int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001393{
1394 struct kvm_vcpu *vcpu = ctxt->vcpu;
1395
1396 switch (dr) {
1397 case 0 ... 3:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001398 *dest = kvm_x86_ops->get_dr(vcpu, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001399 return X86EMUL_CONTINUE;
1400 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001401 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001402 return X86EMUL_UNHANDLEABLE;
1403 }
1404}
1405
1406int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1407{
1408 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1409 int exception;
1410
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001411 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001412 if (exception) {
1413 /* FIXME: better handling */
1414 return X86EMUL_UNHANDLEABLE;
1415 }
1416 return X86EMUL_CONTINUE;
1417}
1418
Avi Kivity054b1362007-09-12 13:21:09 +03001419void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001420{
1421 static int reported;
1422 u8 opcodes[4];
Avi Kivity054b1362007-09-12 13:21:09 +03001423 unsigned long rip = vcpu->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001424 unsigned long rip_linear;
1425
Avi Kivity054b1362007-09-12 13:21:09 +03001426 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001427
1428 if (reported)
1429 return;
1430
Avi Kivity054b1362007-09-12 13:21:09 +03001431 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001432
Avi Kivity054b1362007-09-12 13:21:09 +03001433 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1434 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001435 reported = 1;
1436}
Avi Kivity054b1362007-09-12 13:21:09 +03001437EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001438
1439struct x86_emulate_ops emulate_ops = {
1440 .read_std = emulator_read_std,
1441 .write_std = emulator_write_std,
1442 .read_emulated = emulator_read_emulated,
1443 .write_emulated = emulator_write_emulated,
1444 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1445};
1446
1447int emulate_instruction(struct kvm_vcpu *vcpu,
1448 struct kvm_run *run,
1449 unsigned long cr2,
Laurent Vivier34273182007-09-18 11:27:37 +02001450 u16 error_code,
1451 int no_decode)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001452{
Laurent Viviera22436b2007-09-24 17:00:58 +02001453 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001454
Avi Kivitye7df56e2007-03-14 15:54:54 +02001455 vcpu->mmio_fault_cr2 = cr2;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001456 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001457
Avi Kivity6aa8b732006-12-10 02:21:36 -08001458 vcpu->mmio_is_write = 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03001459 vcpu->pio.string = 0;
Laurent Vivier34273182007-09-18 11:27:37 +02001460
1461 if (!no_decode) {
1462 int cs_db, cs_l;
1463 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1464
1465 vcpu->emulate_ctxt.vcpu = vcpu;
1466 vcpu->emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
1467 vcpu->emulate_ctxt.cr2 = cr2;
1468 vcpu->emulate_ctxt.mode =
1469 (vcpu->emulate_ctxt.eflags & X86_EFLAGS_VM)
1470 ? X86EMUL_MODE_REAL : cs_l
1471 ? X86EMUL_MODE_PROT64 : cs_db
1472 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1473
1474 if (vcpu->emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1475 vcpu->emulate_ctxt.cs_base = 0;
1476 vcpu->emulate_ctxt.ds_base = 0;
1477 vcpu->emulate_ctxt.es_base = 0;
1478 vcpu->emulate_ctxt.ss_base = 0;
1479 } else {
1480 vcpu->emulate_ctxt.cs_base =
1481 get_segment_base(vcpu, VCPU_SREG_CS);
1482 vcpu->emulate_ctxt.ds_base =
1483 get_segment_base(vcpu, VCPU_SREG_DS);
1484 vcpu->emulate_ctxt.es_base =
1485 get_segment_base(vcpu, VCPU_SREG_ES);
1486 vcpu->emulate_ctxt.ss_base =
1487 get_segment_base(vcpu, VCPU_SREG_SS);
1488 }
1489
1490 vcpu->emulate_ctxt.gs_base =
1491 get_segment_base(vcpu, VCPU_SREG_GS);
1492 vcpu->emulate_ctxt.fs_base =
1493 get_segment_base(vcpu, VCPU_SREG_FS);
1494
1495 r = x86_decode_insn(&vcpu->emulate_ctxt, &emulate_ops);
Laurent Viviera22436b2007-09-24 17:00:58 +02001496 if (r) {
1497 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1498 return EMULATE_DONE;
1499 return EMULATE_FAIL;
1500 }
Laurent Vivier34273182007-09-18 11:27:37 +02001501 }
1502
Laurent Viviera22436b2007-09-24 17:00:58 +02001503 r = x86_emulate_insn(&vcpu->emulate_ctxt, &emulate_ops);
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001504
Laurent Viviere70669a2007-08-05 10:36:40 +03001505 if (vcpu->pio.string)
1506 return EMULATE_DO_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507
1508 if ((r || vcpu->mmio_is_write) && run) {
Jeff Dike8fc0d082007-07-17 12:26:59 -04001509 run->exit_reason = KVM_EXIT_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001510 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1511 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1512 run->mmio.len = vcpu->mmio_size;
1513 run->mmio.is_write = vcpu->mmio_is_write;
1514 }
1515
1516 if (r) {
Avi Kivitya4360362007-01-05 16:36:45 -08001517 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1518 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001519 if (!vcpu->mmio_needed) {
Avi Kivity054b1362007-09-12 13:21:09 +03001520 kvm_report_emulation_failure(vcpu, "mmio");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001521 return EMULATE_FAIL;
1522 }
1523 return EMULATE_DO_MMIO;
1524 }
1525
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001526 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier34273182007-09-18 11:27:37 +02001527 kvm_x86_ops->set_rflags(vcpu, vcpu->emulate_ctxt.eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001528
Avi Kivity02c83202007-04-29 15:02:17 +03001529 if (vcpu->mmio_is_write) {
1530 vcpu->mmio_needed = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001531 return EMULATE_DO_MMIO;
Avi Kivity02c83202007-04-29 15:02:17 +03001532 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001533
1534 return EMULATE_DONE;
1535}
1536EXPORT_SYMBOL_GPL(emulate_instruction);
1537
Eddie Dongb6958ce2007-07-18 12:15:21 +03001538/*
1539 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1540 */
He, Qingc5ec1532007-09-03 17:07:41 +03001541static void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001542{
1543 DECLARE_WAITQUEUE(wait, current);
1544
1545 add_wait_queue(&vcpu->wq, &wait);
1546
1547 /*
1548 * We will block until either an interrupt or a signal wakes us up
1549 */
He, Qingc5ec1532007-09-03 17:07:41 +03001550 while (!kvm_cpu_has_interrupt(vcpu)
1551 && !signal_pending(current)
1552 && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE
1553 && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) {
Eddie Dongb6958ce2007-07-18 12:15:21 +03001554 set_current_state(TASK_INTERRUPTIBLE);
1555 vcpu_put(vcpu);
1556 schedule();
1557 vcpu_load(vcpu);
1558 }
1559
He, Qingc5ec1532007-09-03 17:07:41 +03001560 __set_current_state(TASK_RUNNING);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001561 remove_wait_queue(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001562}
1563
Avi Kivityd3bef152007-06-05 15:53:05 +03001564int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1565{
Avi Kivityd3bef152007-06-05 15:53:05 +03001566 ++vcpu->stat.halt_exits;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001567 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc5ec1532007-09-03 17:07:41 +03001568 vcpu->mp_state = VCPU_MP_STATE_HALTED;
1569 kvm_vcpu_block(vcpu);
1570 if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE)
1571 return -EINTR;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001572 return 1;
1573 } else {
1574 vcpu->run->exit_reason = KVM_EXIT_HLT;
1575 return 0;
1576 }
Avi Kivityd3bef152007-06-05 15:53:05 +03001577}
1578EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1579
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001580int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
Avi Kivity270fd9b2007-02-19 14:37:47 +02001581{
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001582 unsigned long nr, a0, a1, a2, a3, ret;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001583
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001584 kvm_x86_ops->cache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001585
1586 nr = vcpu->regs[VCPU_REGS_RAX];
1587 a0 = vcpu->regs[VCPU_REGS_RBX];
1588 a1 = vcpu->regs[VCPU_REGS_RCX];
1589 a2 = vcpu->regs[VCPU_REGS_RDX];
1590 a3 = vcpu->regs[VCPU_REGS_RSI];
1591
1592 if (!is_long_mode(vcpu)) {
1593 nr &= 0xFFFFFFFF;
1594 a0 &= 0xFFFFFFFF;
1595 a1 &= 0xFFFFFFFF;
1596 a2 &= 0xFFFFFFFF;
1597 a3 &= 0xFFFFFFFF;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001598 }
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001599
Avi Kivity270fd9b2007-02-19 14:37:47 +02001600 switch (nr) {
1601 default:
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001602 ret = -KVM_ENOSYS;
1603 break;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001604 }
1605 vcpu->regs[VCPU_REGS_RAX] = ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001606 kvm_x86_ops->decache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001607 return 0;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001608}
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001609EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
1610
1611int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
1612{
1613 char instruction[3];
1614 int ret = 0;
1615
1616 mutex_lock(&vcpu->kvm->lock);
1617
1618 /*
1619 * Blow out the MMU to ensure that no other VCPU has an active mapping
1620 * to ensure that the updated hypercall appears atomically across all
1621 * VCPUs.
1622 */
1623 kvm_mmu_zap_all(vcpu->kvm);
1624
1625 kvm_x86_ops->cache_regs(vcpu);
1626 kvm_x86_ops->patch_hypercall(vcpu, instruction);
1627 if (emulator_write_emulated(vcpu->rip, instruction, 3, vcpu)
1628 != X86EMUL_CONTINUE)
1629 ret = -EFAULT;
1630
1631 mutex_unlock(&vcpu->kvm->lock);
1632
1633 return ret;
1634}
Avi Kivity270fd9b2007-02-19 14:37:47 +02001635
Avi Kivity6aa8b732006-12-10 02:21:36 -08001636static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1637{
1638 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1639}
1640
1641void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1642{
1643 struct descriptor_table dt = { limit, base };
1644
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001645 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001646}
1647
1648void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1649{
1650 struct descriptor_table dt = { limit, base };
1651
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001652 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001653}
1654
1655void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1656 unsigned long *rflags)
1657{
1658 lmsw(vcpu, msw);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001659 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001660}
1661
1662unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1663{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001664 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001665 switch (cr) {
1666 case 0:
1667 return vcpu->cr0;
1668 case 2:
1669 return vcpu->cr2;
1670 case 3:
1671 return vcpu->cr3;
1672 case 4:
1673 return vcpu->cr4;
1674 default:
1675 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1676 return 0;
1677 }
1678}
1679
1680void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1681 unsigned long *rflags)
1682{
1683 switch (cr) {
1684 case 0:
1685 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001686 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001687 break;
1688 case 2:
1689 vcpu->cr2 = val;
1690 break;
1691 case 3:
1692 set_cr3(vcpu, val);
1693 break;
1694 case 4:
1695 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1696 break;
1697 default:
1698 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1699 }
1700}
1701
Avi Kivity3bab1f52006-12-29 16:49:48 -08001702int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1703{
1704 u64 data;
1705
1706 switch (msr) {
1707 case 0xc0010010: /* SYSCFG */
1708 case 0xc0010015: /* HWCR */
1709 case MSR_IA32_PLATFORM_ID:
1710 case MSR_IA32_P5_MC_ADDR:
1711 case MSR_IA32_P5_MC_TYPE:
1712 case MSR_IA32_MC0_CTL:
1713 case MSR_IA32_MCG_STATUS:
1714 case MSR_IA32_MCG_CAP:
1715 case MSR_IA32_MC0_MISC:
1716 case MSR_IA32_MC0_MISC+4:
1717 case MSR_IA32_MC0_MISC+8:
1718 case MSR_IA32_MC0_MISC+12:
1719 case MSR_IA32_MC0_MISC+16:
1720 case MSR_IA32_UCODE_REV:
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001721 case MSR_IA32_PERF_STATUS:
Matthew Gregan2dc70942007-05-06 10:59:46 +03001722 case MSR_IA32_EBL_CR_POWERON:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001723 /* MTRR registers */
1724 case 0xfe:
1725 case 0x200 ... 0x2ff:
1726 data = 0;
1727 break;
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001728 case 0xcd: /* fsb frequency */
1729 data = 3;
1730 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001731 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001732 data = kvm_get_apic_base(vcpu);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001733 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001734 case MSR_IA32_MISC_ENABLE:
1735 data = vcpu->ia32_misc_enable_msr;
1736 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001737#ifdef CONFIG_X86_64
1738 case MSR_EFER:
1739 data = vcpu->shadow_efer;
1740 break;
1741#endif
1742 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001743 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001744 return 1;
1745 }
1746 *pdata = data;
1747 return 0;
1748}
1749EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1750
Avi Kivity6aa8b732006-12-10 02:21:36 -08001751/*
1752 * Reads an msr value (of 'msr_index') into 'pdata'.
1753 * Returns 0 on success, non-0 otherwise.
1754 * Assumes vcpu_load() was already called.
1755 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001756int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001757{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001758 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001759}
1760
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001761#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001762
Avi Kivity3bab1f52006-12-29 16:49:48 -08001763static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001764{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001765 if (efer & EFER_RESERVED_BITS) {
1766 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1767 efer);
1768 inject_gp(vcpu);
1769 return;
1770 }
1771
1772 if (is_paging(vcpu)
1773 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1774 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1775 inject_gp(vcpu);
1776 return;
1777 }
1778
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001779 kvm_x86_ops->set_efer(vcpu, efer);
Avi Kivity7725f0b2006-12-13 00:34:01 -08001780
Avi Kivity6aa8b732006-12-10 02:21:36 -08001781 efer &= ~EFER_LMA;
1782 efer |= vcpu->shadow_efer & EFER_LMA;
1783
1784 vcpu->shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001785}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001786
1787#endif
1788
Avi Kivity3bab1f52006-12-29 16:49:48 -08001789int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1790{
1791 switch (msr) {
1792#ifdef CONFIG_X86_64
1793 case MSR_EFER:
1794 set_efer(vcpu, data);
1795 break;
1796#endif
1797 case MSR_IA32_MC0_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001798 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
Avi Kivity3bab1f52006-12-29 16:49:48 -08001799 __FUNCTION__, data);
1800 break;
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001801 case MSR_IA32_MCG_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001802 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001803 __FUNCTION__, data);
1804 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001805 case MSR_IA32_UCODE_REV:
1806 case MSR_IA32_UCODE_WRITE:
1807 case 0x200 ... 0x2ff: /* MTRRs */
1808 break;
1809 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001810 kvm_set_apic_base(vcpu, data);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001811 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001812 case MSR_IA32_MISC_ENABLE:
1813 vcpu->ia32_misc_enable_msr = data;
1814 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001815 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001816 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001817 return 1;
1818 }
1819 return 0;
1820}
1821EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1822
Avi Kivity6aa8b732006-12-10 02:21:36 -08001823/*
1824 * Writes msr value into into the appropriate "register".
1825 * Returns 0 on success, non-0 otherwise.
1826 * Assumes vcpu_load() was already called.
1827 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001828int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001829{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001830 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001831}
1832
1833void kvm_resched(struct kvm_vcpu *vcpu)
1834{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001835 if (!need_resched())
1836 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001837 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001838}
1839EXPORT_SYMBOL_GPL(kvm_resched);
1840
Avi Kivity06465c52007-02-28 20:46:53 +02001841void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1842{
1843 int i;
1844 u32 function;
1845 struct kvm_cpuid_entry *e, *best;
1846
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001847 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001848 function = vcpu->regs[VCPU_REGS_RAX];
1849 vcpu->regs[VCPU_REGS_RAX] = 0;
1850 vcpu->regs[VCPU_REGS_RBX] = 0;
1851 vcpu->regs[VCPU_REGS_RCX] = 0;
1852 vcpu->regs[VCPU_REGS_RDX] = 0;
1853 best = NULL;
1854 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1855 e = &vcpu->cpuid_entries[i];
1856 if (e->function == function) {
1857 best = e;
1858 break;
1859 }
1860 /*
1861 * Both basic or both extended?
1862 */
1863 if (((e->function ^ function) & 0x80000000) == 0)
1864 if (!best || e->function > best->function)
1865 best = e;
1866 }
1867 if (best) {
1868 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1869 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1870 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1871 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1872 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001873 kvm_x86_ops->decache_regs(vcpu);
1874 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001875}
1876EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1877
Avi Kivity039576c2007-03-20 12:46:50 +02001878static int pio_copy_data(struct kvm_vcpu *vcpu)
Avi Kivity46fc1472007-02-22 19:39:30 +02001879{
Avi Kivity039576c2007-03-20 12:46:50 +02001880 void *p = vcpu->pio_data;
1881 void *q;
1882 unsigned bytes;
1883 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1884
Avi Kivity039576c2007-03-20 12:46:50 +02001885 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1886 PAGE_KERNEL);
1887 if (!q) {
Avi Kivity039576c2007-03-20 12:46:50 +02001888 free_pio_guest_pages(vcpu);
1889 return -ENOMEM;
1890 }
1891 q += vcpu->pio.guest_page_offset;
1892 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1893 if (vcpu->pio.in)
1894 memcpy(q, p, bytes);
1895 else
1896 memcpy(p, q, bytes);
1897 q -= vcpu->pio.guest_page_offset;
1898 vunmap(q);
Avi Kivity039576c2007-03-20 12:46:50 +02001899 free_pio_guest_pages(vcpu);
1900 return 0;
1901}
1902
1903static int complete_pio(struct kvm_vcpu *vcpu)
1904{
1905 struct kvm_pio_request *io = &vcpu->pio;
Avi Kivity46fc1472007-02-22 19:39:30 +02001906 long delta;
Avi Kivity039576c2007-03-20 12:46:50 +02001907 int r;
Avi Kivity46fc1472007-02-22 19:39:30 +02001908
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001909 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001910
1911 if (!io->string) {
Avi Kivity039576c2007-03-20 12:46:50 +02001912 if (io->in)
1913 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
Avi Kivity46fc1472007-02-22 19:39:30 +02001914 io->size);
1915 } else {
Avi Kivity039576c2007-03-20 12:46:50 +02001916 if (io->in) {
1917 r = pio_copy_data(vcpu);
1918 if (r) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001919 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02001920 return r;
1921 }
1922 }
1923
Avi Kivity46fc1472007-02-22 19:39:30 +02001924 delta = 1;
1925 if (io->rep) {
Avi Kivity039576c2007-03-20 12:46:50 +02001926 delta *= io->cur_count;
Avi Kivity46fc1472007-02-22 19:39:30 +02001927 /*
1928 * The size of the register should really depend on
1929 * current address size.
1930 */
1931 vcpu->regs[VCPU_REGS_RCX] -= delta;
1932 }
Avi Kivity039576c2007-03-20 12:46:50 +02001933 if (io->down)
Avi Kivity46fc1472007-02-22 19:39:30 +02001934 delta = -delta;
1935 delta *= io->size;
Avi Kivity039576c2007-03-20 12:46:50 +02001936 if (io->in)
Avi Kivity46fc1472007-02-22 19:39:30 +02001937 vcpu->regs[VCPU_REGS_RDI] += delta;
1938 else
1939 vcpu->regs[VCPU_REGS_RSI] += delta;
1940 }
1941
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001942 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001943
Avi Kivity039576c2007-03-20 12:46:50 +02001944 io->count -= io->cur_count;
1945 io->cur_count = 0;
1946
Avi Kivity039576c2007-03-20 12:46:50 +02001947 return 0;
Avi Kivity46fc1472007-02-22 19:39:30 +02001948}
1949
Eddie Dong65619eb2007-07-17 11:52:33 +03001950static void kernel_pio(struct kvm_io_device *pio_dev,
1951 struct kvm_vcpu *vcpu,
1952 void *pd)
Eddie Dong74906342007-06-19 18:05:03 +03001953{
1954 /* TODO: String I/O for in kernel device */
1955
Eddie Dong9cf98822007-07-22 10:36:31 +03001956 mutex_lock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001957 if (vcpu->pio.in)
1958 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1959 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001960 pd);
Eddie Dong74906342007-06-19 18:05:03 +03001961 else
1962 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1963 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001964 pd);
Eddie Dong9cf98822007-07-22 10:36:31 +03001965 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001966}
1967
1968static void pio_string_write(struct kvm_io_device *pio_dev,
1969 struct kvm_vcpu *vcpu)
1970{
1971 struct kvm_pio_request *io = &vcpu->pio;
1972 void *pd = vcpu->pio_data;
1973 int i;
1974
Eddie Dong9cf98822007-07-22 10:36:31 +03001975 mutex_lock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001976 for (i = 0; i < io->cur_count; i++) {
1977 kvm_iodevice_write(pio_dev, io->port,
1978 io->size,
1979 pd);
1980 pd += io->size;
1981 }
Eddie Dong9cf98822007-07-22 10:36:31 +03001982 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001983}
1984
Mike Dayd77c26f2007-10-08 09:02:08 -04001985int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
Laurent Vivier3090dd72007-08-05 10:43:32 +03001986 int size, unsigned port)
1987{
1988 struct kvm_io_device *pio_dev;
1989
1990 vcpu->run->exit_reason = KVM_EXIT_IO;
1991 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1992 vcpu->run->io.size = vcpu->pio.size = size;
1993 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1994 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1;
1995 vcpu->run->io.port = vcpu->pio.port = port;
1996 vcpu->pio.in = in;
1997 vcpu->pio.string = 0;
1998 vcpu->pio.down = 0;
1999 vcpu->pio.guest_page_offset = 0;
2000 vcpu->pio.rep = 0;
2001
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002002 kvm_x86_ops->cache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03002003 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002004 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03002005
Avi Kivity0967b7b2007-09-15 17:34:36 +03002006 kvm_x86_ops->skip_emulated_instruction(vcpu);
2007
Laurent Vivier3090dd72007-08-05 10:43:32 +03002008 pio_dev = vcpu_find_pio_dev(vcpu, port);
2009 if (pio_dev) {
2010 kernel_pio(pio_dev, vcpu, vcpu->pio_data);
2011 complete_pio(vcpu);
2012 return 1;
2013 }
2014 return 0;
2015}
2016EXPORT_SYMBOL_GPL(kvm_emulate_pio);
2017
2018int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2019 int size, unsigned long count, int down,
Avi Kivity039576c2007-03-20 12:46:50 +02002020 gva_t address, int rep, unsigned port)
2021{
2022 unsigned now, in_page;
Eddie Dong65619eb2007-07-17 11:52:33 +03002023 int i, ret = 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002024 int nr_pages = 1;
2025 struct page *page;
Eddie Dong74906342007-06-19 18:05:03 +03002026 struct kvm_io_device *pio_dev;
Avi Kivity039576c2007-03-20 12:46:50 +02002027
2028 vcpu->run->exit_reason = KVM_EXIT_IO;
2029 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Laurent Vivier3090dd72007-08-05 10:43:32 +03002030 vcpu->run->io.size = vcpu->pio.size = size;
Avi Kivity039576c2007-03-20 12:46:50 +02002031 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Laurent Vivier3090dd72007-08-05 10:43:32 +03002032 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count;
2033 vcpu->run->io.port = vcpu->pio.port = port;
Avi Kivity039576c2007-03-20 12:46:50 +02002034 vcpu->pio.in = in;
Laurent Vivier3090dd72007-08-05 10:43:32 +03002035 vcpu->pio.string = 1;
Avi Kivity039576c2007-03-20 12:46:50 +02002036 vcpu->pio.down = down;
2037 vcpu->pio.guest_page_offset = offset_in_page(address);
2038 vcpu->pio.rep = rep;
2039
Avi Kivity039576c2007-03-20 12:46:50 +02002040 if (!count) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002041 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02002042 return 1;
2043 }
2044
Avi Kivity039576c2007-03-20 12:46:50 +02002045 if (!down)
2046 in_page = PAGE_SIZE - offset_in_page(address);
2047 else
2048 in_page = offset_in_page(address) + size;
2049 now = min(count, (unsigned long)in_page / size);
2050 if (!now) {
2051 /*
2052 * String I/O straddles page boundary. Pin two guest pages
2053 * so that we satisfy atomicity constraints. Do just one
2054 * transaction to avoid complexity.
2055 */
2056 nr_pages = 2;
2057 now = 1;
2058 }
2059 if (down) {
2060 /*
2061 * String I/O in reverse. Yuck. Kill the guest, fix later.
2062 */
Rusty Russellf0242472007-08-01 10:48:02 +10002063 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivity039576c2007-03-20 12:46:50 +02002064 inject_gp(vcpu);
2065 return 1;
2066 }
2067 vcpu->run->io.count = now;
2068 vcpu->pio.cur_count = now;
2069
Avi Kivity0967b7b2007-09-15 17:34:36 +03002070 if (vcpu->pio.cur_count == vcpu->pio.count)
2071 kvm_x86_ops->skip_emulated_instruction(vcpu);
2072
Avi Kivity039576c2007-03-20 12:46:50 +02002073 for (i = 0; i < nr_pages; ++i) {
Shaohua Li11ec2802007-07-23 14:51:37 +08002074 mutex_lock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02002075 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
Avi Kivity039576c2007-03-20 12:46:50 +02002076 vcpu->pio.guest_pages[i] = page;
Shaohua Li11ec2802007-07-23 14:51:37 +08002077 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02002078 if (!page) {
2079 inject_gp(vcpu);
2080 free_pio_guest_pages(vcpu);
2081 return 1;
2082 }
2083 }
2084
Laurent Vivier3090dd72007-08-05 10:43:32 +03002085 pio_dev = vcpu_find_pio_dev(vcpu, port);
Eddie Dong65619eb2007-07-17 11:52:33 +03002086 if (!vcpu->pio.in) {
2087 /* string PIO write */
2088 ret = pio_copy_data(vcpu);
2089 if (ret >= 0 && pio_dev) {
2090 pio_string_write(pio_dev, vcpu);
2091 complete_pio(vcpu);
2092 if (vcpu->pio.count == 0)
2093 ret = 1;
2094 }
2095 } else if (pio_dev)
Rusty Russellf0242472007-08-01 10:48:02 +10002096 pr_unimpl(vcpu, "no string pio read support yet, "
Eddie Dong65619eb2007-07-17 11:52:33 +03002097 "port %x size %d count %ld\n",
2098 port, size, count);
2099
2100 return ret;
Avi Kivity039576c2007-03-20 12:46:50 +02002101}
Laurent Vivier3090dd72007-08-05 10:43:32 +03002102EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
Avi Kivity039576c2007-03-20 12:46:50 +02002103
Avi Kivity04d2cc72007-09-10 18:10:54 +03002104/*
2105 * Check if userspace requested an interrupt window, and that the
2106 * interrupt window is open.
2107 *
2108 * No need to exit to userspace if we already have an interrupt queued.
2109 */
2110static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2111 struct kvm_run *kvm_run)
2112{
2113 return (!vcpu->irq_summary &&
2114 kvm_run->request_interrupt_window &&
2115 vcpu->interrupt_window_open &&
2116 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
2117}
2118
2119static void post_kvm_run_save(struct kvm_vcpu *vcpu,
2120 struct kvm_run *kvm_run)
2121{
2122 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
2123 kvm_run->cr8 = get_cr8(vcpu);
2124 kvm_run->apic_base = kvm_get_apic_base(vcpu);
2125 if (irqchip_in_kernel(vcpu->kvm))
2126 kvm_run->ready_for_interrupt_injection = 1;
2127 else
2128 kvm_run->ready_for_interrupt_injection =
2129 (vcpu->interrupt_window_open &&
2130 vcpu->irq_summary == 0);
2131}
2132
2133static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2134{
2135 int r;
2136
2137 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002138 pr_debug("vcpu %d received sipi with vector # %x\n",
Avi Kivity04d2cc72007-09-10 18:10:54 +03002139 vcpu->vcpu_id, vcpu->sipi_vector);
2140 kvm_lapic_reset(vcpu);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002141 r = kvm_x86_ops->vcpu_reset(vcpu);
2142 if (r)
2143 return r;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002144 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
2145 }
2146
2147preempted:
2148 if (vcpu->guest_debug.enabled)
2149 kvm_x86_ops->guest_debug_pre(vcpu);
2150
2151again:
2152 r = kvm_mmu_reload(vcpu);
2153 if (unlikely(r))
2154 goto out;
2155
Avi Kivityab6ef342007-10-16 16:23:22 +02002156 kvm_inject_pending_timer_irqs(vcpu);
2157
Avi Kivity04d2cc72007-09-10 18:10:54 +03002158 preempt_disable();
2159
2160 kvm_x86_ops->prepare_guest_switch(vcpu);
2161 kvm_load_guest_fpu(vcpu);
2162
2163 local_irq_disable();
2164
2165 if (signal_pending(current)) {
2166 local_irq_enable();
2167 preempt_enable();
2168 r = -EINTR;
2169 kvm_run->exit_reason = KVM_EXIT_INTR;
2170 ++vcpu->stat.signal_exits;
2171 goto out;
2172 }
2173
2174 if (irqchip_in_kernel(vcpu->kvm))
2175 kvm_x86_ops->inject_pending_irq(vcpu);
2176 else if (!vcpu->mmio_read_completed)
2177 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2178
2179 vcpu->guest_mode = 1;
Laurent Vivierd172fcd2007-10-15 17:00:19 +02002180 kvm_guest_enter();
Avi Kivity04d2cc72007-09-10 18:10:54 +03002181
2182 if (vcpu->requests)
Avi Kivity3176bc32007-10-16 17:22:08 +02002183 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivity04d2cc72007-09-10 18:10:54 +03002184 kvm_x86_ops->tlb_flush(vcpu);
2185
2186 kvm_x86_ops->run(vcpu, kvm_run);
2187
2188 vcpu->guest_mode = 0;
2189 local_irq_enable();
2190
2191 ++vcpu->stat.exits;
2192
Laurent Vivier0552f732007-10-18 15:19:01 +02002193 /*
2194 * We must have an instruction between local_irq_enable() and
2195 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2196 * the interrupt shadow. The stat.exits increment will do nicely.
2197 * But we need to prevent reordering, hence this barrier():
2198 */
2199 barrier();
2200
2201 kvm_guest_exit();
2202
Avi Kivity04d2cc72007-09-10 18:10:54 +03002203 preempt_enable();
2204
2205 /*
2206 * Profile KVM exit RIPs:
2207 */
2208 if (unlikely(prof_on == KVM_PROFILING)) {
2209 kvm_x86_ops->cache_regs(vcpu);
2210 profile_hit(KVM_PROFILING, (void *)vcpu->rip);
2211 }
2212
2213 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2214
2215 if (r > 0) {
2216 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2217 r = -EINTR;
2218 kvm_run->exit_reason = KVM_EXIT_INTR;
2219 ++vcpu->stat.request_irq_exits;
2220 goto out;
2221 }
2222 if (!need_resched()) {
2223 ++vcpu->stat.light_exits;
2224 goto again;
2225 }
2226 }
2227
2228out:
2229 if (r > 0) {
2230 kvm_resched(vcpu);
2231 goto preempted;
2232 }
2233
2234 post_kvm_run_save(vcpu, kvm_run);
2235
2236 return r;
2237}
2238
2239
Avi Kivitybccf2152007-02-21 18:04:26 +02002240static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002241{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002242 int r;
Avi Kivity1961d272007-03-05 19:46:05 +02002243 sigset_t sigsaved;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002244
Avi Kivitybccf2152007-02-21 18:04:26 +02002245 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002246
He, Qingc5ec1532007-09-03 17:07:41 +03002247 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
2248 kvm_vcpu_block(vcpu);
2249 vcpu_put(vcpu);
2250 return -EAGAIN;
2251 }
2252
Avi Kivity1961d272007-03-05 19:46:05 +02002253 if (vcpu->sigset_active)
2254 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2255
Dor Laor54810342007-02-12 00:54:39 -08002256 /* re-sync apic's tpr */
He, Qing5cd4f6f2007-08-30 17:04:26 +08002257 if (!irqchip_in_kernel(vcpu->kvm))
2258 set_cr8(vcpu, kvm_run->cr8);
Dor Laor54810342007-02-12 00:54:39 -08002259
Avi Kivity02c83202007-04-29 15:02:17 +03002260 if (vcpu->pio.cur_count) {
2261 r = complete_pio(vcpu);
2262 if (r)
2263 goto out;
2264 }
Zhang Xiantao34c16ee2007-10-20 15:34:38 +08002265#if CONFIG_HAS_IOMEM
Avi Kivity02c83202007-04-29 15:02:17 +03002266 if (vcpu->mmio_needed) {
2267 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
2268 vcpu->mmio_read_completed = 1;
2269 vcpu->mmio_needed = 0;
2270 r = emulate_instruction(vcpu, kvm_run,
Laurent Vivier34273182007-09-18 11:27:37 +02002271 vcpu->mmio_fault_cr2, 0, 1);
Avi Kivity02c83202007-04-29 15:02:17 +03002272 if (r == EMULATE_DO_MMIO) {
2273 /*
2274 * Read-modify-write. Back to userspace.
2275 */
Avi Kivity02c83202007-04-29 15:02:17 +03002276 r = 0;
2277 goto out;
Avi Kivity46fc1472007-02-22 19:39:30 +02002278 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002279 }
Zhang Xiantao34c16ee2007-10-20 15:34:38 +08002280#endif
Avi Kivity8eb7d332007-03-04 14:17:08 +02002281 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002282 kvm_x86_ops->cache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002283 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002284 kvm_x86_ops->decache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002285 }
2286
Avi Kivity04d2cc72007-09-10 18:10:54 +03002287 r = __vcpu_run(vcpu, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002288
Avi Kivity039576c2007-03-20 12:46:50 +02002289out:
Avi Kivity1961d272007-03-05 19:46:05 +02002290 if (vcpu->sigset_active)
2291 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2292
Avi Kivity6aa8b732006-12-10 02:21:36 -08002293 vcpu_put(vcpu);
2294 return r;
2295}
2296
Avi Kivitybccf2152007-02-21 18:04:26 +02002297static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
2298 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002299{
Avi Kivitybccf2152007-02-21 18:04:26 +02002300 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002301
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002302 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002303
2304 regs->rax = vcpu->regs[VCPU_REGS_RAX];
2305 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
2306 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
2307 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
2308 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
2309 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
2310 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
2311 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002312#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002313 regs->r8 = vcpu->regs[VCPU_REGS_R8];
2314 regs->r9 = vcpu->regs[VCPU_REGS_R9];
2315 regs->r10 = vcpu->regs[VCPU_REGS_R10];
2316 regs->r11 = vcpu->regs[VCPU_REGS_R11];
2317 regs->r12 = vcpu->regs[VCPU_REGS_R12];
2318 regs->r13 = vcpu->regs[VCPU_REGS_R13];
2319 regs->r14 = vcpu->regs[VCPU_REGS_R14];
2320 regs->r15 = vcpu->regs[VCPU_REGS_R15];
2321#endif
2322
2323 regs->rip = vcpu->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002324 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002325
2326 /*
2327 * Don't leak debug flags in case they were set for guest debugging
2328 */
2329 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2330 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2331
2332 vcpu_put(vcpu);
2333
2334 return 0;
2335}
2336
Avi Kivitybccf2152007-02-21 18:04:26 +02002337static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
2338 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002339{
Avi Kivitybccf2152007-02-21 18:04:26 +02002340 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002341
2342 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
2343 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
2344 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
2345 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
2346 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
2347 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
2348 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
2349 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002350#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002351 vcpu->regs[VCPU_REGS_R8] = regs->r8;
2352 vcpu->regs[VCPU_REGS_R9] = regs->r9;
2353 vcpu->regs[VCPU_REGS_R10] = regs->r10;
2354 vcpu->regs[VCPU_REGS_R11] = regs->r11;
2355 vcpu->regs[VCPU_REGS_R12] = regs->r12;
2356 vcpu->regs[VCPU_REGS_R13] = regs->r13;
2357 vcpu->regs[VCPU_REGS_R14] = regs->r14;
2358 vcpu->regs[VCPU_REGS_R15] = regs->r15;
2359#endif
2360
2361 vcpu->rip = regs->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002362 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002363
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002364 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002365
2366 vcpu_put(vcpu);
2367
2368 return 0;
2369}
2370
2371static void get_segment(struct kvm_vcpu *vcpu,
2372 struct kvm_segment *var, int seg)
2373{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002374 return kvm_x86_ops->get_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002375}
2376
Avi Kivitybccf2152007-02-21 18:04:26 +02002377static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2378 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002379{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002380 struct descriptor_table dt;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002381 int pending_vec;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002382
Avi Kivitybccf2152007-02-21 18:04:26 +02002383 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002384
2385 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2386 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2387 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2388 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2389 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2390 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2391
2392 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2393 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2394
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002395 kvm_x86_ops->get_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002396 sregs->idt.limit = dt.limit;
2397 sregs->idt.base = dt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002398 kvm_x86_ops->get_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002399 sregs->gdt.limit = dt.limit;
2400 sregs->gdt.base = dt.base;
2401
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002402 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002403 sregs->cr0 = vcpu->cr0;
2404 sregs->cr2 = vcpu->cr2;
2405 sregs->cr3 = vcpu->cr3;
2406 sregs->cr4 = vcpu->cr4;
Eddie Dong7017fc32007-07-18 11:34:57 +03002407 sregs->cr8 = get_cr8(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002408 sregs->efer = vcpu->shadow_efer;
Eddie Dong7017fc32007-07-18 11:34:57 +03002409 sregs->apic_base = kvm_get_apic_base(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002410
Eddie Dong2a8067f2007-08-06 16:29:07 +03002411 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc52fb352007-08-02 14:03:07 +03002412 memset(sregs->interrupt_bitmap, 0,
2413 sizeof sregs->interrupt_bitmap);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002414 pending_vec = kvm_x86_ops->get_irq(vcpu);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002415 if (pending_vec >= 0)
Mike Dayd77c26f2007-10-08 09:02:08 -04002416 set_bit(pending_vec,
2417 (unsigned long *)sregs->interrupt_bitmap);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002418 } else
He, Qingc52fb352007-08-02 14:03:07 +03002419 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2420 sizeof sregs->interrupt_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002421
2422 vcpu_put(vcpu);
2423
2424 return 0;
2425}
2426
2427static void set_segment(struct kvm_vcpu *vcpu,
2428 struct kvm_segment *var, int seg)
2429{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002430 return kvm_x86_ops->set_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002431}
2432
Avi Kivitybccf2152007-02-21 18:04:26 +02002433static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2434 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002435{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002436 int mmu_reset_needed = 0;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002437 int i, pending_vec, max_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002438 struct descriptor_table dt;
2439
Avi Kivitybccf2152007-02-21 18:04:26 +02002440 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002441
Avi Kivity6aa8b732006-12-10 02:21:36 -08002442 dt.limit = sregs->idt.limit;
2443 dt.base = sregs->idt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002444 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002445 dt.limit = sregs->gdt.limit;
2446 dt.base = sregs->gdt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002447 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002448
2449 vcpu->cr2 = sregs->cr2;
2450 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2451 vcpu->cr3 = sregs->cr3;
2452
Eddie Dong7017fc32007-07-18 11:34:57 +03002453 set_cr8(vcpu, sregs->cr8);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002454
2455 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002456#ifdef CONFIG_X86_64
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002457 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002458#endif
Eddie Dong7017fc32007-07-18 11:34:57 +03002459 kvm_set_apic_base(vcpu, sregs->apic_base);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002460
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002461 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity399badf2007-01-05 16:36:38 -08002462
Avi Kivity6aa8b732006-12-10 02:21:36 -08002463 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
Rusty Russell81f50e32007-09-06 01:20:38 +10002464 vcpu->cr0 = sregs->cr0;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002465 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002466
2467 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002468 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Avi Kivity1b0973b2007-01-05 16:36:41 -08002469 if (!is_long_mode(vcpu) && is_pae(vcpu))
2470 load_pdptrs(vcpu, vcpu->cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002471
2472 if (mmu_reset_needed)
2473 kvm_mmu_reset_context(vcpu);
2474
He, Qingc52fb352007-08-02 14:03:07 +03002475 if (!irqchip_in_kernel(vcpu->kvm)) {
2476 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2477 sizeof vcpu->irq_pending);
2478 vcpu->irq_summary = 0;
2479 for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
2480 if (vcpu->irq_pending[i])
2481 __set_bit(i, &vcpu->irq_summary);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002482 } else {
2483 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
2484 pending_vec = find_first_bit(
2485 (const unsigned long *)sregs->interrupt_bitmap,
2486 max_bits);
2487 /* Only pending external irq is handled here */
2488 if (pending_vec < max_bits) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002489 kvm_x86_ops->set_irq(vcpu, pending_vec);
Mike Dayd77c26f2007-10-08 09:02:08 -04002490 pr_debug("Set back pending irq %d\n",
2491 pending_vec);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002492 }
He, Qingc52fb352007-08-02 14:03:07 +03002493 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002494
Avi Kivity024aa1c2007-03-21 13:44:58 +02002495 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2496 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2497 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2498 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2499 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2500 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2501
2502 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2503 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2504
Avi Kivity6aa8b732006-12-10 02:21:36 -08002505 vcpu_put(vcpu);
2506
2507 return 0;
2508}
2509
Rusty Russell1747fb72007-09-06 01:21:32 +10002510void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2511{
2512 struct kvm_segment cs;
2513
2514 get_segment(vcpu, &cs, VCPU_SREG_CS);
2515 *db = cs.db;
2516 *l = cs.l;
2517}
2518EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2519
Avi Kivity6aa8b732006-12-10 02:21:36 -08002520/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002521 * Translate a guest virtual address to a guest physical address.
2522 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002523static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2524 struct kvm_translation *tr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002525{
2526 unsigned long vaddr = tr->linear_address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002527 gpa_t gpa;
2528
Avi Kivitybccf2152007-02-21 18:04:26 +02002529 vcpu_load(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +08002530 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002531 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2532 tr->physical_address = gpa;
2533 tr->valid = gpa != UNMAPPED_GVA;
2534 tr->writeable = 1;
2535 tr->usermode = 0;
Shaohua Li11ec2802007-07-23 14:51:37 +08002536 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002537 vcpu_put(vcpu);
2538
2539 return 0;
2540}
2541
Avi Kivitybccf2152007-02-21 18:04:26 +02002542static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2543 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002544{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002545 if (irq->irq < 0 || irq->irq >= 256)
2546 return -EINVAL;
Eddie Dong97222cc2007-09-12 10:58:04 +03002547 if (irqchip_in_kernel(vcpu->kvm))
2548 return -ENXIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02002549 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002550
2551 set_bit(irq->irq, vcpu->irq_pending);
2552 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2553
2554 vcpu_put(vcpu);
2555
2556 return 0;
2557}
2558
Avi Kivitybccf2152007-02-21 18:04:26 +02002559static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2560 struct kvm_debug_guest *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002561{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002562 int r;
2563
Avi Kivitybccf2152007-02-21 18:04:26 +02002564 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002565
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002566 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002567
2568 vcpu_put(vcpu);
2569
2570 return r;
2571}
2572
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002573static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2574 unsigned long address,
2575 int *type)
2576{
2577 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2578 unsigned long pgoff;
2579 struct page *page;
2580
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002581 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +02002582 if (pgoff == 0)
2583 page = virt_to_page(vcpu->run);
2584 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2585 page = virt_to_page(vcpu->pio_data);
2586 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002587 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002588 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002589 if (type != NULL)
2590 *type = VM_FAULT_MINOR;
2591
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002592 return page;
2593}
2594
2595static struct vm_operations_struct kvm_vcpu_vm_ops = {
2596 .nopage = kvm_vcpu_nopage,
2597};
2598
2599static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2600{
2601 vma->vm_ops = &kvm_vcpu_vm_ops;
2602 return 0;
2603}
2604
Avi Kivitybccf2152007-02-21 18:04:26 +02002605static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2606{
2607 struct kvm_vcpu *vcpu = filp->private_data;
2608
2609 fput(vcpu->kvm->filp);
2610 return 0;
2611}
2612
2613static struct file_operations kvm_vcpu_fops = {
2614 .release = kvm_vcpu_release,
2615 .unlocked_ioctl = kvm_vcpu_ioctl,
2616 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002617 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02002618};
2619
2620/*
2621 * Allocates an inode for the vcpu.
2622 */
2623static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2624{
2625 int fd, r;
2626 struct inode *inode;
2627 struct file *file;
2628
Avi Kivityd6d28162007-06-28 08:38:16 -04002629 r = anon_inode_getfd(&fd, &inode, &file,
2630 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2631 if (r)
2632 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002633 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +02002634 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02002635}
2636
Avi Kivityc5ea7662007-02-20 18:41:05 +02002637/*
2638 * Creates some virtual cpus. Good luck creating more than one.
2639 */
2640static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2641{
2642 int r;
2643 struct kvm_vcpu *vcpu;
2644
Avi Kivityc5ea7662007-02-20 18:41:05 +02002645 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002646 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002647
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002648 vcpu = kvm_x86_ops->vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002649 if (IS_ERR(vcpu))
2650 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002651
Avi Kivity15ad7142007-07-11 18:17:21 +03002652 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
2653
Rusty Russellb114b082007-07-30 21:13:43 +10002654 /* We do fxsave: this must be aligned. */
2655 BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF);
2656
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002657 vcpu_load(vcpu);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002658 r = kvm_x86_ops->vcpu_reset(vcpu);
2659 if (r == 0)
2660 r = kvm_mmu_setup(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002661 vcpu_put(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002662 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002663 goto free_vcpu;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002664
Shaohua Li11ec2802007-07-23 14:51:37 +08002665 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002666 if (kvm->vcpus[n]) {
2667 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +08002668 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002669 goto mmu_unload;
2670 }
2671 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08002672 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002673
2674 /* Now it's all set up, let userspace reach it */
Avi Kivitybccf2152007-02-21 18:04:26 +02002675 r = create_vcpu_fd(vcpu);
2676 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002677 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02002678 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002679
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002680unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08002681 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002682 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +08002683 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002684
2685mmu_unload:
2686 vcpu_load(vcpu);
2687 kvm_mmu_unload(vcpu);
2688 vcpu_put(vcpu);
2689
2690free_vcpu:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002691 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002692 return r;
2693}
2694
Avi Kivity1961d272007-03-05 19:46:05 +02002695static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2696{
2697 if (sigset) {
2698 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2699 vcpu->sigset_active = 1;
2700 vcpu->sigset = *sigset;
2701 } else
2702 vcpu->sigset_active = 0;
2703 return 0;
2704}
2705
Avi Kivityb8836732007-04-01 16:34:31 +03002706/*
2707 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2708 * we have asm/x86/processor.h
2709 */
2710struct fxsave {
2711 u16 cwd;
2712 u16 swd;
2713 u16 twd;
2714 u16 fop;
2715 u64 rip;
2716 u64 rdp;
2717 u32 mxcsr;
2718 u32 mxcsr_mask;
2719 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2720#ifdef CONFIG_X86_64
2721 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2722#else
2723 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2724#endif
2725};
2726
2727static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2728{
Rusty Russellb114b082007-07-30 21:13:43 +10002729 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002730
2731 vcpu_load(vcpu);
2732
2733 memcpy(fpu->fpr, fxsave->st_space, 128);
2734 fpu->fcw = fxsave->cwd;
2735 fpu->fsw = fxsave->swd;
2736 fpu->ftwx = fxsave->twd;
2737 fpu->last_opcode = fxsave->fop;
2738 fpu->last_ip = fxsave->rip;
2739 fpu->last_dp = fxsave->rdp;
2740 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2741
2742 vcpu_put(vcpu);
2743
2744 return 0;
2745}
2746
2747static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2748{
Rusty Russellb114b082007-07-30 21:13:43 +10002749 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002750
2751 vcpu_load(vcpu);
2752
2753 memcpy(fxsave->st_space, fpu->fpr, 128);
2754 fxsave->cwd = fpu->fcw;
2755 fxsave->swd = fpu->fsw;
2756 fxsave->twd = fpu->ftwx;
2757 fxsave->fop = fpu->last_opcode;
2758 fxsave->rip = fpu->last_ip;
2759 fxsave->rdp = fpu->last_dp;
2760 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2761
2762 vcpu_put(vcpu);
2763
2764 return 0;
2765}
2766
Avi Kivitybccf2152007-02-21 18:04:26 +02002767static long kvm_vcpu_ioctl(struct file *filp,
2768 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002769{
Avi Kivitybccf2152007-02-21 18:04:26 +02002770 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +00002771 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02002772 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002773
2774 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002775 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002776 r = -EINVAL;
2777 if (arg)
2778 goto out;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002779 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002780 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002781 case KVM_GET_REGS: {
2782 struct kvm_regs kvm_regs;
2783
Avi Kivitybccf2152007-02-21 18:04:26 +02002784 memset(&kvm_regs, 0, sizeof kvm_regs);
2785 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002786 if (r)
2787 goto out;
2788 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002789 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002790 goto out;
2791 r = 0;
2792 break;
2793 }
2794 case KVM_SET_REGS: {
2795 struct kvm_regs kvm_regs;
2796
2797 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002798 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002799 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002800 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002801 if (r)
2802 goto out;
2803 r = 0;
2804 break;
2805 }
2806 case KVM_GET_SREGS: {
2807 struct kvm_sregs kvm_sregs;
2808
Avi Kivitybccf2152007-02-21 18:04:26 +02002809 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2810 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002811 if (r)
2812 goto out;
2813 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002814 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002815 goto out;
2816 r = 0;
2817 break;
2818 }
2819 case KVM_SET_SREGS: {
2820 struct kvm_sregs kvm_sregs;
2821
2822 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002823 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002824 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002825 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002826 if (r)
2827 goto out;
2828 r = 0;
2829 break;
2830 }
2831 case KVM_TRANSLATE: {
2832 struct kvm_translation tr;
2833
2834 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002835 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002836 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002837 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002838 if (r)
2839 goto out;
2840 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002841 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002842 goto out;
2843 r = 0;
2844 break;
2845 }
2846 case KVM_INTERRUPT: {
2847 struct kvm_interrupt irq;
2848
2849 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002850 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002851 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002852 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002853 if (r)
2854 goto out;
2855 r = 0;
2856 break;
2857 }
2858 case KVM_DEBUG_GUEST: {
2859 struct kvm_debug_guest dbg;
2860
2861 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002862 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002863 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002864 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002865 if (r)
2866 goto out;
2867 r = 0;
2868 break;
2869 }
Avi Kivity1961d272007-03-05 19:46:05 +02002870 case KVM_SET_SIGNAL_MASK: {
2871 struct kvm_signal_mask __user *sigmask_arg = argp;
2872 struct kvm_signal_mask kvm_sigmask;
2873 sigset_t sigset, *p;
2874
2875 p = NULL;
2876 if (argp) {
2877 r = -EFAULT;
2878 if (copy_from_user(&kvm_sigmask, argp,
2879 sizeof kvm_sigmask))
2880 goto out;
2881 r = -EINVAL;
2882 if (kvm_sigmask.len != sizeof sigset)
2883 goto out;
2884 r = -EFAULT;
2885 if (copy_from_user(&sigset, sigmask_arg->sigset,
2886 sizeof sigset))
2887 goto out;
2888 p = &sigset;
2889 }
2890 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2891 break;
2892 }
Avi Kivityb8836732007-04-01 16:34:31 +03002893 case KVM_GET_FPU: {
2894 struct kvm_fpu fpu;
2895
2896 memset(&fpu, 0, sizeof fpu);
2897 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2898 if (r)
2899 goto out;
2900 r = -EFAULT;
2901 if (copy_to_user(argp, &fpu, sizeof fpu))
2902 goto out;
2903 r = 0;
2904 break;
2905 }
2906 case KVM_SET_FPU: {
2907 struct kvm_fpu fpu;
2908
2909 r = -EFAULT;
2910 if (copy_from_user(&fpu, argp, sizeof fpu))
2911 goto out;
2912 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2913 if (r)
2914 goto out;
2915 r = 0;
2916 break;
2917 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002918 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02002919 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02002920 }
2921out:
2922 return r;
2923}
2924
2925static long kvm_vm_ioctl(struct file *filp,
2926 unsigned int ioctl, unsigned long arg)
2927{
2928 struct kvm *kvm = filp->private_data;
2929 void __user *argp = (void __user *)arg;
2930 int r = -EINVAL;
2931
2932 switch (ioctl) {
2933 case KVM_CREATE_VCPU:
2934 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2935 if (r < 0)
2936 goto out;
2937 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002938 case KVM_SET_MEMORY_REGION: {
2939 struct kvm_memory_region kvm_mem;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002940 struct kvm_userspace_memory_region kvm_userspace_mem;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002941
2942 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002943 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002944 goto out;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002945 kvm_userspace_mem.slot = kvm_mem.slot;
2946 kvm_userspace_mem.flags = kvm_mem.flags;
2947 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
2948 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
2949 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
2950 if (r)
2951 goto out;
2952 break;
2953 }
2954 case KVM_SET_USER_MEMORY_REGION: {
2955 struct kvm_userspace_memory_region kvm_userspace_mem;
2956
2957 r = -EFAULT;
2958 if (copy_from_user(&kvm_userspace_mem, argp,
2959 sizeof kvm_userspace_mem))
2960 goto out;
2961
2962 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002963 if (r)
2964 goto out;
2965 break;
2966 }
Izik Eidus82ce2c92007-10-02 18:52:55 +02002967 case KVM_SET_NR_MMU_PAGES:
2968 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
2969 if (r)
2970 goto out;
2971 break;
2972 case KVM_GET_NR_MMU_PAGES:
2973 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
2974 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002975 case KVM_GET_DIRTY_LOG: {
2976 struct kvm_dirty_log log;
2977
2978 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002979 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002980 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002981 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002982 if (r)
2983 goto out;
2984 break;
2985 }
Avi Kivitye8207542007-03-30 16:54:30 +03002986 case KVM_SET_MEMORY_ALIAS: {
2987 struct kvm_memory_alias alias;
2988
2989 r = -EFAULT;
2990 if (copy_from_user(&alias, argp, sizeof alias))
2991 goto out;
2992 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
2993 if (r)
2994 goto out;
2995 break;
2996 }
Eddie Dong85f455f2007-07-06 12:20:49 +03002997 case KVM_CREATE_IRQCHIP:
2998 r = -ENOMEM;
2999 kvm->vpic = kvm_create_pic(kvm);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03003000 if (kvm->vpic) {
3001 r = kvm_ioapic_init(kvm);
3002 if (r) {
3003 kfree(kvm->vpic);
3004 kvm->vpic = NULL;
3005 goto out;
3006 }
Mike Dayd77c26f2007-10-08 09:02:08 -04003007 } else
Eddie Dong85f455f2007-07-06 12:20:49 +03003008 goto out;
3009 break;
3010 case KVM_IRQ_LINE: {
3011 struct kvm_irq_level irq_event;
3012
3013 r = -EFAULT;
3014 if (copy_from_user(&irq_event, argp, sizeof irq_event))
3015 goto out;
3016 if (irqchip_in_kernel(kvm)) {
Eddie Dong9cf98822007-07-22 10:36:31 +03003017 mutex_lock(&kvm->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +03003018 if (irq_event.irq < 16)
3019 kvm_pic_set_irq(pic_irqchip(kvm),
3020 irq_event.irq,
3021 irq_event.level);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03003022 kvm_ioapic_set_irq(kvm->vioapic,
3023 irq_event.irq,
3024 irq_event.level);
Eddie Dong9cf98822007-07-22 10:36:31 +03003025 mutex_unlock(&kvm->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +03003026 r = 0;
3027 }
3028 break;
3029 }
He, Qing6ceb9d72007-07-26 11:05:18 +03003030 case KVM_GET_IRQCHIP: {
3031 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3032 struct kvm_irqchip chip;
3033
3034 r = -EFAULT;
3035 if (copy_from_user(&chip, argp, sizeof chip))
3036 goto out;
3037 r = -ENXIO;
3038 if (!irqchip_in_kernel(kvm))
3039 goto out;
3040 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
3041 if (r)
3042 goto out;
3043 r = -EFAULT;
3044 if (copy_to_user(argp, &chip, sizeof chip))
3045 goto out;
3046 r = 0;
3047 break;
3048 }
3049 case KVM_SET_IRQCHIP: {
3050 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3051 struct kvm_irqchip chip;
3052
3053 r = -EFAULT;
3054 if (copy_from_user(&chip, argp, sizeof chip))
3055 goto out;
3056 r = -ENXIO;
3057 if (!irqchip_in_kernel(kvm))
3058 goto out;
3059 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
3060 if (r)
3061 goto out;
3062 r = 0;
3063 break;
3064 }
Avi Kivityf17abe92007-02-21 19:28:04 +02003065 default:
3066 ;
3067 }
3068out:
3069 return r;
3070}
3071
3072static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
3073 unsigned long address,
3074 int *type)
3075{
3076 struct kvm *kvm = vma->vm_file->private_data;
3077 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02003078 struct page *page;
3079
Avi Kivityf17abe92007-02-21 19:28:04 +02003080 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity954bbbc22007-03-30 14:02:32 +03003081 page = gfn_to_page(kvm, pgoff);
Izik Eidus8a7ae052007-10-18 11:09:33 +02003082 if (is_error_page(page)) {
3083 kvm_release_page(page);
Avi Kivityf17abe92007-02-21 19:28:04 +02003084 return NOPAGE_SIGBUS;
Izik Eidus8a7ae052007-10-18 11:09:33 +02003085 }
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03003086 if (type != NULL)
3087 *type = VM_FAULT_MINOR;
3088
Avi Kivityf17abe92007-02-21 19:28:04 +02003089 return page;
3090}
3091
3092static struct vm_operations_struct kvm_vm_vm_ops = {
3093 .nopage = kvm_vm_nopage,
3094};
3095
3096static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
3097{
3098 vma->vm_ops = &kvm_vm_vm_ops;
3099 return 0;
3100}
3101
3102static struct file_operations kvm_vm_fops = {
3103 .release = kvm_vm_release,
3104 .unlocked_ioctl = kvm_vm_ioctl,
3105 .compat_ioctl = kvm_vm_ioctl,
3106 .mmap = kvm_vm_mmap,
3107};
3108
3109static int kvm_dev_ioctl_create_vm(void)
3110{
3111 int fd, r;
3112 struct inode *inode;
3113 struct file *file;
3114 struct kvm *kvm;
3115
Avi Kivityf17abe92007-02-21 19:28:04 +02003116 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04003117 if (IS_ERR(kvm))
3118 return PTR_ERR(kvm);
3119 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
3120 if (r) {
3121 kvm_destroy_vm(kvm);
3122 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02003123 }
3124
Avi Kivitybccf2152007-02-21 18:04:26 +02003125 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02003126
Avi Kivityf17abe92007-02-21 19:28:04 +02003127 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02003128}
3129
3130static long kvm_dev_ioctl(struct file *filp,
3131 unsigned int ioctl, unsigned long arg)
3132{
3133 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02003134 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02003135
3136 switch (ioctl) {
3137 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02003138 r = -EINVAL;
3139 if (arg)
3140 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02003141 r = KVM_API_VERSION;
3142 break;
3143 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02003144 r = -EINVAL;
3145 if (arg)
3146 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02003147 r = kvm_dev_ioctl_create_vm();
3148 break;
Eddie Dong85f455f2007-07-06 12:20:49 +03003149 case KVM_CHECK_EXTENSION: {
3150 int ext = (long)argp;
3151
3152 switch (ext) {
3153 case KVM_CAP_IRQCHIP:
Eddie Dongb6958ce2007-07-18 12:15:21 +03003154 case KVM_CAP_HLT:
Izik Eidus82ce2c92007-10-02 18:52:55 +02003155 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Izik Eidus6fc138d2007-10-09 19:20:39 +02003156 case KVM_CAP_USER_MEMORY:
Eddie Dong85f455f2007-07-06 12:20:49 +03003157 r = 1;
3158 break;
3159 default:
3160 r = 0;
3161 break;
3162 }
Avi Kivity5d308f42007-03-01 17:56:20 +02003163 break;
Eddie Dong85f455f2007-07-06 12:20:49 +03003164 }
Avi Kivity07c45a32007-03-07 13:05:38 +02003165 case KVM_GET_VCPU_MMAP_SIZE:
3166 r = -EINVAL;
3167 if (arg)
3168 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02003169 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02003170 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003171 default:
Carsten Otte043405e2007-10-10 17:16:19 +02003172 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003173 }
3174out:
3175 return r;
3176}
3177
Avi Kivity6aa8b732006-12-10 02:21:36 -08003178static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003179 .unlocked_ioctl = kvm_dev_ioctl,
3180 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003181};
3182
3183static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02003184 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003185 "kvm",
3186 &kvm_chardev_ops,
3187};
3188
Avi Kivity774c47f2007-02-12 00:54:47 -08003189/*
3190 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
3191 * cached on it.
3192 */
3193static void decache_vcpus_on_cpu(int cpu)
3194{
3195 struct kvm *vm;
3196 struct kvm_vcpu *vcpu;
3197 int i;
3198
3199 spin_lock(&kvm_lock);
Shaohua Li11ec2802007-07-23 14:51:37 +08003200 list_for_each_entry(vm, &vm_list, vm_list)
Avi Kivity774c47f2007-02-12 00:54:47 -08003201 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003202 vcpu = vm->vcpus[i];
3203 if (!vcpu)
3204 continue;
Avi Kivity774c47f2007-02-12 00:54:47 -08003205 /*
3206 * If the vcpu is locked, then it is running on some
3207 * other cpu and therefore it is not cached on the
3208 * cpu in question.
3209 *
3210 * If it's not locked, check the last cpu it executed
3211 * on.
3212 */
3213 if (mutex_trylock(&vcpu->mutex)) {
3214 if (vcpu->cpu == cpu) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003215 kvm_x86_ops->vcpu_decache(vcpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08003216 vcpu->cpu = -1;
3217 }
3218 mutex_unlock(&vcpu->mutex);
3219 }
3220 }
3221 spin_unlock(&kvm_lock);
3222}
3223
Avi Kivity1b6c0162007-05-24 13:03:52 +03003224static void hardware_enable(void *junk)
3225{
3226 int cpu = raw_smp_processor_id();
3227
3228 if (cpu_isset(cpu, cpus_hardware_enabled))
3229 return;
3230 cpu_set(cpu, cpus_hardware_enabled);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003231 kvm_x86_ops->hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003232}
3233
3234static void hardware_disable(void *junk)
3235{
3236 int cpu = raw_smp_processor_id();
3237
3238 if (!cpu_isset(cpu, cpus_hardware_enabled))
3239 return;
3240 cpu_clear(cpu, cpus_hardware_enabled);
3241 decache_vcpus_on_cpu(cpu);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003242 kvm_x86_ops->hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003243}
3244
Avi Kivity774c47f2007-02-12 00:54:47 -08003245static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
3246 void *v)
3247{
3248 int cpu = (long)v;
3249
3250 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03003251 case CPU_DYING:
3252 case CPU_DYING_FROZEN:
Avi Kivity6ec8a852007-08-19 15:57:26 +03003253 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3254 cpu);
3255 hardware_disable(NULL);
3256 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08003257 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003258 case CPU_UP_CANCELED_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003259 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3260 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003261 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003262 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02003263 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003264 case CPU_ONLINE_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003265 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
3266 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003267 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003268 break;
3269 }
3270 return NOTIFY_OK;
3271}
3272
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003273static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04003274 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003275{
3276 if (val == SYS_RESTART) {
3277 /*
3278 * Some (well, at least mine) BIOSes hang on reboot if
3279 * in vmx root mode.
3280 */
3281 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
3282 on_each_cpu(hardware_disable, NULL, 0, 1);
3283 }
3284 return NOTIFY_OK;
3285}
3286
3287static struct notifier_block kvm_reboot_notifier = {
3288 .notifier_call = kvm_reboot,
3289 .priority = 0,
3290};
3291
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04003292void kvm_io_bus_init(struct kvm_io_bus *bus)
3293{
3294 memset(bus, 0, sizeof(*bus));
3295}
3296
3297void kvm_io_bus_destroy(struct kvm_io_bus *bus)
3298{
3299 int i;
3300
3301 for (i = 0; i < bus->dev_count; i++) {
3302 struct kvm_io_device *pos = bus->devs[i];
3303
3304 kvm_iodevice_destructor(pos);
3305 }
3306}
3307
3308struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
3309{
3310 int i;
3311
3312 for (i = 0; i < bus->dev_count; i++) {
3313 struct kvm_io_device *pos = bus->devs[i];
3314
3315 if (pos->in_range(pos, addr))
3316 return pos;
3317 }
3318
3319 return NULL;
3320}
3321
3322void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
3323{
3324 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
3325
3326 bus->devs[bus->dev_count++] = dev;
3327}
3328
Avi Kivity774c47f2007-02-12 00:54:47 -08003329static struct notifier_block kvm_cpu_notifier = {
3330 .notifier_call = kvm_cpu_hotplug,
3331 .priority = 20, /* must be > scheduler priority */
3332};
3333
Avi Kivity1165f5f2007-04-19 17:27:43 +03003334static u64 stat_get(void *_offset)
3335{
3336 unsigned offset = (long)_offset;
3337 u64 total = 0;
3338 struct kvm *kvm;
3339 struct kvm_vcpu *vcpu;
3340 int i;
3341
3342 spin_lock(&kvm_lock);
3343 list_for_each_entry(kvm, &vm_list, vm_list)
3344 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003345 vcpu = kvm->vcpus[i];
3346 if (vcpu)
3347 total += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03003348 }
3349 spin_unlock(&kvm_lock);
3350 return total;
3351}
3352
Rusty Russell3dea7ca2007-08-01 10:12:22 +10003353DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n");
Avi Kivity1165f5f2007-04-19 17:27:43 +03003354
Avi Kivity6aa8b732006-12-10 02:21:36 -08003355static __init void kvm_init_debug(void)
3356{
3357 struct kvm_stats_debugfs_item *p;
3358
Al Viro8b6d44c2007-02-09 16:38:40 +00003359 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003360 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03003361 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
3362 (void *)(long)p->offset,
3363 &stat_fops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003364}
3365
3366static void kvm_exit_debug(void)
3367{
3368 struct kvm_stats_debugfs_item *p;
3369
3370 for (p = debugfs_entries; p->name; ++p)
3371 debugfs_remove(p->dentry);
3372 debugfs_remove(debugfs_dir);
3373}
3374
Avi Kivity59ae6c62007-02-12 00:54:48 -08003375static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3376{
Avi Kivity4267c412007-05-24 13:09:41 +03003377 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003378 return 0;
3379}
3380
3381static int kvm_resume(struct sys_device *dev)
3382{
Avi Kivity4267c412007-05-24 13:09:41 +03003383 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003384 return 0;
3385}
3386
3387static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01003388 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08003389 .suspend = kvm_suspend,
3390 .resume = kvm_resume,
3391};
3392
3393static struct sys_device kvm_sysdev = {
3394 .id = 0,
3395 .cls = &kvm_sysdev_class,
3396};
3397
Izik Eiduscea7bb22007-10-17 19:17:48 +02003398struct page *bad_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003399
Avi Kivity15ad7142007-07-11 18:17:21 +03003400static inline
3401struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
3402{
3403 return container_of(pn, struct kvm_vcpu, preempt_notifier);
3404}
3405
3406static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
3407{
3408 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3409
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003410 kvm_x86_ops->vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003411}
3412
3413static void kvm_sched_out(struct preempt_notifier *pn,
3414 struct task_struct *next)
3415{
3416 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3417
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003418 kvm_x86_ops->vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003419}
3420
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003421int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10003422 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003423{
3424 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003425 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003426
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003427 if (kvm_x86_ops) {
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08003428 printk(KERN_ERR "kvm: already loaded the other module\n");
3429 return -EEXIST;
3430 }
3431
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003432 if (!ops->cpu_has_kvm_support()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003433 printk(KERN_ERR "kvm: no hardware support\n");
3434 return -EOPNOTSUPP;
3435 }
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003436 if (ops->disabled_by_bios()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003437 printk(KERN_ERR "kvm: disabled by bios\n");
3438 return -EOPNOTSUPP;
3439 }
3440
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003441 kvm_x86_ops = ops;
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003442
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003443 r = kvm_x86_ops->hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003444 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02003445 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003446
Yang, Sheng002c7f72007-07-31 14:23:01 +03003447 for_each_online_cpu(cpu) {
3448 smp_call_function_single(cpu,
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003449 kvm_x86_ops->check_processor_compatibility,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003450 &r, 0, 1);
3451 if (r < 0)
3452 goto out_free_0;
3453 }
3454
Avi Kivity1b6c0162007-05-24 13:03:52 +03003455 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003456 r = register_cpu_notifier(&kvm_cpu_notifier);
3457 if (r)
3458 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003459 register_reboot_notifier(&kvm_reboot_notifier);
3460
Avi Kivity59ae6c62007-02-12 00:54:48 -08003461 r = sysdev_class_register(&kvm_sysdev_class);
3462 if (r)
3463 goto out_free_2;
3464
3465 r = sysdev_register(&kvm_sysdev);
3466 if (r)
3467 goto out_free_3;
3468
Rusty Russellc16f8622007-07-30 21:12:19 +10003469 /* A kmem cache lets us meet the alignment requirements of fx_save. */
3470 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
3471 __alignof__(struct kvm_vcpu), 0, 0);
3472 if (!kvm_vcpu_cache) {
3473 r = -ENOMEM;
3474 goto out_free_4;
3475 }
3476
Avi Kivity6aa8b732006-12-10 02:21:36 -08003477 kvm_chardev_ops.owner = module;
3478
3479 r = misc_register(&kvm_dev);
3480 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04003481 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08003482 goto out_free;
3483 }
3484
Avi Kivity15ad7142007-07-11 18:17:21 +03003485 kvm_preempt_ops.sched_in = kvm_sched_in;
3486 kvm_preempt_ops.sched_out = kvm_sched_out;
3487
Avi Kivityc7addb92007-09-16 18:58:32 +02003488 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
3489
3490 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003491
3492out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10003493 kmem_cache_destroy(kvm_vcpu_cache);
3494out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08003495 sysdev_unregister(&kvm_sysdev);
3496out_free_3:
3497 sysdev_class_unregister(&kvm_sysdev_class);
3498out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003499 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08003500 unregister_cpu_notifier(&kvm_cpu_notifier);
3501out_free_1:
Avi Kivity1b6c0162007-05-24 13:03:52 +03003502 on_each_cpu(hardware_disable, NULL, 0, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03003503out_free_0:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003504 kvm_x86_ops->hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02003505out:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003506 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003507 return r;
3508}
Mike Dayd77c26f2007-10-08 09:02:08 -04003509EXPORT_SYMBOL_GPL(kvm_init_x86);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003510
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003511void kvm_exit_x86(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003512{
3513 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10003514 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003515 sysdev_unregister(&kvm_sysdev);
3516 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003517 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003518 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003519 on_each_cpu(hardware_disable, NULL, 0, 1);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003520 kvm_x86_ops->hardware_unsetup();
3521 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003522}
Mike Dayd77c26f2007-10-08 09:02:08 -04003523EXPORT_SYMBOL_GPL(kvm_exit_x86);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003524
3525static __init int kvm_init(void)
3526{
Avi Kivity37e29d92007-02-20 14:07:37 +02003527 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003528
Avi Kivityb5a33a72007-04-15 16:31:09 +03003529 r = kvm_mmu_module_init();
3530 if (r)
3531 goto out4;
3532
Avi Kivity6aa8b732006-12-10 02:21:36 -08003533 kvm_init_debug();
3534
Carsten Otte043405e2007-10-10 17:16:19 +02003535 kvm_arch_init();
Michael Riepebf591b22006-12-22 01:05:36 -08003536
Izik Eiduscea7bb22007-10-17 19:17:48 +02003537 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
Mike Dayd77c26f2007-10-08 09:02:08 -04003538
3539 if (bad_page == NULL) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003540 r = -ENOMEM;
3541 goto out;
3542 }
3543
Avi Kivity58e690e2007-02-26 16:29:43 +02003544 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003545
3546out:
3547 kvm_exit_debug();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003548 kvm_mmu_module_exit();
3549out4:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003550 return r;
3551}
3552
3553static __exit void kvm_exit(void)
3554{
3555 kvm_exit_debug();
Izik Eiduscea7bb22007-10-17 19:17:48 +02003556 __free_page(bad_page);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003557 kvm_mmu_module_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003558}
3559
3560module_init(kvm_init)
3561module_exit(kvm_exit)