blob: b19734606cd9d6f6e1dac3bd97181fdf873d4ef1 [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 Russell707d92f2007-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{
Rusty Russelld5894442007-10-08 10:48:30 +1000271 kvm_free_lapic(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000272 kvm_mmu_destroy(vcpu);
273 free_page((unsigned long)vcpu->pio_data);
274 free_page((unsigned long)vcpu->run);
275}
276EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
277
Avi Kivityf17abe92007-02-21 19:28:04 +0200278static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800279{
280 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800281
282 if (!kvm)
Avi Kivityf17abe92007-02-21 19:28:04 +0200283 return ERR_PTR(-ENOMEM);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800284
Eddie Dong74906342007-06-19 18:05:03 +0300285 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800286 mutex_init(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800287 INIT_LIST_HEAD(&kvm->active_mmu_pages);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400288 kvm_io_bus_init(&kvm->mmio_bus);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000289 spin_lock(&kvm_lock);
290 list_add(&kvm->vm_list, &vm_list);
291 spin_unlock(&kvm_lock);
Avi Kivityf17abe92007-02-21 19:28:04 +0200292 return kvm;
293}
294
Avi Kivity6aa8b732006-12-10 02:21:36 -0800295/*
296 * Free any memory in @free but not in @dont.
297 */
298static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
299 struct kvm_memory_slot *dont)
300{
301 int i;
302
303 if (!dont || free->phys_mem != dont->phys_mem)
304 if (free->phys_mem) {
305 for (i = 0; i < free->npages; ++i)
Avi Kivity55a54f72006-12-29 16:49:58 -0800306 if (free->phys_mem[i])
307 __free_page(free->phys_mem[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800308 vfree(free->phys_mem);
309 }
Izik Eidus290fc382007-09-27 14:11:22 +0200310 if (!dont || free->rmap != dont->rmap)
311 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800312
313 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
314 vfree(free->dirty_bitmap);
315
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 Kivity1342d352007-01-05 16:36:39 -0800401 int ret;
Rusty Russellc820c2a2007-07-25 13:29:51 +1000402 u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800403
Shaohua Li11ec2802007-07-23 14:51:37 +0800404 mutex_lock(&vcpu->kvm->lock);
Izik Eidus195aefd2007-10-01 22:14:18 +0200405 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
406 offset * sizeof(u64), sizeof(pdpte));
407 if (ret < 0) {
Rusty Russellc820c2a2007-07-25 13:29:51 +1000408 ret = 0;
409 goto out;
410 }
Rusty Russellc820c2a2007-07-25 13:29:51 +1000411 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
412 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
Avi Kivity1342d352007-01-05 16:36:39 -0800413 ret = 0;
414 goto out;
415 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800416 }
Rusty Russellc820c2a2007-07-25 13:29:51 +1000417 ret = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800418
Rusty Russellc820c2a2007-07-25 13:29:51 +1000419 memcpy(vcpu->pdptrs, pdpte, sizeof(vcpu->pdptrs));
Avi Kivity1342d352007-01-05 16:36:39 -0800420out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800421 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800422
Avi Kivity1342d352007-01-05 16:36:39 -0800423 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424}
425
426void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
427{
Rusty Russell707d92f2007-07-17 23:19:08 +1000428 if (cr0 & CR0_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800429 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
430 cr0, vcpu->cr0);
431 inject_gp(vcpu);
432 return;
433 }
434
Rusty Russell707d92f2007-07-17 23:19:08 +1000435 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800436 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
437 inject_gp(vcpu);
438 return;
439 }
440
Rusty Russell707d92f2007-07-17 23:19:08 +1000441 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800442 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
443 "and a clear PE flag\n");
444 inject_gp(vcpu);
445 return;
446 }
447
Rusty Russell707d92f2007-07-17 23:19:08 +1000448 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800449#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800450 if ((vcpu->shadow_efer & EFER_LME)) {
451 int cs_db, cs_l;
452
453 if (!is_pae(vcpu)) {
454 printk(KERN_DEBUG "set_cr0: #GP, start paging "
455 "in long mode while PAE is disabled\n");
456 inject_gp(vcpu);
457 return;
458 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300459 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800460 if (cs_l) {
461 printk(KERN_DEBUG "set_cr0: #GP, start paging "
462 "in long mode while CS.L == 1\n");
463 inject_gp(vcpu);
464 return;
465
466 }
467 } else
468#endif
Avi Kivity1342d352007-01-05 16:36:39 -0800469 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800470 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
471 "reserved bits\n");
472 inject_gp(vcpu);
473 return;
474 }
475
476 }
477
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300478 kvm_x86_ops->set_cr0(vcpu, cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800479 vcpu->cr0 = cr0;
480
Shaohua Li11ec2802007-07-23 14:51:37 +0800481 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800482 kvm_mmu_reset_context(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800483 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800484 return;
485}
486EXPORT_SYMBOL_GPL(set_cr0);
487
488void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
489{
490 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
491}
492EXPORT_SYMBOL_GPL(lmsw);
493
494void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
495{
Rusty Russell66aee912007-07-17 23:34:16 +1000496 if (cr4 & CR4_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800497 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
498 inject_gp(vcpu);
499 return;
500 }
501
Avi Kivitya9058ec2006-12-29 16:49:37 -0800502 if (is_long_mode(vcpu)) {
Rusty Russell66aee912007-07-17 23:34:16 +1000503 if (!(cr4 & X86_CR4_PAE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800504 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
505 "in long mode\n");
506 inject_gp(vcpu);
507 return;
508 }
Rusty Russell66aee912007-07-17 23:34:16 +1000509 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
Avi Kivity1342d352007-01-05 16:36:39 -0800510 && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800511 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
512 inject_gp(vcpu);
Rusty Russell310bc762007-07-23 17:11:02 +1000513 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800514 }
515
Rusty Russell66aee912007-07-17 23:34:16 +1000516 if (cr4 & X86_CR4_VMXE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800517 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
518 inject_gp(vcpu);
519 return;
520 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300521 kvm_x86_ops->set_cr4(vcpu, cr4);
Rusty Russell81f50e32007-09-06 01:20:38 +1000522 vcpu->cr4 = cr4;
Shaohua Li11ec2802007-07-23 14:51:37 +0800523 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800524 kvm_mmu_reset_context(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800525 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800526}
527EXPORT_SYMBOL_GPL(set_cr4);
528
529void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
530{
Avi Kivitya9058ec2006-12-29 16:49:37 -0800531 if (is_long_mode(vcpu)) {
Rusty Russellf802a302007-07-17 23:32:55 +1000532 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800533 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
534 inject_gp(vcpu);
535 return;
536 }
537 } else {
Rusty Russellf802a302007-07-17 23:32:55 +1000538 if (is_pae(vcpu)) {
539 if (cr3 & CR3_PAE_RESERVED_BITS) {
540 printk(KERN_DEBUG
541 "set_cr3: #GP, reserved bits\n");
542 inject_gp(vcpu);
543 return;
544 }
545 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
546 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
547 "reserved bits\n");
548 inject_gp(vcpu);
549 return;
550 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800551 }
Ryan Harper21764862007-09-18 14:05:16 -0500552 /*
553 * We don't check reserved bits in nonpae mode, because
554 * this isn't enforced, and VMware depends on this.
555 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800556 }
557
Shaohua Li11ec2802007-07-23 14:51:37 +0800558 mutex_lock(&vcpu->kvm->lock);
Ingo Molnard21225e2007-01-05 16:36:59 -0800559 /*
560 * Does the new cr3 value map to physical memory? (Note, we
561 * catch an invalid cr3 even in real-mode, because it would
562 * cause trouble later on when we turn on paging anyway.)
563 *
564 * A real CPU would silently accept an invalid cr3 and would
565 * attempt to use it - with largely undefined (and often hard
566 * to debug) behavior on the guest side.
567 */
568 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
569 inject_gp(vcpu);
Rusty Russellfb764412007-07-31 20:45:03 +1000570 else {
571 vcpu->cr3 = cr3;
Ingo Molnard21225e2007-01-05 16:36:59 -0800572 vcpu->mmu.new_cr3(vcpu);
Rusty Russellfb764412007-07-31 20:45:03 +1000573 }
Shaohua Li11ec2802007-07-23 14:51:37 +0800574 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800575}
576EXPORT_SYMBOL_GPL(set_cr3);
577
578void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
579{
Rusty Russell7075bc82007-07-17 23:37:17 +1000580 if (cr8 & CR8_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800581 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
582 inject_gp(vcpu);
583 return;
584 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300585 if (irqchip_in_kernel(vcpu->kvm))
586 kvm_lapic_set_tpr(vcpu, cr8);
587 else
588 vcpu->cr8 = cr8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800589}
590EXPORT_SYMBOL_GPL(set_cr8);
591
Eddie Dong7017fc32007-07-18 11:34:57 +0300592unsigned long get_cr8(struct kvm_vcpu *vcpu)
593{
Eddie Dong97222cc2007-09-12 10:58:04 +0300594 if (irqchip_in_kernel(vcpu->kvm))
595 return kvm_lapic_get_cr8(vcpu);
596 else
597 return vcpu->cr8;
Eddie Dong7017fc32007-07-18 11:34:57 +0300598}
599EXPORT_SYMBOL_GPL(get_cr8);
600
601u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
602{
Eddie Dong97222cc2007-09-12 10:58:04 +0300603 if (irqchip_in_kernel(vcpu->kvm))
604 return vcpu->apic_base;
605 else
606 return vcpu->apic_base;
Eddie Dong7017fc32007-07-18 11:34:57 +0300607}
608EXPORT_SYMBOL_GPL(kvm_get_apic_base);
609
610void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
611{
Eddie Dong97222cc2007-09-12 10:58:04 +0300612 /* TODO: reserve bits check */
613 if (irqchip_in_kernel(vcpu->kvm))
614 kvm_lapic_set_base(vcpu, data);
615 else
616 vcpu->apic_base = data;
Eddie Dong7017fc32007-07-18 11:34:57 +0300617}
618EXPORT_SYMBOL_GPL(kvm_set_apic_base);
619
Avi Kivity6aa8b732006-12-10 02:21:36 -0800620void fx_init(struct kvm_vcpu *vcpu)
621{
Rusty Russellb114b082007-07-30 21:13:43 +1000622 unsigned after_mxcsr_mask;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800623
Rusty Russell9bd01502007-07-30 16:29:56 +1000624 /* Initialize guest FPU by resetting ours and saving into guest's */
625 preempt_disable();
Rusty Russellb114b082007-07-30 21:13:43 +1000626 fx_save(&vcpu->host_fx_image);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800627 fpu_init();
Rusty Russellb114b082007-07-30 21:13:43 +1000628 fx_save(&vcpu->guest_fx_image);
629 fx_restore(&vcpu->host_fx_image);
Rusty Russell9bd01502007-07-30 16:29:56 +1000630 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800631
Amit Shah380102c2007-08-25 11:35:52 +0300632 vcpu->cr0 |= X86_CR0_ET;
Rusty Russellb114b082007-07-30 21:13:43 +1000633 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
634 vcpu->guest_fx_image.mxcsr = 0x1f80;
635 memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask,
636 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800637}
638EXPORT_SYMBOL_GPL(fx_init);
639
640/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800641 * Allocate some memory and give it an address in the guest physical address
642 * space.
643 *
644 * Discontiguous memory is allowed, mostly for framebuffers.
645 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200646static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
647 struct kvm_memory_region *mem)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800648{
649 int r;
650 gfn_t base_gfn;
651 unsigned long npages;
652 unsigned long i;
653 struct kvm_memory_slot *memslot;
654 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800655
656 r = -EINVAL;
657 /* General sanity checks */
658 if (mem->memory_size & (PAGE_SIZE - 1))
659 goto out;
660 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
661 goto out;
662 if (mem->slot >= KVM_MEMORY_SLOTS)
663 goto out;
664 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
665 goto out;
666
667 memslot = &kvm->memslots[mem->slot];
668 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
669 npages = mem->memory_size >> PAGE_SHIFT;
670
671 if (!npages)
672 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
673
Shaohua Li11ec2802007-07-23 14:51:37 +0800674 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800675
Avi Kivity6aa8b732006-12-10 02:21:36 -0800676 new = old = *memslot;
677
678 new.base_gfn = base_gfn;
679 new.npages = npages;
680 new.flags = mem->flags;
681
682 /* Disallow changing a memory slot's size. */
683 r = -EINVAL;
684 if (npages && old.npages && npages != old.npages)
685 goto out_unlock;
686
687 /* Check for overlaps */
688 r = -EEXIST;
689 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
690 struct kvm_memory_slot *s = &kvm->memslots[i];
691
692 if (s == memslot)
693 continue;
694 if (!((base_gfn + npages <= s->base_gfn) ||
695 (base_gfn >= s->base_gfn + s->npages)))
696 goto out_unlock;
697 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800698
699 /* Deallocate if slot is being removed */
700 if (!npages)
Al Viro8b6d44c2007-02-09 16:38:40 +0000701 new.phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800702
703 /* Free page dirty bitmap if unneeded */
704 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000705 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800706
707 r = -ENOMEM;
708
709 /* Allocate if a slot is being created */
710 if (npages && !new.phys_mem) {
711 new.phys_mem = vmalloc(npages * sizeof(struct page *));
712
713 if (!new.phys_mem)
Laurent Vivier0d8d2bd2007-08-30 14:56:21 +0200714 goto out_unlock;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800715
Izik Eidus290fc382007-09-27 14:11:22 +0200716 new.rmap = vmalloc(npages * sizeof(struct page*));
717
718 if (!new.rmap)
719 goto out_unlock;
720
Avi Kivity6aa8b732006-12-10 02:21:36 -0800721 memset(new.phys_mem, 0, npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200722 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800723 for (i = 0; i < npages; ++i) {
724 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
725 | __GFP_ZERO);
726 if (!new.phys_mem[i])
Laurent Vivier0d8d2bd2007-08-30 14:56:21 +0200727 goto out_unlock;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800728 }
729 }
730
731 /* Allocate page dirty bitmap if needed */
732 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
733 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
734
735 new.dirty_bitmap = vmalloc(dirty_bytes);
736 if (!new.dirty_bitmap)
Laurent Vivier0d8d2bd2007-08-30 14:56:21 +0200737 goto out_unlock;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800738 memset(new.dirty_bitmap, 0, dirty_bytes);
739 }
740
Avi Kivity6aa8b732006-12-10 02:21:36 -0800741 if (mem->slot >= kvm->nmemslots)
742 kvm->nmemslots = mem->slot + 1;
743
Izik Eidus82ce2c92007-10-02 18:52:55 +0200744 if (!kvm->n_requested_mmu_pages) {
745 unsigned int n_pages;
746
747 if (npages) {
748 n_pages = npages * KVM_PERMILLE_MMU_PAGES / 1000;
749 kvm_mmu_change_mmu_pages(kvm, kvm->n_alloc_mmu_pages +
750 n_pages);
751 } else {
752 unsigned int nr_mmu_pages;
753
754 n_pages = old.npages * KVM_PERMILLE_MMU_PAGES / 1000;
755 nr_mmu_pages = kvm->n_alloc_mmu_pages - n_pages;
756 nr_mmu_pages = max(nr_mmu_pages,
757 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
758 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
759 }
760 }
761
Avi Kivity6aa8b732006-12-10 02:21:36 -0800762 *memslot = new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800763
Avi Kivity90cb0522007-07-17 13:04:56 +0300764 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
765 kvm_flush_remote_tlbs(kvm);
766
Shaohua Li11ec2802007-07-23 14:51:37 +0800767 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800768
Avi Kivity6aa8b732006-12-10 02:21:36 -0800769 kvm_free_physmem_slot(&old, &new);
770 return 0;
771
772out_unlock:
Shaohua Li11ec2802007-07-23 14:51:37 +0800773 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800774 kvm_free_physmem_slot(&new, &old);
775out:
776 return r;
777}
778
Izik Eidus82ce2c92007-10-02 18:52:55 +0200779static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
780 u32 kvm_nr_mmu_pages)
781{
782 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
783 return -EINVAL;
784
785 mutex_lock(&kvm->lock);
786
787 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
788 kvm->n_requested_mmu_pages = kvm_nr_mmu_pages;
789
790 mutex_unlock(&kvm->lock);
791 return 0;
792}
793
794static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
795{
796 return kvm->n_alloc_mmu_pages;
797}
798
Avi Kivity6aa8b732006-12-10 02:21:36 -0800799/*
800 * Get (and clear) the dirty memory log for a memory slot.
801 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200802static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
803 struct kvm_dirty_log *log)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800804{
805 struct kvm_memory_slot *memslot;
806 int r, i;
807 int n;
808 unsigned long any = 0;
809
Shaohua Li11ec2802007-07-23 14:51:37 +0800810 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800811
Avi Kivity6aa8b732006-12-10 02:21:36 -0800812 r = -EINVAL;
813 if (log->slot >= KVM_MEMORY_SLOTS)
814 goto out;
815
816 memslot = &kvm->memslots[log->slot];
817 r = -ENOENT;
818 if (!memslot->dirty_bitmap)
819 goto out;
820
Uri Lublincd1a4a92007-02-22 16:43:09 +0200821 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800822
Uri Lublincd1a4a92007-02-22 16:43:09 +0200823 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800824 any = memslot->dirty_bitmap[i];
825
826 r = -EFAULT;
827 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
828 goto out;
829
Rusty Russell39214912007-07-31 19:57:47 +1000830 /* If nothing is dirty, don't bother messing with page tables. */
831 if (any) {
Rusty Russell39214912007-07-31 19:57:47 +1000832 kvm_mmu_slot_remove_write_access(kvm, log->slot);
833 kvm_flush_remote_tlbs(kvm);
834 memset(memslot->dirty_bitmap, 0, n);
Rusty Russell39214912007-07-31 19:57:47 +1000835 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800836
837 r = 0;
838
839out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800840 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800841 return r;
842}
843
Avi Kivitye8207542007-03-30 16:54:30 +0300844/*
845 * Set a new alias region. Aliases map a portion of physical memory into
846 * another portion. This is useful for memory windows, for example the PC
847 * VGA region.
848 */
849static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
850 struct kvm_memory_alias *alias)
851{
852 int r, n;
853 struct kvm_mem_alias *p;
854
855 r = -EINVAL;
856 /* General sanity checks */
857 if (alias->memory_size & (PAGE_SIZE - 1))
858 goto out;
859 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
860 goto out;
861 if (alias->slot >= KVM_ALIAS_SLOTS)
862 goto out;
863 if (alias->guest_phys_addr + alias->memory_size
864 < alias->guest_phys_addr)
865 goto out;
866 if (alias->target_phys_addr + alias->memory_size
867 < alias->target_phys_addr)
868 goto out;
869
Shaohua Li11ec2802007-07-23 14:51:37 +0800870 mutex_lock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300871
872 p = &kvm->aliases[alias->slot];
873 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
874 p->npages = alias->memory_size >> PAGE_SHIFT;
875 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
876
877 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
878 if (kvm->aliases[n - 1].npages)
879 break;
880 kvm->naliases = n;
881
Avi Kivity90cb0522007-07-17 13:04:56 +0300882 kvm_mmu_zap_all(kvm);
Avi Kivitye8207542007-03-30 16:54:30 +0300883
Shaohua Li11ec2802007-07-23 14:51:37 +0800884 mutex_unlock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300885
886 return 0;
887
888out:
889 return r;
890}
891
He, Qing6ceb9d72007-07-26 11:05:18 +0300892static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
893{
894 int r;
895
896 r = 0;
897 switch (chip->chip_id) {
898 case KVM_IRQCHIP_PIC_MASTER:
899 memcpy (&chip->chip.pic,
900 &pic_irqchip(kvm)->pics[0],
901 sizeof(struct kvm_pic_state));
902 break;
903 case KVM_IRQCHIP_PIC_SLAVE:
904 memcpy (&chip->chip.pic,
905 &pic_irqchip(kvm)->pics[1],
906 sizeof(struct kvm_pic_state));
907 break;
He, Qing6bf9e962007-08-05 10:49:16 +0300908 case KVM_IRQCHIP_IOAPIC:
909 memcpy (&chip->chip.ioapic,
910 ioapic_irqchip(kvm),
911 sizeof(struct kvm_ioapic_state));
912 break;
He, Qing6ceb9d72007-07-26 11:05:18 +0300913 default:
914 r = -EINVAL;
915 break;
916 }
917 return r;
918}
919
920static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
921{
922 int r;
923
924 r = 0;
925 switch (chip->chip_id) {
926 case KVM_IRQCHIP_PIC_MASTER:
927 memcpy (&pic_irqchip(kvm)->pics[0],
928 &chip->chip.pic,
929 sizeof(struct kvm_pic_state));
930 break;
931 case KVM_IRQCHIP_PIC_SLAVE:
932 memcpy (&pic_irqchip(kvm)->pics[1],
933 &chip->chip.pic,
934 sizeof(struct kvm_pic_state));
935 break;
He, Qing6bf9e962007-08-05 10:49:16 +0300936 case KVM_IRQCHIP_IOAPIC:
937 memcpy (ioapic_irqchip(kvm),
938 &chip->chip.ioapic,
939 sizeof(struct kvm_ioapic_state));
940 break;
He, Qing6ceb9d72007-07-26 11:05:18 +0300941 default:
942 r = -EINVAL;
943 break;
944 }
945 kvm_pic_update_irq(pic_irqchip(kvm));
946 return r;
947}
948
Izik Eidus290fc382007-09-27 14:11:22 +0200949gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
Avi Kivitye8207542007-03-30 16:54:30 +0300950{
951 int i;
952 struct kvm_mem_alias *alias;
953
954 for (i = 0; i < kvm->naliases; ++i) {
955 alias = &kvm->aliases[i];
956 if (gfn >= alias->base_gfn
957 && gfn < alias->base_gfn + alias->npages)
958 return alias->target_gfn + gfn - alias->base_gfn;
959 }
960 return gfn;
961}
962
963static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800964{
965 int i;
966
967 for (i = 0; i < kvm->nmemslots; ++i) {
968 struct kvm_memory_slot *memslot = &kvm->memslots[i];
969
970 if (gfn >= memslot->base_gfn
971 && gfn < memslot->base_gfn + memslot->npages)
972 return memslot;
973 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000974 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800975}
Avi Kivitye8207542007-03-30 16:54:30 +0300976
977struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
978{
979 gfn = unalias_gfn(kvm, gfn);
980 return __gfn_to_memslot(kvm, gfn);
981}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982
Avi Kivity954bbbc22007-03-30 14:02:32 +0300983struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
984{
985 struct kvm_memory_slot *slot;
986
Avi Kivitye8207542007-03-30 16:54:30 +0300987 gfn = unalias_gfn(kvm, gfn);
988 slot = __gfn_to_memslot(kvm, gfn);
Avi Kivity954bbbc22007-03-30 14:02:32 +0300989 if (!slot)
990 return NULL;
991 return slot->phys_mem[gfn - slot->base_gfn];
992}
993EXPORT_SYMBOL_GPL(gfn_to_page);
994
Izik Eidus195aefd2007-10-01 22:14:18 +0200995static int next_segment(unsigned long len, int offset)
996{
997 if (len > PAGE_SIZE - offset)
998 return PAGE_SIZE - offset;
999 else
1000 return len;
1001}
1002
1003int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1004 int len)
1005{
1006 void *page_virt;
1007 struct page *page;
1008
1009 page = gfn_to_page(kvm, gfn);
1010 if (!page)
1011 return -EFAULT;
1012 page_virt = kmap_atomic(page, KM_USER0);
1013
1014 memcpy(data, page_virt + offset, len);
1015
1016 kunmap_atomic(page_virt, KM_USER0);
1017 return 0;
1018}
1019EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1020
1021int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1022{
1023 gfn_t gfn = gpa >> PAGE_SHIFT;
1024 int seg;
1025 int offset = offset_in_page(gpa);
1026 int ret;
1027
1028 while ((seg = next_segment(len, offset)) != 0) {
1029 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1030 if (ret < 0)
1031 return ret;
1032 offset = 0;
1033 len -= seg;
1034 data += seg;
1035 ++gfn;
1036 }
1037 return 0;
1038}
1039EXPORT_SYMBOL_GPL(kvm_read_guest);
1040
1041int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1042 int offset, int len)
1043{
1044 void *page_virt;
1045 struct page *page;
1046
1047 page = gfn_to_page(kvm, gfn);
1048 if (!page)
1049 return -EFAULT;
1050 page_virt = kmap_atomic(page, KM_USER0);
1051
1052 memcpy(page_virt + offset, data, len);
1053
1054 kunmap_atomic(page_virt, KM_USER0);
1055 mark_page_dirty(kvm, gfn);
1056 return 0;
1057}
1058EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1059
1060int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1061 unsigned long len)
1062{
1063 gfn_t gfn = gpa >> PAGE_SHIFT;
1064 int seg;
1065 int offset = offset_in_page(gpa);
1066 int ret;
1067
1068 while ((seg = next_segment(len, offset)) != 0) {
1069 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1070 if (ret < 0)
1071 return ret;
1072 offset = 0;
1073 len -= seg;
1074 data += seg;
1075 ++gfn;
1076 }
1077 return 0;
1078}
1079
1080int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1081{
1082 void *page_virt;
1083 struct page *page;
1084
1085 page = gfn_to_page(kvm, gfn);
1086 if (!page)
1087 return -EFAULT;
1088 page_virt = kmap_atomic(page, KM_USER0);
1089
1090 memset(page_virt + offset, 0, len);
1091
1092 kunmap_atomic(page_virt, KM_USER0);
1093 return 0;
1094}
1095EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1096
1097int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1098{
1099 gfn_t gfn = gpa >> PAGE_SHIFT;
1100 int seg;
1101 int offset = offset_in_page(gpa);
1102 int ret;
1103
1104 while ((seg = next_segment(len, offset)) != 0) {
1105 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1106 if (ret < 0)
1107 return ret;
1108 offset = 0;
1109 len -= seg;
1110 ++gfn;
1111 }
1112 return 0;
1113}
1114EXPORT_SYMBOL_GPL(kvm_clear_guest);
1115
Rusty Russell7e9d6192007-07-31 20:41:14 +10001116/* WARNING: Does not work on aliased pages. */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001117void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1118{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001119 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001120
Rusty Russell7e9d6192007-07-31 20:41:14 +10001121 memslot = __gfn_to_memslot(kvm, gfn);
1122 if (memslot && memslot->dirty_bitmap) {
1123 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001124
Rusty Russell7e9d6192007-07-31 20:41:14 +10001125 /* avoid RMW */
1126 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1127 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001128 }
1129}
1130
Laurent Viviere7d5d762007-07-30 13:41:19 +03001131int emulator_read_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001132 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001133 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001134 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001135{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001136 void *data = val;
1137
1138 while (bytes) {
1139 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1140 unsigned offset = addr & (PAGE_SIZE-1);
1141 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
Izik Eidus195aefd2007-10-01 22:14:18 +02001142 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001143
1144 if (gpa == UNMAPPED_GVA)
1145 return X86EMUL_PROPAGATE_FAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001146 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
1147 if (ret < 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001148 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001149
1150 bytes -= tocopy;
1151 data += tocopy;
1152 addr += tocopy;
1153 }
1154
1155 return X86EMUL_CONTINUE;
1156}
Laurent Viviere7d5d762007-07-30 13:41:19 +03001157EXPORT_SYMBOL_GPL(emulator_read_std);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001158
1159static int emulator_write_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001160 const void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001161 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001162 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001163{
Rusty Russellf0242472007-08-01 10:48:02 +10001164 pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001165 return X86EMUL_UNHANDLEABLE;
1166}
1167
Eddie Dong97222cc2007-09-12 10:58:04 +03001168/*
1169 * Only apic need an MMIO device hook, so shortcut now..
1170 */
1171static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
1172 gpa_t addr)
1173{
1174 struct kvm_io_device *dev;
1175
1176 if (vcpu->apic) {
1177 dev = &vcpu->apic->dev;
1178 if (dev->in_range(dev, addr))
1179 return dev;
1180 }
1181 return NULL;
1182}
1183
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001184static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1185 gpa_t addr)
1186{
Eddie Dong97222cc2007-09-12 10:58:04 +03001187 struct kvm_io_device *dev;
1188
1189 dev = vcpu_find_pervcpu_dev(vcpu, addr);
1190 if (dev == NULL)
1191 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1192 return dev;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001193}
1194
Eddie Dong74906342007-06-19 18:05:03 +03001195static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
1196 gpa_t addr)
1197{
1198 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
1199}
1200
Avi Kivity6aa8b732006-12-10 02:21:36 -08001201static int emulator_read_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001202 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001203 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001204 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001205{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001206 struct kvm_io_device *mmio_dev;
1207 gpa_t gpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001208
1209 if (vcpu->mmio_read_completed) {
1210 memcpy(val, vcpu->mmio_data, bytes);
1211 vcpu->mmio_read_completed = 0;
1212 return X86EMUL_CONTINUE;
Laurent Viviercebff022007-07-30 13:35:24 +03001213 } else if (emulator_read_std(addr, val, bytes, vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001214 == X86EMUL_CONTINUE)
1215 return X86EMUL_CONTINUE;
Avi Kivityd27d4ac2007-02-19 14:37:46 +02001216
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001217 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1218 if (gpa == UNMAPPED_GVA)
1219 return X86EMUL_PROPAGATE_FAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001220
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001221 /*
1222 * Is this MMIO handled locally?
1223 */
1224 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1225 if (mmio_dev) {
1226 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1227 return X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001228 }
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001229
1230 vcpu->mmio_needed = 1;
1231 vcpu->mmio_phys_addr = gpa;
1232 vcpu->mmio_size = bytes;
1233 vcpu->mmio_is_write = 0;
1234
1235 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001236}
1237
Avi Kivityda4a00f2007-01-05 16:36:44 -08001238static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity4c690a12007-04-22 15:28:19 +03001239 const void *val, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001240{
Izik Eidus195aefd2007-10-01 22:14:18 +02001241 int ret;
Avi Kivityda4a00f2007-01-05 16:36:44 -08001242
Izik Eidus195aefd2007-10-01 22:14:18 +02001243 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
1244 if (ret < 0)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001245 return 0;
Shaohua Life551882007-07-23 14:51:39 +08001246 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001247 return 1;
1248}
1249
Avi Kivityb0fcd902007-07-22 18:48:54 +03001250static int emulator_write_emulated_onepage(unsigned long addr,
1251 const void *val,
1252 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001253 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001254{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001255 struct kvm_io_device *mmio_dev;
1256 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001257
Avi Kivityc9047f52007-04-17 10:53:22 +03001258 if (gpa == UNMAPPED_GVA) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001259 kvm_x86_ops->inject_page_fault(vcpu, addr, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001260 return X86EMUL_PROPAGATE_FAULT;
Avi Kivityc9047f52007-04-17 10:53:22 +03001261 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001262
Avi Kivityda4a00f2007-01-05 16:36:44 -08001263 if (emulator_write_phys(vcpu, gpa, val, bytes))
1264 return X86EMUL_CONTINUE;
1265
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001266 /*
1267 * Is this MMIO handled locally?
1268 */
1269 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1270 if (mmio_dev) {
1271 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1272 return X86EMUL_CONTINUE;
1273 }
1274
Avi Kivity6aa8b732006-12-10 02:21:36 -08001275 vcpu->mmio_needed = 1;
1276 vcpu->mmio_phys_addr = gpa;
1277 vcpu->mmio_size = bytes;
1278 vcpu->mmio_is_write = 1;
Avi Kivity4c690a12007-04-22 15:28:19 +03001279 memcpy(vcpu->mmio_data, val, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001280
1281 return X86EMUL_CONTINUE;
1282}
1283
Laurent Viviere7d5d762007-07-30 13:41:19 +03001284int emulator_write_emulated(unsigned long addr,
Avi Kivityb0fcd902007-07-22 18:48:54 +03001285 const void *val,
1286 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001287 struct kvm_vcpu *vcpu)
Avi Kivityb0fcd902007-07-22 18:48:54 +03001288{
1289 /* Crossing a page boundary? */
1290 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1291 int rc, now;
1292
1293 now = -addr & ~PAGE_MASK;
Laurent Viviercebff022007-07-30 13:35:24 +03001294 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001295 if (rc != X86EMUL_CONTINUE)
1296 return rc;
1297 addr += now;
1298 val += now;
1299 bytes -= now;
1300 }
Laurent Viviercebff022007-07-30 13:35:24 +03001301 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001302}
Laurent Viviere7d5d762007-07-30 13:41:19 +03001303EXPORT_SYMBOL_GPL(emulator_write_emulated);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001304
Avi Kivity6aa8b732006-12-10 02:21:36 -08001305static int emulator_cmpxchg_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001306 const void *old,
1307 const void *new,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001308 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001309 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001310{
1311 static int reported;
1312
1313 if (!reported) {
1314 reported = 1;
1315 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1316 }
Laurent Viviercebff022007-07-30 13:35:24 +03001317 return emulator_write_emulated(addr, new, bytes, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001318}
1319
1320static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1321{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001322 return kvm_x86_ops->get_segment_base(vcpu, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001323}
1324
1325int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1326{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001327 return X86EMUL_CONTINUE;
1328}
1329
1330int emulate_clts(struct kvm_vcpu *vcpu)
1331{
Amit Shah404fb882007-11-19 17:57:35 +02001332 kvm_x86_ops->set_cr0(vcpu, vcpu->cr0 & ~X86_CR0_TS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001333 return X86EMUL_CONTINUE;
1334}
1335
1336int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1337{
1338 struct kvm_vcpu *vcpu = ctxt->vcpu;
1339
1340 switch (dr) {
1341 case 0 ... 3:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001342 *dest = kvm_x86_ops->get_dr(vcpu, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001343 return X86EMUL_CONTINUE;
1344 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001345 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001346 return X86EMUL_UNHANDLEABLE;
1347 }
1348}
1349
1350int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1351{
1352 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1353 int exception;
1354
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001355 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001356 if (exception) {
1357 /* FIXME: better handling */
1358 return X86EMUL_UNHANDLEABLE;
1359 }
1360 return X86EMUL_CONTINUE;
1361}
1362
Avi Kivity054b1362007-09-12 13:21:09 +03001363void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001364{
1365 static int reported;
1366 u8 opcodes[4];
Avi Kivity054b1362007-09-12 13:21:09 +03001367 unsigned long rip = vcpu->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001368 unsigned long rip_linear;
1369
Avi Kivity054b1362007-09-12 13:21:09 +03001370 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001371
1372 if (reported)
1373 return;
1374
Avi Kivity054b1362007-09-12 13:21:09 +03001375 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001376
Avi Kivity054b1362007-09-12 13:21:09 +03001377 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1378 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001379 reported = 1;
1380}
Avi Kivity054b1362007-09-12 13:21:09 +03001381EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001382
1383struct x86_emulate_ops emulate_ops = {
1384 .read_std = emulator_read_std,
1385 .write_std = emulator_write_std,
1386 .read_emulated = emulator_read_emulated,
1387 .write_emulated = emulator_write_emulated,
1388 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1389};
1390
1391int emulate_instruction(struct kvm_vcpu *vcpu,
1392 struct kvm_run *run,
1393 unsigned long cr2,
Laurent Vivier34273182007-09-18 11:27:37 +02001394 u16 error_code,
1395 int no_decode)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001396{
Laurent Viviera22436b2007-09-24 17:00:58 +02001397 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001398
Avi Kivitye7df56e2007-03-14 15:54:54 +02001399 vcpu->mmio_fault_cr2 = cr2;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001400 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001401
Avi Kivity6aa8b732006-12-10 02:21:36 -08001402 vcpu->mmio_is_write = 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03001403 vcpu->pio.string = 0;
Laurent Vivier34273182007-09-18 11:27:37 +02001404
1405 if (!no_decode) {
1406 int cs_db, cs_l;
1407 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1408
1409 vcpu->emulate_ctxt.vcpu = vcpu;
1410 vcpu->emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
1411 vcpu->emulate_ctxt.cr2 = cr2;
1412 vcpu->emulate_ctxt.mode =
1413 (vcpu->emulate_ctxt.eflags & X86_EFLAGS_VM)
1414 ? X86EMUL_MODE_REAL : cs_l
1415 ? X86EMUL_MODE_PROT64 : cs_db
1416 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1417
1418 if (vcpu->emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1419 vcpu->emulate_ctxt.cs_base = 0;
1420 vcpu->emulate_ctxt.ds_base = 0;
1421 vcpu->emulate_ctxt.es_base = 0;
1422 vcpu->emulate_ctxt.ss_base = 0;
1423 } else {
1424 vcpu->emulate_ctxt.cs_base =
1425 get_segment_base(vcpu, VCPU_SREG_CS);
1426 vcpu->emulate_ctxt.ds_base =
1427 get_segment_base(vcpu, VCPU_SREG_DS);
1428 vcpu->emulate_ctxt.es_base =
1429 get_segment_base(vcpu, VCPU_SREG_ES);
1430 vcpu->emulate_ctxt.ss_base =
1431 get_segment_base(vcpu, VCPU_SREG_SS);
1432 }
1433
1434 vcpu->emulate_ctxt.gs_base =
1435 get_segment_base(vcpu, VCPU_SREG_GS);
1436 vcpu->emulate_ctxt.fs_base =
1437 get_segment_base(vcpu, VCPU_SREG_FS);
1438
1439 r = x86_decode_insn(&vcpu->emulate_ctxt, &emulate_ops);
Laurent Viviera22436b2007-09-24 17:00:58 +02001440 if (r) {
1441 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1442 return EMULATE_DONE;
1443 return EMULATE_FAIL;
1444 }
Laurent Vivier34273182007-09-18 11:27:37 +02001445 }
1446
Laurent Viviera22436b2007-09-24 17:00:58 +02001447 r = x86_emulate_insn(&vcpu->emulate_ctxt, &emulate_ops);
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001448
Laurent Viviere70669a2007-08-05 10:36:40 +03001449 if (vcpu->pio.string)
1450 return EMULATE_DO_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001451
1452 if ((r || vcpu->mmio_is_write) && run) {
Jeff Dike8fc0d082007-07-17 12:26:59 -04001453 run->exit_reason = KVM_EXIT_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001454 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1455 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1456 run->mmio.len = vcpu->mmio_size;
1457 run->mmio.is_write = vcpu->mmio_is_write;
1458 }
1459
1460 if (r) {
Avi Kivitya4360362007-01-05 16:36:45 -08001461 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1462 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001463 if (!vcpu->mmio_needed) {
Avi Kivity054b1362007-09-12 13:21:09 +03001464 kvm_report_emulation_failure(vcpu, "mmio");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001465 return EMULATE_FAIL;
1466 }
1467 return EMULATE_DO_MMIO;
1468 }
1469
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001470 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier34273182007-09-18 11:27:37 +02001471 kvm_x86_ops->set_rflags(vcpu, vcpu->emulate_ctxt.eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001472
Avi Kivity02c83202007-04-29 15:02:17 +03001473 if (vcpu->mmio_is_write) {
1474 vcpu->mmio_needed = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001475 return EMULATE_DO_MMIO;
Avi Kivity02c83202007-04-29 15:02:17 +03001476 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001477
1478 return EMULATE_DONE;
1479}
1480EXPORT_SYMBOL_GPL(emulate_instruction);
1481
Eddie Dongb6958ce2007-07-18 12:15:21 +03001482/*
1483 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1484 */
He, Qingc5ec1532007-09-03 17:07:41 +03001485static void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001486{
1487 DECLARE_WAITQUEUE(wait, current);
1488
1489 add_wait_queue(&vcpu->wq, &wait);
1490
1491 /*
1492 * We will block until either an interrupt or a signal wakes us up
1493 */
He, Qingc5ec1532007-09-03 17:07:41 +03001494 while (!kvm_cpu_has_interrupt(vcpu)
1495 && !signal_pending(current)
1496 && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE
1497 && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) {
Eddie Dongb6958ce2007-07-18 12:15:21 +03001498 set_current_state(TASK_INTERRUPTIBLE);
1499 vcpu_put(vcpu);
1500 schedule();
1501 vcpu_load(vcpu);
1502 }
1503
He, Qingc5ec1532007-09-03 17:07:41 +03001504 __set_current_state(TASK_RUNNING);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001505 remove_wait_queue(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001506}
1507
Avi Kivityd3bef152007-06-05 15:53:05 +03001508int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1509{
Avi Kivityd3bef152007-06-05 15:53:05 +03001510 ++vcpu->stat.halt_exits;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001511 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc5ec1532007-09-03 17:07:41 +03001512 vcpu->mp_state = VCPU_MP_STATE_HALTED;
1513 kvm_vcpu_block(vcpu);
1514 if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE)
1515 return -EINTR;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001516 return 1;
1517 } else {
1518 vcpu->run->exit_reason = KVM_EXIT_HLT;
1519 return 0;
1520 }
Avi Kivityd3bef152007-06-05 15:53:05 +03001521}
1522EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1523
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001524int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
Avi Kivity270fd9b2007-02-19 14:37:47 +02001525{
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001526 unsigned long nr, a0, a1, a2, a3, ret;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001527
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001528 kvm_x86_ops->cache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001529
1530 nr = vcpu->regs[VCPU_REGS_RAX];
1531 a0 = vcpu->regs[VCPU_REGS_RBX];
1532 a1 = vcpu->regs[VCPU_REGS_RCX];
1533 a2 = vcpu->regs[VCPU_REGS_RDX];
1534 a3 = vcpu->regs[VCPU_REGS_RSI];
1535
1536 if (!is_long_mode(vcpu)) {
1537 nr &= 0xFFFFFFFF;
1538 a0 &= 0xFFFFFFFF;
1539 a1 &= 0xFFFFFFFF;
1540 a2 &= 0xFFFFFFFF;
1541 a3 &= 0xFFFFFFFF;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001542 }
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001543
Avi Kivity270fd9b2007-02-19 14:37:47 +02001544 switch (nr) {
1545 default:
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001546 ret = -KVM_ENOSYS;
1547 break;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001548 }
1549 vcpu->regs[VCPU_REGS_RAX] = ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001550 kvm_x86_ops->decache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001551 return 0;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001552}
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001553EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
1554
1555int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
1556{
1557 char instruction[3];
1558 int ret = 0;
1559
1560 mutex_lock(&vcpu->kvm->lock);
1561
1562 /*
1563 * Blow out the MMU to ensure that no other VCPU has an active mapping
1564 * to ensure that the updated hypercall appears atomically across all
1565 * VCPUs.
1566 */
1567 kvm_mmu_zap_all(vcpu->kvm);
1568
1569 kvm_x86_ops->cache_regs(vcpu);
1570 kvm_x86_ops->patch_hypercall(vcpu, instruction);
1571 if (emulator_write_emulated(vcpu->rip, instruction, 3, vcpu)
1572 != X86EMUL_CONTINUE)
1573 ret = -EFAULT;
1574
1575 mutex_unlock(&vcpu->kvm->lock);
1576
1577 return ret;
1578}
Avi Kivity270fd9b2007-02-19 14:37:47 +02001579
Avi Kivity6aa8b732006-12-10 02:21:36 -08001580static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1581{
1582 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1583}
1584
1585void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1586{
1587 struct descriptor_table dt = { limit, base };
1588
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001589 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001590}
1591
1592void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1593{
1594 struct descriptor_table dt = { limit, base };
1595
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001596 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001597}
1598
1599void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1600 unsigned long *rflags)
1601{
1602 lmsw(vcpu, msw);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001603 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001604}
1605
1606unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1607{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001608 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001609 switch (cr) {
1610 case 0:
1611 return vcpu->cr0;
1612 case 2:
1613 return vcpu->cr2;
1614 case 3:
1615 return vcpu->cr3;
1616 case 4:
1617 return vcpu->cr4;
1618 default:
1619 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1620 return 0;
1621 }
1622}
1623
1624void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1625 unsigned long *rflags)
1626{
1627 switch (cr) {
1628 case 0:
1629 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001630 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001631 break;
1632 case 2:
1633 vcpu->cr2 = val;
1634 break;
1635 case 3:
1636 set_cr3(vcpu, val);
1637 break;
1638 case 4:
1639 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1640 break;
1641 default:
1642 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1643 }
1644}
1645
Avi Kivity3bab1f52006-12-29 16:49:48 -08001646int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1647{
1648 u64 data;
1649
1650 switch (msr) {
1651 case 0xc0010010: /* SYSCFG */
1652 case 0xc0010015: /* HWCR */
1653 case MSR_IA32_PLATFORM_ID:
1654 case MSR_IA32_P5_MC_ADDR:
1655 case MSR_IA32_P5_MC_TYPE:
1656 case MSR_IA32_MC0_CTL:
1657 case MSR_IA32_MCG_STATUS:
1658 case MSR_IA32_MCG_CAP:
1659 case MSR_IA32_MC0_MISC:
1660 case MSR_IA32_MC0_MISC+4:
1661 case MSR_IA32_MC0_MISC+8:
1662 case MSR_IA32_MC0_MISC+12:
1663 case MSR_IA32_MC0_MISC+16:
1664 case MSR_IA32_UCODE_REV:
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001665 case MSR_IA32_PERF_STATUS:
Matthew Gregan2dc70942007-05-06 10:59:46 +03001666 case MSR_IA32_EBL_CR_POWERON:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001667 /* MTRR registers */
1668 case 0xfe:
1669 case 0x200 ... 0x2ff:
1670 data = 0;
1671 break;
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001672 case 0xcd: /* fsb frequency */
1673 data = 3;
1674 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001675 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001676 data = kvm_get_apic_base(vcpu);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001677 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001678 case MSR_IA32_MISC_ENABLE:
1679 data = vcpu->ia32_misc_enable_msr;
1680 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001681#ifdef CONFIG_X86_64
1682 case MSR_EFER:
1683 data = vcpu->shadow_efer;
1684 break;
1685#endif
1686 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001687 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001688 return 1;
1689 }
1690 *pdata = data;
1691 return 0;
1692}
1693EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1694
Avi Kivity6aa8b732006-12-10 02:21:36 -08001695/*
1696 * Reads an msr value (of 'msr_index') into 'pdata'.
1697 * Returns 0 on success, non-0 otherwise.
1698 * Assumes vcpu_load() was already called.
1699 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001700int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001701{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001702 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001703}
1704
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001705#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001706
Avi Kivity3bab1f52006-12-29 16:49:48 -08001707static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001708{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001709 if (efer & EFER_RESERVED_BITS) {
1710 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1711 efer);
1712 inject_gp(vcpu);
1713 return;
1714 }
1715
1716 if (is_paging(vcpu)
1717 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1718 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1719 inject_gp(vcpu);
1720 return;
1721 }
1722
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001723 kvm_x86_ops->set_efer(vcpu, efer);
Avi Kivity7725f0b2006-12-13 00:34:01 -08001724
Avi Kivity6aa8b732006-12-10 02:21:36 -08001725 efer &= ~EFER_LMA;
1726 efer |= vcpu->shadow_efer & EFER_LMA;
1727
1728 vcpu->shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001729}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001730
1731#endif
1732
Avi Kivity3bab1f52006-12-29 16:49:48 -08001733int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1734{
1735 switch (msr) {
1736#ifdef CONFIG_X86_64
1737 case MSR_EFER:
1738 set_efer(vcpu, data);
1739 break;
1740#endif
1741 case MSR_IA32_MC0_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001742 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
Avi Kivity3bab1f52006-12-29 16:49:48 -08001743 __FUNCTION__, data);
1744 break;
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001745 case MSR_IA32_MCG_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001746 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001747 __FUNCTION__, data);
1748 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001749 case MSR_IA32_UCODE_REV:
1750 case MSR_IA32_UCODE_WRITE:
1751 case 0x200 ... 0x2ff: /* MTRRs */
1752 break;
1753 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001754 kvm_set_apic_base(vcpu, data);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001755 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001756 case MSR_IA32_MISC_ENABLE:
1757 vcpu->ia32_misc_enable_msr = data;
1758 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001759 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001760 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001761 return 1;
1762 }
1763 return 0;
1764}
1765EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1766
Avi Kivity6aa8b732006-12-10 02:21:36 -08001767/*
1768 * Writes msr value into into the appropriate "register".
1769 * Returns 0 on success, non-0 otherwise.
1770 * Assumes vcpu_load() was already called.
1771 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001772int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001773{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001774 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001775}
1776
1777void kvm_resched(struct kvm_vcpu *vcpu)
1778{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001779 if (!need_resched())
1780 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001781 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001782}
1783EXPORT_SYMBOL_GPL(kvm_resched);
1784
Avi Kivity06465c52007-02-28 20:46:53 +02001785void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1786{
1787 int i;
1788 u32 function;
1789 struct kvm_cpuid_entry *e, *best;
1790
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001791 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001792 function = vcpu->regs[VCPU_REGS_RAX];
1793 vcpu->regs[VCPU_REGS_RAX] = 0;
1794 vcpu->regs[VCPU_REGS_RBX] = 0;
1795 vcpu->regs[VCPU_REGS_RCX] = 0;
1796 vcpu->regs[VCPU_REGS_RDX] = 0;
1797 best = NULL;
1798 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1799 e = &vcpu->cpuid_entries[i];
1800 if (e->function == function) {
1801 best = e;
1802 break;
1803 }
1804 /*
1805 * Both basic or both extended?
1806 */
1807 if (((e->function ^ function) & 0x80000000) == 0)
1808 if (!best || e->function > best->function)
1809 best = e;
1810 }
1811 if (best) {
1812 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1813 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1814 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1815 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1816 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001817 kvm_x86_ops->decache_regs(vcpu);
1818 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001819}
1820EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1821
Avi Kivity039576c2007-03-20 12:46:50 +02001822static int pio_copy_data(struct kvm_vcpu *vcpu)
Avi Kivity46fc1472007-02-22 19:39:30 +02001823{
Avi Kivity039576c2007-03-20 12:46:50 +02001824 void *p = vcpu->pio_data;
1825 void *q;
1826 unsigned bytes;
1827 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1828
Avi Kivity039576c2007-03-20 12:46:50 +02001829 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1830 PAGE_KERNEL);
1831 if (!q) {
Avi Kivity039576c2007-03-20 12:46:50 +02001832 free_pio_guest_pages(vcpu);
1833 return -ENOMEM;
1834 }
1835 q += vcpu->pio.guest_page_offset;
1836 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1837 if (vcpu->pio.in)
1838 memcpy(q, p, bytes);
1839 else
1840 memcpy(p, q, bytes);
1841 q -= vcpu->pio.guest_page_offset;
1842 vunmap(q);
Avi Kivity039576c2007-03-20 12:46:50 +02001843 free_pio_guest_pages(vcpu);
1844 return 0;
1845}
1846
1847static int complete_pio(struct kvm_vcpu *vcpu)
1848{
1849 struct kvm_pio_request *io = &vcpu->pio;
Avi Kivity46fc1472007-02-22 19:39:30 +02001850 long delta;
Avi Kivity039576c2007-03-20 12:46:50 +02001851 int r;
Avi Kivity46fc1472007-02-22 19:39:30 +02001852
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001853 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001854
1855 if (!io->string) {
Avi Kivity039576c2007-03-20 12:46:50 +02001856 if (io->in)
1857 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
Avi Kivity46fc1472007-02-22 19:39:30 +02001858 io->size);
1859 } else {
Avi Kivity039576c2007-03-20 12:46:50 +02001860 if (io->in) {
1861 r = pio_copy_data(vcpu);
1862 if (r) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001863 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02001864 return r;
1865 }
1866 }
1867
Avi Kivity46fc1472007-02-22 19:39:30 +02001868 delta = 1;
1869 if (io->rep) {
Avi Kivity039576c2007-03-20 12:46:50 +02001870 delta *= io->cur_count;
Avi Kivity46fc1472007-02-22 19:39:30 +02001871 /*
1872 * The size of the register should really depend on
1873 * current address size.
1874 */
1875 vcpu->regs[VCPU_REGS_RCX] -= delta;
1876 }
Avi Kivity039576c2007-03-20 12:46:50 +02001877 if (io->down)
Avi Kivity46fc1472007-02-22 19:39:30 +02001878 delta = -delta;
1879 delta *= io->size;
Avi Kivity039576c2007-03-20 12:46:50 +02001880 if (io->in)
Avi Kivity46fc1472007-02-22 19:39:30 +02001881 vcpu->regs[VCPU_REGS_RDI] += delta;
1882 else
1883 vcpu->regs[VCPU_REGS_RSI] += delta;
1884 }
1885
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001886 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001887
Avi Kivity039576c2007-03-20 12:46:50 +02001888 io->count -= io->cur_count;
1889 io->cur_count = 0;
1890
Avi Kivity039576c2007-03-20 12:46:50 +02001891 return 0;
Avi Kivity46fc1472007-02-22 19:39:30 +02001892}
1893
Eddie Dong65619eb2007-07-17 11:52:33 +03001894static void kernel_pio(struct kvm_io_device *pio_dev,
1895 struct kvm_vcpu *vcpu,
1896 void *pd)
Eddie Dong74906342007-06-19 18:05:03 +03001897{
1898 /* TODO: String I/O for in kernel device */
1899
Eddie Dong9cf98822007-07-22 10:36:31 +03001900 mutex_lock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001901 if (vcpu->pio.in)
1902 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1903 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001904 pd);
Eddie Dong74906342007-06-19 18:05:03 +03001905 else
1906 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1907 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001908 pd);
Eddie Dong9cf98822007-07-22 10:36:31 +03001909 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001910}
1911
1912static void pio_string_write(struct kvm_io_device *pio_dev,
1913 struct kvm_vcpu *vcpu)
1914{
1915 struct kvm_pio_request *io = &vcpu->pio;
1916 void *pd = vcpu->pio_data;
1917 int i;
1918
Eddie Dong9cf98822007-07-22 10:36:31 +03001919 mutex_lock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001920 for (i = 0; i < io->cur_count; i++) {
1921 kvm_iodevice_write(pio_dev, io->port,
1922 io->size,
1923 pd);
1924 pd += io->size;
1925 }
Eddie Dong9cf98822007-07-22 10:36:31 +03001926 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001927}
1928
Laurent Vivier3090dd72007-08-05 10:43:32 +03001929int kvm_emulate_pio (struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1930 int size, unsigned port)
1931{
1932 struct kvm_io_device *pio_dev;
1933
1934 vcpu->run->exit_reason = KVM_EXIT_IO;
1935 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1936 vcpu->run->io.size = vcpu->pio.size = size;
1937 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1938 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1;
1939 vcpu->run->io.port = vcpu->pio.port = port;
1940 vcpu->pio.in = in;
1941 vcpu->pio.string = 0;
1942 vcpu->pio.down = 0;
1943 vcpu->pio.guest_page_offset = 0;
1944 vcpu->pio.rep = 0;
1945
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001946 kvm_x86_ops->cache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03001947 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001948 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03001949
Avi Kivity0967b7b2007-09-15 17:34:36 +03001950 kvm_x86_ops->skip_emulated_instruction(vcpu);
1951
Laurent Vivier3090dd72007-08-05 10:43:32 +03001952 pio_dev = vcpu_find_pio_dev(vcpu, port);
1953 if (pio_dev) {
1954 kernel_pio(pio_dev, vcpu, vcpu->pio_data);
1955 complete_pio(vcpu);
1956 return 1;
1957 }
1958 return 0;
1959}
1960EXPORT_SYMBOL_GPL(kvm_emulate_pio);
1961
1962int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1963 int size, unsigned long count, int down,
Avi Kivity039576c2007-03-20 12:46:50 +02001964 gva_t address, int rep, unsigned port)
1965{
1966 unsigned now, in_page;
Eddie Dong65619eb2007-07-17 11:52:33 +03001967 int i, ret = 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001968 int nr_pages = 1;
1969 struct page *page;
Eddie Dong74906342007-06-19 18:05:03 +03001970 struct kvm_io_device *pio_dev;
Avi Kivity039576c2007-03-20 12:46:50 +02001971
1972 vcpu->run->exit_reason = KVM_EXIT_IO;
1973 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Laurent Vivier3090dd72007-08-05 10:43:32 +03001974 vcpu->run->io.size = vcpu->pio.size = size;
Avi Kivity039576c2007-03-20 12:46:50 +02001975 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Laurent Vivier3090dd72007-08-05 10:43:32 +03001976 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count;
1977 vcpu->run->io.port = vcpu->pio.port = port;
Avi Kivity039576c2007-03-20 12:46:50 +02001978 vcpu->pio.in = in;
Laurent Vivier3090dd72007-08-05 10:43:32 +03001979 vcpu->pio.string = 1;
Avi Kivity039576c2007-03-20 12:46:50 +02001980 vcpu->pio.down = down;
1981 vcpu->pio.guest_page_offset = offset_in_page(address);
1982 vcpu->pio.rep = rep;
1983
Avi Kivity039576c2007-03-20 12:46:50 +02001984 if (!count) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001985 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02001986 return 1;
1987 }
1988
Avi Kivity039576c2007-03-20 12:46:50 +02001989 if (!down)
1990 in_page = PAGE_SIZE - offset_in_page(address);
1991 else
1992 in_page = offset_in_page(address) + size;
1993 now = min(count, (unsigned long)in_page / size);
1994 if (!now) {
1995 /*
1996 * String I/O straddles page boundary. Pin two guest pages
1997 * so that we satisfy atomicity constraints. Do just one
1998 * transaction to avoid complexity.
1999 */
2000 nr_pages = 2;
2001 now = 1;
2002 }
2003 if (down) {
2004 /*
2005 * String I/O in reverse. Yuck. Kill the guest, fix later.
2006 */
Rusty Russellf0242472007-08-01 10:48:02 +10002007 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivity039576c2007-03-20 12:46:50 +02002008 inject_gp(vcpu);
2009 return 1;
2010 }
2011 vcpu->run->io.count = now;
2012 vcpu->pio.cur_count = now;
2013
Avi Kivity0967b7b2007-09-15 17:34:36 +03002014 if (vcpu->pio.cur_count == vcpu->pio.count)
2015 kvm_x86_ops->skip_emulated_instruction(vcpu);
2016
Avi Kivity039576c2007-03-20 12:46:50 +02002017 for (i = 0; i < nr_pages; ++i) {
Shaohua Li11ec2802007-07-23 14:51:37 +08002018 mutex_lock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02002019 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
2020 if (page)
2021 get_page(page);
2022 vcpu->pio.guest_pages[i] = page;
Shaohua Li11ec2802007-07-23 14:51:37 +08002023 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02002024 if (!page) {
2025 inject_gp(vcpu);
2026 free_pio_guest_pages(vcpu);
2027 return 1;
2028 }
2029 }
2030
Laurent Vivier3090dd72007-08-05 10:43:32 +03002031 pio_dev = vcpu_find_pio_dev(vcpu, port);
Eddie Dong65619eb2007-07-17 11:52:33 +03002032 if (!vcpu->pio.in) {
2033 /* string PIO write */
2034 ret = pio_copy_data(vcpu);
2035 if (ret >= 0 && pio_dev) {
2036 pio_string_write(pio_dev, vcpu);
2037 complete_pio(vcpu);
2038 if (vcpu->pio.count == 0)
2039 ret = 1;
2040 }
2041 } else if (pio_dev)
Rusty Russellf0242472007-08-01 10:48:02 +10002042 pr_unimpl(vcpu, "no string pio read support yet, "
Eddie Dong65619eb2007-07-17 11:52:33 +03002043 "port %x size %d count %ld\n",
2044 port, size, count);
2045
2046 return ret;
Avi Kivity039576c2007-03-20 12:46:50 +02002047}
Laurent Vivier3090dd72007-08-05 10:43:32 +03002048EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
Avi Kivity039576c2007-03-20 12:46:50 +02002049
Avi Kivity04d2cc72007-09-10 18:10:54 +03002050/*
2051 * Check if userspace requested an interrupt window, and that the
2052 * interrupt window is open.
2053 *
2054 * No need to exit to userspace if we already have an interrupt queued.
2055 */
2056static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2057 struct kvm_run *kvm_run)
2058{
2059 return (!vcpu->irq_summary &&
2060 kvm_run->request_interrupt_window &&
2061 vcpu->interrupt_window_open &&
2062 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
2063}
2064
2065static void post_kvm_run_save(struct kvm_vcpu *vcpu,
2066 struct kvm_run *kvm_run)
2067{
2068 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
2069 kvm_run->cr8 = get_cr8(vcpu);
2070 kvm_run->apic_base = kvm_get_apic_base(vcpu);
2071 if (irqchip_in_kernel(vcpu->kvm))
2072 kvm_run->ready_for_interrupt_injection = 1;
2073 else
2074 kvm_run->ready_for_interrupt_injection =
2075 (vcpu->interrupt_window_open &&
2076 vcpu->irq_summary == 0);
2077}
2078
2079static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2080{
2081 int r;
2082
2083 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
2084 printk("vcpu %d received sipi with vector # %x\n",
2085 vcpu->vcpu_id, vcpu->sipi_vector);
2086 kvm_lapic_reset(vcpu);
2087 kvm_x86_ops->vcpu_reset(vcpu);
2088 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
2089 }
2090
2091preempted:
2092 if (vcpu->guest_debug.enabled)
2093 kvm_x86_ops->guest_debug_pre(vcpu);
2094
2095again:
2096 r = kvm_mmu_reload(vcpu);
2097 if (unlikely(r))
2098 goto out;
2099
2100 preempt_disable();
2101
2102 kvm_x86_ops->prepare_guest_switch(vcpu);
2103 kvm_load_guest_fpu(vcpu);
2104
2105 local_irq_disable();
2106
2107 if (signal_pending(current)) {
2108 local_irq_enable();
2109 preempt_enable();
2110 r = -EINTR;
2111 kvm_run->exit_reason = KVM_EXIT_INTR;
2112 ++vcpu->stat.signal_exits;
2113 goto out;
2114 }
2115
2116 if (irqchip_in_kernel(vcpu->kvm))
2117 kvm_x86_ops->inject_pending_irq(vcpu);
2118 else if (!vcpu->mmio_read_completed)
2119 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2120
2121 vcpu->guest_mode = 1;
Laurent Vivierd172fcd2007-10-15 17:00:19 +02002122 kvm_guest_enter();
Avi Kivity04d2cc72007-09-10 18:10:54 +03002123
2124 if (vcpu->requests)
2125 if (test_and_clear_bit(KVM_TLB_FLUSH, &vcpu->requests))
2126 kvm_x86_ops->tlb_flush(vcpu);
2127
2128 kvm_x86_ops->run(vcpu, kvm_run);
2129
2130 vcpu->guest_mode = 0;
2131 local_irq_enable();
2132
2133 ++vcpu->stat.exits;
2134
Laurent Vivier0552f732007-10-18 15:19:01 +02002135 /*
2136 * We must have an instruction between local_irq_enable() and
2137 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2138 * the interrupt shadow. The stat.exits increment will do nicely.
2139 * But we need to prevent reordering, hence this barrier():
2140 */
2141 barrier();
2142
2143 kvm_guest_exit();
2144
Avi Kivity04d2cc72007-09-10 18:10:54 +03002145 preempt_enable();
2146
2147 /*
2148 * Profile KVM exit RIPs:
2149 */
2150 if (unlikely(prof_on == KVM_PROFILING)) {
2151 kvm_x86_ops->cache_regs(vcpu);
2152 profile_hit(KVM_PROFILING, (void *)vcpu->rip);
2153 }
2154
2155 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2156
2157 if (r > 0) {
2158 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2159 r = -EINTR;
2160 kvm_run->exit_reason = KVM_EXIT_INTR;
2161 ++vcpu->stat.request_irq_exits;
2162 goto out;
2163 }
2164 if (!need_resched()) {
2165 ++vcpu->stat.light_exits;
2166 goto again;
2167 }
2168 }
2169
2170out:
2171 if (r > 0) {
2172 kvm_resched(vcpu);
2173 goto preempted;
2174 }
2175
2176 post_kvm_run_save(vcpu, kvm_run);
2177
2178 return r;
2179}
2180
2181
Avi Kivitybccf2152007-02-21 18:04:26 +02002182static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002183{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002184 int r;
Avi Kivity1961d272007-03-05 19:46:05 +02002185 sigset_t sigsaved;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002186
Avi Kivitybccf2152007-02-21 18:04:26 +02002187 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002188
He, Qingc5ec1532007-09-03 17:07:41 +03002189 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
2190 kvm_vcpu_block(vcpu);
2191 vcpu_put(vcpu);
2192 return -EAGAIN;
2193 }
2194
Avi Kivity1961d272007-03-05 19:46:05 +02002195 if (vcpu->sigset_active)
2196 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2197
Dor Laor54810342007-02-12 00:54:39 -08002198 /* re-sync apic's tpr */
He, Qing5cd4f6f2007-08-30 17:04:26 +08002199 if (!irqchip_in_kernel(vcpu->kvm))
2200 set_cr8(vcpu, kvm_run->cr8);
Dor Laor54810342007-02-12 00:54:39 -08002201
Avi Kivity02c83202007-04-29 15:02:17 +03002202 if (vcpu->pio.cur_count) {
2203 r = complete_pio(vcpu);
2204 if (r)
2205 goto out;
2206 }
2207
2208 if (vcpu->mmio_needed) {
2209 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
2210 vcpu->mmio_read_completed = 1;
2211 vcpu->mmio_needed = 0;
2212 r = emulate_instruction(vcpu, kvm_run,
Laurent Vivier34273182007-09-18 11:27:37 +02002213 vcpu->mmio_fault_cr2, 0, 1);
Avi Kivity02c83202007-04-29 15:02:17 +03002214 if (r == EMULATE_DO_MMIO) {
2215 /*
2216 * Read-modify-write. Back to userspace.
2217 */
Avi Kivity02c83202007-04-29 15:02:17 +03002218 r = 0;
2219 goto out;
Avi Kivity46fc1472007-02-22 19:39:30 +02002220 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002221 }
2222
Avi Kivity8eb7d332007-03-04 14:17:08 +02002223 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002224 kvm_x86_ops->cache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002225 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002226 kvm_x86_ops->decache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002227 }
2228
Avi Kivity04d2cc72007-09-10 18:10:54 +03002229 r = __vcpu_run(vcpu, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002230
Avi Kivity039576c2007-03-20 12:46:50 +02002231out:
Avi Kivity1961d272007-03-05 19:46:05 +02002232 if (vcpu->sigset_active)
2233 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2234
Avi Kivity6aa8b732006-12-10 02:21:36 -08002235 vcpu_put(vcpu);
2236 return r;
2237}
2238
Avi Kivitybccf2152007-02-21 18:04:26 +02002239static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
2240 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002241{
Avi Kivitybccf2152007-02-21 18:04:26 +02002242 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002243
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002244 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002245
2246 regs->rax = vcpu->regs[VCPU_REGS_RAX];
2247 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
2248 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
2249 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
2250 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
2251 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
2252 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
2253 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002254#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002255 regs->r8 = vcpu->regs[VCPU_REGS_R8];
2256 regs->r9 = vcpu->regs[VCPU_REGS_R9];
2257 regs->r10 = vcpu->regs[VCPU_REGS_R10];
2258 regs->r11 = vcpu->regs[VCPU_REGS_R11];
2259 regs->r12 = vcpu->regs[VCPU_REGS_R12];
2260 regs->r13 = vcpu->regs[VCPU_REGS_R13];
2261 regs->r14 = vcpu->regs[VCPU_REGS_R14];
2262 regs->r15 = vcpu->regs[VCPU_REGS_R15];
2263#endif
2264
2265 regs->rip = vcpu->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002266 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002267
2268 /*
2269 * Don't leak debug flags in case they were set for guest debugging
2270 */
2271 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2272 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2273
2274 vcpu_put(vcpu);
2275
2276 return 0;
2277}
2278
Avi Kivitybccf2152007-02-21 18:04:26 +02002279static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
2280 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002281{
Avi Kivitybccf2152007-02-21 18:04:26 +02002282 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002283
2284 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
2285 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
2286 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
2287 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
2288 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
2289 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
2290 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
2291 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002292#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002293 vcpu->regs[VCPU_REGS_R8] = regs->r8;
2294 vcpu->regs[VCPU_REGS_R9] = regs->r9;
2295 vcpu->regs[VCPU_REGS_R10] = regs->r10;
2296 vcpu->regs[VCPU_REGS_R11] = regs->r11;
2297 vcpu->regs[VCPU_REGS_R12] = regs->r12;
2298 vcpu->regs[VCPU_REGS_R13] = regs->r13;
2299 vcpu->regs[VCPU_REGS_R14] = regs->r14;
2300 vcpu->regs[VCPU_REGS_R15] = regs->r15;
2301#endif
2302
2303 vcpu->rip = regs->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002304 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002305
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002306 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002307
2308 vcpu_put(vcpu);
2309
2310 return 0;
2311}
2312
2313static void get_segment(struct kvm_vcpu *vcpu,
2314 struct kvm_segment *var, int seg)
2315{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002316 return kvm_x86_ops->get_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002317}
2318
Avi Kivitybccf2152007-02-21 18:04:26 +02002319static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2320 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002322 struct descriptor_table dt;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002323 int pending_vec;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002324
Avi Kivitybccf2152007-02-21 18:04:26 +02002325 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002326
2327 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2328 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2329 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2330 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2331 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2332 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2333
2334 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2335 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2336
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002337 kvm_x86_ops->get_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002338 sregs->idt.limit = dt.limit;
2339 sregs->idt.base = dt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002340 kvm_x86_ops->get_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002341 sregs->gdt.limit = dt.limit;
2342 sregs->gdt.base = dt.base;
2343
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002344 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002345 sregs->cr0 = vcpu->cr0;
2346 sregs->cr2 = vcpu->cr2;
2347 sregs->cr3 = vcpu->cr3;
2348 sregs->cr4 = vcpu->cr4;
Eddie Dong7017fc32007-07-18 11:34:57 +03002349 sregs->cr8 = get_cr8(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002350 sregs->efer = vcpu->shadow_efer;
Eddie Dong7017fc32007-07-18 11:34:57 +03002351 sregs->apic_base = kvm_get_apic_base(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002352
Eddie Dong2a8067f2007-08-06 16:29:07 +03002353 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc52fb352007-08-02 14:03:07 +03002354 memset(sregs->interrupt_bitmap, 0,
2355 sizeof sregs->interrupt_bitmap);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002356 pending_vec = kvm_x86_ops->get_irq(vcpu);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002357 if (pending_vec >= 0)
2358 set_bit(pending_vec, (unsigned long *)sregs->interrupt_bitmap);
2359 } else
He, Qingc52fb352007-08-02 14:03:07 +03002360 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2361 sizeof sregs->interrupt_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002362
2363 vcpu_put(vcpu);
2364
2365 return 0;
2366}
2367
2368static void set_segment(struct kvm_vcpu *vcpu,
2369 struct kvm_segment *var, int seg)
2370{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002371 return kvm_x86_ops->set_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002372}
2373
Avi Kivitybccf2152007-02-21 18:04:26 +02002374static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2375 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002376{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002377 int mmu_reset_needed = 0;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002378 int i, pending_vec, max_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002379 struct descriptor_table dt;
2380
Avi Kivitybccf2152007-02-21 18:04:26 +02002381 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002382
Avi Kivity6aa8b732006-12-10 02:21:36 -08002383 dt.limit = sregs->idt.limit;
2384 dt.base = sregs->idt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002385 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002386 dt.limit = sregs->gdt.limit;
2387 dt.base = sregs->gdt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002388 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002389
2390 vcpu->cr2 = sregs->cr2;
2391 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2392 vcpu->cr3 = sregs->cr3;
2393
Eddie Dong7017fc32007-07-18 11:34:57 +03002394 set_cr8(vcpu, sregs->cr8);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002395
2396 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002397#ifdef CONFIG_X86_64
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002398 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002399#endif
Eddie Dong7017fc32007-07-18 11:34:57 +03002400 kvm_set_apic_base(vcpu, sregs->apic_base);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002401
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002402 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity399badf2007-01-05 16:36:38 -08002403
Avi Kivity6aa8b732006-12-10 02:21:36 -08002404 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
Rusty Russell81f50e32007-09-06 01:20:38 +10002405 vcpu->cr0 = sregs->cr0;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002406 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002407
2408 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002409 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Avi Kivity1b0973b2007-01-05 16:36:41 -08002410 if (!is_long_mode(vcpu) && is_pae(vcpu))
2411 load_pdptrs(vcpu, vcpu->cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002412
2413 if (mmu_reset_needed)
2414 kvm_mmu_reset_context(vcpu);
2415
He, Qingc52fb352007-08-02 14:03:07 +03002416 if (!irqchip_in_kernel(vcpu->kvm)) {
2417 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2418 sizeof vcpu->irq_pending);
2419 vcpu->irq_summary = 0;
2420 for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
2421 if (vcpu->irq_pending[i])
2422 __set_bit(i, &vcpu->irq_summary);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002423 } else {
2424 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
2425 pending_vec = find_first_bit(
2426 (const unsigned long *)sregs->interrupt_bitmap,
2427 max_bits);
2428 /* Only pending external irq is handled here */
2429 if (pending_vec < max_bits) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002430 kvm_x86_ops->set_irq(vcpu, pending_vec);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002431 printk("Set back pending irq %d\n", pending_vec);
2432 }
He, Qingc52fb352007-08-02 14:03:07 +03002433 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002434
Avi Kivity024aa1c2007-03-21 13:44:58 +02002435 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2436 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2437 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2438 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2439 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2440 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2441
2442 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2443 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2444
Avi Kivity6aa8b732006-12-10 02:21:36 -08002445 vcpu_put(vcpu);
2446
2447 return 0;
2448}
2449
Rusty Russell1747fb72007-09-06 01:21:32 +10002450void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2451{
2452 struct kvm_segment cs;
2453
2454 get_segment(vcpu, &cs, VCPU_SREG_CS);
2455 *db = cs.db;
2456 *l = cs.l;
2457}
2458EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2459
Avi Kivity6aa8b732006-12-10 02:21:36 -08002460/*
2461 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2462 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
Michael Riepebf591b22006-12-22 01:05:36 -08002463 *
2464 * This list is modified at module load time to reflect the
2465 * capabilities of the host cpu.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002466 */
2467static u32 msrs_to_save[] = {
2468 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
2469 MSR_K6_STAR,
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002470#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002471 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
2472#endif
2473 MSR_IA32_TIME_STAMP_COUNTER,
2474};
2475
Michael Riepebf591b22006-12-22 01:05:36 -08002476static unsigned num_msrs_to_save;
2477
Avi Kivity6f00e682007-01-26 00:56:40 -08002478static u32 emulated_msrs[] = {
2479 MSR_IA32_MISC_ENABLE,
2480};
2481
Michael Riepebf591b22006-12-22 01:05:36 -08002482static __init void kvm_init_msr_list(void)
2483{
2484 u32 dummy[2];
2485 unsigned i, j;
2486
2487 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2488 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2489 continue;
2490 if (j < i)
2491 msrs_to_save[j] = msrs_to_save[i];
2492 j++;
2493 }
2494 num_msrs_to_save = j;
2495}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002496
2497/*
2498 * Adapt set_msr() to msr_io()'s calling convention
2499 */
2500static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2501{
Avi Kivity35f3f282007-07-17 14:20:30 +03002502 return kvm_set_msr(vcpu, index, *data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002503}
2504
2505/*
2506 * Read or write a bunch of msrs. All parameters are kernel addresses.
2507 *
2508 * @return number of msrs set successfully.
2509 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002510static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002511 struct kvm_msr_entry *entries,
2512 int (*do_msr)(struct kvm_vcpu *vcpu,
2513 unsigned index, u64 *data))
2514{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002515 int i;
2516
Avi Kivitybccf2152007-02-21 18:04:26 +02002517 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002518
2519 for (i = 0; i < msrs->nmsrs; ++i)
2520 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2521 break;
2522
2523 vcpu_put(vcpu);
2524
2525 return i;
2526}
2527
2528/*
2529 * Read or write a bunch of msrs. Parameters are user addresses.
2530 *
2531 * @return number of msrs set successfully.
2532 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002533static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002534 int (*do_msr)(struct kvm_vcpu *vcpu,
2535 unsigned index, u64 *data),
2536 int writeback)
2537{
2538 struct kvm_msrs msrs;
2539 struct kvm_msr_entry *entries;
2540 int r, n;
2541 unsigned size;
2542
2543 r = -EFAULT;
2544 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2545 goto out;
2546
2547 r = -E2BIG;
2548 if (msrs.nmsrs >= MAX_IO_MSRS)
2549 goto out;
2550
2551 r = -ENOMEM;
2552 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
2553 entries = vmalloc(size);
2554 if (!entries)
2555 goto out;
2556
2557 r = -EFAULT;
2558 if (copy_from_user(entries, user_msrs->entries, size))
2559 goto out_free;
2560
Avi Kivitybccf2152007-02-21 18:04:26 +02002561 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002562 if (r < 0)
2563 goto out_free;
2564
2565 r = -EFAULT;
2566 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2567 goto out_free;
2568
2569 r = n;
2570
2571out_free:
2572 vfree(entries);
2573out:
2574 return r;
2575}
2576
2577/*
2578 * Translate a guest virtual address to a guest physical address.
2579 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002580static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2581 struct kvm_translation *tr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002582{
2583 unsigned long vaddr = tr->linear_address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002584 gpa_t gpa;
2585
Avi Kivitybccf2152007-02-21 18:04:26 +02002586 vcpu_load(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +08002587 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002588 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2589 tr->physical_address = gpa;
2590 tr->valid = gpa != UNMAPPED_GVA;
2591 tr->writeable = 1;
2592 tr->usermode = 0;
Shaohua Li11ec2802007-07-23 14:51:37 +08002593 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002594 vcpu_put(vcpu);
2595
2596 return 0;
2597}
2598
Avi Kivitybccf2152007-02-21 18:04:26 +02002599static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2600 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002601{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002602 if (irq->irq < 0 || irq->irq >= 256)
2603 return -EINVAL;
Eddie Dong97222cc2007-09-12 10:58:04 +03002604 if (irqchip_in_kernel(vcpu->kvm))
2605 return -ENXIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02002606 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002607
2608 set_bit(irq->irq, vcpu->irq_pending);
2609 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2610
2611 vcpu_put(vcpu);
2612
2613 return 0;
2614}
2615
Avi Kivitybccf2152007-02-21 18:04:26 +02002616static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2617 struct kvm_debug_guest *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002618{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002619 int r;
2620
Avi Kivitybccf2152007-02-21 18:04:26 +02002621 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002622
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002623 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002624
2625 vcpu_put(vcpu);
2626
2627 return r;
2628}
2629
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002630static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2631 unsigned long address,
2632 int *type)
2633{
2634 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2635 unsigned long pgoff;
2636 struct page *page;
2637
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002638 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +02002639 if (pgoff == 0)
2640 page = virt_to_page(vcpu->run);
2641 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2642 page = virt_to_page(vcpu->pio_data);
2643 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002644 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002645 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002646 if (type != NULL)
2647 *type = VM_FAULT_MINOR;
2648
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002649 return page;
2650}
2651
2652static struct vm_operations_struct kvm_vcpu_vm_ops = {
2653 .nopage = kvm_vcpu_nopage,
2654};
2655
2656static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2657{
2658 vma->vm_ops = &kvm_vcpu_vm_ops;
2659 return 0;
2660}
2661
Avi Kivitybccf2152007-02-21 18:04:26 +02002662static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2663{
2664 struct kvm_vcpu *vcpu = filp->private_data;
2665
2666 fput(vcpu->kvm->filp);
2667 return 0;
2668}
2669
2670static struct file_operations kvm_vcpu_fops = {
2671 .release = kvm_vcpu_release,
2672 .unlocked_ioctl = kvm_vcpu_ioctl,
2673 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002674 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02002675};
2676
2677/*
2678 * Allocates an inode for the vcpu.
2679 */
2680static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2681{
2682 int fd, r;
2683 struct inode *inode;
2684 struct file *file;
2685
Avi Kivityd6d28162007-06-28 08:38:16 -04002686 r = anon_inode_getfd(&fd, &inode, &file,
2687 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2688 if (r)
2689 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002690 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +02002691 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02002692}
2693
Avi Kivityc5ea7662007-02-20 18:41:05 +02002694/*
2695 * Creates some virtual cpus. Good luck creating more than one.
2696 */
2697static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2698{
2699 int r;
2700 struct kvm_vcpu *vcpu;
2701
Avi Kivityc5ea7662007-02-20 18:41:05 +02002702 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002703 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002704
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002705 vcpu = kvm_x86_ops->vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002706 if (IS_ERR(vcpu))
2707 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002708
Avi Kivity15ad7142007-07-11 18:17:21 +03002709 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
2710
Rusty Russellb114b082007-07-30 21:13:43 +10002711 /* We do fxsave: this must be aligned. */
2712 BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF);
2713
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002714 vcpu_load(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002715 r = kvm_mmu_setup(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002716 vcpu_put(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002717 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002718 goto free_vcpu;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002719
Shaohua Li11ec2802007-07-23 14:51:37 +08002720 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002721 if (kvm->vcpus[n]) {
2722 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +08002723 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002724 goto mmu_unload;
2725 }
2726 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08002727 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002728
2729 /* Now it's all set up, let userspace reach it */
Avi Kivitybccf2152007-02-21 18:04:26 +02002730 r = create_vcpu_fd(vcpu);
2731 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002732 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02002733 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002734
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002735unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08002736 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002737 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +08002738 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002739
2740mmu_unload:
2741 vcpu_load(vcpu);
2742 kvm_mmu_unload(vcpu);
2743 vcpu_put(vcpu);
2744
2745free_vcpu:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002746 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002747 return r;
2748}
2749
Eddie Dong2cc51562007-05-21 07:28:09 +03002750static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
2751{
2752 u64 efer;
2753 int i;
2754 struct kvm_cpuid_entry *e, *entry;
2755
2756 rdmsrl(MSR_EFER, efer);
2757 entry = NULL;
2758 for (i = 0; i < vcpu->cpuid_nent; ++i) {
2759 e = &vcpu->cpuid_entries[i];
2760 if (e->function == 0x80000001) {
2761 entry = e;
2762 break;
2763 }
2764 }
Avi Kivity4c981b42007-07-25 09:22:12 +03002765 if (entry && (entry->edx & (1 << 20)) && !(efer & EFER_NX)) {
Eddie Dong2cc51562007-05-21 07:28:09 +03002766 entry->edx &= ~(1 << 20);
Avi Kivity4c981b42007-07-25 09:22:12 +03002767 printk(KERN_INFO "kvm: guest NX capability removed\n");
Eddie Dong2cc51562007-05-21 07:28:09 +03002768 }
2769}
2770
Avi Kivity06465c52007-02-28 20:46:53 +02002771static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
2772 struct kvm_cpuid *cpuid,
2773 struct kvm_cpuid_entry __user *entries)
2774{
2775 int r;
2776
2777 r = -E2BIG;
2778 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2779 goto out;
2780 r = -EFAULT;
2781 if (copy_from_user(&vcpu->cpuid_entries, entries,
2782 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
2783 goto out;
2784 vcpu->cpuid_nent = cpuid->nent;
Eddie Dong2cc51562007-05-21 07:28:09 +03002785 cpuid_fix_nx_cap(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002786 return 0;
2787
2788out:
2789 return r;
2790}
2791
Avi Kivity1961d272007-03-05 19:46:05 +02002792static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2793{
2794 if (sigset) {
2795 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2796 vcpu->sigset_active = 1;
2797 vcpu->sigset = *sigset;
2798 } else
2799 vcpu->sigset_active = 0;
2800 return 0;
2801}
2802
Avi Kivityb8836732007-04-01 16:34:31 +03002803/*
2804 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2805 * we have asm/x86/processor.h
2806 */
2807struct fxsave {
2808 u16 cwd;
2809 u16 swd;
2810 u16 twd;
2811 u16 fop;
2812 u64 rip;
2813 u64 rdp;
2814 u32 mxcsr;
2815 u32 mxcsr_mask;
2816 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2817#ifdef CONFIG_X86_64
2818 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2819#else
2820 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2821#endif
2822};
2823
2824static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2825{
Rusty Russellb114b082007-07-30 21:13:43 +10002826 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002827
2828 vcpu_load(vcpu);
2829
2830 memcpy(fpu->fpr, fxsave->st_space, 128);
2831 fpu->fcw = fxsave->cwd;
2832 fpu->fsw = fxsave->swd;
2833 fpu->ftwx = fxsave->twd;
2834 fpu->last_opcode = fxsave->fop;
2835 fpu->last_ip = fxsave->rip;
2836 fpu->last_dp = fxsave->rdp;
2837 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2838
2839 vcpu_put(vcpu);
2840
2841 return 0;
2842}
2843
2844static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2845{
Rusty Russellb114b082007-07-30 21:13:43 +10002846 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002847
2848 vcpu_load(vcpu);
2849
2850 memcpy(fxsave->st_space, fpu->fpr, 128);
2851 fxsave->cwd = fpu->fcw;
2852 fxsave->swd = fpu->fsw;
2853 fxsave->twd = fpu->ftwx;
2854 fxsave->fop = fpu->last_opcode;
2855 fxsave->rip = fpu->last_ip;
2856 fxsave->rdp = fpu->last_dp;
2857 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2858
2859 vcpu_put(vcpu);
2860
2861 return 0;
2862}
2863
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002864static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
2865 struct kvm_lapic_state *s)
2866{
2867 vcpu_load(vcpu);
2868 memcpy(s->regs, vcpu->apic->regs, sizeof *s);
2869 vcpu_put(vcpu);
2870
2871 return 0;
2872}
2873
2874static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
2875 struct kvm_lapic_state *s)
2876{
2877 vcpu_load(vcpu);
2878 memcpy(vcpu->apic->regs, s->regs, sizeof *s);
2879 kvm_apic_post_state_restore(vcpu);
2880 vcpu_put(vcpu);
2881
2882 return 0;
2883}
2884
Avi Kivitybccf2152007-02-21 18:04:26 +02002885static long kvm_vcpu_ioctl(struct file *filp,
2886 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002887{
Avi Kivitybccf2152007-02-21 18:04:26 +02002888 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +00002889 void __user *argp = (void __user *)arg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002890 int r = -EINVAL;
2891
2892 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002893 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002894 r = -EINVAL;
2895 if (arg)
2896 goto out;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002897 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002898 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002899 case KVM_GET_REGS: {
2900 struct kvm_regs kvm_regs;
2901
Avi Kivitybccf2152007-02-21 18:04:26 +02002902 memset(&kvm_regs, 0, sizeof kvm_regs);
2903 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002904 if (r)
2905 goto out;
2906 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002907 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002908 goto out;
2909 r = 0;
2910 break;
2911 }
2912 case KVM_SET_REGS: {
2913 struct kvm_regs kvm_regs;
2914
2915 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002916 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002917 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002918 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002919 if (r)
2920 goto out;
2921 r = 0;
2922 break;
2923 }
2924 case KVM_GET_SREGS: {
2925 struct kvm_sregs kvm_sregs;
2926
Avi Kivitybccf2152007-02-21 18:04:26 +02002927 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2928 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002929 if (r)
2930 goto out;
2931 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002932 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002933 goto out;
2934 r = 0;
2935 break;
2936 }
2937 case KVM_SET_SREGS: {
2938 struct kvm_sregs kvm_sregs;
2939
2940 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002941 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002942 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002943 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002944 if (r)
2945 goto out;
2946 r = 0;
2947 break;
2948 }
2949 case KVM_TRANSLATE: {
2950 struct kvm_translation tr;
2951
2952 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002953 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002954 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002955 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002956 if (r)
2957 goto out;
2958 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002959 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002960 goto out;
2961 r = 0;
2962 break;
2963 }
2964 case KVM_INTERRUPT: {
2965 struct kvm_interrupt irq;
2966
2967 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002968 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002969 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002970 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002971 if (r)
2972 goto out;
2973 r = 0;
2974 break;
2975 }
2976 case KVM_DEBUG_GUEST: {
2977 struct kvm_debug_guest dbg;
2978
2979 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002980 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002981 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002982 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002983 if (r)
2984 goto out;
2985 r = 0;
2986 break;
2987 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002988 case KVM_GET_MSRS:
Avi Kivity35f3f282007-07-17 14:20:30 +03002989 r = msr_io(vcpu, argp, kvm_get_msr, 1);
Avi Kivitybccf2152007-02-21 18:04:26 +02002990 break;
2991 case KVM_SET_MSRS:
2992 r = msr_io(vcpu, argp, do_set_msr, 0);
2993 break;
Avi Kivity06465c52007-02-28 20:46:53 +02002994 case KVM_SET_CPUID: {
2995 struct kvm_cpuid __user *cpuid_arg = argp;
2996 struct kvm_cpuid cpuid;
2997
2998 r = -EFAULT;
2999 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
3000 goto out;
3001 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
3002 if (r)
3003 goto out;
3004 break;
3005 }
Avi Kivity1961d272007-03-05 19:46:05 +02003006 case KVM_SET_SIGNAL_MASK: {
3007 struct kvm_signal_mask __user *sigmask_arg = argp;
3008 struct kvm_signal_mask kvm_sigmask;
3009 sigset_t sigset, *p;
3010
3011 p = NULL;
3012 if (argp) {
3013 r = -EFAULT;
3014 if (copy_from_user(&kvm_sigmask, argp,
3015 sizeof kvm_sigmask))
3016 goto out;
3017 r = -EINVAL;
3018 if (kvm_sigmask.len != sizeof sigset)
3019 goto out;
3020 r = -EFAULT;
3021 if (copy_from_user(&sigset, sigmask_arg->sigset,
3022 sizeof sigset))
3023 goto out;
3024 p = &sigset;
3025 }
3026 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
3027 break;
3028 }
Avi Kivityb8836732007-04-01 16:34:31 +03003029 case KVM_GET_FPU: {
3030 struct kvm_fpu fpu;
3031
3032 memset(&fpu, 0, sizeof fpu);
3033 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
3034 if (r)
3035 goto out;
3036 r = -EFAULT;
3037 if (copy_to_user(argp, &fpu, sizeof fpu))
3038 goto out;
3039 r = 0;
3040 break;
3041 }
3042 case KVM_SET_FPU: {
3043 struct kvm_fpu fpu;
3044
3045 r = -EFAULT;
3046 if (copy_from_user(&fpu, argp, sizeof fpu))
3047 goto out;
3048 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
3049 if (r)
3050 goto out;
3051 r = 0;
3052 break;
3053 }
Eddie Dong96ad2cc2007-09-06 12:22:56 +03003054 case KVM_GET_LAPIC: {
3055 struct kvm_lapic_state lapic;
3056
3057 memset(&lapic, 0, sizeof lapic);
3058 r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic);
3059 if (r)
3060 goto out;
3061 r = -EFAULT;
3062 if (copy_to_user(argp, &lapic, sizeof lapic))
3063 goto out;
3064 r = 0;
3065 break;
3066 }
3067 case KVM_SET_LAPIC: {
3068 struct kvm_lapic_state lapic;
3069
3070 r = -EFAULT;
3071 if (copy_from_user(&lapic, argp, sizeof lapic))
3072 goto out;
3073 r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);;
3074 if (r)
3075 goto out;
3076 r = 0;
3077 break;
3078 }
Avi Kivitybccf2152007-02-21 18:04:26 +02003079 default:
3080 ;
3081 }
3082out:
3083 return r;
3084}
3085
3086static long kvm_vm_ioctl(struct file *filp,
3087 unsigned int ioctl, unsigned long arg)
3088{
3089 struct kvm *kvm = filp->private_data;
3090 void __user *argp = (void __user *)arg;
3091 int r = -EINVAL;
3092
3093 switch (ioctl) {
3094 case KVM_CREATE_VCPU:
3095 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
3096 if (r < 0)
3097 goto out;
3098 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003099 case KVM_SET_MEMORY_REGION: {
3100 struct kvm_memory_region kvm_mem;
3101
3102 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00003103 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003104 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02003105 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003106 if (r)
3107 goto out;
3108 break;
3109 }
Izik Eidus82ce2c92007-10-02 18:52:55 +02003110 case KVM_SET_NR_MMU_PAGES:
3111 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
3112 if (r)
3113 goto out;
3114 break;
3115 case KVM_GET_NR_MMU_PAGES:
3116 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
3117 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003118 case KVM_GET_DIRTY_LOG: {
3119 struct kvm_dirty_log log;
3120
3121 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00003122 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003123 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02003124 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003125 if (r)
3126 goto out;
3127 break;
3128 }
Avi Kivitye8207542007-03-30 16:54:30 +03003129 case KVM_SET_MEMORY_ALIAS: {
3130 struct kvm_memory_alias alias;
3131
3132 r = -EFAULT;
3133 if (copy_from_user(&alias, argp, sizeof alias))
3134 goto out;
3135 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
3136 if (r)
3137 goto out;
3138 break;
3139 }
Eddie Dong85f455f2007-07-06 12:20:49 +03003140 case KVM_CREATE_IRQCHIP:
3141 r = -ENOMEM;
3142 kvm->vpic = kvm_create_pic(kvm);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03003143 if (kvm->vpic) {
3144 r = kvm_ioapic_init(kvm);
3145 if (r) {
3146 kfree(kvm->vpic);
3147 kvm->vpic = NULL;
3148 goto out;
3149 }
3150 }
Eddie Dong85f455f2007-07-06 12:20:49 +03003151 else
3152 goto out;
3153 break;
3154 case KVM_IRQ_LINE: {
3155 struct kvm_irq_level irq_event;
3156
3157 r = -EFAULT;
3158 if (copy_from_user(&irq_event, argp, sizeof irq_event))
3159 goto out;
3160 if (irqchip_in_kernel(kvm)) {
Eddie Dong9cf98822007-07-22 10:36:31 +03003161 mutex_lock(&kvm->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +03003162 if (irq_event.irq < 16)
3163 kvm_pic_set_irq(pic_irqchip(kvm),
3164 irq_event.irq,
3165 irq_event.level);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03003166 kvm_ioapic_set_irq(kvm->vioapic,
3167 irq_event.irq,
3168 irq_event.level);
Eddie Dong9cf98822007-07-22 10:36:31 +03003169 mutex_unlock(&kvm->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +03003170 r = 0;
3171 }
3172 break;
3173 }
He, Qing6ceb9d72007-07-26 11:05:18 +03003174 case KVM_GET_IRQCHIP: {
3175 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3176 struct kvm_irqchip chip;
3177
3178 r = -EFAULT;
3179 if (copy_from_user(&chip, argp, sizeof chip))
3180 goto out;
3181 r = -ENXIO;
3182 if (!irqchip_in_kernel(kvm))
3183 goto out;
3184 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
3185 if (r)
3186 goto out;
3187 r = -EFAULT;
3188 if (copy_to_user(argp, &chip, sizeof chip))
3189 goto out;
3190 r = 0;
3191 break;
3192 }
3193 case KVM_SET_IRQCHIP: {
3194 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3195 struct kvm_irqchip chip;
3196
3197 r = -EFAULT;
3198 if (copy_from_user(&chip, argp, sizeof chip))
3199 goto out;
3200 r = -ENXIO;
3201 if (!irqchip_in_kernel(kvm))
3202 goto out;
3203 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
3204 if (r)
3205 goto out;
3206 r = 0;
3207 break;
3208 }
Avi Kivityf17abe92007-02-21 19:28:04 +02003209 default:
3210 ;
3211 }
3212out:
3213 return r;
3214}
3215
3216static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
3217 unsigned long address,
3218 int *type)
3219{
3220 struct kvm *kvm = vma->vm_file->private_data;
3221 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02003222 struct page *page;
3223
Avi Kivityf17abe92007-02-21 19:28:04 +02003224 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity954bbbc22007-03-30 14:02:32 +03003225 page = gfn_to_page(kvm, pgoff);
Avi Kivityf17abe92007-02-21 19:28:04 +02003226 if (!page)
3227 return NOPAGE_SIGBUS;
3228 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03003229 if (type != NULL)
3230 *type = VM_FAULT_MINOR;
3231
Avi Kivityf17abe92007-02-21 19:28:04 +02003232 return page;
3233}
3234
3235static struct vm_operations_struct kvm_vm_vm_ops = {
3236 .nopage = kvm_vm_nopage,
3237};
3238
3239static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
3240{
3241 vma->vm_ops = &kvm_vm_vm_ops;
3242 return 0;
3243}
3244
3245static struct file_operations kvm_vm_fops = {
3246 .release = kvm_vm_release,
3247 .unlocked_ioctl = kvm_vm_ioctl,
3248 .compat_ioctl = kvm_vm_ioctl,
3249 .mmap = kvm_vm_mmap,
3250};
3251
3252static int kvm_dev_ioctl_create_vm(void)
3253{
3254 int fd, r;
3255 struct inode *inode;
3256 struct file *file;
3257 struct kvm *kvm;
3258
Avi Kivityf17abe92007-02-21 19:28:04 +02003259 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04003260 if (IS_ERR(kvm))
3261 return PTR_ERR(kvm);
3262 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
3263 if (r) {
3264 kvm_destroy_vm(kvm);
3265 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02003266 }
3267
Avi Kivitybccf2152007-02-21 18:04:26 +02003268 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02003269
Avi Kivityf17abe92007-02-21 19:28:04 +02003270 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02003271}
3272
3273static long kvm_dev_ioctl(struct file *filp,
3274 unsigned int ioctl, unsigned long arg)
3275{
3276 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02003277 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02003278
3279 switch (ioctl) {
3280 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02003281 r = -EINVAL;
3282 if (arg)
3283 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02003284 r = KVM_API_VERSION;
3285 break;
3286 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02003287 r = -EINVAL;
3288 if (arg)
3289 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02003290 r = kvm_dev_ioctl_create_vm();
3291 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003292 case KVM_GET_MSR_INDEX_LIST: {
Al Viro2f366982007-02-09 16:38:35 +00003293 struct kvm_msr_list __user *user_msr_list = argp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003294 struct kvm_msr_list msr_list;
3295 unsigned n;
3296
3297 r = -EFAULT;
3298 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
3299 goto out;
3300 n = msr_list.nmsrs;
Avi Kivity6f00e682007-01-26 00:56:40 -08003301 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003302 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
3303 goto out;
3304 r = -E2BIG;
Michael Riepebf591b22006-12-22 01:05:36 -08003305 if (n < num_msrs_to_save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003306 goto out;
3307 r = -EFAULT;
3308 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
Michael Riepebf591b22006-12-22 01:05:36 -08003309 num_msrs_to_save * sizeof(u32)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003310 goto out;
Avi Kivity6f00e682007-01-26 00:56:40 -08003311 if (copy_to_user(user_msr_list->indices
3312 + num_msrs_to_save * sizeof(u32),
3313 &emulated_msrs,
3314 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
3315 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003316 r = 0;
Avi Kivitycc1d8952007-01-05 16:36:58 -08003317 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003318 }
Eddie Dong85f455f2007-07-06 12:20:49 +03003319 case KVM_CHECK_EXTENSION: {
3320 int ext = (long)argp;
3321
3322 switch (ext) {
3323 case KVM_CAP_IRQCHIP:
Eddie Dongb6958ce2007-07-18 12:15:21 +03003324 case KVM_CAP_HLT:
Izik Eidus82ce2c92007-10-02 18:52:55 +02003325 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Eddie Dong85f455f2007-07-06 12:20:49 +03003326 r = 1;
3327 break;
3328 default:
3329 r = 0;
3330 break;
3331 }
Avi Kivity5d308f42007-03-01 17:56:20 +02003332 break;
Eddie Dong85f455f2007-07-06 12:20:49 +03003333 }
Avi Kivity07c45a32007-03-07 13:05:38 +02003334 case KVM_GET_VCPU_MMAP_SIZE:
3335 r = -EINVAL;
3336 if (arg)
3337 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02003338 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02003339 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003340 default:
3341 ;
3342 }
3343out:
3344 return r;
3345}
3346
Avi Kivity6aa8b732006-12-10 02:21:36 -08003347static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003348 .unlocked_ioctl = kvm_dev_ioctl,
3349 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003350};
3351
3352static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02003353 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003354 "kvm",
3355 &kvm_chardev_ops,
3356};
3357
Avi Kivity774c47f2007-02-12 00:54:47 -08003358/*
3359 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
3360 * cached on it.
3361 */
3362static void decache_vcpus_on_cpu(int cpu)
3363{
3364 struct kvm *vm;
3365 struct kvm_vcpu *vcpu;
3366 int i;
3367
3368 spin_lock(&kvm_lock);
Shaohua Li11ec2802007-07-23 14:51:37 +08003369 list_for_each_entry(vm, &vm_list, vm_list)
Avi Kivity774c47f2007-02-12 00:54:47 -08003370 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003371 vcpu = vm->vcpus[i];
3372 if (!vcpu)
3373 continue;
Avi Kivity774c47f2007-02-12 00:54:47 -08003374 /*
3375 * If the vcpu is locked, then it is running on some
3376 * other cpu and therefore it is not cached on the
3377 * cpu in question.
3378 *
3379 * If it's not locked, check the last cpu it executed
3380 * on.
3381 */
3382 if (mutex_trylock(&vcpu->mutex)) {
3383 if (vcpu->cpu == cpu) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003384 kvm_x86_ops->vcpu_decache(vcpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08003385 vcpu->cpu = -1;
3386 }
3387 mutex_unlock(&vcpu->mutex);
3388 }
3389 }
3390 spin_unlock(&kvm_lock);
3391}
3392
Avi Kivity1b6c0162007-05-24 13:03:52 +03003393static void hardware_enable(void *junk)
3394{
3395 int cpu = raw_smp_processor_id();
3396
3397 if (cpu_isset(cpu, cpus_hardware_enabled))
3398 return;
3399 cpu_set(cpu, cpus_hardware_enabled);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003400 kvm_x86_ops->hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003401}
3402
3403static void hardware_disable(void *junk)
3404{
3405 int cpu = raw_smp_processor_id();
3406
3407 if (!cpu_isset(cpu, cpus_hardware_enabled))
3408 return;
3409 cpu_clear(cpu, cpus_hardware_enabled);
3410 decache_vcpus_on_cpu(cpu);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003411 kvm_x86_ops->hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003412}
3413
Avi Kivity774c47f2007-02-12 00:54:47 -08003414static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
3415 void *v)
3416{
3417 int cpu = (long)v;
3418
3419 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03003420 case CPU_DYING:
3421 case CPU_DYING_FROZEN:
Avi Kivity6ec8a852007-08-19 15:57:26 +03003422 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3423 cpu);
3424 hardware_disable(NULL);
3425 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08003426 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003427 case CPU_UP_CANCELED_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003428 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3429 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003430 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003431 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02003432 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003433 case CPU_ONLINE_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003434 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
3435 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003436 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003437 break;
3438 }
3439 return NOTIFY_OK;
3440}
3441
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003442static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
3443 void *v)
3444{
3445 if (val == SYS_RESTART) {
3446 /*
3447 * Some (well, at least mine) BIOSes hang on reboot if
3448 * in vmx root mode.
3449 */
3450 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
3451 on_each_cpu(hardware_disable, NULL, 0, 1);
3452 }
3453 return NOTIFY_OK;
3454}
3455
3456static struct notifier_block kvm_reboot_notifier = {
3457 .notifier_call = kvm_reboot,
3458 .priority = 0,
3459};
3460
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04003461void kvm_io_bus_init(struct kvm_io_bus *bus)
3462{
3463 memset(bus, 0, sizeof(*bus));
3464}
3465
3466void kvm_io_bus_destroy(struct kvm_io_bus *bus)
3467{
3468 int i;
3469
3470 for (i = 0; i < bus->dev_count; i++) {
3471 struct kvm_io_device *pos = bus->devs[i];
3472
3473 kvm_iodevice_destructor(pos);
3474 }
3475}
3476
3477struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
3478{
3479 int i;
3480
3481 for (i = 0; i < bus->dev_count; i++) {
3482 struct kvm_io_device *pos = bus->devs[i];
3483
3484 if (pos->in_range(pos, addr))
3485 return pos;
3486 }
3487
3488 return NULL;
3489}
3490
3491void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
3492{
3493 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
3494
3495 bus->devs[bus->dev_count++] = dev;
3496}
3497
Avi Kivity774c47f2007-02-12 00:54:47 -08003498static struct notifier_block kvm_cpu_notifier = {
3499 .notifier_call = kvm_cpu_hotplug,
3500 .priority = 20, /* must be > scheduler priority */
3501};
3502
Avi Kivity1165f5f2007-04-19 17:27:43 +03003503static u64 stat_get(void *_offset)
3504{
3505 unsigned offset = (long)_offset;
3506 u64 total = 0;
3507 struct kvm *kvm;
3508 struct kvm_vcpu *vcpu;
3509 int i;
3510
3511 spin_lock(&kvm_lock);
3512 list_for_each_entry(kvm, &vm_list, vm_list)
3513 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003514 vcpu = kvm->vcpus[i];
3515 if (vcpu)
3516 total += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03003517 }
3518 spin_unlock(&kvm_lock);
3519 return total;
3520}
3521
Rusty Russell3dea7ca2007-08-01 10:12:22 +10003522DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n");
Avi Kivity1165f5f2007-04-19 17:27:43 +03003523
Avi Kivity6aa8b732006-12-10 02:21:36 -08003524static __init void kvm_init_debug(void)
3525{
3526 struct kvm_stats_debugfs_item *p;
3527
Al Viro8b6d44c2007-02-09 16:38:40 +00003528 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003529 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03003530 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
3531 (void *)(long)p->offset,
3532 &stat_fops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003533}
3534
3535static void kvm_exit_debug(void)
3536{
3537 struct kvm_stats_debugfs_item *p;
3538
3539 for (p = debugfs_entries; p->name; ++p)
3540 debugfs_remove(p->dentry);
3541 debugfs_remove(debugfs_dir);
3542}
3543
Avi Kivity59ae6c62007-02-12 00:54:48 -08003544static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3545{
Avi Kivity4267c412007-05-24 13:09:41 +03003546 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003547 return 0;
3548}
3549
3550static int kvm_resume(struct sys_device *dev)
3551{
Avi Kivity4267c412007-05-24 13:09:41 +03003552 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003553 return 0;
3554}
3555
3556static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01003557 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08003558 .suspend = kvm_suspend,
3559 .resume = kvm_resume,
3560};
3561
3562static struct sys_device kvm_sysdev = {
3563 .id = 0,
3564 .cls = &kvm_sysdev_class,
3565};
3566
Avi Kivity6aa8b732006-12-10 02:21:36 -08003567hpa_t bad_page_address;
3568
Avi Kivity15ad7142007-07-11 18:17:21 +03003569static inline
3570struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
3571{
3572 return container_of(pn, struct kvm_vcpu, preempt_notifier);
3573}
3574
3575static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
3576{
3577 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3578
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003579 kvm_x86_ops->vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003580}
3581
3582static void kvm_sched_out(struct preempt_notifier *pn,
3583 struct task_struct *next)
3584{
3585 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3586
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003587 kvm_x86_ops->vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003588}
3589
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003590int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10003591 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003592{
3593 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003594 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003595
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003596 if (kvm_x86_ops) {
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08003597 printk(KERN_ERR "kvm: already loaded the other module\n");
3598 return -EEXIST;
3599 }
3600
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003601 if (!ops->cpu_has_kvm_support()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003602 printk(KERN_ERR "kvm: no hardware support\n");
3603 return -EOPNOTSUPP;
3604 }
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003605 if (ops->disabled_by_bios()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003606 printk(KERN_ERR "kvm: disabled by bios\n");
3607 return -EOPNOTSUPP;
3608 }
3609
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003610 kvm_x86_ops = ops;
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003611
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003612 r = kvm_x86_ops->hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003613 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02003614 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003615
Yang, Sheng002c7f72007-07-31 14:23:01 +03003616 for_each_online_cpu(cpu) {
3617 smp_call_function_single(cpu,
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003618 kvm_x86_ops->check_processor_compatibility,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003619 &r, 0, 1);
3620 if (r < 0)
3621 goto out_free_0;
3622 }
3623
Avi Kivity1b6c0162007-05-24 13:03:52 +03003624 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003625 r = register_cpu_notifier(&kvm_cpu_notifier);
3626 if (r)
3627 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003628 register_reboot_notifier(&kvm_reboot_notifier);
3629
Avi Kivity59ae6c62007-02-12 00:54:48 -08003630 r = sysdev_class_register(&kvm_sysdev_class);
3631 if (r)
3632 goto out_free_2;
3633
3634 r = sysdev_register(&kvm_sysdev);
3635 if (r)
3636 goto out_free_3;
3637
Rusty Russellc16f8622007-07-30 21:12:19 +10003638 /* A kmem cache lets us meet the alignment requirements of fx_save. */
3639 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
3640 __alignof__(struct kvm_vcpu), 0, 0);
3641 if (!kvm_vcpu_cache) {
3642 r = -ENOMEM;
3643 goto out_free_4;
3644 }
3645
Avi Kivity6aa8b732006-12-10 02:21:36 -08003646 kvm_chardev_ops.owner = module;
3647
3648 r = misc_register(&kvm_dev);
3649 if (r) {
3650 printk (KERN_ERR "kvm: misc device register failed\n");
3651 goto out_free;
3652 }
3653
Avi Kivity15ad7142007-07-11 18:17:21 +03003654 kvm_preempt_ops.sched_in = kvm_sched_in;
3655 kvm_preempt_ops.sched_out = kvm_sched_out;
3656
Avi Kivityc7addb92007-09-16 18:58:32 +02003657 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
3658
3659 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003660
3661out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10003662 kmem_cache_destroy(kvm_vcpu_cache);
3663out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08003664 sysdev_unregister(&kvm_sysdev);
3665out_free_3:
3666 sysdev_class_unregister(&kvm_sysdev_class);
3667out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003668 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08003669 unregister_cpu_notifier(&kvm_cpu_notifier);
3670out_free_1:
Avi Kivity1b6c0162007-05-24 13:03:52 +03003671 on_each_cpu(hardware_disable, NULL, 0, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03003672out_free_0:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003673 kvm_x86_ops->hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02003674out:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003675 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003676 return r;
3677}
3678
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003679void kvm_exit_x86(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003680{
3681 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10003682 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003683 sysdev_unregister(&kvm_sysdev);
3684 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003685 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003686 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003687 on_each_cpu(hardware_disable, NULL, 0, 1);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003688 kvm_x86_ops->hardware_unsetup();
3689 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003690}
3691
3692static __init int kvm_init(void)
3693{
3694 static struct page *bad_page;
Avi Kivity37e29d92007-02-20 14:07:37 +02003695 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003696
Avi Kivityb5a33a72007-04-15 16:31:09 +03003697 r = kvm_mmu_module_init();
3698 if (r)
3699 goto out4;
3700
Avi Kivity6aa8b732006-12-10 02:21:36 -08003701 kvm_init_debug();
3702
Michael Riepebf591b22006-12-22 01:05:36 -08003703 kvm_init_msr_list();
3704
Avi Kivity6aa8b732006-12-10 02:21:36 -08003705 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
3706 r = -ENOMEM;
3707 goto out;
3708 }
3709
3710 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
3711 memset(__va(bad_page_address), 0, PAGE_SIZE);
3712
Avi Kivity58e690e2007-02-26 16:29:43 +02003713 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003714
3715out:
3716 kvm_exit_debug();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003717 kvm_mmu_module_exit();
3718out4:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003719 return r;
3720}
3721
3722static __exit void kvm_exit(void)
3723{
3724 kvm_exit_debug();
3725 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
Avi Kivityb5a33a72007-04-15 16:31:09 +03003726 kvm_mmu_module_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003727}
3728
3729module_init(kvm_init)
3730module_exit(kvm_exit)
3731
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003732EXPORT_SYMBOL_GPL(kvm_init_x86);
3733EXPORT_SYMBOL_GPL(kvm_exit_x86);