blob: 22b143feb66d79f702fbf642c5309e4507f2642d [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>
Izik Eidus6fc138d2007-10-09 19:20:39 +020043#include <linux/pagemap.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080044
Avi Kivitye4956062007-06-28 14:15:57 -040045#include <asm/processor.h>
46#include <asm/msr.h>
47#include <asm/io.h>
48#include <asm/uaccess.h>
49#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080050
51MODULE_AUTHOR("Qumranet");
52MODULE_LICENSE("GPL");
53
Avi Kivity133de902007-02-12 00:54:44 -080054static DEFINE_SPINLOCK(kvm_lock);
55static LIST_HEAD(vm_list);
56
Avi Kivity1b6c0162007-05-24 13:03:52 +030057static cpumask_t cpus_hardware_enabled;
58
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +030059struct kvm_x86_ops *kvm_x86_ops;
Rusty Russellc16f8622007-07-30 21:12:19 +100060struct kmem_cache *kvm_vcpu_cache;
61EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030062
Avi Kivity15ad7142007-07-11 18:17:21 +030063static __read_mostly struct preempt_ops kvm_preempt_ops;
64
Avi Kivity1165f5f2007-04-19 17:27:43 +030065#define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
Avi Kivity6aa8b732006-12-10 02:21:36 -080066
67static struct kvm_stats_debugfs_item {
68 const char *name;
Avi Kivity1165f5f2007-04-19 17:27:43 +030069 int offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -080070 struct dentry *dentry;
71} debugfs_entries[] = {
Avi Kivity1165f5f2007-04-19 17:27:43 +030072 { "pf_fixed", STAT_OFFSET(pf_fixed) },
73 { "pf_guest", STAT_OFFSET(pf_guest) },
74 { "tlb_flush", STAT_OFFSET(tlb_flush) },
75 { "invlpg", STAT_OFFSET(invlpg) },
76 { "exits", STAT_OFFSET(exits) },
77 { "io_exits", STAT_OFFSET(io_exits) },
78 { "mmio_exits", STAT_OFFSET(mmio_exits) },
79 { "signal_exits", STAT_OFFSET(signal_exits) },
80 { "irq_window", STAT_OFFSET(irq_window_exits) },
81 { "halt_exits", STAT_OFFSET(halt_exits) },
Eddie Dongb6958ce2007-07-18 12:15:21 +030082 { "halt_wakeup", STAT_OFFSET(halt_wakeup) },
Avi Kivity1165f5f2007-04-19 17:27:43 +030083 { "request_irq", STAT_OFFSET(request_irq_exits) },
84 { "irq_exits", STAT_OFFSET(irq_exits) },
Avi Kivitye6adf282007-04-30 16:07:54 +030085 { "light_exits", STAT_OFFSET(light_exits) },
Eddie Dong2cc51562007-05-21 07:28:09 +030086 { "efer_reload", STAT_OFFSET(efer_reload) },
Avi Kivity1165f5f2007-04-19 17:27:43 +030087 { NULL }
Avi Kivity6aa8b732006-12-10 02:21:36 -080088};
89
90static struct dentry *debugfs_dir;
91
92#define MAX_IO_MSRS 256
93
Rusty Russell707d92f2007-07-17 23:19:08 +100094#define CR0_RESERVED_BITS \
95 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
96 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
97 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
Rusty Russell66aee912007-07-17 23:34:16 +100098#define CR4_RESERVED_BITS \
99 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
100 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
101 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
102 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
103
Rusty Russell7075bc82007-07-17 23:37:17 +1000104#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800105#define EFER_RESERVED_BITS 0xfffffffffffff2fe
106
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800107#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400108/* LDT or TSS descriptor in the GDT. 16 bytes. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800109struct segment_descriptor_64 {
110 struct segment_descriptor s;
111 u32 base_higher;
112 u32 pad_zero;
113};
114
115#endif
116
Avi Kivitybccf2152007-02-21 18:04:26 +0200117static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
118 unsigned long arg);
119
Avi Kivity6aa8b732006-12-10 02:21:36 -0800120unsigned long segment_base(u16 selector)
121{
122 struct descriptor_table gdt;
123 struct segment_descriptor *d;
124 unsigned long table_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800125 unsigned long v;
126
127 if (selector == 0)
128 return 0;
129
Mike Dayd77c26f2007-10-08 09:02:08 -0400130 asm("sgdt %0" : "=m"(gdt));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131 table_base = gdt.base;
132
133 if (selector & 4) { /* from ldt */
134 u16 ldt_selector;
135
Mike Dayd77c26f2007-10-08 09:02:08 -0400136 asm("sldt %0" : "=g"(ldt_selector));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800137 table_base = segment_base(ldt_selector);
138 }
139 d = (struct segment_descriptor *)(table_base + (selector & ~7));
Mike Dayd77c26f2007-10-08 09:02:08 -0400140 v = d->base_low | ((unsigned long)d->base_mid << 16) |
141 ((unsigned long)d->base_high << 24);
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800142#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400143 if (d->system == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
144 v |= ((unsigned long) \
145 ((struct segment_descriptor_64 *)d)->base_higher) << 32;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800146#endif
147 return v;
148}
149EXPORT_SYMBOL_GPL(segment_base);
150
James Morris5aacf0c2006-12-22 01:04:55 -0800151static inline int valid_vcpu(int n)
152{
153 return likely(n >= 0 && n < KVM_MAX_VCPUS);
154}
155
Avi Kivity7702fd12007-06-14 16:27:40 +0300156void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
157{
158 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
159 return;
160
161 vcpu->guest_fpu_loaded = 1;
Rusty Russellb114b082007-07-30 21:13:43 +1000162 fx_save(&vcpu->host_fx_image);
163 fx_restore(&vcpu->guest_fx_image);
Avi Kivity7702fd12007-06-14 16:27:40 +0300164}
165EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
166
167void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
168{
169 if (!vcpu->guest_fpu_loaded)
170 return;
171
172 vcpu->guest_fpu_loaded = 0;
Rusty Russellb114b082007-07-30 21:13:43 +1000173 fx_save(&vcpu->guest_fx_image);
174 fx_restore(&vcpu->host_fx_image);
Avi Kivity7702fd12007-06-14 16:27:40 +0300175}
176EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
177
Avi Kivity6aa8b732006-12-10 02:21:36 -0800178/*
179 * Switches to specified vcpu, until a matching vcpu_put()
180 */
Avi Kivitybccf2152007-02-21 18:04:26 +0200181static void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800182{
Avi Kivity15ad7142007-07-11 18:17:21 +0300183 int cpu;
184
Avi Kivitybccf2152007-02-21 18:04:26 +0200185 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300186 cpu = get_cpu();
187 preempt_notifier_register(&vcpu->preempt_notifier);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300188 kvm_x86_ops->vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300189 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200190}
191
Avi Kivity6aa8b732006-12-10 02:21:36 -0800192static void vcpu_put(struct kvm_vcpu *vcpu)
193{
Avi Kivity15ad7142007-07-11 18:17:21 +0300194 preempt_disable();
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300195 kvm_x86_ops->vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300196 preempt_notifier_unregister(&vcpu->preempt_notifier);
197 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800198 mutex_unlock(&vcpu->mutex);
199}
200
Avi Kivityd9e368d2007-06-07 19:18:30 +0300201static void ack_flush(void *_completed)
202{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300203}
204
205void kvm_flush_remote_tlbs(struct kvm *kvm)
206{
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200207 int i, cpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300208 cpumask_t cpus;
209 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300210
Avi Kivityd9e368d2007-06-07 19:18:30 +0300211 cpus_clear(cpus);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000212 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
213 vcpu = kvm->vcpus[i];
214 if (!vcpu)
215 continue;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300216 if (test_and_set_bit(KVM_TLB_FLUSH, &vcpu->requests))
217 continue;
218 cpu = vcpu->cpu;
219 if (cpu != -1 && cpu != raw_smp_processor_id())
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200220 cpu_set(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300221 }
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200222 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300223}
224
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000225int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
226{
227 struct page *page;
228 int r;
229
230 mutex_init(&vcpu->mutex);
231 vcpu->cpu = -1;
232 vcpu->mmu.root_hpa = INVALID_PAGE;
233 vcpu->kvm = kvm;
234 vcpu->vcpu_id = id;
He, Qingc5ec1532007-09-03 17:07:41 +0300235 if (!irqchip_in_kernel(kvm) || id == 0)
236 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
237 else
238 vcpu->mp_state = VCPU_MP_STATE_UNINITIALIZED;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300239 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000240
241 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
242 if (!page) {
243 r = -ENOMEM;
244 goto fail;
245 }
246 vcpu->run = page_address(page);
247
248 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
249 if (!page) {
250 r = -ENOMEM;
251 goto fail_free_run;
252 }
253 vcpu->pio_data = page_address(page);
254
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000255 r = kvm_mmu_create(vcpu);
256 if (r < 0)
257 goto fail_free_pio_data;
258
Rusty Russell76fafa52007-10-08 10:50:48 +1000259 if (irqchip_in_kernel(kvm)) {
260 r = kvm_create_lapic(vcpu);
261 if (r < 0)
262 goto fail_mmu_destroy;
263 }
264
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000265 return 0;
266
Rusty Russell76fafa52007-10-08 10:50:48 +1000267fail_mmu_destroy:
268 kvm_mmu_destroy(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000269fail_free_pio_data:
270 free_page((unsigned long)vcpu->pio_data);
271fail_free_run:
272 free_page((unsigned long)vcpu->run);
273fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000274 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000275}
276EXPORT_SYMBOL_GPL(kvm_vcpu_init);
277
278void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
279{
Rusty Russelld5894442007-10-08 10:48:30 +1000280 kvm_free_lapic(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000281 kvm_mmu_destroy(vcpu);
282 free_page((unsigned long)vcpu->pio_data);
283 free_page((unsigned long)vcpu->run);
284}
285EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
286
Avi Kivityf17abe92007-02-21 19:28:04 +0200287static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800288{
289 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800290
291 if (!kvm)
Avi Kivityf17abe92007-02-21 19:28:04 +0200292 return ERR_PTR(-ENOMEM);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800293
Eddie Dong74906342007-06-19 18:05:03 +0300294 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800295 mutex_init(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800296 INIT_LIST_HEAD(&kvm->active_mmu_pages);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400297 kvm_io_bus_init(&kvm->mmio_bus);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000298 spin_lock(&kvm_lock);
299 list_add(&kvm->vm_list, &vm_list);
300 spin_unlock(&kvm_lock);
Avi Kivityf17abe92007-02-21 19:28:04 +0200301 return kvm;
302}
303
Izik Eidus6fc138d2007-10-09 19:20:39 +0200304static void kvm_free_userspace_physmem(struct kvm_memory_slot *free)
305{
306 int i;
307
308 for (i = 0; i < free->npages; ++i) {
309 if (free->phys_mem[i]) {
310 if (!PageReserved(free->phys_mem[i]))
311 SetPageDirty(free->phys_mem[i]);
312 page_cache_release(free->phys_mem[i]);
313 }
314 }
315}
316
317static void kvm_free_kernel_physmem(struct kvm_memory_slot *free)
318{
319 int i;
320
321 for (i = 0; i < free->npages; ++i)
322 if (free->phys_mem[i])
323 __free_page(free->phys_mem[i]);
324}
325
Avi Kivity6aa8b732006-12-10 02:21:36 -0800326/*
327 * Free any memory in @free but not in @dont.
328 */
329static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
330 struct kvm_memory_slot *dont)
331{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800332 if (!dont || free->phys_mem != dont->phys_mem)
333 if (free->phys_mem) {
Izik Eidus6fc138d2007-10-09 19:20:39 +0200334 if (free->user_alloc)
335 kvm_free_userspace_physmem(free);
336 else
337 kvm_free_kernel_physmem(free);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800338 vfree(free->phys_mem);
339 }
Izik Eidus290fc382007-09-27 14:11:22 +0200340 if (!dont || free->rmap != dont->rmap)
341 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800342
343 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
344 vfree(free->dirty_bitmap);
345
Al Viro8b6d44c2007-02-09 16:38:40 +0000346 free->phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800347 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000348 free->dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800349}
350
351static void kvm_free_physmem(struct kvm *kvm)
352{
353 int i;
354
355 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000356 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800357}
358
Avi Kivity039576c2007-03-20 12:46:50 +0200359static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
360{
361 int i;
362
Rusty Russell3077c4512007-07-30 16:41:57 +1000363 for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i)
Avi Kivity039576c2007-03-20 12:46:50 +0200364 if (vcpu->pio.guest_pages[i]) {
365 __free_page(vcpu->pio.guest_pages[i]);
366 vcpu->pio.guest_pages[i] = NULL;
367 }
368}
369
Avi Kivity7b53aa52007-06-05 12:17:03 +0300370static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
371{
Avi Kivity7b53aa52007-06-05 12:17:03 +0300372 vcpu_load(vcpu);
373 kvm_mmu_unload(vcpu);
374 vcpu_put(vcpu);
375}
376
Avi Kivity6aa8b732006-12-10 02:21:36 -0800377static void kvm_free_vcpus(struct kvm *kvm)
378{
379 unsigned int i;
380
Avi Kivity7b53aa52007-06-05 12:17:03 +0300381 /*
382 * Unpin any mmu pages first.
383 */
384 for (i = 0; i < KVM_MAX_VCPUS; ++i)
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000385 if (kvm->vcpus[i])
386 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
387 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
388 if (kvm->vcpus[i]) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300389 kvm_x86_ops->vcpu_free(kvm->vcpus[i]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000390 kvm->vcpus[i] = NULL;
391 }
392 }
393
Avi Kivity6aa8b732006-12-10 02:21:36 -0800394}
395
Avi Kivityf17abe92007-02-21 19:28:04 +0200396static void kvm_destroy_vm(struct kvm *kvm)
397{
Avi Kivity133de902007-02-12 00:54:44 -0800398 spin_lock(&kvm_lock);
399 list_del(&kvm->vm_list);
400 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300401 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400402 kvm_io_bus_destroy(&kvm->mmio_bus);
Eddie Dong85f455f2007-07-06 12:20:49 +0300403 kfree(kvm->vpic);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300404 kfree(kvm->vioapic);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800405 kvm_free_vcpus(kvm);
406 kvm_free_physmem(kvm);
407 kfree(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200408}
409
410static int kvm_vm_release(struct inode *inode, struct file *filp)
411{
412 struct kvm *kvm = filp->private_data;
413
414 kvm_destroy_vm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800415 return 0;
416}
417
418static void inject_gp(struct kvm_vcpu *vcpu)
419{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300420 kvm_x86_ops->inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800421}
422
Avi Kivity1342d352007-01-05 16:36:39 -0800423/*
424 * Load the pae pdptrs. Return true is they are all valid.
425 */
426static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800427{
428 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
Avi Kivity1342d352007-01-05 16:36:39 -0800429 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800430 int i;
Avi Kivity1342d352007-01-05 16:36:39 -0800431 int ret;
Rusty Russellc820c2a2007-07-25 13:29:51 +1000432 u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800433
Shaohua Li11ec2802007-07-23 14:51:37 +0800434 mutex_lock(&vcpu->kvm->lock);
Izik Eidus195aefd2007-10-01 22:14:18 +0200435 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
436 offset * sizeof(u64), sizeof(pdpte));
437 if (ret < 0) {
Rusty Russellc820c2a2007-07-25 13:29:51 +1000438 ret = 0;
439 goto out;
440 }
Rusty Russellc820c2a2007-07-25 13:29:51 +1000441 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
442 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
Avi Kivity1342d352007-01-05 16:36:39 -0800443 ret = 0;
444 goto out;
445 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800446 }
Rusty Russellc820c2a2007-07-25 13:29:51 +1000447 ret = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800448
Rusty Russellc820c2a2007-07-25 13:29:51 +1000449 memcpy(vcpu->pdptrs, pdpte, sizeof(vcpu->pdptrs));
Avi Kivity1342d352007-01-05 16:36:39 -0800450out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800451 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800452
Avi Kivity1342d352007-01-05 16:36:39 -0800453 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800454}
455
456void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
457{
Rusty Russell707d92f2007-07-17 23:19:08 +1000458 if (cr0 & CR0_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800459 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
460 cr0, vcpu->cr0);
461 inject_gp(vcpu);
462 return;
463 }
464
Rusty Russell707d92f2007-07-17 23:19:08 +1000465 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800466 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
467 inject_gp(vcpu);
468 return;
469 }
470
Rusty Russell707d92f2007-07-17 23:19:08 +1000471 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800472 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
473 "and a clear PE flag\n");
474 inject_gp(vcpu);
475 return;
476 }
477
Rusty Russell707d92f2007-07-17 23:19:08 +1000478 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800479#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800480 if ((vcpu->shadow_efer & EFER_LME)) {
481 int cs_db, cs_l;
482
483 if (!is_pae(vcpu)) {
484 printk(KERN_DEBUG "set_cr0: #GP, start paging "
485 "in long mode while PAE is disabled\n");
486 inject_gp(vcpu);
487 return;
488 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300489 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800490 if (cs_l) {
491 printk(KERN_DEBUG "set_cr0: #GP, start paging "
492 "in long mode while CS.L == 1\n");
493 inject_gp(vcpu);
494 return;
495
496 }
497 } else
498#endif
Avi Kivity1342d352007-01-05 16:36:39 -0800499 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800500 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
501 "reserved bits\n");
502 inject_gp(vcpu);
503 return;
504 }
505
506 }
507
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300508 kvm_x86_ops->set_cr0(vcpu, cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800509 vcpu->cr0 = cr0;
510
Shaohua Li11ec2802007-07-23 14:51:37 +0800511 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800512 kvm_mmu_reset_context(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800513 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800514 return;
515}
516EXPORT_SYMBOL_GPL(set_cr0);
517
518void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
519{
520 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
521}
522EXPORT_SYMBOL_GPL(lmsw);
523
524void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
525{
Rusty Russell66aee912007-07-17 23:34:16 +1000526 if (cr4 & CR4_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800527 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
528 inject_gp(vcpu);
529 return;
530 }
531
Avi Kivitya9058ec2006-12-29 16:49:37 -0800532 if (is_long_mode(vcpu)) {
Rusty Russell66aee912007-07-17 23:34:16 +1000533 if (!(cr4 & X86_CR4_PAE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800534 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
535 "in long mode\n");
536 inject_gp(vcpu);
537 return;
538 }
Rusty Russell66aee912007-07-17 23:34:16 +1000539 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
Avi Kivity1342d352007-01-05 16:36:39 -0800540 && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800541 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
542 inject_gp(vcpu);
Rusty Russell310bc762007-07-23 17:11:02 +1000543 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800544 }
545
Rusty Russell66aee912007-07-17 23:34:16 +1000546 if (cr4 & X86_CR4_VMXE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800547 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
548 inject_gp(vcpu);
549 return;
550 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300551 kvm_x86_ops->set_cr4(vcpu, cr4);
Rusty Russell81f50e32007-09-06 01:20:38 +1000552 vcpu->cr4 = cr4;
Shaohua Li11ec2802007-07-23 14:51:37 +0800553 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800554 kvm_mmu_reset_context(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800555 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800556}
557EXPORT_SYMBOL_GPL(set_cr4);
558
559void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
560{
Avi Kivitya9058ec2006-12-29 16:49:37 -0800561 if (is_long_mode(vcpu)) {
Rusty Russellf802a302007-07-17 23:32:55 +1000562 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800563 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
564 inject_gp(vcpu);
565 return;
566 }
567 } else {
Rusty Russellf802a302007-07-17 23:32:55 +1000568 if (is_pae(vcpu)) {
569 if (cr3 & CR3_PAE_RESERVED_BITS) {
570 printk(KERN_DEBUG
571 "set_cr3: #GP, reserved bits\n");
572 inject_gp(vcpu);
573 return;
574 }
575 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
576 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
577 "reserved bits\n");
578 inject_gp(vcpu);
579 return;
580 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800581 }
Ryan Harper21764862007-09-18 14:05:16 -0500582 /*
583 * We don't check reserved bits in nonpae mode, because
584 * this isn't enforced, and VMware depends on this.
585 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800586 }
587
Shaohua Li11ec2802007-07-23 14:51:37 +0800588 mutex_lock(&vcpu->kvm->lock);
Ingo Molnard21225e2007-01-05 16:36:59 -0800589 /*
590 * Does the new cr3 value map to physical memory? (Note, we
591 * catch an invalid cr3 even in real-mode, because it would
592 * cause trouble later on when we turn on paging anyway.)
593 *
594 * A real CPU would silently accept an invalid cr3 and would
595 * attempt to use it - with largely undefined (and often hard
596 * to debug) behavior on the guest side.
597 */
598 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
599 inject_gp(vcpu);
Rusty Russellfb764412007-07-31 20:45:03 +1000600 else {
601 vcpu->cr3 = cr3;
Ingo Molnard21225e2007-01-05 16:36:59 -0800602 vcpu->mmu.new_cr3(vcpu);
Rusty Russellfb764412007-07-31 20:45:03 +1000603 }
Shaohua Li11ec2802007-07-23 14:51:37 +0800604 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800605}
606EXPORT_SYMBOL_GPL(set_cr3);
607
608void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
609{
Rusty Russell7075bc82007-07-17 23:37:17 +1000610 if (cr8 & CR8_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800611 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
612 inject_gp(vcpu);
613 return;
614 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300615 if (irqchip_in_kernel(vcpu->kvm))
616 kvm_lapic_set_tpr(vcpu, cr8);
617 else
618 vcpu->cr8 = cr8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800619}
620EXPORT_SYMBOL_GPL(set_cr8);
621
Eddie Dong7017fc32007-07-18 11:34:57 +0300622unsigned long get_cr8(struct kvm_vcpu *vcpu)
623{
Eddie Dong97222cc2007-09-12 10:58:04 +0300624 if (irqchip_in_kernel(vcpu->kvm))
625 return kvm_lapic_get_cr8(vcpu);
626 else
627 return vcpu->cr8;
Eddie Dong7017fc32007-07-18 11:34:57 +0300628}
629EXPORT_SYMBOL_GPL(get_cr8);
630
631u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
632{
Eddie Dong97222cc2007-09-12 10:58:04 +0300633 if (irqchip_in_kernel(vcpu->kvm))
634 return vcpu->apic_base;
635 else
636 return vcpu->apic_base;
Eddie Dong7017fc32007-07-18 11:34:57 +0300637}
638EXPORT_SYMBOL_GPL(kvm_get_apic_base);
639
640void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
641{
Eddie Dong97222cc2007-09-12 10:58:04 +0300642 /* TODO: reserve bits check */
643 if (irqchip_in_kernel(vcpu->kvm))
644 kvm_lapic_set_base(vcpu, data);
645 else
646 vcpu->apic_base = data;
Eddie Dong7017fc32007-07-18 11:34:57 +0300647}
648EXPORT_SYMBOL_GPL(kvm_set_apic_base);
649
Avi Kivity6aa8b732006-12-10 02:21:36 -0800650void fx_init(struct kvm_vcpu *vcpu)
651{
Rusty Russellb114b082007-07-30 21:13:43 +1000652 unsigned after_mxcsr_mask;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800653
Rusty Russell9bd01502007-07-30 16:29:56 +1000654 /* Initialize guest FPU by resetting ours and saving into guest's */
655 preempt_disable();
Rusty Russellb114b082007-07-30 21:13:43 +1000656 fx_save(&vcpu->host_fx_image);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800657 fpu_init();
Rusty Russellb114b082007-07-30 21:13:43 +1000658 fx_save(&vcpu->guest_fx_image);
659 fx_restore(&vcpu->host_fx_image);
Rusty Russell9bd01502007-07-30 16:29:56 +1000660 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800661
Amit Shah380102c2007-08-25 11:35:52 +0300662 vcpu->cr0 |= X86_CR0_ET;
Rusty Russellb114b082007-07-30 21:13:43 +1000663 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
664 vcpu->guest_fx_image.mxcsr = 0x1f80;
665 memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask,
666 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800667}
668EXPORT_SYMBOL_GPL(fx_init);
669
670/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800671 * Allocate some memory and give it an address in the guest physical address
672 * space.
673 *
674 * Discontiguous memory is allowed, mostly for framebuffers.
675 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200676static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
Izik Eidus6fc138d2007-10-09 19:20:39 +0200677 struct
678 kvm_userspace_memory_region *mem,
679 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800680{
681 int r;
682 gfn_t base_gfn;
683 unsigned long npages;
684 unsigned long i;
685 struct kvm_memory_slot *memslot;
686 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800687
688 r = -EINVAL;
689 /* General sanity checks */
690 if (mem->memory_size & (PAGE_SIZE - 1))
691 goto out;
692 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
693 goto out;
694 if (mem->slot >= KVM_MEMORY_SLOTS)
695 goto out;
696 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
697 goto out;
698
699 memslot = &kvm->memslots[mem->slot];
700 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
701 npages = mem->memory_size >> PAGE_SHIFT;
702
703 if (!npages)
704 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
705
Shaohua Li11ec2802007-07-23 14:51:37 +0800706 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800707
Avi Kivity6aa8b732006-12-10 02:21:36 -0800708 new = old = *memslot;
709
710 new.base_gfn = base_gfn;
711 new.npages = npages;
712 new.flags = mem->flags;
713
714 /* Disallow changing a memory slot's size. */
715 r = -EINVAL;
716 if (npages && old.npages && npages != old.npages)
717 goto out_unlock;
718
719 /* Check for overlaps */
720 r = -EEXIST;
721 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
722 struct kvm_memory_slot *s = &kvm->memslots[i];
723
724 if (s == memslot)
725 continue;
726 if (!((base_gfn + npages <= s->base_gfn) ||
727 (base_gfn >= s->base_gfn + s->npages)))
728 goto out_unlock;
729 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800730
731 /* Deallocate if slot is being removed */
732 if (!npages)
Al Viro8b6d44c2007-02-09 16:38:40 +0000733 new.phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800734
735 /* Free page dirty bitmap if unneeded */
736 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000737 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800738
739 r = -ENOMEM;
740
741 /* Allocate if a slot is being created */
742 if (npages && !new.phys_mem) {
743 new.phys_mem = vmalloc(npages * sizeof(struct page *));
744
745 if (!new.phys_mem)
Laurent Vivier0d8d2bd2007-08-30 14:56:21 +0200746 goto out_unlock;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800747
Mike Dayd77c26f2007-10-08 09:02:08 -0400748 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200749
750 if (!new.rmap)
751 goto out_unlock;
752
Avi Kivity6aa8b732006-12-10 02:21:36 -0800753 memset(new.phys_mem, 0, npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200754 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Izik Eidus6fc138d2007-10-09 19:20:39 +0200755 if (user_alloc) {
756 unsigned long pages_num;
757
758 new.user_alloc = 1;
759 down_read(&current->mm->mmap_sem);
760
761 pages_num = get_user_pages(current, current->mm,
762 mem->userspace_addr,
763 npages, 1, 1, new.phys_mem,
764 NULL);
765
766 up_read(&current->mm->mmap_sem);
767 if (pages_num != npages)
Laurent Vivier0d8d2bd2007-08-30 14:56:21 +0200768 goto out_unlock;
Izik Eidus6fc138d2007-10-09 19:20:39 +0200769 } else {
770 for (i = 0; i < npages; ++i) {
771 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
772 | __GFP_ZERO);
773 if (!new.phys_mem[i])
774 goto out_unlock;
775 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800776 }
777 }
778
779 /* Allocate page dirty bitmap if needed */
780 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
781 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
782
783 new.dirty_bitmap = vmalloc(dirty_bytes);
784 if (!new.dirty_bitmap)
Laurent Vivier0d8d2bd2007-08-30 14:56:21 +0200785 goto out_unlock;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800786 memset(new.dirty_bitmap, 0, dirty_bytes);
787 }
788
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789 if (mem->slot >= kvm->nmemslots)
790 kvm->nmemslots = mem->slot + 1;
791
Izik Eidus82ce2c92007-10-02 18:52:55 +0200792 if (!kvm->n_requested_mmu_pages) {
793 unsigned int n_pages;
794
795 if (npages) {
796 n_pages = npages * KVM_PERMILLE_MMU_PAGES / 1000;
797 kvm_mmu_change_mmu_pages(kvm, kvm->n_alloc_mmu_pages +
798 n_pages);
799 } else {
800 unsigned int nr_mmu_pages;
801
802 n_pages = old.npages * KVM_PERMILLE_MMU_PAGES / 1000;
803 nr_mmu_pages = kvm->n_alloc_mmu_pages - n_pages;
804 nr_mmu_pages = max(nr_mmu_pages,
805 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
806 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
807 }
808 }
809
Avi Kivity6aa8b732006-12-10 02:21:36 -0800810 *memslot = new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800811
Avi Kivity90cb0522007-07-17 13:04:56 +0300812 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
813 kvm_flush_remote_tlbs(kvm);
814
Shaohua Li11ec2802007-07-23 14:51:37 +0800815 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800816
Avi Kivity6aa8b732006-12-10 02:21:36 -0800817 kvm_free_physmem_slot(&old, &new);
818 return 0;
819
820out_unlock:
Shaohua Li11ec2802007-07-23 14:51:37 +0800821 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800822 kvm_free_physmem_slot(&new, &old);
823out:
824 return r;
825}
826
Izik Eidus82ce2c92007-10-02 18:52:55 +0200827static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
828 u32 kvm_nr_mmu_pages)
829{
830 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
831 return -EINVAL;
832
833 mutex_lock(&kvm->lock);
834
835 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
836 kvm->n_requested_mmu_pages = kvm_nr_mmu_pages;
837
838 mutex_unlock(&kvm->lock);
839 return 0;
840}
841
842static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
843{
844 return kvm->n_alloc_mmu_pages;
845}
846
Avi Kivity6aa8b732006-12-10 02:21:36 -0800847/*
848 * Get (and clear) the dirty memory log for a memory slot.
849 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200850static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
851 struct kvm_dirty_log *log)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800852{
853 struct kvm_memory_slot *memslot;
854 int r, i;
855 int n;
856 unsigned long any = 0;
857
Shaohua Li11ec2802007-07-23 14:51:37 +0800858 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800859
Avi Kivity6aa8b732006-12-10 02:21:36 -0800860 r = -EINVAL;
861 if (log->slot >= KVM_MEMORY_SLOTS)
862 goto out;
863
864 memslot = &kvm->memslots[log->slot];
865 r = -ENOENT;
866 if (!memslot->dirty_bitmap)
867 goto out;
868
Uri Lublincd1a4a92007-02-22 16:43:09 +0200869 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800870
Uri Lublincd1a4a92007-02-22 16:43:09 +0200871 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800872 any = memslot->dirty_bitmap[i];
873
874 r = -EFAULT;
875 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
876 goto out;
877
Rusty Russell39214912007-07-31 19:57:47 +1000878 /* If nothing is dirty, don't bother messing with page tables. */
879 if (any) {
Rusty Russell39214912007-07-31 19:57:47 +1000880 kvm_mmu_slot_remove_write_access(kvm, log->slot);
881 kvm_flush_remote_tlbs(kvm);
882 memset(memslot->dirty_bitmap, 0, n);
Rusty Russell39214912007-07-31 19:57:47 +1000883 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800884
885 r = 0;
886
887out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800888 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800889 return r;
890}
891
Avi Kivitye8207542007-03-30 16:54:30 +0300892/*
893 * Set a new alias region. Aliases map a portion of physical memory into
894 * another portion. This is useful for memory windows, for example the PC
895 * VGA region.
896 */
897static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
898 struct kvm_memory_alias *alias)
899{
900 int r, n;
901 struct kvm_mem_alias *p;
902
903 r = -EINVAL;
904 /* General sanity checks */
905 if (alias->memory_size & (PAGE_SIZE - 1))
906 goto out;
907 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
908 goto out;
909 if (alias->slot >= KVM_ALIAS_SLOTS)
910 goto out;
911 if (alias->guest_phys_addr + alias->memory_size
912 < alias->guest_phys_addr)
913 goto out;
914 if (alias->target_phys_addr + alias->memory_size
915 < alias->target_phys_addr)
916 goto out;
917
Shaohua Li11ec2802007-07-23 14:51:37 +0800918 mutex_lock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300919
920 p = &kvm->aliases[alias->slot];
921 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
922 p->npages = alias->memory_size >> PAGE_SHIFT;
923 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
924
925 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
926 if (kvm->aliases[n - 1].npages)
927 break;
928 kvm->naliases = n;
929
Avi Kivity90cb0522007-07-17 13:04:56 +0300930 kvm_mmu_zap_all(kvm);
Avi Kivitye8207542007-03-30 16:54:30 +0300931
Shaohua Li11ec2802007-07-23 14:51:37 +0800932 mutex_unlock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300933
934 return 0;
935
936out:
937 return r;
938}
939
He, Qing6ceb9d72007-07-26 11:05:18 +0300940static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
941{
942 int r;
943
944 r = 0;
945 switch (chip->chip_id) {
946 case KVM_IRQCHIP_PIC_MASTER:
Mike Dayd77c26f2007-10-08 09:02:08 -0400947 memcpy(&chip->chip.pic,
He, Qing6ceb9d72007-07-26 11:05:18 +0300948 &pic_irqchip(kvm)->pics[0],
949 sizeof(struct kvm_pic_state));
950 break;
951 case KVM_IRQCHIP_PIC_SLAVE:
Mike Dayd77c26f2007-10-08 09:02:08 -0400952 memcpy(&chip->chip.pic,
He, Qing6ceb9d72007-07-26 11:05:18 +0300953 &pic_irqchip(kvm)->pics[1],
954 sizeof(struct kvm_pic_state));
955 break;
He, Qing6bf9e962007-08-05 10:49:16 +0300956 case KVM_IRQCHIP_IOAPIC:
Mike Dayd77c26f2007-10-08 09:02:08 -0400957 memcpy(&chip->chip.ioapic,
He, Qing6bf9e962007-08-05 10:49:16 +0300958 ioapic_irqchip(kvm),
959 sizeof(struct kvm_ioapic_state));
960 break;
He, Qing6ceb9d72007-07-26 11:05:18 +0300961 default:
962 r = -EINVAL;
963 break;
964 }
965 return r;
966}
967
968static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
969{
970 int r;
971
972 r = 0;
973 switch (chip->chip_id) {
974 case KVM_IRQCHIP_PIC_MASTER:
Mike Dayd77c26f2007-10-08 09:02:08 -0400975 memcpy(&pic_irqchip(kvm)->pics[0],
He, Qing6ceb9d72007-07-26 11:05:18 +0300976 &chip->chip.pic,
977 sizeof(struct kvm_pic_state));
978 break;
979 case KVM_IRQCHIP_PIC_SLAVE:
Mike Dayd77c26f2007-10-08 09:02:08 -0400980 memcpy(&pic_irqchip(kvm)->pics[1],
He, Qing6ceb9d72007-07-26 11:05:18 +0300981 &chip->chip.pic,
982 sizeof(struct kvm_pic_state));
983 break;
He, Qing6bf9e962007-08-05 10:49:16 +0300984 case KVM_IRQCHIP_IOAPIC:
Mike Dayd77c26f2007-10-08 09:02:08 -0400985 memcpy(ioapic_irqchip(kvm),
He, Qing6bf9e962007-08-05 10:49:16 +0300986 &chip->chip.ioapic,
987 sizeof(struct kvm_ioapic_state));
988 break;
He, Qing6ceb9d72007-07-26 11:05:18 +0300989 default:
990 r = -EINVAL;
991 break;
992 }
993 kvm_pic_update_irq(pic_irqchip(kvm));
994 return r;
995}
996
Izik Eidus290fc382007-09-27 14:11:22 +0200997gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
Avi Kivitye8207542007-03-30 16:54:30 +0300998{
999 int i;
1000 struct kvm_mem_alias *alias;
1001
1002 for (i = 0; i < kvm->naliases; ++i) {
1003 alias = &kvm->aliases[i];
1004 if (gfn >= alias->base_gfn
1005 && gfn < alias->base_gfn + alias->npages)
1006 return alias->target_gfn + gfn - alias->base_gfn;
1007 }
1008 return gfn;
1009}
1010
1011static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012{
1013 int i;
1014
1015 for (i = 0; i < kvm->nmemslots; ++i) {
1016 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1017
1018 if (gfn >= memslot->base_gfn
1019 && gfn < memslot->base_gfn + memslot->npages)
1020 return memslot;
1021 }
Al Viro8b6d44c2007-02-09 16:38:40 +00001022 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001023}
Avi Kivitye8207542007-03-30 16:54:30 +03001024
1025struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1026{
1027 gfn = unalias_gfn(kvm, gfn);
1028 return __gfn_to_memslot(kvm, gfn);
1029}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001030
Avi Kivity954bbbc22007-03-30 14:02:32 +03001031struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1032{
1033 struct kvm_memory_slot *slot;
1034
Avi Kivitye8207542007-03-30 16:54:30 +03001035 gfn = unalias_gfn(kvm, gfn);
1036 slot = __gfn_to_memslot(kvm, gfn);
Avi Kivity954bbbc22007-03-30 14:02:32 +03001037 if (!slot)
1038 return NULL;
1039 return slot->phys_mem[gfn - slot->base_gfn];
1040}
1041EXPORT_SYMBOL_GPL(gfn_to_page);
1042
Izik Eidus195aefd2007-10-01 22:14:18 +02001043static int next_segment(unsigned long len, int offset)
1044{
1045 if (len > PAGE_SIZE - offset)
1046 return PAGE_SIZE - offset;
1047 else
1048 return len;
1049}
1050
1051int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
1052 int len)
1053{
1054 void *page_virt;
1055 struct page *page;
1056
1057 page = gfn_to_page(kvm, gfn);
1058 if (!page)
1059 return -EFAULT;
1060 page_virt = kmap_atomic(page, KM_USER0);
1061
1062 memcpy(data, page_virt + offset, len);
1063
1064 kunmap_atomic(page_virt, KM_USER0);
1065 return 0;
1066}
1067EXPORT_SYMBOL_GPL(kvm_read_guest_page);
1068
1069int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
1070{
1071 gfn_t gfn = gpa >> PAGE_SHIFT;
1072 int seg;
1073 int offset = offset_in_page(gpa);
1074 int ret;
1075
1076 while ((seg = next_segment(len, offset)) != 0) {
1077 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
1078 if (ret < 0)
1079 return ret;
1080 offset = 0;
1081 len -= seg;
1082 data += seg;
1083 ++gfn;
1084 }
1085 return 0;
1086}
1087EXPORT_SYMBOL_GPL(kvm_read_guest);
1088
1089int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
1090 int offset, int len)
1091{
1092 void *page_virt;
1093 struct page *page;
1094
1095 page = gfn_to_page(kvm, gfn);
1096 if (!page)
1097 return -EFAULT;
1098 page_virt = kmap_atomic(page, KM_USER0);
1099
1100 memcpy(page_virt + offset, data, len);
1101
1102 kunmap_atomic(page_virt, KM_USER0);
1103 mark_page_dirty(kvm, gfn);
1104 return 0;
1105}
1106EXPORT_SYMBOL_GPL(kvm_write_guest_page);
1107
1108int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
1109 unsigned long len)
1110{
1111 gfn_t gfn = gpa >> PAGE_SHIFT;
1112 int seg;
1113 int offset = offset_in_page(gpa);
1114 int ret;
1115
1116 while ((seg = next_segment(len, offset)) != 0) {
1117 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
1118 if (ret < 0)
1119 return ret;
1120 offset = 0;
1121 len -= seg;
1122 data += seg;
1123 ++gfn;
1124 }
1125 return 0;
1126}
1127
1128int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
1129{
1130 void *page_virt;
1131 struct page *page;
1132
1133 page = gfn_to_page(kvm, gfn);
1134 if (!page)
1135 return -EFAULT;
1136 page_virt = kmap_atomic(page, KM_USER0);
1137
1138 memset(page_virt + offset, 0, len);
1139
1140 kunmap_atomic(page_virt, KM_USER0);
1141 return 0;
1142}
1143EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1144
1145int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1146{
1147 gfn_t gfn = gpa >> PAGE_SHIFT;
1148 int seg;
1149 int offset = offset_in_page(gpa);
1150 int ret;
1151
1152 while ((seg = next_segment(len, offset)) != 0) {
1153 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1154 if (ret < 0)
1155 return ret;
1156 offset = 0;
1157 len -= seg;
1158 ++gfn;
1159 }
1160 return 0;
1161}
1162EXPORT_SYMBOL_GPL(kvm_clear_guest);
1163
Rusty Russell7e9d6192007-07-31 20:41:14 +10001164/* WARNING: Does not work on aliased pages. */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001165void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1166{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001167 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001168
Rusty Russell7e9d6192007-07-31 20:41:14 +10001169 memslot = __gfn_to_memslot(kvm, gfn);
1170 if (memslot && memslot->dirty_bitmap) {
1171 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001172
Rusty Russell7e9d6192007-07-31 20:41:14 +10001173 /* avoid RMW */
1174 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1175 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001176 }
1177}
1178
Laurent Viviere7d5d762007-07-30 13:41:19 +03001179int emulator_read_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001180 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001181 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001182 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001183{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001184 void *data = val;
1185
1186 while (bytes) {
1187 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1188 unsigned offset = addr & (PAGE_SIZE-1);
1189 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
Izik Eidus195aefd2007-10-01 22:14:18 +02001190 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001191
1192 if (gpa == UNMAPPED_GVA)
1193 return X86EMUL_PROPAGATE_FAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001194 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
1195 if (ret < 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001196 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001197
1198 bytes -= tocopy;
1199 data += tocopy;
1200 addr += tocopy;
1201 }
1202
1203 return X86EMUL_CONTINUE;
1204}
Laurent Viviere7d5d762007-07-30 13:41:19 +03001205EXPORT_SYMBOL_GPL(emulator_read_std);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001206
1207static int emulator_write_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001208 const void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001209 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001210 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001211{
Rusty Russellf0242472007-08-01 10:48:02 +10001212 pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001213 return X86EMUL_UNHANDLEABLE;
1214}
1215
Eddie Dong97222cc2007-09-12 10:58:04 +03001216/*
1217 * Only apic need an MMIO device hook, so shortcut now..
1218 */
1219static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
1220 gpa_t addr)
1221{
1222 struct kvm_io_device *dev;
1223
1224 if (vcpu->apic) {
1225 dev = &vcpu->apic->dev;
1226 if (dev->in_range(dev, addr))
1227 return dev;
1228 }
1229 return NULL;
1230}
1231
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001232static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1233 gpa_t addr)
1234{
Eddie Dong97222cc2007-09-12 10:58:04 +03001235 struct kvm_io_device *dev;
1236
1237 dev = vcpu_find_pervcpu_dev(vcpu, addr);
1238 if (dev == NULL)
1239 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1240 return dev;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001241}
1242
Eddie Dong74906342007-06-19 18:05:03 +03001243static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
1244 gpa_t addr)
1245{
1246 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
1247}
1248
Avi Kivity6aa8b732006-12-10 02:21:36 -08001249static int emulator_read_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001250 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001251 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001252 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001253{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001254 struct kvm_io_device *mmio_dev;
1255 gpa_t gpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001256
1257 if (vcpu->mmio_read_completed) {
1258 memcpy(val, vcpu->mmio_data, bytes);
1259 vcpu->mmio_read_completed = 0;
1260 return X86EMUL_CONTINUE;
Laurent Viviercebff022007-07-30 13:35:24 +03001261 } else if (emulator_read_std(addr, val, bytes, vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001262 == X86EMUL_CONTINUE)
1263 return X86EMUL_CONTINUE;
Avi Kivityd27d4ac2007-02-19 14:37:46 +02001264
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001265 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1266 if (gpa == UNMAPPED_GVA)
1267 return X86EMUL_PROPAGATE_FAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001268
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001269 /*
1270 * Is this MMIO handled locally?
1271 */
1272 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1273 if (mmio_dev) {
1274 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1275 return X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001276 }
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001277
1278 vcpu->mmio_needed = 1;
1279 vcpu->mmio_phys_addr = gpa;
1280 vcpu->mmio_size = bytes;
1281 vcpu->mmio_is_write = 0;
1282
1283 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001284}
1285
Avi Kivityda4a00f2007-01-05 16:36:44 -08001286static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity4c690a12007-04-22 15:28:19 +03001287 const void *val, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001288{
Izik Eidus195aefd2007-10-01 22:14:18 +02001289 int ret;
Avi Kivityda4a00f2007-01-05 16:36:44 -08001290
Izik Eidus195aefd2007-10-01 22:14:18 +02001291 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
1292 if (ret < 0)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001293 return 0;
Shaohua Life551882007-07-23 14:51:39 +08001294 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001295 return 1;
1296}
1297
Avi Kivityb0fcd902007-07-22 18:48:54 +03001298static int emulator_write_emulated_onepage(unsigned long addr,
1299 const void *val,
1300 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001301 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001302{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001303 struct kvm_io_device *mmio_dev;
1304 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001305
Avi Kivityc9047f52007-04-17 10:53:22 +03001306 if (gpa == UNMAPPED_GVA) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001307 kvm_x86_ops->inject_page_fault(vcpu, addr, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001308 return X86EMUL_PROPAGATE_FAULT;
Avi Kivityc9047f52007-04-17 10:53:22 +03001309 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001310
Avi Kivityda4a00f2007-01-05 16:36:44 -08001311 if (emulator_write_phys(vcpu, gpa, val, bytes))
1312 return X86EMUL_CONTINUE;
1313
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001314 /*
1315 * Is this MMIO handled locally?
1316 */
1317 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1318 if (mmio_dev) {
1319 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1320 return X86EMUL_CONTINUE;
1321 }
1322
Avi Kivity6aa8b732006-12-10 02:21:36 -08001323 vcpu->mmio_needed = 1;
1324 vcpu->mmio_phys_addr = gpa;
1325 vcpu->mmio_size = bytes;
1326 vcpu->mmio_is_write = 1;
Avi Kivity4c690a12007-04-22 15:28:19 +03001327 memcpy(vcpu->mmio_data, val, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001328
1329 return X86EMUL_CONTINUE;
1330}
1331
Laurent Viviere7d5d762007-07-30 13:41:19 +03001332int emulator_write_emulated(unsigned long addr,
Avi Kivityb0fcd902007-07-22 18:48:54 +03001333 const void *val,
1334 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001335 struct kvm_vcpu *vcpu)
Avi Kivityb0fcd902007-07-22 18:48:54 +03001336{
1337 /* Crossing a page boundary? */
1338 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1339 int rc, now;
1340
1341 now = -addr & ~PAGE_MASK;
Laurent Viviercebff022007-07-30 13:35:24 +03001342 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001343 if (rc != X86EMUL_CONTINUE)
1344 return rc;
1345 addr += now;
1346 val += now;
1347 bytes -= now;
1348 }
Laurent Viviercebff022007-07-30 13:35:24 +03001349 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001350}
Laurent Viviere7d5d762007-07-30 13:41:19 +03001351EXPORT_SYMBOL_GPL(emulator_write_emulated);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001352
Avi Kivity6aa8b732006-12-10 02:21:36 -08001353static int emulator_cmpxchg_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001354 const void *old,
1355 const void *new,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001356 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001357 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001358{
1359 static int reported;
1360
1361 if (!reported) {
1362 reported = 1;
1363 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1364 }
Laurent Viviercebff022007-07-30 13:35:24 +03001365 return emulator_write_emulated(addr, new, bytes, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366}
1367
1368static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1369{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001370 return kvm_x86_ops->get_segment_base(vcpu, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001371}
1372
1373int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1374{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001375 return X86EMUL_CONTINUE;
1376}
1377
1378int emulate_clts(struct kvm_vcpu *vcpu)
1379{
Amit Shah404fb882007-11-19 17:57:35 +02001380 kvm_x86_ops->set_cr0(vcpu, vcpu->cr0 & ~X86_CR0_TS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001381 return X86EMUL_CONTINUE;
1382}
1383
Mike Dayd77c26f2007-10-08 09:02:08 -04001384int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385{
1386 struct kvm_vcpu *vcpu = ctxt->vcpu;
1387
1388 switch (dr) {
1389 case 0 ... 3:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001390 *dest = kvm_x86_ops->get_dr(vcpu, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001391 return X86EMUL_CONTINUE;
1392 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001393 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001394 return X86EMUL_UNHANDLEABLE;
1395 }
1396}
1397
1398int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1399{
1400 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1401 int exception;
1402
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001403 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001404 if (exception) {
1405 /* FIXME: better handling */
1406 return X86EMUL_UNHANDLEABLE;
1407 }
1408 return X86EMUL_CONTINUE;
1409}
1410
Avi Kivity054b1362007-09-12 13:21:09 +03001411void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001412{
1413 static int reported;
1414 u8 opcodes[4];
Avi Kivity054b1362007-09-12 13:21:09 +03001415 unsigned long rip = vcpu->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001416 unsigned long rip_linear;
1417
Avi Kivity054b1362007-09-12 13:21:09 +03001418 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001419
1420 if (reported)
1421 return;
1422
Avi Kivity054b1362007-09-12 13:21:09 +03001423 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001424
Avi Kivity054b1362007-09-12 13:21:09 +03001425 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1426 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001427 reported = 1;
1428}
Avi Kivity054b1362007-09-12 13:21:09 +03001429EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001430
1431struct x86_emulate_ops emulate_ops = {
1432 .read_std = emulator_read_std,
1433 .write_std = emulator_write_std,
1434 .read_emulated = emulator_read_emulated,
1435 .write_emulated = emulator_write_emulated,
1436 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1437};
1438
1439int emulate_instruction(struct kvm_vcpu *vcpu,
1440 struct kvm_run *run,
1441 unsigned long cr2,
Laurent Vivier34273182007-09-18 11:27:37 +02001442 u16 error_code,
1443 int no_decode)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001444{
Laurent Viviera22436b2007-09-24 17:00:58 +02001445 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001446
Avi Kivitye7df56e2007-03-14 15:54:54 +02001447 vcpu->mmio_fault_cr2 = cr2;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001448 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001449
Avi Kivity6aa8b732006-12-10 02:21:36 -08001450 vcpu->mmio_is_write = 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03001451 vcpu->pio.string = 0;
Laurent Vivier34273182007-09-18 11:27:37 +02001452
1453 if (!no_decode) {
1454 int cs_db, cs_l;
1455 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1456
1457 vcpu->emulate_ctxt.vcpu = vcpu;
1458 vcpu->emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
1459 vcpu->emulate_ctxt.cr2 = cr2;
1460 vcpu->emulate_ctxt.mode =
1461 (vcpu->emulate_ctxt.eflags & X86_EFLAGS_VM)
1462 ? X86EMUL_MODE_REAL : cs_l
1463 ? X86EMUL_MODE_PROT64 : cs_db
1464 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1465
1466 if (vcpu->emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1467 vcpu->emulate_ctxt.cs_base = 0;
1468 vcpu->emulate_ctxt.ds_base = 0;
1469 vcpu->emulate_ctxt.es_base = 0;
1470 vcpu->emulate_ctxt.ss_base = 0;
1471 } else {
1472 vcpu->emulate_ctxt.cs_base =
1473 get_segment_base(vcpu, VCPU_SREG_CS);
1474 vcpu->emulate_ctxt.ds_base =
1475 get_segment_base(vcpu, VCPU_SREG_DS);
1476 vcpu->emulate_ctxt.es_base =
1477 get_segment_base(vcpu, VCPU_SREG_ES);
1478 vcpu->emulate_ctxt.ss_base =
1479 get_segment_base(vcpu, VCPU_SREG_SS);
1480 }
1481
1482 vcpu->emulate_ctxt.gs_base =
1483 get_segment_base(vcpu, VCPU_SREG_GS);
1484 vcpu->emulate_ctxt.fs_base =
1485 get_segment_base(vcpu, VCPU_SREG_FS);
1486
1487 r = x86_decode_insn(&vcpu->emulate_ctxt, &emulate_ops);
Laurent Viviera22436b2007-09-24 17:00:58 +02001488 if (r) {
1489 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1490 return EMULATE_DONE;
1491 return EMULATE_FAIL;
1492 }
Laurent Vivier34273182007-09-18 11:27:37 +02001493 }
1494
Laurent Viviera22436b2007-09-24 17:00:58 +02001495 r = x86_emulate_insn(&vcpu->emulate_ctxt, &emulate_ops);
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001496
Laurent Viviere70669a2007-08-05 10:36:40 +03001497 if (vcpu->pio.string)
1498 return EMULATE_DO_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001499
1500 if ((r || vcpu->mmio_is_write) && run) {
Jeff Dike8fc0d082007-07-17 12:26:59 -04001501 run->exit_reason = KVM_EXIT_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001502 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1503 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1504 run->mmio.len = vcpu->mmio_size;
1505 run->mmio.is_write = vcpu->mmio_is_write;
1506 }
1507
1508 if (r) {
Avi Kivitya4360362007-01-05 16:36:45 -08001509 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1510 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001511 if (!vcpu->mmio_needed) {
Avi Kivity054b1362007-09-12 13:21:09 +03001512 kvm_report_emulation_failure(vcpu, "mmio");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001513 return EMULATE_FAIL;
1514 }
1515 return EMULATE_DO_MMIO;
1516 }
1517
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001518 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier34273182007-09-18 11:27:37 +02001519 kvm_x86_ops->set_rflags(vcpu, vcpu->emulate_ctxt.eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001520
Avi Kivity02c83202007-04-29 15:02:17 +03001521 if (vcpu->mmio_is_write) {
1522 vcpu->mmio_needed = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001523 return EMULATE_DO_MMIO;
Avi Kivity02c83202007-04-29 15:02:17 +03001524 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001525
1526 return EMULATE_DONE;
1527}
1528EXPORT_SYMBOL_GPL(emulate_instruction);
1529
Eddie Dongb6958ce2007-07-18 12:15:21 +03001530/*
1531 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1532 */
He, Qingc5ec1532007-09-03 17:07:41 +03001533static void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001534{
1535 DECLARE_WAITQUEUE(wait, current);
1536
1537 add_wait_queue(&vcpu->wq, &wait);
1538
1539 /*
1540 * We will block until either an interrupt or a signal wakes us up
1541 */
He, Qingc5ec1532007-09-03 17:07:41 +03001542 while (!kvm_cpu_has_interrupt(vcpu)
1543 && !signal_pending(current)
1544 && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE
1545 && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) {
Eddie Dongb6958ce2007-07-18 12:15:21 +03001546 set_current_state(TASK_INTERRUPTIBLE);
1547 vcpu_put(vcpu);
1548 schedule();
1549 vcpu_load(vcpu);
1550 }
1551
He, Qingc5ec1532007-09-03 17:07:41 +03001552 __set_current_state(TASK_RUNNING);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001553 remove_wait_queue(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001554}
1555
Avi Kivityd3bef152007-06-05 15:53:05 +03001556int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1557{
Avi Kivityd3bef152007-06-05 15:53:05 +03001558 ++vcpu->stat.halt_exits;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001559 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc5ec1532007-09-03 17:07:41 +03001560 vcpu->mp_state = VCPU_MP_STATE_HALTED;
1561 kvm_vcpu_block(vcpu);
1562 if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE)
1563 return -EINTR;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001564 return 1;
1565 } else {
1566 vcpu->run->exit_reason = KVM_EXIT_HLT;
1567 return 0;
1568 }
Avi Kivityd3bef152007-06-05 15:53:05 +03001569}
1570EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1571
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001572int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
Avi Kivity270fd9b2007-02-19 14:37:47 +02001573{
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001574 unsigned long nr, a0, a1, a2, a3, ret;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001575
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001576 kvm_x86_ops->cache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001577
1578 nr = vcpu->regs[VCPU_REGS_RAX];
1579 a0 = vcpu->regs[VCPU_REGS_RBX];
1580 a1 = vcpu->regs[VCPU_REGS_RCX];
1581 a2 = vcpu->regs[VCPU_REGS_RDX];
1582 a3 = vcpu->regs[VCPU_REGS_RSI];
1583
1584 if (!is_long_mode(vcpu)) {
1585 nr &= 0xFFFFFFFF;
1586 a0 &= 0xFFFFFFFF;
1587 a1 &= 0xFFFFFFFF;
1588 a2 &= 0xFFFFFFFF;
1589 a3 &= 0xFFFFFFFF;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001590 }
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001591
Avi Kivity270fd9b2007-02-19 14:37:47 +02001592 switch (nr) {
1593 default:
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001594 ret = -KVM_ENOSYS;
1595 break;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001596 }
1597 vcpu->regs[VCPU_REGS_RAX] = ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001598 kvm_x86_ops->decache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001599 return 0;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001600}
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001601EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
1602
1603int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
1604{
1605 char instruction[3];
1606 int ret = 0;
1607
1608 mutex_lock(&vcpu->kvm->lock);
1609
1610 /*
1611 * Blow out the MMU to ensure that no other VCPU has an active mapping
1612 * to ensure that the updated hypercall appears atomically across all
1613 * VCPUs.
1614 */
1615 kvm_mmu_zap_all(vcpu->kvm);
1616
1617 kvm_x86_ops->cache_regs(vcpu);
1618 kvm_x86_ops->patch_hypercall(vcpu, instruction);
1619 if (emulator_write_emulated(vcpu->rip, instruction, 3, vcpu)
1620 != X86EMUL_CONTINUE)
1621 ret = -EFAULT;
1622
1623 mutex_unlock(&vcpu->kvm->lock);
1624
1625 return ret;
1626}
Avi Kivity270fd9b2007-02-19 14:37:47 +02001627
Avi Kivity6aa8b732006-12-10 02:21:36 -08001628static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1629{
1630 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1631}
1632
1633void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1634{
1635 struct descriptor_table dt = { limit, base };
1636
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001637 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001638}
1639
1640void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1641{
1642 struct descriptor_table dt = { limit, base };
1643
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001644 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001645}
1646
1647void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1648 unsigned long *rflags)
1649{
1650 lmsw(vcpu, msw);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001651 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001652}
1653
1654unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1655{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001656 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001657 switch (cr) {
1658 case 0:
1659 return vcpu->cr0;
1660 case 2:
1661 return vcpu->cr2;
1662 case 3:
1663 return vcpu->cr3;
1664 case 4:
1665 return vcpu->cr4;
1666 default:
1667 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1668 return 0;
1669 }
1670}
1671
1672void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1673 unsigned long *rflags)
1674{
1675 switch (cr) {
1676 case 0:
1677 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001678 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001679 break;
1680 case 2:
1681 vcpu->cr2 = val;
1682 break;
1683 case 3:
1684 set_cr3(vcpu, val);
1685 break;
1686 case 4:
1687 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1688 break;
1689 default:
1690 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1691 }
1692}
1693
Avi Kivity3bab1f52006-12-29 16:49:48 -08001694int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1695{
1696 u64 data;
1697
1698 switch (msr) {
1699 case 0xc0010010: /* SYSCFG */
1700 case 0xc0010015: /* HWCR */
1701 case MSR_IA32_PLATFORM_ID:
1702 case MSR_IA32_P5_MC_ADDR:
1703 case MSR_IA32_P5_MC_TYPE:
1704 case MSR_IA32_MC0_CTL:
1705 case MSR_IA32_MCG_STATUS:
1706 case MSR_IA32_MCG_CAP:
1707 case MSR_IA32_MC0_MISC:
1708 case MSR_IA32_MC0_MISC+4:
1709 case MSR_IA32_MC0_MISC+8:
1710 case MSR_IA32_MC0_MISC+12:
1711 case MSR_IA32_MC0_MISC+16:
1712 case MSR_IA32_UCODE_REV:
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001713 case MSR_IA32_PERF_STATUS:
Matthew Gregan2dc70942007-05-06 10:59:46 +03001714 case MSR_IA32_EBL_CR_POWERON:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001715 /* MTRR registers */
1716 case 0xfe:
1717 case 0x200 ... 0x2ff:
1718 data = 0;
1719 break;
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001720 case 0xcd: /* fsb frequency */
1721 data = 3;
1722 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001723 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001724 data = kvm_get_apic_base(vcpu);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001725 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001726 case MSR_IA32_MISC_ENABLE:
1727 data = vcpu->ia32_misc_enable_msr;
1728 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001729#ifdef CONFIG_X86_64
1730 case MSR_EFER:
1731 data = vcpu->shadow_efer;
1732 break;
1733#endif
1734 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001735 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001736 return 1;
1737 }
1738 *pdata = data;
1739 return 0;
1740}
1741EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1742
Avi Kivity6aa8b732006-12-10 02:21:36 -08001743/*
1744 * Reads an msr value (of 'msr_index') into 'pdata'.
1745 * Returns 0 on success, non-0 otherwise.
1746 * Assumes vcpu_load() was already called.
1747 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001748int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001749{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001750 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001751}
1752
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001753#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001754
Avi Kivity3bab1f52006-12-29 16:49:48 -08001755static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001756{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001757 if (efer & EFER_RESERVED_BITS) {
1758 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1759 efer);
1760 inject_gp(vcpu);
1761 return;
1762 }
1763
1764 if (is_paging(vcpu)
1765 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1766 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1767 inject_gp(vcpu);
1768 return;
1769 }
1770
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001771 kvm_x86_ops->set_efer(vcpu, efer);
Avi Kivity7725f0b2006-12-13 00:34:01 -08001772
Avi Kivity6aa8b732006-12-10 02:21:36 -08001773 efer &= ~EFER_LMA;
1774 efer |= vcpu->shadow_efer & EFER_LMA;
1775
1776 vcpu->shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001777}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001778
1779#endif
1780
Avi Kivity3bab1f52006-12-29 16:49:48 -08001781int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1782{
1783 switch (msr) {
1784#ifdef CONFIG_X86_64
1785 case MSR_EFER:
1786 set_efer(vcpu, data);
1787 break;
1788#endif
1789 case MSR_IA32_MC0_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001790 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
Avi Kivity3bab1f52006-12-29 16:49:48 -08001791 __FUNCTION__, data);
1792 break;
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001793 case MSR_IA32_MCG_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001794 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001795 __FUNCTION__, data);
1796 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001797 case MSR_IA32_UCODE_REV:
1798 case MSR_IA32_UCODE_WRITE:
1799 case 0x200 ... 0x2ff: /* MTRRs */
1800 break;
1801 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001802 kvm_set_apic_base(vcpu, data);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001803 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001804 case MSR_IA32_MISC_ENABLE:
1805 vcpu->ia32_misc_enable_msr = data;
1806 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001807 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001808 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001809 return 1;
1810 }
1811 return 0;
1812}
1813EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1814
Avi Kivity6aa8b732006-12-10 02:21:36 -08001815/*
1816 * Writes msr value into into the appropriate "register".
1817 * Returns 0 on success, non-0 otherwise.
1818 * Assumes vcpu_load() was already called.
1819 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001820int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001821{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001822 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001823}
1824
1825void kvm_resched(struct kvm_vcpu *vcpu)
1826{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001827 if (!need_resched())
1828 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001829 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001830}
1831EXPORT_SYMBOL_GPL(kvm_resched);
1832
Avi Kivity06465c52007-02-28 20:46:53 +02001833void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1834{
1835 int i;
1836 u32 function;
1837 struct kvm_cpuid_entry *e, *best;
1838
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001839 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001840 function = vcpu->regs[VCPU_REGS_RAX];
1841 vcpu->regs[VCPU_REGS_RAX] = 0;
1842 vcpu->regs[VCPU_REGS_RBX] = 0;
1843 vcpu->regs[VCPU_REGS_RCX] = 0;
1844 vcpu->regs[VCPU_REGS_RDX] = 0;
1845 best = NULL;
1846 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1847 e = &vcpu->cpuid_entries[i];
1848 if (e->function == function) {
1849 best = e;
1850 break;
1851 }
1852 /*
1853 * Both basic or both extended?
1854 */
1855 if (((e->function ^ function) & 0x80000000) == 0)
1856 if (!best || e->function > best->function)
1857 best = e;
1858 }
1859 if (best) {
1860 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1861 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1862 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1863 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1864 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001865 kvm_x86_ops->decache_regs(vcpu);
1866 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001867}
1868EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1869
Avi Kivity039576c2007-03-20 12:46:50 +02001870static int pio_copy_data(struct kvm_vcpu *vcpu)
Avi Kivity46fc1472007-02-22 19:39:30 +02001871{
Avi Kivity039576c2007-03-20 12:46:50 +02001872 void *p = vcpu->pio_data;
1873 void *q;
1874 unsigned bytes;
1875 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1876
Avi Kivity039576c2007-03-20 12:46:50 +02001877 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1878 PAGE_KERNEL);
1879 if (!q) {
Avi Kivity039576c2007-03-20 12:46:50 +02001880 free_pio_guest_pages(vcpu);
1881 return -ENOMEM;
1882 }
1883 q += vcpu->pio.guest_page_offset;
1884 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1885 if (vcpu->pio.in)
1886 memcpy(q, p, bytes);
1887 else
1888 memcpy(p, q, bytes);
1889 q -= vcpu->pio.guest_page_offset;
1890 vunmap(q);
Avi Kivity039576c2007-03-20 12:46:50 +02001891 free_pio_guest_pages(vcpu);
1892 return 0;
1893}
1894
1895static int complete_pio(struct kvm_vcpu *vcpu)
1896{
1897 struct kvm_pio_request *io = &vcpu->pio;
Avi Kivity46fc1472007-02-22 19:39:30 +02001898 long delta;
Avi Kivity039576c2007-03-20 12:46:50 +02001899 int r;
Avi Kivity46fc1472007-02-22 19:39:30 +02001900
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001901 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001902
1903 if (!io->string) {
Avi Kivity039576c2007-03-20 12:46:50 +02001904 if (io->in)
1905 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
Avi Kivity46fc1472007-02-22 19:39:30 +02001906 io->size);
1907 } else {
Avi Kivity039576c2007-03-20 12:46:50 +02001908 if (io->in) {
1909 r = pio_copy_data(vcpu);
1910 if (r) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001911 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02001912 return r;
1913 }
1914 }
1915
Avi Kivity46fc1472007-02-22 19:39:30 +02001916 delta = 1;
1917 if (io->rep) {
Avi Kivity039576c2007-03-20 12:46:50 +02001918 delta *= io->cur_count;
Avi Kivity46fc1472007-02-22 19:39:30 +02001919 /*
1920 * The size of the register should really depend on
1921 * current address size.
1922 */
1923 vcpu->regs[VCPU_REGS_RCX] -= delta;
1924 }
Avi Kivity039576c2007-03-20 12:46:50 +02001925 if (io->down)
Avi Kivity46fc1472007-02-22 19:39:30 +02001926 delta = -delta;
1927 delta *= io->size;
Avi Kivity039576c2007-03-20 12:46:50 +02001928 if (io->in)
Avi Kivity46fc1472007-02-22 19:39:30 +02001929 vcpu->regs[VCPU_REGS_RDI] += delta;
1930 else
1931 vcpu->regs[VCPU_REGS_RSI] += delta;
1932 }
1933
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001934 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001935
Avi Kivity039576c2007-03-20 12:46:50 +02001936 io->count -= io->cur_count;
1937 io->cur_count = 0;
1938
Avi Kivity039576c2007-03-20 12:46:50 +02001939 return 0;
Avi Kivity46fc1472007-02-22 19:39:30 +02001940}
1941
Eddie Dong65619eb2007-07-17 11:52:33 +03001942static void kernel_pio(struct kvm_io_device *pio_dev,
1943 struct kvm_vcpu *vcpu,
1944 void *pd)
Eddie Dong74906342007-06-19 18:05:03 +03001945{
1946 /* TODO: String I/O for in kernel device */
1947
Eddie Dong9cf98822007-07-22 10:36:31 +03001948 mutex_lock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001949 if (vcpu->pio.in)
1950 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1951 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001952 pd);
Eddie Dong74906342007-06-19 18:05:03 +03001953 else
1954 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1955 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001956 pd);
Eddie Dong9cf98822007-07-22 10:36:31 +03001957 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001958}
1959
1960static void pio_string_write(struct kvm_io_device *pio_dev,
1961 struct kvm_vcpu *vcpu)
1962{
1963 struct kvm_pio_request *io = &vcpu->pio;
1964 void *pd = vcpu->pio_data;
1965 int i;
1966
Eddie Dong9cf98822007-07-22 10:36:31 +03001967 mutex_lock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001968 for (i = 0; i < io->cur_count; i++) {
1969 kvm_iodevice_write(pio_dev, io->port,
1970 io->size,
1971 pd);
1972 pd += io->size;
1973 }
Eddie Dong9cf98822007-07-22 10:36:31 +03001974 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001975}
1976
Mike Dayd77c26f2007-10-08 09:02:08 -04001977int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
Laurent Vivier3090dd72007-08-05 10:43:32 +03001978 int size, unsigned port)
1979{
1980 struct kvm_io_device *pio_dev;
1981
1982 vcpu->run->exit_reason = KVM_EXIT_IO;
1983 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1984 vcpu->run->io.size = vcpu->pio.size = size;
1985 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1986 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1;
1987 vcpu->run->io.port = vcpu->pio.port = port;
1988 vcpu->pio.in = in;
1989 vcpu->pio.string = 0;
1990 vcpu->pio.down = 0;
1991 vcpu->pio.guest_page_offset = 0;
1992 vcpu->pio.rep = 0;
1993
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001994 kvm_x86_ops->cache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03001995 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001996 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03001997
Avi Kivity0967b7b2007-09-15 17:34:36 +03001998 kvm_x86_ops->skip_emulated_instruction(vcpu);
1999
Laurent Vivier3090dd72007-08-05 10:43:32 +03002000 pio_dev = vcpu_find_pio_dev(vcpu, port);
2001 if (pio_dev) {
2002 kernel_pio(pio_dev, vcpu, vcpu->pio_data);
2003 complete_pio(vcpu);
2004 return 1;
2005 }
2006 return 0;
2007}
2008EXPORT_SYMBOL_GPL(kvm_emulate_pio);
2009
2010int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2011 int size, unsigned long count, int down,
Avi Kivity039576c2007-03-20 12:46:50 +02002012 gva_t address, int rep, unsigned port)
2013{
2014 unsigned now, in_page;
Eddie Dong65619eb2007-07-17 11:52:33 +03002015 int i, ret = 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002016 int nr_pages = 1;
2017 struct page *page;
Eddie Dong74906342007-06-19 18:05:03 +03002018 struct kvm_io_device *pio_dev;
Avi Kivity039576c2007-03-20 12:46:50 +02002019
2020 vcpu->run->exit_reason = KVM_EXIT_IO;
2021 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Laurent Vivier3090dd72007-08-05 10:43:32 +03002022 vcpu->run->io.size = vcpu->pio.size = size;
Avi Kivity039576c2007-03-20 12:46:50 +02002023 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Laurent Vivier3090dd72007-08-05 10:43:32 +03002024 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count;
2025 vcpu->run->io.port = vcpu->pio.port = port;
Avi Kivity039576c2007-03-20 12:46:50 +02002026 vcpu->pio.in = in;
Laurent Vivier3090dd72007-08-05 10:43:32 +03002027 vcpu->pio.string = 1;
Avi Kivity039576c2007-03-20 12:46:50 +02002028 vcpu->pio.down = down;
2029 vcpu->pio.guest_page_offset = offset_in_page(address);
2030 vcpu->pio.rep = rep;
2031
Avi Kivity039576c2007-03-20 12:46:50 +02002032 if (!count) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002033 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02002034 return 1;
2035 }
2036
Avi Kivity039576c2007-03-20 12:46:50 +02002037 if (!down)
2038 in_page = PAGE_SIZE - offset_in_page(address);
2039 else
2040 in_page = offset_in_page(address) + size;
2041 now = min(count, (unsigned long)in_page / size);
2042 if (!now) {
2043 /*
2044 * String I/O straddles page boundary. Pin two guest pages
2045 * so that we satisfy atomicity constraints. Do just one
2046 * transaction to avoid complexity.
2047 */
2048 nr_pages = 2;
2049 now = 1;
2050 }
2051 if (down) {
2052 /*
2053 * String I/O in reverse. Yuck. Kill the guest, fix later.
2054 */
Rusty Russellf0242472007-08-01 10:48:02 +10002055 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivity039576c2007-03-20 12:46:50 +02002056 inject_gp(vcpu);
2057 return 1;
2058 }
2059 vcpu->run->io.count = now;
2060 vcpu->pio.cur_count = now;
2061
Avi Kivity0967b7b2007-09-15 17:34:36 +03002062 if (vcpu->pio.cur_count == vcpu->pio.count)
2063 kvm_x86_ops->skip_emulated_instruction(vcpu);
2064
Avi Kivity039576c2007-03-20 12:46:50 +02002065 for (i = 0; i < nr_pages; ++i) {
Shaohua Li11ec2802007-07-23 14:51:37 +08002066 mutex_lock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02002067 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
2068 if (page)
2069 get_page(page);
2070 vcpu->pio.guest_pages[i] = page;
Shaohua Li11ec2802007-07-23 14:51:37 +08002071 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02002072 if (!page) {
2073 inject_gp(vcpu);
2074 free_pio_guest_pages(vcpu);
2075 return 1;
2076 }
2077 }
2078
Laurent Vivier3090dd72007-08-05 10:43:32 +03002079 pio_dev = vcpu_find_pio_dev(vcpu, port);
Eddie Dong65619eb2007-07-17 11:52:33 +03002080 if (!vcpu->pio.in) {
2081 /* string PIO write */
2082 ret = pio_copy_data(vcpu);
2083 if (ret >= 0 && pio_dev) {
2084 pio_string_write(pio_dev, vcpu);
2085 complete_pio(vcpu);
2086 if (vcpu->pio.count == 0)
2087 ret = 1;
2088 }
2089 } else if (pio_dev)
Rusty Russellf0242472007-08-01 10:48:02 +10002090 pr_unimpl(vcpu, "no string pio read support yet, "
Eddie Dong65619eb2007-07-17 11:52:33 +03002091 "port %x size %d count %ld\n",
2092 port, size, count);
2093
2094 return ret;
Avi Kivity039576c2007-03-20 12:46:50 +02002095}
Laurent Vivier3090dd72007-08-05 10:43:32 +03002096EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
Avi Kivity039576c2007-03-20 12:46:50 +02002097
Avi Kivity04d2cc72007-09-10 18:10:54 +03002098/*
2099 * Check if userspace requested an interrupt window, and that the
2100 * interrupt window is open.
2101 *
2102 * No need to exit to userspace if we already have an interrupt queued.
2103 */
2104static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2105 struct kvm_run *kvm_run)
2106{
2107 return (!vcpu->irq_summary &&
2108 kvm_run->request_interrupt_window &&
2109 vcpu->interrupt_window_open &&
2110 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
2111}
2112
2113static void post_kvm_run_save(struct kvm_vcpu *vcpu,
2114 struct kvm_run *kvm_run)
2115{
2116 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
2117 kvm_run->cr8 = get_cr8(vcpu);
2118 kvm_run->apic_base = kvm_get_apic_base(vcpu);
2119 if (irqchip_in_kernel(vcpu->kvm))
2120 kvm_run->ready_for_interrupt_injection = 1;
2121 else
2122 kvm_run->ready_for_interrupt_injection =
2123 (vcpu->interrupt_window_open &&
2124 vcpu->irq_summary == 0);
2125}
2126
2127static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2128{
2129 int r;
2130
2131 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
Mike Dayd77c26f2007-10-08 09:02:08 -04002132 pr_debug("vcpu %d received sipi with vector # %x\n",
Avi Kivity04d2cc72007-09-10 18:10:54 +03002133 vcpu->vcpu_id, vcpu->sipi_vector);
2134 kvm_lapic_reset(vcpu);
2135 kvm_x86_ops->vcpu_reset(vcpu);
2136 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
2137 }
2138
2139preempted:
2140 if (vcpu->guest_debug.enabled)
2141 kvm_x86_ops->guest_debug_pre(vcpu);
2142
2143again:
2144 r = kvm_mmu_reload(vcpu);
2145 if (unlikely(r))
2146 goto out;
2147
2148 preempt_disable();
2149
2150 kvm_x86_ops->prepare_guest_switch(vcpu);
2151 kvm_load_guest_fpu(vcpu);
2152
2153 local_irq_disable();
2154
2155 if (signal_pending(current)) {
2156 local_irq_enable();
2157 preempt_enable();
2158 r = -EINTR;
2159 kvm_run->exit_reason = KVM_EXIT_INTR;
2160 ++vcpu->stat.signal_exits;
2161 goto out;
2162 }
2163
2164 if (irqchip_in_kernel(vcpu->kvm))
2165 kvm_x86_ops->inject_pending_irq(vcpu);
2166 else if (!vcpu->mmio_read_completed)
2167 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2168
2169 vcpu->guest_mode = 1;
Laurent Vivierd172fcd2007-10-15 17:00:19 +02002170 kvm_guest_enter();
Avi Kivity04d2cc72007-09-10 18:10:54 +03002171
2172 if (vcpu->requests)
2173 if (test_and_clear_bit(KVM_TLB_FLUSH, &vcpu->requests))
2174 kvm_x86_ops->tlb_flush(vcpu);
2175
2176 kvm_x86_ops->run(vcpu, kvm_run);
2177
2178 vcpu->guest_mode = 0;
2179 local_irq_enable();
2180
2181 ++vcpu->stat.exits;
2182
Laurent Vivier0552f732007-10-18 15:19:01 +02002183 /*
2184 * We must have an instruction between local_irq_enable() and
2185 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2186 * the interrupt shadow. The stat.exits increment will do nicely.
2187 * But we need to prevent reordering, hence this barrier():
2188 */
2189 barrier();
2190
2191 kvm_guest_exit();
2192
Avi Kivity04d2cc72007-09-10 18:10:54 +03002193 preempt_enable();
2194
2195 /*
2196 * Profile KVM exit RIPs:
2197 */
2198 if (unlikely(prof_on == KVM_PROFILING)) {
2199 kvm_x86_ops->cache_regs(vcpu);
2200 profile_hit(KVM_PROFILING, (void *)vcpu->rip);
2201 }
2202
2203 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2204
2205 if (r > 0) {
2206 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2207 r = -EINTR;
2208 kvm_run->exit_reason = KVM_EXIT_INTR;
2209 ++vcpu->stat.request_irq_exits;
2210 goto out;
2211 }
2212 if (!need_resched()) {
2213 ++vcpu->stat.light_exits;
2214 goto again;
2215 }
2216 }
2217
2218out:
2219 if (r > 0) {
2220 kvm_resched(vcpu);
2221 goto preempted;
2222 }
2223
2224 post_kvm_run_save(vcpu, kvm_run);
2225
2226 return r;
2227}
2228
2229
Avi Kivitybccf2152007-02-21 18:04:26 +02002230static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002231{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002232 int r;
Avi Kivity1961d272007-03-05 19:46:05 +02002233 sigset_t sigsaved;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002234
Avi Kivitybccf2152007-02-21 18:04:26 +02002235 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002236
He, Qingc5ec1532007-09-03 17:07:41 +03002237 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
2238 kvm_vcpu_block(vcpu);
2239 vcpu_put(vcpu);
2240 return -EAGAIN;
2241 }
2242
Avi Kivity1961d272007-03-05 19:46:05 +02002243 if (vcpu->sigset_active)
2244 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2245
Dor Laor54810342007-02-12 00:54:39 -08002246 /* re-sync apic's tpr */
He, Qing5cd4f6f2007-08-30 17:04:26 +08002247 if (!irqchip_in_kernel(vcpu->kvm))
2248 set_cr8(vcpu, kvm_run->cr8);
Dor Laor54810342007-02-12 00:54:39 -08002249
Avi Kivity02c83202007-04-29 15:02:17 +03002250 if (vcpu->pio.cur_count) {
2251 r = complete_pio(vcpu);
2252 if (r)
2253 goto out;
2254 }
2255
2256 if (vcpu->mmio_needed) {
2257 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
2258 vcpu->mmio_read_completed = 1;
2259 vcpu->mmio_needed = 0;
2260 r = emulate_instruction(vcpu, kvm_run,
Laurent Vivier34273182007-09-18 11:27:37 +02002261 vcpu->mmio_fault_cr2, 0, 1);
Avi Kivity02c83202007-04-29 15:02:17 +03002262 if (r == EMULATE_DO_MMIO) {
2263 /*
2264 * Read-modify-write. Back to userspace.
2265 */
Avi Kivity02c83202007-04-29 15:02:17 +03002266 r = 0;
2267 goto out;
Avi Kivity46fc1472007-02-22 19:39:30 +02002268 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002269 }
2270
Avi Kivity8eb7d332007-03-04 14:17:08 +02002271 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002272 kvm_x86_ops->cache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002273 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002274 kvm_x86_ops->decache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002275 }
2276
Avi Kivity04d2cc72007-09-10 18:10:54 +03002277 r = __vcpu_run(vcpu, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002278
Avi Kivity039576c2007-03-20 12:46:50 +02002279out:
Avi Kivity1961d272007-03-05 19:46:05 +02002280 if (vcpu->sigset_active)
2281 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2282
Avi Kivity6aa8b732006-12-10 02:21:36 -08002283 vcpu_put(vcpu);
2284 return r;
2285}
2286
Avi Kivitybccf2152007-02-21 18:04:26 +02002287static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
2288 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002289{
Avi Kivitybccf2152007-02-21 18:04:26 +02002290 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002291
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002292 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002293
2294 regs->rax = vcpu->regs[VCPU_REGS_RAX];
2295 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
2296 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
2297 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
2298 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
2299 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
2300 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
2301 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002302#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002303 regs->r8 = vcpu->regs[VCPU_REGS_R8];
2304 regs->r9 = vcpu->regs[VCPU_REGS_R9];
2305 regs->r10 = vcpu->regs[VCPU_REGS_R10];
2306 regs->r11 = vcpu->regs[VCPU_REGS_R11];
2307 regs->r12 = vcpu->regs[VCPU_REGS_R12];
2308 regs->r13 = vcpu->regs[VCPU_REGS_R13];
2309 regs->r14 = vcpu->regs[VCPU_REGS_R14];
2310 regs->r15 = vcpu->regs[VCPU_REGS_R15];
2311#endif
2312
2313 regs->rip = vcpu->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002314 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002315
2316 /*
2317 * Don't leak debug flags in case they were set for guest debugging
2318 */
2319 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2320 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2321
2322 vcpu_put(vcpu);
2323
2324 return 0;
2325}
2326
Avi Kivitybccf2152007-02-21 18:04:26 +02002327static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
2328 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002329{
Avi Kivitybccf2152007-02-21 18:04:26 +02002330 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002331
2332 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
2333 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
2334 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
2335 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
2336 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
2337 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
2338 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
2339 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002340#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002341 vcpu->regs[VCPU_REGS_R8] = regs->r8;
2342 vcpu->regs[VCPU_REGS_R9] = regs->r9;
2343 vcpu->regs[VCPU_REGS_R10] = regs->r10;
2344 vcpu->regs[VCPU_REGS_R11] = regs->r11;
2345 vcpu->regs[VCPU_REGS_R12] = regs->r12;
2346 vcpu->regs[VCPU_REGS_R13] = regs->r13;
2347 vcpu->regs[VCPU_REGS_R14] = regs->r14;
2348 vcpu->regs[VCPU_REGS_R15] = regs->r15;
2349#endif
2350
2351 vcpu->rip = regs->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002352 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002353
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002354 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002355
2356 vcpu_put(vcpu);
2357
2358 return 0;
2359}
2360
2361static void get_segment(struct kvm_vcpu *vcpu,
2362 struct kvm_segment *var, int seg)
2363{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002364 return kvm_x86_ops->get_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002365}
2366
Avi Kivitybccf2152007-02-21 18:04:26 +02002367static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2368 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002369{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002370 struct descriptor_table dt;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002371 int pending_vec;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002372
Avi Kivitybccf2152007-02-21 18:04:26 +02002373 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002374
2375 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2376 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2377 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2378 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2379 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2380 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2381
2382 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2383 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2384
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002385 kvm_x86_ops->get_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002386 sregs->idt.limit = dt.limit;
2387 sregs->idt.base = dt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002388 kvm_x86_ops->get_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002389 sregs->gdt.limit = dt.limit;
2390 sregs->gdt.base = dt.base;
2391
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002392 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002393 sregs->cr0 = vcpu->cr0;
2394 sregs->cr2 = vcpu->cr2;
2395 sregs->cr3 = vcpu->cr3;
2396 sregs->cr4 = vcpu->cr4;
Eddie Dong7017fc32007-07-18 11:34:57 +03002397 sregs->cr8 = get_cr8(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002398 sregs->efer = vcpu->shadow_efer;
Eddie Dong7017fc32007-07-18 11:34:57 +03002399 sregs->apic_base = kvm_get_apic_base(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002400
Eddie Dong2a8067f2007-08-06 16:29:07 +03002401 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc52fb352007-08-02 14:03:07 +03002402 memset(sregs->interrupt_bitmap, 0,
2403 sizeof sregs->interrupt_bitmap);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002404 pending_vec = kvm_x86_ops->get_irq(vcpu);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002405 if (pending_vec >= 0)
Mike Dayd77c26f2007-10-08 09:02:08 -04002406 set_bit(pending_vec,
2407 (unsigned long *)sregs->interrupt_bitmap);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002408 } else
He, Qingc52fb352007-08-02 14:03:07 +03002409 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2410 sizeof sregs->interrupt_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002411
2412 vcpu_put(vcpu);
2413
2414 return 0;
2415}
2416
2417static void set_segment(struct kvm_vcpu *vcpu,
2418 struct kvm_segment *var, int seg)
2419{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002420 return kvm_x86_ops->set_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002421}
2422
Avi Kivitybccf2152007-02-21 18:04:26 +02002423static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2424 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002425{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002426 int mmu_reset_needed = 0;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002427 int i, pending_vec, max_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002428 struct descriptor_table dt;
2429
Avi Kivitybccf2152007-02-21 18:04:26 +02002430 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002431
Avi Kivity6aa8b732006-12-10 02:21:36 -08002432 dt.limit = sregs->idt.limit;
2433 dt.base = sregs->idt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002434 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002435 dt.limit = sregs->gdt.limit;
2436 dt.base = sregs->gdt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002437 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002438
2439 vcpu->cr2 = sregs->cr2;
2440 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2441 vcpu->cr3 = sregs->cr3;
2442
Eddie Dong7017fc32007-07-18 11:34:57 +03002443 set_cr8(vcpu, sregs->cr8);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002444
2445 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002446#ifdef CONFIG_X86_64
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002447 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002448#endif
Eddie Dong7017fc32007-07-18 11:34:57 +03002449 kvm_set_apic_base(vcpu, sregs->apic_base);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002450
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002451 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity399badf2007-01-05 16:36:38 -08002452
Avi Kivity6aa8b732006-12-10 02:21:36 -08002453 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
Rusty Russell81f50e32007-09-06 01:20:38 +10002454 vcpu->cr0 = sregs->cr0;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002455 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002456
2457 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002458 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Avi Kivity1b0973b2007-01-05 16:36:41 -08002459 if (!is_long_mode(vcpu) && is_pae(vcpu))
2460 load_pdptrs(vcpu, vcpu->cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002461
2462 if (mmu_reset_needed)
2463 kvm_mmu_reset_context(vcpu);
2464
He, Qingc52fb352007-08-02 14:03:07 +03002465 if (!irqchip_in_kernel(vcpu->kvm)) {
2466 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2467 sizeof vcpu->irq_pending);
2468 vcpu->irq_summary = 0;
2469 for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
2470 if (vcpu->irq_pending[i])
2471 __set_bit(i, &vcpu->irq_summary);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002472 } else {
2473 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
2474 pending_vec = find_first_bit(
2475 (const unsigned long *)sregs->interrupt_bitmap,
2476 max_bits);
2477 /* Only pending external irq is handled here */
2478 if (pending_vec < max_bits) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002479 kvm_x86_ops->set_irq(vcpu, pending_vec);
Mike Dayd77c26f2007-10-08 09:02:08 -04002480 pr_debug("Set back pending irq %d\n",
2481 pending_vec);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002482 }
He, Qingc52fb352007-08-02 14:03:07 +03002483 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002484
Avi Kivity024aa1c2007-03-21 13:44:58 +02002485 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2486 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2487 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2488 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2489 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2490 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2491
2492 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2493 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2494
Avi Kivity6aa8b732006-12-10 02:21:36 -08002495 vcpu_put(vcpu);
2496
2497 return 0;
2498}
2499
Rusty Russell1747fb72007-09-06 01:21:32 +10002500void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2501{
2502 struct kvm_segment cs;
2503
2504 get_segment(vcpu, &cs, VCPU_SREG_CS);
2505 *db = cs.db;
2506 *l = cs.l;
2507}
2508EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2509
Avi Kivity6aa8b732006-12-10 02:21:36 -08002510/*
2511 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2512 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
Michael Riepebf591b22006-12-22 01:05:36 -08002513 *
2514 * This list is modified at module load time to reflect the
2515 * capabilities of the host cpu.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002516 */
2517static u32 msrs_to_save[] = {
2518 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
2519 MSR_K6_STAR,
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002520#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002521 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
2522#endif
2523 MSR_IA32_TIME_STAMP_COUNTER,
2524};
2525
Michael Riepebf591b22006-12-22 01:05:36 -08002526static unsigned num_msrs_to_save;
2527
Avi Kivity6f00e682007-01-26 00:56:40 -08002528static u32 emulated_msrs[] = {
2529 MSR_IA32_MISC_ENABLE,
2530};
2531
Michael Riepebf591b22006-12-22 01:05:36 -08002532static __init void kvm_init_msr_list(void)
2533{
2534 u32 dummy[2];
2535 unsigned i, j;
2536
2537 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2538 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2539 continue;
2540 if (j < i)
2541 msrs_to_save[j] = msrs_to_save[i];
2542 j++;
2543 }
2544 num_msrs_to_save = j;
2545}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002546
2547/*
2548 * Adapt set_msr() to msr_io()'s calling convention
2549 */
2550static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2551{
Avi Kivity35f3f282007-07-17 14:20:30 +03002552 return kvm_set_msr(vcpu, index, *data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002553}
2554
2555/*
2556 * Read or write a bunch of msrs. All parameters are kernel addresses.
2557 *
2558 * @return number of msrs set successfully.
2559 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002560static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002561 struct kvm_msr_entry *entries,
2562 int (*do_msr)(struct kvm_vcpu *vcpu,
2563 unsigned index, u64 *data))
2564{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002565 int i;
2566
Avi Kivitybccf2152007-02-21 18:04:26 +02002567 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002568
2569 for (i = 0; i < msrs->nmsrs; ++i)
2570 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2571 break;
2572
2573 vcpu_put(vcpu);
2574
2575 return i;
2576}
2577
2578/*
2579 * Read or write a bunch of msrs. Parameters are user addresses.
2580 *
2581 * @return number of msrs set successfully.
2582 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002583static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002584 int (*do_msr)(struct kvm_vcpu *vcpu,
2585 unsigned index, u64 *data),
2586 int writeback)
2587{
2588 struct kvm_msrs msrs;
2589 struct kvm_msr_entry *entries;
2590 int r, n;
2591 unsigned size;
2592
2593 r = -EFAULT;
2594 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2595 goto out;
2596
2597 r = -E2BIG;
2598 if (msrs.nmsrs >= MAX_IO_MSRS)
2599 goto out;
2600
2601 r = -ENOMEM;
2602 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
2603 entries = vmalloc(size);
2604 if (!entries)
2605 goto out;
2606
2607 r = -EFAULT;
2608 if (copy_from_user(entries, user_msrs->entries, size))
2609 goto out_free;
2610
Avi Kivitybccf2152007-02-21 18:04:26 +02002611 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002612 if (r < 0)
2613 goto out_free;
2614
2615 r = -EFAULT;
2616 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2617 goto out_free;
2618
2619 r = n;
2620
2621out_free:
2622 vfree(entries);
2623out:
2624 return r;
2625}
2626
2627/*
2628 * Translate a guest virtual address to a guest physical address.
2629 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002630static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2631 struct kvm_translation *tr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002632{
2633 unsigned long vaddr = tr->linear_address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002634 gpa_t gpa;
2635
Avi Kivitybccf2152007-02-21 18:04:26 +02002636 vcpu_load(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +08002637 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002638 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2639 tr->physical_address = gpa;
2640 tr->valid = gpa != UNMAPPED_GVA;
2641 tr->writeable = 1;
2642 tr->usermode = 0;
Shaohua Li11ec2802007-07-23 14:51:37 +08002643 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644 vcpu_put(vcpu);
2645
2646 return 0;
2647}
2648
Avi Kivitybccf2152007-02-21 18:04:26 +02002649static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2650 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002651{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002652 if (irq->irq < 0 || irq->irq >= 256)
2653 return -EINVAL;
Eddie Dong97222cc2007-09-12 10:58:04 +03002654 if (irqchip_in_kernel(vcpu->kvm))
2655 return -ENXIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02002656 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002657
2658 set_bit(irq->irq, vcpu->irq_pending);
2659 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2660
2661 vcpu_put(vcpu);
2662
2663 return 0;
2664}
2665
Avi Kivitybccf2152007-02-21 18:04:26 +02002666static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2667 struct kvm_debug_guest *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002668{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002669 int r;
2670
Avi Kivitybccf2152007-02-21 18:04:26 +02002671 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002672
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002673 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002674
2675 vcpu_put(vcpu);
2676
2677 return r;
2678}
2679
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002680static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2681 unsigned long address,
2682 int *type)
2683{
2684 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2685 unsigned long pgoff;
2686 struct page *page;
2687
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002688 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +02002689 if (pgoff == 0)
2690 page = virt_to_page(vcpu->run);
2691 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2692 page = virt_to_page(vcpu->pio_data);
2693 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002694 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002695 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002696 if (type != NULL)
2697 *type = VM_FAULT_MINOR;
2698
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002699 return page;
2700}
2701
2702static struct vm_operations_struct kvm_vcpu_vm_ops = {
2703 .nopage = kvm_vcpu_nopage,
2704};
2705
2706static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2707{
2708 vma->vm_ops = &kvm_vcpu_vm_ops;
2709 return 0;
2710}
2711
Avi Kivitybccf2152007-02-21 18:04:26 +02002712static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2713{
2714 struct kvm_vcpu *vcpu = filp->private_data;
2715
2716 fput(vcpu->kvm->filp);
2717 return 0;
2718}
2719
2720static struct file_operations kvm_vcpu_fops = {
2721 .release = kvm_vcpu_release,
2722 .unlocked_ioctl = kvm_vcpu_ioctl,
2723 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002724 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02002725};
2726
2727/*
2728 * Allocates an inode for the vcpu.
2729 */
2730static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2731{
2732 int fd, r;
2733 struct inode *inode;
2734 struct file *file;
2735
Avi Kivityd6d28162007-06-28 08:38:16 -04002736 r = anon_inode_getfd(&fd, &inode, &file,
2737 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2738 if (r)
2739 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002740 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +02002741 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02002742}
2743
Avi Kivityc5ea7662007-02-20 18:41:05 +02002744/*
2745 * Creates some virtual cpus. Good luck creating more than one.
2746 */
2747static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2748{
2749 int r;
2750 struct kvm_vcpu *vcpu;
2751
Avi Kivityc5ea7662007-02-20 18:41:05 +02002752 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002753 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002754
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002755 vcpu = kvm_x86_ops->vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002756 if (IS_ERR(vcpu))
2757 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002758
Avi Kivity15ad7142007-07-11 18:17:21 +03002759 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
2760
Rusty Russellb114b082007-07-30 21:13:43 +10002761 /* We do fxsave: this must be aligned. */
2762 BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF);
2763
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002764 vcpu_load(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002765 r = kvm_mmu_setup(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002766 vcpu_put(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002767 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002768 goto free_vcpu;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002769
Shaohua Li11ec2802007-07-23 14:51:37 +08002770 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002771 if (kvm->vcpus[n]) {
2772 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +08002773 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002774 goto mmu_unload;
2775 }
2776 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08002777 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002778
2779 /* Now it's all set up, let userspace reach it */
Avi Kivitybccf2152007-02-21 18:04:26 +02002780 r = create_vcpu_fd(vcpu);
2781 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002782 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02002783 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002784
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002785unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08002786 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002787 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +08002788 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002789
2790mmu_unload:
2791 vcpu_load(vcpu);
2792 kvm_mmu_unload(vcpu);
2793 vcpu_put(vcpu);
2794
2795free_vcpu:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002796 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002797 return r;
2798}
2799
Eddie Dong2cc51562007-05-21 07:28:09 +03002800static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
2801{
2802 u64 efer;
2803 int i;
2804 struct kvm_cpuid_entry *e, *entry;
2805
2806 rdmsrl(MSR_EFER, efer);
2807 entry = NULL;
2808 for (i = 0; i < vcpu->cpuid_nent; ++i) {
2809 e = &vcpu->cpuid_entries[i];
2810 if (e->function == 0x80000001) {
2811 entry = e;
2812 break;
2813 }
2814 }
Avi Kivity4c981b42007-07-25 09:22:12 +03002815 if (entry && (entry->edx & (1 << 20)) && !(efer & EFER_NX)) {
Eddie Dong2cc51562007-05-21 07:28:09 +03002816 entry->edx &= ~(1 << 20);
Avi Kivity4c981b42007-07-25 09:22:12 +03002817 printk(KERN_INFO "kvm: guest NX capability removed\n");
Eddie Dong2cc51562007-05-21 07:28:09 +03002818 }
2819}
2820
Avi Kivity06465c52007-02-28 20:46:53 +02002821static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
2822 struct kvm_cpuid *cpuid,
2823 struct kvm_cpuid_entry __user *entries)
2824{
2825 int r;
2826
2827 r = -E2BIG;
2828 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2829 goto out;
2830 r = -EFAULT;
2831 if (copy_from_user(&vcpu->cpuid_entries, entries,
2832 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
2833 goto out;
2834 vcpu->cpuid_nent = cpuid->nent;
Eddie Dong2cc51562007-05-21 07:28:09 +03002835 cpuid_fix_nx_cap(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002836 return 0;
2837
2838out:
2839 return r;
2840}
2841
Avi Kivity1961d272007-03-05 19:46:05 +02002842static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2843{
2844 if (sigset) {
2845 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2846 vcpu->sigset_active = 1;
2847 vcpu->sigset = *sigset;
2848 } else
2849 vcpu->sigset_active = 0;
2850 return 0;
2851}
2852
Avi Kivityb8836732007-04-01 16:34:31 +03002853/*
2854 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2855 * we have asm/x86/processor.h
2856 */
2857struct fxsave {
2858 u16 cwd;
2859 u16 swd;
2860 u16 twd;
2861 u16 fop;
2862 u64 rip;
2863 u64 rdp;
2864 u32 mxcsr;
2865 u32 mxcsr_mask;
2866 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2867#ifdef CONFIG_X86_64
2868 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2869#else
2870 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2871#endif
2872};
2873
2874static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2875{
Rusty Russellb114b082007-07-30 21:13:43 +10002876 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002877
2878 vcpu_load(vcpu);
2879
2880 memcpy(fpu->fpr, fxsave->st_space, 128);
2881 fpu->fcw = fxsave->cwd;
2882 fpu->fsw = fxsave->swd;
2883 fpu->ftwx = fxsave->twd;
2884 fpu->last_opcode = fxsave->fop;
2885 fpu->last_ip = fxsave->rip;
2886 fpu->last_dp = fxsave->rdp;
2887 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2888
2889 vcpu_put(vcpu);
2890
2891 return 0;
2892}
2893
2894static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2895{
Rusty Russellb114b082007-07-30 21:13:43 +10002896 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002897
2898 vcpu_load(vcpu);
2899
2900 memcpy(fxsave->st_space, fpu->fpr, 128);
2901 fxsave->cwd = fpu->fcw;
2902 fxsave->swd = fpu->fsw;
2903 fxsave->twd = fpu->ftwx;
2904 fxsave->fop = fpu->last_opcode;
2905 fxsave->rip = fpu->last_ip;
2906 fxsave->rdp = fpu->last_dp;
2907 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2908
2909 vcpu_put(vcpu);
2910
2911 return 0;
2912}
2913
Eddie Dong96ad2cc2007-09-06 12:22:56 +03002914static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
2915 struct kvm_lapic_state *s)
2916{
2917 vcpu_load(vcpu);
2918 memcpy(s->regs, vcpu->apic->regs, sizeof *s);
2919 vcpu_put(vcpu);
2920
2921 return 0;
2922}
2923
2924static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
2925 struct kvm_lapic_state *s)
2926{
2927 vcpu_load(vcpu);
2928 memcpy(vcpu->apic->regs, s->regs, sizeof *s);
2929 kvm_apic_post_state_restore(vcpu);
2930 vcpu_put(vcpu);
2931
2932 return 0;
2933}
2934
Avi Kivitybccf2152007-02-21 18:04:26 +02002935static long kvm_vcpu_ioctl(struct file *filp,
2936 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002937{
Avi Kivitybccf2152007-02-21 18:04:26 +02002938 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +00002939 void __user *argp = (void __user *)arg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002940 int r = -EINVAL;
2941
2942 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002943 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002944 r = -EINVAL;
2945 if (arg)
2946 goto out;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002947 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002948 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002949 case KVM_GET_REGS: {
2950 struct kvm_regs kvm_regs;
2951
Avi Kivitybccf2152007-02-21 18:04:26 +02002952 memset(&kvm_regs, 0, sizeof kvm_regs);
2953 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002954 if (r)
2955 goto out;
2956 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002957 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002958 goto out;
2959 r = 0;
2960 break;
2961 }
2962 case KVM_SET_REGS: {
2963 struct kvm_regs kvm_regs;
2964
2965 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002966 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002967 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002968 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002969 if (r)
2970 goto out;
2971 r = 0;
2972 break;
2973 }
2974 case KVM_GET_SREGS: {
2975 struct kvm_sregs kvm_sregs;
2976
Avi Kivitybccf2152007-02-21 18:04:26 +02002977 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2978 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002979 if (r)
2980 goto out;
2981 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002982 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002983 goto out;
2984 r = 0;
2985 break;
2986 }
2987 case KVM_SET_SREGS: {
2988 struct kvm_sregs kvm_sregs;
2989
2990 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002991 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002992 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002993 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002994 if (r)
2995 goto out;
2996 r = 0;
2997 break;
2998 }
2999 case KVM_TRANSLATE: {
3000 struct kvm_translation tr;
3001
3002 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00003003 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003004 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02003005 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003006 if (r)
3007 goto out;
3008 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00003009 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003010 goto out;
3011 r = 0;
3012 break;
3013 }
3014 case KVM_INTERRUPT: {
3015 struct kvm_interrupt irq;
3016
3017 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00003018 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003019 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02003020 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003021 if (r)
3022 goto out;
3023 r = 0;
3024 break;
3025 }
3026 case KVM_DEBUG_GUEST: {
3027 struct kvm_debug_guest dbg;
3028
3029 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00003030 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003031 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02003032 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003033 if (r)
3034 goto out;
3035 r = 0;
3036 break;
3037 }
Avi Kivitybccf2152007-02-21 18:04:26 +02003038 case KVM_GET_MSRS:
Avi Kivity35f3f282007-07-17 14:20:30 +03003039 r = msr_io(vcpu, argp, kvm_get_msr, 1);
Avi Kivitybccf2152007-02-21 18:04:26 +02003040 break;
3041 case KVM_SET_MSRS:
3042 r = msr_io(vcpu, argp, do_set_msr, 0);
3043 break;
Avi Kivity06465c52007-02-28 20:46:53 +02003044 case KVM_SET_CPUID: {
3045 struct kvm_cpuid __user *cpuid_arg = argp;
3046 struct kvm_cpuid cpuid;
3047
3048 r = -EFAULT;
3049 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
3050 goto out;
3051 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
3052 if (r)
3053 goto out;
3054 break;
3055 }
Avi Kivity1961d272007-03-05 19:46:05 +02003056 case KVM_SET_SIGNAL_MASK: {
3057 struct kvm_signal_mask __user *sigmask_arg = argp;
3058 struct kvm_signal_mask kvm_sigmask;
3059 sigset_t sigset, *p;
3060
3061 p = NULL;
3062 if (argp) {
3063 r = -EFAULT;
3064 if (copy_from_user(&kvm_sigmask, argp,
3065 sizeof kvm_sigmask))
3066 goto out;
3067 r = -EINVAL;
3068 if (kvm_sigmask.len != sizeof sigset)
3069 goto out;
3070 r = -EFAULT;
3071 if (copy_from_user(&sigset, sigmask_arg->sigset,
3072 sizeof sigset))
3073 goto out;
3074 p = &sigset;
3075 }
3076 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
3077 break;
3078 }
Avi Kivityb8836732007-04-01 16:34:31 +03003079 case KVM_GET_FPU: {
3080 struct kvm_fpu fpu;
3081
3082 memset(&fpu, 0, sizeof fpu);
3083 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
3084 if (r)
3085 goto out;
3086 r = -EFAULT;
3087 if (copy_to_user(argp, &fpu, sizeof fpu))
3088 goto out;
3089 r = 0;
3090 break;
3091 }
3092 case KVM_SET_FPU: {
3093 struct kvm_fpu fpu;
3094
3095 r = -EFAULT;
3096 if (copy_from_user(&fpu, argp, sizeof fpu))
3097 goto out;
3098 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
3099 if (r)
3100 goto out;
3101 r = 0;
3102 break;
3103 }
Eddie Dong96ad2cc2007-09-06 12:22:56 +03003104 case KVM_GET_LAPIC: {
3105 struct kvm_lapic_state lapic;
3106
3107 memset(&lapic, 0, sizeof lapic);
3108 r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic);
3109 if (r)
3110 goto out;
3111 r = -EFAULT;
3112 if (copy_to_user(argp, &lapic, sizeof lapic))
3113 goto out;
3114 r = 0;
3115 break;
3116 }
3117 case KVM_SET_LAPIC: {
3118 struct kvm_lapic_state lapic;
3119
3120 r = -EFAULT;
3121 if (copy_from_user(&lapic, argp, sizeof lapic))
3122 goto out;
3123 r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);;
3124 if (r)
3125 goto out;
3126 r = 0;
3127 break;
3128 }
Avi Kivitybccf2152007-02-21 18:04:26 +02003129 default:
3130 ;
3131 }
3132out:
3133 return r;
3134}
3135
3136static long kvm_vm_ioctl(struct file *filp,
3137 unsigned int ioctl, unsigned long arg)
3138{
3139 struct kvm *kvm = filp->private_data;
3140 void __user *argp = (void __user *)arg;
3141 int r = -EINVAL;
3142
3143 switch (ioctl) {
3144 case KVM_CREATE_VCPU:
3145 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
3146 if (r < 0)
3147 goto out;
3148 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003149 case KVM_SET_MEMORY_REGION: {
3150 struct kvm_memory_region kvm_mem;
Izik Eidus6fc138d2007-10-09 19:20:39 +02003151 struct kvm_userspace_memory_region kvm_userspace_mem;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003152
3153 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00003154 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003155 goto out;
Izik Eidus6fc138d2007-10-09 19:20:39 +02003156 kvm_userspace_mem.slot = kvm_mem.slot;
3157 kvm_userspace_mem.flags = kvm_mem.flags;
3158 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
3159 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
3160 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
3161 if (r)
3162 goto out;
3163 break;
3164 }
3165 case KVM_SET_USER_MEMORY_REGION: {
3166 struct kvm_userspace_memory_region kvm_userspace_mem;
3167
3168 r = -EFAULT;
3169 if (copy_from_user(&kvm_userspace_mem, argp,
3170 sizeof kvm_userspace_mem))
3171 goto out;
3172
3173 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003174 if (r)
3175 goto out;
3176 break;
3177 }
Izik Eidus82ce2c92007-10-02 18:52:55 +02003178 case KVM_SET_NR_MMU_PAGES:
3179 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
3180 if (r)
3181 goto out;
3182 break;
3183 case KVM_GET_NR_MMU_PAGES:
3184 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
3185 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003186 case KVM_GET_DIRTY_LOG: {
3187 struct kvm_dirty_log log;
3188
3189 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00003190 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003191 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02003192 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003193 if (r)
3194 goto out;
3195 break;
3196 }
Avi Kivitye8207542007-03-30 16:54:30 +03003197 case KVM_SET_MEMORY_ALIAS: {
3198 struct kvm_memory_alias alias;
3199
3200 r = -EFAULT;
3201 if (copy_from_user(&alias, argp, sizeof alias))
3202 goto out;
3203 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
3204 if (r)
3205 goto out;
3206 break;
3207 }
Eddie Dong85f455f2007-07-06 12:20:49 +03003208 case KVM_CREATE_IRQCHIP:
3209 r = -ENOMEM;
3210 kvm->vpic = kvm_create_pic(kvm);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03003211 if (kvm->vpic) {
3212 r = kvm_ioapic_init(kvm);
3213 if (r) {
3214 kfree(kvm->vpic);
3215 kvm->vpic = NULL;
3216 goto out;
3217 }
Mike Dayd77c26f2007-10-08 09:02:08 -04003218 } else
Eddie Dong85f455f2007-07-06 12:20:49 +03003219 goto out;
3220 break;
3221 case KVM_IRQ_LINE: {
3222 struct kvm_irq_level irq_event;
3223
3224 r = -EFAULT;
3225 if (copy_from_user(&irq_event, argp, sizeof irq_event))
3226 goto out;
3227 if (irqchip_in_kernel(kvm)) {
Eddie Dong9cf98822007-07-22 10:36:31 +03003228 mutex_lock(&kvm->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +03003229 if (irq_event.irq < 16)
3230 kvm_pic_set_irq(pic_irqchip(kvm),
3231 irq_event.irq,
3232 irq_event.level);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +03003233 kvm_ioapic_set_irq(kvm->vioapic,
3234 irq_event.irq,
3235 irq_event.level);
Eddie Dong9cf98822007-07-22 10:36:31 +03003236 mutex_unlock(&kvm->lock);
Eddie Dong85f455f2007-07-06 12:20:49 +03003237 r = 0;
3238 }
3239 break;
3240 }
He, Qing6ceb9d72007-07-26 11:05:18 +03003241 case KVM_GET_IRQCHIP: {
3242 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3243 struct kvm_irqchip chip;
3244
3245 r = -EFAULT;
3246 if (copy_from_user(&chip, argp, sizeof chip))
3247 goto out;
3248 r = -ENXIO;
3249 if (!irqchip_in_kernel(kvm))
3250 goto out;
3251 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
3252 if (r)
3253 goto out;
3254 r = -EFAULT;
3255 if (copy_to_user(argp, &chip, sizeof chip))
3256 goto out;
3257 r = 0;
3258 break;
3259 }
3260 case KVM_SET_IRQCHIP: {
3261 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
3262 struct kvm_irqchip chip;
3263
3264 r = -EFAULT;
3265 if (copy_from_user(&chip, argp, sizeof chip))
3266 goto out;
3267 r = -ENXIO;
3268 if (!irqchip_in_kernel(kvm))
3269 goto out;
3270 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
3271 if (r)
3272 goto out;
3273 r = 0;
3274 break;
3275 }
Avi Kivityf17abe92007-02-21 19:28:04 +02003276 default:
3277 ;
3278 }
3279out:
3280 return r;
3281}
3282
3283static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
3284 unsigned long address,
3285 int *type)
3286{
3287 struct kvm *kvm = vma->vm_file->private_data;
3288 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02003289 struct page *page;
3290
Avi Kivityf17abe92007-02-21 19:28:04 +02003291 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity954bbbc22007-03-30 14:02:32 +03003292 page = gfn_to_page(kvm, pgoff);
Avi Kivityf17abe92007-02-21 19:28:04 +02003293 if (!page)
3294 return NOPAGE_SIGBUS;
3295 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03003296 if (type != NULL)
3297 *type = VM_FAULT_MINOR;
3298
Avi Kivityf17abe92007-02-21 19:28:04 +02003299 return page;
3300}
3301
3302static struct vm_operations_struct kvm_vm_vm_ops = {
3303 .nopage = kvm_vm_nopage,
3304};
3305
3306static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
3307{
3308 vma->vm_ops = &kvm_vm_vm_ops;
3309 return 0;
3310}
3311
3312static struct file_operations kvm_vm_fops = {
3313 .release = kvm_vm_release,
3314 .unlocked_ioctl = kvm_vm_ioctl,
3315 .compat_ioctl = kvm_vm_ioctl,
3316 .mmap = kvm_vm_mmap,
3317};
3318
3319static int kvm_dev_ioctl_create_vm(void)
3320{
3321 int fd, r;
3322 struct inode *inode;
3323 struct file *file;
3324 struct kvm *kvm;
3325
Avi Kivityf17abe92007-02-21 19:28:04 +02003326 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04003327 if (IS_ERR(kvm))
3328 return PTR_ERR(kvm);
3329 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
3330 if (r) {
3331 kvm_destroy_vm(kvm);
3332 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02003333 }
3334
Avi Kivitybccf2152007-02-21 18:04:26 +02003335 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02003336
Avi Kivityf17abe92007-02-21 19:28:04 +02003337 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02003338}
3339
3340static long kvm_dev_ioctl(struct file *filp,
3341 unsigned int ioctl, unsigned long arg)
3342{
3343 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02003344 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02003345
3346 switch (ioctl) {
3347 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02003348 r = -EINVAL;
3349 if (arg)
3350 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02003351 r = KVM_API_VERSION;
3352 break;
3353 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02003354 r = -EINVAL;
3355 if (arg)
3356 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02003357 r = kvm_dev_ioctl_create_vm();
3358 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003359 case KVM_GET_MSR_INDEX_LIST: {
Al Viro2f366982007-02-09 16:38:35 +00003360 struct kvm_msr_list __user *user_msr_list = argp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003361 struct kvm_msr_list msr_list;
3362 unsigned n;
3363
3364 r = -EFAULT;
3365 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
3366 goto out;
3367 n = msr_list.nmsrs;
Avi Kivity6f00e682007-01-26 00:56:40 -08003368 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003369 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
3370 goto out;
3371 r = -E2BIG;
Michael Riepebf591b22006-12-22 01:05:36 -08003372 if (n < num_msrs_to_save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003373 goto out;
3374 r = -EFAULT;
3375 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
Michael Riepebf591b22006-12-22 01:05:36 -08003376 num_msrs_to_save * sizeof(u32)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003377 goto out;
Avi Kivity6f00e682007-01-26 00:56:40 -08003378 if (copy_to_user(user_msr_list->indices
3379 + num_msrs_to_save * sizeof(u32),
3380 &emulated_msrs,
3381 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
3382 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003383 r = 0;
Avi Kivitycc1d8952007-01-05 16:36:58 -08003384 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003385 }
Eddie Dong85f455f2007-07-06 12:20:49 +03003386 case KVM_CHECK_EXTENSION: {
3387 int ext = (long)argp;
3388
3389 switch (ext) {
3390 case KVM_CAP_IRQCHIP:
Eddie Dongb6958ce2007-07-18 12:15:21 +03003391 case KVM_CAP_HLT:
Izik Eidus82ce2c92007-10-02 18:52:55 +02003392 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Izik Eidus6fc138d2007-10-09 19:20:39 +02003393 case KVM_CAP_USER_MEMORY:
Eddie Dong85f455f2007-07-06 12:20:49 +03003394 r = 1;
3395 break;
3396 default:
3397 r = 0;
3398 break;
3399 }
Avi Kivity5d308f42007-03-01 17:56:20 +02003400 break;
Eddie Dong85f455f2007-07-06 12:20:49 +03003401 }
Avi Kivity07c45a32007-03-07 13:05:38 +02003402 case KVM_GET_VCPU_MMAP_SIZE:
3403 r = -EINVAL;
3404 if (arg)
3405 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02003406 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02003407 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003408 default:
3409 ;
3410 }
3411out:
3412 return r;
3413}
3414
Avi Kivity6aa8b732006-12-10 02:21:36 -08003415static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003416 .unlocked_ioctl = kvm_dev_ioctl,
3417 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003418};
3419
3420static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02003421 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003422 "kvm",
3423 &kvm_chardev_ops,
3424};
3425
Avi Kivity774c47f2007-02-12 00:54:47 -08003426/*
3427 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
3428 * cached on it.
3429 */
3430static void decache_vcpus_on_cpu(int cpu)
3431{
3432 struct kvm *vm;
3433 struct kvm_vcpu *vcpu;
3434 int i;
3435
3436 spin_lock(&kvm_lock);
Shaohua Li11ec2802007-07-23 14:51:37 +08003437 list_for_each_entry(vm, &vm_list, vm_list)
Avi Kivity774c47f2007-02-12 00:54:47 -08003438 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003439 vcpu = vm->vcpus[i];
3440 if (!vcpu)
3441 continue;
Avi Kivity774c47f2007-02-12 00:54:47 -08003442 /*
3443 * If the vcpu is locked, then it is running on some
3444 * other cpu and therefore it is not cached on the
3445 * cpu in question.
3446 *
3447 * If it's not locked, check the last cpu it executed
3448 * on.
3449 */
3450 if (mutex_trylock(&vcpu->mutex)) {
3451 if (vcpu->cpu == cpu) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003452 kvm_x86_ops->vcpu_decache(vcpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08003453 vcpu->cpu = -1;
3454 }
3455 mutex_unlock(&vcpu->mutex);
3456 }
3457 }
3458 spin_unlock(&kvm_lock);
3459}
3460
Avi Kivity1b6c0162007-05-24 13:03:52 +03003461static void hardware_enable(void *junk)
3462{
3463 int cpu = raw_smp_processor_id();
3464
3465 if (cpu_isset(cpu, cpus_hardware_enabled))
3466 return;
3467 cpu_set(cpu, cpus_hardware_enabled);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003468 kvm_x86_ops->hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003469}
3470
3471static void hardware_disable(void *junk)
3472{
3473 int cpu = raw_smp_processor_id();
3474
3475 if (!cpu_isset(cpu, cpus_hardware_enabled))
3476 return;
3477 cpu_clear(cpu, cpus_hardware_enabled);
3478 decache_vcpus_on_cpu(cpu);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003479 kvm_x86_ops->hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003480}
3481
Avi Kivity774c47f2007-02-12 00:54:47 -08003482static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
3483 void *v)
3484{
3485 int cpu = (long)v;
3486
3487 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03003488 case CPU_DYING:
3489 case CPU_DYING_FROZEN:
Avi Kivity6ec8a852007-08-19 15:57:26 +03003490 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3491 cpu);
3492 hardware_disable(NULL);
3493 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08003494 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003495 case CPU_UP_CANCELED_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003496 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3497 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003498 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003499 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02003500 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003501 case CPU_ONLINE_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003502 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
3503 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003504 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003505 break;
3506 }
3507 return NOTIFY_OK;
3508}
3509
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003510static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04003511 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003512{
3513 if (val == SYS_RESTART) {
3514 /*
3515 * Some (well, at least mine) BIOSes hang on reboot if
3516 * in vmx root mode.
3517 */
3518 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
3519 on_each_cpu(hardware_disable, NULL, 0, 1);
3520 }
3521 return NOTIFY_OK;
3522}
3523
3524static struct notifier_block kvm_reboot_notifier = {
3525 .notifier_call = kvm_reboot,
3526 .priority = 0,
3527};
3528
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04003529void kvm_io_bus_init(struct kvm_io_bus *bus)
3530{
3531 memset(bus, 0, sizeof(*bus));
3532}
3533
3534void kvm_io_bus_destroy(struct kvm_io_bus *bus)
3535{
3536 int i;
3537
3538 for (i = 0; i < bus->dev_count; i++) {
3539 struct kvm_io_device *pos = bus->devs[i];
3540
3541 kvm_iodevice_destructor(pos);
3542 }
3543}
3544
3545struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
3546{
3547 int i;
3548
3549 for (i = 0; i < bus->dev_count; i++) {
3550 struct kvm_io_device *pos = bus->devs[i];
3551
3552 if (pos->in_range(pos, addr))
3553 return pos;
3554 }
3555
3556 return NULL;
3557}
3558
3559void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
3560{
3561 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
3562
3563 bus->devs[bus->dev_count++] = dev;
3564}
3565
Avi Kivity774c47f2007-02-12 00:54:47 -08003566static struct notifier_block kvm_cpu_notifier = {
3567 .notifier_call = kvm_cpu_hotplug,
3568 .priority = 20, /* must be > scheduler priority */
3569};
3570
Avi Kivity1165f5f2007-04-19 17:27:43 +03003571static u64 stat_get(void *_offset)
3572{
3573 unsigned offset = (long)_offset;
3574 u64 total = 0;
3575 struct kvm *kvm;
3576 struct kvm_vcpu *vcpu;
3577 int i;
3578
3579 spin_lock(&kvm_lock);
3580 list_for_each_entry(kvm, &vm_list, vm_list)
3581 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003582 vcpu = kvm->vcpus[i];
3583 if (vcpu)
3584 total += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03003585 }
3586 spin_unlock(&kvm_lock);
3587 return total;
3588}
3589
Rusty Russell3dea7ca2007-08-01 10:12:22 +10003590DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n");
Avi Kivity1165f5f2007-04-19 17:27:43 +03003591
Avi Kivity6aa8b732006-12-10 02:21:36 -08003592static __init void kvm_init_debug(void)
3593{
3594 struct kvm_stats_debugfs_item *p;
3595
Al Viro8b6d44c2007-02-09 16:38:40 +00003596 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003597 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03003598 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
3599 (void *)(long)p->offset,
3600 &stat_fops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003601}
3602
3603static void kvm_exit_debug(void)
3604{
3605 struct kvm_stats_debugfs_item *p;
3606
3607 for (p = debugfs_entries; p->name; ++p)
3608 debugfs_remove(p->dentry);
3609 debugfs_remove(debugfs_dir);
3610}
3611
Avi Kivity59ae6c62007-02-12 00:54:48 -08003612static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3613{
Avi Kivity4267c412007-05-24 13:09:41 +03003614 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003615 return 0;
3616}
3617
3618static int kvm_resume(struct sys_device *dev)
3619{
Avi Kivity4267c412007-05-24 13:09:41 +03003620 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003621 return 0;
3622}
3623
3624static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01003625 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08003626 .suspend = kvm_suspend,
3627 .resume = kvm_resume,
3628};
3629
3630static struct sys_device kvm_sysdev = {
3631 .id = 0,
3632 .cls = &kvm_sysdev_class,
3633};
3634
Avi Kivity6aa8b732006-12-10 02:21:36 -08003635hpa_t bad_page_address;
3636
Avi Kivity15ad7142007-07-11 18:17:21 +03003637static inline
3638struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
3639{
3640 return container_of(pn, struct kvm_vcpu, preempt_notifier);
3641}
3642
3643static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
3644{
3645 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3646
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003647 kvm_x86_ops->vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003648}
3649
3650static void kvm_sched_out(struct preempt_notifier *pn,
3651 struct task_struct *next)
3652{
3653 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3654
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003655 kvm_x86_ops->vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003656}
3657
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003658int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10003659 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003660{
3661 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003662 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003663
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003664 if (kvm_x86_ops) {
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08003665 printk(KERN_ERR "kvm: already loaded the other module\n");
3666 return -EEXIST;
3667 }
3668
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003669 if (!ops->cpu_has_kvm_support()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003670 printk(KERN_ERR "kvm: no hardware support\n");
3671 return -EOPNOTSUPP;
3672 }
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003673 if (ops->disabled_by_bios()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003674 printk(KERN_ERR "kvm: disabled by bios\n");
3675 return -EOPNOTSUPP;
3676 }
3677
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003678 kvm_x86_ops = ops;
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003679
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003680 r = kvm_x86_ops->hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003681 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02003682 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003683
Yang, Sheng002c7f72007-07-31 14:23:01 +03003684 for_each_online_cpu(cpu) {
3685 smp_call_function_single(cpu,
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003686 kvm_x86_ops->check_processor_compatibility,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003687 &r, 0, 1);
3688 if (r < 0)
3689 goto out_free_0;
3690 }
3691
Avi Kivity1b6c0162007-05-24 13:03:52 +03003692 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003693 r = register_cpu_notifier(&kvm_cpu_notifier);
3694 if (r)
3695 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003696 register_reboot_notifier(&kvm_reboot_notifier);
3697
Avi Kivity59ae6c62007-02-12 00:54:48 -08003698 r = sysdev_class_register(&kvm_sysdev_class);
3699 if (r)
3700 goto out_free_2;
3701
3702 r = sysdev_register(&kvm_sysdev);
3703 if (r)
3704 goto out_free_3;
3705
Rusty Russellc16f8622007-07-30 21:12:19 +10003706 /* A kmem cache lets us meet the alignment requirements of fx_save. */
3707 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
3708 __alignof__(struct kvm_vcpu), 0, 0);
3709 if (!kvm_vcpu_cache) {
3710 r = -ENOMEM;
3711 goto out_free_4;
3712 }
3713
Avi Kivity6aa8b732006-12-10 02:21:36 -08003714 kvm_chardev_ops.owner = module;
3715
3716 r = misc_register(&kvm_dev);
3717 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04003718 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08003719 goto out_free;
3720 }
3721
Avi Kivity15ad7142007-07-11 18:17:21 +03003722 kvm_preempt_ops.sched_in = kvm_sched_in;
3723 kvm_preempt_ops.sched_out = kvm_sched_out;
3724
Avi Kivityc7addb92007-09-16 18:58:32 +02003725 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
3726
3727 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003728
3729out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10003730 kmem_cache_destroy(kvm_vcpu_cache);
3731out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08003732 sysdev_unregister(&kvm_sysdev);
3733out_free_3:
3734 sysdev_class_unregister(&kvm_sysdev_class);
3735out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003736 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08003737 unregister_cpu_notifier(&kvm_cpu_notifier);
3738out_free_1:
Avi Kivity1b6c0162007-05-24 13:03:52 +03003739 on_each_cpu(hardware_disable, NULL, 0, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03003740out_free_0:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003741 kvm_x86_ops->hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02003742out:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003743 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003744 return r;
3745}
Mike Dayd77c26f2007-10-08 09:02:08 -04003746EXPORT_SYMBOL_GPL(kvm_init_x86);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003747
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003748void kvm_exit_x86(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003749{
3750 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10003751 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003752 sysdev_unregister(&kvm_sysdev);
3753 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003754 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003755 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003756 on_each_cpu(hardware_disable, NULL, 0, 1);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003757 kvm_x86_ops->hardware_unsetup();
3758 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003759}
Mike Dayd77c26f2007-10-08 09:02:08 -04003760EXPORT_SYMBOL_GPL(kvm_exit_x86);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003761
3762static __init int kvm_init(void)
3763{
3764 static struct page *bad_page;
Avi Kivity37e29d92007-02-20 14:07:37 +02003765 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003766
Avi Kivityb5a33a72007-04-15 16:31:09 +03003767 r = kvm_mmu_module_init();
3768 if (r)
3769 goto out4;
3770
Avi Kivity6aa8b732006-12-10 02:21:36 -08003771 kvm_init_debug();
3772
Michael Riepebf591b22006-12-22 01:05:36 -08003773 kvm_init_msr_list();
3774
Mike Dayd77c26f2007-10-08 09:02:08 -04003775 bad_page = alloc_page(GFP_KERNEL);
3776
3777 if (bad_page == NULL) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003778 r = -ENOMEM;
3779 goto out;
3780 }
3781
3782 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
3783 memset(__va(bad_page_address), 0, PAGE_SIZE);
3784
Avi Kivity58e690e2007-02-26 16:29:43 +02003785 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003786
3787out:
3788 kvm_exit_debug();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003789 kvm_mmu_module_exit();
3790out4:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003791 return r;
3792}
3793
3794static __exit void kvm_exit(void)
3795{
3796 kvm_exit_debug();
3797 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
Avi Kivityb5a33a72007-04-15 16:31:09 +03003798 kvm_mmu_module_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003799}
3800
3801module_init(kvm_init)
3802module_exit(kvm_exit)