blob: a044856bd02b1bd38a25faec2f16b67bcf1d3628 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 *
9 * Authors:
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
17
18#include "kvm.h"
Carsten Otte043405e2007-10-10 17:16:19 +020019#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040020#include "x86_emulate.h"
21#include "segment_descriptor.h"
Eddie Dong85f455f2007-07-06 12:20:49 +030022#include "irq.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080023
24#include <linux/kvm.h>
25#include <linux/module.h>
26#include <linux/errno.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/percpu.h>
28#include <linux/gfp.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080029#include <linux/mm.h>
30#include <linux/miscdevice.h>
31#include <linux/vmalloc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080032#include <linux/reboot.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080033#include <linux/debugfs.h>
34#include <linux/highmem.h>
35#include <linux/file.h>
Avi Kivity59ae6c62007-02-12 00:54:48 -080036#include <linux/sysdev.h>
Avi Kivity774c47f2007-02-12 00:54:47 -080037#include <linux/cpu.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040038#include <linux/sched.h>
Avi Kivityd9e368d2007-06-07 19:18:30 +030039#include <linux/cpumask.h>
40#include <linux/smp.h>
Avi Kivityd6d28162007-06-28 08:38:16 -040041#include <linux/anon_inodes.h>
Avi Kivity04d2cc72007-09-10 18:10:54 +030042#include <linux/profile.h>
Anthony Liguori7aa81cc2007-09-17 14:57:50 -050043#include <linux/kvm_para.h>
Izik Eidus6fc138d2007-10-09 19:20:39 +020044#include <linux/pagemap.h>
Anthony Liguori8d4e1282007-10-18 09:59:34 -050045#include <linux/mman.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080046
Avi Kivitye4956062007-06-28 14:15:57 -040047#include <asm/processor.h>
48#include <asm/msr.h>
49#include <asm/io.h>
50#include <asm/uaccess.h>
51#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080052
53MODULE_AUTHOR("Qumranet");
54MODULE_LICENSE("GPL");
55
Avi Kivity133de902007-02-12 00:54:44 -080056static DEFINE_SPINLOCK(kvm_lock);
57static LIST_HEAD(vm_list);
58
Avi Kivity1b6c0162007-05-24 13:03:52 +030059static cpumask_t cpus_hardware_enabled;
60
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +030061struct kvm_x86_ops *kvm_x86_ops;
Rusty Russellc16f8622007-07-30 21:12:19 +100062struct kmem_cache *kvm_vcpu_cache;
63EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030064
Avi Kivity15ad7142007-07-11 18:17:21 +030065static __read_mostly struct preempt_ops kvm_preempt_ops;
66
Avi Kivity1165f5f2007-04-19 17:27:43 +030067#define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
Avi Kivity6aa8b732006-12-10 02:21:36 -080068
69static struct kvm_stats_debugfs_item {
70 const char *name;
Avi Kivity1165f5f2007-04-19 17:27:43 +030071 int offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -080072 struct dentry *dentry;
73} debugfs_entries[] = {
Avi Kivity1165f5f2007-04-19 17:27:43 +030074 { "pf_fixed", STAT_OFFSET(pf_fixed) },
75 { "pf_guest", STAT_OFFSET(pf_guest) },
76 { "tlb_flush", STAT_OFFSET(tlb_flush) },
77 { "invlpg", STAT_OFFSET(invlpg) },
78 { "exits", STAT_OFFSET(exits) },
79 { "io_exits", STAT_OFFSET(io_exits) },
80 { "mmio_exits", STAT_OFFSET(mmio_exits) },
81 { "signal_exits", STAT_OFFSET(signal_exits) },
82 { "irq_window", STAT_OFFSET(irq_window_exits) },
83 { "halt_exits", STAT_OFFSET(halt_exits) },
Eddie Dongb6958ce2007-07-18 12:15:21 +030084 { "halt_wakeup", STAT_OFFSET(halt_wakeup) },
Avi Kivity1165f5f2007-04-19 17:27:43 +030085 { "request_irq", STAT_OFFSET(request_irq_exits) },
86 { "irq_exits", STAT_OFFSET(irq_exits) },
Avi Kivitye6adf282007-04-30 16:07:54 +030087 { "light_exits", STAT_OFFSET(light_exits) },
Eddie Dong2cc51562007-05-21 07:28:09 +030088 { "efer_reload", STAT_OFFSET(efer_reload) },
Avi Kivity1165f5f2007-04-19 17:27:43 +030089 { NULL }
Avi Kivity6aa8b732006-12-10 02:21:36 -080090};
91
92static struct dentry *debugfs_dir;
93
Rusty Russell707d92f2007-07-17 23:19:08 +100094#define CR0_RESERVED_BITS \
95 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
96 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
97 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
Rusty Russell66aee912007-07-17 23:34:16 +100098#define CR4_RESERVED_BITS \
99 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
100 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
101 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
102 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
103
Rusty Russell7075bc82007-07-17 23:37:17 +1000104#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800105#define EFER_RESERVED_BITS 0xfffffffffffff2fe
106
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800107#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400108/* LDT or TSS descriptor in the GDT. 16 bytes. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800109struct segment_descriptor_64 {
110 struct segment_descriptor s;
111 u32 base_higher;
112 u32 pad_zero;
113};
114
115#endif
116
Avi Kivitybccf2152007-02-21 18:04:26 +0200117static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
118 unsigned long arg);
119
Avi Kivity6aa8b732006-12-10 02:21:36 -0800120unsigned long segment_base(u16 selector)
121{
122 struct descriptor_table gdt;
123 struct segment_descriptor *d;
124 unsigned long table_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800125 unsigned long v;
126
127 if (selector == 0)
128 return 0;
129
Mike Dayd77c26f2007-10-08 09:02:08 -0400130 asm("sgdt %0" : "=m"(gdt));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131 table_base = gdt.base;
132
133 if (selector & 4) { /* from ldt */
134 u16 ldt_selector;
135
Mike Dayd77c26f2007-10-08 09:02:08 -0400136 asm("sldt %0" : "=g"(ldt_selector));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800137 table_base = segment_base(ldt_selector);
138 }
139 d = (struct segment_descriptor *)(table_base + (selector & ~7));
Mike Dayd77c26f2007-10-08 09:02:08 -0400140 v = d->base_low | ((unsigned long)d->base_mid << 16) |
141 ((unsigned long)d->base_high << 24);
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800142#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400143 if (d->system == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
144 v |= ((unsigned long) \
145 ((struct segment_descriptor_64 *)d)->base_higher) << 32;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800146#endif
147 return v;
148}
149EXPORT_SYMBOL_GPL(segment_base);
150
James Morris5aacf0c2006-12-22 01:04:55 -0800151static inline int valid_vcpu(int n)
152{
153 return likely(n >= 0 && n < KVM_MAX_VCPUS);
154}
155
Avi Kivity7702fd12007-06-14 16:27:40 +0300156void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
157{
158 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
159 return;
160
161 vcpu->guest_fpu_loaded = 1;
Rusty Russellb114b082007-07-30 21:13:43 +1000162 fx_save(&vcpu->host_fx_image);
163 fx_restore(&vcpu->guest_fx_image);
Avi Kivity7702fd12007-06-14 16:27:40 +0300164}
165EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
166
167void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
168{
169 if (!vcpu->guest_fpu_loaded)
170 return;
171
172 vcpu->guest_fpu_loaded = 0;
Rusty Russellb114b082007-07-30 21:13:43 +1000173 fx_save(&vcpu->guest_fx_image);
174 fx_restore(&vcpu->host_fx_image);
Avi Kivity7702fd12007-06-14 16:27:40 +0300175}
176EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
177
Avi Kivity6aa8b732006-12-10 02:21:36 -0800178/*
179 * Switches to specified vcpu, until a matching vcpu_put()
180 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200181void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800182{
Avi Kivity15ad7142007-07-11 18:17:21 +0300183 int cpu;
184
Avi Kivitybccf2152007-02-21 18:04:26 +0200185 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300186 cpu = get_cpu();
187 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200188 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300189 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200190}
191
Carsten Otte313a3dc2007-10-11 19:16:52 +0200192void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800193{
Avi Kivity15ad7142007-07-11 18:17:21 +0300194 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200195 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300196 preempt_notifier_unregister(&vcpu->preempt_notifier);
197 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800198 mutex_unlock(&vcpu->mutex);
199}
200
Avi Kivityd9e368d2007-06-07 19:18:30 +0300201static void ack_flush(void *_completed)
202{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300203}
204
205void kvm_flush_remote_tlbs(struct kvm *kvm)
206{
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200207 int i, cpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300208 cpumask_t cpus;
209 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300210
Avi Kivityd9e368d2007-06-07 19:18:30 +0300211 cpus_clear(cpus);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000212 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
213 vcpu = kvm->vcpus[i];
214 if (!vcpu)
215 continue;
Avi Kivity3176bc32007-10-16 17:22:08 +0200216 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300217 continue;
218 cpu = vcpu->cpu;
219 if (cpu != -1 && cpu != raw_smp_processor_id())
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200220 cpu_set(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300221 }
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200222 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300223}
224
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000225int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
226{
227 struct page *page;
228 int r;
229
230 mutex_init(&vcpu->mutex);
231 vcpu->cpu = -1;
232 vcpu->mmu.root_hpa = INVALID_PAGE;
233 vcpu->kvm = kvm;
234 vcpu->vcpu_id = id;
He, Qingc5ec1532007-09-03 17:07:41 +0300235 if (!irqchip_in_kernel(kvm) || id == 0)
236 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
237 else
238 vcpu->mp_state = VCPU_MP_STATE_UNINITIALIZED;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300239 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000240
241 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
242 if (!page) {
243 r = -ENOMEM;
244 goto fail;
245 }
246 vcpu->run = page_address(page);
247
248 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
249 if (!page) {
250 r = -ENOMEM;
251 goto fail_free_run;
252 }
253 vcpu->pio_data = page_address(page);
254
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000255 r = kvm_mmu_create(vcpu);
256 if (r < 0)
257 goto fail_free_pio_data;
258
Rusty Russell76fafa52007-10-08 10:50:48 +1000259 if (irqchip_in_kernel(kvm)) {
260 r = kvm_create_lapic(vcpu);
261 if (r < 0)
262 goto fail_mmu_destroy;
263 }
264
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000265 return 0;
266
Rusty Russell76fafa52007-10-08 10:50:48 +1000267fail_mmu_destroy:
268 kvm_mmu_destroy(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000269fail_free_pio_data:
270 free_page((unsigned long)vcpu->pio_data);
271fail_free_run:
272 free_page((unsigned long)vcpu->run);
273fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000274 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000275}
276EXPORT_SYMBOL_GPL(kvm_vcpu_init);
277
278void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
279{
Rusty Russelld5894442007-10-08 10:48:30 +1000280 kvm_free_lapic(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000281 kvm_mmu_destroy(vcpu);
282 free_page((unsigned long)vcpu->pio_data);
283 free_page((unsigned long)vcpu->run);
284}
285EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
286
Avi Kivityf17abe92007-02-21 19:28:04 +0200287static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800288{
289 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800290
291 if (!kvm)
Avi Kivityf17abe92007-02-21 19:28:04 +0200292 return ERR_PTR(-ENOMEM);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800293
Eddie Dong74906342007-06-19 18:05:03 +0300294 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800295 mutex_init(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800296 INIT_LIST_HEAD(&kvm->active_mmu_pages);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400297 kvm_io_bus_init(&kvm->mmio_bus);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000298 spin_lock(&kvm_lock);
299 list_add(&kvm->vm_list, &vm_list);
300 spin_unlock(&kvm_lock);
Avi Kivityf17abe92007-02-21 19:28:04 +0200301 return kvm;
302}
303
Avi Kivity6aa8b732006-12-10 02:21:36 -0800304/*
305 * Free any memory in @free but not in @dont.
306 */
307static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
308 struct kvm_memory_slot *dont)
309{
Izik Eidus290fc382007-09-27 14:11:22 +0200310 if (!dont || free->rmap != dont->rmap)
311 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800312
313 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
314 vfree(free->dirty_bitmap);
315
Avi Kivity6aa8b732006-12-10 02:21:36 -0800316 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000317 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500318 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800319}
320
321static void kvm_free_physmem(struct kvm *kvm)
322{
323 int i;
324
325 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000326 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800327}
328
Avi Kivity039576c2007-03-20 12:46:50 +0200329static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
330{
331 int i;
332
Rusty Russell3077c4512007-07-30 16:41:57 +1000333 for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i)
Avi Kivity039576c2007-03-20 12:46:50 +0200334 if (vcpu->pio.guest_pages[i]) {
Izik Eidus8a7ae052007-10-18 11:09:33 +0200335 kvm_release_page(vcpu->pio.guest_pages[i]);
Avi Kivity039576c2007-03-20 12:46:50 +0200336 vcpu->pio.guest_pages[i] = NULL;
337 }
338}
339
Avi Kivity7b53aa52007-06-05 12:17:03 +0300340static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
341{
Avi Kivity7b53aa52007-06-05 12:17:03 +0300342 vcpu_load(vcpu);
343 kvm_mmu_unload(vcpu);
344 vcpu_put(vcpu);
345}
346
Avi Kivity6aa8b732006-12-10 02:21:36 -0800347static void kvm_free_vcpus(struct kvm *kvm)
348{
349 unsigned int i;
350
Avi Kivity7b53aa52007-06-05 12:17:03 +0300351 /*
352 * Unpin any mmu pages first.
353 */
354 for (i = 0; i < KVM_MAX_VCPUS; ++i)
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000355 if (kvm->vcpus[i])
356 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
357 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
358 if (kvm->vcpus[i]) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300359 kvm_x86_ops->vcpu_free(kvm->vcpus[i]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000360 kvm->vcpus[i] = NULL;
361 }
362 }
363
Avi Kivity6aa8b732006-12-10 02:21:36 -0800364}
365
Avi Kivityf17abe92007-02-21 19:28:04 +0200366static void kvm_destroy_vm(struct kvm *kvm)
367{
Avi Kivity133de902007-02-12 00:54:44 -0800368 spin_lock(&kvm_lock);
369 list_del(&kvm->vm_list);
370 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300371 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400372 kvm_io_bus_destroy(&kvm->mmio_bus);
Eddie Dong85f455f2007-07-06 12:20:49 +0300373 kfree(kvm->vpic);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300374 kfree(kvm->vioapic);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800375 kvm_free_vcpus(kvm);
376 kvm_free_physmem(kvm);
377 kfree(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200378}
379
380static int kvm_vm_release(struct inode *inode, struct file *filp)
381{
382 struct kvm *kvm = filp->private_data;
383
384 kvm_destroy_vm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800385 return 0;
386}
387
388static void inject_gp(struct kvm_vcpu *vcpu)
389{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300390 kvm_x86_ops->inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800391}
392
Avi Kivity1342d352007-01-05 16:36:39 -0800393/*
394 * Load the pae pdptrs. Return true is they are all valid.
395 */
396static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800397{
398 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
Avi Kivity1342d352007-01-05 16:36:39 -0800399 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800400 int i;
Avi Kivity1342d352007-01-05 16:36:39 -0800401 int ret;
Rusty Russellc820c2a2007-07-25 13:29:51 +1000402 u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800403
Shaohua Li11ec2802007-07-23 14:51:37 +0800404 mutex_lock(&vcpu->kvm->lock);
Izik Eidus195aefd2007-10-01 22:14:18 +0200405 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
406 offset * sizeof(u64), sizeof(pdpte));
407 if (ret < 0) {
Rusty Russellc820c2a2007-07-25 13:29:51 +1000408 ret = 0;
409 goto out;
410 }
Rusty Russellc820c2a2007-07-25 13:29:51 +1000411 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
412 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
Avi Kivity1342d352007-01-05 16:36:39 -0800413 ret = 0;
414 goto out;
415 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800416 }
Rusty Russellc820c2a2007-07-25 13:29:51 +1000417 ret = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800418
Rusty Russellc820c2a2007-07-25 13:29:51 +1000419 memcpy(vcpu->pdptrs, pdpte, sizeof(vcpu->pdptrs));
Avi Kivity1342d352007-01-05 16:36:39 -0800420out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800421 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800422
Avi Kivity1342d352007-01-05 16:36:39 -0800423 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424}
425
426void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
427{
Rusty Russell707d92f2007-07-17 23:19:08 +1000428 if (cr0 & CR0_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800429 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
430 cr0, vcpu->cr0);
431 inject_gp(vcpu);
432 return;
433 }
434
Rusty Russell707d92f2007-07-17 23:19:08 +1000435 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800436 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
437 inject_gp(vcpu);
438 return;
439 }
440
Rusty Russell707d92f2007-07-17 23:19:08 +1000441 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800442 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
443 "and a clear PE flag\n");
444 inject_gp(vcpu);
445 return;
446 }
447
Rusty Russell707d92f2007-07-17 23:19:08 +1000448 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800449#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800450 if ((vcpu->shadow_efer & EFER_LME)) {
451 int cs_db, cs_l;
452
453 if (!is_pae(vcpu)) {
454 printk(KERN_DEBUG "set_cr0: #GP, start paging "
455 "in long mode while PAE is disabled\n");
456 inject_gp(vcpu);
457 return;
458 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300459 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800460 if (cs_l) {
461 printk(KERN_DEBUG "set_cr0: #GP, start paging "
462 "in long mode while CS.L == 1\n");
463 inject_gp(vcpu);
464 return;
465
466 }
467 } else
468#endif
Avi Kivity1342d352007-01-05 16:36:39 -0800469 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800470 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
471 "reserved bits\n");
472 inject_gp(vcpu);
473 return;
474 }
475
476 }
477
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300478 kvm_x86_ops->set_cr0(vcpu, cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800479 vcpu->cr0 = cr0;
480
Shaohua Li11ec2802007-07-23 14:51:37 +0800481 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800482 kvm_mmu_reset_context(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800483 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800484 return;
485}
486EXPORT_SYMBOL_GPL(set_cr0);
487
488void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
489{
490 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
491}
492EXPORT_SYMBOL_GPL(lmsw);
493
494void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
495{
Rusty Russell66aee912007-07-17 23:34:16 +1000496 if (cr4 & CR4_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800497 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
498 inject_gp(vcpu);
499 return;
500 }
501
Avi Kivitya9058ec2006-12-29 16:49:37 -0800502 if (is_long_mode(vcpu)) {
Rusty Russell66aee912007-07-17 23:34:16 +1000503 if (!(cr4 & X86_CR4_PAE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800504 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
505 "in long mode\n");
506 inject_gp(vcpu);
507 return;
508 }
Rusty Russell66aee912007-07-17 23:34:16 +1000509 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
Avi Kivity1342d352007-01-05 16:36:39 -0800510 && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800511 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
512 inject_gp(vcpu);
Rusty Russell310bc762007-07-23 17:11:02 +1000513 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800514 }
515
Rusty Russell66aee912007-07-17 23:34:16 +1000516 if (cr4 & X86_CR4_VMXE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800517 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
518 inject_gp(vcpu);
519 return;
520 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300521 kvm_x86_ops->set_cr4(vcpu, cr4);
Rusty Russell81f50e32007-09-06 01:20:38 +1000522 vcpu->cr4 = cr4;
Shaohua Li11ec2802007-07-23 14:51:37 +0800523 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800524 kvm_mmu_reset_context(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800525 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800526}
527EXPORT_SYMBOL_GPL(set_cr4);
528
529void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
530{
Avi Kivitya9058ec2006-12-29 16:49:37 -0800531 if (is_long_mode(vcpu)) {
Rusty Russellf802a302007-07-17 23:32:55 +1000532 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800533 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
534 inject_gp(vcpu);
535 return;
536 }
537 } else {
Rusty Russellf802a302007-07-17 23:32:55 +1000538 if (is_pae(vcpu)) {
539 if (cr3 & CR3_PAE_RESERVED_BITS) {
540 printk(KERN_DEBUG
541 "set_cr3: #GP, reserved bits\n");
542 inject_gp(vcpu);
543 return;
544 }
545 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
546 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
547 "reserved bits\n");
548 inject_gp(vcpu);
549 return;
550 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800551 }
Ryan Harper21764862007-09-18 14:05:16 -0500552 /*
553 * We don't check reserved bits in nonpae mode, because
554 * this isn't enforced, and VMware depends on this.
555 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800556 }
557
Shaohua Li11ec2802007-07-23 14:51:37 +0800558 mutex_lock(&vcpu->kvm->lock);
Ingo Molnard21225e2007-01-05 16:36:59 -0800559 /*
560 * Does the new cr3 value map to physical memory? (Note, we
561 * catch an invalid cr3 even in real-mode, because it would
562 * cause trouble later on when we turn on paging anyway.)
563 *
564 * A real CPU would silently accept an invalid cr3 and would
565 * attempt to use it - with largely undefined (and often hard
566 * to debug) behavior on the guest side.
567 */
568 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
569 inject_gp(vcpu);
Rusty Russellfb764412007-07-31 20:45:03 +1000570 else {
571 vcpu->cr3 = cr3;
Ingo Molnard21225e2007-01-05 16:36:59 -0800572 vcpu->mmu.new_cr3(vcpu);
Rusty Russellfb764412007-07-31 20:45:03 +1000573 }
Shaohua Li11ec2802007-07-23 14:51:37 +0800574 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800575}
576EXPORT_SYMBOL_GPL(set_cr3);
577
578void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
579{
Rusty Russell7075bc82007-07-17 23:37:17 +1000580 if (cr8 & CR8_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800581 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
582 inject_gp(vcpu);
583 return;
584 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300585 if (irqchip_in_kernel(vcpu->kvm))
586 kvm_lapic_set_tpr(vcpu, cr8);
587 else
588 vcpu->cr8 = cr8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800589}
590EXPORT_SYMBOL_GPL(set_cr8);
591
Eddie Dong7017fc32007-07-18 11:34:57 +0300592unsigned long get_cr8(struct kvm_vcpu *vcpu)
593{
Eddie Dong97222cc2007-09-12 10:58:04 +0300594 if (irqchip_in_kernel(vcpu->kvm))
595 return kvm_lapic_get_cr8(vcpu);
596 else
597 return vcpu->cr8;
Eddie Dong7017fc32007-07-18 11:34:57 +0300598}
599EXPORT_SYMBOL_GPL(get_cr8);
600
601u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
602{
Eddie Dong97222cc2007-09-12 10:58:04 +0300603 if (irqchip_in_kernel(vcpu->kvm))
604 return vcpu->apic_base;
605 else
606 return vcpu->apic_base;
Eddie Dong7017fc32007-07-18 11:34:57 +0300607}
608EXPORT_SYMBOL_GPL(kvm_get_apic_base);
609
610void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
611{
Eddie Dong97222cc2007-09-12 10:58:04 +0300612 /* TODO: reserve bits check */
613 if (irqchip_in_kernel(vcpu->kvm))
614 kvm_lapic_set_base(vcpu, data);
615 else
616 vcpu->apic_base = data;
Eddie Dong7017fc32007-07-18 11:34:57 +0300617}
618EXPORT_SYMBOL_GPL(kvm_set_apic_base);
619
Avi Kivity6aa8b732006-12-10 02:21:36 -0800620void fx_init(struct kvm_vcpu *vcpu)
621{
Rusty Russellb114b082007-07-30 21:13:43 +1000622 unsigned after_mxcsr_mask;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800623
Rusty Russell9bd01502007-07-30 16:29:56 +1000624 /* Initialize guest FPU by resetting ours and saving into guest's */
625 preempt_disable();
Rusty Russellb114b082007-07-30 21:13:43 +1000626 fx_save(&vcpu->host_fx_image);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800627 fpu_init();
Rusty Russellb114b082007-07-30 21:13:43 +1000628 fx_save(&vcpu->guest_fx_image);
629 fx_restore(&vcpu->host_fx_image);
Rusty Russell9bd01502007-07-30 16:29:56 +1000630 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800631
Amit Shah380102c2007-08-25 11:35:52 +0300632 vcpu->cr0 |= X86_CR0_ET;
Rusty Russellb114b082007-07-30 21:13:43 +1000633 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
634 vcpu->guest_fx_image.mxcsr = 0x1f80;
635 memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask,
636 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800637}
638EXPORT_SYMBOL_GPL(fx_init);
639
640/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800641 * Allocate some memory and give it an address in the guest physical address
642 * space.
643 *
644 * Discontiguous memory is allowed, mostly for framebuffers.
645 */
Izik Eidus210c7c42007-10-24 23:52:57 +0200646int kvm_set_memory_region(struct kvm *kvm,
647 struct kvm_userspace_memory_region *mem,
648 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800649{
650 int r;
651 gfn_t base_gfn;
652 unsigned long npages;
653 unsigned long i;
654 struct kvm_memory_slot *memslot;
655 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800656
657 r = -EINVAL;
658 /* General sanity checks */
659 if (mem->memory_size & (PAGE_SIZE - 1))
660 goto out;
661 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
662 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200663 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800664 goto out;
665 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
666 goto out;
667
668 memslot = &kvm->memslots[mem->slot];
669 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
670 npages = mem->memory_size >> PAGE_SHIFT;
671
672 if (!npages)
673 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
674
Shaohua Li11ec2802007-07-23 14:51:37 +0800675 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800676
Avi Kivity6aa8b732006-12-10 02:21:36 -0800677 new = old = *memslot;
678
679 new.base_gfn = base_gfn;
680 new.npages = npages;
681 new.flags = mem->flags;
682
683 /* Disallow changing a memory slot's size. */
684 r = -EINVAL;
685 if (npages && old.npages && npages != old.npages)
686 goto out_unlock;
687
688 /* Check for overlaps */
689 r = -EEXIST;
690 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
691 struct kvm_memory_slot *s = &kvm->memslots[i];
692
693 if (s == memslot)
694 continue;
695 if (!((base_gfn + npages <= s->base_gfn) ||
696 (base_gfn >= s->base_gfn + s->npages)))
697 goto out_unlock;
698 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800699
Avi Kivity6aa8b732006-12-10 02:21:36 -0800700 /* Free page dirty bitmap if unneeded */
701 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000702 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800703
704 r = -ENOMEM;
705
706 /* Allocate if a slot is being created */
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500707 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400708 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200709
710 if (!new.rmap)
711 goto out_unlock;
712
Izik Eidus290fc382007-09-27 14:11:22 +0200713 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500714
Izik Eidus80b14b52007-10-25 11:54:04 +0200715 new.user_alloc = user_alloc;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500716 if (user_alloc)
Izik Eidus8a7ae052007-10-18 11:09:33 +0200717 new.userspace_addr = mem->userspace_addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500718 else {
719 down_write(&current->mm->mmap_sem);
720 new.userspace_addr = do_mmap(NULL, 0,
721 npages * PAGE_SIZE,
722 PROT_READ | PROT_WRITE,
723 MAP_SHARED | MAP_ANONYMOUS,
724 0);
725 up_write(&current->mm->mmap_sem);
726
727 if (IS_ERR((void *)new.userspace_addr))
728 goto out_unlock;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800729 }
Izik Eidus80b14b52007-10-25 11:54:04 +0200730 } else {
731 if (!old.user_alloc && old.rmap) {
732 int ret;
733
734 down_write(&current->mm->mmap_sem);
735 ret = do_munmap(current->mm, old.userspace_addr,
736 old.npages * PAGE_SIZE);
737 up_write(&current->mm->mmap_sem);
738 if (ret < 0)
739 printk(KERN_WARNING
740 "kvm_vm_ioctl_set_memory_region: "
741 "failed to munmap memory\n");
742 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800743 }
744
745 /* Allocate page dirty bitmap if needed */
746 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
747 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
748
749 new.dirty_bitmap = vmalloc(dirty_bytes);
750 if (!new.dirty_bitmap)
Laurent Vivier0d8d2bd2007-08-30 14:56:21 +0200751 goto out_unlock;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800752 memset(new.dirty_bitmap, 0, dirty_bytes);
753 }
754
Avi Kivity6aa8b732006-12-10 02:21:36 -0800755 if (mem->slot >= kvm->nmemslots)
756 kvm->nmemslots = mem->slot + 1;
757
Izik Eidus82ce2c92007-10-02 18:52:55 +0200758 if (!kvm->n_requested_mmu_pages) {
759 unsigned int n_pages;
760
761 if (npages) {
762 n_pages = npages * KVM_PERMILLE_MMU_PAGES / 1000;
763 kvm_mmu_change_mmu_pages(kvm, kvm->n_alloc_mmu_pages +
764 n_pages);
765 } else {
766 unsigned int nr_mmu_pages;
767
768 n_pages = old.npages * KVM_PERMILLE_MMU_PAGES / 1000;
769 nr_mmu_pages = kvm->n_alloc_mmu_pages - n_pages;
770 nr_mmu_pages = max(nr_mmu_pages,
771 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
772 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
773 }
774 }
775
Avi Kivity6aa8b732006-12-10 02:21:36 -0800776 *memslot = new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777
Avi Kivity90cb0522007-07-17 13:04:56 +0300778 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
779 kvm_flush_remote_tlbs(kvm);
780
Shaohua Li11ec2802007-07-23 14:51:37 +0800781 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800782
Avi Kivity6aa8b732006-12-10 02:21:36 -0800783 kvm_free_physmem_slot(&old, &new);
784 return 0;
785
786out_unlock:
Shaohua Li11ec2802007-07-23 14:51:37 +0800787 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800788 kvm_free_physmem_slot(&new, &old);
789out:
790 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200791
792}
793EXPORT_SYMBOL_GPL(kvm_set_memory_region);
794
795static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
796 struct
797 kvm_userspace_memory_region *mem,
798 int user_alloc)
799{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200800 if (mem->slot >= KVM_MEMORY_SLOTS)
801 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200802 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800803}
804
Izik Eidus82ce2c92007-10-02 18:52:55 +0200805static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
806 u32 kvm_nr_mmu_pages)
807{
808 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
809 return -EINVAL;
810
811 mutex_lock(&kvm->lock);
812
813 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
814 kvm->n_requested_mmu_pages = kvm_nr_mmu_pages;
815
816 mutex_unlock(&kvm->lock);
817 return 0;
818}
819
820static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
821{
822 return kvm->n_alloc_mmu_pages;
823}
824
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825/*
826 * Get (and clear) the dirty memory log for a memory slot.
827 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200828static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
829 struct kvm_dirty_log *log)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800830{
831 struct kvm_memory_slot *memslot;
832 int r, i;
833 int n;
834 unsigned long any = 0;
835
Shaohua Li11ec2802007-07-23 14:51:37 +0800836 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800837
Avi Kivity6aa8b732006-12-10 02:21:36 -0800838 r = -EINVAL;
839 if (log->slot >= KVM_MEMORY_SLOTS)
840 goto out;
841
842 memslot = &kvm->memslots[log->slot];
843 r = -ENOENT;
844 if (!memslot->dirty_bitmap)
845 goto out;
846
Uri Lublincd1a4a92007-02-22 16:43:09 +0200847 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800848
Uri Lublincd1a4a92007-02-22 16:43:09 +0200849 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800850 any = memslot->dirty_bitmap[i];
851
852 r = -EFAULT;
853 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
854 goto out;
855
Rusty Russell39214912007-07-31 19:57:47 +1000856 /* If nothing is dirty, don't bother messing with page tables. */
857 if (any) {
Rusty Russell39214912007-07-31 19:57:47 +1000858 kvm_mmu_slot_remove_write_access(kvm, log->slot);
859 kvm_flush_remote_tlbs(kvm);
860 memset(memslot->dirty_bitmap, 0, n);
Rusty Russell39214912007-07-31 19:57:47 +1000861 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800862
863 r = 0;
864
865out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800866 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800867 return r;
868}
869
Avi Kivitye8207542007-03-30 16:54:30 +0300870/*
871 * Set a new alias region. Aliases map a portion of physical memory into
872 * another portion. This is useful for memory windows, for example the PC
873 * VGA region.
874 */
875static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
876 struct kvm_memory_alias *alias)
877{
878 int r, n;
879 struct kvm_mem_alias *p;
880
881 r = -EINVAL;
882 /* General sanity checks */
883 if (alias->memory_size & (PAGE_SIZE - 1))
884 goto out;
885 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
886 goto out;
887 if (alias->slot >= KVM_ALIAS_SLOTS)
888 goto out;
889 if (alias->guest_phys_addr + alias->memory_size
890 < alias->guest_phys_addr)
891 goto out;
892 if (alias->target_phys_addr + alias->memory_size
893 < alias->target_phys_addr)
894 goto out;
895
Shaohua Li11ec2802007-07-23 14:51:37 +0800896 mutex_lock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300897
898 p = &kvm->aliases[alias->slot];
899 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
900 p->npages = alias->memory_size >> PAGE_SHIFT;
901 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
902
903 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
904 if (kvm->aliases[n - 1].npages)
905 break;
906 kvm->naliases = n;
907
Avi Kivity90cb0522007-07-17 13:04:56 +0300908 kvm_mmu_zap_all(kvm);
Avi Kivitye8207542007-03-30 16:54:30 +0300909
Shaohua Li11ec2802007-07-23 14:51:37 +0800910 mutex_unlock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300911
912 return 0;
913
914out:
915 return r;
916}
917
He, Qing6ceb9d72007-07-26 11:05:18 +0300918static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
919{
920 int r;
921
922 r = 0;
923 switch (chip->chip_id) {
924 case KVM_IRQCHIP_PIC_MASTER:
Mike Dayd77c26f2007-10-08 09:02:08 -0400925 memcpy(&chip->chip.pic,
He, Qing6ceb9d72007-07-26 11:05:18 +0300926 &pic_irqchip(kvm)->pics[0],
927 sizeof(struct kvm_pic_state));
928 break;
929 case KVM_IRQCHIP_PIC_SLAVE:
Mike Dayd77c26f2007-10-08 09:02:08 -0400930 memcpy(&chip->chip.pic,
He, Qing6ceb9d72007-07-26 11:05:18 +0300931 &pic_irqchip(kvm)->pics[1],
932 sizeof(struct kvm_pic_state));
933 break;
He, Qing6bf9e962007-08-05 10:49:16 +0300934 case KVM_IRQCHIP_IOAPIC:
Mike Dayd77c26f2007-10-08 09:02:08 -0400935 memcpy(&chip->chip.ioapic,
He, Qing6bf9e962007-08-05 10:49:16 +0300936 ioapic_irqchip(kvm),
937 sizeof(struct kvm_ioapic_state));
938 break;
He, Qing6ceb9d72007-07-26 11:05:18 +0300939 default:
940 r = -EINVAL;
941 break;
942 }
943 return r;
944}
945
946static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
947{
948 int r;
949
950 r = 0;
951 switch (chip->chip_id) {
952 case KVM_IRQCHIP_PIC_MASTER:
Mike Dayd77c26f2007-10-08 09:02:08 -0400953 memcpy(&pic_irqchip(kvm)->pics[0],
He, Qing6ceb9d72007-07-26 11:05:18 +0300954 &chip->chip.pic,
955 sizeof(struct kvm_pic_state));
956 break;
957 case KVM_IRQCHIP_PIC_SLAVE:
Mike Dayd77c26f2007-10-08 09:02:08 -0400958 memcpy(&pic_irqchip(kvm)->pics[1],
He, Qing6ceb9d72007-07-26 11:05:18 +0300959 &chip->chip.pic,
960 sizeof(struct kvm_pic_state));
961 break;
He, Qing6bf9e962007-08-05 10:49:16 +0300962 case KVM_IRQCHIP_IOAPIC:
Mike Dayd77c26f2007-10-08 09:02:08 -0400963 memcpy(ioapic_irqchip(kvm),
He, Qing6bf9e962007-08-05 10:49:16 +0300964 &chip->chip.ioapic,
965 sizeof(struct kvm_ioapic_state));
966 break;
He, Qing6ceb9d72007-07-26 11:05:18 +0300967 default:
968 r = -EINVAL;
969 break;
970 }
971 kvm_pic_update_irq(pic_irqchip(kvm));
972 return r;
973}
974
Izik Eiduscea7bb22007-10-17 19:17:48 +0200975int is_error_page(struct page *page)
976{
977 return page == bad_page;
978}
979EXPORT_SYMBOL_GPL(is_error_page);
980
Izik Eidus290fc382007-09-27 14:11:22 +0200981gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
Avi Kivitye8207542007-03-30 16:54:30 +0300982{
983 int i;
984 struct kvm_mem_alias *alias;
985
986 for (i = 0; i < kvm->naliases; ++i) {
987 alias = &kvm->aliases[i];
988 if (gfn >= alias->base_gfn
989 && gfn < alias->base_gfn + alias->npages)
990 return alias->target_gfn + gfn - alias->base_gfn;
991 }
992 return gfn;
993}
994
995static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800996{
997 int i;
998
999 for (i = 0; i < kvm->nmemslots; ++i) {
1000 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1001
1002 if (gfn >= memslot->base_gfn
1003 && gfn < memslot->base_gfn + memslot->npages)
1004 return memslot;
1005 }
Al Viro8b6d44c2007-02-09 16:38:40 +00001006 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001007}
Avi Kivitye8207542007-03-30 16:54:30 +03001008
1009struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1010{
1011 gfn = unalias_gfn(kvm, gfn);
1012 return __gfn_to_memslot(kvm, gfn);
1013}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014
Izik Eiduse0d62c72007-10-24 23:57:46 +02001015int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
1016{
1017 int i;
1018
1019 gfn = unalias_gfn(kvm, gfn);
1020 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1021 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1022
1023 if (gfn >= memslot->base_gfn
1024 && gfn < memslot->base_gfn + memslot->npages)
1025 return 1;
1026 }
1027 return 0;
1028}
1029EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1030
Avi Kivity954bbbc22007-03-30 14:02:32 +03001031struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1032{
1033 struct kvm_memory_slot *slot;
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001034 struct page *page[1];
1035 int npages;
Avi Kivity954bbbc22007-03-30 14:02:32 +03001036
Avi Kivity60395222007-10-21 11:03:36 +02001037 might_sleep();
1038
Avi Kivitye8207542007-03-30 16:54:30 +03001039 gfn = unalias_gfn(kvm, gfn);
1040 slot = __gfn_to_memslot(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001041 if (!slot) {
1042 get_page(bad_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +02001043 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001044 }
Izik Eidus8a7ae052007-10-18 11:09:33 +02001045
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001046 down_read(&current->mm->mmap_sem);
1047 npages = get_user_pages(current, current->mm,
1048 slot->userspace_addr
1049 + (gfn - slot->base_gfn) * PAGE_SIZE, 1,
1050 1, 1, page, NULL);
1051 up_read(&current->mm->mmap_sem);
1052 if (npages != 1) {
1053 get_page(bad_page);
1054 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001055 }
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001056
1057 return page[0];
Avi Kivity954bbbc22007-03-30 14:02:32 +03001058}
1059EXPORT_SYMBOL_GPL(gfn_to_page);
1060
Izik Eidus8a7ae052007-10-18 11:09:33 +02001061void kvm_release_page(struct page *page)
1062{
1063 if (!PageReserved(page))
1064 SetPageDirty(page);
1065 put_page(page);
1066}
1067EXPORT_SYMBOL_GPL(kvm_release_page);
1068
Izik Eidus195aefd2007-10-01 22:14:18 +02001069static int next_segment(unsigned long len, int offset)
1070{
1071 if (len > PAGE_SIZE - offset)
1072 return PAGE_SIZE - offset;
1073 else
1074 return len;
1075}
1076
1077int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1078 int len)
1079{
1080 void *page_virt;
1081 struct page *page;
1082
1083 page = gfn_to_page(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001084 if (is_error_page(page)) {
1085 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001086 return -EFAULT;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001087 }
Izik Eidus195aefd2007-10-01 22:14:18 +02001088 page_virt = kmap_atomic(page, KM_USER0);
1089
1090 memcpy(data, page_virt + offset, len);
1091
1092 kunmap_atomic(page_virt, KM_USER0);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001093 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001094 return 0;
1095}
1096EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1097
1098int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1099{
1100 gfn_t gfn = gpa >> PAGE_SHIFT;
1101 int seg;
1102 int offset = offset_in_page(gpa);
1103 int ret;
1104
1105 while ((seg = next_segment(len, offset)) != 0) {
1106 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1107 if (ret < 0)
1108 return ret;
1109 offset = 0;
1110 len -= seg;
1111 data += seg;
1112 ++gfn;
1113 }
1114 return 0;
1115}
1116EXPORT_SYMBOL_GPL(kvm_read_guest);
1117
1118int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1119 int offset, int len)
1120{
1121 void *page_virt;
1122 struct page *page;
1123
1124 page = gfn_to_page(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001125 if (is_error_page(page)) {
1126 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001127 return -EFAULT;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001128 }
Izik Eidus195aefd2007-10-01 22:14:18 +02001129 page_virt = kmap_atomic(page, KM_USER0);
1130
1131 memcpy(page_virt + offset, data, len);
1132
1133 kunmap_atomic(page_virt, KM_USER0);
1134 mark_page_dirty(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001135 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001136 return 0;
1137}
1138EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1139
1140int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1141 unsigned long len)
1142{
1143 gfn_t gfn = gpa >> PAGE_SHIFT;
1144 int seg;
1145 int offset = offset_in_page(gpa);
1146 int ret;
1147
1148 while ((seg = next_segment(len, offset)) != 0) {
1149 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1150 if (ret < 0)
1151 return ret;
1152 offset = 0;
1153 len -= seg;
1154 data += seg;
1155 ++gfn;
1156 }
1157 return 0;
1158}
1159
1160int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1161{
1162 void *page_virt;
1163 struct page *page;
1164
1165 page = gfn_to_page(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001166 if (is_error_page(page)) {
1167 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001168 return -EFAULT;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001169 }
Izik Eidus195aefd2007-10-01 22:14:18 +02001170 page_virt = kmap_atomic(page, KM_USER0);
1171
1172 memset(page_virt + offset, 0, len);
1173
1174 kunmap_atomic(page_virt, KM_USER0);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001175 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001176 return 0;
1177}
1178EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1179
1180int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1181{
1182 gfn_t gfn = gpa >> PAGE_SHIFT;
1183 int seg;
1184 int offset = offset_in_page(gpa);
1185 int ret;
1186
1187 while ((seg = next_segment(len, offset)) != 0) {
1188 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1189 if (ret < 0)
1190 return ret;
1191 offset = 0;
1192 len -= seg;
1193 ++gfn;
1194 }
1195 return 0;
1196}
1197EXPORT_SYMBOL_GPL(kvm_clear_guest);
1198
Rusty Russell7e9d6192007-07-31 20:41:14 +10001199/* WARNING: Does not work on aliased pages. */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001200void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1201{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001202 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001203
Rusty Russell7e9d6192007-07-31 20:41:14 +10001204 memslot = __gfn_to_memslot(kvm, gfn);
1205 if (memslot && memslot->dirty_bitmap) {
1206 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001207
Rusty Russell7e9d6192007-07-31 20:41:14 +10001208 /* avoid RMW */
1209 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1210 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001211 }
1212}
1213
Laurent Viviere7d5d762007-07-30 13:41:19 +03001214int emulator_read_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001215 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001216 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001217 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001218{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001219 void *data = val;
1220
1221 while (bytes) {
1222 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1223 unsigned offset = addr & (PAGE_SIZE-1);
1224 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
Izik Eidus195aefd2007-10-01 22:14:18 +02001225 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001226
1227 if (gpa == UNMAPPED_GVA)
1228 return X86EMUL_PROPAGATE_FAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001229 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
1230 if (ret < 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001231 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001232
1233 bytes -= tocopy;
1234 data += tocopy;
1235 addr += tocopy;
1236 }
1237
1238 return X86EMUL_CONTINUE;
1239}
Laurent Viviere7d5d762007-07-30 13:41:19 +03001240EXPORT_SYMBOL_GPL(emulator_read_std);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001241
1242static int emulator_write_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001243 const void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001244 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001245 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001246{
Rusty Russellf0242472007-08-01 10:48:02 +10001247 pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001248 return X86EMUL_UNHANDLEABLE;
1249}
1250
Eddie Dong97222cc2007-09-12 10:58:04 +03001251/*
1252 * Only apic need an MMIO device hook, so shortcut now..
1253 */
1254static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
1255 gpa_t addr)
1256{
1257 struct kvm_io_device *dev;
1258
1259 if (vcpu->apic) {
1260 dev = &vcpu->apic->dev;
1261 if (dev->in_range(dev, addr))
1262 return dev;
1263 }
1264 return NULL;
1265}
1266
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001267static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1268 gpa_t addr)
1269{
Eddie Dong97222cc2007-09-12 10:58:04 +03001270 struct kvm_io_device *dev;
1271
1272 dev = vcpu_find_pervcpu_dev(vcpu, addr);
1273 if (dev == NULL)
1274 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1275 return dev;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001276}
1277
Eddie Dong74906342007-06-19 18:05:03 +03001278static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
1279 gpa_t addr)
1280{
1281 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
1282}
1283
Avi Kivity6aa8b732006-12-10 02:21:36 -08001284static int emulator_read_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001285 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001286 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001287 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001288{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001289 struct kvm_io_device *mmio_dev;
1290 gpa_t gpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001291
1292 if (vcpu->mmio_read_completed) {
1293 memcpy(val, vcpu->mmio_data, bytes);
1294 vcpu->mmio_read_completed = 0;
1295 return X86EMUL_CONTINUE;
Laurent Viviercebff022007-07-30 13:35:24 +03001296 } else if (emulator_read_std(addr, val, bytes, vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001297 == X86EMUL_CONTINUE)
1298 return X86EMUL_CONTINUE;
Avi Kivityd27d4ac2007-02-19 14:37:46 +02001299
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001300 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1301 if (gpa == UNMAPPED_GVA)
1302 return X86EMUL_PROPAGATE_FAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001303
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001304 /*
1305 * Is this MMIO handled locally?
1306 */
1307 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1308 if (mmio_dev) {
1309 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1310 return X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001311 }
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001312
1313 vcpu->mmio_needed = 1;
1314 vcpu->mmio_phys_addr = gpa;
1315 vcpu->mmio_size = bytes;
1316 vcpu->mmio_is_write = 0;
1317
1318 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001319}
1320
Avi Kivityda4a00f2007-01-05 16:36:44 -08001321static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity4c690a12007-04-22 15:28:19 +03001322 const void *val, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001323{
Izik Eidus195aefd2007-10-01 22:14:18 +02001324 int ret;
Avi Kivityda4a00f2007-01-05 16:36:44 -08001325
Izik Eidus195aefd2007-10-01 22:14:18 +02001326 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
1327 if (ret < 0)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001328 return 0;
Shaohua Life551882007-07-23 14:51:39 +08001329 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001330 return 1;
1331}
1332
Avi Kivityb0fcd902007-07-22 18:48:54 +03001333static int emulator_write_emulated_onepage(unsigned long addr,
1334 const void *val,
1335 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001336 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001337{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001338 struct kvm_io_device *mmio_dev;
1339 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001340
Avi Kivityc9047f52007-04-17 10:53:22 +03001341 if (gpa == UNMAPPED_GVA) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001342 kvm_x86_ops->inject_page_fault(vcpu, addr, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001343 return X86EMUL_PROPAGATE_FAULT;
Avi Kivityc9047f52007-04-17 10:53:22 +03001344 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001345
Avi Kivityda4a00f2007-01-05 16:36:44 -08001346 if (emulator_write_phys(vcpu, gpa, val, bytes))
1347 return X86EMUL_CONTINUE;
1348
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001349 /*
1350 * Is this MMIO handled locally?
1351 */
1352 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1353 if (mmio_dev) {
1354 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1355 return X86EMUL_CONTINUE;
1356 }
1357
Avi Kivity6aa8b732006-12-10 02:21:36 -08001358 vcpu->mmio_needed = 1;
1359 vcpu->mmio_phys_addr = gpa;
1360 vcpu->mmio_size = bytes;
1361 vcpu->mmio_is_write = 1;
Avi Kivity4c690a12007-04-22 15:28:19 +03001362 memcpy(vcpu->mmio_data, val, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001363
1364 return X86EMUL_CONTINUE;
1365}
1366
Laurent Viviere7d5d762007-07-30 13:41:19 +03001367int emulator_write_emulated(unsigned long addr,
Avi Kivityb0fcd902007-07-22 18:48:54 +03001368 const void *val,
1369 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001370 struct kvm_vcpu *vcpu)
Avi Kivityb0fcd902007-07-22 18:48:54 +03001371{
1372 /* Crossing a page boundary? */
1373 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1374 int rc, now;
1375
1376 now = -addr & ~PAGE_MASK;
Laurent Viviercebff022007-07-30 13:35:24 +03001377 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001378 if (rc != X86EMUL_CONTINUE)
1379 return rc;
1380 addr += now;
1381 val += now;
1382 bytes -= now;
1383 }
Laurent Viviercebff022007-07-30 13:35:24 +03001384 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001385}
Laurent Viviere7d5d762007-07-30 13:41:19 +03001386EXPORT_SYMBOL_GPL(emulator_write_emulated);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001387
Avi Kivity6aa8b732006-12-10 02:21:36 -08001388static int emulator_cmpxchg_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001389 const void *old,
1390 const void *new,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001391 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001392 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001393{
1394 static int reported;
1395
1396 if (!reported) {
1397 reported = 1;
1398 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1399 }
Laurent Viviercebff022007-07-30 13:35:24 +03001400 return emulator_write_emulated(addr, new, bytes, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001401}
1402
1403static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1404{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001405 return kvm_x86_ops->get_segment_base(vcpu, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001406}
1407
1408int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1409{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001410 return X86EMUL_CONTINUE;
1411}
1412
1413int emulate_clts(struct kvm_vcpu *vcpu)
1414{
Amit Shah404fb882007-11-19 17:57:35 +02001415 kvm_x86_ops->set_cr0(vcpu, vcpu->cr0 & ~X86_CR0_TS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001416 return X86EMUL_CONTINUE;
1417}
1418
Mike Dayd77c26f2007-10-08 09:02:08 -04001419int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001420{
1421 struct kvm_vcpu *vcpu = ctxt->vcpu;
1422
1423 switch (dr) {
1424 case 0 ... 3:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001425 *dest = kvm_x86_ops->get_dr(vcpu, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001426 return X86EMUL_CONTINUE;
1427 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001428 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429 return X86EMUL_UNHANDLEABLE;
1430 }
1431}
1432
1433int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1434{
1435 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1436 int exception;
1437
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001438 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001439 if (exception) {
1440 /* FIXME: better handling */
1441 return X86EMUL_UNHANDLEABLE;
1442 }
1443 return X86EMUL_CONTINUE;
1444}
1445
Avi Kivity054b1362007-09-12 13:21:09 +03001446void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001447{
1448 static int reported;
1449 u8 opcodes[4];
Avi Kivity054b1362007-09-12 13:21:09 +03001450 unsigned long rip = vcpu->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001451 unsigned long rip_linear;
1452
Avi Kivity054b1362007-09-12 13:21:09 +03001453 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001454
1455 if (reported)
1456 return;
1457
Avi Kivity054b1362007-09-12 13:21:09 +03001458 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001459
Avi Kivity054b1362007-09-12 13:21:09 +03001460 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1461 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001462 reported = 1;
1463}
Avi Kivity054b1362007-09-12 13:21:09 +03001464EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001465
1466struct x86_emulate_ops emulate_ops = {
1467 .read_std = emulator_read_std,
1468 .write_std = emulator_write_std,
1469 .read_emulated = emulator_read_emulated,
1470 .write_emulated = emulator_write_emulated,
1471 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1472};
1473
1474int emulate_instruction(struct kvm_vcpu *vcpu,
1475 struct kvm_run *run,
1476 unsigned long cr2,
Laurent Vivier34273182007-09-18 11:27:37 +02001477 u16 error_code,
1478 int no_decode)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001479{
Laurent Viviera22436b2007-09-24 17:00:58 +02001480 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001481
Avi Kivitye7df56e2007-03-14 15:54:54 +02001482 vcpu->mmio_fault_cr2 = cr2;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001483 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001484
Avi Kivity6aa8b732006-12-10 02:21:36 -08001485 vcpu->mmio_is_write = 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03001486 vcpu->pio.string = 0;
Laurent Vivier34273182007-09-18 11:27:37 +02001487
1488 if (!no_decode) {
1489 int cs_db, cs_l;
1490 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1491
1492 vcpu->emulate_ctxt.vcpu = vcpu;
1493 vcpu->emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
1494 vcpu->emulate_ctxt.cr2 = cr2;
1495 vcpu->emulate_ctxt.mode =
1496 (vcpu->emulate_ctxt.eflags & X86_EFLAGS_VM)
1497 ? X86EMUL_MODE_REAL : cs_l
1498 ? X86EMUL_MODE_PROT64 : cs_db
1499 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1500
1501 if (vcpu->emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1502 vcpu->emulate_ctxt.cs_base = 0;
1503 vcpu->emulate_ctxt.ds_base = 0;
1504 vcpu->emulate_ctxt.es_base = 0;
1505 vcpu->emulate_ctxt.ss_base = 0;
1506 } else {
1507 vcpu->emulate_ctxt.cs_base =
1508 get_segment_base(vcpu, VCPU_SREG_CS);
1509 vcpu->emulate_ctxt.ds_base =
1510 get_segment_base(vcpu, VCPU_SREG_DS);
1511 vcpu->emulate_ctxt.es_base =
1512 get_segment_base(vcpu, VCPU_SREG_ES);
1513 vcpu->emulate_ctxt.ss_base =
1514 get_segment_base(vcpu, VCPU_SREG_SS);
1515 }
1516
1517 vcpu->emulate_ctxt.gs_base =
1518 get_segment_base(vcpu, VCPU_SREG_GS);
1519 vcpu->emulate_ctxt.fs_base =
1520 get_segment_base(vcpu, VCPU_SREG_FS);
1521
1522 r = x86_decode_insn(&vcpu->emulate_ctxt, &emulate_ops);
Laurent Viviera22436b2007-09-24 17:00:58 +02001523 if (r) {
1524 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1525 return EMULATE_DONE;
1526 return EMULATE_FAIL;
1527 }
Laurent Vivier34273182007-09-18 11:27:37 +02001528 }
1529
Laurent Viviera22436b2007-09-24 17:00:58 +02001530 r = x86_emulate_insn(&vcpu->emulate_ctxt, &emulate_ops);
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001531
Laurent Viviere70669a2007-08-05 10:36:40 +03001532 if (vcpu->pio.string)
1533 return EMULATE_DO_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001534
1535 if ((r || vcpu->mmio_is_write) && run) {
Jeff Dike8fc0d082007-07-17 12:26:59 -04001536 run->exit_reason = KVM_EXIT_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001537 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1538 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1539 run->mmio.len = vcpu->mmio_size;
1540 run->mmio.is_write = vcpu->mmio_is_write;
1541 }
1542
1543 if (r) {
Avi Kivitya4360362007-01-05 16:36:45 -08001544 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1545 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001546 if (!vcpu->mmio_needed) {
Avi Kivity054b1362007-09-12 13:21:09 +03001547 kvm_report_emulation_failure(vcpu, "mmio");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001548 return EMULATE_FAIL;
1549 }
1550 return EMULATE_DO_MMIO;
1551 }
1552
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001553 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier34273182007-09-18 11:27:37 +02001554 kvm_x86_ops->set_rflags(vcpu, vcpu->emulate_ctxt.eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001555
Avi Kivity02c83202007-04-29 15:02:17 +03001556 if (vcpu->mmio_is_write) {
1557 vcpu->mmio_needed = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001558 return EMULATE_DO_MMIO;
Avi Kivity02c83202007-04-29 15:02:17 +03001559 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001560
1561 return EMULATE_DONE;
1562}
1563EXPORT_SYMBOL_GPL(emulate_instruction);
1564
Eddie Dongb6958ce2007-07-18 12:15:21 +03001565/*
1566 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1567 */
He, Qingc5ec1532007-09-03 17:07:41 +03001568static void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001569{
1570 DECLARE_WAITQUEUE(wait, current);
1571
1572 add_wait_queue(&vcpu->wq, &wait);
1573
1574 /*
1575 * We will block until either an interrupt or a signal wakes us up
1576 */
He, Qingc5ec1532007-09-03 17:07:41 +03001577 while (!kvm_cpu_has_interrupt(vcpu)
1578 && !signal_pending(current)
1579 && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE
1580 && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) {
Eddie Dongb6958ce2007-07-18 12:15:21 +03001581 set_current_state(TASK_INTERRUPTIBLE);
1582 vcpu_put(vcpu);
1583 schedule();
1584 vcpu_load(vcpu);
1585 }
1586
He, Qingc5ec1532007-09-03 17:07:41 +03001587 __set_current_state(TASK_RUNNING);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001588 remove_wait_queue(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001589}
1590
Avi Kivityd3bef152007-06-05 15:53:05 +03001591int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1592{
Avi Kivityd3bef152007-06-05 15:53:05 +03001593 ++vcpu->stat.halt_exits;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001594 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc5ec1532007-09-03 17:07:41 +03001595 vcpu->mp_state = VCPU_MP_STATE_HALTED;
1596 kvm_vcpu_block(vcpu);
1597 if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE)
1598 return -EINTR;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001599 return 1;
1600 } else {
1601 vcpu->run->exit_reason = KVM_EXIT_HLT;
1602 return 0;
1603 }
Avi Kivityd3bef152007-06-05 15:53:05 +03001604}
1605EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1606
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001607int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
Avi Kivity270fd9b2007-02-19 14:37:47 +02001608{
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001609 unsigned long nr, a0, a1, a2, a3, ret;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001610
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001611 kvm_x86_ops->cache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001612
1613 nr = vcpu->regs[VCPU_REGS_RAX];
1614 a0 = vcpu->regs[VCPU_REGS_RBX];
1615 a1 = vcpu->regs[VCPU_REGS_RCX];
1616 a2 = vcpu->regs[VCPU_REGS_RDX];
1617 a3 = vcpu->regs[VCPU_REGS_RSI];
1618
1619 if (!is_long_mode(vcpu)) {
1620 nr &= 0xFFFFFFFF;
1621 a0 &= 0xFFFFFFFF;
1622 a1 &= 0xFFFFFFFF;
1623 a2 &= 0xFFFFFFFF;
1624 a3 &= 0xFFFFFFFF;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001625 }
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001626
Avi Kivity270fd9b2007-02-19 14:37:47 +02001627 switch (nr) {
1628 default:
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001629 ret = -KVM_ENOSYS;
1630 break;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001631 }
1632 vcpu->regs[VCPU_REGS_RAX] = ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001633 kvm_x86_ops->decache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001634 return 0;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001635}
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001636EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
1637
1638int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
1639{
1640 char instruction[3];
1641 int ret = 0;
1642
1643 mutex_lock(&vcpu->kvm->lock);
1644
1645 /*
1646 * Blow out the MMU to ensure that no other VCPU has an active mapping
1647 * to ensure that the updated hypercall appears atomically across all
1648 * VCPUs.
1649 */
1650 kvm_mmu_zap_all(vcpu->kvm);
1651
1652 kvm_x86_ops->cache_regs(vcpu);
1653 kvm_x86_ops->patch_hypercall(vcpu, instruction);
1654 if (emulator_write_emulated(vcpu->rip, instruction, 3, vcpu)
1655 != X86EMUL_CONTINUE)
1656 ret = -EFAULT;
1657
1658 mutex_unlock(&vcpu->kvm->lock);
1659
1660 return ret;
1661}
Avi Kivity270fd9b2007-02-19 14:37:47 +02001662
Avi Kivity6aa8b732006-12-10 02:21:36 -08001663static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1664{
1665 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1666}
1667
1668void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1669{
1670 struct descriptor_table dt = { limit, base };
1671
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001672 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001673}
1674
1675void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1676{
1677 struct descriptor_table dt = { limit, base };
1678
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001679 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001680}
1681
1682void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1683 unsigned long *rflags)
1684{
1685 lmsw(vcpu, msw);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001686 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001687}
1688
1689unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1690{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001691 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001692 switch (cr) {
1693 case 0:
1694 return vcpu->cr0;
1695 case 2:
1696 return vcpu->cr2;
1697 case 3:
1698 return vcpu->cr3;
1699 case 4:
1700 return vcpu->cr4;
1701 default:
1702 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1703 return 0;
1704 }
1705}
1706
1707void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1708 unsigned long *rflags)
1709{
1710 switch (cr) {
1711 case 0:
1712 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001713 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001714 break;
1715 case 2:
1716 vcpu->cr2 = val;
1717 break;
1718 case 3:
1719 set_cr3(vcpu, val);
1720 break;
1721 case 4:
1722 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1723 break;
1724 default:
1725 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1726 }
1727}
1728
Avi Kivity3bab1f52006-12-29 16:49:48 -08001729int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1730{
1731 u64 data;
1732
1733 switch (msr) {
1734 case 0xc0010010: /* SYSCFG */
1735 case 0xc0010015: /* HWCR */
1736 case MSR_IA32_PLATFORM_ID:
1737 case MSR_IA32_P5_MC_ADDR:
1738 case MSR_IA32_P5_MC_TYPE:
1739 case MSR_IA32_MC0_CTL:
1740 case MSR_IA32_MCG_STATUS:
1741 case MSR_IA32_MCG_CAP:
1742 case MSR_IA32_MC0_MISC:
1743 case MSR_IA32_MC0_MISC+4:
1744 case MSR_IA32_MC0_MISC+8:
1745 case MSR_IA32_MC0_MISC+12:
1746 case MSR_IA32_MC0_MISC+16:
1747 case MSR_IA32_UCODE_REV:
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001748 case MSR_IA32_PERF_STATUS:
Matthew Gregan2dc70942007-05-06 10:59:46 +03001749 case MSR_IA32_EBL_CR_POWERON:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001750 /* MTRR registers */
1751 case 0xfe:
1752 case 0x200 ... 0x2ff:
1753 data = 0;
1754 break;
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001755 case 0xcd: /* fsb frequency */
1756 data = 3;
1757 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001758 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001759 data = kvm_get_apic_base(vcpu);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001760 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001761 case MSR_IA32_MISC_ENABLE:
1762 data = vcpu->ia32_misc_enable_msr;
1763 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001764#ifdef CONFIG_X86_64
1765 case MSR_EFER:
1766 data = vcpu->shadow_efer;
1767 break;
1768#endif
1769 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001770 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001771 return 1;
1772 }
1773 *pdata = data;
1774 return 0;
1775}
1776EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1777
Avi Kivity6aa8b732006-12-10 02:21:36 -08001778/*
1779 * Reads an msr value (of 'msr_index') into 'pdata'.
1780 * Returns 0 on success, non-0 otherwise.
1781 * Assumes vcpu_load() was already called.
1782 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001783int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001784{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001785 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001786}
1787
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001788#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001789
Avi Kivity3bab1f52006-12-29 16:49:48 -08001790static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001791{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001792 if (efer & EFER_RESERVED_BITS) {
1793 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1794 efer);
1795 inject_gp(vcpu);
1796 return;
1797 }
1798
1799 if (is_paging(vcpu)
1800 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1801 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1802 inject_gp(vcpu);
1803 return;
1804 }
1805
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001806 kvm_x86_ops->set_efer(vcpu, efer);
Avi Kivity7725f0b2006-12-13 00:34:01 -08001807
Avi Kivity6aa8b732006-12-10 02:21:36 -08001808 efer &= ~EFER_LMA;
1809 efer |= vcpu->shadow_efer & EFER_LMA;
1810
1811 vcpu->shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001812}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001813
1814#endif
1815
Avi Kivity3bab1f52006-12-29 16:49:48 -08001816int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1817{
1818 switch (msr) {
1819#ifdef CONFIG_X86_64
1820 case MSR_EFER:
1821 set_efer(vcpu, data);
1822 break;
1823#endif
1824 case MSR_IA32_MC0_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001825 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
Avi Kivity3bab1f52006-12-29 16:49:48 -08001826 __FUNCTION__, data);
1827 break;
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001828 case MSR_IA32_MCG_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001829 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001830 __FUNCTION__, data);
1831 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001832 case MSR_IA32_UCODE_REV:
1833 case MSR_IA32_UCODE_WRITE:
1834 case 0x200 ... 0x2ff: /* MTRRs */
1835 break;
1836 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001837 kvm_set_apic_base(vcpu, data);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001838 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001839 case MSR_IA32_MISC_ENABLE:
1840 vcpu->ia32_misc_enable_msr = data;
1841 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001842 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001843 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001844 return 1;
1845 }
1846 return 0;
1847}
1848EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1849
Avi Kivity6aa8b732006-12-10 02:21:36 -08001850/*
1851 * Writes msr value into into the appropriate "register".
1852 * Returns 0 on success, non-0 otherwise.
1853 * Assumes vcpu_load() was already called.
1854 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001855int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001856{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001857 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001858}
1859
1860void kvm_resched(struct kvm_vcpu *vcpu)
1861{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001862 if (!need_resched())
1863 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001864 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001865}
1866EXPORT_SYMBOL_GPL(kvm_resched);
1867
Avi Kivity06465c52007-02-28 20:46:53 +02001868void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1869{
1870 int i;
1871 u32 function;
1872 struct kvm_cpuid_entry *e, *best;
1873
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001874 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001875 function = vcpu->regs[VCPU_REGS_RAX];
1876 vcpu->regs[VCPU_REGS_RAX] = 0;
1877 vcpu->regs[VCPU_REGS_RBX] = 0;
1878 vcpu->regs[VCPU_REGS_RCX] = 0;
1879 vcpu->regs[VCPU_REGS_RDX] = 0;
1880 best = NULL;
1881 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1882 e = &vcpu->cpuid_entries[i];
1883 if (e->function == function) {
1884 best = e;
1885 break;
1886 }
1887 /*
1888 * Both basic or both extended?
1889 */
1890 if (((e->function ^ function) & 0x80000000) == 0)
1891 if (!best || e->function > best->function)
1892 best = e;
1893 }
1894 if (best) {
1895 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1896 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1897 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1898 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1899 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001900 kvm_x86_ops->decache_regs(vcpu);
1901 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001902}
1903EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1904
Avi Kivity039576c2007-03-20 12:46:50 +02001905static int pio_copy_data(struct kvm_vcpu *vcpu)
Avi Kivity46fc1472007-02-22 19:39:30 +02001906{
Avi Kivity039576c2007-03-20 12:46:50 +02001907 void *p = vcpu->pio_data;
1908 void *q;
1909 unsigned bytes;
1910 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1911
Avi Kivity039576c2007-03-20 12:46:50 +02001912 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1913 PAGE_KERNEL);
1914 if (!q) {
Avi Kivity039576c2007-03-20 12:46:50 +02001915 free_pio_guest_pages(vcpu);
1916 return -ENOMEM;
1917 }
1918 q += vcpu->pio.guest_page_offset;
1919 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1920 if (vcpu->pio.in)
1921 memcpy(q, p, bytes);
1922 else
1923 memcpy(p, q, bytes);
1924 q -= vcpu->pio.guest_page_offset;
1925 vunmap(q);
Avi Kivity039576c2007-03-20 12:46:50 +02001926 free_pio_guest_pages(vcpu);
1927 return 0;
1928}
1929
1930static int complete_pio(struct kvm_vcpu *vcpu)
1931{
1932 struct kvm_pio_request *io = &vcpu->pio;
Avi Kivity46fc1472007-02-22 19:39:30 +02001933 long delta;
Avi Kivity039576c2007-03-20 12:46:50 +02001934 int r;
Avi Kivity46fc1472007-02-22 19:39:30 +02001935
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001936 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001937
1938 if (!io->string) {
Avi Kivity039576c2007-03-20 12:46:50 +02001939 if (io->in)
1940 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
Avi Kivity46fc1472007-02-22 19:39:30 +02001941 io->size);
1942 } else {
Avi Kivity039576c2007-03-20 12:46:50 +02001943 if (io->in) {
1944 r = pio_copy_data(vcpu);
1945 if (r) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001946 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02001947 return r;
1948 }
1949 }
1950
Avi Kivity46fc1472007-02-22 19:39:30 +02001951 delta = 1;
1952 if (io->rep) {
Avi Kivity039576c2007-03-20 12:46:50 +02001953 delta *= io->cur_count;
Avi Kivity46fc1472007-02-22 19:39:30 +02001954 /*
1955 * The size of the register should really depend on
1956 * current address size.
1957 */
1958 vcpu->regs[VCPU_REGS_RCX] -= delta;
1959 }
Avi Kivity039576c2007-03-20 12:46:50 +02001960 if (io->down)
Avi Kivity46fc1472007-02-22 19:39:30 +02001961 delta = -delta;
1962 delta *= io->size;
Avi Kivity039576c2007-03-20 12:46:50 +02001963 if (io->in)
Avi Kivity46fc1472007-02-22 19:39:30 +02001964 vcpu->regs[VCPU_REGS_RDI] += delta;
1965 else
1966 vcpu->regs[VCPU_REGS_RSI] += delta;
1967 }
1968
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001969 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001970
Avi Kivity039576c2007-03-20 12:46:50 +02001971 io->count -= io->cur_count;
1972 io->cur_count = 0;
1973
Avi Kivity039576c2007-03-20 12:46:50 +02001974 return 0;
Avi Kivity46fc1472007-02-22 19:39:30 +02001975}
1976
Eddie Dong65619eb2007-07-17 11:52:33 +03001977static void kernel_pio(struct kvm_io_device *pio_dev,
1978 struct kvm_vcpu *vcpu,
1979 void *pd)
Eddie Dong74906342007-06-19 18:05:03 +03001980{
1981 /* TODO: String I/O for in kernel device */
1982
Eddie Dong9cf98822007-07-22 10:36:31 +03001983 mutex_lock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001984 if (vcpu->pio.in)
1985 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1986 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001987 pd);
Eddie Dong74906342007-06-19 18:05:03 +03001988 else
1989 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1990 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001991 pd);
Eddie Dong9cf98822007-07-22 10:36:31 +03001992 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001993}
1994
1995static void pio_string_write(struct kvm_io_device *pio_dev,
1996 struct kvm_vcpu *vcpu)
1997{
1998 struct kvm_pio_request *io = &vcpu->pio;
1999 void *pd = vcpu->pio_data;
2000 int i;
2001
Eddie Dong9cf98822007-07-22 10:36:31 +03002002 mutex_lock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03002003 for (i = 0; i < io->cur_count; i++) {
2004 kvm_iodevice_write(pio_dev, io->port,
2005 io->size,
2006 pd);
2007 pd += io->size;
2008 }
Eddie Dong9cf98822007-07-22 10:36:31 +03002009 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03002010}
2011
Mike Dayd77c26f2007-10-08 09:02:08 -04002012int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
Laurent Vivier3090dd72007-08-05 10:43:32 +03002013 int size, unsigned port)
2014{
2015 struct kvm_io_device *pio_dev;
2016
2017 vcpu->run->exit_reason = KVM_EXIT_IO;
2018 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
2019 vcpu->run->io.size = vcpu->pio.size = size;
2020 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
2021 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1;
2022 vcpu->run->io.port = vcpu->pio.port = port;
2023 vcpu->pio.in = in;
2024 vcpu->pio.string = 0;
2025 vcpu->pio.down = 0;
2026 vcpu->pio.guest_page_offset = 0;
2027 vcpu->pio.rep = 0;
2028
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002029 kvm_x86_ops->cache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03002030 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002031 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03002032
Avi Kivity0967b7b2007-09-15 17:34:36 +03002033 kvm_x86_ops->skip_emulated_instruction(vcpu);
2034
Laurent Vivier3090dd72007-08-05 10:43:32 +03002035 pio_dev = vcpu_find_pio_dev(vcpu, port);
2036 if (pio_dev) {
2037 kernel_pio(pio_dev, vcpu, vcpu->pio_data);
2038 complete_pio(vcpu);
2039 return 1;
2040 }
2041 return 0;
2042}
2043EXPORT_SYMBOL_GPL(kvm_emulate_pio);
2044
2045int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2046 int size, unsigned long count, int down,
Avi Kivity039576c2007-03-20 12:46:50 +02002047 gva_t address, int rep, unsigned port)
2048{
2049 unsigned now, in_page;
Eddie Dong65619eb2007-07-17 11:52:33 +03002050 int i, ret = 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002051 int nr_pages = 1;
2052 struct page *page;
Eddie Dong74906342007-06-19 18:05:03 +03002053 struct kvm_io_device *pio_dev;
Avi Kivity039576c2007-03-20 12:46:50 +02002054
2055 vcpu->run->exit_reason = KVM_EXIT_IO;
2056 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Laurent Vivier3090dd72007-08-05 10:43:32 +03002057 vcpu->run->io.size = vcpu->pio.size = size;
Avi Kivity039576c2007-03-20 12:46:50 +02002058 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Laurent Vivier3090dd72007-08-05 10:43:32 +03002059 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count;
2060 vcpu->run->io.port = vcpu->pio.port = port;
Avi Kivity039576c2007-03-20 12:46:50 +02002061 vcpu->pio.in = in;
Laurent Vivier3090dd72007-08-05 10:43:32 +03002062 vcpu->pio.string = 1;
Avi Kivity039576c2007-03-20 12:46:50 +02002063 vcpu->pio.down = down;
2064 vcpu->pio.guest_page_offset = offset_in_page(address);
2065 vcpu->pio.rep = rep;
2066
Avi Kivity039576c2007-03-20 12:46:50 +02002067 if (!count) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002068 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02002069 return 1;
2070 }
2071
Avi Kivity039576c2007-03-20 12:46:50 +02002072 if (!down)
2073 in_page = PAGE_SIZE - offset_in_page(address);
2074 else
2075 in_page = offset_in_page(address) + size;
2076 now = min(count, (unsigned long)in_page / size);
2077 if (!now) {
2078 /*
2079 * String I/O straddles page boundary. Pin two guest pages
2080 * so that we satisfy atomicity constraints. Do just one
2081 * transaction to avoid complexity.
2082 */
2083 nr_pages = 2;
2084 now = 1;
2085 }
2086 if (down) {
2087 /*
2088 * String I/O in reverse. Yuck. Kill the guest, fix later.
2089 */
Rusty Russellf0242472007-08-01 10:48:02 +10002090 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivity039576c2007-03-20 12:46:50 +02002091 inject_gp(vcpu);
2092 return 1;
2093 }
2094 vcpu->run->io.count = now;
2095 vcpu->pio.cur_count = now;
2096
Avi Kivity0967b7b2007-09-15 17:34:36 +03002097 if (vcpu->pio.cur_count == vcpu->pio.count)
2098 kvm_x86_ops->skip_emulated_instruction(vcpu);
2099
Avi Kivity039576c2007-03-20 12:46:50 +02002100 for (i = 0; i < nr_pages; ++i) {
Shaohua Li11ec2802007-07-23 14:51:37 +08002101 mutex_lock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02002102 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
Avi Kivity039576c2007-03-20 12:46:50 +02002103 vcpu->pio.guest_pages[i] = page;
Shaohua Li11ec2802007-07-23 14:51:37 +08002104 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02002105 if (!page) {
2106 inject_gp(vcpu);
2107 free_pio_guest_pages(vcpu);
2108 return 1;
2109 }
2110 }
2111
Laurent Vivier3090dd72007-08-05 10:43:32 +03002112 pio_dev = vcpu_find_pio_dev(vcpu, port);
Eddie Dong65619eb2007-07-17 11:52:33 +03002113 if (!vcpu->pio.in) {
2114 /* string PIO write */
2115 ret = pio_copy_data(vcpu);
2116 if (ret >= 0 && pio_dev) {
2117 pio_string_write(pio_dev, vcpu);
2118 complete_pio(vcpu);
2119 if (vcpu->pio.count == 0)
2120 ret = 1;
2121 }
2122 } else if (pio_dev)
Rusty Russellf0242472007-08-01 10:48:02 +10002123 pr_unimpl(vcpu, "no string pio read support yet, "
Eddie Dong65619eb2007-07-17 11:52:33 +03002124 "port %x size %d count %ld\n",
2125 port, size, count);
2126
2127 return ret;
Avi Kivity039576c2007-03-20 12:46:50 +02002128}
Laurent Vivier3090dd72007-08-05 10:43:32 +03002129EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
Avi Kivity039576c2007-03-20 12:46:50 +02002130
Avi Kivity04d2cc72007-09-10 18:10:54 +03002131/*
2132 * Check if userspace requested an interrupt window, and that the
2133 * interrupt window is open.
2134 *
2135 * No need to exit to userspace if we already have an interrupt queued.
2136 */
2137static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2138 struct kvm_run *kvm_run)
2139{
2140 return (!vcpu->irq_summary &&
2141 kvm_run->request_interrupt_window &&
2142 vcpu->interrupt_window_open &&
2143 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
2144}
2145
2146static void post_kvm_run_save(struct kvm_vcpu *vcpu,
2147 struct kvm_run *kvm_run)
2148{
2149 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
2150 kvm_run->cr8 = get_cr8(vcpu);
2151 kvm_run->apic_base = kvm_get_apic_base(vcpu);
2152 if (irqchip_in_kernel(vcpu->kvm))
2153 kvm_run->ready_for_interrupt_injection = 1;
2154 else
2155 kvm_run->ready_for_interrupt_injection =
2156 (vcpu->interrupt_window_open &&
2157 vcpu->irq_summary == 0);
2158}
2159
2160static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2161{
2162 int r;
2163
2164 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002165 pr_debug("vcpu %d received sipi with vector # %x\n",
Avi Kivity04d2cc72007-09-10 18:10:54 +03002166 vcpu->vcpu_id, vcpu->sipi_vector);
2167 kvm_lapic_reset(vcpu);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002168 r = kvm_x86_ops->vcpu_reset(vcpu);
2169 if (r)
2170 return r;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002171 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
2172 }
2173
2174preempted:
2175 if (vcpu->guest_debug.enabled)
2176 kvm_x86_ops->guest_debug_pre(vcpu);
2177
2178again:
2179 r = kvm_mmu_reload(vcpu);
2180 if (unlikely(r))
2181 goto out;
2182
Avi Kivityab6ef342007-10-16 16:23:22 +02002183 kvm_inject_pending_timer_irqs(vcpu);
2184
Avi Kivity04d2cc72007-09-10 18:10:54 +03002185 preempt_disable();
2186
2187 kvm_x86_ops->prepare_guest_switch(vcpu);
2188 kvm_load_guest_fpu(vcpu);
2189
2190 local_irq_disable();
2191
2192 if (signal_pending(current)) {
2193 local_irq_enable();
2194 preempt_enable();
2195 r = -EINTR;
2196 kvm_run->exit_reason = KVM_EXIT_INTR;
2197 ++vcpu->stat.signal_exits;
2198 goto out;
2199 }
2200
2201 if (irqchip_in_kernel(vcpu->kvm))
2202 kvm_x86_ops->inject_pending_irq(vcpu);
2203 else if (!vcpu->mmio_read_completed)
2204 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2205
2206 vcpu->guest_mode = 1;
Laurent Vivierd172fcd2007-10-15 17:00:19 +02002207 kvm_guest_enter();
Avi Kivity04d2cc72007-09-10 18:10:54 +03002208
2209 if (vcpu->requests)
Avi Kivity3176bc32007-10-16 17:22:08 +02002210 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivity04d2cc72007-09-10 18:10:54 +03002211 kvm_x86_ops->tlb_flush(vcpu);
2212
2213 kvm_x86_ops->run(vcpu, kvm_run);
2214
2215 vcpu->guest_mode = 0;
2216 local_irq_enable();
2217
2218 ++vcpu->stat.exits;
2219
Laurent Vivier0552f732007-10-18 15:19:01 +02002220 /*
2221 * We must have an instruction between local_irq_enable() and
2222 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2223 * the interrupt shadow. The stat.exits increment will do nicely.
2224 * But we need to prevent reordering, hence this barrier():
2225 */
2226 barrier();
2227
2228 kvm_guest_exit();
2229
Avi Kivity04d2cc72007-09-10 18:10:54 +03002230 preempt_enable();
2231
2232 /*
2233 * Profile KVM exit RIPs:
2234 */
2235 if (unlikely(prof_on == KVM_PROFILING)) {
2236 kvm_x86_ops->cache_regs(vcpu);
2237 profile_hit(KVM_PROFILING, (void *)vcpu->rip);
2238 }
2239
2240 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2241
2242 if (r > 0) {
2243 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2244 r = -EINTR;
2245 kvm_run->exit_reason = KVM_EXIT_INTR;
2246 ++vcpu->stat.request_irq_exits;
2247 goto out;
2248 }
2249 if (!need_resched()) {
2250 ++vcpu->stat.light_exits;
2251 goto again;
2252 }
2253 }
2254
2255out:
2256 if (r > 0) {
2257 kvm_resched(vcpu);
2258 goto preempted;
2259 }
2260
2261 post_kvm_run_save(vcpu, kvm_run);
2262
2263 return r;
2264}
2265
2266
Avi Kivitybccf2152007-02-21 18:04:26 +02002267static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002268{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002269 int r;
Avi Kivity1961d272007-03-05 19:46:05 +02002270 sigset_t sigsaved;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002271
Avi Kivitybccf2152007-02-21 18:04:26 +02002272 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002273
He, Qingc5ec1532007-09-03 17:07:41 +03002274 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
2275 kvm_vcpu_block(vcpu);
2276 vcpu_put(vcpu);
2277 return -EAGAIN;
2278 }
2279
Avi Kivity1961d272007-03-05 19:46:05 +02002280 if (vcpu->sigset_active)
2281 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2282
Dor Laor54810342007-02-12 00:54:39 -08002283 /* re-sync apic's tpr */
He, Qing5cd4f6f2007-08-30 17:04:26 +08002284 if (!irqchip_in_kernel(vcpu->kvm))
2285 set_cr8(vcpu, kvm_run->cr8);
Dor Laor54810342007-02-12 00:54:39 -08002286
Avi Kivity02c83202007-04-29 15:02:17 +03002287 if (vcpu->pio.cur_count) {
2288 r = complete_pio(vcpu);
2289 if (r)
2290 goto out;
2291 }
Zhang Xiantao34c16ee2007-10-20 15:34:38 +08002292#if CONFIG_HAS_IOMEM
Avi Kivity02c83202007-04-29 15:02:17 +03002293 if (vcpu->mmio_needed) {
2294 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
2295 vcpu->mmio_read_completed = 1;
2296 vcpu->mmio_needed = 0;
2297 r = emulate_instruction(vcpu, kvm_run,
Laurent Vivier34273182007-09-18 11:27:37 +02002298 vcpu->mmio_fault_cr2, 0, 1);
Avi Kivity02c83202007-04-29 15:02:17 +03002299 if (r == EMULATE_DO_MMIO) {
2300 /*
2301 * Read-modify-write. Back to userspace.
2302 */
Avi Kivity02c83202007-04-29 15:02:17 +03002303 r = 0;
2304 goto out;
Avi Kivity46fc1472007-02-22 19:39:30 +02002305 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002306 }
Zhang Xiantao34c16ee2007-10-20 15:34:38 +08002307#endif
Avi Kivity8eb7d332007-03-04 14:17:08 +02002308 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002309 kvm_x86_ops->cache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002310 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002311 kvm_x86_ops->decache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002312 }
2313
Avi Kivity04d2cc72007-09-10 18:10:54 +03002314 r = __vcpu_run(vcpu, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002315
Avi Kivity039576c2007-03-20 12:46:50 +02002316out:
Avi Kivity1961d272007-03-05 19:46:05 +02002317 if (vcpu->sigset_active)
2318 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2319
Avi Kivity6aa8b732006-12-10 02:21:36 -08002320 vcpu_put(vcpu);
2321 return r;
2322}
2323
Avi Kivitybccf2152007-02-21 18:04:26 +02002324static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
2325 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002326{
Avi Kivitybccf2152007-02-21 18:04:26 +02002327 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002328
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002329 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002330
2331 regs->rax = vcpu->regs[VCPU_REGS_RAX];
2332 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
2333 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
2334 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
2335 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
2336 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
2337 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
2338 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002339#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002340 regs->r8 = vcpu->regs[VCPU_REGS_R8];
2341 regs->r9 = vcpu->regs[VCPU_REGS_R9];
2342 regs->r10 = vcpu->regs[VCPU_REGS_R10];
2343 regs->r11 = vcpu->regs[VCPU_REGS_R11];
2344 regs->r12 = vcpu->regs[VCPU_REGS_R12];
2345 regs->r13 = vcpu->regs[VCPU_REGS_R13];
2346 regs->r14 = vcpu->regs[VCPU_REGS_R14];
2347 regs->r15 = vcpu->regs[VCPU_REGS_R15];
2348#endif
2349
2350 regs->rip = vcpu->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002351 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002352
2353 /*
2354 * Don't leak debug flags in case they were set for guest debugging
2355 */
2356 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2357 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2358
2359 vcpu_put(vcpu);
2360
2361 return 0;
2362}
2363
Avi Kivitybccf2152007-02-21 18:04:26 +02002364static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
2365 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002366{
Avi Kivitybccf2152007-02-21 18:04:26 +02002367 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002368
2369 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
2370 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
2371 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
2372 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
2373 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
2374 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
2375 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
2376 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002377#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002378 vcpu->regs[VCPU_REGS_R8] = regs->r8;
2379 vcpu->regs[VCPU_REGS_R9] = regs->r9;
2380 vcpu->regs[VCPU_REGS_R10] = regs->r10;
2381 vcpu->regs[VCPU_REGS_R11] = regs->r11;
2382 vcpu->regs[VCPU_REGS_R12] = regs->r12;
2383 vcpu->regs[VCPU_REGS_R13] = regs->r13;
2384 vcpu->regs[VCPU_REGS_R14] = regs->r14;
2385 vcpu->regs[VCPU_REGS_R15] = regs->r15;
2386#endif
2387
2388 vcpu->rip = regs->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002389 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002390
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002391 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002392
2393 vcpu_put(vcpu);
2394
2395 return 0;
2396}
2397
2398static void get_segment(struct kvm_vcpu *vcpu,
2399 struct kvm_segment *var, int seg)
2400{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002401 return kvm_x86_ops->get_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002402}
2403
Avi Kivitybccf2152007-02-21 18:04:26 +02002404static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2405 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002406{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002407 struct descriptor_table dt;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002408 int pending_vec;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002409
Avi Kivitybccf2152007-02-21 18:04:26 +02002410 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002411
2412 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2413 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2414 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2415 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2416 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2417 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2418
2419 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2420 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2421
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002422 kvm_x86_ops->get_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002423 sregs->idt.limit = dt.limit;
2424 sregs->idt.base = dt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002425 kvm_x86_ops->get_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002426 sregs->gdt.limit = dt.limit;
2427 sregs->gdt.base = dt.base;
2428
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002429 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002430 sregs->cr0 = vcpu->cr0;
2431 sregs->cr2 = vcpu->cr2;
2432 sregs->cr3 = vcpu->cr3;
2433 sregs->cr4 = vcpu->cr4;
Eddie Dong7017fc32007-07-18 11:34:57 +03002434 sregs->cr8 = get_cr8(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002435 sregs->efer = vcpu->shadow_efer;
Eddie Dong7017fc32007-07-18 11:34:57 +03002436 sregs->apic_base = kvm_get_apic_base(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002437
Eddie Dong2a8067f2007-08-06 16:29:07 +03002438 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc52fb352007-08-02 14:03:07 +03002439 memset(sregs->interrupt_bitmap, 0,
2440 sizeof sregs->interrupt_bitmap);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002441 pending_vec = kvm_x86_ops->get_irq(vcpu);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002442 if (pending_vec >= 0)
Mike Dayd77c26f2007-10-08 09:02:08 -04002443 set_bit(pending_vec,
2444 (unsigned long *)sregs->interrupt_bitmap);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002445 } else
He, Qingc52fb352007-08-02 14:03:07 +03002446 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2447 sizeof sregs->interrupt_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002448
2449 vcpu_put(vcpu);
2450
2451 return 0;
2452}
2453
2454static void set_segment(struct kvm_vcpu *vcpu,
2455 struct kvm_segment *var, int seg)
2456{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002457 return kvm_x86_ops->set_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002458}
2459
Avi Kivitybccf2152007-02-21 18:04:26 +02002460static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2461 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002462{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002463 int mmu_reset_needed = 0;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002464 int i, pending_vec, max_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002465 struct descriptor_table dt;
2466
Avi Kivitybccf2152007-02-21 18:04:26 +02002467 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002468
Avi Kivity6aa8b732006-12-10 02:21:36 -08002469 dt.limit = sregs->idt.limit;
2470 dt.base = sregs->idt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002471 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002472 dt.limit = sregs->gdt.limit;
2473 dt.base = sregs->gdt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002474 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002475
2476 vcpu->cr2 = sregs->cr2;
2477 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2478 vcpu->cr3 = sregs->cr3;
2479
Eddie Dong7017fc32007-07-18 11:34:57 +03002480 set_cr8(vcpu, sregs->cr8);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002481
2482 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002483#ifdef CONFIG_X86_64
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002484 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002485#endif
Eddie Dong7017fc32007-07-18 11:34:57 +03002486 kvm_set_apic_base(vcpu, sregs->apic_base);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002487
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002488 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity399badf2007-01-05 16:36:38 -08002489
Avi Kivity6aa8b732006-12-10 02:21:36 -08002490 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
Rusty Russell81f50e32007-09-06 01:20:38 +10002491 vcpu->cr0 = sregs->cr0;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002492 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002493
2494 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002495 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Avi Kivity1b0973b2007-01-05 16:36:41 -08002496 if (!is_long_mode(vcpu) && is_pae(vcpu))
2497 load_pdptrs(vcpu, vcpu->cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002498
2499 if (mmu_reset_needed)
2500 kvm_mmu_reset_context(vcpu);
2501
He, Qingc52fb352007-08-02 14:03:07 +03002502 if (!irqchip_in_kernel(vcpu->kvm)) {
2503 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2504 sizeof vcpu->irq_pending);
2505 vcpu->irq_summary = 0;
2506 for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
2507 if (vcpu->irq_pending[i])
2508 __set_bit(i, &vcpu->irq_summary);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002509 } else {
2510 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
2511 pending_vec = find_first_bit(
2512 (const unsigned long *)sregs->interrupt_bitmap,
2513 max_bits);
2514 /* Only pending external irq is handled here */
2515 if (pending_vec < max_bits) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002516 kvm_x86_ops->set_irq(vcpu, pending_vec);
Mike Dayd77c26f2007-10-08 09:02:08 -04002517 pr_debug("Set back pending irq %d\n",
2518 pending_vec);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002519 }
He, Qingc52fb352007-08-02 14:03:07 +03002520 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002521
Avi Kivity024aa1c2007-03-21 13:44:58 +02002522 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2523 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2524 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2525 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2526 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2527 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2528
2529 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2530 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2531
Avi Kivity6aa8b732006-12-10 02:21:36 -08002532 vcpu_put(vcpu);
2533
2534 return 0;
2535}
2536
Rusty Russell1747fb72007-09-06 01:21:32 +10002537void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2538{
2539 struct kvm_segment cs;
2540
2541 get_segment(vcpu, &cs, VCPU_SREG_CS);
2542 *db = cs.db;
2543 *l = cs.l;
2544}
2545EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2546
Avi Kivity6aa8b732006-12-10 02:21:36 -08002547/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002548 * Translate a guest virtual address to a guest physical address.
2549 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002550static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2551 struct kvm_translation *tr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002552{
2553 unsigned long vaddr = tr->linear_address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002554 gpa_t gpa;
2555
Avi Kivitybccf2152007-02-21 18:04:26 +02002556 vcpu_load(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +08002557 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002558 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2559 tr->physical_address = gpa;
2560 tr->valid = gpa != UNMAPPED_GVA;
2561 tr->writeable = 1;
2562 tr->usermode = 0;
Shaohua Li11ec2802007-07-23 14:51:37 +08002563 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002564 vcpu_put(vcpu);
2565
2566 return 0;
2567}
2568
Avi Kivitybccf2152007-02-21 18:04:26 +02002569static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2570 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002571{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002572 if (irq->irq < 0 || irq->irq >= 256)
2573 return -EINVAL;
Eddie Dong97222cc2007-09-12 10:58:04 +03002574 if (irqchip_in_kernel(vcpu->kvm))
2575 return -ENXIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02002576 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002577
2578 set_bit(irq->irq, vcpu->irq_pending);
2579 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2580
2581 vcpu_put(vcpu);
2582
2583 return 0;
2584}
2585
Avi Kivitybccf2152007-02-21 18:04:26 +02002586static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2587 struct kvm_debug_guest *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002588{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002589 int r;
2590
Avi Kivitybccf2152007-02-21 18:04:26 +02002591 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002592
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002593 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002594
2595 vcpu_put(vcpu);
2596
2597 return r;
2598}
2599
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002600static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2601 unsigned long address,
2602 int *type)
2603{
2604 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2605 unsigned long pgoff;
2606 struct page *page;
2607
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002608 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +02002609 if (pgoff == 0)
2610 page = virt_to_page(vcpu->run);
2611 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2612 page = virt_to_page(vcpu->pio_data);
2613 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002614 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002615 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002616 if (type != NULL)
2617 *type = VM_FAULT_MINOR;
2618
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002619 return page;
2620}
2621
2622static struct vm_operations_struct kvm_vcpu_vm_ops = {
2623 .nopage = kvm_vcpu_nopage,
2624};
2625
2626static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2627{
2628 vma->vm_ops = &kvm_vcpu_vm_ops;
2629 return 0;
2630}
2631
Avi Kivitybccf2152007-02-21 18:04:26 +02002632static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2633{
2634 struct kvm_vcpu *vcpu = filp->private_data;
2635
2636 fput(vcpu->kvm->filp);
2637 return 0;
2638}
2639
2640static struct file_operations kvm_vcpu_fops = {
2641 .release = kvm_vcpu_release,
2642 .unlocked_ioctl = kvm_vcpu_ioctl,
2643 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002644 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02002645};
2646
2647/*
2648 * Allocates an inode for the vcpu.
2649 */
2650static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2651{
2652 int fd, r;
2653 struct inode *inode;
2654 struct file *file;
2655
Avi Kivityd6d28162007-06-28 08:38:16 -04002656 r = anon_inode_getfd(&fd, &inode, &file,
2657 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2658 if (r)
2659 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002660 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +02002661 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02002662}
2663
Avi Kivityc5ea7662007-02-20 18:41:05 +02002664/*
2665 * Creates some virtual cpus. Good luck creating more than one.
2666 */
2667static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2668{
2669 int r;
2670 struct kvm_vcpu *vcpu;
2671
Avi Kivityc5ea7662007-02-20 18:41:05 +02002672 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002673 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002674
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002675 vcpu = kvm_x86_ops->vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002676 if (IS_ERR(vcpu))
2677 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002678
Avi Kivity15ad7142007-07-11 18:17:21 +03002679 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
2680
Rusty Russellb114b082007-07-30 21:13:43 +10002681 /* We do fxsave: this must be aligned. */
2682 BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF);
2683
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002684 vcpu_load(vcpu);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002685 r = kvm_x86_ops->vcpu_reset(vcpu);
2686 if (r == 0)
2687 r = kvm_mmu_setup(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002688 vcpu_put(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002689 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002690 goto free_vcpu;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002691
Shaohua Li11ec2802007-07-23 14:51:37 +08002692 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002693 if (kvm->vcpus[n]) {
2694 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +08002695 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002696 goto mmu_unload;
2697 }
2698 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08002699 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002700
2701 /* Now it's all set up, let userspace reach it */
Avi Kivitybccf2152007-02-21 18:04:26 +02002702 r = create_vcpu_fd(vcpu);
2703 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002704 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02002705 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002706
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002707unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08002708 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002709 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +08002710 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002711
2712mmu_unload:
2713 vcpu_load(vcpu);
2714 kvm_mmu_unload(vcpu);
2715 vcpu_put(vcpu);
2716
2717free_vcpu:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002718 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002719 return r;
2720}
2721
Avi Kivity1961d272007-03-05 19:46:05 +02002722static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2723{
2724 if (sigset) {
2725 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2726 vcpu->sigset_active = 1;
2727 vcpu->sigset = *sigset;
2728 } else
2729 vcpu->sigset_active = 0;
2730 return 0;
2731}
2732
Avi Kivityb8836732007-04-01 16:34:31 +03002733/*
2734 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2735 * we have asm/x86/processor.h
2736 */
2737struct fxsave {
2738 u16 cwd;
2739 u16 swd;
2740 u16 twd;
2741 u16 fop;
2742 u64 rip;
2743 u64 rdp;
2744 u32 mxcsr;
2745 u32 mxcsr_mask;
2746 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2747#ifdef CONFIG_X86_64
2748 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2749#else
2750 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2751#endif
2752};
2753
2754static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2755{
Rusty Russellb114b082007-07-30 21:13:43 +10002756 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002757
2758 vcpu_load(vcpu);
2759
2760 memcpy(fpu->fpr, fxsave->st_space, 128);
2761 fpu->fcw = fxsave->cwd;
2762 fpu->fsw = fxsave->swd;
2763 fpu->ftwx = fxsave->twd;
2764 fpu->last_opcode = fxsave->fop;
2765 fpu->last_ip = fxsave->rip;
2766 fpu->last_dp = fxsave->rdp;
2767 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2768
2769 vcpu_put(vcpu);
2770
2771 return 0;
2772}
2773
2774static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2775{
Rusty Russellb114b082007-07-30 21:13:43 +10002776 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002777
2778 vcpu_load(vcpu);
2779
2780 memcpy(fxsave->st_space, fpu->fpr, 128);
2781 fxsave->cwd = fpu->fcw;
2782 fxsave->swd = fpu->fsw;
2783 fxsave->twd = fpu->ftwx;
2784 fxsave->fop = fpu->last_opcode;
2785 fxsave->rip = fpu->last_ip;
2786 fxsave->rdp = fpu->last_dp;
2787 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2788
2789 vcpu_put(vcpu);
2790
2791 return 0;
2792}
2793
Avi Kivitybccf2152007-02-21 18:04:26 +02002794static long kvm_vcpu_ioctl(struct file *filp,
2795 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002796{
Avi Kivitybccf2152007-02-21 18:04:26 +02002797 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +00002798 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02002799 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002800
2801 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002802 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002803 r = -EINVAL;
2804 if (arg)
2805 goto out;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002806 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002807 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002808 case KVM_GET_REGS: {
2809 struct kvm_regs kvm_regs;
2810
Avi Kivitybccf2152007-02-21 18:04:26 +02002811 memset(&kvm_regs, 0, sizeof kvm_regs);
2812 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002813 if (r)
2814 goto out;
2815 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002816 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002817 goto out;
2818 r = 0;
2819 break;
2820 }
2821 case KVM_SET_REGS: {
2822 struct kvm_regs kvm_regs;
2823
2824 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002825 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002826 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002827 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002828 if (r)
2829 goto out;
2830 r = 0;
2831 break;
2832 }
2833 case KVM_GET_SREGS: {
2834 struct kvm_sregs kvm_sregs;
2835
Avi Kivitybccf2152007-02-21 18:04:26 +02002836 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2837 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
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, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002842 goto out;
2843 r = 0;
2844 break;
2845 }
2846 case KVM_SET_SREGS: {
2847 struct kvm_sregs kvm_sregs;
2848
2849 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002850 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002851 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002852 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002853 if (r)
2854 goto out;
2855 r = 0;
2856 break;
2857 }
2858 case KVM_TRANSLATE: {
2859 struct kvm_translation tr;
2860
2861 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002862 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002863 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002864 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002865 if (r)
2866 goto out;
2867 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002868 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002869 goto out;
2870 r = 0;
2871 break;
2872 }
2873 case KVM_INTERRUPT: {
2874 struct kvm_interrupt irq;
2875
2876 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002877 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002878 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002879 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002880 if (r)
2881 goto out;
2882 r = 0;
2883 break;
2884 }
2885 case KVM_DEBUG_GUEST: {
2886 struct kvm_debug_guest dbg;
2887
2888 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002889 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002890 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002891 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002892 if (r)
2893 goto out;
2894 r = 0;
2895 break;
2896 }
Avi Kivity1961d272007-03-05 19:46:05 +02002897 case KVM_SET_SIGNAL_MASK: {
2898 struct kvm_signal_mask __user *sigmask_arg = argp;
2899 struct kvm_signal_mask kvm_sigmask;
2900 sigset_t sigset, *p;
2901
2902 p = NULL;
2903 if (argp) {
2904 r = -EFAULT;
2905 if (copy_from_user(&kvm_sigmask, argp,
2906 sizeof kvm_sigmask))
2907 goto out;
2908 r = -EINVAL;
2909 if (kvm_sigmask.len != sizeof sigset)
2910 goto out;
2911 r = -EFAULT;
2912 if (copy_from_user(&sigset, sigmask_arg->sigset,
2913 sizeof sigset))
2914 goto out;
2915 p = &sigset;
2916 }
2917 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2918 break;
2919 }
Avi Kivityb8836732007-04-01 16:34:31 +03002920 case KVM_GET_FPU: {
2921 struct kvm_fpu fpu;
2922
2923 memset(&fpu, 0, sizeof fpu);
2924 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2925 if (r)
2926 goto out;
2927 r = -EFAULT;
2928 if (copy_to_user(argp, &fpu, sizeof fpu))
2929 goto out;
2930 r = 0;
2931 break;
2932 }
2933 case KVM_SET_FPU: {
2934 struct kvm_fpu fpu;
2935
2936 r = -EFAULT;
2937 if (copy_from_user(&fpu, argp, sizeof fpu))
2938 goto out;
2939 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2940 if (r)
2941 goto out;
2942 r = 0;
2943 break;
2944 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002945 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02002946 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02002947 }
2948out:
2949 return r;
2950}
2951
2952static long kvm_vm_ioctl(struct file *filp,
2953 unsigned int ioctl, unsigned long arg)
2954{
2955 struct kvm *kvm = filp->private_data;
2956 void __user *argp = (void __user *)arg;
2957 int r = -EINVAL;
2958
2959 switch (ioctl) {
2960 case KVM_CREATE_VCPU:
2961 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2962 if (r < 0)
2963 goto out;
2964 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002965 case KVM_SET_MEMORY_REGION: {
2966 struct kvm_memory_region kvm_mem;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002967 struct kvm_userspace_memory_region kvm_userspace_mem;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002968
2969 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002970 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002971 goto out;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002972 kvm_userspace_mem.slot = kvm_mem.slot;
2973 kvm_userspace_mem.flags = kvm_mem.flags;
2974 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
2975 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
2976 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
2977 if (r)
2978 goto out;
2979 break;
2980 }
2981 case KVM_SET_USER_MEMORY_REGION: {
2982 struct kvm_userspace_memory_region kvm_userspace_mem;
2983
2984 r = -EFAULT;
2985 if (copy_from_user(&kvm_userspace_mem, argp,
2986 sizeof kvm_userspace_mem))
2987 goto out;
2988
2989 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002990 if (r)
2991 goto out;
2992 break;
2993 }
Izik Eidus82ce2c92007-10-02 18:52:55 +02002994 case KVM_SET_NR_MMU_PAGES:
2995 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
2996 if (r)
2997 goto out;
2998 break;
2999 case KVM_GET_NR_MMU_PAGES:
3000 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
3001 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003002 case KVM_GET_DIRTY_LOG: {
3003 struct kvm_dirty_log log;
3004
3005 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00003006 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003007 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02003008 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003009 if (r)
3010 goto out;
3011 break;
3012 }
Avi Kivitye8207542007-03-30 16:54:30 +03003013 case KVM_SET_MEMORY_ALIAS: {
3014 struct kvm_memory_alias alias;
3015
3016 r = -EFAULT;
3017 if (copy_from_user(&alias, argp, sizeof alias))
3018 goto out;
3019 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
3020 if (r)
3021 goto out;
3022 break;
3023 }
Eddie Dong85f455f2007-07-06 12:20:49 +03003024 case KVM_CREATE_IRQCHIP:
3025 r = -ENOMEM;
3026 kvm->vpic = kvm_create_pic(kvm);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03003027 if (kvm->vpic) {
3028 r = kvm_ioapic_init(kvm);
3029 if (r) {
3030 kfree(kvm->vpic);
3031 kvm->vpic = NULL;
3032 goto out;
3033 }
Mike Dayd77c26f2007-10-08 09:02:08 -04003034 } else
Eddie Dong85f455f2007-07-06 12:20:49 +03003035 goto out;
3036 break;
3037 case KVM_IRQ_LINE: {
3038 struct kvm_irq_level irq_event;
3039
3040 r = -EFAULT;
3041 if (copy_from_user(&irq_event, argp, sizeof irq_event))
3042 goto out;
3043 if (irqchip_in_kernel(kvm)) {
Eddie Dong9cf98822007-07-22 10:36:31 +03003044 mutex_lock(&kvm->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +03003045 if (irq_event.irq < 16)
3046 kvm_pic_set_irq(pic_irqchip(kvm),
3047 irq_event.irq,
3048 irq_event.level);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03003049 kvm_ioapic_set_irq(kvm->vioapic,
3050 irq_event.irq,
3051 irq_event.level);
Eddie Dong9cf98822007-07-22 10:36:31 +03003052 mutex_unlock(&kvm->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +03003053 r = 0;
3054 }
3055 break;
3056 }
He, Qing6ceb9d72007-07-26 11:05:18 +03003057 case KVM_GET_IRQCHIP: {
3058 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3059 struct kvm_irqchip chip;
3060
3061 r = -EFAULT;
3062 if (copy_from_user(&chip, argp, sizeof chip))
3063 goto out;
3064 r = -ENXIO;
3065 if (!irqchip_in_kernel(kvm))
3066 goto out;
3067 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
3068 if (r)
3069 goto out;
3070 r = -EFAULT;
3071 if (copy_to_user(argp, &chip, sizeof chip))
3072 goto out;
3073 r = 0;
3074 break;
3075 }
3076 case KVM_SET_IRQCHIP: {
3077 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3078 struct kvm_irqchip chip;
3079
3080 r = -EFAULT;
3081 if (copy_from_user(&chip, argp, sizeof chip))
3082 goto out;
3083 r = -ENXIO;
3084 if (!irqchip_in_kernel(kvm))
3085 goto out;
3086 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
3087 if (r)
3088 goto out;
3089 r = 0;
3090 break;
3091 }
Avi Kivityf17abe92007-02-21 19:28:04 +02003092 default:
3093 ;
3094 }
3095out:
3096 return r;
3097}
3098
3099static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
3100 unsigned long address,
3101 int *type)
3102{
3103 struct kvm *kvm = vma->vm_file->private_data;
3104 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02003105 struct page *page;
3106
Avi Kivityf17abe92007-02-21 19:28:04 +02003107 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Izik Eiduse0d62c72007-10-24 23:57:46 +02003108 if (!kvm_is_visible_gfn(kvm, pgoff))
3109 return NOPAGE_SIGBUS;
Avi Kivity954bbbc22007-03-30 14:02:32 +03003110 page = gfn_to_page(kvm, pgoff);
Izik Eidus8a7ae052007-10-18 11:09:33 +02003111 if (is_error_page(page)) {
3112 kvm_release_page(page);
Avi Kivityf17abe92007-02-21 19:28:04 +02003113 return NOPAGE_SIGBUS;
Izik Eidus8a7ae052007-10-18 11:09:33 +02003114 }
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03003115 if (type != NULL)
3116 *type = VM_FAULT_MINOR;
3117
Avi Kivityf17abe92007-02-21 19:28:04 +02003118 return page;
3119}
3120
3121static struct vm_operations_struct kvm_vm_vm_ops = {
3122 .nopage = kvm_vm_nopage,
3123};
3124
3125static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
3126{
3127 vma->vm_ops = &kvm_vm_vm_ops;
3128 return 0;
3129}
3130
3131static struct file_operations kvm_vm_fops = {
3132 .release = kvm_vm_release,
3133 .unlocked_ioctl = kvm_vm_ioctl,
3134 .compat_ioctl = kvm_vm_ioctl,
3135 .mmap = kvm_vm_mmap,
3136};
3137
3138static int kvm_dev_ioctl_create_vm(void)
3139{
3140 int fd, r;
3141 struct inode *inode;
3142 struct file *file;
3143 struct kvm *kvm;
3144
Avi Kivityf17abe92007-02-21 19:28:04 +02003145 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04003146 if (IS_ERR(kvm))
3147 return PTR_ERR(kvm);
3148 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
3149 if (r) {
3150 kvm_destroy_vm(kvm);
3151 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02003152 }
3153
Avi Kivitybccf2152007-02-21 18:04:26 +02003154 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02003155
Avi Kivityf17abe92007-02-21 19:28:04 +02003156 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02003157}
3158
3159static long kvm_dev_ioctl(struct file *filp,
3160 unsigned int ioctl, unsigned long arg)
3161{
3162 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02003163 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02003164
3165 switch (ioctl) {
3166 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02003167 r = -EINVAL;
3168 if (arg)
3169 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02003170 r = KVM_API_VERSION;
3171 break;
3172 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02003173 r = -EINVAL;
3174 if (arg)
3175 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02003176 r = kvm_dev_ioctl_create_vm();
3177 break;
Eddie Dong85f455f2007-07-06 12:20:49 +03003178 case KVM_CHECK_EXTENSION: {
3179 int ext = (long)argp;
3180
3181 switch (ext) {
3182 case KVM_CAP_IRQCHIP:
Eddie Dongb6958ce2007-07-18 12:15:21 +03003183 case KVM_CAP_HLT:
Izik Eidus82ce2c92007-10-02 18:52:55 +02003184 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Izik Eidus6fc138d2007-10-09 19:20:39 +02003185 case KVM_CAP_USER_MEMORY:
Eddie Dong85f455f2007-07-06 12:20:49 +03003186 r = 1;
3187 break;
3188 default:
3189 r = 0;
3190 break;
3191 }
Avi Kivity5d308f42007-03-01 17:56:20 +02003192 break;
Eddie Dong85f455f2007-07-06 12:20:49 +03003193 }
Avi Kivity07c45a32007-03-07 13:05:38 +02003194 case KVM_GET_VCPU_MMAP_SIZE:
3195 r = -EINVAL;
3196 if (arg)
3197 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02003198 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02003199 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003200 default:
Carsten Otte043405e2007-10-10 17:16:19 +02003201 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003202 }
3203out:
3204 return r;
3205}
3206
Avi Kivity6aa8b732006-12-10 02:21:36 -08003207static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003208 .unlocked_ioctl = kvm_dev_ioctl,
3209 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003210};
3211
3212static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02003213 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003214 "kvm",
3215 &kvm_chardev_ops,
3216};
3217
Avi Kivity774c47f2007-02-12 00:54:47 -08003218/*
3219 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
3220 * cached on it.
3221 */
3222static void decache_vcpus_on_cpu(int cpu)
3223{
3224 struct kvm *vm;
3225 struct kvm_vcpu *vcpu;
3226 int i;
3227
3228 spin_lock(&kvm_lock);
Shaohua Li11ec2802007-07-23 14:51:37 +08003229 list_for_each_entry(vm, &vm_list, vm_list)
Avi Kivity774c47f2007-02-12 00:54:47 -08003230 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003231 vcpu = vm->vcpus[i];
3232 if (!vcpu)
3233 continue;
Avi Kivity774c47f2007-02-12 00:54:47 -08003234 /*
3235 * If the vcpu is locked, then it is running on some
3236 * other cpu and therefore it is not cached on the
3237 * cpu in question.
3238 *
3239 * If it's not locked, check the last cpu it executed
3240 * on.
3241 */
3242 if (mutex_trylock(&vcpu->mutex)) {
3243 if (vcpu->cpu == cpu) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003244 kvm_x86_ops->vcpu_decache(vcpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08003245 vcpu->cpu = -1;
3246 }
3247 mutex_unlock(&vcpu->mutex);
3248 }
3249 }
3250 spin_unlock(&kvm_lock);
3251}
3252
Avi Kivity1b6c0162007-05-24 13:03:52 +03003253static void hardware_enable(void *junk)
3254{
3255 int cpu = raw_smp_processor_id();
3256
3257 if (cpu_isset(cpu, cpus_hardware_enabled))
3258 return;
3259 cpu_set(cpu, cpus_hardware_enabled);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003260 kvm_x86_ops->hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003261}
3262
3263static void hardware_disable(void *junk)
3264{
3265 int cpu = raw_smp_processor_id();
3266
3267 if (!cpu_isset(cpu, cpus_hardware_enabled))
3268 return;
3269 cpu_clear(cpu, cpus_hardware_enabled);
3270 decache_vcpus_on_cpu(cpu);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003271 kvm_x86_ops->hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003272}
3273
Avi Kivity774c47f2007-02-12 00:54:47 -08003274static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
3275 void *v)
3276{
3277 int cpu = (long)v;
3278
3279 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03003280 case CPU_DYING:
3281 case CPU_DYING_FROZEN:
Avi Kivity6ec8a852007-08-19 15:57:26 +03003282 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3283 cpu);
3284 hardware_disable(NULL);
3285 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08003286 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003287 case CPU_UP_CANCELED_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003288 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3289 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003290 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003291 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02003292 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003293 case CPU_ONLINE_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003294 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
3295 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003296 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003297 break;
3298 }
3299 return NOTIFY_OK;
3300}
3301
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003302static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04003303 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003304{
3305 if (val == SYS_RESTART) {
3306 /*
3307 * Some (well, at least mine) BIOSes hang on reboot if
3308 * in vmx root mode.
3309 */
3310 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
3311 on_each_cpu(hardware_disable, NULL, 0, 1);
3312 }
3313 return NOTIFY_OK;
3314}
3315
3316static struct notifier_block kvm_reboot_notifier = {
3317 .notifier_call = kvm_reboot,
3318 .priority = 0,
3319};
3320
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04003321void kvm_io_bus_init(struct kvm_io_bus *bus)
3322{
3323 memset(bus, 0, sizeof(*bus));
3324}
3325
3326void kvm_io_bus_destroy(struct kvm_io_bus *bus)
3327{
3328 int i;
3329
3330 for (i = 0; i < bus->dev_count; i++) {
3331 struct kvm_io_device *pos = bus->devs[i];
3332
3333 kvm_iodevice_destructor(pos);
3334 }
3335}
3336
3337struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
3338{
3339 int i;
3340
3341 for (i = 0; i < bus->dev_count; i++) {
3342 struct kvm_io_device *pos = bus->devs[i];
3343
3344 if (pos->in_range(pos, addr))
3345 return pos;
3346 }
3347
3348 return NULL;
3349}
3350
3351void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
3352{
3353 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
3354
3355 bus->devs[bus->dev_count++] = dev;
3356}
3357
Avi Kivity774c47f2007-02-12 00:54:47 -08003358static struct notifier_block kvm_cpu_notifier = {
3359 .notifier_call = kvm_cpu_hotplug,
3360 .priority = 20, /* must be > scheduler priority */
3361};
3362
Avi Kivity1165f5f2007-04-19 17:27:43 +03003363static u64 stat_get(void *_offset)
3364{
3365 unsigned offset = (long)_offset;
3366 u64 total = 0;
3367 struct kvm *kvm;
3368 struct kvm_vcpu *vcpu;
3369 int i;
3370
3371 spin_lock(&kvm_lock);
3372 list_for_each_entry(kvm, &vm_list, vm_list)
3373 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003374 vcpu = kvm->vcpus[i];
3375 if (vcpu)
3376 total += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03003377 }
3378 spin_unlock(&kvm_lock);
3379 return total;
3380}
3381
Rusty Russell3dea7ca2007-08-01 10:12:22 +10003382DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n");
Avi Kivity1165f5f2007-04-19 17:27:43 +03003383
Avi Kivity6aa8b732006-12-10 02:21:36 -08003384static __init void kvm_init_debug(void)
3385{
3386 struct kvm_stats_debugfs_item *p;
3387
Al Viro8b6d44c2007-02-09 16:38:40 +00003388 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003389 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03003390 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
3391 (void *)(long)p->offset,
3392 &stat_fops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003393}
3394
3395static void kvm_exit_debug(void)
3396{
3397 struct kvm_stats_debugfs_item *p;
3398
3399 for (p = debugfs_entries; p->name; ++p)
3400 debugfs_remove(p->dentry);
3401 debugfs_remove(debugfs_dir);
3402}
3403
Avi Kivity59ae6c62007-02-12 00:54:48 -08003404static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3405{
Avi Kivity4267c412007-05-24 13:09:41 +03003406 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003407 return 0;
3408}
3409
3410static int kvm_resume(struct sys_device *dev)
3411{
Avi Kivity4267c412007-05-24 13:09:41 +03003412 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003413 return 0;
3414}
3415
3416static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01003417 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08003418 .suspend = kvm_suspend,
3419 .resume = kvm_resume,
3420};
3421
3422static struct sys_device kvm_sysdev = {
3423 .id = 0,
3424 .cls = &kvm_sysdev_class,
3425};
3426
Izik Eiduscea7bb22007-10-17 19:17:48 +02003427struct page *bad_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003428
Avi Kivity15ad7142007-07-11 18:17:21 +03003429static inline
3430struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
3431{
3432 return container_of(pn, struct kvm_vcpu, preempt_notifier);
3433}
3434
3435static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
3436{
3437 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3438
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003439 kvm_x86_ops->vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003440}
3441
3442static void kvm_sched_out(struct preempt_notifier *pn,
3443 struct task_struct *next)
3444{
3445 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3446
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003447 kvm_x86_ops->vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003448}
3449
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003450int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10003451 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003452{
3453 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003454 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003455
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003456 if (kvm_x86_ops) {
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08003457 printk(KERN_ERR "kvm: already loaded the other module\n");
3458 return -EEXIST;
3459 }
3460
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003461 if (!ops->cpu_has_kvm_support()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003462 printk(KERN_ERR "kvm: no hardware support\n");
3463 return -EOPNOTSUPP;
3464 }
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003465 if (ops->disabled_by_bios()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003466 printk(KERN_ERR "kvm: disabled by bios\n");
3467 return -EOPNOTSUPP;
3468 }
3469
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003470 kvm_x86_ops = ops;
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003471
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003472 r = kvm_x86_ops->hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003473 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02003474 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003475
Yang, Sheng002c7f72007-07-31 14:23:01 +03003476 for_each_online_cpu(cpu) {
3477 smp_call_function_single(cpu,
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003478 kvm_x86_ops->check_processor_compatibility,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003479 &r, 0, 1);
3480 if (r < 0)
3481 goto out_free_0;
3482 }
3483
Avi Kivity1b6c0162007-05-24 13:03:52 +03003484 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003485 r = register_cpu_notifier(&kvm_cpu_notifier);
3486 if (r)
3487 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003488 register_reboot_notifier(&kvm_reboot_notifier);
3489
Avi Kivity59ae6c62007-02-12 00:54:48 -08003490 r = sysdev_class_register(&kvm_sysdev_class);
3491 if (r)
3492 goto out_free_2;
3493
3494 r = sysdev_register(&kvm_sysdev);
3495 if (r)
3496 goto out_free_3;
3497
Rusty Russellc16f8622007-07-30 21:12:19 +10003498 /* A kmem cache lets us meet the alignment requirements of fx_save. */
3499 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
3500 __alignof__(struct kvm_vcpu), 0, 0);
3501 if (!kvm_vcpu_cache) {
3502 r = -ENOMEM;
3503 goto out_free_4;
3504 }
3505
Avi Kivity6aa8b732006-12-10 02:21:36 -08003506 kvm_chardev_ops.owner = module;
3507
3508 r = misc_register(&kvm_dev);
3509 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04003510 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08003511 goto out_free;
3512 }
3513
Avi Kivity15ad7142007-07-11 18:17:21 +03003514 kvm_preempt_ops.sched_in = kvm_sched_in;
3515 kvm_preempt_ops.sched_out = kvm_sched_out;
3516
Avi Kivityc7addb92007-09-16 18:58:32 +02003517 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
3518
3519 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003520
3521out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10003522 kmem_cache_destroy(kvm_vcpu_cache);
3523out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08003524 sysdev_unregister(&kvm_sysdev);
3525out_free_3:
3526 sysdev_class_unregister(&kvm_sysdev_class);
3527out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003528 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08003529 unregister_cpu_notifier(&kvm_cpu_notifier);
3530out_free_1:
Avi Kivity1b6c0162007-05-24 13:03:52 +03003531 on_each_cpu(hardware_disable, NULL, 0, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03003532out_free_0:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003533 kvm_x86_ops->hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02003534out:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003535 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003536 return r;
3537}
Mike Dayd77c26f2007-10-08 09:02:08 -04003538EXPORT_SYMBOL_GPL(kvm_init_x86);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003539
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003540void kvm_exit_x86(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003541{
3542 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10003543 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003544 sysdev_unregister(&kvm_sysdev);
3545 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003546 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003547 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003548 on_each_cpu(hardware_disable, NULL, 0, 1);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003549 kvm_x86_ops->hardware_unsetup();
3550 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003551}
Mike Dayd77c26f2007-10-08 09:02:08 -04003552EXPORT_SYMBOL_GPL(kvm_exit_x86);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003553
3554static __init int kvm_init(void)
3555{
Avi Kivity37e29d92007-02-20 14:07:37 +02003556 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003557
Avi Kivityb5a33a72007-04-15 16:31:09 +03003558 r = kvm_mmu_module_init();
3559 if (r)
3560 goto out4;
3561
Avi Kivity6aa8b732006-12-10 02:21:36 -08003562 kvm_init_debug();
3563
Carsten Otte043405e2007-10-10 17:16:19 +02003564 kvm_arch_init();
Michael Riepebf591b22006-12-22 01:05:36 -08003565
Izik Eiduscea7bb22007-10-17 19:17:48 +02003566 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
Mike Dayd77c26f2007-10-08 09:02:08 -04003567
3568 if (bad_page == NULL) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003569 r = -ENOMEM;
3570 goto out;
3571 }
3572
Avi Kivity58e690e2007-02-26 16:29:43 +02003573 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003574
3575out:
3576 kvm_exit_debug();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003577 kvm_mmu_module_exit();
3578out4:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003579 return r;
3580}
3581
3582static __exit void kvm_exit(void)
3583{
3584 kvm_exit_debug();
Izik Eiduscea7bb22007-10-17 19:17:48 +02003585 __free_page(bad_page);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003586 kvm_mmu_module_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003587}
3588
3589module_init(kvm_init)
3590module_exit(kvm_exit)