blob: 8c458f26287253c3958c6101e21bfbca1f90894b [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"
Avi Kivitye4956062007-06-28 14:15:57 -040019#include "x86_emulate.h"
20#include "segment_descriptor.h"
Eddie Dong85f455f2007-07-06 12:20:49 +030021#include "irq.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080022
23#include <linux/kvm.h>
24#include <linux/module.h>
25#include <linux/errno.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/percpu.h>
27#include <linux/gfp.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028#include <linux/mm.h>
29#include <linux/miscdevice.h>
30#include <linux/vmalloc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080031#include <linux/reboot.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080032#include <linux/debugfs.h>
33#include <linux/highmem.h>
34#include <linux/file.h>
Avi Kivity59ae6c62007-02-12 00:54:48 -080035#include <linux/sysdev.h>
Avi Kivity774c47f2007-02-12 00:54:47 -080036#include <linux/cpu.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040037#include <linux/sched.h>
Avi Kivityd9e368d2007-06-07 19:18:30 +030038#include <linux/cpumask.h>
39#include <linux/smp.h>
Avi Kivityd6d28162007-06-28 08:38:16 -040040#include <linux/anon_inodes.h>
Avi Kivity04d2cc72007-09-10 18:10:54 +030041#include <linux/profile.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080042
Avi Kivitye4956062007-06-28 14:15:57 -040043#include <asm/processor.h>
44#include <asm/msr.h>
45#include <asm/io.h>
46#include <asm/uaccess.h>
47#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080048
49MODULE_AUTHOR("Qumranet");
50MODULE_LICENSE("GPL");
51
Avi Kivity133de902007-02-12 00:54:44 -080052static DEFINE_SPINLOCK(kvm_lock);
53static LIST_HEAD(vm_list);
54
Avi Kivity1b6c0162007-05-24 13:03:52 +030055static cpumask_t cpus_hardware_enabled;
56
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +030057struct kvm_x86_ops *kvm_x86_ops;
Rusty Russellc16f8622007-07-30 21:12:19 +100058struct kmem_cache *kvm_vcpu_cache;
59EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030060
Avi Kivity15ad7142007-07-11 18:17:21 +030061static __read_mostly struct preempt_ops kvm_preempt_ops;
62
Avi Kivity1165f5f2007-04-19 17:27:43 +030063#define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
Avi Kivity6aa8b732006-12-10 02:21:36 -080064
65static struct kvm_stats_debugfs_item {
66 const char *name;
Avi Kivity1165f5f2007-04-19 17:27:43 +030067 int offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -080068 struct dentry *dentry;
69} debugfs_entries[] = {
Avi Kivity1165f5f2007-04-19 17:27:43 +030070 { "pf_fixed", STAT_OFFSET(pf_fixed) },
71 { "pf_guest", STAT_OFFSET(pf_guest) },
72 { "tlb_flush", STAT_OFFSET(tlb_flush) },
73 { "invlpg", STAT_OFFSET(invlpg) },
74 { "exits", STAT_OFFSET(exits) },
75 { "io_exits", STAT_OFFSET(io_exits) },
76 { "mmio_exits", STAT_OFFSET(mmio_exits) },
77 { "signal_exits", STAT_OFFSET(signal_exits) },
78 { "irq_window", STAT_OFFSET(irq_window_exits) },
79 { "halt_exits", STAT_OFFSET(halt_exits) },
Eddie Dongb6958ce2007-07-18 12:15:21 +030080 { "halt_wakeup", STAT_OFFSET(halt_wakeup) },
Avi Kivity1165f5f2007-04-19 17:27:43 +030081 { "request_irq", STAT_OFFSET(request_irq_exits) },
82 { "irq_exits", STAT_OFFSET(irq_exits) },
Avi Kivitye6adf282007-04-30 16:07:54 +030083 { "light_exits", STAT_OFFSET(light_exits) },
Eddie Dong2cc51562007-05-21 07:28:09 +030084 { "efer_reload", STAT_OFFSET(efer_reload) },
Avi Kivity1165f5f2007-04-19 17:27:43 +030085 { NULL }
Avi Kivity6aa8b732006-12-10 02:21:36 -080086};
87
88static struct dentry *debugfs_dir;
89
90#define MAX_IO_MSRS 256
91
Rusty Russell707d92fa2007-07-17 23:19:08 +100092#define CR0_RESERVED_BITS \
93 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
94 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
95 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
Rusty Russell66aee912007-07-17 23:34:16 +100096#define CR4_RESERVED_BITS \
97 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
98 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
99 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
100 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
101
Rusty Russell7075bc82007-07-17 23:37:17 +1000102#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800103#define EFER_RESERVED_BITS 0xfffffffffffff2fe
104
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800105#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800106// LDT or TSS descriptor in the GDT. 16 bytes.
107struct segment_descriptor_64 {
108 struct segment_descriptor s;
109 u32 base_higher;
110 u32 pad_zero;
111};
112
113#endif
114
Avi Kivitybccf2152007-02-21 18:04:26 +0200115static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
116 unsigned long arg);
117
Avi Kivity6aa8b732006-12-10 02:21:36 -0800118unsigned long segment_base(u16 selector)
119{
120 struct descriptor_table gdt;
121 struct segment_descriptor *d;
122 unsigned long table_base;
123 typedef unsigned long ul;
124 unsigned long v;
125
126 if (selector == 0)
127 return 0;
128
129 asm ("sgdt %0" : "=m"(gdt));
130 table_base = gdt.base;
131
132 if (selector & 4) { /* from ldt */
133 u16 ldt_selector;
134
135 asm ("sldt %0" : "=g"(ldt_selector));
136 table_base = segment_base(ldt_selector);
137 }
138 d = (struct segment_descriptor *)(table_base + (selector & ~7));
139 v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800140#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800141 if (d->system == 0
142 && (d->type == 2 || d->type == 9 || d->type == 11))
143 v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
144#endif
145 return v;
146}
147EXPORT_SYMBOL_GPL(segment_base);
148
James Morris5aacf0c2006-12-22 01:04:55 -0800149static inline int valid_vcpu(int n)
150{
151 return likely(n >= 0 && n < KVM_MAX_VCPUS);
152}
153
Avi Kivity7702fd12007-06-14 16:27:40 +0300154void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
155{
156 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
157 return;
158
159 vcpu->guest_fpu_loaded = 1;
Rusty Russellb114b082007-07-30 21:13:43 +1000160 fx_save(&vcpu->host_fx_image);
161 fx_restore(&vcpu->guest_fx_image);
Avi Kivity7702fd12007-06-14 16:27:40 +0300162}
163EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
164
165void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
166{
167 if (!vcpu->guest_fpu_loaded)
168 return;
169
170 vcpu->guest_fpu_loaded = 0;
Rusty Russellb114b082007-07-30 21:13:43 +1000171 fx_save(&vcpu->guest_fx_image);
172 fx_restore(&vcpu->host_fx_image);
Avi Kivity7702fd12007-06-14 16:27:40 +0300173}
174EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
175
Avi Kivity6aa8b732006-12-10 02:21:36 -0800176/*
177 * Switches to specified vcpu, until a matching vcpu_put()
178 */
Avi Kivitybccf2152007-02-21 18:04:26 +0200179static void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800180{
Avi Kivity15ad7142007-07-11 18:17:21 +0300181 int cpu;
182
Avi Kivitybccf2152007-02-21 18:04:26 +0200183 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300184 cpu = get_cpu();
185 preempt_notifier_register(&vcpu->preempt_notifier);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300186 kvm_x86_ops->vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300187 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200188}
189
Avi Kivity6aa8b732006-12-10 02:21:36 -0800190static void vcpu_put(struct kvm_vcpu *vcpu)
191{
Avi Kivity15ad7142007-07-11 18:17:21 +0300192 preempt_disable();
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300193 kvm_x86_ops->vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300194 preempt_notifier_unregister(&vcpu->preempt_notifier);
195 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196 mutex_unlock(&vcpu->mutex);
197}
198
Avi Kivityd9e368d2007-06-07 19:18:30 +0300199static void ack_flush(void *_completed)
200{
201 atomic_t *completed = _completed;
202
203 atomic_inc(completed);
204}
205
206void kvm_flush_remote_tlbs(struct kvm *kvm)
207{
208 int i, cpu, needed;
209 cpumask_t cpus;
210 struct kvm_vcpu *vcpu;
211 atomic_t completed;
212
213 atomic_set(&completed, 0);
214 cpus_clear(cpus);
215 needed = 0;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000216 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
217 vcpu = kvm->vcpus[i];
218 if (!vcpu)
219 continue;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300220 if (test_and_set_bit(KVM_TLB_FLUSH, &vcpu->requests))
221 continue;
222 cpu = vcpu->cpu;
223 if (cpu != -1 && cpu != raw_smp_processor_id())
224 if (!cpu_isset(cpu, cpus)) {
225 cpu_set(cpu, cpus);
226 ++needed;
227 }
228 }
229
230 /*
231 * We really want smp_call_function_mask() here. But that's not
232 * available, so ipi all cpus in parallel and wait for them
233 * to complete.
234 */
235 for (cpu = first_cpu(cpus); cpu != NR_CPUS; cpu = next_cpu(cpu, cpus))
236 smp_call_function_single(cpu, ack_flush, &completed, 1, 0);
237 while (atomic_read(&completed) != needed) {
238 cpu_relax();
239 barrier();
240 }
241}
242
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000243int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
244{
245 struct page *page;
246 int r;
247
248 mutex_init(&vcpu->mutex);
249 vcpu->cpu = -1;
250 vcpu->mmu.root_hpa = INVALID_PAGE;
251 vcpu->kvm = kvm;
252 vcpu->vcpu_id = id;
He, Qingc5ec1532007-09-03 17:07:41 +0300253 if (!irqchip_in_kernel(kvm) || id == 0)
254 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
255 else
256 vcpu->mp_state = VCPU_MP_STATE_UNINITIALIZED;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300257 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000258
259 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
260 if (!page) {
261 r = -ENOMEM;
262 goto fail;
263 }
264 vcpu->run = page_address(page);
265
266 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
267 if (!page) {
268 r = -ENOMEM;
269 goto fail_free_run;
270 }
271 vcpu->pio_data = page_address(page);
272
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000273 r = kvm_mmu_create(vcpu);
274 if (r < 0)
275 goto fail_free_pio_data;
276
277 return 0;
278
279fail_free_pio_data:
280 free_page((unsigned long)vcpu->pio_data);
281fail_free_run:
282 free_page((unsigned long)vcpu->run);
283fail:
284 return -ENOMEM;
285}
286EXPORT_SYMBOL_GPL(kvm_vcpu_init);
287
288void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
289{
290 kvm_mmu_destroy(vcpu);
Eddie Dong1b9778d2007-09-03 16:56:58 +0300291 if (vcpu->apic)
292 hrtimer_cancel(&vcpu->apic->timer.dev);
Eddie Dong97222cc2007-09-12 10:58:04 +0300293 kvm_free_apic(vcpu->apic);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000294 free_page((unsigned long)vcpu->pio_data);
295 free_page((unsigned long)vcpu->run);
296}
297EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
298
Avi Kivityf17abe92007-02-21 19:28:04 +0200299static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800300{
301 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800302
303 if (!kvm)
Avi Kivityf17abe92007-02-21 19:28:04 +0200304 return ERR_PTR(-ENOMEM);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800305
Eddie Dong74906342007-06-19 18:05:03 +0300306 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800307 mutex_init(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800308 INIT_LIST_HEAD(&kvm->active_mmu_pages);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400309 kvm_io_bus_init(&kvm->mmio_bus);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000310 spin_lock(&kvm_lock);
311 list_add(&kvm->vm_list, &vm_list);
312 spin_unlock(&kvm_lock);
Avi Kivityf17abe92007-02-21 19:28:04 +0200313 return kvm;
314}
315
Avi Kivity6aa8b732006-12-10 02:21:36 -0800316/*
317 * Free any memory in @free but not in @dont.
318 */
319static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
320 struct kvm_memory_slot *dont)
321{
322 int i;
323
324 if (!dont || free->phys_mem != dont->phys_mem)
325 if (free->phys_mem) {
326 for (i = 0; i < free->npages; ++i)
Avi Kivity55a54f72006-12-29 16:49:58 -0800327 if (free->phys_mem[i])
328 __free_page(free->phys_mem[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800329 vfree(free->phys_mem);
330 }
331
332 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
333 vfree(free->dirty_bitmap);
334
Al Viro8b6d44c2007-02-09 16:38:40 +0000335 free->phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800336 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000337 free->dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800338}
339
340static void kvm_free_physmem(struct kvm *kvm)
341{
342 int i;
343
344 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000345 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800346}
347
Avi Kivity039576c2007-03-20 12:46:50 +0200348static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
349{
350 int i;
351
Rusty Russell3077c4512007-07-30 16:41:57 +1000352 for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i)
Avi Kivity039576c2007-03-20 12:46:50 +0200353 if (vcpu->pio.guest_pages[i]) {
354 __free_page(vcpu->pio.guest_pages[i]);
355 vcpu->pio.guest_pages[i] = NULL;
356 }
357}
358
Avi Kivity7b53aa52007-06-05 12:17:03 +0300359static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
360{
Avi Kivity7b53aa52007-06-05 12:17:03 +0300361 vcpu_load(vcpu);
362 kvm_mmu_unload(vcpu);
363 vcpu_put(vcpu);
364}
365
Avi Kivity6aa8b732006-12-10 02:21:36 -0800366static void kvm_free_vcpus(struct kvm *kvm)
367{
368 unsigned int i;
369
Avi Kivity7b53aa52007-06-05 12:17:03 +0300370 /*
371 * Unpin any mmu pages first.
372 */
373 for (i = 0; i < KVM_MAX_VCPUS; ++i)
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000374 if (kvm->vcpus[i])
375 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
376 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
377 if (kvm->vcpus[i]) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300378 kvm_x86_ops->vcpu_free(kvm->vcpus[i]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000379 kvm->vcpus[i] = NULL;
380 }
381 }
382
Avi Kivity6aa8b732006-12-10 02:21:36 -0800383}
384
Avi Kivityf17abe92007-02-21 19:28:04 +0200385static void kvm_destroy_vm(struct kvm *kvm)
386{
Avi Kivity133de902007-02-12 00:54:44 -0800387 spin_lock(&kvm_lock);
388 list_del(&kvm->vm_list);
389 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300390 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400391 kvm_io_bus_destroy(&kvm->mmio_bus);
Eddie Dong85f455f2007-07-06 12:20:49 +0300392 kfree(kvm->vpic);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300393 kfree(kvm->vioapic);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800394 kvm_free_vcpus(kvm);
395 kvm_free_physmem(kvm);
396 kfree(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200397}
398
399static int kvm_vm_release(struct inode *inode, struct file *filp)
400{
401 struct kvm *kvm = filp->private_data;
402
403 kvm_destroy_vm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800404 return 0;
405}
406
407static void inject_gp(struct kvm_vcpu *vcpu)
408{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300409 kvm_x86_ops->inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800410}
411
Avi Kivity1342d352007-01-05 16:36:39 -0800412/*
413 * Load the pae pdptrs. Return true is they are all valid.
414 */
415static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800416{
417 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
Avi Kivity1342d352007-01-05 16:36:39 -0800418 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800419 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800420 u64 *pdpt;
Avi Kivity1342d352007-01-05 16:36:39 -0800421 int ret;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300422 struct page *page;
Rusty Russellc820c2a2007-07-25 13:29:51 +1000423 u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424
Shaohua Li11ec2802007-07-23 14:51:37 +0800425 mutex_lock(&vcpu->kvm->lock);
Avi Kivity954bbbc2007-03-30 14:02:32 +0300426 page = gfn_to_page(vcpu->kvm, pdpt_gfn);
Rusty Russellc820c2a2007-07-25 13:29:51 +1000427 if (!page) {
428 ret = 0;
429 goto out;
430 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800431
Rusty Russellc820c2a2007-07-25 13:29:51 +1000432 pdpt = kmap_atomic(page, KM_USER0);
433 memcpy(pdpte, pdpt+offset, sizeof(pdpte));
434 kunmap_atomic(pdpt, KM_USER0);
435
436 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
437 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
Avi Kivity1342d352007-01-05 16:36:39 -0800438 ret = 0;
439 goto out;
440 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800441 }
Rusty Russellc820c2a2007-07-25 13:29:51 +1000442 ret = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800443
Rusty Russellc820c2a2007-07-25 13:29:51 +1000444 memcpy(vcpu->pdptrs, pdpte, sizeof(vcpu->pdptrs));
Avi Kivity1342d352007-01-05 16:36:39 -0800445out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800446 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800447
Avi Kivity1342d352007-01-05 16:36:39 -0800448 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800449}
450
451void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
452{
Rusty Russell707d92fa2007-07-17 23:19:08 +1000453 if (cr0 & CR0_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800454 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
455 cr0, vcpu->cr0);
456 inject_gp(vcpu);
457 return;
458 }
459
Rusty Russell707d92fa2007-07-17 23:19:08 +1000460 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800461 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
462 inject_gp(vcpu);
463 return;
464 }
465
Rusty Russell707d92fa2007-07-17 23:19:08 +1000466 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800467 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
468 "and a clear PE flag\n");
469 inject_gp(vcpu);
470 return;
471 }
472
Rusty Russell707d92fa2007-07-17 23:19:08 +1000473 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800474#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800475 if ((vcpu->shadow_efer & EFER_LME)) {
476 int cs_db, cs_l;
477
478 if (!is_pae(vcpu)) {
479 printk(KERN_DEBUG "set_cr0: #GP, start paging "
480 "in long mode while PAE is disabled\n");
481 inject_gp(vcpu);
482 return;
483 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300484 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800485 if (cs_l) {
486 printk(KERN_DEBUG "set_cr0: #GP, start paging "
487 "in long mode while CS.L == 1\n");
488 inject_gp(vcpu);
489 return;
490
491 }
492 } else
493#endif
Avi Kivity1342d352007-01-05 16:36:39 -0800494 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800495 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
496 "reserved bits\n");
497 inject_gp(vcpu);
498 return;
499 }
500
501 }
502
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300503 kvm_x86_ops->set_cr0(vcpu, cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800504 vcpu->cr0 = cr0;
505
Shaohua Li11ec2802007-07-23 14:51:37 +0800506 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800507 kvm_mmu_reset_context(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800508 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800509 return;
510}
511EXPORT_SYMBOL_GPL(set_cr0);
512
513void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
514{
515 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
516}
517EXPORT_SYMBOL_GPL(lmsw);
518
519void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
520{
Rusty Russell66aee912007-07-17 23:34:16 +1000521 if (cr4 & CR4_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800522 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
523 inject_gp(vcpu);
524 return;
525 }
526
Avi Kivitya9058ec2006-12-29 16:49:37 -0800527 if (is_long_mode(vcpu)) {
Rusty Russell66aee912007-07-17 23:34:16 +1000528 if (!(cr4 & X86_CR4_PAE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800529 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
530 "in long mode\n");
531 inject_gp(vcpu);
532 return;
533 }
Rusty Russell66aee912007-07-17 23:34:16 +1000534 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
Avi Kivity1342d352007-01-05 16:36:39 -0800535 && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800536 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
537 inject_gp(vcpu);
Rusty Russell310bc762007-07-23 17:11:02 +1000538 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800539 }
540
Rusty Russell66aee912007-07-17 23:34:16 +1000541 if (cr4 & X86_CR4_VMXE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800542 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
543 inject_gp(vcpu);
544 return;
545 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300546 kvm_x86_ops->set_cr4(vcpu, cr4);
Rusty Russell81f50e32007-09-06 01:20:38 +1000547 vcpu->cr4 = cr4;
Shaohua Li11ec2802007-07-23 14:51:37 +0800548 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800549 kvm_mmu_reset_context(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800550 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800551}
552EXPORT_SYMBOL_GPL(set_cr4);
553
554void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
555{
Avi Kivitya9058ec2006-12-29 16:49:37 -0800556 if (is_long_mode(vcpu)) {
Rusty Russellf802a302007-07-17 23:32:55 +1000557 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800558 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
559 inject_gp(vcpu);
560 return;
561 }
562 } else {
Rusty Russellf802a302007-07-17 23:32:55 +1000563 if (is_pae(vcpu)) {
564 if (cr3 & CR3_PAE_RESERVED_BITS) {
565 printk(KERN_DEBUG
566 "set_cr3: #GP, reserved bits\n");
567 inject_gp(vcpu);
568 return;
569 }
570 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
571 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
572 "reserved bits\n");
573 inject_gp(vcpu);
574 return;
575 }
576 } else {
577 if (cr3 & CR3_NONPAE_RESERVED_BITS) {
578 printk(KERN_DEBUG
579 "set_cr3: #GP, reserved bits\n");
580 inject_gp(vcpu);
581 return;
582 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800583 }
584 }
585
Shaohua Li11ec2802007-07-23 14:51:37 +0800586 mutex_lock(&vcpu->kvm->lock);
Ingo Molnard21225e2007-01-05 16:36:59 -0800587 /*
588 * Does the new cr3 value map to physical memory? (Note, we
589 * catch an invalid cr3 even in real-mode, because it would
590 * cause trouble later on when we turn on paging anyway.)
591 *
592 * A real CPU would silently accept an invalid cr3 and would
593 * attempt to use it - with largely undefined (and often hard
594 * to debug) behavior on the guest side.
595 */
596 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
597 inject_gp(vcpu);
Rusty Russellfb764412007-07-31 20:45:03 +1000598 else {
599 vcpu->cr3 = cr3;
Ingo Molnard21225e2007-01-05 16:36:59 -0800600 vcpu->mmu.new_cr3(vcpu);
Rusty Russellfb764412007-07-31 20:45:03 +1000601 }
Shaohua Li11ec2802007-07-23 14:51:37 +0800602 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800603}
604EXPORT_SYMBOL_GPL(set_cr3);
605
606void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
607{
Rusty Russell7075bc82007-07-17 23:37:17 +1000608 if (cr8 & CR8_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800609 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
610 inject_gp(vcpu);
611 return;
612 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300613 if (irqchip_in_kernel(vcpu->kvm))
614 kvm_lapic_set_tpr(vcpu, cr8);
615 else
616 vcpu->cr8 = cr8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800617}
618EXPORT_SYMBOL_GPL(set_cr8);
619
Eddie Dong7017fc32007-07-18 11:34:57 +0300620unsigned long get_cr8(struct kvm_vcpu *vcpu)
621{
Eddie Dong97222cc2007-09-12 10:58:04 +0300622 if (irqchip_in_kernel(vcpu->kvm))
623 return kvm_lapic_get_cr8(vcpu);
624 else
625 return vcpu->cr8;
Eddie Dong7017fc32007-07-18 11:34:57 +0300626}
627EXPORT_SYMBOL_GPL(get_cr8);
628
629u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
630{
Eddie Dong97222cc2007-09-12 10:58:04 +0300631 if (irqchip_in_kernel(vcpu->kvm))
632 return vcpu->apic_base;
633 else
634 return vcpu->apic_base;
Eddie Dong7017fc32007-07-18 11:34:57 +0300635}
636EXPORT_SYMBOL_GPL(kvm_get_apic_base);
637
638void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
639{
Eddie Dong97222cc2007-09-12 10:58:04 +0300640 /* TODO: reserve bits check */
641 if (irqchip_in_kernel(vcpu->kvm))
642 kvm_lapic_set_base(vcpu, data);
643 else
644 vcpu->apic_base = data;
Eddie Dong7017fc32007-07-18 11:34:57 +0300645}
646EXPORT_SYMBOL_GPL(kvm_set_apic_base);
647
Avi Kivity6aa8b732006-12-10 02:21:36 -0800648void fx_init(struct kvm_vcpu *vcpu)
649{
Rusty Russellb114b082007-07-30 21:13:43 +1000650 unsigned after_mxcsr_mask;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800651
Rusty Russell9bd01502007-07-30 16:29:56 +1000652 /* Initialize guest FPU by resetting ours and saving into guest's */
653 preempt_disable();
Rusty Russellb114b082007-07-30 21:13:43 +1000654 fx_save(&vcpu->host_fx_image);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800655 fpu_init();
Rusty Russellb114b082007-07-30 21:13:43 +1000656 fx_save(&vcpu->guest_fx_image);
657 fx_restore(&vcpu->host_fx_image);
Rusty Russell9bd01502007-07-30 16:29:56 +1000658 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800659
Amit Shah380102c2007-08-25 11:35:52 +0300660 vcpu->cr0 |= X86_CR0_ET;
Rusty Russellb114b082007-07-30 21:13:43 +1000661 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
662 vcpu->guest_fx_image.mxcsr = 0x1f80;
663 memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask,
664 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800665}
666EXPORT_SYMBOL_GPL(fx_init);
667
668/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800669 * Allocate some memory and give it an address in the guest physical address
670 * space.
671 *
672 * Discontiguous memory is allowed, mostly for framebuffers.
673 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200674static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
675 struct kvm_memory_region *mem)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800676{
677 int r;
678 gfn_t base_gfn;
679 unsigned long npages;
680 unsigned long i;
681 struct kvm_memory_slot *memslot;
682 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800683
684 r = -EINVAL;
685 /* General sanity checks */
686 if (mem->memory_size & (PAGE_SIZE - 1))
687 goto out;
688 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
689 goto out;
690 if (mem->slot >= KVM_MEMORY_SLOTS)
691 goto out;
692 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
693 goto out;
694
695 memslot = &kvm->memslots[mem->slot];
696 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
697 npages = mem->memory_size >> PAGE_SHIFT;
698
699 if (!npages)
700 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
701
Shaohua Li11ec2802007-07-23 14:51:37 +0800702 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800703
Avi Kivity6aa8b732006-12-10 02:21:36 -0800704 new = old = *memslot;
705
706 new.base_gfn = base_gfn;
707 new.npages = npages;
708 new.flags = mem->flags;
709
710 /* Disallow changing a memory slot's size. */
711 r = -EINVAL;
712 if (npages && old.npages && npages != old.npages)
713 goto out_unlock;
714
715 /* Check for overlaps */
716 r = -EEXIST;
717 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
718 struct kvm_memory_slot *s = &kvm->memslots[i];
719
720 if (s == memslot)
721 continue;
722 if (!((base_gfn + npages <= s->base_gfn) ||
723 (base_gfn >= s->base_gfn + s->npages)))
724 goto out_unlock;
725 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800726
727 /* Deallocate if slot is being removed */
728 if (!npages)
Al Viro8b6d44c2007-02-09 16:38:40 +0000729 new.phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800730
731 /* Free page dirty bitmap if unneeded */
732 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000733 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800734
735 r = -ENOMEM;
736
737 /* Allocate if a slot is being created */
738 if (npages && !new.phys_mem) {
739 new.phys_mem = vmalloc(npages * sizeof(struct page *));
740
741 if (!new.phys_mem)
Laurent Vivier0d8d2bd2007-08-30 14:56:21 +0200742 goto out_unlock;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800743
744 memset(new.phys_mem, 0, npages * sizeof(struct page *));
745 for (i = 0; i < npages; ++i) {
746 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
747 | __GFP_ZERO);
748 if (!new.phys_mem[i])
Laurent Vivier0d8d2bd2007-08-30 14:56:21 +0200749 goto out_unlock;
Markus Rechberger5972e952007-02-19 14:37:47 +0200750 set_page_private(new.phys_mem[i],0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800751 }
752 }
753
754 /* Allocate page dirty bitmap if needed */
755 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
756 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
757
758 new.dirty_bitmap = vmalloc(dirty_bytes);
759 if (!new.dirty_bitmap)
Laurent Vivier0d8d2bd2007-08-30 14:56:21 +0200760 goto out_unlock;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800761 memset(new.dirty_bitmap, 0, dirty_bytes);
762 }
763
Avi Kivity6aa8b732006-12-10 02:21:36 -0800764 if (mem->slot >= kvm->nmemslots)
765 kvm->nmemslots = mem->slot + 1;
766
767 *memslot = new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800768
Avi Kivity90cb0522007-07-17 13:04:56 +0300769 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
770 kvm_flush_remote_tlbs(kvm);
771
Shaohua Li11ec2802007-07-23 14:51:37 +0800772 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800773
Avi Kivity6aa8b732006-12-10 02:21:36 -0800774 kvm_free_physmem_slot(&old, &new);
775 return 0;
776
777out_unlock:
Shaohua Li11ec2802007-07-23 14:51:37 +0800778 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800779 kvm_free_physmem_slot(&new, &old);
780out:
781 return r;
782}
783
784/*
785 * Get (and clear) the dirty memory log for a memory slot.
786 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200787static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
788 struct kvm_dirty_log *log)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789{
790 struct kvm_memory_slot *memslot;
791 int r, i;
792 int n;
793 unsigned long any = 0;
794
Shaohua Li11ec2802007-07-23 14:51:37 +0800795 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800796
Avi Kivity6aa8b732006-12-10 02:21:36 -0800797 r = -EINVAL;
798 if (log->slot >= KVM_MEMORY_SLOTS)
799 goto out;
800
801 memslot = &kvm->memslots[log->slot];
802 r = -ENOENT;
803 if (!memslot->dirty_bitmap)
804 goto out;
805
Uri Lublincd1a4a92007-02-22 16:43:09 +0200806 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800807
Uri Lublincd1a4a92007-02-22 16:43:09 +0200808 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800809 any = memslot->dirty_bitmap[i];
810
811 r = -EFAULT;
812 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
813 goto out;
814
Rusty Russell39214912007-07-31 19:57:47 +1000815 /* If nothing is dirty, don't bother messing with page tables. */
816 if (any) {
Rusty Russell39214912007-07-31 19:57:47 +1000817 kvm_mmu_slot_remove_write_access(kvm, log->slot);
818 kvm_flush_remote_tlbs(kvm);
819 memset(memslot->dirty_bitmap, 0, n);
Rusty Russell39214912007-07-31 19:57:47 +1000820 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800821
822 r = 0;
823
824out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800825 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800826 return r;
827}
828
Avi Kivitye8207542007-03-30 16:54:30 +0300829/*
830 * Set a new alias region. Aliases map a portion of physical memory into
831 * another portion. This is useful for memory windows, for example the PC
832 * VGA region.
833 */
834static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
835 struct kvm_memory_alias *alias)
836{
837 int r, n;
838 struct kvm_mem_alias *p;
839
840 r = -EINVAL;
841 /* General sanity checks */
842 if (alias->memory_size & (PAGE_SIZE - 1))
843 goto out;
844 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
845 goto out;
846 if (alias->slot >= KVM_ALIAS_SLOTS)
847 goto out;
848 if (alias->guest_phys_addr + alias->memory_size
849 < alias->guest_phys_addr)
850 goto out;
851 if (alias->target_phys_addr + alias->memory_size
852 < alias->target_phys_addr)
853 goto out;
854
Shaohua Li11ec2802007-07-23 14:51:37 +0800855 mutex_lock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300856
857 p = &kvm->aliases[alias->slot];
858 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
859 p->npages = alias->memory_size >> PAGE_SHIFT;
860 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
861
862 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
863 if (kvm->aliases[n - 1].npages)
864 break;
865 kvm->naliases = n;
866
Avi Kivity90cb0522007-07-17 13:04:56 +0300867 kvm_mmu_zap_all(kvm);
Avi Kivitye8207542007-03-30 16:54:30 +0300868
Shaohua Li11ec2802007-07-23 14:51:37 +0800869 mutex_unlock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300870
871 return 0;
872
873out:
874 return r;
875}
876
He, Qing6ceb9d72007-07-26 11:05:18 +0300877static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
878{
879 int r;
880
881 r = 0;
882 switch (chip->chip_id) {
883 case KVM_IRQCHIP_PIC_MASTER:
884 memcpy (&chip->chip.pic,
885 &pic_irqchip(kvm)->pics[0],
886 sizeof(struct kvm_pic_state));
887 break;
888 case KVM_IRQCHIP_PIC_SLAVE:
889 memcpy (&chip->chip.pic,
890 &pic_irqchip(kvm)->pics[1],
891 sizeof(struct kvm_pic_state));
892 break;
He, Qing6bf9e962007-08-05 10:49:16 +0300893 case KVM_IRQCHIP_IOAPIC:
894 memcpy (&chip->chip.ioapic,
895 ioapic_irqchip(kvm),
896 sizeof(struct kvm_ioapic_state));
897 break;
He, Qing6ceb9d72007-07-26 11:05:18 +0300898 default:
899 r = -EINVAL;
900 break;
901 }
902 return r;
903}
904
905static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
906{
907 int r;
908
909 r = 0;
910 switch (chip->chip_id) {
911 case KVM_IRQCHIP_PIC_MASTER:
912 memcpy (&pic_irqchip(kvm)->pics[0],
913 &chip->chip.pic,
914 sizeof(struct kvm_pic_state));
915 break;
916 case KVM_IRQCHIP_PIC_SLAVE:
917 memcpy (&pic_irqchip(kvm)->pics[1],
918 &chip->chip.pic,
919 sizeof(struct kvm_pic_state));
920 break;
He, Qing6bf9e962007-08-05 10:49:16 +0300921 case KVM_IRQCHIP_IOAPIC:
922 memcpy (ioapic_irqchip(kvm),
923 &chip->chip.ioapic,
924 sizeof(struct kvm_ioapic_state));
925 break;
He, Qing6ceb9d72007-07-26 11:05:18 +0300926 default:
927 r = -EINVAL;
928 break;
929 }
930 kvm_pic_update_irq(pic_irqchip(kvm));
931 return r;
932}
933
Avi Kivitye8207542007-03-30 16:54:30 +0300934static gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
935{
936 int i;
937 struct kvm_mem_alias *alias;
938
939 for (i = 0; i < kvm->naliases; ++i) {
940 alias = &kvm->aliases[i];
941 if (gfn >= alias->base_gfn
942 && gfn < alias->base_gfn + alias->npages)
943 return alias->target_gfn + gfn - alias->base_gfn;
944 }
945 return gfn;
946}
947
948static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800949{
950 int i;
951
952 for (i = 0; i < kvm->nmemslots; ++i) {
953 struct kvm_memory_slot *memslot = &kvm->memslots[i];
954
955 if (gfn >= memslot->base_gfn
956 && gfn < memslot->base_gfn + memslot->npages)
957 return memslot;
958 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000959 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800960}
Avi Kivitye8207542007-03-30 16:54:30 +0300961
962struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
963{
964 gfn = unalias_gfn(kvm, gfn);
965 return __gfn_to_memslot(kvm, gfn);
966}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800967
Avi Kivity954bbbc2007-03-30 14:02:32 +0300968struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
969{
970 struct kvm_memory_slot *slot;
971
Avi Kivitye8207542007-03-30 16:54:30 +0300972 gfn = unalias_gfn(kvm, gfn);
973 slot = __gfn_to_memslot(kvm, gfn);
Avi Kivity954bbbc2007-03-30 14:02:32 +0300974 if (!slot)
975 return NULL;
976 return slot->phys_mem[gfn - slot->base_gfn];
977}
978EXPORT_SYMBOL_GPL(gfn_to_page);
979
Rusty Russell7e9d6192007-07-31 20:41:14 +1000980/* WARNING: Does not work on aliased pages. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800981void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
982{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +0300983 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984
Rusty Russell7e9d6192007-07-31 20:41:14 +1000985 memslot = __gfn_to_memslot(kvm, gfn);
986 if (memslot && memslot->dirty_bitmap) {
987 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988
Rusty Russell7e9d6192007-07-31 20:41:14 +1000989 /* avoid RMW */
990 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
991 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992 }
993}
994
Laurent Viviere7d5d762007-07-30 13:41:19 +0300995int emulator_read_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +0300996 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +0300998 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800999{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001000 void *data = val;
1001
1002 while (bytes) {
1003 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1004 unsigned offset = addr & (PAGE_SIZE-1);
1005 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
1006 unsigned long pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001007 struct page *page;
1008 void *page_virt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009
1010 if (gpa == UNMAPPED_GVA)
1011 return X86EMUL_PROPAGATE_FAULT;
1012 pfn = gpa >> PAGE_SHIFT;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001013 page = gfn_to_page(vcpu->kvm, pfn);
1014 if (!page)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015 return X86EMUL_UNHANDLEABLE;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001016 page_virt = kmap_atomic(page, KM_USER0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001017
Avi Kivity954bbbc2007-03-30 14:02:32 +03001018 memcpy(data, page_virt + offset, tocopy);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001019
Avi Kivity954bbbc2007-03-30 14:02:32 +03001020 kunmap_atomic(page_virt, KM_USER0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021
1022 bytes -= tocopy;
1023 data += tocopy;
1024 addr += tocopy;
1025 }
1026
1027 return X86EMUL_CONTINUE;
1028}
Laurent Viviere7d5d762007-07-30 13:41:19 +03001029EXPORT_SYMBOL_GPL(emulator_read_std);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001030
1031static int emulator_write_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001032 const void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001034 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035{
Rusty Russellf0242472007-08-01 10:48:02 +10001036 pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001037 return X86EMUL_UNHANDLEABLE;
1038}
1039
Eddie Dong97222cc2007-09-12 10:58:04 +03001040/*
1041 * Only apic need an MMIO device hook, so shortcut now..
1042 */
1043static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
1044 gpa_t addr)
1045{
1046 struct kvm_io_device *dev;
1047
1048 if (vcpu->apic) {
1049 dev = &vcpu->apic->dev;
1050 if (dev->in_range(dev, addr))
1051 return dev;
1052 }
1053 return NULL;
1054}
1055
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001056static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1057 gpa_t addr)
1058{
Eddie Dong97222cc2007-09-12 10:58:04 +03001059 struct kvm_io_device *dev;
1060
1061 dev = vcpu_find_pervcpu_dev(vcpu, addr);
1062 if (dev == NULL)
1063 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1064 return dev;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001065}
1066
Eddie Dong74906342007-06-19 18:05:03 +03001067static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
1068 gpa_t addr)
1069{
1070 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
1071}
1072
Avi Kivity6aa8b732006-12-10 02:21:36 -08001073static int emulator_read_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001074 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001075 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001076 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001077{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001078 struct kvm_io_device *mmio_dev;
1079 gpa_t gpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001080
1081 if (vcpu->mmio_read_completed) {
1082 memcpy(val, vcpu->mmio_data, bytes);
1083 vcpu->mmio_read_completed = 0;
1084 return X86EMUL_CONTINUE;
Laurent Viviercebff022007-07-30 13:35:24 +03001085 } else if (emulator_read_std(addr, val, bytes, vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001086 == X86EMUL_CONTINUE)
1087 return X86EMUL_CONTINUE;
Avi Kivityd27d4ac2007-02-19 14:37:46 +02001088
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001089 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1090 if (gpa == UNMAPPED_GVA)
1091 return X86EMUL_PROPAGATE_FAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001092
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001093 /*
1094 * Is this MMIO handled locally?
1095 */
1096 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1097 if (mmio_dev) {
1098 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1099 return X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001100 }
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001101
1102 vcpu->mmio_needed = 1;
1103 vcpu->mmio_phys_addr = gpa;
1104 vcpu->mmio_size = bytes;
1105 vcpu->mmio_is_write = 0;
1106
1107 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108}
1109
Avi Kivityda4a00f2007-01-05 16:36:44 -08001110static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity4c690a12007-04-22 15:28:19 +03001111 const void *val, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001112{
Avi Kivityda4a00f2007-01-05 16:36:44 -08001113 struct page *page;
1114 void *virt;
1115
1116 if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
1117 return 0;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001118 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1119 if (!page)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001120 return 0;
Uri Lublinab51a432007-02-21 18:25:21 +02001121 mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001122 virt = kmap_atomic(page, KM_USER0);
Shaohua Life5518812007-07-23 14:51:39 +08001123 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
Avi Kivity7cfa4b02007-07-23 18:33:14 +03001124 memcpy(virt + offset_in_page(gpa), val, bytes);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001125 kunmap_atomic(virt, KM_USER0);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001126 return 1;
1127}
1128
Avi Kivityb0fcd902007-07-22 18:48:54 +03001129static int emulator_write_emulated_onepage(unsigned long addr,
1130 const void *val,
1131 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001132 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001133{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001134 struct kvm_io_device *mmio_dev;
1135 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001136
Avi Kivityc9047f52007-04-17 10:53:22 +03001137 if (gpa == UNMAPPED_GVA) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001138 kvm_x86_ops->inject_page_fault(vcpu, addr, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001139 return X86EMUL_PROPAGATE_FAULT;
Avi Kivityc9047f52007-04-17 10:53:22 +03001140 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141
Avi Kivityda4a00f2007-01-05 16:36:44 -08001142 if (emulator_write_phys(vcpu, gpa, val, bytes))
1143 return X86EMUL_CONTINUE;
1144
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001145 /*
1146 * Is this MMIO handled locally?
1147 */
1148 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1149 if (mmio_dev) {
1150 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1151 return X86EMUL_CONTINUE;
1152 }
1153
Avi Kivity6aa8b732006-12-10 02:21:36 -08001154 vcpu->mmio_needed = 1;
1155 vcpu->mmio_phys_addr = gpa;
1156 vcpu->mmio_size = bytes;
1157 vcpu->mmio_is_write = 1;
Avi Kivity4c690a12007-04-22 15:28:19 +03001158 memcpy(vcpu->mmio_data, val, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159
1160 return X86EMUL_CONTINUE;
1161}
1162
Laurent Viviere7d5d762007-07-30 13:41:19 +03001163int emulator_write_emulated(unsigned long addr,
Avi Kivityb0fcd902007-07-22 18:48:54 +03001164 const void *val,
1165 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001166 struct kvm_vcpu *vcpu)
Avi Kivityb0fcd902007-07-22 18:48:54 +03001167{
1168 /* Crossing a page boundary? */
1169 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1170 int rc, now;
1171
1172 now = -addr & ~PAGE_MASK;
Laurent Viviercebff022007-07-30 13:35:24 +03001173 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001174 if (rc != X86EMUL_CONTINUE)
1175 return rc;
1176 addr += now;
1177 val += now;
1178 bytes -= now;
1179 }
Laurent Viviercebff022007-07-30 13:35:24 +03001180 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001181}
Laurent Viviere7d5d762007-07-30 13:41:19 +03001182EXPORT_SYMBOL_GPL(emulator_write_emulated);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001183
Avi Kivity6aa8b732006-12-10 02:21:36 -08001184static int emulator_cmpxchg_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001185 const void *old,
1186 const void *new,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001187 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001188 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001189{
1190 static int reported;
1191
1192 if (!reported) {
1193 reported = 1;
1194 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1195 }
Laurent Viviercebff022007-07-30 13:35:24 +03001196 return emulator_write_emulated(addr, new, bytes, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001197}
1198
1199static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1200{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001201 return kvm_x86_ops->get_segment_base(vcpu, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001202}
1203
1204int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1205{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001206 return X86EMUL_CONTINUE;
1207}
1208
1209int emulate_clts(struct kvm_vcpu *vcpu)
1210{
Rusty Russell81f50e32007-09-06 01:20:38 +10001211 vcpu->cr0 &= ~X86_CR0_TS;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001212 kvm_x86_ops->set_cr0(vcpu, vcpu->cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001213 return X86EMUL_CONTINUE;
1214}
1215
1216int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1217{
1218 struct kvm_vcpu *vcpu = ctxt->vcpu;
1219
1220 switch (dr) {
1221 case 0 ... 3:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001222 *dest = kvm_x86_ops->get_dr(vcpu, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001223 return X86EMUL_CONTINUE;
1224 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001225 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001226 return X86EMUL_UNHANDLEABLE;
1227 }
1228}
1229
1230int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1231{
1232 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1233 int exception;
1234
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001235 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001236 if (exception) {
1237 /* FIXME: better handling */
1238 return X86EMUL_UNHANDLEABLE;
1239 }
1240 return X86EMUL_CONTINUE;
1241}
1242
Avi Kivity054b1362007-09-12 13:21:09 +03001243void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001244{
1245 static int reported;
1246 u8 opcodes[4];
Avi Kivity054b1362007-09-12 13:21:09 +03001247 unsigned long rip = vcpu->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001248 unsigned long rip_linear;
1249
Avi Kivity054b1362007-09-12 13:21:09 +03001250 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001251
1252 if (reported)
1253 return;
1254
Avi Kivity054b1362007-09-12 13:21:09 +03001255 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001256
Avi Kivity054b1362007-09-12 13:21:09 +03001257 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1258 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001259 reported = 1;
1260}
Avi Kivity054b1362007-09-12 13:21:09 +03001261EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001262
1263struct x86_emulate_ops emulate_ops = {
1264 .read_std = emulator_read_std,
1265 .write_std = emulator_write_std,
1266 .read_emulated = emulator_read_emulated,
1267 .write_emulated = emulator_write_emulated,
1268 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1269};
1270
1271int emulate_instruction(struct kvm_vcpu *vcpu,
1272 struct kvm_run *run,
1273 unsigned long cr2,
1274 u16 error_code)
1275{
1276 struct x86_emulate_ctxt emulate_ctxt;
1277 int r;
1278 int cs_db, cs_l;
1279
Avi Kivitye7df56e2007-03-14 15:54:54 +02001280 vcpu->mmio_fault_cr2 = cr2;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001281 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001282
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001283 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001284
1285 emulate_ctxt.vcpu = vcpu;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001286 emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001287 emulate_ctxt.cr2 = cr2;
1288 emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
1289 ? X86EMUL_MODE_REAL : cs_l
1290 ? X86EMUL_MODE_PROT64 : cs_db
1291 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1292
1293 if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1294 emulate_ctxt.cs_base = 0;
1295 emulate_ctxt.ds_base = 0;
1296 emulate_ctxt.es_base = 0;
1297 emulate_ctxt.ss_base = 0;
1298 } else {
1299 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1300 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1301 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1302 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1303 }
1304
1305 emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1306 emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1307
1308 vcpu->mmio_is_write = 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03001309 vcpu->pio.string = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001310 r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
Laurent Viviere70669a2007-08-05 10:36:40 +03001311 if (vcpu->pio.string)
1312 return EMULATE_DO_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001313
1314 if ((r || vcpu->mmio_is_write) && run) {
Jeff Dike8fc0d082007-07-17 12:26:59 -04001315 run->exit_reason = KVM_EXIT_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001316 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1317 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1318 run->mmio.len = vcpu->mmio_size;
1319 run->mmio.is_write = vcpu->mmio_is_write;
1320 }
1321
1322 if (r) {
Avi Kivitya4360362007-01-05 16:36:45 -08001323 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1324 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001325 if (!vcpu->mmio_needed) {
Avi Kivity054b1362007-09-12 13:21:09 +03001326 kvm_report_emulation_failure(vcpu, "mmio");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001327 return EMULATE_FAIL;
1328 }
1329 return EMULATE_DO_MMIO;
1330 }
1331
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001332 kvm_x86_ops->decache_regs(vcpu);
1333 kvm_x86_ops->set_rflags(vcpu, emulate_ctxt.eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001334
Avi Kivity02c83202007-04-29 15:02:17 +03001335 if (vcpu->mmio_is_write) {
1336 vcpu->mmio_needed = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001337 return EMULATE_DO_MMIO;
Avi Kivity02c83202007-04-29 15:02:17 +03001338 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001339
1340 return EMULATE_DONE;
1341}
1342EXPORT_SYMBOL_GPL(emulate_instruction);
1343
Eddie Dongb6958ce2007-07-18 12:15:21 +03001344/*
1345 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1346 */
He, Qingc5ec1532007-09-03 17:07:41 +03001347static void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001348{
1349 DECLARE_WAITQUEUE(wait, current);
1350
1351 add_wait_queue(&vcpu->wq, &wait);
1352
1353 /*
1354 * We will block until either an interrupt or a signal wakes us up
1355 */
He, Qingc5ec1532007-09-03 17:07:41 +03001356 while (!kvm_cpu_has_interrupt(vcpu)
1357 && !signal_pending(current)
1358 && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE
1359 && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) {
Eddie Dongb6958ce2007-07-18 12:15:21 +03001360 set_current_state(TASK_INTERRUPTIBLE);
1361 vcpu_put(vcpu);
1362 schedule();
1363 vcpu_load(vcpu);
1364 }
1365
He, Qingc5ec1532007-09-03 17:07:41 +03001366 __set_current_state(TASK_RUNNING);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001367 remove_wait_queue(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001368}
1369
Avi Kivityd3bef152007-06-05 15:53:05 +03001370int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1371{
Avi Kivityd3bef152007-06-05 15:53:05 +03001372 ++vcpu->stat.halt_exits;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001373 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc5ec1532007-09-03 17:07:41 +03001374 vcpu->mp_state = VCPU_MP_STATE_HALTED;
1375 kvm_vcpu_block(vcpu);
1376 if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE)
1377 return -EINTR;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001378 return 1;
1379 } else {
1380 vcpu->run->exit_reason = KVM_EXIT_HLT;
1381 return 0;
1382 }
Avi Kivityd3bef152007-06-05 15:53:05 +03001383}
1384EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1385
Avi Kivity270fd9b2007-02-19 14:37:47 +02001386int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
1387{
1388 unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
1389
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001390 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity270fd9b2007-02-19 14:37:47 +02001391 ret = -KVM_EINVAL;
1392#ifdef CONFIG_X86_64
1393 if (is_long_mode(vcpu)) {
1394 nr = vcpu->regs[VCPU_REGS_RAX];
1395 a0 = vcpu->regs[VCPU_REGS_RDI];
1396 a1 = vcpu->regs[VCPU_REGS_RSI];
1397 a2 = vcpu->regs[VCPU_REGS_RDX];
1398 a3 = vcpu->regs[VCPU_REGS_RCX];
1399 a4 = vcpu->regs[VCPU_REGS_R8];
1400 a5 = vcpu->regs[VCPU_REGS_R9];
1401 } else
1402#endif
1403 {
1404 nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
1405 a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
1406 a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
1407 a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
1408 a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
1409 a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
1410 a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
1411 }
1412 switch (nr) {
1413 default:
Jeff Dike519ef352007-07-16 15:24:47 -04001414 run->hypercall.nr = nr;
Avi Kivityb4e63f52007-03-04 13:59:30 +02001415 run->hypercall.args[0] = a0;
1416 run->hypercall.args[1] = a1;
1417 run->hypercall.args[2] = a2;
1418 run->hypercall.args[3] = a3;
1419 run->hypercall.args[4] = a4;
1420 run->hypercall.args[5] = a5;
1421 run->hypercall.ret = ret;
1422 run->hypercall.longmode = is_long_mode(vcpu);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001423 kvm_x86_ops->decache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02001424 return 0;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001425 }
1426 vcpu->regs[VCPU_REGS_RAX] = ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001427 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity270fd9b2007-02-19 14:37:47 +02001428 return 1;
1429}
1430EXPORT_SYMBOL_GPL(kvm_hypercall);
1431
Avi Kivity6aa8b732006-12-10 02:21:36 -08001432static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1433{
1434 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1435}
1436
1437void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1438{
1439 struct descriptor_table dt = { limit, base };
1440
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001441 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001442}
1443
1444void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1445{
1446 struct descriptor_table dt = { limit, base };
1447
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001448 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001449}
1450
1451void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1452 unsigned long *rflags)
1453{
1454 lmsw(vcpu, msw);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001455 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001456}
1457
1458unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1459{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001460 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001461 switch (cr) {
1462 case 0:
1463 return vcpu->cr0;
1464 case 2:
1465 return vcpu->cr2;
1466 case 3:
1467 return vcpu->cr3;
1468 case 4:
1469 return vcpu->cr4;
1470 default:
1471 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1472 return 0;
1473 }
1474}
1475
1476void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1477 unsigned long *rflags)
1478{
1479 switch (cr) {
1480 case 0:
1481 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001482 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001483 break;
1484 case 2:
1485 vcpu->cr2 = val;
1486 break;
1487 case 3:
1488 set_cr3(vcpu, val);
1489 break;
1490 case 4:
1491 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1492 break;
1493 default:
1494 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1495 }
1496}
1497
Ingo Molnar102d8322007-02-19 14:37:47 +02001498/*
1499 * Register the para guest with the host:
1500 */
1501static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
1502{
1503 struct kvm_vcpu_para_state *para_state;
1504 hpa_t para_state_hpa, hypercall_hpa;
1505 struct page *para_state_page;
1506 unsigned char *hypercall;
1507 gpa_t hypercall_gpa;
1508
1509 printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
1510 printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
1511
1512 /*
1513 * Needs to be page aligned:
1514 */
1515 if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
1516 goto err_gp;
1517
1518 para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
1519 printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
1520 if (is_error_hpa(para_state_hpa))
1521 goto err_gp;
1522
Uri Lublinab51a432007-02-21 18:25:21 +02001523 mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
Ingo Molnar102d8322007-02-19 14:37:47 +02001524 para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
Shaohua Life5518812007-07-23 14:51:39 +08001525 para_state = kmap(para_state_page);
Ingo Molnar102d8322007-02-19 14:37:47 +02001526
1527 printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
1528 printk(KERN_DEBUG ".... size: %d\n", para_state->size);
1529
1530 para_state->host_version = KVM_PARA_API_VERSION;
1531 /*
1532 * We cannot support guests that try to register themselves
1533 * with a newer API version than the host supports:
1534 */
1535 if (para_state->guest_version > KVM_PARA_API_VERSION) {
1536 para_state->ret = -KVM_EINVAL;
1537 goto err_kunmap_skip;
1538 }
1539
1540 hypercall_gpa = para_state->hypercall_gpa;
1541 hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
1542 printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
1543 if (is_error_hpa(hypercall_hpa)) {
1544 para_state->ret = -KVM_EINVAL;
1545 goto err_kunmap_skip;
1546 }
1547
1548 printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
1549 vcpu->para_state_page = para_state_page;
1550 vcpu->para_state_gpa = para_state_gpa;
1551 vcpu->hypercall_gpa = hypercall_gpa;
1552
Uri Lublinab51a432007-02-21 18:25:21 +02001553 mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
Ingo Molnar102d8322007-02-19 14:37:47 +02001554 hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
1555 KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001556 kvm_x86_ops->patch_hypercall(vcpu, hypercall);
Ingo Molnar102d8322007-02-19 14:37:47 +02001557 kunmap_atomic(hypercall, KM_USER1);
1558
1559 para_state->ret = 0;
1560err_kunmap_skip:
Shaohua Life5518812007-07-23 14:51:39 +08001561 kunmap(para_state_page);
Ingo Molnar102d8322007-02-19 14:37:47 +02001562 return 0;
1563err_gp:
1564 return 1;
1565}
1566
Avi Kivity3bab1f52006-12-29 16:49:48 -08001567int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1568{
1569 u64 data;
1570
1571 switch (msr) {
1572 case 0xc0010010: /* SYSCFG */
1573 case 0xc0010015: /* HWCR */
1574 case MSR_IA32_PLATFORM_ID:
1575 case MSR_IA32_P5_MC_ADDR:
1576 case MSR_IA32_P5_MC_TYPE:
1577 case MSR_IA32_MC0_CTL:
1578 case MSR_IA32_MCG_STATUS:
1579 case MSR_IA32_MCG_CAP:
1580 case MSR_IA32_MC0_MISC:
1581 case MSR_IA32_MC0_MISC+4:
1582 case MSR_IA32_MC0_MISC+8:
1583 case MSR_IA32_MC0_MISC+12:
1584 case MSR_IA32_MC0_MISC+16:
1585 case MSR_IA32_UCODE_REV:
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001586 case MSR_IA32_PERF_STATUS:
Matthew Gregan2dc70942007-05-06 10:59:46 +03001587 case MSR_IA32_EBL_CR_POWERON:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001588 /* MTRR registers */
1589 case 0xfe:
1590 case 0x200 ... 0x2ff:
1591 data = 0;
1592 break;
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001593 case 0xcd: /* fsb frequency */
1594 data = 3;
1595 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001596 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001597 data = kvm_get_apic_base(vcpu);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001598 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001599 case MSR_IA32_MISC_ENABLE:
1600 data = vcpu->ia32_misc_enable_msr;
1601 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001602#ifdef CONFIG_X86_64
1603 case MSR_EFER:
1604 data = vcpu->shadow_efer;
1605 break;
1606#endif
1607 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001608 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001609 return 1;
1610 }
1611 *pdata = data;
1612 return 0;
1613}
1614EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1615
Avi Kivity6aa8b732006-12-10 02:21:36 -08001616/*
1617 * Reads an msr value (of 'msr_index') into 'pdata'.
1618 * Returns 0 on success, non-0 otherwise.
1619 * Assumes vcpu_load() was already called.
1620 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001621int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001622{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001623 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001624}
1625
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001626#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001627
Avi Kivity3bab1f52006-12-29 16:49:48 -08001628static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001629{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001630 if (efer & EFER_RESERVED_BITS) {
1631 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1632 efer);
1633 inject_gp(vcpu);
1634 return;
1635 }
1636
1637 if (is_paging(vcpu)
1638 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1639 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1640 inject_gp(vcpu);
1641 return;
1642 }
1643
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001644 kvm_x86_ops->set_efer(vcpu, efer);
Avi Kivity7725f0b2006-12-13 00:34:01 -08001645
Avi Kivity6aa8b732006-12-10 02:21:36 -08001646 efer &= ~EFER_LMA;
1647 efer |= vcpu->shadow_efer & EFER_LMA;
1648
1649 vcpu->shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001650}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001651
1652#endif
1653
Avi Kivity3bab1f52006-12-29 16:49:48 -08001654int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1655{
1656 switch (msr) {
1657#ifdef CONFIG_X86_64
1658 case MSR_EFER:
1659 set_efer(vcpu, data);
1660 break;
1661#endif
1662 case MSR_IA32_MC0_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001663 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
Avi Kivity3bab1f52006-12-29 16:49:48 -08001664 __FUNCTION__, data);
1665 break;
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001666 case MSR_IA32_MCG_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001667 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001668 __FUNCTION__, data);
1669 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001670 case MSR_IA32_UCODE_REV:
1671 case MSR_IA32_UCODE_WRITE:
1672 case 0x200 ... 0x2ff: /* MTRRs */
1673 break;
1674 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001675 kvm_set_apic_base(vcpu, data);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001676 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001677 case MSR_IA32_MISC_ENABLE:
1678 vcpu->ia32_misc_enable_msr = data;
1679 break;
Ingo Molnar102d8322007-02-19 14:37:47 +02001680 /*
1681 * This is the 'probe whether the host is KVM' logic:
1682 */
1683 case MSR_KVM_API_MAGIC:
1684 return vcpu_register_para(vcpu, data);
1685
Avi Kivity3bab1f52006-12-29 16:49:48 -08001686 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001687 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001688 return 1;
1689 }
1690 return 0;
1691}
1692EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1693
Avi Kivity6aa8b732006-12-10 02:21:36 -08001694/*
1695 * Writes msr value into into the appropriate "register".
1696 * Returns 0 on success, non-0 otherwise.
1697 * Assumes vcpu_load() was already called.
1698 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001699int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001700{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001701 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001702}
1703
1704void kvm_resched(struct kvm_vcpu *vcpu)
1705{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001706 if (!need_resched())
1707 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001708 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001709}
1710EXPORT_SYMBOL_GPL(kvm_resched);
1711
Avi Kivity06465c52007-02-28 20:46:53 +02001712void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1713{
1714 int i;
1715 u32 function;
1716 struct kvm_cpuid_entry *e, *best;
1717
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001718 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001719 function = vcpu->regs[VCPU_REGS_RAX];
1720 vcpu->regs[VCPU_REGS_RAX] = 0;
1721 vcpu->regs[VCPU_REGS_RBX] = 0;
1722 vcpu->regs[VCPU_REGS_RCX] = 0;
1723 vcpu->regs[VCPU_REGS_RDX] = 0;
1724 best = NULL;
1725 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1726 e = &vcpu->cpuid_entries[i];
1727 if (e->function == function) {
1728 best = e;
1729 break;
1730 }
1731 /*
1732 * Both basic or both extended?
1733 */
1734 if (((e->function ^ function) & 0x80000000) == 0)
1735 if (!best || e->function > best->function)
1736 best = e;
1737 }
1738 if (best) {
1739 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1740 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1741 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1742 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1743 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001744 kvm_x86_ops->decache_regs(vcpu);
1745 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001746}
1747EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1748
Avi Kivity039576c2007-03-20 12:46:50 +02001749static int pio_copy_data(struct kvm_vcpu *vcpu)
Avi Kivity46fc1472007-02-22 19:39:30 +02001750{
Avi Kivity039576c2007-03-20 12:46:50 +02001751 void *p = vcpu->pio_data;
1752 void *q;
1753 unsigned bytes;
1754 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1755
Avi Kivity039576c2007-03-20 12:46:50 +02001756 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1757 PAGE_KERNEL);
1758 if (!q) {
Avi Kivity039576c2007-03-20 12:46:50 +02001759 free_pio_guest_pages(vcpu);
1760 return -ENOMEM;
1761 }
1762 q += vcpu->pio.guest_page_offset;
1763 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1764 if (vcpu->pio.in)
1765 memcpy(q, p, bytes);
1766 else
1767 memcpy(p, q, bytes);
1768 q -= vcpu->pio.guest_page_offset;
1769 vunmap(q);
Avi Kivity039576c2007-03-20 12:46:50 +02001770 free_pio_guest_pages(vcpu);
1771 return 0;
1772}
1773
1774static int complete_pio(struct kvm_vcpu *vcpu)
1775{
1776 struct kvm_pio_request *io = &vcpu->pio;
Avi Kivity46fc1472007-02-22 19:39:30 +02001777 long delta;
Avi Kivity039576c2007-03-20 12:46:50 +02001778 int r;
Avi Kivity46fc1472007-02-22 19:39:30 +02001779
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001780 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001781
1782 if (!io->string) {
Avi Kivity039576c2007-03-20 12:46:50 +02001783 if (io->in)
1784 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
Avi Kivity46fc1472007-02-22 19:39:30 +02001785 io->size);
1786 } else {
Avi Kivity039576c2007-03-20 12:46:50 +02001787 if (io->in) {
1788 r = pio_copy_data(vcpu);
1789 if (r) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001790 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02001791 return r;
1792 }
1793 }
1794
Avi Kivity46fc1472007-02-22 19:39:30 +02001795 delta = 1;
1796 if (io->rep) {
Avi Kivity039576c2007-03-20 12:46:50 +02001797 delta *= io->cur_count;
Avi Kivity46fc1472007-02-22 19:39:30 +02001798 /*
1799 * The size of the register should really depend on
1800 * current address size.
1801 */
1802 vcpu->regs[VCPU_REGS_RCX] -= delta;
1803 }
Avi Kivity039576c2007-03-20 12:46:50 +02001804 if (io->down)
Avi Kivity46fc1472007-02-22 19:39:30 +02001805 delta = -delta;
1806 delta *= io->size;
Avi Kivity039576c2007-03-20 12:46:50 +02001807 if (io->in)
Avi Kivity46fc1472007-02-22 19:39:30 +02001808 vcpu->regs[VCPU_REGS_RDI] += delta;
1809 else
1810 vcpu->regs[VCPU_REGS_RSI] += delta;
1811 }
1812
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001813 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001814
Avi Kivity039576c2007-03-20 12:46:50 +02001815 io->count -= io->cur_count;
1816 io->cur_count = 0;
1817
Avi Kivity039576c2007-03-20 12:46:50 +02001818 return 0;
Avi Kivity46fc1472007-02-22 19:39:30 +02001819}
1820
Eddie Dong65619eb2007-07-17 11:52:33 +03001821static void kernel_pio(struct kvm_io_device *pio_dev,
1822 struct kvm_vcpu *vcpu,
1823 void *pd)
Eddie Dong74906342007-06-19 18:05:03 +03001824{
1825 /* TODO: String I/O for in kernel device */
1826
Eddie Dong9cf98822007-07-22 10:36:31 +03001827 mutex_lock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001828 if (vcpu->pio.in)
1829 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1830 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001831 pd);
Eddie Dong74906342007-06-19 18:05:03 +03001832 else
1833 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1834 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001835 pd);
Eddie Dong9cf98822007-07-22 10:36:31 +03001836 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001837}
1838
1839static void pio_string_write(struct kvm_io_device *pio_dev,
1840 struct kvm_vcpu *vcpu)
1841{
1842 struct kvm_pio_request *io = &vcpu->pio;
1843 void *pd = vcpu->pio_data;
1844 int i;
1845
Eddie Dong9cf98822007-07-22 10:36:31 +03001846 mutex_lock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001847 for (i = 0; i < io->cur_count; i++) {
1848 kvm_iodevice_write(pio_dev, io->port,
1849 io->size,
1850 pd);
1851 pd += io->size;
1852 }
Eddie Dong9cf98822007-07-22 10:36:31 +03001853 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001854}
1855
Laurent Vivier3090dd72007-08-05 10:43:32 +03001856int kvm_emulate_pio (struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1857 int size, unsigned port)
1858{
1859 struct kvm_io_device *pio_dev;
1860
1861 vcpu->run->exit_reason = KVM_EXIT_IO;
1862 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1863 vcpu->run->io.size = vcpu->pio.size = size;
1864 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1865 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1;
1866 vcpu->run->io.port = vcpu->pio.port = port;
1867 vcpu->pio.in = in;
1868 vcpu->pio.string = 0;
1869 vcpu->pio.down = 0;
1870 vcpu->pio.guest_page_offset = 0;
1871 vcpu->pio.rep = 0;
1872
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001873 kvm_x86_ops->cache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03001874 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001875 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03001876
Avi Kivity0967b7b2007-09-15 17:34:36 +03001877 kvm_x86_ops->skip_emulated_instruction(vcpu);
1878
Laurent Vivier3090dd72007-08-05 10:43:32 +03001879 pio_dev = vcpu_find_pio_dev(vcpu, port);
1880 if (pio_dev) {
1881 kernel_pio(pio_dev, vcpu, vcpu->pio_data);
1882 complete_pio(vcpu);
1883 return 1;
1884 }
1885 return 0;
1886}
1887EXPORT_SYMBOL_GPL(kvm_emulate_pio);
1888
1889int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1890 int size, unsigned long count, int down,
Avi Kivity039576c2007-03-20 12:46:50 +02001891 gva_t address, int rep, unsigned port)
1892{
1893 unsigned now, in_page;
Eddie Dong65619eb2007-07-17 11:52:33 +03001894 int i, ret = 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001895 int nr_pages = 1;
1896 struct page *page;
Eddie Dong74906342007-06-19 18:05:03 +03001897 struct kvm_io_device *pio_dev;
Avi Kivity039576c2007-03-20 12:46:50 +02001898
1899 vcpu->run->exit_reason = KVM_EXIT_IO;
1900 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Laurent Vivier3090dd72007-08-05 10:43:32 +03001901 vcpu->run->io.size = vcpu->pio.size = size;
Avi Kivity039576c2007-03-20 12:46:50 +02001902 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Laurent Vivier3090dd72007-08-05 10:43:32 +03001903 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count;
1904 vcpu->run->io.port = vcpu->pio.port = port;
Avi Kivity039576c2007-03-20 12:46:50 +02001905 vcpu->pio.in = in;
Laurent Vivier3090dd72007-08-05 10:43:32 +03001906 vcpu->pio.string = 1;
Avi Kivity039576c2007-03-20 12:46:50 +02001907 vcpu->pio.down = down;
1908 vcpu->pio.guest_page_offset = offset_in_page(address);
1909 vcpu->pio.rep = rep;
1910
Avi Kivity039576c2007-03-20 12:46:50 +02001911 if (!count) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001912 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02001913 return 1;
1914 }
1915
Avi Kivity039576c2007-03-20 12:46:50 +02001916 if (!down)
1917 in_page = PAGE_SIZE - offset_in_page(address);
1918 else
1919 in_page = offset_in_page(address) + size;
1920 now = min(count, (unsigned long)in_page / size);
1921 if (!now) {
1922 /*
1923 * String I/O straddles page boundary. Pin two guest pages
1924 * so that we satisfy atomicity constraints. Do just one
1925 * transaction to avoid complexity.
1926 */
1927 nr_pages = 2;
1928 now = 1;
1929 }
1930 if (down) {
1931 /*
1932 * String I/O in reverse. Yuck. Kill the guest, fix later.
1933 */
Rusty Russellf0242472007-08-01 10:48:02 +10001934 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivity039576c2007-03-20 12:46:50 +02001935 inject_gp(vcpu);
1936 return 1;
1937 }
1938 vcpu->run->io.count = now;
1939 vcpu->pio.cur_count = now;
1940
Avi Kivity0967b7b2007-09-15 17:34:36 +03001941 if (vcpu->pio.cur_count == vcpu->pio.count)
1942 kvm_x86_ops->skip_emulated_instruction(vcpu);
1943
Avi Kivity039576c2007-03-20 12:46:50 +02001944 for (i = 0; i < nr_pages; ++i) {
Shaohua Li11ec2802007-07-23 14:51:37 +08001945 mutex_lock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02001946 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
1947 if (page)
1948 get_page(page);
1949 vcpu->pio.guest_pages[i] = page;
Shaohua Li11ec2802007-07-23 14:51:37 +08001950 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02001951 if (!page) {
1952 inject_gp(vcpu);
1953 free_pio_guest_pages(vcpu);
1954 return 1;
1955 }
1956 }
1957
Laurent Vivier3090dd72007-08-05 10:43:32 +03001958 pio_dev = vcpu_find_pio_dev(vcpu, port);
Eddie Dong65619eb2007-07-17 11:52:33 +03001959 if (!vcpu->pio.in) {
1960 /* string PIO write */
1961 ret = pio_copy_data(vcpu);
1962 if (ret >= 0 && pio_dev) {
1963 pio_string_write(pio_dev, vcpu);
1964 complete_pio(vcpu);
1965 if (vcpu->pio.count == 0)
1966 ret = 1;
1967 }
1968 } else if (pio_dev)
Rusty Russellf0242472007-08-01 10:48:02 +10001969 pr_unimpl(vcpu, "no string pio read support yet, "
Eddie Dong65619eb2007-07-17 11:52:33 +03001970 "port %x size %d count %ld\n",
1971 port, size, count);
1972
1973 return ret;
Avi Kivity039576c2007-03-20 12:46:50 +02001974}
Laurent Vivier3090dd72007-08-05 10:43:32 +03001975EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
Avi Kivity039576c2007-03-20 12:46:50 +02001976
Avi Kivity04d2cc72007-09-10 18:10:54 +03001977/*
1978 * Check if userspace requested an interrupt window, and that the
1979 * interrupt window is open.
1980 *
1981 * No need to exit to userspace if we already have an interrupt queued.
1982 */
1983static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
1984 struct kvm_run *kvm_run)
1985{
1986 return (!vcpu->irq_summary &&
1987 kvm_run->request_interrupt_window &&
1988 vcpu->interrupt_window_open &&
1989 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
1990}
1991
1992static void post_kvm_run_save(struct kvm_vcpu *vcpu,
1993 struct kvm_run *kvm_run)
1994{
1995 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
1996 kvm_run->cr8 = get_cr8(vcpu);
1997 kvm_run->apic_base = kvm_get_apic_base(vcpu);
1998 if (irqchip_in_kernel(vcpu->kvm))
1999 kvm_run->ready_for_interrupt_injection = 1;
2000 else
2001 kvm_run->ready_for_interrupt_injection =
2002 (vcpu->interrupt_window_open &&
2003 vcpu->irq_summary == 0);
2004}
2005
2006static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2007{
2008 int r;
2009
2010 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
2011 printk("vcpu %d received sipi with vector # %x\n",
2012 vcpu->vcpu_id, vcpu->sipi_vector);
2013 kvm_lapic_reset(vcpu);
2014 kvm_x86_ops->vcpu_reset(vcpu);
2015 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
2016 }
2017
2018preempted:
2019 if (vcpu->guest_debug.enabled)
2020 kvm_x86_ops->guest_debug_pre(vcpu);
2021
2022again:
2023 r = kvm_mmu_reload(vcpu);
2024 if (unlikely(r))
2025 goto out;
2026
2027 preempt_disable();
2028
2029 kvm_x86_ops->prepare_guest_switch(vcpu);
2030 kvm_load_guest_fpu(vcpu);
2031
2032 local_irq_disable();
2033
2034 if (signal_pending(current)) {
2035 local_irq_enable();
2036 preempt_enable();
2037 r = -EINTR;
2038 kvm_run->exit_reason = KVM_EXIT_INTR;
2039 ++vcpu->stat.signal_exits;
2040 goto out;
2041 }
2042
2043 if (irqchip_in_kernel(vcpu->kvm))
2044 kvm_x86_ops->inject_pending_irq(vcpu);
2045 else if (!vcpu->mmio_read_completed)
2046 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2047
2048 vcpu->guest_mode = 1;
Laurent Vivierd172fcd2007-10-15 17:00:19 +02002049 kvm_guest_enter();
Avi Kivity04d2cc72007-09-10 18:10:54 +03002050
2051 if (vcpu->requests)
2052 if (test_and_clear_bit(KVM_TLB_FLUSH, &vcpu->requests))
2053 kvm_x86_ops->tlb_flush(vcpu);
2054
2055 kvm_x86_ops->run(vcpu, kvm_run);
2056
2057 vcpu->guest_mode = 0;
2058 local_irq_enable();
2059
2060 ++vcpu->stat.exits;
2061
Laurent Vivier0552f732007-10-18 15:19:01 +02002062 /*
2063 * We must have an instruction between local_irq_enable() and
2064 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2065 * the interrupt shadow. The stat.exits increment will do nicely.
2066 * But we need to prevent reordering, hence this barrier():
2067 */
2068 barrier();
2069
2070 kvm_guest_exit();
2071
Avi Kivity04d2cc72007-09-10 18:10:54 +03002072 preempt_enable();
2073
2074 /*
2075 * Profile KVM exit RIPs:
2076 */
2077 if (unlikely(prof_on == KVM_PROFILING)) {
2078 kvm_x86_ops->cache_regs(vcpu);
2079 profile_hit(KVM_PROFILING, (void *)vcpu->rip);
2080 }
2081
2082 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2083
2084 if (r > 0) {
2085 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2086 r = -EINTR;
2087 kvm_run->exit_reason = KVM_EXIT_INTR;
2088 ++vcpu->stat.request_irq_exits;
2089 goto out;
2090 }
2091 if (!need_resched()) {
2092 ++vcpu->stat.light_exits;
2093 goto again;
2094 }
2095 }
2096
2097out:
2098 if (r > 0) {
2099 kvm_resched(vcpu);
2100 goto preempted;
2101 }
2102
2103 post_kvm_run_save(vcpu, kvm_run);
2104
2105 return r;
2106}
2107
2108
Avi Kivitybccf2152007-02-21 18:04:26 +02002109static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002110{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002111 int r;
Avi Kivity1961d272007-03-05 19:46:05 +02002112 sigset_t sigsaved;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002113
Avi Kivitybccf2152007-02-21 18:04:26 +02002114 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002115
He, Qingc5ec1532007-09-03 17:07:41 +03002116 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
2117 kvm_vcpu_block(vcpu);
2118 vcpu_put(vcpu);
2119 return -EAGAIN;
2120 }
2121
Avi Kivity1961d272007-03-05 19:46:05 +02002122 if (vcpu->sigset_active)
2123 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2124
Dor Laor54810342007-02-12 00:54:39 -08002125 /* re-sync apic's tpr */
He, Qing5cd4f6f2007-08-30 17:04:26 +08002126 if (!irqchip_in_kernel(vcpu->kvm))
2127 set_cr8(vcpu, kvm_run->cr8);
Dor Laor54810342007-02-12 00:54:39 -08002128
Avi Kivity02c83202007-04-29 15:02:17 +03002129 if (vcpu->pio.cur_count) {
2130 r = complete_pio(vcpu);
2131 if (r)
2132 goto out;
2133 }
2134
2135 if (vcpu->mmio_needed) {
2136 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
2137 vcpu->mmio_read_completed = 1;
2138 vcpu->mmio_needed = 0;
2139 r = emulate_instruction(vcpu, kvm_run,
2140 vcpu->mmio_fault_cr2, 0);
2141 if (r == EMULATE_DO_MMIO) {
2142 /*
2143 * Read-modify-write. Back to userspace.
2144 */
Avi Kivity02c83202007-04-29 15:02:17 +03002145 r = 0;
2146 goto out;
Avi Kivity46fc1472007-02-22 19:39:30 +02002147 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002148 }
2149
Avi Kivity8eb7d332007-03-04 14:17:08 +02002150 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002151 kvm_x86_ops->cache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002152 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002153 kvm_x86_ops->decache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002154 }
2155
Avi Kivity04d2cc72007-09-10 18:10:54 +03002156 r = __vcpu_run(vcpu, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002157
Avi Kivity039576c2007-03-20 12:46:50 +02002158out:
Avi Kivity1961d272007-03-05 19:46:05 +02002159 if (vcpu->sigset_active)
2160 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2161
Avi Kivity6aa8b732006-12-10 02:21:36 -08002162 vcpu_put(vcpu);
2163 return r;
2164}
2165
Avi Kivitybccf2152007-02-21 18:04:26 +02002166static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
2167 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002168{
Avi Kivitybccf2152007-02-21 18:04:26 +02002169 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002170
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002171 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002172
2173 regs->rax = vcpu->regs[VCPU_REGS_RAX];
2174 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
2175 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
2176 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
2177 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
2178 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
2179 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
2180 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002181#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002182 regs->r8 = vcpu->regs[VCPU_REGS_R8];
2183 regs->r9 = vcpu->regs[VCPU_REGS_R9];
2184 regs->r10 = vcpu->regs[VCPU_REGS_R10];
2185 regs->r11 = vcpu->regs[VCPU_REGS_R11];
2186 regs->r12 = vcpu->regs[VCPU_REGS_R12];
2187 regs->r13 = vcpu->regs[VCPU_REGS_R13];
2188 regs->r14 = vcpu->regs[VCPU_REGS_R14];
2189 regs->r15 = vcpu->regs[VCPU_REGS_R15];
2190#endif
2191
2192 regs->rip = vcpu->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002193 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002194
2195 /*
2196 * Don't leak debug flags in case they were set for guest debugging
2197 */
2198 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2199 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2200
2201 vcpu_put(vcpu);
2202
2203 return 0;
2204}
2205
Avi Kivitybccf2152007-02-21 18:04:26 +02002206static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
2207 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002208{
Avi Kivitybccf2152007-02-21 18:04:26 +02002209 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002210
2211 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
2212 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
2213 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
2214 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
2215 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
2216 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
2217 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
2218 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002219#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002220 vcpu->regs[VCPU_REGS_R8] = regs->r8;
2221 vcpu->regs[VCPU_REGS_R9] = regs->r9;
2222 vcpu->regs[VCPU_REGS_R10] = regs->r10;
2223 vcpu->regs[VCPU_REGS_R11] = regs->r11;
2224 vcpu->regs[VCPU_REGS_R12] = regs->r12;
2225 vcpu->regs[VCPU_REGS_R13] = regs->r13;
2226 vcpu->regs[VCPU_REGS_R14] = regs->r14;
2227 vcpu->regs[VCPU_REGS_R15] = regs->r15;
2228#endif
2229
2230 vcpu->rip = regs->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002231 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002232
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002233 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002234
2235 vcpu_put(vcpu);
2236
2237 return 0;
2238}
2239
2240static void get_segment(struct kvm_vcpu *vcpu,
2241 struct kvm_segment *var, int seg)
2242{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002243 return kvm_x86_ops->get_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002244}
2245
Avi Kivitybccf2152007-02-21 18:04:26 +02002246static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2247 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002248{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002249 struct descriptor_table dt;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002250 int pending_vec;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002251
Avi Kivitybccf2152007-02-21 18:04:26 +02002252 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002253
2254 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2255 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2256 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2257 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2258 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2259 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2260
2261 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2262 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2263
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002264 kvm_x86_ops->get_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002265 sregs->idt.limit = dt.limit;
2266 sregs->idt.base = dt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002267 kvm_x86_ops->get_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002268 sregs->gdt.limit = dt.limit;
2269 sregs->gdt.base = dt.base;
2270
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002271 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002272 sregs->cr0 = vcpu->cr0;
2273 sregs->cr2 = vcpu->cr2;
2274 sregs->cr3 = vcpu->cr3;
2275 sregs->cr4 = vcpu->cr4;
Eddie Dong7017fc32007-07-18 11:34:57 +03002276 sregs->cr8 = get_cr8(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002277 sregs->efer = vcpu->shadow_efer;
Eddie Dong7017fc32007-07-18 11:34:57 +03002278 sregs->apic_base = kvm_get_apic_base(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002279
Eddie Dong2a8067f2007-08-06 16:29:07 +03002280 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc52fb352007-08-02 14:03:07 +03002281 memset(sregs->interrupt_bitmap, 0,
2282 sizeof sregs->interrupt_bitmap);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002283 pending_vec = kvm_x86_ops->get_irq(vcpu);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002284 if (pending_vec >= 0)
2285 set_bit(pending_vec, (unsigned long *)sregs->interrupt_bitmap);
2286 } else
He, Qingc52fb352007-08-02 14:03:07 +03002287 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2288 sizeof sregs->interrupt_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002289
2290 vcpu_put(vcpu);
2291
2292 return 0;
2293}
2294
2295static void set_segment(struct kvm_vcpu *vcpu,
2296 struct kvm_segment *var, int seg)
2297{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002298 return kvm_x86_ops->set_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002299}
2300
Avi Kivitybccf2152007-02-21 18:04:26 +02002301static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2302 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002303{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002304 int mmu_reset_needed = 0;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002305 int i, pending_vec, max_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002306 struct descriptor_table dt;
2307
Avi Kivitybccf2152007-02-21 18:04:26 +02002308 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002309
Avi Kivity6aa8b732006-12-10 02:21:36 -08002310 dt.limit = sregs->idt.limit;
2311 dt.base = sregs->idt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002312 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002313 dt.limit = sregs->gdt.limit;
2314 dt.base = sregs->gdt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002315 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002316
2317 vcpu->cr2 = sregs->cr2;
2318 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2319 vcpu->cr3 = sregs->cr3;
2320
Eddie Dong7017fc32007-07-18 11:34:57 +03002321 set_cr8(vcpu, sregs->cr8);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002322
2323 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002324#ifdef CONFIG_X86_64
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002325 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002326#endif
Eddie Dong7017fc32007-07-18 11:34:57 +03002327 kvm_set_apic_base(vcpu, sregs->apic_base);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002328
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002329 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity399badf2007-01-05 16:36:38 -08002330
Avi Kivity6aa8b732006-12-10 02:21:36 -08002331 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
Rusty Russell81f50e32007-09-06 01:20:38 +10002332 vcpu->cr0 = sregs->cr0;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002333 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002334
2335 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002336 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Avi Kivity1b0973b2007-01-05 16:36:41 -08002337 if (!is_long_mode(vcpu) && is_pae(vcpu))
2338 load_pdptrs(vcpu, vcpu->cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002339
2340 if (mmu_reset_needed)
2341 kvm_mmu_reset_context(vcpu);
2342
He, Qingc52fb352007-08-02 14:03:07 +03002343 if (!irqchip_in_kernel(vcpu->kvm)) {
2344 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2345 sizeof vcpu->irq_pending);
2346 vcpu->irq_summary = 0;
2347 for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
2348 if (vcpu->irq_pending[i])
2349 __set_bit(i, &vcpu->irq_summary);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002350 } else {
2351 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
2352 pending_vec = find_first_bit(
2353 (const unsigned long *)sregs->interrupt_bitmap,
2354 max_bits);
2355 /* Only pending external irq is handled here */
2356 if (pending_vec < max_bits) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002357 kvm_x86_ops->set_irq(vcpu, pending_vec);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002358 printk("Set back pending irq %d\n", pending_vec);
2359 }
He, Qingc52fb352007-08-02 14:03:07 +03002360 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002361
Avi Kivity024aa1c2007-03-21 13:44:58 +02002362 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2363 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2364 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2365 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2366 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2367 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2368
2369 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2370 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2371
Avi Kivity6aa8b732006-12-10 02:21:36 -08002372 vcpu_put(vcpu);
2373
2374 return 0;
2375}
2376
Rusty Russell1747fb72007-09-06 01:21:32 +10002377void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2378{
2379 struct kvm_segment cs;
2380
2381 get_segment(vcpu, &cs, VCPU_SREG_CS);
2382 *db = cs.db;
2383 *l = cs.l;
2384}
2385EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2386
Avi Kivity6aa8b732006-12-10 02:21:36 -08002387/*
2388 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2389 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
Michael Riepebf591b22006-12-22 01:05:36 -08002390 *
2391 * This list is modified at module load time to reflect the
2392 * capabilities of the host cpu.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002393 */
2394static u32 msrs_to_save[] = {
2395 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
2396 MSR_K6_STAR,
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002397#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002398 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
2399#endif
2400 MSR_IA32_TIME_STAMP_COUNTER,
2401};
2402
Michael Riepebf591b22006-12-22 01:05:36 -08002403static unsigned num_msrs_to_save;
2404
Avi Kivity6f00e682007-01-26 00:56:40 -08002405static u32 emulated_msrs[] = {
2406 MSR_IA32_MISC_ENABLE,
2407};
2408
Michael Riepebf591b22006-12-22 01:05:36 -08002409static __init void kvm_init_msr_list(void)
2410{
2411 u32 dummy[2];
2412 unsigned i, j;
2413
2414 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2415 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2416 continue;
2417 if (j < i)
2418 msrs_to_save[j] = msrs_to_save[i];
2419 j++;
2420 }
2421 num_msrs_to_save = j;
2422}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002423
2424/*
2425 * Adapt set_msr() to msr_io()'s calling convention
2426 */
2427static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2428{
Avi Kivity35f3f282007-07-17 14:20:30 +03002429 return kvm_set_msr(vcpu, index, *data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002430}
2431
2432/*
2433 * Read or write a bunch of msrs. All parameters are kernel addresses.
2434 *
2435 * @return number of msrs set successfully.
2436 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002437static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002438 struct kvm_msr_entry *entries,
2439 int (*do_msr)(struct kvm_vcpu *vcpu,
2440 unsigned index, u64 *data))
2441{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002442 int i;
2443
Avi Kivitybccf2152007-02-21 18:04:26 +02002444 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002445
2446 for (i = 0; i < msrs->nmsrs; ++i)
2447 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2448 break;
2449
2450 vcpu_put(vcpu);
2451
2452 return i;
2453}
2454
2455/*
2456 * Read or write a bunch of msrs. Parameters are user addresses.
2457 *
2458 * @return number of msrs set successfully.
2459 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002460static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002461 int (*do_msr)(struct kvm_vcpu *vcpu,
2462 unsigned index, u64 *data),
2463 int writeback)
2464{
2465 struct kvm_msrs msrs;
2466 struct kvm_msr_entry *entries;
2467 int r, n;
2468 unsigned size;
2469
2470 r = -EFAULT;
2471 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2472 goto out;
2473
2474 r = -E2BIG;
2475 if (msrs.nmsrs >= MAX_IO_MSRS)
2476 goto out;
2477
2478 r = -ENOMEM;
2479 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
2480 entries = vmalloc(size);
2481 if (!entries)
2482 goto out;
2483
2484 r = -EFAULT;
2485 if (copy_from_user(entries, user_msrs->entries, size))
2486 goto out_free;
2487
Avi Kivitybccf2152007-02-21 18:04:26 +02002488 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002489 if (r < 0)
2490 goto out_free;
2491
2492 r = -EFAULT;
2493 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2494 goto out_free;
2495
2496 r = n;
2497
2498out_free:
2499 vfree(entries);
2500out:
2501 return r;
2502}
2503
2504/*
2505 * Translate a guest virtual address to a guest physical address.
2506 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002507static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2508 struct kvm_translation *tr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002509{
2510 unsigned long vaddr = tr->linear_address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002511 gpa_t gpa;
2512
Avi Kivitybccf2152007-02-21 18:04:26 +02002513 vcpu_load(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +08002514 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002515 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2516 tr->physical_address = gpa;
2517 tr->valid = gpa != UNMAPPED_GVA;
2518 tr->writeable = 1;
2519 tr->usermode = 0;
Shaohua Li11ec2802007-07-23 14:51:37 +08002520 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002521 vcpu_put(vcpu);
2522
2523 return 0;
2524}
2525
Avi Kivitybccf2152007-02-21 18:04:26 +02002526static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2527 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002528{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002529 if (irq->irq < 0 || irq->irq >= 256)
2530 return -EINVAL;
Eddie Dong97222cc2007-09-12 10:58:04 +03002531 if (irqchip_in_kernel(vcpu->kvm))
2532 return -ENXIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02002533 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002534
2535 set_bit(irq->irq, vcpu->irq_pending);
2536 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2537
2538 vcpu_put(vcpu);
2539
2540 return 0;
2541}
2542
Avi Kivitybccf2152007-02-21 18:04:26 +02002543static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2544 struct kvm_debug_guest *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002545{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002546 int r;
2547
Avi Kivitybccf2152007-02-21 18:04:26 +02002548 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002549
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002550 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002551
2552 vcpu_put(vcpu);
2553
2554 return r;
2555}
2556
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002557static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2558 unsigned long address,
2559 int *type)
2560{
2561 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2562 unsigned long pgoff;
2563 struct page *page;
2564
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002565 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +02002566 if (pgoff == 0)
2567 page = virt_to_page(vcpu->run);
2568 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2569 page = virt_to_page(vcpu->pio_data);
2570 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002571 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002572 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002573 if (type != NULL)
2574 *type = VM_FAULT_MINOR;
2575
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002576 return page;
2577}
2578
2579static struct vm_operations_struct kvm_vcpu_vm_ops = {
2580 .nopage = kvm_vcpu_nopage,
2581};
2582
2583static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2584{
2585 vma->vm_ops = &kvm_vcpu_vm_ops;
2586 return 0;
2587}
2588
Avi Kivitybccf2152007-02-21 18:04:26 +02002589static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2590{
2591 struct kvm_vcpu *vcpu = filp->private_data;
2592
2593 fput(vcpu->kvm->filp);
2594 return 0;
2595}
2596
2597static struct file_operations kvm_vcpu_fops = {
2598 .release = kvm_vcpu_release,
2599 .unlocked_ioctl = kvm_vcpu_ioctl,
2600 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002601 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02002602};
2603
2604/*
2605 * Allocates an inode for the vcpu.
2606 */
2607static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2608{
2609 int fd, r;
2610 struct inode *inode;
2611 struct file *file;
2612
Avi Kivityd6d28162007-06-28 08:38:16 -04002613 r = anon_inode_getfd(&fd, &inode, &file,
2614 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2615 if (r)
2616 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002617 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +02002618 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02002619}
2620
Avi Kivityc5ea7662007-02-20 18:41:05 +02002621/*
2622 * Creates some virtual cpus. Good luck creating more than one.
2623 */
2624static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2625{
2626 int r;
2627 struct kvm_vcpu *vcpu;
2628
Avi Kivityc5ea7662007-02-20 18:41:05 +02002629 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002630 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002631
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002632 vcpu = kvm_x86_ops->vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002633 if (IS_ERR(vcpu))
2634 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002635
Avi Kivity15ad7142007-07-11 18:17:21 +03002636 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
2637
Rusty Russellb114b082007-07-30 21:13:43 +10002638 /* We do fxsave: this must be aligned. */
2639 BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF);
2640
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002641 vcpu_load(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002642 r = kvm_mmu_setup(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002643 vcpu_put(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002644 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002645 goto free_vcpu;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002646
Shaohua Li11ec2802007-07-23 14:51:37 +08002647 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002648 if (kvm->vcpus[n]) {
2649 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +08002650 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002651 goto mmu_unload;
2652 }
2653 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08002654 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002655
2656 /* Now it's all set up, let userspace reach it */
Avi Kivitybccf2152007-02-21 18:04:26 +02002657 r = create_vcpu_fd(vcpu);
2658 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002659 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02002660 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002661
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002662unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08002663 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002664 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +08002665 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002666
2667mmu_unload:
2668 vcpu_load(vcpu);
2669 kvm_mmu_unload(vcpu);
2670 vcpu_put(vcpu);
2671
2672free_vcpu:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002673 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002674 return r;
2675}
2676
Eddie Dong2cc51562007-05-21 07:28:09 +03002677static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
2678{
2679 u64 efer;
2680 int i;
2681 struct kvm_cpuid_entry *e, *entry;
2682
2683 rdmsrl(MSR_EFER, efer);
2684 entry = NULL;
2685 for (i = 0; i < vcpu->cpuid_nent; ++i) {
2686 e = &vcpu->cpuid_entries[i];
2687 if (e->function == 0x80000001) {
2688 entry = e;
2689 break;
2690 }
2691 }
Avi Kivity4c981b42007-07-25 09:22:12 +03002692 if (entry && (entry->edx & (1 << 20)) && !(efer & EFER_NX)) {
Eddie Dong2cc51562007-05-21 07:28:09 +03002693 entry->edx &= ~(1 << 20);
Avi Kivity4c981b42007-07-25 09:22:12 +03002694 printk(KERN_INFO "kvm: guest NX capability removed\n");
Eddie Dong2cc51562007-05-21 07:28:09 +03002695 }
2696}
2697
Avi Kivity06465c52007-02-28 20:46:53 +02002698static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
2699 struct kvm_cpuid *cpuid,
2700 struct kvm_cpuid_entry __user *entries)
2701{
2702 int r;
2703
2704 r = -E2BIG;
2705 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2706 goto out;
2707 r = -EFAULT;
2708 if (copy_from_user(&vcpu->cpuid_entries, entries,
2709 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
2710 goto out;
2711 vcpu->cpuid_nent = cpuid->nent;
Eddie Dong2cc51562007-05-21 07:28:09 +03002712 cpuid_fix_nx_cap(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002713 return 0;
2714
2715out:
2716 return r;
2717}
2718
Avi Kivity1961d272007-03-05 19:46:05 +02002719static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2720{
2721 if (sigset) {
2722 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2723 vcpu->sigset_active = 1;
2724 vcpu->sigset = *sigset;
2725 } else
2726 vcpu->sigset_active = 0;
2727 return 0;
2728}
2729
Avi Kivityb8836732007-04-01 16:34:31 +03002730/*
2731 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2732 * we have asm/x86/processor.h
2733 */
2734struct fxsave {
2735 u16 cwd;
2736 u16 swd;
2737 u16 twd;
2738 u16 fop;
2739 u64 rip;
2740 u64 rdp;
2741 u32 mxcsr;
2742 u32 mxcsr_mask;
2743 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2744#ifdef CONFIG_X86_64
2745 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2746#else
2747 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2748#endif
2749};
2750
2751static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2752{
Rusty Russellb114b082007-07-30 21:13:43 +10002753 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002754
2755 vcpu_load(vcpu);
2756
2757 memcpy(fpu->fpr, fxsave->st_space, 128);
2758 fpu->fcw = fxsave->cwd;
2759 fpu->fsw = fxsave->swd;
2760 fpu->ftwx = fxsave->twd;
2761 fpu->last_opcode = fxsave->fop;
2762 fpu->last_ip = fxsave->rip;
2763 fpu->last_dp = fxsave->rdp;
2764 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2765
2766 vcpu_put(vcpu);
2767
2768 return 0;
2769}
2770
2771static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2772{
Rusty Russellb114b082007-07-30 21:13:43 +10002773 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002774
2775 vcpu_load(vcpu);
2776
2777 memcpy(fxsave->st_space, fpu->fpr, 128);
2778 fxsave->cwd = fpu->fcw;
2779 fxsave->swd = fpu->fsw;
2780 fxsave->twd = fpu->ftwx;
2781 fxsave->fop = fpu->last_opcode;
2782 fxsave->rip = fpu->last_ip;
2783 fxsave->rdp = fpu->last_dp;
2784 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2785
2786 vcpu_put(vcpu);
2787
2788 return 0;
2789}
2790
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002791static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
2792 struct kvm_lapic_state *s)
2793{
2794 vcpu_load(vcpu);
2795 memcpy(s->regs, vcpu->apic->regs, sizeof *s);
2796 vcpu_put(vcpu);
2797
2798 return 0;
2799}
2800
2801static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
2802 struct kvm_lapic_state *s)
2803{
2804 vcpu_load(vcpu);
2805 memcpy(vcpu->apic->regs, s->regs, sizeof *s);
2806 kvm_apic_post_state_restore(vcpu);
2807 vcpu_put(vcpu);
2808
2809 return 0;
2810}
2811
Avi Kivitybccf2152007-02-21 18:04:26 +02002812static long kvm_vcpu_ioctl(struct file *filp,
2813 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002814{
Avi Kivitybccf2152007-02-21 18:04:26 +02002815 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00002816 void __user *argp = (void __user *)arg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002817 int r = -EINVAL;
2818
2819 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002820 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002821 r = -EINVAL;
2822 if (arg)
2823 goto out;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002824 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002825 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002826 case KVM_GET_REGS: {
2827 struct kvm_regs kvm_regs;
2828
Avi Kivitybccf2152007-02-21 18:04:26 +02002829 memset(&kvm_regs, 0, sizeof kvm_regs);
2830 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002831 if (r)
2832 goto out;
2833 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002834 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002835 goto out;
2836 r = 0;
2837 break;
2838 }
2839 case KVM_SET_REGS: {
2840 struct kvm_regs kvm_regs;
2841
2842 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002843 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002844 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002845 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002846 if (r)
2847 goto out;
2848 r = 0;
2849 break;
2850 }
2851 case KVM_GET_SREGS: {
2852 struct kvm_sregs kvm_sregs;
2853
Avi Kivitybccf2152007-02-21 18:04:26 +02002854 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2855 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002856 if (r)
2857 goto out;
2858 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002859 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002860 goto out;
2861 r = 0;
2862 break;
2863 }
2864 case KVM_SET_SREGS: {
2865 struct kvm_sregs kvm_sregs;
2866
2867 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002868 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002869 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002870 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002871 if (r)
2872 goto out;
2873 r = 0;
2874 break;
2875 }
2876 case KVM_TRANSLATE: {
2877 struct kvm_translation tr;
2878
2879 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002880 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002881 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002882 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002883 if (r)
2884 goto out;
2885 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002886 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002887 goto out;
2888 r = 0;
2889 break;
2890 }
2891 case KVM_INTERRUPT: {
2892 struct kvm_interrupt irq;
2893
2894 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002895 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002896 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002897 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002898 if (r)
2899 goto out;
2900 r = 0;
2901 break;
2902 }
2903 case KVM_DEBUG_GUEST: {
2904 struct kvm_debug_guest dbg;
2905
2906 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002907 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002908 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002909 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002910 if (r)
2911 goto out;
2912 r = 0;
2913 break;
2914 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002915 case KVM_GET_MSRS:
Avi Kivity35f3f282007-07-17 14:20:30 +03002916 r = msr_io(vcpu, argp, kvm_get_msr, 1);
Avi Kivitybccf2152007-02-21 18:04:26 +02002917 break;
2918 case KVM_SET_MSRS:
2919 r = msr_io(vcpu, argp, do_set_msr, 0);
2920 break;
Avi Kivity06465c52007-02-28 20:46:53 +02002921 case KVM_SET_CPUID: {
2922 struct kvm_cpuid __user *cpuid_arg = argp;
2923 struct kvm_cpuid cpuid;
2924
2925 r = -EFAULT;
2926 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2927 goto out;
2928 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2929 if (r)
2930 goto out;
2931 break;
2932 }
Avi Kivity1961d272007-03-05 19:46:05 +02002933 case KVM_SET_SIGNAL_MASK: {
2934 struct kvm_signal_mask __user *sigmask_arg = argp;
2935 struct kvm_signal_mask kvm_sigmask;
2936 sigset_t sigset, *p;
2937
2938 p = NULL;
2939 if (argp) {
2940 r = -EFAULT;
2941 if (copy_from_user(&kvm_sigmask, argp,
2942 sizeof kvm_sigmask))
2943 goto out;
2944 r = -EINVAL;
2945 if (kvm_sigmask.len != sizeof sigset)
2946 goto out;
2947 r = -EFAULT;
2948 if (copy_from_user(&sigset, sigmask_arg->sigset,
2949 sizeof sigset))
2950 goto out;
2951 p = &sigset;
2952 }
2953 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2954 break;
2955 }
Avi Kivityb8836732007-04-01 16:34:31 +03002956 case KVM_GET_FPU: {
2957 struct kvm_fpu fpu;
2958
2959 memset(&fpu, 0, sizeof fpu);
2960 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2961 if (r)
2962 goto out;
2963 r = -EFAULT;
2964 if (copy_to_user(argp, &fpu, sizeof fpu))
2965 goto out;
2966 r = 0;
2967 break;
2968 }
2969 case KVM_SET_FPU: {
2970 struct kvm_fpu fpu;
2971
2972 r = -EFAULT;
2973 if (copy_from_user(&fpu, argp, sizeof fpu))
2974 goto out;
2975 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2976 if (r)
2977 goto out;
2978 r = 0;
2979 break;
2980 }
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002981 case KVM_GET_LAPIC: {
2982 struct kvm_lapic_state lapic;
2983
2984 memset(&lapic, 0, sizeof lapic);
2985 r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic);
2986 if (r)
2987 goto out;
2988 r = -EFAULT;
2989 if (copy_to_user(argp, &lapic, sizeof lapic))
2990 goto out;
2991 r = 0;
2992 break;
2993 }
2994 case KVM_SET_LAPIC: {
2995 struct kvm_lapic_state lapic;
2996
2997 r = -EFAULT;
2998 if (copy_from_user(&lapic, argp, sizeof lapic))
2999 goto out;
3000 r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);;
3001 if (r)
3002 goto out;
3003 r = 0;
3004 break;
3005 }
Avi Kivitybccf2152007-02-21 18:04:26 +02003006 default:
3007 ;
3008 }
3009out:
3010 return r;
3011}
3012
3013static long kvm_vm_ioctl(struct file *filp,
3014 unsigned int ioctl, unsigned long arg)
3015{
3016 struct kvm *kvm = filp->private_data;
3017 void __user *argp = (void __user *)arg;
3018 int r = -EINVAL;
3019
3020 switch (ioctl) {
3021 case KVM_CREATE_VCPU:
3022 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
3023 if (r < 0)
3024 goto out;
3025 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003026 case KVM_SET_MEMORY_REGION: {
3027 struct kvm_memory_region kvm_mem;
3028
3029 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00003030 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003031 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02003032 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003033 if (r)
3034 goto out;
3035 break;
3036 }
3037 case KVM_GET_DIRTY_LOG: {
3038 struct kvm_dirty_log log;
3039
3040 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00003041 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003042 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02003043 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003044 if (r)
3045 goto out;
3046 break;
3047 }
Avi Kivitye8207542007-03-30 16:54:30 +03003048 case KVM_SET_MEMORY_ALIAS: {
3049 struct kvm_memory_alias alias;
3050
3051 r = -EFAULT;
3052 if (copy_from_user(&alias, argp, sizeof alias))
3053 goto out;
3054 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
3055 if (r)
3056 goto out;
3057 break;
3058 }
Eddie Dong85f455f2007-07-06 12:20:49 +03003059 case KVM_CREATE_IRQCHIP:
3060 r = -ENOMEM;
3061 kvm->vpic = kvm_create_pic(kvm);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03003062 if (kvm->vpic) {
3063 r = kvm_ioapic_init(kvm);
3064 if (r) {
3065 kfree(kvm->vpic);
3066 kvm->vpic = NULL;
3067 goto out;
3068 }
3069 }
Eddie Dong85f455f2007-07-06 12:20:49 +03003070 else
3071 goto out;
3072 break;
3073 case KVM_IRQ_LINE: {
3074 struct kvm_irq_level irq_event;
3075
3076 r = -EFAULT;
3077 if (copy_from_user(&irq_event, argp, sizeof irq_event))
3078 goto out;
3079 if (irqchip_in_kernel(kvm)) {
Eddie Dong9cf98822007-07-22 10:36:31 +03003080 mutex_lock(&kvm->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +03003081 if (irq_event.irq < 16)
3082 kvm_pic_set_irq(pic_irqchip(kvm),
3083 irq_event.irq,
3084 irq_event.level);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03003085 kvm_ioapic_set_irq(kvm->vioapic,
3086 irq_event.irq,
3087 irq_event.level);
Eddie Dong9cf98822007-07-22 10:36:31 +03003088 mutex_unlock(&kvm->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +03003089 r = 0;
3090 }
3091 break;
3092 }
He, Qing6ceb9d72007-07-26 11:05:18 +03003093 case KVM_GET_IRQCHIP: {
3094 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3095 struct kvm_irqchip chip;
3096
3097 r = -EFAULT;
3098 if (copy_from_user(&chip, argp, sizeof chip))
3099 goto out;
3100 r = -ENXIO;
3101 if (!irqchip_in_kernel(kvm))
3102 goto out;
3103 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
3104 if (r)
3105 goto out;
3106 r = -EFAULT;
3107 if (copy_to_user(argp, &chip, sizeof chip))
3108 goto out;
3109 r = 0;
3110 break;
3111 }
3112 case KVM_SET_IRQCHIP: {
3113 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3114 struct kvm_irqchip chip;
3115
3116 r = -EFAULT;
3117 if (copy_from_user(&chip, argp, sizeof chip))
3118 goto out;
3119 r = -ENXIO;
3120 if (!irqchip_in_kernel(kvm))
3121 goto out;
3122 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
3123 if (r)
3124 goto out;
3125 r = 0;
3126 break;
3127 }
Avi Kivityf17abe92007-02-21 19:28:04 +02003128 default:
3129 ;
3130 }
3131out:
3132 return r;
3133}
3134
3135static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
3136 unsigned long address,
3137 int *type)
3138{
3139 struct kvm *kvm = vma->vm_file->private_data;
3140 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02003141 struct page *page;
3142
Avi Kivityf17abe92007-02-21 19:28:04 +02003143 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity954bbbc2007-03-30 14:02:32 +03003144 page = gfn_to_page(kvm, pgoff);
Avi Kivityf17abe92007-02-21 19:28:04 +02003145 if (!page)
3146 return NOPAGE_SIGBUS;
3147 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03003148 if (type != NULL)
3149 *type = VM_FAULT_MINOR;
3150
Avi Kivityf17abe92007-02-21 19:28:04 +02003151 return page;
3152}
3153
3154static struct vm_operations_struct kvm_vm_vm_ops = {
3155 .nopage = kvm_vm_nopage,
3156};
3157
3158static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
3159{
3160 vma->vm_ops = &kvm_vm_vm_ops;
3161 return 0;
3162}
3163
3164static struct file_operations kvm_vm_fops = {
3165 .release = kvm_vm_release,
3166 .unlocked_ioctl = kvm_vm_ioctl,
3167 .compat_ioctl = kvm_vm_ioctl,
3168 .mmap = kvm_vm_mmap,
3169};
3170
3171static int kvm_dev_ioctl_create_vm(void)
3172{
3173 int fd, r;
3174 struct inode *inode;
3175 struct file *file;
3176 struct kvm *kvm;
3177
Avi Kivityf17abe92007-02-21 19:28:04 +02003178 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04003179 if (IS_ERR(kvm))
3180 return PTR_ERR(kvm);
3181 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
3182 if (r) {
3183 kvm_destroy_vm(kvm);
3184 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02003185 }
3186
Avi Kivitybccf2152007-02-21 18:04:26 +02003187 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02003188
Avi Kivityf17abe92007-02-21 19:28:04 +02003189 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02003190}
3191
3192static long kvm_dev_ioctl(struct file *filp,
3193 unsigned int ioctl, unsigned long arg)
3194{
3195 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02003196 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02003197
3198 switch (ioctl) {
3199 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02003200 r = -EINVAL;
3201 if (arg)
3202 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02003203 r = KVM_API_VERSION;
3204 break;
3205 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02003206 r = -EINVAL;
3207 if (arg)
3208 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02003209 r = kvm_dev_ioctl_create_vm();
3210 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003211 case KVM_GET_MSR_INDEX_LIST: {
Al Viro2f3669872007-02-09 16:38:35 +00003212 struct kvm_msr_list __user *user_msr_list = argp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003213 struct kvm_msr_list msr_list;
3214 unsigned n;
3215
3216 r = -EFAULT;
3217 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
3218 goto out;
3219 n = msr_list.nmsrs;
Avi Kivity6f00e682007-01-26 00:56:40 -08003220 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003221 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
3222 goto out;
3223 r = -E2BIG;
Michael Riepebf591b22006-12-22 01:05:36 -08003224 if (n < num_msrs_to_save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003225 goto out;
3226 r = -EFAULT;
3227 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
Michael Riepebf591b22006-12-22 01:05:36 -08003228 num_msrs_to_save * sizeof(u32)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003229 goto out;
Avi Kivity6f00e682007-01-26 00:56:40 -08003230 if (copy_to_user(user_msr_list->indices
3231 + num_msrs_to_save * sizeof(u32),
3232 &emulated_msrs,
3233 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
3234 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003235 r = 0;
Avi Kivitycc1d8952007-01-05 16:36:58 -08003236 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003237 }
Eddie Dong85f455f2007-07-06 12:20:49 +03003238 case KVM_CHECK_EXTENSION: {
3239 int ext = (long)argp;
3240
3241 switch (ext) {
3242 case KVM_CAP_IRQCHIP:
Eddie Dongb6958ce2007-07-18 12:15:21 +03003243 case KVM_CAP_HLT:
Eddie Dong85f455f2007-07-06 12:20:49 +03003244 r = 1;
3245 break;
3246 default:
3247 r = 0;
3248 break;
3249 }
Avi Kivity5d308f42007-03-01 17:56:20 +02003250 break;
Eddie Dong85f455f2007-07-06 12:20:49 +03003251 }
Avi Kivity07c45a32007-03-07 13:05:38 +02003252 case KVM_GET_VCPU_MMAP_SIZE:
3253 r = -EINVAL;
3254 if (arg)
3255 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02003256 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02003257 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003258 default:
3259 ;
3260 }
3261out:
3262 return r;
3263}
3264
Avi Kivity6aa8b732006-12-10 02:21:36 -08003265static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003266 .unlocked_ioctl = kvm_dev_ioctl,
3267 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003268};
3269
3270static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02003271 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003272 "kvm",
3273 &kvm_chardev_ops,
3274};
3275
Avi Kivity774c47f2007-02-12 00:54:47 -08003276/*
3277 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
3278 * cached on it.
3279 */
3280static void decache_vcpus_on_cpu(int cpu)
3281{
3282 struct kvm *vm;
3283 struct kvm_vcpu *vcpu;
3284 int i;
3285
3286 spin_lock(&kvm_lock);
Shaohua Li11ec2802007-07-23 14:51:37 +08003287 list_for_each_entry(vm, &vm_list, vm_list)
Avi Kivity774c47f2007-02-12 00:54:47 -08003288 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003289 vcpu = vm->vcpus[i];
3290 if (!vcpu)
3291 continue;
Avi Kivity774c47f2007-02-12 00:54:47 -08003292 /*
3293 * If the vcpu is locked, then it is running on some
3294 * other cpu and therefore it is not cached on the
3295 * cpu in question.
3296 *
3297 * If it's not locked, check the last cpu it executed
3298 * on.
3299 */
3300 if (mutex_trylock(&vcpu->mutex)) {
3301 if (vcpu->cpu == cpu) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003302 kvm_x86_ops->vcpu_decache(vcpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08003303 vcpu->cpu = -1;
3304 }
3305 mutex_unlock(&vcpu->mutex);
3306 }
3307 }
3308 spin_unlock(&kvm_lock);
3309}
3310
Avi Kivity1b6c0162007-05-24 13:03:52 +03003311static void hardware_enable(void *junk)
3312{
3313 int cpu = raw_smp_processor_id();
3314
3315 if (cpu_isset(cpu, cpus_hardware_enabled))
3316 return;
3317 cpu_set(cpu, cpus_hardware_enabled);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003318 kvm_x86_ops->hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003319}
3320
3321static void hardware_disable(void *junk)
3322{
3323 int cpu = raw_smp_processor_id();
3324
3325 if (!cpu_isset(cpu, cpus_hardware_enabled))
3326 return;
3327 cpu_clear(cpu, cpus_hardware_enabled);
3328 decache_vcpus_on_cpu(cpu);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003329 kvm_x86_ops->hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003330}
3331
Avi Kivity774c47f2007-02-12 00:54:47 -08003332static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
3333 void *v)
3334{
3335 int cpu = (long)v;
3336
3337 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03003338 case CPU_DYING:
3339 case CPU_DYING_FROZEN:
Avi Kivity6ec8a852007-08-19 15:57:26 +03003340 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3341 cpu);
3342 hardware_disable(NULL);
3343 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08003344 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003345 case CPU_UP_CANCELED_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003346 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3347 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003348 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003349 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02003350 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003351 case CPU_ONLINE_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003352 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
3353 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003354 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003355 break;
3356 }
3357 return NOTIFY_OK;
3358}
3359
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003360static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
3361 void *v)
3362{
3363 if (val == SYS_RESTART) {
3364 /*
3365 * Some (well, at least mine) BIOSes hang on reboot if
3366 * in vmx root mode.
3367 */
3368 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
3369 on_each_cpu(hardware_disable, NULL, 0, 1);
3370 }
3371 return NOTIFY_OK;
3372}
3373
3374static struct notifier_block kvm_reboot_notifier = {
3375 .notifier_call = kvm_reboot,
3376 .priority = 0,
3377};
3378
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04003379void kvm_io_bus_init(struct kvm_io_bus *bus)
3380{
3381 memset(bus, 0, sizeof(*bus));
3382}
3383
3384void kvm_io_bus_destroy(struct kvm_io_bus *bus)
3385{
3386 int i;
3387
3388 for (i = 0; i < bus->dev_count; i++) {
3389 struct kvm_io_device *pos = bus->devs[i];
3390
3391 kvm_iodevice_destructor(pos);
3392 }
3393}
3394
3395struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
3396{
3397 int i;
3398
3399 for (i = 0; i < bus->dev_count; i++) {
3400 struct kvm_io_device *pos = bus->devs[i];
3401
3402 if (pos->in_range(pos, addr))
3403 return pos;
3404 }
3405
3406 return NULL;
3407}
3408
3409void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
3410{
3411 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
3412
3413 bus->devs[bus->dev_count++] = dev;
3414}
3415
Avi Kivity774c47f2007-02-12 00:54:47 -08003416static struct notifier_block kvm_cpu_notifier = {
3417 .notifier_call = kvm_cpu_hotplug,
3418 .priority = 20, /* must be > scheduler priority */
3419};
3420
Avi Kivity1165f5f2007-04-19 17:27:43 +03003421static u64 stat_get(void *_offset)
3422{
3423 unsigned offset = (long)_offset;
3424 u64 total = 0;
3425 struct kvm *kvm;
3426 struct kvm_vcpu *vcpu;
3427 int i;
3428
3429 spin_lock(&kvm_lock);
3430 list_for_each_entry(kvm, &vm_list, vm_list)
3431 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003432 vcpu = kvm->vcpus[i];
3433 if (vcpu)
3434 total += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03003435 }
3436 spin_unlock(&kvm_lock);
3437 return total;
3438}
3439
Rusty Russell3dea7ca2007-08-01 10:12:22 +10003440DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n");
Avi Kivity1165f5f2007-04-19 17:27:43 +03003441
Avi Kivity6aa8b732006-12-10 02:21:36 -08003442static __init void kvm_init_debug(void)
3443{
3444 struct kvm_stats_debugfs_item *p;
3445
Al Viro8b6d44c2007-02-09 16:38:40 +00003446 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003447 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03003448 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
3449 (void *)(long)p->offset,
3450 &stat_fops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003451}
3452
3453static void kvm_exit_debug(void)
3454{
3455 struct kvm_stats_debugfs_item *p;
3456
3457 for (p = debugfs_entries; p->name; ++p)
3458 debugfs_remove(p->dentry);
3459 debugfs_remove(debugfs_dir);
3460}
3461
Avi Kivity59ae6c62007-02-12 00:54:48 -08003462static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3463{
Avi Kivity4267c412007-05-24 13:09:41 +03003464 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003465 return 0;
3466}
3467
3468static int kvm_resume(struct sys_device *dev)
3469{
Avi Kivity4267c412007-05-24 13:09:41 +03003470 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003471 return 0;
3472}
3473
3474static struct sysdev_class kvm_sysdev_class = {
3475 set_kset_name("kvm"),
3476 .suspend = kvm_suspend,
3477 .resume = kvm_resume,
3478};
3479
3480static struct sys_device kvm_sysdev = {
3481 .id = 0,
3482 .cls = &kvm_sysdev_class,
3483};
3484
Avi Kivity6aa8b732006-12-10 02:21:36 -08003485hpa_t bad_page_address;
3486
Avi Kivity15ad7142007-07-11 18:17:21 +03003487static inline
3488struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
3489{
3490 return container_of(pn, struct kvm_vcpu, preempt_notifier);
3491}
3492
3493static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
3494{
3495 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3496
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003497 kvm_x86_ops->vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003498}
3499
3500static void kvm_sched_out(struct preempt_notifier *pn,
3501 struct task_struct *next)
3502{
3503 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3504
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003505 kvm_x86_ops->vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003506}
3507
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003508int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10003509 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003510{
3511 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003512 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003513
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003514 if (kvm_x86_ops) {
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08003515 printk(KERN_ERR "kvm: already loaded the other module\n");
3516 return -EEXIST;
3517 }
3518
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003519 if (!ops->cpu_has_kvm_support()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003520 printk(KERN_ERR "kvm: no hardware support\n");
3521 return -EOPNOTSUPP;
3522 }
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003523 if (ops->disabled_by_bios()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003524 printk(KERN_ERR "kvm: disabled by bios\n");
3525 return -EOPNOTSUPP;
3526 }
3527
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003528 kvm_x86_ops = ops;
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003529
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003530 r = kvm_x86_ops->hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003531 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02003532 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003533
Yang, Sheng002c7f72007-07-31 14:23:01 +03003534 for_each_online_cpu(cpu) {
3535 smp_call_function_single(cpu,
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003536 kvm_x86_ops->check_processor_compatibility,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003537 &r, 0, 1);
3538 if (r < 0)
3539 goto out_free_0;
3540 }
3541
Avi Kivity1b6c0162007-05-24 13:03:52 +03003542 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003543 r = register_cpu_notifier(&kvm_cpu_notifier);
3544 if (r)
3545 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003546 register_reboot_notifier(&kvm_reboot_notifier);
3547
Avi Kivity59ae6c62007-02-12 00:54:48 -08003548 r = sysdev_class_register(&kvm_sysdev_class);
3549 if (r)
3550 goto out_free_2;
3551
3552 r = sysdev_register(&kvm_sysdev);
3553 if (r)
3554 goto out_free_3;
3555
Rusty Russellc16f8622007-07-30 21:12:19 +10003556 /* A kmem cache lets us meet the alignment requirements of fx_save. */
3557 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
3558 __alignof__(struct kvm_vcpu), 0, 0);
3559 if (!kvm_vcpu_cache) {
3560 r = -ENOMEM;
3561 goto out_free_4;
3562 }
3563
Avi Kivity6aa8b732006-12-10 02:21:36 -08003564 kvm_chardev_ops.owner = module;
3565
3566 r = misc_register(&kvm_dev);
3567 if (r) {
3568 printk (KERN_ERR "kvm: misc device register failed\n");
3569 goto out_free;
3570 }
3571
Avi Kivity15ad7142007-07-11 18:17:21 +03003572 kvm_preempt_ops.sched_in = kvm_sched_in;
3573 kvm_preempt_ops.sched_out = kvm_sched_out;
3574
Avi Kivity6aa8b732006-12-10 02:21:36 -08003575 return r;
3576
3577out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10003578 kmem_cache_destroy(kvm_vcpu_cache);
3579out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08003580 sysdev_unregister(&kvm_sysdev);
3581out_free_3:
3582 sysdev_class_unregister(&kvm_sysdev_class);
3583out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003584 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08003585 unregister_cpu_notifier(&kvm_cpu_notifier);
3586out_free_1:
Avi Kivity1b6c0162007-05-24 13:03:52 +03003587 on_each_cpu(hardware_disable, NULL, 0, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03003588out_free_0:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003589 kvm_x86_ops->hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02003590out:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003591 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003592 return r;
3593}
3594
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003595void kvm_exit_x86(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003596{
3597 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10003598 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003599 sysdev_unregister(&kvm_sysdev);
3600 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003601 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003602 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003603 on_each_cpu(hardware_disable, NULL, 0, 1);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003604 kvm_x86_ops->hardware_unsetup();
3605 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003606}
3607
3608static __init int kvm_init(void)
3609{
3610 static struct page *bad_page;
Avi Kivity37e29d92007-02-20 14:07:37 +02003611 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003612
Avi Kivityb5a33a72007-04-15 16:31:09 +03003613 r = kvm_mmu_module_init();
3614 if (r)
3615 goto out4;
3616
Avi Kivity6aa8b732006-12-10 02:21:36 -08003617 kvm_init_debug();
3618
Michael Riepebf591b22006-12-22 01:05:36 -08003619 kvm_init_msr_list();
3620
Avi Kivity6aa8b732006-12-10 02:21:36 -08003621 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
3622 r = -ENOMEM;
3623 goto out;
3624 }
3625
3626 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
3627 memset(__va(bad_page_address), 0, PAGE_SIZE);
3628
Avi Kivity58e690e2007-02-26 16:29:43 +02003629 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003630
3631out:
3632 kvm_exit_debug();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003633 kvm_mmu_module_exit();
3634out4:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003635 return r;
3636}
3637
3638static __exit void kvm_exit(void)
3639{
3640 kvm_exit_debug();
3641 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
Avi Kivityb5a33a72007-04-15 16:31:09 +03003642 kvm_mmu_module_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003643}
3644
3645module_init(kvm_init)
3646module_exit(kvm_exit)
3647
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003648EXPORT_SYMBOL_GPL(kvm_init_x86);
3649EXPORT_SYMBOL_GPL(kvm_exit_x86);