blob: ec696887b2224c13d030f20e9e44bc1223c76feb [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>
Avi Kivity6aa8b732006-12-10 02:21:36 -080045
Avi Kivitye4956062007-06-28 14:15:57 -040046#include <asm/processor.h>
47#include <asm/msr.h>
48#include <asm/io.h>
49#include <asm/uaccess.h>
50#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080051
52MODULE_AUTHOR("Qumranet");
53MODULE_LICENSE("GPL");
54
Avi Kivity133de902007-02-12 00:54:44 -080055static DEFINE_SPINLOCK(kvm_lock);
56static LIST_HEAD(vm_list);
57
Avi Kivity1b6c0162007-05-24 13:03:52 +030058static cpumask_t cpus_hardware_enabled;
59
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +030060struct kvm_x86_ops *kvm_x86_ops;
Rusty Russellc16f8622007-07-30 21:12:19 +100061struct kmem_cache *kvm_vcpu_cache;
62EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030063
Avi Kivity15ad7142007-07-11 18:17:21 +030064static __read_mostly struct preempt_ops kvm_preempt_ops;
65
Avi Kivity1165f5f2007-04-19 17:27:43 +030066#define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
Avi Kivity6aa8b732006-12-10 02:21:36 -080067
68static struct kvm_stats_debugfs_item {
69 const char *name;
Avi Kivity1165f5f2007-04-19 17:27:43 +030070 int offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -080071 struct dentry *dentry;
72} debugfs_entries[] = {
Avi Kivity1165f5f2007-04-19 17:27:43 +030073 { "pf_fixed", STAT_OFFSET(pf_fixed) },
74 { "pf_guest", STAT_OFFSET(pf_guest) },
75 { "tlb_flush", STAT_OFFSET(tlb_flush) },
76 { "invlpg", STAT_OFFSET(invlpg) },
77 { "exits", STAT_OFFSET(exits) },
78 { "io_exits", STAT_OFFSET(io_exits) },
79 { "mmio_exits", STAT_OFFSET(mmio_exits) },
80 { "signal_exits", STAT_OFFSET(signal_exits) },
81 { "irq_window", STAT_OFFSET(irq_window_exits) },
82 { "halt_exits", STAT_OFFSET(halt_exits) },
Eddie Dongb6958ce2007-07-18 12:15:21 +030083 { "halt_wakeup", STAT_OFFSET(halt_wakeup) },
Avi Kivity1165f5f2007-04-19 17:27:43 +030084 { "request_irq", STAT_OFFSET(request_irq_exits) },
85 { "irq_exits", STAT_OFFSET(irq_exits) },
Avi Kivitye6adf282007-04-30 16:07:54 +030086 { "light_exits", STAT_OFFSET(light_exits) },
Eddie Dong2cc51562007-05-21 07:28:09 +030087 { "efer_reload", STAT_OFFSET(efer_reload) },
Avi Kivity1165f5f2007-04-19 17:27:43 +030088 { NULL }
Avi Kivity6aa8b732006-12-10 02:21:36 -080089};
90
91static struct dentry *debugfs_dir;
92
93#define MAX_IO_MSRS 256
94
Rusty Russell707d92f2007-07-17 23:19:08 +100095#define CR0_RESERVED_BITS \
96 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
97 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
98 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
Rusty Russell66aee912007-07-17 23:34:16 +100099#define CR4_RESERVED_BITS \
100 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
101 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
102 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
103 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
104
Rusty Russell7075bc82007-07-17 23:37:17 +1000105#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800106#define EFER_RESERVED_BITS 0xfffffffffffff2fe
107
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800108#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400109/* LDT or TSS descriptor in the GDT. 16 bytes. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800110struct segment_descriptor_64 {
111 struct segment_descriptor s;
112 u32 base_higher;
113 u32 pad_zero;
114};
115
116#endif
117
Avi Kivitybccf2152007-02-21 18:04:26 +0200118static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
119 unsigned long arg);
120
Avi Kivity6aa8b732006-12-10 02:21:36 -0800121unsigned long segment_base(u16 selector)
122{
123 struct descriptor_table gdt;
124 struct segment_descriptor *d;
125 unsigned long table_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800126 unsigned long v;
127
128 if (selector == 0)
129 return 0;
130
Mike Dayd77c26f2007-10-08 09:02:08 -0400131 asm("sgdt %0" : "=m"(gdt));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800132 table_base = gdt.base;
133
134 if (selector & 4) { /* from ldt */
135 u16 ldt_selector;
136
Mike Dayd77c26f2007-10-08 09:02:08 -0400137 asm("sldt %0" : "=g"(ldt_selector));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800138 table_base = segment_base(ldt_selector);
139 }
140 d = (struct segment_descriptor *)(table_base + (selector & ~7));
Mike Dayd77c26f2007-10-08 09:02:08 -0400141 v = d->base_low | ((unsigned long)d->base_mid << 16) |
142 ((unsigned long)d->base_high << 24);
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800143#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400144 if (d->system == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
145 v |= ((unsigned long) \
146 ((struct segment_descriptor_64 *)d)->base_higher) << 32;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800147#endif
148 return v;
149}
150EXPORT_SYMBOL_GPL(segment_base);
151
James Morris5aacf0c2006-12-22 01:04:55 -0800152static inline int valid_vcpu(int n)
153{
154 return likely(n >= 0 && n < KVM_MAX_VCPUS);
155}
156
Avi Kivity7702fd12007-06-14 16:27:40 +0300157void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
158{
159 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
160 return;
161
162 vcpu->guest_fpu_loaded = 1;
Rusty Russellb114b082007-07-30 21:13:43 +1000163 fx_save(&vcpu->host_fx_image);
164 fx_restore(&vcpu->guest_fx_image);
Avi Kivity7702fd12007-06-14 16:27:40 +0300165}
166EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
167
168void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
169{
170 if (!vcpu->guest_fpu_loaded)
171 return;
172
173 vcpu->guest_fpu_loaded = 0;
Rusty Russellb114b082007-07-30 21:13:43 +1000174 fx_save(&vcpu->guest_fx_image);
175 fx_restore(&vcpu->host_fx_image);
Avi Kivity7702fd12007-06-14 16:27:40 +0300176}
177EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
178
Avi Kivity6aa8b732006-12-10 02:21:36 -0800179/*
180 * Switches to specified vcpu, until a matching vcpu_put()
181 */
Avi Kivitybccf2152007-02-21 18:04:26 +0200182static void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800183{
Avi Kivity15ad7142007-07-11 18:17:21 +0300184 int cpu;
185
Avi Kivitybccf2152007-02-21 18:04:26 +0200186 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300187 cpu = get_cpu();
188 preempt_notifier_register(&vcpu->preempt_notifier);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300189 kvm_x86_ops->vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300190 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200191}
192
Avi Kivity6aa8b732006-12-10 02:21:36 -0800193static void vcpu_put(struct kvm_vcpu *vcpu)
194{
Avi Kivity15ad7142007-07-11 18:17:21 +0300195 preempt_disable();
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300196 kvm_x86_ops->vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300197 preempt_notifier_unregister(&vcpu->preempt_notifier);
198 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800199 mutex_unlock(&vcpu->mutex);
200}
201
Avi Kivityd9e368d2007-06-07 19:18:30 +0300202static void ack_flush(void *_completed)
203{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300204}
205
206void kvm_flush_remote_tlbs(struct kvm *kvm)
207{
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200208 int i, cpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300209 cpumask_t cpus;
210 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300211
Avi Kivityd9e368d2007-06-07 19:18:30 +0300212 cpus_clear(cpus);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000213 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
214 vcpu = kvm->vcpus[i];
215 if (!vcpu)
216 continue;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300217 if (test_and_set_bit(KVM_TLB_FLUSH, &vcpu->requests))
218 continue;
219 cpu = vcpu->cpu;
220 if (cpu != -1 && cpu != raw_smp_processor_id())
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200221 cpu_set(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300222 }
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200223 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300224}
225
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000226int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
227{
228 struct page *page;
229 int r;
230
231 mutex_init(&vcpu->mutex);
232 vcpu->cpu = -1;
233 vcpu->mmu.root_hpa = INVALID_PAGE;
234 vcpu->kvm = kvm;
235 vcpu->vcpu_id = id;
He, Qingc5ec1532007-09-03 17:07:41 +0300236 if (!irqchip_in_kernel(kvm) || id == 0)
237 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
238 else
239 vcpu->mp_state = VCPU_MP_STATE_UNINITIALIZED;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300240 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000241
242 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
243 if (!page) {
244 r = -ENOMEM;
245 goto fail;
246 }
247 vcpu->run = page_address(page);
248
249 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
250 if (!page) {
251 r = -ENOMEM;
252 goto fail_free_run;
253 }
254 vcpu->pio_data = page_address(page);
255
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000256 r = kvm_mmu_create(vcpu);
257 if (r < 0)
258 goto fail_free_pio_data;
259
Rusty Russell76fafa52007-10-08 10:50:48 +1000260 if (irqchip_in_kernel(kvm)) {
261 r = kvm_create_lapic(vcpu);
262 if (r < 0)
263 goto fail_mmu_destroy;
264 }
265
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000266 return 0;
267
Rusty Russell76fafa52007-10-08 10:50:48 +1000268fail_mmu_destroy:
269 kvm_mmu_destroy(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000270fail_free_pio_data:
271 free_page((unsigned long)vcpu->pio_data);
272fail_free_run:
273 free_page((unsigned long)vcpu->run);
274fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000275 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000276}
277EXPORT_SYMBOL_GPL(kvm_vcpu_init);
278
279void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
280{
Rusty Russelld5894442007-10-08 10:48:30 +1000281 kvm_free_lapic(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000282 kvm_mmu_destroy(vcpu);
283 free_page((unsigned long)vcpu->pio_data);
284 free_page((unsigned long)vcpu->run);
285}
286EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
287
Avi Kivityf17abe92007-02-21 19:28:04 +0200288static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800289{
290 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800291
292 if (!kvm)
Avi Kivityf17abe92007-02-21 19:28:04 +0200293 return ERR_PTR(-ENOMEM);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800294
Eddie Dong74906342007-06-19 18:05:03 +0300295 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800296 mutex_init(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800297 INIT_LIST_HEAD(&kvm->active_mmu_pages);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400298 kvm_io_bus_init(&kvm->mmio_bus);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000299 spin_lock(&kvm_lock);
300 list_add(&kvm->vm_list, &vm_list);
301 spin_unlock(&kvm_lock);
Avi Kivityf17abe92007-02-21 19:28:04 +0200302 return kvm;
303}
304
Izik Eidus6fc138d2007-10-09 19:20:39 +0200305static void kvm_free_userspace_physmem(struct kvm_memory_slot *free)
306{
307 int i;
308
309 for (i = 0; i < free->npages; ++i) {
310 if (free->phys_mem[i]) {
311 if (!PageReserved(free->phys_mem[i]))
312 SetPageDirty(free->phys_mem[i]);
313 page_cache_release(free->phys_mem[i]);
314 }
315 }
316}
317
318static void kvm_free_kernel_physmem(struct kvm_memory_slot *free)
319{
320 int i;
321
322 for (i = 0; i < free->npages; ++i)
323 if (free->phys_mem[i])
324 __free_page(free->phys_mem[i]);
325}
326
Avi Kivity6aa8b732006-12-10 02:21:36 -0800327/*
328 * Free any memory in @free but not in @dont.
329 */
330static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
331 struct kvm_memory_slot *dont)
332{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800333 if (!dont || free->phys_mem != dont->phys_mem)
334 if (free->phys_mem) {
Izik Eidus6fc138d2007-10-09 19:20:39 +0200335 if (free->user_alloc)
336 kvm_free_userspace_physmem(free);
337 else
338 kvm_free_kernel_physmem(free);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800339 vfree(free->phys_mem);
340 }
Izik Eidus290fc382007-09-27 14:11:22 +0200341 if (!dont || free->rmap != dont->rmap)
342 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800343
344 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
345 vfree(free->dirty_bitmap);
346
Al Viro8b6d44c2007-02-09 16:38:40 +0000347 free->phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800348 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000349 free->dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800350}
351
352static void kvm_free_physmem(struct kvm *kvm)
353{
354 int i;
355
356 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000357 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800358}
359
Avi Kivity039576c2007-03-20 12:46:50 +0200360static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
361{
362 int i;
363
Rusty Russell3077c4512007-07-30 16:41:57 +1000364 for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i)
Avi Kivity039576c2007-03-20 12:46:50 +0200365 if (vcpu->pio.guest_pages[i]) {
366 __free_page(vcpu->pio.guest_pages[i]);
367 vcpu->pio.guest_pages[i] = NULL;
368 }
369}
370
Avi Kivity7b53aa52007-06-05 12:17:03 +0300371static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
372{
Avi Kivity7b53aa52007-06-05 12:17:03 +0300373 vcpu_load(vcpu);
374 kvm_mmu_unload(vcpu);
375 vcpu_put(vcpu);
376}
377
Avi Kivity6aa8b732006-12-10 02:21:36 -0800378static void kvm_free_vcpus(struct kvm *kvm)
379{
380 unsigned int i;
381
Avi Kivity7b53aa52007-06-05 12:17:03 +0300382 /*
383 * Unpin any mmu pages first.
384 */
385 for (i = 0; i < KVM_MAX_VCPUS; ++i)
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000386 if (kvm->vcpus[i])
387 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
388 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
389 if (kvm->vcpus[i]) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300390 kvm_x86_ops->vcpu_free(kvm->vcpus[i]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000391 kvm->vcpus[i] = NULL;
392 }
393 }
394
Avi Kivity6aa8b732006-12-10 02:21:36 -0800395}
396
Avi Kivityf17abe92007-02-21 19:28:04 +0200397static void kvm_destroy_vm(struct kvm *kvm)
398{
Avi Kivity133de902007-02-12 00:54:44 -0800399 spin_lock(&kvm_lock);
400 list_del(&kvm->vm_list);
401 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300402 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400403 kvm_io_bus_destroy(&kvm->mmio_bus);
Eddie Dong85f455f2007-07-06 12:20:49 +0300404 kfree(kvm->vpic);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300405 kfree(kvm->vioapic);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800406 kvm_free_vcpus(kvm);
407 kvm_free_physmem(kvm);
408 kfree(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200409}
410
411static int kvm_vm_release(struct inode *inode, struct file *filp)
412{
413 struct kvm *kvm = filp->private_data;
414
415 kvm_destroy_vm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800416 return 0;
417}
418
419static void inject_gp(struct kvm_vcpu *vcpu)
420{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300421 kvm_x86_ops->inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800422}
423
Avi Kivity1342d352007-01-05 16:36:39 -0800424/*
425 * Load the pae pdptrs. Return true is they are all valid.
426 */
427static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800428{
429 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
Avi Kivity1342d352007-01-05 16:36:39 -0800430 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800431 int i;
Avi Kivity1342d352007-01-05 16:36:39 -0800432 int ret;
Rusty Russellc820c2a2007-07-25 13:29:51 +1000433 u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800434
Shaohua Li11ec2802007-07-23 14:51:37 +0800435 mutex_lock(&vcpu->kvm->lock);
Izik Eidus195aefd2007-10-01 22:14:18 +0200436 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
437 offset * sizeof(u64), sizeof(pdpte));
438 if (ret < 0) {
Rusty Russellc820c2a2007-07-25 13:29:51 +1000439 ret = 0;
440 goto out;
441 }
Rusty Russellc820c2a2007-07-25 13:29:51 +1000442 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
443 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
Avi Kivity1342d352007-01-05 16:36:39 -0800444 ret = 0;
445 goto out;
446 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800447 }
Rusty Russellc820c2a2007-07-25 13:29:51 +1000448 ret = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800449
Rusty Russellc820c2a2007-07-25 13:29:51 +1000450 memcpy(vcpu->pdptrs, pdpte, sizeof(vcpu->pdptrs));
Avi Kivity1342d352007-01-05 16:36:39 -0800451out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800452 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800453
Avi Kivity1342d352007-01-05 16:36:39 -0800454 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800455}
456
457void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
458{
Rusty Russell707d92f2007-07-17 23:19:08 +1000459 if (cr0 & CR0_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800460 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
461 cr0, vcpu->cr0);
462 inject_gp(vcpu);
463 return;
464 }
465
Rusty Russell707d92f2007-07-17 23:19:08 +1000466 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800467 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
468 inject_gp(vcpu);
469 return;
470 }
471
Rusty Russell707d92f2007-07-17 23:19:08 +1000472 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800473 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
474 "and a clear PE flag\n");
475 inject_gp(vcpu);
476 return;
477 }
478
Rusty Russell707d92f2007-07-17 23:19:08 +1000479 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800480#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800481 if ((vcpu->shadow_efer & EFER_LME)) {
482 int cs_db, cs_l;
483
484 if (!is_pae(vcpu)) {
485 printk(KERN_DEBUG "set_cr0: #GP, start paging "
486 "in long mode while PAE is disabled\n");
487 inject_gp(vcpu);
488 return;
489 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300490 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800491 if (cs_l) {
492 printk(KERN_DEBUG "set_cr0: #GP, start paging "
493 "in long mode while CS.L == 1\n");
494 inject_gp(vcpu);
495 return;
496
497 }
498 } else
499#endif
Avi Kivity1342d352007-01-05 16:36:39 -0800500 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800501 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
502 "reserved bits\n");
503 inject_gp(vcpu);
504 return;
505 }
506
507 }
508
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300509 kvm_x86_ops->set_cr0(vcpu, cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800510 vcpu->cr0 = cr0;
511
Shaohua Li11ec2802007-07-23 14:51:37 +0800512 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800513 kvm_mmu_reset_context(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800514 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800515 return;
516}
517EXPORT_SYMBOL_GPL(set_cr0);
518
519void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
520{
521 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
522}
523EXPORT_SYMBOL_GPL(lmsw);
524
525void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
526{
Rusty Russell66aee912007-07-17 23:34:16 +1000527 if (cr4 & CR4_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800528 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
529 inject_gp(vcpu);
530 return;
531 }
532
Avi Kivitya9058ec2006-12-29 16:49:37 -0800533 if (is_long_mode(vcpu)) {
Rusty Russell66aee912007-07-17 23:34:16 +1000534 if (!(cr4 & X86_CR4_PAE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800535 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
536 "in long mode\n");
537 inject_gp(vcpu);
538 return;
539 }
Rusty Russell66aee912007-07-17 23:34:16 +1000540 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
Avi Kivity1342d352007-01-05 16:36:39 -0800541 && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800542 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
543 inject_gp(vcpu);
Rusty Russell310bc762007-07-23 17:11:02 +1000544 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800545 }
546
Rusty Russell66aee912007-07-17 23:34:16 +1000547 if (cr4 & X86_CR4_VMXE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800548 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
549 inject_gp(vcpu);
550 return;
551 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300552 kvm_x86_ops->set_cr4(vcpu, cr4);
Rusty Russell81f50e32007-09-06 01:20:38 +1000553 vcpu->cr4 = cr4;
Shaohua Li11ec2802007-07-23 14:51:37 +0800554 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800555 kvm_mmu_reset_context(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800556 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800557}
558EXPORT_SYMBOL_GPL(set_cr4);
559
560void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
561{
Avi Kivitya9058ec2006-12-29 16:49:37 -0800562 if (is_long_mode(vcpu)) {
Rusty Russellf802a302007-07-17 23:32:55 +1000563 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800564 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
565 inject_gp(vcpu);
566 return;
567 }
568 } else {
Rusty Russellf802a302007-07-17 23:32:55 +1000569 if (is_pae(vcpu)) {
570 if (cr3 & CR3_PAE_RESERVED_BITS) {
571 printk(KERN_DEBUG
572 "set_cr3: #GP, reserved bits\n");
573 inject_gp(vcpu);
574 return;
575 }
576 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
577 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
578 "reserved bits\n");
579 inject_gp(vcpu);
580 return;
581 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800582 }
Ryan Harper21764862007-09-18 14:05:16 -0500583 /*
584 * We don't check reserved bits in nonpae mode, because
585 * this isn't enforced, and VMware depends on this.
586 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800587 }
588
Shaohua Li11ec2802007-07-23 14:51:37 +0800589 mutex_lock(&vcpu->kvm->lock);
Ingo Molnard21225e2007-01-05 16:36:59 -0800590 /*
591 * Does the new cr3 value map to physical memory? (Note, we
592 * catch an invalid cr3 even in real-mode, because it would
593 * cause trouble later on when we turn on paging anyway.)
594 *
595 * A real CPU would silently accept an invalid cr3 and would
596 * attempt to use it - with largely undefined (and often hard
597 * to debug) behavior on the guest side.
598 */
599 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
600 inject_gp(vcpu);
Rusty Russellfb764412007-07-31 20:45:03 +1000601 else {
602 vcpu->cr3 = cr3;
Ingo Molnard21225e2007-01-05 16:36:59 -0800603 vcpu->mmu.new_cr3(vcpu);
Rusty Russellfb764412007-07-31 20:45:03 +1000604 }
Shaohua Li11ec2802007-07-23 14:51:37 +0800605 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800606}
607EXPORT_SYMBOL_GPL(set_cr3);
608
609void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
610{
Rusty Russell7075bc82007-07-17 23:37:17 +1000611 if (cr8 & CR8_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800612 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
613 inject_gp(vcpu);
614 return;
615 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300616 if (irqchip_in_kernel(vcpu->kvm))
617 kvm_lapic_set_tpr(vcpu, cr8);
618 else
619 vcpu->cr8 = cr8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800620}
621EXPORT_SYMBOL_GPL(set_cr8);
622
Eddie Dong7017fc32007-07-18 11:34:57 +0300623unsigned long get_cr8(struct kvm_vcpu *vcpu)
624{
Eddie Dong97222cc2007-09-12 10:58:04 +0300625 if (irqchip_in_kernel(vcpu->kvm))
626 return kvm_lapic_get_cr8(vcpu);
627 else
628 return vcpu->cr8;
Eddie Dong7017fc32007-07-18 11:34:57 +0300629}
630EXPORT_SYMBOL_GPL(get_cr8);
631
632u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
633{
Eddie Dong97222cc2007-09-12 10:58:04 +0300634 if (irqchip_in_kernel(vcpu->kvm))
635 return vcpu->apic_base;
636 else
637 return vcpu->apic_base;
Eddie Dong7017fc32007-07-18 11:34:57 +0300638}
639EXPORT_SYMBOL_GPL(kvm_get_apic_base);
640
641void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
642{
Eddie Dong97222cc2007-09-12 10:58:04 +0300643 /* TODO: reserve bits check */
644 if (irqchip_in_kernel(vcpu->kvm))
645 kvm_lapic_set_base(vcpu, data);
646 else
647 vcpu->apic_base = data;
Eddie Dong7017fc32007-07-18 11:34:57 +0300648}
649EXPORT_SYMBOL_GPL(kvm_set_apic_base);
650
Avi Kivity6aa8b732006-12-10 02:21:36 -0800651void fx_init(struct kvm_vcpu *vcpu)
652{
Rusty Russellb114b082007-07-30 21:13:43 +1000653 unsigned after_mxcsr_mask;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800654
Rusty Russell9bd01502007-07-30 16:29:56 +1000655 /* Initialize guest FPU by resetting ours and saving into guest's */
656 preempt_disable();
Rusty Russellb114b082007-07-30 21:13:43 +1000657 fx_save(&vcpu->host_fx_image);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800658 fpu_init();
Rusty Russellb114b082007-07-30 21:13:43 +1000659 fx_save(&vcpu->guest_fx_image);
660 fx_restore(&vcpu->host_fx_image);
Rusty Russell9bd01502007-07-30 16:29:56 +1000661 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800662
Amit Shah380102c2007-08-25 11:35:52 +0300663 vcpu->cr0 |= X86_CR0_ET;
Rusty Russellb114b082007-07-30 21:13:43 +1000664 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
665 vcpu->guest_fx_image.mxcsr = 0x1f80;
666 memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask,
667 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800668}
669EXPORT_SYMBOL_GPL(fx_init);
670
671/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800672 * Allocate some memory and give it an address in the guest physical address
673 * space.
674 *
675 * Discontiguous memory is allowed, mostly for framebuffers.
676 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200677static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
Izik Eidus6fc138d2007-10-09 19:20:39 +0200678 struct
679 kvm_userspace_memory_region *mem,
680 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800681{
682 int r;
683 gfn_t base_gfn;
684 unsigned long npages;
685 unsigned long i;
686 struct kvm_memory_slot *memslot;
687 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800688
689 r = -EINVAL;
690 /* General sanity checks */
691 if (mem->memory_size & (PAGE_SIZE - 1))
692 goto out;
693 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
694 goto out;
695 if (mem->slot >= KVM_MEMORY_SLOTS)
696 goto out;
697 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
698 goto out;
699
700 memslot = &kvm->memslots[mem->slot];
701 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
702 npages = mem->memory_size >> PAGE_SHIFT;
703
704 if (!npages)
705 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
706
Shaohua Li11ec2802007-07-23 14:51:37 +0800707 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800708
Avi Kivity6aa8b732006-12-10 02:21:36 -0800709 new = old = *memslot;
710
711 new.base_gfn = base_gfn;
712 new.npages = npages;
713 new.flags = mem->flags;
714
715 /* Disallow changing a memory slot's size. */
716 r = -EINVAL;
717 if (npages && old.npages && npages != old.npages)
718 goto out_unlock;
719
720 /* Check for overlaps */
721 r = -EEXIST;
722 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
723 struct kvm_memory_slot *s = &kvm->memslots[i];
724
725 if (s == memslot)
726 continue;
727 if (!((base_gfn + npages <= s->base_gfn) ||
728 (base_gfn >= s->base_gfn + s->npages)))
729 goto out_unlock;
730 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800731
732 /* Deallocate if slot is being removed */
733 if (!npages)
Al Viro8b6d44c2007-02-09 16:38:40 +0000734 new.phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800735
736 /* Free page dirty bitmap if unneeded */
737 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000738 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800739
740 r = -ENOMEM;
741
742 /* Allocate if a slot is being created */
743 if (npages && !new.phys_mem) {
744 new.phys_mem = vmalloc(npages * sizeof(struct page *));
745
746 if (!new.phys_mem)
Laurent Vivier0d8d2bd2007-08-30 14:56:21 +0200747 goto out_unlock;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800748
Mike Dayd77c26f2007-10-08 09:02:08 -0400749 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200750
751 if (!new.rmap)
752 goto out_unlock;
753
Avi Kivity6aa8b732006-12-10 02:21:36 -0800754 memset(new.phys_mem, 0, npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200755 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Izik Eidus6fc138d2007-10-09 19:20:39 +0200756 if (user_alloc) {
757 unsigned long pages_num;
758
759 new.user_alloc = 1;
760 down_read(&current->mm->mmap_sem);
761
762 pages_num = get_user_pages(current, current->mm,
763 mem->userspace_addr,
764 npages, 1, 1, new.phys_mem,
765 NULL);
766
767 up_read(&current->mm->mmap_sem);
768 if (pages_num != npages)
Laurent Vivier0d8d2bd2007-08-30 14:56:21 +0200769 goto out_unlock;
Izik Eidus6fc138d2007-10-09 19:20:39 +0200770 } else {
771 for (i = 0; i < npages; ++i) {
772 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
773 | __GFP_ZERO);
774 if (!new.phys_mem[i])
775 goto out_unlock;
776 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777 }
778 }
779
780 /* Allocate page dirty bitmap if needed */
781 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
782 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
783
784 new.dirty_bitmap = vmalloc(dirty_bytes);
785 if (!new.dirty_bitmap)
Laurent Vivier0d8d2bd2007-08-30 14:56:21 +0200786 goto out_unlock;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800787 memset(new.dirty_bitmap, 0, dirty_bytes);
788 }
789
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790 if (mem->slot >= kvm->nmemslots)
791 kvm->nmemslots = mem->slot + 1;
792
Izik Eidus82ce2c92007-10-02 18:52:55 +0200793 if (!kvm->n_requested_mmu_pages) {
794 unsigned int n_pages;
795
796 if (npages) {
797 n_pages = npages * KVM_PERMILLE_MMU_PAGES / 1000;
798 kvm_mmu_change_mmu_pages(kvm, kvm->n_alloc_mmu_pages +
799 n_pages);
800 } else {
801 unsigned int nr_mmu_pages;
802
803 n_pages = old.npages * KVM_PERMILLE_MMU_PAGES / 1000;
804 nr_mmu_pages = kvm->n_alloc_mmu_pages - n_pages;
805 nr_mmu_pages = max(nr_mmu_pages,
806 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
807 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
808 }
809 }
810
Avi Kivity6aa8b732006-12-10 02:21:36 -0800811 *memslot = new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800812
Avi Kivity90cb0522007-07-17 13:04:56 +0300813 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
814 kvm_flush_remote_tlbs(kvm);
815
Shaohua Li11ec2802007-07-23 14:51:37 +0800816 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800817
Avi Kivity6aa8b732006-12-10 02:21:36 -0800818 kvm_free_physmem_slot(&old, &new);
819 return 0;
820
821out_unlock:
Shaohua Li11ec2802007-07-23 14:51:37 +0800822 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800823 kvm_free_physmem_slot(&new, &old);
824out:
825 return r;
826}
827
Izik Eidus82ce2c92007-10-02 18:52:55 +0200828static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
829 u32 kvm_nr_mmu_pages)
830{
831 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
832 return -EINVAL;
833
834 mutex_lock(&kvm->lock);
835
836 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
837 kvm->n_requested_mmu_pages = kvm_nr_mmu_pages;
838
839 mutex_unlock(&kvm->lock);
840 return 0;
841}
842
843static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
844{
845 return kvm->n_alloc_mmu_pages;
846}
847
Avi Kivity6aa8b732006-12-10 02:21:36 -0800848/*
849 * Get (and clear) the dirty memory log for a memory slot.
850 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200851static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
852 struct kvm_dirty_log *log)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800853{
854 struct kvm_memory_slot *memslot;
855 int r, i;
856 int n;
857 unsigned long any = 0;
858
Shaohua Li11ec2802007-07-23 14:51:37 +0800859 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800860
Avi Kivity6aa8b732006-12-10 02:21:36 -0800861 r = -EINVAL;
862 if (log->slot >= KVM_MEMORY_SLOTS)
863 goto out;
864
865 memslot = &kvm->memslots[log->slot];
866 r = -ENOENT;
867 if (!memslot->dirty_bitmap)
868 goto out;
869
Uri Lublincd1a4a92007-02-22 16:43:09 +0200870 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800871
Uri Lublincd1a4a92007-02-22 16:43:09 +0200872 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800873 any = memslot->dirty_bitmap[i];
874
875 r = -EFAULT;
876 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
877 goto out;
878
Rusty Russell39214912007-07-31 19:57:47 +1000879 /* If nothing is dirty, don't bother messing with page tables. */
880 if (any) {
Rusty Russell39214912007-07-31 19:57:47 +1000881 kvm_mmu_slot_remove_write_access(kvm, log->slot);
882 kvm_flush_remote_tlbs(kvm);
883 memset(memslot->dirty_bitmap, 0, n);
Rusty Russell39214912007-07-31 19:57:47 +1000884 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800885
886 r = 0;
887
888out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800889 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890 return r;
891}
892
Avi Kivitye8207542007-03-30 16:54:30 +0300893/*
894 * Set a new alias region. Aliases map a portion of physical memory into
895 * another portion. This is useful for memory windows, for example the PC
896 * VGA region.
897 */
898static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
899 struct kvm_memory_alias *alias)
900{
901 int r, n;
902 struct kvm_mem_alias *p;
903
904 r = -EINVAL;
905 /* General sanity checks */
906 if (alias->memory_size & (PAGE_SIZE - 1))
907 goto out;
908 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
909 goto out;
910 if (alias->slot >= KVM_ALIAS_SLOTS)
911 goto out;
912 if (alias->guest_phys_addr + alias->memory_size
913 < alias->guest_phys_addr)
914 goto out;
915 if (alias->target_phys_addr + alias->memory_size
916 < alias->target_phys_addr)
917 goto out;
918
Shaohua Li11ec2802007-07-23 14:51:37 +0800919 mutex_lock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300920
921 p = &kvm->aliases[alias->slot];
922 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
923 p->npages = alias->memory_size >> PAGE_SHIFT;
924 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
925
926 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
927 if (kvm->aliases[n - 1].npages)
928 break;
929 kvm->naliases = n;
930
Avi Kivity90cb0522007-07-17 13:04:56 +0300931 kvm_mmu_zap_all(kvm);
Avi Kivitye8207542007-03-30 16:54:30 +0300932
Shaohua Li11ec2802007-07-23 14:51:37 +0800933 mutex_unlock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300934
935 return 0;
936
937out:
938 return r;
939}
940
He, Qing6ceb9d72007-07-26 11:05:18 +0300941static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
942{
943 int r;
944
945 r = 0;
946 switch (chip->chip_id) {
947 case KVM_IRQCHIP_PIC_MASTER:
Mike Dayd77c26f2007-10-08 09:02:08 -0400948 memcpy(&chip->chip.pic,
He, Qing6ceb9d72007-07-26 11:05:18 +0300949 &pic_irqchip(kvm)->pics[0],
950 sizeof(struct kvm_pic_state));
951 break;
952 case KVM_IRQCHIP_PIC_SLAVE:
Mike Dayd77c26f2007-10-08 09:02:08 -0400953 memcpy(&chip->chip.pic,
He, Qing6ceb9d72007-07-26 11:05:18 +0300954 &pic_irqchip(kvm)->pics[1],
955 sizeof(struct kvm_pic_state));
956 break;
He, Qing6bf9e962007-08-05 10:49:16 +0300957 case KVM_IRQCHIP_IOAPIC:
Mike Dayd77c26f2007-10-08 09:02:08 -0400958 memcpy(&chip->chip.ioapic,
He, Qing6bf9e962007-08-05 10:49:16 +0300959 ioapic_irqchip(kvm),
960 sizeof(struct kvm_ioapic_state));
961 break;
He, Qing6ceb9d72007-07-26 11:05:18 +0300962 default:
963 r = -EINVAL;
964 break;
965 }
966 return r;
967}
968
969static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
970{
971 int r;
972
973 r = 0;
974 switch (chip->chip_id) {
975 case KVM_IRQCHIP_PIC_MASTER:
Mike Dayd77c26f2007-10-08 09:02:08 -0400976 memcpy(&pic_irqchip(kvm)->pics[0],
He, Qing6ceb9d72007-07-26 11:05:18 +0300977 &chip->chip.pic,
978 sizeof(struct kvm_pic_state));
979 break;
980 case KVM_IRQCHIP_PIC_SLAVE:
Mike Dayd77c26f2007-10-08 09:02:08 -0400981 memcpy(&pic_irqchip(kvm)->pics[1],
He, Qing6ceb9d72007-07-26 11:05:18 +0300982 &chip->chip.pic,
983 sizeof(struct kvm_pic_state));
984 break;
He, Qing6bf9e962007-08-05 10:49:16 +0300985 case KVM_IRQCHIP_IOAPIC:
Mike Dayd77c26f2007-10-08 09:02:08 -0400986 memcpy(ioapic_irqchip(kvm),
He, Qing6bf9e962007-08-05 10:49:16 +0300987 &chip->chip.ioapic,
988 sizeof(struct kvm_ioapic_state));
989 break;
He, Qing6ceb9d72007-07-26 11:05:18 +0300990 default:
991 r = -EINVAL;
992 break;
993 }
994 kvm_pic_update_irq(pic_irqchip(kvm));
995 return r;
996}
997
Izik Eidus290fc382007-09-27 14:11:22 +0200998gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
Avi Kivitye8207542007-03-30 16:54:30 +0300999{
1000 int i;
1001 struct kvm_mem_alias *alias;
1002
1003 for (i = 0; i < kvm->naliases; ++i) {
1004 alias = &kvm->aliases[i];
1005 if (gfn >= alias->base_gfn
1006 && gfn < alias->base_gfn + alias->npages)
1007 return alias->target_gfn + gfn - alias->base_gfn;
1008 }
1009 return gfn;
1010}
1011
1012static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001013{
1014 int i;
1015
1016 for (i = 0; i < kvm->nmemslots; ++i) {
1017 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1018
1019 if (gfn >= memslot->base_gfn
1020 && gfn < memslot->base_gfn + memslot->npages)
1021 return memslot;
1022 }
Al Viro8b6d44c2007-02-09 16:38:40 +00001023 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001024}
Avi Kivitye8207542007-03-30 16:54:30 +03001025
1026struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1027{
1028 gfn = unalias_gfn(kvm, gfn);
1029 return __gfn_to_memslot(kvm, gfn);
1030}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001031
Avi Kivity954bbbc22007-03-30 14:02:32 +03001032struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1033{
1034 struct kvm_memory_slot *slot;
1035
Avi Kivitye8207542007-03-30 16:54:30 +03001036 gfn = unalias_gfn(kvm, gfn);
1037 slot = __gfn_to_memslot(kvm, gfn);
Avi Kivity954bbbc22007-03-30 14:02:32 +03001038 if (!slot)
1039 return NULL;
1040 return slot->phys_mem[gfn - slot->base_gfn];
1041}
1042EXPORT_SYMBOL_GPL(gfn_to_page);
1043
Izik Eidus195aefd2007-10-01 22:14:18 +02001044static int next_segment(unsigned long len, int offset)
1045{
1046 if (len > PAGE_SIZE - offset)
1047 return PAGE_SIZE - offset;
1048 else
1049 return len;
1050}
1051
1052int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1053 int len)
1054{
1055 void *page_virt;
1056 struct page *page;
1057
1058 page = gfn_to_page(kvm, gfn);
1059 if (!page)
1060 return -EFAULT;
1061 page_virt = kmap_atomic(page, KM_USER0);
1062
1063 memcpy(data, page_virt + offset, len);
1064
1065 kunmap_atomic(page_virt, KM_USER0);
1066 return 0;
1067}
1068EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1069
1070int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1071{
1072 gfn_t gfn = gpa >> PAGE_SHIFT;
1073 int seg;
1074 int offset = offset_in_page(gpa);
1075 int ret;
1076
1077 while ((seg = next_segment(len, offset)) != 0) {
1078 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1079 if (ret < 0)
1080 return ret;
1081 offset = 0;
1082 len -= seg;
1083 data += seg;
1084 ++gfn;
1085 }
1086 return 0;
1087}
1088EXPORT_SYMBOL_GPL(kvm_read_guest);
1089
1090int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1091 int offset, int len)
1092{
1093 void *page_virt;
1094 struct page *page;
1095
1096 page = gfn_to_page(kvm, gfn);
1097 if (!page)
1098 return -EFAULT;
1099 page_virt = kmap_atomic(page, KM_USER0);
1100
1101 memcpy(page_virt + offset, data, len);
1102
1103 kunmap_atomic(page_virt, KM_USER0);
1104 mark_page_dirty(kvm, gfn);
1105 return 0;
1106}
1107EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1108
1109int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1110 unsigned long len)
1111{
1112 gfn_t gfn = gpa >> PAGE_SHIFT;
1113 int seg;
1114 int offset = offset_in_page(gpa);
1115 int ret;
1116
1117 while ((seg = next_segment(len, offset)) != 0) {
1118 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1119 if (ret < 0)
1120 return ret;
1121 offset = 0;
1122 len -= seg;
1123 data += seg;
1124 ++gfn;
1125 }
1126 return 0;
1127}
1128
1129int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1130{
1131 void *page_virt;
1132 struct page *page;
1133
1134 page = gfn_to_page(kvm, gfn);
1135 if (!page)
1136 return -EFAULT;
1137 page_virt = kmap_atomic(page, KM_USER0);
1138
1139 memset(page_virt + offset, 0, len);
1140
1141 kunmap_atomic(page_virt, KM_USER0);
1142 return 0;
1143}
1144EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1145
1146int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1147{
1148 gfn_t gfn = gpa >> PAGE_SHIFT;
1149 int seg;
1150 int offset = offset_in_page(gpa);
1151 int ret;
1152
1153 while ((seg = next_segment(len, offset)) != 0) {
1154 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1155 if (ret < 0)
1156 return ret;
1157 offset = 0;
1158 len -= seg;
1159 ++gfn;
1160 }
1161 return 0;
1162}
1163EXPORT_SYMBOL_GPL(kvm_clear_guest);
1164
Rusty Russell7e9d6192007-07-31 20:41:14 +10001165/* WARNING: Does not work on aliased pages. */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001166void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1167{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001168 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001169
Rusty Russell7e9d6192007-07-31 20:41:14 +10001170 memslot = __gfn_to_memslot(kvm, gfn);
1171 if (memslot && memslot->dirty_bitmap) {
1172 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001173
Rusty Russell7e9d6192007-07-31 20:41:14 +10001174 /* avoid RMW */
1175 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1176 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001177 }
1178}
1179
Laurent Viviere7d5d762007-07-30 13:41:19 +03001180int emulator_read_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001181 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001182 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001183 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001184{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001185 void *data = val;
1186
1187 while (bytes) {
1188 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1189 unsigned offset = addr & (PAGE_SIZE-1);
1190 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
Izik Eidus195aefd2007-10-01 22:14:18 +02001191 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001192
1193 if (gpa == UNMAPPED_GVA)
1194 return X86EMUL_PROPAGATE_FAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001195 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
1196 if (ret < 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001197 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001198
1199 bytes -= tocopy;
1200 data += tocopy;
1201 addr += tocopy;
1202 }
1203
1204 return X86EMUL_CONTINUE;
1205}
Laurent Viviere7d5d762007-07-30 13:41:19 +03001206EXPORT_SYMBOL_GPL(emulator_read_std);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001207
1208static int emulator_write_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001209 const void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001210 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001211 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001212{
Rusty Russellf0242472007-08-01 10:48:02 +10001213 pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001214 return X86EMUL_UNHANDLEABLE;
1215}
1216
Eddie Dong97222cc2007-09-12 10:58:04 +03001217/*
1218 * Only apic need an MMIO device hook, so shortcut now..
1219 */
1220static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
1221 gpa_t addr)
1222{
1223 struct kvm_io_device *dev;
1224
1225 if (vcpu->apic) {
1226 dev = &vcpu->apic->dev;
1227 if (dev->in_range(dev, addr))
1228 return dev;
1229 }
1230 return NULL;
1231}
1232
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001233static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1234 gpa_t addr)
1235{
Eddie Dong97222cc2007-09-12 10:58:04 +03001236 struct kvm_io_device *dev;
1237
1238 dev = vcpu_find_pervcpu_dev(vcpu, addr);
1239 if (dev == NULL)
1240 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1241 return dev;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001242}
1243
Eddie Dong74906342007-06-19 18:05:03 +03001244static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
1245 gpa_t addr)
1246{
1247 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
1248}
1249
Avi Kivity6aa8b732006-12-10 02:21:36 -08001250static int emulator_read_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001251 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001252 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001253 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001254{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001255 struct kvm_io_device *mmio_dev;
1256 gpa_t gpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001257
1258 if (vcpu->mmio_read_completed) {
1259 memcpy(val, vcpu->mmio_data, bytes);
1260 vcpu->mmio_read_completed = 0;
1261 return X86EMUL_CONTINUE;
Laurent Viviercebff022007-07-30 13:35:24 +03001262 } else if (emulator_read_std(addr, val, bytes, vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001263 == X86EMUL_CONTINUE)
1264 return X86EMUL_CONTINUE;
Avi Kivityd27d4ac2007-02-19 14:37:46 +02001265
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001266 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1267 if (gpa == UNMAPPED_GVA)
1268 return X86EMUL_PROPAGATE_FAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001269
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001270 /*
1271 * Is this MMIO handled locally?
1272 */
1273 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1274 if (mmio_dev) {
1275 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1276 return X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001277 }
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001278
1279 vcpu->mmio_needed = 1;
1280 vcpu->mmio_phys_addr = gpa;
1281 vcpu->mmio_size = bytes;
1282 vcpu->mmio_is_write = 0;
1283
1284 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001285}
1286
Avi Kivityda4a00f2007-01-05 16:36:44 -08001287static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity4c690a12007-04-22 15:28:19 +03001288 const void *val, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001289{
Izik Eidus195aefd2007-10-01 22:14:18 +02001290 int ret;
Avi Kivityda4a00f2007-01-05 16:36:44 -08001291
Izik Eidus195aefd2007-10-01 22:14:18 +02001292 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
1293 if (ret < 0)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001294 return 0;
Shaohua Life551882007-07-23 14:51:39 +08001295 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001296 return 1;
1297}
1298
Avi Kivityb0fcd902007-07-22 18:48:54 +03001299static int emulator_write_emulated_onepage(unsigned long addr,
1300 const void *val,
1301 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001302 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001303{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001304 struct kvm_io_device *mmio_dev;
1305 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001306
Avi Kivityc9047f52007-04-17 10:53:22 +03001307 if (gpa == UNMAPPED_GVA) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001308 kvm_x86_ops->inject_page_fault(vcpu, addr, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001309 return X86EMUL_PROPAGATE_FAULT;
Avi Kivityc9047f52007-04-17 10:53:22 +03001310 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001311
Avi Kivityda4a00f2007-01-05 16:36:44 -08001312 if (emulator_write_phys(vcpu, gpa, val, bytes))
1313 return X86EMUL_CONTINUE;
1314
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001315 /*
1316 * Is this MMIO handled locally?
1317 */
1318 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1319 if (mmio_dev) {
1320 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1321 return X86EMUL_CONTINUE;
1322 }
1323
Avi Kivity6aa8b732006-12-10 02:21:36 -08001324 vcpu->mmio_needed = 1;
1325 vcpu->mmio_phys_addr = gpa;
1326 vcpu->mmio_size = bytes;
1327 vcpu->mmio_is_write = 1;
Avi Kivity4c690a12007-04-22 15:28:19 +03001328 memcpy(vcpu->mmio_data, val, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001329
1330 return X86EMUL_CONTINUE;
1331}
1332
Laurent Viviere7d5d762007-07-30 13:41:19 +03001333int emulator_write_emulated(unsigned long addr,
Avi Kivityb0fcd902007-07-22 18:48:54 +03001334 const void *val,
1335 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001336 struct kvm_vcpu *vcpu)
Avi Kivityb0fcd902007-07-22 18:48:54 +03001337{
1338 /* Crossing a page boundary? */
1339 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1340 int rc, now;
1341
1342 now = -addr & ~PAGE_MASK;
Laurent Viviercebff022007-07-30 13:35:24 +03001343 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001344 if (rc != X86EMUL_CONTINUE)
1345 return rc;
1346 addr += now;
1347 val += now;
1348 bytes -= now;
1349 }
Laurent Viviercebff022007-07-30 13:35:24 +03001350 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001351}
Laurent Viviere7d5d762007-07-30 13:41:19 +03001352EXPORT_SYMBOL_GPL(emulator_write_emulated);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001353
Avi Kivity6aa8b732006-12-10 02:21:36 -08001354static int emulator_cmpxchg_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001355 const void *old,
1356 const void *new,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001357 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001358 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001359{
1360 static int reported;
1361
1362 if (!reported) {
1363 reported = 1;
1364 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1365 }
Laurent Viviercebff022007-07-30 13:35:24 +03001366 return emulator_write_emulated(addr, new, bytes, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001367}
1368
1369static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1370{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001371 return kvm_x86_ops->get_segment_base(vcpu, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001372}
1373
1374int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1375{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001376 return X86EMUL_CONTINUE;
1377}
1378
1379int emulate_clts(struct kvm_vcpu *vcpu)
1380{
Amit Shah404fb882007-11-19 17:57:35 +02001381 kvm_x86_ops->set_cr0(vcpu, vcpu->cr0 & ~X86_CR0_TS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001382 return X86EMUL_CONTINUE;
1383}
1384
Mike Dayd77c26f2007-10-08 09:02:08 -04001385int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001386{
1387 struct kvm_vcpu *vcpu = ctxt->vcpu;
1388
1389 switch (dr) {
1390 case 0 ... 3:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001391 *dest = kvm_x86_ops->get_dr(vcpu, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001392 return X86EMUL_CONTINUE;
1393 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001394 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001395 return X86EMUL_UNHANDLEABLE;
1396 }
1397}
1398
1399int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1400{
1401 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1402 int exception;
1403
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001404 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001405 if (exception) {
1406 /* FIXME: better handling */
1407 return X86EMUL_UNHANDLEABLE;
1408 }
1409 return X86EMUL_CONTINUE;
1410}
1411
Avi Kivity054b1362007-09-12 13:21:09 +03001412void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001413{
1414 static int reported;
1415 u8 opcodes[4];
Avi Kivity054b1362007-09-12 13:21:09 +03001416 unsigned long rip = vcpu->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001417 unsigned long rip_linear;
1418
Avi Kivity054b1362007-09-12 13:21:09 +03001419 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001420
1421 if (reported)
1422 return;
1423
Avi Kivity054b1362007-09-12 13:21:09 +03001424 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001425
Avi Kivity054b1362007-09-12 13:21:09 +03001426 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1427 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001428 reported = 1;
1429}
Avi Kivity054b1362007-09-12 13:21:09 +03001430EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001431
1432struct x86_emulate_ops emulate_ops = {
1433 .read_std = emulator_read_std,
1434 .write_std = emulator_write_std,
1435 .read_emulated = emulator_read_emulated,
1436 .write_emulated = emulator_write_emulated,
1437 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1438};
1439
1440int emulate_instruction(struct kvm_vcpu *vcpu,
1441 struct kvm_run *run,
1442 unsigned long cr2,
Laurent Vivier34273182007-09-18 11:27:37 +02001443 u16 error_code,
1444 int no_decode)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001445{
Laurent Viviera22436b2007-09-24 17:00:58 +02001446 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001447
Avi Kivitye7df56e2007-03-14 15:54:54 +02001448 vcpu->mmio_fault_cr2 = cr2;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001449 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001450
Avi Kivity6aa8b732006-12-10 02:21:36 -08001451 vcpu->mmio_is_write = 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03001452 vcpu->pio.string = 0;
Laurent Vivier34273182007-09-18 11:27:37 +02001453
1454 if (!no_decode) {
1455 int cs_db, cs_l;
1456 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1457
1458 vcpu->emulate_ctxt.vcpu = vcpu;
1459 vcpu->emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
1460 vcpu->emulate_ctxt.cr2 = cr2;
1461 vcpu->emulate_ctxt.mode =
1462 (vcpu->emulate_ctxt.eflags & X86_EFLAGS_VM)
1463 ? X86EMUL_MODE_REAL : cs_l
1464 ? X86EMUL_MODE_PROT64 : cs_db
1465 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1466
1467 if (vcpu->emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1468 vcpu->emulate_ctxt.cs_base = 0;
1469 vcpu->emulate_ctxt.ds_base = 0;
1470 vcpu->emulate_ctxt.es_base = 0;
1471 vcpu->emulate_ctxt.ss_base = 0;
1472 } else {
1473 vcpu->emulate_ctxt.cs_base =
1474 get_segment_base(vcpu, VCPU_SREG_CS);
1475 vcpu->emulate_ctxt.ds_base =
1476 get_segment_base(vcpu, VCPU_SREG_DS);
1477 vcpu->emulate_ctxt.es_base =
1478 get_segment_base(vcpu, VCPU_SREG_ES);
1479 vcpu->emulate_ctxt.ss_base =
1480 get_segment_base(vcpu, VCPU_SREG_SS);
1481 }
1482
1483 vcpu->emulate_ctxt.gs_base =
1484 get_segment_base(vcpu, VCPU_SREG_GS);
1485 vcpu->emulate_ctxt.fs_base =
1486 get_segment_base(vcpu, VCPU_SREG_FS);
1487
1488 r = x86_decode_insn(&vcpu->emulate_ctxt, &emulate_ops);
Laurent Viviera22436b2007-09-24 17:00:58 +02001489 if (r) {
1490 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1491 return EMULATE_DONE;
1492 return EMULATE_FAIL;
1493 }
Laurent Vivier34273182007-09-18 11:27:37 +02001494 }
1495
Laurent Viviera22436b2007-09-24 17:00:58 +02001496 r = x86_emulate_insn(&vcpu->emulate_ctxt, &emulate_ops);
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001497
Laurent Viviere70669a2007-08-05 10:36:40 +03001498 if (vcpu->pio.string)
1499 return EMULATE_DO_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001500
1501 if ((r || vcpu->mmio_is_write) && run) {
Jeff Dike8fc0d082007-07-17 12:26:59 -04001502 run->exit_reason = KVM_EXIT_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001503 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1504 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1505 run->mmio.len = vcpu->mmio_size;
1506 run->mmio.is_write = vcpu->mmio_is_write;
1507 }
1508
1509 if (r) {
Avi Kivitya4360362007-01-05 16:36:45 -08001510 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1511 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001512 if (!vcpu->mmio_needed) {
Avi Kivity054b1362007-09-12 13:21:09 +03001513 kvm_report_emulation_failure(vcpu, "mmio");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001514 return EMULATE_FAIL;
1515 }
1516 return EMULATE_DO_MMIO;
1517 }
1518
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001519 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier34273182007-09-18 11:27:37 +02001520 kvm_x86_ops->set_rflags(vcpu, vcpu->emulate_ctxt.eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001521
Avi Kivity02c83202007-04-29 15:02:17 +03001522 if (vcpu->mmio_is_write) {
1523 vcpu->mmio_needed = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001524 return EMULATE_DO_MMIO;
Avi Kivity02c83202007-04-29 15:02:17 +03001525 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001526
1527 return EMULATE_DONE;
1528}
1529EXPORT_SYMBOL_GPL(emulate_instruction);
1530
Eddie Dongb6958ce2007-07-18 12:15:21 +03001531/*
1532 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1533 */
He, Qingc5ec1532007-09-03 17:07:41 +03001534static void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001535{
1536 DECLARE_WAITQUEUE(wait, current);
1537
1538 add_wait_queue(&vcpu->wq, &wait);
1539
1540 /*
1541 * We will block until either an interrupt or a signal wakes us up
1542 */
He, Qingc5ec1532007-09-03 17:07:41 +03001543 while (!kvm_cpu_has_interrupt(vcpu)
1544 && !signal_pending(current)
1545 && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE
1546 && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) {
Eddie Dongb6958ce2007-07-18 12:15:21 +03001547 set_current_state(TASK_INTERRUPTIBLE);
1548 vcpu_put(vcpu);
1549 schedule();
1550 vcpu_load(vcpu);
1551 }
1552
He, Qingc5ec1532007-09-03 17:07:41 +03001553 __set_current_state(TASK_RUNNING);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001554 remove_wait_queue(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001555}
1556
Avi Kivityd3bef152007-06-05 15:53:05 +03001557int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1558{
Avi Kivityd3bef152007-06-05 15:53:05 +03001559 ++vcpu->stat.halt_exits;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001560 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc5ec1532007-09-03 17:07:41 +03001561 vcpu->mp_state = VCPU_MP_STATE_HALTED;
1562 kvm_vcpu_block(vcpu);
1563 if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE)
1564 return -EINTR;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001565 return 1;
1566 } else {
1567 vcpu->run->exit_reason = KVM_EXIT_HLT;
1568 return 0;
1569 }
Avi Kivityd3bef152007-06-05 15:53:05 +03001570}
1571EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1572
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001573int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
Avi Kivity270fd9b2007-02-19 14:37:47 +02001574{
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001575 unsigned long nr, a0, a1, a2, a3, ret;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001576
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001577 kvm_x86_ops->cache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001578
1579 nr = vcpu->regs[VCPU_REGS_RAX];
1580 a0 = vcpu->regs[VCPU_REGS_RBX];
1581 a1 = vcpu->regs[VCPU_REGS_RCX];
1582 a2 = vcpu->regs[VCPU_REGS_RDX];
1583 a3 = vcpu->regs[VCPU_REGS_RSI];
1584
1585 if (!is_long_mode(vcpu)) {
1586 nr &= 0xFFFFFFFF;
1587 a0 &= 0xFFFFFFFF;
1588 a1 &= 0xFFFFFFFF;
1589 a2 &= 0xFFFFFFFF;
1590 a3 &= 0xFFFFFFFF;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001591 }
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001592
Avi Kivity270fd9b2007-02-19 14:37:47 +02001593 switch (nr) {
1594 default:
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001595 ret = -KVM_ENOSYS;
1596 break;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001597 }
1598 vcpu->regs[VCPU_REGS_RAX] = ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001599 kvm_x86_ops->decache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001600 return 0;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001601}
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001602EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
1603
1604int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
1605{
1606 char instruction[3];
1607 int ret = 0;
1608
1609 mutex_lock(&vcpu->kvm->lock);
1610
1611 /*
1612 * Blow out the MMU to ensure that no other VCPU has an active mapping
1613 * to ensure that the updated hypercall appears atomically across all
1614 * VCPUs.
1615 */
1616 kvm_mmu_zap_all(vcpu->kvm);
1617
1618 kvm_x86_ops->cache_regs(vcpu);
1619 kvm_x86_ops->patch_hypercall(vcpu, instruction);
1620 if (emulator_write_emulated(vcpu->rip, instruction, 3, vcpu)
1621 != X86EMUL_CONTINUE)
1622 ret = -EFAULT;
1623
1624 mutex_unlock(&vcpu->kvm->lock);
1625
1626 return ret;
1627}
Avi Kivity270fd9b2007-02-19 14:37:47 +02001628
Avi Kivity6aa8b732006-12-10 02:21:36 -08001629static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1630{
1631 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1632}
1633
1634void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1635{
1636 struct descriptor_table dt = { limit, base };
1637
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001638 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001639}
1640
1641void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1642{
1643 struct descriptor_table dt = { limit, base };
1644
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001645 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001646}
1647
1648void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1649 unsigned long *rflags)
1650{
1651 lmsw(vcpu, msw);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001652 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001653}
1654
1655unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1656{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001657 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001658 switch (cr) {
1659 case 0:
1660 return vcpu->cr0;
1661 case 2:
1662 return vcpu->cr2;
1663 case 3:
1664 return vcpu->cr3;
1665 case 4:
1666 return vcpu->cr4;
1667 default:
1668 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1669 return 0;
1670 }
1671}
1672
1673void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1674 unsigned long *rflags)
1675{
1676 switch (cr) {
1677 case 0:
1678 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001679 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001680 break;
1681 case 2:
1682 vcpu->cr2 = val;
1683 break;
1684 case 3:
1685 set_cr3(vcpu, val);
1686 break;
1687 case 4:
1688 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1689 break;
1690 default:
1691 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1692 }
1693}
1694
Avi Kivity3bab1f52006-12-29 16:49:48 -08001695int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1696{
1697 u64 data;
1698
1699 switch (msr) {
1700 case 0xc0010010: /* SYSCFG */
1701 case 0xc0010015: /* HWCR */
1702 case MSR_IA32_PLATFORM_ID:
1703 case MSR_IA32_P5_MC_ADDR:
1704 case MSR_IA32_P5_MC_TYPE:
1705 case MSR_IA32_MC0_CTL:
1706 case MSR_IA32_MCG_STATUS:
1707 case MSR_IA32_MCG_CAP:
1708 case MSR_IA32_MC0_MISC:
1709 case MSR_IA32_MC0_MISC+4:
1710 case MSR_IA32_MC0_MISC+8:
1711 case MSR_IA32_MC0_MISC+12:
1712 case MSR_IA32_MC0_MISC+16:
1713 case MSR_IA32_UCODE_REV:
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001714 case MSR_IA32_PERF_STATUS:
Matthew Gregan2dc70942007-05-06 10:59:46 +03001715 case MSR_IA32_EBL_CR_POWERON:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001716 /* MTRR registers */
1717 case 0xfe:
1718 case 0x200 ... 0x2ff:
1719 data = 0;
1720 break;
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001721 case 0xcd: /* fsb frequency */
1722 data = 3;
1723 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001724 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001725 data = kvm_get_apic_base(vcpu);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001726 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001727 case MSR_IA32_MISC_ENABLE:
1728 data = vcpu->ia32_misc_enable_msr;
1729 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001730#ifdef CONFIG_X86_64
1731 case MSR_EFER:
1732 data = vcpu->shadow_efer;
1733 break;
1734#endif
1735 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001736 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001737 return 1;
1738 }
1739 *pdata = data;
1740 return 0;
1741}
1742EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1743
Avi Kivity6aa8b732006-12-10 02:21:36 -08001744/*
1745 * Reads an msr value (of 'msr_index') into 'pdata'.
1746 * Returns 0 on success, non-0 otherwise.
1747 * Assumes vcpu_load() was already called.
1748 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001749int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001750{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001751 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001752}
1753
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001754#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001755
Avi Kivity3bab1f52006-12-29 16:49:48 -08001756static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001757{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001758 if (efer & EFER_RESERVED_BITS) {
1759 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1760 efer);
1761 inject_gp(vcpu);
1762 return;
1763 }
1764
1765 if (is_paging(vcpu)
1766 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1767 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1768 inject_gp(vcpu);
1769 return;
1770 }
1771
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001772 kvm_x86_ops->set_efer(vcpu, efer);
Avi Kivity7725f0b2006-12-13 00:34:01 -08001773
Avi Kivity6aa8b732006-12-10 02:21:36 -08001774 efer &= ~EFER_LMA;
1775 efer |= vcpu->shadow_efer & EFER_LMA;
1776
1777 vcpu->shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001778}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001779
1780#endif
1781
Avi Kivity3bab1f52006-12-29 16:49:48 -08001782int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1783{
1784 switch (msr) {
1785#ifdef CONFIG_X86_64
1786 case MSR_EFER:
1787 set_efer(vcpu, data);
1788 break;
1789#endif
1790 case MSR_IA32_MC0_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001791 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
Avi Kivity3bab1f52006-12-29 16:49:48 -08001792 __FUNCTION__, data);
1793 break;
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001794 case MSR_IA32_MCG_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001795 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001796 __FUNCTION__, data);
1797 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001798 case MSR_IA32_UCODE_REV:
1799 case MSR_IA32_UCODE_WRITE:
1800 case 0x200 ... 0x2ff: /* MTRRs */
1801 break;
1802 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001803 kvm_set_apic_base(vcpu, data);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001804 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001805 case MSR_IA32_MISC_ENABLE:
1806 vcpu->ia32_misc_enable_msr = data;
1807 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001808 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001809 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001810 return 1;
1811 }
1812 return 0;
1813}
1814EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1815
Avi Kivity6aa8b732006-12-10 02:21:36 -08001816/*
1817 * Writes msr value into into the appropriate "register".
1818 * Returns 0 on success, non-0 otherwise.
1819 * Assumes vcpu_load() was already called.
1820 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001821int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001822{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001823 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001824}
1825
1826void kvm_resched(struct kvm_vcpu *vcpu)
1827{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001828 if (!need_resched())
1829 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001830 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001831}
1832EXPORT_SYMBOL_GPL(kvm_resched);
1833
Avi Kivity06465c52007-02-28 20:46:53 +02001834void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1835{
1836 int i;
1837 u32 function;
1838 struct kvm_cpuid_entry *e, *best;
1839
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001840 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001841 function = vcpu->regs[VCPU_REGS_RAX];
1842 vcpu->regs[VCPU_REGS_RAX] = 0;
1843 vcpu->regs[VCPU_REGS_RBX] = 0;
1844 vcpu->regs[VCPU_REGS_RCX] = 0;
1845 vcpu->regs[VCPU_REGS_RDX] = 0;
1846 best = NULL;
1847 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1848 e = &vcpu->cpuid_entries[i];
1849 if (e->function == function) {
1850 best = e;
1851 break;
1852 }
1853 /*
1854 * Both basic or both extended?
1855 */
1856 if (((e->function ^ function) & 0x80000000) == 0)
1857 if (!best || e->function > best->function)
1858 best = e;
1859 }
1860 if (best) {
1861 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1862 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1863 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1864 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1865 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001866 kvm_x86_ops->decache_regs(vcpu);
1867 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001868}
1869EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1870
Avi Kivity039576c2007-03-20 12:46:50 +02001871static int pio_copy_data(struct kvm_vcpu *vcpu)
Avi Kivity46fc1472007-02-22 19:39:30 +02001872{
Avi Kivity039576c2007-03-20 12:46:50 +02001873 void *p = vcpu->pio_data;
1874 void *q;
1875 unsigned bytes;
1876 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1877
Avi Kivity039576c2007-03-20 12:46:50 +02001878 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1879 PAGE_KERNEL);
1880 if (!q) {
Avi Kivity039576c2007-03-20 12:46:50 +02001881 free_pio_guest_pages(vcpu);
1882 return -ENOMEM;
1883 }
1884 q += vcpu->pio.guest_page_offset;
1885 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1886 if (vcpu->pio.in)
1887 memcpy(q, p, bytes);
1888 else
1889 memcpy(p, q, bytes);
1890 q -= vcpu->pio.guest_page_offset;
1891 vunmap(q);
Avi Kivity039576c2007-03-20 12:46:50 +02001892 free_pio_guest_pages(vcpu);
1893 return 0;
1894}
1895
1896static int complete_pio(struct kvm_vcpu *vcpu)
1897{
1898 struct kvm_pio_request *io = &vcpu->pio;
Avi Kivity46fc1472007-02-22 19:39:30 +02001899 long delta;
Avi Kivity039576c2007-03-20 12:46:50 +02001900 int r;
Avi Kivity46fc1472007-02-22 19:39:30 +02001901
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001902 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001903
1904 if (!io->string) {
Avi Kivity039576c2007-03-20 12:46:50 +02001905 if (io->in)
1906 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
Avi Kivity46fc1472007-02-22 19:39:30 +02001907 io->size);
1908 } else {
Avi Kivity039576c2007-03-20 12:46:50 +02001909 if (io->in) {
1910 r = pio_copy_data(vcpu);
1911 if (r) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001912 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02001913 return r;
1914 }
1915 }
1916
Avi Kivity46fc1472007-02-22 19:39:30 +02001917 delta = 1;
1918 if (io->rep) {
Avi Kivity039576c2007-03-20 12:46:50 +02001919 delta *= io->cur_count;
Avi Kivity46fc1472007-02-22 19:39:30 +02001920 /*
1921 * The size of the register should really depend on
1922 * current address size.
1923 */
1924 vcpu->regs[VCPU_REGS_RCX] -= delta;
1925 }
Avi Kivity039576c2007-03-20 12:46:50 +02001926 if (io->down)
Avi Kivity46fc1472007-02-22 19:39:30 +02001927 delta = -delta;
1928 delta *= io->size;
Avi Kivity039576c2007-03-20 12:46:50 +02001929 if (io->in)
Avi Kivity46fc1472007-02-22 19:39:30 +02001930 vcpu->regs[VCPU_REGS_RDI] += delta;
1931 else
1932 vcpu->regs[VCPU_REGS_RSI] += delta;
1933 }
1934
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001935 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001936
Avi Kivity039576c2007-03-20 12:46:50 +02001937 io->count -= io->cur_count;
1938 io->cur_count = 0;
1939
Avi Kivity039576c2007-03-20 12:46:50 +02001940 return 0;
Avi Kivity46fc1472007-02-22 19:39:30 +02001941}
1942
Eddie Dong65619eb2007-07-17 11:52:33 +03001943static void kernel_pio(struct kvm_io_device *pio_dev,
1944 struct kvm_vcpu *vcpu,
1945 void *pd)
Eddie Dong74906342007-06-19 18:05:03 +03001946{
1947 /* TODO: String I/O for in kernel device */
1948
Eddie Dong9cf98822007-07-22 10:36:31 +03001949 mutex_lock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001950 if (vcpu->pio.in)
1951 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1952 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001953 pd);
Eddie Dong74906342007-06-19 18:05:03 +03001954 else
1955 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1956 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001957 pd);
Eddie Dong9cf98822007-07-22 10:36:31 +03001958 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001959}
1960
1961static void pio_string_write(struct kvm_io_device *pio_dev,
1962 struct kvm_vcpu *vcpu)
1963{
1964 struct kvm_pio_request *io = &vcpu->pio;
1965 void *pd = vcpu->pio_data;
1966 int i;
1967
Eddie Dong9cf98822007-07-22 10:36:31 +03001968 mutex_lock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001969 for (i = 0; i < io->cur_count; i++) {
1970 kvm_iodevice_write(pio_dev, io->port,
1971 io->size,
1972 pd);
1973 pd += io->size;
1974 }
Eddie Dong9cf98822007-07-22 10:36:31 +03001975 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001976}
1977
Mike Dayd77c26f2007-10-08 09:02:08 -04001978int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
Laurent Vivier3090dd72007-08-05 10:43:32 +03001979 int size, unsigned port)
1980{
1981 struct kvm_io_device *pio_dev;
1982
1983 vcpu->run->exit_reason = KVM_EXIT_IO;
1984 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1985 vcpu->run->io.size = vcpu->pio.size = size;
1986 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1987 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1;
1988 vcpu->run->io.port = vcpu->pio.port = port;
1989 vcpu->pio.in = in;
1990 vcpu->pio.string = 0;
1991 vcpu->pio.down = 0;
1992 vcpu->pio.guest_page_offset = 0;
1993 vcpu->pio.rep = 0;
1994
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001995 kvm_x86_ops->cache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03001996 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001997 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03001998
Avi Kivity0967b7b2007-09-15 17:34:36 +03001999 kvm_x86_ops->skip_emulated_instruction(vcpu);
2000
Laurent Vivier3090dd72007-08-05 10:43:32 +03002001 pio_dev = vcpu_find_pio_dev(vcpu, port);
2002 if (pio_dev) {
2003 kernel_pio(pio_dev, vcpu, vcpu->pio_data);
2004 complete_pio(vcpu);
2005 return 1;
2006 }
2007 return 0;
2008}
2009EXPORT_SYMBOL_GPL(kvm_emulate_pio);
2010
2011int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2012 int size, unsigned long count, int down,
Avi Kivity039576c2007-03-20 12:46:50 +02002013 gva_t address, int rep, unsigned port)
2014{
2015 unsigned now, in_page;
Eddie Dong65619eb2007-07-17 11:52:33 +03002016 int i, ret = 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002017 int nr_pages = 1;
2018 struct page *page;
Eddie Dong74906342007-06-19 18:05:03 +03002019 struct kvm_io_device *pio_dev;
Avi Kivity039576c2007-03-20 12:46:50 +02002020
2021 vcpu->run->exit_reason = KVM_EXIT_IO;
2022 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Laurent Vivier3090dd72007-08-05 10:43:32 +03002023 vcpu->run->io.size = vcpu->pio.size = size;
Avi Kivity039576c2007-03-20 12:46:50 +02002024 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Laurent Vivier3090dd72007-08-05 10:43:32 +03002025 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count;
2026 vcpu->run->io.port = vcpu->pio.port = port;
Avi Kivity039576c2007-03-20 12:46:50 +02002027 vcpu->pio.in = in;
Laurent Vivier3090dd72007-08-05 10:43:32 +03002028 vcpu->pio.string = 1;
Avi Kivity039576c2007-03-20 12:46:50 +02002029 vcpu->pio.down = down;
2030 vcpu->pio.guest_page_offset = offset_in_page(address);
2031 vcpu->pio.rep = rep;
2032
Avi Kivity039576c2007-03-20 12:46:50 +02002033 if (!count) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002034 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02002035 return 1;
2036 }
2037
Avi Kivity039576c2007-03-20 12:46:50 +02002038 if (!down)
2039 in_page = PAGE_SIZE - offset_in_page(address);
2040 else
2041 in_page = offset_in_page(address) + size;
2042 now = min(count, (unsigned long)in_page / size);
2043 if (!now) {
2044 /*
2045 * String I/O straddles page boundary. Pin two guest pages
2046 * so that we satisfy atomicity constraints. Do just one
2047 * transaction to avoid complexity.
2048 */
2049 nr_pages = 2;
2050 now = 1;
2051 }
2052 if (down) {
2053 /*
2054 * String I/O in reverse. Yuck. Kill the guest, fix later.
2055 */
Rusty Russellf0242472007-08-01 10:48:02 +10002056 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivity039576c2007-03-20 12:46:50 +02002057 inject_gp(vcpu);
2058 return 1;
2059 }
2060 vcpu->run->io.count = now;
2061 vcpu->pio.cur_count = now;
2062
Avi Kivity0967b7b2007-09-15 17:34:36 +03002063 if (vcpu->pio.cur_count == vcpu->pio.count)
2064 kvm_x86_ops->skip_emulated_instruction(vcpu);
2065
Avi Kivity039576c2007-03-20 12:46:50 +02002066 for (i = 0; i < nr_pages; ++i) {
Shaohua Li11ec2802007-07-23 14:51:37 +08002067 mutex_lock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02002068 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
2069 if (page)
2070 get_page(page);
2071 vcpu->pio.guest_pages[i] = page;
Shaohua Li11ec2802007-07-23 14:51:37 +08002072 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02002073 if (!page) {
2074 inject_gp(vcpu);
2075 free_pio_guest_pages(vcpu);
2076 return 1;
2077 }
2078 }
2079
Laurent Vivier3090dd72007-08-05 10:43:32 +03002080 pio_dev = vcpu_find_pio_dev(vcpu, port);
Eddie Dong65619eb2007-07-17 11:52:33 +03002081 if (!vcpu->pio.in) {
2082 /* string PIO write */
2083 ret = pio_copy_data(vcpu);
2084 if (ret >= 0 && pio_dev) {
2085 pio_string_write(pio_dev, vcpu);
2086 complete_pio(vcpu);
2087 if (vcpu->pio.count == 0)
2088 ret = 1;
2089 }
2090 } else if (pio_dev)
Rusty Russellf0242472007-08-01 10:48:02 +10002091 pr_unimpl(vcpu, "no string pio read support yet, "
Eddie Dong65619eb2007-07-17 11:52:33 +03002092 "port %x size %d count %ld\n",
2093 port, size, count);
2094
2095 return ret;
Avi Kivity039576c2007-03-20 12:46:50 +02002096}
Laurent Vivier3090dd72007-08-05 10:43:32 +03002097EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
Avi Kivity039576c2007-03-20 12:46:50 +02002098
Avi Kivity04d2cc72007-09-10 18:10:54 +03002099/*
2100 * Check if userspace requested an interrupt window, and that the
2101 * interrupt window is open.
2102 *
2103 * No need to exit to userspace if we already have an interrupt queued.
2104 */
2105static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2106 struct kvm_run *kvm_run)
2107{
2108 return (!vcpu->irq_summary &&
2109 kvm_run->request_interrupt_window &&
2110 vcpu->interrupt_window_open &&
2111 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
2112}
2113
2114static void post_kvm_run_save(struct kvm_vcpu *vcpu,
2115 struct kvm_run *kvm_run)
2116{
2117 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
2118 kvm_run->cr8 = get_cr8(vcpu);
2119 kvm_run->apic_base = kvm_get_apic_base(vcpu);
2120 if (irqchip_in_kernel(vcpu->kvm))
2121 kvm_run->ready_for_interrupt_injection = 1;
2122 else
2123 kvm_run->ready_for_interrupt_injection =
2124 (vcpu->interrupt_window_open &&
2125 vcpu->irq_summary == 0);
2126}
2127
2128static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2129{
2130 int r;
2131
2132 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002133 pr_debug("vcpu %d received sipi with vector # %x\n",
Avi Kivity04d2cc72007-09-10 18:10:54 +03002134 vcpu->vcpu_id, vcpu->sipi_vector);
2135 kvm_lapic_reset(vcpu);
2136 kvm_x86_ops->vcpu_reset(vcpu);
2137 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
2138 }
2139
2140preempted:
2141 if (vcpu->guest_debug.enabled)
2142 kvm_x86_ops->guest_debug_pre(vcpu);
2143
2144again:
2145 r = kvm_mmu_reload(vcpu);
2146 if (unlikely(r))
2147 goto out;
2148
2149 preempt_disable();
2150
2151 kvm_x86_ops->prepare_guest_switch(vcpu);
2152 kvm_load_guest_fpu(vcpu);
2153
2154 local_irq_disable();
2155
2156 if (signal_pending(current)) {
2157 local_irq_enable();
2158 preempt_enable();
2159 r = -EINTR;
2160 kvm_run->exit_reason = KVM_EXIT_INTR;
2161 ++vcpu->stat.signal_exits;
2162 goto out;
2163 }
2164
2165 if (irqchip_in_kernel(vcpu->kvm))
2166 kvm_x86_ops->inject_pending_irq(vcpu);
2167 else if (!vcpu->mmio_read_completed)
2168 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2169
2170 vcpu->guest_mode = 1;
Laurent Vivierd172fcd2007-10-15 17:00:19 +02002171 kvm_guest_enter();
Avi Kivity04d2cc72007-09-10 18:10:54 +03002172
2173 if (vcpu->requests)
2174 if (test_and_clear_bit(KVM_TLB_FLUSH, &vcpu->requests))
2175 kvm_x86_ops->tlb_flush(vcpu);
2176
2177 kvm_x86_ops->run(vcpu, kvm_run);
2178
2179 vcpu->guest_mode = 0;
2180 local_irq_enable();
2181
2182 ++vcpu->stat.exits;
2183
Laurent Vivier0552f732007-10-18 15:19:01 +02002184 /*
2185 * We must have an instruction between local_irq_enable() and
2186 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2187 * the interrupt shadow. The stat.exits increment will do nicely.
2188 * But we need to prevent reordering, hence this barrier():
2189 */
2190 barrier();
2191
2192 kvm_guest_exit();
2193
Avi Kivity04d2cc72007-09-10 18:10:54 +03002194 preempt_enable();
2195
2196 /*
2197 * Profile KVM exit RIPs:
2198 */
2199 if (unlikely(prof_on == KVM_PROFILING)) {
2200 kvm_x86_ops->cache_regs(vcpu);
2201 profile_hit(KVM_PROFILING, (void *)vcpu->rip);
2202 }
2203
2204 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2205
2206 if (r > 0) {
2207 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2208 r = -EINTR;
2209 kvm_run->exit_reason = KVM_EXIT_INTR;
2210 ++vcpu->stat.request_irq_exits;
2211 goto out;
2212 }
2213 if (!need_resched()) {
2214 ++vcpu->stat.light_exits;
2215 goto again;
2216 }
2217 }
2218
2219out:
2220 if (r > 0) {
2221 kvm_resched(vcpu);
2222 goto preempted;
2223 }
2224
2225 post_kvm_run_save(vcpu, kvm_run);
2226
2227 return r;
2228}
2229
2230
Avi Kivitybccf2152007-02-21 18:04:26 +02002231static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002232{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002233 int r;
Avi Kivity1961d272007-03-05 19:46:05 +02002234 sigset_t sigsaved;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002235
Avi Kivitybccf2152007-02-21 18:04:26 +02002236 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002237
He, Qingc5ec1532007-09-03 17:07:41 +03002238 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
2239 kvm_vcpu_block(vcpu);
2240 vcpu_put(vcpu);
2241 return -EAGAIN;
2242 }
2243
Avi Kivity1961d272007-03-05 19:46:05 +02002244 if (vcpu->sigset_active)
2245 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2246
Dor Laor54810342007-02-12 00:54:39 -08002247 /* re-sync apic's tpr */
He, Qing5cd4f6f2007-08-30 17:04:26 +08002248 if (!irqchip_in_kernel(vcpu->kvm))
2249 set_cr8(vcpu, kvm_run->cr8);
Dor Laor54810342007-02-12 00:54:39 -08002250
Avi Kivity02c83202007-04-29 15:02:17 +03002251 if (vcpu->pio.cur_count) {
2252 r = complete_pio(vcpu);
2253 if (r)
2254 goto out;
2255 }
2256
2257 if (vcpu->mmio_needed) {
2258 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
2259 vcpu->mmio_read_completed = 1;
2260 vcpu->mmio_needed = 0;
2261 r = emulate_instruction(vcpu, kvm_run,
Laurent Vivier34273182007-09-18 11:27:37 +02002262 vcpu->mmio_fault_cr2, 0, 1);
Avi Kivity02c83202007-04-29 15:02:17 +03002263 if (r == EMULATE_DO_MMIO) {
2264 /*
2265 * Read-modify-write. Back to userspace.
2266 */
Avi Kivity02c83202007-04-29 15:02:17 +03002267 r = 0;
2268 goto out;
Avi Kivity46fc1472007-02-22 19:39:30 +02002269 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002270 }
2271
Avi Kivity8eb7d332007-03-04 14:17:08 +02002272 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002273 kvm_x86_ops->cache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002274 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002275 kvm_x86_ops->decache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002276 }
2277
Avi Kivity04d2cc72007-09-10 18:10:54 +03002278 r = __vcpu_run(vcpu, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002279
Avi Kivity039576c2007-03-20 12:46:50 +02002280out:
Avi Kivity1961d272007-03-05 19:46:05 +02002281 if (vcpu->sigset_active)
2282 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2283
Avi Kivity6aa8b732006-12-10 02:21:36 -08002284 vcpu_put(vcpu);
2285 return r;
2286}
2287
Avi Kivitybccf2152007-02-21 18:04:26 +02002288static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
2289 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002290{
Avi Kivitybccf2152007-02-21 18:04:26 +02002291 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002292
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002293 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002294
2295 regs->rax = vcpu->regs[VCPU_REGS_RAX];
2296 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
2297 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
2298 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
2299 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
2300 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
2301 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
2302 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002303#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002304 regs->r8 = vcpu->regs[VCPU_REGS_R8];
2305 regs->r9 = vcpu->regs[VCPU_REGS_R9];
2306 regs->r10 = vcpu->regs[VCPU_REGS_R10];
2307 regs->r11 = vcpu->regs[VCPU_REGS_R11];
2308 regs->r12 = vcpu->regs[VCPU_REGS_R12];
2309 regs->r13 = vcpu->regs[VCPU_REGS_R13];
2310 regs->r14 = vcpu->regs[VCPU_REGS_R14];
2311 regs->r15 = vcpu->regs[VCPU_REGS_R15];
2312#endif
2313
2314 regs->rip = vcpu->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002315 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002316
2317 /*
2318 * Don't leak debug flags in case they were set for guest debugging
2319 */
2320 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2321 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2322
2323 vcpu_put(vcpu);
2324
2325 return 0;
2326}
2327
Avi Kivitybccf2152007-02-21 18:04:26 +02002328static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
2329 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002330{
Avi Kivitybccf2152007-02-21 18:04:26 +02002331 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002332
2333 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
2334 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
2335 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
2336 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
2337 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
2338 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
2339 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
2340 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002341#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002342 vcpu->regs[VCPU_REGS_R8] = regs->r8;
2343 vcpu->regs[VCPU_REGS_R9] = regs->r9;
2344 vcpu->regs[VCPU_REGS_R10] = regs->r10;
2345 vcpu->regs[VCPU_REGS_R11] = regs->r11;
2346 vcpu->regs[VCPU_REGS_R12] = regs->r12;
2347 vcpu->regs[VCPU_REGS_R13] = regs->r13;
2348 vcpu->regs[VCPU_REGS_R14] = regs->r14;
2349 vcpu->regs[VCPU_REGS_R15] = regs->r15;
2350#endif
2351
2352 vcpu->rip = regs->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002353 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002354
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002355 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002356
2357 vcpu_put(vcpu);
2358
2359 return 0;
2360}
2361
2362static void get_segment(struct kvm_vcpu *vcpu,
2363 struct kvm_segment *var, int seg)
2364{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002365 return kvm_x86_ops->get_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002366}
2367
Avi Kivitybccf2152007-02-21 18:04:26 +02002368static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2369 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002370{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002371 struct descriptor_table dt;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002372 int pending_vec;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373
Avi Kivitybccf2152007-02-21 18:04:26 +02002374 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002375
2376 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2377 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2378 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2379 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2380 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2381 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2382
2383 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2384 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2385
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002386 kvm_x86_ops->get_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002387 sregs->idt.limit = dt.limit;
2388 sregs->idt.base = dt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002389 kvm_x86_ops->get_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002390 sregs->gdt.limit = dt.limit;
2391 sregs->gdt.base = dt.base;
2392
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002393 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002394 sregs->cr0 = vcpu->cr0;
2395 sregs->cr2 = vcpu->cr2;
2396 sregs->cr3 = vcpu->cr3;
2397 sregs->cr4 = vcpu->cr4;
Eddie Dong7017fc32007-07-18 11:34:57 +03002398 sregs->cr8 = get_cr8(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002399 sregs->efer = vcpu->shadow_efer;
Eddie Dong7017fc32007-07-18 11:34:57 +03002400 sregs->apic_base = kvm_get_apic_base(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002401
Eddie Dong2a8067f2007-08-06 16:29:07 +03002402 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc52fb352007-08-02 14:03:07 +03002403 memset(sregs->interrupt_bitmap, 0,
2404 sizeof sregs->interrupt_bitmap);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002405 pending_vec = kvm_x86_ops->get_irq(vcpu);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002406 if (pending_vec >= 0)
Mike Dayd77c26f2007-10-08 09:02:08 -04002407 set_bit(pending_vec,
2408 (unsigned long *)sregs->interrupt_bitmap);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002409 } else
He, Qingc52fb352007-08-02 14:03:07 +03002410 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2411 sizeof sregs->interrupt_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002412
2413 vcpu_put(vcpu);
2414
2415 return 0;
2416}
2417
2418static void set_segment(struct kvm_vcpu *vcpu,
2419 struct kvm_segment *var, int seg)
2420{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002421 return kvm_x86_ops->set_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002422}
2423
Avi Kivitybccf2152007-02-21 18:04:26 +02002424static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2425 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002426{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002427 int mmu_reset_needed = 0;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002428 int i, pending_vec, max_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002429 struct descriptor_table dt;
2430
Avi Kivitybccf2152007-02-21 18:04:26 +02002431 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002432
Avi Kivity6aa8b732006-12-10 02:21:36 -08002433 dt.limit = sregs->idt.limit;
2434 dt.base = sregs->idt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002435 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002436 dt.limit = sregs->gdt.limit;
2437 dt.base = sregs->gdt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002438 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002439
2440 vcpu->cr2 = sregs->cr2;
2441 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2442 vcpu->cr3 = sregs->cr3;
2443
Eddie Dong7017fc32007-07-18 11:34:57 +03002444 set_cr8(vcpu, sregs->cr8);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002445
2446 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002447#ifdef CONFIG_X86_64
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002448 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002449#endif
Eddie Dong7017fc32007-07-18 11:34:57 +03002450 kvm_set_apic_base(vcpu, sregs->apic_base);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002451
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002452 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity399badf2007-01-05 16:36:38 -08002453
Avi Kivity6aa8b732006-12-10 02:21:36 -08002454 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
Rusty Russell81f50e32007-09-06 01:20:38 +10002455 vcpu->cr0 = sregs->cr0;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002456 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002457
2458 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002459 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Avi Kivity1b0973b2007-01-05 16:36:41 -08002460 if (!is_long_mode(vcpu) && is_pae(vcpu))
2461 load_pdptrs(vcpu, vcpu->cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002462
2463 if (mmu_reset_needed)
2464 kvm_mmu_reset_context(vcpu);
2465
He, Qingc52fb352007-08-02 14:03:07 +03002466 if (!irqchip_in_kernel(vcpu->kvm)) {
2467 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2468 sizeof vcpu->irq_pending);
2469 vcpu->irq_summary = 0;
2470 for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
2471 if (vcpu->irq_pending[i])
2472 __set_bit(i, &vcpu->irq_summary);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002473 } else {
2474 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
2475 pending_vec = find_first_bit(
2476 (const unsigned long *)sregs->interrupt_bitmap,
2477 max_bits);
2478 /* Only pending external irq is handled here */
2479 if (pending_vec < max_bits) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002480 kvm_x86_ops->set_irq(vcpu, pending_vec);
Mike Dayd77c26f2007-10-08 09:02:08 -04002481 pr_debug("Set back pending irq %d\n",
2482 pending_vec);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002483 }
He, Qingc52fb352007-08-02 14:03:07 +03002484 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002485
Avi Kivity024aa1c2007-03-21 13:44:58 +02002486 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2487 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2488 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2489 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2490 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2491 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2492
2493 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2494 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2495
Avi Kivity6aa8b732006-12-10 02:21:36 -08002496 vcpu_put(vcpu);
2497
2498 return 0;
2499}
2500
Rusty Russell1747fb72007-09-06 01:21:32 +10002501void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2502{
2503 struct kvm_segment cs;
2504
2505 get_segment(vcpu, &cs, VCPU_SREG_CS);
2506 *db = cs.db;
2507 *l = cs.l;
2508}
2509EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2510
Avi Kivity6aa8b732006-12-10 02:21:36 -08002511/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002512 * Adapt set_msr() to msr_io()'s calling convention
2513 */
2514static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2515{
Avi Kivity35f3f282007-07-17 14:20:30 +03002516 return kvm_set_msr(vcpu, index, *data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002517}
2518
2519/*
2520 * Read or write a bunch of msrs. All parameters are kernel addresses.
2521 *
2522 * @return number of msrs set successfully.
2523 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002524static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002525 struct kvm_msr_entry *entries,
2526 int (*do_msr)(struct kvm_vcpu *vcpu,
2527 unsigned index, u64 *data))
2528{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002529 int i;
2530
Avi Kivitybccf2152007-02-21 18:04:26 +02002531 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002532
2533 for (i = 0; i < msrs->nmsrs; ++i)
2534 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2535 break;
2536
2537 vcpu_put(vcpu);
2538
2539 return i;
2540}
2541
2542/*
2543 * Read or write a bunch of msrs. Parameters are user addresses.
2544 *
2545 * @return number of msrs set successfully.
2546 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002547static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002548 int (*do_msr)(struct kvm_vcpu *vcpu,
2549 unsigned index, u64 *data),
2550 int writeback)
2551{
2552 struct kvm_msrs msrs;
2553 struct kvm_msr_entry *entries;
2554 int r, n;
2555 unsigned size;
2556
2557 r = -EFAULT;
2558 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2559 goto out;
2560
2561 r = -E2BIG;
2562 if (msrs.nmsrs >= MAX_IO_MSRS)
2563 goto out;
2564
2565 r = -ENOMEM;
2566 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
2567 entries = vmalloc(size);
2568 if (!entries)
2569 goto out;
2570
2571 r = -EFAULT;
2572 if (copy_from_user(entries, user_msrs->entries, size))
2573 goto out_free;
2574
Avi Kivitybccf2152007-02-21 18:04:26 +02002575 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002576 if (r < 0)
2577 goto out_free;
2578
2579 r = -EFAULT;
2580 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2581 goto out_free;
2582
2583 r = n;
2584
2585out_free:
2586 vfree(entries);
2587out:
2588 return r;
2589}
2590
2591/*
2592 * Translate a guest virtual address to a guest physical address.
2593 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002594static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2595 struct kvm_translation *tr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002596{
2597 unsigned long vaddr = tr->linear_address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002598 gpa_t gpa;
2599
Avi Kivitybccf2152007-02-21 18:04:26 +02002600 vcpu_load(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +08002601 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002602 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2603 tr->physical_address = gpa;
2604 tr->valid = gpa != UNMAPPED_GVA;
2605 tr->writeable = 1;
2606 tr->usermode = 0;
Shaohua Li11ec2802007-07-23 14:51:37 +08002607 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002608 vcpu_put(vcpu);
2609
2610 return 0;
2611}
2612
Avi Kivitybccf2152007-02-21 18:04:26 +02002613static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2614 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002615{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002616 if (irq->irq < 0 || irq->irq >= 256)
2617 return -EINVAL;
Eddie Dong97222cc2007-09-12 10:58:04 +03002618 if (irqchip_in_kernel(vcpu->kvm))
2619 return -ENXIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02002620 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002621
2622 set_bit(irq->irq, vcpu->irq_pending);
2623 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2624
2625 vcpu_put(vcpu);
2626
2627 return 0;
2628}
2629
Avi Kivitybccf2152007-02-21 18:04:26 +02002630static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2631 struct kvm_debug_guest *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002632{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002633 int r;
2634
Avi Kivitybccf2152007-02-21 18:04:26 +02002635 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002636
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002637 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002638
2639 vcpu_put(vcpu);
2640
2641 return r;
2642}
2643
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002644static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2645 unsigned long address,
2646 int *type)
2647{
2648 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2649 unsigned long pgoff;
2650 struct page *page;
2651
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002652 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +02002653 if (pgoff == 0)
2654 page = virt_to_page(vcpu->run);
2655 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2656 page = virt_to_page(vcpu->pio_data);
2657 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002658 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002659 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002660 if (type != NULL)
2661 *type = VM_FAULT_MINOR;
2662
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002663 return page;
2664}
2665
2666static struct vm_operations_struct kvm_vcpu_vm_ops = {
2667 .nopage = kvm_vcpu_nopage,
2668};
2669
2670static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2671{
2672 vma->vm_ops = &kvm_vcpu_vm_ops;
2673 return 0;
2674}
2675
Avi Kivitybccf2152007-02-21 18:04:26 +02002676static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2677{
2678 struct kvm_vcpu *vcpu = filp->private_data;
2679
2680 fput(vcpu->kvm->filp);
2681 return 0;
2682}
2683
2684static struct file_operations kvm_vcpu_fops = {
2685 .release = kvm_vcpu_release,
2686 .unlocked_ioctl = kvm_vcpu_ioctl,
2687 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002688 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02002689};
2690
2691/*
2692 * Allocates an inode for the vcpu.
2693 */
2694static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2695{
2696 int fd, r;
2697 struct inode *inode;
2698 struct file *file;
2699
Avi Kivityd6d28162007-06-28 08:38:16 -04002700 r = anon_inode_getfd(&fd, &inode, &file,
2701 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2702 if (r)
2703 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002704 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +02002705 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02002706}
2707
Avi Kivityc5ea7662007-02-20 18:41:05 +02002708/*
2709 * Creates some virtual cpus. Good luck creating more than one.
2710 */
2711static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2712{
2713 int r;
2714 struct kvm_vcpu *vcpu;
2715
Avi Kivityc5ea7662007-02-20 18:41:05 +02002716 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002717 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002718
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002719 vcpu = kvm_x86_ops->vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002720 if (IS_ERR(vcpu))
2721 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002722
Avi Kivity15ad7142007-07-11 18:17:21 +03002723 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
2724
Rusty Russellb114b082007-07-30 21:13:43 +10002725 /* We do fxsave: this must be aligned. */
2726 BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF);
2727
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002728 vcpu_load(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002729 r = kvm_mmu_setup(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002730 vcpu_put(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002731 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002732 goto free_vcpu;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002733
Shaohua Li11ec2802007-07-23 14:51:37 +08002734 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002735 if (kvm->vcpus[n]) {
2736 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +08002737 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002738 goto mmu_unload;
2739 }
2740 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08002741 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002742
2743 /* Now it's all set up, let userspace reach it */
Avi Kivitybccf2152007-02-21 18:04:26 +02002744 r = create_vcpu_fd(vcpu);
2745 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002746 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02002747 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002748
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002749unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08002750 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002751 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +08002752 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002753
2754mmu_unload:
2755 vcpu_load(vcpu);
2756 kvm_mmu_unload(vcpu);
2757 vcpu_put(vcpu);
2758
2759free_vcpu:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002760 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002761 return r;
2762}
2763
Eddie Dong2cc51562007-05-21 07:28:09 +03002764static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
2765{
2766 u64 efer;
2767 int i;
2768 struct kvm_cpuid_entry *e, *entry;
2769
2770 rdmsrl(MSR_EFER, efer);
2771 entry = NULL;
2772 for (i = 0; i < vcpu->cpuid_nent; ++i) {
2773 e = &vcpu->cpuid_entries[i];
2774 if (e->function == 0x80000001) {
2775 entry = e;
2776 break;
2777 }
2778 }
Avi Kivity4c981b42007-07-25 09:22:12 +03002779 if (entry && (entry->edx & (1 << 20)) && !(efer & EFER_NX)) {
Eddie Dong2cc51562007-05-21 07:28:09 +03002780 entry->edx &= ~(1 << 20);
Avi Kivity4c981b42007-07-25 09:22:12 +03002781 printk(KERN_INFO "kvm: guest NX capability removed\n");
Eddie Dong2cc51562007-05-21 07:28:09 +03002782 }
2783}
2784
Avi Kivity06465c52007-02-28 20:46:53 +02002785static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
2786 struct kvm_cpuid *cpuid,
2787 struct kvm_cpuid_entry __user *entries)
2788{
2789 int r;
2790
2791 r = -E2BIG;
2792 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2793 goto out;
2794 r = -EFAULT;
2795 if (copy_from_user(&vcpu->cpuid_entries, entries,
2796 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
2797 goto out;
2798 vcpu->cpuid_nent = cpuid->nent;
Eddie Dong2cc51562007-05-21 07:28:09 +03002799 cpuid_fix_nx_cap(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002800 return 0;
2801
2802out:
2803 return r;
2804}
2805
Avi Kivity1961d272007-03-05 19:46:05 +02002806static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2807{
2808 if (sigset) {
2809 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2810 vcpu->sigset_active = 1;
2811 vcpu->sigset = *sigset;
2812 } else
2813 vcpu->sigset_active = 0;
2814 return 0;
2815}
2816
Avi Kivityb8836732007-04-01 16:34:31 +03002817/*
2818 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2819 * we have asm/x86/processor.h
2820 */
2821struct fxsave {
2822 u16 cwd;
2823 u16 swd;
2824 u16 twd;
2825 u16 fop;
2826 u64 rip;
2827 u64 rdp;
2828 u32 mxcsr;
2829 u32 mxcsr_mask;
2830 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2831#ifdef CONFIG_X86_64
2832 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2833#else
2834 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2835#endif
2836};
2837
2838static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2839{
Rusty Russellb114b082007-07-30 21:13:43 +10002840 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002841
2842 vcpu_load(vcpu);
2843
2844 memcpy(fpu->fpr, fxsave->st_space, 128);
2845 fpu->fcw = fxsave->cwd;
2846 fpu->fsw = fxsave->swd;
2847 fpu->ftwx = fxsave->twd;
2848 fpu->last_opcode = fxsave->fop;
2849 fpu->last_ip = fxsave->rip;
2850 fpu->last_dp = fxsave->rdp;
2851 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2852
2853 vcpu_put(vcpu);
2854
2855 return 0;
2856}
2857
2858static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2859{
Rusty Russellb114b082007-07-30 21:13:43 +10002860 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002861
2862 vcpu_load(vcpu);
2863
2864 memcpy(fxsave->st_space, fpu->fpr, 128);
2865 fxsave->cwd = fpu->fcw;
2866 fxsave->swd = fpu->fsw;
2867 fxsave->twd = fpu->ftwx;
2868 fxsave->fop = fpu->last_opcode;
2869 fxsave->rip = fpu->last_ip;
2870 fxsave->rdp = fpu->last_dp;
2871 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2872
2873 vcpu_put(vcpu);
2874
2875 return 0;
2876}
2877
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002878static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
2879 struct kvm_lapic_state *s)
2880{
2881 vcpu_load(vcpu);
2882 memcpy(s->regs, vcpu->apic->regs, sizeof *s);
2883 vcpu_put(vcpu);
2884
2885 return 0;
2886}
2887
2888static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
2889 struct kvm_lapic_state *s)
2890{
2891 vcpu_load(vcpu);
2892 memcpy(vcpu->apic->regs, s->regs, sizeof *s);
2893 kvm_apic_post_state_restore(vcpu);
2894 vcpu_put(vcpu);
2895
2896 return 0;
2897}
2898
Avi Kivitybccf2152007-02-21 18:04:26 +02002899static long kvm_vcpu_ioctl(struct file *filp,
2900 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002901{
Avi Kivitybccf2152007-02-21 18:04:26 +02002902 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +00002903 void __user *argp = (void __user *)arg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002904 int r = -EINVAL;
2905
2906 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002907 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002908 r = -EINVAL;
2909 if (arg)
2910 goto out;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002911 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002912 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002913 case KVM_GET_REGS: {
2914 struct kvm_regs kvm_regs;
2915
Avi Kivitybccf2152007-02-21 18:04:26 +02002916 memset(&kvm_regs, 0, sizeof kvm_regs);
2917 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002918 if (r)
2919 goto out;
2920 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002921 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002922 goto out;
2923 r = 0;
2924 break;
2925 }
2926 case KVM_SET_REGS: {
2927 struct kvm_regs kvm_regs;
2928
2929 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002930 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002931 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002932 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002933 if (r)
2934 goto out;
2935 r = 0;
2936 break;
2937 }
2938 case KVM_GET_SREGS: {
2939 struct kvm_sregs kvm_sregs;
2940
Avi Kivitybccf2152007-02-21 18:04:26 +02002941 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2942 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002943 if (r)
2944 goto out;
2945 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002946 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002947 goto out;
2948 r = 0;
2949 break;
2950 }
2951 case KVM_SET_SREGS: {
2952 struct kvm_sregs kvm_sregs;
2953
2954 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002955 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002956 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002957 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002958 if (r)
2959 goto out;
2960 r = 0;
2961 break;
2962 }
2963 case KVM_TRANSLATE: {
2964 struct kvm_translation tr;
2965
2966 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002967 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002968 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002969 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002970 if (r)
2971 goto out;
2972 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002973 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002974 goto out;
2975 r = 0;
2976 break;
2977 }
2978 case KVM_INTERRUPT: {
2979 struct kvm_interrupt irq;
2980
2981 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002982 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002983 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002984 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002985 if (r)
2986 goto out;
2987 r = 0;
2988 break;
2989 }
2990 case KVM_DEBUG_GUEST: {
2991 struct kvm_debug_guest dbg;
2992
2993 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002994 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002995 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002996 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002997 if (r)
2998 goto out;
2999 r = 0;
3000 break;
3001 }
Avi Kivitybccf2152007-02-21 18:04:26 +02003002 case KVM_GET_MSRS:
Avi Kivity35f3f282007-07-17 14:20:30 +03003003 r = msr_io(vcpu, argp, kvm_get_msr, 1);
Avi Kivitybccf2152007-02-21 18:04:26 +02003004 break;
3005 case KVM_SET_MSRS:
3006 r = msr_io(vcpu, argp, do_set_msr, 0);
3007 break;
Avi Kivity06465c52007-02-28 20:46:53 +02003008 case KVM_SET_CPUID: {
3009 struct kvm_cpuid __user *cpuid_arg = argp;
3010 struct kvm_cpuid cpuid;
3011
3012 r = -EFAULT;
3013 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
3014 goto out;
3015 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
3016 if (r)
3017 goto out;
3018 break;
3019 }
Avi Kivity1961d272007-03-05 19:46:05 +02003020 case KVM_SET_SIGNAL_MASK: {
3021 struct kvm_signal_mask __user *sigmask_arg = argp;
3022 struct kvm_signal_mask kvm_sigmask;
3023 sigset_t sigset, *p;
3024
3025 p = NULL;
3026 if (argp) {
3027 r = -EFAULT;
3028 if (copy_from_user(&kvm_sigmask, argp,
3029 sizeof kvm_sigmask))
3030 goto out;
3031 r = -EINVAL;
3032 if (kvm_sigmask.len != sizeof sigset)
3033 goto out;
3034 r = -EFAULT;
3035 if (copy_from_user(&sigset, sigmask_arg->sigset,
3036 sizeof sigset))
3037 goto out;
3038 p = &sigset;
3039 }
3040 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
3041 break;
3042 }
Avi Kivityb8836732007-04-01 16:34:31 +03003043 case KVM_GET_FPU: {
3044 struct kvm_fpu fpu;
3045
3046 memset(&fpu, 0, sizeof fpu);
3047 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
3048 if (r)
3049 goto out;
3050 r = -EFAULT;
3051 if (copy_to_user(argp, &fpu, sizeof fpu))
3052 goto out;
3053 r = 0;
3054 break;
3055 }
3056 case KVM_SET_FPU: {
3057 struct kvm_fpu fpu;
3058
3059 r = -EFAULT;
3060 if (copy_from_user(&fpu, argp, sizeof fpu))
3061 goto out;
3062 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
3063 if (r)
3064 goto out;
3065 r = 0;
3066 break;
3067 }
Eddie Dong96ad2cc2007-09-06 12:22:56 +03003068 case KVM_GET_LAPIC: {
3069 struct kvm_lapic_state lapic;
3070
3071 memset(&lapic, 0, sizeof lapic);
3072 r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic);
3073 if (r)
3074 goto out;
3075 r = -EFAULT;
3076 if (copy_to_user(argp, &lapic, sizeof lapic))
3077 goto out;
3078 r = 0;
3079 break;
3080 }
3081 case KVM_SET_LAPIC: {
3082 struct kvm_lapic_state lapic;
3083
3084 r = -EFAULT;
3085 if (copy_from_user(&lapic, argp, sizeof lapic))
3086 goto out;
3087 r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);;
3088 if (r)
3089 goto out;
3090 r = 0;
3091 break;
3092 }
Avi Kivitybccf2152007-02-21 18:04:26 +02003093 default:
3094 ;
3095 }
3096out:
3097 return r;
3098}
3099
3100static long kvm_vm_ioctl(struct file *filp,
3101 unsigned int ioctl, unsigned long arg)
3102{
3103 struct kvm *kvm = filp->private_data;
3104 void __user *argp = (void __user *)arg;
3105 int r = -EINVAL;
3106
3107 switch (ioctl) {
3108 case KVM_CREATE_VCPU:
3109 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
3110 if (r < 0)
3111 goto out;
3112 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003113 case KVM_SET_MEMORY_REGION: {
3114 struct kvm_memory_region kvm_mem;
Izik Eidus6fc138d2007-10-09 19:20:39 +02003115 struct kvm_userspace_memory_region kvm_userspace_mem;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003116
3117 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00003118 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003119 goto out;
Izik Eidus6fc138d2007-10-09 19:20:39 +02003120 kvm_userspace_mem.slot = kvm_mem.slot;
3121 kvm_userspace_mem.flags = kvm_mem.flags;
3122 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
3123 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
3124 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
3125 if (r)
3126 goto out;
3127 break;
3128 }
3129 case KVM_SET_USER_MEMORY_REGION: {
3130 struct kvm_userspace_memory_region kvm_userspace_mem;
3131
3132 r = -EFAULT;
3133 if (copy_from_user(&kvm_userspace_mem, argp,
3134 sizeof kvm_userspace_mem))
3135 goto out;
3136
3137 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003138 if (r)
3139 goto out;
3140 break;
3141 }
Izik Eidus82ce2c92007-10-02 18:52:55 +02003142 case KVM_SET_NR_MMU_PAGES:
3143 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
3144 if (r)
3145 goto out;
3146 break;
3147 case KVM_GET_NR_MMU_PAGES:
3148 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
3149 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003150 case KVM_GET_DIRTY_LOG: {
3151 struct kvm_dirty_log log;
3152
3153 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00003154 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003155 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02003156 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003157 if (r)
3158 goto out;
3159 break;
3160 }
Avi Kivitye8207542007-03-30 16:54:30 +03003161 case KVM_SET_MEMORY_ALIAS: {
3162 struct kvm_memory_alias alias;
3163
3164 r = -EFAULT;
3165 if (copy_from_user(&alias, argp, sizeof alias))
3166 goto out;
3167 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
3168 if (r)
3169 goto out;
3170 break;
3171 }
Eddie Dong85f455f2007-07-06 12:20:49 +03003172 case KVM_CREATE_IRQCHIP:
3173 r = -ENOMEM;
3174 kvm->vpic = kvm_create_pic(kvm);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03003175 if (kvm->vpic) {
3176 r = kvm_ioapic_init(kvm);
3177 if (r) {
3178 kfree(kvm->vpic);
3179 kvm->vpic = NULL;
3180 goto out;
3181 }
Mike Dayd77c26f2007-10-08 09:02:08 -04003182 } else
Eddie Dong85f455f2007-07-06 12:20:49 +03003183 goto out;
3184 break;
3185 case KVM_IRQ_LINE: {
3186 struct kvm_irq_level irq_event;
3187
3188 r = -EFAULT;
3189 if (copy_from_user(&irq_event, argp, sizeof irq_event))
3190 goto out;
3191 if (irqchip_in_kernel(kvm)) {
Eddie Dong9cf98822007-07-22 10:36:31 +03003192 mutex_lock(&kvm->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +03003193 if (irq_event.irq < 16)
3194 kvm_pic_set_irq(pic_irqchip(kvm),
3195 irq_event.irq,
3196 irq_event.level);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03003197 kvm_ioapic_set_irq(kvm->vioapic,
3198 irq_event.irq,
3199 irq_event.level);
Eddie Dong9cf98822007-07-22 10:36:31 +03003200 mutex_unlock(&kvm->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +03003201 r = 0;
3202 }
3203 break;
3204 }
He, Qing6ceb9d72007-07-26 11:05:18 +03003205 case KVM_GET_IRQCHIP: {
3206 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3207 struct kvm_irqchip chip;
3208
3209 r = -EFAULT;
3210 if (copy_from_user(&chip, argp, sizeof chip))
3211 goto out;
3212 r = -ENXIO;
3213 if (!irqchip_in_kernel(kvm))
3214 goto out;
3215 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
3216 if (r)
3217 goto out;
3218 r = -EFAULT;
3219 if (copy_to_user(argp, &chip, sizeof chip))
3220 goto out;
3221 r = 0;
3222 break;
3223 }
3224 case KVM_SET_IRQCHIP: {
3225 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3226 struct kvm_irqchip chip;
3227
3228 r = -EFAULT;
3229 if (copy_from_user(&chip, argp, sizeof chip))
3230 goto out;
3231 r = -ENXIO;
3232 if (!irqchip_in_kernel(kvm))
3233 goto out;
3234 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
3235 if (r)
3236 goto out;
3237 r = 0;
3238 break;
3239 }
Avi Kivityf17abe92007-02-21 19:28:04 +02003240 default:
3241 ;
3242 }
3243out:
3244 return r;
3245}
3246
3247static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
3248 unsigned long address,
3249 int *type)
3250{
3251 struct kvm *kvm = vma->vm_file->private_data;
3252 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02003253 struct page *page;
3254
Avi Kivityf17abe92007-02-21 19:28:04 +02003255 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity954bbbc22007-03-30 14:02:32 +03003256 page = gfn_to_page(kvm, pgoff);
Avi Kivityf17abe92007-02-21 19:28:04 +02003257 if (!page)
3258 return NOPAGE_SIGBUS;
3259 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03003260 if (type != NULL)
3261 *type = VM_FAULT_MINOR;
3262
Avi Kivityf17abe92007-02-21 19:28:04 +02003263 return page;
3264}
3265
3266static struct vm_operations_struct kvm_vm_vm_ops = {
3267 .nopage = kvm_vm_nopage,
3268};
3269
3270static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
3271{
3272 vma->vm_ops = &kvm_vm_vm_ops;
3273 return 0;
3274}
3275
3276static struct file_operations kvm_vm_fops = {
3277 .release = kvm_vm_release,
3278 .unlocked_ioctl = kvm_vm_ioctl,
3279 .compat_ioctl = kvm_vm_ioctl,
3280 .mmap = kvm_vm_mmap,
3281};
3282
3283static int kvm_dev_ioctl_create_vm(void)
3284{
3285 int fd, r;
3286 struct inode *inode;
3287 struct file *file;
3288 struct kvm *kvm;
3289
Avi Kivityf17abe92007-02-21 19:28:04 +02003290 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04003291 if (IS_ERR(kvm))
3292 return PTR_ERR(kvm);
3293 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
3294 if (r) {
3295 kvm_destroy_vm(kvm);
3296 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02003297 }
3298
Avi Kivitybccf2152007-02-21 18:04:26 +02003299 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02003300
Avi Kivityf17abe92007-02-21 19:28:04 +02003301 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02003302}
3303
3304static long kvm_dev_ioctl(struct file *filp,
3305 unsigned int ioctl, unsigned long arg)
3306{
3307 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02003308 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02003309
3310 switch (ioctl) {
3311 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02003312 r = -EINVAL;
3313 if (arg)
3314 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02003315 r = KVM_API_VERSION;
3316 break;
3317 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02003318 r = -EINVAL;
3319 if (arg)
3320 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02003321 r = kvm_dev_ioctl_create_vm();
3322 break;
Eddie Dong85f455f2007-07-06 12:20:49 +03003323 case KVM_CHECK_EXTENSION: {
3324 int ext = (long)argp;
3325
3326 switch (ext) {
3327 case KVM_CAP_IRQCHIP:
Eddie Dongb6958ce2007-07-18 12:15:21 +03003328 case KVM_CAP_HLT:
Izik Eidus82ce2c92007-10-02 18:52:55 +02003329 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Izik Eidus6fc138d2007-10-09 19:20:39 +02003330 case KVM_CAP_USER_MEMORY:
Eddie Dong85f455f2007-07-06 12:20:49 +03003331 r = 1;
3332 break;
3333 default:
3334 r = 0;
3335 break;
3336 }
Avi Kivity5d308f42007-03-01 17:56:20 +02003337 break;
Eddie Dong85f455f2007-07-06 12:20:49 +03003338 }
Avi Kivity07c45a32007-03-07 13:05:38 +02003339 case KVM_GET_VCPU_MMAP_SIZE:
3340 r = -EINVAL;
3341 if (arg)
3342 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02003343 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02003344 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003345 default:
Carsten Otte043405e2007-10-10 17:16:19 +02003346 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003347 }
3348out:
3349 return r;
3350}
3351
Avi Kivity6aa8b732006-12-10 02:21:36 -08003352static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003353 .unlocked_ioctl = kvm_dev_ioctl,
3354 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003355};
3356
3357static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02003358 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003359 "kvm",
3360 &kvm_chardev_ops,
3361};
3362
Avi Kivity774c47f2007-02-12 00:54:47 -08003363/*
3364 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
3365 * cached on it.
3366 */
3367static void decache_vcpus_on_cpu(int cpu)
3368{
3369 struct kvm *vm;
3370 struct kvm_vcpu *vcpu;
3371 int i;
3372
3373 spin_lock(&kvm_lock);
Shaohua Li11ec2802007-07-23 14:51:37 +08003374 list_for_each_entry(vm, &vm_list, vm_list)
Avi Kivity774c47f2007-02-12 00:54:47 -08003375 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003376 vcpu = vm->vcpus[i];
3377 if (!vcpu)
3378 continue;
Avi Kivity774c47f2007-02-12 00:54:47 -08003379 /*
3380 * If the vcpu is locked, then it is running on some
3381 * other cpu and therefore it is not cached on the
3382 * cpu in question.
3383 *
3384 * If it's not locked, check the last cpu it executed
3385 * on.
3386 */
3387 if (mutex_trylock(&vcpu->mutex)) {
3388 if (vcpu->cpu == cpu) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003389 kvm_x86_ops->vcpu_decache(vcpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08003390 vcpu->cpu = -1;
3391 }
3392 mutex_unlock(&vcpu->mutex);
3393 }
3394 }
3395 spin_unlock(&kvm_lock);
3396}
3397
Avi Kivity1b6c0162007-05-24 13:03:52 +03003398static void hardware_enable(void *junk)
3399{
3400 int cpu = raw_smp_processor_id();
3401
3402 if (cpu_isset(cpu, cpus_hardware_enabled))
3403 return;
3404 cpu_set(cpu, cpus_hardware_enabled);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003405 kvm_x86_ops->hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003406}
3407
3408static void hardware_disable(void *junk)
3409{
3410 int cpu = raw_smp_processor_id();
3411
3412 if (!cpu_isset(cpu, cpus_hardware_enabled))
3413 return;
3414 cpu_clear(cpu, cpus_hardware_enabled);
3415 decache_vcpus_on_cpu(cpu);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003416 kvm_x86_ops->hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003417}
3418
Avi Kivity774c47f2007-02-12 00:54:47 -08003419static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
3420 void *v)
3421{
3422 int cpu = (long)v;
3423
3424 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03003425 case CPU_DYING:
3426 case CPU_DYING_FROZEN:
Avi Kivity6ec8a852007-08-19 15:57:26 +03003427 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3428 cpu);
3429 hardware_disable(NULL);
3430 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08003431 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003432 case CPU_UP_CANCELED_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003433 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3434 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003435 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003436 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02003437 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003438 case CPU_ONLINE_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003439 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
3440 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003441 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003442 break;
3443 }
3444 return NOTIFY_OK;
3445}
3446
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003447static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04003448 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003449{
3450 if (val == SYS_RESTART) {
3451 /*
3452 * Some (well, at least mine) BIOSes hang on reboot if
3453 * in vmx root mode.
3454 */
3455 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
3456 on_each_cpu(hardware_disable, NULL, 0, 1);
3457 }
3458 return NOTIFY_OK;
3459}
3460
3461static struct notifier_block kvm_reboot_notifier = {
3462 .notifier_call = kvm_reboot,
3463 .priority = 0,
3464};
3465
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04003466void kvm_io_bus_init(struct kvm_io_bus *bus)
3467{
3468 memset(bus, 0, sizeof(*bus));
3469}
3470
3471void kvm_io_bus_destroy(struct kvm_io_bus *bus)
3472{
3473 int i;
3474
3475 for (i = 0; i < bus->dev_count; i++) {
3476 struct kvm_io_device *pos = bus->devs[i];
3477
3478 kvm_iodevice_destructor(pos);
3479 }
3480}
3481
3482struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
3483{
3484 int i;
3485
3486 for (i = 0; i < bus->dev_count; i++) {
3487 struct kvm_io_device *pos = bus->devs[i];
3488
3489 if (pos->in_range(pos, addr))
3490 return pos;
3491 }
3492
3493 return NULL;
3494}
3495
3496void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
3497{
3498 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
3499
3500 bus->devs[bus->dev_count++] = dev;
3501}
3502
Avi Kivity774c47f2007-02-12 00:54:47 -08003503static struct notifier_block kvm_cpu_notifier = {
3504 .notifier_call = kvm_cpu_hotplug,
3505 .priority = 20, /* must be > scheduler priority */
3506};
3507
Avi Kivity1165f5f2007-04-19 17:27:43 +03003508static u64 stat_get(void *_offset)
3509{
3510 unsigned offset = (long)_offset;
3511 u64 total = 0;
3512 struct kvm *kvm;
3513 struct kvm_vcpu *vcpu;
3514 int i;
3515
3516 spin_lock(&kvm_lock);
3517 list_for_each_entry(kvm, &vm_list, vm_list)
3518 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003519 vcpu = kvm->vcpus[i];
3520 if (vcpu)
3521 total += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03003522 }
3523 spin_unlock(&kvm_lock);
3524 return total;
3525}
3526
Rusty Russell3dea7ca2007-08-01 10:12:22 +10003527DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n");
Avi Kivity1165f5f2007-04-19 17:27:43 +03003528
Avi Kivity6aa8b732006-12-10 02:21:36 -08003529static __init void kvm_init_debug(void)
3530{
3531 struct kvm_stats_debugfs_item *p;
3532
Al Viro8b6d44c2007-02-09 16:38:40 +00003533 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003534 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03003535 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
3536 (void *)(long)p->offset,
3537 &stat_fops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003538}
3539
3540static void kvm_exit_debug(void)
3541{
3542 struct kvm_stats_debugfs_item *p;
3543
3544 for (p = debugfs_entries; p->name; ++p)
3545 debugfs_remove(p->dentry);
3546 debugfs_remove(debugfs_dir);
3547}
3548
Avi Kivity59ae6c62007-02-12 00:54:48 -08003549static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3550{
Avi Kivity4267c412007-05-24 13:09:41 +03003551 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003552 return 0;
3553}
3554
3555static int kvm_resume(struct sys_device *dev)
3556{
Avi Kivity4267c412007-05-24 13:09:41 +03003557 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003558 return 0;
3559}
3560
3561static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01003562 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08003563 .suspend = kvm_suspend,
3564 .resume = kvm_resume,
3565};
3566
3567static struct sys_device kvm_sysdev = {
3568 .id = 0,
3569 .cls = &kvm_sysdev_class,
3570};
3571
Avi Kivity6aa8b732006-12-10 02:21:36 -08003572hpa_t bad_page_address;
3573
Avi Kivity15ad7142007-07-11 18:17:21 +03003574static inline
3575struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
3576{
3577 return container_of(pn, struct kvm_vcpu, preempt_notifier);
3578}
3579
3580static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
3581{
3582 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3583
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003584 kvm_x86_ops->vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003585}
3586
3587static void kvm_sched_out(struct preempt_notifier *pn,
3588 struct task_struct *next)
3589{
3590 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3591
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003592 kvm_x86_ops->vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003593}
3594
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003595int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10003596 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003597{
3598 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003599 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003600
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003601 if (kvm_x86_ops) {
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08003602 printk(KERN_ERR "kvm: already loaded the other module\n");
3603 return -EEXIST;
3604 }
3605
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003606 if (!ops->cpu_has_kvm_support()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003607 printk(KERN_ERR "kvm: no hardware support\n");
3608 return -EOPNOTSUPP;
3609 }
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003610 if (ops->disabled_by_bios()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003611 printk(KERN_ERR "kvm: disabled by bios\n");
3612 return -EOPNOTSUPP;
3613 }
3614
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003615 kvm_x86_ops = ops;
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003616
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003617 r = kvm_x86_ops->hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003618 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02003619 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003620
Yang, Sheng002c7f72007-07-31 14:23:01 +03003621 for_each_online_cpu(cpu) {
3622 smp_call_function_single(cpu,
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003623 kvm_x86_ops->check_processor_compatibility,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003624 &r, 0, 1);
3625 if (r < 0)
3626 goto out_free_0;
3627 }
3628
Avi Kivity1b6c0162007-05-24 13:03:52 +03003629 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003630 r = register_cpu_notifier(&kvm_cpu_notifier);
3631 if (r)
3632 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003633 register_reboot_notifier(&kvm_reboot_notifier);
3634
Avi Kivity59ae6c62007-02-12 00:54:48 -08003635 r = sysdev_class_register(&kvm_sysdev_class);
3636 if (r)
3637 goto out_free_2;
3638
3639 r = sysdev_register(&kvm_sysdev);
3640 if (r)
3641 goto out_free_3;
3642
Rusty Russellc16f8622007-07-30 21:12:19 +10003643 /* A kmem cache lets us meet the alignment requirements of fx_save. */
3644 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
3645 __alignof__(struct kvm_vcpu), 0, 0);
3646 if (!kvm_vcpu_cache) {
3647 r = -ENOMEM;
3648 goto out_free_4;
3649 }
3650
Avi Kivity6aa8b732006-12-10 02:21:36 -08003651 kvm_chardev_ops.owner = module;
3652
3653 r = misc_register(&kvm_dev);
3654 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04003655 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08003656 goto out_free;
3657 }
3658
Avi Kivity15ad7142007-07-11 18:17:21 +03003659 kvm_preempt_ops.sched_in = kvm_sched_in;
3660 kvm_preempt_ops.sched_out = kvm_sched_out;
3661
Avi Kivityc7addb92007-09-16 18:58:32 +02003662 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
3663
3664 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003665
3666out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10003667 kmem_cache_destroy(kvm_vcpu_cache);
3668out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08003669 sysdev_unregister(&kvm_sysdev);
3670out_free_3:
3671 sysdev_class_unregister(&kvm_sysdev_class);
3672out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003673 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08003674 unregister_cpu_notifier(&kvm_cpu_notifier);
3675out_free_1:
Avi Kivity1b6c0162007-05-24 13:03:52 +03003676 on_each_cpu(hardware_disable, NULL, 0, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03003677out_free_0:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003678 kvm_x86_ops->hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02003679out:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003680 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003681 return r;
3682}
Mike Dayd77c26f2007-10-08 09:02:08 -04003683EXPORT_SYMBOL_GPL(kvm_init_x86);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003684
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003685void kvm_exit_x86(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003686{
3687 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10003688 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003689 sysdev_unregister(&kvm_sysdev);
3690 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003691 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003692 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003693 on_each_cpu(hardware_disable, NULL, 0, 1);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003694 kvm_x86_ops->hardware_unsetup();
3695 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003696}
Mike Dayd77c26f2007-10-08 09:02:08 -04003697EXPORT_SYMBOL_GPL(kvm_exit_x86);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003698
3699static __init int kvm_init(void)
3700{
3701 static struct page *bad_page;
Avi Kivity37e29d92007-02-20 14:07:37 +02003702 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003703
Avi Kivityb5a33a72007-04-15 16:31:09 +03003704 r = kvm_mmu_module_init();
3705 if (r)
3706 goto out4;
3707
Avi Kivity6aa8b732006-12-10 02:21:36 -08003708 kvm_init_debug();
3709
Carsten Otte043405e2007-10-10 17:16:19 +02003710 kvm_arch_init();
Michael Riepebf591b22006-12-22 01:05:36 -08003711
Mike Dayd77c26f2007-10-08 09:02:08 -04003712 bad_page = alloc_page(GFP_KERNEL);
3713
3714 if (bad_page == NULL) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003715 r = -ENOMEM;
3716 goto out;
3717 }
3718
3719 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
3720 memset(__va(bad_page_address), 0, PAGE_SIZE);
3721
Avi Kivity58e690e2007-02-26 16:29:43 +02003722 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003723
3724out:
3725 kvm_exit_debug();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003726 kvm_mmu_module_exit();
3727out4:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003728 return r;
3729}
3730
3731static __exit void kvm_exit(void)
3732{
3733 kvm_exit_debug();
3734 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
Avi Kivityb5a33a72007-04-15 16:31:09 +03003735 kvm_mmu_module_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003736}
3737
3738module_init(kvm_init)
3739module_exit(kvm_exit)