blob: cac328b8421c48d0b396119cc72873f670d05fd4 [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>
Anthony Liguori7aa81cc2007-09-17 14:57:50 -050042#include <linux/kvm_para.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080043
Avi Kivitye4956062007-06-28 14:15:57 -040044#include <asm/processor.h>
45#include <asm/msr.h>
46#include <asm/io.h>
47#include <asm/uaccess.h>
48#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080049
50MODULE_AUTHOR("Qumranet");
51MODULE_LICENSE("GPL");
52
Avi Kivity133de902007-02-12 00:54:44 -080053static DEFINE_SPINLOCK(kvm_lock);
54static LIST_HEAD(vm_list);
55
Avi Kivity1b6c0162007-05-24 13:03:52 +030056static cpumask_t cpus_hardware_enabled;
57
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +030058struct kvm_x86_ops *kvm_x86_ops;
Rusty Russellc16f8622007-07-30 21:12:19 +100059struct kmem_cache *kvm_vcpu_cache;
60EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030061
Avi Kivity15ad7142007-07-11 18:17:21 +030062static __read_mostly struct preempt_ops kvm_preempt_ops;
63
Avi Kivity1165f5f2007-04-19 17:27:43 +030064#define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
Avi Kivity6aa8b732006-12-10 02:21:36 -080065
66static struct kvm_stats_debugfs_item {
67 const char *name;
Avi Kivity1165f5f2007-04-19 17:27:43 +030068 int offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -080069 struct dentry *dentry;
70} debugfs_entries[] = {
Avi Kivity1165f5f2007-04-19 17:27:43 +030071 { "pf_fixed", STAT_OFFSET(pf_fixed) },
72 { "pf_guest", STAT_OFFSET(pf_guest) },
73 { "tlb_flush", STAT_OFFSET(tlb_flush) },
74 { "invlpg", STAT_OFFSET(invlpg) },
75 { "exits", STAT_OFFSET(exits) },
76 { "io_exits", STAT_OFFSET(io_exits) },
77 { "mmio_exits", STAT_OFFSET(mmio_exits) },
78 { "signal_exits", STAT_OFFSET(signal_exits) },
79 { "irq_window", STAT_OFFSET(irq_window_exits) },
80 { "halt_exits", STAT_OFFSET(halt_exits) },
Eddie Dongb6958ce2007-07-18 12:15:21 +030081 { "halt_wakeup", STAT_OFFSET(halt_wakeup) },
Avi Kivity1165f5f2007-04-19 17:27:43 +030082 { "request_irq", STAT_OFFSET(request_irq_exits) },
83 { "irq_exits", STAT_OFFSET(irq_exits) },
Avi Kivitye6adf282007-04-30 16:07:54 +030084 { "light_exits", STAT_OFFSET(light_exits) },
Eddie Dong2cc51562007-05-21 07:28:09 +030085 { "efer_reload", STAT_OFFSET(efer_reload) },
Avi Kivity1165f5f2007-04-19 17:27:43 +030086 { NULL }
Avi Kivity6aa8b732006-12-10 02:21:36 -080087};
88
89static struct dentry *debugfs_dir;
90
91#define MAX_IO_MSRS 256
92
Rusty Russell707d92fa2007-07-17 23:19:08 +100093#define CR0_RESERVED_BITS \
94 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
95 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
96 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
Rusty Russell66aee912007-07-17 23:34:16 +100097#define CR4_RESERVED_BITS \
98 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
99 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
100 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
101 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
102
Rusty Russell7075bc82007-07-17 23:37:17 +1000103#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800104#define EFER_RESERVED_BITS 0xfffffffffffff2fe
105
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800106#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800107// LDT or TSS descriptor in the GDT. 16 bytes.
108struct segment_descriptor_64 {
109 struct segment_descriptor s;
110 u32 base_higher;
111 u32 pad_zero;
112};
113
114#endif
115
Avi Kivitybccf2152007-02-21 18:04:26 +0200116static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
117 unsigned long arg);
118
Avi Kivity6aa8b732006-12-10 02:21:36 -0800119unsigned long segment_base(u16 selector)
120{
121 struct descriptor_table gdt;
122 struct segment_descriptor *d;
123 unsigned long table_base;
124 typedef unsigned long ul;
125 unsigned long v;
126
127 if (selector == 0)
128 return 0;
129
130 asm ("sgdt %0" : "=m"(gdt));
131 table_base = gdt.base;
132
133 if (selector & 4) { /* from ldt */
134 u16 ldt_selector;
135
136 asm ("sldt %0" : "=g"(ldt_selector));
137 table_base = segment_base(ldt_selector);
138 }
139 d = (struct segment_descriptor *)(table_base + (selector & ~7));
140 v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800141#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800142 if (d->system == 0
143 && (d->type == 2 || d->type == 9 || d->type == 11))
144 v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
145#endif
146 return v;
147}
148EXPORT_SYMBOL_GPL(segment_base);
149
James Morris5aacf0c2006-12-22 01:04:55 -0800150static inline int valid_vcpu(int n)
151{
152 return likely(n >= 0 && n < KVM_MAX_VCPUS);
153}
154
Avi Kivity7702fd12007-06-14 16:27:40 +0300155void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
156{
157 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
158 return;
159
160 vcpu->guest_fpu_loaded = 1;
Rusty Russellb114b082007-07-30 21:13:43 +1000161 fx_save(&vcpu->host_fx_image);
162 fx_restore(&vcpu->guest_fx_image);
Avi Kivity7702fd12007-06-14 16:27:40 +0300163}
164EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
165
166void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
167{
168 if (!vcpu->guest_fpu_loaded)
169 return;
170
171 vcpu->guest_fpu_loaded = 0;
Rusty Russellb114b082007-07-30 21:13:43 +1000172 fx_save(&vcpu->guest_fx_image);
173 fx_restore(&vcpu->host_fx_image);
Avi Kivity7702fd12007-06-14 16:27:40 +0300174}
175EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
176
Avi Kivity6aa8b732006-12-10 02:21:36 -0800177/*
178 * Switches to specified vcpu, until a matching vcpu_put()
179 */
Avi Kivitybccf2152007-02-21 18:04:26 +0200180static void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800181{
Avi Kivity15ad7142007-07-11 18:17:21 +0300182 int cpu;
183
Avi Kivitybccf2152007-02-21 18:04:26 +0200184 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300185 cpu = get_cpu();
186 preempt_notifier_register(&vcpu->preempt_notifier);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300187 kvm_x86_ops->vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300188 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200189}
190
Avi Kivity6aa8b732006-12-10 02:21:36 -0800191static void vcpu_put(struct kvm_vcpu *vcpu)
192{
Avi Kivity15ad7142007-07-11 18:17:21 +0300193 preempt_disable();
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300194 kvm_x86_ops->vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300195 preempt_notifier_unregister(&vcpu->preempt_notifier);
196 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800197 mutex_unlock(&vcpu->mutex);
198}
199
Avi Kivityd9e368d2007-06-07 19:18:30 +0300200static void ack_flush(void *_completed)
201{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300202}
203
204void kvm_flush_remote_tlbs(struct kvm *kvm)
205{
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200206 int i, cpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300207 cpumask_t cpus;
208 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300209
Avi Kivityd9e368d2007-06-07 19:18:30 +0300210 cpus_clear(cpus);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000211 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
212 vcpu = kvm->vcpus[i];
213 if (!vcpu)
214 continue;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300215 if (test_and_set_bit(KVM_TLB_FLUSH, &vcpu->requests))
216 continue;
217 cpu = vcpu->cpu;
218 if (cpu != -1 && cpu != raw_smp_processor_id())
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200219 cpu_set(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300220 }
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200221 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300222}
223
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000224int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
225{
226 struct page *page;
227 int r;
228
229 mutex_init(&vcpu->mutex);
230 vcpu->cpu = -1;
231 vcpu->mmu.root_hpa = INVALID_PAGE;
232 vcpu->kvm = kvm;
233 vcpu->vcpu_id = id;
He, Qingc5ec1532007-09-03 17:07:41 +0300234 if (!irqchip_in_kernel(kvm) || id == 0)
235 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
236 else
237 vcpu->mp_state = VCPU_MP_STATE_UNINITIALIZED;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300238 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000239
240 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
241 if (!page) {
242 r = -ENOMEM;
243 goto fail;
244 }
245 vcpu->run = page_address(page);
246
247 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
248 if (!page) {
249 r = -ENOMEM;
250 goto fail_free_run;
251 }
252 vcpu->pio_data = page_address(page);
253
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000254 r = kvm_mmu_create(vcpu);
255 if (r < 0)
256 goto fail_free_pio_data;
257
258 return 0;
259
260fail_free_pio_data:
261 free_page((unsigned long)vcpu->pio_data);
262fail_free_run:
263 free_page((unsigned long)vcpu->run);
264fail:
265 return -ENOMEM;
266}
267EXPORT_SYMBOL_GPL(kvm_vcpu_init);
268
269void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
270{
271 kvm_mmu_destroy(vcpu);
Eddie Dong1b9778d2007-09-03 16:56:58 +0300272 if (vcpu->apic)
273 hrtimer_cancel(&vcpu->apic->timer.dev);
Eddie Dong97222cc2007-09-12 10:58:04 +0300274 kvm_free_apic(vcpu->apic);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000275 free_page((unsigned long)vcpu->pio_data);
276 free_page((unsigned long)vcpu->run);
277}
278EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
279
Avi Kivityf17abe92007-02-21 19:28:04 +0200280static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800281{
282 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800283
284 if (!kvm)
Avi Kivityf17abe92007-02-21 19:28:04 +0200285 return ERR_PTR(-ENOMEM);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800286
Eddie Dong74906342007-06-19 18:05:03 +0300287 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800288 mutex_init(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800289 INIT_LIST_HEAD(&kvm->active_mmu_pages);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400290 kvm_io_bus_init(&kvm->mmio_bus);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000291 spin_lock(&kvm_lock);
292 list_add(&kvm->vm_list, &vm_list);
293 spin_unlock(&kvm_lock);
Avi Kivityf17abe92007-02-21 19:28:04 +0200294 return kvm;
295}
296
Avi Kivity6aa8b732006-12-10 02:21:36 -0800297/*
298 * Free any memory in @free but not in @dont.
299 */
300static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
301 struct kvm_memory_slot *dont)
302{
303 int i;
304
305 if (!dont || free->phys_mem != dont->phys_mem)
306 if (free->phys_mem) {
307 for (i = 0; i < free->npages; ++i)
Avi Kivity55a54f72006-12-29 16:49:58 -0800308 if (free->phys_mem[i])
309 __free_page(free->phys_mem[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800310 vfree(free->phys_mem);
311 }
Izik Eidus290fc382007-09-27 14:11:22 +0200312 if (!dont || free->rmap != dont->rmap)
313 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800314
315 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
316 vfree(free->dirty_bitmap);
317
Al Viro8b6d44c2007-02-09 16:38:40 +0000318 free->phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800319 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000320 free->dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800321}
322
323static void kvm_free_physmem(struct kvm *kvm)
324{
325 int i;
326
327 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000328 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800329}
330
Avi Kivity039576c2007-03-20 12:46:50 +0200331static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
332{
333 int i;
334
Rusty Russell3077c4512007-07-30 16:41:57 +1000335 for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i)
Avi Kivity039576c2007-03-20 12:46:50 +0200336 if (vcpu->pio.guest_pages[i]) {
337 __free_page(vcpu->pio.guest_pages[i]);
338 vcpu->pio.guest_pages[i] = NULL;
339 }
340}
341
Avi Kivity7b53aa52007-06-05 12:17:03 +0300342static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
343{
Avi Kivity7b53aa52007-06-05 12:17:03 +0300344 vcpu_load(vcpu);
345 kvm_mmu_unload(vcpu);
346 vcpu_put(vcpu);
347}
348
Avi Kivity6aa8b732006-12-10 02:21:36 -0800349static void kvm_free_vcpus(struct kvm *kvm)
350{
351 unsigned int i;
352
Avi Kivity7b53aa52007-06-05 12:17:03 +0300353 /*
354 * Unpin any mmu pages first.
355 */
356 for (i = 0; i < KVM_MAX_VCPUS; ++i)
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000357 if (kvm->vcpus[i])
358 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
359 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
360 if (kvm->vcpus[i]) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300361 kvm_x86_ops->vcpu_free(kvm->vcpus[i]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000362 kvm->vcpus[i] = NULL;
363 }
364 }
365
Avi Kivity6aa8b732006-12-10 02:21:36 -0800366}
367
Avi Kivityf17abe92007-02-21 19:28:04 +0200368static void kvm_destroy_vm(struct kvm *kvm)
369{
Avi Kivity133de902007-02-12 00:54:44 -0800370 spin_lock(&kvm_lock);
371 list_del(&kvm->vm_list);
372 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300373 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400374 kvm_io_bus_destroy(&kvm->mmio_bus);
Eddie Dong85f455f2007-07-06 12:20:49 +0300375 kfree(kvm->vpic);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300376 kfree(kvm->vioapic);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800377 kvm_free_vcpus(kvm);
378 kvm_free_physmem(kvm);
379 kfree(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200380}
381
382static int kvm_vm_release(struct inode *inode, struct file *filp)
383{
384 struct kvm *kvm = filp->private_data;
385
386 kvm_destroy_vm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800387 return 0;
388}
389
390static void inject_gp(struct kvm_vcpu *vcpu)
391{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300392 kvm_x86_ops->inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800393}
394
Avi Kivity1342d352007-01-05 16:36:39 -0800395/*
396 * Load the pae pdptrs. Return true is they are all valid.
397 */
398static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800399{
400 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
Avi Kivity1342d352007-01-05 16:36:39 -0800401 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800402 int i;
Avi Kivity1342d352007-01-05 16:36:39 -0800403 int ret;
Rusty Russellc820c2a2007-07-25 13:29:51 +1000404 u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800405
Shaohua Li11ec2802007-07-23 14:51:37 +0800406 mutex_lock(&vcpu->kvm->lock);
Izik Eidus195aefd2007-10-01 22:14:18 +0200407 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
408 offset * sizeof(u64), sizeof(pdpte));
409 if (ret < 0) {
Rusty Russellc820c2a2007-07-25 13:29:51 +1000410 ret = 0;
411 goto out;
412 }
Rusty Russellc820c2a2007-07-25 13:29:51 +1000413 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
414 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
Avi Kivity1342d352007-01-05 16:36:39 -0800415 ret = 0;
416 goto out;
417 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800418 }
Rusty Russellc820c2a2007-07-25 13:29:51 +1000419 ret = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800420
Rusty Russellc820c2a2007-07-25 13:29:51 +1000421 memcpy(vcpu->pdptrs, pdpte, sizeof(vcpu->pdptrs));
Avi Kivity1342d352007-01-05 16:36:39 -0800422out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800423 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424
Avi Kivity1342d352007-01-05 16:36:39 -0800425 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800426}
427
428void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
429{
Rusty Russell707d92fa2007-07-17 23:19:08 +1000430 if (cr0 & CR0_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800431 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
432 cr0, vcpu->cr0);
433 inject_gp(vcpu);
434 return;
435 }
436
Rusty Russell707d92fa2007-07-17 23:19:08 +1000437 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800438 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
439 inject_gp(vcpu);
440 return;
441 }
442
Rusty Russell707d92fa2007-07-17 23:19:08 +1000443 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800444 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
445 "and a clear PE flag\n");
446 inject_gp(vcpu);
447 return;
448 }
449
Rusty Russell707d92fa2007-07-17 23:19:08 +1000450 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800451#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800452 if ((vcpu->shadow_efer & EFER_LME)) {
453 int cs_db, cs_l;
454
455 if (!is_pae(vcpu)) {
456 printk(KERN_DEBUG "set_cr0: #GP, start paging "
457 "in long mode while PAE is disabled\n");
458 inject_gp(vcpu);
459 return;
460 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300461 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800462 if (cs_l) {
463 printk(KERN_DEBUG "set_cr0: #GP, start paging "
464 "in long mode while CS.L == 1\n");
465 inject_gp(vcpu);
466 return;
467
468 }
469 } else
470#endif
Avi Kivity1342d352007-01-05 16:36:39 -0800471 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800472 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
473 "reserved bits\n");
474 inject_gp(vcpu);
475 return;
476 }
477
478 }
479
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300480 kvm_x86_ops->set_cr0(vcpu, cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800481 vcpu->cr0 = cr0;
482
Shaohua Li11ec2802007-07-23 14:51:37 +0800483 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800484 kvm_mmu_reset_context(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800485 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800486 return;
487}
488EXPORT_SYMBOL_GPL(set_cr0);
489
490void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
491{
492 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
493}
494EXPORT_SYMBOL_GPL(lmsw);
495
496void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
497{
Rusty Russell66aee912007-07-17 23:34:16 +1000498 if (cr4 & CR4_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800499 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
500 inject_gp(vcpu);
501 return;
502 }
503
Avi Kivitya9058ec2006-12-29 16:49:37 -0800504 if (is_long_mode(vcpu)) {
Rusty Russell66aee912007-07-17 23:34:16 +1000505 if (!(cr4 & X86_CR4_PAE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800506 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
507 "in long mode\n");
508 inject_gp(vcpu);
509 return;
510 }
Rusty Russell66aee912007-07-17 23:34:16 +1000511 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
Avi Kivity1342d352007-01-05 16:36:39 -0800512 && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800513 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
514 inject_gp(vcpu);
Rusty Russell310bc762007-07-23 17:11:02 +1000515 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800516 }
517
Rusty Russell66aee912007-07-17 23:34:16 +1000518 if (cr4 & X86_CR4_VMXE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800519 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
520 inject_gp(vcpu);
521 return;
522 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300523 kvm_x86_ops->set_cr4(vcpu, cr4);
Rusty Russell81f50e32007-09-06 01:20:38 +1000524 vcpu->cr4 = cr4;
Shaohua Li11ec2802007-07-23 14:51:37 +0800525 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800526 kvm_mmu_reset_context(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800527 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800528}
529EXPORT_SYMBOL_GPL(set_cr4);
530
531void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
532{
Avi Kivitya9058ec2006-12-29 16:49:37 -0800533 if (is_long_mode(vcpu)) {
Rusty Russellf802a302007-07-17 23:32:55 +1000534 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800535 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
536 inject_gp(vcpu);
537 return;
538 }
539 } else {
Rusty Russellf802a302007-07-17 23:32:55 +1000540 if (is_pae(vcpu)) {
541 if (cr3 & CR3_PAE_RESERVED_BITS) {
542 printk(KERN_DEBUG
543 "set_cr3: #GP, reserved bits\n");
544 inject_gp(vcpu);
545 return;
546 }
547 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
548 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
549 "reserved bits\n");
550 inject_gp(vcpu);
551 return;
552 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800553 }
Ryan Harper21764862007-09-18 14:05:16 -0500554 /*
555 * We don't check reserved bits in nonpae mode, because
556 * this isn't enforced, and VMware depends on this.
557 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800558 }
559
Shaohua Li11ec2802007-07-23 14:51:37 +0800560 mutex_lock(&vcpu->kvm->lock);
Ingo Molnard21225e2007-01-05 16:36:59 -0800561 /*
562 * Does the new cr3 value map to physical memory? (Note, we
563 * catch an invalid cr3 even in real-mode, because it would
564 * cause trouble later on when we turn on paging anyway.)
565 *
566 * A real CPU would silently accept an invalid cr3 and would
567 * attempt to use it - with largely undefined (and often hard
568 * to debug) behavior on the guest side.
569 */
570 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
571 inject_gp(vcpu);
Rusty Russellfb764412007-07-31 20:45:03 +1000572 else {
573 vcpu->cr3 = cr3;
Ingo Molnard21225e2007-01-05 16:36:59 -0800574 vcpu->mmu.new_cr3(vcpu);
Rusty Russellfb764412007-07-31 20:45:03 +1000575 }
Shaohua Li11ec2802007-07-23 14:51:37 +0800576 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800577}
578EXPORT_SYMBOL_GPL(set_cr3);
579
580void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
581{
Rusty Russell7075bc82007-07-17 23:37:17 +1000582 if (cr8 & CR8_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800583 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
584 inject_gp(vcpu);
585 return;
586 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300587 if (irqchip_in_kernel(vcpu->kvm))
588 kvm_lapic_set_tpr(vcpu, cr8);
589 else
590 vcpu->cr8 = cr8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800591}
592EXPORT_SYMBOL_GPL(set_cr8);
593
Eddie Dong7017fc32007-07-18 11:34:57 +0300594unsigned long get_cr8(struct kvm_vcpu *vcpu)
595{
Eddie Dong97222cc2007-09-12 10:58:04 +0300596 if (irqchip_in_kernel(vcpu->kvm))
597 return kvm_lapic_get_cr8(vcpu);
598 else
599 return vcpu->cr8;
Eddie Dong7017fc32007-07-18 11:34:57 +0300600}
601EXPORT_SYMBOL_GPL(get_cr8);
602
603u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
604{
Eddie Dong97222cc2007-09-12 10:58:04 +0300605 if (irqchip_in_kernel(vcpu->kvm))
606 return vcpu->apic_base;
607 else
608 return vcpu->apic_base;
Eddie Dong7017fc32007-07-18 11:34:57 +0300609}
610EXPORT_SYMBOL_GPL(kvm_get_apic_base);
611
612void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
613{
Eddie Dong97222cc2007-09-12 10:58:04 +0300614 /* TODO: reserve bits check */
615 if (irqchip_in_kernel(vcpu->kvm))
616 kvm_lapic_set_base(vcpu, data);
617 else
618 vcpu->apic_base = data;
Eddie Dong7017fc32007-07-18 11:34:57 +0300619}
620EXPORT_SYMBOL_GPL(kvm_set_apic_base);
621
Avi Kivity6aa8b732006-12-10 02:21:36 -0800622void fx_init(struct kvm_vcpu *vcpu)
623{
Rusty Russellb114b082007-07-30 21:13:43 +1000624 unsigned after_mxcsr_mask;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800625
Rusty Russell9bd01502007-07-30 16:29:56 +1000626 /* Initialize guest FPU by resetting ours and saving into guest's */
627 preempt_disable();
Rusty Russellb114b082007-07-30 21:13:43 +1000628 fx_save(&vcpu->host_fx_image);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800629 fpu_init();
Rusty Russellb114b082007-07-30 21:13:43 +1000630 fx_save(&vcpu->guest_fx_image);
631 fx_restore(&vcpu->host_fx_image);
Rusty Russell9bd01502007-07-30 16:29:56 +1000632 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800633
Amit Shah380102c2007-08-25 11:35:52 +0300634 vcpu->cr0 |= X86_CR0_ET;
Rusty Russellb114b082007-07-30 21:13:43 +1000635 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
636 vcpu->guest_fx_image.mxcsr = 0x1f80;
637 memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask,
638 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800639}
640EXPORT_SYMBOL_GPL(fx_init);
641
642/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800643 * Allocate some memory and give it an address in the guest physical address
644 * space.
645 *
646 * Discontiguous memory is allowed, mostly for framebuffers.
647 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200648static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
649 struct kvm_memory_region *mem)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800650{
651 int r;
652 gfn_t base_gfn;
653 unsigned long npages;
654 unsigned long i;
655 struct kvm_memory_slot *memslot;
656 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800657
658 r = -EINVAL;
659 /* General sanity checks */
660 if (mem->memory_size & (PAGE_SIZE - 1))
661 goto out;
662 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
663 goto out;
664 if (mem->slot >= KVM_MEMORY_SLOTS)
665 goto out;
666 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
667 goto out;
668
669 memslot = &kvm->memslots[mem->slot];
670 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
671 npages = mem->memory_size >> PAGE_SHIFT;
672
673 if (!npages)
674 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
675
Shaohua Li11ec2802007-07-23 14:51:37 +0800676 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800677
Avi Kivity6aa8b732006-12-10 02:21:36 -0800678 new = old = *memslot;
679
680 new.base_gfn = base_gfn;
681 new.npages = npages;
682 new.flags = mem->flags;
683
684 /* Disallow changing a memory slot's size. */
685 r = -EINVAL;
686 if (npages && old.npages && npages != old.npages)
687 goto out_unlock;
688
689 /* Check for overlaps */
690 r = -EEXIST;
691 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
692 struct kvm_memory_slot *s = &kvm->memslots[i];
693
694 if (s == memslot)
695 continue;
696 if (!((base_gfn + npages <= s->base_gfn) ||
697 (base_gfn >= s->base_gfn + s->npages)))
698 goto out_unlock;
699 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800700
701 /* Deallocate if slot is being removed */
702 if (!npages)
Al Viro8b6d44c2007-02-09 16:38:40 +0000703 new.phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800704
705 /* Free page dirty bitmap if unneeded */
706 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000707 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800708
709 r = -ENOMEM;
710
711 /* Allocate if a slot is being created */
712 if (npages && !new.phys_mem) {
713 new.phys_mem = vmalloc(npages * sizeof(struct page *));
714
715 if (!new.phys_mem)
Laurent Vivier0d8d2bd2007-08-30 14:56:21 +0200716 goto out_unlock;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800717
Izik Eidus290fc382007-09-27 14:11:22 +0200718 new.rmap = vmalloc(npages * sizeof(struct page*));
719
720 if (!new.rmap)
721 goto out_unlock;
722
Avi Kivity6aa8b732006-12-10 02:21:36 -0800723 memset(new.phys_mem, 0, npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200724 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800725 for (i = 0; i < npages; ++i) {
726 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
727 | __GFP_ZERO);
728 if (!new.phys_mem[i])
Laurent Vivier0d8d2bd2007-08-30 14:56:21 +0200729 goto out_unlock;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800730 }
731 }
732
733 /* Allocate page dirty bitmap if needed */
734 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
735 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
736
737 new.dirty_bitmap = vmalloc(dirty_bytes);
738 if (!new.dirty_bitmap)
Laurent Vivier0d8d2bd2007-08-30 14:56:21 +0200739 goto out_unlock;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800740 memset(new.dirty_bitmap, 0, dirty_bytes);
741 }
742
Avi Kivity6aa8b732006-12-10 02:21:36 -0800743 if (mem->slot >= kvm->nmemslots)
744 kvm->nmemslots = mem->slot + 1;
745
746 *memslot = new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800747
Avi Kivity90cb0522007-07-17 13:04:56 +0300748 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
749 kvm_flush_remote_tlbs(kvm);
750
Shaohua Li11ec2802007-07-23 14:51:37 +0800751 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800752
Avi Kivity6aa8b732006-12-10 02:21:36 -0800753 kvm_free_physmem_slot(&old, &new);
754 return 0;
755
756out_unlock:
Shaohua Li11ec2802007-07-23 14:51:37 +0800757 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800758 kvm_free_physmem_slot(&new, &old);
759out:
760 return r;
761}
762
763/*
764 * Get (and clear) the dirty memory log for a memory slot.
765 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200766static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
767 struct kvm_dirty_log *log)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800768{
769 struct kvm_memory_slot *memslot;
770 int r, i;
771 int n;
772 unsigned long any = 0;
773
Shaohua Li11ec2802007-07-23 14:51:37 +0800774 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800775
Avi Kivity6aa8b732006-12-10 02:21:36 -0800776 r = -EINVAL;
777 if (log->slot >= KVM_MEMORY_SLOTS)
778 goto out;
779
780 memslot = &kvm->memslots[log->slot];
781 r = -ENOENT;
782 if (!memslot->dirty_bitmap)
783 goto out;
784
Uri Lublincd1a4a92007-02-22 16:43:09 +0200785 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800786
Uri Lublincd1a4a92007-02-22 16:43:09 +0200787 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800788 any = memslot->dirty_bitmap[i];
789
790 r = -EFAULT;
791 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
792 goto out;
793
Rusty Russell39214912007-07-31 19:57:47 +1000794 /* If nothing is dirty, don't bother messing with page tables. */
795 if (any) {
Rusty Russell39214912007-07-31 19:57:47 +1000796 kvm_mmu_slot_remove_write_access(kvm, log->slot);
797 kvm_flush_remote_tlbs(kvm);
798 memset(memslot->dirty_bitmap, 0, n);
Rusty Russell39214912007-07-31 19:57:47 +1000799 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800800
801 r = 0;
802
803out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800804 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800805 return r;
806}
807
Avi Kivitye8207542007-03-30 16:54:30 +0300808/*
809 * Set a new alias region. Aliases map a portion of physical memory into
810 * another portion. This is useful for memory windows, for example the PC
811 * VGA region.
812 */
813static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
814 struct kvm_memory_alias *alias)
815{
816 int r, n;
817 struct kvm_mem_alias *p;
818
819 r = -EINVAL;
820 /* General sanity checks */
821 if (alias->memory_size & (PAGE_SIZE - 1))
822 goto out;
823 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
824 goto out;
825 if (alias->slot >= KVM_ALIAS_SLOTS)
826 goto out;
827 if (alias->guest_phys_addr + alias->memory_size
828 < alias->guest_phys_addr)
829 goto out;
830 if (alias->target_phys_addr + alias->memory_size
831 < alias->target_phys_addr)
832 goto out;
833
Shaohua Li11ec2802007-07-23 14:51:37 +0800834 mutex_lock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300835
836 p = &kvm->aliases[alias->slot];
837 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
838 p->npages = alias->memory_size >> PAGE_SHIFT;
839 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
840
841 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
842 if (kvm->aliases[n - 1].npages)
843 break;
844 kvm->naliases = n;
845
Avi Kivity90cb0522007-07-17 13:04:56 +0300846 kvm_mmu_zap_all(kvm);
Avi Kivitye8207542007-03-30 16:54:30 +0300847
Shaohua Li11ec2802007-07-23 14:51:37 +0800848 mutex_unlock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300849
850 return 0;
851
852out:
853 return r;
854}
855
He, Qing6ceb9d72007-07-26 11:05:18 +0300856static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
857{
858 int r;
859
860 r = 0;
861 switch (chip->chip_id) {
862 case KVM_IRQCHIP_PIC_MASTER:
863 memcpy (&chip->chip.pic,
864 &pic_irqchip(kvm)->pics[0],
865 sizeof(struct kvm_pic_state));
866 break;
867 case KVM_IRQCHIP_PIC_SLAVE:
868 memcpy (&chip->chip.pic,
869 &pic_irqchip(kvm)->pics[1],
870 sizeof(struct kvm_pic_state));
871 break;
He, Qing6bf9e962007-08-05 10:49:16 +0300872 case KVM_IRQCHIP_IOAPIC:
873 memcpy (&chip->chip.ioapic,
874 ioapic_irqchip(kvm),
875 sizeof(struct kvm_ioapic_state));
876 break;
He, Qing6ceb9d72007-07-26 11:05:18 +0300877 default:
878 r = -EINVAL;
879 break;
880 }
881 return r;
882}
883
884static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
885{
886 int r;
887
888 r = 0;
889 switch (chip->chip_id) {
890 case KVM_IRQCHIP_PIC_MASTER:
891 memcpy (&pic_irqchip(kvm)->pics[0],
892 &chip->chip.pic,
893 sizeof(struct kvm_pic_state));
894 break;
895 case KVM_IRQCHIP_PIC_SLAVE:
896 memcpy (&pic_irqchip(kvm)->pics[1],
897 &chip->chip.pic,
898 sizeof(struct kvm_pic_state));
899 break;
He, Qing6bf9e962007-08-05 10:49:16 +0300900 case KVM_IRQCHIP_IOAPIC:
901 memcpy (ioapic_irqchip(kvm),
902 &chip->chip.ioapic,
903 sizeof(struct kvm_ioapic_state));
904 break;
He, Qing6ceb9d72007-07-26 11:05:18 +0300905 default:
906 r = -EINVAL;
907 break;
908 }
909 kvm_pic_update_irq(pic_irqchip(kvm));
910 return r;
911}
912
Izik Eidus290fc382007-09-27 14:11:22 +0200913gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
Avi Kivitye8207542007-03-30 16:54:30 +0300914{
915 int i;
916 struct kvm_mem_alias *alias;
917
918 for (i = 0; i < kvm->naliases; ++i) {
919 alias = &kvm->aliases[i];
920 if (gfn >= alias->base_gfn
921 && gfn < alias->base_gfn + alias->npages)
922 return alias->target_gfn + gfn - alias->base_gfn;
923 }
924 return gfn;
925}
926
927static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928{
929 int i;
930
931 for (i = 0; i < kvm->nmemslots; ++i) {
932 struct kvm_memory_slot *memslot = &kvm->memslots[i];
933
934 if (gfn >= memslot->base_gfn
935 && gfn < memslot->base_gfn + memslot->npages)
936 return memslot;
937 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000938 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800939}
Avi Kivitye8207542007-03-30 16:54:30 +0300940
941struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
942{
943 gfn = unalias_gfn(kvm, gfn);
944 return __gfn_to_memslot(kvm, gfn);
945}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800946
Avi Kivity954bbbc2007-03-30 14:02:32 +0300947struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
948{
949 struct kvm_memory_slot *slot;
950
Avi Kivitye8207542007-03-30 16:54:30 +0300951 gfn = unalias_gfn(kvm, gfn);
952 slot = __gfn_to_memslot(kvm, gfn);
Avi Kivity954bbbc2007-03-30 14:02:32 +0300953 if (!slot)
954 return NULL;
955 return slot->phys_mem[gfn - slot->base_gfn];
956}
957EXPORT_SYMBOL_GPL(gfn_to_page);
958
Izik Eidus195aefd2007-10-01 22:14:18 +0200959static int next_segment(unsigned long len, int offset)
960{
961 if (len > PAGE_SIZE - offset)
962 return PAGE_SIZE - offset;
963 else
964 return len;
965}
966
967int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
968 int len)
969{
970 void *page_virt;
971 struct page *page;
972
973 page = gfn_to_page(kvm, gfn);
974 if (!page)
975 return -EFAULT;
976 page_virt = kmap_atomic(page, KM_USER0);
977
978 memcpy(data, page_virt + offset, len);
979
980 kunmap_atomic(page_virt, KM_USER0);
981 return 0;
982}
983EXPORT_SYMBOL_GPL(kvm_read_guest_page);
984
985int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
986{
987 gfn_t gfn = gpa >> PAGE_SHIFT;
988 int seg;
989 int offset = offset_in_page(gpa);
990 int ret;
991
992 while ((seg = next_segment(len, offset)) != 0) {
993 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
994 if (ret < 0)
995 return ret;
996 offset = 0;
997 len -= seg;
998 data += seg;
999 ++gfn;
1000 }
1001 return 0;
1002}
1003EXPORT_SYMBOL_GPL(kvm_read_guest);
1004
1005int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1006 int offset, int len)
1007{
1008 void *page_virt;
1009 struct page *page;
1010
1011 page = gfn_to_page(kvm, gfn);
1012 if (!page)
1013 return -EFAULT;
1014 page_virt = kmap_atomic(page, KM_USER0);
1015
1016 memcpy(page_virt + offset, data, len);
1017
1018 kunmap_atomic(page_virt, KM_USER0);
1019 mark_page_dirty(kvm, gfn);
1020 return 0;
1021}
1022EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1023
1024int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1025 unsigned long len)
1026{
1027 gfn_t gfn = gpa >> PAGE_SHIFT;
1028 int seg;
1029 int offset = offset_in_page(gpa);
1030 int ret;
1031
1032 while ((seg = next_segment(len, offset)) != 0) {
1033 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1034 if (ret < 0)
1035 return ret;
1036 offset = 0;
1037 len -= seg;
1038 data += seg;
1039 ++gfn;
1040 }
1041 return 0;
1042}
1043
1044int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1045{
1046 void *page_virt;
1047 struct page *page;
1048
1049 page = gfn_to_page(kvm, gfn);
1050 if (!page)
1051 return -EFAULT;
1052 page_virt = kmap_atomic(page, KM_USER0);
1053
1054 memset(page_virt + offset, 0, len);
1055
1056 kunmap_atomic(page_virt, KM_USER0);
1057 return 0;
1058}
1059EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1060
1061int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1062{
1063 gfn_t gfn = gpa >> PAGE_SHIFT;
1064 int seg;
1065 int offset = offset_in_page(gpa);
1066 int ret;
1067
1068 while ((seg = next_segment(len, offset)) != 0) {
1069 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1070 if (ret < 0)
1071 return ret;
1072 offset = 0;
1073 len -= seg;
1074 ++gfn;
1075 }
1076 return 0;
1077}
1078EXPORT_SYMBOL_GPL(kvm_clear_guest);
1079
Rusty Russell7e9d6192007-07-31 20:41:14 +10001080/* WARNING: Does not work on aliased pages. */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001081void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1082{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001083 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001084
Rusty Russell7e9d6192007-07-31 20:41:14 +10001085 memslot = __gfn_to_memslot(kvm, gfn);
1086 if (memslot && memslot->dirty_bitmap) {
1087 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001088
Rusty Russell7e9d6192007-07-31 20:41:14 +10001089 /* avoid RMW */
1090 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1091 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001092 }
1093}
1094
Laurent Viviere7d5d762007-07-30 13:41:19 +03001095int emulator_read_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001096 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001097 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001098 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001099{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001100 void *data = val;
1101
1102 while (bytes) {
1103 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1104 unsigned offset = addr & (PAGE_SIZE-1);
1105 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
Izik Eidus195aefd2007-10-01 22:14:18 +02001106 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001107
1108 if (gpa == UNMAPPED_GVA)
1109 return X86EMUL_PROPAGATE_FAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001110 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
1111 if (ret < 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001113
1114 bytes -= tocopy;
1115 data += tocopy;
1116 addr += tocopy;
1117 }
1118
1119 return X86EMUL_CONTINUE;
1120}
Laurent Viviere7d5d762007-07-30 13:41:19 +03001121EXPORT_SYMBOL_GPL(emulator_read_std);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001122
1123static int emulator_write_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001124 const void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001125 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001126 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001127{
Rusty Russellf0242472007-08-01 10:48:02 +10001128 pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001129 return X86EMUL_UNHANDLEABLE;
1130}
1131
Eddie Dong97222cc2007-09-12 10:58:04 +03001132/*
1133 * Only apic need an MMIO device hook, so shortcut now..
1134 */
1135static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
1136 gpa_t addr)
1137{
1138 struct kvm_io_device *dev;
1139
1140 if (vcpu->apic) {
1141 dev = &vcpu->apic->dev;
1142 if (dev->in_range(dev, addr))
1143 return dev;
1144 }
1145 return NULL;
1146}
1147
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001148static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1149 gpa_t addr)
1150{
Eddie Dong97222cc2007-09-12 10:58:04 +03001151 struct kvm_io_device *dev;
1152
1153 dev = vcpu_find_pervcpu_dev(vcpu, addr);
1154 if (dev == NULL)
1155 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1156 return dev;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001157}
1158
Eddie Dong74906342007-06-19 18:05:03 +03001159static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
1160 gpa_t addr)
1161{
1162 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
1163}
1164
Avi Kivity6aa8b732006-12-10 02:21:36 -08001165static int emulator_read_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001166 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001167 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001168 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001169{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001170 struct kvm_io_device *mmio_dev;
1171 gpa_t gpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001172
1173 if (vcpu->mmio_read_completed) {
1174 memcpy(val, vcpu->mmio_data, bytes);
1175 vcpu->mmio_read_completed = 0;
1176 return X86EMUL_CONTINUE;
Laurent Viviercebff022007-07-30 13:35:24 +03001177 } else if (emulator_read_std(addr, val, bytes, vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001178 == X86EMUL_CONTINUE)
1179 return X86EMUL_CONTINUE;
Avi Kivityd27d4ac2007-02-19 14:37:46 +02001180
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001181 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1182 if (gpa == UNMAPPED_GVA)
1183 return X86EMUL_PROPAGATE_FAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001184
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001185 /*
1186 * Is this MMIO handled locally?
1187 */
1188 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1189 if (mmio_dev) {
1190 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1191 return X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001192 }
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001193
1194 vcpu->mmio_needed = 1;
1195 vcpu->mmio_phys_addr = gpa;
1196 vcpu->mmio_size = bytes;
1197 vcpu->mmio_is_write = 0;
1198
1199 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001200}
1201
Avi Kivityda4a00f2007-01-05 16:36:44 -08001202static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity4c690a12007-04-22 15:28:19 +03001203 const void *val, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001204{
Izik Eidus195aefd2007-10-01 22:14:18 +02001205 int ret;
Avi Kivityda4a00f2007-01-05 16:36:44 -08001206
Izik Eidus195aefd2007-10-01 22:14:18 +02001207 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
1208 if (ret < 0)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001209 return 0;
Shaohua Life5518812007-07-23 14:51:39 +08001210 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001211 return 1;
1212}
1213
Avi Kivityb0fcd902007-07-22 18:48:54 +03001214static int emulator_write_emulated_onepage(unsigned long addr,
1215 const void *val,
1216 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001217 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001218{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001219 struct kvm_io_device *mmio_dev;
1220 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001221
Avi Kivityc9047f52007-04-17 10:53:22 +03001222 if (gpa == UNMAPPED_GVA) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001223 kvm_x86_ops->inject_page_fault(vcpu, addr, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001224 return X86EMUL_PROPAGATE_FAULT;
Avi Kivityc9047f52007-04-17 10:53:22 +03001225 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001226
Avi Kivityda4a00f2007-01-05 16:36:44 -08001227 if (emulator_write_phys(vcpu, gpa, val, bytes))
1228 return X86EMUL_CONTINUE;
1229
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001230 /*
1231 * Is this MMIO handled locally?
1232 */
1233 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1234 if (mmio_dev) {
1235 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1236 return X86EMUL_CONTINUE;
1237 }
1238
Avi Kivity6aa8b732006-12-10 02:21:36 -08001239 vcpu->mmio_needed = 1;
1240 vcpu->mmio_phys_addr = gpa;
1241 vcpu->mmio_size = bytes;
1242 vcpu->mmio_is_write = 1;
Avi Kivity4c690a12007-04-22 15:28:19 +03001243 memcpy(vcpu->mmio_data, val, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001244
1245 return X86EMUL_CONTINUE;
1246}
1247
Laurent Viviere7d5d762007-07-30 13:41:19 +03001248int emulator_write_emulated(unsigned long addr,
Avi Kivityb0fcd902007-07-22 18:48:54 +03001249 const void *val,
1250 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001251 struct kvm_vcpu *vcpu)
Avi Kivityb0fcd902007-07-22 18:48:54 +03001252{
1253 /* Crossing a page boundary? */
1254 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1255 int rc, now;
1256
1257 now = -addr & ~PAGE_MASK;
Laurent Viviercebff022007-07-30 13:35:24 +03001258 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001259 if (rc != X86EMUL_CONTINUE)
1260 return rc;
1261 addr += now;
1262 val += now;
1263 bytes -= now;
1264 }
Laurent Viviercebff022007-07-30 13:35:24 +03001265 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001266}
Laurent Viviere7d5d762007-07-30 13:41:19 +03001267EXPORT_SYMBOL_GPL(emulator_write_emulated);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001268
Avi Kivity6aa8b732006-12-10 02:21:36 -08001269static int emulator_cmpxchg_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001270 const void *old,
1271 const void *new,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001272 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001273 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001274{
1275 static int reported;
1276
1277 if (!reported) {
1278 reported = 1;
1279 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1280 }
Laurent Viviercebff022007-07-30 13:35:24 +03001281 return emulator_write_emulated(addr, new, bytes, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001282}
1283
1284static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1285{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001286 return kvm_x86_ops->get_segment_base(vcpu, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001287}
1288
1289int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1290{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001291 return X86EMUL_CONTINUE;
1292}
1293
1294int emulate_clts(struct kvm_vcpu *vcpu)
1295{
Amit Shah404fb882007-11-19 17:57:35 +02001296 kvm_x86_ops->set_cr0(vcpu, vcpu->cr0 & ~X86_CR0_TS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001297 return X86EMUL_CONTINUE;
1298}
1299
1300int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1301{
1302 struct kvm_vcpu *vcpu = ctxt->vcpu;
1303
1304 switch (dr) {
1305 case 0 ... 3:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001306 *dest = kvm_x86_ops->get_dr(vcpu, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001307 return X86EMUL_CONTINUE;
1308 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001309 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001310 return X86EMUL_UNHANDLEABLE;
1311 }
1312}
1313
1314int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1315{
1316 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1317 int exception;
1318
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001319 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001320 if (exception) {
1321 /* FIXME: better handling */
1322 return X86EMUL_UNHANDLEABLE;
1323 }
1324 return X86EMUL_CONTINUE;
1325}
1326
Avi Kivity054b1362007-09-12 13:21:09 +03001327void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001328{
1329 static int reported;
1330 u8 opcodes[4];
Avi Kivity054b1362007-09-12 13:21:09 +03001331 unsigned long rip = vcpu->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001332 unsigned long rip_linear;
1333
Avi Kivity054b1362007-09-12 13:21:09 +03001334 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001335
1336 if (reported)
1337 return;
1338
Avi Kivity054b1362007-09-12 13:21:09 +03001339 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001340
Avi Kivity054b1362007-09-12 13:21:09 +03001341 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1342 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001343 reported = 1;
1344}
Avi Kivity054b1362007-09-12 13:21:09 +03001345EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001346
1347struct x86_emulate_ops emulate_ops = {
1348 .read_std = emulator_read_std,
1349 .write_std = emulator_write_std,
1350 .read_emulated = emulator_read_emulated,
1351 .write_emulated = emulator_write_emulated,
1352 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1353};
1354
1355int emulate_instruction(struct kvm_vcpu *vcpu,
1356 struct kvm_run *run,
1357 unsigned long cr2,
Laurent Vivier34273182007-09-18 11:27:37 +02001358 u16 error_code,
1359 int no_decode)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001360{
Laurent Viviera22436b2007-09-24 17:00:58 +02001361 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001362
Avi Kivitye7df56e2007-03-14 15:54:54 +02001363 vcpu->mmio_fault_cr2 = cr2;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001364 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001365
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366 vcpu->mmio_is_write = 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03001367 vcpu->pio.string = 0;
Laurent Vivier34273182007-09-18 11:27:37 +02001368
1369 if (!no_decode) {
1370 int cs_db, cs_l;
1371 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1372
1373 vcpu->emulate_ctxt.vcpu = vcpu;
1374 vcpu->emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
1375 vcpu->emulate_ctxt.cr2 = cr2;
1376 vcpu->emulate_ctxt.mode =
1377 (vcpu->emulate_ctxt.eflags & X86_EFLAGS_VM)
1378 ? X86EMUL_MODE_REAL : cs_l
1379 ? X86EMUL_MODE_PROT64 : cs_db
1380 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1381
1382 if (vcpu->emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1383 vcpu->emulate_ctxt.cs_base = 0;
1384 vcpu->emulate_ctxt.ds_base = 0;
1385 vcpu->emulate_ctxt.es_base = 0;
1386 vcpu->emulate_ctxt.ss_base = 0;
1387 } else {
1388 vcpu->emulate_ctxt.cs_base =
1389 get_segment_base(vcpu, VCPU_SREG_CS);
1390 vcpu->emulate_ctxt.ds_base =
1391 get_segment_base(vcpu, VCPU_SREG_DS);
1392 vcpu->emulate_ctxt.es_base =
1393 get_segment_base(vcpu, VCPU_SREG_ES);
1394 vcpu->emulate_ctxt.ss_base =
1395 get_segment_base(vcpu, VCPU_SREG_SS);
1396 }
1397
1398 vcpu->emulate_ctxt.gs_base =
1399 get_segment_base(vcpu, VCPU_SREG_GS);
1400 vcpu->emulate_ctxt.fs_base =
1401 get_segment_base(vcpu, VCPU_SREG_FS);
1402
1403 r = x86_decode_insn(&vcpu->emulate_ctxt, &emulate_ops);
Laurent Viviera22436b2007-09-24 17:00:58 +02001404 if (r) {
1405 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1406 return EMULATE_DONE;
1407 return EMULATE_FAIL;
1408 }
Laurent Vivier34273182007-09-18 11:27:37 +02001409 }
1410
Laurent Viviera22436b2007-09-24 17:00:58 +02001411 r = x86_emulate_insn(&vcpu->emulate_ctxt, &emulate_ops);
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001412
Laurent Viviere70669a2007-08-05 10:36:40 +03001413 if (vcpu->pio.string)
1414 return EMULATE_DO_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001415
1416 if ((r || vcpu->mmio_is_write) && run) {
Jeff Dike8fc0d082007-07-17 12:26:59 -04001417 run->exit_reason = KVM_EXIT_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001418 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1419 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1420 run->mmio.len = vcpu->mmio_size;
1421 run->mmio.is_write = vcpu->mmio_is_write;
1422 }
1423
1424 if (r) {
Avi Kivitya4360362007-01-05 16:36:45 -08001425 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1426 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001427 if (!vcpu->mmio_needed) {
Avi Kivity054b1362007-09-12 13:21:09 +03001428 kvm_report_emulation_failure(vcpu, "mmio");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429 return EMULATE_FAIL;
1430 }
1431 return EMULATE_DO_MMIO;
1432 }
1433
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001434 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier34273182007-09-18 11:27:37 +02001435 kvm_x86_ops->set_rflags(vcpu, vcpu->emulate_ctxt.eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001436
Avi Kivity02c83202007-04-29 15:02:17 +03001437 if (vcpu->mmio_is_write) {
1438 vcpu->mmio_needed = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001439 return EMULATE_DO_MMIO;
Avi Kivity02c83202007-04-29 15:02:17 +03001440 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001441
1442 return EMULATE_DONE;
1443}
1444EXPORT_SYMBOL_GPL(emulate_instruction);
1445
Eddie Dongb6958ce2007-07-18 12:15:21 +03001446/*
1447 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1448 */
He, Qingc5ec1532007-09-03 17:07:41 +03001449static void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001450{
1451 DECLARE_WAITQUEUE(wait, current);
1452
1453 add_wait_queue(&vcpu->wq, &wait);
1454
1455 /*
1456 * We will block until either an interrupt or a signal wakes us up
1457 */
He, Qingc5ec1532007-09-03 17:07:41 +03001458 while (!kvm_cpu_has_interrupt(vcpu)
1459 && !signal_pending(current)
1460 && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE
1461 && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) {
Eddie Dongb6958ce2007-07-18 12:15:21 +03001462 set_current_state(TASK_INTERRUPTIBLE);
1463 vcpu_put(vcpu);
1464 schedule();
1465 vcpu_load(vcpu);
1466 }
1467
He, Qingc5ec1532007-09-03 17:07:41 +03001468 __set_current_state(TASK_RUNNING);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001469 remove_wait_queue(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001470}
1471
Avi Kivityd3bef152007-06-05 15:53:05 +03001472int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1473{
Avi Kivityd3bef152007-06-05 15:53:05 +03001474 ++vcpu->stat.halt_exits;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001475 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc5ec1532007-09-03 17:07:41 +03001476 vcpu->mp_state = VCPU_MP_STATE_HALTED;
1477 kvm_vcpu_block(vcpu);
1478 if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE)
1479 return -EINTR;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001480 return 1;
1481 } else {
1482 vcpu->run->exit_reason = KVM_EXIT_HLT;
1483 return 0;
1484 }
Avi Kivityd3bef152007-06-05 15:53:05 +03001485}
1486EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1487
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001488int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
Avi Kivity270fd9b2007-02-19 14:37:47 +02001489{
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001490 unsigned long nr, a0, a1, a2, a3, ret;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001491
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001492 kvm_x86_ops->cache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001493
1494 nr = vcpu->regs[VCPU_REGS_RAX];
1495 a0 = vcpu->regs[VCPU_REGS_RBX];
1496 a1 = vcpu->regs[VCPU_REGS_RCX];
1497 a2 = vcpu->regs[VCPU_REGS_RDX];
1498 a3 = vcpu->regs[VCPU_REGS_RSI];
1499
1500 if (!is_long_mode(vcpu)) {
1501 nr &= 0xFFFFFFFF;
1502 a0 &= 0xFFFFFFFF;
1503 a1 &= 0xFFFFFFFF;
1504 a2 &= 0xFFFFFFFF;
1505 a3 &= 0xFFFFFFFF;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001506 }
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001507
Avi Kivity270fd9b2007-02-19 14:37:47 +02001508 switch (nr) {
1509 default:
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001510 ret = -KVM_ENOSYS;
1511 break;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001512 }
1513 vcpu->regs[VCPU_REGS_RAX] = ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001514 kvm_x86_ops->decache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001515 return 0;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001516}
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001517EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
1518
1519int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
1520{
1521 char instruction[3];
1522 int ret = 0;
1523
1524 mutex_lock(&vcpu->kvm->lock);
1525
1526 /*
1527 * Blow out the MMU to ensure that no other VCPU has an active mapping
1528 * to ensure that the updated hypercall appears atomically across all
1529 * VCPUs.
1530 */
1531 kvm_mmu_zap_all(vcpu->kvm);
1532
1533 kvm_x86_ops->cache_regs(vcpu);
1534 kvm_x86_ops->patch_hypercall(vcpu, instruction);
1535 if (emulator_write_emulated(vcpu->rip, instruction, 3, vcpu)
1536 != X86EMUL_CONTINUE)
1537 ret = -EFAULT;
1538
1539 mutex_unlock(&vcpu->kvm->lock);
1540
1541 return ret;
1542}
Avi Kivity270fd9b2007-02-19 14:37:47 +02001543
Avi Kivity6aa8b732006-12-10 02:21:36 -08001544static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1545{
1546 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1547}
1548
1549void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1550{
1551 struct descriptor_table dt = { limit, base };
1552
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001553 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001554}
1555
1556void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1557{
1558 struct descriptor_table dt = { limit, base };
1559
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001560 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001561}
1562
1563void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1564 unsigned long *rflags)
1565{
1566 lmsw(vcpu, msw);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001567 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001568}
1569
1570unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1571{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001572 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001573 switch (cr) {
1574 case 0:
1575 return vcpu->cr0;
1576 case 2:
1577 return vcpu->cr2;
1578 case 3:
1579 return vcpu->cr3;
1580 case 4:
1581 return vcpu->cr4;
1582 default:
1583 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1584 return 0;
1585 }
1586}
1587
1588void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1589 unsigned long *rflags)
1590{
1591 switch (cr) {
1592 case 0:
1593 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001594 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001595 break;
1596 case 2:
1597 vcpu->cr2 = val;
1598 break;
1599 case 3:
1600 set_cr3(vcpu, val);
1601 break;
1602 case 4:
1603 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1604 break;
1605 default:
1606 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1607 }
1608}
1609
Avi Kivity3bab1f52006-12-29 16:49:48 -08001610int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1611{
1612 u64 data;
1613
1614 switch (msr) {
1615 case 0xc0010010: /* SYSCFG */
1616 case 0xc0010015: /* HWCR */
1617 case MSR_IA32_PLATFORM_ID:
1618 case MSR_IA32_P5_MC_ADDR:
1619 case MSR_IA32_P5_MC_TYPE:
1620 case MSR_IA32_MC0_CTL:
1621 case MSR_IA32_MCG_STATUS:
1622 case MSR_IA32_MCG_CAP:
1623 case MSR_IA32_MC0_MISC:
1624 case MSR_IA32_MC0_MISC+4:
1625 case MSR_IA32_MC0_MISC+8:
1626 case MSR_IA32_MC0_MISC+12:
1627 case MSR_IA32_MC0_MISC+16:
1628 case MSR_IA32_UCODE_REV:
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001629 case MSR_IA32_PERF_STATUS:
Matthew Gregan2dc70942007-05-06 10:59:46 +03001630 case MSR_IA32_EBL_CR_POWERON:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001631 /* MTRR registers */
1632 case 0xfe:
1633 case 0x200 ... 0x2ff:
1634 data = 0;
1635 break;
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001636 case 0xcd: /* fsb frequency */
1637 data = 3;
1638 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001639 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001640 data = kvm_get_apic_base(vcpu);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001641 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001642 case MSR_IA32_MISC_ENABLE:
1643 data = vcpu->ia32_misc_enable_msr;
1644 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001645#ifdef CONFIG_X86_64
1646 case MSR_EFER:
1647 data = vcpu->shadow_efer;
1648 break;
1649#endif
1650 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001651 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001652 return 1;
1653 }
1654 *pdata = data;
1655 return 0;
1656}
1657EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1658
Avi Kivity6aa8b732006-12-10 02:21:36 -08001659/*
1660 * Reads an msr value (of 'msr_index') into 'pdata'.
1661 * Returns 0 on success, non-0 otherwise.
1662 * Assumes vcpu_load() was already called.
1663 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001664int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001665{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001666 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001667}
1668
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001669#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001670
Avi Kivity3bab1f52006-12-29 16:49:48 -08001671static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001672{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001673 if (efer & EFER_RESERVED_BITS) {
1674 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1675 efer);
1676 inject_gp(vcpu);
1677 return;
1678 }
1679
1680 if (is_paging(vcpu)
1681 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1682 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1683 inject_gp(vcpu);
1684 return;
1685 }
1686
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001687 kvm_x86_ops->set_efer(vcpu, efer);
Avi Kivity7725f0b2006-12-13 00:34:01 -08001688
Avi Kivity6aa8b732006-12-10 02:21:36 -08001689 efer &= ~EFER_LMA;
1690 efer |= vcpu->shadow_efer & EFER_LMA;
1691
1692 vcpu->shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001693}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001694
1695#endif
1696
Avi Kivity3bab1f52006-12-29 16:49:48 -08001697int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1698{
1699 switch (msr) {
1700#ifdef CONFIG_X86_64
1701 case MSR_EFER:
1702 set_efer(vcpu, data);
1703 break;
1704#endif
1705 case MSR_IA32_MC0_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001706 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
Avi Kivity3bab1f52006-12-29 16:49:48 -08001707 __FUNCTION__, data);
1708 break;
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001709 case MSR_IA32_MCG_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001710 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001711 __FUNCTION__, data);
1712 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001713 case MSR_IA32_UCODE_REV:
1714 case MSR_IA32_UCODE_WRITE:
1715 case 0x200 ... 0x2ff: /* MTRRs */
1716 break;
1717 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001718 kvm_set_apic_base(vcpu, data);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001719 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001720 case MSR_IA32_MISC_ENABLE:
1721 vcpu->ia32_misc_enable_msr = data;
1722 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001723 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001724 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001725 return 1;
1726 }
1727 return 0;
1728}
1729EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1730
Avi Kivity6aa8b732006-12-10 02:21:36 -08001731/*
1732 * Writes msr value into into the appropriate "register".
1733 * Returns 0 on success, non-0 otherwise.
1734 * Assumes vcpu_load() was already called.
1735 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001736int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001737{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001738 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001739}
1740
1741void kvm_resched(struct kvm_vcpu *vcpu)
1742{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001743 if (!need_resched())
1744 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001745 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001746}
1747EXPORT_SYMBOL_GPL(kvm_resched);
1748
Avi Kivity06465c52007-02-28 20:46:53 +02001749void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1750{
1751 int i;
1752 u32 function;
1753 struct kvm_cpuid_entry *e, *best;
1754
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001755 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001756 function = vcpu->regs[VCPU_REGS_RAX];
1757 vcpu->regs[VCPU_REGS_RAX] = 0;
1758 vcpu->regs[VCPU_REGS_RBX] = 0;
1759 vcpu->regs[VCPU_REGS_RCX] = 0;
1760 vcpu->regs[VCPU_REGS_RDX] = 0;
1761 best = NULL;
1762 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1763 e = &vcpu->cpuid_entries[i];
1764 if (e->function == function) {
1765 best = e;
1766 break;
1767 }
1768 /*
1769 * Both basic or both extended?
1770 */
1771 if (((e->function ^ function) & 0x80000000) == 0)
1772 if (!best || e->function > best->function)
1773 best = e;
1774 }
1775 if (best) {
1776 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1777 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1778 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1779 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1780 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001781 kvm_x86_ops->decache_regs(vcpu);
1782 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001783}
1784EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1785
Avi Kivity039576c2007-03-20 12:46:50 +02001786static int pio_copy_data(struct kvm_vcpu *vcpu)
Avi Kivity46fc1472007-02-22 19:39:30 +02001787{
Avi Kivity039576c2007-03-20 12:46:50 +02001788 void *p = vcpu->pio_data;
1789 void *q;
1790 unsigned bytes;
1791 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1792
Avi Kivity039576c2007-03-20 12:46:50 +02001793 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1794 PAGE_KERNEL);
1795 if (!q) {
Avi Kivity039576c2007-03-20 12:46:50 +02001796 free_pio_guest_pages(vcpu);
1797 return -ENOMEM;
1798 }
1799 q += vcpu->pio.guest_page_offset;
1800 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1801 if (vcpu->pio.in)
1802 memcpy(q, p, bytes);
1803 else
1804 memcpy(p, q, bytes);
1805 q -= vcpu->pio.guest_page_offset;
1806 vunmap(q);
Avi Kivity039576c2007-03-20 12:46:50 +02001807 free_pio_guest_pages(vcpu);
1808 return 0;
1809}
1810
1811static int complete_pio(struct kvm_vcpu *vcpu)
1812{
1813 struct kvm_pio_request *io = &vcpu->pio;
Avi Kivity46fc1472007-02-22 19:39:30 +02001814 long delta;
Avi Kivity039576c2007-03-20 12:46:50 +02001815 int r;
Avi Kivity46fc1472007-02-22 19:39:30 +02001816
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001817 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001818
1819 if (!io->string) {
Avi Kivity039576c2007-03-20 12:46:50 +02001820 if (io->in)
1821 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
Avi Kivity46fc1472007-02-22 19:39:30 +02001822 io->size);
1823 } else {
Avi Kivity039576c2007-03-20 12:46:50 +02001824 if (io->in) {
1825 r = pio_copy_data(vcpu);
1826 if (r) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001827 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02001828 return r;
1829 }
1830 }
1831
Avi Kivity46fc1472007-02-22 19:39:30 +02001832 delta = 1;
1833 if (io->rep) {
Avi Kivity039576c2007-03-20 12:46:50 +02001834 delta *= io->cur_count;
Avi Kivity46fc1472007-02-22 19:39:30 +02001835 /*
1836 * The size of the register should really depend on
1837 * current address size.
1838 */
1839 vcpu->regs[VCPU_REGS_RCX] -= delta;
1840 }
Avi Kivity039576c2007-03-20 12:46:50 +02001841 if (io->down)
Avi Kivity46fc1472007-02-22 19:39:30 +02001842 delta = -delta;
1843 delta *= io->size;
Avi Kivity039576c2007-03-20 12:46:50 +02001844 if (io->in)
Avi Kivity46fc1472007-02-22 19:39:30 +02001845 vcpu->regs[VCPU_REGS_RDI] += delta;
1846 else
1847 vcpu->regs[VCPU_REGS_RSI] += delta;
1848 }
1849
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001850 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001851
Avi Kivity039576c2007-03-20 12:46:50 +02001852 io->count -= io->cur_count;
1853 io->cur_count = 0;
1854
Avi Kivity039576c2007-03-20 12:46:50 +02001855 return 0;
Avi Kivity46fc1472007-02-22 19:39:30 +02001856}
1857
Eddie Dong65619eb2007-07-17 11:52:33 +03001858static void kernel_pio(struct kvm_io_device *pio_dev,
1859 struct kvm_vcpu *vcpu,
1860 void *pd)
Eddie Dong74906342007-06-19 18:05:03 +03001861{
1862 /* TODO: String I/O for in kernel device */
1863
Eddie Dong9cf98822007-07-22 10:36:31 +03001864 mutex_lock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001865 if (vcpu->pio.in)
1866 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1867 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001868 pd);
Eddie Dong74906342007-06-19 18:05:03 +03001869 else
1870 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1871 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001872 pd);
Eddie Dong9cf98822007-07-22 10:36:31 +03001873 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001874}
1875
1876static void pio_string_write(struct kvm_io_device *pio_dev,
1877 struct kvm_vcpu *vcpu)
1878{
1879 struct kvm_pio_request *io = &vcpu->pio;
1880 void *pd = vcpu->pio_data;
1881 int i;
1882
Eddie Dong9cf98822007-07-22 10:36:31 +03001883 mutex_lock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001884 for (i = 0; i < io->cur_count; i++) {
1885 kvm_iodevice_write(pio_dev, io->port,
1886 io->size,
1887 pd);
1888 pd += io->size;
1889 }
Eddie Dong9cf98822007-07-22 10:36:31 +03001890 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001891}
1892
Laurent Vivier3090dd72007-08-05 10:43:32 +03001893int kvm_emulate_pio (struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1894 int size, unsigned port)
1895{
1896 struct kvm_io_device *pio_dev;
1897
1898 vcpu->run->exit_reason = KVM_EXIT_IO;
1899 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1900 vcpu->run->io.size = vcpu->pio.size = size;
1901 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1902 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1;
1903 vcpu->run->io.port = vcpu->pio.port = port;
1904 vcpu->pio.in = in;
1905 vcpu->pio.string = 0;
1906 vcpu->pio.down = 0;
1907 vcpu->pio.guest_page_offset = 0;
1908 vcpu->pio.rep = 0;
1909
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001910 kvm_x86_ops->cache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03001911 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001912 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03001913
Avi Kivity0967b7b2007-09-15 17:34:36 +03001914 kvm_x86_ops->skip_emulated_instruction(vcpu);
1915
Laurent Vivier3090dd72007-08-05 10:43:32 +03001916 pio_dev = vcpu_find_pio_dev(vcpu, port);
1917 if (pio_dev) {
1918 kernel_pio(pio_dev, vcpu, vcpu->pio_data);
1919 complete_pio(vcpu);
1920 return 1;
1921 }
1922 return 0;
1923}
1924EXPORT_SYMBOL_GPL(kvm_emulate_pio);
1925
1926int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1927 int size, unsigned long count, int down,
Avi Kivity039576c2007-03-20 12:46:50 +02001928 gva_t address, int rep, unsigned port)
1929{
1930 unsigned now, in_page;
Eddie Dong65619eb2007-07-17 11:52:33 +03001931 int i, ret = 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001932 int nr_pages = 1;
1933 struct page *page;
Eddie Dong74906342007-06-19 18:05:03 +03001934 struct kvm_io_device *pio_dev;
Avi Kivity039576c2007-03-20 12:46:50 +02001935
1936 vcpu->run->exit_reason = KVM_EXIT_IO;
1937 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Laurent Vivier3090dd72007-08-05 10:43:32 +03001938 vcpu->run->io.size = vcpu->pio.size = size;
Avi Kivity039576c2007-03-20 12:46:50 +02001939 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Laurent Vivier3090dd72007-08-05 10:43:32 +03001940 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count;
1941 vcpu->run->io.port = vcpu->pio.port = port;
Avi Kivity039576c2007-03-20 12:46:50 +02001942 vcpu->pio.in = in;
Laurent Vivier3090dd72007-08-05 10:43:32 +03001943 vcpu->pio.string = 1;
Avi Kivity039576c2007-03-20 12:46:50 +02001944 vcpu->pio.down = down;
1945 vcpu->pio.guest_page_offset = offset_in_page(address);
1946 vcpu->pio.rep = rep;
1947
Avi Kivity039576c2007-03-20 12:46:50 +02001948 if (!count) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001949 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02001950 return 1;
1951 }
1952
Avi Kivity039576c2007-03-20 12:46:50 +02001953 if (!down)
1954 in_page = PAGE_SIZE - offset_in_page(address);
1955 else
1956 in_page = offset_in_page(address) + size;
1957 now = min(count, (unsigned long)in_page / size);
1958 if (!now) {
1959 /*
1960 * String I/O straddles page boundary. Pin two guest pages
1961 * so that we satisfy atomicity constraints. Do just one
1962 * transaction to avoid complexity.
1963 */
1964 nr_pages = 2;
1965 now = 1;
1966 }
1967 if (down) {
1968 /*
1969 * String I/O in reverse. Yuck. Kill the guest, fix later.
1970 */
Rusty Russellf0242472007-08-01 10:48:02 +10001971 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivity039576c2007-03-20 12:46:50 +02001972 inject_gp(vcpu);
1973 return 1;
1974 }
1975 vcpu->run->io.count = now;
1976 vcpu->pio.cur_count = now;
1977
Avi Kivity0967b7b2007-09-15 17:34:36 +03001978 if (vcpu->pio.cur_count == vcpu->pio.count)
1979 kvm_x86_ops->skip_emulated_instruction(vcpu);
1980
Avi Kivity039576c2007-03-20 12:46:50 +02001981 for (i = 0; i < nr_pages; ++i) {
Shaohua Li11ec2802007-07-23 14:51:37 +08001982 mutex_lock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02001983 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
1984 if (page)
1985 get_page(page);
1986 vcpu->pio.guest_pages[i] = page;
Shaohua Li11ec2802007-07-23 14:51:37 +08001987 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02001988 if (!page) {
1989 inject_gp(vcpu);
1990 free_pio_guest_pages(vcpu);
1991 return 1;
1992 }
1993 }
1994
Laurent Vivier3090dd72007-08-05 10:43:32 +03001995 pio_dev = vcpu_find_pio_dev(vcpu, port);
Eddie Dong65619eb2007-07-17 11:52:33 +03001996 if (!vcpu->pio.in) {
1997 /* string PIO write */
1998 ret = pio_copy_data(vcpu);
1999 if (ret >= 0 && pio_dev) {
2000 pio_string_write(pio_dev, vcpu);
2001 complete_pio(vcpu);
2002 if (vcpu->pio.count == 0)
2003 ret = 1;
2004 }
2005 } else if (pio_dev)
Rusty Russellf0242472007-08-01 10:48:02 +10002006 pr_unimpl(vcpu, "no string pio read support yet, "
Eddie Dong65619eb2007-07-17 11:52:33 +03002007 "port %x size %d count %ld\n",
2008 port, size, count);
2009
2010 return ret;
Avi Kivity039576c2007-03-20 12:46:50 +02002011}
Laurent Vivier3090dd72007-08-05 10:43:32 +03002012EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
Avi Kivity039576c2007-03-20 12:46:50 +02002013
Avi Kivity04d2cc72007-09-10 18:10:54 +03002014/*
2015 * Check if userspace requested an interrupt window, and that the
2016 * interrupt window is open.
2017 *
2018 * No need to exit to userspace if we already have an interrupt queued.
2019 */
2020static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2021 struct kvm_run *kvm_run)
2022{
2023 return (!vcpu->irq_summary &&
2024 kvm_run->request_interrupt_window &&
2025 vcpu->interrupt_window_open &&
2026 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
2027}
2028
2029static void post_kvm_run_save(struct kvm_vcpu *vcpu,
2030 struct kvm_run *kvm_run)
2031{
2032 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
2033 kvm_run->cr8 = get_cr8(vcpu);
2034 kvm_run->apic_base = kvm_get_apic_base(vcpu);
2035 if (irqchip_in_kernel(vcpu->kvm))
2036 kvm_run->ready_for_interrupt_injection = 1;
2037 else
2038 kvm_run->ready_for_interrupt_injection =
2039 (vcpu->interrupt_window_open &&
2040 vcpu->irq_summary == 0);
2041}
2042
2043static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2044{
2045 int r;
2046
2047 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
2048 printk("vcpu %d received sipi with vector # %x\n",
2049 vcpu->vcpu_id, vcpu->sipi_vector);
2050 kvm_lapic_reset(vcpu);
2051 kvm_x86_ops->vcpu_reset(vcpu);
2052 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
2053 }
2054
2055preempted:
2056 if (vcpu->guest_debug.enabled)
2057 kvm_x86_ops->guest_debug_pre(vcpu);
2058
2059again:
2060 r = kvm_mmu_reload(vcpu);
2061 if (unlikely(r))
2062 goto out;
2063
2064 preempt_disable();
2065
2066 kvm_x86_ops->prepare_guest_switch(vcpu);
2067 kvm_load_guest_fpu(vcpu);
2068
2069 local_irq_disable();
2070
2071 if (signal_pending(current)) {
2072 local_irq_enable();
2073 preempt_enable();
2074 r = -EINTR;
2075 kvm_run->exit_reason = KVM_EXIT_INTR;
2076 ++vcpu->stat.signal_exits;
2077 goto out;
2078 }
2079
2080 if (irqchip_in_kernel(vcpu->kvm))
2081 kvm_x86_ops->inject_pending_irq(vcpu);
2082 else if (!vcpu->mmio_read_completed)
2083 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2084
2085 vcpu->guest_mode = 1;
Laurent Vivierd172fcd2007-10-15 17:00:19 +02002086 kvm_guest_enter();
Avi Kivity04d2cc72007-09-10 18:10:54 +03002087
2088 if (vcpu->requests)
2089 if (test_and_clear_bit(KVM_TLB_FLUSH, &vcpu->requests))
2090 kvm_x86_ops->tlb_flush(vcpu);
2091
2092 kvm_x86_ops->run(vcpu, kvm_run);
2093
2094 vcpu->guest_mode = 0;
2095 local_irq_enable();
2096
2097 ++vcpu->stat.exits;
2098
Laurent Vivier0552f732007-10-18 15:19:01 +02002099 /*
2100 * We must have an instruction between local_irq_enable() and
2101 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2102 * the interrupt shadow. The stat.exits increment will do nicely.
2103 * But we need to prevent reordering, hence this barrier():
2104 */
2105 barrier();
2106
2107 kvm_guest_exit();
2108
Avi Kivity04d2cc72007-09-10 18:10:54 +03002109 preempt_enable();
2110
2111 /*
2112 * Profile KVM exit RIPs:
2113 */
2114 if (unlikely(prof_on == KVM_PROFILING)) {
2115 kvm_x86_ops->cache_regs(vcpu);
2116 profile_hit(KVM_PROFILING, (void *)vcpu->rip);
2117 }
2118
2119 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2120
2121 if (r > 0) {
2122 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2123 r = -EINTR;
2124 kvm_run->exit_reason = KVM_EXIT_INTR;
2125 ++vcpu->stat.request_irq_exits;
2126 goto out;
2127 }
2128 if (!need_resched()) {
2129 ++vcpu->stat.light_exits;
2130 goto again;
2131 }
2132 }
2133
2134out:
2135 if (r > 0) {
2136 kvm_resched(vcpu);
2137 goto preempted;
2138 }
2139
2140 post_kvm_run_save(vcpu, kvm_run);
2141
2142 return r;
2143}
2144
2145
Avi Kivitybccf2152007-02-21 18:04:26 +02002146static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002147{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002148 int r;
Avi Kivity1961d272007-03-05 19:46:05 +02002149 sigset_t sigsaved;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002150
Avi Kivitybccf2152007-02-21 18:04:26 +02002151 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002152
He, Qingc5ec1532007-09-03 17:07:41 +03002153 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
2154 kvm_vcpu_block(vcpu);
2155 vcpu_put(vcpu);
2156 return -EAGAIN;
2157 }
2158
Avi Kivity1961d272007-03-05 19:46:05 +02002159 if (vcpu->sigset_active)
2160 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2161
Dor Laor54810342007-02-12 00:54:39 -08002162 /* re-sync apic's tpr */
He, Qing5cd4f6f2007-08-30 17:04:26 +08002163 if (!irqchip_in_kernel(vcpu->kvm))
2164 set_cr8(vcpu, kvm_run->cr8);
Dor Laor54810342007-02-12 00:54:39 -08002165
Avi Kivity02c83202007-04-29 15:02:17 +03002166 if (vcpu->pio.cur_count) {
2167 r = complete_pio(vcpu);
2168 if (r)
2169 goto out;
2170 }
2171
2172 if (vcpu->mmio_needed) {
2173 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
2174 vcpu->mmio_read_completed = 1;
2175 vcpu->mmio_needed = 0;
2176 r = emulate_instruction(vcpu, kvm_run,
Laurent Vivier34273182007-09-18 11:27:37 +02002177 vcpu->mmio_fault_cr2, 0, 1);
Avi Kivity02c83202007-04-29 15:02:17 +03002178 if (r == EMULATE_DO_MMIO) {
2179 /*
2180 * Read-modify-write. Back to userspace.
2181 */
Avi Kivity02c83202007-04-29 15:02:17 +03002182 r = 0;
2183 goto out;
Avi Kivity46fc1472007-02-22 19:39:30 +02002184 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002185 }
2186
Avi Kivity8eb7d332007-03-04 14:17:08 +02002187 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002188 kvm_x86_ops->cache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002189 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002190 kvm_x86_ops->decache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002191 }
2192
Avi Kivity04d2cc72007-09-10 18:10:54 +03002193 r = __vcpu_run(vcpu, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002194
Avi Kivity039576c2007-03-20 12:46:50 +02002195out:
Avi Kivity1961d272007-03-05 19:46:05 +02002196 if (vcpu->sigset_active)
2197 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2198
Avi Kivity6aa8b732006-12-10 02:21:36 -08002199 vcpu_put(vcpu);
2200 return r;
2201}
2202
Avi Kivitybccf2152007-02-21 18:04:26 +02002203static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
2204 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002205{
Avi Kivitybccf2152007-02-21 18:04:26 +02002206 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002207
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002208 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002209
2210 regs->rax = vcpu->regs[VCPU_REGS_RAX];
2211 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
2212 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
2213 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
2214 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
2215 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
2216 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
2217 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002218#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002219 regs->r8 = vcpu->regs[VCPU_REGS_R8];
2220 regs->r9 = vcpu->regs[VCPU_REGS_R9];
2221 regs->r10 = vcpu->regs[VCPU_REGS_R10];
2222 regs->r11 = vcpu->regs[VCPU_REGS_R11];
2223 regs->r12 = vcpu->regs[VCPU_REGS_R12];
2224 regs->r13 = vcpu->regs[VCPU_REGS_R13];
2225 regs->r14 = vcpu->regs[VCPU_REGS_R14];
2226 regs->r15 = vcpu->regs[VCPU_REGS_R15];
2227#endif
2228
2229 regs->rip = vcpu->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002230 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002231
2232 /*
2233 * Don't leak debug flags in case they were set for guest debugging
2234 */
2235 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2236 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2237
2238 vcpu_put(vcpu);
2239
2240 return 0;
2241}
2242
Avi Kivitybccf2152007-02-21 18:04:26 +02002243static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
2244 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002245{
Avi Kivitybccf2152007-02-21 18:04:26 +02002246 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002247
2248 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
2249 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
2250 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
2251 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
2252 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
2253 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
2254 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
2255 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002256#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002257 vcpu->regs[VCPU_REGS_R8] = regs->r8;
2258 vcpu->regs[VCPU_REGS_R9] = regs->r9;
2259 vcpu->regs[VCPU_REGS_R10] = regs->r10;
2260 vcpu->regs[VCPU_REGS_R11] = regs->r11;
2261 vcpu->regs[VCPU_REGS_R12] = regs->r12;
2262 vcpu->regs[VCPU_REGS_R13] = regs->r13;
2263 vcpu->regs[VCPU_REGS_R14] = regs->r14;
2264 vcpu->regs[VCPU_REGS_R15] = regs->r15;
2265#endif
2266
2267 vcpu->rip = regs->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002268 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002269
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002270 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002271
2272 vcpu_put(vcpu);
2273
2274 return 0;
2275}
2276
2277static void get_segment(struct kvm_vcpu *vcpu,
2278 struct kvm_segment *var, int seg)
2279{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002280 return kvm_x86_ops->get_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002281}
2282
Avi Kivitybccf2152007-02-21 18:04:26 +02002283static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2284 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002285{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002286 struct descriptor_table dt;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002287 int pending_vec;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002288
Avi Kivitybccf2152007-02-21 18:04:26 +02002289 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002290
2291 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2292 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2293 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2294 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2295 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2296 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2297
2298 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2299 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2300
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002301 kvm_x86_ops->get_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002302 sregs->idt.limit = dt.limit;
2303 sregs->idt.base = dt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002304 kvm_x86_ops->get_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002305 sregs->gdt.limit = dt.limit;
2306 sregs->gdt.base = dt.base;
2307
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002308 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002309 sregs->cr0 = vcpu->cr0;
2310 sregs->cr2 = vcpu->cr2;
2311 sregs->cr3 = vcpu->cr3;
2312 sregs->cr4 = vcpu->cr4;
Eddie Dong7017fc32007-07-18 11:34:57 +03002313 sregs->cr8 = get_cr8(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002314 sregs->efer = vcpu->shadow_efer;
Eddie Dong7017fc32007-07-18 11:34:57 +03002315 sregs->apic_base = kvm_get_apic_base(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002316
Eddie Dong2a8067f2007-08-06 16:29:07 +03002317 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc52fb352007-08-02 14:03:07 +03002318 memset(sregs->interrupt_bitmap, 0,
2319 sizeof sregs->interrupt_bitmap);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002320 pending_vec = kvm_x86_ops->get_irq(vcpu);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002321 if (pending_vec >= 0)
2322 set_bit(pending_vec, (unsigned long *)sregs->interrupt_bitmap);
2323 } else
He, Qingc52fb352007-08-02 14:03:07 +03002324 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2325 sizeof sregs->interrupt_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002326
2327 vcpu_put(vcpu);
2328
2329 return 0;
2330}
2331
2332static void set_segment(struct kvm_vcpu *vcpu,
2333 struct kvm_segment *var, int seg)
2334{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002335 return kvm_x86_ops->set_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002336}
2337
Avi Kivitybccf2152007-02-21 18:04:26 +02002338static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2339 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002340{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002341 int mmu_reset_needed = 0;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002342 int i, pending_vec, max_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002343 struct descriptor_table dt;
2344
Avi Kivitybccf2152007-02-21 18:04:26 +02002345 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002346
Avi Kivity6aa8b732006-12-10 02:21:36 -08002347 dt.limit = sregs->idt.limit;
2348 dt.base = sregs->idt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002349 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002350 dt.limit = sregs->gdt.limit;
2351 dt.base = sregs->gdt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002352 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002353
2354 vcpu->cr2 = sregs->cr2;
2355 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2356 vcpu->cr3 = sregs->cr3;
2357
Eddie Dong7017fc32007-07-18 11:34:57 +03002358 set_cr8(vcpu, sregs->cr8);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002359
2360 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002361#ifdef CONFIG_X86_64
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002362 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002363#endif
Eddie Dong7017fc32007-07-18 11:34:57 +03002364 kvm_set_apic_base(vcpu, sregs->apic_base);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002365
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002366 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity399badf2007-01-05 16:36:38 -08002367
Avi Kivity6aa8b732006-12-10 02:21:36 -08002368 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
Rusty Russell81f50e32007-09-06 01:20:38 +10002369 vcpu->cr0 = sregs->cr0;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002370 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002371
2372 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002373 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Avi Kivity1b0973b2007-01-05 16:36:41 -08002374 if (!is_long_mode(vcpu) && is_pae(vcpu))
2375 load_pdptrs(vcpu, vcpu->cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002376
2377 if (mmu_reset_needed)
2378 kvm_mmu_reset_context(vcpu);
2379
He, Qingc52fb352007-08-02 14:03:07 +03002380 if (!irqchip_in_kernel(vcpu->kvm)) {
2381 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2382 sizeof vcpu->irq_pending);
2383 vcpu->irq_summary = 0;
2384 for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
2385 if (vcpu->irq_pending[i])
2386 __set_bit(i, &vcpu->irq_summary);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002387 } else {
2388 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
2389 pending_vec = find_first_bit(
2390 (const unsigned long *)sregs->interrupt_bitmap,
2391 max_bits);
2392 /* Only pending external irq is handled here */
2393 if (pending_vec < max_bits) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002394 kvm_x86_ops->set_irq(vcpu, pending_vec);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002395 printk("Set back pending irq %d\n", pending_vec);
2396 }
He, Qingc52fb352007-08-02 14:03:07 +03002397 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002398
Avi Kivity024aa1c2007-03-21 13:44:58 +02002399 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2400 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2401 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2402 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2403 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2404 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2405
2406 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2407 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2408
Avi Kivity6aa8b732006-12-10 02:21:36 -08002409 vcpu_put(vcpu);
2410
2411 return 0;
2412}
2413
Rusty Russell1747fb72007-09-06 01:21:32 +10002414void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2415{
2416 struct kvm_segment cs;
2417
2418 get_segment(vcpu, &cs, VCPU_SREG_CS);
2419 *db = cs.db;
2420 *l = cs.l;
2421}
2422EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2423
Avi Kivity6aa8b732006-12-10 02:21:36 -08002424/*
2425 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2426 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
Michael Riepebf591b22006-12-22 01:05:36 -08002427 *
2428 * This list is modified at module load time to reflect the
2429 * capabilities of the host cpu.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002430 */
2431static u32 msrs_to_save[] = {
2432 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
2433 MSR_K6_STAR,
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002434#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002435 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
2436#endif
2437 MSR_IA32_TIME_STAMP_COUNTER,
2438};
2439
Michael Riepebf591b22006-12-22 01:05:36 -08002440static unsigned num_msrs_to_save;
2441
Avi Kivity6f00e682007-01-26 00:56:40 -08002442static u32 emulated_msrs[] = {
2443 MSR_IA32_MISC_ENABLE,
2444};
2445
Michael Riepebf591b22006-12-22 01:05:36 -08002446static __init void kvm_init_msr_list(void)
2447{
2448 u32 dummy[2];
2449 unsigned i, j;
2450
2451 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2452 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2453 continue;
2454 if (j < i)
2455 msrs_to_save[j] = msrs_to_save[i];
2456 j++;
2457 }
2458 num_msrs_to_save = j;
2459}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002460
2461/*
2462 * Adapt set_msr() to msr_io()'s calling convention
2463 */
2464static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2465{
Avi Kivity35f3f282007-07-17 14:20:30 +03002466 return kvm_set_msr(vcpu, index, *data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002467}
2468
2469/*
2470 * Read or write a bunch of msrs. All parameters are kernel addresses.
2471 *
2472 * @return number of msrs set successfully.
2473 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002474static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002475 struct kvm_msr_entry *entries,
2476 int (*do_msr)(struct kvm_vcpu *vcpu,
2477 unsigned index, u64 *data))
2478{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002479 int i;
2480
Avi Kivitybccf2152007-02-21 18:04:26 +02002481 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002482
2483 for (i = 0; i < msrs->nmsrs; ++i)
2484 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2485 break;
2486
2487 vcpu_put(vcpu);
2488
2489 return i;
2490}
2491
2492/*
2493 * Read or write a bunch of msrs. Parameters are user addresses.
2494 *
2495 * @return number of msrs set successfully.
2496 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002497static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002498 int (*do_msr)(struct kvm_vcpu *vcpu,
2499 unsigned index, u64 *data),
2500 int writeback)
2501{
2502 struct kvm_msrs msrs;
2503 struct kvm_msr_entry *entries;
2504 int r, n;
2505 unsigned size;
2506
2507 r = -EFAULT;
2508 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2509 goto out;
2510
2511 r = -E2BIG;
2512 if (msrs.nmsrs >= MAX_IO_MSRS)
2513 goto out;
2514
2515 r = -ENOMEM;
2516 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
2517 entries = vmalloc(size);
2518 if (!entries)
2519 goto out;
2520
2521 r = -EFAULT;
2522 if (copy_from_user(entries, user_msrs->entries, size))
2523 goto out_free;
2524
Avi Kivitybccf2152007-02-21 18:04:26 +02002525 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002526 if (r < 0)
2527 goto out_free;
2528
2529 r = -EFAULT;
2530 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2531 goto out_free;
2532
2533 r = n;
2534
2535out_free:
2536 vfree(entries);
2537out:
2538 return r;
2539}
2540
2541/*
2542 * Translate a guest virtual address to a guest physical address.
2543 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002544static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2545 struct kvm_translation *tr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002546{
2547 unsigned long vaddr = tr->linear_address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002548 gpa_t gpa;
2549
Avi Kivitybccf2152007-02-21 18:04:26 +02002550 vcpu_load(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +08002551 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002552 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2553 tr->physical_address = gpa;
2554 tr->valid = gpa != UNMAPPED_GVA;
2555 tr->writeable = 1;
2556 tr->usermode = 0;
Shaohua Li11ec2802007-07-23 14:51:37 +08002557 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002558 vcpu_put(vcpu);
2559
2560 return 0;
2561}
2562
Avi Kivitybccf2152007-02-21 18:04:26 +02002563static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2564 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002565{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002566 if (irq->irq < 0 || irq->irq >= 256)
2567 return -EINVAL;
Eddie Dong97222cc2007-09-12 10:58:04 +03002568 if (irqchip_in_kernel(vcpu->kvm))
2569 return -ENXIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02002570 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002571
2572 set_bit(irq->irq, vcpu->irq_pending);
2573 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2574
2575 vcpu_put(vcpu);
2576
2577 return 0;
2578}
2579
Avi Kivitybccf2152007-02-21 18:04:26 +02002580static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2581 struct kvm_debug_guest *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002582{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002583 int r;
2584
Avi Kivitybccf2152007-02-21 18:04:26 +02002585 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002586
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002587 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002588
2589 vcpu_put(vcpu);
2590
2591 return r;
2592}
2593
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002594static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2595 unsigned long address,
2596 int *type)
2597{
2598 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2599 unsigned long pgoff;
2600 struct page *page;
2601
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002602 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +02002603 if (pgoff == 0)
2604 page = virt_to_page(vcpu->run);
2605 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2606 page = virt_to_page(vcpu->pio_data);
2607 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002608 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002609 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002610 if (type != NULL)
2611 *type = VM_FAULT_MINOR;
2612
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002613 return page;
2614}
2615
2616static struct vm_operations_struct kvm_vcpu_vm_ops = {
2617 .nopage = kvm_vcpu_nopage,
2618};
2619
2620static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2621{
2622 vma->vm_ops = &kvm_vcpu_vm_ops;
2623 return 0;
2624}
2625
Avi Kivitybccf2152007-02-21 18:04:26 +02002626static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2627{
2628 struct kvm_vcpu *vcpu = filp->private_data;
2629
2630 fput(vcpu->kvm->filp);
2631 return 0;
2632}
2633
2634static struct file_operations kvm_vcpu_fops = {
2635 .release = kvm_vcpu_release,
2636 .unlocked_ioctl = kvm_vcpu_ioctl,
2637 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002638 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02002639};
2640
2641/*
2642 * Allocates an inode for the vcpu.
2643 */
2644static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2645{
2646 int fd, r;
2647 struct inode *inode;
2648 struct file *file;
2649
Avi Kivityd6d28162007-06-28 08:38:16 -04002650 r = anon_inode_getfd(&fd, &inode, &file,
2651 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2652 if (r)
2653 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002654 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +02002655 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02002656}
2657
Avi Kivityc5ea7662007-02-20 18:41:05 +02002658/*
2659 * Creates some virtual cpus. Good luck creating more than one.
2660 */
2661static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2662{
2663 int r;
2664 struct kvm_vcpu *vcpu;
2665
Avi Kivityc5ea7662007-02-20 18:41:05 +02002666 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002667 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002668
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002669 vcpu = kvm_x86_ops->vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002670 if (IS_ERR(vcpu))
2671 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002672
Avi Kivity15ad7142007-07-11 18:17:21 +03002673 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
2674
Rusty Russellb114b082007-07-30 21:13:43 +10002675 /* We do fxsave: this must be aligned. */
2676 BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF);
2677
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002678 vcpu_load(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002679 r = kvm_mmu_setup(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002680 vcpu_put(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002681 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002682 goto free_vcpu;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002683
Shaohua Li11ec2802007-07-23 14:51:37 +08002684 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002685 if (kvm->vcpus[n]) {
2686 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +08002687 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002688 goto mmu_unload;
2689 }
2690 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08002691 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002692
2693 /* Now it's all set up, let userspace reach it */
Avi Kivitybccf2152007-02-21 18:04:26 +02002694 r = create_vcpu_fd(vcpu);
2695 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002696 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02002697 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002698
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002699unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08002700 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002701 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +08002702 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002703
2704mmu_unload:
2705 vcpu_load(vcpu);
2706 kvm_mmu_unload(vcpu);
2707 vcpu_put(vcpu);
2708
2709free_vcpu:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002710 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002711 return r;
2712}
2713
Eddie Dong2cc51562007-05-21 07:28:09 +03002714static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
2715{
2716 u64 efer;
2717 int i;
2718 struct kvm_cpuid_entry *e, *entry;
2719
2720 rdmsrl(MSR_EFER, efer);
2721 entry = NULL;
2722 for (i = 0; i < vcpu->cpuid_nent; ++i) {
2723 e = &vcpu->cpuid_entries[i];
2724 if (e->function == 0x80000001) {
2725 entry = e;
2726 break;
2727 }
2728 }
Avi Kivity4c981b42007-07-25 09:22:12 +03002729 if (entry && (entry->edx & (1 << 20)) && !(efer & EFER_NX)) {
Eddie Dong2cc51562007-05-21 07:28:09 +03002730 entry->edx &= ~(1 << 20);
Avi Kivity4c981b42007-07-25 09:22:12 +03002731 printk(KERN_INFO "kvm: guest NX capability removed\n");
Eddie Dong2cc51562007-05-21 07:28:09 +03002732 }
2733}
2734
Avi Kivity06465c52007-02-28 20:46:53 +02002735static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
2736 struct kvm_cpuid *cpuid,
2737 struct kvm_cpuid_entry __user *entries)
2738{
2739 int r;
2740
2741 r = -E2BIG;
2742 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2743 goto out;
2744 r = -EFAULT;
2745 if (copy_from_user(&vcpu->cpuid_entries, entries,
2746 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
2747 goto out;
2748 vcpu->cpuid_nent = cpuid->nent;
Eddie Dong2cc51562007-05-21 07:28:09 +03002749 cpuid_fix_nx_cap(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002750 return 0;
2751
2752out:
2753 return r;
2754}
2755
Avi Kivity1961d272007-03-05 19:46:05 +02002756static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2757{
2758 if (sigset) {
2759 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2760 vcpu->sigset_active = 1;
2761 vcpu->sigset = *sigset;
2762 } else
2763 vcpu->sigset_active = 0;
2764 return 0;
2765}
2766
Avi Kivityb8836732007-04-01 16:34:31 +03002767/*
2768 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2769 * we have asm/x86/processor.h
2770 */
2771struct fxsave {
2772 u16 cwd;
2773 u16 swd;
2774 u16 twd;
2775 u16 fop;
2776 u64 rip;
2777 u64 rdp;
2778 u32 mxcsr;
2779 u32 mxcsr_mask;
2780 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2781#ifdef CONFIG_X86_64
2782 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2783#else
2784 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2785#endif
2786};
2787
2788static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2789{
Rusty Russellb114b082007-07-30 21:13:43 +10002790 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002791
2792 vcpu_load(vcpu);
2793
2794 memcpy(fpu->fpr, fxsave->st_space, 128);
2795 fpu->fcw = fxsave->cwd;
2796 fpu->fsw = fxsave->swd;
2797 fpu->ftwx = fxsave->twd;
2798 fpu->last_opcode = fxsave->fop;
2799 fpu->last_ip = fxsave->rip;
2800 fpu->last_dp = fxsave->rdp;
2801 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2802
2803 vcpu_put(vcpu);
2804
2805 return 0;
2806}
2807
2808static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2809{
Rusty Russellb114b082007-07-30 21:13:43 +10002810 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002811
2812 vcpu_load(vcpu);
2813
2814 memcpy(fxsave->st_space, fpu->fpr, 128);
2815 fxsave->cwd = fpu->fcw;
2816 fxsave->swd = fpu->fsw;
2817 fxsave->twd = fpu->ftwx;
2818 fxsave->fop = fpu->last_opcode;
2819 fxsave->rip = fpu->last_ip;
2820 fxsave->rdp = fpu->last_dp;
2821 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2822
2823 vcpu_put(vcpu);
2824
2825 return 0;
2826}
2827
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002828static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
2829 struct kvm_lapic_state *s)
2830{
2831 vcpu_load(vcpu);
2832 memcpy(s->regs, vcpu->apic->regs, sizeof *s);
2833 vcpu_put(vcpu);
2834
2835 return 0;
2836}
2837
2838static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
2839 struct kvm_lapic_state *s)
2840{
2841 vcpu_load(vcpu);
2842 memcpy(vcpu->apic->regs, s->regs, sizeof *s);
2843 kvm_apic_post_state_restore(vcpu);
2844 vcpu_put(vcpu);
2845
2846 return 0;
2847}
2848
Avi Kivitybccf2152007-02-21 18:04:26 +02002849static long kvm_vcpu_ioctl(struct file *filp,
2850 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002851{
Avi Kivitybccf2152007-02-21 18:04:26 +02002852 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00002853 void __user *argp = (void __user *)arg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002854 int r = -EINVAL;
2855
2856 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002857 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002858 r = -EINVAL;
2859 if (arg)
2860 goto out;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002861 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002862 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002863 case KVM_GET_REGS: {
2864 struct kvm_regs kvm_regs;
2865
Avi Kivitybccf2152007-02-21 18:04:26 +02002866 memset(&kvm_regs, 0, sizeof kvm_regs);
2867 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002868 if (r)
2869 goto out;
2870 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002871 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002872 goto out;
2873 r = 0;
2874 break;
2875 }
2876 case KVM_SET_REGS: {
2877 struct kvm_regs kvm_regs;
2878
2879 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002880 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002881 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002882 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002883 if (r)
2884 goto out;
2885 r = 0;
2886 break;
2887 }
2888 case KVM_GET_SREGS: {
2889 struct kvm_sregs kvm_sregs;
2890
Avi Kivitybccf2152007-02-21 18:04:26 +02002891 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2892 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002893 if (r)
2894 goto out;
2895 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002896 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002897 goto out;
2898 r = 0;
2899 break;
2900 }
2901 case KVM_SET_SREGS: {
2902 struct kvm_sregs kvm_sregs;
2903
2904 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002905 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002906 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002907 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002908 if (r)
2909 goto out;
2910 r = 0;
2911 break;
2912 }
2913 case KVM_TRANSLATE: {
2914 struct kvm_translation tr;
2915
2916 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002917 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002918 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002919 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002920 if (r)
2921 goto out;
2922 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002923 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002924 goto out;
2925 r = 0;
2926 break;
2927 }
2928 case KVM_INTERRUPT: {
2929 struct kvm_interrupt irq;
2930
2931 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002932 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002933 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002934 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002935 if (r)
2936 goto out;
2937 r = 0;
2938 break;
2939 }
2940 case KVM_DEBUG_GUEST: {
2941 struct kvm_debug_guest dbg;
2942
2943 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002944 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002945 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002946 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002947 if (r)
2948 goto out;
2949 r = 0;
2950 break;
2951 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002952 case KVM_GET_MSRS:
Avi Kivity35f3f282007-07-17 14:20:30 +03002953 r = msr_io(vcpu, argp, kvm_get_msr, 1);
Avi Kivitybccf2152007-02-21 18:04:26 +02002954 break;
2955 case KVM_SET_MSRS:
2956 r = msr_io(vcpu, argp, do_set_msr, 0);
2957 break;
Avi Kivity06465c52007-02-28 20:46:53 +02002958 case KVM_SET_CPUID: {
2959 struct kvm_cpuid __user *cpuid_arg = argp;
2960 struct kvm_cpuid cpuid;
2961
2962 r = -EFAULT;
2963 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2964 goto out;
2965 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2966 if (r)
2967 goto out;
2968 break;
2969 }
Avi Kivity1961d272007-03-05 19:46:05 +02002970 case KVM_SET_SIGNAL_MASK: {
2971 struct kvm_signal_mask __user *sigmask_arg = argp;
2972 struct kvm_signal_mask kvm_sigmask;
2973 sigset_t sigset, *p;
2974
2975 p = NULL;
2976 if (argp) {
2977 r = -EFAULT;
2978 if (copy_from_user(&kvm_sigmask, argp,
2979 sizeof kvm_sigmask))
2980 goto out;
2981 r = -EINVAL;
2982 if (kvm_sigmask.len != sizeof sigset)
2983 goto out;
2984 r = -EFAULT;
2985 if (copy_from_user(&sigset, sigmask_arg->sigset,
2986 sizeof sigset))
2987 goto out;
2988 p = &sigset;
2989 }
2990 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2991 break;
2992 }
Avi Kivityb8836732007-04-01 16:34:31 +03002993 case KVM_GET_FPU: {
2994 struct kvm_fpu fpu;
2995
2996 memset(&fpu, 0, sizeof fpu);
2997 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2998 if (r)
2999 goto out;
3000 r = -EFAULT;
3001 if (copy_to_user(argp, &fpu, sizeof fpu))
3002 goto out;
3003 r = 0;
3004 break;
3005 }
3006 case KVM_SET_FPU: {
3007 struct kvm_fpu fpu;
3008
3009 r = -EFAULT;
3010 if (copy_from_user(&fpu, argp, sizeof fpu))
3011 goto out;
3012 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
3013 if (r)
3014 goto out;
3015 r = 0;
3016 break;
3017 }
Eddie Dong96ad2cc2007-09-06 12:22:56 +03003018 case KVM_GET_LAPIC: {
3019 struct kvm_lapic_state lapic;
3020
3021 memset(&lapic, 0, sizeof lapic);
3022 r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic);
3023 if (r)
3024 goto out;
3025 r = -EFAULT;
3026 if (copy_to_user(argp, &lapic, sizeof lapic))
3027 goto out;
3028 r = 0;
3029 break;
3030 }
3031 case KVM_SET_LAPIC: {
3032 struct kvm_lapic_state lapic;
3033
3034 r = -EFAULT;
3035 if (copy_from_user(&lapic, argp, sizeof lapic))
3036 goto out;
3037 r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);;
3038 if (r)
3039 goto out;
3040 r = 0;
3041 break;
3042 }
Avi Kivitybccf2152007-02-21 18:04:26 +02003043 default:
3044 ;
3045 }
3046out:
3047 return r;
3048}
3049
3050static long kvm_vm_ioctl(struct file *filp,
3051 unsigned int ioctl, unsigned long arg)
3052{
3053 struct kvm *kvm = filp->private_data;
3054 void __user *argp = (void __user *)arg;
3055 int r = -EINVAL;
3056
3057 switch (ioctl) {
3058 case KVM_CREATE_VCPU:
3059 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
3060 if (r < 0)
3061 goto out;
3062 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003063 case KVM_SET_MEMORY_REGION: {
3064 struct kvm_memory_region kvm_mem;
3065
3066 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00003067 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003068 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02003069 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003070 if (r)
3071 goto out;
3072 break;
3073 }
3074 case KVM_GET_DIRTY_LOG: {
3075 struct kvm_dirty_log log;
3076
3077 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00003078 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003079 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02003080 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003081 if (r)
3082 goto out;
3083 break;
3084 }
Avi Kivitye8207542007-03-30 16:54:30 +03003085 case KVM_SET_MEMORY_ALIAS: {
3086 struct kvm_memory_alias alias;
3087
3088 r = -EFAULT;
3089 if (copy_from_user(&alias, argp, sizeof alias))
3090 goto out;
3091 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
3092 if (r)
3093 goto out;
3094 break;
3095 }
Eddie Dong85f455f2007-07-06 12:20:49 +03003096 case KVM_CREATE_IRQCHIP:
3097 r = -ENOMEM;
3098 kvm->vpic = kvm_create_pic(kvm);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03003099 if (kvm->vpic) {
3100 r = kvm_ioapic_init(kvm);
3101 if (r) {
3102 kfree(kvm->vpic);
3103 kvm->vpic = NULL;
3104 goto out;
3105 }
3106 }
Eddie Dong85f455f2007-07-06 12:20:49 +03003107 else
3108 goto out;
3109 break;
3110 case KVM_IRQ_LINE: {
3111 struct kvm_irq_level irq_event;
3112
3113 r = -EFAULT;
3114 if (copy_from_user(&irq_event, argp, sizeof irq_event))
3115 goto out;
3116 if (irqchip_in_kernel(kvm)) {
Eddie Dong9cf98822007-07-22 10:36:31 +03003117 mutex_lock(&kvm->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +03003118 if (irq_event.irq < 16)
3119 kvm_pic_set_irq(pic_irqchip(kvm),
3120 irq_event.irq,
3121 irq_event.level);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03003122 kvm_ioapic_set_irq(kvm->vioapic,
3123 irq_event.irq,
3124 irq_event.level);
Eddie Dong9cf98822007-07-22 10:36:31 +03003125 mutex_unlock(&kvm->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +03003126 r = 0;
3127 }
3128 break;
3129 }
He, Qing6ceb9d72007-07-26 11:05:18 +03003130 case KVM_GET_IRQCHIP: {
3131 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3132 struct kvm_irqchip chip;
3133
3134 r = -EFAULT;
3135 if (copy_from_user(&chip, argp, sizeof chip))
3136 goto out;
3137 r = -ENXIO;
3138 if (!irqchip_in_kernel(kvm))
3139 goto out;
3140 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
3141 if (r)
3142 goto out;
3143 r = -EFAULT;
3144 if (copy_to_user(argp, &chip, sizeof chip))
3145 goto out;
3146 r = 0;
3147 break;
3148 }
3149 case KVM_SET_IRQCHIP: {
3150 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3151 struct kvm_irqchip chip;
3152
3153 r = -EFAULT;
3154 if (copy_from_user(&chip, argp, sizeof chip))
3155 goto out;
3156 r = -ENXIO;
3157 if (!irqchip_in_kernel(kvm))
3158 goto out;
3159 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
3160 if (r)
3161 goto out;
3162 r = 0;
3163 break;
3164 }
Avi Kivityf17abe92007-02-21 19:28:04 +02003165 default:
3166 ;
3167 }
3168out:
3169 return r;
3170}
3171
3172static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
3173 unsigned long address,
3174 int *type)
3175{
3176 struct kvm *kvm = vma->vm_file->private_data;
3177 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02003178 struct page *page;
3179
Avi Kivityf17abe92007-02-21 19:28:04 +02003180 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity954bbbc2007-03-30 14:02:32 +03003181 page = gfn_to_page(kvm, pgoff);
Avi Kivityf17abe92007-02-21 19:28:04 +02003182 if (!page)
3183 return NOPAGE_SIGBUS;
3184 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03003185 if (type != NULL)
3186 *type = VM_FAULT_MINOR;
3187
Avi Kivityf17abe92007-02-21 19:28:04 +02003188 return page;
3189}
3190
3191static struct vm_operations_struct kvm_vm_vm_ops = {
3192 .nopage = kvm_vm_nopage,
3193};
3194
3195static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
3196{
3197 vma->vm_ops = &kvm_vm_vm_ops;
3198 return 0;
3199}
3200
3201static struct file_operations kvm_vm_fops = {
3202 .release = kvm_vm_release,
3203 .unlocked_ioctl = kvm_vm_ioctl,
3204 .compat_ioctl = kvm_vm_ioctl,
3205 .mmap = kvm_vm_mmap,
3206};
3207
3208static int kvm_dev_ioctl_create_vm(void)
3209{
3210 int fd, r;
3211 struct inode *inode;
3212 struct file *file;
3213 struct kvm *kvm;
3214
Avi Kivityf17abe92007-02-21 19:28:04 +02003215 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04003216 if (IS_ERR(kvm))
3217 return PTR_ERR(kvm);
3218 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
3219 if (r) {
3220 kvm_destroy_vm(kvm);
3221 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02003222 }
3223
Avi Kivitybccf2152007-02-21 18:04:26 +02003224 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02003225
Avi Kivityf17abe92007-02-21 19:28:04 +02003226 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02003227}
3228
3229static long kvm_dev_ioctl(struct file *filp,
3230 unsigned int ioctl, unsigned long arg)
3231{
3232 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02003233 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02003234
3235 switch (ioctl) {
3236 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02003237 r = -EINVAL;
3238 if (arg)
3239 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02003240 r = KVM_API_VERSION;
3241 break;
3242 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02003243 r = -EINVAL;
3244 if (arg)
3245 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02003246 r = kvm_dev_ioctl_create_vm();
3247 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003248 case KVM_GET_MSR_INDEX_LIST: {
Al Viro2f3669872007-02-09 16:38:35 +00003249 struct kvm_msr_list __user *user_msr_list = argp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003250 struct kvm_msr_list msr_list;
3251 unsigned n;
3252
3253 r = -EFAULT;
3254 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
3255 goto out;
3256 n = msr_list.nmsrs;
Avi Kivity6f00e682007-01-26 00:56:40 -08003257 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003258 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
3259 goto out;
3260 r = -E2BIG;
Michael Riepebf591b22006-12-22 01:05:36 -08003261 if (n < num_msrs_to_save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003262 goto out;
3263 r = -EFAULT;
3264 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
Michael Riepebf591b22006-12-22 01:05:36 -08003265 num_msrs_to_save * sizeof(u32)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003266 goto out;
Avi Kivity6f00e682007-01-26 00:56:40 -08003267 if (copy_to_user(user_msr_list->indices
3268 + num_msrs_to_save * sizeof(u32),
3269 &emulated_msrs,
3270 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
3271 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003272 r = 0;
Avi Kivitycc1d8952007-01-05 16:36:58 -08003273 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003274 }
Eddie Dong85f455f2007-07-06 12:20:49 +03003275 case KVM_CHECK_EXTENSION: {
3276 int ext = (long)argp;
3277
3278 switch (ext) {
3279 case KVM_CAP_IRQCHIP:
Eddie Dongb6958ce2007-07-18 12:15:21 +03003280 case KVM_CAP_HLT:
Eddie Dong85f455f2007-07-06 12:20:49 +03003281 r = 1;
3282 break;
3283 default:
3284 r = 0;
3285 break;
3286 }
Avi Kivity5d308f42007-03-01 17:56:20 +02003287 break;
Eddie Dong85f455f2007-07-06 12:20:49 +03003288 }
Avi Kivity07c45a32007-03-07 13:05:38 +02003289 case KVM_GET_VCPU_MMAP_SIZE:
3290 r = -EINVAL;
3291 if (arg)
3292 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02003293 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02003294 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003295 default:
3296 ;
3297 }
3298out:
3299 return r;
3300}
3301
Avi Kivity6aa8b732006-12-10 02:21:36 -08003302static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003303 .unlocked_ioctl = kvm_dev_ioctl,
3304 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003305};
3306
3307static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02003308 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003309 "kvm",
3310 &kvm_chardev_ops,
3311};
3312
Avi Kivity774c47f2007-02-12 00:54:47 -08003313/*
3314 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
3315 * cached on it.
3316 */
3317static void decache_vcpus_on_cpu(int cpu)
3318{
3319 struct kvm *vm;
3320 struct kvm_vcpu *vcpu;
3321 int i;
3322
3323 spin_lock(&kvm_lock);
Shaohua Li11ec2802007-07-23 14:51:37 +08003324 list_for_each_entry(vm, &vm_list, vm_list)
Avi Kivity774c47f2007-02-12 00:54:47 -08003325 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003326 vcpu = vm->vcpus[i];
3327 if (!vcpu)
3328 continue;
Avi Kivity774c47f2007-02-12 00:54:47 -08003329 /*
3330 * If the vcpu is locked, then it is running on some
3331 * other cpu and therefore it is not cached on the
3332 * cpu in question.
3333 *
3334 * If it's not locked, check the last cpu it executed
3335 * on.
3336 */
3337 if (mutex_trylock(&vcpu->mutex)) {
3338 if (vcpu->cpu == cpu) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003339 kvm_x86_ops->vcpu_decache(vcpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08003340 vcpu->cpu = -1;
3341 }
3342 mutex_unlock(&vcpu->mutex);
3343 }
3344 }
3345 spin_unlock(&kvm_lock);
3346}
3347
Avi Kivity1b6c0162007-05-24 13:03:52 +03003348static void hardware_enable(void *junk)
3349{
3350 int cpu = raw_smp_processor_id();
3351
3352 if (cpu_isset(cpu, cpus_hardware_enabled))
3353 return;
3354 cpu_set(cpu, cpus_hardware_enabled);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003355 kvm_x86_ops->hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003356}
3357
3358static void hardware_disable(void *junk)
3359{
3360 int cpu = raw_smp_processor_id();
3361
3362 if (!cpu_isset(cpu, cpus_hardware_enabled))
3363 return;
3364 cpu_clear(cpu, cpus_hardware_enabled);
3365 decache_vcpus_on_cpu(cpu);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003366 kvm_x86_ops->hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003367}
3368
Avi Kivity774c47f2007-02-12 00:54:47 -08003369static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
3370 void *v)
3371{
3372 int cpu = (long)v;
3373
3374 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03003375 case CPU_DYING:
3376 case CPU_DYING_FROZEN:
Avi Kivity6ec8a852007-08-19 15:57:26 +03003377 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3378 cpu);
3379 hardware_disable(NULL);
3380 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08003381 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003382 case CPU_UP_CANCELED_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003383 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3384 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003385 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003386 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02003387 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003388 case CPU_ONLINE_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003389 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
3390 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003391 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003392 break;
3393 }
3394 return NOTIFY_OK;
3395}
3396
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003397static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
3398 void *v)
3399{
3400 if (val == SYS_RESTART) {
3401 /*
3402 * Some (well, at least mine) BIOSes hang on reboot if
3403 * in vmx root mode.
3404 */
3405 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
3406 on_each_cpu(hardware_disable, NULL, 0, 1);
3407 }
3408 return NOTIFY_OK;
3409}
3410
3411static struct notifier_block kvm_reboot_notifier = {
3412 .notifier_call = kvm_reboot,
3413 .priority = 0,
3414};
3415
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04003416void kvm_io_bus_init(struct kvm_io_bus *bus)
3417{
3418 memset(bus, 0, sizeof(*bus));
3419}
3420
3421void kvm_io_bus_destroy(struct kvm_io_bus *bus)
3422{
3423 int i;
3424
3425 for (i = 0; i < bus->dev_count; i++) {
3426 struct kvm_io_device *pos = bus->devs[i];
3427
3428 kvm_iodevice_destructor(pos);
3429 }
3430}
3431
3432struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
3433{
3434 int i;
3435
3436 for (i = 0; i < bus->dev_count; i++) {
3437 struct kvm_io_device *pos = bus->devs[i];
3438
3439 if (pos->in_range(pos, addr))
3440 return pos;
3441 }
3442
3443 return NULL;
3444}
3445
3446void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
3447{
3448 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
3449
3450 bus->devs[bus->dev_count++] = dev;
3451}
3452
Avi Kivity774c47f2007-02-12 00:54:47 -08003453static struct notifier_block kvm_cpu_notifier = {
3454 .notifier_call = kvm_cpu_hotplug,
3455 .priority = 20, /* must be > scheduler priority */
3456};
3457
Avi Kivity1165f5f2007-04-19 17:27:43 +03003458static u64 stat_get(void *_offset)
3459{
3460 unsigned offset = (long)_offset;
3461 u64 total = 0;
3462 struct kvm *kvm;
3463 struct kvm_vcpu *vcpu;
3464 int i;
3465
3466 spin_lock(&kvm_lock);
3467 list_for_each_entry(kvm, &vm_list, vm_list)
3468 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003469 vcpu = kvm->vcpus[i];
3470 if (vcpu)
3471 total += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03003472 }
3473 spin_unlock(&kvm_lock);
3474 return total;
3475}
3476
Rusty Russell3dea7ca2007-08-01 10:12:22 +10003477DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n");
Avi Kivity1165f5f2007-04-19 17:27:43 +03003478
Avi Kivity6aa8b732006-12-10 02:21:36 -08003479static __init void kvm_init_debug(void)
3480{
3481 struct kvm_stats_debugfs_item *p;
3482
Al Viro8b6d44c2007-02-09 16:38:40 +00003483 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003484 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03003485 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
3486 (void *)(long)p->offset,
3487 &stat_fops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003488}
3489
3490static void kvm_exit_debug(void)
3491{
3492 struct kvm_stats_debugfs_item *p;
3493
3494 for (p = debugfs_entries; p->name; ++p)
3495 debugfs_remove(p->dentry);
3496 debugfs_remove(debugfs_dir);
3497}
3498
Avi Kivity59ae6c62007-02-12 00:54:48 -08003499static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3500{
Avi Kivity4267c412007-05-24 13:09:41 +03003501 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003502 return 0;
3503}
3504
3505static int kvm_resume(struct sys_device *dev)
3506{
Avi Kivity4267c412007-05-24 13:09:41 +03003507 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003508 return 0;
3509}
3510
3511static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01003512 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08003513 .suspend = kvm_suspend,
3514 .resume = kvm_resume,
3515};
3516
3517static struct sys_device kvm_sysdev = {
3518 .id = 0,
3519 .cls = &kvm_sysdev_class,
3520};
3521
Avi Kivity6aa8b732006-12-10 02:21:36 -08003522hpa_t bad_page_address;
3523
Avi Kivity15ad7142007-07-11 18:17:21 +03003524static inline
3525struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
3526{
3527 return container_of(pn, struct kvm_vcpu, preempt_notifier);
3528}
3529
3530static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
3531{
3532 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3533
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003534 kvm_x86_ops->vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003535}
3536
3537static void kvm_sched_out(struct preempt_notifier *pn,
3538 struct task_struct *next)
3539{
3540 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3541
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003542 kvm_x86_ops->vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003543}
3544
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003545int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10003546 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003547{
3548 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003549 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003550
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003551 if (kvm_x86_ops) {
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08003552 printk(KERN_ERR "kvm: already loaded the other module\n");
3553 return -EEXIST;
3554 }
3555
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003556 if (!ops->cpu_has_kvm_support()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003557 printk(KERN_ERR "kvm: no hardware support\n");
3558 return -EOPNOTSUPP;
3559 }
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003560 if (ops->disabled_by_bios()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003561 printk(KERN_ERR "kvm: disabled by bios\n");
3562 return -EOPNOTSUPP;
3563 }
3564
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003565 kvm_x86_ops = ops;
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003566
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003567 r = kvm_x86_ops->hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003568 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02003569 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003570
Yang, Sheng002c7f72007-07-31 14:23:01 +03003571 for_each_online_cpu(cpu) {
3572 smp_call_function_single(cpu,
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003573 kvm_x86_ops->check_processor_compatibility,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003574 &r, 0, 1);
3575 if (r < 0)
3576 goto out_free_0;
3577 }
3578
Avi Kivity1b6c0162007-05-24 13:03:52 +03003579 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003580 r = register_cpu_notifier(&kvm_cpu_notifier);
3581 if (r)
3582 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003583 register_reboot_notifier(&kvm_reboot_notifier);
3584
Avi Kivity59ae6c62007-02-12 00:54:48 -08003585 r = sysdev_class_register(&kvm_sysdev_class);
3586 if (r)
3587 goto out_free_2;
3588
3589 r = sysdev_register(&kvm_sysdev);
3590 if (r)
3591 goto out_free_3;
3592
Rusty Russellc16f8622007-07-30 21:12:19 +10003593 /* A kmem cache lets us meet the alignment requirements of fx_save. */
3594 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
3595 __alignof__(struct kvm_vcpu), 0, 0);
3596 if (!kvm_vcpu_cache) {
3597 r = -ENOMEM;
3598 goto out_free_4;
3599 }
3600
Avi Kivity6aa8b732006-12-10 02:21:36 -08003601 kvm_chardev_ops.owner = module;
3602
3603 r = misc_register(&kvm_dev);
3604 if (r) {
3605 printk (KERN_ERR "kvm: misc device register failed\n");
3606 goto out_free;
3607 }
3608
Avi Kivity15ad7142007-07-11 18:17:21 +03003609 kvm_preempt_ops.sched_in = kvm_sched_in;
3610 kvm_preempt_ops.sched_out = kvm_sched_out;
3611
Avi Kivityc7addb92007-09-16 18:58:32 +02003612 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
3613
3614 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003615
3616out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10003617 kmem_cache_destroy(kvm_vcpu_cache);
3618out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08003619 sysdev_unregister(&kvm_sysdev);
3620out_free_3:
3621 sysdev_class_unregister(&kvm_sysdev_class);
3622out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003623 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08003624 unregister_cpu_notifier(&kvm_cpu_notifier);
3625out_free_1:
Avi Kivity1b6c0162007-05-24 13:03:52 +03003626 on_each_cpu(hardware_disable, NULL, 0, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03003627out_free_0:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003628 kvm_x86_ops->hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02003629out:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003630 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003631 return r;
3632}
3633
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003634void kvm_exit_x86(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003635{
3636 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10003637 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003638 sysdev_unregister(&kvm_sysdev);
3639 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003640 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003641 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003642 on_each_cpu(hardware_disable, NULL, 0, 1);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003643 kvm_x86_ops->hardware_unsetup();
3644 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003645}
3646
3647static __init int kvm_init(void)
3648{
3649 static struct page *bad_page;
Avi Kivity37e29d92007-02-20 14:07:37 +02003650 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003651
Avi Kivityb5a33a72007-04-15 16:31:09 +03003652 r = kvm_mmu_module_init();
3653 if (r)
3654 goto out4;
3655
Avi Kivity6aa8b732006-12-10 02:21:36 -08003656 kvm_init_debug();
3657
Michael Riepebf591b22006-12-22 01:05:36 -08003658 kvm_init_msr_list();
3659
Avi Kivity6aa8b732006-12-10 02:21:36 -08003660 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
3661 r = -ENOMEM;
3662 goto out;
3663 }
3664
3665 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
3666 memset(__va(bad_page_address), 0, PAGE_SIZE);
3667
Avi Kivity58e690e2007-02-26 16:29:43 +02003668 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003669
3670out:
3671 kvm_exit_debug();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003672 kvm_mmu_module_exit();
3673out4:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003674 return r;
3675}
3676
3677static __exit void kvm_exit(void)
3678{
3679 kvm_exit_debug();
3680 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
Avi Kivityb5a33a72007-04-15 16:31:09 +03003681 kvm_mmu_module_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003682}
3683
3684module_init(kvm_init)
3685module_exit(kvm_exit)
3686
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003687EXPORT_SYMBOL_GPL(kvm_init_x86);
3688EXPORT_SYMBOL_GPL(kvm_exit_x86);