blob: 1c662f63b7a9cf50f7a6a51e1929643c474bc3a4 [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 }
312
313 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
314 vfree(free->dirty_bitmap);
315
Al Viro8b6d44c2007-02-09 16:38:40 +0000316 free->phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800317 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000318 free->dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800319}
320
321static void kvm_free_physmem(struct kvm *kvm)
322{
323 int i;
324
325 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000326 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800327}
328
Avi Kivity039576c2007-03-20 12:46:50 +0200329static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
330{
331 int i;
332
Rusty Russell3077c4512007-07-30 16:41:57 +1000333 for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i)
Avi Kivity039576c2007-03-20 12:46:50 +0200334 if (vcpu->pio.guest_pages[i]) {
335 __free_page(vcpu->pio.guest_pages[i]);
336 vcpu->pio.guest_pages[i] = NULL;
337 }
338}
339
Avi Kivity7b53aa52007-06-05 12:17:03 +0300340static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
341{
Avi Kivity7b53aa52007-06-05 12:17:03 +0300342 vcpu_load(vcpu);
343 kvm_mmu_unload(vcpu);
344 vcpu_put(vcpu);
345}
346
Avi Kivity6aa8b732006-12-10 02:21:36 -0800347static void kvm_free_vcpus(struct kvm *kvm)
348{
349 unsigned int i;
350
Avi Kivity7b53aa52007-06-05 12:17:03 +0300351 /*
352 * Unpin any mmu pages first.
353 */
354 for (i = 0; i < KVM_MAX_VCPUS; ++i)
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000355 if (kvm->vcpus[i])
356 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
357 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
358 if (kvm->vcpus[i]) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300359 kvm_x86_ops->vcpu_free(kvm->vcpus[i]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000360 kvm->vcpus[i] = NULL;
361 }
362 }
363
Avi Kivity6aa8b732006-12-10 02:21:36 -0800364}
365
Avi Kivityf17abe92007-02-21 19:28:04 +0200366static void kvm_destroy_vm(struct kvm *kvm)
367{
Avi Kivity133de902007-02-12 00:54:44 -0800368 spin_lock(&kvm_lock);
369 list_del(&kvm->vm_list);
370 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300371 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400372 kvm_io_bus_destroy(&kvm->mmio_bus);
Eddie Dong85f455f2007-07-06 12:20:49 +0300373 kfree(kvm->vpic);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300374 kfree(kvm->vioapic);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800375 kvm_free_vcpus(kvm);
376 kvm_free_physmem(kvm);
377 kfree(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200378}
379
380static int kvm_vm_release(struct inode *inode, struct file *filp)
381{
382 struct kvm *kvm = filp->private_data;
383
384 kvm_destroy_vm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800385 return 0;
386}
387
388static void inject_gp(struct kvm_vcpu *vcpu)
389{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300390 kvm_x86_ops->inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800391}
392
Avi Kivity1342d352007-01-05 16:36:39 -0800393/*
394 * Load the pae pdptrs. Return true is they are all valid.
395 */
396static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800397{
398 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
Avi Kivity1342d352007-01-05 16:36:39 -0800399 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800400 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800401 u64 *pdpt;
Avi Kivity1342d352007-01-05 16:36:39 -0800402 int ret;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300403 struct page *page;
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);
Avi Kivity954bbbc2007-03-30 14:02:32 +0300407 page = gfn_to_page(vcpu->kvm, pdpt_gfn);
Rusty Russellc820c2a2007-07-25 13:29:51 +1000408 if (!page) {
409 ret = 0;
410 goto out;
411 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800412
Rusty Russellc820c2a2007-07-25 13:29:51 +1000413 pdpt = kmap_atomic(page, KM_USER0);
414 memcpy(pdpte, pdpt+offset, sizeof(pdpte));
415 kunmap_atomic(pdpt, KM_USER0);
416
417 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
418 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
Avi Kivity1342d352007-01-05 16:36:39 -0800419 ret = 0;
420 goto out;
421 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800422 }
Rusty Russellc820c2a2007-07-25 13:29:51 +1000423 ret = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424
Rusty Russellc820c2a2007-07-25 13:29:51 +1000425 memcpy(vcpu->pdptrs, pdpte, sizeof(vcpu->pdptrs));
Avi Kivity1342d352007-01-05 16:36:39 -0800426out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800427 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800428
Avi Kivity1342d352007-01-05 16:36:39 -0800429 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800430}
431
432void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
433{
Rusty Russell707d92fa2007-07-17 23:19:08 +1000434 if (cr0 & CR0_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800435 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
436 cr0, vcpu->cr0);
437 inject_gp(vcpu);
438 return;
439 }
440
Rusty Russell707d92fa2007-07-17 23:19:08 +1000441 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800442 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
443 inject_gp(vcpu);
444 return;
445 }
446
Rusty Russell707d92fa2007-07-17 23:19:08 +1000447 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800448 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
449 "and a clear PE flag\n");
450 inject_gp(vcpu);
451 return;
452 }
453
Rusty Russell707d92fa2007-07-17 23:19:08 +1000454 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800455#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800456 if ((vcpu->shadow_efer & EFER_LME)) {
457 int cs_db, cs_l;
458
459 if (!is_pae(vcpu)) {
460 printk(KERN_DEBUG "set_cr0: #GP, start paging "
461 "in long mode while PAE is disabled\n");
462 inject_gp(vcpu);
463 return;
464 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300465 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800466 if (cs_l) {
467 printk(KERN_DEBUG "set_cr0: #GP, start paging "
468 "in long mode while CS.L == 1\n");
469 inject_gp(vcpu);
470 return;
471
472 }
473 } else
474#endif
Avi Kivity1342d352007-01-05 16:36:39 -0800475 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800476 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
477 "reserved bits\n");
478 inject_gp(vcpu);
479 return;
480 }
481
482 }
483
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300484 kvm_x86_ops->set_cr0(vcpu, cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800485 vcpu->cr0 = cr0;
486
Shaohua Li11ec2802007-07-23 14:51:37 +0800487 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800488 kvm_mmu_reset_context(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800489 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800490 return;
491}
492EXPORT_SYMBOL_GPL(set_cr0);
493
494void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
495{
496 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
497}
498EXPORT_SYMBOL_GPL(lmsw);
499
500void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
501{
Rusty Russell66aee912007-07-17 23:34:16 +1000502 if (cr4 & CR4_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800503 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
504 inject_gp(vcpu);
505 return;
506 }
507
Avi Kivitya9058ec2006-12-29 16:49:37 -0800508 if (is_long_mode(vcpu)) {
Rusty Russell66aee912007-07-17 23:34:16 +1000509 if (!(cr4 & X86_CR4_PAE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800510 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
511 "in long mode\n");
512 inject_gp(vcpu);
513 return;
514 }
Rusty Russell66aee912007-07-17 23:34:16 +1000515 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
Avi Kivity1342d352007-01-05 16:36:39 -0800516 && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800517 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
518 inject_gp(vcpu);
Rusty Russell310bc762007-07-23 17:11:02 +1000519 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800520 }
521
Rusty Russell66aee912007-07-17 23:34:16 +1000522 if (cr4 & X86_CR4_VMXE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800523 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
524 inject_gp(vcpu);
525 return;
526 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300527 kvm_x86_ops->set_cr4(vcpu, cr4);
Rusty Russell81f50e32007-09-06 01:20:38 +1000528 vcpu->cr4 = cr4;
Shaohua Li11ec2802007-07-23 14:51:37 +0800529 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800530 kvm_mmu_reset_context(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800531 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800532}
533EXPORT_SYMBOL_GPL(set_cr4);
534
535void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
536{
Avi Kivitya9058ec2006-12-29 16:49:37 -0800537 if (is_long_mode(vcpu)) {
Rusty Russellf802a302007-07-17 23:32:55 +1000538 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800539 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
540 inject_gp(vcpu);
541 return;
542 }
543 } else {
Rusty Russellf802a302007-07-17 23:32:55 +1000544 if (is_pae(vcpu)) {
545 if (cr3 & CR3_PAE_RESERVED_BITS) {
546 printk(KERN_DEBUG
547 "set_cr3: #GP, reserved bits\n");
548 inject_gp(vcpu);
549 return;
550 }
551 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
552 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
553 "reserved bits\n");
554 inject_gp(vcpu);
555 return;
556 }
557 } else {
558 if (cr3 & CR3_NONPAE_RESERVED_BITS) {
559 printk(KERN_DEBUG
560 "set_cr3: #GP, reserved bits\n");
561 inject_gp(vcpu);
562 return;
563 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800564 }
565 }
566
Shaohua Li11ec2802007-07-23 14:51:37 +0800567 mutex_lock(&vcpu->kvm->lock);
Ingo Molnard21225e2007-01-05 16:36:59 -0800568 /*
569 * Does the new cr3 value map to physical memory? (Note, we
570 * catch an invalid cr3 even in real-mode, because it would
571 * cause trouble later on when we turn on paging anyway.)
572 *
573 * A real CPU would silently accept an invalid cr3 and would
574 * attempt to use it - with largely undefined (and often hard
575 * to debug) behavior on the guest side.
576 */
577 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
578 inject_gp(vcpu);
Rusty Russellfb764412007-07-31 20:45:03 +1000579 else {
580 vcpu->cr3 = cr3;
Ingo Molnard21225e2007-01-05 16:36:59 -0800581 vcpu->mmu.new_cr3(vcpu);
Rusty Russellfb764412007-07-31 20:45:03 +1000582 }
Shaohua Li11ec2802007-07-23 14:51:37 +0800583 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800584}
585EXPORT_SYMBOL_GPL(set_cr3);
586
587void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
588{
Rusty Russell7075bc82007-07-17 23:37:17 +1000589 if (cr8 & CR8_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800590 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
591 inject_gp(vcpu);
592 return;
593 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300594 if (irqchip_in_kernel(vcpu->kvm))
595 kvm_lapic_set_tpr(vcpu, cr8);
596 else
597 vcpu->cr8 = cr8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800598}
599EXPORT_SYMBOL_GPL(set_cr8);
600
Eddie Dong7017fc32007-07-18 11:34:57 +0300601unsigned long get_cr8(struct kvm_vcpu *vcpu)
602{
Eddie Dong97222cc2007-09-12 10:58:04 +0300603 if (irqchip_in_kernel(vcpu->kvm))
604 return kvm_lapic_get_cr8(vcpu);
605 else
606 return vcpu->cr8;
Eddie Dong7017fc32007-07-18 11:34:57 +0300607}
608EXPORT_SYMBOL_GPL(get_cr8);
609
610u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
611{
Eddie Dong97222cc2007-09-12 10:58:04 +0300612 if (irqchip_in_kernel(vcpu->kvm))
613 return vcpu->apic_base;
614 else
615 return vcpu->apic_base;
Eddie Dong7017fc32007-07-18 11:34:57 +0300616}
617EXPORT_SYMBOL_GPL(kvm_get_apic_base);
618
619void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
620{
Eddie Dong97222cc2007-09-12 10:58:04 +0300621 /* TODO: reserve bits check */
622 if (irqchip_in_kernel(vcpu->kvm))
623 kvm_lapic_set_base(vcpu, data);
624 else
625 vcpu->apic_base = data;
Eddie Dong7017fc32007-07-18 11:34:57 +0300626}
627EXPORT_SYMBOL_GPL(kvm_set_apic_base);
628
Avi Kivity6aa8b732006-12-10 02:21:36 -0800629void fx_init(struct kvm_vcpu *vcpu)
630{
Rusty Russellb114b082007-07-30 21:13:43 +1000631 unsigned after_mxcsr_mask;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800632
Rusty Russell9bd01502007-07-30 16:29:56 +1000633 /* Initialize guest FPU by resetting ours and saving into guest's */
634 preempt_disable();
Rusty Russellb114b082007-07-30 21:13:43 +1000635 fx_save(&vcpu->host_fx_image);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800636 fpu_init();
Rusty Russellb114b082007-07-30 21:13:43 +1000637 fx_save(&vcpu->guest_fx_image);
638 fx_restore(&vcpu->host_fx_image);
Rusty Russell9bd01502007-07-30 16:29:56 +1000639 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800640
Amit Shah380102c2007-08-25 11:35:52 +0300641 vcpu->cr0 |= X86_CR0_ET;
Rusty Russellb114b082007-07-30 21:13:43 +1000642 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
643 vcpu->guest_fx_image.mxcsr = 0x1f80;
644 memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask,
645 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800646}
647EXPORT_SYMBOL_GPL(fx_init);
648
649/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800650 * Allocate some memory and give it an address in the guest physical address
651 * space.
652 *
653 * Discontiguous memory is allowed, mostly for framebuffers.
654 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200655static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
656 struct kvm_memory_region *mem)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800657{
658 int r;
659 gfn_t base_gfn;
660 unsigned long npages;
661 unsigned long i;
662 struct kvm_memory_slot *memslot;
663 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800664
665 r = -EINVAL;
666 /* General sanity checks */
667 if (mem->memory_size & (PAGE_SIZE - 1))
668 goto out;
669 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
670 goto out;
671 if (mem->slot >= KVM_MEMORY_SLOTS)
672 goto out;
673 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
674 goto out;
675
676 memslot = &kvm->memslots[mem->slot];
677 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
678 npages = mem->memory_size >> PAGE_SHIFT;
679
680 if (!npages)
681 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
682
Shaohua Li11ec2802007-07-23 14:51:37 +0800683 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800684
Avi Kivity6aa8b732006-12-10 02:21:36 -0800685 new = old = *memslot;
686
687 new.base_gfn = base_gfn;
688 new.npages = npages;
689 new.flags = mem->flags;
690
691 /* Disallow changing a memory slot's size. */
692 r = -EINVAL;
693 if (npages && old.npages && npages != old.npages)
694 goto out_unlock;
695
696 /* Check for overlaps */
697 r = -EEXIST;
698 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
699 struct kvm_memory_slot *s = &kvm->memslots[i];
700
701 if (s == memslot)
702 continue;
703 if (!((base_gfn + npages <= s->base_gfn) ||
704 (base_gfn >= s->base_gfn + s->npages)))
705 goto out_unlock;
706 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800707
708 /* Deallocate if slot is being removed */
709 if (!npages)
Al Viro8b6d44c2007-02-09 16:38:40 +0000710 new.phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800711
712 /* Free page dirty bitmap if unneeded */
713 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000714 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800715
716 r = -ENOMEM;
717
718 /* Allocate if a slot is being created */
719 if (npages && !new.phys_mem) {
720 new.phys_mem = vmalloc(npages * sizeof(struct page *));
721
722 if (!new.phys_mem)
Laurent Vivier0d8d2bd2007-08-30 14:56:21 +0200723 goto out_unlock;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800724
725 memset(new.phys_mem, 0, npages * sizeof(struct page *));
726 for (i = 0; i < npages; ++i) {
727 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
728 | __GFP_ZERO);
729 if (!new.phys_mem[i])
Laurent Vivier0d8d2bd2007-08-30 14:56:21 +0200730 goto out_unlock;
Markus Rechberger5972e952007-02-19 14:37:47 +0200731 set_page_private(new.phys_mem[i],0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800732 }
733 }
734
735 /* Allocate page dirty bitmap if needed */
736 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
737 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
738
739 new.dirty_bitmap = vmalloc(dirty_bytes);
740 if (!new.dirty_bitmap)
Laurent Vivier0d8d2bd2007-08-30 14:56:21 +0200741 goto out_unlock;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800742 memset(new.dirty_bitmap, 0, dirty_bytes);
743 }
744
Avi Kivity6aa8b732006-12-10 02:21:36 -0800745 if (mem->slot >= kvm->nmemslots)
746 kvm->nmemslots = mem->slot + 1;
747
748 *memslot = new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800749
Avi Kivity90cb0522007-07-17 13:04:56 +0300750 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
751 kvm_flush_remote_tlbs(kvm);
752
Shaohua Li11ec2802007-07-23 14:51:37 +0800753 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800754
Avi Kivity6aa8b732006-12-10 02:21:36 -0800755 kvm_free_physmem_slot(&old, &new);
756 return 0;
757
758out_unlock:
Shaohua Li11ec2802007-07-23 14:51:37 +0800759 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800760 kvm_free_physmem_slot(&new, &old);
761out:
762 return r;
763}
764
765/*
766 * Get (and clear) the dirty memory log for a memory slot.
767 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200768static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
769 struct kvm_dirty_log *log)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800770{
771 struct kvm_memory_slot *memslot;
772 int r, i;
773 int n;
774 unsigned long any = 0;
775
Shaohua Li11ec2802007-07-23 14:51:37 +0800776 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777
Avi Kivity6aa8b732006-12-10 02:21:36 -0800778 r = -EINVAL;
779 if (log->slot >= KVM_MEMORY_SLOTS)
780 goto out;
781
782 memslot = &kvm->memslots[log->slot];
783 r = -ENOENT;
784 if (!memslot->dirty_bitmap)
785 goto out;
786
Uri Lublincd1a4a92007-02-22 16:43:09 +0200787 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800788
Uri Lublincd1a4a92007-02-22 16:43:09 +0200789 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790 any = memslot->dirty_bitmap[i];
791
792 r = -EFAULT;
793 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
794 goto out;
795
Rusty Russell39214912007-07-31 19:57:47 +1000796 /* If nothing is dirty, don't bother messing with page tables. */
797 if (any) {
Rusty Russell39214912007-07-31 19:57:47 +1000798 kvm_mmu_slot_remove_write_access(kvm, log->slot);
799 kvm_flush_remote_tlbs(kvm);
800 memset(memslot->dirty_bitmap, 0, n);
Rusty Russell39214912007-07-31 19:57:47 +1000801 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800802
803 r = 0;
804
805out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800806 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800807 return r;
808}
809
Avi Kivitye8207542007-03-30 16:54:30 +0300810/*
811 * Set a new alias region. Aliases map a portion of physical memory into
812 * another portion. This is useful for memory windows, for example the PC
813 * VGA region.
814 */
815static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
816 struct kvm_memory_alias *alias)
817{
818 int r, n;
819 struct kvm_mem_alias *p;
820
821 r = -EINVAL;
822 /* General sanity checks */
823 if (alias->memory_size & (PAGE_SIZE - 1))
824 goto out;
825 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
826 goto out;
827 if (alias->slot >= KVM_ALIAS_SLOTS)
828 goto out;
829 if (alias->guest_phys_addr + alias->memory_size
830 < alias->guest_phys_addr)
831 goto out;
832 if (alias->target_phys_addr + alias->memory_size
833 < alias->target_phys_addr)
834 goto out;
835
Shaohua Li11ec2802007-07-23 14:51:37 +0800836 mutex_lock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300837
838 p = &kvm->aliases[alias->slot];
839 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
840 p->npages = alias->memory_size >> PAGE_SHIFT;
841 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
842
843 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
844 if (kvm->aliases[n - 1].npages)
845 break;
846 kvm->naliases = n;
847
Avi Kivity90cb0522007-07-17 13:04:56 +0300848 kvm_mmu_zap_all(kvm);
Avi Kivitye8207542007-03-30 16:54:30 +0300849
Shaohua Li11ec2802007-07-23 14:51:37 +0800850 mutex_unlock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300851
852 return 0;
853
854out:
855 return r;
856}
857
He, Qing6ceb9d72007-07-26 11:05:18 +0300858static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
859{
860 int r;
861
862 r = 0;
863 switch (chip->chip_id) {
864 case KVM_IRQCHIP_PIC_MASTER:
865 memcpy (&chip->chip.pic,
866 &pic_irqchip(kvm)->pics[0],
867 sizeof(struct kvm_pic_state));
868 break;
869 case KVM_IRQCHIP_PIC_SLAVE:
870 memcpy (&chip->chip.pic,
871 &pic_irqchip(kvm)->pics[1],
872 sizeof(struct kvm_pic_state));
873 break;
He, Qing6bf9e962007-08-05 10:49:16 +0300874 case KVM_IRQCHIP_IOAPIC:
875 memcpy (&chip->chip.ioapic,
876 ioapic_irqchip(kvm),
877 sizeof(struct kvm_ioapic_state));
878 break;
He, Qing6ceb9d72007-07-26 11:05:18 +0300879 default:
880 r = -EINVAL;
881 break;
882 }
883 return r;
884}
885
886static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
887{
888 int r;
889
890 r = 0;
891 switch (chip->chip_id) {
892 case KVM_IRQCHIP_PIC_MASTER:
893 memcpy (&pic_irqchip(kvm)->pics[0],
894 &chip->chip.pic,
895 sizeof(struct kvm_pic_state));
896 break;
897 case KVM_IRQCHIP_PIC_SLAVE:
898 memcpy (&pic_irqchip(kvm)->pics[1],
899 &chip->chip.pic,
900 sizeof(struct kvm_pic_state));
901 break;
He, Qing6bf9e962007-08-05 10:49:16 +0300902 case KVM_IRQCHIP_IOAPIC:
903 memcpy (ioapic_irqchip(kvm),
904 &chip->chip.ioapic,
905 sizeof(struct kvm_ioapic_state));
906 break;
He, Qing6ceb9d72007-07-26 11:05:18 +0300907 default:
908 r = -EINVAL;
909 break;
910 }
911 kvm_pic_update_irq(pic_irqchip(kvm));
912 return r;
913}
914
Avi Kivitye8207542007-03-30 16:54:30 +0300915static gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
916{
917 int i;
918 struct kvm_mem_alias *alias;
919
920 for (i = 0; i < kvm->naliases; ++i) {
921 alias = &kvm->aliases[i];
922 if (gfn >= alias->base_gfn
923 && gfn < alias->base_gfn + alias->npages)
924 return alias->target_gfn + gfn - alias->base_gfn;
925 }
926 return gfn;
927}
928
929static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800930{
931 int i;
932
933 for (i = 0; i < kvm->nmemslots; ++i) {
934 struct kvm_memory_slot *memslot = &kvm->memslots[i];
935
936 if (gfn >= memslot->base_gfn
937 && gfn < memslot->base_gfn + memslot->npages)
938 return memslot;
939 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000940 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800941}
Avi Kivitye8207542007-03-30 16:54:30 +0300942
943struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
944{
945 gfn = unalias_gfn(kvm, gfn);
946 return __gfn_to_memslot(kvm, gfn);
947}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800948
Avi Kivity954bbbc2007-03-30 14:02:32 +0300949struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
950{
951 struct kvm_memory_slot *slot;
952
Avi Kivitye8207542007-03-30 16:54:30 +0300953 gfn = unalias_gfn(kvm, gfn);
954 slot = __gfn_to_memslot(kvm, gfn);
Avi Kivity954bbbc2007-03-30 14:02:32 +0300955 if (!slot)
956 return NULL;
957 return slot->phys_mem[gfn - slot->base_gfn];
958}
959EXPORT_SYMBOL_GPL(gfn_to_page);
960
Rusty Russell7e9d6192007-07-31 20:41:14 +1000961/* WARNING: Does not work on aliased pages. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
963{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +0300964 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965
Rusty Russell7e9d6192007-07-31 20:41:14 +1000966 memslot = __gfn_to_memslot(kvm, gfn);
967 if (memslot && memslot->dirty_bitmap) {
968 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969
Rusty Russell7e9d6192007-07-31 20:41:14 +1000970 /* avoid RMW */
971 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
972 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800973 }
974}
975
Laurent Viviere7d5d762007-07-30 13:41:19 +0300976int emulator_read_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +0300977 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800978 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +0300979 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800980{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800981 void *data = val;
982
983 while (bytes) {
984 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
985 unsigned offset = addr & (PAGE_SIZE-1);
986 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
987 unsigned long pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300988 struct page *page;
989 void *page_virt;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800990
991 if (gpa == UNMAPPED_GVA)
992 return X86EMUL_PROPAGATE_FAULT;
993 pfn = gpa >> PAGE_SHIFT;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300994 page = gfn_to_page(vcpu->kvm, pfn);
995 if (!page)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800996 return X86EMUL_UNHANDLEABLE;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300997 page_virt = kmap_atomic(page, KM_USER0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998
Avi Kivity954bbbc2007-03-30 14:02:32 +0300999 memcpy(data, page_virt + offset, tocopy);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001000
Avi Kivity954bbbc2007-03-30 14:02:32 +03001001 kunmap_atomic(page_virt, KM_USER0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001002
1003 bytes -= tocopy;
1004 data += tocopy;
1005 addr += tocopy;
1006 }
1007
1008 return X86EMUL_CONTINUE;
1009}
Laurent Viviere7d5d762007-07-30 13:41:19 +03001010EXPORT_SYMBOL_GPL(emulator_read_std);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001011
1012static int emulator_write_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001013 const void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001015 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001016{
Rusty Russellf0242472007-08-01 10:48:02 +10001017 pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001018 return X86EMUL_UNHANDLEABLE;
1019}
1020
Eddie Dong97222cc2007-09-12 10:58:04 +03001021/*
1022 * Only apic need an MMIO device hook, so shortcut now..
1023 */
1024static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
1025 gpa_t addr)
1026{
1027 struct kvm_io_device *dev;
1028
1029 if (vcpu->apic) {
1030 dev = &vcpu->apic->dev;
1031 if (dev->in_range(dev, addr))
1032 return dev;
1033 }
1034 return NULL;
1035}
1036
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001037static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1038 gpa_t addr)
1039{
Eddie Dong97222cc2007-09-12 10:58:04 +03001040 struct kvm_io_device *dev;
1041
1042 dev = vcpu_find_pervcpu_dev(vcpu, addr);
1043 if (dev == NULL)
1044 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1045 return dev;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001046}
1047
Eddie Dong74906342007-06-19 18:05:03 +03001048static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
1049 gpa_t addr)
1050{
1051 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
1052}
1053
Avi Kivity6aa8b732006-12-10 02:21:36 -08001054static int emulator_read_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001055 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001056 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001057 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001058{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001059 struct kvm_io_device *mmio_dev;
1060 gpa_t gpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001061
1062 if (vcpu->mmio_read_completed) {
1063 memcpy(val, vcpu->mmio_data, bytes);
1064 vcpu->mmio_read_completed = 0;
1065 return X86EMUL_CONTINUE;
Laurent Viviercebff022007-07-30 13:35:24 +03001066 } else if (emulator_read_std(addr, val, bytes, vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001067 == X86EMUL_CONTINUE)
1068 return X86EMUL_CONTINUE;
Avi Kivityd27d4ac2007-02-19 14:37:46 +02001069
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001070 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1071 if (gpa == UNMAPPED_GVA)
1072 return X86EMUL_PROPAGATE_FAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001073
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001074 /*
1075 * Is this MMIO handled locally?
1076 */
1077 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1078 if (mmio_dev) {
1079 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1080 return X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001081 }
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001082
1083 vcpu->mmio_needed = 1;
1084 vcpu->mmio_phys_addr = gpa;
1085 vcpu->mmio_size = bytes;
1086 vcpu->mmio_is_write = 0;
1087
1088 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089}
1090
Avi Kivityda4a00f2007-01-05 16:36:44 -08001091static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity4c690a12007-04-22 15:28:19 +03001092 const void *val, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001093{
Avi Kivityda4a00f2007-01-05 16:36:44 -08001094 struct page *page;
1095 void *virt;
1096
1097 if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
1098 return 0;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001099 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1100 if (!page)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001101 return 0;
Uri Lublinab51a432007-02-21 18:25:21 +02001102 mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001103 virt = kmap_atomic(page, KM_USER0);
Shaohua Life551882007-07-23 14:51:39 +08001104 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
Avi Kivity7cfa4b02007-07-23 18:33:14 +03001105 memcpy(virt + offset_in_page(gpa), val, bytes);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001106 kunmap_atomic(virt, KM_USER0);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001107 return 1;
1108}
1109
Avi Kivityb0fcd902007-07-22 18:48:54 +03001110static int emulator_write_emulated_onepage(unsigned long addr,
1111 const void *val,
1112 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001113 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001114{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001115 struct kvm_io_device *mmio_dev;
1116 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001117
Avi Kivityc9047f52007-04-17 10:53:22 +03001118 if (gpa == UNMAPPED_GVA) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001119 kvm_x86_ops->inject_page_fault(vcpu, addr, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001120 return X86EMUL_PROPAGATE_FAULT;
Avi Kivityc9047f52007-04-17 10:53:22 +03001121 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001122
Avi Kivityda4a00f2007-01-05 16:36:44 -08001123 if (emulator_write_phys(vcpu, gpa, val, bytes))
1124 return X86EMUL_CONTINUE;
1125
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001126 /*
1127 * Is this MMIO handled locally?
1128 */
1129 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1130 if (mmio_dev) {
1131 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1132 return X86EMUL_CONTINUE;
1133 }
1134
Avi Kivity6aa8b732006-12-10 02:21:36 -08001135 vcpu->mmio_needed = 1;
1136 vcpu->mmio_phys_addr = gpa;
1137 vcpu->mmio_size = bytes;
1138 vcpu->mmio_is_write = 1;
Avi Kivity4c690a12007-04-22 15:28:19 +03001139 memcpy(vcpu->mmio_data, val, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001140
1141 return X86EMUL_CONTINUE;
1142}
1143
Laurent Viviere7d5d762007-07-30 13:41:19 +03001144int emulator_write_emulated(unsigned long addr,
Avi Kivityb0fcd902007-07-22 18:48:54 +03001145 const void *val,
1146 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001147 struct kvm_vcpu *vcpu)
Avi Kivityb0fcd902007-07-22 18:48:54 +03001148{
1149 /* Crossing a page boundary? */
1150 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1151 int rc, now;
1152
1153 now = -addr & ~PAGE_MASK;
Laurent Viviercebff022007-07-30 13:35:24 +03001154 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001155 if (rc != X86EMUL_CONTINUE)
1156 return rc;
1157 addr += now;
1158 val += now;
1159 bytes -= now;
1160 }
Laurent Viviercebff022007-07-30 13:35:24 +03001161 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001162}
Laurent Viviere7d5d762007-07-30 13:41:19 +03001163EXPORT_SYMBOL_GPL(emulator_write_emulated);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001164
Avi Kivity6aa8b732006-12-10 02:21:36 -08001165static int emulator_cmpxchg_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001166 const void *old,
1167 const void *new,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001168 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001169 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001170{
1171 static int reported;
1172
1173 if (!reported) {
1174 reported = 1;
1175 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1176 }
Laurent Viviercebff022007-07-30 13:35:24 +03001177 return emulator_write_emulated(addr, new, bytes, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001178}
1179
1180static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1181{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001182 return kvm_x86_ops->get_segment_base(vcpu, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001183}
1184
1185int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1186{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001187 return X86EMUL_CONTINUE;
1188}
1189
1190int emulate_clts(struct kvm_vcpu *vcpu)
1191{
Amit Shah404fb882007-11-19 17:57:35 +02001192 kvm_x86_ops->set_cr0(vcpu, vcpu->cr0 & ~X86_CR0_TS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001193 return X86EMUL_CONTINUE;
1194}
1195
1196int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1197{
1198 struct kvm_vcpu *vcpu = ctxt->vcpu;
1199
1200 switch (dr) {
1201 case 0 ... 3:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001202 *dest = kvm_x86_ops->get_dr(vcpu, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001203 return X86EMUL_CONTINUE;
1204 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001205 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001206 return X86EMUL_UNHANDLEABLE;
1207 }
1208}
1209
1210int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1211{
1212 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1213 int exception;
1214
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001215 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001216 if (exception) {
1217 /* FIXME: better handling */
1218 return X86EMUL_UNHANDLEABLE;
1219 }
1220 return X86EMUL_CONTINUE;
1221}
1222
Avi Kivity054b1362007-09-12 13:21:09 +03001223void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001224{
1225 static int reported;
1226 u8 opcodes[4];
Avi Kivity054b1362007-09-12 13:21:09 +03001227 unsigned long rip = vcpu->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001228 unsigned long rip_linear;
1229
Avi Kivity054b1362007-09-12 13:21:09 +03001230 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001231
1232 if (reported)
1233 return;
1234
Avi Kivity054b1362007-09-12 13:21:09 +03001235 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001236
Avi Kivity054b1362007-09-12 13:21:09 +03001237 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1238 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001239 reported = 1;
1240}
Avi Kivity054b1362007-09-12 13:21:09 +03001241EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001242
1243struct x86_emulate_ops emulate_ops = {
1244 .read_std = emulator_read_std,
1245 .write_std = emulator_write_std,
1246 .read_emulated = emulator_read_emulated,
1247 .write_emulated = emulator_write_emulated,
1248 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1249};
1250
1251int emulate_instruction(struct kvm_vcpu *vcpu,
1252 struct kvm_run *run,
1253 unsigned long cr2,
1254 u16 error_code)
1255{
1256 struct x86_emulate_ctxt emulate_ctxt;
1257 int r;
1258 int cs_db, cs_l;
1259
Avi Kivitye7df56e2007-03-14 15:54:54 +02001260 vcpu->mmio_fault_cr2 = cr2;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001261 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001262
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001263 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001264
1265 emulate_ctxt.vcpu = vcpu;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001266 emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001267 emulate_ctxt.cr2 = cr2;
1268 emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
1269 ? X86EMUL_MODE_REAL : cs_l
1270 ? X86EMUL_MODE_PROT64 : cs_db
1271 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1272
1273 if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1274 emulate_ctxt.cs_base = 0;
1275 emulate_ctxt.ds_base = 0;
1276 emulate_ctxt.es_base = 0;
1277 emulate_ctxt.ss_base = 0;
1278 } else {
1279 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1280 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1281 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1282 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1283 }
1284
1285 emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1286 emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1287
1288 vcpu->mmio_is_write = 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03001289 vcpu->pio.string = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001290 r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
Laurent Viviere70669a2007-08-05 10:36:40 +03001291 if (vcpu->pio.string)
1292 return EMULATE_DO_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001293
1294 if ((r || vcpu->mmio_is_write) && run) {
Jeff Dike8fc0d082007-07-17 12:26:59 -04001295 run->exit_reason = KVM_EXIT_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001296 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1297 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1298 run->mmio.len = vcpu->mmio_size;
1299 run->mmio.is_write = vcpu->mmio_is_write;
1300 }
1301
1302 if (r) {
Avi Kivitya4360362007-01-05 16:36:45 -08001303 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1304 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001305 if (!vcpu->mmio_needed) {
Avi Kivity054b1362007-09-12 13:21:09 +03001306 kvm_report_emulation_failure(vcpu, "mmio");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001307 return EMULATE_FAIL;
1308 }
1309 return EMULATE_DO_MMIO;
1310 }
1311
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001312 kvm_x86_ops->decache_regs(vcpu);
1313 kvm_x86_ops->set_rflags(vcpu, emulate_ctxt.eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001314
Avi Kivity02c83202007-04-29 15:02:17 +03001315 if (vcpu->mmio_is_write) {
1316 vcpu->mmio_needed = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001317 return EMULATE_DO_MMIO;
Avi Kivity02c83202007-04-29 15:02:17 +03001318 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001319
1320 return EMULATE_DONE;
1321}
1322EXPORT_SYMBOL_GPL(emulate_instruction);
1323
Eddie Dongb6958ce2007-07-18 12:15:21 +03001324/*
1325 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1326 */
He, Qingc5ec1532007-09-03 17:07:41 +03001327static void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001328{
1329 DECLARE_WAITQUEUE(wait, current);
1330
1331 add_wait_queue(&vcpu->wq, &wait);
1332
1333 /*
1334 * We will block until either an interrupt or a signal wakes us up
1335 */
He, Qingc5ec1532007-09-03 17:07:41 +03001336 while (!kvm_cpu_has_interrupt(vcpu)
1337 && !signal_pending(current)
1338 && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE
1339 && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) {
Eddie Dongb6958ce2007-07-18 12:15:21 +03001340 set_current_state(TASK_INTERRUPTIBLE);
1341 vcpu_put(vcpu);
1342 schedule();
1343 vcpu_load(vcpu);
1344 }
1345
He, Qingc5ec1532007-09-03 17:07:41 +03001346 __set_current_state(TASK_RUNNING);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001347 remove_wait_queue(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001348}
1349
Avi Kivityd3bef152007-06-05 15:53:05 +03001350int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1351{
Avi Kivityd3bef152007-06-05 15:53:05 +03001352 ++vcpu->stat.halt_exits;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001353 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc5ec1532007-09-03 17:07:41 +03001354 vcpu->mp_state = VCPU_MP_STATE_HALTED;
1355 kvm_vcpu_block(vcpu);
1356 if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE)
1357 return -EINTR;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001358 return 1;
1359 } else {
1360 vcpu->run->exit_reason = KVM_EXIT_HLT;
1361 return 0;
1362 }
Avi Kivityd3bef152007-06-05 15:53:05 +03001363}
1364EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1365
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001366int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
Avi Kivity270fd9b2007-02-19 14:37:47 +02001367{
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001368 unsigned long nr, a0, a1, a2, a3, ret;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001369
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001370 kvm_x86_ops->cache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001371
1372 nr = vcpu->regs[VCPU_REGS_RAX];
1373 a0 = vcpu->regs[VCPU_REGS_RBX];
1374 a1 = vcpu->regs[VCPU_REGS_RCX];
1375 a2 = vcpu->regs[VCPU_REGS_RDX];
1376 a3 = vcpu->regs[VCPU_REGS_RSI];
1377
1378 if (!is_long_mode(vcpu)) {
1379 nr &= 0xFFFFFFFF;
1380 a0 &= 0xFFFFFFFF;
1381 a1 &= 0xFFFFFFFF;
1382 a2 &= 0xFFFFFFFF;
1383 a3 &= 0xFFFFFFFF;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001384 }
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001385
Avi Kivity270fd9b2007-02-19 14:37:47 +02001386 switch (nr) {
1387 default:
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001388 ret = -KVM_ENOSYS;
1389 break;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001390 }
1391 vcpu->regs[VCPU_REGS_RAX] = ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001392 kvm_x86_ops->decache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001393 return 0;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001394}
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001395EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
1396
1397int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
1398{
1399 char instruction[3];
1400 int ret = 0;
1401
1402 mutex_lock(&vcpu->kvm->lock);
1403
1404 /*
1405 * Blow out the MMU to ensure that no other VCPU has an active mapping
1406 * to ensure that the updated hypercall appears atomically across all
1407 * VCPUs.
1408 */
1409 kvm_mmu_zap_all(vcpu->kvm);
1410
1411 kvm_x86_ops->cache_regs(vcpu);
1412 kvm_x86_ops->patch_hypercall(vcpu, instruction);
1413 if (emulator_write_emulated(vcpu->rip, instruction, 3, vcpu)
1414 != X86EMUL_CONTINUE)
1415 ret = -EFAULT;
1416
1417 mutex_unlock(&vcpu->kvm->lock);
1418
1419 return ret;
1420}
Avi Kivity270fd9b2007-02-19 14:37:47 +02001421
Avi Kivity6aa8b732006-12-10 02:21:36 -08001422static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1423{
1424 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1425}
1426
1427void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1428{
1429 struct descriptor_table dt = { limit, base };
1430
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001431 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001432}
1433
1434void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1435{
1436 struct descriptor_table dt = { limit, base };
1437
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001438 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001439}
1440
1441void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1442 unsigned long *rflags)
1443{
1444 lmsw(vcpu, msw);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001445 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001446}
1447
1448unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1449{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001450 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001451 switch (cr) {
1452 case 0:
1453 return vcpu->cr0;
1454 case 2:
1455 return vcpu->cr2;
1456 case 3:
1457 return vcpu->cr3;
1458 case 4:
1459 return vcpu->cr4;
1460 default:
1461 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1462 return 0;
1463 }
1464}
1465
1466void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1467 unsigned long *rflags)
1468{
1469 switch (cr) {
1470 case 0:
1471 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001472 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001473 break;
1474 case 2:
1475 vcpu->cr2 = val;
1476 break;
1477 case 3:
1478 set_cr3(vcpu, val);
1479 break;
1480 case 4:
1481 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1482 break;
1483 default:
1484 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1485 }
1486}
1487
Avi Kivity3bab1f52006-12-29 16:49:48 -08001488int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1489{
1490 u64 data;
1491
1492 switch (msr) {
1493 case 0xc0010010: /* SYSCFG */
1494 case 0xc0010015: /* HWCR */
1495 case MSR_IA32_PLATFORM_ID:
1496 case MSR_IA32_P5_MC_ADDR:
1497 case MSR_IA32_P5_MC_TYPE:
1498 case MSR_IA32_MC0_CTL:
1499 case MSR_IA32_MCG_STATUS:
1500 case MSR_IA32_MCG_CAP:
1501 case MSR_IA32_MC0_MISC:
1502 case MSR_IA32_MC0_MISC+4:
1503 case MSR_IA32_MC0_MISC+8:
1504 case MSR_IA32_MC0_MISC+12:
1505 case MSR_IA32_MC0_MISC+16:
1506 case MSR_IA32_UCODE_REV:
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001507 case MSR_IA32_PERF_STATUS:
Matthew Gregan2dc70942007-05-06 10:59:46 +03001508 case MSR_IA32_EBL_CR_POWERON:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001509 /* MTRR registers */
1510 case 0xfe:
1511 case 0x200 ... 0x2ff:
1512 data = 0;
1513 break;
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001514 case 0xcd: /* fsb frequency */
1515 data = 3;
1516 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001517 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001518 data = kvm_get_apic_base(vcpu);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001519 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001520 case MSR_IA32_MISC_ENABLE:
1521 data = vcpu->ia32_misc_enable_msr;
1522 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001523#ifdef CONFIG_X86_64
1524 case MSR_EFER:
1525 data = vcpu->shadow_efer;
1526 break;
1527#endif
1528 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001529 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001530 return 1;
1531 }
1532 *pdata = data;
1533 return 0;
1534}
1535EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1536
Avi Kivity6aa8b732006-12-10 02:21:36 -08001537/*
1538 * Reads an msr value (of 'msr_index') into 'pdata'.
1539 * Returns 0 on success, non-0 otherwise.
1540 * Assumes vcpu_load() was already called.
1541 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001542int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001543{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001544 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001545}
1546
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001547#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001548
Avi Kivity3bab1f52006-12-29 16:49:48 -08001549static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001550{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001551 if (efer & EFER_RESERVED_BITS) {
1552 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1553 efer);
1554 inject_gp(vcpu);
1555 return;
1556 }
1557
1558 if (is_paging(vcpu)
1559 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1560 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1561 inject_gp(vcpu);
1562 return;
1563 }
1564
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001565 kvm_x86_ops->set_efer(vcpu, efer);
Avi Kivity7725f0b2006-12-13 00:34:01 -08001566
Avi Kivity6aa8b732006-12-10 02:21:36 -08001567 efer &= ~EFER_LMA;
1568 efer |= vcpu->shadow_efer & EFER_LMA;
1569
1570 vcpu->shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001571}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001572
1573#endif
1574
Avi Kivity3bab1f52006-12-29 16:49:48 -08001575int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1576{
1577 switch (msr) {
1578#ifdef CONFIG_X86_64
1579 case MSR_EFER:
1580 set_efer(vcpu, data);
1581 break;
1582#endif
1583 case MSR_IA32_MC0_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001584 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
Avi Kivity3bab1f52006-12-29 16:49:48 -08001585 __FUNCTION__, data);
1586 break;
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001587 case MSR_IA32_MCG_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001588 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001589 __FUNCTION__, data);
1590 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001591 case MSR_IA32_UCODE_REV:
1592 case MSR_IA32_UCODE_WRITE:
1593 case 0x200 ... 0x2ff: /* MTRRs */
1594 break;
1595 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001596 kvm_set_apic_base(vcpu, data);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001597 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001598 case MSR_IA32_MISC_ENABLE:
1599 vcpu->ia32_misc_enable_msr = data;
1600 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001601 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001602 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001603 return 1;
1604 }
1605 return 0;
1606}
1607EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1608
Avi Kivity6aa8b732006-12-10 02:21:36 -08001609/*
1610 * Writes msr value into into the appropriate "register".
1611 * Returns 0 on success, non-0 otherwise.
1612 * Assumes vcpu_load() was already called.
1613 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001614int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001615{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001616 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001617}
1618
1619void kvm_resched(struct kvm_vcpu *vcpu)
1620{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001621 if (!need_resched())
1622 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001623 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001624}
1625EXPORT_SYMBOL_GPL(kvm_resched);
1626
Avi Kivity06465c52007-02-28 20:46:53 +02001627void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1628{
1629 int i;
1630 u32 function;
1631 struct kvm_cpuid_entry *e, *best;
1632
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001633 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001634 function = vcpu->regs[VCPU_REGS_RAX];
1635 vcpu->regs[VCPU_REGS_RAX] = 0;
1636 vcpu->regs[VCPU_REGS_RBX] = 0;
1637 vcpu->regs[VCPU_REGS_RCX] = 0;
1638 vcpu->regs[VCPU_REGS_RDX] = 0;
1639 best = NULL;
1640 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1641 e = &vcpu->cpuid_entries[i];
1642 if (e->function == function) {
1643 best = e;
1644 break;
1645 }
1646 /*
1647 * Both basic or both extended?
1648 */
1649 if (((e->function ^ function) & 0x80000000) == 0)
1650 if (!best || e->function > best->function)
1651 best = e;
1652 }
1653 if (best) {
1654 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1655 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1656 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1657 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1658 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001659 kvm_x86_ops->decache_regs(vcpu);
1660 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001661}
1662EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1663
Avi Kivity039576c2007-03-20 12:46:50 +02001664static int pio_copy_data(struct kvm_vcpu *vcpu)
Avi Kivity46fc1472007-02-22 19:39:30 +02001665{
Avi Kivity039576c2007-03-20 12:46:50 +02001666 void *p = vcpu->pio_data;
1667 void *q;
1668 unsigned bytes;
1669 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1670
Avi Kivity039576c2007-03-20 12:46:50 +02001671 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1672 PAGE_KERNEL);
1673 if (!q) {
Avi Kivity039576c2007-03-20 12:46:50 +02001674 free_pio_guest_pages(vcpu);
1675 return -ENOMEM;
1676 }
1677 q += vcpu->pio.guest_page_offset;
1678 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1679 if (vcpu->pio.in)
1680 memcpy(q, p, bytes);
1681 else
1682 memcpy(p, q, bytes);
1683 q -= vcpu->pio.guest_page_offset;
1684 vunmap(q);
Avi Kivity039576c2007-03-20 12:46:50 +02001685 free_pio_guest_pages(vcpu);
1686 return 0;
1687}
1688
1689static int complete_pio(struct kvm_vcpu *vcpu)
1690{
1691 struct kvm_pio_request *io = &vcpu->pio;
Avi Kivity46fc1472007-02-22 19:39:30 +02001692 long delta;
Avi Kivity039576c2007-03-20 12:46:50 +02001693 int r;
Avi Kivity46fc1472007-02-22 19:39:30 +02001694
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001695 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001696
1697 if (!io->string) {
Avi Kivity039576c2007-03-20 12:46:50 +02001698 if (io->in)
1699 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
Avi Kivity46fc1472007-02-22 19:39:30 +02001700 io->size);
1701 } else {
Avi Kivity039576c2007-03-20 12:46:50 +02001702 if (io->in) {
1703 r = pio_copy_data(vcpu);
1704 if (r) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001705 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02001706 return r;
1707 }
1708 }
1709
Avi Kivity46fc1472007-02-22 19:39:30 +02001710 delta = 1;
1711 if (io->rep) {
Avi Kivity039576c2007-03-20 12:46:50 +02001712 delta *= io->cur_count;
Avi Kivity46fc1472007-02-22 19:39:30 +02001713 /*
1714 * The size of the register should really depend on
1715 * current address size.
1716 */
1717 vcpu->regs[VCPU_REGS_RCX] -= delta;
1718 }
Avi Kivity039576c2007-03-20 12:46:50 +02001719 if (io->down)
Avi Kivity46fc1472007-02-22 19:39:30 +02001720 delta = -delta;
1721 delta *= io->size;
Avi Kivity039576c2007-03-20 12:46:50 +02001722 if (io->in)
Avi Kivity46fc1472007-02-22 19:39:30 +02001723 vcpu->regs[VCPU_REGS_RDI] += delta;
1724 else
1725 vcpu->regs[VCPU_REGS_RSI] += delta;
1726 }
1727
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001728 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001729
Avi Kivity039576c2007-03-20 12:46:50 +02001730 io->count -= io->cur_count;
1731 io->cur_count = 0;
1732
Avi Kivity039576c2007-03-20 12:46:50 +02001733 return 0;
Avi Kivity46fc1472007-02-22 19:39:30 +02001734}
1735
Eddie Dong65619eb2007-07-17 11:52:33 +03001736static void kernel_pio(struct kvm_io_device *pio_dev,
1737 struct kvm_vcpu *vcpu,
1738 void *pd)
Eddie Dong74906342007-06-19 18:05:03 +03001739{
1740 /* TODO: String I/O for in kernel device */
1741
Eddie Dong9cf98822007-07-22 10:36:31 +03001742 mutex_lock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001743 if (vcpu->pio.in)
1744 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1745 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001746 pd);
Eddie Dong74906342007-06-19 18:05:03 +03001747 else
1748 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1749 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001750 pd);
Eddie Dong9cf98822007-07-22 10:36:31 +03001751 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001752}
1753
1754static void pio_string_write(struct kvm_io_device *pio_dev,
1755 struct kvm_vcpu *vcpu)
1756{
1757 struct kvm_pio_request *io = &vcpu->pio;
1758 void *pd = vcpu->pio_data;
1759 int i;
1760
Eddie Dong9cf98822007-07-22 10:36:31 +03001761 mutex_lock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001762 for (i = 0; i < io->cur_count; i++) {
1763 kvm_iodevice_write(pio_dev, io->port,
1764 io->size,
1765 pd);
1766 pd += io->size;
1767 }
Eddie Dong9cf98822007-07-22 10:36:31 +03001768 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001769}
1770
Laurent Vivier3090dd72007-08-05 10:43:32 +03001771int kvm_emulate_pio (struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1772 int size, unsigned port)
1773{
1774 struct kvm_io_device *pio_dev;
1775
1776 vcpu->run->exit_reason = KVM_EXIT_IO;
1777 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1778 vcpu->run->io.size = vcpu->pio.size = size;
1779 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1780 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1;
1781 vcpu->run->io.port = vcpu->pio.port = port;
1782 vcpu->pio.in = in;
1783 vcpu->pio.string = 0;
1784 vcpu->pio.down = 0;
1785 vcpu->pio.guest_page_offset = 0;
1786 vcpu->pio.rep = 0;
1787
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001788 kvm_x86_ops->cache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03001789 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001790 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03001791
Avi Kivity0967b7b2007-09-15 17:34:36 +03001792 kvm_x86_ops->skip_emulated_instruction(vcpu);
1793
Laurent Vivier3090dd72007-08-05 10:43:32 +03001794 pio_dev = vcpu_find_pio_dev(vcpu, port);
1795 if (pio_dev) {
1796 kernel_pio(pio_dev, vcpu, vcpu->pio_data);
1797 complete_pio(vcpu);
1798 return 1;
1799 }
1800 return 0;
1801}
1802EXPORT_SYMBOL_GPL(kvm_emulate_pio);
1803
1804int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1805 int size, unsigned long count, int down,
Avi Kivity039576c2007-03-20 12:46:50 +02001806 gva_t address, int rep, unsigned port)
1807{
1808 unsigned now, in_page;
Eddie Dong65619eb2007-07-17 11:52:33 +03001809 int i, ret = 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001810 int nr_pages = 1;
1811 struct page *page;
Eddie Dong74906342007-06-19 18:05:03 +03001812 struct kvm_io_device *pio_dev;
Avi Kivity039576c2007-03-20 12:46:50 +02001813
1814 vcpu->run->exit_reason = KVM_EXIT_IO;
1815 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Laurent Vivier3090dd72007-08-05 10:43:32 +03001816 vcpu->run->io.size = vcpu->pio.size = size;
Avi Kivity039576c2007-03-20 12:46:50 +02001817 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Laurent Vivier3090dd72007-08-05 10:43:32 +03001818 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count;
1819 vcpu->run->io.port = vcpu->pio.port = port;
Avi Kivity039576c2007-03-20 12:46:50 +02001820 vcpu->pio.in = in;
Laurent Vivier3090dd72007-08-05 10:43:32 +03001821 vcpu->pio.string = 1;
Avi Kivity039576c2007-03-20 12:46:50 +02001822 vcpu->pio.down = down;
1823 vcpu->pio.guest_page_offset = offset_in_page(address);
1824 vcpu->pio.rep = rep;
1825
Avi Kivity039576c2007-03-20 12:46:50 +02001826 if (!count) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001827 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02001828 return 1;
1829 }
1830
Avi Kivity039576c2007-03-20 12:46:50 +02001831 if (!down)
1832 in_page = PAGE_SIZE - offset_in_page(address);
1833 else
1834 in_page = offset_in_page(address) + size;
1835 now = min(count, (unsigned long)in_page / size);
1836 if (!now) {
1837 /*
1838 * String I/O straddles page boundary. Pin two guest pages
1839 * so that we satisfy atomicity constraints. Do just one
1840 * transaction to avoid complexity.
1841 */
1842 nr_pages = 2;
1843 now = 1;
1844 }
1845 if (down) {
1846 /*
1847 * String I/O in reverse. Yuck. Kill the guest, fix later.
1848 */
Rusty Russellf0242472007-08-01 10:48:02 +10001849 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivity039576c2007-03-20 12:46:50 +02001850 inject_gp(vcpu);
1851 return 1;
1852 }
1853 vcpu->run->io.count = now;
1854 vcpu->pio.cur_count = now;
1855
Avi Kivity0967b7b2007-09-15 17:34:36 +03001856 if (vcpu->pio.cur_count == vcpu->pio.count)
1857 kvm_x86_ops->skip_emulated_instruction(vcpu);
1858
Avi Kivity039576c2007-03-20 12:46:50 +02001859 for (i = 0; i < nr_pages; ++i) {
Shaohua Li11ec2802007-07-23 14:51:37 +08001860 mutex_lock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02001861 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
1862 if (page)
1863 get_page(page);
1864 vcpu->pio.guest_pages[i] = page;
Shaohua Li11ec2802007-07-23 14:51:37 +08001865 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02001866 if (!page) {
1867 inject_gp(vcpu);
1868 free_pio_guest_pages(vcpu);
1869 return 1;
1870 }
1871 }
1872
Laurent Vivier3090dd72007-08-05 10:43:32 +03001873 pio_dev = vcpu_find_pio_dev(vcpu, port);
Eddie Dong65619eb2007-07-17 11:52:33 +03001874 if (!vcpu->pio.in) {
1875 /* string PIO write */
1876 ret = pio_copy_data(vcpu);
1877 if (ret >= 0 && pio_dev) {
1878 pio_string_write(pio_dev, vcpu);
1879 complete_pio(vcpu);
1880 if (vcpu->pio.count == 0)
1881 ret = 1;
1882 }
1883 } else if (pio_dev)
Rusty Russellf0242472007-08-01 10:48:02 +10001884 pr_unimpl(vcpu, "no string pio read support yet, "
Eddie Dong65619eb2007-07-17 11:52:33 +03001885 "port %x size %d count %ld\n",
1886 port, size, count);
1887
1888 return ret;
Avi Kivity039576c2007-03-20 12:46:50 +02001889}
Laurent Vivier3090dd72007-08-05 10:43:32 +03001890EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
Avi Kivity039576c2007-03-20 12:46:50 +02001891
Avi Kivity04d2cc72007-09-10 18:10:54 +03001892/*
1893 * Check if userspace requested an interrupt window, and that the
1894 * interrupt window is open.
1895 *
1896 * No need to exit to userspace if we already have an interrupt queued.
1897 */
1898static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
1899 struct kvm_run *kvm_run)
1900{
1901 return (!vcpu->irq_summary &&
1902 kvm_run->request_interrupt_window &&
1903 vcpu->interrupt_window_open &&
1904 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
1905}
1906
1907static void post_kvm_run_save(struct kvm_vcpu *vcpu,
1908 struct kvm_run *kvm_run)
1909{
1910 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
1911 kvm_run->cr8 = get_cr8(vcpu);
1912 kvm_run->apic_base = kvm_get_apic_base(vcpu);
1913 if (irqchip_in_kernel(vcpu->kvm))
1914 kvm_run->ready_for_interrupt_injection = 1;
1915 else
1916 kvm_run->ready_for_interrupt_injection =
1917 (vcpu->interrupt_window_open &&
1918 vcpu->irq_summary == 0);
1919}
1920
1921static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1922{
1923 int r;
1924
1925 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
1926 printk("vcpu %d received sipi with vector # %x\n",
1927 vcpu->vcpu_id, vcpu->sipi_vector);
1928 kvm_lapic_reset(vcpu);
1929 kvm_x86_ops->vcpu_reset(vcpu);
1930 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
1931 }
1932
1933preempted:
1934 if (vcpu->guest_debug.enabled)
1935 kvm_x86_ops->guest_debug_pre(vcpu);
1936
1937again:
1938 r = kvm_mmu_reload(vcpu);
1939 if (unlikely(r))
1940 goto out;
1941
1942 preempt_disable();
1943
1944 kvm_x86_ops->prepare_guest_switch(vcpu);
1945 kvm_load_guest_fpu(vcpu);
1946
1947 local_irq_disable();
1948
1949 if (signal_pending(current)) {
1950 local_irq_enable();
1951 preempt_enable();
1952 r = -EINTR;
1953 kvm_run->exit_reason = KVM_EXIT_INTR;
1954 ++vcpu->stat.signal_exits;
1955 goto out;
1956 }
1957
1958 if (irqchip_in_kernel(vcpu->kvm))
1959 kvm_x86_ops->inject_pending_irq(vcpu);
1960 else if (!vcpu->mmio_read_completed)
1961 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
1962
1963 vcpu->guest_mode = 1;
Laurent Vivierd172fcd2007-10-15 17:00:19 +02001964 kvm_guest_enter();
Avi Kivity04d2cc72007-09-10 18:10:54 +03001965
1966 if (vcpu->requests)
1967 if (test_and_clear_bit(KVM_TLB_FLUSH, &vcpu->requests))
1968 kvm_x86_ops->tlb_flush(vcpu);
1969
1970 kvm_x86_ops->run(vcpu, kvm_run);
1971
1972 vcpu->guest_mode = 0;
1973 local_irq_enable();
1974
1975 ++vcpu->stat.exits;
1976
Laurent Vivier0552f732007-10-18 15:19:01 +02001977 /*
1978 * We must have an instruction between local_irq_enable() and
1979 * kvm_guest_exit(), so the timer interrupt isn't delayed by
1980 * the interrupt shadow. The stat.exits increment will do nicely.
1981 * But we need to prevent reordering, hence this barrier():
1982 */
1983 barrier();
1984
1985 kvm_guest_exit();
1986
Avi Kivity04d2cc72007-09-10 18:10:54 +03001987 preempt_enable();
1988
1989 /*
1990 * Profile KVM exit RIPs:
1991 */
1992 if (unlikely(prof_on == KVM_PROFILING)) {
1993 kvm_x86_ops->cache_regs(vcpu);
1994 profile_hit(KVM_PROFILING, (void *)vcpu->rip);
1995 }
1996
1997 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
1998
1999 if (r > 0) {
2000 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2001 r = -EINTR;
2002 kvm_run->exit_reason = KVM_EXIT_INTR;
2003 ++vcpu->stat.request_irq_exits;
2004 goto out;
2005 }
2006 if (!need_resched()) {
2007 ++vcpu->stat.light_exits;
2008 goto again;
2009 }
2010 }
2011
2012out:
2013 if (r > 0) {
2014 kvm_resched(vcpu);
2015 goto preempted;
2016 }
2017
2018 post_kvm_run_save(vcpu, kvm_run);
2019
2020 return r;
2021}
2022
2023
Avi Kivitybccf2152007-02-21 18:04:26 +02002024static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002025{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002026 int r;
Avi Kivity1961d272007-03-05 19:46:05 +02002027 sigset_t sigsaved;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002028
Avi Kivitybccf2152007-02-21 18:04:26 +02002029 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002030
He, Qingc5ec1532007-09-03 17:07:41 +03002031 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
2032 kvm_vcpu_block(vcpu);
2033 vcpu_put(vcpu);
2034 return -EAGAIN;
2035 }
2036
Avi Kivity1961d272007-03-05 19:46:05 +02002037 if (vcpu->sigset_active)
2038 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2039
Dor Laor54810342007-02-12 00:54:39 -08002040 /* re-sync apic's tpr */
He, Qing5cd4f6f2007-08-30 17:04:26 +08002041 if (!irqchip_in_kernel(vcpu->kvm))
2042 set_cr8(vcpu, kvm_run->cr8);
Dor Laor54810342007-02-12 00:54:39 -08002043
Avi Kivity02c83202007-04-29 15:02:17 +03002044 if (vcpu->pio.cur_count) {
2045 r = complete_pio(vcpu);
2046 if (r)
2047 goto out;
2048 }
2049
2050 if (vcpu->mmio_needed) {
2051 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
2052 vcpu->mmio_read_completed = 1;
2053 vcpu->mmio_needed = 0;
2054 r = emulate_instruction(vcpu, kvm_run,
2055 vcpu->mmio_fault_cr2, 0);
2056 if (r == EMULATE_DO_MMIO) {
2057 /*
2058 * Read-modify-write. Back to userspace.
2059 */
Avi Kivity02c83202007-04-29 15:02:17 +03002060 r = 0;
2061 goto out;
Avi Kivity46fc1472007-02-22 19:39:30 +02002062 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002063 }
2064
Avi Kivity8eb7d332007-03-04 14:17:08 +02002065 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002066 kvm_x86_ops->cache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002067 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002068 kvm_x86_ops->decache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002069 }
2070
Avi Kivity04d2cc72007-09-10 18:10:54 +03002071 r = __vcpu_run(vcpu, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002072
Avi Kivity039576c2007-03-20 12:46:50 +02002073out:
Avi Kivity1961d272007-03-05 19:46:05 +02002074 if (vcpu->sigset_active)
2075 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2076
Avi Kivity6aa8b732006-12-10 02:21:36 -08002077 vcpu_put(vcpu);
2078 return r;
2079}
2080
Avi Kivitybccf2152007-02-21 18:04:26 +02002081static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
2082 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002083{
Avi Kivitybccf2152007-02-21 18:04:26 +02002084 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002085
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002086 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002087
2088 regs->rax = vcpu->regs[VCPU_REGS_RAX];
2089 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
2090 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
2091 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
2092 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
2093 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
2094 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
2095 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002096#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002097 regs->r8 = vcpu->regs[VCPU_REGS_R8];
2098 regs->r9 = vcpu->regs[VCPU_REGS_R9];
2099 regs->r10 = vcpu->regs[VCPU_REGS_R10];
2100 regs->r11 = vcpu->regs[VCPU_REGS_R11];
2101 regs->r12 = vcpu->regs[VCPU_REGS_R12];
2102 regs->r13 = vcpu->regs[VCPU_REGS_R13];
2103 regs->r14 = vcpu->regs[VCPU_REGS_R14];
2104 regs->r15 = vcpu->regs[VCPU_REGS_R15];
2105#endif
2106
2107 regs->rip = vcpu->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002108 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002109
2110 /*
2111 * Don't leak debug flags in case they were set for guest debugging
2112 */
2113 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2114 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2115
2116 vcpu_put(vcpu);
2117
2118 return 0;
2119}
2120
Avi Kivitybccf2152007-02-21 18:04:26 +02002121static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
2122 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002123{
Avi Kivitybccf2152007-02-21 18:04:26 +02002124 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002125
2126 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
2127 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
2128 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
2129 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
2130 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
2131 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
2132 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
2133 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002134#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002135 vcpu->regs[VCPU_REGS_R8] = regs->r8;
2136 vcpu->regs[VCPU_REGS_R9] = regs->r9;
2137 vcpu->regs[VCPU_REGS_R10] = regs->r10;
2138 vcpu->regs[VCPU_REGS_R11] = regs->r11;
2139 vcpu->regs[VCPU_REGS_R12] = regs->r12;
2140 vcpu->regs[VCPU_REGS_R13] = regs->r13;
2141 vcpu->regs[VCPU_REGS_R14] = regs->r14;
2142 vcpu->regs[VCPU_REGS_R15] = regs->r15;
2143#endif
2144
2145 vcpu->rip = regs->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002146 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002147
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002148 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002149
2150 vcpu_put(vcpu);
2151
2152 return 0;
2153}
2154
2155static void get_segment(struct kvm_vcpu *vcpu,
2156 struct kvm_segment *var, int seg)
2157{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002158 return kvm_x86_ops->get_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002159}
2160
Avi Kivitybccf2152007-02-21 18:04:26 +02002161static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2162 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002163{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002164 struct descriptor_table dt;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002165 int pending_vec;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002166
Avi Kivitybccf2152007-02-21 18:04:26 +02002167 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002168
2169 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2170 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2171 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2172 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2173 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2174 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2175
2176 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2177 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2178
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002179 kvm_x86_ops->get_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002180 sregs->idt.limit = dt.limit;
2181 sregs->idt.base = dt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002182 kvm_x86_ops->get_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002183 sregs->gdt.limit = dt.limit;
2184 sregs->gdt.base = dt.base;
2185
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002186 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002187 sregs->cr0 = vcpu->cr0;
2188 sregs->cr2 = vcpu->cr2;
2189 sregs->cr3 = vcpu->cr3;
2190 sregs->cr4 = vcpu->cr4;
Eddie Dong7017fc32007-07-18 11:34:57 +03002191 sregs->cr8 = get_cr8(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002192 sregs->efer = vcpu->shadow_efer;
Eddie Dong7017fc32007-07-18 11:34:57 +03002193 sregs->apic_base = kvm_get_apic_base(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002194
Eddie Dong2a8067f2007-08-06 16:29:07 +03002195 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc52fb352007-08-02 14:03:07 +03002196 memset(sregs->interrupt_bitmap, 0,
2197 sizeof sregs->interrupt_bitmap);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002198 pending_vec = kvm_x86_ops->get_irq(vcpu);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002199 if (pending_vec >= 0)
2200 set_bit(pending_vec, (unsigned long *)sregs->interrupt_bitmap);
2201 } else
He, Qingc52fb352007-08-02 14:03:07 +03002202 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2203 sizeof sregs->interrupt_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002204
2205 vcpu_put(vcpu);
2206
2207 return 0;
2208}
2209
2210static void set_segment(struct kvm_vcpu *vcpu,
2211 struct kvm_segment *var, int seg)
2212{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002213 return kvm_x86_ops->set_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002214}
2215
Avi Kivitybccf2152007-02-21 18:04:26 +02002216static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2217 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002218{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002219 int mmu_reset_needed = 0;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002220 int i, pending_vec, max_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002221 struct descriptor_table dt;
2222
Avi Kivitybccf2152007-02-21 18:04:26 +02002223 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002224
Avi Kivity6aa8b732006-12-10 02:21:36 -08002225 dt.limit = sregs->idt.limit;
2226 dt.base = sregs->idt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002227 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002228 dt.limit = sregs->gdt.limit;
2229 dt.base = sregs->gdt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002230 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002231
2232 vcpu->cr2 = sregs->cr2;
2233 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2234 vcpu->cr3 = sregs->cr3;
2235
Eddie Dong7017fc32007-07-18 11:34:57 +03002236 set_cr8(vcpu, sregs->cr8);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002237
2238 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002239#ifdef CONFIG_X86_64
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002240 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002241#endif
Eddie Dong7017fc32007-07-18 11:34:57 +03002242 kvm_set_apic_base(vcpu, sregs->apic_base);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002243
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002244 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity399badf2007-01-05 16:36:38 -08002245
Avi Kivity6aa8b732006-12-10 02:21:36 -08002246 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
Rusty Russell81f50e32007-09-06 01:20:38 +10002247 vcpu->cr0 = sregs->cr0;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002248 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002249
2250 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002251 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Avi Kivity1b0973b2007-01-05 16:36:41 -08002252 if (!is_long_mode(vcpu) && is_pae(vcpu))
2253 load_pdptrs(vcpu, vcpu->cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002254
2255 if (mmu_reset_needed)
2256 kvm_mmu_reset_context(vcpu);
2257
He, Qingc52fb352007-08-02 14:03:07 +03002258 if (!irqchip_in_kernel(vcpu->kvm)) {
2259 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2260 sizeof vcpu->irq_pending);
2261 vcpu->irq_summary = 0;
2262 for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
2263 if (vcpu->irq_pending[i])
2264 __set_bit(i, &vcpu->irq_summary);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002265 } else {
2266 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
2267 pending_vec = find_first_bit(
2268 (const unsigned long *)sregs->interrupt_bitmap,
2269 max_bits);
2270 /* Only pending external irq is handled here */
2271 if (pending_vec < max_bits) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002272 kvm_x86_ops->set_irq(vcpu, pending_vec);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002273 printk("Set back pending irq %d\n", pending_vec);
2274 }
He, Qingc52fb352007-08-02 14:03:07 +03002275 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002276
Avi Kivity024aa1c2007-03-21 13:44:58 +02002277 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2278 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2279 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2280 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2281 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2282 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2283
2284 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2285 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2286
Avi Kivity6aa8b732006-12-10 02:21:36 -08002287 vcpu_put(vcpu);
2288
2289 return 0;
2290}
2291
Rusty Russell1747fb72007-09-06 01:21:32 +10002292void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2293{
2294 struct kvm_segment cs;
2295
2296 get_segment(vcpu, &cs, VCPU_SREG_CS);
2297 *db = cs.db;
2298 *l = cs.l;
2299}
2300EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2301
Avi Kivity6aa8b732006-12-10 02:21:36 -08002302/*
2303 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2304 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
Michael Riepebf591b22006-12-22 01:05:36 -08002305 *
2306 * This list is modified at module load time to reflect the
2307 * capabilities of the host cpu.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002308 */
2309static u32 msrs_to_save[] = {
2310 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
2311 MSR_K6_STAR,
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002312#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002313 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
2314#endif
2315 MSR_IA32_TIME_STAMP_COUNTER,
2316};
2317
Michael Riepebf591b22006-12-22 01:05:36 -08002318static unsigned num_msrs_to_save;
2319
Avi Kivity6f00e682007-01-26 00:56:40 -08002320static u32 emulated_msrs[] = {
2321 MSR_IA32_MISC_ENABLE,
2322};
2323
Michael Riepebf591b22006-12-22 01:05:36 -08002324static __init void kvm_init_msr_list(void)
2325{
2326 u32 dummy[2];
2327 unsigned i, j;
2328
2329 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2330 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2331 continue;
2332 if (j < i)
2333 msrs_to_save[j] = msrs_to_save[i];
2334 j++;
2335 }
2336 num_msrs_to_save = j;
2337}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002338
2339/*
2340 * Adapt set_msr() to msr_io()'s calling convention
2341 */
2342static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2343{
Avi Kivity35f3f282007-07-17 14:20:30 +03002344 return kvm_set_msr(vcpu, index, *data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002345}
2346
2347/*
2348 * Read or write a bunch of msrs. All parameters are kernel addresses.
2349 *
2350 * @return number of msrs set successfully.
2351 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002352static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002353 struct kvm_msr_entry *entries,
2354 int (*do_msr)(struct kvm_vcpu *vcpu,
2355 unsigned index, u64 *data))
2356{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002357 int i;
2358
Avi Kivitybccf2152007-02-21 18:04:26 +02002359 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002360
2361 for (i = 0; i < msrs->nmsrs; ++i)
2362 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2363 break;
2364
2365 vcpu_put(vcpu);
2366
2367 return i;
2368}
2369
2370/*
2371 * Read or write a bunch of msrs. Parameters are user addresses.
2372 *
2373 * @return number of msrs set successfully.
2374 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002375static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002376 int (*do_msr)(struct kvm_vcpu *vcpu,
2377 unsigned index, u64 *data),
2378 int writeback)
2379{
2380 struct kvm_msrs msrs;
2381 struct kvm_msr_entry *entries;
2382 int r, n;
2383 unsigned size;
2384
2385 r = -EFAULT;
2386 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2387 goto out;
2388
2389 r = -E2BIG;
2390 if (msrs.nmsrs >= MAX_IO_MSRS)
2391 goto out;
2392
2393 r = -ENOMEM;
2394 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
2395 entries = vmalloc(size);
2396 if (!entries)
2397 goto out;
2398
2399 r = -EFAULT;
2400 if (copy_from_user(entries, user_msrs->entries, size))
2401 goto out_free;
2402
Avi Kivitybccf2152007-02-21 18:04:26 +02002403 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002404 if (r < 0)
2405 goto out_free;
2406
2407 r = -EFAULT;
2408 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2409 goto out_free;
2410
2411 r = n;
2412
2413out_free:
2414 vfree(entries);
2415out:
2416 return r;
2417}
2418
2419/*
2420 * Translate a guest virtual address to a guest physical address.
2421 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002422static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2423 struct kvm_translation *tr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002424{
2425 unsigned long vaddr = tr->linear_address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002426 gpa_t gpa;
2427
Avi Kivitybccf2152007-02-21 18:04:26 +02002428 vcpu_load(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +08002429 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002430 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2431 tr->physical_address = gpa;
2432 tr->valid = gpa != UNMAPPED_GVA;
2433 tr->writeable = 1;
2434 tr->usermode = 0;
Shaohua Li11ec2802007-07-23 14:51:37 +08002435 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002436 vcpu_put(vcpu);
2437
2438 return 0;
2439}
2440
Avi Kivitybccf2152007-02-21 18:04:26 +02002441static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2442 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002443{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002444 if (irq->irq < 0 || irq->irq >= 256)
2445 return -EINVAL;
Eddie Dong97222cc2007-09-12 10:58:04 +03002446 if (irqchip_in_kernel(vcpu->kvm))
2447 return -ENXIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02002448 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002449
2450 set_bit(irq->irq, vcpu->irq_pending);
2451 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2452
2453 vcpu_put(vcpu);
2454
2455 return 0;
2456}
2457
Avi Kivitybccf2152007-02-21 18:04:26 +02002458static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2459 struct kvm_debug_guest *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002460{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002461 int r;
2462
Avi Kivitybccf2152007-02-21 18:04:26 +02002463 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002464
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002465 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002466
2467 vcpu_put(vcpu);
2468
2469 return r;
2470}
2471
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002472static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2473 unsigned long address,
2474 int *type)
2475{
2476 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2477 unsigned long pgoff;
2478 struct page *page;
2479
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002480 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +02002481 if (pgoff == 0)
2482 page = virt_to_page(vcpu->run);
2483 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2484 page = virt_to_page(vcpu->pio_data);
2485 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002486 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002487 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002488 if (type != NULL)
2489 *type = VM_FAULT_MINOR;
2490
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002491 return page;
2492}
2493
2494static struct vm_operations_struct kvm_vcpu_vm_ops = {
2495 .nopage = kvm_vcpu_nopage,
2496};
2497
2498static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2499{
2500 vma->vm_ops = &kvm_vcpu_vm_ops;
2501 return 0;
2502}
2503
Avi Kivitybccf2152007-02-21 18:04:26 +02002504static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2505{
2506 struct kvm_vcpu *vcpu = filp->private_data;
2507
2508 fput(vcpu->kvm->filp);
2509 return 0;
2510}
2511
2512static struct file_operations kvm_vcpu_fops = {
2513 .release = kvm_vcpu_release,
2514 .unlocked_ioctl = kvm_vcpu_ioctl,
2515 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002516 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02002517};
2518
2519/*
2520 * Allocates an inode for the vcpu.
2521 */
2522static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2523{
2524 int fd, r;
2525 struct inode *inode;
2526 struct file *file;
2527
Avi Kivityd6d28162007-06-28 08:38:16 -04002528 r = anon_inode_getfd(&fd, &inode, &file,
2529 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2530 if (r)
2531 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002532 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +02002533 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02002534}
2535
Avi Kivityc5ea7662007-02-20 18:41:05 +02002536/*
2537 * Creates some virtual cpus. Good luck creating more than one.
2538 */
2539static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2540{
2541 int r;
2542 struct kvm_vcpu *vcpu;
2543
Avi Kivityc5ea7662007-02-20 18:41:05 +02002544 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002545 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002546
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002547 vcpu = kvm_x86_ops->vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002548 if (IS_ERR(vcpu))
2549 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002550
Avi Kivity15ad7142007-07-11 18:17:21 +03002551 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
2552
Rusty Russellb114b082007-07-30 21:13:43 +10002553 /* We do fxsave: this must be aligned. */
2554 BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF);
2555
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002556 vcpu_load(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002557 r = kvm_mmu_setup(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002558 vcpu_put(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002559 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002560 goto free_vcpu;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002561
Shaohua Li11ec2802007-07-23 14:51:37 +08002562 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002563 if (kvm->vcpus[n]) {
2564 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +08002565 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002566 goto mmu_unload;
2567 }
2568 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08002569 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002570
2571 /* Now it's all set up, let userspace reach it */
Avi Kivitybccf2152007-02-21 18:04:26 +02002572 r = create_vcpu_fd(vcpu);
2573 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002574 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02002575 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002576
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002577unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08002578 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002579 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +08002580 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002581
2582mmu_unload:
2583 vcpu_load(vcpu);
2584 kvm_mmu_unload(vcpu);
2585 vcpu_put(vcpu);
2586
2587free_vcpu:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002588 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002589 return r;
2590}
2591
Eddie Dong2cc51562007-05-21 07:28:09 +03002592static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
2593{
2594 u64 efer;
2595 int i;
2596 struct kvm_cpuid_entry *e, *entry;
2597
2598 rdmsrl(MSR_EFER, efer);
2599 entry = NULL;
2600 for (i = 0; i < vcpu->cpuid_nent; ++i) {
2601 e = &vcpu->cpuid_entries[i];
2602 if (e->function == 0x80000001) {
2603 entry = e;
2604 break;
2605 }
2606 }
Avi Kivity4c981b42007-07-25 09:22:12 +03002607 if (entry && (entry->edx & (1 << 20)) && !(efer & EFER_NX)) {
Eddie Dong2cc51562007-05-21 07:28:09 +03002608 entry->edx &= ~(1 << 20);
Avi Kivity4c981b42007-07-25 09:22:12 +03002609 printk(KERN_INFO "kvm: guest NX capability removed\n");
Eddie Dong2cc51562007-05-21 07:28:09 +03002610 }
2611}
2612
Avi Kivity06465c52007-02-28 20:46:53 +02002613static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
2614 struct kvm_cpuid *cpuid,
2615 struct kvm_cpuid_entry __user *entries)
2616{
2617 int r;
2618
2619 r = -E2BIG;
2620 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2621 goto out;
2622 r = -EFAULT;
2623 if (copy_from_user(&vcpu->cpuid_entries, entries,
2624 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
2625 goto out;
2626 vcpu->cpuid_nent = cpuid->nent;
Eddie Dong2cc51562007-05-21 07:28:09 +03002627 cpuid_fix_nx_cap(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002628 return 0;
2629
2630out:
2631 return r;
2632}
2633
Avi Kivity1961d272007-03-05 19:46:05 +02002634static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2635{
2636 if (sigset) {
2637 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2638 vcpu->sigset_active = 1;
2639 vcpu->sigset = *sigset;
2640 } else
2641 vcpu->sigset_active = 0;
2642 return 0;
2643}
2644
Avi Kivityb8836732007-04-01 16:34:31 +03002645/*
2646 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2647 * we have asm/x86/processor.h
2648 */
2649struct fxsave {
2650 u16 cwd;
2651 u16 swd;
2652 u16 twd;
2653 u16 fop;
2654 u64 rip;
2655 u64 rdp;
2656 u32 mxcsr;
2657 u32 mxcsr_mask;
2658 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2659#ifdef CONFIG_X86_64
2660 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2661#else
2662 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2663#endif
2664};
2665
2666static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2667{
Rusty Russellb114b082007-07-30 21:13:43 +10002668 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002669
2670 vcpu_load(vcpu);
2671
2672 memcpy(fpu->fpr, fxsave->st_space, 128);
2673 fpu->fcw = fxsave->cwd;
2674 fpu->fsw = fxsave->swd;
2675 fpu->ftwx = fxsave->twd;
2676 fpu->last_opcode = fxsave->fop;
2677 fpu->last_ip = fxsave->rip;
2678 fpu->last_dp = fxsave->rdp;
2679 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2680
2681 vcpu_put(vcpu);
2682
2683 return 0;
2684}
2685
2686static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2687{
Rusty Russellb114b082007-07-30 21:13:43 +10002688 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002689
2690 vcpu_load(vcpu);
2691
2692 memcpy(fxsave->st_space, fpu->fpr, 128);
2693 fxsave->cwd = fpu->fcw;
2694 fxsave->swd = fpu->fsw;
2695 fxsave->twd = fpu->ftwx;
2696 fxsave->fop = fpu->last_opcode;
2697 fxsave->rip = fpu->last_ip;
2698 fxsave->rdp = fpu->last_dp;
2699 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2700
2701 vcpu_put(vcpu);
2702
2703 return 0;
2704}
2705
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002706static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
2707 struct kvm_lapic_state *s)
2708{
2709 vcpu_load(vcpu);
2710 memcpy(s->regs, vcpu->apic->regs, sizeof *s);
2711 vcpu_put(vcpu);
2712
2713 return 0;
2714}
2715
2716static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
2717 struct kvm_lapic_state *s)
2718{
2719 vcpu_load(vcpu);
2720 memcpy(vcpu->apic->regs, s->regs, sizeof *s);
2721 kvm_apic_post_state_restore(vcpu);
2722 vcpu_put(vcpu);
2723
2724 return 0;
2725}
2726
Avi Kivitybccf2152007-02-21 18:04:26 +02002727static long kvm_vcpu_ioctl(struct file *filp,
2728 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002729{
Avi Kivitybccf2152007-02-21 18:04:26 +02002730 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00002731 void __user *argp = (void __user *)arg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002732 int r = -EINVAL;
2733
2734 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002735 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002736 r = -EINVAL;
2737 if (arg)
2738 goto out;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002739 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002740 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002741 case KVM_GET_REGS: {
2742 struct kvm_regs kvm_regs;
2743
Avi Kivitybccf2152007-02-21 18:04:26 +02002744 memset(&kvm_regs, 0, sizeof kvm_regs);
2745 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002746 if (r)
2747 goto out;
2748 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002749 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002750 goto out;
2751 r = 0;
2752 break;
2753 }
2754 case KVM_SET_REGS: {
2755 struct kvm_regs kvm_regs;
2756
2757 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002758 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002759 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002760 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002761 if (r)
2762 goto out;
2763 r = 0;
2764 break;
2765 }
2766 case KVM_GET_SREGS: {
2767 struct kvm_sregs kvm_sregs;
2768
Avi Kivitybccf2152007-02-21 18:04:26 +02002769 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2770 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002771 if (r)
2772 goto out;
2773 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002774 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002775 goto out;
2776 r = 0;
2777 break;
2778 }
2779 case KVM_SET_SREGS: {
2780 struct kvm_sregs kvm_sregs;
2781
2782 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002783 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002784 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002785 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002786 if (r)
2787 goto out;
2788 r = 0;
2789 break;
2790 }
2791 case KVM_TRANSLATE: {
2792 struct kvm_translation tr;
2793
2794 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002795 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002796 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002797 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002798 if (r)
2799 goto out;
2800 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002801 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002802 goto out;
2803 r = 0;
2804 break;
2805 }
2806 case KVM_INTERRUPT: {
2807 struct kvm_interrupt irq;
2808
2809 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002810 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002811 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002812 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002813 if (r)
2814 goto out;
2815 r = 0;
2816 break;
2817 }
2818 case KVM_DEBUG_GUEST: {
2819 struct kvm_debug_guest dbg;
2820
2821 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002822 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002823 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002824 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002825 if (r)
2826 goto out;
2827 r = 0;
2828 break;
2829 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002830 case KVM_GET_MSRS:
Avi Kivity35f3f282007-07-17 14:20:30 +03002831 r = msr_io(vcpu, argp, kvm_get_msr, 1);
Avi Kivitybccf2152007-02-21 18:04:26 +02002832 break;
2833 case KVM_SET_MSRS:
2834 r = msr_io(vcpu, argp, do_set_msr, 0);
2835 break;
Avi Kivity06465c52007-02-28 20:46:53 +02002836 case KVM_SET_CPUID: {
2837 struct kvm_cpuid __user *cpuid_arg = argp;
2838 struct kvm_cpuid cpuid;
2839
2840 r = -EFAULT;
2841 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2842 goto out;
2843 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2844 if (r)
2845 goto out;
2846 break;
2847 }
Avi Kivity1961d272007-03-05 19:46:05 +02002848 case KVM_SET_SIGNAL_MASK: {
2849 struct kvm_signal_mask __user *sigmask_arg = argp;
2850 struct kvm_signal_mask kvm_sigmask;
2851 sigset_t sigset, *p;
2852
2853 p = NULL;
2854 if (argp) {
2855 r = -EFAULT;
2856 if (copy_from_user(&kvm_sigmask, argp,
2857 sizeof kvm_sigmask))
2858 goto out;
2859 r = -EINVAL;
2860 if (kvm_sigmask.len != sizeof sigset)
2861 goto out;
2862 r = -EFAULT;
2863 if (copy_from_user(&sigset, sigmask_arg->sigset,
2864 sizeof sigset))
2865 goto out;
2866 p = &sigset;
2867 }
2868 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2869 break;
2870 }
Avi Kivityb8836732007-04-01 16:34:31 +03002871 case KVM_GET_FPU: {
2872 struct kvm_fpu fpu;
2873
2874 memset(&fpu, 0, sizeof fpu);
2875 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2876 if (r)
2877 goto out;
2878 r = -EFAULT;
2879 if (copy_to_user(argp, &fpu, sizeof fpu))
2880 goto out;
2881 r = 0;
2882 break;
2883 }
2884 case KVM_SET_FPU: {
2885 struct kvm_fpu fpu;
2886
2887 r = -EFAULT;
2888 if (copy_from_user(&fpu, argp, sizeof fpu))
2889 goto out;
2890 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2891 if (r)
2892 goto out;
2893 r = 0;
2894 break;
2895 }
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002896 case KVM_GET_LAPIC: {
2897 struct kvm_lapic_state lapic;
2898
2899 memset(&lapic, 0, sizeof lapic);
2900 r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic);
2901 if (r)
2902 goto out;
2903 r = -EFAULT;
2904 if (copy_to_user(argp, &lapic, sizeof lapic))
2905 goto out;
2906 r = 0;
2907 break;
2908 }
2909 case KVM_SET_LAPIC: {
2910 struct kvm_lapic_state lapic;
2911
2912 r = -EFAULT;
2913 if (copy_from_user(&lapic, argp, sizeof lapic))
2914 goto out;
2915 r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);;
2916 if (r)
2917 goto out;
2918 r = 0;
2919 break;
2920 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002921 default:
2922 ;
2923 }
2924out:
2925 return r;
2926}
2927
2928static long kvm_vm_ioctl(struct file *filp,
2929 unsigned int ioctl, unsigned long arg)
2930{
2931 struct kvm *kvm = filp->private_data;
2932 void __user *argp = (void __user *)arg;
2933 int r = -EINVAL;
2934
2935 switch (ioctl) {
2936 case KVM_CREATE_VCPU:
2937 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2938 if (r < 0)
2939 goto out;
2940 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002941 case KVM_SET_MEMORY_REGION: {
2942 struct kvm_memory_region kvm_mem;
2943
2944 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002945 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002946 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002947 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002948 if (r)
2949 goto out;
2950 break;
2951 }
2952 case KVM_GET_DIRTY_LOG: {
2953 struct kvm_dirty_log log;
2954
2955 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002956 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002957 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002958 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002959 if (r)
2960 goto out;
2961 break;
2962 }
Avi Kivitye8207542007-03-30 16:54:30 +03002963 case KVM_SET_MEMORY_ALIAS: {
2964 struct kvm_memory_alias alias;
2965
2966 r = -EFAULT;
2967 if (copy_from_user(&alias, argp, sizeof alias))
2968 goto out;
2969 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
2970 if (r)
2971 goto out;
2972 break;
2973 }
Eddie Dong85f455f2007-07-06 12:20:49 +03002974 case KVM_CREATE_IRQCHIP:
2975 r = -ENOMEM;
2976 kvm->vpic = kvm_create_pic(kvm);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03002977 if (kvm->vpic) {
2978 r = kvm_ioapic_init(kvm);
2979 if (r) {
2980 kfree(kvm->vpic);
2981 kvm->vpic = NULL;
2982 goto out;
2983 }
2984 }
Eddie Dong85f455f2007-07-06 12:20:49 +03002985 else
2986 goto out;
2987 break;
2988 case KVM_IRQ_LINE: {
2989 struct kvm_irq_level irq_event;
2990
2991 r = -EFAULT;
2992 if (copy_from_user(&irq_event, argp, sizeof irq_event))
2993 goto out;
2994 if (irqchip_in_kernel(kvm)) {
Eddie Dong9cf98822007-07-22 10:36:31 +03002995 mutex_lock(&kvm->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +03002996 if (irq_event.irq < 16)
2997 kvm_pic_set_irq(pic_irqchip(kvm),
2998 irq_event.irq,
2999 irq_event.level);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03003000 kvm_ioapic_set_irq(kvm->vioapic,
3001 irq_event.irq,
3002 irq_event.level);
Eddie Dong9cf98822007-07-22 10:36:31 +03003003 mutex_unlock(&kvm->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +03003004 r = 0;
3005 }
3006 break;
3007 }
He, Qing6ceb9d72007-07-26 11:05:18 +03003008 case KVM_GET_IRQCHIP: {
3009 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3010 struct kvm_irqchip chip;
3011
3012 r = -EFAULT;
3013 if (copy_from_user(&chip, argp, sizeof chip))
3014 goto out;
3015 r = -ENXIO;
3016 if (!irqchip_in_kernel(kvm))
3017 goto out;
3018 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
3019 if (r)
3020 goto out;
3021 r = -EFAULT;
3022 if (copy_to_user(argp, &chip, sizeof chip))
3023 goto out;
3024 r = 0;
3025 break;
3026 }
3027 case KVM_SET_IRQCHIP: {
3028 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3029 struct kvm_irqchip chip;
3030
3031 r = -EFAULT;
3032 if (copy_from_user(&chip, argp, sizeof chip))
3033 goto out;
3034 r = -ENXIO;
3035 if (!irqchip_in_kernel(kvm))
3036 goto out;
3037 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
3038 if (r)
3039 goto out;
3040 r = 0;
3041 break;
3042 }
Avi Kivityf17abe92007-02-21 19:28:04 +02003043 default:
3044 ;
3045 }
3046out:
3047 return r;
3048}
3049
3050static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
3051 unsigned long address,
3052 int *type)
3053{
3054 struct kvm *kvm = vma->vm_file->private_data;
3055 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02003056 struct page *page;
3057
Avi Kivityf17abe92007-02-21 19:28:04 +02003058 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity954bbbc2007-03-30 14:02:32 +03003059 page = gfn_to_page(kvm, pgoff);
Avi Kivityf17abe92007-02-21 19:28:04 +02003060 if (!page)
3061 return NOPAGE_SIGBUS;
3062 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03003063 if (type != NULL)
3064 *type = VM_FAULT_MINOR;
3065
Avi Kivityf17abe92007-02-21 19:28:04 +02003066 return page;
3067}
3068
3069static struct vm_operations_struct kvm_vm_vm_ops = {
3070 .nopage = kvm_vm_nopage,
3071};
3072
3073static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
3074{
3075 vma->vm_ops = &kvm_vm_vm_ops;
3076 return 0;
3077}
3078
3079static struct file_operations kvm_vm_fops = {
3080 .release = kvm_vm_release,
3081 .unlocked_ioctl = kvm_vm_ioctl,
3082 .compat_ioctl = kvm_vm_ioctl,
3083 .mmap = kvm_vm_mmap,
3084};
3085
3086static int kvm_dev_ioctl_create_vm(void)
3087{
3088 int fd, r;
3089 struct inode *inode;
3090 struct file *file;
3091 struct kvm *kvm;
3092
Avi Kivityf17abe92007-02-21 19:28:04 +02003093 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04003094 if (IS_ERR(kvm))
3095 return PTR_ERR(kvm);
3096 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
3097 if (r) {
3098 kvm_destroy_vm(kvm);
3099 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02003100 }
3101
Avi Kivitybccf2152007-02-21 18:04:26 +02003102 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02003103
Avi Kivityf17abe92007-02-21 19:28:04 +02003104 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02003105}
3106
3107static long kvm_dev_ioctl(struct file *filp,
3108 unsigned int ioctl, unsigned long arg)
3109{
3110 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02003111 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02003112
3113 switch (ioctl) {
3114 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02003115 r = -EINVAL;
3116 if (arg)
3117 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02003118 r = KVM_API_VERSION;
3119 break;
3120 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02003121 r = -EINVAL;
3122 if (arg)
3123 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02003124 r = kvm_dev_ioctl_create_vm();
3125 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003126 case KVM_GET_MSR_INDEX_LIST: {
Al Viro2f3669872007-02-09 16:38:35 +00003127 struct kvm_msr_list __user *user_msr_list = argp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003128 struct kvm_msr_list msr_list;
3129 unsigned n;
3130
3131 r = -EFAULT;
3132 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
3133 goto out;
3134 n = msr_list.nmsrs;
Avi Kivity6f00e682007-01-26 00:56:40 -08003135 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003136 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
3137 goto out;
3138 r = -E2BIG;
Michael Riepebf591b22006-12-22 01:05:36 -08003139 if (n < num_msrs_to_save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003140 goto out;
3141 r = -EFAULT;
3142 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
Michael Riepebf591b22006-12-22 01:05:36 -08003143 num_msrs_to_save * sizeof(u32)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003144 goto out;
Avi Kivity6f00e682007-01-26 00:56:40 -08003145 if (copy_to_user(user_msr_list->indices
3146 + num_msrs_to_save * sizeof(u32),
3147 &emulated_msrs,
3148 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
3149 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003150 r = 0;
Avi Kivitycc1d8952007-01-05 16:36:58 -08003151 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003152 }
Eddie Dong85f455f2007-07-06 12:20:49 +03003153 case KVM_CHECK_EXTENSION: {
3154 int ext = (long)argp;
3155
3156 switch (ext) {
3157 case KVM_CAP_IRQCHIP:
Eddie Dongb6958ce2007-07-18 12:15:21 +03003158 case KVM_CAP_HLT:
Eddie Dong85f455f2007-07-06 12:20:49 +03003159 r = 1;
3160 break;
3161 default:
3162 r = 0;
3163 break;
3164 }
Avi Kivity5d308f42007-03-01 17:56:20 +02003165 break;
Eddie Dong85f455f2007-07-06 12:20:49 +03003166 }
Avi Kivity07c45a32007-03-07 13:05:38 +02003167 case KVM_GET_VCPU_MMAP_SIZE:
3168 r = -EINVAL;
3169 if (arg)
3170 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02003171 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02003172 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003173 default:
3174 ;
3175 }
3176out:
3177 return r;
3178}
3179
Avi Kivity6aa8b732006-12-10 02:21:36 -08003180static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003181 .unlocked_ioctl = kvm_dev_ioctl,
3182 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003183};
3184
3185static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02003186 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003187 "kvm",
3188 &kvm_chardev_ops,
3189};
3190
Avi Kivity774c47f2007-02-12 00:54:47 -08003191/*
3192 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
3193 * cached on it.
3194 */
3195static void decache_vcpus_on_cpu(int cpu)
3196{
3197 struct kvm *vm;
3198 struct kvm_vcpu *vcpu;
3199 int i;
3200
3201 spin_lock(&kvm_lock);
Shaohua Li11ec2802007-07-23 14:51:37 +08003202 list_for_each_entry(vm, &vm_list, vm_list)
Avi Kivity774c47f2007-02-12 00:54:47 -08003203 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003204 vcpu = vm->vcpus[i];
3205 if (!vcpu)
3206 continue;
Avi Kivity774c47f2007-02-12 00:54:47 -08003207 /*
3208 * If the vcpu is locked, then it is running on some
3209 * other cpu and therefore it is not cached on the
3210 * cpu in question.
3211 *
3212 * If it's not locked, check the last cpu it executed
3213 * on.
3214 */
3215 if (mutex_trylock(&vcpu->mutex)) {
3216 if (vcpu->cpu == cpu) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003217 kvm_x86_ops->vcpu_decache(vcpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08003218 vcpu->cpu = -1;
3219 }
3220 mutex_unlock(&vcpu->mutex);
3221 }
3222 }
3223 spin_unlock(&kvm_lock);
3224}
3225
Avi Kivity1b6c0162007-05-24 13:03:52 +03003226static void hardware_enable(void *junk)
3227{
3228 int cpu = raw_smp_processor_id();
3229
3230 if (cpu_isset(cpu, cpus_hardware_enabled))
3231 return;
3232 cpu_set(cpu, cpus_hardware_enabled);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003233 kvm_x86_ops->hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003234}
3235
3236static void hardware_disable(void *junk)
3237{
3238 int cpu = raw_smp_processor_id();
3239
3240 if (!cpu_isset(cpu, cpus_hardware_enabled))
3241 return;
3242 cpu_clear(cpu, cpus_hardware_enabled);
3243 decache_vcpus_on_cpu(cpu);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003244 kvm_x86_ops->hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003245}
3246
Avi Kivity774c47f2007-02-12 00:54:47 -08003247static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
3248 void *v)
3249{
3250 int cpu = (long)v;
3251
3252 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03003253 case CPU_DYING:
3254 case CPU_DYING_FROZEN:
Avi Kivity6ec8a8562007-08-19 15:57:26 +03003255 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3256 cpu);
3257 hardware_disable(NULL);
3258 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08003259 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003260 case CPU_UP_CANCELED_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003261 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3262 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003263 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003264 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02003265 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003266 case CPU_ONLINE_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003267 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
3268 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003269 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003270 break;
3271 }
3272 return NOTIFY_OK;
3273}
3274
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003275static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
3276 void *v)
3277{
3278 if (val == SYS_RESTART) {
3279 /*
3280 * Some (well, at least mine) BIOSes hang on reboot if
3281 * in vmx root mode.
3282 */
3283 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
3284 on_each_cpu(hardware_disable, NULL, 0, 1);
3285 }
3286 return NOTIFY_OK;
3287}
3288
3289static struct notifier_block kvm_reboot_notifier = {
3290 .notifier_call = kvm_reboot,
3291 .priority = 0,
3292};
3293
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04003294void kvm_io_bus_init(struct kvm_io_bus *bus)
3295{
3296 memset(bus, 0, sizeof(*bus));
3297}
3298
3299void kvm_io_bus_destroy(struct kvm_io_bus *bus)
3300{
3301 int i;
3302
3303 for (i = 0; i < bus->dev_count; i++) {
3304 struct kvm_io_device *pos = bus->devs[i];
3305
3306 kvm_iodevice_destructor(pos);
3307 }
3308}
3309
3310struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
3311{
3312 int i;
3313
3314 for (i = 0; i < bus->dev_count; i++) {
3315 struct kvm_io_device *pos = bus->devs[i];
3316
3317 if (pos->in_range(pos, addr))
3318 return pos;
3319 }
3320
3321 return NULL;
3322}
3323
3324void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
3325{
3326 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
3327
3328 bus->devs[bus->dev_count++] = dev;
3329}
3330
Avi Kivity774c47f2007-02-12 00:54:47 -08003331static struct notifier_block kvm_cpu_notifier = {
3332 .notifier_call = kvm_cpu_hotplug,
3333 .priority = 20, /* must be > scheduler priority */
3334};
3335
Avi Kivity1165f5f2007-04-19 17:27:43 +03003336static u64 stat_get(void *_offset)
3337{
3338 unsigned offset = (long)_offset;
3339 u64 total = 0;
3340 struct kvm *kvm;
3341 struct kvm_vcpu *vcpu;
3342 int i;
3343
3344 spin_lock(&kvm_lock);
3345 list_for_each_entry(kvm, &vm_list, vm_list)
3346 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003347 vcpu = kvm->vcpus[i];
3348 if (vcpu)
3349 total += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03003350 }
3351 spin_unlock(&kvm_lock);
3352 return total;
3353}
3354
Rusty Russell3dea7ca2007-08-01 10:12:22 +10003355DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n");
Avi Kivity1165f5f2007-04-19 17:27:43 +03003356
Avi Kivity6aa8b732006-12-10 02:21:36 -08003357static __init void kvm_init_debug(void)
3358{
3359 struct kvm_stats_debugfs_item *p;
3360
Al Viro8b6d44c2007-02-09 16:38:40 +00003361 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003362 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03003363 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
3364 (void *)(long)p->offset,
3365 &stat_fops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003366}
3367
3368static void kvm_exit_debug(void)
3369{
3370 struct kvm_stats_debugfs_item *p;
3371
3372 for (p = debugfs_entries; p->name; ++p)
3373 debugfs_remove(p->dentry);
3374 debugfs_remove(debugfs_dir);
3375}
3376
Avi Kivity59ae6c62007-02-12 00:54:48 -08003377static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3378{
Avi Kivity4267c412007-05-24 13:09:41 +03003379 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003380 return 0;
3381}
3382
3383static int kvm_resume(struct sys_device *dev)
3384{
Avi Kivity4267c412007-05-24 13:09:41 +03003385 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003386 return 0;
3387}
3388
3389static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01003390 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08003391 .suspend = kvm_suspend,
3392 .resume = kvm_resume,
3393};
3394
3395static struct sys_device kvm_sysdev = {
3396 .id = 0,
3397 .cls = &kvm_sysdev_class,
3398};
3399
Avi Kivity6aa8b732006-12-10 02:21:36 -08003400hpa_t bad_page_address;
3401
Avi Kivity15ad7142007-07-11 18:17:21 +03003402static inline
3403struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
3404{
3405 return container_of(pn, struct kvm_vcpu, preempt_notifier);
3406}
3407
3408static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
3409{
3410 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3411
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003412 kvm_x86_ops->vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003413}
3414
3415static void kvm_sched_out(struct preempt_notifier *pn,
3416 struct task_struct *next)
3417{
3418 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3419
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003420 kvm_x86_ops->vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003421}
3422
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003423int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10003424 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003425{
3426 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003427 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003428
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003429 if (kvm_x86_ops) {
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08003430 printk(KERN_ERR "kvm: already loaded the other module\n");
3431 return -EEXIST;
3432 }
3433
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003434 if (!ops->cpu_has_kvm_support()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003435 printk(KERN_ERR "kvm: no hardware support\n");
3436 return -EOPNOTSUPP;
3437 }
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003438 if (ops->disabled_by_bios()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003439 printk(KERN_ERR "kvm: disabled by bios\n");
3440 return -EOPNOTSUPP;
3441 }
3442
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003443 kvm_x86_ops = ops;
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003444
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003445 r = kvm_x86_ops->hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003446 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02003447 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003448
Yang, Sheng002c7f72007-07-31 14:23:01 +03003449 for_each_online_cpu(cpu) {
3450 smp_call_function_single(cpu,
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003451 kvm_x86_ops->check_processor_compatibility,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003452 &r, 0, 1);
3453 if (r < 0)
3454 goto out_free_0;
3455 }
3456
Avi Kivity1b6c0162007-05-24 13:03:52 +03003457 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003458 r = register_cpu_notifier(&kvm_cpu_notifier);
3459 if (r)
3460 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003461 register_reboot_notifier(&kvm_reboot_notifier);
3462
Avi Kivity59ae6c62007-02-12 00:54:48 -08003463 r = sysdev_class_register(&kvm_sysdev_class);
3464 if (r)
3465 goto out_free_2;
3466
3467 r = sysdev_register(&kvm_sysdev);
3468 if (r)
3469 goto out_free_3;
3470
Rusty Russellc16f8622007-07-30 21:12:19 +10003471 /* A kmem cache lets us meet the alignment requirements of fx_save. */
3472 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
3473 __alignof__(struct kvm_vcpu), 0, 0);
3474 if (!kvm_vcpu_cache) {
3475 r = -ENOMEM;
3476 goto out_free_4;
3477 }
3478
Avi Kivity6aa8b732006-12-10 02:21:36 -08003479 kvm_chardev_ops.owner = module;
3480
3481 r = misc_register(&kvm_dev);
3482 if (r) {
3483 printk (KERN_ERR "kvm: misc device register failed\n");
3484 goto out_free;
3485 }
3486
Avi Kivity15ad7142007-07-11 18:17:21 +03003487 kvm_preempt_ops.sched_in = kvm_sched_in;
3488 kvm_preempt_ops.sched_out = kvm_sched_out;
3489
Avi Kivity6aa8b732006-12-10 02:21:36 -08003490 return r;
3491
3492out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10003493 kmem_cache_destroy(kvm_vcpu_cache);
3494out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08003495 sysdev_unregister(&kvm_sysdev);
3496out_free_3:
3497 sysdev_class_unregister(&kvm_sysdev_class);
3498out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003499 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08003500 unregister_cpu_notifier(&kvm_cpu_notifier);
3501out_free_1:
Avi Kivity1b6c0162007-05-24 13:03:52 +03003502 on_each_cpu(hardware_disable, NULL, 0, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03003503out_free_0:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003504 kvm_x86_ops->hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02003505out:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003506 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003507 return r;
3508}
3509
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003510void kvm_exit_x86(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003511{
3512 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10003513 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003514 sysdev_unregister(&kvm_sysdev);
3515 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003516 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003517 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003518 on_each_cpu(hardware_disable, NULL, 0, 1);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003519 kvm_x86_ops->hardware_unsetup();
3520 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003521}
3522
3523static __init int kvm_init(void)
3524{
3525 static struct page *bad_page;
Avi Kivity37e29d92007-02-20 14:07:37 +02003526 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003527
Avi Kivityb5a33a72007-04-15 16:31:09 +03003528 r = kvm_mmu_module_init();
3529 if (r)
3530 goto out4;
3531
Avi Kivity6aa8b732006-12-10 02:21:36 -08003532 kvm_init_debug();
3533
Michael Riepebf591b22006-12-22 01:05:36 -08003534 kvm_init_msr_list();
3535
Avi Kivity6aa8b732006-12-10 02:21:36 -08003536 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
3537 r = -ENOMEM;
3538 goto out;
3539 }
3540
3541 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
3542 memset(__va(bad_page_address), 0, PAGE_SIZE);
3543
Avi Kivity58e690e2007-02-26 16:29:43 +02003544 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003545
3546out:
3547 kvm_exit_debug();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003548 kvm_mmu_module_exit();
3549out4:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003550 return r;
3551}
3552
3553static __exit void kvm_exit(void)
3554{
3555 kvm_exit_debug();
3556 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
Avi Kivityb5a33a72007-04-15 16:31:09 +03003557 kvm_mmu_module_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003558}
3559
3560module_init(kvm_init)
3561module_exit(kvm_exit)
3562
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003563EXPORT_SYMBOL_GPL(kvm_init_x86);
3564EXPORT_SYMBOL_GPL(kvm_exit_x86);